~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/intel/isl/isl.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
40
 
isl_memcpy_linear_to_tiled(uint32_t xt1, uint32_t xt2,
41
 
                           uint32_t yt1, uint32_t yt2,
42
 
                           char *dst, const char *src,
43
 
                           uint32_t dst_pitch, int32_t src_pitch,
44
 
                           bool has_swizzling,
45
 
                           enum isl_tiling tiling,
46
 
                           isl_memcpy_type copy_type)
47
 
{
48
 
#ifdef USE_SSE41
49
 
   if (copy_type == ISL_MEMCPY_STREAMING_LOAD) {
50
 
      _isl_memcpy_linear_to_tiled_sse41(
51
 
         xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
52
 
         tiling, copy_type);
53
 
      return;
54
 
   }
55
 
#endif
56
 
 
57
 
   _isl_memcpy_linear_to_tiled(
58
 
      xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
59
 
      tiling, copy_type);
60
 
}
61
 
 
62
 
void
63
 
isl_memcpy_tiled_to_linear(uint32_t xt1, uint32_t xt2,
64
 
                           uint32_t yt1, uint32_t yt2,
65
 
                           char *dst, const char *src,
66
 
                           int32_t dst_pitch, uint32_t src_pitch,
67
 
                           bool has_swizzling,
68
 
                           enum isl_tiling tiling,
69
 
                           isl_memcpy_type copy_type)
70
 
{
71
 
#ifdef USE_SSE41
72
 
   if (copy_type == ISL_MEMCPY_STREAMING_LOAD) {
73
 
      _isl_memcpy_tiled_to_linear_sse41(
74
 
         xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
75
 
         tiling, copy_type);
76
 
      return;
77
 
   }
78
 
#endif
79
 
 
80
 
   _isl_memcpy_tiled_to_linear(
81
 
      xt1, xt2, yt1, yt2, dst, src, dst_pitch, src_pitch, has_swizzling,
82
 
      tiling, copy_type);
83
 
}
84
 
 
85
 
void PRINTFLIKE(3, 4) UNUSED
86
 
__isl_finishme(const char *file, int line, const char *fmt, ...)
87
 
{
88
 
   va_list ap;
89
 
   char buf[512];
90
 
 
91
 
   va_start(ap, fmt);
92
 
   vsnprintf(buf, sizeof(buf), fmt, ap);
93
 
   va_end(ap);
94
 
 
95
 
   fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buf);
96
 
}
97
 
 
98
 
static void
99
 
isl_device_setup_mocs(struct isl_device *dev)
100
 
