isl: Add helpers for creating different types of aux surfaces
[mesa.git] / src / intel / isl / isl.c
1 /*
2 * Copyright 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "isl.h"
29 #include "isl_gen4.h"
30 #include "isl_gen6.h"
31 #include "isl_gen7.h"
32 #include "isl_gen8.h"
33 #include "isl_gen9.h"
34 #include "isl_priv.h"
35
36 void PRINTFLIKE(3, 4) UNUSED
37 __isl_finishme(const char *file, int line, const char *fmt, ...)
38 {
39 va_list ap;
40 char buf[512];
41
42 va_start(ap, fmt);
43 vsnprintf(buf, sizeof(buf), fmt, ap);
44 va_end(ap);
45
46 fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buf);
47 }
48
49 void
50 isl_device_init(struct isl_device *dev,
51 const struct brw_device_info *info,
52 bool has_bit6_swizzling)
53 {
54 dev->info = info;
55 dev->use_separate_stencil = ISL_DEV_GEN(dev) >= 6;
56 dev->has_bit6_swizzling = has_bit6_swizzling;
57
58 /* The ISL_DEV macros may be defined in the CFLAGS, thus hardcoding some
59 * device properties at buildtime. Verify that the macros with the device
60 * properties chosen during runtime.
61 */
62 ISL_DEV_GEN_SANITIZE(dev);
63 ISL_DEV_USE_SEPARATE_STENCIL_SANITIZE(dev);
64
65 /* Did we break hiz or stencil? */
66 if (ISL_DEV_USE_SEPARATE_STENCIL(dev))
67 assert(info->has_hiz_and_separate_stencil);
68 if (info->must_use_separate_stencil)
69 assert(ISL_DEV_USE_SEPARATE_STENCIL(dev));
70 }
71
72 /**
73 * @brief Query the set of multisamples supported by the device.
74 *
75 * This function always returns non-zero, as ISL_SAMPLE_COUNT_1_BIT is always
76 * supported.
77 */
78 isl_sample_count_mask_t ATTRIBUTE_CONST
79 isl_device_get_sample_counts(struct isl_device *dev)
80 {
81 if (ISL_DEV_GEN(dev) >= 9) {
82 return ISL_SAMPLE_COUNT_1_BIT |
83 ISL_SAMPLE_COUNT_2_BIT |
84 ISL_SAMPLE_COUNT_4_BIT |
85 ISL_SAMPLE_COUNT_8_BIT |
86 ISL_SAMPLE_COUNT_16_BIT;
87 } else if (ISL_DEV_GEN(dev) >= 8) {
88 return ISL_SAMPLE_COUNT_1_BIT |
89 ISL_SAMPLE_COUNT_2_BIT |
90 ISL_SAMPLE_COUNT_4_BIT |
91 ISL_SAMPLE_COUNT_8_BIT;
92 } else if (ISL_DEV_GEN(dev) >= 7) {
93 return ISL_SAMPLE_COUNT_1_BIT |
94 ISL_SAMPLE_COUNT_4_BIT |
95 ISL_SAMPLE_COUNT_8_BIT;
96 } else if (ISL_DEV_GEN(dev) >= 6) {
97 return ISL_SAMPLE_COUNT_1_BIT |
98 ISL_SAMPLE_COUNT_4_BIT;
99 } else {
100 return ISL_SAMPLE_COUNT_1_BIT;
101 }
102 }
103
104 /**
105 * @param[out] info is written only on success
106 */
107 bool
108 isl_tiling_get_info(const struct isl_device *dev,
109 enum isl_tiling tiling,
110 uint32_t format_bpb,
111 struct isl_tile_info *tile_info)
112 {
113 const uint32_t bs = format_bpb / 8;
114 struct isl_extent2d logical_el, phys_B;
115
116 assert(tiling == ISL_TILING_LINEAR || isl_is_pow2(format_bpb));
117
118 switch (tiling) {
119 case ISL_TILING_LINEAR:
120 assert(bs > 0);
121 logical_el = isl_extent2d(1, 1);
122 phys_B = isl_extent2d(bs, 1);
123 break;
124
125 case ISL_TILING_X:
126 assert(bs > 0);
127 logical_el = isl_extent2d(512 / bs, 8);
128 phys_B = isl_extent2d(512, 8);
129 break;
130
131 case ISL_TILING_Y0:
132 assert(bs > 0);
133 logical_el = isl_extent2d(128 / bs, 32);
134 phys_B = isl_extent2d(128, 32);
135 break;
136
137 case ISL_TILING_W:
138 assert(bs == 1);
139 logical_el = isl_extent2d(64, 64);
140 /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfacePitch:
141 *
142 * "If the surface is a stencil buffer (and thus has Tile Mode set
143 * to TILEMODE_WMAJOR), the pitch must be set to 2x the value
144 * computed based on width, as the stencil buffer is stored with two
145 * rows interleaved."
146 *
147 * This, together with the fact that stencil buffers are referred to as
148 * being Y-tiled in the PRMs for older hardware implies that the
149 * physical size of a W-tile is actually the same as for a Y-tile.
150 */
151 phys_B = isl_extent2d(128, 32);
152 break;
153
154 case ISL_TILING_Yf:
155 case ISL_TILING_Ys: {
156 if (ISL_DEV_GEN(dev) < 9)
157 return false;
158
159 if (!isl_is_pow2(bs))
160 return false;
161
162 bool is_Ys = tiling == ISL_TILING_Ys;
163
164 assert(bs > 0);
165 unsigned width = 1 << (6 + (ffs(bs) / 2) + (2 * is_Ys));
166 unsigned height = 1 << (6 - (ffs(bs) / 2) + (2 * is_Ys));
167
168 logical_el = isl_extent2d(width / bs, height);
169 phys_B = isl_extent2d(width, height);
170 break;
171 }
172
173 case ISL_TILING_HIZ:
174 /* HiZ buffers are required to have ISL_FORMAT_HIZ which is an 8x4
175 * 128bpb format. The tiling has the same physical dimensions as
176 * Y-tiling but actually has two HiZ columns per Y-tiled column.
177 */
178 assert(bs == 16);
179 logical_el = isl_extent2d(16, 16);
180 phys_B = isl_extent2d(128, 32);
181 break;
182
183 case ISL_TILING_CCS:
184 /* CCS surfaces are required to have one of the GENX_CCS_* formats which
185 * have a block size of 1 or 2 bits per block and each CCS element
186 * corresponds to one cache-line pair in the main surface. From the Sky
187 * Lake PRM Vol. 12 in the section on planes:
188 *
189 * "The Color Control Surface (CCS) contains the compression status
190 * of the cache-line pairs. The compression state of the cache-line
191 * pair is specified by 2 bits in the CCS. Each CCS cache-line
192 * represents an area on the main surface of 16x16 sets of 128 byte
193 * Y-tiled cache-line-pairs. CCS is always Y tiled."
194 *
195 * The CCS being Y-tiled implies that it's an 8x8 grid of cache-lines.
196 * Since each cache line corresponds to a 16x16 set of cache-line pairs,
197 * that yields total tile area of 128x128 cache-line pairs or CCS
198 * elements. On older hardware, each CCS element is 1 bit and the tile
199 * is 128x256 elements.
200 */
201 assert(format_bpb == 1 || format_bpb == 2);
202 logical_el = isl_extent2d(128, 256 / format_bpb);
203 phys_B = isl_extent2d(128, 32);
204 break;
205
206 default:
207 unreachable("not reached");
208 } /* end switch */
209
210 *tile_info = (struct isl_tile_info) {
211 .tiling = tiling,
212 .logical_extent_el = logical_el,
213 .phys_extent_B = phys_B,
214 };
215
216 return true;
217 }
218
219 /**
220 * @param[out] tiling is set only on success
221 */
222 bool
223 isl_surf_choose_tiling(const struct isl_device *dev,
224 const struct isl_surf_init_info *restrict info,
225 enum isl_tiling *tiling)
226 {
227 isl_tiling_flags_t tiling_flags = info->tiling_flags;
228
229 if (ISL_DEV_GEN(dev) >= 7) {
230 gen7_filter_tiling(dev, info, &tiling_flags);
231 } else {
232 isl_finishme("%s: gen%u", __func__, ISL_DEV_GEN(dev));
233 gen7_filter_tiling(dev, info, &tiling_flags);
234 }
235
236 #define CHOOSE(__tiling) \
237 do { \
238 if (tiling_flags & (1u << (__tiling))) { \
239 *tiling = (__tiling); \
240 return true; \
241 } \
242 } while (0)
243
244 /* Of the tiling modes remaining, choose the one that offers the best
245 * performance.
246 */
247
248 if (info->dim == ISL_SURF_DIM_1D) {
249 /* Prefer linear for 1D surfaces because they do not benefit from
250 * tiling. To the contrary, tiling leads to wasted memory and poor
251 * memory locality due to the swizzling and alignment restrictions
252 * required in tiled surfaces.
253 */
254 CHOOSE(ISL_TILING_LINEAR);
255 }
256
257 CHOOSE(ISL_TILING_CCS);
258 CHOOSE(ISL_TILING_HIZ);
259 CHOOSE(ISL_TILING_Ys);
260 CHOOSE(ISL_TILING_Yf);
261 CHOOSE(ISL_TILING_Y0);
262 CHOOSE(ISL_TILING_X);
263 CHOOSE(ISL_TILING_W);
264 CHOOSE(ISL_TILING_LINEAR);
265
266 #undef CHOOSE
267
268 /* No tiling mode accomodates the inputs. */
269 return false;
270 }
271
272 static bool
273 isl_choose_msaa_layout(const struct isl_device *dev,
274 const struct isl_surf_init_info *info,
275 enum isl_tiling tiling,
276 enum isl_msaa_layout *msaa_layout)
277 {
278 if (ISL_DEV_GEN(dev) >= 8) {
279 return gen8_choose_msaa_layout(dev, info, tiling, msaa_layout);
280 } else if (ISL_DEV_GEN(dev) >= 7) {
281 return gen7_choose_msaa_layout(dev, info, tiling, msaa_layout);
282 } else if (ISL_DEV_GEN(dev) >= 6) {
283 return gen6_choose_msaa_layout(dev, info, tiling, msaa_layout);
284 } else {
285 return gen4_choose_msaa_layout(dev, info, tiling, msaa_layout);
286 }
287 }
288
289 static void
290 isl_msaa_interleaved_scale_px_to_sa(uint32_t samples,
291 uint32_t *width, uint32_t *height)
292 {
293 assert(isl_is_pow2(samples));
294
295 /* From the Broadwell PRM >> Volume 5: Memory Views >> Computing Mip Level
296 * Sizes (p133):
297 *
298 * If the surface is multisampled and it is a depth or stencil surface
299 * or Multisampled Surface StorageFormat in SURFACE_STATE is
300 * MSFMT_DEPTH_STENCIL, W_L and H_L must be adjusted as follows before
301 * proceeding: [...]
302 */
303 if (width)
304 *width = isl_align(*width, 2) << ((ffs(samples) - 0) / 2);
305 if (height)
306 *height = isl_align(*height, 2) << ((ffs(samples) - 1) / 2);
307 }
308
309 static enum isl_array_pitch_span
310 isl_choose_array_pitch_span(const struct isl_device *dev,
311 const struct isl_surf_init_info *restrict info,
312 enum isl_dim_layout dim_layout,
313 const struct isl_extent4d *phys_level0_sa)
314 {
315 switch (dim_layout) {
316 case ISL_DIM_LAYOUT_GEN9_1D:
317 case ISL_DIM_LAYOUT_GEN4_2D:
318 if (ISL_DEV_GEN(dev) >= 8) {
319 /* QPitch becomes programmable in Broadwell. So choose the
320 * most compact QPitch possible in order to conserve memory.
321 *
322 * From the Broadwell PRM >> Volume 2d: Command Reference: Structures
323 * >> RENDER_SURFACE_STATE Surface QPitch (p325):
324 *
325 * - Software must ensure that this field is set to a value
326 * sufficiently large such that the array slices in the surface
327 * do not overlap. Refer to the Memory Data Formats section for
328 * information on how surfaces are stored in memory.
329 *
330 * - This field specifies the distance in rows between array
331 * slices. It is used only in the following cases:
332 *
333 * - Surface Array is enabled OR
334 * - Number of Mulitsamples is not NUMSAMPLES_1 and
335 * Multisampled Surface Storage Format set to MSFMT_MSS OR
336 * - Surface Type is SURFTYPE_CUBE
337 */
338 return ISL_ARRAY_PITCH_SPAN_COMPACT;
339 } else if (ISL_DEV_GEN(dev) >= 7) {
340 /* Note that Ivybridge introduces
341 * RENDER_SURFACE_STATE.SurfaceArraySpacing, which provides the
342 * driver more control over the QPitch.
343 */
344
345 if (phys_level0_sa->array_len == 1) {
346 /* The hardware will never use the QPitch. So choose the most
347 * compact QPitch possible in order to conserve memory.
348 */
349 return ISL_ARRAY_PITCH_SPAN_COMPACT;
350 }
351
352 if (isl_surf_usage_is_depth_or_stencil(info->usage) ||
353 (info->usage & ISL_SURF_USAGE_HIZ_BIT)) {
354 /* From the Ivybridge PRM >> Volume 1 Part 1: Graphics Core >>
355 * Section 6.18.4.7: Surface Arrays (p112):
356 *
357 * If Surface Array Spacing is set to ARYSPC_FULL (note that
358 * the depth buffer and stencil buffer have an implied value of
359 * ARYSPC_FULL):
360 */
361 return ISL_ARRAY_PITCH_SPAN_FULL;
362 }
363
364 if (info->levels == 1) {
365 /* We are able to set RENDER_SURFACE_STATE.SurfaceArraySpacing
366 * to ARYSPC_LOD0.
367 */
368 return ISL_ARRAY_PITCH_SPAN_COMPACT;
369 }
370
371 return ISL_ARRAY_PITCH_SPAN_FULL;
372 } else if ((ISL_DEV_GEN(dev) == 5 || ISL_DEV_GEN(dev) == 6) &&
373 ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
374 isl_surf_usage_is_stencil(info->usage)) {
375 /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
376 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
377 *
378 * The separate stencil buffer does not support mip mapping, thus
379 * the storage for LODs other than LOD 0 is not needed.
380 */
381 assert(info->levels == 1);
382 assert(phys_level0_sa->array_len == 1);
383 return ISL_ARRAY_PITCH_SPAN_COMPACT;
384 } else {
385 if ((ISL_DEV_GEN(dev) == 5 || ISL_DEV_GEN(dev) == 6) &&
386 ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
387 isl_surf_usage_is_stencil(info->usage)) {
388 /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
389 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
390 *
391 * The separate stencil buffer does not support mip mapping,
392 * thus the storage for LODs other than LOD 0 is not needed.
393 */
394 assert(info->levels == 1);
395 assert(phys_level0_sa->array_len == 1);
396 return ISL_ARRAY_PITCH_SPAN_COMPACT;
397 }
398
399 if (phys_level0_sa->array_len == 1) {
400 /* The hardware will never use the QPitch. So choose the most
401 * compact QPitch possible in order to conserve memory.
402 */
403 return ISL_ARRAY_PITCH_SPAN_COMPACT;
404 }
405
406 return ISL_ARRAY_PITCH_SPAN_FULL;
407 }
408
409 case ISL_DIM_LAYOUT_GEN4_3D:
410 /* The hardware will never use the QPitch. So choose the most
411 * compact QPitch possible in order to conserve memory.
412 */
413 return ISL_ARRAY_PITCH_SPAN_COMPACT;
414 }
415
416 unreachable("bad isl_dim_layout");
417 return ISL_ARRAY_PITCH_SPAN_FULL;
418 }
419
420 static void
421 isl_choose_image_alignment_el(const struct isl_device *dev,
422 const struct isl_surf_init_info *restrict info,
423 enum isl_tiling tiling,
424 enum isl_msaa_layout msaa_layout,
425 struct isl_extent3d *image_align_el)
426 {
427 if (info->format == ISL_FORMAT_HIZ) {
428 assert(ISL_DEV_GEN(dev) >= 6);
429 /* HiZ surfaces are always aligned to 16x8 pixels in the primary surface
430 * which works out to 2x2 HiZ elments.
431 */
432 *image_align_el = isl_extent3d(2, 2, 1);
433 return;
434 }
435
436 if (ISL_DEV_GEN(dev) >= 9) {
437 gen9_choose_image_alignment_el(dev, info, tiling, msaa_layout,
438 image_align_el);
439 } else if (ISL_DEV_GEN(dev) >= 8) {
440 gen8_choose_image_alignment_el(dev, info, tiling, msaa_layout,
441 image_align_el);
442 } else if (ISL_DEV_GEN(dev) >= 7) {
443 gen7_choose_image_alignment_el(dev, info, tiling, msaa_layout,
444 image_align_el);
445 } else if (ISL_DEV_GEN(dev) >= 6) {
446 gen6_choose_image_alignment_el(dev, info, tiling, msaa_layout,
447 image_align_el);
448 } else {
449 gen4_choose_image_alignment_el(dev, info, tiling, msaa_layout,
450 image_align_el);
451 }
452 }
453
454 static enum isl_dim_layout
455 isl_surf_choose_dim_layout(const struct isl_device *dev,
456 enum isl_surf_dim logical_dim)
457 {
458 if (ISL_DEV_GEN(dev) >= 9) {
459 switch (logical_dim) {
460 case ISL_SURF_DIM_1D:
461 return ISL_DIM_LAYOUT_GEN9_1D;
462 case ISL_SURF_DIM_2D:
463 case ISL_SURF_DIM_3D:
464 return ISL_DIM_LAYOUT_GEN4_2D;
465 }
466 } else {
467 switch (logical_dim) {
468 case ISL_SURF_DIM_1D:
469 case ISL_SURF_DIM_2D:
470 return ISL_DIM_LAYOUT_GEN4_2D;
471 case ISL_SURF_DIM_3D:
472 return ISL_DIM_LAYOUT_GEN4_3D;
473 }
474 }
475
476 unreachable("bad isl_surf_dim");
477 return ISL_DIM_LAYOUT_GEN4_2D;
478 }
479
480 /**
481 * Calculate the physical extent of the surface's first level, in units of
482 * surface samples. The result is aligned to the format's compression block.
483 */
484 static void
485 isl_calc_phys_level0_extent_sa(const struct isl_device *dev,
486 const struct isl_surf_init_info *restrict info,
487 enum isl_dim_layout dim_layout,
488 enum isl_tiling tiling,
489 enum isl_msaa_layout msaa_layout,
490 struct isl_extent4d *phys_level0_sa)
491 {
492 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
493
494 if (isl_format_is_yuv(info->format))
495 isl_finishme("%s:%s: YUV format", __FILE__, __func__);
496
497 switch (info->dim) {
498 case ISL_SURF_DIM_1D:
499 assert(info->height == 1);
500 assert(info->depth == 1);
501 assert(info->samples == 1);
502 assert(!isl_format_is_compressed(info->format));
503
504 switch (dim_layout) {
505 case ISL_DIM_LAYOUT_GEN4_3D:
506 unreachable("bad isl_dim_layout");
507
508 case ISL_DIM_LAYOUT_GEN9_1D:
509 case ISL_DIM_LAYOUT_GEN4_2D:
510 *phys_level0_sa = (struct isl_extent4d) {
511 .w = info->width,
512 .h = 1,
513 .d = 1,
514 .a = info->array_len,
515 };
516 break;
517 }
518 break;
519
520 case ISL_SURF_DIM_2D:
521 assert(dim_layout == ISL_DIM_LAYOUT_GEN4_2D);
522
523 if (tiling == ISL_TILING_Ys && info->samples > 1)
524 isl_finishme("%s:%s: multisample TileYs layout", __FILE__, __func__);
525
526 switch (msaa_layout) {
527 case ISL_MSAA_LAYOUT_NONE:
528 assert(info->depth == 1);
529 assert(info->samples == 1);
530
531 *phys_level0_sa = (struct isl_extent4d) {
532 .w = isl_align_npot(info->width, fmtl->bw),
533 .h = isl_align_npot(info->height, fmtl->bh),
534 .d = 1,
535 .a = info->array_len,
536 };
537 break;
538
539 case ISL_MSAA_LAYOUT_ARRAY:
540 assert(info->depth == 1);
541 assert(info->array_len == 1);
542 assert(!isl_format_is_compressed(info->format));
543
544 *phys_level0_sa = (struct isl_extent4d) {
545 .w = info->width,
546 .h = info->height,
547 .d = 1,
548 .a = info->samples,
549 };
550 break;
551
552 case ISL_MSAA_LAYOUT_INTERLEAVED:
553 assert(info->depth == 1);
554 assert(info->array_len == 1);
555 assert(!isl_format_is_compressed(info->format));
556
557 *phys_level0_sa = (struct isl_extent4d) {
558 .w = info->width,
559 .h = info->height,
560 .d = 1,
561 .a = 1,
562 };
563
564 isl_msaa_interleaved_scale_px_to_sa(info->samples,
565 &phys_level0_sa->w,
566 &phys_level0_sa->h);
567 break;
568 }
569 break;
570
571 case ISL_SURF_DIM_3D:
572 assert(info->array_len == 1);
573 assert(info->samples == 1);
574
575 if (fmtl->bd > 1) {
576 isl_finishme("%s:%s: compression block with depth > 1",
577 __FILE__, __func__);
578 }
579
580 switch (dim_layout) {
581 case ISL_DIM_LAYOUT_GEN9_1D:
582 unreachable("bad isl_dim_layout");
583
584 case ISL_DIM_LAYOUT_GEN4_2D:
585 assert(ISL_DEV_GEN(dev) >= 9);
586
587 *phys_level0_sa = (struct isl_extent4d) {
588 .w = isl_align_npot(info->width, fmtl->bw),
589 .h = isl_align_npot(info->height, fmtl->bh),
590 .d = 1,
591 .a = info->depth,
592 };
593 break;
594
595 case ISL_DIM_LAYOUT_GEN4_3D:
596 assert(ISL_DEV_GEN(dev) < 9);
597 *phys_level0_sa = (struct isl_extent4d) {
598 .w = isl_align(info->width, fmtl->bw),
599 .h = isl_align(info->height, fmtl->bh),
600 .d = info->depth,
601 .a = 1,
602 };
603 break;
604 }
605 break;
606 }
607 }
608
609 /**
610 * A variant of isl_calc_phys_slice0_extent_sa() specific to
611 * ISL_DIM_LAYOUT_GEN4_2D.
612 */
613 static void
614 isl_calc_phys_slice0_extent_sa_gen4_2d(
615 const struct isl_device *dev,
616 const struct isl_surf_init_info *restrict info,
617 enum isl_msaa_layout msaa_layout,
618 const struct isl_extent3d *image_align_sa,
619 const struct isl_extent4d *phys_level0_sa,
620 struct isl_extent2d *phys_slice0_sa)
621 {
622 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
623
624 assert(phys_level0_sa->depth == 1);
625
626 if (info->levels == 1) {
627 /* Do not pad the surface to the image alignment. Instead, pad it only
628 * to the pixel format's block alignment.
629 *
630 * For tiled surfaces, using a reduced alignment here avoids wasting CPU
631 * cycles on the below mipmap layout caluclations. Reducing the
632 * alignment here is safe because we later align the row pitch and array
633 * pitch to the tile boundary. It is safe even for
634 * ISL_MSAA_LAYOUT_INTERLEAVED, because phys_level0_sa is already scaled
635 * to accomodate the interleaved samples.
636 *
637 * For linear surfaces, reducing the alignment here permits us to later
638 * choose an arbitrary, non-aligned row pitch. If the surface backs
639 * a VkBuffer, then an arbitrary pitch may be needed to accomodate
640 * VkBufferImageCopy::bufferRowLength.
641 */
642 *phys_slice0_sa = (struct isl_extent2d) {
643 .w = isl_align_npot(phys_level0_sa->w, fmtl->bw),
644 .h = isl_align_npot(phys_level0_sa->h, fmtl->bh),
645 };
646 return;
647 }
648
649 uint32_t slice_top_w = 0;
650 uint32_t slice_bottom_w = 0;
651 uint32_t slice_left_h = 0;
652 uint32_t slice_right_h = 0;
653
654 uint32_t W0 = phys_level0_sa->w;
655 uint32_t H0 = phys_level0_sa->h;
656
657 for (uint32_t l = 0; l < info->levels; ++l) {
658 uint32_t W = isl_minify(W0, l);
659 uint32_t H = isl_minify(H0, l);
660
661 uint32_t w = isl_align_npot(W, image_align_sa->w);
662 uint32_t h = isl_align_npot(H, image_align_sa->h);
663
664 if (l == 0) {
665 slice_top_w = w;
666 slice_left_h = h;
667 slice_right_h = h;
668 } else if (l == 1) {
669 slice_bottom_w = w;
670 slice_left_h += h;
671 } else if (l == 2) {
672 slice_bottom_w += w;
673 slice_right_h += h;
674 } else {
675 slice_right_h += h;
676 }
677 }
678
679 *phys_slice0_sa = (struct isl_extent2d) {
680 .w = MAX(slice_top_w, slice_bottom_w),
681 .h = MAX(slice_left_h, slice_right_h),
682 };
683 }
684
685 /**
686 * A variant of isl_calc_phys_slice0_extent_sa() specific to
687 * ISL_DIM_LAYOUT_GEN4_3D.
688 */
689 static void
690 isl_calc_phys_slice0_extent_sa_gen4_3d(
691 const struct isl_device *dev,
692 const struct isl_surf_init_info *restrict info,
693 const struct isl_extent3d *image_align_sa,
694 const struct isl_extent4d *phys_level0_sa,
695 struct isl_extent2d *phys_slice0_sa)
696 {
697 assert(info->samples == 1);
698 assert(phys_level0_sa->array_len == 1);
699
700 uint32_t slice_w = 0;
701 uint32_t slice_h = 0;
702
703 uint32_t W0 = phys_level0_sa->w;
704 uint32_t H0 = phys_level0_sa->h;
705 uint32_t D0 = phys_level0_sa->d;
706
707 for (uint32_t l = 0; l < info->levels; ++l) {
708 uint32_t level_w = isl_align_npot(isl_minify(W0, l), image_align_sa->w);
709 uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa->h);
710 uint32_t level_d = isl_align_npot(isl_minify(D0, l), image_align_sa->d);
711
712 uint32_t max_layers_horiz = MIN(level_d, 1u << l);
713 uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
714
715 slice_w = MAX(slice_w, level_w * max_layers_horiz);
716 slice_h += level_h * max_layers_vert;
717 }
718
719 *phys_slice0_sa = (struct isl_extent2d) {
720 .w = slice_w,
721 .h = slice_h,
722 };
723 }
724
725 /**
726 * A variant of isl_calc_phys_slice0_extent_sa() specific to
727 * ISL_DIM_LAYOUT_GEN9_1D.
728 */
729 static void
730 isl_calc_phys_slice0_extent_sa_gen9_1d(
731 const struct isl_device *dev,
732 const struct isl_surf_init_info *restrict info,
733 const struct isl_extent3d *image_align_sa,
734 const struct isl_extent4d *phys_level0_sa,
735 struct isl_extent2d *phys_slice0_sa)
736 {
737 MAYBE_UNUSED const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
738
739 assert(phys_level0_sa->height == 1);
740 assert(phys_level0_sa->depth == 1);
741 assert(info->samples == 1);
742 assert(image_align_sa->w >= fmtl->bw);
743
744 uint32_t slice_w = 0;
745 const uint32_t W0 = phys_level0_sa->w;
746
747 for (uint32_t l = 0; l < info->levels; ++l) {
748 uint32_t W = isl_minify(W0, l);
749 uint32_t w = isl_align_npot(W, image_align_sa->w);
750
751 slice_w += w;
752 }
753
754 *phys_slice0_sa = isl_extent2d(slice_w, 1);
755 }
756
757 /**
758 * Calculate the physical extent of the surface's first array slice, in units
759 * of surface samples. If the surface is multi-leveled, then the result will
760 * be aligned to \a image_align_sa.
761 */
762 static void
763 isl_calc_phys_slice0_extent_sa(const struct isl_device *dev,
764 const struct isl_surf_init_info *restrict info,
765 enum isl_dim_layout dim_layout,
766 enum isl_msaa_layout msaa_layout,
767 const struct isl_extent3d *image_align_sa,
768 const struct isl_extent4d *phys_level0_sa,
769 struct isl_extent2d *phys_slice0_sa)
770 {
771 switch (dim_layout) {
772 case ISL_DIM_LAYOUT_GEN9_1D:
773 isl_calc_phys_slice0_extent_sa_gen9_1d(dev, info,
774 image_align_sa, phys_level0_sa,
775 phys_slice0_sa);
776 return;
777 case ISL_DIM_LAYOUT_GEN4_2D:
778 isl_calc_phys_slice0_extent_sa_gen4_2d(dev, info, msaa_layout,
779 image_align_sa, phys_level0_sa,
780 phys_slice0_sa);
781 return;
782 case ISL_DIM_LAYOUT_GEN4_3D:
783 isl_calc_phys_slice0_extent_sa_gen4_3d(dev, info, image_align_sa,
784 phys_level0_sa, phys_slice0_sa);
785 return;
786 }
787 }
788
789 /**
790 * Calculate the pitch between physical array slices, in units of rows of
791 * surface elements.
792 */
793 static uint32_t
794 isl_calc_array_pitch_el_rows(const struct isl_device *dev,
795 const struct isl_surf_init_info *restrict info,
796 const struct isl_tile_info *tile_info,
797 enum isl_dim_layout dim_layout,
798 enum isl_array_pitch_span array_pitch_span,
799 const struct isl_extent3d *image_align_sa,
800 const struct isl_extent4d *phys_level0_sa,
801 const struct isl_extent2d *phys_slice0_sa)
802 {
803 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
804 uint32_t pitch_sa_rows = 0;
805
806 switch (dim_layout) {
807 case ISL_DIM_LAYOUT_GEN9_1D:
808 /* Each row is an array slice */
809 pitch_sa_rows = 1;
810 break;
811 case ISL_DIM_LAYOUT_GEN4_2D:
812 switch (array_pitch_span) {
813 case ISL_ARRAY_PITCH_SPAN_COMPACT:
814 pitch_sa_rows = isl_align_npot(phys_slice0_sa->h, image_align_sa->h);
815 break;
816 case ISL_ARRAY_PITCH_SPAN_FULL: {
817 /* The QPitch equation is found in the Broadwell PRM >> Volume 5:
818 * Memory Views >> Common Surface Formats >> Surface Layout >> 2D
819 * Surfaces >> Surface Arrays.
820 */
821 uint32_t H0_sa = phys_level0_sa->h;
822 uint32_t H1_sa = isl_minify(H0_sa, 1);
823
824 uint32_t h0_sa = isl_align_npot(H0_sa, image_align_sa->h);
825 uint32_t h1_sa = isl_align_npot(H1_sa, image_align_sa->h);
826
827 uint32_t m;
828 if (ISL_DEV_GEN(dev) >= 7) {
829 /* The QPitch equation changed slightly in Ivybridge. */
830 m = 12;
831 } else {
832 m = 11;
833 }
834
835 pitch_sa_rows = h0_sa + h1_sa + (m * image_align_sa->h);
836
837 if (ISL_DEV_GEN(dev) == 6 && info->samples > 1 &&
838 (info->height % 4 == 1)) {
839 /* [SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
840 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
841 *
842 * [SNB] Errata: Sampler MSAA Qpitch will be 4 greater than
843 * the value calculated in the equation above , for every
844 * other odd Surface Height starting from 1 i.e. 1,5,9,13.
845 *
846 * XXX(chadv): Is the errata natural corollary of the physical
847 * layout of interleaved samples?
848 */
849 pitch_sa_rows += 4;
850 }
851
852 pitch_sa_rows = isl_align_npot(pitch_sa_rows, fmtl->bh);
853 } /* end case */
854 break;
855 }
856 break;
857 case ISL_DIM_LAYOUT_GEN4_3D:
858 assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
859 pitch_sa_rows = isl_align_npot(phys_slice0_sa->h, image_align_sa->h);
860 break;
861 default:
862 unreachable("bad isl_dim_layout");
863 break;
864 }
865
866 assert(pitch_sa_rows % fmtl->bh == 0);
867 uint32_t pitch_el_rows = pitch_sa_rows / fmtl->bh;
868
869 if (ISL_DEV_GEN(dev) >= 9 && fmtl->txc == ISL_TXC_CCS) {
870 /*
871 * From the Sky Lake PRM Vol 7, "MCS Buffer for Render Target(s)" (p. 632):
872 *
873 * "Mip-mapped and arrayed surfaces are supported with MCS buffer
874 * layout with these alignments in the RT space: Horizontal
875 * Alignment = 128 and Vertical Alignment = 64."
876 *
877 * From the Sky Lake PRM Vol. 2d, "RENDER_SURFACE_STATE" (p. 435):
878 *
879 * "For non-multisampled render target's CCS auxiliary surface,
880 * QPitch must be computed with Horizontal Alignment = 128 and
881 * Surface Vertical Alignment = 256. These alignments are only for
882 * CCS buffer and not for associated render target."
883 *
884 * The first restriction is already handled by isl_choose_image_alignment_el
885 * but the second restriction, which is an extension of the first, only
886 * applies to qpitch and must be applied here.
887 */
888 assert(fmtl->bh == 4);
889 pitch_el_rows = isl_align(pitch_el_rows, 256 / 4);
890 }
891
892 if (ISL_DEV_GEN(dev) >= 9 &&
893 info->dim == ISL_SURF_DIM_3D &&
894 tile_info->tiling != ISL_TILING_LINEAR) {
895 /* From the Skylake BSpec >> RENDER_SURFACE_STATE >> Surface QPitch:
896 *
897 * Tile Mode != Linear: This field must be set to an integer multiple
898 * of the tile height
899 */
900 pitch_el_rows = isl_align(pitch_el_rows, tile_info->logical_extent_el.height);
901 }
902
903 return pitch_el_rows;
904 }
905
906 /**
907 * Calculate the pitch of each surface row, in bytes.
908 */
909 static uint32_t
910 isl_calc_linear_row_pitch(const struct isl_device *dev,
911 const struct isl_surf_init_info *restrict info,
912 const struct isl_extent2d *phys_slice0_sa)
913 {
914 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
915
916 uint32_t row_pitch = info->min_pitch;
917
918 /* First, align the surface to a cache line boundary, as the PRM explains
919 * below.
920 *
921 * From the Broadwell PRM >> Volume 5: Memory Views >> Common Surface
922 * Formats >> Surface Padding Requirements >> Render Target and Media
923 * Surfaces:
924 *
925 * The data port accesses data (pixels) outside of the surface if they
926 * are contained in the same cache request as pixels that are within the
927 * surface. These pixels will not be returned by the requesting message,
928 * however if these pixels lie outside of defined pages in the GTT,
929 * a GTT error will result when the cache request is processed. In order
930 * to avoid these GTT errors, “padding” at the bottom of the surface is
931 * sometimes necessary.
932 *
933 * From the Broadwell PRM >> Volume 5: Memory Views >> Common Surface
934 * Formats >> Surface Padding Requirements >> Sampling Engine Surfaces:
935 *
936 * The sampling engine accesses texels outside of the surface if they
937 * are contained in the same cache line as texels that are within the
938 * surface. These texels will not participate in any calculation
939 * performed by the sampling engine and will not affect the result of
940 * any sampling engine operation, however if these texels lie outside of
941 * defined pages in the GTT, a GTT error will result when the cache line
942 * is accessed. In order to avoid these GTT errors, “padding” at the
943 * bottom and right side of a sampling engine surface is sometimes
944 * necessary.
945 *
946 * It is possible that a cache line will straddle a page boundary if the
947 * base address or pitch is not aligned. All pages included in the cache
948 * lines that are part of the surface must map to valid GTT entries to
949 * avoid errors. To determine the necessary padding on the bottom and
950 * right side of the surface, refer to the table in Alignment Unit Size
951 * section for the i and j parameters for the surface format in use. The
952 * surface must then be extended to the next multiple of the alignment
953 * unit size in each dimension, and all texels contained in this
954 * extended surface must have valid GTT entries.
955 *
956 * For example, suppose the surface size is 15 texels by 10 texels and
957 * the alignment parameters are i=4 and j=2. In this case, the extended
958 * surface would be 16 by 10. Note that these calculations are done in
959 * texels, and must be converted to bytes based on the surface format
960 * being used to determine whether additional pages need to be defined.
961 */
962 assert(phys_slice0_sa->w % fmtl->bw == 0);
963 const uint32_t bs = fmtl->bpb / 8;
964 row_pitch = MAX(row_pitch, bs * (phys_slice0_sa->w / fmtl->bw));
965
966 /* From the Broadwel PRM >> Volume 2d: Command Reference: Structures >>
967 * RENDER_SURFACE_STATE Surface Pitch (p349):
968 *
969 * - For linear render target surfaces and surfaces accessed with the
970 * typed data port messages, the pitch must be a multiple of the
971 * element size for non-YUV surface formats. Pitch must be
972 * a multiple of 2 * element size for YUV surface formats.
973 *
974 * - [Requirements for SURFTYPE_BUFFER and SURFTYPE_STRBUF, which we
975 * ignore because isl doesn't do buffers.]
976 *
977 * - For other linear surfaces, the pitch can be any multiple of
978 * bytes.
979 */
980 if (info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
981 if (isl_format_is_yuv(info->format)) {
982 row_pitch = isl_align_npot(row_pitch, 2 * bs);
983 } else {
984 row_pitch = isl_align_npot(row_pitch, bs);
985 }
986 }
987
988 return row_pitch;
989 }
990
991 /**
992 * Calculate and apply any padding required for the surface.
993 *
994 * @param[inout] total_h_el is updated with the new height
995 * @param[out] pad_bytes is overwritten with additional padding requirements.
996 */
997 static void
998 isl_apply_surface_padding(const struct isl_device *dev,
999 const struct isl_surf_init_info *restrict info,
1000 const struct isl_tile_info *tile_info,
1001 uint32_t *total_h_el,
1002 uint32_t *pad_bytes)
1003 {
1004 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1005
1006 *pad_bytes = 0;
1007
1008 /* From the Broadwell PRM >> Volume 5: Memory Views >> Common Surface
1009 * Formats >> Surface Padding Requirements >> Render Target and Media
1010 * Surfaces:
1011 *
1012 * The data port accesses data (pixels) outside of the surface if they
1013 * are contained in the same cache request as pixels that are within the
1014 * surface. These pixels will not be returned by the requesting message,
1015 * however if these pixels lie outside of defined pages in the GTT,
1016 * a GTT error will result when the cache request is processed. In
1017 * order to avoid these GTT errors, “padding” at the bottom of the
1018 * surface is sometimes necessary.
1019 *
1020 * From the Broadwell PRM >> Volume 5: Memory Views >> Common Surface
1021 * Formats >> Surface Padding Requirements >> Sampling Engine Surfaces:
1022 *
1023 * ... Lots of padding requirements, all listed separately below.
1024 */
1025
1026 /* We can safely ignore the first padding requirement, quoted below,
1027 * because isl doesn't do buffers.
1028 *
1029 * - [pre-BDW] For buffers, which have no inherent “height,” padding
1030 * requirements are different. A buffer must be padded to the next
1031 * multiple of 256 array elements, with an additional 16 bytes added
1032 * beyond that to account for the L1 cache line.
1033 */
1034
1035 /*
1036 * - For compressed textures [...], padding at the bottom of the surface
1037 * is to an even compressed row.
1038 */
1039 if (isl_format_is_compressed(info->format))
1040 *total_h_el = isl_align(*total_h_el, 2);
1041
1042 /*
1043 * - For cube surfaces, an additional two rows of padding are required
1044 * at the bottom of the surface.
1045 */
1046 if (info->usage & ISL_SURF_USAGE_CUBE_BIT)
1047 *total_h_el += 2;
1048
1049 /*
1050 * - For packed YUV, 96 bpt, 48 bpt, and 24 bpt surface formats,
1051 * additional padding is required. These surfaces require an extra row
1052 * plus 16 bytes of padding at the bottom in addition to the general
1053 * padding requirements.
1054 */
1055 if (isl_format_is_yuv(info->format) &&
1056 (fmtl->bpb == 96 || fmtl->bpb == 48|| fmtl->bpb == 24)) {
1057 *total_h_el += 1;
1058 *pad_bytes += 16;
1059 }
1060
1061 /*
1062 * - For linear surfaces, additional padding of 64 bytes is required at
1063 * the bottom of the surface. This is in addition to the padding
1064 * required above.
1065 */
1066 if (tile_info->tiling == ISL_TILING_LINEAR)
1067 *pad_bytes += 64;
1068
1069 /* The below text weakens, not strengthens, the padding requirements for
1070 * linear surfaces. Therefore we can safely ignore it.
1071 *
1072 * - [BDW+] For SURFTYPE_BUFFER, SURFTYPE_1D, and SURFTYPE_2D non-array,
1073 * non-MSAA, non-mip-mapped surfaces in linear memory, the only
1074 * padding requirement is to the next aligned 64-byte boundary beyond
1075 * the end of the surface. The rest of the padding requirements
1076 * documented above do not apply to these surfaces.
1077 */
1078
1079 /*
1080 * - [SKL+] For SURFTYPE_2D and SURFTYPE_3D with linear mode and
1081 * height % 4 != 0, the surface must be padded with
1082 * 4-(height % 4)*Surface Pitch # of bytes.
1083 */
1084 if (ISL_DEV_GEN(dev) >= 9 &&
1085 tile_info->tiling == ISL_TILING_LINEAR &&
1086 (info->dim == ISL_SURF_DIM_2D || info->dim == ISL_SURF_DIM_3D)) {
1087 *total_h_el = isl_align(*total_h_el, 4);
1088 }
1089
1090 /*
1091 * - [SKL+] For SURFTYPE_1D with linear mode, the surface must be padded
1092 * to 4 times the Surface Pitch # of bytes
1093 */
1094 if (ISL_DEV_GEN(dev) >= 9 &&
1095 tile_info->tiling == ISL_TILING_LINEAR &&
1096 info->dim == ISL_SURF_DIM_1D) {
1097 *total_h_el += 4;
1098 }
1099 }
1100
1101 bool
1102 isl_surf_init_s(const struct isl_device *dev,
1103 struct isl_surf *surf,
1104 const struct isl_surf_init_info *restrict info)
1105 {
1106 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1107
1108 const struct isl_extent4d logical_level0_px = {
1109 .w = info->width,
1110 .h = info->height,
1111 .d = info->depth,
1112 .a = info->array_len,
1113 };
1114
1115 enum isl_dim_layout dim_layout =
1116 isl_surf_choose_dim_layout(dev, info->dim);
1117
1118 enum isl_tiling tiling;
1119 if (!isl_surf_choose_tiling(dev, info, &tiling))
1120 return false;
1121
1122 struct isl_tile_info tile_info;
1123 if (!isl_tiling_get_info(dev, tiling, fmtl->bpb, &tile_info))
1124 return false;
1125
1126 enum isl_msaa_layout msaa_layout;
1127 if (!isl_choose_msaa_layout(dev, info, tiling, &msaa_layout))
1128 return false;
1129
1130 struct isl_extent3d image_align_el;
1131 isl_choose_image_alignment_el(dev, info, tiling, msaa_layout,
1132 &image_align_el);
1133
1134 struct isl_extent3d image_align_sa =
1135 isl_extent3d_el_to_sa(info->format, image_align_el);
1136
1137 struct isl_extent4d phys_level0_sa;
1138 isl_calc_phys_level0_extent_sa(dev, info, dim_layout, tiling, msaa_layout,
1139 &phys_level0_sa);
1140 assert(phys_level0_sa.w % fmtl->bw == 0);
1141 assert(phys_level0_sa.h % fmtl->bh == 0);
1142
1143 enum isl_array_pitch_span array_pitch_span =
1144 isl_choose_array_pitch_span(dev, info, dim_layout, &phys_level0_sa);
1145
1146 struct isl_extent2d phys_slice0_sa;
1147 isl_calc_phys_slice0_extent_sa(dev, info, dim_layout, msaa_layout,
1148 &image_align_sa, &phys_level0_sa,
1149 &phys_slice0_sa);
1150 assert(phys_slice0_sa.w % fmtl->bw == 0);
1151 assert(phys_slice0_sa.h % fmtl->bh == 0);
1152
1153 const uint32_t array_pitch_el_rows =
1154 isl_calc_array_pitch_el_rows(dev, info, &tile_info, dim_layout,
1155 array_pitch_span, &image_align_sa,
1156 &phys_level0_sa, &phys_slice0_sa);
1157
1158 uint32_t total_h_el = phys_level0_sa.array_len * array_pitch_el_rows;
1159
1160 uint32_t pad_bytes;
1161 isl_apply_surface_padding(dev, info, &tile_info, &total_h_el, &pad_bytes);
1162
1163 uint32_t row_pitch, size, base_alignment;
1164 if (tiling == ISL_TILING_LINEAR) {
1165 row_pitch = isl_calc_linear_row_pitch(dev, info, &phys_slice0_sa);
1166 size = row_pitch * total_h_el + pad_bytes;
1167
1168 /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfaceBaseAddress:
1169 *
1170 * "The Base Address for linear render target surfaces and surfaces
1171 * accessed with the typed surface read/write data port messages must
1172 * be element-size aligned, for non-YUV surface formats, or a
1173 * multiple of 2 element-sizes for YUV surface formats. Other linear
1174 * surfaces have no alignment requirements (byte alignment is
1175 * sufficient.)"
1176 */
1177 base_alignment = MAX(1, info->min_alignment);
1178 if (info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1179 if (isl_format_is_yuv(info->format)) {
1180 base_alignment = MAX(base_alignment, fmtl->bpb / 4);
1181 } else {
1182 base_alignment = MAX(base_alignment, fmtl->bpb / 8);
1183 }
1184 }
1185 } else {
1186 assert(phys_slice0_sa.w % fmtl->bw == 0);
1187 const uint32_t total_w_el = phys_slice0_sa.width / fmtl->bw;
1188 const uint32_t total_w_tl =
1189 isl_align_div(total_w_el, tile_info.logical_extent_el.width);
1190
1191 row_pitch = total_w_tl * tile_info.phys_extent_B.width;
1192 if (row_pitch < info->min_pitch) {
1193 row_pitch = isl_align(info->min_pitch, tile_info.phys_extent_B.width);
1194 }
1195
1196 total_h_el += isl_align_div_npot(pad_bytes, row_pitch);
1197 const uint32_t total_h_tl =
1198 isl_align_div(total_h_el, tile_info.logical_extent_el.height);
1199
1200 size = total_h_tl * tile_info.phys_extent_B.height * row_pitch;
1201
1202 const uint32_t tile_size = tile_info.phys_extent_B.width *
1203 tile_info.phys_extent_B.height;
1204 assert(isl_is_pow2(info->min_alignment) && isl_is_pow2(tile_size));
1205 base_alignment = MAX(info->min_alignment, tile_size);
1206 }
1207
1208 *surf = (struct isl_surf) {
1209 .dim = info->dim,
1210 .dim_layout = dim_layout,
1211 .msaa_layout = msaa_layout,
1212 .tiling = tiling,
1213 .format = info->format,
1214
1215 .levels = info->levels,
1216 .samples = info->samples,
1217
1218 .image_alignment_el = image_align_el,
1219 .logical_level0_px = logical_level0_px,
1220 .phys_level0_sa = phys_level0_sa,
1221
1222 .size = size,
1223 .alignment = base_alignment,
1224 .row_pitch = row_pitch,
1225 .array_pitch_el_rows = array_pitch_el_rows,
1226 .array_pitch_span = array_pitch_span,
1227
1228 .usage = info->usage,
1229 };
1230
1231 return true;
1232 }
1233
1234 void
1235 isl_surf_get_tile_info(const struct isl_device *dev,
1236 const struct isl_surf *surf,
1237 struct isl_tile_info *tile_info)
1238 {
1239 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1240 isl_tiling_get_info(dev, surf->tiling, fmtl->bpb, tile_info);
1241 }
1242
1243 void
1244 isl_surf_get_hiz_surf(const struct isl_device *dev,
1245 const struct isl_surf *surf,
1246 struct isl_surf *hiz_surf)
1247 {
1248 assert(ISL_DEV_GEN(dev) >= 5 && ISL_DEV_USE_SEPARATE_STENCIL(dev));
1249
1250 /* Multisampled depth is always interleaved */
1251 assert(surf->msaa_layout == ISL_MSAA_LAYOUT_NONE ||
1252 surf->msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1253
1254 isl_surf_init(dev, hiz_surf,
1255 .dim = ISL_SURF_DIM_2D,
1256 .format = ISL_FORMAT_HIZ,
1257 .width = surf->logical_level0_px.width,
1258 .height = surf->logical_level0_px.height,
1259 .depth = 1,
1260 .levels = surf->levels,
1261 .array_len = surf->logical_level0_px.array_len,
1262 /* On SKL+, HiZ is always single-sampled */
1263 .samples = ISL_DEV_GEN(dev) >= 9 ? 1 : surf->samples,
1264 .usage = ISL_SURF_USAGE_HIZ_BIT,
1265 .tiling_flags = ISL_TILING_HIZ_BIT);
1266 }
1267
1268 void
1269 isl_surf_get_mcs_surf(const struct isl_device *dev,
1270 const struct isl_surf *surf,
1271 struct isl_surf *mcs_surf)
1272 {
1273 /* It must be multisampled with an array layout */
1274 assert(surf->samples > 1 && surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
1275
1276 /* The following are true of all multisampled surfaces */
1277 assert(surf->dim == ISL_SURF_DIM_2D);
1278 assert(surf->levels == 1);
1279 assert(surf->logical_level0_px.depth == 1);
1280
1281 enum isl_format mcs_format;
1282 switch (surf->samples) {
1283 case 2: mcs_format = ISL_FORMAT_MCS_2X; break;
1284 case 4: mcs_format = ISL_FORMAT_MCS_4X; break;
1285 case 8: mcs_format = ISL_FORMAT_MCS_8X; break;
1286 case 16: mcs_format = ISL_FORMAT_MCS_16X; break;
1287 default:
1288 unreachable("Invalid sample count");
1289 }
1290
1291 isl_surf_init(dev, mcs_surf,
1292 .dim = ISL_SURF_DIM_2D,
1293 .format = mcs_format,
1294 .width = surf->logical_level0_px.width,
1295 .height = surf->logical_level0_px.height,
1296 .depth = 1,
1297 .levels = 1,
1298 .array_len = surf->logical_level0_px.array_len,
1299 .samples = 1, /* MCS surfaces are really single-sampled */
1300 .usage = ISL_SURF_USAGE_MCS_BIT,
1301 .tiling_flags = ISL_TILING_Y0_BIT);
1302 }
1303
1304 bool
1305 isl_surf_get_ccs_surf(const struct isl_device *dev,
1306 const struct isl_surf *surf,
1307 struct isl_surf *ccs_surf)
1308 {
1309 assert(surf->samples == 1 && surf->msaa_layout == ISL_MSAA_LAYOUT_NONE);
1310 assert(ISL_DEV_GEN(dev) >= 7);
1311
1312 assert(surf->dim == ISL_SURF_DIM_2D);
1313 assert(surf->logical_level0_px.depth == 1);
1314
1315 /* TODO: More conditions where it can fail. */
1316
1317 enum isl_format ccs_format;
1318 if (ISL_DEV_GEN(dev) >= 9) {
1319 if (!isl_tiling_is_any_y(surf->tiling))
1320 return false;
1321
1322 switch (isl_format_get_layout(surf->format)->bpb) {
1323 case 32: ccs_format = ISL_FORMAT_GEN9_CCS_32BPP; break;
1324 case 64: ccs_format = ISL_FORMAT_GEN9_CCS_64BPP; break;
1325 case 128: ccs_format = ISL_FORMAT_GEN9_CCS_128BPP; break;
1326 default:
1327 return false;
1328 }
1329 } else if (surf->tiling == ISL_TILING_Y0) {
1330 switch (isl_format_get_layout(surf->format)->bpb) {
1331 case 32: ccs_format = ISL_FORMAT_GEN7_CCS_32BPP_Y; break;
1332 case 64: ccs_format = ISL_FORMAT_GEN7_CCS_64BPP_Y; break;
1333 case 128: ccs_format = ISL_FORMAT_GEN7_CCS_128BPP_Y; break;
1334 default:
1335 return false;
1336 }
1337 } else if (surf->tiling == ISL_TILING_X) {
1338 switch (isl_format_get_layout(surf->format)->bpb) {
1339 case 32: ccs_format = ISL_FORMAT_GEN7_CCS_32BPP_X; break;
1340 case 64: ccs_format = ISL_FORMAT_GEN7_CCS_64BPP_X; break;
1341 case 128: ccs_format = ISL_FORMAT_GEN7_CCS_128BPP_X; break;
1342 default:
1343 return false;
1344 }
1345 } else {
1346 return false;
1347 }
1348
1349 isl_surf_init(dev, ccs_surf,
1350 .dim = ISL_SURF_DIM_2D,
1351 .format = ccs_format,
1352 .width = surf->logical_level0_px.width,
1353 .height = surf->logical_level0_px.height,
1354 .depth = 1,
1355 .levels = surf->levels,
1356 .array_len = surf->logical_level0_px.array_len,
1357 .samples = 1,
1358 .usage = ISL_SURF_USAGE_CCS_BIT,
1359 .tiling_flags = ISL_TILING_CCS_BIT);
1360
1361 return true;
1362 }
1363
1364 void
1365 isl_surf_fill_state_s(const struct isl_device *dev, void *state,
1366 const struct isl_surf_fill_state_info *restrict info)
1367 {
1368 #ifndef NDEBUG
1369 isl_surf_usage_flags_t _base_usage =
1370 info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
1371 ISL_SURF_USAGE_TEXTURE_BIT |
1372 ISL_SURF_USAGE_STORAGE_BIT);
1373 /* They may only specify one of the above bits at a time */
1374 assert(__builtin_popcount(_base_usage) == 1);
1375 /* The only other allowed bit is ISL_SURF_USAGE_CUBE_BIT */
1376 assert((info->view->usage & ~ISL_SURF_USAGE_CUBE_BIT) == _base_usage);
1377 #endif
1378
1379 if (info->surf->dim == ISL_SURF_DIM_3D) {
1380 assert(info->view->base_array_layer + info->view->array_len <=
1381 info->surf->logical_level0_px.depth);
1382 } else {
1383 assert(info->view->base_array_layer + info->view->array_len <=
1384 info->surf->logical_level0_px.array_len);
1385 }
1386
1387 switch (ISL_DEV_GEN(dev)) {
1388 case 4:
1389 if (ISL_DEV_IS_G4X(dev)) {
1390 /* G45 surface state is the same as gen5 */
1391 isl_gen5_surf_fill_state_s(dev, state, info);
1392 } else {
1393 isl_gen4_surf_fill_state_s(dev, state, info);
1394 }
1395 break;
1396 case 5:
1397 isl_gen5_surf_fill_state_s(dev, state, info);
1398 break;
1399 case 6:
1400 isl_gen6_surf_fill_state_s(dev, state, info);
1401 break;
1402 case 7:
1403 if (ISL_DEV_IS_HASWELL(dev)) {
1404 isl_gen75_surf_fill_state_s(dev, state, info);
1405 } else {
1406 isl_gen7_surf_fill_state_s(dev, state, info);
1407 }
1408 break;
1409 case 8:
1410 isl_gen8_surf_fill_state_s(dev, state, info);
1411 break;
1412 case 9:
1413 isl_gen9_surf_fill_state_s(dev, state, info);
1414 break;
1415 default:
1416 assert(!"Cannot fill surface state for this gen");
1417 }
1418 }
1419
1420 void
1421 isl_buffer_fill_state_s(const struct isl_device *dev, void *state,
1422 const struct isl_buffer_fill_state_info *restrict info)
1423 {
1424 switch (ISL_DEV_GEN(dev)) {
1425 case 4:
1426 case 5:
1427 /* Gen 4-5 are all the same when it comes to buffer surfaces */
1428 isl_gen5_buffer_fill_state_s(state, info);
1429 break;
1430 case 6:
1431 isl_gen6_buffer_fill_state_s(state, info);
1432 break;
1433 case 7:
1434 if (ISL_DEV_IS_HASWELL(dev)) {
1435 isl_gen75_buffer_fill_state_s(state, info);
1436 } else {
1437 isl_gen7_buffer_fill_state_s(state, info);
1438 }
1439 break;
1440 case 8:
1441 isl_gen8_buffer_fill_state_s(state, info);
1442 break;
1443 case 9:
1444 isl_gen9_buffer_fill_state_s(state, info);
1445 break;
1446 default:
1447 assert(!"Cannot fill surface state for this gen");
1448 }
1449 }
1450
1451 /**
1452 * A variant of isl_surf_get_image_offset_sa() specific to
1453 * ISL_DIM_LAYOUT_GEN4_2D.
1454 */
1455 static void
1456 get_image_offset_sa_gen4_2d(const struct isl_surf *surf,
1457 uint32_t level, uint32_t logical_array_layer,
1458 uint32_t *x_offset_sa,
1459 uint32_t *y_offset_sa)
1460 {
1461 assert(level < surf->levels);
1462 if (surf->dim == ISL_SURF_DIM_3D)
1463 assert(logical_array_layer < surf->logical_level0_px.depth);
1464 else
1465 assert(logical_array_layer < surf->logical_level0_px.array_len);
1466
1467 const struct isl_extent3d image_align_sa =
1468 isl_surf_get_image_alignment_sa(surf);
1469
1470 const uint32_t W0 = surf->phys_level0_sa.width;
1471 const uint32_t H0 = surf->phys_level0_sa.height;
1472
1473 const uint32_t phys_layer = logical_array_layer *
1474 (surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY ? surf->samples : 1);
1475
1476 uint32_t x = 0;
1477 uint32_t y = phys_layer * isl_surf_get_array_pitch_sa_rows(surf);
1478
1479 for (uint32_t l = 0; l < level; ++l) {
1480 if (l == 1) {
1481 uint32_t W = isl_minify(W0, l);
1482 x += isl_align_npot(W, image_align_sa.w);
1483 } else {
1484 uint32_t H = isl_minify(H0, l);
1485 y += isl_align_npot(H, image_align_sa.h);
1486 }
1487 }
1488
1489 *x_offset_sa = x;
1490 *y_offset_sa = y;
1491 }
1492
1493 /**
1494 * A variant of isl_surf_get_image_offset_sa() specific to
1495 * ISL_DIM_LAYOUT_GEN4_3D.
1496 */
1497 static void
1498 get_image_offset_sa_gen4_3d(const struct isl_surf *surf,
1499 uint32_t level, uint32_t logical_z_offset_px,
1500 uint32_t *x_offset_sa,
1501 uint32_t *y_offset_sa)
1502 {
1503 assert(level < surf->levels);
1504 assert(logical_z_offset_px < isl_minify(surf->phys_level0_sa.depth, level));
1505 assert(surf->phys_level0_sa.array_len == 1);
1506
1507 const struct isl_extent3d image_align_sa =
1508 isl_surf_get_image_alignment_sa(surf);
1509
1510 const uint32_t W0 = surf->phys_level0_sa.width;
1511 const uint32_t H0 = surf->phys_level0_sa.height;
1512 const uint32_t D0 = surf->phys_level0_sa.depth;
1513
1514 uint32_t x = 0;
1515 uint32_t y = 0;
1516
1517 for (uint32_t l = 0; l < level; ++l) {
1518 const uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa.h);
1519 const uint32_t level_d = isl_align_npot(isl_minify(D0, l), image_align_sa.d);
1520 const uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
1521
1522 y += level_h * max_layers_vert;
1523 }
1524
1525 const uint32_t level_w = isl_align_npot(isl_minify(W0, level), image_align_sa.w);
1526 const uint32_t level_h = isl_align_npot(isl_minify(H0, level), image_align_sa.h);
1527 const uint32_t level_d = isl_align_npot(isl_minify(D0, level), image_align_sa.d);
1528
1529 const uint32_t max_layers_horiz = MIN(level_d, 1u << level);
1530
1531 x += level_w * (logical_z_offset_px % max_layers_horiz);
1532 y += level_h * (logical_z_offset_px / max_layers_horiz);
1533
1534 *x_offset_sa = x;
1535 *y_offset_sa = y;
1536 }
1537
1538 /**
1539 * A variant of isl_surf_get_image_offset_sa() specific to
1540 * ISL_DIM_LAYOUT_GEN9_1D.
1541 */
1542 static void
1543 get_image_offset_sa_gen9_1d(const struct isl_surf *surf,
1544 uint32_t level, uint32_t layer,
1545 uint32_t *x_offset_sa,
1546 uint32_t *y_offset_sa)
1547 {
1548 assert(level < surf->levels);
1549 assert(layer < surf->phys_level0_sa.array_len);
1550 assert(surf->phys_level0_sa.height == 1);
1551 assert(surf->phys_level0_sa.depth == 1);
1552 assert(surf->samples == 1);
1553
1554 const uint32_t W0 = surf->phys_level0_sa.width;
1555 const struct isl_extent3d image_align_sa =
1556 isl_surf_get_image_alignment_sa(surf);
1557
1558 uint32_t x = 0;
1559
1560 for (uint32_t l = 0; l < level; ++l) {
1561 uint32_t W = isl_minify(W0, l);
1562 uint32_t w = isl_align_npot(W, image_align_sa.w);
1563
1564 x += w;
1565 }
1566
1567 *x_offset_sa = x;
1568 *y_offset_sa = layer * isl_surf_get_array_pitch_sa_rows(surf);
1569 }
1570
1571 /**
1572 * Calculate the offset, in units of surface samples, to a subimage in the
1573 * surface.
1574 *
1575 * @invariant level < surface levels
1576 * @invariant logical_array_layer < logical array length of surface
1577 * @invariant logical_z_offset_px < logical depth of surface at level
1578 */
1579 void
1580 isl_surf_get_image_offset_sa(const struct isl_surf *surf,
1581 uint32_t level,
1582 uint32_t logical_array_layer,
1583 uint32_t logical_z_offset_px,
1584 uint32_t *x_offset_sa,
1585 uint32_t *y_offset_sa)
1586 {
1587 assert(level < surf->levels);
1588 assert(logical_array_layer < surf->logical_level0_px.array_len);
1589 assert(logical_z_offset_px
1590 < isl_minify(surf->logical_level0_px.depth, level));
1591
1592 switch (surf->dim_layout) {
1593 case ISL_DIM_LAYOUT_GEN9_1D:
1594 get_image_offset_sa_gen9_1d(surf, level, logical_array_layer,
1595 x_offset_sa, y_offset_sa);
1596 break;
1597 case ISL_DIM_LAYOUT_GEN4_2D:
1598 get_image_offset_sa_gen4_2d(surf, level, logical_array_layer
1599 + logical_z_offset_px,
1600 x_offset_sa, y_offset_sa);
1601 break;
1602 case ISL_DIM_LAYOUT_GEN4_3D:
1603 get_image_offset_sa_gen4_3d(surf, level, logical_z_offset_px,
1604 x_offset_sa, y_offset_sa);
1605 break;
1606
1607 default:
1608 unreachable("not reached");
1609 }
1610 }
1611
1612 void
1613 isl_surf_get_image_offset_el(const struct isl_surf *surf,
1614 uint32_t level,
1615 uint32_t logical_array_layer,
1616 uint32_t logical_z_offset_px,
1617 uint32_t *x_offset_el,
1618 uint32_t *y_offset_el)
1619 {
1620 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1621
1622 assert(level < surf->levels);
1623 assert(logical_array_layer < surf->logical_level0_px.array_len);
1624 assert(logical_z_offset_px
1625 < isl_minify(surf->logical_level0_px.depth, level));
1626
1627 uint32_t x_offset_sa, y_offset_sa;
1628 isl_surf_get_image_offset_sa(surf, level,
1629 logical_array_layer,
1630 logical_z_offset_px,
1631 &x_offset_sa,
1632 &y_offset_sa);
1633
1634 *x_offset_el = x_offset_sa / fmtl->bw;
1635 *y_offset_el = y_offset_sa / fmtl->bh;
1636 }
1637
1638 void
1639 isl_tiling_get_intratile_offset_el(const struct isl_device *dev,
1640 enum isl_tiling tiling,
1641 uint8_t bs,
1642 uint32_t row_pitch,
1643 uint32_t total_x_offset_el,
1644 uint32_t total_y_offset_el,
1645 uint32_t *base_address_offset,
1646 uint32_t *x_offset_el,
1647 uint32_t *y_offset_el)
1648 {
1649 /* This function only really works for power-of-two surfaces. In
1650 * theory, we could make it work for non-power-of-two surfaces by going
1651 * to the left until we find a block that is bs-aligned. The Vulkan
1652 * driver doesn't use non-power-of-two tiled surfaces so we'll leave
1653 * this unimplemented for now.
1654 */
1655 assert(tiling == ISL_TILING_LINEAR || isl_is_pow2(bs));
1656
1657 if (tiling == ISL_TILING_LINEAR) {
1658 *base_address_offset = total_y_offset_el * row_pitch +
1659 total_x_offset_el * bs;
1660 *x_offset_el = 0;
1661 *y_offset_el = 0;
1662 return;
1663 }
1664
1665 struct isl_tile_info tile_info;
1666 isl_tiling_get_info(dev, tiling, bs * 8, &tile_info);
1667
1668 /* Compute the offset into the tile */
1669 *x_offset_el = total_x_offset_el % tile_info.logical_extent_el.w;
1670 *y_offset_el = total_y_offset_el % tile_info.logical_extent_el.h;
1671
1672 /* Compute the offset of the tile in units of whole tiles */
1673 uint32_t x_offset_tl = total_x_offset_el / tile_info.logical_extent_el.w;
1674 uint32_t y_offset_tl = total_y_offset_el / tile_info.logical_extent_el.h;
1675
1676 assert(row_pitch % tile_info.phys_extent_B.width == 0);
1677 *base_address_offset =
1678 y_offset_tl * tile_info.phys_extent_B.h * row_pitch +
1679 x_offset_tl * tile_info.phys_extent_B.h * tile_info.phys_extent_B.w;
1680 }
1681
1682 uint32_t
1683 isl_surf_get_depth_format(const struct isl_device *dev,
1684 const struct isl_surf *surf)
1685 {
1686 /* Support for separate stencil buffers began in gen5. Support for
1687 * interleaved depthstencil buffers ceased in gen7. The intermediate gens,
1688 * those that supported separate and interleaved stencil, were gen5 and
1689 * gen6.
1690 *
1691 * For a list of all available formats, see the Sandybridge PRM >> Volume
1692 * 2 Part 1: 3D/Media - 3D Pipeline >> 3DSTATE_DEPTH_BUFFER >> Surface
1693 * Format (p321).
1694 */
1695
1696 bool has_stencil = surf->usage & ISL_SURF_USAGE_STENCIL_BIT;
1697
1698 assert(surf->usage & ISL_SURF_USAGE_DEPTH_BIT);
1699
1700 if (has_stencil)
1701 assert(ISL_DEV_GEN(dev) < 7);
1702
1703 switch (surf->format) {
1704 default:
1705 unreachable("bad isl depth format");
1706 case ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS:
1707 assert(ISL_DEV_GEN(dev) < 7);
1708 return 0; /* D32_FLOAT_S8X24_UINT */
1709 case ISL_FORMAT_R32_FLOAT:
1710 assert(!has_stencil);
1711 return 1; /* D32_FLOAT */
1712 case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
1713 if (has_stencil) {
1714 assert(ISL_DEV_GEN(dev) < 7);
1715 return 2; /* D24_UNORM_S8_UINT */
1716 } else {
1717 assert(ISL_DEV_GEN(dev) >= 5);
1718 return 3; /* D24_UNORM_X8_UINT */
1719 }
1720 case ISL_FORMAT_R16_UNORM:
1721 assert(!has_stencil);
1722 return 5; /* D16_UNORM */
1723 }
1724 }