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