{
101
 
   if (dev->info->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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
 
   /* We are seeing failures with mcs on dg2, so disable it for now. */
2101
 
   if (intel_device_info_is_dg2(dev->info))
2102
 
      return false;
2103
 
 
2104
 
   /* The following are true of all multisampled surfaces */
2105
 
   assert(surf->samples > 1);
2106
 
   assert(surf->dim == ISL_SURF_DIM_2D);
2107
 
   assert(surf->levels == 1);
2108
 
   assert(surf->logical_level0_px.depth == 1);
2109
 
   assert(isl_format_supports_multisampling(dev->info, surf->format));
2110
 
 
2111
 
   enum isl_format mcs_format;
2112
 
   switch (surf->samples) {
2113
 
   case 2:  mcs_format = ISL_FORMAT_MCS_2X;  break;
2114
 
   case 4:  mcs_format = ISL_FORMAT_MCS_4X;  break;
2115
 
   case 8:  mcs_format = ISL_FORMAT_MCS_8X;  break;
2116
 
   case 16: mcs_format = ISL_FORMAT_MCS_16X; break;
2117
 
   default:
2118
 
      unreachable("Invalid sample count");
2119
 
   }
2120
 
 
2121
 
   return isl_surf_init(dev, mcs_surf,
2122
 
                        .dim = ISL_SURF_DIM_2D,
2123
 
                        .format = mcs_format,
2124
 
                        .width = surf->logical_level0_px.width,
2125
 
                        .height = surf->logical_level0_px.height,
2126
 
                        .depth = 1,
2127
 
                        .levels = 1,
2128
 
                        .array_len = surf->logical_level0_px.array_len,
2129
 
                        .samples = 1, /* MCS surfaces are really single-sampled */
2130
 
                        .usage = ISL_SURF_USAGE_MCS_BIT,
2131
 
                        .tiling_flags = ISL_TILING_ANY_MASK);
2132
 
}
2133
 
 
2134
 
bool
2135
 
isl_surf_supports_ccs(const struct isl_device *dev,
2136
 
                      const struct isl_surf *surf,
2137
 
                      const struct isl_surf *hiz_or_mcs_surf)
2138
 
{
2139
 
   if (surf->usage & ISL_SURF_USAGE_DISABLE_AUX_BIT)
2140
 
      return false;
2141
 
 
2142
 
   if (!isl_format_supports_ccs_d(dev->info, surf->format) &&
2143
 
       !isl_format_supports_ccs_e(dev->info, surf->format))
2144
 
      return false;
2145
 
 
2146
 
   /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
2147
 
    * Target(s)", beneath the "Fast Color Clear" bullet (p326):
2148
 
    *
2149
 
    *     - Support is limited to tiled render targets.
2150
 
    *
2151
 
    * From the Skylake documentation, it is made clear that X-tiling is no
2152
 
    * longer supported:
2153
 
    *
2154
 
    *     - MCS and Lossless compression is supported for
2155
 
    *       TiledY/TileYs/TileYf non-MSRTs only.
2156
 
    *
2157
 
    * From the BSpec (44930) for Gfx12:
2158
 
    *
2159
 
    *    Linear CCS is only allowed for Untyped Buffers but only via HDC
2160
 
    *    Data-Port messages.
2161
 
    *
2162
 
    * We never use untyped messages on surfaces created by ISL on Gfx9+ so
2163
 
    * this means linear is out on Gfx12+ as well.
2164
 
    */
2165
 
   if (surf->tiling == ISL_TILING_LINEAR)
2166
 
      return false;
2167
 
 
2168
 
   /* TODO: Disable for now, as we're not sure about the meaning of
2169
 
    * 3DSTATE_CPSIZE_CONTROL_BUFFER::CPCBCompressionEnable
2170
 
    */
2171
 
   if (isl_surf_usage_is_cpb(surf->usage))
2172
 
      return false;
2173
 
 
2174
 
   if (ISL_GFX_VER(dev) >= 12) {
2175
 
      if (isl_surf_usage_is_stencil(surf->usage)) {
2176
 
         /* HiZ and MCS aren't allowed with stencil */
2177
 
         assert(hiz_or_mcs_surf == NULL || hiz_or_mcs_surf->size_B == 0);
2178
 
 
2179
 
         /* Multi-sampled stencil cannot have CCS */
2180
 
         if (surf->samples > 1)
2181
 
            return false;
2182
 
      } else if (isl_surf_usage_is_depth(surf->usage)) {
2183
 
         const struct isl_surf *hiz_surf = hiz_or_mcs_surf;
2184
 
 
2185
 
         /* With depth surfaces, HIZ is required for CCS. */
2186
 
         if (hiz_surf == NULL || hiz_surf->size_B == 0)
2187
 
            return false;
2188
 
 
2189
 
         assert(hiz_surf->usage & ISL_SURF_USAGE_HIZ_BIT);
2190
 
         assert(hiz_surf->tiling == ISL_TILING_HIZ);
2191
 
         assert(isl_format_is_hiz(hiz_surf->format));
2192
 
      } else if (surf->samples > 1) {
2193
 
         const struct isl_surf *mcs_surf = hiz_or_mcs_surf;
2194
 
 
2195
 
         /* With multisampled color, CCS requires MCS */
2196
 
         if (mcs_surf == NULL || mcs_surf->size_B == 0)
2197
 
            return false;
2198
 
 
2199
 
         assert(mcs_surf->usage & ISL_SURF_USAGE_MCS_BIT);
2200
 
         assert(isl_format_is_mcs(mcs_surf->format));
2201
 
      } else {
2202
 
         /* Single-sampled color can't have MCS or HiZ */
2203
 
         assert(hiz_or_mcs_surf == NULL || hiz_or_mcs_surf->size_B == 0);
2204
 
      }
2205
 
 
2206
 
      /* On Gfx12, all CCS-compressed surface pitches must be multiples of
2207
 
       * 512B.
2208
 
       */
2209
 
      if (surf->row_pitch_B % 512 != 0)
2210
 
         return false;
2211
 
 
2212
 
      /* According to Wa_1406738321, 3D textures need a blit to a new
2213
 
       * surface in order to perform a resolve. For now, just disable CCS.
2214
 
       */
2215
 
      if (surf->dim == ISL_SURF_DIM_3D) {
2216
 
         isl_finishme("%s:%s: CCS for 3D textures is disabled, but a workaround"
2217
 
                      " is available.", __FILE__, __func__);
2218
 
         return false;
2219
 
      }
2220
 
 
2221
 
      /* Wa_1207137018
2222
 
       *
2223
 
       * TODO: implement following workaround currently covered by the
2224
 
       * restriction above. If following conditions are met:
2225
 
       *
2226
 
       *    - RENDER_SURFACE_STATE.Surface Type == 3D
2227
 
       *    - RENDER_SURFACE_STATE.Auxiliary Surface Mode != AUX_NONE
2228
 
       *    - RENDER_SURFACE_STATE.Tiled ResourceMode is TYF or TYS
2229
 
       *
2230
 
       * Set the value of RENDER_SURFACE_STATE.Mip Tail Start LOD to a mip
2231
 
       * that larger than those present in the surface (i.e. 15)
2232
 
       */
2233
 
 
2234
 
      /* TODO: Handle the other tiling formats */
2235
 
      if (surf->tiling != ISL_TILING_Y0 && surf->tiling != ISL_TILING_4 &&
2236
 
          surf->tiling != ISL_TILING_64)
2237
 
         return false;
2238
 
 
2239
 
      /* TODO: Handle single-sampled Tile64. */
2240
 
      if (surf->samples == 1 && surf->tiling == ISL_TILING_64)
2241
 
         return false;
2242
 
   } else {
2243
 
      /* ISL_GFX_VER(dev) < 12 */
2244
 
      if (surf->samples > 1)
2245
 
         return false;
2246
 
 
2247
 
      /* CCS is only for color images on Gfx7-11 */
2248
 
      if (isl_surf_usage_is_depth_or_stencil(surf->usage))
2249
 
         return false;
2250
 
 
2251
 
      /* We're single-sampled color so having HiZ or MCS makes no sense */
2252
 
      assert(hiz_or_mcs_surf == NULL || hiz_or_mcs_surf->size_B == 0);
2253
 
 
2254
 
      /* The PRM doesn't say this explicitly, but fast-clears don't appear to
2255
 
       * work for 3D textures until gfx9 where the layout of 3D textures
2256
 
       * changes to match 2D array textures.
2257
 
       */
2258
 
      if (ISL_GFX_VER(dev) <= 8 && surf->dim != ISL_SURF_DIM_2D)
2259
 
         return false;
2260
 
 
2261
 
      /* From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652 (Color Clear of
2262
 
       * Non-MultiSampler Render Target Restrictions):
2263
 
       *
2264
 
       *    "Support is for non-mip-mapped and non-array surface types only."
2265
 
       *
2266
 
       * This restriction is lifted on gfx8+.  Technically, it may be possible
2267
 
       * to create a CCS for an arrayed or mipmapped image and only enable
2268
 
       * CCS_D when rendering to the base slice.  However, there is no
2269
 
       * documentation tell us what the hardware would do in that case or what
2270
 
       * it does if you walk off the bases slice.  (Does it ignore CCS or does
2271
 
       * it start scribbling over random memory?)  We play it safe and just
2272
 
       * follow the docs and don't allow CCS_D for arrayed or mip-mapped
2273
 
       * surfaces.
2274
 
       */
2275
 
      if (ISL_GFX_VER(dev) <= 7 &&
2276
 
          (surf->levels > 1 || surf->logical_level0_px.array_len > 1))
2277
 
         return false;
2278
 
 
2279
 
      /* From the Skylake documentation, it is made clear that X-tiling is no
2280
 
       * longer supported:
2281
 
       *
2282
 
       *     - MCS and Lossless compression is supported for
2283
 
       *     TiledY/TileYs/TileYf non-MSRTs only.
2284
 
       */
2285
 
      if (ISL_GFX_VER(dev) >= 9 && !isl_tiling_is_any_y(surf->tiling))
2286
 
         return false;
2287
 
   }
2288
 
 
2289
 
   return true;
2290
 
}
2291
 
 
2292
 
bool
2293
 
isl_surf_get_ccs_surf(const struct isl_device *dev,
2294
 
                      const struct isl_surf *surf,
2295
 
                      const struct isl_surf *hiz_or_mcs_surf,
2296
 
                      struct isl_surf *ccs_surf,
2297
 
                      uint32_t row_pitch_B)
2298
 
{
2299
 
   if (!isl_surf_supports_ccs(dev, surf, hiz_or_mcs_surf))
2300
 
      return false;
2301
 
 
2302
 
   if (ISL_GFX_VER(dev) >= 12) {
2303
 
      enum isl_format ccs_format;
2304
 
      switch (isl_format_get_layout(surf->format)->bpb) {
2305
 
      case 8:     ccs_format = ISL_FORMAT_GFX12_CCS_8BPP_Y0;    break;
2306
 
      case 16:    ccs_format = ISL_FORMAT_GFX12_CCS_16BPP_Y0;   break;
2307
 
      case 32:    ccs_format = ISL_FORMAT_GFX12_CCS_32BPP_Y0;   break;
2308
 
      case 64:    ccs_format = ISL_FORMAT_GFX12_CCS_64BPP_Y0;   break;
2309
 
      case 128:   ccs_format = ISL_FORMAT_GFX12_CCS_128BPP_Y0;  break;
2310
 
      default:
2311
 
         return false;
2312
 
      }
2313
 
 
2314
 
      /* On Gfx12, the CCS is a scaled-down version of the main surface. We
2315
 
       * model this as the CCS compressing a 2D-view of the entire surface.
2316
 
       */
2317
 
      const bool ok =
2318
 
         isl_surf_init(dev, ccs_surf,
2319
 
                       .dim = ISL_SURF_DIM_2D,
2320
 
                       .format = ccs_format,
2321
 
                       .width = isl_surf_get_row_pitch_el(surf),
2322
 
                       .height = surf->size_B / surf->row_pitch_B,
2323
 
                       .depth = 1,
2324
 
                       .levels = 1,
2325
 
                       .array_len = 1,
2326
 
                       .samples = 1,
2327
 
                       .row_pitch_B = row_pitch_B,
2328
 
                       .usage = ISL_SURF_USAGE_CCS_BIT,
2329
 
                       .tiling_flags = ISL_TILING_GFX12_CCS_BIT);
2330
 
      assert(!ok || ccs_surf->size_B == surf->size_B / 256);
2331
 
      return ok;
2332
 
   } else {
2333
 
      enum isl_format ccs_format;
2334
 
      if (ISL_GFX_VER(dev) >= 9) {
2335
 
         switch (isl_format_get_layout(surf->format)->bpb) {
2336
 
         case 32:    ccs_format = ISL_FORMAT_GFX9_CCS_32BPP;   break;
2337
 
         case 64:    ccs_format = ISL_FORMAT_GFX9_CCS_64BPP;   break;
2338
 
         case 128:   ccs_format = ISL_FORMAT_GFX9_CCS_128BPP;  break;
2339
 
         default:    unreachable("Unsupported CCS format");
2340
 
            return false;
2341
 
         }
2342
 
      } else if (surf->tiling == ISL_TILING_Y0) {
2343
 
         switch (isl_format_get_layout(surf->format)->bpb) {
2344
 
         case 32:    ccs_format = ISL_FORMAT_GFX7_CCS_32BPP_Y;    break;
2345
 
         case 64:    ccs_format = ISL_FORMAT_GFX7_CCS_64BPP_Y;    break;
2346
 
         case 128:   ccs_format = ISL_FORMAT_GFX7_CCS_128BPP_Y;   break;
2347
 
         default:    unreachable("Unsupported CCS format");
2348
 
         }
2349
 
      } else if (surf->tiling == ISL_TILING_X) {
2350
 
         switch (isl_format_get_layout(surf->format)->bpb) {
2351
 
         case 32:    ccs_format = ISL_FORMAT_GFX7_CCS_32BPP_X;    break;
2352
 
         case 64:    ccs_format = ISL_FORMAT_GFX7_CCS_64BPP_X;    break;
2353
 
         case 128:   ccs_format = ISL_FORMAT_GFX7_CCS_128BPP_X;   break;
2354
 
         default:    unreachable("Unsupported CCS format");
2355
 
         }
2356
 
      } else {
2357
 
         unreachable("Invalid tiling format");
2358
 
      }
2359
 
 
2360
 
      return isl_surf_init(dev, ccs_surf,
2361
 
                           .dim = surf->dim,
2362
 
                           .format = ccs_format,
2363
 
                           .width = surf->logical_level0_px.width,
2364
 
                           .height = surf->logical_level0_px.height,
2365
 
                           .depth = surf->logical_level0_px.depth,
2366
 
                           .levels = surf->levels,
2367
 
                           .array_len = surf->logical_level0_px.array_len,
2368
 
                           .samples = 1,
2369
 
                           .row_pitch_B = row_pitch_B,
2370
 
                           .usage = ISL_SURF_USAGE_CCS_BIT,
2371
 
                           .tiling_flags = ISL_TILING_CCS_BIT);
2372
 
   }
2373
 
}
2374
 
 
2375
 
#define isl_genX_call(dev, func, ...)              \
2376
 
   switch (ISL_GFX_VERX10(dev)) {                  \
2377
 
   case 40:                                        \
2378
 
      isl_gfx4_##func(__VA_ARGS__);                \
2379
 
      break;                                       \
2380
 
   case 45:                                        \
2381
 
      /* G45 surface state is the same as gfx5 */  \
2382
 
   case 50:                                        \
2383
 
      isl_gfx5_##func(__VA_ARGS__);                \
2384
 
      break;                                       \
2385
 
   case 60:                                        \
2386
 
      isl_gfx6_##func(__VA_ARGS__);                \
2387
 
      break;                                       \
2388
 
   case 70:                                        \
2389
 
      isl_gfx7_##func(__VA_ARGS__);                \
2390
 
      break;                                       \
2391
 
   case 75:                                        \
2392
 
      isl_gfx75_##func(__VA_ARGS__);               \
2393
 
      break;                                       \
2394
 
   case 80:                                        \
2395
 
      isl_gfx8_##func(__VA_ARGS__);                \
2396
 
      break;                                       \
2397
 
   case 90:                                        \
2398
 
      isl_gfx9_##func(__VA_ARGS__);                \
2399
 
      break;                                       \
2400
 
   case 110:                                       \
2401
 
      isl_gfx11_##func(__VA_ARGS__);               \
2402
 
      break;                                       \
2403
 
   case 120:                                       \
2404
 
      isl_gfx12_##func(__VA_ARGS__);               \
2405
 
      break;                                       \
2406
 
   case 125:                                       \
2407
 
      isl_gfx125_##func(__VA_ARGS__);              \
2408
 
      break;                                       \
2409
 
   default:                                        \
2410
 
      assert(!"Unknown hardware generation");      \
2411
 
   }
2412
 
 
2413
 
void
2414
 
isl_surf_fill_state_s(const struct isl_device *dev, void *state,
2415
 
                      const struct isl_surf_fill_state_info *restrict info)
2416
 
{
2417
 
#ifndef NDEBUG
2418
 
   isl_surf_usage_flags_t _base_usage =
2419
 
      info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
2420
 
                           ISL_SURF_USAGE_TEXTURE_BIT |
2421
 
                           ISL_SURF_USAGE_STORAGE_BIT);
2422
 
   /* They may only specify one of the above bits at a time */
2423
 
   assert(__builtin_popcount(_base_usage) == 1);
2424
 
   /* The only other allowed bit is ISL_SURF_USAGE_CUBE_BIT */
2425
 
   assert((info->view->usage & ~ISL_SURF_USAGE_CUBE_BIT) == _base_usage);
2426
 
#endif
2427
 
 
2428
 
   if (info->surf->dim == ISL_SURF_DIM_3D) {
2429
 
      assert(info->view->base_array_layer + info->view->array_len <=
2430
 
             info->surf->logical_level0_px.depth);
2431
 
   } else {
2432
 
      assert(info->view->base_array_layer + info->view->array_len <=
2433
 
             info->surf->logical_level0_px.array_len);
2434
 
   }
2435
 
 
2436
 
   isl_genX_call(dev, surf_fill_state_s, dev, state, info);
2437
 
}
2438
 
 
2439
 
