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