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