void
2440
 
isl_buffer_fill_state_s(const struct isl_device *dev, void *state,
2441
 
                        const struct isl_buffer_fill_state_info *restrict info)
2442
 
{
2443
 
   isl_genX_call(dev, buffer_fill_state_s, dev, state, info);
2444
 
}
2445
 
 
2446
 
void
2447
 
isl_null_fill_state_s(const struct isl_device *dev, void *state,
2448
 
                      const struct isl_null_fill_state_info *restrict info)
2449
 
{
2450
 
   isl_genX_call(dev, null_fill_state, dev, state, info);
2451
 
}
2452
 
 
2453
 
void
2454
 
isl_emit_depth_stencil_hiz_s(const struct isl_device *dev, void *batch,
2455
 
                             const struct isl_depth_stencil_hiz_emit_info *restrict info)
2456
 
{
2457
 
   if (info->depth_surf && info->stencil_surf) {
2458
 
      if (!dev->info->has_hiz_and_separate_stencil) {
2459
 
         assert(info->depth_surf == info->stencil_surf);
2460
 
         assert(info->depth_address == info->stencil_address);
2461
 
      }
2462
 
      assert(info->depth_surf->dim == info->stencil_surf->dim);
2463
 
   }
2464
 
 
2465
 
   if (info->depth_surf) {
2466
 
      assert((info->depth_surf->usage & ISL_SURF_USAGE_DEPTH_BIT));
2467
 
      if (info->depth_surf->dim == ISL_SURF_DIM_3D) {
2468
 
         assert(info->view->base_array_layer + info->view->array_len <=
2469
 
                info->depth_surf->logical_level0_px.depth);
2470
 
      } else {
2471
 
         assert(info->view->base_array_layer + info->view->array_len <=
2472
 
                info->depth_surf->logical_level0_px.array_len);
2473
 
      }
2474
 
   }
2475
 
 
2476
 
   if (info->stencil_surf) {
2477
 
      assert((info->stencil_surf->usage & ISL_SURF_USAGE_STENCIL_BIT));
2478
 
      if (info->stencil_surf->dim == ISL_SURF_DIM_3D) {
2479
 
         assert(info->view->base_array_layer + info->view->array_len <=
2480
 
                info->stencil_surf->logical_level0_px.depth);
2481
 
      } else {
2482
 
         assert(info->view->base_array_layer + info->view->array_len <=
2483
 
                info->stencil_surf->logical_level0_px.array_len);
2484
 
      }
2485
 
   }
2486
 
 
2487
 
   isl_genX_call(dev, emit_depth_stencil_hiz_s, dev, batch, info);
2488
 
}
2489
 
 
2490
 
void
2491
 
isl_emit_cpb_control_s(const struct isl_device *dev, void *batch,
2492
 
                       const struct isl_cpb_emit_info *restrict info)
2493
 
{
2494
 
   if (info->surf) {
2495
 
      assert((info->surf->usage & ISL_SURF_USAGE_CPB_BIT));
2496
 
      assert(info->surf->dim != ISL_SURF_DIM_3D);
2497
 
      assert(info->surf->tiling == ISL_TILING_4 ||
2498
 
             info->surf->tiling == ISL_TILING_64);
2499
 
      assert(info->surf->format == ISL_FORMAT_R8_UINT);
2500
 
   }
2501
 
 
2502
 
   isl_genX_call(dev, emit_cpb_control_s, dev, batch, info);
2503
 
}
2504
 
 
2505
 
/**
2506
 
 * A variant of isl_surf_get_image_offset_sa() specific to
2507
 
 * ISL_DIM_LAYOUT_GFX4_2D.
2508
 
 */
2509
 
static void
2510
 
get_image_offset_sa_gfx4_2d(const struct isl_surf *surf,
2511
 
                            uint32_t level, uint32_t logical_array_layer,
2512
 
                            uint32_t *x_offset_sa,
2513
 
                            uint32_t *y_offset_sa)
2514
 
{
2515
 
   assert(level < surf->levels);
2516
 
   if (surf->dim == ISL_SURF_DIM_3D)
2517
 
      assert(logical_array_layer < surf->logical_level0_px.depth);
2518
 
   else
2519
 
      assert(logical_array_layer < surf->logical_level0_px.array_len);
2520
 
 
2521
 
   const struct isl_extent3d image_align_sa =
2522
 
      isl_surf_get_image_alignment_sa(surf);
2523
 
 
2524
 
   const uint32_t W0 = surf->phys_level0_sa.width;
2525
 
   const uint32_t H0 = surf->phys_level0_sa.height;
2526
 
 
2527
 
   const uint32_t phys_layer = logical_array_layer *
2528
 
      (surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY ? surf->samples : 1);
2529
 
 
2530
 
   uint32_t x = 0;
2531
 
   uint32_t y = phys_layer * isl_surf_get_array_pitch_sa_rows(surf);
2532
 
 
2533
 
   for (uint32_t l = 0; l < level; ++l) {
2534
 
      if (l == 1) {
2535
 
         uint32_t W = isl_minify(W0, l);
2536
 
         x += isl_align_npot(W, image_align_sa.w);
2537
 
      } else {
2538
 
         uint32_t H = isl_minify(H0, l);
2539
 
         y += isl_align_npot(H, image_align_sa.h);
2540
 
      }
2541
 
   }
2542
 
 
2543
 
   *x_offset_sa = x;
2544
 
   *y_offset_sa = y;
2545
 
}
2546
 
 
2547
 
/**
2548
 
 * A variant of isl_surf_get_image_offset_sa() specific to
2549
 
 * ISL_DIM_LAYOUT_GFX4_3D.
2550
 
 */
2551
 
static void
2552
 
get_image_offset_sa_gfx4_3d(const struct isl_surf *surf,
2553
 
                            uint32_t level, uint32_t logical_z_offset_px,
2554
 
                            uint32_t *x_offset_sa,
2555
 
                            uint32_t *y_offset_sa)
2556
 
{
2557
 
   assert(level < surf->levels);
2558
 
   if (surf->dim == ISL_SURF_DIM_3D) {
2559
 
      assert(surf->phys_level0_sa.array_len == 1);
2560
 
      assert(logical_z_offset_px < isl_minify(surf->phys_level0_sa.depth, level));
2561
 
   } else {
2562
 
      assert(surf->dim == ISL_SURF_DIM_2D);
2563
 
      assert(surf->usage & ISL_SURF_USAGE_CUBE_BIT);
2564
 
      assert(surf->phys_level0_sa.array_len == 6);
2565
 
      assert(logical_z_offset_px < surf->phys_level0_sa.array_len);
2566
 
   }
2567
 
 
2568
 
   const struct isl_extent3d image_align_sa =
2569
 
      isl_surf_get_image_alignment_sa(surf);
2570
 
 
2571
 
   const uint32_t W0 = surf->phys_level0_sa.width;
2572
 
   const uint32_t H0 = surf->phys_level0_sa.height;
2573
 
   const uint32_t D0 = surf->phys_level0_sa.depth;
2574
 
   const uint32_t AL = surf->phys_level0_sa.array_len;
2575
 
 
2576
 
   uint32_t x = 0;
2577
 
   uint32_t y = 0;
2578
 
 
2579
 
   for (uint32_t l = 0; l < level; ++l) {
2580
 
      const uint32_t level_h = isl_align_npot(isl_minify(H0, l), image_align_sa.h);
2581
 
      const uint32_t level_d =
2582
 
         isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, l) : AL,
2583
 
                        image_align_sa.d);
2584
 
      const uint32_t max_layers_vert = isl_align(level_d, 1u << l) / (1u << l);
2585
 
 
2586
 
      y += level_h * max_layers_vert;
2587
 
   }
2588
 
 
2589
 
   const uint32_t level_w = isl_align_npot(isl_minify(W0, level), image_align_sa.w);
2590
 
   const uint32_t level_h = isl_align_npot(isl_minify(H0, level), image_align_sa.h);
2591
 
   const uint32_t level_d =
2592
 
      isl_align_npot(surf->dim == ISL_SURF_DIM_3D ? isl_minify(D0, level) : AL,
2593
 
                     image_align_sa.d);
2594
 
 
2595
 
   const uint32_t max_layers_horiz = MIN(level_d, 1u << level);
2596
 
 
2597
 
   x += level_w * (logical_z_offset_px % max_layers_horiz);
2598
 
   y += level_h * (logical_z_offset_px / max_layers_horiz);
2599
 
 
2600
 
   *x_offset_sa = x;
2601
 
   *y_offset_sa = y;
2602
 
}
2603
 
 
2604
 
static void
2605
 
get_image_offset_sa_gfx6_stencil_hiz(const struct isl_surf *surf,
2606
 
                                     uint32_t level,
2607
 
                                     uint32_t logical_array_layer,
2608
 
                                     uint32_t *x_offset_sa,
2609
 
                                     uint32_t *y_offset_sa)
2610
 
{
2611
 
   assert(level < surf->levels);
2612
 
   assert(surf->logical_level0_px.depth == 1);
2613
 
   assert(logical_array_layer < surf->logical_level0_px.array_len);
2614
 
 
2615
 
   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2616
 
 
2617
 
   const struct isl_extent3d image_align_sa =
2618
 
      isl_surf_get_image_alignment_sa(surf);
2619
 
 
2620
 
   struct isl_tile_info tile_info;
2621
 
   isl_surf_get_tile_info(surf, &tile_info);
2622
 
   const struct isl_extent2d tile_extent_sa = {
2623
 
      .w = tile_info.logical_extent_el.w * fmtl->bw,
2624
 
      .h = tile_info.logical_extent_el.h * fmtl->bh,
2625
 
   };
2626
 
   /* Tile size is a multiple of image alignment */
2627
 
   assert(tile_extent_sa.w % image_align_sa.w == 0);
2628
 
   assert(tile_extent_sa.h % image_align_sa.h == 0);
2629
 
 
2630
 
   const uint32_t W0 = surf->phys_level0_sa.w;
2631
 
   const uint32_t H0 = surf->phys_level0_sa.h;
2632
 
 
2633
 
   /* Each image has the same height as LOD0 because the hardware thinks
2634
 
    * everything is LOD0
2635
 
    */
2636
 
   const uint32_t H = isl_align(H0, image_align_sa.h);
2637
 
 
2638
 
   /* Quick sanity check for consistency */
2639
 
   if (surf->phys_level0_sa.array_len > 1)
2640
 
      assert(surf->array_pitch_el_rows == isl_assert_div(H, fmtl->bh));
2641
 
 
2642
 
   uint32_t x = 0, y = 0;
2643
 
   for (uint32_t l = 0; l < level; ++l) {
2644
 
      const uint32_t W = isl_minify(W0, l);
2645
 
 
2646
 
      const uint32_t w = isl_align(W, tile_extent_sa.w);
2647
 
      const uint32_t h = isl_align(H * surf->phys_level0_sa.a,
2648
 
                                   tile_extent_sa.h);
2649
 
 
2650
 
      if (l == 0) {
2651
 
         y += h;
2652
 
      } else {
2653
 
         x += w;
2654
 
      }
2655
 
   }
2656
 
 
2657
 
   y += H * logical_array_layer;
2658
 
 
2659
 
   *x_offset_sa = x;
2660
 
   *y_offset_sa = y;
2661
 
}
2662
 
 
2663
 
