isl: Reduce assertions during aux surf creation
[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_gen12.h"
37 #include "isl_priv.h"
38
39 void
40 isl_memcpy_linear_to_tiled(uint32_t xt1, uint32_t xt2,
41 uint32_t yt1, uint32_t yt2,
42 char *dst, const char *src,
43 uint32_t dst_pitch, int32_t src_pitch,
44 bool has_swizzling,
45 enum isl_tiling tiling,
46 isl_memcpy_type copy_type)
47 {
48 #ifdef USE_SSE41
49 if (copy_type == ISL_MEMCPY_STREAMING_LOAD) {
50 _isl_memcpy_linear_to_tiled_sse41(
51 xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
52 tiling, copy_type);
53 return;
54 }
55 #endif
56
57 _isl_memcpy_linear_to_tiled(
58 xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
59 tiling, copy_type);
60 }
61
62 void
63 isl_memcpy_tiled_to_linear(uint32_t xt1, uint32_t xt2,
64 uint32_t yt1, uint32_t yt2,
65 char *dst, const char *src,
66 int32_t dst_pitch, uint32_t src_pitch,
67 bool has_swizzling,
68 enum isl_tiling tiling,
69 isl_memcpy_type copy_type)
70 {
71 #ifdef USE_SSE41
72 if (copy_type == ISL_MEMCPY_STREAMING_LOAD) {
73 _isl_memcpy_tiled_to_linear_sse41(
74 xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
75 tiling, copy_type);
76 return;
77 }
78 #endif
79
80 _isl_memcpy_tiled_to_linear(
81 xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
82 tiling, copy_type);
83 }
84
85 void PRINTFLIKE(3, 4) UNUSED
86 __isl_finishme(const char *file, int line, const char *fmt, ...)
87 {
88 va_list ap;
89 char buf[512];
90
91 va_start(ap, fmt);
92 vsnprintf(buf, sizeof(buf), fmt, ap);
93 va_end(ap);
94
95 fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buf);
96 }
97
98 void
99 isl_device_init(struct isl_device *dev,
100 const struct gen_device_info *info,
101 bool has_bit6_swizzling)
102 {
103 /* Gen8+ don't have bit6 swizzling, ensure callsite is not confused. */
104 assert(!(has_bit6_swizzling && info->gen >= 8));
105
106 dev->info = info;
107 dev->use_separate_stencil = ISL_DEV_GEN(dev) >= 6;
108 dev->has_bit6_swizzling = has_bit6_swizzling;
109
110 /* The ISL_DEV macros may be defined in the CFLAGS, thus hardcoding some
111 * device properties at buildtime. Verify that the macros with the device
112 * properties chosen during runtime.
113 */
114 ISL_DEV_GEN_SANITIZE(dev);
115 ISL_DEV_USE_SEPARATE_STENCIL_SANITIZE(dev);
116
117 /* Did we break hiz or stencil? */
118 if (ISL_DEV_USE_SEPARATE_STENCIL(dev))
119 assert(info->has_hiz_and_separate_stencil);
120 if (info->must_use_separate_stencil)
121 assert(ISL_DEV_USE_SEPARATE_STENCIL(dev));
122
123 dev->ss.size = RENDER_SURFACE_STATE_length(info) * 4;
124 dev->ss.align = isl_align(dev->ss.size, 32);
125
126 dev->ss.clear_color_state_size =
127 isl_align(CLEAR_COLOR_length(info) * 4, 64);
128 dev->ss.clear_color_state_offset =
129 RENDER_SURFACE_STATE_ClearValueAddress_start(info) / 32 * 4;
130
131 dev->ss.clear_value_size =
132 isl_align(RENDER_SURFACE_STATE_RedClearColor_bits(info) +
133 RENDER_SURFACE_STATE_GreenClearColor_bits(info) +
134 RENDER_SURFACE_STATE_BlueClearColor_bits(info) +
135 RENDER_SURFACE_STATE_AlphaClearColor_bits(info), 32) / 8;
136
137 dev->ss.clear_value_offset =
138 RENDER_SURFACE_STATE_RedClearColor_start(info) / 32 * 4;
139
140 assert(RENDER_SURFACE_STATE_SurfaceBaseAddress_start(info) % 8 == 0);
141 dev->ss.addr_offset =
142 RENDER_SURFACE_STATE_SurfaceBaseAddress_start(info) / 8;
143
144 /* The "Auxiliary Surface Base Address" field starts a bit higher up
145 * because the bottom 12 bits are used for other things. Round down to
146 * the nearest dword before.
147 */
148 dev->ss.aux_addr_offset =
149 (RENDER_SURFACE_STATE_AuxiliarySurfaceBaseAddress_start(info) & ~31) / 8;
150
151 dev->ds.size = _3DSTATE_DEPTH_BUFFER_length(info) * 4;
152 assert(_3DSTATE_DEPTH_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
153 dev->ds.depth_offset =
154 _3DSTATE_DEPTH_BUFFER_SurfaceBaseAddress_start(info) / 8;
155
156 if (dev->use_separate_stencil) {
157 dev->ds.size += _3DSTATE_STENCIL_BUFFER_length(info) * 4 +
158 _3DSTATE_HIER_DEPTH_BUFFER_length(info) * 4 +
159 _3DSTATE_CLEAR_PARAMS_length(info) * 4;
160
161 assert(_3DSTATE_STENCIL_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
162 dev->ds.stencil_offset =
163 _3DSTATE_DEPTH_BUFFER_length(info) * 4 +
164 _3DSTATE_STENCIL_BUFFER_SurfaceBaseAddress_start(info) / 8;
165
166 assert(_3DSTATE_HIER_DEPTH_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
167 dev->ds.hiz_offset =
168 _3DSTATE_DEPTH_BUFFER_length(info) * 4 +
169 _3DSTATE_STENCIL_BUFFER_length(info) * 4 +
170 _3DSTATE_HIER_DEPTH_BUFFER_SurfaceBaseAddress_start(info) / 8;
171 } else {
172 dev->ds.stencil_offset = 0;
173 dev->ds.hiz_offset = 0;
174 }
175 }
176
177 /**
178 * @brief Query the set of multisamples supported by the device.
179 *
180 * This function always returns non-zero, as ISL_SAMPLE_COUNT_1_BIT is always
181 * supported.
182 */
183 isl_sample_count_mask_t ATTRIBUTE_CONST
184 isl_device_get_sample_counts(struct isl_device *dev)
185 {
186 if (ISL_DEV_GEN(dev) >= 9) {
187 return ISL_SAMPLE_COUNT_1_BIT |
188 ISL_SAMPLE_COUNT_2_BIT |
189 ISL_SAMPLE_COUNT_4_BIT |
190 ISL_SAMPLE_COUNT_8_BIT |
191 ISL_SAMPLE_COUNT_16_BIT;
192 } else if (ISL_DEV_GEN(dev) >= 8) {
193 return ISL_SAMPLE_COUNT_1_BIT |
194 ISL_SAMPLE_COUNT_2_BIT |
195 ISL_SAMPLE_COUNT_4_BIT |
196 ISL_SAMPLE_COUNT_8_BIT;
197 } else if (ISL_DEV_GEN(dev) >= 7) {
198 return ISL_SAMPLE_COUNT_1_BIT |
199 ISL_SAMPLE_COUNT_4_BIT |
200 ISL_SAMPLE_COUNT_8_BIT;
201 } else if (ISL_DEV_GEN(dev) >= 6) {
202 return ISL_SAMPLE_COUNT_1_BIT |
203 ISL_SAMPLE_COUNT_4_BIT;
204 } else {
205 return ISL_SAMPLE_COUNT_1_BIT;
206 }
207 }
208
209 /**
210 * @param[out] info is written only on success
211 */
212 static void
213 isl_tiling_get_info(enum isl_tiling tiling,
214 uint32_t format_bpb,
215 struct isl_tile_info *tile_info)
216 {
217 const uint32_t bs = format_bpb / 8;
218 struct isl_extent2d logical_el, phys_B;
219
220 if (tiling != ISL_TILING_LINEAR && !isl_is_pow2(format_bpb)) {
221 /* It is possible to have non-power-of-two formats in a tiled buffer.
222 * The easiest way to handle this is to treat the tile as if it is three
223 * times as wide. This way no pixel will ever cross a tile boundary.
224 * This really only works on legacy X and Y tiling formats.
225 */
226 assert(tiling == ISL_TILING_X || tiling == ISL_TILING_Y0);
227 assert(bs % 3 == 0 && isl_is_pow2(format_bpb / 3));
228 isl_tiling_get_info(tiling, format_bpb / 3, tile_info);
229 return;
230 }
231
232 switch (tiling) {
233 case ISL_TILING_LINEAR:
234 assert(bs > 0);
235 logical_el = isl_extent2d(1, 1);
236 phys_B = isl_extent2d(bs, 1);
237 break;
238
239 case ISL_TILING_X:
240 assert(bs > 0);
241 logical_el = isl_extent2d(512 / bs, 8);
242 phys_B = isl_extent2d(512, 8);
243 break;
244
245 case ISL_TILING_Y0:
246 assert(bs > 0);
247 logical_el = isl_extent2d(128 / bs, 32);
248 phys_B = isl_extent2d(128, 32);
249 break;
250
251 case ISL_TILING_W:
252 assert(bs == 1);
253 logical_el = isl_extent2d(64, 64);
254 /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfacePitch:
255 *
256 * "If the surface is a stencil buffer (and thus has Tile Mode set
257 * to TILEMODE_WMAJOR), the pitch must be set to 2x the value
258 * computed based on width, as the stencil buffer is stored with two
259 * rows interleaved."
260 *
261 * This, together with the fact that stencil buffers are referred to as
262 * being Y-tiled in the PRMs for older hardware implies that the
263 * physical size of a W-tile is actually the same as for a Y-tile.
264 */
265 phys_B = isl_extent2d(128, 32);
266 break;
267
268 case ISL_TILING_Yf:
269 case ISL_TILING_Ys: {
270 bool is_Ys = tiling == ISL_TILING_Ys;
271
272 assert(bs > 0);
273 unsigned width = 1 << (6 + (ffs(bs) / 2) + (2 * is_Ys));
274 unsigned height = 1 << (6 - (ffs(bs) / 2) + (2 * is_Ys));
275
276 logical_el = isl_extent2d(width / bs, height);
277 phys_B = isl_extent2d(width, height);
278 break;
279 }
280
281 case ISL_TILING_HIZ:
282 /* HiZ buffers are required to have ISL_FORMAT_HIZ which is an 8x4
283 * 128bpb format. The tiling has the same physical dimensions as
284 * Y-tiling but actually has two HiZ columns per Y-tiled column.
285 */
286 assert(bs == 16);
287 logical_el = isl_extent2d(16, 16);
288 phys_B = isl_extent2d(128, 32);
289 break;
290
291 case ISL_TILING_CCS:
292 /* CCS surfaces are required to have one of the GENX_CCS_* formats which
293 * have a block size of 1 or 2 bits per block and each CCS element
294 * corresponds to one cache-line pair in the main surface. From the Sky
295 * Lake PRM Vol. 12 in the section on planes:
296 *
297 * "The Color Control Surface (CCS) contains the compression status
298 * of the cache-line pairs. The compression state of the cache-line
299 * pair is specified by 2 bits in the CCS. Each CCS cache-line
300 * represents an area on the main surface of 16x16 sets of 128 byte
301 * Y-tiled cache-line-pairs. CCS is always Y tiled."
302 *
303 * The CCS being Y-tiled implies that it's an 8x8 grid of cache-lines.
304 * Since each cache line corresponds to a 16x16 set of cache-line pairs,
305 * that yields total tile area of 128x128 cache-line pairs or CCS
306 * elements. On older hardware, each CCS element is 1 bit and the tile
307 * is 128x256 elements.
308 */
309 assert(format_bpb == 1 || format_bpb == 2);
310 logical_el = isl_extent2d(128, 256 / format_bpb);
311 phys_B = isl_extent2d(128, 32);
312 break;
313
314 case ISL_TILING_GEN12_CCS:
315 /* From the Bspec, Gen Graphics > Gen12 > Memory Data Formats > Memory
316 * Compression > Memory Compression - Gen12:
317 *
318 * 4 bits of auxiliary plane data are required for 2 cachelines of
319 * main surface data. This results in a single cacheline of auxiliary
320 * plane data mapping to 4 4K pages of main surface data for the 4K
321 * pages (tile Y ) and 1 64K Tile Ys page.
322 *
323 * The Y-tiled pairing bit of 9 shown in the table below that Bspec
324 * section expresses that the 2 cachelines of main surface data are
325 * horizontally adjacent.
326 *
327 * TODO: Handle Ys, Yf and their pairing bits.
328 *
329 * Therefore, each CCS cacheline represents a 512Bx32 row area and each
330 * element represents a 32Bx4 row area.
331 */
332 assert(format_bpb == 4);
333 logical_el = isl_extent2d(16, 8);
334 phys_B = isl_extent2d(64, 1);
335 break;
336
337 default:
338 unreachable("not reached");
339 } /* end switch */
340
341 *tile_info = (struct isl_tile_info) {
342 .tiling = tiling,
343 .format_bpb = format_bpb,
344 .logical_extent_el = logical_el,
345 .phys_extent_B = phys_B,
346 };
347 }
348
349 bool
350 isl_color_value_is_zero(union isl_color_value value,
351 enum isl_format format)
352 {
353 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
354
355 #define RETURN_FALSE_IF_NOT_0(c, i) \
356 if (fmtl->channels.c.bits && value.u32[i] != 0) \
357 return false
358
359 RETURN_FALSE_IF_NOT_0(r, 0);
360 RETURN_FALSE_IF_NOT_0(g, 1);
361 RETURN_FALSE_IF_NOT_0(b, 2);
362 RETURN_FALSE_IF_NOT_0(a, 3);
363
364 #undef RETURN_FALSE_IF_NOT_0
365
366 return true;
367 }
368
369 bool
370 isl_color_value_is_zero_one(union isl_color_value value,
371 enum isl_format format)
372 {
373 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
374
375 #define RETURN_FALSE_IF_NOT_0_1(c, i, field) \
376 if (fmtl->channels.c.bits && value.field[i] != 0 && value.field[i] != 1) \
377 return false
378
379 if (isl_format_has_int_channel(format)) {
380 RETURN_FALSE_IF_NOT_0_1(r, 0, u32);
381 RETURN_FALSE_IF_NOT_0_1(g, 1, u32);
382 RETURN_FALSE_IF_NOT_0_1(b, 2, u32);
383 RETURN_FALSE_IF_NOT_0_1(a, 3, u32);
384 } else {
385 RETURN_FALSE_IF_NOT_0_1(r, 0, f32);
386 RETURN_FALSE_IF_NOT_0_1(g, 1, f32);
387 RETURN_FALSE_IF_NOT_0_1(b, 2, f32);
388 RETURN_FALSE_IF_NOT_0_1(a, 3, f32);
389 }
390
391 #undef RETURN_FALSE_IF_NOT_0_1
392
393 return true;
394 }
395
396 /**
397 * @param[out] tiling is set only on success
398 */
399 static bool
400 isl_surf_choose_tiling(const struct isl_device *dev,
401 const struct isl_surf_init_info *restrict info,
402 enum isl_tiling *tiling)
403 {
404 isl_tiling_flags_t tiling_flags = info->tiling_flags;
405
406 /* HiZ surfaces always use the HiZ tiling */
407 if (info->usage & ISL_SURF_USAGE_HIZ_BIT) {
408 assert(info->format == ISL_FORMAT_HIZ);
409 assert(tiling_flags == ISL_TILING_HIZ_BIT);
410 *tiling = isl_tiling_flag_to_enum(tiling_flags);
411 return true;
412 }
413
414 /* CCS surfaces always use the CCS tiling */
415 if (info->usage & ISL_SURF_USAGE_CCS_BIT) {
416 assert(isl_format_get_layout(info->format)->txc == ISL_TXC_CCS);
417 UNUSED bool ivb_ccs = ISL_DEV_GEN(dev) < 12 &&
418 tiling_flags == ISL_TILING_CCS_BIT;
419 UNUSED bool tgl_ccs = ISL_DEV_GEN(dev) >= 12 &&
420 tiling_flags == ISL_TILING_GEN12_CCS_BIT;
421 assert(ivb_ccs != tgl_ccs);
422 *tiling = isl_tiling_flag_to_enum(tiling_flags);
423 return true;
424 }
425
426 if (ISL_DEV_GEN(dev) >= 6) {
427 isl_gen6_filter_tiling(dev, info, &tiling_flags);
428 } else {
429 isl_gen4_filter_tiling(dev, info, &tiling_flags);
430 }
431
432 #define CHOOSE(__tiling) \
433 do { \
434 if (tiling_flags & (1u << (__tiling))) { \
435 *tiling = (__tiling); \
436 return true; \
437 } \
438 } while (0)
439
440 /* Of the tiling modes remaining, choose the one that offers the best
441 * performance.
442 */
443
444 if (info->dim == ISL_SURF_DIM_1D) {
445 /* Prefer linear for 1D surfaces because they do not benefit from
446 * tiling. To the contrary, tiling leads to wasted memory and poor
447 * memory locality due to the swizzling and alignment restrictions
448 * required in tiled surfaces.
449 */
450 CHOOSE(ISL_TILING_LINEAR);
451 }
452
453 CHOOSE(ISL_TILING_Ys);
454 CHOOSE(ISL_TILING_Yf);
455 CHOOSE(ISL_TILING_Y0);
456 CHOOSE(ISL_TILING_X);
457 CHOOSE(ISL_TILING_W);
458 CHOOSE(ISL_TILING_LINEAR);
459
460 #undef CHOOSE
461
462 /* No tiling mode accomodates the inputs. */
463 return false;
464 }
465
466 static bool
467 isl_choose_msaa_layout(const struct isl_device *dev,
468 const struct isl_surf_init_info *info,
469 enum isl_tiling tiling,
470 enum isl_msaa_layout *msaa_layout)
471 {
472 if (ISL_DEV_GEN(dev) >= 8) {
473 return isl_gen8_choose_msaa_layout(dev, info, tiling, msaa_layout);
474 } else if (ISL_DEV_GEN(dev) >= 7) {
475 return isl_gen7_choose_msaa_layout(dev, info, tiling, msaa_layout);
476 } else if (ISL_DEV_GEN(dev) >= 6) {
477 return isl_gen6_choose_msaa_layout(dev, info, tiling, msaa_layout);
478 } else {
479 return isl_gen4_choose_msaa_layout(dev, info, tiling, msaa_layout);
480 }
481 }
482
483 struct isl_extent2d
484 isl_get_interleaved_msaa_px_size_sa(uint32_t samples)
485 {
486 assert(isl_is_pow2(samples));
487
488 /* From the Broadwell PRM >> Volume 5: Memory Views >> Computing Mip Level
489 * Sizes (p133):
490 *
491 * If the surface is multisampled and it is a depth or stencil surface
492 * or Multisampled Surface StorageFormat in SURFACE_STATE is
493 * MSFMT_DEPTH_STENCIL, W_L and H_L must be adjusted as follows before
494 * proceeding: [...]
495 */
496 return (struct isl_extent2d) {
497 .width = 1 << ((ffs(samples) - 0) / 2),
498 .height = 1 << ((ffs(samples) - 1) / 2),
499 };
500 }
501
502 static void
503 isl_msaa_interleaved_scale_px_to_sa(uint32_t samples,
504 uint32_t *width, uint32_t *height)
505 {
506 const struct isl_extent2d px_size_sa =
507 isl_get_interleaved_msaa_px_size_sa(samples);
508
509 if (width)
510 *width = isl_align(*width, 2) * px_size_sa.width;
511 if (height)
512 *height = isl_align(*height, 2) * px_size_sa.height;
513 }
514
515 static enum isl_array_pitch_span
516 isl_choose_array_pitch_span(const struct isl_device *dev,
517 const struct isl_surf_init_info *restrict info,
518 enum isl_dim_layout dim_layout,
519 const struct isl_extent4d *phys_level0_sa)
520 {
521 switch (dim_layout) {
522 case ISL_DIM_LAYOUT_GEN9_1D:
523 case ISL_DIM_LAYOUT_GEN4_2D:
524 if (ISL_DEV_GEN(dev) >= 8) {
525 /* QPitch becomes programmable in Broadwell. So choose the
526 * most compact QPitch possible in order to conserve memory.
527 *
528 * From the Broadwell PRM >> Volume 2d: Command Reference: Structures
529 * >> RENDER_SURFACE_STATE Surface QPitch (p325):
530 *
531 * - Software must ensure that this field is set to a value
532 * sufficiently large such that the array slices in the surface
533 * do not overlap. Refer to the Memory Data Formats section for
534 * information on how surfaces are stored in memory.
535 *
536 * - This field specifies the distance in rows between array
537 * slices. It is used only in the following cases:
538 *
539 * - Surface Array is enabled OR
540 * - Number of Mulitsamples is not NUMSAMPLES_1 and
541 * Multisampled Surface Storage Format set to MSFMT_MSS OR
542 * - Surface Type is SURFTYPE_CUBE
543 */
544 return ISL_ARRAY_PITCH_SPAN_COMPACT;
545 } else if (ISL_DEV_GEN(dev) >= 7) {
546 /* Note that Ivybridge introduces
547 * RENDER_SURFACE_STATE.SurfaceArraySpacing, which provides the
548 * driver more control over the QPitch.
549 */
550
551 if (phys_level0_sa->array_len == 1) {
552 /* The hardware will never use the QPitch. So choose the most
553 * compact QPitch possible in order to conserve memory.
554 */
555 return ISL_ARRAY_PITCH_SPAN_COMPACT;
556 }
557
558 if (isl_surf_usage_is_depth_or_stencil(info->usage) ||
559 (info->usage & ISL_SURF_USAGE_HIZ_BIT)) {
560 /* From the Ivybridge PRM >> Volume 1 Part 1: Graphics Core >>
561 * Section 6.18.4.7: Surface Arrays (p112):
562 *
563 * If Surface Array Spacing is set to ARYSPC_FULL (note that
564 * the depth buffer and stencil buffer have an implied value of
565 * ARYSPC_FULL):
566 */
567 return ISL_ARRAY_PITCH_SPAN_FULL;
568 }
569
570 if (info->levels == 1) {
571 /* We are able to set RENDER_SURFACE_STATE.SurfaceArraySpacing
572 * to ARYSPC_LOD0.
573 */
574 return ISL_ARRAY_PITCH_SPAN_COMPACT;
575 }
576
577 return ISL_ARRAY_PITCH_SPAN_FULL;
578 } else if ((ISL_DEV_GEN(dev) == 5 || ISL_DEV_GEN(dev) == 6) &&
579 ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
580 isl_surf_usage_is_stencil(info->usage)) {
581 /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
582 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
583 *
584 * The separate stencil buffer does not support mip mapping, thus
585 * the storage for LODs other than LOD 0 is not needed.
586 */
587 assert(info->levels == 1);
588 return ISL_ARRAY_PITCH_SPAN_COMPACT;
589 } else {
590 if ((ISL_DEV_GEN(dev) == 5 || ISL_DEV_GEN(dev) == 6) &&
591 ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
592 isl_surf_usage_is_stencil(info->usage)) {
593 /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
594 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
595 *
596 * The separate stencil buffer does not support mip mapping,
597 * thus the storage for LODs other than LOD 0 is not needed.
598 */
599 assert(info->levels == 1);
600 assert(phys_level0_sa->array_len == 1);
601 return ISL_ARRAY_PITCH_SPAN_COMPACT;
602 }
603
604 if (phys_level0_sa->array_len == 1) {
605 /* The hardware will never use the QPitch. So choose the most
606 * compact QPitch possible in order to conserve memory.
607 */
608 return ISL_ARRAY_PITCH_SPAN_COMPACT;
609 }
610
611 return ISL_ARRAY_PITCH_SPAN_FULL;
612 }
613
614 case ISL_DIM_LAYOUT_GEN4_3D:
615 /* The hardware will never use the QPitch. So choose the most
616 * compact QPitch possible in order to conserve memory.
617 */
618 return ISL_ARRAY_PITCH_SPAN_COMPACT;
619
620 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ:
621 /* Each array image in the gen6 stencil of HiZ surface is compact in the
622 * sense that every LOD is a compact array of the same size as LOD0.
623 */
624 return ISL_ARRAY_PITCH_SPAN_COMPACT;
625 }
626
627 unreachable("bad isl_dim_layout");
628 return ISL_ARRAY_PITCH_SPAN_FULL;
629 }
630
631 static void
632 isl_choose_image_alignment_el(const struct isl_device *dev,
633 const struct isl_surf_init_info *restrict info,
634 enum isl_tiling tiling,
635 enum isl_dim_layout dim_layout,
636 enum isl_msaa_layout msaa_layout,
637 struct isl_extent3d *image_align_el)
638 {
639 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
640 if (fmtl->txc == ISL_TXC_MCS) {
641 assert(tiling == ISL_TILING_Y0);
642
643 /*
644 * IvyBrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
645 *
646 * Height, width, and layout of MCS buffer in this case must match with
647 * Render Target height, width, and layout. MCS buffer is tiledY.
648 *
649 * To avoid wasting memory, choose the smallest alignment possible:
650 * HALIGN_4 and VALIGN_4.
651 */
652 *image_align_el = isl_extent3d(4, 4, 1);
653 return;
654 } else if (info->format == ISL_FORMAT_HIZ) {
655 assert(ISL_DEV_GEN(dev) >= 6);
656 if (ISL_DEV_GEN(dev) == 6) {
657 /* HiZ surfaces on Sandy Bridge are packed tightly. */
658 *image_align_el = isl_extent3d(1, 1, 1);
659 } else if (ISL_DEV_GEN(dev) < 12) {
660 /* On gen7+, HiZ surfaces are always aligned to 16x8 pixels in the
661 * primary surface which works out to 2x2 HiZ elments.
662 */
663 *image_align_el = isl_extent3d(2, 2, 1);
664 } else {
665 /* On gen12+, HiZ surfaces are always aligned to 16x16 pixels in the
666 * primary surface which works out to 2x4 HiZ elments.
667 * TODO: Verify
668 */
669 *image_align_el = isl_extent3d(2, 4, 1);
670 }
671 return;
672 }
673
674 if (ISL_DEV_GEN(dev) >= 12) {
675 isl_gen12_choose_image_alignment_el(dev, info, tiling, dim_layout,
676 msaa_layout, image_align_el);
677 } else if (ISL_DEV_GEN(dev) >= 9) {
678 isl_gen9_choose_image_alignment_el(dev, info, tiling, dim_layout,
679 msaa_layout, image_align_el);
680 } else if (ISL_DEV_GEN(dev) >= 8) {
681 isl_gen8_choose_image_alignment_el(dev, info, tiling, dim_layout,
682 msaa_layout, image_align_el);
683 } else if (ISL_DEV_GEN(dev) >= 7) {
684 isl_gen7_choose_image_alignment_el(dev, info, tiling, dim_layout,
685 msaa_layout, image_align_el);
686 } else if (ISL_DEV_GEN(dev) >= 6) {
687 isl_gen6_choose_image_alignment_el(dev, info, tiling, dim_layout,
688 msaa_layout, image_align_el);
689 } else {
690 isl_gen4_choose_image_alignment_el(dev, info, tiling, dim_layout,
691 msaa_layout, image_align_el);
692 }
693 }
694
695 static enum isl_dim_layout
696 isl_surf_choose_dim_layout(const struct isl_device *dev,
697 enum isl_surf_dim logical_dim,
698 enum isl_tiling tiling,
699 isl_surf_usage_flags_t usage)
700 {
701 /* Sandy bridge needs a special layout for HiZ and stencil. */
702 if (ISL_DEV_GEN(dev) == 6 &&
703 (tiling == ISL_TILING_W || tiling == ISL_TILING_HIZ))
704 return ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ;
705
706 if (ISL_DEV_GEN(dev) >= 9) {
707 switch (logical_dim) {
708 case ISL_SURF_DIM_1D:
709 /* From the Sky Lake PRM Vol. 5, "1D Surfaces":
710 *
711 * One-dimensional surfaces use a tiling mode of linear.
712 * Technically, they are not tiled resources, but the Tiled
713 * Resource Mode field in RENDER_SURFACE_STATE is still used to
714 * indicate the alignment requirements for this linear surface
715 * (See 1D Alignment requirements for how 4K and 64KB Tiled
716 * Resource Modes impact alignment). Alternatively, a 1D surface
717 * can be defined as a 2D tiled surface (e.g. TileY or TileX) with
718 * a height of 0.
719 *
720 * In other words, ISL_DIM_LAYOUT_GEN9_1D is only used for linear
721 * surfaces and, for tiled surfaces, ISL_DIM_LAYOUT_GEN4_2D is used.
722 */
723 if (tiling == ISL_TILING_LINEAR)
724 return ISL_DIM_LAYOUT_GEN9_1D;
725 else
726 return ISL_DIM_LAYOUT_GEN4_2D;
727 case ISL_SURF_DIM_2D:
728 case ISL_SURF_DIM_3D:
729 return ISL_DIM_LAYOUT_GEN4_2D;
730 }
731 } else {
732 switch (logical_dim) {
733 case ISL_SURF_DIM_1D:
734 case ISL_SURF_DIM_2D:
735 /* From the G45 PRM Vol. 1a, "6.17.4.1 Hardware Cube Map Layout":
736 *
737 * The cube face textures are stored in the same way as 3D surfaces
738 * are stored (see section 6.17.5 for details). For cube surfaces,
739 * however, the depth is equal to the number of faces (always 6) and
740 * is not reduced for each MIP.
741 */
742 if (ISL_DEV_GEN(dev) == 4 && (usage & ISL_SURF_USAGE_CUBE_BIT))
743 return ISL_DIM_LAYOUT_GEN4_3D;
744
745 return ISL_DIM_LAYOUT_GEN4_2D;
746 case ISL_SURF_DIM_3D:
747 return ISL_DIM_LAYOUT_GEN4_3D;
748 }
749 }
750
751 unreachable("bad isl_surf_dim");
752 return ISL_DIM_LAYOUT_GEN4_2D;
753 }
754
755 /**
756 * Calculate the physical extent of the surface's first level, in units of
757 * surface samples.
758 */
759 static void
760 isl_calc_phys_level0_extent_sa(const struct isl_device *dev,
761 const struct isl_surf_init_info *restrict info,
762 enum isl_dim_layout dim_layout,
763 enum isl_tiling tiling,
764 enum isl_msaa_layout msaa_layout,
765 struct isl_extent4d *phys_level0_sa)
766 {
767 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
768
769 if (isl_format_is_yuv(info->format))
770 isl_finishme("%s:%s: YUV format", __FILE__, __func__);
771
772 switch (info->dim) {
773 case ISL_SURF_DIM_1D:
774 assert(info->height == 1);
775 assert(info->depth == 1);
776 assert(info->samples == 1);
777
778 switch (dim_layout) {
779 case ISL_DIM_LAYOUT_GEN4_3D:
780 unreachable("bad isl_dim_layout");
781
782 case ISL_DIM_LAYOUT_GEN9_1D:
783 case ISL_DIM_LAYOUT_GEN4_2D:
784 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ:
785 *phys_level0_sa = (struct isl_extent4d) {
786 .w = info->width,
787 .h = 1,
788 .d = 1,
789 .a = info->array_len,
790 };
791 break;
792 }
793 break;
794
795 case ISL_SURF_DIM_2D:
796 if (ISL_DEV_GEN(dev) == 4 && (info->usage & ISL_SURF_USAGE_CUBE_BIT))
797 assert(dim_layout == ISL_DIM_LAYOUT_GEN4_3D);
798 else
799 assert(dim_layout == ISL_DIM_LAYOUT_GEN4_2D ||
800 dim_layout == ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ);
801
802 if (tiling == ISL_TILING_Ys && info->samples > 1)
803 isl_finishme("%s:%s: multisample TileYs layout", __FILE__, __func__);
804
805 switch (msaa_layout) {
806 case ISL_MSAA_LAYOUT_NONE:
807 assert(info->depth == 1);
808 assert(info->samples == 1);
809
810 *phys_level0_sa = (struct isl_extent4d) {
811 .w = info->width,
812 .h = info->height,
813 .d = 1,
814 .a = info->array_len,
815 };
816 break;
817
818 case ISL_MSAA_LAYOUT_ARRAY:
819 assert(info->depth == 1);
820 assert(info->levels == 1);
821 assert(isl_format_supports_multisampling(dev->info, info->format));
822 assert(fmtl->bw == 1 && fmtl->bh == 1);
823
824 *phys_level0_sa = (struct isl_extent4d) {
825 .w = info->width,
826 .h = info->height,
827 .d = 1,
828 .a = info->array_len * info->samples,
829 };
830 break;
831
832 case ISL_MSAA_LAYOUT_INTERLEAVED:
833 assert(info->depth == 1);
834 assert(info->levels == 1);
835 assert(isl_format_supports_multisampling(dev->info, info->format));
836
837 *phys_level0_sa = (struct isl_extent4d) {
838 .w = info->width,
839 .h = info->height,
840 .d = 1,
841 .a = info->array_len,
842 };
843
844 isl_msaa_interleaved_scale_px_to_sa(info->samples,
845 &phys_level0_sa->w,
846 &phys_level0_sa->h);
847 break;
848 }
849 break;
850
851 case ISL_SURF_DIM_3D:
852 assert(info->array_len == 1);
853 assert(info->samples == 1);
854
855 if (fmtl->bd > 1) {
856 isl_finishme("%s:%s: compression block with depth > 1",
857 __FILE__, __func__);
858 }
859
860 switch (dim_layout) {
861 case ISL_DIM_LAYOUT_GEN9_1D:
862 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ:
863 unreachable("bad isl_dim_layout");
864
865 case ISL_DIM_LAYOUT_GEN4_2D:
866 assert(ISL_DEV_GEN(dev) >= 9);
867
868 *phys_level0_sa = (struct isl_extent4d) {
869 .w = info->width,
870 .h = info->height,
871 .d = 1,
872 .a = info->depth,
873 };
874 break;
875
876 case ISL_DIM_LAYOUT_GEN4_3D:
877 assert(ISL_DEV_GEN(dev) < 9);
878 *phys_level0_sa = (struct isl_extent4d) {
879 .w = info->width,
880 .h = info->height,
881 .d = info->depth,
882 .a = 1,
883 };
884 break;
885 }
886 break;
887 }
888 }
889
890 /**
891 * Calculate the pitch between physical array slices, in units of rows of
892 * surface elements.
893 */
894 static uint32_t
895 isl_calc_array_pitch_el_rows_gen4_2d(
896 const struct isl_device *dev,
897 const struct isl_surf_init_info *restrict info,
898 const struct isl_tile_info *tile_info,
899 const struct isl_extent3d *image_align_sa,
900 const struct isl_extent4d *phys_level0_sa,
901 enum isl_array_pitch_span array_pitch_span,
902 const struct isl_extent2d *phys_slice0_sa)
903 {
904 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
905 uint32_t pitch_sa_rows = 0;
906
907 switch (array_pitch_span) {
908 case ISL_ARRAY_PITCH_SPAN_COMPACT:
909 pitch_sa_rows = isl_align_npot(phys_slice0_sa->h, image_align_sa->h);
910 break;
911 case ISL_ARRAY_PITCH_SPAN_FULL: {
912 /* The QPitch equation is found in the Broadwell PRM >> Volume 5:
913 * Memory Views >> Common Surface Formats >> Surface Layout >> 2D
914 * Surfaces >> Surface Arrays.
915 */
916 uint32_t H0_sa = phys_level0_sa->h;
917 uint32_t H1_sa = isl_minify(H0_sa, 1);
918
919 uint32_t h0_sa = isl_align_npot(H0_sa, image_align_sa->h);
920 uint32_t h1_sa = isl_align_npot(H1_sa, image_align_sa->h);
921
922 uint32_t m;
923 if (ISL_DEV_GEN(dev) >= 7) {
924 /* The QPitch equation changed slightly in Ivybridge. */
925 m = 12;
926 } else {
927 m = 11;
928 }
929
930 pitch_sa_rows = h0_sa + h1_sa + (m * image_align_sa->h);
931
932 if (ISL_DEV_GEN(dev) == 6 && info->samples > 1 &&
933 (info->height % 4 == 1)) {
934 /* [SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
935 * Graphics Core >> Section 7.18.3.7: Surface Arrays:
936 *
937 * [SNB] Errata: Sampler MSAA Qpitch will be 4 greater than
938 * the value calculated in the equation above , for every
939 * other odd Surface Height starting from 1 i.e. 1,5,9,13.
940 *
941 * XXX(chadv): Is the errata natural corollary of the physical
942 * layout of interleaved samples?
943 */
944 pitch_sa_rows += 4;
945 }
946
947 pitch_sa_rows = isl_align_npot(pitch_sa_rows, fmtl->bh);
948 } /* end case */
949 break;
950 }
951
952 assert(pitch_sa_rows % fmtl->bh == 0);
953 uint32_t pitch_el_rows = pitch_sa_rows / fmtl->bh;
954
955 if (ISL_DEV_GEN(dev) >= 9 && ISL_DEV_GEN(dev) <= 11 &&
956 fmtl->txc == ISL_TXC_CCS) {
957 /*
958 * From the Sky Lake PRM Vol 7, "MCS Buffer for Render Target(s)" (p. 632):
959 *
960 * "Mip-mapped and arrayed surfaces are supported with MCS buffer
961 * layout with these alignments in the RT space: Horizontal
962 * Alignment = 128 and Vertical Alignment = 64."
963 *
964 * From the Sky Lake PRM Vol. 2d, "RENDER_SURFACE_STATE" (p. 435):
965 *
966 * "For non-multisampled render target's CCS auxiliary surface,
967 * QPitch must be computed with Horizontal Alignment = 128 and
968 * Surface Vertical Alignment = 256. These alignments are only for
969 * CCS buffer and not for associated render target."
970 *
971 * The first restriction is already handled by isl_choose_image_alignment_el
972 * but the second restriction, which is an extension of the first, only
973 * applies to qpitch and must be applied here.
974 *
975 * The second restriction disappears on Gen12.
976 */
977 assert(fmtl->bh == 4);
978 pitch_el_rows = isl_align(pitch_el_rows, 256 / 4);
979 }
980
981 if (ISL_DEV_GEN(dev) >= 9 &&
982 info->dim == ISL_SURF_DIM_3D &&
983 tile_info->tiling != ISL_TILING_LINEAR) {
984 /* From the Skylake BSpec >> RENDER_SURFACE_STATE >> Surface QPitch:
985 *
986 * Tile Mode != Linear: This field must be set to an integer multiple
987 * of the tile height
988 */
989 pitch_el_rows = isl_align(pitch_el_rows, tile_info->logical_extent_el.height);
990 }
991
992 return pitch_el_rows;
993 }
994
995 /**
996 * A variant of isl_calc_phys_slice0_extent_sa() specific to
997 * ISL_DIM_LAYOUT_GEN4_2D.
998 */
999 static void
1000 isl_calc_phys_slice0_extent_sa_gen4_2d(
1001 const struct isl_device *dev,
1002 const struct isl_surf_init_info *restrict info,
1003 enum isl_msaa_layout msaa_layout,
1004 const struct isl_extent3d *image_align_sa,
1005 const struct isl_extent4d *phys_level0_sa,
1006 struct isl_extent2d *phys_slice0_sa)
1007 {
1008 assert(phys_level0_sa->depth == 1);
1009
1010 if (info->levels == 1) {
1011 /* Do not pad the surface to the image alignment.
1012 *
1013 * For tiled surfaces, using a reduced alignment here avoids wasting CPU
1014 * cycles on the below mipmap layout caluclations. Reducing the
1015 * alignment here is safe because we later align the row pitch and array
1016 * pitch to the tile boundary. It is safe even for
1017 * ISL_MSAA_LAYOUT_INTERLEAVED, because phys_level0_sa is already scaled
1018 * to accomodate the interleaved samples.
1019 *
1020 * For linear surfaces, reducing the alignment here permits us to later
1021 * choose an arbitrary, non-aligned row pitch. If the surface backs
1022 * a VkBuffer, then an arbitrary pitch may be needed to accomodate
1023 * VkBufferImageCopy::bufferRowLength.
1024 */
1025 *phys_slice0_sa = (struct isl_extent2d) {
1026 .w = phys_level0_sa->w,
1027 .h = phys_level0_sa->h,
1028 };
1029 return;
1030 }
1031
1032 uint32_t slice_top_w = 0;
1033 uint32_t slice_bottom_w = 0;
1034 uint32_t slice_left_h = 0;
1035 uint32_t slice_right_h = 0;
1036
1037 uint32_t W0 = phys_level0_sa->w;
1038 uint32_t H0 = phys_level0_sa->h;
1039
1040 for (uint32_t l = 0; l < info->levels; ++l) {
1041 uint32_t W = isl_minify(W0, l);
1042 uint32_t H = isl_minify(H0, l);
1043
1044 uint32_t w = isl_align_npot(W, image_align_sa->w);
1045 uint32_t h = isl_align_npot(H, image_align_sa->h);
1046
1047 if (l == 0) {
1048 slice_top_w = w;
1049 slice_left_h = h;
1050 slice_right_h = h;
1051 } else if (l == 1) {
1052 slice_bottom_w = w;
1053 slice_left_h += h;
1054 } else if (l == 2) {
1055 slice_bottom_w += w;
1056 slice_right_h += h;
1057 } else {
1058 slice_right_h += h;
1059 }
1060 }
1061
1062 *phys_slice0_sa = (struct isl_extent2d) {
1063 .w = MAX(slice_top_w, slice_bottom_w),
1064 .h = MAX(slice_left_h, slice_right_h),
1065 };
1066 }
1067
1068 static void
1069 isl_calc_phys_total_extent_el_gen4_2d(
1070 const struct isl_device *dev,
1071 const struct isl_surf_init_info *restrict info,
1072 const struct isl_tile_info *tile_info,
1073 enum isl_msaa_layout msaa_layout,
1074 const struct isl_extent3d *image_align_sa,
1075 const struct isl_extent4d *phys_level0_sa,
1076 enum isl_array_pitch_span array_pitch_span,
1077 uint32_t *array_pitch_el_rows,
1078 struct isl_extent2d *total_extent_el)
1079 {
1080 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1081
1082 struct isl_extent2d phys_slice0_sa;
1083 isl_calc_phys_slice0_extent_sa_gen4_2d(dev, info, msaa_layout,
1084 image_align_sa, phys_level0_sa,
1085 &phys_slice0_sa);
1086 *array_pitch_el_rows =
1087 isl_calc_array_pitch_el_rows_gen4_2d(dev, info, tile_info,
1088 image_align_sa, phys_level0_sa,
1089 array_pitch_span,
1090 &phys_slice0_sa);
1091 *total_extent_el = (struct isl_extent2d) {
1092 .w = isl_align_div_npot(phys_slice0_sa.w, fmtl->bw),
1093 .h = *array_pitch_el_rows * (phys_level0_sa->array_len - 1) +
1094 isl_align_div_npot(phys_slice0_sa.h, fmtl->bh),
1095 };
1096 }
1097
1098 /**
1099 * A variant of isl_calc_phys_slice0_extent_sa() specific to
1100 * ISL_DIM_LAYOUT_GEN4_3D.
1101 */
1102 static void
1103 isl_calc_phys_total_extent_el_gen4_3d(
1104 const struct isl_device *dev,
1105 const struct isl_surf_init_info *restrict info,
1106 const struct isl_extent3d *image_align_sa,
1107 const struct isl_extent4d *phys_level0_sa,
1108 uint32_t *array_pitch_el_rows,
1109 struct isl_extent2d *phys_total_el)
1110 {
1111 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1112
1113 assert(info->samples == 1);
1114
1115 if (info->dim != ISL_SURF_DIM_3D) {
1116 /* From the G45 PRM Vol. 1a, "6.17.4.1 Hardware Cube Map Layout":
1117 *
1118 * The cube face textures are stored in the same way as 3D surfaces
1119 * are stored (see section 6.17.5 for details). For cube surfaces,
1120 * however, the depth is equal to the number of faces (always 6) and
1121 * is not reduced for each MIP.
1122 */
1123 assert(ISL_DEV_GEN(dev) == 4);
1124 assert(info->usage & ISL_SURF_USAGE_CUBE_BIT);
1125 assert(phys_level0_sa->array_len == 6);
1126 } else {
1127 assert(phys_level0_sa->array_len == 1);
1128 }
1129
1130 uint32_t total_w = 0;
1131 uint32_t total_h = 0;
1132
1133 uint32_t W0 = phys_level0_sa->w;
1134 uint32_t H0 = phys_level0_sa->h;
1135 uint32_t D0 = phys_level0_sa->d;
1136 uint32_t A0 = phys_level0_sa->a;
1137
1138 for (uint32_t l = 0; l < info->levels; ++l) {
1139 uint32_t level_w = isl_align_npot(isl_minify(W0, l), image_align_sa->w);
1140 uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa->h);
1141 uint32_t level_d = info->dim == ISL_SURF_DIM_3D ? isl_minify(D0, l) : A0;
1142
1143 uint32_t max_layers_horiz = MIN(level_d, 1u << l);
1144 uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
1145
1146 total_w = MAX(total_w, level_w * max_layers_horiz);
1147 total_h += level_h * max_layers_vert;
1148 }
1149
1150 /* GEN4_3D layouts don't really have an array pitch since each LOD has a
1151 * different number of horizontal and vertical layers. We have to set it
1152 * to something, so at least make it true for LOD0.
1153 */
1154 *array_pitch_el_rows =
1155 isl_align_npot(phys_level0_sa->h, image_align_sa->h) / fmtl->bw;
1156 *phys_total_el = (struct isl_extent2d) {
1157 .w = isl_assert_div(total_w, fmtl->bw),
1158 .h = isl_assert_div(total_h, fmtl->bh),
1159 };
1160 }
1161
1162 /**
1163 * A variant of isl_calc_phys_slice0_extent_sa() specific to
1164 * ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ.
1165 */
1166 static void
1167 isl_calc_phys_total_extent_el_gen6_stencil_hiz(
1168 const struct isl_device *dev,
1169 const struct isl_surf_init_info *restrict info,
1170 const struct isl_tile_info *tile_info,
1171 const struct isl_extent3d *image_align_sa,
1172 const struct isl_extent4d *phys_level0_sa,
1173 uint32_t *array_pitch_el_rows,
1174 struct isl_extent2d *phys_total_el)
1175 {
1176 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1177
1178 const struct isl_extent2d tile_extent_sa = {
1179 .w = tile_info->logical_extent_el.w * fmtl->bw,
1180 .h = tile_info->logical_extent_el.h * fmtl->bh,
1181 };
1182 /* Tile size is a multiple of image alignment */
1183 assert(tile_extent_sa.w % image_align_sa->w == 0);
1184 assert(tile_extent_sa.h % image_align_sa->h == 0);
1185
1186 const uint32_t W0 = phys_level0_sa->w;
1187 const uint32_t H0 = phys_level0_sa->h;
1188
1189 /* Each image has the same height as LOD0 because the hardware thinks
1190 * everything is LOD0
1191 */
1192 const uint32_t H = isl_align(H0, image_align_sa->h) * phys_level0_sa->a;
1193
1194 uint32_t total_top_w = 0;
1195 uint32_t total_bottom_w = 0;
1196 uint32_t total_h = 0;
1197
1198 for (uint32_t l = 0; l < info->levels; ++l) {
1199 const uint32_t W = isl_minify(W0, l);
1200
1201 const uint32_t w = isl_align(W, tile_extent_sa.w);
1202 const uint32_t h = isl_align(H, tile_extent_sa.h);
1203
1204 if (l == 0) {
1205 total_top_w = w;
1206 total_h = h;
1207 } else if (l == 1) {
1208 total_bottom_w = w;
1209 total_h += h;
1210 } else {
1211 total_bottom_w += w;
1212 }
1213 }
1214
1215 *array_pitch_el_rows =
1216 isl_assert_div(isl_align(H0, image_align_sa->h), fmtl->bh);
1217 *phys_total_el = (struct isl_extent2d) {
1218 .w = isl_assert_div(MAX(total_top_w, total_bottom_w), fmtl->bw),
1219 .h = isl_assert_div(total_h, fmtl->bh),
1220 };
1221 }
1222
1223 /**
1224 * A variant of isl_calc_phys_slice0_extent_sa() specific to
1225 * ISL_DIM_LAYOUT_GEN9_1D.
1226 */
1227 static void
1228 isl_calc_phys_total_extent_el_gen9_1d(
1229 const struct isl_device *dev,
1230 const struct isl_surf_init_info *restrict info,
1231 const struct isl_extent3d *image_align_sa,
1232 const struct isl_extent4d *phys_level0_sa,
1233 uint32_t *array_pitch_el_rows,
1234 struct isl_extent2d *phys_total_el)
1235 {
1236 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1237
1238 assert(phys_level0_sa->height == 1);
1239 assert(phys_level0_sa->depth == 1);
1240 assert(info->samples == 1);
1241 assert(image_align_sa->w >= fmtl->bw);
1242
1243 uint32_t slice_w = 0;
1244 const uint32_t W0 = phys_level0_sa->w;
1245
1246 for (uint32_t l = 0; l < info->levels; ++l) {
1247 uint32_t W = isl_minify(W0, l);
1248 uint32_t w = isl_align_npot(W, image_align_sa->w);
1249
1250 slice_w += w;
1251 }
1252
1253 *array_pitch_el_rows = 1;
1254 *phys_total_el = (struct isl_extent2d) {
1255 .w = isl_assert_div(slice_w, fmtl->bw),
1256 .h = phys_level0_sa->array_len,
1257 };
1258 }
1259
1260 /**
1261 * Calculate the two-dimensional total physical extent of the surface, in
1262 * units of surface elements.
1263 */
1264 static void
1265 isl_calc_phys_total_extent_el(const struct isl_device *dev,
1266 const struct isl_surf_init_info *restrict info,
1267 const struct isl_tile_info *tile_info,
1268 enum isl_dim_layout dim_layout,
1269 enum isl_msaa_layout msaa_layout,
1270 const struct isl_extent3d *image_align_sa,
1271 const struct isl_extent4d *phys_level0_sa,
1272 enum isl_array_pitch_span array_pitch_span,
1273 uint32_t *array_pitch_el_rows,
1274 struct isl_extent2d *total_extent_el)
1275 {
1276 switch (dim_layout) {
1277 case ISL_DIM_LAYOUT_GEN9_1D:
1278 assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
1279 isl_calc_phys_total_extent_el_gen9_1d(dev, info,
1280 image_align_sa, phys_level0_sa,
1281 array_pitch_el_rows,
1282 total_extent_el);
1283 return;
1284 case ISL_DIM_LAYOUT_GEN4_2D:
1285 isl_calc_phys_total_extent_el_gen4_2d(dev, info, tile_info, msaa_layout,
1286 image_align_sa, phys_level0_sa,
1287 array_pitch_span,
1288 array_pitch_el_rows,
1289 total_extent_el);
1290 return;
1291 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ:
1292 assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
1293 isl_calc_phys_total_extent_el_gen6_stencil_hiz(dev, info, tile_info,
1294 image_align_sa,
1295 phys_level0_sa,
1296 array_pitch_el_rows,
1297 total_extent_el);
1298 return;
1299 case ISL_DIM_LAYOUT_GEN4_3D:
1300 assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
1301 isl_calc_phys_total_extent_el_gen4_3d(dev, info,
1302 image_align_sa, phys_level0_sa,
1303 array_pitch_el_rows,
1304 total_extent_el);
1305 return;
1306 }
1307
1308 unreachable("invalid value for dim_layout");
1309 }
1310
1311 static uint32_t
1312 isl_calc_row_pitch_alignment(const struct isl_surf_init_info *surf_info,
1313 const struct isl_tile_info *tile_info)
1314 {
1315 if (tile_info->tiling != ISL_TILING_LINEAR)
1316 return tile_info->phys_extent_B.width;
1317
1318 /* From the Broadwel PRM >> Volume 2d: Command Reference: Structures >>
1319 * RENDER_SURFACE_STATE Surface Pitch (p349):
1320 *
1321 * - For linear render target surfaces and surfaces accessed with the
1322 * typed data port messages, the pitch must be a multiple of the
1323 * element size for non-YUV surface formats. Pitch must be
1324 * a multiple of 2 * element size for YUV surface formats.
1325 *
1326 * - [Requirements for SURFTYPE_BUFFER and SURFTYPE_STRBUF, which we
1327 * ignore because isl doesn't do buffers.]
1328 *
1329 * - For other linear surfaces, the pitch can be any multiple of
1330 * bytes.
1331 */
1332 const struct isl_format_layout *fmtl = isl_format_get_layout(surf_info->format);
1333 const uint32_t bs = fmtl->bpb / 8;
1334
1335 if (surf_info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1336 if (isl_format_is_yuv(surf_info->format)) {
1337 return 2 * bs;
1338 } else {
1339 return bs;
1340 }
1341 }
1342
1343 return 1;
1344 }
1345
1346 static uint32_t
1347 isl_calc_linear_min_row_pitch(const struct isl_device *dev,
1348 const struct isl_surf_init_info *info,
1349 const struct isl_extent2d *phys_total_el,
1350 uint32_t alignment_B)
1351 {
1352 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1353 const uint32_t bs = fmtl->bpb / 8;
1354
1355 return isl_align_npot(bs * phys_total_el->w, alignment_B);
1356 }
1357
1358 static uint32_t
1359 isl_calc_tiled_min_row_pitch(const struct isl_device *dev,
1360 const struct isl_surf_init_info *surf_info,
1361 const struct isl_tile_info *tile_info,
1362 const struct isl_extent2d *phys_total_el,
1363 uint32_t alignment_B)
1364 {
1365 const struct isl_format_layout *fmtl = isl_format_get_layout(surf_info->format);
1366
1367 assert(fmtl->bpb % tile_info->format_bpb == 0);
1368
1369 const uint32_t tile_el_scale = fmtl->bpb / tile_info->format_bpb;
1370 const uint32_t total_w_tl =
1371 isl_align_div(phys_total_el->w * tile_el_scale,
1372 tile_info->logical_extent_el.width);
1373
1374 assert(alignment_B == tile_info->phys_extent_B.width);
1375 return total_w_tl * tile_info->phys_extent_B.width;
1376 }
1377
1378 static uint32_t
1379 isl_calc_min_row_pitch(const struct isl_device *dev,
1380 const struct isl_surf_init_info *surf_info,
1381 const struct isl_tile_info *tile_info,
1382 const struct isl_extent2d *phys_total_el,
1383 uint32_t alignment_B)
1384 {
1385 if (tile_info->tiling == ISL_TILING_LINEAR) {
1386 return isl_calc_linear_min_row_pitch(dev, surf_info, phys_total_el,
1387 alignment_B);
1388 } else {
1389 return isl_calc_tiled_min_row_pitch(dev, surf_info, tile_info,
1390 phys_total_el, alignment_B);
1391 }
1392 }
1393
1394 /**
1395 * Is `pitch` in the valid range for a hardware bitfield, if the bitfield's
1396 * size is `bits` bits?
1397 *
1398 * Hardware pitch fields are offset by 1. For example, if the size of
1399 * RENDER_SURFACE_STATE::SurfacePitch is B bits, then the range of valid
1400 * pitches is [1, 2^b] inclusive. If the surface pitch is N, then
1401 * RENDER_SURFACE_STATE::SurfacePitch must be set to N-1.
1402 */
1403 static bool
1404 pitch_in_range(uint32_t n, uint32_t bits)
1405 {
1406 assert(n != 0);
1407 return likely(bits != 0 && 1 <= n && n <= (1 << bits));
1408 }
1409
1410 static bool
1411 isl_calc_row_pitch(const struct isl_device *dev,
1412 const struct isl_surf_init_info *surf_info,
1413 const struct isl_tile_info *tile_info,
1414 enum isl_dim_layout dim_layout,
1415 const struct isl_extent2d *phys_total_el,
1416 uint32_t *out_row_pitch_B)
1417 {
1418 uint32_t alignment_B =
1419 isl_calc_row_pitch_alignment(surf_info, tile_info);
1420
1421 const uint32_t min_row_pitch_B =
1422 isl_calc_min_row_pitch(dev, surf_info, tile_info, phys_total_el,
1423 alignment_B);
1424
1425 if (surf_info->row_pitch_B != 0) {
1426 if (surf_info->row_pitch_B < min_row_pitch_B)
1427 return false;
1428
1429 if (surf_info->row_pitch_B % alignment_B != 0)
1430 return false;
1431 }
1432
1433 const uint32_t row_pitch_B =
1434 surf_info->row_pitch_B != 0 ?
1435 surf_info->row_pitch_B :
1436 /* According to BSpec: 44930, Gen12's CCS-compressed surface pitches
1437 * must be 512B-aligned.
1438 */
1439 ISL_DEV_GEN(dev) >= 12 &&
1440 isl_format_supports_ccs_e(dev->info, surf_info->format) ?
1441 isl_align(min_row_pitch_B, 512) :
1442 /* Else */
1443 min_row_pitch_B;
1444
1445 const uint32_t row_pitch_tl = row_pitch_B / tile_info->phys_extent_B.width;
1446
1447 if (row_pitch_B == 0)
1448 return false;
1449
1450 if (dim_layout == ISL_DIM_LAYOUT_GEN9_1D) {
1451 /* SurfacePitch is ignored for this layout. */
1452 goto done;
1453 }
1454
1455 if ((surf_info->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
1456 ISL_SURF_USAGE_TEXTURE_BIT |
1457 ISL_SURF_USAGE_STORAGE_BIT)) &&
1458 !pitch_in_range(row_pitch_B, RENDER_SURFACE_STATE_SurfacePitch_bits(dev->info)))
1459 return false;
1460
1461 if ((surf_info->usage & (ISL_SURF_USAGE_CCS_BIT |
1462 ISL_SURF_USAGE_MCS_BIT)) &&
1463 !pitch_in_range(row_pitch_tl, RENDER_SURFACE_STATE_AuxiliarySurfacePitch_bits(dev->info)))
1464 return false;
1465
1466 if ((surf_info->usage & ISL_SURF_USAGE_DEPTH_BIT) &&
1467 !pitch_in_range(row_pitch_B, _3DSTATE_DEPTH_BUFFER_SurfacePitch_bits(dev->info)))
1468 return false;
1469
1470 if ((surf_info->usage & ISL_SURF_USAGE_HIZ_BIT) &&
1471 !pitch_in_range(row_pitch_B, _3DSTATE_HIER_DEPTH_BUFFER_SurfacePitch_bits(dev->info)))
1472 return false;
1473
1474 const uint32_t stencil_pitch_bits = dev->use_separate_stencil ?
1475 _3DSTATE_STENCIL_BUFFER_SurfacePitch_bits(dev->info) :
1476 _3DSTATE_DEPTH_BUFFER_SurfacePitch_bits(dev->info);
1477
1478 if ((surf_info->usage & ISL_SURF_USAGE_STENCIL_BIT) &&
1479 !pitch_in_range(row_pitch_B, stencil_pitch_bits))
1480 return false;
1481
1482 done:
1483 *out_row_pitch_B = row_pitch_B;
1484 return true;
1485 }
1486
1487 bool
1488 isl_surf_init_s(const struct isl_device *dev,
1489 struct isl_surf *surf,
1490 const struct isl_surf_init_info *restrict info)
1491 {
1492 const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1493
1494 const struct isl_extent4d logical_level0_px = {
1495 .w = info->width,
1496 .h = info->height,
1497 .d = info->depth,
1498 .a = info->array_len,
1499 };
1500
1501 enum isl_tiling tiling;
1502 if (!isl_surf_choose_tiling(dev, info, &tiling))
1503 return false;
1504
1505 struct isl_tile_info tile_info;
1506 isl_tiling_get_info(tiling, fmtl->bpb, &tile_info);
1507
1508 const enum isl_dim_layout dim_layout =
1509 isl_surf_choose_dim_layout(dev, info->dim, tiling, info->usage);
1510
1511 enum isl_msaa_layout msaa_layout;
1512 if (!isl_choose_msaa_layout(dev, info, tiling, &msaa_layout))
1513 return false;
1514
1515 struct isl_extent3d image_align_el;
1516 isl_choose_image_alignment_el(dev, info, tiling, dim_layout, msaa_layout,
1517 &image_align_el);
1518
1519 struct isl_extent3d image_align_sa =
1520 isl_extent3d_el_to_sa(info->format, image_align_el);
1521
1522 struct isl_extent4d phys_level0_sa;
1523 isl_calc_phys_level0_extent_sa(dev, info, dim_layout, tiling, msaa_layout,
1524 &phys_level0_sa);
1525
1526 enum isl_array_pitch_span array_pitch_span =
1527 isl_choose_array_pitch_span(dev, info, dim_layout, &phys_level0_sa);
1528
1529 uint32_t array_pitch_el_rows;
1530 struct isl_extent2d phys_total_el;
1531 isl_calc_phys_total_extent_el(dev, info, &tile_info,
1532 dim_layout, msaa_layout,
1533 &image_align_sa, &phys_level0_sa,
1534 array_pitch_span, &array_pitch_el_rows,
1535 &phys_total_el);
1536
1537 uint32_t row_pitch_B;
1538 if (!isl_calc_row_pitch(dev, info, &tile_info, dim_layout,
1539 &phys_total_el, &row_pitch_B))
1540 return false;
1541
1542 uint32_t base_alignment_B;
1543 uint64_t size_B;
1544 if (tiling == ISL_TILING_LINEAR) {
1545 size_B = (uint64_t) row_pitch_B * phys_total_el.h;
1546
1547 /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfaceBaseAddress:
1548 *
1549 * "The Base Address for linear render target surfaces and surfaces
1550 * accessed with the typed surface read/write data port messages must
1551 * be element-size aligned, for non-YUV surface formats, or a
1552 * multiple of 2 element-sizes for YUV surface formats. Other linear
1553 * surfaces have no alignment requirements (byte alignment is
1554 * sufficient.)"
1555 */
1556 base_alignment_B = MAX(1, info->min_alignment_B);
1557 if (info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1558 if (isl_format_is_yuv(info->format)) {
1559 base_alignment_B = MAX(base_alignment_B, fmtl->bpb / 4);
1560 } else {
1561 base_alignment_B = MAX(base_alignment_B, fmtl->bpb / 8);
1562 }
1563 }
1564 base_alignment_B = isl_round_up_to_power_of_two(base_alignment_B);
1565
1566 /* From the Skylake PRM Vol 2c, PLANE_STRIDE::Stride:
1567 *
1568 * "For Linear memory, this field specifies the stride in chunks of
1569 * 64 bytes (1 cache line)."
1570 */
1571 if (isl_surf_usage_is_display(info->usage))
1572 base_alignment_B = MAX(base_alignment_B, 64);
1573 } else {
1574 const uint32_t total_h_tl =
1575 isl_align_div(phys_total_el.h, tile_info.logical_extent_el.height);
1576
1577 size_B = (uint64_t) total_h_tl * tile_info.phys_extent_B.height * row_pitch_B;
1578
1579 const uint32_t tile_size_B = tile_info.phys_extent_B.width *
1580 tile_info.phys_extent_B.height;
1581 assert(isl_is_pow2(info->min_alignment_B) && isl_is_pow2(tile_size_B));
1582 base_alignment_B = MAX(info->min_alignment_B, tile_size_B);
1583
1584 /* The diagram in the Bspec section Memory Compression - Gen12, shows
1585 * that the CCS is indexed in 256B chunks. However, the
1586 * PLANE_AUX_DIST::Auxiliary Surface Distance field is in units of 4K
1587 * pages. We currently don't assign the usage field like we do for main
1588 * surfaces, so just use 4K for now.
1589 */
1590 if (tiling == ISL_TILING_GEN12_CCS)
1591 base_alignment_B = MAX(base_alignment_B, 4096);
1592 }
1593
1594 if (ISL_DEV_GEN(dev) >= 12) {
1595 base_alignment_B = MAX(base_alignment_B, 64 * 1024);
1596 }
1597
1598 if (ISL_DEV_GEN(dev) < 9) {
1599 /* From the Broadwell PRM Vol 5, Surface Layout:
1600 *
1601 * "In addition to restrictions on maximum height, width, and depth,
1602 * surfaces are also restricted to a maximum size in bytes. This
1603 * maximum is 2 GB for all products and all surface types."
1604 *
1605 * This comment is applicable to all Pre-gen9 platforms.
1606 */
1607 if (size_B > (uint64_t) 1 << 31)
1608 return false;
1609 } else if (ISL_DEV_GEN(dev) < 11) {
1610 /* From the Skylake PRM Vol 5, Maximum Surface Size in Bytes:
1611 * "In addition to restrictions on maximum height, width, and depth,
1612 * surfaces are also restricted to a maximum size of 2^38 bytes.
1613 * All pixels within the surface must be contained within 2^38 bytes
1614 * of the base address."
1615 */
1616 if (size_B > (uint64_t) 1 << 38)
1617 return false;
1618 } else {
1619 /* gen11+ platforms raised this limit to 2^44 bytes. */
1620 if (size_B > (uint64_t) 1 << 44)
1621 return false;
1622 }
1623
1624 *surf = (struct isl_surf) {
1625 .dim = info->dim,
1626 .dim_layout = dim_layout,
1627 .msaa_layout = msaa_layout,
1628 .tiling = tiling,
1629 .format = info->format,
1630
1631 .levels = info->levels,
1632 .samples = info->samples,
1633
1634 .image_alignment_el = image_align_el,
1635 .logical_level0_px = logical_level0_px,
1636 .phys_level0_sa = phys_level0_sa,
1637
1638 .size_B = size_B,
1639 .alignment_B = base_alignment_B,
1640 .row_pitch_B = row_pitch_B,
1641 .array_pitch_el_rows = array_pitch_el_rows,
1642 .array_pitch_span = array_pitch_span,
1643
1644 .usage = info->usage,
1645 };
1646
1647 return true;
1648 }
1649
1650 void
1651 isl_surf_get_tile_info(const struct isl_surf *surf,
1652 struct isl_tile_info *tile_info)
1653 {
1654 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1655 isl_tiling_get_info(surf->tiling, fmtl->bpb, tile_info);
1656 }
1657
1658 bool
1659 isl_surf_get_hiz_surf(const struct isl_device *dev,
1660 const struct isl_surf *surf,
1661 struct isl_surf *hiz_surf)
1662 {
1663 assert(ISL_DEV_GEN(dev) >= 5 && ISL_DEV_USE_SEPARATE_STENCIL(dev));
1664
1665 if (!isl_surf_usage_is_depth(surf->usage))
1666 return false;
1667
1668 /* HiZ only works with Y-tiled depth buffers */
1669 if (!isl_tiling_is_any_y(surf->tiling))
1670 return false;
1671
1672 /* On SNB+, compressed depth buffers cannot be interleaved with stencil. */
1673 switch (surf->format) {
1674 case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
1675 if (isl_surf_usage_is_depth_and_stencil(surf->usage)) {
1676 assert(ISL_DEV_GEN(dev) == 5);
1677 unreachable("This should work, but is untested");
1678 }
1679 /* Fall through */
1680 case ISL_FORMAT_R16_UNORM:
1681 case ISL_FORMAT_R32_FLOAT:
1682 break;
1683 case ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS:
1684 if (ISL_DEV_GEN(dev) == 5) {
1685 assert(isl_surf_usage_is_depth_and_stencil(surf->usage));
1686 unreachable("This should work, but is untested");
1687 }
1688 /* Fall through */
1689 default:
1690 return false;
1691 }
1692
1693 /* Multisampled depth is always interleaved */
1694 assert(surf->msaa_layout == ISL_MSAA_LAYOUT_NONE ||
1695 surf->msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1696
1697 /* From the Broadwell PRM Vol. 7, "Hierarchical Depth Buffer":
1698 *
1699 * "The Surface Type, Height, Width, Depth, Minimum Array Element, Render
1700 * Target View Extent, and Depth Coordinate Offset X/Y of the
1701 * hierarchical depth buffer are inherited from the depth buffer. The
1702 * height and width of the hierarchical depth buffer that must be
1703 * allocated are computed by the following formulas, where HZ is the
1704 * hierarchical depth buffer and Z is the depth buffer. The Z_Height,
1705 * Z_Width, and Z_Depth values given in these formulas are those present
1706 * in 3DSTATE_DEPTH_BUFFER incremented by one.
1707 *
1708 * "The value of Z_Height and Z_Width must each be multiplied by 2 before
1709 * being applied to the table below if Number of Multisamples is set to
1710 * NUMSAMPLES_4. The value of Z_Height must be multiplied by 2 and
1711 * Z_Width must be multiplied by 4 before being applied to the table
1712 * below if Number of Multisamples is set to NUMSAMPLES_8."
1713 *
1714 * In the Sky Lake PRM, the second paragraph is replaced with this:
1715 *
1716 * "The Z_Height and Z_Width values must equal those present in
1717 * 3DSTATE_DEPTH_BUFFER incremented by one."
1718 *
1719 * In other words, on Sandy Bridge through Broadwell, each 128-bit HiZ
1720 * block corresponds to a region of 8x4 samples in the primary depth
1721 * surface. On Sky Lake, on the other hand, each HiZ block corresponds to
1722 * a region of 8x4 pixels in the primary depth surface regardless of the
1723 * number of samples. The dimensions of a HiZ block in both pixels and
1724 * samples are given in the table below:
1725 *
1726 * | SNB - BDW | SKL+
1727 * ------+-----------+-------------
1728 * 1x | 8 x 4 sa | 8 x 4 sa
1729 * MSAA | 8 x 4 px | 8 x 4 px
1730 * ------+-----------+-------------
1731 * 2x | 8 x 4 sa | 16 x 4 sa
1732 * MSAA | 4 x 4 px | 8 x 4 px
1733 * ------+-----------+-------------
1734 * 4x | 8 x 4 sa | 16 x 8 sa
1735 * MSAA | 4 x 2 px | 8 x 4 px
1736 * ------+-----------+-------------
1737 * 8x | 8 x 4 sa | 32 x 8 sa
1738 * MSAA | 2 x 2 px | 8 x 4 px
1739 * ------+-----------+-------------
1740 * 16x | N/A | 32 x 16 sa
1741 * MSAA | N/A | 8 x 4 px
1742 * ------+-----------+-------------
1743 *
1744 * There are a number of different ways that this discrepency could be
1745 * handled. The way we have chosen is to simply make MSAA HiZ have the
1746 * same number of samples as the parent surface pre-Sky Lake and always be
1747 * single-sampled on Sky Lake and above. Since the block sizes of
1748 * compressed formats are given in samples, this neatly handles everything
1749 * without the need for additional HiZ formats with different block sizes
1750 * on SKL+.
1751 */
1752 const unsigned samples = ISL_DEV_GEN(dev) >= 9 ? 1 : surf->samples;
1753
1754 return isl_surf_init(dev, hiz_surf,
1755 .dim = surf->dim,
1756 .format = ISL_FORMAT_HIZ,
1757 .width = surf->logical_level0_px.width,
1758 .height = surf->logical_level0_px.height,
1759 .depth = surf->logical_level0_px.depth,
1760 .levels = surf->levels,
1761 .array_len = surf->logical_level0_px.array_len,
1762 .samples = samples,
1763 .usage = ISL_SURF_USAGE_HIZ_BIT,
1764 .tiling_flags = ISL_TILING_HIZ_BIT);
1765 }
1766
1767 bool
1768 isl_surf_get_mcs_surf(const struct isl_device *dev,
1769 const struct isl_surf *surf,
1770 struct isl_surf *mcs_surf)
1771 {
1772 /* It must be multisampled with an array layout */
1773 if (surf->msaa_layout != ISL_MSAA_LAYOUT_ARRAY)
1774 return false;
1775
1776 /* The following are true of all multisampled surfaces */
1777 assert(surf->samples > 1);
1778 assert(surf->dim == ISL_SURF_DIM_2D);
1779 assert(surf->levels == 1);
1780 assert(surf->logical_level0_px.depth == 1);
1781
1782 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
1783 *
1784 * This field must be set to 0 for all SINT MSRTs when all RT channels
1785 * are not written
1786 *
1787 * In practice this means that we have to disable MCS for all signed
1788 * integer MSAA buffers. The alternative, to disable MCS only when one
1789 * of the render target channels is disabled, is impractical because it
1790 * would require converting between CMS and UMS MSAA layouts on the fly,
1791 * which is expensive.
1792 */
1793 if (ISL_DEV_GEN(dev) == 7 && isl_format_has_sint_channel(surf->format))
1794 return false;
1795
1796 /* The "Auxiliary Surface Pitch" field in RENDER_SURFACE_STATE is only 9
1797 * bits which means the maximum pitch of a compression surface is 512
1798 * tiles or 64KB (since MCS is always Y-tiled). Since a 16x MCS buffer is
1799 * 64bpp, this gives us a maximum width of 8192 pixels. We can create
1800 * larger multisampled surfaces, we just can't compress them. For 2x, 4x,
1801 * and 8x, we have enough room for the full 16k supported by the hardware.
1802 */
1803 if (surf->samples == 16 && surf->logical_level0_px.width > 8192)
1804 return false;
1805
1806 enum isl_format mcs_format;
1807 switch (surf->samples) {
1808 case 2: mcs_format = ISL_FORMAT_MCS_2X; break;
1809 case 4: mcs_format = ISL_FORMAT_MCS_4X; break;
1810 case 8: mcs_format = ISL_FORMAT_MCS_8X; break;
1811 case 16: mcs_format = ISL_FORMAT_MCS_16X; break;
1812 default:
1813 unreachable("Invalid sample count");
1814 }
1815
1816 return isl_surf_init(dev, mcs_surf,
1817 .dim = ISL_SURF_DIM_2D,
1818 .format = mcs_format,
1819 .width = surf->logical_level0_px.width,
1820 .height = surf->logical_level0_px.height,
1821 .depth = 1,
1822 .levels = 1,
1823 .array_len = surf->logical_level0_px.array_len,
1824 .samples = 1, /* MCS surfaces are really single-sampled */
1825 .usage = ISL_SURF_USAGE_MCS_BIT,
1826 .tiling_flags = ISL_TILING_Y0_BIT);
1827 }
1828
1829 bool
1830 isl_surf_get_ccs_surf(const struct isl_device *dev,
1831 const struct isl_surf *surf,
1832 struct isl_surf *ccs_surf,
1833 uint32_t row_pitch_B)
1834 {
1835 if (surf->samples > 1)
1836 return false;
1837
1838 assert(surf->msaa_layout == ISL_MSAA_LAYOUT_NONE);
1839
1840 /* CCS support does not exist prior to Gen7 */
1841 if (ISL_DEV_GEN(dev) <= 6)
1842 return false;
1843
1844 if (surf->usage & ISL_SURF_USAGE_DISABLE_AUX_BIT)
1845 return false;
1846
1847 /* Callers don't yet support this configuration. */
1848 if (isl_surf_usage_is_depth_or_stencil(surf->usage))
1849 return false;
1850
1851 /* The PRM doesn't say this explicitly, but fast-clears don't appear to
1852 * work for 3D textures until gen9 where the layout of 3D textures changes
1853 * to match 2D array textures.
1854 */
1855 if (ISL_DEV_GEN(dev) <= 8 && surf->dim != ISL_SURF_DIM_2D)
1856 return false;
1857
1858 /* From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652 (Color Clear of
1859 * Non-MultiSampler Render Target Restrictions):
1860 *
1861 * "Support is for non-mip-mapped and non-array surface types only."
1862 *
1863 * This restriction is lifted on gen8+. Technically, it may be possible to
1864 * create a CCS for an arrayed or mipmapped image and only enable CCS_D
1865 * when rendering to the base slice. However, there is no documentation
1866 * tell us what the hardware would do in that case or what it does if you
1867 * walk off the bases slice. (Does it ignore CCS or does it start
1868 * scribbling over random memory?) We play it safe and just follow the
1869 * docs and don't allow CCS_D for arrayed or mip-mapped surfaces.
1870 */
1871 if (ISL_DEV_GEN(dev) <= 7 &&
1872 (surf->levels > 1 || surf->logical_level0_px.array_len > 1))
1873 return false;
1874
1875 /* On Gen12, 8BPP surfaces cannot be compressed if any level is not
1876 * 32Bx4row-aligned. For now, just reject the cases where alignment
1877 * matters.
1878 */
1879 if (ISL_DEV_GEN(dev) >= 12 &&
1880 isl_format_get_layout(surf->format)->bpb == 8 && surf->levels >= 3) {
1881 isl_finishme("%s:%s: CCS for 8BPP textures with 3+ miplevels is "
1882 "disabled, but support for more levels is possible.",
1883 __FILE__, __func__);
1884 return false;
1885 }
1886
1887 /* On Gen12, all CCS-compressed surface pitches must be multiples of 512B.
1888 */
1889 if (ISL_DEV_GEN(dev) >= 12 && surf->row_pitch_B % 512 != 0)
1890 return false;
1891
1892 if (isl_format_is_compressed(surf->format))
1893 return false;
1894
1895 /* According to GEN:BUG:1406738321, 3D textures need a blit to a new
1896 * surface in order to perform a resolve. For now, just disable CCS.
1897 */
1898 if (ISL_DEV_GEN(dev) >= 12 && surf->dim == ISL_SURF_DIM_3D) {
1899 isl_finishme("%s:%s: CCS for 3D textures is disabled, but a workaround"
1900 " is available.", __FILE__, __func__);
1901 return false;
1902 }
1903
1904 /* TODO: More conditions where it can fail. */
1905
1906 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
1907 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
1908 *
1909 * - Support is limited to tiled render targets.
1910 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
1911 * 64bpp, and 128bpp.
1912 *
1913 * From the Skylake documentation, it is made clear that X-tiling is no
1914 * longer supported:
1915 *
1916 * - MCS and Lossless compression is supported for
1917 * TiledY/TileYs/TileYf non-MSRTs only.
1918 */
1919 enum isl_format ccs_format;
1920 if (ISL_DEV_GEN(dev) >= 12) {
1921 /* TODO: Handle the other tiling formats */
1922 if (surf->tiling != ISL_TILING_Y0)
1923 return false;
1924
1925 switch (isl_format_get_layout(surf->format)->bpb) {
1926 case 8: ccs_format = ISL_FORMAT_GEN12_CCS_8BPP_Y0; break;
1927 case 16: ccs_format = ISL_FORMAT_GEN12_CCS_16BPP_Y0; break;
1928 case 32: ccs_format = ISL_FORMAT_GEN12_CCS_32BPP_Y0; break;
1929 case 64: ccs_format = ISL_FORMAT_GEN12_CCS_64BPP_Y0; break;
1930 case 128: ccs_format = ISL_FORMAT_GEN12_CCS_128BPP_Y0; break;
1931 default:
1932 return false;
1933 }
1934 } else if (ISL_DEV_GEN(dev) >= 9) {
1935 if (!isl_tiling_is_any_y(surf->tiling))
1936 return false;
1937
1938 switch (isl_format_get_layout(surf->format)->bpb) {
1939 case 32: ccs_format = ISL_FORMAT_GEN9_CCS_32BPP; break;
1940 case 64: ccs_format = ISL_FORMAT_GEN9_CCS_64BPP; break;
1941 case 128: ccs_format = ISL_FORMAT_GEN9_CCS_128BPP; break;
1942 default:
1943 return false;
1944 }
1945 } else if (surf->tiling == ISL_TILING_Y0) {
1946 switch (isl_format_get_layout(surf->format)->bpb) {
1947 case 32: ccs_format = ISL_FORMAT_GEN7_CCS_32BPP_Y; break;
1948 case 64: ccs_format = ISL_FORMAT_GEN7_CCS_64BPP_Y; break;
1949 case 128: ccs_format = ISL_FORMAT_GEN7_CCS_128BPP_Y; break;
1950 default:
1951 return false;
1952 }
1953 } else if (surf->tiling == ISL_TILING_X) {
1954 switch (isl_format_get_layout(surf->format)->bpb) {
1955 case 32: ccs_format = ISL_FORMAT_GEN7_CCS_32BPP_X; break;
1956 case 64: ccs_format = ISL_FORMAT_GEN7_CCS_64BPP_X; break;
1957 case 128: ccs_format = ISL_FORMAT_GEN7_CCS_128BPP_X; break;
1958 default:
1959 return false;
1960 }
1961 } else {
1962 return false;
1963 }
1964
1965 if (ISL_DEV_GEN(dev) >= 12) {
1966 /* On Gen12, the CCS is a scaled-down version of the main surface. We
1967 * model this as the CCS compressing a 2D-view of the entire surface.
1968 */
1969 const bool ok =
1970 isl_surf_init(dev, ccs_surf,
1971 .dim = ISL_SURF_DIM_2D,
1972 .format = ccs_format,
1973 .width = isl_surf_get_row_pitch_el(surf),
1974 .height = surf->size_B / surf->row_pitch_B,
1975 .depth = 1,
1976 .levels = 1,
1977 .array_len = 1,
1978 .samples = 1,
1979 .row_pitch_B = row_pitch_B,
1980 .usage = ISL_SURF_USAGE_CCS_BIT,
1981 .tiling_flags = ISL_TILING_GEN12_CCS_BIT);
1982 assert(!ok || ccs_surf->size_B == surf->size_B / 256);
1983 return ok;
1984 } else {
1985 return isl_surf_init(dev, ccs_surf,
1986 .dim = surf->dim,
1987 .format = ccs_format,
1988 .width = surf->logical_level0_px.width,
1989 .height = surf->logical_level0_px.height,
1990 .depth = surf->logical_level0_px.depth,
1991 .levels = surf->levels,
1992 .array_len = surf->logical_level0_px.array_len,
1993 .samples = 1,
1994 .row_pitch_B = row_pitch_B,
1995 .usage = ISL_SURF_USAGE_CCS_BIT,
1996 .tiling_flags = ISL_TILING_CCS_BIT);
1997 }
1998 }
1999
2000 #define isl_genX_call(dev, func, ...) \
2001 switch (ISL_DEV_GEN(dev)) { \
2002 case 4: \
2003 /* G45 surface state is the same as gen5 */ \
2004 if (ISL_DEV_IS_G4X(dev)) { \
2005 isl_gen5_##func(__VA_ARGS__); \
2006 } else { \
2007 isl_gen4_##func(__VA_ARGS__); \
2008 } \
2009 break; \
2010 case 5: \
2011 isl_gen5_##func(__VA_ARGS__); \
2012 break; \
2013 case 6: \
2014 isl_gen6_##func(__VA_ARGS__); \
2015 break; \
2016 case 7: \
2017 if (ISL_DEV_IS_HASWELL(dev)) { \
2018 isl_gen75_##func(__VA_ARGS__); \
2019 } else { \
2020 isl_gen7_##func(__VA_ARGS__); \
2021 } \
2022 break; \
2023 case 8: \
2024 isl_gen8_##func(__VA_ARGS__); \
2025 break; \
2026 case 9: \
2027 isl_gen9_##func(__VA_ARGS__); \
2028 break; \
2029 case 10: \
2030 isl_gen10_##func(__VA_ARGS__); \
2031 break; \
2032 case 11: \
2033 isl_gen11_##func(__VA_ARGS__); \
2034 break; \
2035 case 12: \
2036 isl_gen12_##func(__VA_ARGS__); \
2037 break; \
2038 default: \
2039 assert(!"Unknown hardware generation"); \
2040 }
2041
2042 void
2043 isl_surf_fill_state_s(const struct isl_device *dev, void *state,
2044 const struct isl_surf_fill_state_info *restrict info)
2045 {
2046 #ifndef NDEBUG
2047 isl_surf_usage_flags_t _base_usage =
2048 info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
2049 ISL_SURF_USAGE_TEXTURE_BIT |
2050 ISL_SURF_USAGE_STORAGE_BIT);
2051 /* They may only specify one of the above bits at a time */
2052 assert(__builtin_popcount(_base_usage) == 1);
2053 /* The only other allowed bit is ISL_SURF_USAGE_CUBE_BIT */
2054 assert((info->view->usage & ~ISL_SURF_USAGE_CUBE_BIT) == _base_usage);
2055 #endif
2056
2057 if (info->surf->dim == ISL_SURF_DIM_3D) {
2058 assert(info->view->base_array_layer + info->view->array_len <=
2059 info->surf->logical_level0_px.depth);
2060 } else {
2061 assert(info->view->base_array_layer + info->view->array_len <=
2062 info->surf->logical_level0_px.array_len);
2063 }
2064
2065 isl_genX_call(dev, surf_fill_state_s, dev, state, info);
2066 }
2067
2068 void
2069 isl_buffer_fill_state_s(const struct isl_device *dev, void *state,
2070 const struct isl_buffer_fill_state_info *restrict info)
2071 {
2072 isl_genX_call(dev, buffer_fill_state_s, state, info);
2073 }
2074
2075 void
2076 isl_null_fill_state(const struct isl_device *dev, void *state,
2077 struct isl_extent3d size)
2078 {
2079 isl_genX_call(dev, null_fill_state, state, size);
2080 }
2081
2082 void
2083 isl_emit_depth_stencil_hiz_s(const struct isl_device *dev, void *batch,
2084 const struct isl_depth_stencil_hiz_emit_info *restrict info)
2085 {
2086 if (info->depth_surf && info->stencil_surf) {
2087 if (!dev->info->has_hiz_and_separate_stencil) {
2088 assert(info->depth_surf == info->stencil_surf);
2089 assert(info->depth_address == info->stencil_address);
2090 }
2091 assert(info->depth_surf->dim == info->stencil_surf->dim);
2092 }
2093
2094 if (info->depth_surf) {
2095 assert((info->depth_surf->usage & ISL_SURF_USAGE_DEPTH_BIT));
2096 if (info->depth_surf->dim == ISL_SURF_DIM_3D) {
2097 assert(info->view->base_array_layer + info->view->array_len <=
2098 info->depth_surf->logical_level0_px.depth);
2099 } else {
2100 assert(info->view->base_array_layer + info->view->array_len <=
2101 info->depth_surf->logical_level0_px.array_len);
2102 }
2103 }
2104
2105 if (info->stencil_surf) {
2106 assert((info->stencil_surf->usage & ISL_SURF_USAGE_STENCIL_BIT));
2107 if (info->stencil_surf->dim == ISL_SURF_DIM_3D) {
2108 assert(info->view->base_array_layer + info->view->array_len <=
2109 info->stencil_surf->logical_level0_px.depth);
2110 } else {
2111 assert(info->view->base_array_layer + info->view->array_len <=
2112 info->stencil_surf->logical_level0_px.array_len);
2113 }
2114 }
2115
2116 isl_genX_call(dev, emit_depth_stencil_hiz_s, dev, batch, info);
2117 }
2118
2119 /**
2120 * A variant of isl_surf_get_image_offset_sa() specific to
2121 * ISL_DIM_LAYOUT_GEN4_2D.
2122 */
2123 static void
2124 get_image_offset_sa_gen4_2d(const struct isl_surf *surf,
2125 uint32_t level, uint32_t logical_array_layer,
2126 uint32_t *x_offset_sa,
2127 uint32_t *y_offset_sa)
2128 {
2129 assert(level < surf->levels);
2130 if (surf->dim == ISL_SURF_DIM_3D)
2131 assert(logical_array_layer < surf->logical_level0_px.depth);
2132 else
2133 assert(logical_array_layer < surf->logical_level0_px.array_len);
2134
2135 const struct isl_extent3d image_align_sa =
2136 isl_surf_get_image_alignment_sa(surf);
2137
2138 const uint32_t W0 = surf->phys_level0_sa.width;
2139 const uint32_t H0 = surf->phys_level0_sa.height;
2140
2141 const uint32_t phys_layer = logical_array_layer *
2142 (surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY ? surf->samples : 1);
2143
2144 uint32_t x = 0;
2145 uint32_t y = phys_layer * isl_surf_get_array_pitch_sa_rows(surf);
2146
2147 for (uint32_t l = 0; l < level; ++l) {
2148 if (l == 1) {
2149 uint32_t W = isl_minify(W0, l);
2150 x += isl_align_npot(W, image_align_sa.w);
2151 } else {
2152 uint32_t H = isl_minify(H0, l);
2153 y += isl_align_npot(H, image_align_sa.h);
2154 }
2155 }
2156
2157 *x_offset_sa = x;
2158 *y_offset_sa = y;
2159 }
2160
2161 /**
2162 * A variant of isl_surf_get_image_offset_sa() specific to
2163 * ISL_DIM_LAYOUT_GEN4_3D.
2164 */
2165 static void
2166 get_image_offset_sa_gen4_3d(const struct isl_surf *surf,
2167 uint32_t level, uint32_t logical_z_offset_px,
2168 uint32_t *x_offset_sa,
2169 uint32_t *y_offset_sa)
2170 {
2171 assert(level < surf->levels);
2172 if (surf->dim == ISL_SURF_DIM_3D) {
2173 assert(surf->phys_level0_sa.array_len == 1);
2174 assert(logical_z_offset_px < isl_minify(surf->phys_level0_sa.depth, level));
2175 } else {
2176 assert(surf->dim == ISL_SURF_DIM_2D);
2177 assert(surf->usage & ISL_SURF_USAGE_CUBE_BIT);
2178 assert(surf->phys_level0_sa.array_len == 6);
2179 assert(logical_z_offset_px < surf->phys_level0_sa.array_len);
2180 }
2181
2182 const struct isl_extent3d image_align_sa =
2183 isl_surf_get_image_alignment_sa(surf);
2184
2185 const uint32_t W0 = surf->phys_level0_sa.width;
2186 const uint32_t H0 = surf->phys_level0_sa.height;
2187 const uint32_t D0 = surf->phys_level0_sa.depth;
2188 const uint32_t AL = surf->phys_level0_sa.array_len;
2189
2190 uint32_t x = 0;
2191 uint32_t y = 0;
2192
2193 for (uint32_t l = 0; l < level; ++l) {
2194 const uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa.h);
2195 const uint32_t level_d =
2196 isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, l) : AL,
2197 image_align_sa.d);
2198 const uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
2199
2200 y += level_h * max_layers_vert;
2201 }
2202
2203 const uint32_t level_w = isl_align_npot(isl_minify(W0, level), image_align_sa.w);
2204 const uint32_t level_h = isl_align_npot(isl_minify(H0, level), image_align_sa.h);
2205 const uint32_t level_d =
2206 isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, level) : AL,
2207 image_align_sa.d);
2208
2209 const uint32_t max_layers_horiz = MIN(level_d, 1u << level);
2210
2211 x += level_w * (logical_z_offset_px % max_layers_horiz);
2212 y += level_h * (logical_z_offset_px / max_layers_horiz);
2213
2214 *x_offset_sa = x;
2215 *y_offset_sa = y;
2216 }
2217
2218 static void
2219 get_image_offset_sa_gen6_stencil_hiz(const struct isl_surf *surf,
2220 uint32_t level,
2221 uint32_t logical_array_layer,
2222 uint32_t *x_offset_sa,
2223 uint32_t *y_offset_sa)
2224 {
2225 assert(level < surf->levels);
2226 assert(surf->logical_level0_px.depth == 1);
2227 assert(logical_array_layer < surf->logical_level0_px.array_len);
2228
2229 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2230
2231 const struct isl_extent3d image_align_sa =
2232 isl_surf_get_image_alignment_sa(surf);
2233
2234 struct isl_tile_info tile_info;
2235 isl_tiling_get_info(surf->tiling, fmtl->bpb, &tile_info);
2236 const struct isl_extent2d tile_extent_sa = {
2237 .w = tile_info.logical_extent_el.w * fmtl->bw,
2238 .h = tile_info.logical_extent_el.h * fmtl->bh,
2239 };
2240 /* Tile size is a multiple of image alignment */
2241 assert(tile_extent_sa.w % image_align_sa.w == 0);
2242 assert(tile_extent_sa.h % image_align_sa.h == 0);
2243
2244 const uint32_t W0 = surf->phys_level0_sa.w;
2245 const uint32_t H0 = surf->phys_level0_sa.h;
2246
2247 /* Each image has the same height as LOD0 because the hardware thinks
2248 * everything is LOD0
2249 */
2250 const uint32_t H = isl_align(H0, image_align_sa.h);
2251
2252 /* Quick sanity check for consistency */
2253 if (surf->phys_level0_sa.array_len > 1)
2254 assert(surf->array_pitch_el_rows == isl_assert_div(H, fmtl->bh));
2255
2256 uint32_t x = 0, y = 0;
2257 for (uint32_t l = 0; l < level; ++l) {
2258 const uint32_t W = isl_minify(W0, l);
2259
2260 const uint32_t w = isl_align(W, tile_extent_sa.w);
2261 const uint32_t h = isl_align(H * surf->phys_level0_sa.a,
2262 tile_extent_sa.h);
2263
2264 if (l == 0) {
2265 y += h;
2266 } else {
2267 x += w;
2268 }
2269 }
2270
2271 y += H * logical_array_layer;
2272
2273 *x_offset_sa = x;
2274 *y_offset_sa = y;
2275 }
2276
2277 /**
2278 * A variant of isl_surf_get_image_offset_sa() specific to
2279 * ISL_DIM_LAYOUT_GEN9_1D.
2280 */
2281 static void
2282 get_image_offset_sa_gen9_1d(const struct isl_surf *surf,
2283 uint32_t level, uint32_t layer,
2284 uint32_t *x_offset_sa,
2285 uint32_t *y_offset_sa)
2286 {
2287 assert(level < surf->levels);
2288 assert(layer < surf->phys_level0_sa.array_len);
2289 assert(surf->phys_level0_sa.height == 1);
2290 assert(surf->phys_level0_sa.depth == 1);
2291 assert(surf->samples == 1);
2292
2293 const uint32_t W0 = surf->phys_level0_sa.width;
2294 const struct isl_extent3d image_align_sa =
2295 isl_surf_get_image_alignment_sa(surf);
2296
2297 uint32_t x = 0;
2298
2299 for (uint32_t l = 0; l < level; ++l) {
2300 uint32_t W = isl_minify(W0, l);
2301 uint32_t w = isl_align_npot(W, image_align_sa.w);
2302
2303 x += w;
2304 }
2305
2306 *x_offset_sa = x;
2307 *y_offset_sa = layer * isl_surf_get_array_pitch_sa_rows(surf);
2308 }
2309
2310 /**
2311 * Calculate the offset, in units of surface samples, to a subimage in the
2312 * surface.
2313 *
2314 * @invariant level < surface levels
2315 * @invariant logical_array_layer < logical array length of surface
2316 * @invariant logical_z_offset_px < logical depth of surface at level
2317 */
2318 void
2319 isl_surf_get_image_offset_sa(const struct isl_surf *surf,
2320 uint32_t level,
2321 uint32_t logical_array_layer,
2322 uint32_t logical_z_offset_px,
2323 uint32_t *x_offset_sa,
2324 uint32_t *y_offset_sa)
2325 {
2326 assert(level < surf->levels);
2327 assert(logical_array_layer < surf->logical_level0_px.array_len);
2328 assert(logical_z_offset_px
2329 < isl_minify(surf->logical_level0_px.depth, level));
2330
2331 switch (surf->dim_layout) {
2332 case ISL_DIM_LAYOUT_GEN9_1D:
2333 get_image_offset_sa_gen9_1d(surf, level, logical_array_layer,
2334 x_offset_sa, y_offset_sa);
2335 break;
2336 case ISL_DIM_LAYOUT_GEN4_2D:
2337 get_image_offset_sa_gen4_2d(surf, level, logical_array_layer
2338 + logical_z_offset_px,
2339 x_offset_sa, y_offset_sa);
2340 break;
2341 case ISL_DIM_LAYOUT_GEN4_3D:
2342 get_image_offset_sa_gen4_3d(surf, level, logical_array_layer +
2343 logical_z_offset_px,
2344 x_offset_sa, y_offset_sa);
2345 break;
2346 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ:
2347 get_image_offset_sa_gen6_stencil_hiz(surf, level, logical_array_layer +
2348 logical_z_offset_px,
2349 x_offset_sa, y_offset_sa);
2350 break;
2351
2352 default:
2353 unreachable("not reached");
2354 }
2355 }
2356
2357 void
2358 isl_surf_get_image_offset_el(const struct isl_surf *surf,
2359 uint32_t level,
2360 uint32_t logical_array_layer,
2361 uint32_t logical_z_offset_px,
2362 uint32_t *x_offset_el,
2363 uint32_t *y_offset_el)
2364 {
2365 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2366
2367 assert(level < surf->levels);
2368 assert(logical_array_layer < surf->logical_level0_px.array_len);
2369 assert(logical_z_offset_px
2370 < isl_minify(surf->logical_level0_px.depth, level));
2371
2372 uint32_t x_offset_sa, y_offset_sa;
2373 isl_surf_get_image_offset_sa(surf, level,
2374 logical_array_layer,
2375 logical_z_offset_px,
2376 &x_offset_sa,
2377 &y_offset_sa);
2378
2379 *x_offset_el = x_offset_sa / fmtl->bw;
2380 *y_offset_el = y_offset_sa / fmtl->bh;
2381 }
2382
2383 void
2384 isl_surf_get_image_offset_B_tile_sa(const struct isl_surf *surf,
2385 uint32_t level,
2386 uint32_t logical_array_layer,
2387 uint32_t logical_z_offset_px,
2388 uint32_t *offset_B,
2389 uint32_t *x_offset_sa,
2390 uint32_t *y_offset_sa)
2391 {
2392 const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2393
2394 uint32_t total_x_offset_el, total_y_offset_el;
2395 isl_surf_get_image_offset_el(surf, level, logical_array_layer,
2396 logical_z_offset_px,
2397 &total_x_offset_el,
2398 &total_y_offset_el);
2399
2400 uint32_t x_offset_el, y_offset_el;
2401 isl_tiling_get_intratile_offset_el(surf->tiling, fmtl->bpb,
2402 surf->row_pitch_B,
2403 total_x_offset_el,
2404 total_y_offset_el,
2405 offset_B,
2406 &x_offset_el,
2407 &y_offset_el);
2408
2409 if (x_offset_sa) {
2410 *x_offset_sa = x_offset_el * fmtl->bw;
2411 } else {
2412 assert(x_offset_el == 0);
2413 }
2414
2415 if (y_offset_sa) {
2416 *y_offset_sa = y_offset_el * fmtl->bh;
2417 } else {
2418 assert(y_offset_el == 0);
2419 }
2420 }
2421
2422 void
2423 isl_surf_get_image_surf(const struct isl_device *dev,
2424 const struct isl_surf *surf,
2425 uint32_t level,
2426 uint32_t logical_array_layer,
2427 uint32_t logical_z_offset_px,
2428 struct isl_surf *image_surf,
2429 uint32_t *offset_B,
2430 uint32_t *x_offset_sa,
2431 uint32_t *y_offset_sa)
2432 {
2433 isl_surf_get_image_offset_B_tile_sa(surf,
2434 level,
2435 logical_array_layer,
2436 logical_z_offset_px,
2437 offset_B,
2438 x_offset_sa,
2439 y_offset_sa);
2440
2441 /* Even for cube maps there will be only single face, therefore drop the
2442 * corresponding flag if present.
2443 */
2444 const isl_surf_usage_flags_t usage =
2445 surf->usage & (~ISL_SURF_USAGE_CUBE_BIT);
2446
2447 bool ok UNUSED;
2448 ok = isl_surf_init(dev, image_surf,
2449 .dim = ISL_SURF_DIM_2D,
2450 .format = surf->format,
2451 .width = isl_minify(surf->logical_level0_px.w, level),
2452 .height = isl_minify(surf->logical_level0_px.h, level),
2453 .depth = 1,
2454 .levels = 1,
2455 .array_len = 1,
2456 .samples = surf->samples,
2457 .row_pitch_B = surf->row_pitch_B,
2458 .usage = usage,
2459 .tiling_flags = (1 << surf->tiling));
2460 assert(ok);
2461 }
2462
2463 void
2464 isl_tiling_get_intratile_offset_el(enum isl_tiling tiling,
2465 uint32_t bpb,
2466 uint32_t row_pitch_B,
2467 uint32_t total_x_offset_el,
2468 uint32_t total_y_offset_el,
2469 uint32_t *base_address_offset,
2470 uint32_t *x_offset_el,
2471 uint32_t *y_offset_el)
2472 {
2473 if (tiling == ISL_TILING_LINEAR) {
2474 assert(bpb % 8 == 0);
2475 *base_address_offset = total_y_offset_el * row_pitch_B +
2476 total_x_offset_el * (bpb / 8);
2477 *x_offset_el = 0;
2478 *y_offset_el = 0;
2479 return;
2480 }
2481
2482 struct isl_tile_info tile_info;
2483 isl_tiling_get_info(tiling, bpb, &tile_info);
2484
2485 assert(row_pitch_B % tile_info.phys_extent_B.width == 0);
2486
2487 /* For non-power-of-two formats, we need the address to be both tile and
2488 * element-aligned. The easiest way to achieve this is to work with a tile
2489 * that is three times as wide as the regular tile.
2490 *
2491 * The tile info returned by get_tile_info has a logical size that is an
2492 * integer number of tile_info.format_bpb size elements. To scale the
2493 * tile, we scale up the physical width and then treat the logical tile
2494 * size as if it has bpb size elements.
2495 */
2496 const uint32_t tile_el_scale = bpb / tile_info.format_bpb;
2497 tile_info.phys_extent_B.width *= tile_el_scale;
2498
2499 /* Compute the offset into the tile */
2500 *x_offset_el = total_x_offset_el % tile_info.logical_extent_el.w;
2501 *y_offset_el = total_y_offset_el % tile_info.logical_extent_el.h;
2502
2503 /* Compute the offset of the tile in units of whole tiles */
2504 uint32_t x_offset_tl = total_x_offset_el / tile_info.logical_extent_el.w;
2505 uint32_t y_offset_tl = total_y_offset_el / tile_info.logical_extent_el.h;
2506
2507 *base_address_offset =
2508 y_offset_tl * tile_info.phys_extent_B.h * row_pitch_B +
2509 x_offset_tl * tile_info.phys_extent_B.h * tile_info.phys_extent_B.w;
2510 }
2511
2512 uint32_t
2513 isl_surf_get_depth_format(const struct isl_device *dev,
2514 const struct isl_surf *surf)
2515 {
2516 /* Support for separate stencil buffers began in gen5. Support for
2517 * interleaved depthstencil buffers ceased in gen7. The intermediate gens,
2518 * those that supported separate and interleaved stencil, were gen5 and
2519 * gen6.
2520 *
2521 * For a list of all available formats, see the Sandybridge PRM >> Volume
2522 * 2 Part 1: 3D/Media - 3D Pipeline >> 3DSTATE_DEPTH_BUFFER >> Surface
2523 * Format (p321).
2524 */
2525
2526 bool has_stencil = surf->usage & ISL_SURF_USAGE_STENCIL_BIT;
2527
2528 assert(surf->usage & ISL_SURF_USAGE_DEPTH_BIT);
2529
2530 if (has_stencil)
2531 assert(ISL_DEV_GEN(dev) < 7);
2532
2533 switch (surf->format) {
2534 default:
2535 unreachable("bad isl depth format");
2536 case ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS:
2537 assert(ISL_DEV_GEN(dev) < 7);
2538 return 0; /* D32_FLOAT_S8X24_UINT */
2539 case ISL_FORMAT_R32_FLOAT:
2540 assert(!has_stencil);
2541 return 1; /* D32_FLOAT */
2542 case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
2543 if (has_stencil) {
2544 assert(ISL_DEV_GEN(dev) < 7);
2545 return 2; /* D24_UNORM_S8_UINT */
2546 } else {
2547 assert(ISL_DEV_GEN(dev) >= 5);
2548 return 3; /* D24_UNORM_X8_UINT */
2549 }
2550 case ISL_FORMAT_R16_UNORM:
2551 assert(!has_stencil);
2552 return 5; /* D16_UNORM */
2553 }
2554 }
2555
2556 bool
2557 isl_swizzle_supports_rendering(const struct gen_device_info *devinfo,
2558 struct isl_swizzle swizzle)
2559 {
2560 if (devinfo->is_haswell) {
2561 /* From the Haswell PRM,
2562 * RENDER_SURFACE_STATE::Shader Channel Select Red
2563 *
2564 * "The Shader channel selects also define which shader channels are
2565 * written to which surface channel. If the Shader channel select is
2566 * SCS_ZERO or SCS_ONE then it is not written to the surface. If the
2567 * shader channel select is SCS_RED it is written to the surface red
2568 * channel and so on. If more than one shader channel select is set
2569 * to the same surface channel only the first shader channel in RGBA
2570 * order will be written."
2571 */
2572 return true;
2573 } else if (devinfo->gen <= 7) {
2574 /* Ivy Bridge and early doesn't have any swizzling */
2575 return isl_swizzle_is_identity(swizzle);
2576 } else {
2577 /* From the Sky Lake PRM Vol. 2d,
2578 * RENDER_SURFACE_STATE::Shader Channel Select Red
2579 *
2580 * "For Render Target, Red, Green and Blue Shader Channel Selects
2581 * MUST be such that only valid components can be swapped i.e. only
2582 * change the order of components in the pixel. Any other values for
2583 * these Shader Channel Select fields are not valid for Render
2584 * Targets. This also means that there MUST not be multiple shader
2585 * channels mapped to the same RT channel."
2586 *
2587 * From the Sky Lake PRM Vol. 2d,
2588 * RENDER_SURFACE_STATE::Shader Channel Select Alpha
2589 *
2590 * "For Render Target, this field MUST be programmed to
2591 * value = SCS_ALPHA."
2592 */
2593 return (swizzle.r == ISL_CHANNEL_SELECT_RED ||
2594 swizzle.r == ISL_CHANNEL_SELECT_GREEN ||
2595 swizzle.r == ISL_CHANNEL_SELECT_BLUE) &&
2596 (swizzle.g == ISL_CHANNEL_SELECT_RED ||
2597 swizzle.g == ISL_CHANNEL_SELECT_GREEN ||
2598 swizzle.g == ISL_CHANNEL_SELECT_BLUE) &&
2599 (swizzle.b == ISL_CHANNEL_SELECT_RED ||
2600 swizzle.b == ISL_CHANNEL_SELECT_GREEN ||
2601 swizzle.b == ISL_CHANNEL_SELECT_BLUE) &&
2602 swizzle.r != swizzle.g &&
2603 swizzle.r != swizzle.b &&
2604 swizzle.g != swizzle.b &&
2605 swizzle.a == ISL_CHANNEL_SELECT_ALPHA;
2606 }
2607 }
2608
2609 static enum isl_channel_select
2610 swizzle_select(enum isl_channel_select chan, struct isl_swizzle swizzle)
2611 {
2612 switch (chan) {
2613 case ISL_CHANNEL_SELECT_ZERO:
2614 case ISL_CHANNEL_SELECT_ONE:
2615 return chan;
2616 case ISL_CHANNEL_SELECT_RED:
2617 return swizzle.r;
2618 case ISL_CHANNEL_SELECT_GREEN:
2619 return swizzle.g;
2620 case ISL_CHANNEL_SELECT_BLUE:
2621 return swizzle.b;
2622 case ISL_CHANNEL_SELECT_ALPHA:
2623 return swizzle.a;
2624 default:
2625 unreachable("Invalid swizzle component");
2626 }
2627 }
2628
2629 /**
2630 * Returns the single swizzle that is equivalent to applying the two given
2631 * swizzles in sequence.
2632 */
2633 struct isl_swizzle
2634 isl_swizzle_compose(struct isl_swizzle first, struct isl_swizzle second)
2635 {
2636 return (struct isl_swizzle) {
2637 .r = swizzle_select(first.r, second),
2638 .g = swizzle_select(first.g, second),
2639 .b = swizzle_select(first.b, second),
2640 .a = swizzle_select(first.a, second),
2641 };
2642 }
2643
2644 /**
2645 * Returns a swizzle that is the pseudo-inverse of this swizzle.
2646 */
2647 struct isl_swizzle
2648 isl_swizzle_invert(struct isl_swizzle swizzle)
2649 {
2650 /* Default to zero for channels which do not show up in the swizzle */
2651 enum isl_channel_select chans[4] = {
2652 ISL_CHANNEL_SELECT_ZERO,
2653 ISL_CHANNEL_SELECT_ZERO,
2654 ISL_CHANNEL_SELECT_ZERO,
2655 ISL_CHANNEL_SELECT_ZERO,
2656 };
2657
2658 /* We go in ABGR order so that, if there are any duplicates, the first one
2659 * is taken if you look at it in RGBA order. This is what Haswell hardware
2660 * does for render target swizzles.
2661 */
2662 if ((unsigned)(swizzle.a - ISL_CHANNEL_SELECT_RED) < 4)
2663 chans[swizzle.a - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_ALPHA;
2664 if ((unsigned)(swizzle.b - ISL_CHANNEL_SELECT_RED) < 4)
2665 chans[swizzle.b - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_BLUE;
2666 if ((unsigned)(swizzle.g - ISL_CHANNEL_SELECT_RED) < 4)
2667 chans[swizzle.g - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_GREEN;
2668 if ((unsigned)(swizzle.r - ISL_CHANNEL_SELECT_RED) < 4)
2669 chans[swizzle.r - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_RED;
2670
2671 return (struct isl_swizzle) { chans[0], chans[1], chans[2], chans[3] };
2672 }