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