/**
2664
 
 * A variant of isl_surf_get_image_offset_sa() specific to
2665
 
 * ISL_DIM_LAYOUT_GFX9_1D.
2666
 
 */
2667
 
static void
2668
 
get_image_offset_sa_gfx9_1d(const struct isl_surf *surf,
2669
 
                            uint32_t level, uint32_t layer,
2670
 
                            uint32_t *x_offset_sa,
2671
 
                            uint32_t *y_offset_sa)
2672
 
{
2673
 
   assert(level < surf->levels);
2674
 
   assert(layer < surf->phys_level0_sa.array_len);
2675
 
   assert(surf->phys_level0_sa.height == 1);
2676
 
   assert(surf->phys_level0_sa.depth == 1);
2677
 
   assert(surf->samples == 1);
2678
 
 
2679
 
   const uint32_t W0 = surf->phys_level0_sa.width;
2680
 
   const struct isl_extent3d image_align_sa =
2681
 
      isl_surf_get_image_alignment_sa(surf);
2682
 
 
2683
 
   uint32_t x = 0;
2684
 
 
2685
 
   for (uint32_t l = 0; l < level; ++l) {
2686
 
      uint32_t W = isl_minify(W0, l);
2687
 
      uint32_t w = isl_align_npot(W, image_align_sa.w);
2688
 
 
2689
 
      x += w;
2690
 
   }
2691
 
 
2692
 
   *x_offset_sa = x;
2693
 
   *y_offset_sa = layer * isl_surf_get_array_pitch_sa_rows(surf);
2694
 
}
2695
 
 
2696
 
/**
2697
 
 * Calculate the offset, in units of surface samples, to a subimage in the
2698
 
 * surface.
2699
 
 *
2700
 
 * @invariant level < surface levels
2701
 
 * @invariant logical_array_layer < logical array length of surface
2702
 
 * @invariant logical_z_offset_px < logical depth of surface at level
2703
 
 */
2704
 
void
2705
 
isl_surf_get_image_offset_sa(const struct isl_surf *surf,
2706
 
                             uint32_t level,
2707
 
                             uint32_t logical_array_layer,
2708
 
                             uint32_t logical_z_offset_px,
2709
 
                             uint32_t *x_offset_sa,
2710
 
                             uint32_t *y_offset_sa,
2711
 
                             uint32_t *z_offset_sa,
2712
 
                             uint32_t *array_offset)
2713
 
{
2714
 
   assert(level < surf->levels);
2715
 
   assert(logical_array_layer < surf->logical_level0_px.array_len);
2716
 
   assert(logical_z_offset_px
2717
 
          < isl_minify(surf->logical_level0_px.depth, level));
2718
 
 
2719
 
   switch (surf->dim_layout) {
2720
 
   case ISL_DIM_LAYOUT_GFX9_1D:
2721
 
      get_image_offset_sa_gfx9_1d(surf, level, logical_array_layer,
2722
 
                                  x_offset_sa, y_offset_sa);
2723
 
      *z_offset_sa = 0;
2724
 
      *array_offset = 0;
2725
 
      break;
2726
 
   case ISL_DIM_LAYOUT_GFX4_2D:
2727
 
      get_image_offset_sa_gfx4_2d(surf, level, logical_array_layer
2728
 
                                  + logical_z_offset_px,
2729
 
                                  x_offset_sa, y_offset_sa);
2730
 
      *z_offset_sa = 0;
2731
 
      *array_offset = 0;
2732
 
      break;
2733
 
   case ISL_DIM_LAYOUT_GFX4_3D:
2734
 
      get_image_offset_sa_gfx4_3d(surf, level, logical_array_layer +
2735
 
                                  logical_z_offset_px,
2736
 
                                  x_offset_sa, y_offset_sa);
2737
 
      *z_offset_sa = 0;
2738
 
      *array_offset = 0;
2739
 
      break;
2740
 
   case ISL_DIM_LAYOUT_GFX6_STENCIL_HIZ:
2741
 
      get_image_offset_sa_gfx6_stencil_hiz(surf, level, logical_array_layer +
2742
 
                                           logical_z_offset_px,
2743
 
                                           x_offset_sa, y_offset_sa);
2744
 
      *z_offset_sa = 0;
2745
 
      *array_offset = 0;
2746
 
      break;
2747
 
 
2748
 
   default:
2749
 
      unreachable("not reached");
2750
 
   }
2751
 
}
2752
 
 
2753
 
void
2754
 
isl_surf_get_image_offset_el(const struct isl_surf *surf,
2755
 
                             uint32_t level,
2756
 
                             uint32_t logical_array_layer,
2757
 
                             uint32_t logical_z_offset_px,
2758
 
                             uint32_t *x_offset_el,
2759
 
                             uint32_t *y_offset_el,
2760
 
                             uint32_t *z_offset_el,
2761
 
                             uint32_t *array_offset)
2762
 
{
2763
 
   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2764
 
 
2765
 
   assert(level < surf->levels);
2766
 
   assert(logical_array_layer < surf->logical_level0_px.array_len);
2767
 
   assert(logical_z_offset_px
2768
 
          < isl_minify(surf->logical_level0_px.depth, level));
2769
 
 
2770
 
   uint32_t x_offset_sa, y_offset_sa, z_offset_sa;
2771
 
   isl_surf_get_image_offset_sa(surf, level,
2772
 
                                logical_array_layer,
2773
 
                                logical_z_offset_px,
2774
 
                                &x_offset_sa,
2775
 
                                &y_offset_sa,
2776
 
                                &z_offset_sa,
2777
 
                                array_offset);
2778
 
 
2779
 
   *x_offset_el = x_offset_sa / fmtl->bw;
2780
 
   *y_offset_el = y_offset_sa / fmtl->bh;
2781
 
   *z_offset_el = z_offset_sa / fmtl->bd;
2782
 
}
2783
 
 
2784
 
void
2785
 
isl_surf_get_image_offset_B_tile_sa(const struct isl_surf *surf,
2786
 
                                    uint32_t level,
2787
 
                                    uint32_t logical_array_layer,
2788
 
                                    uint32_t logical_z_offset_px,
2789
 
                                    uint64_t *offset_B,
2790
 
                                    uint32_t *x_offset_sa,
2791
 
                                    uint32_t *y_offset_sa)
2792
 
{
2793
 
   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2794
 
 
2795
 
   uint32_t x_offset_el, y_offset_el;
2796
 
   isl_surf_get_image_offset_B_tile_el(surf, level,
2797
 
                                       logical_array_layer,
2798
 
                                       logical_z_offset_px,
2799
 
                                       offset_B,
2800
 
                                       &x_offset_el,
2801
 
                                       &y_offset_el);
2802
 
 
2803
 
   if (x_offset_sa) {
2804
 
      *x_offset_sa = x_offset_el * fmtl->bw;
2805
 
   } else {
2806
 
      assert(x_offset_el == 0);
2807
 
   }
2808
 
 
2809
 
   if (y_offset_sa) {
2810
 
      *y_offset_sa = y_offset_el * fmtl->bh;
2811
 
   } else {
2812
 
      assert(y_offset_el == 0);
2813
 
   }
2814
 
}
2815
 
 
2816
 
void
2817
 
isl_surf_get_image_offset_B_tile_el(const struct isl_surf *surf,
2818
 
                                    uint32_t level,
2819
 
                                    uint32_t logical_array_layer,
2820
 
                                    uint32_t logical_z_offset_px,
2821
 
                                    uint64_t *offset_B,
2822
 
                                    uint32_t *x_offset_el,
2823
 
                                    uint32_t *y_offset_el)
2824
 
{
2825
 
   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2826
 
 
2827
 
   uint32_t total_x_offset_el, total_y_offset_el;
2828
 
   uint32_t total_z_offset_el, total_array_offset;
2829
 
   isl_surf_get_image_offset_el(surf, level, logical_array_layer,
2830
 
                                logical_z_offset_px,
2831
 
                                &total_x_offset_el,
2832
 
                                &total_y_offset_el,
2833
 
                                &total_z_offset_el,
2834
 
                                &total_array_offset);
2835
 
 
2836
 
   uint32_t z_offset_el, array_offset;
2837
 
   isl_tiling_get_intratile_offset_el(surf->tiling, surf->dim,
2838
 
                                      surf->msaa_layout, fmtl->bpb,
2839
 
                                      surf->samples,
2840
 
                                      surf->row_pitch_B,
2841
 
                                      surf->array_pitch_el_rows,
2842
 
                                      total_x_offset_el,
2843
 
                                      total_y_offset_el,
2844
 
                                      total_z_offset_el,
2845
 
                                      total_array_offset,
2846
 
                                      offset_B,
2847
 
                                      x_offset_el,
2848
 
                                      y_offset_el,
2849
 
                                      &z_offset_el,
2850
 
                                      &array_offset);
2851
 
   assert(z_offset_el == 0);
2852
 
   assert(array_offset == 0);
2853
 
}
2854
 
 
2855
 
void
2856
 
isl_surf_get_image_range_B_tile(const struct isl_surf *surf,
2857
 
                                uint32_t level,
2858
 
                                uint32_t logical_array_layer,
2859
 
                                uint32_t logical_z_offset_px,
2860
 
                                uint64_t *start_tile_B,
2861
 
                                uint64_t *end_tile_B)
