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_gfx4.h"
32 #include "isl_gfx6.h"
33 #include "isl_gfx7.h"
34 #include "isl_gfx8.h"
35 #include "isl_gfx9.h"
36 #include "isl_gfx12.h"
37 #include "isl_priv.h"
38 
39 void
isl_memcpy_linear_to_tiled(uint32_t xt1,uint32_t xt2,uint32_t yt1,uint32_t yt2,char * dst,const char * src,uint32_t dst_pitch,int32_t src_pitch,bool has_swizzling,enum isl_tiling tiling,isl_memcpy_type copy_type)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
isl_memcpy_tiled_to_linear(uint32_t xt1,uint32_t xt2,uint32_t yt1,uint32_t yt2,char * dst,const char * src,int32_t dst_pitch,uint32_t src_pitch,bool has_swizzling,enum isl_tiling tiling,isl_memcpy_type copy_type)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
__isl_finishme(const char * file,int line,const char * fmt,...)86 __isl_finishme(const char *file, int line, const char *fmt, ...)
87 {
88    va_list ap;
89    char buf[512];
90 
91    va_start(ap, fmt);
92    vsnprintf(buf, sizeof(buf), fmt, ap);
93    va_end(ap);
94 
95    fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buf);
96 }
97 
98 static void
isl_device_setup_mocs(struct isl_device * dev)99 isl_device_setup_mocs(struct isl_device *dev)
100 {
101    if (dev->info->ver >= 12) {
102       if (intel_device_info_is_dg2(dev->info)) {
103          /* L3CC=WB; BSpec: 45101 */
104          dev->mocs.internal = 3 << 1;
105          dev->mocs.external = 3 << 1;
106 
107          /* XY_BLOCK_COPY_BLT MOCS fields have programming notes which say:
108           *
109           *    "Destination MOCS value, which is used to program MOCS index
110           *     for writing to memory, should select a MOCS register having
111           *     "L3 Cacheability Control" programmed as uncacheable(UC) and
112           *     "Global GO" parameter set as GOMemory (pushes GO point to
113           *     memory). The MOCS Register may have L3 Lookup programmed as
114           *     UCL3LKDIS for better efficiency."
115           *
116           * The GO:Memory setting requires us to use MOCS 1 or 2.  MOCS 2
117           * has LKUP set to 0 and is marked "Non-Coherent", which we assume
118           * is probably the "better efficiency" they mention...
119           *
120           *   "Source MOCS value, which is used to program MOCS index for
121           *    reading from memory, should select a MOCS register having
122           *    "L3 Cacheability Control" programmed as uncacheable(UC).
123           *    The MOCS Register may have L3 Lookup programmed as UCL3LKDIS
124           *    for better efficiency."
125           *
126           * Any MOCS except 3 should work.  We use MOCS 2...
127           */
128          dev->mocs.blitter_dst = 2 << 1;
129          dev->mocs.blitter_src = 2 << 1;
130       } else if (dev->info->platform == INTEL_PLATFORM_DG1) {
131          /* L3CC=WB */
132          dev->mocs.internal = 5 << 1;
133          /* Displayables on DG1 are free to cache in L3 since L3 is transient
134           * and flushed at bottom of each submission.
135           */
136          dev->mocs.external = 5 << 1;
137       } else {
138          /* TC=1/LLC Only, LeCC=1/UC, LRUM=0, L3CC=3/WB */
139          dev->mocs.external = 61 << 1;
140          /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */
141          dev->mocs.internal = 2 << 1;
142 
143          /* L1 - HDC:L1 + L3 + LLC */
144          dev->mocs.l1_hdc_l3_llc = 48 << 1;
145       }
146    } else if (dev->info->ver >= 9) {
147       /* TC=LLC/eLLC, LeCC=PTE, LRUM=3, L3CC=WB */
148       dev->mocs.external = 1 << 1;
149       /* TC=LLC/eLLC, LeCC=WB, LRUM=3, L3CC=WB */
150       dev->mocs.internal = 2 << 1;
151    } else if (dev->info->ver >= 8) {
152       /* MEMORY_OBJECT_CONTROL_STATE:
153        * .MemoryTypeLLCeLLCCacheabilityControl = UCwithFenceifcoherentcycle,
154        * .TargetCache = L3DefertoPATforLLCeLLCselection,
155        * .AgeforQUADLRU = 0
156        */
157       dev->mocs.external = 0x18;
158       /* MEMORY_OBJECT_CONTROL_STATE:
159        * .MemoryTypeLLCeLLCCacheabilityControl = WB,
160        * .TargetCache = L3DefertoPATforLLCeLLCselection,
161        * .AgeforQUADLRU = 0
162        */
163       dev->mocs.internal = 0x78;
164    } else if (dev->info->ver >= 7) {
165       if (dev->info->platform == INTEL_PLATFORM_HSW) {
166          /* MEMORY_OBJECT_CONTROL_STATE:
167           * .LLCeLLCCacheabilityControlLLCCC             = 0,
168           * .L3CacheabilityControlL3CC                   = 1,
169           */
170          dev->mocs.internal = 1;
171          dev->mocs.external = 1;
172       } else {
173          /* MEMORY_OBJECT_CONTROL_STATE:
174           * .GraphicsDataTypeGFDT                        = 0,
175           * .LLCCacheabilityControlLLCCC                 = 0,
176           * .L3CacheabilityControlL3CC                   = 1,
177           */
178          dev->mocs.internal = 1;
179          dev->mocs.external = 1;
180       }
181    } else {
182       dev->mocs.internal = 0;
183       dev->mocs.external = 0;
184    }
185 }
186 
187 /**
188  * Return an appropriate MOCS entry for the given usage flags.
189  */
190 uint32_t
isl_mocs(const struct isl_device * dev,isl_surf_usage_flags_t usage,bool external)191 isl_mocs(const struct isl_device *dev, isl_surf_usage_flags_t usage,
192          bool external)
193 {
194    if (external)
195       return dev->mocs.external;
196 
197    if (dev->info->verx10 == 120 && dev->info->platform != INTEL_PLATFORM_DG1) {
198       if (usage & ISL_SURF_USAGE_STAGING_BIT)
199          return dev->mocs.internal;
200 
201       if (usage & ISL_SURF_USAGE_CPB_BIT)
202          return dev->mocs.internal;
203 
204       /* Using L1:HDC for storage buffers breaks Vulkan memory model
205        * tests that use shader atomics.  This isn't likely to work out,
206        * and we can't know a priori whether they'll be used.  So just
207        * continue with ordinary internal MOCS for now.
208        */
209       if (usage & ISL_SURF_USAGE_STORAGE_BIT)
210          return dev->mocs.internal;
211 
212       if (usage & (ISL_SURF_USAGE_CONSTANT_BUFFER_BIT |
213                    ISL_SURF_USAGE_RENDER_TARGET_BIT |
214                    ISL_SURF_USAGE_TEXTURE_BIT))
215          return dev->mocs.l1_hdc_l3_llc;
216    }
217 
218    return dev->mocs.internal;
219 }
220 
221 void
isl_device_init(struct isl_device * dev,const struct intel_device_info * info)222 isl_device_init(struct isl_device *dev,
223                 const struct intel_device_info *info)
224 {
225    /* Gfx8+ don't have bit6 swizzling, ensure callsite is not confused. */
226    assert(!(info->has_bit6_swizzle && info->ver >= 8));
227 
228    dev->info = info;
229    dev->use_separate_stencil = ISL_GFX_VER(dev) >= 6;
230    dev->has_bit6_swizzling = info->has_bit6_swizzle;
231 
232    /* The ISL_DEV macros may be defined in the CFLAGS, thus hardcoding some
233     * device properties at buildtime. Verify that the macros with the device
234     * properties chosen during runtime.
235     */
236    ISL_GFX_VER_SANITIZE(dev);
237    ISL_DEV_USE_SEPARATE_STENCIL_SANITIZE(dev);
238 
239    /* Did we break hiz or stencil? */
240    if (ISL_DEV_USE_SEPARATE_STENCIL(dev))
241       assert(info->has_hiz_and_separate_stencil);
242    if (info->must_use_separate_stencil)
243       assert(ISL_DEV_USE_SEPARATE_STENCIL(dev));
244 
245    dev->ss.size = RENDER_SURFACE_STATE_length(info) * 4;
246    dev->ss.align = isl_align(dev->ss.size, 32);
247 
248    dev->ss.clear_color_state_size =
249       isl_align(CLEAR_COLOR_length(info) * 4, 64);
250    dev->ss.clear_color_state_offset =
251       RENDER_SURFACE_STATE_ClearValueAddress_start(info) / 32 * 4;
252 
253    dev->ss.clear_value_size =
254       isl_align(RENDER_SURFACE_STATE_RedClearColor_bits(info) +
255                 RENDER_SURFACE_STATE_GreenClearColor_bits(info) +
256                 RENDER_SURFACE_STATE_BlueClearColor_bits(info) +
257                 RENDER_SURFACE_STATE_AlphaClearColor_bits(info), 32) / 8;
258 
259    dev->ss.clear_value_offset =
260       RENDER_SURFACE_STATE_RedClearColor_start(info) / 32 * 4;
261 
262    assert(RENDER_SURFACE_STATE_SurfaceBaseAddress_start(info) % 8 == 0);
263    dev->ss.addr_offset =
264       RENDER_SURFACE_STATE_SurfaceBaseAddress_start(info) / 8;
265 
266    /* The "Auxiliary Surface Base Address" field starts a bit higher up
267     * because the bottom 12 bits are used for other things.  Round down to
268     * the nearest dword before.
269     */
270    dev->ss.aux_addr_offset =
271       (RENDER_SURFACE_STATE_AuxiliarySurfaceBaseAddress_start(info) & ~31) / 8;
272 
273    dev->ds.size = _3DSTATE_DEPTH_BUFFER_length(info) * 4;
274    assert(_3DSTATE_DEPTH_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
275    dev->ds.depth_offset =
276       _3DSTATE_DEPTH_BUFFER_SurfaceBaseAddress_start(info) / 8;
277 
278    if (dev->use_separate_stencil) {
279       dev->ds.size += _3DSTATE_STENCIL_BUFFER_length(info) * 4 +
280                       _3DSTATE_HIER_DEPTH_BUFFER_length(info) * 4 +
281                       _3DSTATE_CLEAR_PARAMS_length(info) * 4;
282 
283       assert(_3DSTATE_STENCIL_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
284       dev->ds.stencil_offset =
285          _3DSTATE_DEPTH_BUFFER_length(info) * 4 +
286          _3DSTATE_STENCIL_BUFFER_SurfaceBaseAddress_start(info) / 8;
287 
288       assert(_3DSTATE_HIER_DEPTH_BUFFER_SurfaceBaseAddress_start(info) % 8 == 0);
289       dev->ds.hiz_offset =
290          _3DSTATE_DEPTH_BUFFER_length(info) * 4 +
291          _3DSTATE_STENCIL_BUFFER_length(info) * 4 +
292          _3DSTATE_HIER_DEPTH_BUFFER_SurfaceBaseAddress_start(info) / 8;
293    } else {
294       dev->ds.stencil_offset = 0;
295       dev->ds.hiz_offset = 0;
296    }
297 
298    if (ISL_GFX_VER(dev) >= 7) {
299       /* From the IVB PRM, SURFACE_STATE::Height,
300        *
301        *    For typed buffer and structured buffer surfaces, the number
302        *    of entries in the buffer ranges from 1 to 2^27. For raw buffer
303        *    surfaces, the number of entries in the buffer is the number of bytes
304        *    which can range from 1 to 2^30.
305        *
306        * This limit is only concerned with raw buffers.
307        */
308       dev->max_buffer_size = 1ull << 30;
309    } else {
310       dev->max_buffer_size = 1ull << 27;
311    }
312 
313    dev->cpb.size = _3DSTATE_CPSIZE_CONTROL_BUFFER_length(info) * 4;
314    dev->cpb.offset =
315       _3DSTATE_CPSIZE_CONTROL_BUFFER_SurfaceBaseAddress_start(info) / 8;
316 
317    isl_device_setup_mocs(dev);
318 }
319 
320 /**
321  * @brief Query the set of multisamples supported by the device.
322  *
323  * This function always returns non-zero, as ISL_SAMPLE_COUNT_1_BIT is always
324  * supported.
325  */
326 isl_sample_count_mask_t ATTRIBUTE_CONST
isl_device_get_sample_counts(struct isl_device * dev)327 isl_device_get_sample_counts(struct isl_device *dev)
328 {
329    if (ISL_GFX_VER(dev) >= 9) {
330       return ISL_SAMPLE_COUNT_1_BIT |
331              ISL_SAMPLE_COUNT_2_BIT |
332              ISL_SAMPLE_COUNT_4_BIT |
333              ISL_SAMPLE_COUNT_8_BIT |
334              ISL_SAMPLE_COUNT_16_BIT;
335    } else if (ISL_GFX_VER(dev) >= 8) {
336       return ISL_SAMPLE_COUNT_1_BIT |
337              ISL_SAMPLE_COUNT_2_BIT |
338              ISL_SAMPLE_COUNT_4_BIT |
339              ISL_SAMPLE_COUNT_8_BIT;
340    } else if (ISL_GFX_VER(dev) >= 7) {
341       return ISL_SAMPLE_COUNT_1_BIT |
342              ISL_SAMPLE_COUNT_4_BIT |
343              ISL_SAMPLE_COUNT_8_BIT;
344    } else if (ISL_GFX_VER(dev) >= 6) {
345       return ISL_SAMPLE_COUNT_1_BIT |
346              ISL_SAMPLE_COUNT_4_BIT;
347    } else {
348       return ISL_SAMPLE_COUNT_1_BIT;
349    }
350 }
351 
352 /**
353  * Returns an isl_tile_info representation of the given isl_tiling when
354  * combined when used in the given configuration.
355  *
356  * @param[in]  tiling      The tiling format to introspect
357  * @param[in]  dim         The dimensionality of the surface being tiled
358  * @param[in]  msaa_layout The layout of samples in the surface being tiled
359  * @param[in]  format_bpb  The number of bits per surface element (block) for
360  *                         the surface being tiled
361  * @param[in]  samples     The samples in the surface being tiled
362  * @param[out] tile_info   Return parameter for the tiling information
363  */
364 void
isl_tiling_get_info(enum isl_tiling tiling,enum isl_surf_dim dim,enum isl_msaa_layout msaa_layout,uint32_t format_bpb,uint32_t samples,struct isl_tile_info * tile_info)365 isl_tiling_get_info(enum isl_tiling tiling,
366                     enum isl_surf_dim dim,
367                     enum isl_msaa_layout msaa_layout,
368                     uint32_t format_bpb,
369                     uint32_t samples,
370                     struct isl_tile_info *tile_info)
371 {
372    const uint32_t bs = format_bpb / 8;
373    struct isl_extent4d logical_el;
374    struct isl_extent2d phys_B;
375 
376    if (tiling != ISL_TILING_LINEAR && !isl_is_pow2(format_bpb)) {
377       /* It is possible to have non-power-of-two formats in a tiled buffer.
378        * The easiest way to handle this is to treat the tile as if it is three
379        * times as wide.  This way no pixel will ever cross a tile boundary.
380        * This really only works on a subset of tiling formats.
381        */
382       assert(tiling == ISL_TILING_X || tiling == ISL_TILING_Y0 ||
383              tiling == ISL_TILING_4);
384       assert(bs % 3 == 0 && isl_is_pow2(format_bpb / 3));
385       isl_tiling_get_info(tiling, dim, msaa_layout, format_bpb / 3, samples,
386                           tile_info);
387       return;
388    }
389 
390    switch (tiling) {
391    case ISL_TILING_LINEAR:
392       assert(bs > 0);
393       logical_el = isl_extent4d(1, 1, 1, 1);
394       phys_B = isl_extent2d(bs, 1);
395       break;
396 
397    case ISL_TILING_X:
398       assert(bs > 0);
399       logical_el = isl_extent4d(512 / bs, 8, 1, 1);
400       phys_B = isl_extent2d(512, 8);
401       break;
402 
403    case ISL_TILING_Y0:
404    case ISL_TILING_4:
405       assert(bs > 0);
406       logical_el = isl_extent4d(128 / bs, 32, 1, 1);
407       phys_B = isl_extent2d(128, 32);
408       break;
409 
410    case ISL_TILING_W:
411       assert(bs == 1);
412       logical_el = isl_extent4d(64, 64, 1, 1);
413       /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfacePitch:
414        *
415        *    "If the surface is a stencil buffer (and thus has Tile Mode set
416        *    to TILEMODE_WMAJOR), the pitch must be set to 2x the value
417        *    computed based on width, as the stencil buffer is stored with two
418        *    rows interleaved."
419        *
420        * This, together with the fact that stencil buffers are referred to as
421        * being Y-tiled in the PRMs for older hardware implies that the
422        * physical size of a W-tile is actually the same as for a Y-tile.
423        */
424       phys_B = isl_extent2d(128, 32);
425       break;
426 
427    case ISL_TILING_Yf:
428    case ISL_TILING_Ys: {
429       bool is_Ys = tiling == ISL_TILING_Ys;
430 
431       assert(bs > 0);
432       unsigned width = 1 << (6 + (ffs(bs) / 2) + (2 * is_Ys));
433       unsigned height = 1 << (6 - (ffs(bs) / 2) + (2 * is_Ys));
434 
435       logical_el = isl_extent4d(width / bs, height, 1, 1);
436       phys_B = isl_extent2d(width, height);
437       break;
438    }
439    case ISL_TILING_64:
440       /* The tables below are taken from the "2D Surfaces" page in the Bspec
441        * which are formulated in terms of the Cv and Cu constants. This is
442        * different from the tables in the "Tile64 Format" page which should be
443        * equivalent but are usually in terms of pixels. Also note that Cv and
444        * Cu are HxW order to match the Bspec table, not WxH order like you
445        * might expect.
446        *
447        * From the Bspec's "Tile64 Format" page:
448        *
449        *    MSAA Depth/Stencil surface use IMS (Interleaved Multi Samples)
450        *    which means:
451        *
452        *    - Use the 1X MSAA (non-MSRT) version of the Tile64 equations and
453        *      let the client unit do the swizzling internally
454        *
455        * Surfaces using the IMS layout will use the mapping for 1x MSAA.
456        */
457 #define tile_extent(bs, cv, cu, a) \
458       isl_extent4d((1 << cu) / bs, 1 << cv, 1, a)
459 
460       /* Only 2D surfaces are handled. */
461       assert(dim == ISL_SURF_DIM_2D);
462 
463       if (samples == 1 || msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
464          switch (format_bpb) {
465          case 128: logical_el = tile_extent(bs, 6, 10, 1); break;
466          case  64: logical_el = tile_extent(bs, 6, 10, 1); break;
467          case  32: logical_el = tile_extent(bs, 7,  9, 1); break;
468          case  16: logical_el = tile_extent(bs, 7,  9, 1); break;
469          case   8: logical_el = tile_extent(bs, 8,  8, 1); break;
470          default: unreachable("Unsupported format size.");
471          }
472       } else if (samples == 2) {
473          switch (format_bpb) {
474          case 128: logical_el = tile_extent(bs, 6,  9, 2); break;
475          case  64: logical_el = tile_extent(bs, 6,  9, 2); break;
476          case  32: logical_el = tile_extent(bs, 7,  8, 2); break;
477          case  16: logical_el = tile_extent(bs, 7,  8, 2); break;
478          case   8: logical_el = tile_extent(bs, 8,  7, 2); break;
479          default: unreachable("Unsupported format size.");
480          }
481       } else {
482          switch (format_bpb) {
483          case 128: logical_el = tile_extent(bs, 5,  9, 4); break;
484          case  64: logical_el = tile_extent(bs, 5,  9, 4); break;
485          case  32: logical_el = tile_extent(bs, 6,  8, 4); break;
486          case  16: logical_el = tile_extent(bs, 6,  8, 4); break;
487          case   8: logical_el = tile_extent(bs, 7,  7, 4); break;
488          default: unreachable("Unsupported format size.");
489          }
490       }
491 
492 #undef tile_extent
493 
494       phys_B.w = logical_el.w * bs;
495       phys_B.h = 64 * 1024 / phys_B.w;
496       break;
497 
498    case ISL_TILING_HIZ:
499       /* HiZ buffers are required to have a 128bpb HiZ format. The tiling has
500        * the same physical dimensions as Y-tiling but actually has two HiZ
501        * columns per Y-tiled column.
502        */
503       assert(bs == 16);
504       logical_el = isl_extent4d(16, 16, 1, 1);
505       phys_B = isl_extent2d(128, 32);
506       break;
507 
508    case ISL_TILING_CCS:
509       /* CCS surfaces are required to have one of the GENX_CCS_* formats which
510        * have a block size of 1 or 2 bits per block and each CCS element
511        * corresponds to one cache-line pair in the main surface.  From the Sky
512        * Lake PRM Vol. 12 in the section on planes:
513        *
514        *    "The Color Control Surface (CCS) contains the compression status
515        *    of the cache-line pairs. The compression state of the cache-line
516        *    pair is specified by 2 bits in the CCS.  Each CCS cache-line
517        *    represents an area on the main surface of 16x16 sets of 128 byte
518        *    Y-tiled cache-line-pairs. CCS is always Y tiled."
519        *
520        * The CCS being Y-tiled implies that it's an 8x8 grid of cache-lines.
521        * Since each cache line corresponds to a 16x16 set of cache-line pairs,
522        * that yields total tile area of 128x128 cache-line pairs or CCS
523        * elements.  On older hardware, each CCS element is 1 bit and the tile
524        * is 128x256 elements.
525        */
526       assert(format_bpb == 1 || format_bpb == 2);
527       logical_el = isl_extent4d(128, 256 / format_bpb, 1, 1);
528       phys_B = isl_extent2d(128, 32);
529       break;
530 
531    case ISL_TILING_GFX12_CCS:
532       /* From the Bspec, Gen Graphics > Gfx12 > Memory Data Formats > Memory
533        * Compression > Memory Compression - Gfx12:
534        *
535        *    4 bits of auxiliary plane data are required for 2 cachelines of
536        *    main surface data. This results in a single cacheline of auxiliary
537        *    plane data mapping to 4 4K pages of main surface data for the 4K
538        *    pages (tile Y ) and 1 64K Tile Ys page.
539        *
540        * The Y-tiled pairing bit of 9 shown in the table below that Bspec
541        * section expresses that the 2 cachelines of main surface data are
542        * horizontally adjacent.
543        *
544        * TODO: Handle Ys, Yf and their pairing bits.
545        *
546        * Therefore, each CCS cacheline represents a 512Bx32 row area and each
547        * element represents a 32Bx4 row area.
548        */
549       assert(format_bpb == 4);
550       logical_el = isl_extent4d(16, 8, 1, 1);
551       phys_B = isl_extent2d(64, 1);
552       break;
553 
554    default:
555       unreachable("not reached");
556    } /* end switch */
557 
558    *tile_info = (struct isl_tile_info) {
559       .tiling = tiling,
560       .format_bpb = format_bpb,
561       .logical_extent_el = logical_el,
562       .phys_extent_B = phys_B,
563    };
564 }
565 
566 bool
isl_color_value_is_zero(union isl_color_value value,enum isl_format format)567 isl_color_value_is_zero(union isl_color_value value,
568                         enum isl_format format)
569 {
570    const struct isl_format_layout *fmtl = isl_format_get_layout(format);
571 
572 #define RETURN_FALSE_IF_NOT_0(c, i) \
573    if (fmtl->channels.c.bits && value.u32[i] != 0) \
574       return false
575 
576    RETURN_FALSE_IF_NOT_0(r, 0);
577    RETURN_FALSE_IF_NOT_0(g, 1);
578    RETURN_FALSE_IF_NOT_0(b, 2);
579    RETURN_FALSE_IF_NOT_0(a, 3);
580 
581 #undef RETURN_FALSE_IF_NOT_0
582 
583    return true;
584 }
585 
586 bool
isl_color_value_is_zero_one(union isl_color_value value,enum isl_format format)587 isl_color_value_is_zero_one(union isl_color_value value,
588                             enum isl_format format)
589 {
590    const struct isl_format_layout *fmtl = isl_format_get_layout(format);
591 
592 #define RETURN_FALSE_IF_NOT_0_1(c, i, field) \
593    if (fmtl->channels.c.bits && value.field[i] != 0 && value.field[i] != 1) \
594       return false
595 
596    if (isl_format_has_int_channel(format)) {
597       RETURN_FALSE_IF_NOT_0_1(r, 0, u32);
598       RETURN_FALSE_IF_NOT_0_1(g, 1, u32);
599       RETURN_FALSE_IF_NOT_0_1(b, 2, u32);
600       RETURN_FALSE_IF_NOT_0_1(a, 3, u32);
601    } else {
602       RETURN_FALSE_IF_NOT_0_1(r, 0, f32);
603       RETURN_FALSE_IF_NOT_0_1(g, 1, f32);
604       RETURN_FALSE_IF_NOT_0_1(b, 2, f32);
605       RETURN_FALSE_IF_NOT_0_1(a, 3, f32);
606    }
607 
608 #undef RETURN_FALSE_IF_NOT_0_1
609 
610    return true;
611 }
612 
613 /**
614  * @param[out] tiling is set only on success
615  */
616 static bool
isl_surf_choose_tiling(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,enum isl_tiling * tiling)617 isl_surf_choose_tiling(const struct isl_device *dev,
618                        const struct isl_surf_init_info *restrict info,
619                        enum isl_tiling *tiling)
620 {
621    isl_tiling_flags_t tiling_flags = info->tiling_flags;
622 
623    /* HiZ surfaces always use the HiZ tiling */
624    if (info->usage & ISL_SURF_USAGE_HIZ_BIT) {
625       assert(isl_format_is_hiz(info->format));
626       assert(tiling_flags == ISL_TILING_HIZ_BIT);
627       *tiling = isl_tiling_flag_to_enum(tiling_flags);
628       return true;
629    }
630 
631    /* CCS surfaces always use the CCS tiling */
632    if (info->usage & ISL_SURF_USAGE_CCS_BIT) {
633       assert(isl_format_get_layout(info->format)->txc == ISL_TXC_CCS);
634       UNUSED bool ivb_ccs = ISL_GFX_VER(dev) < 12 &&
635                             tiling_flags == ISL_TILING_CCS_BIT;
636       UNUSED bool tgl_ccs = ISL_GFX_VER(dev) >= 12 &&
637                             tiling_flags == ISL_TILING_GFX12_CCS_BIT;
638       assert(ivb_ccs != tgl_ccs);
639       *tiling = isl_tiling_flag_to_enum(tiling_flags);
640       return true;
641    }
642 
643    if (ISL_GFX_VERX10(dev) >= 125) {
644       isl_gfx125_filter_tiling(dev, info, &tiling_flags);
645    } else if (ISL_GFX_VER(dev) >= 6) {
646       isl_gfx6_filter_tiling(dev, info, &tiling_flags);
647    } else {
648       isl_gfx4_filter_tiling(dev, info, &tiling_flags);
649    }
650 
651    #define CHOOSE(__tiling) \
652       do { \
653          if (tiling_flags & (1u << (__tiling))) { \
654             *tiling = (__tiling); \
655             return true; \
656           } \
657       } while (0)
658 
659    /* Of the tiling modes remaining, choose the one that offers the best
660     * performance.
661     */
662 
663    if (info->dim == ISL_SURF_DIM_1D) {
664       /* Prefer linear for 1D surfaces because they do not benefit from
665        * tiling. To the contrary, tiling leads to wasted memory and poor
666        * memory locality due to the swizzling and alignment restrictions
667        * required in tiled surfaces.
668        */
669       CHOOSE(ISL_TILING_LINEAR);
670    }
671 
672    CHOOSE(ISL_TILING_4);
673    CHOOSE(ISL_TILING_64);
674    CHOOSE(ISL_TILING_Ys);
675    CHOOSE(ISL_TILING_Yf);
676    CHOOSE(ISL_TILING_Y0);
677    CHOOSE(ISL_TILING_X);
678    CHOOSE(ISL_TILING_W);
679    CHOOSE(ISL_TILING_LINEAR);
680 
681    #undef CHOOSE
682 
683    /* No tiling mode accomodates the inputs. */
684    return false;
685 }
686 
687 static bool
isl_choose_msaa_layout(const struct isl_device * dev,const struct isl_surf_init_info * info,enum isl_tiling tiling,enum isl_msaa_layout * msaa_layout)688 isl_choose_msaa_layout(const struct isl_device *dev,
689                  const struct isl_surf_init_info *info,
690                  enum isl_tiling tiling,
691                  enum isl_msaa_layout *msaa_layout)
692 {
693    if (ISL_GFX_VER(dev) >= 8) {
694       return isl_gfx8_choose_msaa_layout(dev, info, tiling, msaa_layout);
695    } else if (ISL_GFX_VER(dev) >= 7) {
696       return isl_gfx7_choose_msaa_layout(dev, info, tiling, msaa_layout);
697    } else if (ISL_GFX_VER(dev) >= 6) {
698       return isl_gfx6_choose_msaa_layout(dev, info, tiling, msaa_layout);
699    } else {
700       return isl_gfx4_choose_msaa_layout(dev, info, tiling, msaa_layout);
701    }
702 }
703 
704 struct isl_extent2d
isl_get_interleaved_msaa_px_size_sa(uint32_t samples)705 isl_get_interleaved_msaa_px_size_sa(uint32_t samples)
706 {
707    assert(isl_is_pow2(samples));
708 
709    /* From the Broadwell PRM >> Volume 5: Memory Views >> Computing Mip Level
710     * Sizes (p133):
711     *
712     *    If the surface is multisampled and it is a depth or stencil surface
713     *    or Multisampled Surface StorageFormat in SURFACE_STATE is
714     *    MSFMT_DEPTH_STENCIL, W_L and H_L must be adjusted as follows before
715     *    proceeding: [...]
716     */
717    return (struct isl_extent2d) {
718       .width = 1 << ((ffs(samples) - 0) / 2),
719       .height = 1 << ((ffs(samples) - 1) / 2),
720    };
721 }
722 
723 static void
isl_msaa_interleaved_scale_px_to_sa(uint32_t samples,uint32_t * width,uint32_t * height)724 isl_msaa_interleaved_scale_px_to_sa(uint32_t samples,
725                                     uint32_t *width, uint32_t *height)
726 {
727    const struct isl_extent2d px_size_sa =
728       isl_get_interleaved_msaa_px_size_sa(samples);
729 
730    if (width)
731       *width = isl_align(*width, 2) * px_size_sa.width;
732    if (height)
733       *height = isl_align(*height, 2) * px_size_sa.height;
734 }
735 
736 static enum isl_array_pitch_span
isl_choose_array_pitch_span(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,enum isl_dim_layout dim_layout,const struct isl_extent4d * phys_level0_sa)737 isl_choose_array_pitch_span(const struct isl_device *dev,
738                             const struct isl_surf_init_info *restrict info,
739                             enum isl_dim_layout dim_layout,
740                             const struct isl_extent4d *phys_level0_sa)
741 {
742    switch (dim_layout) {
743    case ISL_DIM_LAYOUT_GFX9_1D:
744    case ISL_DIM_LAYOUT_GFX4_2D:
745       if (ISL_GFX_VER(dev) >= 8) {
746          /* QPitch becomes programmable in Broadwell. So choose the
747           * most compact QPitch possible in order to conserve memory.
748           *
749           * From the Broadwell PRM >> Volume 2d: Command Reference: Structures
750           * >> RENDER_SURFACE_STATE Surface QPitch (p325):
751           *
752           *    - Software must ensure that this field is set to a value
753           *      sufficiently large such that the array slices in the surface
754           *      do not overlap. Refer to the Memory Data Formats section for
755           *      information on how surfaces are stored in memory.
756           *
757           *    - This field specifies the distance in rows between array
758           *      slices.  It is used only in the following cases:
759           *
760           *          - Surface Array is enabled OR
761           *          - Number of Mulitsamples is not NUMSAMPLES_1 and
762           *            Multisampled Surface Storage Format set to MSFMT_MSS OR
763           *          - Surface Type is SURFTYPE_CUBE
764           */
765          return ISL_ARRAY_PITCH_SPAN_COMPACT;
766       } else if (ISL_GFX_VER(dev) >= 7) {
767          /* Note that Ivybridge introduces
768           * RENDER_SURFACE_STATE.SurfaceArraySpacing, which provides the
769           * driver more control over the QPitch.
770           */
771 
772          if (phys_level0_sa->array_len == 1) {
773             /* The hardware will never use the QPitch. So choose the most
774              * compact QPitch possible in order to conserve memory.
775              */
776             return ISL_ARRAY_PITCH_SPAN_COMPACT;
777          }
778 
779          if (isl_surf_usage_is_depth_or_stencil(info->usage) ||
780              (info->usage & ISL_SURF_USAGE_HIZ_BIT)) {
781             /* From the Ivybridge PRM >> Volume 1 Part 1: Graphics Core >>
782              * Section 6.18.4.7: Surface Arrays (p112):
783              *
784              *    If Surface Array Spacing is set to ARYSPC_FULL (note that
785              *    the depth buffer and stencil buffer have an implied value of
786              *    ARYSPC_FULL):
787              */
788             return ISL_ARRAY_PITCH_SPAN_FULL;
789          }
790 
791          if (info->levels == 1) {
792             /* We are able to set RENDER_SURFACE_STATE.SurfaceArraySpacing
793              * to ARYSPC_LOD0.
794              */
795             return ISL_ARRAY_PITCH_SPAN_COMPACT;
796          }
797 
798          return ISL_ARRAY_PITCH_SPAN_FULL;
799       } else if ((ISL_GFX_VER(dev) == 5 || ISL_GFX_VER(dev) == 6) &&
800                  ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
801                  isl_surf_usage_is_stencil(info->usage)) {
802          /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
803           * Graphics Core >> Section 7.18.3.7: Surface Arrays:
804           *
805           *    The separate stencil buffer does not support mip mapping, thus
806           *    the storage for LODs other than LOD 0 is not needed.
807           */
808          assert(info->levels == 1);
809          return ISL_ARRAY_PITCH_SPAN_COMPACT;
810       } else {
811          if ((ISL_GFX_VER(dev) == 5 || ISL_GFX_VER(dev) == 6) &&
812              ISL_DEV_USE_SEPARATE_STENCIL(dev) &&
813              isl_surf_usage_is_stencil(info->usage)) {
814             /* [ILK-SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
815              * Graphics Core >> Section 7.18.3.7: Surface Arrays:
816              *
817              *    The separate stencil buffer does not support mip mapping,
818              *    thus the storage for LODs other than LOD 0 is not needed.
819              */
820             assert(info->levels == 1);
821             assert(phys_level0_sa->array_len == 1);
822             return ISL_ARRAY_PITCH_SPAN_COMPACT;
823          }
824 
825          if (phys_level0_sa->array_len == 1) {
826             /* The hardware will never use the QPitch. So choose the most
827              * compact QPitch possible in order to conserve memory.
828              */
829             return ISL_ARRAY_PITCH_SPAN_COMPACT;
830          }
831 
832          return ISL_ARRAY_PITCH_SPAN_FULL;
833       }
834 
835    case ISL_DIM_LAYOUT_GFX4_3D:
836       /* The hardware will never use the QPitch. So choose the most
837        * compact QPitch possible in order to conserve memory.
838        */
839       return ISL_ARRAY_PITCH_SPAN_COMPACT;
840 
841    case ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ:
842       /* Each array image in the gfx6 stencil of HiZ surface is compact in the
843        * sense that every LOD is a compact array of the same size as LOD0.
844        */
845       return ISL_ARRAY_PITCH_SPAN_COMPACT;
846    }
847 
848    unreachable("bad isl_dim_layout");
849    return ISL_ARRAY_PITCH_SPAN_FULL;
850 }
851 
852 static void
isl_choose_image_alignment_el(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,enum isl_tiling tiling,enum isl_dim_layout dim_layout,enum isl_msaa_layout msaa_layout,struct isl_extent3d * image_align_el)853 isl_choose_image_alignment_el(const struct isl_device *dev,
854                               const struct isl_surf_init_info *restrict info,
855                               enum isl_tiling tiling,
856                               enum isl_dim_layout dim_layout,
857                               enum isl_msaa_layout msaa_layout,
858                               struct isl_extent3d *image_align_el)
859 {
860    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
861    if (fmtl->txc == ISL_TXC_MCS) {
862       /*
863        * IvyBrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
864        *
865        * Height, width, and layout of MCS buffer in this case must match with
866        * Render Target height, width, and layout. MCS buffer is tiledY.
867        *
868        * To avoid wasting memory, choose the smallest alignment possible:
869        * HALIGN_4 and VALIGN_4.
870        */
871       *image_align_el = isl_extent3d(4, 4, 1);
872       return;
873    } else if (fmtl->txc == ISL_TXC_HIZ) {
874       assert(ISL_GFX_VER(dev) >= 6);
875       if (ISL_GFX_VER(dev) == 6) {
876          /* HiZ surfaces on Sandy Bridge are packed tightly. */
877          *image_align_el = isl_extent3d(1, 1, 1);
878       } else if (ISL_GFX_VER(dev) < 12) {
879          /* On gfx7+, HiZ surfaces are always aligned to 16x8 pixels in the
880           * primary surface which works out to 2x2 HiZ elments.
881           */
882          *image_align_el = isl_extent3d(2, 2, 1);
883       } else {
884          /* We choose the alignments based on the docs and what we've seen on
885           * prior platforms. From the TGL PRM Vol. 9, "Hierarchical Depth
886           * Buffer":
887           *
888           *    The height and width of the hierarchical depth buffer that must
889           *    be allocated are computed by the following formulas, where HZ
890           *    is the hierarchical depth buffer and Z is the depth buffer. The
891           *    Z_Height, Z_Width, and Z_Depth values given in these formulas
892           *    are those present in 3DSTATE_DEPTH_BUFFER incremented by one.
893           *
894           * The note about 3DSTATE_DEPTH_BUFFER tells us that the dimensions
895           * in the following formula refers to the base level. The key formula
896           * for the horizontal alignment is:
897           *
898           *    HZ_Width (bytes) [=]
899           *    ceiling(Z_Width / 16) * 16
900           *
901           * This type of formula is used when sizing compression blocks. So,
902           * the docs seem to say that the HiZ format has a block width of 16,
903           * and thus, the surface has a minimum horizontal alignment of 16
904           * pixels. This formula hasn't changed from prior platforms (where
905           * we've chosen a horizontal alignment of 16), so we should be on the
906           * right track. As for the vertical alignment, we're told:
907           *
908           *    To compute the minimum QPitch for the HZ surface, the height of
909           *    each LOD in pixels is determined using the equations for hL in
910           *    the GPU Overview volume, using a vertical alignment j=16.
911           *
912           * We're not calculating the QPitch right now, but the vertical
913           * alignment is plainly given as 16 rows in the depth buffer.
914           *
915           * As a result, we believe that HiZ surfaces are aligned to 16x16
916           * pixels in the primary surface. We divide this area by the HiZ
917           * block dimensions to get the alignment in terms of HiZ blocks.
918           */
919          *image_align_el = isl_extent3d(16 / fmtl->bw, 16 / fmtl->bh, 1);
920       }
921       return;
922    }
923 
924    if (ISL_GFX_VERX10(dev) >= 125) {
925       isl_gfx125_choose_image_alignment_el(dev, info, tiling, dim_layout,
926                                            msaa_layout, image_align_el);
927    } else if (ISL_GFX_VER(dev) >= 12) {
928       isl_gfx12_choose_image_alignment_el(dev, info, tiling, dim_layout,
929                                           msaa_layout, image_align_el);
930    } else if (ISL_GFX_VER(dev) >= 9) {
931       isl_gfx9_choose_image_alignment_el(dev, info, tiling, dim_layout,
932                                          msaa_layout, image_align_el);
933    } else if (ISL_GFX_VER(dev) >= 8) {
934       isl_gfx8_choose_image_alignment_el(dev, info, tiling, dim_layout,
935                                          msaa_layout, image_align_el);
936    } else if (ISL_GFX_VER(dev) >= 7) {
937       isl_gfx7_choose_image_alignment_el(dev, info, tiling, dim_layout,
938                                           msaa_layout, image_align_el);
939    } else if (ISL_GFX_VER(dev) >= 6) {
940       isl_gfx6_choose_image_alignment_el(dev, info, tiling, dim_layout,
941                                          msaa_layout, image_align_el);
942    } else {
943       isl_gfx4_choose_image_alignment_el(dev, info, tiling, dim_layout,
944                                          msaa_layout, image_align_el);
945    }
946 }
947 
948 static enum isl_dim_layout
isl_surf_choose_dim_layout(const struct isl_device * dev,enum isl_surf_dim logical_dim,enum isl_tiling tiling,isl_surf_usage_flags_t usage)949 isl_surf_choose_dim_layout(const struct isl_device *dev,
950                            enum isl_surf_dim logical_dim,
951                            enum isl_tiling tiling,
952                            isl_surf_usage_flags_t usage)
953 {
954    /* Sandy bridge needs a special layout for HiZ and stencil. */
955    if (ISL_GFX_VER(dev) == 6 &&
956        (tiling == ISL_TILING_W || tiling == ISL_TILING_HIZ))
957       return ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ;
958 
959    if (ISL_GFX_VER(dev) >= 9) {
960       switch (logical_dim) {
961       case ISL_SURF_DIM_1D:
962          /* From the Sky Lake PRM Vol. 5, "1D Surfaces":
963           *
964           *    One-dimensional surfaces use a tiling mode of linear.
965           *    Technically, they are not tiled resources, but the Tiled
966           *    Resource Mode field in RENDER_SURFACE_STATE is still used to
967           *    indicate the alignment requirements for this linear surface
968           *    (See 1D Alignment requirements for how 4K and 64KB Tiled
969           *    Resource Modes impact alignment). Alternatively, a 1D surface
970           *    can be defined as a 2D tiled surface (e.g. TileY or TileX) with
971           *    a height of 0.
972           *
973           * In other words, ISL_DIM_LAYOUT_GFX9_1D is only used for linear
974           * surfaces and, for tiled surfaces, ISL_DIM_LAYOUT_GFX4_2D is used.
975           */
976          if (tiling == ISL_TILING_LINEAR)
977             return ISL_DIM_LAYOUT_GFX9_1D;
978          else
979             return ISL_DIM_LAYOUT_GFX4_2D;
980       case ISL_SURF_DIM_2D:
981       case ISL_SURF_DIM_3D:
982          return ISL_DIM_LAYOUT_GFX4_2D;
983       }
984    } else {
985       switch (logical_dim) {
986       case ISL_SURF_DIM_1D:
987       case ISL_SURF_DIM_2D:
988          /* From the G45 PRM Vol. 1a, "6.17.4.1 Hardware Cube Map Layout":
989           *
990           * The cube face textures are stored in the same way as 3D surfaces
991           * are stored (see section 6.17.5 for details).  For cube surfaces,
992           * however, the depth is equal to the number of faces (always 6) and
993           * is not reduced for each MIP.
994           */
995          if (ISL_GFX_VER(dev) == 4 && (usage & ISL_SURF_USAGE_CUBE_BIT))
996             return ISL_DIM_LAYOUT_GFX4_3D;
997 
998          return ISL_DIM_LAYOUT_GFX4_2D;
999       case ISL_SURF_DIM_3D:
1000          return ISL_DIM_LAYOUT_GFX4_3D;
1001       }
1002    }
1003 
1004    unreachable("bad isl_surf_dim");
1005    return ISL_DIM_LAYOUT_GFX4_2D;
1006 }
1007 
1008 /**
1009  * Calculate the physical extent of the surface's first level, in units of
1010  * surface samples.
1011  */
1012 static void
isl_calc_phys_level0_extent_sa(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,enum isl_dim_layout dim_layout,enum isl_tiling tiling,enum isl_msaa_layout msaa_layout,struct isl_extent4d * phys_level0_sa)1013 isl_calc_phys_level0_extent_sa(const struct isl_device *dev,
1014                                const struct isl_surf_init_info *restrict info,
1015                                enum isl_dim_layout dim_layout,
1016                                enum isl_tiling tiling,
1017                                enum isl_msaa_layout msaa_layout,
1018                                struct isl_extent4d *phys_level0_sa)
1019 {
1020    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1021 
1022    if (isl_format_is_planar(info->format))
1023       unreachable("Planar formats unsupported");
1024 
1025    switch (info->dim) {
1026    case ISL_SURF_DIM_1D:
1027       assert(info->height == 1);
1028       assert(info->depth == 1);
1029       assert(info->samples == 1);
1030 
1031       switch (dim_layout) {
1032       case ISL_DIM_LAYOUT_GFX4_3D:
1033          unreachable("bad isl_dim_layout");
1034 
1035       case ISL_DIM_LAYOUT_GFX9_1D:
1036       case ISL_DIM_LAYOUT_GFX4_2D:
1037       case ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ:
1038          *phys_level0_sa = (struct isl_extent4d) {
1039             .w = info->width,
1040             .h = 1,
1041             .d = 1,
1042             .a = info->array_len,
1043          };
1044          break;
1045       }
1046       break;
1047 
1048    case ISL_SURF_DIM_2D:
1049       if (ISL_GFX_VER(dev) == 4 && (info->usage & ISL_SURF_USAGE_CUBE_BIT))
1050          assert(dim_layout == ISL_DIM_LAYOUT_GFX4_3D);
1051       else
1052          assert(dim_layout == ISL_DIM_LAYOUT_GFX4_2D ||
1053                 dim_layout == ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ);
1054 
1055       if (tiling == ISL_TILING_Ys && info->samples > 1)
1056          isl_finishme("%s:%s: multisample TileYs layout", __FILE__, __func__);
1057 
1058       switch (msaa_layout) {
1059       case ISL_MSAA_LAYOUT_NONE:
1060          assert(info->depth == 1);
1061          assert(info->samples == 1);
1062 
1063          *phys_level0_sa = (struct isl_extent4d) {
1064             .w = info->width,
1065             .h = info->height,
1066             .d = 1,
1067             .a = info->array_len,
1068          };
1069          break;
1070 
1071       case ISL_MSAA_LAYOUT_ARRAY:
1072          assert(info->depth == 1);
1073          assert(info->levels == 1);
1074          assert(isl_format_supports_multisampling(dev->info, info->format));
1075          assert(fmtl->bw == 1 && fmtl->bh == 1);
1076 
1077          *phys_level0_sa = (struct isl_extent4d) {
1078             .w = info->width,
1079             .h = info->height,
1080             .d = 1,
1081             .a = info->array_len * info->samples,
1082          };
1083          break;
1084 
1085       case ISL_MSAA_LAYOUT_INTERLEAVED:
1086          assert(info->depth == 1);
1087          assert(info->levels == 1);
1088          assert(isl_format_supports_multisampling(dev->info, info->format));
1089 
1090          *phys_level0_sa = (struct isl_extent4d) {
1091             .w = info->width,
1092             .h = info->height,
1093             .d = 1,
1094             .a = info->array_len,
1095          };
1096 
1097          isl_msaa_interleaved_scale_px_to_sa(info->samples,
1098                                              &phys_level0_sa->w,
1099                                              &phys_level0_sa->h);
1100          break;
1101       }
1102       break;
1103 
1104    case ISL_SURF_DIM_3D:
1105       assert(info->array_len == 1);
1106       assert(info->samples == 1);
1107 
1108       if (fmtl->bd > 1) {
1109          isl_finishme("%s:%s: compression block with depth > 1",
1110                       __FILE__, __func__);
1111       }
1112 
1113       switch (dim_layout) {
1114       case ISL_DIM_LAYOUT_GFX9_1D:
1115       case ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ:
1116          unreachable("bad isl_dim_layout");
1117 
1118       case ISL_DIM_LAYOUT_GFX4_2D:
1119          assert(ISL_GFX_VER(dev) >= 9);
1120 
1121          *phys_level0_sa = (struct isl_extent4d) {
1122             .w = info->width,
1123             .h = info->height,
1124             .d = 1,
1125             .a = info->depth,
1126          };
1127          break;
1128 
1129       case ISL_DIM_LAYOUT_GFX4_3D:
1130          assert(ISL_GFX_VER(dev) < 9);
1131          *phys_level0_sa = (struct isl_extent4d) {
1132             .w = info->width,
1133             .h = info->height,
1134             .d = info->depth,
1135             .a = 1,
1136          };
1137          break;
1138       }
1139       break;
1140    }
1141 }
1142 
1143 /**
1144  * Calculate the pitch between physical array slices, in units of rows of
1145  * surface elements.
1146  */
1147 static uint32_t
isl_calc_array_pitch_el_rows_gfx4_2d(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,const struct isl_tile_info * tile_info,const struct isl_extent3d * image_align_sa,const struct isl_extent4d * phys_level0_sa,enum isl_array_pitch_span array_pitch_span,const struct isl_extent2d * phys_slice0_sa)1148 isl_calc_array_pitch_el_rows_gfx4_2d(
1149       const struct isl_device *dev,
1150       const struct isl_surf_init_info *restrict info,
1151       const struct isl_tile_info *tile_info,
1152       const struct isl_extent3d *image_align_sa,
1153       const struct isl_extent4d *phys_level0_sa,
1154       enum isl_array_pitch_span array_pitch_span,
1155       const struct isl_extent2d *phys_slice0_sa)
1156 {
1157    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1158    uint32_t pitch_sa_rows = 0;
1159 
1160    switch (array_pitch_span) {
1161    case ISL_ARRAY_PITCH_SPAN_COMPACT:
1162       pitch_sa_rows = isl_align_npot(phys_slice0_sa->h, image_align_sa->h);
1163       break;
1164    case ISL_ARRAY_PITCH_SPAN_FULL: {
1165       /* The QPitch equation is found in the Broadwell PRM >> Volume 5:
1166        * Memory Views >> Common Surface Formats >> Surface Layout >> 2D
1167        * Surfaces >> Surface Arrays.
1168        */
1169       uint32_t H0_sa = phys_level0_sa->h;
1170       uint32_t H1_sa = isl_minify(H0_sa, 1);
1171 
1172       uint32_t h0_sa = isl_align_npot(H0_sa, image_align_sa->h);
1173       uint32_t h1_sa = isl_align_npot(H1_sa, image_align_sa->h);
1174 
1175       uint32_t m;
1176       if (ISL_GFX_VER(dev) >= 7) {
1177          /* The QPitch equation changed slightly in Ivybridge. */
1178          m = 12;
1179       } else {
1180          m = 11;
1181       }
1182 
1183       pitch_sa_rows = h0_sa + h1_sa + (m * image_align_sa->h);
1184 
1185       if (ISL_GFX_VER(dev) == 6 && info->samples > 1 &&
1186           (info->height % 4 == 1)) {
1187          /* [SNB] Errata from the Sandy Bridge PRM >> Volume 4 Part 1:
1188           * Graphics Core >> Section 7.18.3.7: Surface Arrays:
1189           *
1190           *    [SNB] Errata: Sampler MSAA Qpitch will be 4 greater than
1191           *    the value calculated in the equation above , for every
1192           *    other odd Surface Height starting from 1 i.e. 1,5,9,13.
1193           *
1194           * XXX(chadv): Is the errata natural corollary of the physical
1195           * layout of interleaved samples?
1196           */
1197          pitch_sa_rows += 4;
1198       }
1199 
1200       pitch_sa_rows = isl_align_npot(pitch_sa_rows, fmtl->bh);
1201       } /* end case */
1202       break;
1203    }
1204 
1205    assert(pitch_sa_rows % fmtl->bh == 0);
1206    uint32_t pitch_el_rows = pitch_sa_rows / fmtl->bh;
1207 
1208    if (ISL_GFX_VER(dev) >= 9 && ISL_GFX_VER(dev) <= 11 &&
1209        fmtl->txc == ISL_TXC_CCS) {
1210       /*
1211        * From the Sky Lake PRM Vol 7, "MCS Buffer for Render Target(s)" (p. 632):
1212        *
1213        *    "Mip-mapped and arrayed surfaces are supported with MCS buffer
1214        *    layout with these alignments in the RT space: Horizontal
1215        *    Alignment = 128 and Vertical Alignment = 64."
1216        *
1217        * From the Sky Lake PRM Vol. 2d, "RENDER_SURFACE_STATE" (p. 435):
1218        *
1219        *    "For non-multisampled render target's CCS auxiliary surface,
1220        *    QPitch must be computed with Horizontal Alignment = 128 and
1221        *    Surface Vertical Alignment = 256. These alignments are only for
1222        *    CCS buffer and not for associated render target."
1223        *
1224        * The first restriction is already handled by isl_choose_image_alignment_el
1225        * but the second restriction, which is an extension of the first, only
1226        * applies to qpitch and must be applied here.
1227        *
1228        * The second restriction disappears on Gfx12.
1229        */
1230       assert(fmtl->bh == 4);
1231       pitch_el_rows = isl_align(pitch_el_rows, 256 / 4);
1232    }
1233 
1234    if (ISL_GFX_VER(dev) >= 9 &&
1235        info->dim == ISL_SURF_DIM_3D &&
1236        tile_info->tiling != ISL_TILING_LINEAR) {
1237       /* From the Skylake BSpec >> RENDER_SURFACE_STATE >> Surface QPitch:
1238        *
1239        *    Tile Mode != Linear: This field must be set to an integer multiple
1240        *    of the tile height
1241        */
1242       pitch_el_rows = isl_align(pitch_el_rows, tile_info->logical_extent_el.height);
1243    }
1244 
1245    return pitch_el_rows;
1246 }
1247 
1248 /**
1249  * A variant of isl_calc_phys_slice0_extent_sa() specific to
1250  * ISL_DIM_LAYOUT_GFX4_2D.
1251  */
1252 static void
isl_calc_phys_slice0_extent_sa_gfx4_2d(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,enum isl_msaa_layout msaa_layout,const struct isl_extent3d * image_align_sa,const struct isl_extent4d * phys_level0_sa,struct isl_extent2d * phys_slice0_sa)1253 isl_calc_phys_slice0_extent_sa_gfx4_2d(
1254       const struct isl_device *dev,
1255       const struct isl_surf_init_info *restrict info,
1256       enum isl_msaa_layout msaa_layout,
1257       const struct isl_extent3d *image_align_sa,
1258       const struct isl_extent4d *phys_level0_sa,
1259       struct isl_extent2d *phys_slice0_sa)
1260 {
1261    assert(phys_level0_sa->depth == 1);
1262 
1263    if (info->levels == 1) {
1264       /* Do not pad the surface to the image alignment.
1265        *
1266        * For tiled surfaces, using a reduced alignment here avoids wasting CPU
1267        * cycles on the below mipmap layout caluclations. Reducing the
1268        * alignment here is safe because we later align the row pitch and array
1269        * pitch to the tile boundary. It is safe even for
1270        * ISL_MSAA_LAYOUT_INTERLEAVED, because phys_level0_sa is already scaled
1271        * to accomodate the interleaved samples.
1272        *
1273        * For linear surfaces, reducing the alignment here permits us to later
1274        * choose an arbitrary, non-aligned row pitch. If the surface backs
1275        * a VkBuffer, then an arbitrary pitch may be needed to accomodate
1276        * VkBufferImageCopy::bufferRowLength.
1277        */
1278       *phys_slice0_sa = (struct isl_extent2d) {
1279          .w = phys_level0_sa->w,
1280          .h = phys_level0_sa->h,
1281       };
1282       return;
1283    }
1284 
1285    uint32_t slice_top_w = 0;
1286    uint32_t slice_bottom_w = 0;
1287    uint32_t slice_left_h = 0;
1288    uint32_t slice_right_h = 0;
1289 
1290    uint32_t W0 = phys_level0_sa->w;
1291    uint32_t H0 = phys_level0_sa->h;
1292 
1293    for (uint32_t l = 0; l < info->levels; ++l) {
1294       uint32_t W = isl_minify(W0, l);
1295       uint32_t H = isl_minify(H0, l);
1296 
1297       uint32_t w = isl_align_npot(W, image_align_sa->w);
1298       uint32_t h = isl_align_npot(H, image_align_sa->h);
1299 
1300       if (l == 0) {
1301          slice_top_w = w;
1302          slice_left_h = h;
1303          slice_right_h = h;
1304       } else if (l == 1) {
1305          slice_bottom_w = w;
1306          slice_left_h += h;
1307       } else if (l == 2) {
1308          slice_bottom_w += w;
1309          slice_right_h += h;
1310       } else {
1311          slice_right_h += h;
1312       }
1313    }
1314 
1315    *phys_slice0_sa = (struct isl_extent2d) {
1316       .w = MAX(slice_top_w, slice_bottom_w),
1317       .h = MAX(slice_left_h, slice_right_h),
1318    };
1319 }
1320 
1321 static void
isl_calc_phys_total_extent_el_gfx4_2d(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,const struct isl_tile_info * tile_info,enum isl_msaa_layout msaa_layout,const struct isl_extent3d * image_align_sa,const struct isl_extent4d * phys_level0_sa,enum isl_array_pitch_span array_pitch_span,uint32_t * array_pitch_el_rows,struct isl_extent4d * phys_total_el)1322 isl_calc_phys_total_extent_el_gfx4_2d(
1323       const struct isl_device *dev,
1324       const struct isl_surf_init_info *restrict info,
1325       const struct isl_tile_info *tile_info,
1326       enum isl_msaa_layout msaa_layout,
1327       const struct isl_extent3d *image_align_sa,
1328       const struct isl_extent4d *phys_level0_sa,
1329       enum isl_array_pitch_span array_pitch_span,
1330       uint32_t *array_pitch_el_rows,
1331       struct isl_extent4d *phys_total_el)
1332 {
1333    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1334 
1335    struct isl_extent2d phys_slice0_sa;
1336    isl_calc_phys_slice0_extent_sa_gfx4_2d(dev, info, msaa_layout,
1337                                           image_align_sa, phys_level0_sa,
1338                                           &phys_slice0_sa);
1339    *array_pitch_el_rows =
1340       isl_calc_array_pitch_el_rows_gfx4_2d(dev, info, tile_info,
1341                                            image_align_sa, phys_level0_sa,
1342                                            array_pitch_span,
1343                                            &phys_slice0_sa);
1344 
1345    if (tile_info->tiling == ISL_TILING_64) {
1346       *phys_total_el = (struct isl_extent4d) {
1347          .w = isl_align_div_npot(phys_slice0_sa.w, fmtl->bw),
1348          .h = isl_align_div_npot(phys_slice0_sa.h, fmtl->bh),
1349          .d = isl_align_div_npot(phys_level0_sa->d, fmtl->bd),
1350          .a = phys_level0_sa->array_len,
1351       };
1352    } else {
1353       *phys_total_el = (struct isl_extent4d) {
1354          .w = isl_align_div_npot(phys_slice0_sa.w, fmtl->bw),
1355          .h = *array_pitch_el_rows * (phys_level0_sa->array_len - 1) +
1356               isl_align_div_npot(phys_slice0_sa.h, fmtl->bh),
1357          .d = 1,
1358          .a = 1,
1359       };
1360    }
1361 }
1362 
1363 /**
1364  * A variant of isl_calc_phys_slice0_extent_sa() specific to
1365  * ISL_DIM_LAYOUT_GFX4_3D.
1366  */
1367 static void
isl_calc_phys_total_extent_el_gfx4_3d(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,const struct isl_extent3d * image_align_sa,const struct isl_extent4d * phys_level0_sa,uint32_t * array_pitch_el_rows,struct isl_extent4d * phys_total_el)1368 isl_calc_phys_total_extent_el_gfx4_3d(
1369       const struct isl_device *dev,
1370       const struct isl_surf_init_info *restrict info,
1371       const struct isl_extent3d *image_align_sa,
1372       const struct isl_extent4d *phys_level0_sa,
1373       uint32_t *array_pitch_el_rows,
1374       struct isl_extent4d *phys_total_el)
1375 {
1376    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1377 
1378    assert(info->samples == 1);
1379 
1380    if (info->dim != ISL_SURF_DIM_3D) {
1381       /* From the G45 PRM Vol. 1a, "6.17.4.1 Hardware Cube Map Layout":
1382        *
1383        * The cube face textures are stored in the same way as 3D surfaces
1384        * are stored (see section 6.17.5 for details).  For cube surfaces,
1385        * however, the depth is equal to the number of faces (always 6) and
1386        * is not reduced for each MIP.
1387        */
1388       assert(ISL_GFX_VER(dev) == 4);
1389       assert(info->usage & ISL_SURF_USAGE_CUBE_BIT);
1390       assert(phys_level0_sa->array_len == 6);
1391    } else {
1392       assert(phys_level0_sa->array_len == 1);
1393    }
1394 
1395    uint32_t total_w = 0;
1396    uint32_t total_h = 0;
1397 
1398    uint32_t W0 = phys_level0_sa->w;
1399    uint32_t H0 = phys_level0_sa->h;
1400    uint32_t D0 = phys_level0_sa->d;
1401    uint32_t A0 = phys_level0_sa->a;
1402 
1403    for (uint32_t l = 0; l < info->levels; ++l) {
1404       uint32_t level_w = isl_align_npot(isl_minify(W0, l), image_align_sa->w);
1405       uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa->h);
1406       uint32_t level_d = info->dim == ISL_SURF_DIM_3D ? isl_minify(D0, l) : A0;
1407 
1408       uint32_t max_layers_horiz = MIN(level_d, 1u << l);
1409       uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
1410 
1411       total_w = MAX(total_w, level_w * max_layers_horiz);
1412       total_h += level_h * max_layers_vert;
1413    }
1414 
1415    /* GFX4_3D layouts don't really have an array pitch since each LOD has a
1416     * different number of horizontal and vertical layers.  We have to set it
1417     * to something, so at least make it true for LOD0.
1418     */
1419    *array_pitch_el_rows =
1420       isl_align_npot(phys_level0_sa->h, image_align_sa->h) / fmtl->bw;
1421    *phys_total_el = (struct isl_extent4d) {
1422       .w = isl_assert_div(total_w, fmtl->bw),
1423       .h = isl_assert_div(total_h, fmtl->bh),
1424       .d = 1,
1425       .a = 1,
1426    };
1427 }
1428 
1429 /**
1430  * A variant of isl_calc_phys_slice0_extent_sa() specific to
1431  * ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ.
1432  */
1433 static void
isl_calc_phys_total_extent_el_gfx6_stencil_hiz(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,const struct isl_tile_info * tile_info,const struct isl_extent3d * image_align_sa,const struct isl_extent4d * phys_level0_sa,uint32_t * array_pitch_el_rows,struct isl_extent4d * phys_total_el)1434 isl_calc_phys_total_extent_el_gfx6_stencil_hiz(
1435       const struct isl_device *dev,
1436       const struct isl_surf_init_info *restrict info,
1437       const struct isl_tile_info *tile_info,
1438       const struct isl_extent3d *image_align_sa,
1439       const struct isl_extent4d *phys_level0_sa,
1440       uint32_t *array_pitch_el_rows,
1441       struct isl_extent4d *phys_total_el)
1442 {
1443    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1444 
1445    const struct isl_extent2d tile_extent_sa = {
1446       .w = tile_info->logical_extent_el.w * fmtl->bw,
1447       .h = tile_info->logical_extent_el.h * fmtl->bh,
1448    };
1449    /* Tile size is a multiple of image alignment */
1450    assert(tile_extent_sa.w % image_align_sa->w == 0);
1451    assert(tile_extent_sa.h % image_align_sa->h == 0);
1452 
1453    const uint32_t W0 = phys_level0_sa->w;
1454    const uint32_t H0 = phys_level0_sa->h;
1455 
1456    /* Each image has the same height as LOD0 because the hardware thinks
1457     * everything is LOD0
1458     */
1459    const uint32_t H = isl_align(H0, image_align_sa->h) * phys_level0_sa->a;
1460 
1461    uint32_t total_top_w = 0;
1462    uint32_t total_bottom_w = 0;
1463    uint32_t total_h = 0;
1464 
1465    for (uint32_t l = 0; l < info->levels; ++l) {
1466       const uint32_t W = isl_minify(W0, l);
1467 
1468       const uint32_t w = isl_align(W, tile_extent_sa.w);
1469       const uint32_t h = isl_align(H, tile_extent_sa.h);
1470 
1471       if (l == 0) {
1472          total_top_w = w;
1473          total_h = h;
1474       } else if (l == 1) {
1475          total_bottom_w = w;
1476          total_h += h;
1477       } else {
1478          total_bottom_w += w;
1479       }
1480    }
1481 
1482    *array_pitch_el_rows =
1483       isl_assert_div(isl_align(H0, image_align_sa->h), fmtl->bh);
1484    *phys_total_el = (struct isl_extent4d) {
1485       .w = isl_assert_div(MAX(total_top_w, total_bottom_w), fmtl->bw),
1486       .h = isl_assert_div(total_h, fmtl->bh),
1487       .d = 1,
1488       .a = 1,
1489    };
1490 }
1491 
1492 /**
1493  * A variant of isl_calc_phys_slice0_extent_sa() specific to
1494  * ISL_DIM_LAYOUT_GFX9_1D.
1495  */
1496 static void
isl_calc_phys_total_extent_el_gfx9_1d(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,const struct isl_extent3d * image_align_sa,const struct isl_extent4d * phys_level0_sa,uint32_t * array_pitch_el_rows,struct isl_extent4d * phys_total_el)1497 isl_calc_phys_total_extent_el_gfx9_1d(
1498       const struct isl_device *dev,
1499       const struct isl_surf_init_info *restrict info,
1500       const struct isl_extent3d *image_align_sa,
1501       const struct isl_extent4d *phys_level0_sa,
1502       uint32_t *array_pitch_el_rows,
1503       struct isl_extent4d *phys_total_el)
1504 {
1505    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1506 
1507    assert(phys_level0_sa->height == 1);
1508    assert(phys_level0_sa->depth == 1);
1509    assert(info->samples == 1);
1510    assert(image_align_sa->w >= fmtl->bw);
1511 
1512    uint32_t slice_w = 0;
1513    const uint32_t W0 = phys_level0_sa->w;
1514 
1515    for (uint32_t l = 0; l < info->levels; ++l) {
1516       uint32_t W = isl_minify(W0, l);
1517       uint32_t w = isl_align_npot(W, image_align_sa->w);
1518 
1519       slice_w += w;
1520    }
1521 
1522    *array_pitch_el_rows = 1;
1523    *phys_total_el = (struct isl_extent4d) {
1524       .w = isl_assert_div(slice_w, fmtl->bw),
1525       .h = phys_level0_sa->array_len,
1526       .d = 1,
1527       .a = 1,
1528    };
1529 }
1530 
1531 /**
1532  * Calculate the two-dimensional total physical extent of the surface, in
1533  * units of surface elements.
1534  */
1535 static void
isl_calc_phys_total_extent_el(const struct isl_device * dev,const struct isl_surf_init_info * restrict info,const struct isl_tile_info * tile_info,enum isl_dim_layout dim_layout,enum isl_msaa_layout msaa_layout,const struct isl_extent3d * image_align_sa,const struct isl_extent4d * phys_level0_sa,enum isl_array_pitch_span array_pitch_span,uint32_t * array_pitch_el_rows,struct isl_extent4d * phys_total_el)1536 isl_calc_phys_total_extent_el(const struct isl_device *dev,
1537                               const struct isl_surf_init_info *restrict info,
1538                               const struct isl_tile_info *tile_info,
1539                               enum isl_dim_layout dim_layout,
1540                               enum isl_msaa_layout msaa_layout,
1541                               const struct isl_extent3d *image_align_sa,
1542                               const struct isl_extent4d *phys_level0_sa,
1543                               enum isl_array_pitch_span array_pitch_span,
1544                               uint32_t *array_pitch_el_rows,
1545                               struct isl_extent4d *phys_total_el)
1546 {
1547    switch (dim_layout) {
1548    case ISL_DIM_LAYOUT_GFX9_1D:
1549       assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
1550       isl_calc_phys_total_extent_el_gfx9_1d(dev, info,
1551                                             image_align_sa, phys_level0_sa,
1552                                             array_pitch_el_rows,
1553                                             phys_total_el);
1554       return;
1555    case ISL_DIM_LAYOUT_GFX4_2D:
1556       isl_calc_phys_total_extent_el_gfx4_2d(dev, info, tile_info, msaa_layout,
1557                                             image_align_sa, phys_level0_sa,
1558                                             array_pitch_span,
1559                                             array_pitch_el_rows,
1560                                             phys_total_el);
1561       return;
1562    case ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ:
1563       assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
1564       isl_calc_phys_total_extent_el_gfx6_stencil_hiz(dev, info, tile_info,
1565                                                      image_align_sa,
1566                                                      phys_level0_sa,
1567                                                      array_pitch_el_rows,
1568                                                      phys_total_el);
1569       return;
1570    case ISL_DIM_LAYOUT_GFX4_3D:
1571       assert(array_pitch_span == ISL_ARRAY_PITCH_SPAN_COMPACT);
1572       isl_calc_phys_total_extent_el_gfx4_3d(dev, info,
1573                                             image_align_sa, phys_level0_sa,
1574                                             array_pitch_el_rows,
1575                                             phys_total_el);
1576       return;
1577    }
1578 
1579    unreachable("invalid value for dim_layout");
1580 }
1581 
1582 static uint32_t
isl_calc_row_pitch_alignment(const struct isl_device * dev,const struct isl_surf_init_info * surf_info,const struct isl_tile_info * tile_info)1583 isl_calc_row_pitch_alignment(const struct isl_device *dev,
1584                              const struct isl_surf_init_info *surf_info,
1585                              const struct isl_tile_info *tile_info)
1586 {
1587    if (tile_info->tiling != ISL_TILING_LINEAR) {
1588       /* According to BSpec: 44930, Gfx12's CCS-compressed surface pitches must
1589        * be 512B-aligned. CCS is only support on Y tilings.
1590        *
1591        * Only consider 512B alignment when :
1592        *    - AUX is not explicitly disabled
1593        *    - the caller has specified no pitch
1594        *
1595        * isl_surf_get_ccs_surf() will check that the main surface alignment
1596        * matches CCS expectations.
1597        */
1598       if (ISL_GFX_VER(dev) >= 12 &&
1599           isl_format_supports_ccs_e(dev->info, surf_info->format) &&
1600           tile_info->tiling != ISL_TILING_X &&
1601           !(surf_info->usage & ISL_SURF_USAGE_DISABLE_AUX_BIT) &&
1602           surf_info->row_pitch_B == 0) {
1603          return isl_align(tile_info->phys_extent_B.width, 512);
1604       }
1605 
1606       return tile_info->phys_extent_B.width;
1607    }
1608 
1609    /* We only support tiled fragment shading rate buffers. */
1610    assert((surf_info->usage & ISL_SURF_USAGE_CPB_BIT) == 0);
1611 
1612    /* From the Broadwel PRM >> Volume 2d: Command Reference: Structures >>
1613     * RENDER_SURFACE_STATE Surface Pitch (p349):
1614     *
1615     *    - For linear render target surfaces and surfaces accessed with the
1616     *      typed data port messages, the pitch must be a multiple of the
1617     *      element size for non-YUV surface formats.  Pitch must be
1618     *      a multiple of 2 * element size for YUV surface formats.
1619     *
1620     *    - [Requirements for SURFTYPE_BUFFER and SURFTYPE_STRBUF, which we
1621     *      ignore because isl doesn't do buffers.]
1622     *
1623     *    - For other linear surfaces, the pitch can be any multiple of
1624     *      bytes.
1625     */
1626    const struct isl_format_layout *fmtl = isl_format_get_layout(surf_info->format);
1627    const uint32_t bs = fmtl->bpb / 8;
1628    uint32_t alignment;
1629 
1630    if (surf_info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1631       if (isl_format_is_yuv(surf_info->format)) {
1632          alignment = 2 * bs;
1633       } else  {
1634          alignment = bs;
1635       }
1636    } else {
1637       alignment = 1;
1638    }
1639 
1640    /* From the Broadwell PRM >> Volume 2c: Command Reference: Registers >>
1641     * PRI_STRIDE Stride (p1254):
1642     *
1643     *    "When using linear memory, this must be at least 64 byte aligned."
1644     *
1645     * However, when displaying on NVIDIA and recent AMD GPUs via PRIME,
1646     * we need a larger pitch of 256 bytes.
1647     *
1648     * If the ISL caller didn't specify a row_pitch_B, then we should assume
1649     * the NVIDIA/AMD requirements. Otherwise, if we have a specified
1650     * row_pitch_B, this is probably because the caller is trying to import a
1651     * buffer. In that case we limit the minimum row pitch to the Intel HW
1652     * requirement.
1653     */
1654    if (surf_info->usage & ISL_SURF_USAGE_DISPLAY_BIT) {
1655       if (surf_info->row_pitch_B == 0)
1656          alignment = isl_align(alignment, 256);
1657       else
1658          alignment = isl_align(alignment, 64);
1659    }
1660 
1661    return alignment;
1662 }
1663 
1664 static uint32_t
isl_calc_linear_min_row_pitch(const struct isl_device * dev,const struct isl_surf_init_info * info,const struct isl_extent4d * phys_total_el,uint32_t alignment_B)1665 isl_calc_linear_min_row_pitch(const struct isl_device *dev,
1666                               const struct isl_surf_init_info *info,
1667                               const struct isl_extent4d *phys_total_el,
1668                               uint32_t alignment_B)
1669 {
1670    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1671    const uint32_t bs = fmtl->bpb / 8;
1672 
1673    return isl_align_npot(bs * phys_total_el->w, alignment_B);
1674 }
1675 
1676 static uint32_t
isl_calc_tiled_min_row_pitch(const struct isl_device * dev,const struct isl_surf_init_info * surf_info,const struct isl_tile_info * tile_info,const struct isl_extent4d * phys_total_el,uint32_t alignment_B)1677 isl_calc_tiled_min_row_pitch(const struct isl_device *dev,
1678                              const struct isl_surf_init_info *surf_info,
1679                              const struct isl_tile_info *tile_info,
1680                              const struct isl_extent4d *phys_total_el,
1681                              uint32_t alignment_B)
1682 {
1683    const struct isl_format_layout *fmtl = isl_format_get_layout(surf_info->format);
1684 
1685    assert(fmtl->bpb % tile_info->format_bpb == 0);
1686 
1687    const uint32_t tile_el_scale = fmtl->bpb / tile_info->format_bpb;
1688    const uint32_t total_w_tl =
1689       isl_align_div(phys_total_el->w * tile_el_scale,
1690                     tile_info->logical_extent_el.width);
1691 
1692    /* In some cases the alignment of the pitch might be > to the tile size
1693     * (for example Gfx12 CCS requires 512B alignment while the tile's width
1694     * can be 128B), so align the row pitch to the alignment.
1695     */
1696    assert(alignment_B >= tile_info->phys_extent_B.width);
1697    return isl_align(total_w_tl * tile_info->phys_extent_B.width, alignment_B);
1698 }
1699 
1700 static uint32_t
isl_calc_min_row_pitch(const struct isl_device * dev,const struct isl_surf_init_info * surf_info,const struct isl_tile_info * tile_info,const struct isl_extent4d * phys_total_el,uint32_t alignment_B)1701 isl_calc_min_row_pitch(const struct isl_device *dev,
1702                        const struct isl_surf_init_info *surf_info,
1703                        const struct isl_tile_info *tile_info,
1704                        const struct isl_extent4d *phys_total_el,
1705                        uint32_t alignment_B)
1706 {
1707    if (tile_info->tiling == ISL_TILING_LINEAR) {
1708       return isl_calc_linear_min_row_pitch(dev, surf_info, phys_total_el,
1709                                            alignment_B);
1710    } else {
1711       return isl_calc_tiled_min_row_pitch(dev, surf_info, tile_info,
1712                                           phys_total_el, alignment_B);
1713    }
1714 }
1715 
1716 /**
1717  * Is `pitch` in the valid range for a hardware bitfield, if the bitfield's
1718  * size is `bits` bits?
1719  *
1720  * Hardware pitch fields are offset by 1. For example, if the size of
1721  * RENDER_SURFACE_STATE::SurfacePitch is B bits, then the range of valid
1722  * pitches is [1, 2^b] inclusive.  If the surface pitch is N, then
1723  * RENDER_SURFACE_STATE::SurfacePitch must be set to N-1.
1724  */
1725 static bool
pitch_in_range(uint32_t n,uint32_t bits)1726 pitch_in_range(uint32_t n, uint32_t bits)
1727 {
1728    assert(n != 0);
1729    return likely(bits != 0 && 1 <= n && n <= (1 << bits));
1730 }
1731 
1732 static bool
isl_calc_row_pitch(const struct isl_device * dev,const struct isl_surf_init_info * surf_info,const struct isl_tile_info * tile_info,enum isl_dim_layout dim_layout,const struct isl_extent4d * phys_total_el,uint32_t * out_row_pitch_B)1733 isl_calc_row_pitch(const struct isl_device *dev,
1734                    const struct isl_surf_init_info *surf_info,
1735                    const struct isl_tile_info *tile_info,
1736                    enum isl_dim_layout dim_layout,
1737                    const struct isl_extent4d *phys_total_el,
1738                    uint32_t *out_row_pitch_B)
1739 {
1740    uint32_t alignment_B =
1741       isl_calc_row_pitch_alignment(dev, surf_info, tile_info);
1742 
1743    const uint32_t min_row_pitch_B =
1744       isl_calc_min_row_pitch(dev, surf_info, tile_info, phys_total_el,
1745                              alignment_B);
1746 
1747    if (surf_info->row_pitch_B != 0) {
1748       if (surf_info->row_pitch_B < min_row_pitch_B)
1749          return false;
1750 
1751       if (surf_info->row_pitch_B % alignment_B != 0)
1752          return false;
1753    }
1754 
1755    const uint32_t row_pitch_B =
1756       surf_info->row_pitch_B != 0 ? surf_info->row_pitch_B : min_row_pitch_B;
1757 
1758    const uint32_t row_pitch_tl = row_pitch_B / tile_info->phys_extent_B.width;
1759 
1760    if (row_pitch_B == 0)
1761       return false;
1762 
1763    if (dim_layout == ISL_DIM_LAYOUT_GFX9_1D) {
1764       /* SurfacePitch is ignored for this layout. */
1765       goto done;
1766    }
1767 
1768    if ((surf_info->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
1769                             ISL_SURF_USAGE_TEXTURE_BIT |
1770                             ISL_SURF_USAGE_STORAGE_BIT)) &&
1771        !pitch_in_range(row_pitch_B, RENDER_SURFACE_STATE_SurfacePitch_bits(dev->info)))
1772       return false;
1773 
1774    if ((surf_info->usage & (ISL_SURF_USAGE_CCS_BIT |
1775                             ISL_SURF_USAGE_MCS_BIT)) &&
1776        !pitch_in_range(row_pitch_tl, RENDER_SURFACE_STATE_AuxiliarySurfacePitch_bits(dev->info)))
1777       return false;
1778 
1779    if ((surf_info->usage & ISL_SURF_USAGE_DEPTH_BIT) &&
1780        !pitch_in_range(row_pitch_B, _3DSTATE_DEPTH_BUFFER_SurfacePitch_bits(dev->info)))
1781       return false;
1782 
1783    if ((surf_info->usage & ISL_SURF_USAGE_HIZ_BIT) &&
1784        !pitch_in_range(row_pitch_B, _3DSTATE_HIER_DEPTH_BUFFER_SurfacePitch_bits(dev->info)))
1785       return false;
1786 
1787    const uint32_t stencil_pitch_bits = dev->use_separate_stencil ?
1788       _3DSTATE_STENCIL_BUFFER_SurfacePitch_bits(dev->info) :
1789       _3DSTATE_DEPTH_BUFFER_SurfacePitch_bits(dev->info);
1790 
1791    if ((surf_info->usage & ISL_SURF_USAGE_STENCIL_BIT) &&
1792        !pitch_in_range(row_pitch_B, stencil_pitch_bits))
1793       return false;
1794 
1795    if ((surf_info->usage & ISL_SURF_USAGE_CPB_BIT) &&
1796        !pitch_in_range(row_pitch_B, _3DSTATE_CPSIZE_CONTROL_BUFFER_SurfacePitch_bits(dev->info)))
1797       return false;
1798 
1799  done:
1800    *out_row_pitch_B = row_pitch_B;
1801    return true;
1802 }
1803 
1804 bool
isl_surf_init_s(const struct isl_device * dev,struct isl_surf * surf,const struct isl_surf_init_info * restrict info)1805 isl_surf_init_s(const struct isl_device *dev,
1806                 struct isl_surf *surf,
1807                 const struct isl_surf_init_info *restrict info)
1808 {
1809    /* Some sanity checks */
1810    assert(!(info->usage & ISL_SURF_USAGE_CPB_BIT) ||
1811           dev->info->has_coarse_pixel_primitive_and_cb);
1812 
1813    const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1814 
1815    const struct isl_extent4d logical_level0_px = {
1816       .w = info->width,
1817       .h = info->height,
1818       .d = info->depth,
1819       .a = info->array_len,
1820    };
1821 
1822    enum isl_tiling tiling;
1823    if (!isl_surf_choose_tiling(dev, info, &tiling))
1824       return false;
1825 
1826    const enum isl_dim_layout dim_layout =
1827       isl_surf_choose_dim_layout(dev, info->dim, tiling, info->usage);
1828 
1829    enum isl_msaa_layout msaa_layout;
1830    if (!isl_choose_msaa_layout(dev, info, tiling, &msaa_layout))
1831        return false;
1832 
1833    struct isl_tile_info tile_info;
1834    isl_tiling_get_info(tiling, info->dim, msaa_layout, fmtl->bpb,
1835                        info->samples, &tile_info);
1836 
1837    struct isl_extent3d image_align_el;
1838    isl_choose_image_alignment_el(dev, info, tiling, dim_layout, msaa_layout,
1839                                  &image_align_el);
1840 
1841    struct isl_extent3d image_align_sa =
1842       isl_extent3d_el_to_sa(info->format, image_align_el);
1843 
1844    struct isl_extent4d phys_level0_sa;
1845    isl_calc_phys_level0_extent_sa(dev, info, dim_layout, tiling, msaa_layout,
1846                                   &phys_level0_sa);
1847 
1848    enum isl_array_pitch_span array_pitch_span =
1849       isl_choose_array_pitch_span(dev, info, dim_layout, &phys_level0_sa);
1850 
1851    uint32_t array_pitch_el_rows;
1852    struct isl_extent4d phys_total_el;
1853    isl_calc_phys_total_extent_el(dev, info, &tile_info,
1854                                  dim_layout, msaa_layout,
1855                                  &image_align_sa, &phys_level0_sa,
1856                                  array_pitch_span, &array_pitch_el_rows,
1857                                  &phys_total_el);
1858 
1859    uint32_t row_pitch_B;
1860    if (!isl_calc_row_pitch(dev, info, &tile_info, dim_layout,
1861                            &phys_total_el, &row_pitch_B))
1862       return false;
1863 
1864    uint32_t base_alignment_B;
1865    uint64_t size_B;
1866    if (tiling == ISL_TILING_LINEAR) {
1867       /* LINEAR tiling has no concept of intra-tile arrays */
1868       assert(phys_total_el.d == 1 && phys_total_el.a == 1);
1869 
1870       size_B = (uint64_t) row_pitch_B * phys_total_el.h;
1871 
1872       /* From the Broadwell PRM Vol 2d, RENDER_SURFACE_STATE::SurfaceBaseAddress:
1873        *
1874        *    "The Base Address for linear render target surfaces and surfaces
1875        *    accessed with the typed surface read/write data port messages must
1876        *    be element-size aligned, for non-YUV surface formats, or a
1877        *    multiple of 2 element-sizes for YUV surface formats. Other linear
1878        *    surfaces have no alignment requirements (byte alignment is
1879        *    sufficient.)"
1880        */
1881       base_alignment_B = MAX(1, info->min_alignment_B);
1882       if (info->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1883          if (isl_format_is_yuv(info->format)) {
1884             base_alignment_B = MAX(base_alignment_B, fmtl->bpb / 4);
1885          } else {
1886             base_alignment_B = MAX(base_alignment_B, fmtl->bpb / 8);
1887          }
1888       }
1889       base_alignment_B = isl_round_up_to_power_of_two(base_alignment_B);
1890 
1891       /* From the Skylake PRM Vol 2c, PLANE_STRIDE::Stride:
1892        *
1893        *     "For Linear memory, this field specifies the stride in chunks of
1894        *     64 bytes (1 cache line)."
1895        */
1896       if (isl_surf_usage_is_display(info->usage))
1897          base_alignment_B = MAX(base_alignment_B, 64);
1898    } else {
1899       /* Pitches must make sense with the tiling */
1900       assert(row_pitch_B % tile_info.phys_extent_B.width == 0);
1901 
1902       uint32_t array_slices, array_pitch_tl_rows;
1903       if (phys_total_el.d > 1) {
1904          assert(phys_total_el.a == 1);
1905          array_pitch_tl_rows = isl_assert_div(array_pitch_el_rows,
1906                                               tile_info.logical_extent_el.h);
1907          array_slices = isl_align_div(phys_total_el.d,
1908                                       tile_info.logical_extent_el.d);
1909       } else if (phys_total_el.a > 1) {
1910          assert(phys_total_el.d == 1);
1911          array_pitch_tl_rows = isl_assert_div(array_pitch_el_rows,
1912                                               tile_info.logical_extent_el.h);
1913          array_slices = isl_align_div(phys_total_el.a,
1914                                       tile_info.logical_extent_el.a);
1915       } else {
1916          assert(phys_total_el.d == 1 && phys_total_el.a == 1);
1917          array_pitch_tl_rows = 0;
1918          array_slices = 1;
1919       }
1920 
1921       const uint32_t total_h_tl =
1922          (array_slices - 1) * array_pitch_tl_rows +
1923          isl_align_div(phys_total_el.h, tile_info.logical_extent_el.height);
1924 
1925       size_B = (uint64_t) total_h_tl * tile_info.phys_extent_B.height * row_pitch_B;
1926 
1927       const uint32_t tile_size_B = tile_info.phys_extent_B.width *
1928                                    tile_info.phys_extent_B.height;
1929       assert(isl_is_pow2(info->min_alignment_B) && isl_is_pow2(tile_size_B));
1930       base_alignment_B = MAX(info->min_alignment_B, tile_size_B);
1931 
1932       /* The diagram in the Bspec section Memory Compression - Gfx12, shows
1933        * that the CCS is indexed in 256B chunks. However, the
1934        * PLANE_AUX_DIST::Auxiliary Surface Distance field is in units of 4K
1935        * pages. We currently don't assign the usage field like we do for main
1936        * surfaces, so just use 4K for now.
1937        */
1938       if (tiling == ISL_TILING_GFX12_CCS)
1939          base_alignment_B = MAX(base_alignment_B, 4096);
1940 
1941       /* Platforms using an aux map require that images be 64K-aligned if
1942        * they're going to used with CCS. This is because the Aux translation
1943        * table maps main surface addresses to aux addresses at a 64K (in the
1944        * main surface) granularity. Because we don't know for sure in ISL if
1945        * a surface will use CCS, we have to guess based on the DISABLE_AUX
1946        * usage bit. The one thing we do know is that we haven't enable CCS on
1947        * linear images yet so we can avoid the extra alignment there.
1948        */
1949       if (dev->info->has_aux_map &&
1950           !(info->usage & ISL_SURF_USAGE_DISABLE_AUX_BIT)) {
1951          base_alignment_B = MAX(base_alignment_B, 64 * 1024);
1952       }
1953    }
1954 
1955    if (ISL_GFX_VER(dev) < 9) {
1956       /* From the Broadwell PRM Vol 5, Surface Layout:
1957        *
1958        *    "In addition to restrictions on maximum height, width, and depth,
1959        *     surfaces are also restricted to a maximum size in bytes. This
1960        *     maximum is 2 GB for all products and all surface types."
1961        *
1962        * This comment is applicable to all Pre-gfx9 platforms.
1963        */
1964       if (size_B > (uint64_t) 1 << 31)
1965          return false;
1966    } else if (ISL_GFX_VER(dev) < 11) {
1967       /* From the Skylake PRM Vol 5, Maximum Surface Size in Bytes:
1968        *    "In addition to restrictions on maximum height, width, and depth,
1969        *     surfaces are also restricted to a maximum size of 2^38 bytes.
1970        *     All pixels within the surface must be contained within 2^38 bytes
1971        *     of the base address."
1972        */
1973       if (size_B > (uint64_t) 1 << 38)
1974          return false;
1975    } else {
1976       /* gfx11+ platforms raised this limit to 2^44 bytes. */
1977       if (size_B > (uint64_t) 1 << 44)
1978          return false;
1979    }
1980 
1981    *surf = (struct isl_surf) {
1982       .dim = info->dim,
1983       .dim_layout = dim_layout,
1984       .msaa_layout = msaa_layout,
1985       .tiling = tiling,
1986       .format = info->format,
1987 
1988       .levels = info->levels,
1989       .samples = info->samples,
1990 
1991       .image_alignment_el = image_align_el,
1992       .logical_level0_px = logical_level0_px,
1993       .phys_level0_sa = phys_level0_sa,
1994 
1995       .size_B = size_B,
1996       .alignment_B = base_alignment_B,
1997       .row_pitch_B = row_pitch_B,
1998       .array_pitch_el_rows = array_pitch_el_rows,
1999       .array_pitch_span = array_pitch_span,
2000 
2001       .usage = info->usage,
2002    };
2003 
2004    return true;
2005 }
2006 
2007 void
isl_surf_get_tile_info(const struct isl_surf * surf,struct isl_tile_info * tile_info)2008 isl_surf_get_tile_info(const struct isl_surf *surf,
2009                        struct isl_tile_info *tile_info)
2010 {
2011    const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2012    isl_tiling_get_info(surf->tiling, surf->dim, surf->msaa_layout, fmtl->bpb,
2013                        surf->samples, tile_info);
2014 }
2015 
2016 bool
isl_surf_get_hiz_surf(const struct isl_device * dev,const struct isl_surf * surf,struct isl_surf * hiz_surf)2017 isl_surf_get_hiz_surf(const struct isl_device *dev,
2018                       const struct isl_surf *surf,
2019                       struct isl_surf *hiz_surf)
2020 {
2021    /* HiZ support does not exist prior to Gfx5 */
2022    if (ISL_GFX_VER(dev) < 5)
2023       return false;
2024 
2025    if (!isl_surf_usage_is_depth(surf->usage))
2026       return false;
2027 
2028    /* From the Sandy Bridge PRM, Vol 2 Part 1,
2029     * 3DSTATE_DEPTH_BUFFER::Hierarchical Depth Buffer Enable,
2030     *
2031     *    If this field is enabled, the Surface Format of the depth buffer
2032     *    cannot be D32_FLOAT_S8X24_UINT or D24_UNORM_S8_UINT. Use of stencil
2033     *    requires the separate stencil buffer.
2034     *
2035     * On SNB+, HiZ can't be used with combined depth-stencil buffers.
2036     */
2037    if (isl_surf_usage_is_stencil(surf->usage))
2038       return false;
2039 
2040    /* Multisampled depth is always interleaved */
2041    assert(surf->msaa_layout == ISL_MSAA_LAYOUT_NONE ||
2042           surf->msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
2043 
2044    /* From the Broadwell PRM Vol. 7, "Hierarchical Depth Buffer":
2045     *
2046     *    "The Surface Type, Height, Width, Depth, Minimum Array Element, Render
2047     *    Target View Extent, and Depth Coordinate Offset X/Y of the
2048     *    hierarchical depth buffer are inherited from the depth buffer. The
2049     *    height and width of the hierarchical depth buffer that must be
2050     *    allocated are computed by the following formulas, where HZ is the
2051     *    hierarchical depth buffer and Z is the depth buffer. The Z_Height,
2052     *    Z_Width, and Z_Depth values given in these formulas are those present
2053     *    in 3DSTATE_DEPTH_BUFFER incremented by one.
2054     *
2055     *    "The value of Z_Height and Z_Width must each be multiplied by 2 before
2056     *    being applied to the table below if Number of Multisamples is set to
2057     *    NUMSAMPLES_4. The value of Z_Height must be multiplied by 2 and
2058     *    Z_Width must be multiplied by 4 before being applied to the table
2059     *    below if Number of Multisamples is set to NUMSAMPLES_8."
2060     *
2061     * In the Sky Lake PRM, the second paragraph is gone.  This means that,
2062     * from Sandy Bridge through Broadwell, HiZ compresses samples in the
2063     * primary depth surface.  On Sky Lake and onward, HiZ compresses pixels.
2064     *
2065     * There are a number of different ways that this discrepency could be
2066     * handled.  The way we have chosen is to simply make MSAA HiZ have the
2067     * same number of samples as the parent surface pre-Sky Lake and always be
2068     * single-sampled on Sky Lake and above.  Since the block sizes of
2069     * compressed formats are given in samples, this neatly handles everything
2070     * without the need for additional HiZ formats with different block sizes
2071     * on SKL+.
2072     */
2073    const unsigned samples = ISL_GFX_VER(dev) >= 9 ? 1 : surf->samples;
2074 
2075    const enum isl_format format =
2076       ISL_GFX_VERX10(dev) >= 125 ? ISL_FORMAT_GFX125_HIZ : ISL_FORMAT_HIZ;
2077 
2078    return isl_surf_init(dev, hiz_surf,
2079                         .dim = surf->dim,
2080                         .format = format,
2081                         .width = surf->logical_level0_px.width,
2082                         .height = surf->logical_level0_px.height,
2083                         .depth = surf->logical_level0_px.depth,
2084                         .levels = surf->levels,
2085                         .array_len = surf->logical_level0_px.array_len,
2086                         .samples = samples,
2087                         .usage = ISL_SURF_USAGE_HIZ_BIT,
2088                         .tiling_flags = ISL_TILING_HIZ_BIT);
2089 }
2090 
2091 bool
isl_surf_get_mcs_surf(const struct isl_device * dev,const struct isl_surf * surf,struct isl_surf * mcs_surf)2092 isl_surf_get_mcs_surf(const struct isl_device *dev,
2093                       const struct isl_surf *surf,
2094                       struct isl_surf *mcs_surf)
2095 {
2096    /* It must be multisampled with an array layout */
2097    if (surf->msaa_layout != ISL_MSAA_LAYOUT_ARRAY)
2098       return false;
2099 
2100    /* The following are true of all multisampled surfaces */
2101    assert(surf->samples > 1);
2102    assert(surf->dim == ISL_SURF_DIM_2D);
2103    assert(surf->levels == 1);
2104    assert(surf->logical_level0_px.depth == 1);
2105    assert(isl_format_supports_multisampling(dev->info, surf->format));
2106 
2107    enum isl_format mcs_format;
2108    switch (surf->samples) {
2109    case 2:  mcs_format = ISL_FORMAT_MCS_2X;  break;
2110    case 4:  mcs_format = ISL_FORMAT_MCS_4X;  break;
2111    case 8:  mcs_format = ISL_FORMAT_MCS_8X;  break;
2112    case 16: mcs_format = ISL_FORMAT_MCS_16X; break;
2113    default:
2114       unreachable("Invalid sample count");
2115    }
2116 
2117    return isl_surf_init(dev, mcs_surf,
2118                         .dim = ISL_SURF_DIM_2D,
2119                         .format = mcs_format,
2120                         .width = surf->logical_level0_px.width,
2121                         .height = surf->logical_level0_px.height,
2122                         .depth = 1,
2123                         .levels = 1,
2124                         .array_len = surf->logical_level0_px.array_len,
2125                         .samples = 1, /* MCS surfaces are really single-sampled */
2126                         .usage = ISL_SURF_USAGE_MCS_BIT,
2127                         .tiling_flags = ISL_TILING_ANY_MASK);
2128 }
2129 
2130 bool
isl_surf_supports_ccs(const struct isl_device * dev,const struct isl_surf * surf,const struct isl_surf * hiz_or_mcs_surf)2131 isl_surf_supports_ccs(const struct isl_device *dev,
2132                       const struct isl_surf *surf,
2133                       const struct isl_surf *hiz_or_mcs_surf)
2134 {
2135    if (surf->usage & ISL_SURF_USAGE_DISABLE_AUX_BIT)
2136       return false;
2137 
2138    if (!isl_format_supports_ccs_d(dev->info, surf->format) &&
2139        !isl_format_supports_ccs_e(dev->info, surf->format))
2140       return false;
2141 
2142    /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
2143     * Target(s)", beneath the "Fast Color Clear" bullet (p326):
2144     *
2145     *     - Support is limited to tiled render targets.
2146     *
2147     * From the Skylake documentation, it is made clear that X-tiling is no
2148     * longer supported:
2149     *
2150     *     - MCS and Lossless compression is supported for
2151     *       TiledY/TileYs/TileYf non-MSRTs only.
2152     *
2153     * From the BSpec (44930) for Gfx12:
2154     *
2155     *    Linear CCS is only allowed for Untyped Buffers but only via HDC
2156     *    Data-Port messages.
2157     *
2158     * We never use untyped messages on surfaces created by ISL on Gfx9+ so
2159     * this means linear is out on Gfx12+ as well.
2160     */
2161    if (surf->tiling == ISL_TILING_LINEAR)
2162       return false;
2163 
2164    /* TODO: Disable for now, as we're not sure about the meaning of
2165     * 3DSTATE_CPSIZE_CONTROL_BUFFER::CPCBCompressionEnable
2166     */
2167    if (isl_surf_usage_is_cpb(surf->usage))
2168       return false;
2169 
2170    if (ISL_GFX_VER(dev) >= 12) {
2171       if (isl_surf_usage_is_stencil(surf->usage)) {
2172          /* HiZ and MCS aren't allowed with stencil */
2173          assert(hiz_or_mcs_surf == NULL || hiz_or_mcs_surf->size_B == 0);
2174 
2175          /* Multi-sampled stencil cannot have CCS */
2176          if (surf->samples > 1)
2177             return false;
2178       } else if (isl_surf_usage_is_depth(surf->usage)) {
2179          const struct isl_surf *hiz_surf = hiz_or_mcs_surf;
2180 
2181          /* With depth surfaces, HIZ is required for CCS. */
2182          if (hiz_surf == NULL || hiz_surf->size_B == 0)
2183             return false;
2184 
2185          assert(hiz_surf->usage & ISL_SURF_USAGE_HIZ_BIT);
2186          assert(hiz_surf->tiling == ISL_TILING_HIZ);
2187          assert(isl_format_is_hiz(hiz_surf->format));
2188       } else if (surf->samples > 1) {
2189          const struct isl_surf *mcs_surf = hiz_or_mcs_surf;
2190 
2191          /* With multisampled color, CCS requires MCS */
2192          if (mcs_surf == NULL || mcs_surf->size_B == 0)
2193             return false;
2194 
2195          assert(mcs_surf->usage & ISL_SURF_USAGE_MCS_BIT);
2196          assert(isl_format_is_mcs(mcs_surf->format));
2197       } else {
2198          /* Single-sampled color can't have MCS or HiZ */
2199          assert(hiz_or_mcs_surf == NULL || hiz_or_mcs_surf->size_B == 0);
2200       }
2201 
2202       /* On Gfx12, all CCS-compressed surface pitches must be multiples of
2203        * 512B.
2204        */
2205       if (surf->row_pitch_B % 512 != 0)
2206          return false;
2207 
2208       /* According to Wa_1406738321, 3D textures need a blit to a new
2209        * surface in order to perform a resolve. For now, just disable CCS.
2210        */
2211       if (surf->dim == ISL_SURF_DIM_3D) {
2212          isl_finishme("%s:%s: CCS for 3D textures is disabled, but a workaround"
2213                       " is available.", __FILE__, __func__);
2214          return false;
2215       }
2216 
2217       /* Wa_1207137018
2218        *
2219        * TODO: implement following workaround currently covered by the
2220        * restriction above. If following conditions are met:
2221        *
2222        *    - RENDER_SURFACE_STATE.Surface Type == 3D
2223        *    - RENDER_SURFACE_STATE.Auxiliary Surface Mode != AUX_NONE
2224        *    - RENDER_SURFACE_STATE.Tiled ResourceMode is TYF or TYS
2225        *
2226        * Set the value of RENDER_SURFACE_STATE.Mip Tail Start LOD to a mip
2227        * that larger than those present in the surface (i.e. 15)
2228        */
2229 
2230       /* TODO: Handle the other tiling formats */
2231       if (surf->tiling != ISL_TILING_Y0 && surf->tiling != ISL_TILING_4 &&
2232           surf->tiling != ISL_TILING_64)
2233          return false;
2234 
2235       /* TODO: Handle single-sampled Tile64. */
2236       if (surf->samples == 1 && surf->tiling == ISL_TILING_64)
2237          return false;
2238    } else {
2239       /* ISL_GFX_VER(dev) < 12 */
2240       if (surf->samples > 1)
2241          return false;
2242 
2243       /* CCS is only for color images on Gfx7-11 */
2244       if (isl_surf_usage_is_depth_or_stencil(surf->usage))
2245          return false;
2246 
2247       /* We're single-sampled color so having HiZ or MCS makes no sense */
2248       assert(hiz_or_mcs_surf == NULL || hiz_or_mcs_surf->size_B == 0);
2249 
2250       /* The PRM doesn't say this explicitly, but fast-clears don't appear to
2251        * work for 3D textures until gfx9 where the layout of 3D textures
2252        * changes to match 2D array textures.
2253        */
2254       if (ISL_GFX_VER(dev) <= 8 && surf->dim != ISL_SURF_DIM_2D)
2255          return false;
2256 
2257       /* From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652 (Color Clear of
2258        * Non-MultiSampler Render Target Restrictions):
2259        *
2260        *    "Support is for non-mip-mapped and non-array surface types only."
2261        *
2262        * This restriction is lifted on gfx8+.  Technically, it may be possible
2263        * to create a CCS for an arrayed or mipmapped image and only enable
2264        * CCS_D when rendering to the base slice.  However, there is no
2265        * documentation tell us what the hardware would do in that case or what
2266        * it does if you walk off the bases slice.  (Does it ignore CCS or does
2267        * it start scribbling over random memory?)  We play it safe and just
2268        * follow the docs and don't allow CCS_D for arrayed or mip-mapped
2269        * surfaces.
2270        */
2271       if (ISL_GFX_VER(dev) <= 7 &&
2272           (surf->levels > 1 || surf->logical_level0_px.array_len > 1))
2273          return false;
2274 
2275       /* From the Skylake documentation, it is made clear that X-tiling is no
2276        * longer supported:
2277        *
2278        *     - MCS and Lossless compression is supported for
2279        *     TiledY/TileYs/TileYf non-MSRTs only.
2280        */
2281       if (ISL_GFX_VER(dev) >= 9 && !isl_tiling_is_any_y(surf->tiling))
2282          return false;
2283    }
2284 
2285    return true;
2286 }
2287 
2288 bool
isl_surf_get_ccs_surf(const struct isl_device * dev,const struct isl_surf * surf,const struct isl_surf * hiz_or_mcs_surf,struct isl_surf * ccs_surf,uint32_t row_pitch_B)2289 isl_surf_get_ccs_surf(const struct isl_device *dev,
2290                       const struct isl_surf *surf,
2291                       const struct isl_surf *hiz_or_mcs_surf,
2292                       struct isl_surf *ccs_surf,
2293                       uint32_t row_pitch_B)
2294 {
2295    if (!isl_surf_supports_ccs(dev, surf, hiz_or_mcs_surf))
2296       return false;
2297 
2298    if (ISL_GFX_VER(dev) >= 12) {
2299       enum isl_format ccs_format;
2300       switch (isl_format_get_layout(surf->format)->bpb) {
2301       case 8:     ccs_format = ISL_FORMAT_GFX12_CCS_8BPP_Y0;    break;
2302       case 16:    ccs_format = ISL_FORMAT_GFX12_CCS_16BPP_Y0;   break;
2303       case 32:    ccs_format = ISL_FORMAT_GFX12_CCS_32BPP_Y0;   break;
2304       case 64:    ccs_format = ISL_FORMAT_GFX12_CCS_64BPP_Y0;   break;
2305       case 128:   ccs_format = ISL_FORMAT_GFX12_CCS_128BPP_Y0;  break;
2306       default:
2307          return false;
2308       }
2309 
2310       /* On Gfx12, the CCS is a scaled-down version of the main surface. We
2311        * model this as the CCS compressing a 2D-view of the entire surface.
2312        */
2313       const bool ok =
2314          isl_surf_init(dev, ccs_surf,
2315                        .dim = ISL_SURF_DIM_2D,
2316                        .format = ccs_format,
2317                        .width = isl_surf_get_row_pitch_el(surf),
2318                        .height = surf->size_B / surf->row_pitch_B,
2319                        .depth = 1,
2320                        .levels = 1,
2321                        .array_len = 1,
2322                        .samples = 1,
2323                        .row_pitch_B = row_pitch_B,
2324                        .usage = ISL_SURF_USAGE_CCS_BIT,
2325                        .tiling_flags = ISL_TILING_GFX12_CCS_BIT);
2326       assert(!ok || ccs_surf->size_B == surf->size_B / 256);
2327       return ok;
2328    } else {
2329       enum isl_format ccs_format;
2330       if (ISL_GFX_VER(dev) >= 9) {
2331          switch (isl_format_get_layout(surf->format)->bpb) {
2332          case 32:    ccs_format = ISL_FORMAT_GFX9_CCS_32BPP;   break;
2333          case 64:    ccs_format = ISL_FORMAT_GFX9_CCS_64BPP;   break;
2334          case 128:   ccs_format = ISL_FORMAT_GFX9_CCS_128BPP;  break;
2335          default:    unreachable("Unsupported CCS format");
2336             return false;
2337          }
2338       } else if (surf->tiling == ISL_TILING_Y0) {
2339          switch (isl_format_get_layout(surf->format)->bpb) {
2340          case 32:    ccs_format = ISL_FORMAT_GFX7_CCS_32BPP_Y;    break;
2341          case 64:    ccs_format = ISL_FORMAT_GFX7_CCS_64BPP_Y;    break;
2342          case 128:   ccs_format = ISL_FORMAT_GFX7_CCS_128BPP_Y;   break;
2343          default:    unreachable("Unsupported CCS format");
2344          }
2345       } else if (surf->tiling == ISL_TILING_X) {
2346          switch (isl_format_get_layout(surf->format)->bpb) {
2347          case 32:    ccs_format = ISL_FORMAT_GFX7_CCS_32BPP_X;    break;
2348          case 64:    ccs_format = ISL_FORMAT_GFX7_CCS_64BPP_X;    break;
2349          case 128:   ccs_format = ISL_FORMAT_GFX7_CCS_128BPP_X;   break;
2350          default:    unreachable("Unsupported CCS format");
2351          }
2352       } else {
2353          unreachable("Invalid tiling format");
2354       }
2355 
2356       return isl_surf_init(dev, ccs_surf,
2357                            .dim = surf->dim,
2358                            .format = ccs_format,
2359                            .width = surf->logical_level0_px.width,
2360                            .height = surf->logical_level0_px.height,
2361                            .depth = surf->logical_level0_px.depth,
2362                            .levels = surf->levels,
2363                            .array_len = surf->logical_level0_px.array_len,
2364                            .samples = 1,
2365                            .row_pitch_B = row_pitch_B,
2366                            .usage = ISL_SURF_USAGE_CCS_BIT,
2367                            .tiling_flags = ISL_TILING_CCS_BIT);
2368    }
2369 }
2370 
2371 #define isl_genX_call(dev, func, ...)              \
2372    switch (ISL_GFX_VERX10(dev)) {                  \
2373    case 40:                                        \
2374       isl_gfx4_##func(__VA_ARGS__);                \
2375       break;                                       \
2376    case 45:                                        \
2377       /* G45 surface state is the same as gfx5 */  \
2378    case 50:                                        \
2379       isl_gfx5_##func(__VA_ARGS__);                \
2380       break;                                       \
2381    case 60:                                        \
2382       isl_gfx6_##func(__VA_ARGS__);                \
2383       break;                                       \
2384    case 70:                                        \
2385       isl_gfx7_##func(__VA_ARGS__);                \
2386       break;                                       \
2387    case 75:                                        \
2388       isl_gfx75_##func(__VA_ARGS__);               \
2389       break;                                       \
2390    case 80:                                        \
2391       isl_gfx8_##func(__VA_ARGS__);                \
2392       break;                                       \
2393    case 90:                                        \
2394       isl_gfx9_##func(__VA_ARGS__);                \
2395       break;                                       \
2396    case 110:                                       \
2397       isl_gfx11_##func(__VA_ARGS__);               \
2398       break;                                       \
2399    case 120:                                       \
2400       isl_gfx12_##func(__VA_ARGS__);               \
2401       break;                                       \
2402    case 125:                                       \
2403       isl_gfx125_##func(__VA_ARGS__);              \
2404       break;                                       \
2405    default:                                        \
2406       assert(!"Unknown hardware generation");      \
2407    }
2408 
2409 void
isl_surf_fill_state_s(const struct isl_device * dev,void * state,const struct isl_surf_fill_state_info * restrict info)2410 isl_surf_fill_state_s(const struct isl_device *dev, void *state,
2411                       const struct isl_surf_fill_state_info *restrict info)
2412 {
2413 #ifndef NDEBUG
2414    isl_surf_usage_flags_t _base_usage =
2415       info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
2416                            ISL_SURF_USAGE_TEXTURE_BIT |
2417                            ISL_SURF_USAGE_STORAGE_BIT);
2418    /* They may only specify one of the above bits at a time */
2419    assert(__builtin_popcount(_base_usage) == 1);
2420    /* The only other allowed bit is ISL_SURF_USAGE_CUBE_BIT */
2421    assert((info->view->usage & ~ISL_SURF_USAGE_CUBE_BIT) == _base_usage);
2422 #endif
2423 
2424    if (info->surf->dim == ISL_SURF_DIM_3D) {
2425       assert(info->view->base_array_layer + info->view->array_len <=
2426              info->surf->logical_level0_px.depth);
2427    } else {
2428       assert(info->view->base_array_layer + info->view->array_len <=
2429              info->surf->logical_level0_px.array_len);
2430    }
2431 
2432    isl_genX_call(dev, surf_fill_state_s, dev, state, info);
2433 }
2434 
2435 void
isl_buffer_fill_state_s(const struct isl_device * dev,void * state,const struct isl_buffer_fill_state_info * restrict info)2436 isl_buffer_fill_state_s(const struct isl_device *dev, void *state,
2437                         const struct isl_buffer_fill_state_info *restrict info)
2438 {
2439    isl_genX_call(dev, buffer_fill_state_s, dev, state, info);
2440 }
2441 
2442 void
isl_null_fill_state_s(const struct isl_device * dev,void * state,const struct isl_null_fill_state_info * restrict info)2443 isl_null_fill_state_s(const struct isl_device *dev, void *state,
2444                       const struct isl_null_fill_state_info *restrict info)
2445 {
2446    isl_genX_call(dev, null_fill_state, dev, state, info);
2447 }
2448 
2449 void
isl_emit_depth_stencil_hiz_s(const struct isl_device * dev,void * batch,const struct isl_depth_stencil_hiz_emit_info * restrict info)2450 isl_emit_depth_stencil_hiz_s(const struct isl_device *dev, void *batch,
2451                              const struct isl_depth_stencil_hiz_emit_info *restrict info)
2452 {
2453    if (info->depth_surf && info->stencil_surf) {
2454       if (!dev->info->has_hiz_and_separate_stencil) {
2455          assert(info->depth_surf == info->stencil_surf);
2456          assert(info->depth_address == info->stencil_address);
2457       }
2458       assert(info->depth_surf->dim == info->stencil_surf->dim);
2459    }
2460 
2461    if (info->depth_surf) {
2462       assert((info->depth_surf->usage & ISL_SURF_USAGE_DEPTH_BIT));
2463       if (info->depth_surf->dim == ISL_SURF_DIM_3D) {
2464          assert(info->view->base_array_layer + info->view->array_len <=
2465                 info->depth_surf->logical_level0_px.depth);
2466       } else {
2467          assert(info->view->base_array_layer + info->view->array_len <=
2468                 info->depth_surf->logical_level0_px.array_len);
2469       }
2470    }
2471 
2472    if (info->stencil_surf) {
2473       assert((info->stencil_surf->usage & ISL_SURF_USAGE_STENCIL_BIT));
2474       if (info->stencil_surf->dim == ISL_SURF_DIM_3D) {
2475          assert(info->view->base_array_layer + info->view->array_len <=
2476                 info->stencil_surf->logical_level0_px.depth);
2477       } else {
2478          assert(info->view->base_array_layer + info->view->array_len <=
2479                 info->stencil_surf->logical_level0_px.array_len);
2480       }
2481    }
2482 
2483    isl_genX_call(dev, emit_depth_stencil_hiz_s, dev, batch, info);
2484 }
2485 
2486 void
isl_emit_cpb_control_s(const struct isl_device * dev,void * batch,const struct isl_cpb_emit_info * restrict info)2487 isl_emit_cpb_control_s(const struct isl_device *dev, void *batch,
2488                        const struct isl_cpb_emit_info *restrict info)
2489 {
2490    if (info->surf) {
2491       assert((info->surf->usage & ISL_SURF_USAGE_CPB_BIT));
2492       assert(info->surf->dim != ISL_SURF_DIM_3D);
2493       assert(info->surf->tiling == ISL_TILING_4 ||
2494              info->surf->tiling == ISL_TILING_64);
2495       assert(info->surf->format == ISL_FORMAT_R8_UINT);
2496    }
2497 
2498    isl_genX_call(dev, emit_cpb_control_s, dev, batch, info);
2499 }
2500 
2501 /**
2502  * A variant of isl_surf_get_image_offset_sa() specific to
2503  * ISL_DIM_LAYOUT_GFX4_2D.
2504  */
2505 static void
get_image_offset_sa_gfx4_2d(const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t * x_offset_sa,uint32_t * y_offset_sa)2506 get_image_offset_sa_gfx4_2d(const struct isl_surf *surf,
2507                             uint32_t level, uint32_t logical_array_layer,
2508                             uint32_t *x_offset_sa,
2509                             uint32_t *y_offset_sa)
2510 {
2511    assert(level < surf->levels);
2512    if (surf->dim == ISL_SURF_DIM_3D)
2513       assert(logical_array_layer < surf->logical_level0_px.depth);
2514    else
2515       assert(logical_array_layer < surf->logical_level0_px.array_len);
2516 
2517    const struct isl_extent3d image_align_sa =
2518       isl_surf_get_image_alignment_sa(surf);
2519 
2520    const uint32_t W0 = surf->phys_level0_sa.width;
2521    const uint32_t H0 = surf->phys_level0_sa.height;
2522 
2523    const uint32_t phys_layer = logical_array_layer *
2524       (surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY ? surf->samples : 1);
2525 
2526    uint32_t x = 0;
2527    uint32_t y = phys_layer * isl_surf_get_array_pitch_sa_rows(surf);
2528 
2529    for (uint32_t l = 0; l < level; ++l) {
2530       if (l == 1) {
2531          uint32_t W = isl_minify(W0, l);
2532          x += isl_align_npot(W, image_align_sa.w);
2533       } else {
2534          uint32_t H = isl_minify(H0, l);
2535          y += isl_align_npot(H, image_align_sa.h);
2536       }
2537    }
2538 
2539    *x_offset_sa = x;
2540    *y_offset_sa = y;
2541 }
2542 
2543 /**
2544  * A variant of isl_surf_get_image_offset_sa() specific to
2545  * ISL_DIM_LAYOUT_GFX4_3D.
2546  */
2547 static void
get_image_offset_sa_gfx4_3d(const struct isl_surf * surf,uint32_t level,uint32_t logical_z_offset_px,uint32_t * x_offset_sa,uint32_t * y_offset_sa)2548 get_image_offset_sa_gfx4_3d(const struct isl_surf *surf,
2549                             uint32_t level, uint32_t logical_z_offset_px,
2550                             uint32_t *x_offset_sa,
2551                             uint32_t *y_offset_sa)
2552 {
2553    assert(level < surf->levels);
2554    if (surf->dim == ISL_SURF_DIM_3D) {
2555       assert(surf->phys_level0_sa.array_len == 1);
2556       assert(logical_z_offset_px < isl_minify(surf->phys_level0_sa.depth, level));
2557    } else {
2558       assert(surf->dim == ISL_SURF_DIM_2D);
2559       assert(surf->usage & ISL_SURF_USAGE_CUBE_BIT);
2560       assert(surf->phys_level0_sa.array_len == 6);
2561       assert(logical_z_offset_px < surf->phys_level0_sa.array_len);
2562    }
2563 
2564    const struct isl_extent3d image_align_sa =
2565       isl_surf_get_image_alignment_sa(surf);
2566 
2567    const uint32_t W0 = surf->phys_level0_sa.width;
2568    const uint32_t H0 = surf->phys_level0_sa.height;
2569    const uint32_t D0 = surf->phys_level0_sa.depth;
2570    const uint32_t AL = surf->phys_level0_sa.array_len;
2571 
2572    uint32_t x = 0;
2573    uint32_t y = 0;
2574 
2575    for (uint32_t l = 0; l < level; ++l) {
2576       const uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa.h);
2577       const uint32_t level_d =
2578          isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, l) : AL,
2579                         image_align_sa.d);
2580       const uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
2581 
2582       y += level_h * max_layers_vert;
2583    }
2584 
2585    const uint32_t level_w = isl_align_npot(isl_minify(W0, level), image_align_sa.w);
2586    const uint32_t level_h = isl_align_npot(isl_minify(H0, level), image_align_sa.h);
2587    const uint32_t level_d =
2588       isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, level) : AL,
2589                      image_align_sa.d);
2590 
2591    const uint32_t max_layers_horiz = MIN(level_d, 1u << level);
2592 
2593    x += level_w * (logical_z_offset_px % max_layers_horiz);
2594    y += level_h * (logical_z_offset_px / max_layers_horiz);
2595 
2596    *x_offset_sa = x;
2597    *y_offset_sa = y;
2598 }
2599 
2600 static void
get_image_offset_sa_gfx6_stencil_hiz(const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t * x_offset_sa,uint32_t * y_offset_sa)2601 get_image_offset_sa_gfx6_stencil_hiz(const struct isl_surf *surf,
2602                                      uint32_t level,
2603                                      uint32_t logical_array_layer,
2604                                      uint32_t *x_offset_sa,
2605                                      uint32_t *y_offset_sa)
2606 {
2607    assert(level < surf->levels);
2608    assert(surf->logical_level0_px.depth == 1);
2609    assert(logical_array_layer < surf->logical_level0_px.array_len);
2610 
2611    const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2612 
2613    const struct isl_extent3d image_align_sa =
2614       isl_surf_get_image_alignment_sa(surf);
2615 
2616    struct isl_tile_info tile_info;
2617    isl_surf_get_tile_info(surf, &tile_info);
2618    const struct isl_extent2d tile_extent_sa = {
2619       .w = tile_info.logical_extent_el.w * fmtl->bw,
2620       .h = tile_info.logical_extent_el.h * fmtl->bh,
2621    };
2622    /* Tile size is a multiple of image alignment */
2623    assert(tile_extent_sa.w % image_align_sa.w == 0);
2624    assert(tile_extent_sa.h % image_align_sa.h == 0);
2625 
2626    const uint32_t W0 = surf->phys_level0_sa.w;
2627    const uint32_t H0 = surf->phys_level0_sa.h;
2628 
2629    /* Each image has the same height as LOD0 because the hardware thinks
2630     * everything is LOD0
2631     */
2632    const uint32_t H = isl_align(H0, image_align_sa.h);
2633 
2634    /* Quick sanity check for consistency */
2635    if (surf->phys_level0_sa.array_len > 1)
2636       assert(surf->array_pitch_el_rows == isl_assert_div(H, fmtl->bh));
2637 
2638    uint32_t x = 0, y = 0;
2639    for (uint32_t l = 0; l < level; ++l) {
2640       const uint32_t W = isl_minify(W0, l);
2641 
2642       const uint32_t w = isl_align(W, tile_extent_sa.w);
2643       const uint32_t h = isl_align(H * surf->phys_level0_sa.a,
2644                                    tile_extent_sa.h);
2645 
2646       if (l == 0) {
2647          y += h;
2648       } else {
2649          x += w;
2650       }
2651    }
2652 
2653    y += H * logical_array_layer;
2654 
2655    *x_offset_sa = x;
2656    *y_offset_sa = y;
2657 }
2658 
2659 /**
2660  * A variant of isl_surf_get_image_offset_sa() specific to
2661  * ISL_DIM_LAYOUT_GFX9_1D.
2662  */
2663 static void
get_image_offset_sa_gfx9_1d(const struct isl_surf * surf,uint32_t level,uint32_t layer,uint32_t * x_offset_sa,uint32_t * y_offset_sa)2664 get_image_offset_sa_gfx9_1d(const struct isl_surf *surf,
2665                             uint32_t level, uint32_t layer,
2666                             uint32_t *x_offset_sa,
2667                             uint32_t *y_offset_sa)
2668 {
2669    assert(level < surf->levels);
2670    assert(layer < surf->phys_level0_sa.array_len);
2671    assert(surf->phys_level0_sa.height == 1);
2672    assert(surf->phys_level0_sa.depth == 1);
2673    assert(surf->samples == 1);
2674 
2675    const uint32_t W0 = surf->phys_level0_sa.width;
2676    const struct isl_extent3d image_align_sa =
2677       isl_surf_get_image_alignment_sa(surf);
2678 
2679    uint32_t x = 0;
2680 
2681    for (uint32_t l = 0; l < level; ++l) {
2682       uint32_t W = isl_minify(W0, l);
2683       uint32_t w = isl_align_npot(W, image_align_sa.w);
2684 
2685       x += w;
2686    }
2687 
2688    *x_offset_sa = x;
2689    *y_offset_sa = layer * isl_surf_get_array_pitch_sa_rows(surf);
2690 }
2691 
2692 /**
2693  * Calculate the offset, in units of surface samples, to a subimage in the
2694  * surface.
2695  *
2696  * @invariant level < surface levels
2697  * @invariant logical_array_layer < logical array length of surface
2698  * @invariant logical_z_offset_px < logical depth of surface at level
2699  */
2700 void
isl_surf_get_image_offset_sa(const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t logical_z_offset_px,uint32_t * x_offset_sa,uint32_t * y_offset_sa,uint32_t * z_offset_sa,uint32_t * array_offset)2701 isl_surf_get_image_offset_sa(const struct isl_surf *surf,
2702                              uint32_t level,
2703                              uint32_t logical_array_layer,
2704                              uint32_t logical_z_offset_px,
2705                              uint32_t *x_offset_sa,
2706                              uint32_t *y_offset_sa,
2707                              uint32_t *z_offset_sa,
2708                              uint32_t *array_offset)
2709 {
2710    assert(level < surf->levels);
2711    assert(logical_array_layer < surf->logical_level0_px.array_len);
2712    assert(logical_z_offset_px
2713           < isl_minify(surf->logical_level0_px.depth, level));
2714 
2715    switch (surf->dim_layout) {
2716    case ISL_DIM_LAYOUT_GFX9_1D:
2717       get_image_offset_sa_gfx9_1d(surf, level, logical_array_layer,
2718                                   x_offset_sa, y_offset_sa);
2719       *z_offset_sa = 0;
2720       *array_offset = 0;
2721       break;
2722    case ISL_DIM_LAYOUT_GFX4_2D:
2723       get_image_offset_sa_gfx4_2d(surf, level, logical_array_layer
2724                                   + logical_z_offset_px,
2725                                   x_offset_sa, y_offset_sa);
2726       *z_offset_sa = 0;
2727       *array_offset = 0;
2728       break;
2729    case ISL_DIM_LAYOUT_GFX4_3D:
2730       get_image_offset_sa_gfx4_3d(surf, level, logical_array_layer +
2731                                   logical_z_offset_px,
2732                                   x_offset_sa, y_offset_sa);
2733       *z_offset_sa = 0;
2734       *array_offset = 0;
2735       break;
2736    case ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ:
2737       get_image_offset_sa_gfx6_stencil_hiz(surf, level, logical_array_layer +
2738                                            logical_z_offset_px,
2739                                            x_offset_sa, y_offset_sa);
2740       *z_offset_sa = 0;
2741       *array_offset = 0;
2742       break;
2743 
2744    default:
2745       unreachable("not reached");
2746    }
2747 }
2748 
2749 void
isl_surf_get_image_offset_el(const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t logical_z_offset_px,uint32_t * x_offset_el,uint32_t * y_offset_el,uint32_t * z_offset_el,uint32_t * array_offset)2750 isl_surf_get_image_offset_el(const struct isl_surf *surf,
2751                              uint32_t level,
2752                              uint32_t logical_array_layer,
2753                              uint32_t logical_z_offset_px,
2754                              uint32_t *x_offset_el,
2755                              uint32_t *y_offset_el,
2756                              uint32_t *z_offset_el,
2757                              uint32_t *array_offset)
2758 {
2759    const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2760 
2761    assert(level < surf->levels);
2762    assert(logical_array_layer < surf->logical_level0_px.array_len);
2763    assert(logical_z_offset_px
2764           < isl_minify(surf->logical_level0_px.depth, level));
2765 
2766    uint32_t x_offset_sa, y_offset_sa, z_offset_sa;
2767    isl_surf_get_image_offset_sa(surf, level,
2768                                 logical_array_layer,
2769                                 logical_z_offset_px,
2770                                 &x_offset_sa,
2771                                 &y_offset_sa,
2772                                 &z_offset_sa,
2773                                 array_offset);
2774 
2775    *x_offset_el = x_offset_sa / fmtl->bw;
2776    *y_offset_el = y_offset_sa / fmtl->bh;
2777    *z_offset_el = z_offset_sa / fmtl->bd;
2778 }
2779 
2780 void
isl_surf_get_image_offset_B_tile_sa(const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t logical_z_offset_px,uint64_t * offset_B,uint32_t * x_offset_sa,uint32_t * y_offset_sa)2781 isl_surf_get_image_offset_B_tile_sa(const struct isl_surf *surf,
2782                                     uint32_t level,
2783                                     uint32_t logical_array_layer,
2784                                     uint32_t logical_z_offset_px,
2785                                     uint64_t *offset_B,
2786                                     uint32_t *x_offset_sa,
2787                                     uint32_t *y_offset_sa)
2788 {
2789    const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2790 
2791    uint32_t x_offset_el, y_offset_el;
2792    isl_surf_get_image_offset_B_tile_el(surf, level,
2793                                        logical_array_layer,
2794                                        logical_z_offset_px,
2795                                        offset_B,
2796                                        &x_offset_el,
2797                                        &y_offset_el);
2798 
2799    if (x_offset_sa) {
2800       *x_offset_sa = x_offset_el * fmtl->bw;
2801    } else {
2802       assert(x_offset_el == 0);
2803    }
2804 
2805    if (y_offset_sa) {
2806       *y_offset_sa = y_offset_el * fmtl->bh;
2807    } else {
2808       assert(y_offset_el == 0);
2809    }
2810 }
2811 
2812 void
isl_surf_get_image_offset_B_tile_el(const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t logical_z_offset_px,uint64_t * offset_B,uint32_t * x_offset_el,uint32_t * y_offset_el)2813 isl_surf_get_image_offset_B_tile_el(const struct isl_surf *surf,
2814                                     uint32_t level,
2815                                     uint32_t logical_array_layer,
2816                                     uint32_t logical_z_offset_px,
2817                                     uint64_t *offset_B,
2818                                     uint32_t *x_offset_el,
2819                                     uint32_t *y_offset_el)
2820 {
2821    const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2822 
2823    uint32_t total_x_offset_el, total_y_offset_el;
2824    uint32_t total_z_offset_el, total_array_offset;
2825    isl_surf_get_image_offset_el(surf, level, logical_array_layer,
2826                                 logical_z_offset_px,
2827                                 &total_x_offset_el,
2828                                 &total_y_offset_el,
2829                                 &total_z_offset_el,
2830                                 &total_array_offset);
2831 
2832    uint32_t z_offset_el, array_offset;
2833    isl_tiling_get_intratile_offset_el(surf->tiling, surf->dim,
2834                                       surf->msaa_layout, fmtl->bpb,
2835                                       surf->samples,
2836                                       surf->row_pitch_B,
2837                                       surf->array_pitch_el_rows,
2838                                       total_x_offset_el,
2839                                       total_y_offset_el,
2840                                       total_z_offset_el,
2841                                       total_array_offset,
2842                                       offset_B,
2843                                       x_offset_el,
2844                                       y_offset_el,
2845                                       &z_offset_el,
2846                                       &array_offset);
2847    assert(z_offset_el == 0);
2848    assert(array_offset == 0);
2849 }
2850 
2851 void
isl_surf_get_image_range_B_tile(const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t logical_z_offset_px,uint64_t * start_tile_B,uint64_t * end_tile_B)2852 isl_surf_get_image_range_B_tile(const struct isl_surf *surf,
2853                                 uint32_t level,
2854                                 uint32_t logical_array_layer,
2855                                 uint32_t logical_z_offset_px,
2856                                 uint64_t *start_tile_B,
2857                                 uint64_t *end_tile_B)
2858 {
2859    uint32_t start_x_offset_el, start_y_offset_el;
2860    uint32_t start_z_offset_el, start_array_slice;
2861    isl_surf_get_image_offset_el(surf, level, logical_array_layer,
2862                                 logical_z_offset_px,
2863                                 &start_x_offset_el,
2864                                 &start_y_offset_el,
2865                                 &start_z_offset_el,
2866                                 &start_array_slice);
2867 
2868    /* Compute the size of the subimage in surface elements */
2869    const uint32_t subimage_w_sa = isl_minify(surf->phys_level0_sa.w, level);
2870    const uint32_t subimage_h_sa = isl_minify(surf->phys_level0_sa.h, level);
2871    const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2872    const uint32_t subimage_w_el = isl_align_div_npot(subimage_w_sa, fmtl->bw);
2873    const uint32_t subimage_h_el = isl_align_div_npot(subimage_h_sa, fmtl->bh);
2874 
2875    /* Find the last pixel */
2876    uint32_t end_x_offset_el = start_x_offset_el + subimage_w_el - 1;
2877    uint32_t end_y_offset_el = start_y_offset_el + subimage_h_el - 1;
2878 
2879    /* We only consider one Z or array slice */
2880    const uint32_t end_z_offset_el = start_z_offset_el;
2881    const uint32_t end_array_slice = start_array_slice;
2882 
2883    UNUSED uint32_t x_offset_el, y_offset_el, z_offset_el, array_slice;
2884    isl_tiling_get_intratile_offset_el(surf->tiling, surf->dim,
2885                                       surf->msaa_layout, fmtl->bpb,
2886                                       surf->samples,
2887                                       surf->row_pitch_B,
2888                                       surf->array_pitch_el_rows,
2889                                       start_x_offset_el,
2890                                       start_y_offset_el,
2891                                       start_z_offset_el,
2892                                       start_array_slice,
2893                                       start_tile_B,
2894                                       &x_offset_el,
2895                                       &y_offset_el,
2896                                       &z_offset_el,
2897                                       &array_slice);
2898 
2899    isl_tiling_get_intratile_offset_el(surf->tiling, surf->dim,
2900                                       surf->msaa_layout, fmtl->bpb,
2901                                       surf->samples,
2902                                       surf->row_pitch_B,
2903                                       surf->array_pitch_el_rows,
2904                                       end_x_offset_el,
2905                                       end_y_offset_el,
2906                                       end_z_offset_el,
2907                                       end_array_slice,
2908                                       end_tile_B,
2909                                       &x_offset_el,
2910                                       &y_offset_el,
2911                                       &z_offset_el,
2912                                       &array_slice);
2913 
2914    /* We want the range we return to be exclusive but the tile containing the
2915     * last pixel (what we just calculated) is inclusive.  Add one.
2916     */
2917    (*end_tile_B)++;
2918 
2919    assert(*end_tile_B <= surf->size_B);
2920 }
2921 
2922 void
isl_surf_get_image_surf(const struct isl_device * dev,const struct isl_surf * surf,uint32_t level,uint32_t logical_array_layer,uint32_t logical_z_offset_px,struct isl_surf * image_surf,uint64_t * offset_B,uint32_t * x_offset_sa,uint32_t * y_offset_sa)2923 isl_surf_get_image_surf(const struct isl_device *dev,
2924                         const struct isl_surf *surf,
2925                         uint32_t level,
2926                         uint32_t logical_array_layer,
2927                         uint32_t logical_z_offset_px,
2928                         struct isl_surf *image_surf,
2929                         uint64_t *offset_B,
2930                         uint32_t *x_offset_sa,
2931                         uint32_t *y_offset_sa)
2932 {
2933    isl_surf_get_image_offset_B_tile_sa(surf,
2934                                        level,
2935                                        logical_array_layer,
2936                                        logical_z_offset_px,
2937                                        offset_B,
2938                                        x_offset_sa,
2939                                        y_offset_sa);
2940 
2941    /* Even for cube maps there will be only single face, therefore drop the
2942     * corresponding flag if present.
2943     */
2944    const isl_surf_usage_flags_t usage =
2945       surf->usage & (~ISL_SURF_USAGE_CUBE_BIT);
2946 
2947    bool ok UNUSED;
2948    ok = isl_surf_init(dev, image_surf,
2949                       .dim = ISL_SURF_DIM_2D,
2950                       .format = surf->format,
2951                       .width = isl_minify(surf->logical_level0_px.w, level),
2952                       .height = isl_minify(surf->logical_level0_px.h, level),
2953                       .depth = 1,
2954                       .levels = 1,
2955                       .array_len = 1,
2956                       .samples = surf->samples,
2957                       .row_pitch_B = surf->row_pitch_B,
2958                       .usage = usage,
2959                       .tiling_flags = (1 << surf->tiling));
2960    assert(ok);
2961 }
2962 
2963 bool
isl_surf_get_uncompressed_surf(const struct isl_device * dev,const struct isl_surf * surf,const struct isl_view * view,struct isl_surf * ucompr_surf,struct isl_view * ucompr_view,uint64_t * offset_B,uint32_t * x_offset_el,uint32_t * y_offset_el)2964 isl_surf_get_uncompressed_surf(const struct isl_device *dev,
2965                                const struct isl_surf *surf,
2966                                const struct isl_view *view,
2967                                struct isl_surf *ucompr_surf,
2968                                struct isl_view *ucompr_view,
2969                                uint64_t *offset_B,
2970                                uint32_t *x_offset_el,
2971                                uint32_t *y_offset_el)
2972 {
2973    const struct isl_format_layout *fmtl =
2974       isl_format_get_layout(surf->format);
2975    const enum isl_format view_format = view->format;
2976 
2977    assert(fmtl->bw > 1 || fmtl->bh > 1 || fmtl->bd > 1);
2978    assert(isl_format_is_compressed(surf->format));
2979    assert(!isl_format_is_compressed(view->format));
2980    assert(isl_format_get_layout(view->format)->bpb == fmtl->bpb);
2981    assert(view->levels == 1);
2982 
2983    const uint32_t view_width_px =
2984       isl_minify(surf->logical_level0_px.width, view->base_level);
2985    const uint32_t view_height_px =
2986       isl_minify(surf->logical_level0_px.height, view->base_level);
2987 
2988    assert(surf->samples == 1);
2989    const uint32_t view_width_el = isl_align_div_npot(view_width_px, fmtl->bw);
2990    const uint32_t view_height_el = isl_align_div_npot(view_height_px, fmtl->bh);
2991 
2992    /* If we ever enable 3D block formats, we'll need to re-think this */
2993    assert(fmtl->bd == 1);
2994 
2995    if (view->array_len > 1) {
2996       /* The Skylake PRM Vol. 2d, "RENDER_SURFACE_STATE::X Offset" says:
2997        *
2998        *    "If Surface Array is enabled, this field must be zero."
2999        *
3000        * The PRMs for other hardware have similar text.  This is also tricky
3001        * to handle with things like BLORP's SW offsetting because the
3002        * increased surface size required for the offset may result in an image
3003        * height greater than qpitch.
3004        */
3005       if (view->base_level > 0)
3006          return false;
3007 
3008       /* On Haswell and earlier, RENDER_SURFACE_STATE doesn't have a QPitch
3009        * field; it only has "array pitch span" which means the QPitch is
3010        * automatically calculated.  Since we're smashing the surface format
3011        * (block formats are subtly different) and the number of miplevels,
3012        * that calculation will get thrown off.  This means we can't do arrays
3013        * even at LOD0
3014        *
3015        * On Broadwell, we do have a QPitch field which we can control.
3016        * However, HALIGN and VALIGN are specified in pixels and are
3017        * hard-coded to align to exactly the block size of the compressed
3018        * texture.  This means that, when reinterpreted as a non-compressed
3019        * the QPitch may be anything but the HW requires it to be properly
3020        * aligned.
3021        */
3022       if (ISL_GFX_VER(dev) < 9)
3023          return false;
3024 
3025       *ucompr_surf = *surf;
3026       ucompr_surf->levels = 1;
3027       ucompr_surf->format = view_format;
3028 
3029       /* We're making an uncompressed view here.  The image dimensions
3030        * need to be scaled down by the block size.
3031        */
3032       assert(ucompr_surf->logical_level0_px.width == view_width_px);
3033       assert(ucompr_surf->logical_level0_px.height == view_height_px);
3034       ucompr_surf->logical_level0_px.width = view_width_el;
3035       ucompr_surf->logical_level0_px.height = view_height_el;
3036       ucompr_surf->phys_level0_sa = isl_surf_get_phys_level0_el(surf);
3037 
3038       /* The surface mostly stays as-is; there is no offset */
3039       *offset_B = 0;
3040       *x_offset_el = 0;
3041       *y_offset_el = 0;
3042 
3043       /* The view remains the same */
3044       *ucompr_view = *view;
3045    } else {
3046       /* If only one array slice is requested, directly offset to that slice.
3047        * We could, in theory, still use arrays in some cases but BLORP isn't
3048        * prepared for this and everyone who calls this function should be
3049        * prepared to handle an X/Y offset.
3050        */
3051       isl_surf_get_image_offset_B_tile_el(surf,
3052                                           view->base_level,
3053                                           surf->dim == ISL_SURF_DIM_3D ?
3054                                              0 : view->base_array_layer,
3055                                           surf->dim == ISL_SURF_DIM_3D ?
3056                                              view->base_array_layer : 0,
3057                                           offset_B,
3058                                           x_offset_el,
3059                                           y_offset_el);
3060 
3061       /* Even for cube maps there will be only single face, therefore drop the
3062        * corresponding flag if present.
3063        */
3064       const isl_surf_usage_flags_t usage =
3065          surf->usage & (~ISL_SURF_USAGE_CUBE_BIT);
3066 
3067       bool ok UNUSED;
3068       ok = isl_surf_init(dev, ucompr_surf,
3069                          .dim = ISL_SURF_DIM_2D,
3070                          .format = view_format,
3071                          .width = view_width_el,
3072                          .height = view_height_el,
3073                          .depth = 1,
3074                          .levels = 1,
3075                          .array_len = 1,
3076                          .samples = 1,
3077                          .row_pitch_B = surf->row_pitch_B,
3078                          .usage = usage,
3079                          .tiling_flags = (1 << surf->tiling));
3080       assert(ok);
3081 
3082       /* The newly created image represents the one subimage we're
3083        * referencing with this view so it only has one array slice and
3084        * miplevel.
3085        */
3086       *ucompr_view = *view;
3087       ucompr_view->base_array_layer = 0;
3088       ucompr_view->base_level = 0;
3089    }
3090 
3091    return true;
3092 }
3093 
3094 void
isl_tiling_get_intratile_offset_el(enum isl_tiling tiling,enum isl_surf_dim dim,enum isl_msaa_layout msaa_layout,uint32_t bpb,uint32_t samples,uint32_t row_pitch_B,uint32_t array_pitch_el_rows,uint32_t total_x_offset_el,uint32_t total_y_offset_el,uint32_t total_z_offset_el,uint32_t total_array_offset,uint64_t * tile_offset_B,uint32_t * x_offset_el,uint32_t * y_offset_el,uint32_t * z_offset_el,uint32_t * array_offset)3095 isl_tiling_get_intratile_offset_el(enum isl_tiling tiling,
3096                                    enum isl_surf_dim dim,
3097                                    enum isl_msaa_layout msaa_layout,
3098                                    uint32_t bpb,
3099                                    uint32_t samples,
3100                                    uint32_t row_pitch_B,
3101                                    uint32_t array_pitch_el_rows,
3102                                    uint32_t total_x_offset_el,
3103                                    uint32_t total_y_offset_el,
3104                                    uint32_t total_z_offset_el,
3105                                    uint32_t total_array_offset,
3106                                    uint64_t *tile_offset_B,
3107                                    uint32_t *x_offset_el,
3108                                    uint32_t *y_offset_el,
3109                                    uint32_t *z_offset_el,
3110                                    uint32_t *array_offset)
3111 {
3112    if (tiling == ISL_TILING_LINEAR) {
3113       assert(bpb % 8 == 0);
3114       assert(samples == 1);
3115       assert(total_z_offset_el == 0 && total_array_offset == 0);
3116       *tile_offset_B = (uint64_t)total_y_offset_el * row_pitch_B +
3117                        (uint64_t)total_x_offset_el * (bpb / 8);
3118       *x_offset_el = 0;
3119       *y_offset_el = 0;
3120       *z_offset_el = 0;
3121       *array_offset = 0;
3122       return;
3123    }
3124 
3125    struct isl_tile_info tile_info;
3126    isl_tiling_get_info(tiling, dim, msaa_layout, bpb, samples, &tile_info);
3127 
3128    /* Pitches must make sense with the tiling */
3129    assert(row_pitch_B % tile_info.phys_extent_B.width == 0);
3130    if (tile_info.logical_extent_el.d > 1 || tile_info.logical_extent_el.a > 1)
3131       assert(array_pitch_el_rows % tile_info.logical_extent_el.h == 0);
3132 
3133    /* For non-power-of-two formats, we need the address to be both tile and
3134     * element-aligned.  The easiest way to achieve this is to work with a tile
3135     * that is three times as wide as the regular tile.
3136     *
3137     * The tile info returned by get_tile_info has a logical size that is an
3138     * integer number of tile_info.format_bpb size elements.  To scale the
3139     * tile, we scale up the physical width and then treat the logical tile
3140     * size as if it has bpb size elements.
3141     */
3142    const uint32_t tile_el_scale = bpb / tile_info.format_bpb;
3143    tile_info.phys_extent_B.width *= tile_el_scale;
3144 
3145    /* Compute the offset into the tile */
3146    *x_offset_el = total_x_offset_el % tile_info.logical_extent_el.w;
3147    *y_offset_el = total_y_offset_el % tile_info.logical_extent_el.h;
3148    *z_offset_el = total_z_offset_el % tile_info.logical_extent_el.d;
3149    *array_offset = total_array_offset % tile_info.logical_extent_el.a;
3150 
3151    /* Compute the offset of the tile in units of whole tiles */
3152    uint32_t x_offset_tl = total_x_offset_el / tile_info.logical_extent_el.w;
3153    uint32_t y_offset_tl = total_y_offset_el / tile_info.logical_extent_el.h;
3154    uint32_t z_offset_tl = total_z_offset_el / tile_info.logical_extent_el.d;
3155    uint32_t a_offset_tl = total_array_offset / tile_info.logical_extent_el.a;
3156 
3157    /* Compute an array pitch in number of tiles */
3158    uint32_t array_pitch_tl_rows =
3159       array_pitch_el_rows / tile_info.logical_extent_el.h;
3160 
3161    /* Add the Z and array offset to the Y offset to get a 2D offset */
3162    y_offset_tl += (z_offset_tl + a_offset_tl) * array_pitch_tl_rows;
3163 
3164    *tile_offset_B =
3165       (uint64_t)y_offset_tl * tile_info.phys_extent_B.h * row_pitch_B +
3166       (uint64_t)x_offset_tl * tile_info.phys_extent_B.h * tile_info.phys_extent_B.w;
3167 }
3168 
3169 uint32_t
isl_surf_get_depth_format(const struct isl_device * dev,const struct isl_surf * surf)3170 isl_surf_get_depth_format(const struct isl_device *dev,
3171                           const struct isl_surf *surf)
3172 {
3173    /* Support for separate stencil buffers began in gfx5. Support for
3174     * interleaved depthstencil buffers ceased in gfx7. The intermediate gens,
3175     * those that supported separate and interleaved stencil, were gfx5 and
3176     * gfx6.
3177     *
3178     * For a list of all available formats, see the Sandybridge PRM >> Volume
3179     * 2 Part 1: 3D/Media - 3D Pipeline >> 3DSTATE_DEPTH_BUFFER >> Surface
3180     * Format (p321).
3181     */
3182 
3183    bool has_stencil = surf->usage & ISL_SURF_USAGE_STENCIL_BIT;
3184 
3185    assert(surf->usage & ISL_SURF_USAGE_DEPTH_BIT);
3186 
3187    if (has_stencil)
3188       assert(ISL_GFX_VER(dev) < 7);
3189 
3190    switch (surf->format) {
3191    default:
3192       unreachable("bad isl depth format");
3193    case ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS:
3194       assert(ISL_GFX_VER(dev) < 7);
3195       return 0; /* D32_FLOAT_S8X24_UINT */
3196    case ISL_FORMAT_R32_FLOAT:
3197       assert(!has_stencil);
3198       return 1; /* D32_FLOAT */
3199    case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
3200       if (has_stencil) {
3201          assert(ISL_GFX_VER(dev) < 7);
3202          return 2; /* D24_UNORM_S8_UINT */
3203       } else {
3204          assert(ISL_GFX_VER(dev) >= 5);
3205          return 3; /* D24_UNORM_X8_UINT */
3206       }
3207    case ISL_FORMAT_R16_UNORM:
3208       assert(!has_stencil);
3209       return 5; /* D16_UNORM */
3210    }
3211 }
3212 
3213 bool
isl_swizzle_supports_rendering(const struct intel_device_info * devinfo,struct isl_swizzle swizzle)3214 isl_swizzle_supports_rendering(const struct intel_device_info *devinfo,
3215                                struct isl_swizzle swizzle)
3216 {
3217    if (devinfo->platform == INTEL_PLATFORM_HSW) {
3218       /* From the Haswell PRM,
3219        * RENDER_SURFACE_STATE::Shader Channel Select Red
3220        *
3221        *    "The Shader channel selects also define which shader channels are
3222        *    written to which surface channel. If the Shader channel select is
3223        *    SCS_ZERO or SCS_ONE then it is not written to the surface. If the
3224        *    shader channel select is SCS_RED it is written to the surface red
3225        *    channel and so on. If more than one shader channel select is set
3226        *    to the same surface channel only the first shader channel in RGBA
3227        *    order will be written."
3228        */
3229       return true;
3230    } else if (devinfo->ver <= 7) {
3231       /* Ivy Bridge and early doesn't have any swizzling */
3232       return isl_swizzle_is_identity(swizzle);
3233    } else {
3234       /* From the Sky Lake PRM Vol. 2d,
3235        * RENDER_SURFACE_STATE::Shader Channel Select Red
3236        *
3237        *    "For Render Target, Red, Green and Blue Shader Channel Selects
3238        *    MUST be such that only valid components can be swapped i.e. only
3239        *    change the order of components in the pixel. Any other values for
3240        *    these Shader Channel Select fields are not valid for Render
3241        *    Targets. This also means that there MUST not be multiple shader
3242        *    channels mapped to the same RT channel."
3243        *
3244        * From the Sky Lake PRM Vol. 2d,
3245        * RENDER_SURFACE_STATE::Shader Channel Select Alpha
3246        *
3247        *    "For Render Target, this field MUST be programmed to
3248        *    value = SCS_ALPHA."
3249        */
3250       return (swizzle.r == ISL_CHANNEL_SELECT_RED ||
3251               swizzle.r == ISL_CHANNEL_SELECT_GREEN ||
3252               swizzle.r == ISL_CHANNEL_SELECT_BLUE) &&
3253              (swizzle.g == ISL_CHANNEL_SELECT_RED ||
3254               swizzle.g == ISL_CHANNEL_SELECT_GREEN ||
3255               swizzle.g == ISL_CHANNEL_SELECT_BLUE) &&
3256              (swizzle.b == ISL_CHANNEL_SELECT_RED ||
3257               swizzle.b == ISL_CHANNEL_SELECT_GREEN ||
3258               swizzle.b == ISL_CHANNEL_SELECT_BLUE) &&
3259              swizzle.r != swizzle.g &&
3260              swizzle.r != swizzle.b &&
3261              swizzle.g != swizzle.b &&
3262              swizzle.a == ISL_CHANNEL_SELECT_ALPHA;
3263    }
3264 }
3265 
3266 static enum isl_channel_select
swizzle_select(enum isl_channel_select chan,struct isl_swizzle swizzle)3267 swizzle_select(enum isl_channel_select chan, struct isl_swizzle swizzle)
3268 {
3269    switch (chan) {
3270    case ISL_CHANNEL_SELECT_ZERO:
3271    case ISL_CHANNEL_SELECT_ONE:
3272       return chan;
3273    case ISL_CHANNEL_SELECT_RED:
3274       return swizzle.r;
3275    case ISL_CHANNEL_SELECT_GREEN:
3276       return swizzle.g;
3277    case ISL_CHANNEL_SELECT_BLUE:
3278       return swizzle.b;
3279    case ISL_CHANNEL_SELECT_ALPHA:
3280       return swizzle.a;
3281    default:
3282       unreachable("Invalid swizzle component");
3283    }
3284 }
3285 
3286 /**
3287  * Returns the single swizzle that is equivalent to applying the two given
3288  * swizzles in sequence.
3289  */
3290 struct isl_swizzle
isl_swizzle_compose(struct isl_swizzle first,struct isl_swizzle second)3291 isl_swizzle_compose(struct isl_swizzle first, struct isl_swizzle second)
3292 {
3293    return (struct isl_swizzle) {
3294       .r = swizzle_select(first.r, second),
3295       .g = swizzle_select(first.g, second),
3296       .b = swizzle_select(first.b, second),
3297       .a = swizzle_select(first.a, second),
3298    };
3299 }
3300 
3301 /**
3302  * Returns a swizzle that is the pseudo-inverse of this swizzle.
3303  */
3304 struct isl_swizzle
isl_swizzle_invert(struct isl_swizzle swizzle)3305 isl_swizzle_invert(struct isl_swizzle swizzle)
3306 {
3307    /* Default to zero for channels which do not show up in the swizzle */
3308    enum isl_channel_select chans[4] = {
3309       ISL_CHANNEL_SELECT_ZERO,
3310       ISL_CHANNEL_SELECT_ZERO,
3311       ISL_CHANNEL_SELECT_ZERO,
3312       ISL_CHANNEL_SELECT_ZERO,
3313    };
3314 
3315    /* We go in ABGR order so that, if there are any duplicates, the first one
3316     * is taken if you look at it in RGBA order.  This is what Haswell hardware
3317     * does for render target swizzles.
3318     */
3319    if ((unsigned)(swizzle.a - ISL_CHANNEL_SELECT_RED) < 4)
3320       chans[swizzle.a - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_ALPHA;
3321    if ((unsigned)(swizzle.b - ISL_CHANNEL_SELECT_RED) < 4)
3322       chans[swizzle.b - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_BLUE;
3323    if ((unsigned)(swizzle.g - ISL_CHANNEL_SELECT_RED) < 4)
3324       chans[swizzle.g - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_GREEN;
3325    if ((unsigned)(swizzle.r - ISL_CHANNEL_SELECT_RED) < 4)
3326       chans[swizzle.r - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_RED;
3327 
3328    return (struct isl_swizzle) { chans[0], chans[1], chans[2], chans[3] };
3329 }
3330 
3331 /** Applies an inverse swizzle to a color value */
3332 union isl_color_value
isl_color_value_swizzle_inv(union isl_color_value src,struct isl_swizzle swizzle)3333 isl_color_value_swizzle_inv(union isl_color_value src,
3334                             struct isl_swizzle swizzle)
3335 {
3336    union isl_color_value dst = { .u32 = { 0, } };
3337 
3338    /* We assign colors in ABGR order so that the first one will be taken in
3339     * RGBA precedence order.  According to the PRM docs for shader channel
3340     * select, this matches Haswell hardware behavior.
3341     */
3342    if ((unsigned)(swizzle.a - ISL_CHANNEL_SELECT_RED) < 4)
3343       dst.u32[swizzle.a - ISL_CHANNEL_SELECT_RED] = src.u32[3];
3344    if ((unsigned)(swizzle.b - ISL_CHANNEL_SELECT_RED) < 4)
3345       dst.u32[swizzle.b - ISL_CHANNEL_SELECT_RED] = src.u32[2];
3346    if ((unsigned)(swizzle.g - ISL_CHANNEL_SELECT_RED) < 4)
3347       dst.u32[swizzle.g - ISL_CHANNEL_SELECT_RED] = src.u32[1];
3348    if ((unsigned)(swizzle.r - ISL_CHANNEL_SELECT_RED) < 4)
3349       dst.u32[swizzle.r - ISL_CHANNEL_SELECT_RED] = src.u32[0];
3350 
3351    return dst;
3352 }
3353 
3354 uint8_t
isl_format_get_aux_map_encoding(enum isl_format format)3355 isl_format_get_aux_map_encoding(enum isl_format format)
3356 {
3357    switch(format) {
3358    case ISL_FORMAT_R32G32B32A32_FLOAT: return 0x11;
3359    case ISL_FORMAT_R32G32B32X32_FLOAT: return 0x11;
3360    case ISL_FORMAT_R32G32B32A32_SINT: return 0x12;
3361    case ISL_FORMAT_R32G32B32A32_UINT: return 0x13;
3362    case ISL_FORMAT_R16G16B16A16_UNORM: return 0x14;
3363    case ISL_FORMAT_R16G16B16A16_SNORM: return 0x15;
3364    case ISL_FORMAT_R16G16B16A16_SINT: return 0x16;
3365    case ISL_FORMAT_R16G16B16A16_UINT: return 0x17;
3366    case ISL_FORMAT_R16G16B16A16_FLOAT: return 0x10;
3367    case ISL_FORMAT_R16G16B16X16_FLOAT: return 0x10;
3368    case ISL_FORMAT_R32G32_FLOAT: return 0x11;
3369    case ISL_FORMAT_R32G32_SINT: return 0x12;
3370    case ISL_FORMAT_R32G32_UINT: return 0x13;
3371    case ISL_FORMAT_B8G8R8A8_UNORM: return 0xA;
3372    case ISL_FORMAT_B8G8R8X8_UNORM: return 0xA;
3373    case ISL_FORMAT_B8G8R8A8_UNORM_SRGB: return 0xA;
3374    case ISL_FORMAT_B8G8R8X8_UNORM_SRGB: return 0xA;
3375    case ISL_FORMAT_R10G10B10A2_UNORM: return 0x18;
3376    case ISL_FORMAT_R10G10B10A2_UNORM_SRGB: return 0x18;
3377    case ISL_FORMAT_R10G10B10_FLOAT_A2_UNORM: return 0x19;
3378    case ISL_FORMAT_R10G10B10A2_UINT: return 0x1A;
3379    case ISL_FORMAT_R8G8B8A8_UNORM: return 0xA;
3380    case ISL_FORMAT_R8G8B8A8_UNORM_SRGB: return 0xA;
3381    case ISL_FORMAT_R8G8B8A8_SNORM: return 0x1B;
3382    case ISL_FORMAT_R8G8B8A8_SINT: return 0x1C;
3383    case ISL_FORMAT_R8G8B8A8_UINT: return 0x1D;
3384    case ISL_FORMAT_R16G16_UNORM: return 0x14;
3385    case ISL_FORMAT_R16G16_SNORM: return 0x15;
3386    case ISL_FORMAT_R16G16_SINT: return 0x16;
3387    case ISL_FORMAT_R16G16_UINT: return 0x17;
3388    case ISL_FORMAT_R16G16_FLOAT: return 0x10;
3389    case ISL_FORMAT_B10G10R10A2_UNORM: return 0x18;
3390    case ISL_FORMAT_B10G10R10A2_UNORM_SRGB: return 0x18;
3391    case ISL_FORMAT_R11G11B10_FLOAT: return 0x1E;
3392    case ISL_FORMAT_R32_SINT: return 0x12;
3393    case ISL_FORMAT_R32_UINT: return 0x13;
3394    case ISL_FORMAT_R32_FLOAT: return 0x11;
3395    case ISL_FORMAT_R24_UNORM_X8_TYPELESS: return 0x13;
3396    case ISL_FORMAT_B5G6R5_UNORM: return 0xA;
3397    case ISL_FORMAT_B5G6R5_UNORM_SRGB: return 0xA;
3398    case ISL_FORMAT_B5G5R5A1_UNORM: return 0xA;
3399    case ISL_FORMAT_B5G5R5A1_UNORM_SRGB: return 0xA;
3400    case ISL_FORMAT_B4G4R4A4_UNORM: return 0xA;
3401    case ISL_FORMAT_B4G4R4A4_UNORM_SRGB: return 0xA;
3402    case ISL_FORMAT_R8G8_UNORM: return 0xA;
3403    case ISL_FORMAT_R8G8_SNORM: return 0x1B;
3404    case ISL_FORMAT_R8G8_SINT: return 0x1C;
3405    case ISL_FORMAT_R8G8_UINT: return 0x1D;
3406    case ISL_FORMAT_R16_UNORM: return 0x14;
3407    case ISL_FORMAT_R16_SNORM: return 0x15;
3408    case ISL_FORMAT_R16_SINT: return 0x16;
3409    case ISL_FORMAT_R16_UINT: return 0x17;
3410    case ISL_FORMAT_R16_FLOAT: return 0x10;
3411    case ISL_FORMAT_B5G5R5X1_UNORM: return 0xA;
3412    case ISL_FORMAT_B5G5R5X1_UNORM_SRGB: return 0xA;
3413    case ISL_FORMAT_A1B5G5R5_UNORM: return 0xA;
3414    case ISL_FORMAT_A4B4G4R4_UNORM: return 0xA;
3415    case ISL_FORMAT_R8_UNORM: return 0xA;
3416    case ISL_FORMAT_R8_SNORM: return 0x1B;
3417    case ISL_FORMAT_R8_SINT: return 0x1C;
3418    case ISL_FORMAT_R8_UINT: return 0x1D;
3419    case ISL_FORMAT_A8_UNORM: return 0xA;
3420    case ISL_FORMAT_PLANAR_420_8: return 0xF;
3421    case ISL_FORMAT_PLANAR_420_10: return 0x7;
3422    case ISL_FORMAT_PLANAR_420_12: return 0x8;
3423    case ISL_FORMAT_PLANAR_420_16: return 0x8;
3424    case ISL_FORMAT_YCRCB_NORMAL: return 0x3;
3425    case ISL_FORMAT_YCRCB_SWAPY: return 0xB;
3426    default:
3427       unreachable("Unsupported aux-map format!");
3428       return 0;
3429    }
3430 }
3431 
3432 /*
3433  * Returns compression format encoding for Unified Lossless Compression
3434  */
3435 uint8_t
isl_get_render_compression_format(enum isl_format format)3436 isl_get_render_compression_format(enum isl_format format)
3437 {
3438    /* From the Bspec, Enumeration_RenderCompressionFormat section (53726): */
3439    switch(format) {
3440    case ISL_FORMAT_R32G32B32A32_FLOAT:
3441    case ISL_FORMAT_R32G32B32X32_FLOAT:
3442    case ISL_FORMAT_R32G32B32A32_SINT:
3443       return 0x0;
3444    case ISL_FORMAT_R32G32B32A32_UINT:
3445       return 0x1;
3446    case ISL_FORMAT_R32G32_FLOAT:
3447    case ISL_FORMAT_R32G32_SINT:
3448       return 0x2;
3449    case ISL_FORMAT_R32G32_UINT:
3450       return 0x3;
3451    case ISL_FORMAT_R16G16B16A16_UNORM:
3452    case ISL_FORMAT_R16G16B16X16_UNORM:
3453    case ISL_FORMAT_R16G16B16A16_UINT:
3454       return 0x4;
3455    case ISL_FORMAT_R16G16B16A16_SNORM:
3456    case ISL_FORMAT_R16G16B16A16_SINT:
3457    case ISL_FORMAT_R16G16B16A16_FLOAT:
3458    case ISL_FORMAT_R16G16B16X16_FLOAT:
3459       return 0x5;
3460    case ISL_FORMAT_R16G16_UNORM:
3461    case ISL_FORMAT_R16G16_UINT:
3462       return 0x6;
3463    case ISL_FORMAT_R16G16_SNORM:
3464    case ISL_FORMAT_R16G16_SINT:
3465    case ISL_FORMAT_R16G16_FLOAT:
3466       return 0x7;
3467    case ISL_FORMAT_B8G8R8A8_UNORM:
3468    case ISL_FORMAT_B8G8R8X8_UNORM:
3469    case ISL_FORMAT_B8G8R8A8_UNORM_SRGB:
3470    case ISL_FORMAT_B8G8R8X8_UNORM_SRGB:
3471    case ISL_FORMAT_R8G8B8A8_UNORM:
3472    case ISL_FORMAT_R8G8B8X8_UNORM:
3473    case ISL_FORMAT_R8G8B8A8_UNORM_SRGB:
3474    case ISL_FORMAT_R8G8B8X8_UNORM_SRGB:
3475    case ISL_FORMAT_R8G8B8A8_UINT:
3476       return 0x8;
3477    case ISL_FORMAT_R8G8B8A8_SNORM:
3478    case ISL_FORMAT_R8G8B8A8_SINT:
3479       return 0x9;
3480    case ISL_FORMAT_B5G6R5_UNORM:
3481    case ISL_FORMAT_B5G6R5_UNORM_SRGB:
3482    case ISL_FORMAT_B5G5R5A1_UNORM:
3483    case ISL_FORMAT_B5G5R5A1_UNORM_SRGB:
3484    case ISL_FORMAT_B4G4R4A4_UNORM:
3485    case ISL_FORMAT_B4G4R4A4_UNORM_SRGB:
3486    case ISL_FORMAT_B5G5R5X1_UNORM:
3487    case ISL_FORMAT_B5G5R5X1_UNORM_SRGB:
3488    case ISL_FORMAT_A1B5G5R5_UNORM:
3489    case ISL_FORMAT_A4B4G4R4_UNORM:
3490    case ISL_FORMAT_R8G8_UNORM:
3491    case ISL_FORMAT_R8G8_UINT:
3492       return 0xA;
3493    case ISL_FORMAT_R8G8_SNORM:
3494    case ISL_FORMAT_R8G8_SINT:
3495       return 0xB;
3496    case ISL_FORMAT_R10G10B10A2_UNORM:
3497    case ISL_FORMAT_R10G10B10A2_UNORM_SRGB:
3498    case ISL_FORMAT_R10G10B10_FLOAT_A2_UNORM:
3499    case ISL_FORMAT_R10G10B10A2_UINT:
3500    case ISL_FORMAT_B10G10R10A2_UNORM:
3501    case ISL_FORMAT_B10G10R10X2_UNORM:
3502    case ISL_FORMAT_B10G10R10A2_UNORM_SRGB:
3503       return 0xC;
3504    case ISL_FORMAT_R11G11B10_FLOAT:
3505       return 0xD;
3506    case ISL_FORMAT_R32_SINT:
3507    case ISL_FORMAT_R32_FLOAT:
3508       return 0x10;
3509    case ISL_FORMAT_R32_UINT:
3510    case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
3511       return 0x11;
3512    case ISL_FORMAT_R16_UNORM:
3513    case ISL_FORMAT_R16_UINT:
3514       return 0x14;
3515    case ISL_FORMAT_R16_SNORM:
3516    case ISL_FORMAT_R16_SINT:
3517    case ISL_FORMAT_R16_FLOAT:
3518       return 0x15;
3519    case ISL_FORMAT_R8_UNORM:
3520    case ISL_FORMAT_R8_UINT:
3521    case ISL_FORMAT_A8_UNORM:
3522       return 0x18;
3523    case ISL_FORMAT_R8_SNORM:
3524    case ISL_FORMAT_R8_SINT:
3525       return 0x19;
3526    default:
3527       unreachable("Unsupported render compression format!");
3528       return 0;
3529    }
3530 }
3531 
3532 const char *
isl_aux_op_to_name(enum isl_aux_op op)3533 isl_aux_op_to_name(enum isl_aux_op op)
3534 {
3535    static const char *names[] = {
3536       [ISL_AUX_OP_NONE]            = "none",
3537       [ISL_AUX_OP_FAST_CLEAR]      = "fast-clear",
3538       [ISL_AUX_OP_FULL_RESOLVE]    = "full-resolve",
3539       [ISL_AUX_OP_PARTIAL_RESOLVE] = "partial-resolve",
3540       [ISL_AUX_OP_AMBIGUATE]       = "ambiguate",
3541    };
3542    assert(op < ARRAY_SIZE(names));
3543    return names[op];
3544 }
3545