b573f0d9973f4204cdf2f184300a916ea3b57cba
[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 "genxml/genX_bits.h"
29
30 #include "isl.h"
31 #include "isl_gen4.h"
32 #include "isl_gen6.h"
33 #include "isl_gen7.h"
34 #include "isl_gen8.h"
35 #include "isl_gen9.h"
36 #include "isl_priv.h"
37
38 void PRINTFLIKE(3, 4) UNUSED
39 __isl_finishme(const char *file, int line, const char *fmt, ...)
40 {
41 va_list ap;
42 char buf[512];
43
44 va_start(ap, fmt);
45 vsnprintf(buf, sizeof(buf), fmt, ap);
46 va_end(ap);
47
48 fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buf);
49 }
50
51 void
52 isl_device_init(struct isl_device *dev,
53 const struct gen_device_info *info,
54 bool has_bit6_swizzling)
55 {
56 dev->info = info;
57 dev->use_separate_stencil = ISL_DEV_GEN(dev) >= 6;
58 dev->has_bit6_swizzling = has_bit6_swizzling;
59
60 /* The ISL_DEV macros may be defined in the CFLAGS, thus hardcoding some
61 * device properties at buildtime. Verify that the macros with the device
62 * properties chosen during runtime.
63 */
64 ISL_DEV_GEN_SANITIZE(dev);
65 ISL_DEV_USE_SEPARATE_STENCIL_SANITIZE(dev);
66
67 /* Did we break hiz or stencil? */
68 if (ISL_DEV_USE_SEPARATE_STENCIL(dev))
69 assert(info->has_hiz_and_separate_stencil);
70 if (info->must_use_separate_stencil)
71 assert(ISL_DEV_USE_SEPARATE_STENCIL(dev));
72
73 dev->ss.size = RENDER_SURFACE_STATE_length(info) * 4;
74 dev->ss.align = isl_align(dev->ss.size, 32);
75
76 assert(RENDER_SURFACE_STATE_SurfaceBaseAddress_start(info) % 8 == 0);
77 dev->ss.addr_offset =
78 RENDER_SURFACE_STATE_SurfaceBaseAddress_start(info) / 8;
79
80 /* The "Auxiliary Surface Base Address" field starts a bit higher up
81 * because the bottom 12 bits are used for other things. Round down to
82 * the nearest dword before.
83 */
84 dev->ss.aux_addr_offset =
85 (RENDER_SURFACE_STATE_AuxiliarySurfaceBaseAddress_start(info) & ~31) / 8;
86
87 dev->ds.size = _3DSTATE_DEPTH_BUFFER_length(info) * 4;
88 assert(_3DSTATE_DEPTH_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
89 dev->ds.depth_offset =
90 _3DSTATE_DEPTH_BUFFER_SurfaceBaseAddress_start(info) / 8;
91
92 if (dev->use_separate_stencil) {
93 dev->ds.size += _3DSTATE_STENCIL_BUFFER_length(info) * 4 +
94 _3DSTATE_HIER_DEPTH_BUFFER_length(info) * 4 +
95 _3DSTATE_CLEAR_PARAMS_length(info) * 4;
96
97 assert(_3DSTATE_STENCIL_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
98 dev->ds.stencil_offset =
99 _3DSTATE_DEPTH_BUFFER_length(info) * 4 +
100 _3DSTATE_STENCIL_BUFFER_SurfaceBaseAddress_start(info) / 8;
101
102 assert(_3DSTATE_HIER_DEPTH_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
103 dev->ds.hiz_offset =
104 _3DSTATE_DEPTH_BUFFER_length(info) * 4 +
105 _3DSTATE_STENCIL_BUFFER_length(info) * 4 +
106 _3DSTATE_HIER_DEPTH_BUFFER_SurfaceBaseAddress_start(info) / 8;
107 } else {
108 dev->ds.stencil_offset = 0;
109 dev->ds.hiz_offset = 0;
110 }
111 }
112
113 /**
114 * @brief Query the set of multisamples supported by the device.
115 *
116 * This function always returns non-zero, as ISL_SAMPLE_COUNT_1_BIT is always
117 * supported.
118 */
119 isl_sample_count_mask_t ATTRIBUTE_CONST
120 isl_device_get_sample_counts(struct isl_device *dev)
121 {
122 if (ISL_DEV_GEN(dev) >= 9) {
123 return ISL_SAMPLE_COUNT_1_BIT |
124 ISL_SAMPLE_COUNT_2_BIT |
125 ISL_SAMPLE_COUNT_4_BIT |
126 ISL_SAMPLE_COUNT_8_BIT |
127 ISL_SAMPLE_COUNT_16_BIT;
128 } else if (ISL_DEV_GEN(dev) >= 8) {
129 return ISL_SAMPLE_COUNT_1_BIT |
130 ISL_SAMPLE_COUNT_2_BIT |
131 ISL_SAMPLE_COUNT_4_BIT |
132 ISL_SAMPLE_COUNT_8_BIT;
133 } else if (ISL_DEV_GEN(dev) >= 7) {
134 return ISL_SAMPLE_COUNT_1_BIT |
135 ISL_SAMPLE_COUNT_4_BIT |
136 ISL_SAMPLE_COUNT_8_BIT;
137 } else if (ISL_DEV_GEN(dev) >= 6) {
138 return ISL_SAMPLE_COUNT_1_BIT |
139 ISL_SAMPLE_COUNT_4_BIT;
140 } else {
141 return ISL_SAMPLE_COUNT_1_BIT;
142 }
143 }
144
145 /**
146 * @param[out] info is written only on success
147 */
148 static void
149 isl_tiling_get_info(enum isl_tiling tiling,
150 uint32_t format_bpb,
151 struct isl_tile_info *tile_info)
152 {
153 const uint32_t bs = format_bpb / 8;
154 struct isl_extent2d logical_el, phys_B;
155
156 if (tiling != ISL_TILING_LINEAR && !isl_is_pow2(format_bpb)) {
157 /* It is possible to have non-power-of-two formats in a tiled buffer.
158 * The easiest way to handle this is to treat the tile as if it is three
159 * times as wide. This way no pixel will ever cross a tile boundary.
160 * This really only works on legacy X and Y tiling formats.
161 */
162 assert(tiling == ISL_TILING_X || tiling == ISL_TILING_Y0);
163 assert(bs % 3 == 0 && isl_is_pow2(format_bpb / 3));
164 isl_tiling_get_info(tiling, format_bpb / 3, tile_info);
165 return;
166 }
167
168 switch (tiling) {
169 case ISL_TILING_LINEAR:
170 assert(bs > 0);
171 logical_el = isl_extent2d(1, 1);
172 phys_B = isl_extent2d(bs, 1);
173 break;
174
175 case ISL_TILING_X:
176 assert(bs > 0);
177 logical_el = isl_extent2d(512 / bs, 8);
178 phys_B = isl_extent2d(512, 8);
179 break;
180
181 case ISL_TILING_Y0:
182 assert(bs > 0);
183 logical_el = isl_extent2d(128 / bs, 32);
184 phys_B = isl_extent2d(128, 32);
185 break;
186
187 case ISL_TILING_W:
188 assert(bs == 1);
189 logical_el = isl_extent2d(64, 64);
190 /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfacePitch:
191 *
192 * "If the surface is a stencil buffer (and thus has Tile Mode set
193 * to TILEMODE_WMAJOR), the pitch must be set to 2x the value
194 * computed based on width, as the stencil buffer is stored with two
195 * rows interleaved."
196 *
197 * This, together with the fact that stencil buffers are referred to as
198 * being Y-tiled in the PRMs for older hardware implies that the
199 * physical size of a W-tile is actually the same as for a Y-tile.
200 */
201 phys_B = isl_extent2d(128, 32);
202 break;
203
204 case ISL_TILING_Yf:
205 case ISL_TILING_Ys: {
206 bool is_Ys = tiling == ISL_TILING_Ys;
207
208 assert(bs > 0);
209 unsigned width = 1 << (6 + (ffs(bs) / 2) + (2 * is_Ys));
210 unsigned height = 1 << (6 - (ffs(bs) / 2) + (2 * is_Ys));
211
212 logical_el = isl_extent2d(width / bs, height);
213 phys_B = isl_extent2d(width, height);
214 break;
215 }
216
217 case ISL_TILING_HIZ:
218 /* HiZ buffers are required to have ISL_FORMAT_HIZ which is an 8x4
219 * 128bpb format. The tiling has the same physical dimensions as
220 * Y-tiling but actually has two HiZ columns per Y-tiled column.
221 */
222 assert(bs == 16);
223 logical_el = isl_extent2d(16, 16);
224 phys_B = isl_extent2d(128, 32);
225 break;
226
227 case ISL_TILING_CCS:
228 /* CCS surfaces are required to have one of the GENX_CCS_* formats which
229 * have a block size of 1 or 2 bits per block and each CCS element
230 * corresponds to one cache-line pair in the main surface. From the Sky
231 * Lake PRM Vol. 12 in the section on planes:
232 *
233 * "The Color Control Surface (CCS) contains the compression status
234 * of the cache-line pairs. The compression state of the cache-line
235 * pair is specified by 2 bits in the CCS. Each CCS cache-line
236 * represents an area on the main surface of 16x16 sets of 128 byte
237 * Y-tiled cache-line-pairs. CCS is always Y tiled."
238 *
239 * The CCS being Y-tiled implies that it's an 8x8 grid of cache-lines.
240 * Since each cache line corresponds to a 16x16 set of cache-line pairs,
241 * that yields total tile area of 128x128 cache-line pairs or CCS
242 * elements. On older hardware, each CCS element is 1 bit and the tile
243 * is 128x256 elements.
244 */
245 assert(format_bpb == 1 || format_bpb == 2);
246 logical_el = isl_extent2d(128, 256 / format_bpb);
247 phys_B = isl_extent2d(128, 32);
248 break;
249
250 default:
251 unreachable("not reached");
252 } /* end switch */
253
254 *tile_info = (struct isl_tile_info) {
255 .tiling = tiling,
256 .format_bpb = format_bpb,
257 .logical_extent_el = logical_el,
258 .phys_extent_B = phys_B,
259 };
260 }
261
262 /**
263 * @param[out] tiling is set only on success
264 */
265 static bool
266 isl_surf_choose_tiling(const struct isl_device *dev,
267 const struct isl_surf_init_info *restrict info,
268 enum isl_tiling *tiling)
269 {
270 isl_tiling_flags_t tiling_flags = info->tiling_flags;
271
272 /* HiZ surfaces always use the HiZ tiling */
273 if (info->usage & ISL_SURF_USAGE_HIZ_BIT) {
274 assert(info->format == ISL_FORMAT_HIZ);
275 assert(tiling_flags == ISL_TILING_HIZ_BIT);
276 *tiling = ISL_TILING_HIZ;
277 return true;
278 }
279
280 /* CCS surfaces always use the CCS tiling */
281 if (info->usage & ISL_SURF_USAGE_CCS_BIT) {
282 assert(isl_format_get_layout(info->format)->txc == ISL_TXC_CCS);
283 assert(tiling_flags == ISL_TILING_CCS_BIT);
284 *tiling = ISL_TILING_CCS;
285 return true;
286 }
287
288 if (ISL_DEV_GEN(dev) >= 6) {
289 isl_gen6_filter_tiling(dev, info, &tiling_flags);
290 } else {
291 isl_gen4_filter_tiling(dev, info, &tiling_flags);
292 }
293
294 #define CHOOSE(__tiling) \
295 do { \
296 if (tiling_flags & (1u << (__tiling))) { \
297 *tiling = (__tiling); \
298 return true; \
299 } \
300 } while (0)
301
302 /* Of the tiling modes remaining, choose the one that offers the best
303 * performance.
304 */
305
306 if (info->dim == ISL_SURF_DIM_1D) {
307 /* Prefer linear for 1D surfaces because they do not benefit from
308 * tiling. To the contrary, tiling leads to wasted memory and poor
309 * memory locality due to the swizzling and alignment restrictions
310 * required in tiled surfaces.
311 */
312 CHOOSE(ISL_TILING_LINEAR);
313 }
314
315 CHOOSE(ISL_TILING_Ys);
316 CHOOSE(ISL_TILING_Yf);
317 CHOOSE(ISL_TILING_Y0);
318 CHOOSE(ISL_TILING_X);
319 CHOOSE(ISL_TILING_W);
320 CHOOSE(ISL_TILING_LINEAR);
321
322 #undef CHOOSE
323
324 /* No tiling mode accomodates the inputs. */
325 return false;
326 }
327
328 static bool
329 isl_choose_msaa_layout(const struct isl_device *dev,
330 const struct isl_surf_init_info *info,
331 enum isl_tiling tiling,
332 enum isl_msaa_layout *msaa_layout)
333 {
334 if (ISL_DEV_GEN(dev) >= 8) {
335 return isl_gen8_choose_msaa_layout(dev, info, tiling, msaa_layout);
336 } else if (ISL_DEV_GEN(dev) >= 7) {
337 return isl_gen7_choose_msaa_layout(dev, info, tiling, msaa_layout);
338 } else if (ISL_DEV_GEN(dev) >= 6) {
339 return isl_gen6_choose_msaa_layout(dev, info, tiling, msaa_layout);
340 } else {
341 return isl_gen4_choose_msaa_layout(dev, info, tiling, msaa_layout);
342 }
343 }
344
345 struct isl_extent2d
346 isl_get_interleaved_msaa_px_size_sa(uint32_t samples)
347 {
348 assert(isl_is_pow2(samples));
349
350 /* From the Broadwell PRM >> Volume 5: Memory Views >> Computing Mip Level
351 * Sizes (p133):
352 *
353 * If the surface is multisampled and it is a depth or stencil surface
354 * or Multisampled Surface StorageFormat in SURFACE_STATE is
355 * MSFMT_DEPTH_STENCIL, W_L and H_L must be adjusted as follows before
356 * proceeding: [...]
357 */
358 return (struct isl_extent2d) {
359 .width = 1 << ((ffs(samples) - 0) / 2),
360 .height = 1 << ((ffs(samples) - 1) / 2),
361 };
362 }
363
364 static void
365 isl_msaa_interleaved_scale_px_to_sa(uint32_t samples,
366 uint32_t *width, uint32_t *height)
367 {
368 const struct isl_extent2d px_size_sa =
369 isl_get_interleaved_msaa_px_size_sa(samples);
370
371 if (width)
372 *width = isl_align(*width, 2) * px_size_sa.width;
373 if (height)
374 *height = isl_align(*height, 2) * px_size_sa.height;
375 }
376
377 static enum isl_array_pitch_span
378 isl_choose_array_pitch_span(const struct isl_device *dev,
379 const struct isl_surf_init_info *restrict info,
380 enum isl_dim_layout dim_layout,
381 const struct isl_extent4d *phys_level0_sa)
382 {
383 switch (dim_layout) {
384 case ISL_DIM_LAYOUT_GEN9_1D:
385 case ISL_DIM_LAYOUT_GEN4_2D:
386 if (ISL_DEV_GEN(dev) >= 8) {
387 /* QPitch becomes programmable in Broadwell. So choose the
388 * most compact QPitch possible in order to conserve memory.
389 *
390 * From the Broadwell PRM >> Volume 2d: Command Reference: Structures
391 * >> RENDER_SURFACE_STATE Surface QPitch (p325):
392 *
393 * - Software must ensure that this field is set to a value
394 * sufficiently large such that the array slices in the surface
395 * do not overlap. Refer to the Memory Data Formats section for
396 * information on how surfaces are stored in memory.
397 *
398 * - This field specifies the distance in rows between array
399 * slices. It is used only in the following cases:
400 *
401 * - Surface Array is enabled OR
402 * - Number of Mulitsamples is not NUMSAMPLES_1 and
403 * Multisampled Surface Storage Format set to MSFMT_MSS OR
404 * - Surface Type is SURFTYPE_CUBE
405 */
406 return ISL_ARRAY_PITCH_SPAN_COMPACT;
407 } else if (ISL_DEV_GEN(dev) >= 7) {
408 /* Note that Ivybridge introduces
409 * RENDER_SURFACE_STATE.SurfaceArraySpacing, which provides the
410 * driver more control over the QPitch.
411 */
412
413 if (phys_level0_sa->array_len == 1) {
414 /* The hardware will never use the QPitch. So choose the most
415 * compact QPitch possible in order to conserve memory.
416 */
417 return ISL_ARRAY_PITCH_SPAN_COMPACT;
418 }
419
420 if (isl_surf_usage_is_depth_or_stencil(info->usage) ||
421 (info->usage & ISL_SURF_USAGE_HIZ_BIT)) {
422 /* From the Ivybridge PRM >> Volume 1 Part 1: Graphics Core >>
423 * Section 6.18.4.7: Surface Arrays (p112):
424 *
425 * If Surface Array Spacing is set to ARYSPC_FULL (note that
426 * the depth buffer and stencil buffer have an implied value of
427 * ARYSPC_FULL):
428 */
429 return ISL_ARRAY_PITCH_SPAN_FULL;
430 }
431
432 if (info->levels == 1) {
433 /* We are able to set RENDER_SURFACE_STATE.SurfaceArraySpacing
434 * to ARYSPC_LOD0.
435 */
436 return ISL_ARRAY_PITCH_SPAN_COMPACT;
437 }
438
439 return ISL_ARRAY_PITCH_SPAN_FULL;
440 } else if ((ISL_DEV_GEN(dev) == 5 || ISL_DEV_GEN(dev) == 6) &&
441 ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
442 isl_surf_usage_is_stencil(info->usage)) {
443 /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
444 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
445 *
446 * The separate stencil buffer does not support mip mapping, thus
447 * the storage for LODs other than LOD 0 is not needed.
448 */
449 assert(info->levels == 1);
450 assert(phys_level0_sa->array_len == 1);
451 return ISL_ARRAY_PITCH_SPAN_COMPACT;
452 } else {
453 if ((ISL_DEV_GEN(dev) == 5 || ISL_DEV_GEN(dev) == 6) &&
454 ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
455 isl_surf_usage_is_stencil(info->usage)) {
456 /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
457 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
458 *
459 * The separate stencil buffer does not support mip mapping,
460 * thus the storage for LODs other than LOD 0 is not needed.
461 */
462 assert(info->levels == 1);
463 assert(phys_level0_sa->array_len == 1);
464 return ISL_ARRAY_PITCH_SPAN_COMPACT;
465 }
466
467 if (phys_level0_sa->array_len == 1) {
468 /* The hardware will never use the QPitch. So choose the most
469 * compact QPitch possible in order to conserve memory.
470 */
471 return ISL_ARRAY_PITCH_SPAN_COMPACT;
472 }
473
474 return ISL_ARRAY_PITCH_SPAN_FULL;
475 }
476
477 case ISL_DIM_LAYOUT_GEN4_3D:
478 /* The hardware will never use the QPitch. So choose the most
479 * compact QPitch possible in order to conserve memory.
480 */
481 return ISL_ARRAY_PITCH_SPAN_COMPACT;
482 }
483
484 unreachable("bad isl_dim_layout");
485 return ISL_ARRAY_PITCH_SPAN_FULL;
486 }
487
488 static void
489 isl_choose_image_alignment_el(const struct isl_device *dev,
490 const struct isl_surf_init_info *restrict info,
491 enum isl_tiling tiling,
492 enum isl_dim_layout dim_layout,
493 enum isl_msaa_layout msaa_layout,
494 struct isl_extent3d *image_align_el)
495 {
496 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
497 if (fmtl->txc == ISL_TXC_MCS) {
498 assert(tiling == ISL_TILING_Y0);
499
500 /*
501 * IvyBrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
502 *
503 * Height, width, and layout of MCS buffer in this case must match with
504 * Render Target height, width, and layout. MCS buffer is tiledY.
505 *
506 * To avoid wasting memory, choose the smallest alignment possible:
507 * HALIGN_4 and VALIGN_4.
508 */
509 *image_align_el = isl_extent3d(4, 4, 1);
510 return;
511 } else if (info->format == ISL_FORMAT_HIZ) {
512 assert(ISL_DEV_GEN(dev) >= 6);
513 /* HiZ surfaces are always aligned to 16x8 pixels in the primary surface
514 * which works out to 2x2 HiZ elments.
515 */
516 *image_align_el = isl_extent3d(2, 2, 1);
517 return;
518 }
519
520 if (ISL_DEV_GEN(dev) >= 9) {
521 isl_gen9_choose_image_alignment_el(dev, info, tiling, dim_layout,
522 msaa_layout, image_align_el);
523 } else if (ISL_DEV_GEN(dev) >= 8) {
524 isl_gen8_choose_image_alignment_el(dev, info, tiling, dim_layout,
525 msaa_layout, image_align_el);
526 } else if (ISL_DEV_GEN(dev) >= 7) {
527 isl_gen7_choose_image_alignment_el(dev, info, tiling, dim_layout,
528 msaa_layout, image_align_el);
529 } else if (ISL_DEV_GEN(dev) >= 6) {
530 isl_gen6_choose_image_alignment_el(dev, info, tiling, dim_layout,
531 msaa_layout, image_align_el);
532 } else {
533 isl_gen4_choose_image_alignment_el(dev, info, tiling, dim_layout,
534 msaa_layout, image_align_el);
535 }
536 }
537
538 static enum isl_dim_layout
539 isl_surf_choose_dim_layout(const struct isl_device *dev,
540 enum isl_surf_dim logical_dim,
541 enum isl_tiling tiling)
542 {
543 if (ISL_DEV_GEN(dev) >= 9) {
544 switch (logical_dim) {
545 case ISL_SURF_DIM_1D:
546 /* From the Sky Lake PRM Vol. 5, "1D Surfaces":
547 *
548 * One-dimensional surfaces use a tiling mode of linear.
549 * Technically, they are not tiled resources, but the Tiled
550 * Resource Mode field in RENDER_SURFACE_STATE is still used to
551 * indicate the alignment requirements for this linear surface
552 * (See 1D Alignment requirements for how 4K and 64KB Tiled
553 * Resource Modes impact alignment). Alternatively, a 1D surface
554 * can be defined as a 2D tiled surface (e.g. TileY or TileX) with
555 * a height of 0.
556 *
557 * In other words, ISL_DIM_LAYOUT_GEN9_1D is only used for linear
558 * surfaces and, for tiled surfaces, ISL_DIM_LAYOUT_GEN4_2D is used.
559 */
560 if (tiling == ISL_TILING_LINEAR)
561 return ISL_DIM_LAYOUT_GEN9_1D;
562 else
563 return ISL_DIM_LAYOUT_GEN4_2D;
564 case ISL_SURF_DIM_2D:
565 case ISL_SURF_DIM_3D:
566 return ISL_DIM_LAYOUT_GEN4_2D;
567 }
568 } else {
569 switch (logical_dim) {
570 case ISL_SURF_DIM_1D:
571 case ISL_SURF_DIM_2D:
572 return ISL_DIM_LAYOUT_GEN4_2D;
573 case ISL_SURF_DIM_3D:
574 return ISL_DIM_LAYOUT_GEN4_3D;
575 }
576 }
577
578 unreachable("bad isl_surf_dim");
579 return ISL_DIM_LAYOUT_GEN4_2D;
580 }
581
582 /**
583 * Calculate the physical extent of the surface's first level, in units of
584 * surface samples. The result is aligned to the format's compression block.
585 */
586 static void
587 isl_calc_phys_level0_extent_sa(const struct isl_device *dev,
588 const struct isl_surf_init_info *restrict info,
589 enum isl_dim_layout dim_layout,
590 enum isl_tiling tiling,
591 enum isl_msaa_layout msaa_layout,
592 struct isl_extent4d *phys_level0_sa)
593 {
594 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
595
596 if (isl_format_is_yuv(info->format))
597 isl_finishme("%s:%s: YUV format", __FILE__, __func__);
598
599 switch (info->dim) {
600 case ISL_SURF_DIM_1D:
601 assert(info->height == 1);
602 assert(info->depth == 1);
603 assert(info->samples == 1);
604
605 switch (dim_layout) {
606 case ISL_DIM_LAYOUT_GEN4_3D:
607 unreachable("bad isl_dim_layout");
608
609 case ISL_DIM_LAYOUT_GEN9_1D:
610 case ISL_DIM_LAYOUT_GEN4_2D:
611 *phys_level0_sa = (struct isl_extent4d) {
612 .w = isl_align_npot(info->width, fmtl->bw),
613 .h = fmtl->bh,
614 .d = 1,
615 .a = info->array_len,
616 };
617 break;
618 }
619 break;
620
621 case ISL_SURF_DIM_2D:
622 assert(dim_layout == ISL_DIM_LAYOUT_GEN4_2D);
623
624 if (tiling == ISL_TILING_Ys && info->samples > 1)
625 isl_finishme("%s:%s: multisample TileYs layout", __FILE__, __func__);
626
627 switch (msaa_layout) {
628 case ISL_MSAA_LAYOUT_NONE:
629 assert(info->depth == 1);
630 assert(info->samples == 1);
631
632 *phys_level0_sa = (struct isl_extent4d) {
633 .w = isl_align_npot(info->width, fmtl->bw),
634 .h = isl_align_npot(info->height, fmtl->bh),
635 .d = 1,
636 .a = info->array_len,
637 };
638 break;
639
640 case ISL_MSAA_LAYOUT_ARRAY:
641 assert(info->depth == 1);
642 assert(info->levels == 1);
643 assert(isl_format_supports_multisampling(dev->info, info->format));
644 assert(fmtl->bw == 1 && fmtl->bh == 1);
645
646 *phys_level0_sa = (struct isl_extent4d) {
647 .w = info->width,
648 .h = info->height,
649 .d = 1,
650 .a = info->array_len * info->samples,
651 };
652 break;
653
654 case ISL_MSAA_LAYOUT_INTERLEAVED:
655 assert(info->depth == 1);
656 assert(info->levels == 1);
657 assert(isl_format_supports_multisampling(dev->info, info->format));
658
659 *phys_level0_sa = (struct isl_extent4d) {
660 .w = info->width,
661 .h = info->height,
662 .d = 1,
663 .a = info->array_len,
664 };
665
666 isl_msaa_interleaved_scale_px_to_sa(info->samples,
667 &phys_level0_sa->w,
668 &phys_level0_sa->h);
669
670 phys_level0_sa->w = isl_align(phys_level0_sa->w, fmtl->bw);
671 phys_level0_sa->h = isl_align(phys_level0_sa->h, fmtl->bh);
672 break;
673 }
674 break;
675
676 case ISL_SURF_DIM_3D:
677 assert(info->array_len == 1);
678 assert(info->samples == 1);
679
680 if (fmtl->bd > 1) {
681 isl_finishme("%s:%s: compression block with depth > 1",
682 __FILE__, __func__);
683 }
684
685 switch (dim_layout) {
686 case ISL_DIM_LAYOUT_GEN9_1D:
687 unreachable("bad isl_dim_layout");
688
689 case ISL_DIM_LAYOUT_GEN4_2D:
690 assert(ISL_DEV_GEN(dev) >= 9);
691
692 *phys_level0_sa = (struct isl_extent4d) {
693 .w = isl_align_npot(info->width, fmtl->bw),
694 .h = isl_align_npot(info->height, fmtl->bh),
695 .d = 1,
696 .a = info->depth,
697 };
698 break;
699
700 case ISL_DIM_LAYOUT_GEN4_3D:
701 assert(ISL_DEV_GEN(dev) < 9);
702 *phys_level0_sa = (struct isl_extent4d) {
703 .w = isl_align(info->width, fmtl->bw),
704 .h = isl_align(info->height, fmtl->bh),
705 .d = info->depth,
706 .a = 1,
707 };
708 break;
709 }
710 break;
711 }
712 }
713
714 /**
715 * Calculate the pitch between physical array slices, in units of rows of
716 * surface elements.
717 */
718 static uint32_t
719 isl_calc_array_pitch_el_rows(const struct isl_device *dev,
720 const struct isl_surf_init_info *restrict info,
721 const struct isl_tile_info *tile_info,
722 enum isl_dim_layout dim_layout,
723 enum isl_array_pitch_span array_pitch_span,
724 const struct isl_extent3d *image_align_sa,
725 const struct isl_extent4d *phys_level0_sa,
726 const struct isl_extent2d *phys_slice0_sa)
727 {
728 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
729 uint32_t pitch_sa_rows = 0;
730
731 switch (dim_layout) {
732 case ISL_DIM_LAYOUT_GEN9_1D:
733 /* Each row is an array slice */
734 pitch_sa_rows = 1;
735 break;
736 case ISL_DIM_LAYOUT_GEN4_2D:
737 switch (array_pitch_span) {
738 case ISL_ARRAY_PITCH_SPAN_COMPACT:
739 pitch_sa_rows = isl_align_npot(phys_slice0_sa->h, image_align_sa->h);
740 break;
741 case ISL_ARRAY_PITCH_SPAN_FULL: {
742 /* The QPitch equation is found in the Broadwell PRM >> Volume 5:
743 * Memory Views >> Common Surface Formats >> Surface Layout >> 2D
744 * Surfaces >> Surface Arrays.
745 */
746 uint32_t H0_sa = phys_level0_sa->h;
747 uint32_t H1_sa = isl_minify(H0_sa, 1);
748
749 uint32_t h0_sa = isl_align_npot(H0_sa, image_align_sa->h);
750 uint32_t h1_sa = isl_align_npot(H1_sa, image_align_sa->h);
751
752 uint32_t m;
753 if (ISL_DEV_GEN(dev) >= 7) {
754 /* The QPitch equation changed slightly in Ivybridge. */
755 m = 12;
756 } else {
757 m = 11;
758 }
759
760 pitch_sa_rows = h0_sa + h1_sa + (m * image_align_sa->h);
761
762 if (ISL_DEV_GEN(dev) == 6 && info->samples > 1 &&
763 (info->height % 4 == 1)) {
764 /* [SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
765 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
766 *
767 * [SNB] Errata: Sampler MSAA Qpitch will be 4 greater than
768 * the value calculated in the equation above , for every
769 * other odd Surface Height starting from 1 i.e. 1,5,9,13.
770 *
771 * XXX(chadv): Is the errata natural corollary of the physical
772 * layout of interleaved samples?
773 */
774 pitch_sa_rows += 4;
775 }
776
777 pitch_sa_rows = isl_align_npot(pitch_sa_rows, fmtl->bh);
778 } /* end case */
779 break;
780 }
781 break;
782 case ISL_DIM_LAYOUT_GEN4_3D:
783 assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
784 pitch_sa_rows = isl_align_npot(phys_slice0_sa->h, image_align_sa->h);
785 break;
786 default:
787 unreachable("bad isl_dim_layout");
788 break;
789 }
790
791 assert(pitch_sa_rows % fmtl->bh == 0);
792 uint32_t pitch_el_rows = pitch_sa_rows / fmtl->bh;
793
794 if (ISL_DEV_GEN(dev) >= 9 && fmtl->txc == ISL_TXC_CCS) {
795 /*
796 * From the Sky Lake PRM Vol 7, "MCS Buffer for Render Target(s)" (p. 632):
797 *
798 * "Mip-mapped and arrayed surfaces are supported with MCS buffer
799 * layout with these alignments in the RT space: Horizontal
800 * Alignment = 128 and Vertical Alignment = 64."
801 *
802 * From the Sky Lake PRM Vol. 2d, "RENDER_SURFACE_STATE" (p. 435):
803 *
804 * "For non-multisampled render target's CCS auxiliary surface,
805 * QPitch must be computed with Horizontal Alignment = 128 and
806 * Surface Vertical Alignment = 256. These alignments are only for
807 * CCS buffer and not for associated render target."
808 *
809 * The first restriction is already handled by isl_choose_image_alignment_el
810 * but the second restriction, which is an extension of the first, only
811 * applies to qpitch and must be applied here.
812 */
813 assert(fmtl->bh == 4);
814 pitch_el_rows = isl_align(pitch_el_rows, 256 / 4);
815 }
816
817 if (ISL_DEV_GEN(dev) >= 9 &&
818 info->dim == ISL_SURF_DIM_3D &&
819 tile_info->tiling != ISL_TILING_LINEAR) {
820 /* From the Skylake BSpec >> RENDER_SURFACE_STATE >> Surface QPitch:
821 *
822 * Tile Mode != Linear: This field must be set to an integer multiple
823 * of the tile height
824 */
825 pitch_el_rows = isl_align(pitch_el_rows, tile_info->logical_extent_el.height);
826 }
827
828 return pitch_el_rows;
829 }
830
831 /**
832 * A variant of isl_calc_phys_slice0_extent_sa() specific to
833 * ISL_DIM_LAYOUT_GEN4_2D.
834 */
835 static void
836 isl_calc_phys_slice0_extent_sa_gen4_2d(
837 const struct isl_device *dev,
838 const struct isl_surf_init_info *restrict info,
839 enum isl_msaa_layout msaa_layout,
840 const struct isl_extent3d *image_align_sa,
841 const struct isl_extent4d *phys_level0_sa,
842 struct isl_extent2d *phys_slice0_sa)
843 {
844 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
845
846 assert(phys_level0_sa->depth == 1);
847
848 if (info->levels == 1) {
849 /* Do not pad the surface to the image alignment. Instead, pad it only
850 * to the pixel format's block alignment.
851 *
852 * For tiled surfaces, using a reduced alignment here avoids wasting CPU
853 * cycles on the below mipmap layout caluclations. Reducing the
854 * alignment here is safe because we later align the row pitch and array
855 * pitch to the tile boundary. It is safe even for
856 * ISL_MSAA_LAYOUT_INTERLEAVED, because phys_level0_sa is already scaled
857 * to accomodate the interleaved samples.
858 *
859 * For linear surfaces, reducing the alignment here permits us to later
860 * choose an arbitrary, non-aligned row pitch. If the surface backs
861 * a VkBuffer, then an arbitrary pitch may be needed to accomodate
862 * VkBufferImageCopy::bufferRowLength.
863 */
864 *phys_slice0_sa = (struct isl_extent2d) {
865 .w = isl_align_npot(phys_level0_sa->w, fmtl->bw),
866 .h = isl_align_npot(phys_level0_sa->h, fmtl->bh),
867 };
868 return;
869 }
870
871 uint32_t slice_top_w = 0;
872 uint32_t slice_bottom_w = 0;
873 uint32_t slice_left_h = 0;
874 uint32_t slice_right_h = 0;
875
876 uint32_t W0 = phys_level0_sa->w;
877 uint32_t H0 = phys_level0_sa->h;
878
879 for (uint32_t l = 0; l < info->levels; ++l) {
880 uint32_t W = isl_minify(W0, l);
881 uint32_t H = isl_minify(H0, l);
882
883 uint32_t w = isl_align_npot(W, image_align_sa->w);
884 uint32_t h = isl_align_npot(H, image_align_sa->h);
885
886 if (l == 0) {
887 slice_top_w = w;
888 slice_left_h = h;
889 slice_right_h = h;
890 } else if (l == 1) {
891 slice_bottom_w = w;
892 slice_left_h += h;
893 } else if (l == 2) {
894 slice_bottom_w += w;
895 slice_right_h += h;
896 } else {
897 slice_right_h += h;
898 }
899 }
900
901 *phys_slice0_sa = (struct isl_extent2d) {
902 .w = MAX(slice_top_w, slice_bottom_w),
903 .h = MAX(slice_left_h, slice_right_h),
904 };
905 }
906
907 /**
908 * A variant of isl_calc_phys_slice0_extent_sa() specific to
909 * ISL_DIM_LAYOUT_GEN4_3D.
910 */
911 static void
912 isl_calc_phys_slice0_extent_sa_gen4_3d(
913 const struct isl_device *dev,
914 const struct isl_surf_init_info *restrict info,
915 const struct isl_extent3d *image_align_sa,
916 const struct isl_extent4d *phys_level0_sa,
917 struct isl_extent2d *phys_slice0_sa)
918 {
919 assert(info->samples == 1);
920 assert(phys_level0_sa->array_len == 1);
921
922 uint32_t slice_w = 0;
923 uint32_t slice_h = 0;
924
925 uint32_t W0 = phys_level0_sa->w;
926 uint32_t H0 = phys_level0_sa->h;
927 uint32_t D0 = phys_level0_sa->d;
928
929 for (uint32_t l = 0; l < info->levels; ++l) {
930 uint32_t level_w = isl_align_npot(isl_minify(W0, l), image_align_sa->w);
931 uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa->h);
932 uint32_t level_d = isl_align_npot(isl_minify(D0, l), image_align_sa->d);
933
934 uint32_t max_layers_horiz = MIN(level_d, 1u << l);
935 uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
936
937 slice_w = MAX(slice_w, level_w * max_layers_horiz);
938 slice_h += level_h * max_layers_vert;
939 }
940
941 *phys_slice0_sa = (struct isl_extent2d) {
942 .w = slice_w,
943 .h = slice_h,
944 };
945 }
946
947 /**
948 * A variant of isl_calc_phys_slice0_extent_sa() specific to
949 * ISL_DIM_LAYOUT_GEN9_1D.
950 */
951 static void
952 isl_calc_phys_slice0_extent_sa_gen9_1d(
953 const struct isl_device *dev,
954 const struct isl_surf_init_info *restrict info,
955 const struct isl_extent3d *image_align_sa,
956 const struct isl_extent4d *phys_level0_sa,
957 struct isl_extent2d *phys_slice0_sa)
958 {
959 MAYBE_UNUSED const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
960
961 assert(phys_level0_sa->height == 1);
962 assert(phys_level0_sa->depth == 1);
963 assert(info->samples == 1);
964 assert(image_align_sa->w >= fmtl->bw);
965
966 uint32_t slice_w = 0;
967 const uint32_t W0 = phys_level0_sa->w;
968
969 for (uint32_t l = 0; l < info->levels; ++l) {
970 uint32_t W = isl_minify(W0, l);
971 uint32_t w = isl_align_npot(W, image_align_sa->w);
972
973 slice_w += w;
974 }
975
976 *phys_slice0_sa = isl_extent2d(slice_w, 1);
977 }
978
979 /**
980 * Calculate the physical extent of the surface's first array slice, in units
981 * of surface samples. If the surface is multi-leveled, then the result will
982 * be aligned to \a image_align_sa.
983 */
984 static void
985 isl_calc_phys_slice0_extent_sa(const struct isl_device *dev,
986 const struct isl_surf_init_info *restrict info,
987 enum isl_dim_layout dim_layout,
988 enum isl_msaa_layout msaa_layout,
989 const struct isl_extent3d *image_align_sa,
990 const struct isl_extent4d *phys_level0_sa,
991 struct isl_extent2d *phys_slice0_sa)
992 {
993 switch (dim_layout) {
994 case ISL_DIM_LAYOUT_GEN9_1D:
995 isl_calc_phys_slice0_extent_sa_gen9_1d(dev, info,
996 image_align_sa, phys_level0_sa,
997 phys_slice0_sa);
998 return;
999 case ISL_DIM_LAYOUT_GEN4_2D:
1000 isl_calc_phys_slice0_extent_sa_gen4_2d(dev, info, msaa_layout,
1001 image_align_sa, phys_level0_sa,
1002 phys_slice0_sa);
1003 return;
1004 case ISL_DIM_LAYOUT_GEN4_3D:
1005 isl_calc_phys_slice0_extent_sa_gen4_3d(dev, info, image_align_sa,
1006 phys_level0_sa, phys_slice0_sa);
1007 return;
1008 }
1009 }
1010
1011 static uint32_t
1012 isl_calc_row_pitch_alignment(const struct isl_surf_init_info *surf_info,
1013 const struct isl_tile_info *tile_info)
1014 {
1015 if (tile_info->tiling != ISL_TILING_LINEAR)
1016 return tile_info->phys_extent_B.width;
1017
1018 /* From the Broadwel PRM >> Volume 2d: Command Reference: Structures >>
1019 * RENDER_SURFACE_STATE Surface Pitch (p349):
1020 *
1021 * - For linear render target surfaces and surfaces accessed with the
1022 * typed data port messages, the pitch must be a multiple of the
1023 * element size for non-YUV surface formats. Pitch must be
1024 * a multiple of 2 * element size for YUV surface formats.
1025 *
1026 * - [Requirements for SURFTYPE_BUFFER and SURFTYPE_STRBUF, which we
1027 * ignore because isl doesn't do buffers.]
1028 *
1029 * - For other linear surfaces, the pitch can be any multiple of
1030 * bytes.
1031 */
1032 const struct isl_format_layout *fmtl = isl_format_get_layout(surf_info->format);
1033 const uint32_t bs = fmtl->bpb / 8;
1034
1035 if (surf_info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1036 if (isl_format_is_yuv(surf_info->format)) {
1037 return 2 * bs;
1038 } else {
1039 return bs;
1040 }
1041 }
1042
1043 return 1;
1044 }
1045
1046 static uint32_t
1047 isl_calc_linear_min_row_pitch(const struct isl_device *dev,
1048 const struct isl_surf_init_info *info,
1049 const struct isl_extent2d *phys_slice0_sa,
1050 uint32_t alignment)
1051 {
1052 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1053 const uint32_t bs = fmtl->bpb / 8;
1054
1055 assert(phys_slice0_sa->w % fmtl->bw == 0);
1056
1057 return isl_align_npot(bs * (phys_slice0_sa->w / fmtl->bw), alignment);
1058 }
1059
1060 static uint32_t
1061 isl_calc_tiled_min_row_pitch(const struct isl_device *dev,
1062 const struct isl_surf_init_info *surf_info,
1063 const struct isl_tile_info *tile_info,
1064 const struct isl_extent2d *phys_slice0_sa,
1065 uint32_t alignment)
1066 {
1067 const struct isl_format_layout *fmtl = isl_format_get_layout(surf_info->format);
1068
1069 assert(fmtl->bpb % tile_info->format_bpb == 0);
1070 assert(phys_slice0_sa->w % fmtl->bw == 0);
1071
1072 const uint32_t tile_el_scale = fmtl->bpb / tile_info->format_bpb;
1073 const uint32_t total_w_el = phys_slice0_sa->width / fmtl->bw;
1074 const uint32_t total_w_tl =
1075 isl_align_div(total_w_el * tile_el_scale,
1076 tile_info->logical_extent_el.width);
1077
1078 assert(alignment == tile_info->phys_extent_B.width);
1079 return total_w_tl * tile_info->phys_extent_B.width;
1080 }
1081
1082 static uint32_t
1083 isl_calc_min_row_pitch(const struct isl_device *dev,
1084 const struct isl_surf_init_info *surf_info,
1085 const struct isl_tile_info *tile_info,
1086 const struct isl_extent2d *phys_slice0_sa,
1087 uint32_t alignment)
1088 {
1089 if (tile_info->tiling == ISL_TILING_LINEAR) {
1090 return isl_calc_linear_min_row_pitch(dev, surf_info, phys_slice0_sa,
1091 alignment);
1092 } else {
1093 return isl_calc_tiled_min_row_pitch(dev, surf_info, tile_info,
1094 phys_slice0_sa, alignment);
1095 }
1096 }
1097
1098 /**
1099 * Is `pitch` in the valid range for a hardware bitfield, if the bitfield's
1100 * size is `bits` bits?
1101 *
1102 * Hardware pitch fields are offset by 1. For example, if the size of
1103 * RENDER_SURFACE_STATE::SurfacePitch is B bits, then the range of valid
1104 * pitches is [1, 2^b] inclusive. If the surface pitch is N, then
1105 * RENDER_SURFACE_STATE::SurfacePitch must be set to N-1.
1106 */
1107 static bool
1108 pitch_in_range(uint32_t n, uint32_t bits)
1109 {
1110 assert(n != 0);
1111 return likely(bits != 0 && 1 <= n && n <= (1 << bits));
1112 }
1113
1114 static bool
1115 isl_calc_row_pitch(const struct isl_device *dev,
1116 const struct isl_surf_init_info *surf_info,
1117 const struct isl_tile_info *tile_info,
1118 enum isl_dim_layout dim_layout,
1119 const struct isl_extent2d *phys_slice0_sa,
1120 uint32_t *out_row_pitch)
1121 {
1122 const uint32_t alignment =
1123 isl_calc_row_pitch_alignment(surf_info, tile_info);
1124
1125 const uint32_t min_row_pitch =
1126 isl_calc_min_row_pitch(dev, surf_info, tile_info, phys_slice0_sa,
1127 alignment);
1128
1129 uint32_t row_pitch = min_row_pitch;
1130
1131 if (surf_info->row_pitch != 0) {
1132 row_pitch = surf_info->row_pitch;
1133
1134 if (row_pitch < min_row_pitch)
1135 return false;
1136
1137 if (row_pitch % alignment != 0)
1138 return false;
1139 }
1140
1141 const uint32_t row_pitch_tiles = row_pitch / tile_info->phys_extent_B.width;
1142
1143 if (row_pitch == 0)
1144 return false;
1145
1146 if (dim_layout == ISL_DIM_LAYOUT_GEN9_1D) {
1147 /* SurfacePitch is ignored for this layout. */
1148 goto done;
1149 }
1150
1151 if ((surf_info->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
1152 ISL_SURF_USAGE_TEXTURE_BIT |
1153 ISL_SURF_USAGE_STORAGE_BIT)) &&
1154 !pitch_in_range(row_pitch, RENDER_SURFACE_STATE_SurfacePitch_bits(dev->info)))
1155 return false;
1156
1157 if ((surf_info->usage & (ISL_SURF_USAGE_CCS_BIT |
1158 ISL_SURF_USAGE_MCS_BIT)) &&
1159 !pitch_in_range(row_pitch_tiles, RENDER_SURFACE_STATE_AuxiliarySurfacePitch_bits(dev->info)))
1160 return false;
1161
1162 if ((surf_info->usage & ISL_SURF_USAGE_DEPTH_BIT) &&
1163 !pitch_in_range(row_pitch, _3DSTATE_DEPTH_BUFFER_SurfacePitch_bits(dev->info)))
1164 return false;
1165
1166 if ((surf_info->usage & ISL_SURF_USAGE_HIZ_BIT) &&
1167 !pitch_in_range(row_pitch, _3DSTATE_HIER_DEPTH_BUFFER_SurfacePitch_bits(dev->info)))
1168 return false;
1169
1170 if (surf_info->usage & ISL_SURF_USAGE_STENCIL_BIT)
1171 isl_finishme("validate row pitch of stencil surfaces");
1172
1173 done:
1174 *out_row_pitch = row_pitch;
1175 return true;
1176 }
1177
1178 /**
1179 * Calculate and apply any padding required for the surface.
1180 *
1181 * @param[inout] total_h_el is updated with the new height
1182 * @param[out] pad_bytes is overwritten with additional padding requirements.
1183 */
1184 static void
1185 isl_apply_surface_padding(const struct isl_device *dev,
1186 const struct isl_surf_init_info *restrict info,
1187 const struct isl_tile_info *tile_info,
1188 uint32_t *total_h_el,
1189 uint32_t *pad_bytes)
1190 {
1191 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1192
1193 *pad_bytes = 0;
1194
1195 /* From the Broadwell PRM >> Volume 5: Memory Views >> Common Surface
1196 * Formats >> Surface Padding Requirements >> Render Target and Media
1197 * Surfaces:
1198 *
1199 * The data port accesses data (pixels) outside of the surface if they
1200 * are contained in the same cache request as pixels that are within the
1201 * surface. These pixels will not be returned by the requesting message,
1202 * however if these pixels lie outside of defined pages in the GTT,
1203 * a GTT error will result when the cache request is processed. In
1204 * order to avoid these GTT errors, “padding” at the bottom of the
1205 * surface is sometimes necessary.
1206 *
1207 * From the Broadwell PRM >> Volume 5: Memory Views >> Common Surface
1208 * Formats >> Surface Padding Requirements >> Sampling Engine Surfaces:
1209 *
1210 * ... Lots of padding requirements, all listed separately below.
1211 */
1212
1213 /* We can safely ignore the first padding requirement, quoted below,
1214 * because isl doesn't do buffers.
1215 *
1216 * - [pre-BDW] For buffers, which have no inherent “height,” padding
1217 * requirements are different. A buffer must be padded to the next
1218 * multiple of 256 array elements, with an additional 16 bytes added
1219 * beyond that to account for the L1 cache line.
1220 */
1221
1222 /*
1223 * - For compressed textures [...], padding at the bottom of the surface
1224 * is to an even compressed row.
1225 */
1226 if (isl_format_is_compressed(info->format))
1227 *total_h_el = isl_align(*total_h_el, 2);
1228
1229 /*
1230 * - For cube surfaces, an additional two rows of padding are required
1231 * at the bottom of the surface.
1232 */
1233 if (info->usage & ISL_SURF_USAGE_CUBE_BIT)
1234 *total_h_el += 2;
1235
1236 /*
1237 * - For packed YUV, 96 bpt, 48 bpt, and 24 bpt surface formats,
1238 * additional padding is required. These surfaces require an extra row
1239 * plus 16 bytes of padding at the bottom in addition to the general
1240 * padding requirements.
1241 */
1242 if (isl_format_is_yuv(info->format) &&
1243 (fmtl->bpb == 96 || fmtl->bpb == 48|| fmtl->bpb == 24)) {
1244 *total_h_el += 1;
1245 *pad_bytes += 16;
1246 }
1247
1248 /*
1249 * - For linear surfaces, additional padding of 64 bytes is required at
1250 * the bottom of the surface. This is in addition to the padding
1251 * required above.
1252 */
1253 if (tile_info->tiling == ISL_TILING_LINEAR)
1254 *pad_bytes += 64;
1255
1256 /* The below text weakens, not strengthens, the padding requirements for
1257 * linear surfaces. Therefore we can safely ignore it.
1258 *
1259 * - [BDW+] For SURFTYPE_BUFFER, SURFTYPE_1D, and SURFTYPE_2D non-array,
1260 * non-MSAA, non-mip-mapped surfaces in linear memory, the only
1261 * padding requirement is to the next aligned 64-byte boundary beyond
1262 * the end of the surface. The rest of the padding requirements
1263 * documented above do not apply to these surfaces.
1264 */
1265
1266 /*
1267 * - [SKL+] For SURFTYPE_2D and SURFTYPE_3D with linear mode and
1268 * height % 4 != 0, the surface must be padded with
1269 * 4-(height % 4)*Surface Pitch # of bytes.
1270 */
1271 if (ISL_DEV_GEN(dev) >= 9 &&
1272 tile_info->tiling == ISL_TILING_LINEAR &&
1273 (info->dim == ISL_SURF_DIM_2D || info->dim == ISL_SURF_DIM_3D)) {
1274 *total_h_el = isl_align(*total_h_el, 4);
1275 }
1276
1277 /*
1278 * - [SKL+] For SURFTYPE_1D with linear mode, the surface must be padded
1279 * to 4 times the Surface Pitch # of bytes
1280 */
1281 if (ISL_DEV_GEN(dev) >= 9 &&
1282 tile_info->tiling == ISL_TILING_LINEAR &&
1283 info->dim == ISL_SURF_DIM_1D) {
1284 *total_h_el += 4;
1285 }
1286 }
1287
1288 bool
1289 isl_surf_init_s(const struct isl_device *dev,
1290 struct isl_surf *surf,
1291 const struct isl_surf_init_info *restrict info)
1292 {
1293 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1294
1295 const struct isl_extent4d logical_level0_px = {
1296 .w = info->width,
1297 .h = info->height,
1298 .d = info->depth,
1299 .a = info->array_len,
1300 };
1301
1302 enum isl_tiling tiling;
1303 if (!isl_surf_choose_tiling(dev, info, &tiling))
1304 return false;
1305
1306 struct isl_tile_info tile_info;
1307 isl_tiling_get_info(tiling, fmtl->bpb, &tile_info);
1308
1309 const enum isl_dim_layout dim_layout =
1310 isl_surf_choose_dim_layout(dev, info->dim, tiling);
1311
1312 enum isl_msaa_layout msaa_layout;
1313 if (!isl_choose_msaa_layout(dev, info, tiling, &msaa_layout))
1314 return false;
1315
1316 struct isl_extent3d image_align_el;
1317 isl_choose_image_alignment_el(dev, info, tiling, dim_layout, msaa_layout,
1318 &image_align_el);
1319
1320 struct isl_extent3d image_align_sa =
1321 isl_extent3d_el_to_sa(info->format, image_align_el);
1322
1323 struct isl_extent4d phys_level0_sa;
1324 isl_calc_phys_level0_extent_sa(dev, info, dim_layout, tiling, msaa_layout,
1325 &phys_level0_sa);
1326 assert(phys_level0_sa.w % fmtl->bw == 0);
1327 assert(phys_level0_sa.h % fmtl->bh == 0);
1328
1329 enum isl_array_pitch_span array_pitch_span =
1330 isl_choose_array_pitch_span(dev, info, dim_layout, &phys_level0_sa);
1331
1332 struct isl_extent2d phys_slice0_sa;
1333 isl_calc_phys_slice0_extent_sa(dev, info, dim_layout, msaa_layout,
1334 &image_align_sa, &phys_level0_sa,
1335 &phys_slice0_sa);
1336 assert(phys_slice0_sa.w % fmtl->bw == 0);
1337 assert(phys_slice0_sa.h % fmtl->bh == 0);
1338
1339 const uint32_t array_pitch_el_rows =
1340 isl_calc_array_pitch_el_rows(dev, info, &tile_info, dim_layout,
1341 array_pitch_span, &image_align_sa,
1342 &phys_level0_sa, &phys_slice0_sa);
1343
1344 uint32_t total_h_el = phys_level0_sa.array_len * array_pitch_el_rows;
1345
1346 uint32_t pad_bytes;
1347 isl_apply_surface_padding(dev, info, &tile_info, &total_h_el, &pad_bytes);
1348
1349 uint32_t row_pitch;
1350 if (!isl_calc_row_pitch(dev, info, &tile_info, dim_layout,
1351 &phys_slice0_sa, &row_pitch))
1352 return false;
1353
1354 uint32_t size, base_alignment;
1355 if (tiling == ISL_TILING_LINEAR) {
1356 size = row_pitch * total_h_el + pad_bytes;
1357
1358 /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfaceBaseAddress:
1359 *
1360 * "The Base Address for linear render target surfaces and surfaces
1361 * accessed with the typed surface read/write data port messages must
1362 * be element-size aligned, for non-YUV surface formats, or a
1363 * multiple of 2 element-sizes for YUV surface formats. Other linear
1364 * surfaces have no alignment requirements (byte alignment is
1365 * sufficient.)"
1366 */
1367 base_alignment = MAX(1, info->min_alignment);
1368 if (info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1369 if (isl_format_is_yuv(info->format)) {
1370 base_alignment = MAX(base_alignment, fmtl->bpb / 4);
1371 } else {
1372 base_alignment = MAX(base_alignment, fmtl->bpb / 8);
1373 }
1374 }
1375 base_alignment = isl_round_up_to_power_of_two(base_alignment);
1376 } else {
1377 total_h_el += isl_align_div_npot(pad_bytes, row_pitch);
1378 const uint32_t total_h_tl =
1379 isl_align_div(total_h_el, tile_info.logical_extent_el.height);
1380
1381 size = total_h_tl * tile_info.phys_extent_B.height * row_pitch;
1382
1383 const uint32_t tile_size = tile_info.phys_extent_B.width *
1384 tile_info.phys_extent_B.height;
1385 assert(isl_is_pow2(info->min_alignment) && isl_is_pow2(tile_size));
1386 base_alignment = MAX(info->min_alignment, tile_size);
1387 }
1388
1389 *surf = (struct isl_surf) {
1390 .dim = info->dim,
1391 .dim_layout = dim_layout,
1392 .msaa_layout = msaa_layout,
1393 .tiling = tiling,
1394 .format = info->format,
1395
1396 .levels = info->levels,
1397 .samples = info->samples,
1398
1399 .image_alignment_el = image_align_el,
1400 .logical_level0_px = logical_level0_px,
1401 .phys_level0_sa = phys_level0_sa,
1402
1403 .size = size,
1404 .alignment = base_alignment,
1405 .row_pitch = row_pitch,
1406 .array_pitch_el_rows = array_pitch_el_rows,
1407 .array_pitch_span = array_pitch_span,
1408
1409 .usage = info->usage,
1410 };
1411
1412 return true;
1413 }
1414
1415 void
1416 isl_surf_get_tile_info(const struct isl_surf *surf,
1417 struct isl_tile_info *tile_info)
1418 {
1419 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1420 isl_tiling_get_info(surf->tiling, fmtl->bpb, tile_info);
1421 }
1422
1423 bool
1424 isl_surf_get_hiz_surf(const struct isl_device *dev,
1425 const struct isl_surf *surf,
1426 struct isl_surf *hiz_surf)
1427 {
1428 assert(ISL_DEV_GEN(dev) >= 5 && ISL_DEV_USE_SEPARATE_STENCIL(dev));
1429
1430 /* Multisampled depth is always interleaved */
1431 assert(surf->msaa_layout == ISL_MSAA_LAYOUT_NONE ||
1432 surf->msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1433
1434 /* From the Broadwell PRM Vol. 7, "Hierarchical Depth Buffer":
1435 *
1436 * "The Surface Type, Height, Width, Depth, Minimum Array Element, Render
1437 * Target View Extent, and Depth Coordinate Offset X/Y of the
1438 * hierarchical depth buffer are inherited from the depth buffer. The
1439 * height and width of the hierarchical depth buffer that must be
1440 * allocated are computed by the following formulas, where HZ is the
1441 * hierarchical depth buffer and Z is the depth buffer. The Z_Height,
1442 * Z_Width, and Z_Depth values given in these formulas are those present
1443 * in 3DSTATE_DEPTH_BUFFER incremented by one.
1444 *
1445 * "The value of Z_Height and Z_Width must each be multiplied by 2 before
1446 * being applied to the table below if Number of Multisamples is set to
1447 * NUMSAMPLES_4. The value of Z_Height must be multiplied by 2 and
1448 * Z_Width must be multiplied by 4 before being applied to the table
1449 * below if Number of Multisamples is set to NUMSAMPLES_8."
1450 *
1451 * In the Sky Lake PRM, the second paragraph is replaced with this:
1452 *
1453 * "The Z_Height and Z_Width values must equal those present in
1454 * 3DSTATE_DEPTH_BUFFER incremented by one."
1455 *
1456 * In other words, on Sandy Bridge through Broadwell, each 128-bit HiZ
1457 * block corresponds to a region of 8x4 samples in the primary depth
1458 * surface. On Sky Lake, on the other hand, each HiZ block corresponds to
1459 * a region of 8x4 pixels in the primary depth surface regardless of the
1460 * number of samples. The dimensions of a HiZ block in both pixels and
1461 * samples are given in the table below:
1462 *
1463 * | SNB - BDW | SKL+
1464 * ------+-----------+-------------
1465 * 1x | 8 x 4 sa | 8 x 4 sa
1466 * MSAA | 8 x 4 px | 8 x 4 px
1467 * ------+-----------+-------------
1468 * 2x | 8 x 4 sa | 16 x 4 sa
1469 * MSAA | 4 x 4 px | 8 x 4 px
1470 * ------+-----------+-------------
1471 * 4x | 8 x 4 sa | 16 x 8 sa
1472 * MSAA | 4 x 2 px | 8 x 4 px
1473 * ------+-----------+-------------
1474 * 8x | 8 x 4 sa | 32 x 8 sa
1475 * MSAA | 2 x 2 px | 8 x 4 px
1476 * ------+-----------+-------------
1477 * 16x | N/A | 32 x 16 sa
1478 * MSAA | N/A | 8 x 4 px
1479 * ------+-----------+-------------
1480 *
1481 * There are a number of different ways that this discrepency could be
1482 * handled. The way we have chosen is to simply make MSAA HiZ have the
1483 * same number of samples as the parent surface pre-Sky Lake and always be
1484 * single-sampled on Sky Lake and above. Since the block sizes of
1485 * compressed formats are given in samples, this neatly handles everything
1486 * without the need for additional HiZ formats with different block sizes
1487 * on SKL+.
1488 */
1489 const unsigned samples = ISL_DEV_GEN(dev) >= 9 ? 1 : surf->samples;
1490
1491 return isl_surf_init(dev, hiz_surf,
1492 .dim = surf->dim,
1493 .format = ISL_FORMAT_HIZ,
1494 .width = surf->logical_level0_px.width,
1495 .height = surf->logical_level0_px.height,
1496 .depth = surf->logical_level0_px.depth,
1497 .levels = surf->levels,
1498 .array_len = surf->logical_level0_px.array_len,
1499 .samples = samples,
1500 .usage = ISL_SURF_USAGE_HIZ_BIT,
1501 .tiling_flags = ISL_TILING_HIZ_BIT);
1502 }
1503
1504 bool
1505 isl_surf_get_mcs_surf(const struct isl_device *dev,
1506 const struct isl_surf *surf,
1507 struct isl_surf *mcs_surf)
1508 {
1509 /* It must be multisampled with an array layout */
1510 assert(surf->samples > 1 && surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
1511
1512 /* The following are true of all multisampled surfaces */
1513 assert(surf->dim == ISL_SURF_DIM_2D);
1514 assert(surf->levels == 1);
1515 assert(surf->logical_level0_px.depth == 1);
1516
1517 /* The "Auxiliary Surface Pitch" field in RENDER_SURFACE_STATE is only 9
1518 * bits which means the maximum pitch of a compression surface is 512
1519 * tiles or 64KB (since MCS is always Y-tiled). Since a 16x MCS buffer is
1520 * 64bpp, this gives us a maximum width of 8192 pixels. We can create
1521 * larger multisampled surfaces, we just can't compress them. For 2x, 4x,
1522 * and 8x, we have enough room for the full 16k supported by the hardware.
1523 */
1524 if (surf->samples == 16 && surf->logical_level0_px.width > 8192)
1525 return false;
1526
1527 enum isl_format mcs_format;
1528 switch (surf->samples) {
1529 case 2: mcs_format = ISL_FORMAT_MCS_2X; break;
1530 case 4: mcs_format = ISL_FORMAT_MCS_4X; break;
1531 case 8: mcs_format = ISL_FORMAT_MCS_8X; break;
1532 case 16: mcs_format = ISL_FORMAT_MCS_16X; break;
1533 default:
1534 unreachable("Invalid sample count");
1535 }
1536
1537 return isl_surf_init(dev, mcs_surf,
1538 .dim = ISL_SURF_DIM_2D,
1539 .format = mcs_format,
1540 .width = surf->logical_level0_px.width,
1541 .height = surf->logical_level0_px.height,
1542 .depth = 1,
1543 .levels = 1,
1544 .array_len = surf->logical_level0_px.array_len,
1545 .samples = 1, /* MCS surfaces are really single-sampled */
1546 .usage = ISL_SURF_USAGE_MCS_BIT,
1547 .tiling_flags = ISL_TILING_Y0_BIT);
1548 }
1549
1550 bool
1551 isl_surf_get_ccs_surf(const struct isl_device *dev,
1552 const struct isl_surf *surf,
1553 struct isl_surf *ccs_surf)
1554 {
1555 assert(surf->samples == 1 && surf->msaa_layout == ISL_MSAA_LAYOUT_NONE);
1556 assert(ISL_DEV_GEN(dev) >= 7);
1557
1558 if (surf->usage & ISL_SURF_USAGE_DISABLE_AUX_BIT)
1559 return false;
1560
1561 if (ISL_DEV_GEN(dev) <= 8 && surf->dim != ISL_SURF_DIM_2D)
1562 return false;
1563
1564 if (isl_format_is_compressed(surf->format))
1565 return false;
1566
1567 /* TODO: More conditions where it can fail. */
1568
1569 enum isl_format ccs_format;
1570 if (ISL_DEV_GEN(dev) >= 9) {
1571 if (!isl_tiling_is_any_y(surf->tiling))
1572 return false;
1573
1574 switch (isl_format_get_layout(surf->format)->bpb) {
1575 case 32: ccs_format = ISL_FORMAT_GEN9_CCS_32BPP; break;
1576 case 64: ccs_format = ISL_FORMAT_GEN9_CCS_64BPP; break;
1577 case 128: ccs_format = ISL_FORMAT_GEN9_CCS_128BPP; break;
1578 default:
1579 return false;
1580 }
1581 } else if (surf->tiling == ISL_TILING_Y0) {
1582 switch (isl_format_get_layout(surf->format)->bpb) {
1583 case 32: ccs_format = ISL_FORMAT_GEN7_CCS_32BPP_Y; break;
1584 case 64: ccs_format = ISL_FORMAT_GEN7_CCS_64BPP_Y; break;
1585 case 128: ccs_format = ISL_FORMAT_GEN7_CCS_128BPP_Y; break;
1586 default:
1587 return false;
1588 }
1589 } else if (surf->tiling == ISL_TILING_X) {
1590 switch (isl_format_get_layout(surf->format)->bpb) {
1591 case 32: ccs_format = ISL_FORMAT_GEN7_CCS_32BPP_X; break;
1592 case 64: ccs_format = ISL_FORMAT_GEN7_CCS_64BPP_X; break;
1593 case 128: ccs_format = ISL_FORMAT_GEN7_CCS_128BPP_X; break;
1594 default:
1595 return false;
1596 }
1597 } else {
1598 return false;
1599 }
1600
1601 return isl_surf_init(dev, ccs_surf,
1602 .dim = surf->dim,
1603 .format = ccs_format,
1604 .width = surf->logical_level0_px.width,
1605 .height = surf->logical_level0_px.height,
1606 .depth = surf->logical_level0_px.depth,
1607 .levels = surf->levels,
1608 .array_len = surf->logical_level0_px.array_len,
1609 .samples = 1,
1610 .usage = ISL_SURF_USAGE_CCS_BIT,
1611 .tiling_flags = ISL_TILING_CCS_BIT);
1612 }
1613
1614 void
1615 isl_surf_fill_state_s(const struct isl_device *dev, void *state,
1616 const struct isl_surf_fill_state_info *restrict info)
1617 {
1618 #ifndef NDEBUG
1619 isl_surf_usage_flags_t _base_usage =
1620 info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
1621 ISL_SURF_USAGE_TEXTURE_BIT |
1622 ISL_SURF_USAGE_STORAGE_BIT);
1623 /* They may only specify one of the above bits at a time */
1624 assert(__builtin_popcount(_base_usage) == 1);
1625 /* The only other allowed bit is ISL_SURF_USAGE_CUBE_BIT */
1626 assert((info->view->usage & ~ISL_SURF_USAGE_CUBE_BIT) == _base_usage);
1627 #endif
1628
1629 if (info->surf->dim == ISL_SURF_DIM_3D) {
1630 assert(info->view->base_array_layer + info->view->array_len <=
1631 info->surf->logical_level0_px.depth);
1632 } else {
1633 assert(info->view->base_array_layer + info->view->array_len <=
1634 info->surf->logical_level0_px.array_len);
1635 }
1636
1637 switch (ISL_DEV_GEN(dev)) {
1638 case 4:
1639 if (ISL_DEV_IS_G4X(dev)) {
1640 /* G45 surface state is the same as gen5 */
1641 isl_gen5_surf_fill_state_s(dev, state, info);
1642 } else {
1643 isl_gen4_surf_fill_state_s(dev, state, info);
1644 }
1645 break;
1646 case 5:
1647 isl_gen5_surf_fill_state_s(dev, state, info);
1648 break;
1649 case 6:
1650 isl_gen6_surf_fill_state_s(dev, state, info);
1651 break;
1652 case 7:
1653 if (ISL_DEV_IS_HASWELL(dev)) {
1654 isl_gen75_surf_fill_state_s(dev, state, info);
1655 } else {
1656 isl_gen7_surf_fill_state_s(dev, state, info);
1657 }
1658 break;
1659 case 8:
1660 isl_gen8_surf_fill_state_s(dev, state, info);
1661 break;
1662 case 9:
1663 isl_gen9_surf_fill_state_s(dev, state, info);
1664 break;
1665 default:
1666 assert(!"Cannot fill surface state for this gen");
1667 }
1668 }
1669
1670 void
1671 isl_buffer_fill_state_s(const struct isl_device *dev, void *state,
1672 const struct isl_buffer_fill_state_info *restrict info)
1673 {
1674 switch (ISL_DEV_GEN(dev)) {
1675 case 4:
1676 case 5:
1677 /* Gen 4-5 are all the same when it comes to buffer surfaces */
1678 isl_gen5_buffer_fill_state_s(state, info);
1679 break;
1680 case 6:
1681 isl_gen6_buffer_fill_state_s(state, info);
1682 break;
1683 case 7:
1684 if (ISL_DEV_IS_HASWELL(dev)) {
1685 isl_gen75_buffer_fill_state_s(state, info);
1686 } else {
1687 isl_gen7_buffer_fill_state_s(state, info);
1688 }
1689 break;
1690 case 8:
1691 isl_gen8_buffer_fill_state_s(state, info);
1692 break;
1693 case 9:
1694 isl_gen9_buffer_fill_state_s(state, info);
1695 break;
1696 default:
1697 assert(!"Cannot fill surface state for this gen");
1698 }
1699 }
1700
1701 void
1702 isl_emit_depth_stencil_hiz_s(const struct isl_device *dev, void *batch,
1703 const struct isl_depth_stencil_hiz_emit_info *restrict info)
1704 {
1705 if (info->depth_surf && info->stencil_surf) {
1706 if (!dev->info->has_hiz_and_separate_stencil) {
1707 assert(info->depth_surf == info->stencil_surf);
1708 assert(info->depth_address == info->stencil_address);
1709 }
1710 assert(info->depth_surf->dim == info->stencil_surf->dim);
1711 }
1712
1713 if (info->depth_surf) {
1714 assert((info->depth_surf->usage & ISL_SURF_USAGE_DEPTH_BIT));
1715 if (info->depth_surf->dim == ISL_SURF_DIM_3D) {
1716 assert(info->view->base_array_layer + info->view->array_len <=
1717 info->depth_surf->logical_level0_px.depth);
1718 } else {
1719 assert(info->view->base_array_layer + info->view->array_len <=
1720 info->depth_surf->logical_level0_px.array_len);
1721 }
1722 }
1723
1724 if (info->stencil_surf) {
1725 assert((info->stencil_surf->usage & ISL_SURF_USAGE_STENCIL_BIT));
1726 if (info->stencil_surf->dim == ISL_SURF_DIM_3D) {
1727 assert(info->view->base_array_layer + info->view->array_len <=
1728 info->stencil_surf->logical_level0_px.depth);
1729 } else {
1730 assert(info->view->base_array_layer + info->view->array_len <=
1731 info->stencil_surf->logical_level0_px.array_len);
1732 }
1733 }
1734
1735 switch (ISL_DEV_GEN(dev)) {
1736 case 4:
1737 if (ISL_DEV_IS_G4X(dev)) {
1738 /* G45 surface state is the same as gen5 */
1739 isl_gen5_emit_depth_stencil_hiz_s(dev, batch, info);
1740 } else {
1741 isl_gen4_emit_depth_stencil_hiz_s(dev, batch, info);
1742 }
1743 break;
1744 case 5:
1745 isl_gen5_emit_depth_stencil_hiz_s(dev, batch, info);
1746 break;
1747 case 6:
1748 isl_gen6_emit_depth_stencil_hiz_s(dev, batch, info);
1749 break;
1750 case 7:
1751 if (ISL_DEV_IS_HASWELL(dev)) {
1752 isl_gen75_emit_depth_stencil_hiz_s(dev, batch, info);
1753 } else {
1754 isl_gen7_emit_depth_stencil_hiz_s(dev, batch, info);
1755 }
1756 break;
1757 case 8:
1758 isl_gen8_emit_depth_stencil_hiz_s(dev, batch, info);
1759 break;
1760 case 9:
1761 isl_gen9_emit_depth_stencil_hiz_s(dev, batch, info);
1762 break;
1763 default:
1764 assert(!"Cannot fill surface state for this gen");
1765 }
1766 }
1767
1768 /**
1769 * A variant of isl_surf_get_image_offset_sa() specific to
1770 * ISL_DIM_LAYOUT_GEN4_2D.
1771 */
1772 static void
1773 get_image_offset_sa_gen4_2d(const struct isl_surf *surf,
1774 uint32_t level, uint32_t logical_array_layer,
1775 uint32_t *x_offset_sa,
1776 uint32_t *y_offset_sa)
1777 {
1778 assert(level < surf->levels);
1779 if (surf->dim == ISL_SURF_DIM_3D)
1780 assert(logical_array_layer < surf->logical_level0_px.depth);
1781 else
1782 assert(logical_array_layer < surf->logical_level0_px.array_len);
1783
1784 const struct isl_extent3d image_align_sa =
1785 isl_surf_get_image_alignment_sa(surf);
1786
1787 const uint32_t W0 = surf->phys_level0_sa.width;
1788 const uint32_t H0 = surf->phys_level0_sa.height;
1789
1790 const uint32_t phys_layer = logical_array_layer *
1791 (surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY ? surf->samples : 1);
1792
1793 uint32_t x = 0;
1794 uint32_t y = phys_layer * isl_surf_get_array_pitch_sa_rows(surf);
1795
1796 for (uint32_t l = 0; l < level; ++l) {
1797 if (l == 1) {
1798 uint32_t W = isl_minify(W0, l);
1799 x += isl_align_npot(W, image_align_sa.w);
1800 } else {
1801 uint32_t H = isl_minify(H0, l);
1802 y += isl_align_npot(H, image_align_sa.h);
1803 }
1804 }
1805
1806 *x_offset_sa = x;
1807 *y_offset_sa = y;
1808 }
1809
1810 /**
1811 * A variant of isl_surf_get_image_offset_sa() specific to
1812 * ISL_DIM_LAYOUT_GEN4_3D.
1813 */
1814 static void
1815 get_image_offset_sa_gen4_3d(const struct isl_surf *surf,
1816 uint32_t level, uint32_t logical_z_offset_px,
1817 uint32_t *x_offset_sa,
1818 uint32_t *y_offset_sa)
1819 {
1820 assert(level < surf->levels);
1821 if (surf->dim == ISL_SURF_DIM_3D) {
1822 assert(surf->phys_level0_sa.array_len == 1);
1823 assert(logical_z_offset_px < isl_minify(surf->phys_level0_sa.depth, level));
1824 } else {
1825 assert(surf->dim == ISL_SURF_DIM_2D);
1826 assert(surf->usage & ISL_SURF_USAGE_CUBE_BIT);
1827 assert(surf->phys_level0_sa.array_len == 6);
1828 assert(logical_z_offset_px < surf->phys_level0_sa.array_len);
1829 }
1830
1831 const struct isl_extent3d image_align_sa =
1832 isl_surf_get_image_alignment_sa(surf);
1833
1834 const uint32_t W0 = surf->phys_level0_sa.width;
1835 const uint32_t H0 = surf->phys_level0_sa.height;
1836 const uint32_t D0 = surf->phys_level0_sa.depth;
1837 const uint32_t AL = surf->phys_level0_sa.array_len;
1838
1839 uint32_t x = 0;
1840 uint32_t y = 0;
1841
1842 for (uint32_t l = 0; l < level; ++l) {
1843 const uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa.h);
1844 const uint32_t level_d =
1845 isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, l) : AL,
1846 image_align_sa.d);
1847 const uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
1848
1849 y += level_h * max_layers_vert;
1850 }
1851
1852 const uint32_t level_w = isl_align_npot(isl_minify(W0, level), image_align_sa.w);
1853 const uint32_t level_h = isl_align_npot(isl_minify(H0, level), image_align_sa.h);
1854 const uint32_t level_d =
1855 isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, level) : AL,
1856 image_align_sa.d);
1857
1858 const uint32_t max_layers_horiz = MIN(level_d, 1u << level);
1859
1860 x += level_w * (logical_z_offset_px % max_layers_horiz);
1861 y += level_h * (logical_z_offset_px / max_layers_horiz);
1862
1863 *x_offset_sa = x;
1864 *y_offset_sa = y;
1865 }
1866
1867 /**
1868 * A variant of isl_surf_get_image_offset_sa() specific to
1869 * ISL_DIM_LAYOUT_GEN9_1D.
1870 */
1871 static void
1872 get_image_offset_sa_gen9_1d(const struct isl_surf *surf,
1873 uint32_t level, uint32_t layer,
1874 uint32_t *x_offset_sa,
1875 uint32_t *y_offset_sa)
1876 {
1877 assert(level < surf->levels);
1878 assert(layer < surf->phys_level0_sa.array_len);
1879 assert(surf->phys_level0_sa.height == 1);
1880 assert(surf->phys_level0_sa.depth == 1);
1881 assert(surf->samples == 1);
1882
1883 const uint32_t W0 = surf->phys_level0_sa.width;
1884 const struct isl_extent3d image_align_sa =
1885 isl_surf_get_image_alignment_sa(surf);
1886
1887 uint32_t x = 0;
1888
1889 for (uint32_t l = 0; l < level; ++l) {
1890 uint32_t W = isl_minify(W0, l);
1891 uint32_t w = isl_align_npot(W, image_align_sa.w);
1892
1893 x += w;
1894 }
1895
1896 *x_offset_sa = x;
1897 *y_offset_sa = layer * isl_surf_get_array_pitch_sa_rows(surf);
1898 }
1899
1900 /**
1901 * Calculate the offset, in units of surface samples, to a subimage in the
1902 * surface.
1903 *
1904 * @invariant level < surface levels
1905 * @invariant logical_array_layer < logical array length of surface
1906 * @invariant logical_z_offset_px < logical depth of surface at level
1907 */
1908 void
1909 isl_surf_get_image_offset_sa(const struct isl_surf *surf,
1910 uint32_t level,
1911 uint32_t logical_array_layer,
1912 uint32_t logical_z_offset_px,
1913 uint32_t *x_offset_sa,
1914 uint32_t *y_offset_sa)
1915 {
1916 assert(level < surf->levels);
1917 assert(logical_array_layer < surf->logical_level0_px.array_len);
1918 assert(logical_z_offset_px
1919 < isl_minify(surf->logical_level0_px.depth, level));
1920
1921 switch (surf->dim_layout) {
1922 case ISL_DIM_LAYOUT_GEN9_1D:
1923 get_image_offset_sa_gen9_1d(surf, level, logical_array_layer,
1924 x_offset_sa, y_offset_sa);
1925 break;
1926 case ISL_DIM_LAYOUT_GEN4_2D:
1927 get_image_offset_sa_gen4_2d(surf, level, logical_array_layer
1928 + logical_z_offset_px,
1929 x_offset_sa, y_offset_sa);
1930 break;
1931 case ISL_DIM_LAYOUT_GEN4_3D:
1932 get_image_offset_sa_gen4_3d(surf, level, logical_array_layer +
1933 logical_z_offset_px,
1934 x_offset_sa, y_offset_sa);
1935 break;
1936
1937 default:
1938 unreachable("not reached");
1939 }
1940 }
1941
1942 void
1943 isl_surf_get_image_offset_el(const struct isl_surf *surf,
1944 uint32_t level,
1945 uint32_t logical_array_layer,
1946 uint32_t logical_z_offset_px,
1947 uint32_t *x_offset_el,
1948 uint32_t *y_offset_el)
1949 {
1950 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1951
1952 assert(level < surf->levels);
1953 assert(logical_array_layer < surf->logical_level0_px.array_len);
1954 assert(logical_z_offset_px
1955 < isl_minify(surf->logical_level0_px.depth, level));
1956
1957 uint32_t x_offset_sa, y_offset_sa;
1958 isl_surf_get_image_offset_sa(surf, level,
1959 logical_array_layer,
1960 logical_z_offset_px,
1961 &x_offset_sa,
1962 &y_offset_sa);
1963
1964 *x_offset_el = x_offset_sa / fmtl->bw;
1965 *y_offset_el = y_offset_sa / fmtl->bh;
1966 }
1967
1968 void
1969 isl_tiling_get_intratile_offset_el(enum isl_tiling tiling,
1970 uint8_t bs,
1971 uint32_t row_pitch,
1972 uint32_t total_x_offset_el,
1973 uint32_t total_y_offset_el,
1974 uint32_t *base_address_offset,
1975 uint32_t *x_offset_el,
1976 uint32_t *y_offset_el)
1977 {
1978 if (tiling == ISL_TILING_LINEAR) {
1979 *base_address_offset = total_y_offset_el * row_pitch +
1980 total_x_offset_el * bs;
1981 *x_offset_el = 0;
1982 *y_offset_el = 0;
1983 return;
1984 }
1985
1986 const uint32_t bpb = bs * 8;
1987
1988 struct isl_tile_info tile_info;
1989 isl_tiling_get_info(tiling, bpb, &tile_info);
1990
1991 assert(row_pitch % tile_info.phys_extent_B.width == 0);
1992
1993 /* For non-power-of-two formats, we need the address to be both tile and
1994 * element-aligned. The easiest way to achieve this is to work with a tile
1995 * that is three times as wide as the regular tile.
1996 *
1997 * The tile info returned by get_tile_info has a logical size that is an
1998 * integer number of tile_info.format_bpb size elements. To scale the
1999 * tile, we scale up the physical width and then treat the logical tile
2000 * size as if it has bpb size elements.
2001 */
2002 const uint32_t tile_el_scale = bpb / tile_info.format_bpb;
2003 tile_info.phys_extent_B.width *= tile_el_scale;
2004
2005 /* Compute the offset into the tile */
2006 *x_offset_el = total_x_offset_el % tile_info.logical_extent_el.w;
2007 *y_offset_el = total_y_offset_el % tile_info.logical_extent_el.h;
2008
2009 /* Compute the offset of the tile in units of whole tiles */
2010 uint32_t x_offset_tl = total_x_offset_el / tile_info.logical_extent_el.w;
2011 uint32_t y_offset_tl = total_y_offset_el / tile_info.logical_extent_el.h;
2012
2013 *base_address_offset =
2014 y_offset_tl * tile_info.phys_extent_B.h * row_pitch +
2015 x_offset_tl * tile_info.phys_extent_B.h * tile_info.phys_extent_B.w;
2016 }
2017
2018 uint32_t
2019 isl_surf_get_depth_format(const struct isl_device *dev,
2020 const struct isl_surf *surf)
2021 {
2022 /* Support for separate stencil buffers began in gen5. Support for
2023 * interleaved depthstencil buffers ceased in gen7. The intermediate gens,
2024 * those that supported separate and interleaved stencil, were gen5 and
2025 * gen6.
2026 *
2027 * For a list of all available formats, see the Sandybridge PRM >> Volume
2028 * 2 Part 1: 3D/Media - 3D Pipeline >> 3DSTATE_DEPTH_BUFFER >> Surface
2029 * Format (p321).
2030 */
2031
2032 bool has_stencil = surf->usage & ISL_SURF_USAGE_STENCIL_BIT;
2033
2034 assert(surf->usage & ISL_SURF_USAGE_DEPTH_BIT);
2035
2036 if (has_stencil)
2037 assert(ISL_DEV_GEN(dev) < 7);
2038
2039 switch (surf->format) {
2040 default:
2041 unreachable("bad isl depth format");
2042 case ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS:
2043 assert(ISL_DEV_GEN(dev) < 7);
2044 return 0; /* D32_FLOAT_S8X24_UINT */
2045 case ISL_FORMAT_R32_FLOAT:
2046 assert(!has_stencil);
2047 return 1; /* D32_FLOAT */
2048 case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
2049 if (has_stencil) {
2050 assert(ISL_DEV_GEN(dev) < 7);
2051 return 2; /* D24_UNORM_S8_UINT */
2052 } else {
2053 assert(ISL_DEV_GEN(dev) >= 5);
2054 return 3; /* D24_UNORM_X8_UINT */
2055 }
2056 case ISL_FORMAT_R16_UNORM:
2057 assert(!has_stencil);
2058 return 5; /* D16_UNORM */
2059 }
2060 }