2862
 
{
2863
 
   uint32_t start_x_offset_el, start_y_offset_el;
2864
 
   uint32_t start_z_offset_el, start_array_slice;
2865
 
   isl_surf_get_image_offset_el(surf, level, logical_array_layer,
2866
 
                                logical_z_offset_px,
2867
 
                                &start_x_offset_el,
2868
 
                                &start_y_offset_el,
2869
 
                                &start_z_offset_el,
2870
 
                                &start_array_slice);
2871
 
 
2872
 
   /* Compute the size of the subimage in surface elements */
2873
 
   const uint32_t subimage_w_sa = isl_minify(surf->phys_level0_sa.w, level);
2874
 
   const uint32_t subimage_h_sa = isl_minify(surf->phys_level0_sa.h, level);
2875
 
   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2876
 
   const uint32_t subimage_w_el = isl_align_div_npot(subimage_w_sa, fmtl->bw);
2877
 
   const uint32_t subimage_h_el = isl_align_div_npot(subimage_h_sa, fmtl->bh);
2878
 
 
2879
 
   /* Find the last pixel */
2880
 
   uint32_t end_x_offset_el = start_x_offset_el + subimage_w_el - 1;
2881
 
   uint32_t end_y_offset_el = start_y_offset_el + subimage_h_el - 1;
2882
 
 
2883
 
   /* We only consider one Z or array slice */
2884
 
   const uint32_t end_z_offset_el = start_z_offset_el;
2885
 
   const uint32_t end_array_slice = start_array_slice;
2886
 
 
2887
 
   UNUSED uint32_t x_offset_el, y_offset_el, z_offset_el, array_slice;
2888
 
   isl_tiling_get_intratile_offset_el(surf->tiling, surf->dim,
2889
 
                                      surf->msaa_layout, fmtl->bpb,
2890
 
                                      surf->samples,
2891
 
                                      surf->row_pitch_B,
2892
 
                                      surf->array_pitch_el_rows,
2893
 
                                      start_x_offset_el,
2894
 
                                      start_y_offset_el,
2895
 
                                      start_z_offset_el,
2896
 
                                      start_array_slice,
2897
 
                                      start_tile_B,
2898
 
                                      &x_offset_el,
2899
 
                                      &y_offset_el,
2900
 
                                      &z_offset_el,
2901
 
                                      &array_slice);
2902
 
 
2903
 
   isl_tiling_get_intratile_offset_el(surf->tiling, surf->dim,
2904
 
                                      surf->msaa_layout, fmtl->bpb,
2905
 
                                      surf->samples,
2906
 
                                      surf->row_pitch_B,
2907
 
                                      surf->array_pitch_el_rows,
2908
 
                                      end_x_offset_el,
2909
 
                                      end_y_offset_el,
2910
 
                                      end_z_offset_el,
2911
 
                                      end_array_slice,
2912
 
                                      end_tile_B,
2913
 
                                      &x_offset_el,
2914
 
                                      &y_offset_el,
2915
 
                                      &z_offset_el,
2916
 
                                      &array_slice);
2917
 
 
2918
 
   /* We want the range we return to be exclusive but the tile containing the
2919
 
    * last pixel (what we just calculated) is inclusive.  Add one.
2920
 
    */
2921
 
   (*end_tile_B)++;
2922
 
 
2923
 
   assert(*end_tile_B <= surf->size_B);
2924
 
}
2925
 
 
2926
 
void
2927
 
isl_surf_get_image_surf(const struct isl_device *dev,
2928
 
                        const struct isl_surf *surf,
2929
 
                        uint32_t level,
2930
 
                        uint32_t logical_array_layer,
2931
 
                        uint32_t logical_z_offset_px,
2932
 
                        struct isl_surf *image_surf,
2933
 
                        uint64_t *offset_B,
2934
 
                        uint32_t *x_offset_sa,
2935
 
                        uint32_t *y_offset_sa)
2936
 
{
2937
 
   isl_surf_get_image_offset_B_tile_sa(surf,
2938
 
                                       level,
2939
 
                                       logical_array_layer,
2940
 
                                       logical_z_offset_px,
2941
 
                                       offset_B,
2942
 
                                       x_offset_sa,
2943
 
                                       y_offset_sa);
2944
 
 
2945
 
   /* Even for cube maps there will be only single face, therefore drop the
2946
 
    * corresponding flag if present.
2947
 
    */
2948
 
   const isl_surf_usage_flags_t usage =
2949
 
      surf->usage & (~ISL_SURF_USAGE_CUBE_BIT);
2950
 
 
2951
 
   bool ok UNUSED;
2952
 
   ok = isl_surf_init(dev, image_surf,
2953
 
                      .dim = ISL_SURF_DIM_2D,
2954
 
                      .format = surf->format,
2955
 
                      .width = isl_minify(surf->logical_level0_px.w, level),
2956
 
                      .height = isl_minify(surf->logical_level0_px.h, level),
2957
 
                      .depth = 1,
2958
 
                      .levels = 1,
2959
 
                      .array_len = 1,
2960
 
                      .samples = surf->samples,
2961
 
                      .row_pitch_B = surf->row_pitch_B,
2962
 
                      .usage = usage,
2963
 
                      .tiling_flags = (1 << surf->tiling));
2964
 
   assert(ok);
2965
 
}
2966
 
 
2967
 
bool
2968
 
isl_surf_get_uncompressed_surf(const struct isl_device *dev,
2969
 
                               const struct isl_surf *surf,
2970
 
                               const struct isl_view *view,
2971
 
                               struct isl_surf *ucompr_surf,
2972
 
                               struct isl_view *ucompr_view,
2973
 
                               uint64_t *offset_B,
2974
 
                               uint32_t *x_offset_el,
2975
 
                               uint32_t *y_offset_el)
2976
 
{
2977
 
   const struct isl_format_layout *fmtl =
2978
 
      isl_format_get_layout(surf->format);
2979
 
   const enum isl_format view_format = view->format;
2980
 
 
2981
 
   assert(fmtl->bw > 1 || fmtl->bh > 1 || fmtl->bd > 1);
2982
 
   assert(isl_format_is_compressed(surf->format));
2983
 
   assert(!isl_format_is_compressed(view->format));
2984
 
   assert(isl_format_get_layout(view->format)->bpb == fmtl->bpb);
2985
 
   assert(view->levels == 1);
2986
 
 
2987
 
   const uint32_t view_width_px =
2988
 
      isl_minify(surf->logical_level0_px.width, view->base_level);
2989
 
   const uint32_t view_height_px =
2990
 
      isl_minify(surf->logical_level0_px.height, view->base_level);
2991
 
 
2992
 
   assert(surf->samples == 1);
2993
 
   const uint32_t view_width_el = isl_align_div_npot(view_width_px, fmtl->bw);
2994
 
   const uint32_t view_height_el = isl_align_div_npot(view_height_px, fmtl->bh);
2995
 
 
2996
 
   /* If we ever enable 3D block formats, we'll need to re-think this */
2997
 
   assert(fmtl->bd == 1);
2998
 
 
2999
 
   if (view->array_len > 1) {
3000
 
      /* The Skylake PRM Vol. 2d, "RENDER_SURFACE_STATE::X Offset" says:
3001
 
       *
3002
 
       *    "If Surface Array is enabled, this field must be zero."
3003
 
       *
3004
 
       * The PRMs for other hardware have similar text.  This is also tricky
3005
 
       * to handle with things like BLORP's SW offsetting because the
3006
 
       * increased surface size required for the offset may result in an image
3007
 
       * height greater than qpitch.
3008
 
       */
3009
 
      if (view->base_level > 0)
3010
 
         return false;
3011
 
 
3012
 
      /* On Haswell and earlier, RENDER_SURFACE_STATE doesn't have a QPitch
3013
 
       * field; it only has "array pitch span" which means the QPitch is
3014
 
       * automatically calculated.  Since we're smashing the surface format
3015
 
       * (block formats are subtly different) and the number of miplevels,
3016
 
       * that calculation will get thrown off.  This means we can't do arrays
3017
 
       * even at LOD0
3018
 
       *
3019
 
       * On Broadwell, we do have a QPitch field which we can control.
3020
 
       * However, HALIGN and VALIGN are specified in pixels and are
3021
 
       * hard-coded to align to exactly the block size of the compressed
3022
 
       * texture.  This means that, when reinterpreted as a non-compressed
3023
 
       * the QPitch may be anything but the HW requires it to be properly
3024
 
       * aligned.
3025
 
       */
3026
 
      if (ISL_GFX_VER(dev) < 9)
3027
 
         return false;
3028
 
 
3029
 
      *ucompr_surf = *surf;
3030
 
      ucompr_surf->levels = 1;
3031
 
      ucompr_surf->format = view_format;
3032
 
 
3033
 
      /* We're making an uncompressed view here.  The image dimensions
3034
 
       * need to be scaled down by the block size.
3035
 
       */
3036
 
      assert(ucompr_surf->logical_level0_px.width == view_width_px);
3037
 
      assert(ucompr_surf->logical_level0_px.height == view_height_px);
3038
 
      ucompr_surf->logical_level0_px.width = view_width_el;
3039
 
      ucompr_surf->logical_level0_px.height = view_height_el;
3040
 
      ucompr_surf->phys_level0_sa = isl_surf_get_phys_level0_el(surf);
3041
 
 
3042
 
      /* The surface mostly stays as-is; there is no offset */
3043
 
      *offset_B = 0;
3044
 
      *x_offset_el = 0;
3045
 
      *y_offset_el = 0;
3046
 
 
3047
 
      /* The view remains the same */
3048
 
      *ucompr_view = *view;
3049
 
   } else {
3050
 
      /* If only one array slice is requested, directly offset to that slice.
3051
 
       * We could, in theory, still use arrays in some cases but BLORP isn't
3052
 
       * prepared for this and everyone who calls this function should be
3053
 
       * prepared to handle an X/Y offset.
3054
 
       */
3055
 
      isl_surf_get_image_offset_B_tile_el(surf,
3056
 
                                          view->base_level,
3057
 
                                          surf->dim == ISL_SURF_DIM_3D ?
3058
 
                                             0 : view->base_array_layer,
3059
 
                                          surf->dim == ISL_SURF_DIM_3D ?
3060
 
                                             view->base_array_layer : 0,
3061
 
                                          offset_B,
3062
 
                                          x_offset_el,
3063
 
                                          y_offset_el);
3064
 
 
3065
 
      /* Even for cube maps there will be only single face, therefore drop the
3066
 
       * corresponding flag if present.
3067
 
       */
3068
 
      const isl_surf_usage_flags_t usage =
3069
 
         surf->usage & (~ISL_SURF_USAGE_CUBE_BIT);
3070
 
 
3071
 
      bool ok UNUSED;
3072
 
      ok = isl_surf_init(dev, ucompr_surf,
3073
 
                         .dim = ISL_SURF_DIM_2D,
3074
 
                         .format = view_format,
3075
 
                         .width = view_width_el,
3076
 
                         .height = view_height_el,
3077
 
                         .depth = 1,
3078
 
                         .levels = 1,
3079
 
                         .array_len = 1,
3080
 
                         .samples = 1,
3081
 
                         .row_pitch_B = surf->row_pitch_B,
3082
 
                         .usage = usage,
3083
 
                         .tiling_flags = (1 << surf->tiling));
3084
 
      assert(ok);
3085
 
 
3086
 
      /* The newly created image represents the one subimage we're
3087
 
       * referencing with this view so it only has one array slice and
3088
 
       * miplevel.
3089
 
       */
3090
 
      *ucompr_view = *view;
3091
 
      ucompr_view->base_array_layer = 0;
3092
 
      ucompr_view->base_level = 0;
3093
 
   }
3094
 
 
3095
 
   return true;
3096
 
}
3097
 
 
3098
 
