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