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