void
3099
 
isl_tiling_get_intratile_offset_el(enum isl_tiling tiling,
3100
 
                                   enum isl_surf_dim dim,
3101
 
                                   enum isl_msaa_layout msaa_layout,
3102
 
                                   uint32_t bpb,
3103
 
                                   uint32_t samples,
3104
 
                                   uint32_t row_pitch_B,
3105
 
                                   uint32_t array_pitch_el_rows,
3106
 
                                   uint32_t total_x_offset_el,
3107
 
                                   uint32_t total_y_offset_el,
3108
 
                                   uint32_t total_z_offset_el,
3109
 
                                   uint32_t total_array_offset,
3110
 
                                   uint64_t *tile_offset_B,
3111
 
                                   uint32_t *x_offset_el,
3112
 
                                   uint32_t *y_offset_el,
3113
 
                                   uint32_t *z_offset_el,
3114
 
                                   uint32_t *array_offset)
3115
 
{
3116
 
   if (tiling == ISL_TILING_LINEAR) {
3117
 
      assert(bpb % 8 == 0);
3118
 
      assert(samples == 1);
3119
 
      assert(total_z_offset_el == 0 && total_array_offset == 0);
3120
 
      *tile_offset_B = (uint64_t)total_y_offset_el * row_pitch_B +
3121
 
                       (uint64_t)total_x_offset_el * (bpb / 8);
3122
 
      *x_offset_el = 0;
3123
 
      *y_offset_el = 0;
3124
 
      *z_offset_el = 0;
3125
 
      *array_offset = 0;
3126
 
      return;
3127
 
   }
3128
 
 
3129
 
   struct isl_tile_info tile_info;
3130
 
   isl_tiling_get_info(tiling, dim, msaa_layout, bpb, samples, &tile_info);
3131
 
 
3132
 
   /* Pitches must make sense with the tiling */
3133
 
   assert(row_pitch_B % tile_info.phys_extent_B.width == 0);
3134
 
   if (tile_info.logical_extent_el.d > 1 || tile_info.logical_extent_el.a > 1)
3135
 
      assert(array_pitch_el_rows % tile_info.logical_extent_el.h == 0);
3136
 
 
3137
 
   /* For non-power-of-two formats, we need the address to be both tile and
3138
 
    * element-aligned.  The easiest way to achieve this is to work with a tile
3139
 
    * that is three times as wide as the regular tile.
3140
 
    *
3141
 
    * The tile info returned by get_tile_info has a logical size that is an
3142
 
    * integer number of tile_info.format_bpb size elements.  To scale the
3143
 
    * tile, we scale up the physical width and then treat the logical tile
3144
 
    * size as if it has bpb size elements.
3145
 
    */
3146
 
   const uint32_t tile_el_scale = bpb / tile_info.format_bpb;
3147
 
   tile_info.phys_extent_B.width *= tile_el_scale;
3148
 
 
3149
 
   /* Compute the offset into the tile */
3150
 
   *x_offset_el = total_x_offset_el % tile_info.logical_extent_el.w;
3151
 
   *y_offset_el = total_y_offset_el % tile_info.logical_extent_el.h;
3152
 
   *z_offset_el = total_z_offset_el % tile_info.logical_extent_el.d;
3153
 
   *array_offset = total_array_offset % tile_info.logical_extent_el.a;
3154
 
 
3155
 
   /* Compute the offset of the tile in units of whole tiles */
3156
 
   uint32_t x_offset_tl = total_x_offset_el / tile_info.logical_extent_el.w;
3157
 
   uint32_t y_offset_tl = total_y_offset_el / tile_info.logical_extent_el.h;
3158
 
   uint32_t z_offset_tl = total_z_offset_el / tile_info.logical_extent_el.d;
3159
 
   uint32_t a_offset_tl = total_array_offset / tile_info.logical_extent_el.a;
3160
 
 
3161
 
   /* Compute an array pitch in number of tiles */
3162
 
   uint32_t array_pitch_tl_rows =
3163
 
      array_pitch_el_rows / tile_info.logical_extent_el.h;
3164
 
 
3165
 
   /* Add the Z and array offset to the Y offset to get a 2D offset */
3166
 
   y_offset_tl += (z_offset_tl + a_offset_tl) * array_pitch_tl_rows;
3167
 
 
3168
 
   *tile_offset_B =
3169
 
      (uint64_t)y_offset_tl * tile_info.phys_extent_B.h * row_pitch_B +
3170
 
      (uint64_t)x_offset_tl * tile_info.phys_extent_B.h * tile_info.phys_extent_B.w;
3171
 
}
3172
 
 
3173
 
uint32_t
3174
 
isl_surf_get_depth_format(const struct isl_device *dev,
3175
 
                          const struct isl_surf *surf)
3176
 
{
3177
 
   /* Support for separate stencil buffers began in gfx5. Support for
3178
 
    * interleaved depthstencil buffers ceased in gfx7. The intermediate gens,
3179
 
    * those that supported separate and interleaved stencil, were gfx5 and
3180
 
    * gfx6.
3181
 
    *
3182
 
    * For a list of all available formats, see the Sandybridge PRM >> Volume
3183
 
    * 2 Part 1: 3D/Media - 3D Pipeline >> 3DSTATE_DEPTH_BUFFER >> Surface
3184
 
    * Format (p321).
3185
 
    */
3186
 
 
3187
 
   bool has_stencil = surf->usage & ISL_SURF_USAGE_STENCIL_BIT;
3188
 
 
3189
 
   assert(surf->usage & ISL_SURF_USAGE_DEPTH_BIT);
3190
 
 
3191
 
   if (has_stencil)
3192
 
      assert(ISL_GFX_VER(dev) < 7);
3193
 
 
3194
 
   switch (surf->format) {
3195
 
   default:
3196
 
      unreachable("bad isl depth format");
3197
 
   case ISL_FORMAT_R32_FLOAT_X8X24_TYPELESS:
3198
 
      assert(ISL_GFX_VER(dev) < 7);
3199
 
      return 0; /* D32_FLOAT_S8X24_UINT */
3200
 
   case ISL_FORMAT_R32_FLOAT:
3201
 
      assert(!has_stencil);
3202
 
      return 1; /* D32_FLOAT */
3203
 
   case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
3204
 
      if (has_stencil) {
3205
 
         assert(ISL_GFX_VER(dev) < 7);
3206
 
         return 2; /* D24_UNORM_S8_UINT */
3207
 
      } else {
3208
 
         assert(ISL_GFX_VER(dev) >= 5);
3209
 
         return 3; /* D24_UNORM_X8_UINT */
3210
 
      }
3211
 
   case ISL_FORMAT_R16_UNORM:
3212
 
      assert(!has_stencil);
3213
 
      return 5; /* D16_UNORM */
3214
 
   }
3215
 
}
3216
 
 
3217
 
bool
3218
 
isl_swizzle_supports_rendering(const struct intel_device_info *devinfo,
3219
 
                               struct isl_swizzle swizzle)
3220
 
{
3221
 
   if (devinfo->platform == INTEL_PLATFORM_HSW) {
3222
 
      /* From the Haswell PRM,
3223
 
       * RENDER_SURFACE_STATE::Shader Channel Select Red
3224
 
       *
3225
 
       *    "The Shader channel selects also define which shader channels are
3226
 
       *    written to which surface channel. If the Shader channel select is
3227
 
       *    SCS_ZERO or SCS_ONE then it is not written to the surface. If the
3228
 
       *    shader channel select is SCS_RED it is written to the surface red
3229
 
       *    channel and so on. If more than one shader channel select is set
3230
 
       *    to the same surface channel only the first shader channel in RGBA
3231
 
       *    order will be written."
3232
 
       */
3233
 
      return true;
3234
 
   } else if (devinfo->ver <= 7) {
3235
 
      /* Ivy Bridge and early doesn't have any swizzling */
3236
 
      return isl_swizzle_is_identity(swizzle);
3237
 
   } else {
3238
 
      /* From the Sky Lake PRM Vol. 2d,
3239
 
       * RENDER_SURFACE_STATE::Shader Channel Select Red
3240
 
       *
3241
 
       *    "For Render Target, Red, Green and Blue Shader Channel Selects
3242
 
       *    MUST be such that only valid components can be swapped i.e. only
3243
 
       *    change the order of components in the pixel. Any other values for
3244
 
       *    these Shader Channel Select fields are not valid for Render
3245
 
       *    Targets. This also means that there MUST not be multiple shader
3246
 
       *    channels mapped to the same RT channel."
3247
 
       *
3248
 
       * From the Sky Lake PRM Vol. 2d,
3249
 
       * RENDER_SURFACE_STATE::Shader Channel Select Alpha
3250
 
       *
3251
 
       *    "For Render Target, this field MUST be programmed to
3252
 
       *    value = SCS_ALPHA."
3253
 
       */
3254
 
      return (swizzle.r == ISL_CHANNEL_SELECT_RED ||
3255
 
              swizzle.r == ISL_CHANNEL_SELECT_GREEN ||
3256
 
              swizzle.r == ISL_CHANNEL_SELECT_BLUE) &&
3257
 
             (swizzle.g == ISL_CHANNEL_SELECT_RED ||
3258
 
              swizzle.g == ISL_CHANNEL_SELECT_GREEN ||
3259
 
              swizzle.g == ISL_CHANNEL_SELECT_BLUE) &&
3260
 
             (swizzle.b == ISL_CHANNEL_SELECT_RED ||
3261
 
              swizzle.b == ISL_CHANNEL_SELECT_GREEN ||
3262
 
              swizzle.b == ISL_CHANNEL_SELECT_BLUE) &&
3263
 
             swizzle.r != swizzle.g &&
3264
 
             swizzle.r != swizzle.b &&
3265
 
             swizzle.g != swizzle.b &&
3266
 
             swizzle.a == ISL_CHANNEL_SELECT_ALPHA;
3267
 
   }
3268
 
}
3269
 
 
3270
 
static enum isl_channel_select
3271
 
swizzle_select(enum isl_channel_select chan, struct isl_swizzle swizzle)
3272
 
{
3273
 
   switch (chan) {
3274
 
   case ISL_CHANNEL_SELECT_ZERO:
3275
 
   case ISL_CHANNEL_SELECT_ONE:
3276
 
      return chan;
3277
 
   case ISL_CHANNEL_SELECT_RED:
3278
 
      return swizzle.r;
3279
 
   case ISL_CHANNEL_SELECT_GREEN:
3280
 
      return swizzle.g;
3281
 
   case ISL_CHANNEL_SELECT_BLUE:
3282
 
      return swizzle.b;
3283
 
   case ISL_CHANNEL_SELECT_ALPHA:
3284
 
      return swizzle.a;
3285
 
   default:
3286
 
      unreachable("Invalid swizzle component");
3287
 
   }
3288
 
}
3289
 
 
3290
 
/**
3291
 
 * Returns the single swizzle that is equivalent to applying the two given
3292
 
 * swizzles in sequence.
3293
 
 */
3294
 
struct isl_swizzle
3295
 
isl_swizzle_compose(struct isl_swizzle first, struct isl_swizzle second)
3296
 
{
3297
 
   return (struct isl_swizzle) {
3298
 
      .r = swizzle_select(first.r, second),
3299
 
      .g = swizzle_select(first.g, second),
3300
 
      .b = swizzle_select(first.b, second),
3301
 
      .a = swizzle_select(first.a, second),
3302
 
   };
3303
 
}
3304
 
 
3305
 
/**
3306
 
 * Returns a swizzle that is the pseudo-inverse of this swizzle.
3307
 
 */
3308
 
struct isl_swizzle
3309
 
isl_swizzle_invert(struct isl_swizzle swizzle)
3310
 
{
3311
 
   /* Default to zero for channels which do not show up in the swizzle */
3312
 
   enum isl_channel_select chans[4] = {
3313
 
      ISL_CHANNEL_SELECT_ZERO,
3314
 
      ISL_CHANNEL_SELECT_ZERO,
3315
 
      ISL_CHANNEL_SELECT_ZERO,
3316
 
      ISL_CHANNEL_SELECT_ZERO,
3317
 
   };
3318
 
 
3319
 
   /* We go in ABGR order so that, if there are any duplicates, the first one
3320
 
    * is taken if you look at it in RGBA order.  This is what Haswell hardware
3321
 
    * does for render target swizzles.
3322
 
    */
3323
 
   if ((unsigned)(swizzle.a - ISL_CHANNEL_SELECT_RED) < 4)
3324
 
      chans[swizzle.a - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_ALPHA;
3325
 
   if ((unsigned)(swizzle.b - ISL_CHANNEL_SELECT_RED) < 4)
3326
 
      chans[swizzle.b - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_BLUE;
3327
 
   if ((unsigned)(swizzle.g - ISL_CHANNEL_SELECT_RED) < 4)
3328
 
      chans[swizzle.g - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_GREEN;
3329
 
   if ((unsigned)(swizzle.r - ISL_CHANNEL_SELECT_RED) < 4)
3330
 
      chans[swizzle.r - ISL_CHANNEL_SELECT_RED] = ISL_CHANNEL_SELECT_RED;
3331
 
 
3332
 
   return (struct isl_swizzle) { chans[0], chans[1], chans[2], chans[3] };
3333
 
}
3334
 
 
3335
 
static uint32_t
3336
 
isl_color_value_channel(union isl_color_value src,
3337
 
                        enum isl_channel_select chan,
3338
 
                        uint32_t one)
3339
 
{
3340
 
   if (chan == ISL_CHANNEL_SELECT_ZERO)
3341
 
      return 0;
3342
 
   if (chan == ISL_CHANNEL_SELECT_ONE)
3343
 
      return one;
3344
 
 
3345
 
   assert(chan >= ISL_CHANNEL_SELECT_RED);
3346
 
   assert(chan < ISL_CHANNEL_SELECT_RED + 4);
3347
 
 
3348
 
   return src.u32[chan - ISL_CHANNEL_SELECT_RED];
3349
 
}
3350
 
 
3351
 
/** Applies an inverse swizzle to a color value */
3352
 
union isl_color_value
3353
 
isl_color_value_swizzle(union isl_color_value src,
3354
 
                        struct isl_swizzle swizzle,
3355
 
                        bool is_float)
3356
 
{
3357
 
   uint32_t one = is_float ? 0x3f800000 : 1;
3358
 
 
3359
 
   return (union isl_color_value) { .u32 = {
3360
 
      isl_color_value_channel(src, swizzle.r, one),
3361
 
      isl_color_value_channel(src, swizzle.g, one),
3362
 
      isl_color_value_channel(src, swizzle.b, one),
3363
 
      isl_color_value_channel(src, swizzle.a, one),
3364
 
   } };
3365
 
}
3366
 
 
3367
 
/** Applies an inverse swizzle to a color value */
3368
 
union isl_color_value
3369
 
isl_color_value_swizzle_inv(union isl_color_value src,
3370
 
                            struct isl_swizzle swizzle)
3371
 
{
3372
 
   union isl_color_value dst = { .u32 = { 0, } };
3373
 
 
3374
 
   /* We assign colors in ABGR order so that the first one will be taken in
3375
 
    * RGBA precedence order.  According to the PRM docs for shader channel
3376
 
    * select, this matches Haswell hardware behavior.
3377
 
    */
3378
 
   if ((unsigned)(swizzle.a - ISL_CHANNEL_SELECT_RED) < 4)
3379
 
      dst.u32[swizzle.a - ISL_CHANNEL_SELECT_RED] = src.u32[3];
3380
 
   if ((unsigned)(swizzle.b - ISL_CHANNEL_SELECT_RED) < 4)
3381
 
      dst.u32[swizzle.b - ISL_CHANNEL_SELECT_RED] = src.u32[2];
3382
 
   if ((unsigned)(swizzle.g - ISL_CHANNEL_SELECT_RED) < 4)
3383
 
      dst.u32[swizzle.g - ISL_CHANNEL_SELECT_RED] = src.u32[1];
3384
 
   if ((unsigned)(swizzle.r - ISL_CHANNEL_SELECT_RED) < 4)
3385
 
      dst.u32[swizzle.r - ISL_CHANNEL_SELECT_RED] = src.u32[0];
3386
 
 
3387
 
   return dst;
3388
 
}
3389
 
 
3390
 
uint8_t
3391
 
isl_format_get_aux_map_encoding(enum isl_format format)
3392
 
{
3393
 
   switch(format) {
3394
 
   case ISL_FORMAT_R32G32B32A32_FLOAT: return 0x11;
3395
 
   case ISL_FORMAT_R32G32B32X32_FLOAT: return 0x11;
3396
 
   case ISL_FORMAT_R32G32B32A32_SINT: return 0x12;
3397
 
   case ISL_FORMAT_R32G32B32A32_UINT: return 0x13;
3398
 
   case ISL_FORMAT_R16G16B16A16_UNORM: return 0x14;
3399
 
   case ISL_FORMAT_R16G16B16A16_SNORM: return 0x15;
3400
 
   case ISL_FORMAT_R16G16B16A16_SINT: return 0x16;
3401
 
   case ISL_FORMAT_R16G16B16A16_UINT: return 0x17;
3402
 
   case ISL_FORMAT_R16G16B16A16_FLOAT: return 0x10;
3403
 
   case ISL_FORMAT_R16G16B16X16_FLOAT: return 0x10;
3404
 
   case ISL_FORMAT_R32G32_FLOAT: return 0x11;
3405
 
   case ISL_FORMAT_R32G32_SINT: return 0x12;
3406
 
   case ISL_FORMAT_R32G32_UINT: return 0x13;
3407
 
   case ISL_FORMAT_B8G8R8A8_UNORM: return 0xA;
3408
 
   case ISL_FORMAT_B8G8R8X8_UNORM: return 0xA;
3409
 
   case ISL_FORMAT_B8G8R8A8_UNORM_SRGB: return 0xA;
3410
 
   case ISL_FORMAT_B8G8R8X8_UNORM_SRGB: return 0xA;
3411
 
   case ISL_FORMAT_R10G10B10A2_UNORM: return 0x18;
3412
 
   case ISL_FORMAT_R10G10B10A2_UNORM_SRGB: return 0x18;
3413
 
   case ISL_FORMAT_R10G10B10_FLOAT_A2_UNORM: return 0x19;
3414
 
   case ISL_FORMAT_R10G10B10A2_UINT: return 0x1A;
3415
 
   case ISL_FORMAT_R8G8B8A8_UNORM: return 0xA;
3416
 
   case ISL_FORMAT_R8G8B8A8_UNORM_SRGB: return 0xA;
3417
 
   case ISL_FORMAT_R8G8B8A8_SNORM: return 0x1B;
3418
 
   case ISL_FORMAT_R8G8B8A8_SINT: return 0x1C;
3419
 
   case ISL_FORMAT_R8G8B8A8_UINT: return 0x1D;
3420
 
   case ISL_FORMAT_R16G16_UNORM: return 0x14;
3421
 
   case ISL_FORMAT_R16G16_SNORM: return 0x15;
3422
 
   case ISL_FORMAT_R16G16_SINT: return 0x16;
3423
 
   case ISL_FORMAT_R16G16_UINT: return 0x17;
3424
 
   case ISL_FORMAT_R16G16_FLOAT: return 0x10;
3425
 
   case ISL_FORMAT_B10G10R10A2_UNORM: return 0x18;
3426
 
   case ISL_FORMAT_B10G10R10A2_UNORM_SRGB: return 0x18;
3427
 
   case ISL_FORMAT_R11G11B10_FLOAT: return 0x1E;
3428
 
   case ISL_FORMAT_R32_SINT: return 0x12;
3429
 
   case ISL_FORMAT_R32_UINT: return 0x13;
3430
 
   case ISL_FORMAT_R32_FLOAT: return 0x11;
3431
 
   case ISL_FORMAT_R24_UNORM_X8_TYPELESS: return 0x13;
3432
 
   case ISL_FORMAT_B5G6R5_UNORM: return 0xA;
3433
 
   case ISL_FORMAT_B5G6R5_UNORM_SRGB: return 0xA;
3434
 
   case ISL_FORMAT_B5G5R5A1_UNORM: return 0xA;
3435
 
   case ISL_FORMAT_B5G5R5A1_UNORM_SRGB: return 0xA;
3436
 
   case ISL_FORMAT_B4G4R4A4_UNORM: return 0xA;
3437
 
   case ISL_FORMAT_B4G4R4A4_UNORM_SRGB: return 0xA;
3438
 
   case ISL_FORMAT_R8G8_UNORM: return 0xA;
3439
 
   case ISL_FORMAT_R8G8_SNORM: return 0x1B;
3440
 
   case ISL_FORMAT_R8G8_SINT: return 0x1C;
3441
 
   case ISL_FORMAT_R8G8_UINT: return 0x1D;
3442
 
   case ISL_FORMAT_R16_UNORM: return 0x14;
3443
 
   case ISL_FORMAT_R16_SNORM: return 0x15;
3444
 
   case ISL_FORMAT_R16_SINT: return 0x16;
3445
 
   case ISL_FORMAT_R16_UINT: return 0x17;
3446
 
   case ISL_FORMAT_R16_FLOAT: return 0x10;
3447
 
   case ISL_FORMAT_B5G5R5X1_UNORM: return 0xA;
3448
 
   case ISL_FORMAT_B5G5R5X1_UNORM_SRGB: return 0xA;
3449
 
   case ISL_FORMAT_A1B5G5R5_UNORM: return 0xA;
3450
 
   case ISL_FORMAT_A4B4G4R4_UNORM: return 0xA;
3451
 
   case ISL_FORMAT_R8_UNORM: return 0xA;
3452
 
   case ISL_FORMAT_R8_SNORM: return 0x1B;
3453
 
   case ISL_FORMAT_R8_SINT: return 0x1C;
3454
 
   case ISL_FORMAT_R8_UINT: return 0x1D;
3455
 
   case ISL_FORMAT_A8_UNORM: return 0xA;
3456
 
   case ISL_FORMAT_PLANAR_420_8: return 0xF;
3457
 
   case ISL_FORMAT_PLANAR_420_10: return 0x7;
3458
 
   case ISL_FORMAT_PLANAR_420_12: return 0x8;
3459
 
   case ISL_FORMAT_PLANAR_420_16: return 0x8;
3460
 
   case ISL_FORMAT_YCRCB_NORMAL: return 0x3;
3461
 
   case ISL_FORMAT_YCRCB_SWAPY: return 0xB;
3462
 
   default:
3463
 
      unreachable("Unsupported aux-map format!");
3464
 
      return 0;
3465
 
   }
3466
 
}
3467
 
 
3468
 
/*
3469
 
 * Returns compression format encoding for Unified Lossless Compression
3470
 
 */
3471
 
uint8_t
3472
 
isl_get_render_compression_format(enum isl_format format)
3473
 
{
3474
 
   /* From the Bspec, Enumeration_RenderCompressionFormat section (53726): */
3475
 
   switch(format) {
3476
 
   case ISL_FORMAT_R32G32B32A32_FLOAT:
3477
 
   case ISL_FORMAT_R32G32B32X32_FLOAT:
3478
 
   case ISL_FORMAT_R32G32B32A32_SINT:
3479
 
      return 0x0;
3480
 
   case ISL_FORMAT_R32G32B32A32_UINT:
3481
 
      return 0x1;
3482
 
   case ISL_FORMAT_R32G32_FLOAT:
3483
 
   case ISL_FORMAT_R32G32_SINT:
3484
 
      return 0x2;
3485
 
   case ISL_FORMAT_R32G32_UINT:
3486
 
      return 0x3;
3487
 
   case ISL_FORMAT_R16G16B16A16_UNORM:
3488
 
   case ISL_FORMAT_R16G16B16X16_UNORM:
3489
 
   case ISL_FORMAT_R16G16B16A16_UINT:
3490
 
      return 0x4;
3491
 
   case ISL_FORMAT_R16G16B16A16_SNORM:
3492
 
   case ISL_FORMAT_R16G16B16A16_SINT:
3493
 
   case ISL_FORMAT_R16G16B16A16_FLOAT:
3494
 
   case ISL_FORMAT_R16G16B16X16_FLOAT:
3495
 
      return 0x5;
3496
 
   case ISL_FORMAT_R16G16_UNORM:
3497
 
   case ISL_FORMAT_R16G16_UINT:
3498
 
      return 0x6;
3499
 
   case ISL_FORMAT_R16G16_SNORM:
3500
 
   case ISL_FORMAT_R16G16_SINT:
3501
 
   case ISL_FORMAT_R16G16_FLOAT:
3502
 
      return 0x7;
3503
 
   case ISL_FORMAT_B8G8R8A8_UNORM:
3504
 
   case ISL_FORMAT_B8G8R8X8_UNORM:
3505
 
   case ISL_FORMAT_B8G8R8A8_UNORM_SRGB:
3506
 
   case ISL_FORMAT_B8G8R8X8_UNORM_SRGB:
3507
 
   case ISL_FORMAT_R8G8B8A8_UNORM:
3508
 
   case ISL_FORMAT_R8G8B8X8_UNORM:
3509
 
   case ISL_FORMAT_R8G8B8A8_UNORM_SRGB:
3510
 
   case ISL_FORMAT_R8G8B8X8_UNORM_SRGB:
3511
 
   case ISL_FORMAT_R8G8B8A8_UINT:
3512
 
      return 0x8;
3513
 
   case ISL_FORMAT_R8G8B8A8_SNORM:
3514
 
   case ISL_FORMAT_R8G8B8A8_SINT:
3515
 
      return 0x9;
3516
 
   case ISL_FORMAT_B5G6R5_UNORM:
3517
 
   case ISL_FORMAT_B5G6R5_UNORM_SRGB:
3518
 
   case ISL_FORMAT_B5G5R5A1_UNORM:
3519
 
   case ISL_FORMAT_B5G5R5A1_UNORM_SRGB:
3520
 
   case ISL_FORMAT_B4G4R4A4_UNORM:
3521
 
   case ISL_FORMAT_B4G4R4A4_UNORM_SRGB:
3522
 
   case ISL_FORMAT_B5G5R5X1_UNORM:
3523
 
   case ISL_FORMAT_B5G5R5X1_UNORM_SRGB:
3524
 
   case ISL_FORMAT_A1B5G5R5_UNORM:
3525
 
   case ISL_FORMAT_A4B4G4R4_UNORM:
3526
 
   case ISL_FORMAT_R8G8_UNORM:
3527
 
   case ISL_FORMAT_R8G8_UINT:
3528
 
      return 0xA;
3529
 
   case ISL_FORMAT_R8G8_SNORM:
3530
 
   case ISL_FORMAT_R8G8_SINT:
3531
 
      return 0xB;
3532
 
   case ISL_FORMAT_R10G10B10A2_UNORM:
3533
 
   case ISL_FORMAT_R10G10B10A2_UNORM_SRGB:
3534
 
   case ISL_FORMAT_R10G10B10_FLOAT_A2_UNORM:
3535
 
   case ISL_FORMAT_R10G10B10A2_UINT:
3536
 
   case ISL_FORMAT_B10G10R10A2_UNORM:
3537
 
   case ISL_FORMAT_B10G10R10X2_UNORM:
3538
 
   case ISL_FORMAT_B10G10R10A2_UNORM_SRGB:
3539
 
      return 0xC;
3540
 
   case ISL_FORMAT_R11G11B10_FLOAT:
3541
 
      return 0xD;
3542
 
   case ISL_FORMAT_R32_SINT:
3543
 
   case ISL_FORMAT_R32_FLOAT:
3544
 
      return 0x10;
3545
 
   case ISL_FORMAT_R32_UINT:
3546
 
   case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
3547
 
      return 0x11;
3548
 
   case ISL_FORMAT_R16_UNORM:
3549
 
   case ISL_FORMAT_R16_UINT:
3550
 
      return 0x14;
3551
 
   case ISL_FORMAT_R16_SNORM:
3552
 
   case ISL_FORMAT_R16_SINT:
3553
 
   case ISL_FORMAT_R16_FLOAT:
3554
 
      return 0x15;
3555
 
   case ISL_FORMAT_R8_UNORM:
3556
 
   case ISL_FORMAT_R8_UINT:
3557
 
   case ISL_FORMAT_A8_UNORM:
3558
 
      return 0x18;
3559
 
   case ISL_FORMAT_R8_SNORM:
3560
 
   case ISL_FORMAT_R8_SINT:
3561
 
      return 0x19;
3562
 
   default:
3563
 
      unreachable("Unsupported render compression format!");
3564
 
      return 0;
3565
 
   }
3566
 
}
3567
 
 
3568
 
const char *
3569
 
isl_aux_op_to_name(enum isl_aux_op op)
3570
 
{
3571
 
   static const char *names[] = {
3572
 
      [ISL_AUX_OP_NONE]            = "none",
3573
 
      [ISL_AUX_OP_FAST_CLEAR]      = "fast-clear",
3574
 
      [ISL_AUX_OP_FULL_RESOLVE]    = "full-resolve",
3575
 
      [ISL_AUX_OP_PARTIAL_RESOLVE] = "partial-resolve",
3576
 
      [ISL_AUX_OP_AMBIGUATE]       = "ambiguate",
3577
 
   };
3578
 
   assert(op < ARRAY_SIZE(names));
3579
 
   return names[op];
3580
 
}