41fb152d5a917dd299a5a5a0c5c78b7f5028832b
[mesa.git] / src / amd / common / ac_surface.c
1 /*
2 * Copyright © 2011 Red Hat All Rights Reserved.
3 * Copyright © 2017 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27
28 #include "ac_surface.h"
29 #include "amd_family.h"
30 #include "addrlib/src/amdgpu_asic_addr.h"
31 #include "ac_gpu_info.h"
32 #include "util/hash_table.h"
33 #include "util/macros.h"
34 #include "util/simple_mtx.h"
35 #include "util/u_atomic.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "sid.h"
39
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <amdgpu.h>
44 #include "drm-uapi/amdgpu_drm.h"
45
46 #include "addrlib/inc/addrinterface.h"
47
48 #ifndef CIASICIDGFXENGINE_SOUTHERNISLAND
49 #define CIASICIDGFXENGINE_SOUTHERNISLAND 0x0000000A
50 #endif
51
52 #ifndef CIASICIDGFXENGINE_ARCTICISLAND
53 #define CIASICIDGFXENGINE_ARCTICISLAND 0x0000000D
54 #endif
55
56 struct ac_addrlib {
57 ADDR_HANDLE handle;
58
59 /* The cache of DCC retile maps for reuse when allocating images of
60 * similar sizes.
61 */
62 simple_mtx_t dcc_retile_map_lock;
63 struct hash_table *dcc_retile_maps;
64 struct hash_table *dcc_retile_tile_indices;
65 };
66
67 struct dcc_retile_map_key {
68 enum radeon_family family;
69 unsigned retile_width;
70 unsigned retile_height;
71 bool rb_aligned;
72 bool pipe_aligned;
73 unsigned dcc_retile_num_elements;
74 ADDR2_COMPUTE_DCC_ADDRFROMCOORD_INPUT input;
75 };
76
77 static uint32_t dcc_retile_map_hash_key(const void *key)
78 {
79 return _mesa_hash_data(key, sizeof(struct dcc_retile_map_key));
80 }
81
82 static bool dcc_retile_map_keys_equal(const void *a, const void *b)
83 {
84 return memcmp(a, b, sizeof(struct dcc_retile_map_key)) == 0;
85 }
86
87 static void dcc_retile_map_free(struct hash_entry *entry)
88 {
89 free((void*)entry->key);
90 free(entry->data);
91 }
92
93 struct dcc_retile_tile_key {
94 enum radeon_family family;
95 unsigned bpp;
96 unsigned swizzle_mode;
97 bool rb_aligned;
98 bool pipe_aligned;
99 };
100
101 struct dcc_retile_tile_data {
102 unsigned tile_width_log2;
103 unsigned tile_height_log2;
104 uint16_t *data;
105 };
106
107 static uint32_t dcc_retile_tile_hash_key(const void *key)
108 {
109 return _mesa_hash_data(key, sizeof(struct dcc_retile_tile_key));
110 }
111
112 static bool dcc_retile_tile_keys_equal(const void *a, const void *b)
113 {
114 return memcmp(a, b, sizeof(struct dcc_retile_tile_key)) == 0;
115 }
116
117 static void dcc_retile_tile_free(struct hash_entry *entry)
118 {
119 free((void*)entry->key);
120 free(((struct dcc_retile_tile_data*)entry->data)->data);
121 free(entry->data);
122 }
123
124 /* Assumes dcc_retile_map_lock is taken. */
125 static const struct dcc_retile_tile_data *
126 ac_compute_dcc_retile_tile_indices(struct ac_addrlib *addrlib,
127 const struct radeon_info *info,
128 unsigned bpp, unsigned swizzle_mode,
129 bool rb_aligned, bool pipe_aligned)
130 {
131 struct dcc_retile_tile_key key = (struct dcc_retile_tile_key) {
132 .family = info->family,
133 .bpp = bpp,
134 .swizzle_mode = swizzle_mode,
135 .rb_aligned = rb_aligned,
136 .pipe_aligned = pipe_aligned
137 };
138
139 struct hash_entry *entry = _mesa_hash_table_search(addrlib->dcc_retile_tile_indices, &key);
140 if (entry)
141 return entry->data;
142
143 ADDR2_COMPUTE_DCCINFO_INPUT din = {0};
144 ADDR2_COMPUTE_DCCINFO_OUTPUT dout = {0};
145 din.size = sizeof(ADDR2_COMPUTE_DCCINFO_INPUT);
146 dout.size = sizeof(ADDR2_COMPUTE_DCCINFO_OUTPUT);
147
148 din.dccKeyFlags.pipeAligned = pipe_aligned;
149 din.dccKeyFlags.rbAligned = rb_aligned;
150 din.resourceType = ADDR_RSRC_TEX_2D;
151 din.swizzleMode = swizzle_mode;
152 din.bpp = bpp;
153 din.unalignedWidth = 1;
154 din.unalignedHeight = 1;
155 din.numSlices = 1;
156 din.numFrags = 1;
157 din.numMipLevels = 1;
158
159 ADDR_E_RETURNCODE ret = Addr2ComputeDccInfo(addrlib->handle, &din, &dout);
160 if (ret != ADDR_OK)
161 return NULL;
162
163 ADDR2_COMPUTE_DCC_ADDRFROMCOORD_INPUT addrin = {0};
164 addrin.size = sizeof(addrin);
165 addrin.swizzleMode = swizzle_mode;
166 addrin.resourceType = ADDR_RSRC_TEX_2D;
167 addrin.bpp = bpp;
168 addrin.numSlices = 1;
169 addrin.numMipLevels = 1;
170 addrin.numFrags = 1;
171 addrin.pitch = dout.pitch;
172 addrin.height = dout.height;
173 addrin.compressBlkWidth = dout.compressBlkWidth;
174 addrin.compressBlkHeight = dout.compressBlkHeight;
175 addrin.compressBlkDepth = dout.compressBlkDepth;
176 addrin.metaBlkWidth = dout.metaBlkWidth;
177 addrin.metaBlkHeight = dout.metaBlkHeight;
178 addrin.metaBlkDepth = dout.metaBlkDepth;
179 addrin.dccKeyFlags.pipeAligned = pipe_aligned;
180 addrin.dccKeyFlags.rbAligned = rb_aligned;
181
182 unsigned w = dout.metaBlkWidth / dout.compressBlkWidth;
183 unsigned h = dout.metaBlkHeight / dout.compressBlkHeight;
184 uint16_t *indices = malloc(w * h * sizeof (uint16_t));
185 if (!indices)
186 return NULL;
187
188 ADDR2_COMPUTE_DCC_ADDRFROMCOORD_OUTPUT addrout = {};
189 addrout.size = sizeof(addrout);
190
191 for (unsigned y = 0; y < h; ++y) {
192 addrin.y = y * dout.compressBlkHeight;
193 for (unsigned x = 0; x < w; ++x) {
194 addrin.x = x * dout.compressBlkWidth;
195 addrout.addr = 0;
196
197 if (Addr2ComputeDccAddrFromCoord(addrlib->handle, &addrin, &addrout) != ADDR_OK) {
198 free(indices);
199 return NULL;
200 }
201 indices[y * w + x] = addrout.addr;
202 }
203 }
204
205 struct dcc_retile_tile_data *data = calloc(1, sizeof(*data));
206 if (!data) {
207 free(indices);
208 return NULL;
209 }
210
211 data->tile_width_log2 = util_logbase2(w);
212 data->tile_height_log2 = util_logbase2(h);
213 data->data = indices;
214
215 struct dcc_retile_tile_key *heap_key = mem_dup(&key, sizeof(key));
216 if (!heap_key) {
217 free(data);
218 free(indices);
219 return NULL;
220 }
221
222 entry = _mesa_hash_table_insert(addrlib->dcc_retile_tile_indices, heap_key, data);
223 if (!entry) {
224 free(heap_key);
225 free(data);
226 free(indices);
227 }
228 return data;
229 }
230
231 static uint32_t ac_compute_retile_tile_addr(const struct dcc_retile_tile_data *tile,
232 unsigned stride, unsigned x, unsigned y)
233 {
234 unsigned x_mask = (1u << tile->tile_width_log2) - 1;
235 unsigned y_mask = (1u << tile->tile_height_log2) - 1;
236 unsigned tile_size_log2 = tile->tile_width_log2 + tile->tile_height_log2;
237
238 unsigned base = ((y >> tile->tile_height_log2) * stride + (x >> tile->tile_width_log2)) << tile_size_log2;
239 unsigned offset_in_tile = tile->data[((y & y_mask) << tile->tile_width_log2) + (x & x_mask)];
240 return base + offset_in_tile;
241 }
242
243 static uint32_t *ac_compute_dcc_retile_map(struct ac_addrlib *addrlib,
244 const struct radeon_info *info,
245 unsigned retile_width, unsigned retile_height,
246 bool rb_aligned, bool pipe_aligned, bool use_uint16,
247 unsigned dcc_retile_num_elements,
248 const ADDR2_COMPUTE_DCC_ADDRFROMCOORD_INPUT *in)
249 {
250 unsigned dcc_retile_map_size = dcc_retile_num_elements * (use_uint16 ? 2 : 4);
251 struct dcc_retile_map_key key;
252
253 assert(in->numFrags == 1 && in->numSlices == 1 && in->numMipLevels == 1);
254
255 memset(&key, 0, sizeof(key));
256 key.family = info->family;
257 key.retile_width = retile_width;
258 key.retile_height = retile_height;
259 key.rb_aligned = rb_aligned;
260 key.pipe_aligned = pipe_aligned;
261 key.dcc_retile_num_elements = dcc_retile_num_elements;
262 memcpy(&key.input, in, sizeof(*in));
263
264 simple_mtx_lock(&addrlib->dcc_retile_map_lock);
265
266 /* If we have already computed this retile map, get it from the hash table. */
267 struct hash_entry *entry = _mesa_hash_table_search(addrlib->dcc_retile_maps, &key);
268 if (entry) {
269 uint32_t *map = entry->data;
270 simple_mtx_unlock(&addrlib->dcc_retile_map_lock);
271 return map;
272 }
273
274 const struct dcc_retile_tile_data *src_tile =
275 ac_compute_dcc_retile_tile_indices(addrlib, info, in->bpp,
276 in->swizzleMode,
277 rb_aligned, pipe_aligned);
278 const struct dcc_retile_tile_data *dst_tile =
279 ac_compute_dcc_retile_tile_indices(addrlib, info, in->bpp,
280 in->swizzleMode, false, false);
281 if (!src_tile || !dst_tile) {
282 simple_mtx_unlock(&addrlib->dcc_retile_map_lock);
283 return NULL;
284 }
285
286 void *dcc_retile_map = malloc(dcc_retile_map_size);
287 if (!dcc_retile_map) {
288 simple_mtx_unlock(&addrlib->dcc_retile_map_lock);
289 return NULL;
290 }
291
292 unsigned index = 0;
293 unsigned w = DIV_ROUND_UP(retile_width, in->compressBlkWidth);
294 unsigned h = DIV_ROUND_UP(retile_height, in->compressBlkHeight);
295 unsigned src_stride = DIV_ROUND_UP(w, 1u << src_tile->tile_width_log2);
296 unsigned dst_stride = DIV_ROUND_UP(w, 1u << dst_tile->tile_width_log2);
297
298 for (unsigned y = 0; y < h; ++y) {
299 for (unsigned x = 0; x < w; ++x) {
300 unsigned src_addr = ac_compute_retile_tile_addr(src_tile, src_stride, x, y);
301 unsigned dst_addr = ac_compute_retile_tile_addr(dst_tile, dst_stride, x, y);
302
303 if (use_uint16) {
304 ((uint16_t*)dcc_retile_map)[2 * index] = src_addr;
305 ((uint16_t*)dcc_retile_map)[2 * index + 1] = dst_addr;
306 } else {
307 ((uint32_t*)dcc_retile_map)[2 * index] = src_addr;
308 ((uint32_t*)dcc_retile_map)[2 * index + 1] = dst_addr;
309 }
310 ++index;
311 }
312 }
313
314 /* Fill the remaining pairs with the last one (for the compute shader). */
315 for (unsigned i = index * 2; i < dcc_retile_num_elements; i++) {
316 if (use_uint16)
317 ((uint16_t*)dcc_retile_map)[i] = ((uint16_t*)dcc_retile_map)[i - 2];
318 else
319 ((uint32_t*)dcc_retile_map)[i] = ((uint32_t*)dcc_retile_map)[i - 2];
320 }
321
322 /* Insert the retile map into the hash table, so that it can be reused and
323 * the computation can be skipped for similar image sizes.
324 */
325 _mesa_hash_table_insert(addrlib->dcc_retile_maps,
326 mem_dup(&key, sizeof(key)), dcc_retile_map);
327
328 simple_mtx_unlock(&addrlib->dcc_retile_map_lock);
329 return dcc_retile_map;
330 }
331
332 static void *ADDR_API allocSysMem(const ADDR_ALLOCSYSMEM_INPUT * pInput)
333 {
334 return malloc(pInput->sizeInBytes);
335 }
336
337 static ADDR_E_RETURNCODE ADDR_API freeSysMem(const ADDR_FREESYSMEM_INPUT * pInput)
338 {
339 free(pInput->pVirtAddr);
340 return ADDR_OK;
341 }
342
343 struct ac_addrlib *ac_addrlib_create(const struct radeon_info *info,
344 const struct amdgpu_gpu_info *amdinfo,
345 uint64_t *max_alignment)
346 {
347 ADDR_CREATE_INPUT addrCreateInput = {0};
348 ADDR_CREATE_OUTPUT addrCreateOutput = {0};
349 ADDR_REGISTER_VALUE regValue = {0};
350 ADDR_CREATE_FLAGS createFlags = {{0}};
351 ADDR_GET_MAX_ALIGNMENTS_OUTPUT addrGetMaxAlignmentsOutput = {0};
352 ADDR_E_RETURNCODE addrRet;
353
354 addrCreateInput.size = sizeof(ADDR_CREATE_INPUT);
355 addrCreateOutput.size = sizeof(ADDR_CREATE_OUTPUT);
356
357 regValue.gbAddrConfig = amdinfo->gb_addr_cfg;
358 createFlags.value = 0;
359
360 addrCreateInput.chipFamily = info->family_id;
361 addrCreateInput.chipRevision = info->chip_external_rev;
362
363 if (addrCreateInput.chipFamily == FAMILY_UNKNOWN)
364 return NULL;
365
366 if (addrCreateInput.chipFamily >= FAMILY_AI) {
367 addrCreateInput.chipEngine = CIASICIDGFXENGINE_ARCTICISLAND;
368 } else {
369 regValue.noOfBanks = amdinfo->mc_arb_ramcfg & 0x3;
370 regValue.noOfRanks = (amdinfo->mc_arb_ramcfg & 0x4) >> 2;
371
372 regValue.backendDisables = amdinfo->enabled_rb_pipes_mask;
373 regValue.pTileConfig = amdinfo->gb_tile_mode;
374 regValue.noOfEntries = ARRAY_SIZE(amdinfo->gb_tile_mode);
375 if (addrCreateInput.chipFamily == FAMILY_SI) {
376 regValue.pMacroTileConfig = NULL;
377 regValue.noOfMacroEntries = 0;
378 } else {
379 regValue.pMacroTileConfig = amdinfo->gb_macro_tile_mode;
380 regValue.noOfMacroEntries = ARRAY_SIZE(amdinfo->gb_macro_tile_mode);
381 }
382
383 createFlags.useTileIndex = 1;
384 createFlags.useHtileSliceAlign = 1;
385
386 addrCreateInput.chipEngine = CIASICIDGFXENGINE_SOUTHERNISLAND;
387 }
388
389 addrCreateInput.callbacks.allocSysMem = allocSysMem;
390 addrCreateInput.callbacks.freeSysMem = freeSysMem;
391 addrCreateInput.callbacks.debugPrint = 0;
392 addrCreateInput.createFlags = createFlags;
393 addrCreateInput.regValue = regValue;
394
395 addrRet = AddrCreate(&addrCreateInput, &addrCreateOutput);
396 if (addrRet != ADDR_OK)
397 return NULL;
398
399 if (max_alignment) {
400 addrRet = AddrGetMaxAlignments(addrCreateOutput.hLib, &addrGetMaxAlignmentsOutput);
401 if (addrRet == ADDR_OK){
402 *max_alignment = addrGetMaxAlignmentsOutput.baseAlign;
403 }
404 }
405
406 struct ac_addrlib *addrlib = calloc(1, sizeof(struct ac_addrlib));
407 if (!addrlib) {
408 AddrDestroy(addrCreateOutput.hLib);
409 return NULL;
410 }
411
412 addrlib->handle = addrCreateOutput.hLib;
413 simple_mtx_init(&addrlib->dcc_retile_map_lock, mtx_plain);
414 addrlib->dcc_retile_maps = _mesa_hash_table_create(NULL, dcc_retile_map_hash_key,
415 dcc_retile_map_keys_equal);
416 addrlib->dcc_retile_tile_indices = _mesa_hash_table_create(NULL, dcc_retile_tile_hash_key,
417 dcc_retile_tile_keys_equal);
418 return addrlib;
419 }
420
421 void ac_addrlib_destroy(struct ac_addrlib *addrlib)
422 {
423 AddrDestroy(addrlib->handle);
424 simple_mtx_destroy(&addrlib->dcc_retile_map_lock);
425 _mesa_hash_table_destroy(addrlib->dcc_retile_maps, dcc_retile_map_free);
426 _mesa_hash_table_destroy(addrlib->dcc_retile_tile_indices, dcc_retile_tile_free);
427 free(addrlib);
428 }
429
430 static int surf_config_sanity(const struct ac_surf_config *config,
431 unsigned flags)
432 {
433 /* FMASK is allocated together with the color surface and can't be
434 * allocated separately.
435 */
436 assert(!(flags & RADEON_SURF_FMASK));
437 if (flags & RADEON_SURF_FMASK)
438 return -EINVAL;
439
440 /* all dimension must be at least 1 ! */
441 if (!config->info.width || !config->info.height || !config->info.depth ||
442 !config->info.array_size || !config->info.levels)
443 return -EINVAL;
444
445 switch (config->info.samples) {
446 case 0:
447 case 1:
448 case 2:
449 case 4:
450 case 8:
451 break;
452 case 16:
453 if (flags & RADEON_SURF_Z_OR_SBUFFER)
454 return -EINVAL;
455 break;
456 default:
457 return -EINVAL;
458 }
459
460 if (!(flags & RADEON_SURF_Z_OR_SBUFFER)) {
461 switch (config->info.storage_samples) {
462 case 0:
463 case 1:
464 case 2:
465 case 4:
466 case 8:
467 break;
468 default:
469 return -EINVAL;
470 }
471 }
472
473 if (config->is_3d && config->info.array_size > 1)
474 return -EINVAL;
475 if (config->is_cube && config->info.depth > 1)
476 return -EINVAL;
477
478 return 0;
479 }
480
481 static int gfx6_compute_level(ADDR_HANDLE addrlib,
482 const struct ac_surf_config *config,
483 struct radeon_surf *surf, bool is_stencil,
484 unsigned level, bool compressed,
485 ADDR_COMPUTE_SURFACE_INFO_INPUT *AddrSurfInfoIn,
486 ADDR_COMPUTE_SURFACE_INFO_OUTPUT *AddrSurfInfoOut,
487 ADDR_COMPUTE_DCCINFO_INPUT *AddrDccIn,
488 ADDR_COMPUTE_DCCINFO_OUTPUT *AddrDccOut,
489 ADDR_COMPUTE_HTILE_INFO_INPUT *AddrHtileIn,
490 ADDR_COMPUTE_HTILE_INFO_OUTPUT *AddrHtileOut)
491 {
492 struct legacy_surf_level *surf_level;
493 ADDR_E_RETURNCODE ret;
494
495 AddrSurfInfoIn->mipLevel = level;
496 AddrSurfInfoIn->width = u_minify(config->info.width, level);
497 AddrSurfInfoIn->height = u_minify(config->info.height, level);
498
499 /* Make GFX6 linear surfaces compatible with GFX9 for hybrid graphics,
500 * because GFX9 needs linear alignment of 256 bytes.
501 */
502 if (config->info.levels == 1 &&
503 AddrSurfInfoIn->tileMode == ADDR_TM_LINEAR_ALIGNED &&
504 AddrSurfInfoIn->bpp &&
505 util_is_power_of_two_or_zero(AddrSurfInfoIn->bpp)) {
506 unsigned alignment = 256 / (AddrSurfInfoIn->bpp / 8);
507
508 AddrSurfInfoIn->width = align(AddrSurfInfoIn->width, alignment);
509 }
510
511 /* addrlib assumes the bytes/pixel is a divisor of 64, which is not
512 * true for r32g32b32 formats. */
513 if (AddrSurfInfoIn->bpp == 96) {
514 assert(config->info.levels == 1);
515 assert(AddrSurfInfoIn->tileMode == ADDR_TM_LINEAR_ALIGNED);
516
517 /* The least common multiple of 64 bytes and 12 bytes/pixel is
518 * 192 bytes, or 16 pixels. */
519 AddrSurfInfoIn->width = align(AddrSurfInfoIn->width, 16);
520 }
521
522 if (config->is_3d)
523 AddrSurfInfoIn->numSlices = u_minify(config->info.depth, level);
524 else if (config->is_cube)
525 AddrSurfInfoIn->numSlices = 6;
526 else
527 AddrSurfInfoIn->numSlices = config->info.array_size;
528
529 if (level > 0) {
530 /* Set the base level pitch. This is needed for calculation
531 * of non-zero levels. */
532 if (is_stencil)
533 AddrSurfInfoIn->basePitch = surf->u.legacy.stencil_level[0].nblk_x;
534 else
535 AddrSurfInfoIn->basePitch = surf->u.legacy.level[0].nblk_x;
536
537 /* Convert blocks to pixels for compressed formats. */
538 if (compressed)
539 AddrSurfInfoIn->basePitch *= surf->blk_w;
540 }
541
542 ret = AddrComputeSurfaceInfo(addrlib,
543 AddrSurfInfoIn,
544 AddrSurfInfoOut);
545 if (ret != ADDR_OK) {
546 return ret;
547 }
548
549 surf_level = is_stencil ? &surf->u.legacy.stencil_level[level] : &surf->u.legacy.level[level];
550 surf_level->offset = align64(surf->surf_size, AddrSurfInfoOut->baseAlign);
551 surf_level->slice_size_dw = AddrSurfInfoOut->sliceSize / 4;
552 surf_level->nblk_x = AddrSurfInfoOut->pitch;
553 surf_level->nblk_y = AddrSurfInfoOut->height;
554
555 switch (AddrSurfInfoOut->tileMode) {
556 case ADDR_TM_LINEAR_ALIGNED:
557 surf_level->mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
558 break;
559 case ADDR_TM_1D_TILED_THIN1:
560 surf_level->mode = RADEON_SURF_MODE_1D;
561 break;
562 case ADDR_TM_2D_TILED_THIN1:
563 surf_level->mode = RADEON_SURF_MODE_2D;
564 break;
565 default:
566 assert(0);
567 }
568
569 if (is_stencil)
570 surf->u.legacy.stencil_tiling_index[level] = AddrSurfInfoOut->tileIndex;
571 else
572 surf->u.legacy.tiling_index[level] = AddrSurfInfoOut->tileIndex;
573
574 surf->surf_size = surf_level->offset + AddrSurfInfoOut->surfSize;
575
576 /* Clear DCC fields at the beginning. */
577 surf_level->dcc_offset = 0;
578
579 /* The previous level's flag tells us if we can use DCC for this level. */
580 if (AddrSurfInfoIn->flags.dccCompatible &&
581 (level == 0 || AddrDccOut->subLvlCompressible)) {
582 bool prev_level_clearable = level == 0 ||
583 AddrDccOut->dccRamSizeAligned;
584
585 AddrDccIn->colorSurfSize = AddrSurfInfoOut->surfSize;
586 AddrDccIn->tileMode = AddrSurfInfoOut->tileMode;
587 AddrDccIn->tileInfo = *AddrSurfInfoOut->pTileInfo;
588 AddrDccIn->tileIndex = AddrSurfInfoOut->tileIndex;
589 AddrDccIn->macroModeIndex = AddrSurfInfoOut->macroModeIndex;
590
591 ret = AddrComputeDccInfo(addrlib,
592 AddrDccIn,
593 AddrDccOut);
594
595 if (ret == ADDR_OK) {
596 surf_level->dcc_offset = surf->dcc_size;
597 surf->num_dcc_levels = level + 1;
598 surf->dcc_size = surf_level->dcc_offset + AddrDccOut->dccRamSize;
599 surf->dcc_alignment = MAX2(surf->dcc_alignment, AddrDccOut->dccRamBaseAlign);
600
601 /* If the DCC size of a subresource (1 mip level or 1 slice)
602 * is not aligned, the DCC memory layout is not contiguous for
603 * that subresource, which means we can't use fast clear.
604 *
605 * We only do fast clears for whole mipmap levels. If we did
606 * per-slice fast clears, the same restriction would apply.
607 * (i.e. only compute the slice size and see if it's aligned)
608 *
609 * The last level can be non-contiguous and still be clearable
610 * if it's interleaved with the next level that doesn't exist.
611 */
612 if (AddrDccOut->dccRamSizeAligned ||
613 (prev_level_clearable && level == config->info.levels - 1))
614 surf_level->dcc_fast_clear_size = AddrDccOut->dccFastClearSize;
615 else
616 surf_level->dcc_fast_clear_size = 0;
617
618 /* Compute the DCC slice size because addrlib doesn't
619 * provide this info. As DCC memory is linear (each
620 * slice is the same size) it's easy to compute.
621 */
622 surf->dcc_slice_size = AddrDccOut->dccRamSize / config->info.array_size;
623
624 /* For arrays, we have to compute the DCC info again
625 * with one slice size to get a correct fast clear
626 * size.
627 */
628 if (config->info.array_size > 1) {
629 AddrDccIn->colorSurfSize = AddrSurfInfoOut->sliceSize;
630 AddrDccIn->tileMode = AddrSurfInfoOut->tileMode;
631 AddrDccIn->tileInfo = *AddrSurfInfoOut->pTileInfo;
632 AddrDccIn->tileIndex = AddrSurfInfoOut->tileIndex;
633 AddrDccIn->macroModeIndex = AddrSurfInfoOut->macroModeIndex;
634
635 ret = AddrComputeDccInfo(addrlib,
636 AddrDccIn, AddrDccOut);
637 if (ret == ADDR_OK) {
638 /* If the DCC memory isn't properly
639 * aligned, the data are interleaved
640 * accross slices.
641 */
642 if (AddrDccOut->dccRamSizeAligned)
643 surf_level->dcc_slice_fast_clear_size = AddrDccOut->dccFastClearSize;
644 else
645 surf_level->dcc_slice_fast_clear_size = 0;
646 }
647
648 if (surf->flags & RADEON_SURF_CONTIGUOUS_DCC_LAYERS &&
649 surf->dcc_slice_size != surf_level->dcc_slice_fast_clear_size) {
650 surf->dcc_size = 0;
651 surf->num_dcc_levels = 0;
652 AddrDccOut->subLvlCompressible = false;
653 }
654 } else {
655 surf_level->dcc_slice_fast_clear_size = surf_level->dcc_fast_clear_size;
656 }
657 }
658 }
659
660 /* HTILE. */
661 if (!is_stencil &&
662 AddrSurfInfoIn->flags.depth &&
663 surf_level->mode == RADEON_SURF_MODE_2D &&
664 level == 0 &&
665 !(surf->flags & RADEON_SURF_NO_HTILE)) {
666 AddrHtileIn->flags.tcCompatible = AddrSurfInfoOut->tcCompatible;
667 AddrHtileIn->pitch = AddrSurfInfoOut->pitch;
668 AddrHtileIn->height = AddrSurfInfoOut->height;
669 AddrHtileIn->numSlices = AddrSurfInfoOut->depth;
670 AddrHtileIn->blockWidth = ADDR_HTILE_BLOCKSIZE_8;
671 AddrHtileIn->blockHeight = ADDR_HTILE_BLOCKSIZE_8;
672 AddrHtileIn->pTileInfo = AddrSurfInfoOut->pTileInfo;
673 AddrHtileIn->tileIndex = AddrSurfInfoOut->tileIndex;
674 AddrHtileIn->macroModeIndex = AddrSurfInfoOut->macroModeIndex;
675
676 ret = AddrComputeHtileInfo(addrlib,
677 AddrHtileIn,
678 AddrHtileOut);
679
680 if (ret == ADDR_OK) {
681 surf->htile_size = AddrHtileOut->htileBytes;
682 surf->htile_slice_size = AddrHtileOut->sliceSize;
683 surf->htile_alignment = AddrHtileOut->baseAlign;
684 }
685 }
686
687 return 0;
688 }
689
690 static void gfx6_set_micro_tile_mode(struct radeon_surf *surf,
691 const struct radeon_info *info)
692 {
693 uint32_t tile_mode = info->si_tile_mode_array[surf->u.legacy.tiling_index[0]];
694
695 if (info->chip_class >= GFX7)
696 surf->micro_tile_mode = G_009910_MICRO_TILE_MODE_NEW(tile_mode);
697 else
698 surf->micro_tile_mode = G_009910_MICRO_TILE_MODE(tile_mode);
699 }
700
701 static unsigned cik_get_macro_tile_index(struct radeon_surf *surf)
702 {
703 unsigned index, tileb;
704
705 tileb = 8 * 8 * surf->bpe;
706 tileb = MIN2(surf->u.legacy.tile_split, tileb);
707
708 for (index = 0; tileb > 64; index++)
709 tileb >>= 1;
710
711 assert(index < 16);
712 return index;
713 }
714
715 static bool get_display_flag(const struct ac_surf_config *config,
716 const struct radeon_surf *surf)
717 {
718 unsigned num_channels = config->info.num_channels;
719 unsigned bpe = surf->bpe;
720
721 if (!config->is_3d &&
722 !config->is_cube &&
723 !(surf->flags & RADEON_SURF_Z_OR_SBUFFER) &&
724 surf->flags & RADEON_SURF_SCANOUT &&
725 config->info.samples <= 1 &&
726 surf->blk_w <= 2 && surf->blk_h == 1) {
727 /* subsampled */
728 if (surf->blk_w == 2 && surf->blk_h == 1)
729 return true;
730
731 if (/* RGBA8 or RGBA16F */
732 (bpe >= 4 && bpe <= 8 && num_channels == 4) ||
733 /* R5G6B5 or R5G5B5A1 */
734 (bpe == 2 && num_channels >= 3) ||
735 /* C8 palette */
736 (bpe == 1 && num_channels == 1))
737 return true;
738 }
739 return false;
740 }
741
742 /**
743 * This must be called after the first level is computed.
744 *
745 * Copy surface-global settings like pipe/bank config from level 0 surface
746 * computation, and compute tile swizzle.
747 */
748 static int gfx6_surface_settings(ADDR_HANDLE addrlib,
749 const struct radeon_info *info,
750 const struct ac_surf_config *config,
751 ADDR_COMPUTE_SURFACE_INFO_OUTPUT* csio,
752 struct radeon_surf *surf)
753 {
754 surf->surf_alignment = csio->baseAlign;
755 surf->u.legacy.pipe_config = csio->pTileInfo->pipeConfig - 1;
756 gfx6_set_micro_tile_mode(surf, info);
757
758 /* For 2D modes only. */
759 if (csio->tileMode >= ADDR_TM_2D_TILED_THIN1) {
760 surf->u.legacy.bankw = csio->pTileInfo->bankWidth;
761 surf->u.legacy.bankh = csio->pTileInfo->bankHeight;
762 surf->u.legacy.mtilea = csio->pTileInfo->macroAspectRatio;
763 surf->u.legacy.tile_split = csio->pTileInfo->tileSplitBytes;
764 surf->u.legacy.num_banks = csio->pTileInfo->banks;
765 surf->u.legacy.macro_tile_index = csio->macroModeIndex;
766 } else {
767 surf->u.legacy.macro_tile_index = 0;
768 }
769
770 /* Compute tile swizzle. */
771 /* TODO: fix tile swizzle with mipmapping for GFX6 */
772 if ((info->chip_class >= GFX7 || config->info.levels == 1) &&
773 config->info.surf_index &&
774 surf->u.legacy.level[0].mode == RADEON_SURF_MODE_2D &&
775 !(surf->flags & (RADEON_SURF_Z_OR_SBUFFER | RADEON_SURF_SHAREABLE)) &&
776 !get_display_flag(config, surf)) {
777 ADDR_COMPUTE_BASE_SWIZZLE_INPUT AddrBaseSwizzleIn = {0};
778 ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT AddrBaseSwizzleOut = {0};
779
780 AddrBaseSwizzleIn.size = sizeof(ADDR_COMPUTE_BASE_SWIZZLE_INPUT);
781 AddrBaseSwizzleOut.size = sizeof(ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT);
782
783 AddrBaseSwizzleIn.surfIndex = p_atomic_inc_return(config->info.surf_index) - 1;
784 AddrBaseSwizzleIn.tileIndex = csio->tileIndex;
785 AddrBaseSwizzleIn.macroModeIndex = csio->macroModeIndex;
786 AddrBaseSwizzleIn.pTileInfo = csio->pTileInfo;
787 AddrBaseSwizzleIn.tileMode = csio->tileMode;
788
789 int r = AddrComputeBaseSwizzle(addrlib, &AddrBaseSwizzleIn,
790 &AddrBaseSwizzleOut);
791 if (r != ADDR_OK)
792 return r;
793
794 assert(AddrBaseSwizzleOut.tileSwizzle <=
795 u_bit_consecutive(0, sizeof(surf->tile_swizzle) * 8));
796 surf->tile_swizzle = AddrBaseSwizzleOut.tileSwizzle;
797 }
798 return 0;
799 }
800
801 static void ac_compute_cmask(const struct radeon_info *info,
802 const struct ac_surf_config *config,
803 struct radeon_surf *surf)
804 {
805 unsigned pipe_interleave_bytes = info->pipe_interleave_bytes;
806 unsigned num_pipes = info->num_tile_pipes;
807 unsigned cl_width, cl_height;
808
809 if (surf->flags & RADEON_SURF_Z_OR_SBUFFER || surf->is_linear ||
810 (config->info.samples >= 2 && !surf->fmask_size))
811 return;
812
813 assert(info->chip_class <= GFX8);
814
815 switch (num_pipes) {
816 case 2:
817 cl_width = 32;
818 cl_height = 16;
819 break;
820 case 4:
821 cl_width = 32;
822 cl_height = 32;
823 break;
824 case 8:
825 cl_width = 64;
826 cl_height = 32;
827 break;
828 case 16: /* Hawaii */
829 cl_width = 64;
830 cl_height = 64;
831 break;
832 default:
833 assert(0);
834 return;
835 }
836
837 unsigned base_align = num_pipes * pipe_interleave_bytes;
838
839 unsigned width = align(surf->u.legacy.level[0].nblk_x, cl_width*8);
840 unsigned height = align(surf->u.legacy.level[0].nblk_y, cl_height*8);
841 unsigned slice_elements = (width * height) / (8*8);
842
843 /* Each element of CMASK is a nibble. */
844 unsigned slice_bytes = slice_elements / 2;
845
846 surf->u.legacy.cmask_slice_tile_max = (width * height) / (128*128);
847 if (surf->u.legacy.cmask_slice_tile_max)
848 surf->u.legacy.cmask_slice_tile_max -= 1;
849
850 unsigned num_layers;
851 if (config->is_3d)
852 num_layers = config->info.depth;
853 else if (config->is_cube)
854 num_layers = 6;
855 else
856 num_layers = config->info.array_size;
857
858 surf->cmask_alignment = MAX2(256, base_align);
859 surf->cmask_slice_size = align(slice_bytes, base_align);
860 surf->cmask_size = surf->cmask_slice_size * num_layers;
861 }
862
863 /**
864 * Fill in the tiling information in \p surf based on the given surface config.
865 *
866 * The following fields of \p surf must be initialized by the caller:
867 * blk_w, blk_h, bpe, flags.
868 */
869 static int gfx6_compute_surface(ADDR_HANDLE addrlib,
870 const struct radeon_info *info,
871 const struct ac_surf_config *config,
872 enum radeon_surf_mode mode,
873 struct radeon_surf *surf)
874 {
875 unsigned level;
876 bool compressed;
877 ADDR_COMPUTE_SURFACE_INFO_INPUT AddrSurfInfoIn = {0};
878 ADDR_COMPUTE_SURFACE_INFO_OUTPUT AddrSurfInfoOut = {0};
879 ADDR_COMPUTE_DCCINFO_INPUT AddrDccIn = {0};
880 ADDR_COMPUTE_DCCINFO_OUTPUT AddrDccOut = {0};
881 ADDR_COMPUTE_HTILE_INFO_INPUT AddrHtileIn = {0};
882 ADDR_COMPUTE_HTILE_INFO_OUTPUT AddrHtileOut = {0};
883 ADDR_TILEINFO AddrTileInfoIn = {0};
884 ADDR_TILEINFO AddrTileInfoOut = {0};
885 int r;
886
887 AddrSurfInfoIn.size = sizeof(ADDR_COMPUTE_SURFACE_INFO_INPUT);
888 AddrSurfInfoOut.size = sizeof(ADDR_COMPUTE_SURFACE_INFO_OUTPUT);
889 AddrDccIn.size = sizeof(ADDR_COMPUTE_DCCINFO_INPUT);
890 AddrDccOut.size = sizeof(ADDR_COMPUTE_DCCINFO_OUTPUT);
891 AddrHtileIn.size = sizeof(ADDR_COMPUTE_HTILE_INFO_INPUT);
892 AddrHtileOut.size = sizeof(ADDR_COMPUTE_HTILE_INFO_OUTPUT);
893 AddrSurfInfoOut.pTileInfo = &AddrTileInfoOut;
894
895 compressed = surf->blk_w == 4 && surf->blk_h == 4;
896
897 /* MSAA requires 2D tiling. */
898 if (config->info.samples > 1)
899 mode = RADEON_SURF_MODE_2D;
900
901 /* DB doesn't support linear layouts. */
902 if (surf->flags & (RADEON_SURF_Z_OR_SBUFFER) &&
903 mode < RADEON_SURF_MODE_1D)
904 mode = RADEON_SURF_MODE_1D;
905
906 /* Set the requested tiling mode. */
907 switch (mode) {
908 case RADEON_SURF_MODE_LINEAR_ALIGNED:
909 AddrSurfInfoIn.tileMode = ADDR_TM_LINEAR_ALIGNED;
910 break;
911 case RADEON_SURF_MODE_1D:
912 AddrSurfInfoIn.tileMode = ADDR_TM_1D_TILED_THIN1;
913 break;
914 case RADEON_SURF_MODE_2D:
915 AddrSurfInfoIn.tileMode = ADDR_TM_2D_TILED_THIN1;
916 break;
917 default:
918 assert(0);
919 }
920
921 /* The format must be set correctly for the allocation of compressed
922 * textures to work. In other cases, setting the bpp is sufficient.
923 */
924 if (compressed) {
925 switch (surf->bpe) {
926 case 8:
927 AddrSurfInfoIn.format = ADDR_FMT_BC1;
928 break;
929 case 16:
930 AddrSurfInfoIn.format = ADDR_FMT_BC3;
931 break;
932 default:
933 assert(0);
934 }
935 }
936 else {
937 AddrDccIn.bpp = AddrSurfInfoIn.bpp = surf->bpe * 8;
938 }
939
940 AddrDccIn.numSamples = AddrSurfInfoIn.numSamples =
941 MAX2(1, config->info.samples);
942 AddrSurfInfoIn.tileIndex = -1;
943
944 if (!(surf->flags & RADEON_SURF_Z_OR_SBUFFER)) {
945 AddrDccIn.numSamples = AddrSurfInfoIn.numFrags =
946 MAX2(1, config->info.storage_samples);
947 }
948
949 /* Set the micro tile type. */
950 if (surf->flags & RADEON_SURF_SCANOUT)
951 AddrSurfInfoIn.tileType = ADDR_DISPLAYABLE;
952 else if (surf->flags & RADEON_SURF_Z_OR_SBUFFER)
953 AddrSurfInfoIn.tileType = ADDR_DEPTH_SAMPLE_ORDER;
954 else
955 AddrSurfInfoIn.tileType = ADDR_NON_DISPLAYABLE;
956
957 AddrSurfInfoIn.flags.color = !(surf->flags & RADEON_SURF_Z_OR_SBUFFER);
958 AddrSurfInfoIn.flags.depth = (surf->flags & RADEON_SURF_ZBUFFER) != 0;
959 AddrSurfInfoIn.flags.cube = config->is_cube;
960 AddrSurfInfoIn.flags.display = get_display_flag(config, surf);
961 AddrSurfInfoIn.flags.pow2Pad = config->info.levels > 1;
962 AddrSurfInfoIn.flags.tcCompatible = (surf->flags & RADEON_SURF_TC_COMPATIBLE_HTILE) != 0;
963
964 /* Only degrade the tile mode for space if TC-compatible HTILE hasn't been
965 * requested, because TC-compatible HTILE requires 2D tiling.
966 */
967 AddrSurfInfoIn.flags.opt4Space = !AddrSurfInfoIn.flags.tcCompatible &&
968 !AddrSurfInfoIn.flags.fmask &&
969 config->info.samples <= 1 &&
970 !(surf->flags & RADEON_SURF_FORCE_SWIZZLE_MODE);
971
972 /* DCC notes:
973 * - If we add MSAA support, keep in mind that CB can't decompress 8bpp
974 * with samples >= 4.
975 * - Mipmapped array textures have low performance (discovered by a closed
976 * driver team).
977 */
978 AddrSurfInfoIn.flags.dccCompatible =
979 info->chip_class >= GFX8 &&
980 info->has_graphics && /* disable DCC on compute-only chips */
981 !(surf->flags & RADEON_SURF_Z_OR_SBUFFER) &&
982 !(surf->flags & RADEON_SURF_DISABLE_DCC) &&
983 !compressed &&
984 ((config->info.array_size == 1 && config->info.depth == 1) ||
985 config->info.levels == 1);
986
987 AddrSurfInfoIn.flags.noStencil = (surf->flags & RADEON_SURF_SBUFFER) == 0;
988 AddrSurfInfoIn.flags.compressZ = !!(surf->flags & RADEON_SURF_Z_OR_SBUFFER);
989
990 /* On GFX7-GFX8, the DB uses the same pitch and tile mode (except tilesplit)
991 * for Z and stencil. This can cause a number of problems which we work
992 * around here:
993 *
994 * - a depth part that is incompatible with mipmapped texturing
995 * - at least on Stoney, entirely incompatible Z/S aspects (e.g.
996 * incorrect tiling applied to the stencil part, stencil buffer
997 * memory accesses that go out of bounds) even without mipmapping
998 *
999 * Some piglit tests that are prone to different types of related
1000 * failures:
1001 * ./bin/ext_framebuffer_multisample-upsample 2 stencil
1002 * ./bin/framebuffer-blit-levels {draw,read} stencil
1003 * ./bin/ext_framebuffer_multisample-unaligned-blit N {depth,stencil} {msaa,upsample,downsample}
1004 * ./bin/fbo-depth-array fs-writes-{depth,stencil} / {depth,stencil}-{clear,layered-clear,draw}
1005 * ./bin/depthstencil-render-miplevels 1024 d=s=z24_s8
1006 */
1007 int stencil_tile_idx = -1;
1008
1009 if (AddrSurfInfoIn.flags.depth && !AddrSurfInfoIn.flags.noStencil &&
1010 (config->info.levels > 1 || info->family == CHIP_STONEY)) {
1011 /* Compute stencilTileIdx that is compatible with the (depth)
1012 * tileIdx. This degrades the depth surface if necessary to
1013 * ensure that a matching stencilTileIdx exists. */
1014 AddrSurfInfoIn.flags.matchStencilTileCfg = 1;
1015
1016 /* Keep the depth mip-tail compatible with texturing. */
1017 AddrSurfInfoIn.flags.noStencil = 1;
1018 }
1019
1020 /* Set preferred macrotile parameters. This is usually required
1021 * for shared resources. This is for 2D tiling only. */
1022 if (AddrSurfInfoIn.tileMode >= ADDR_TM_2D_TILED_THIN1 &&
1023 surf->u.legacy.bankw && surf->u.legacy.bankh &&
1024 surf->u.legacy.mtilea && surf->u.legacy.tile_split) {
1025 /* If any of these parameters are incorrect, the calculation
1026 * will fail. */
1027 AddrTileInfoIn.banks = surf->u.legacy.num_banks;
1028 AddrTileInfoIn.bankWidth = surf->u.legacy.bankw;
1029 AddrTileInfoIn.bankHeight = surf->u.legacy.bankh;
1030 AddrTileInfoIn.macroAspectRatio = surf->u.legacy.mtilea;
1031 AddrTileInfoIn.tileSplitBytes = surf->u.legacy.tile_split;
1032 AddrTileInfoIn.pipeConfig = surf->u.legacy.pipe_config + 1; /* +1 compared to GB_TILE_MODE */
1033 AddrSurfInfoIn.flags.opt4Space = 0;
1034 AddrSurfInfoIn.pTileInfo = &AddrTileInfoIn;
1035
1036 /* If AddrSurfInfoIn.pTileInfo is set, Addrlib doesn't set
1037 * the tile index, because we are expected to know it if
1038 * we know the other parameters.
1039 *
1040 * This is something that can easily be fixed in Addrlib.
1041 * For now, just figure it out here.
1042 * Note that only 2D_TILE_THIN1 is handled here.
1043 */
1044 assert(!(surf->flags & RADEON_SURF_Z_OR_SBUFFER));
1045 assert(AddrSurfInfoIn.tileMode == ADDR_TM_2D_TILED_THIN1);
1046
1047 if (info->chip_class == GFX6) {
1048 if (AddrSurfInfoIn.tileType == ADDR_DISPLAYABLE) {
1049 if (surf->bpe == 2)
1050 AddrSurfInfoIn.tileIndex = 11; /* 16bpp */
1051 else
1052 AddrSurfInfoIn.tileIndex = 12; /* 32bpp */
1053 } else {
1054 if (surf->bpe == 1)
1055 AddrSurfInfoIn.tileIndex = 14; /* 8bpp */
1056 else if (surf->bpe == 2)
1057 AddrSurfInfoIn.tileIndex = 15; /* 16bpp */
1058 else if (surf->bpe == 4)
1059 AddrSurfInfoIn.tileIndex = 16; /* 32bpp */
1060 else
1061 AddrSurfInfoIn.tileIndex = 17; /* 64bpp (and 128bpp) */
1062 }
1063 } else {
1064 /* GFX7 - GFX8 */
1065 if (AddrSurfInfoIn.tileType == ADDR_DISPLAYABLE)
1066 AddrSurfInfoIn.tileIndex = 10; /* 2D displayable */
1067 else
1068 AddrSurfInfoIn.tileIndex = 14; /* 2D non-displayable */
1069
1070 /* Addrlib doesn't set this if tileIndex is forced like above. */
1071 AddrSurfInfoOut.macroModeIndex = cik_get_macro_tile_index(surf);
1072 }
1073 }
1074
1075 surf->has_stencil = !!(surf->flags & RADEON_SURF_SBUFFER);
1076 surf->num_dcc_levels = 0;
1077 surf->surf_size = 0;
1078 surf->dcc_size = 0;
1079 surf->dcc_alignment = 1;
1080 surf->htile_size = 0;
1081 surf->htile_slice_size = 0;
1082 surf->htile_alignment = 1;
1083
1084 const bool only_stencil = (surf->flags & RADEON_SURF_SBUFFER) &&
1085 !(surf->flags & RADEON_SURF_ZBUFFER);
1086
1087 /* Calculate texture layout information. */
1088 if (!only_stencil) {
1089 for (level = 0; level < config->info.levels; level++) {
1090 r = gfx6_compute_level(addrlib, config, surf, false, level, compressed,
1091 &AddrSurfInfoIn, &AddrSurfInfoOut,
1092 &AddrDccIn, &AddrDccOut, &AddrHtileIn, &AddrHtileOut);
1093 if (r)
1094 return r;
1095
1096 if (level > 0)
1097 continue;
1098
1099 if (!AddrSurfInfoOut.tcCompatible) {
1100 AddrSurfInfoIn.flags.tcCompatible = 0;
1101 surf->flags &= ~RADEON_SURF_TC_COMPATIBLE_HTILE;
1102 }
1103
1104 if (AddrSurfInfoIn.flags.matchStencilTileCfg) {
1105 AddrSurfInfoIn.flags.matchStencilTileCfg = 0;
1106 AddrSurfInfoIn.tileIndex = AddrSurfInfoOut.tileIndex;
1107 stencil_tile_idx = AddrSurfInfoOut.stencilTileIdx;
1108
1109 assert(stencil_tile_idx >= 0);
1110 }
1111
1112 r = gfx6_surface_settings(addrlib, info, config,
1113 &AddrSurfInfoOut, surf);
1114 if (r)
1115 return r;
1116 }
1117 }
1118
1119 /* Calculate texture layout information for stencil. */
1120 if (surf->flags & RADEON_SURF_SBUFFER) {
1121 AddrSurfInfoIn.tileIndex = stencil_tile_idx;
1122 AddrSurfInfoIn.bpp = 8;
1123 AddrSurfInfoIn.flags.depth = 0;
1124 AddrSurfInfoIn.flags.stencil = 1;
1125 AddrSurfInfoIn.flags.tcCompatible = 0;
1126 /* This will be ignored if AddrSurfInfoIn.pTileInfo is NULL. */
1127 AddrTileInfoIn.tileSplitBytes = surf->u.legacy.stencil_tile_split;
1128
1129 for (level = 0; level < config->info.levels; level++) {
1130 r = gfx6_compute_level(addrlib, config, surf, true, level, compressed,
1131 &AddrSurfInfoIn, &AddrSurfInfoOut,
1132 &AddrDccIn, &AddrDccOut,
1133 NULL, NULL);
1134 if (r)
1135 return r;
1136
1137 /* DB uses the depth pitch for both stencil and depth. */
1138 if (!only_stencil) {
1139 if (surf->u.legacy.stencil_level[level].nblk_x !=
1140 surf->u.legacy.level[level].nblk_x)
1141 surf->u.legacy.stencil_adjusted = true;
1142 } else {
1143 surf->u.legacy.level[level].nblk_x =
1144 surf->u.legacy.stencil_level[level].nblk_x;
1145 }
1146
1147 if (level == 0) {
1148 if (only_stencil) {
1149 r = gfx6_surface_settings(addrlib, info, config,
1150 &AddrSurfInfoOut, surf);
1151 if (r)
1152 return r;
1153 }
1154
1155 /* For 2D modes only. */
1156 if (AddrSurfInfoOut.tileMode >= ADDR_TM_2D_TILED_THIN1) {
1157 surf->u.legacy.stencil_tile_split =
1158 AddrSurfInfoOut.pTileInfo->tileSplitBytes;
1159 }
1160 }
1161 }
1162 }
1163
1164 /* Compute FMASK. */
1165 if (config->info.samples >= 2 && AddrSurfInfoIn.flags.color &&
1166 info->has_graphics && !(surf->flags & RADEON_SURF_NO_FMASK)) {
1167 ADDR_COMPUTE_FMASK_INFO_INPUT fin = {0};
1168 ADDR_COMPUTE_FMASK_INFO_OUTPUT fout = {0};
1169 ADDR_TILEINFO fmask_tile_info = {};
1170
1171 fin.size = sizeof(fin);
1172 fout.size = sizeof(fout);
1173
1174 fin.tileMode = AddrSurfInfoOut.tileMode;
1175 fin.pitch = AddrSurfInfoOut.pitch;
1176 fin.height = config->info.height;
1177 fin.numSlices = AddrSurfInfoIn.numSlices;
1178 fin.numSamples = AddrSurfInfoIn.numSamples;
1179 fin.numFrags = AddrSurfInfoIn.numFrags;
1180 fin.tileIndex = -1;
1181 fout.pTileInfo = &fmask_tile_info;
1182
1183 r = AddrComputeFmaskInfo(addrlib, &fin, &fout);
1184 if (r)
1185 return r;
1186
1187 surf->fmask_size = fout.fmaskBytes;
1188 surf->fmask_alignment = fout.baseAlign;
1189 surf->fmask_tile_swizzle = 0;
1190
1191 surf->u.legacy.fmask.slice_tile_max =
1192 (fout.pitch * fout.height) / 64;
1193 if (surf->u.legacy.fmask.slice_tile_max)
1194 surf->u.legacy.fmask.slice_tile_max -= 1;
1195
1196 surf->u.legacy.fmask.tiling_index = fout.tileIndex;
1197 surf->u.legacy.fmask.bankh = fout.pTileInfo->bankHeight;
1198 surf->u.legacy.fmask.pitch_in_pixels = fout.pitch;
1199 surf->u.legacy.fmask.slice_size = fout.sliceSize;
1200
1201 /* Compute tile swizzle for FMASK. */
1202 if (config->info.fmask_surf_index &&
1203 !(surf->flags & RADEON_SURF_SHAREABLE)) {
1204 ADDR_COMPUTE_BASE_SWIZZLE_INPUT xin = {0};
1205 ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT xout = {0};
1206
1207 xin.size = sizeof(ADDR_COMPUTE_BASE_SWIZZLE_INPUT);
1208 xout.size = sizeof(ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT);
1209
1210 /* This counter starts from 1 instead of 0. */
1211 xin.surfIndex = p_atomic_inc_return(config->info.fmask_surf_index);
1212 xin.tileIndex = fout.tileIndex;
1213 xin.macroModeIndex = fout.macroModeIndex;
1214 xin.pTileInfo = fout.pTileInfo;
1215 xin.tileMode = fin.tileMode;
1216
1217 int r = AddrComputeBaseSwizzle(addrlib, &xin, &xout);
1218 if (r != ADDR_OK)
1219 return r;
1220
1221 assert(xout.tileSwizzle <=
1222 u_bit_consecutive(0, sizeof(surf->tile_swizzle) * 8));
1223 surf->fmask_tile_swizzle = xout.tileSwizzle;
1224 }
1225 }
1226
1227 /* Recalculate the whole DCC miptree size including disabled levels.
1228 * This is what addrlib does, but calling addrlib would be a lot more
1229 * complicated.
1230 */
1231 if (surf->dcc_size && config->info.levels > 1) {
1232 /* The smallest miplevels that are never compressed by DCC
1233 * still read the DCC buffer via TC if the base level uses DCC,
1234 * and for some reason the DCC buffer needs to be larger if
1235 * the miptree uses non-zero tile_swizzle. Otherwise there are
1236 * VM faults.
1237 *
1238 * "dcc_alignment * 4" was determined by trial and error.
1239 */
1240 surf->dcc_size = align64(surf->surf_size >> 8,
1241 surf->dcc_alignment * 4);
1242 }
1243
1244 /* Make sure HTILE covers the whole miptree, because the shader reads
1245 * TC-compatible HTILE even for levels where it's disabled by DB.
1246 */
1247 if (surf->htile_size && config->info.levels > 1 &&
1248 surf->flags & RADEON_SURF_TC_COMPATIBLE_HTILE) {
1249 /* MSAA can't occur with levels > 1, so ignore the sample count. */
1250 const unsigned total_pixels = surf->surf_size / surf->bpe;
1251 const unsigned htile_block_size = 8 * 8;
1252 const unsigned htile_element_size = 4;
1253
1254 surf->htile_size = (total_pixels / htile_block_size) *
1255 htile_element_size;
1256 surf->htile_size = align(surf->htile_size, surf->htile_alignment);
1257 } else if (!surf->htile_size) {
1258 /* Unset this if HTILE is not present. */
1259 surf->flags &= ~RADEON_SURF_TC_COMPATIBLE_HTILE;
1260 }
1261
1262 surf->is_linear = surf->u.legacy.level[0].mode == RADEON_SURF_MODE_LINEAR_ALIGNED;
1263 surf->is_displayable = surf->is_linear ||
1264 surf->micro_tile_mode == RADEON_MICRO_MODE_DISPLAY ||
1265 surf->micro_tile_mode == RADEON_MICRO_MODE_RENDER;
1266
1267 /* The rotated micro tile mode doesn't work if both CMASK and RB+ are
1268 * used at the same time. This case is not currently expected to occur
1269 * because we don't use rotated. Enforce this restriction on all chips
1270 * to facilitate testing.
1271 */
1272 if (surf->micro_tile_mode == RADEON_MICRO_MODE_RENDER) {
1273 assert(!"rotate micro tile mode is unsupported");
1274 return ADDR_ERROR;
1275 }
1276
1277 ac_compute_cmask(info, config, surf);
1278 return 0;
1279 }
1280
1281 /* This is only called when expecting a tiled layout. */
1282 static int
1283 gfx9_get_preferred_swizzle_mode(ADDR_HANDLE addrlib,
1284 struct radeon_surf *surf,
1285 ADDR2_COMPUTE_SURFACE_INFO_INPUT *in,
1286 bool is_fmask, AddrSwizzleMode *swizzle_mode)
1287 {
1288 ADDR_E_RETURNCODE ret;
1289 ADDR2_GET_PREFERRED_SURF_SETTING_INPUT sin = {0};
1290 ADDR2_GET_PREFERRED_SURF_SETTING_OUTPUT sout = {0};
1291
1292 sin.size = sizeof(ADDR2_GET_PREFERRED_SURF_SETTING_INPUT);
1293 sout.size = sizeof(ADDR2_GET_PREFERRED_SURF_SETTING_OUTPUT);
1294
1295 sin.flags = in->flags;
1296 sin.resourceType = in->resourceType;
1297 sin.format = in->format;
1298 sin.resourceLoction = ADDR_RSRC_LOC_INVIS;
1299 /* TODO: We could allow some of these: */
1300 sin.forbiddenBlock.micro = 1; /* don't allow the 256B swizzle modes */
1301 sin.forbiddenBlock.var = 1; /* don't allow the variable-sized swizzle modes */
1302 sin.bpp = in->bpp;
1303 sin.width = in->width;
1304 sin.height = in->height;
1305 sin.numSlices = in->numSlices;
1306 sin.numMipLevels = in->numMipLevels;
1307 sin.numSamples = in->numSamples;
1308 sin.numFrags = in->numFrags;
1309
1310 if (is_fmask) {
1311 sin.flags.display = 0;
1312 sin.flags.color = 0;
1313 sin.flags.fmask = 1;
1314 }
1315
1316 if (surf->flags & RADEON_SURF_FORCE_MICRO_TILE_MODE) {
1317 sin.forbiddenBlock.linear = 1;
1318
1319 if (surf->micro_tile_mode == RADEON_MICRO_MODE_DISPLAY)
1320 sin.preferredSwSet.sw_D = 1;
1321 else if (surf->micro_tile_mode == RADEON_MICRO_MODE_STANDARD)
1322 sin.preferredSwSet.sw_S = 1;
1323 else if (surf->micro_tile_mode == RADEON_MICRO_MODE_DEPTH)
1324 sin.preferredSwSet.sw_Z = 1;
1325 else if (surf->micro_tile_mode == RADEON_MICRO_MODE_RENDER)
1326 sin.preferredSwSet.sw_R = 1;
1327 }
1328
1329 ret = Addr2GetPreferredSurfaceSetting(addrlib, &sin, &sout);
1330 if (ret != ADDR_OK)
1331 return ret;
1332
1333 *swizzle_mode = sout.swizzleMode;
1334 return 0;
1335 }
1336
1337 static bool is_dcc_supported_by_CB(const struct radeon_info *info, unsigned sw_mode)
1338 {
1339 if (info->chip_class >= GFX10)
1340 return sw_mode == ADDR_SW_64KB_Z_X || sw_mode == ADDR_SW_64KB_R_X;
1341
1342 return sw_mode != ADDR_SW_LINEAR;
1343 }
1344
1345 ASSERTED static bool is_dcc_supported_by_L2(const struct radeon_info *info,
1346 const struct radeon_surf *surf)
1347 {
1348 if (info->chip_class <= GFX9) {
1349 /* Only independent 64B blocks are supported. */
1350 return surf->u.gfx9.dcc.independent_64B_blocks &&
1351 !surf->u.gfx9.dcc.independent_128B_blocks &&
1352 surf->u.gfx9.dcc.max_compressed_block_size == V_028C78_MAX_BLOCK_SIZE_64B;
1353 }
1354
1355 if (info->family == CHIP_NAVI10) {
1356 /* Only independent 128B blocks are supported. */
1357 return !surf->u.gfx9.dcc.independent_64B_blocks &&
1358 surf->u.gfx9.dcc.independent_128B_blocks &&
1359 surf->u.gfx9.dcc.max_compressed_block_size <= V_028C78_MAX_BLOCK_SIZE_128B;
1360 }
1361
1362 if (info->family == CHIP_NAVI12 ||
1363 info->family == CHIP_NAVI14) {
1364 /* Either 64B or 128B can be used, but not both.
1365 * If 64B is used, DCC image stores are unsupported.
1366 */
1367 return surf->u.gfx9.dcc.independent_64B_blocks !=
1368 surf->u.gfx9.dcc.independent_128B_blocks &&
1369 (!surf->u.gfx9.dcc.independent_64B_blocks ||
1370 surf->u.gfx9.dcc.max_compressed_block_size == V_028C78_MAX_BLOCK_SIZE_64B) &&
1371 (!surf->u.gfx9.dcc.independent_128B_blocks ||
1372 surf->u.gfx9.dcc.max_compressed_block_size <= V_028C78_MAX_BLOCK_SIZE_128B);
1373 }
1374
1375 /* 128B is recommended, but 64B can be set too if needed for 4K by DCN.
1376 * Since there is no reason to ever disable 128B, require it.
1377 * DCC image stores are always supported.
1378 */
1379 return surf->u.gfx9.dcc.independent_128B_blocks &&
1380 surf->u.gfx9.dcc.max_compressed_block_size <= V_028C78_MAX_BLOCK_SIZE_128B;
1381 }
1382
1383 static bool is_dcc_supported_by_DCN(const struct radeon_info *info,
1384 const struct ac_surf_config *config,
1385 const struct radeon_surf *surf,
1386 bool rb_aligned, bool pipe_aligned)
1387 {
1388 if (!info->use_display_dcc_unaligned &&
1389 !info->use_display_dcc_with_retile_blit)
1390 return false;
1391
1392 /* 16bpp and 64bpp are more complicated, so they are disallowed for now. */
1393 if (surf->bpe != 4)
1394 return false;
1395
1396 /* Handle unaligned DCC. */
1397 if (info->use_display_dcc_unaligned &&
1398 (rb_aligned || pipe_aligned))
1399 return false;
1400
1401 switch (info->chip_class) {
1402 case GFX9:
1403 /* There are more constraints, but we always set
1404 * INDEPENDENT_64B_BLOCKS = 1 and MAX_COMPRESSED_BLOCK_SIZE = 64B,
1405 * which always works.
1406 */
1407 assert(surf->u.gfx9.dcc.independent_64B_blocks &&
1408 surf->u.gfx9.dcc.max_compressed_block_size == V_028C78_MAX_BLOCK_SIZE_64B);
1409 return true;
1410 case GFX10:
1411 case GFX10_3:
1412 /* DCN requires INDEPENDENT_128B_BLOCKS = 0 only on Navi1x. */
1413 if (info->chip_class == GFX10 &&
1414 surf->u.gfx9.dcc.independent_128B_blocks)
1415 return false;
1416
1417 /* For 4K, DCN requires INDEPENDENT_64B_BLOCKS = 1. */
1418 return ((config->info.width <= 2560 &&
1419 config->info.height <= 2560) ||
1420 (surf->u.gfx9.dcc.independent_64B_blocks &&
1421 surf->u.gfx9.dcc.max_compressed_block_size == V_028C78_MAX_BLOCK_SIZE_64B));
1422 default:
1423 unreachable("unhandled chip");
1424 return false;
1425 }
1426 }
1427
1428 static int gfx9_compute_miptree(struct ac_addrlib *addrlib,
1429 const struct radeon_info *info,
1430 const struct ac_surf_config *config,
1431 struct radeon_surf *surf, bool compressed,
1432 ADDR2_COMPUTE_SURFACE_INFO_INPUT *in)
1433 {
1434 ADDR2_MIP_INFO mip_info[RADEON_SURF_MAX_LEVELS] = {};
1435 ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out = {0};
1436 ADDR_E_RETURNCODE ret;
1437
1438 out.size = sizeof(ADDR2_COMPUTE_SURFACE_INFO_OUTPUT);
1439 out.pMipInfo = mip_info;
1440
1441 ret = Addr2ComputeSurfaceInfo(addrlib->handle, in, &out);
1442 if (ret != ADDR_OK)
1443 return ret;
1444
1445 if (in->flags.stencil) {
1446 surf->u.gfx9.stencil.swizzle_mode = in->swizzleMode;
1447 surf->u.gfx9.stencil.epitch = out.epitchIsHeight ? out.mipChainHeight - 1 :
1448 out.mipChainPitch - 1;
1449 surf->surf_alignment = MAX2(surf->surf_alignment, out.baseAlign);
1450 surf->u.gfx9.stencil_offset = align(surf->surf_size, out.baseAlign);
1451 surf->surf_size = surf->u.gfx9.stencil_offset + out.surfSize;
1452 return 0;
1453 }
1454
1455 surf->u.gfx9.surf.swizzle_mode = in->swizzleMode;
1456 surf->u.gfx9.surf.epitch = out.epitchIsHeight ? out.mipChainHeight - 1 :
1457 out.mipChainPitch - 1;
1458
1459 /* CMASK fast clear uses these even if FMASK isn't allocated.
1460 * FMASK only supports the Z swizzle modes, whose numbers are multiples of 4.
1461 */
1462 surf->u.gfx9.fmask.swizzle_mode = surf->u.gfx9.surf.swizzle_mode & ~0x3;
1463 surf->u.gfx9.fmask.epitch = surf->u.gfx9.surf.epitch;
1464
1465 surf->u.gfx9.surf_slice_size = out.sliceSize;
1466 surf->u.gfx9.surf_pitch = out.pitch;
1467 if (!compressed && surf->blk_w > 1 && out.pitch == out.pixelPitch &&
1468 surf->u.gfx9.surf.swizzle_mode == ADDR_SW_LINEAR) {
1469 /* Adjust surf_pitch to be in elements units,
1470 * not in pixels */
1471 surf->u.gfx9.surf_pitch =
1472 align(surf->u.gfx9.surf_pitch / surf->blk_w, 256 / surf->bpe);
1473 surf->u.gfx9.surf.epitch = MAX2(surf->u.gfx9.surf.epitch,
1474 surf->u.gfx9.surf_pitch * surf->blk_w - 1);
1475 }
1476 surf->u.gfx9.surf_height = out.height;
1477 surf->surf_size = out.surfSize;
1478 surf->surf_alignment = out.baseAlign;
1479
1480 if (in->swizzleMode == ADDR_SW_LINEAR) {
1481 for (unsigned i = 0; i < in->numMipLevels; i++) {
1482 surf->u.gfx9.offset[i] = mip_info[i].offset;
1483 surf->u.gfx9.pitch[i] = mip_info[i].pitch;
1484 }
1485 }
1486
1487 if (in->flags.depth) {
1488 assert(in->swizzleMode != ADDR_SW_LINEAR);
1489
1490 if (surf->flags & RADEON_SURF_NO_HTILE)
1491 return 0;
1492
1493 /* HTILE */
1494 ADDR2_COMPUTE_HTILE_INFO_INPUT hin = {0};
1495 ADDR2_COMPUTE_HTILE_INFO_OUTPUT hout = {0};
1496
1497 hin.size = sizeof(ADDR2_COMPUTE_HTILE_INFO_INPUT);
1498 hout.size = sizeof(ADDR2_COMPUTE_HTILE_INFO_OUTPUT);
1499
1500 assert(in->flags.metaPipeUnaligned == 0);
1501 assert(in->flags.metaRbUnaligned == 0);
1502
1503 hin.hTileFlags.pipeAligned = 1;
1504 hin.hTileFlags.rbAligned = 1;
1505 hin.depthFlags = in->flags;
1506 hin.swizzleMode = in->swizzleMode;
1507 hin.unalignedWidth = in->width;
1508 hin.unalignedHeight = in->height;
1509 hin.numSlices = in->numSlices;
1510 hin.numMipLevels = in->numMipLevels;
1511 hin.firstMipIdInTail = out.firstMipIdInTail;
1512
1513 ret = Addr2ComputeHtileInfo(addrlib->handle, &hin, &hout);
1514 if (ret != ADDR_OK)
1515 return ret;
1516
1517 surf->htile_size = hout.htileBytes;
1518 surf->htile_slice_size = hout.sliceSize;
1519 surf->htile_alignment = hout.baseAlign;
1520 return 0;
1521 }
1522
1523 {
1524 /* Compute tile swizzle for the color surface.
1525 * All *_X and *_T modes can use the swizzle.
1526 */
1527 if (config->info.surf_index &&
1528 in->swizzleMode >= ADDR_SW_64KB_Z_T &&
1529 !out.mipChainInTail &&
1530 !(surf->flags & RADEON_SURF_SHAREABLE) &&
1531 !in->flags.display) {
1532 ADDR2_COMPUTE_PIPEBANKXOR_INPUT xin = {0};
1533 ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT xout = {0};
1534
1535 xin.size = sizeof(ADDR2_COMPUTE_PIPEBANKXOR_INPUT);
1536 xout.size = sizeof(ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT);
1537
1538 xin.surfIndex = p_atomic_inc_return(config->info.surf_index) - 1;
1539 xin.flags = in->flags;
1540 xin.swizzleMode = in->swizzleMode;
1541 xin.resourceType = in->resourceType;
1542 xin.format = in->format;
1543 xin.numSamples = in->numSamples;
1544 xin.numFrags = in->numFrags;
1545
1546 ret = Addr2ComputePipeBankXor(addrlib->handle, &xin, &xout);
1547 if (ret != ADDR_OK)
1548 return ret;
1549
1550 assert(xout.pipeBankXor <=
1551 u_bit_consecutive(0, sizeof(surf->tile_swizzle) * 8));
1552 surf->tile_swizzle = xout.pipeBankXor;
1553 }
1554
1555 /* DCC */
1556 if (info->has_graphics &&
1557 !(surf->flags & RADEON_SURF_DISABLE_DCC) &&
1558 !compressed &&
1559 is_dcc_supported_by_CB(info, in->swizzleMode) &&
1560 (!in->flags.display ||
1561 is_dcc_supported_by_DCN(info, config, surf,
1562 !in->flags.metaRbUnaligned,
1563 !in->flags.metaPipeUnaligned))) {
1564 ADDR2_COMPUTE_DCCINFO_INPUT din = {0};
1565 ADDR2_COMPUTE_DCCINFO_OUTPUT dout = {0};
1566 ADDR2_META_MIP_INFO meta_mip_info[RADEON_SURF_MAX_LEVELS] = {};
1567
1568 din.size = sizeof(ADDR2_COMPUTE_DCCINFO_INPUT);
1569 dout.size = sizeof(ADDR2_COMPUTE_DCCINFO_OUTPUT);
1570 dout.pMipInfo = meta_mip_info;
1571
1572 din.dccKeyFlags.pipeAligned = !in->flags.metaPipeUnaligned;
1573 din.dccKeyFlags.rbAligned = !in->flags.metaRbUnaligned;
1574 din.resourceType = in->resourceType;
1575 din.swizzleMode = in->swizzleMode;
1576 din.bpp = in->bpp;
1577 din.unalignedWidth = in->width;
1578 din.unalignedHeight = in->height;
1579 din.numSlices = in->numSlices;
1580 din.numFrags = in->numFrags;
1581 din.numMipLevels = in->numMipLevels;
1582 din.dataSurfaceSize = out.surfSize;
1583 din.firstMipIdInTail = out.firstMipIdInTail;
1584
1585 ret = Addr2ComputeDccInfo(addrlib->handle, &din, &dout);
1586 if (ret != ADDR_OK)
1587 return ret;
1588
1589 surf->u.gfx9.dcc.rb_aligned = din.dccKeyFlags.rbAligned;
1590 surf->u.gfx9.dcc.pipe_aligned = din.dccKeyFlags.pipeAligned;
1591 surf->u.gfx9.dcc_block_width = dout.compressBlkWidth;
1592 surf->u.gfx9.dcc_block_height = dout.compressBlkHeight;
1593 surf->u.gfx9.dcc_block_depth = dout.compressBlkDepth;
1594 surf->dcc_size = dout.dccRamSize;
1595 surf->dcc_alignment = dout.dccRamBaseAlign;
1596 surf->num_dcc_levels = in->numMipLevels;
1597
1598 /* Disable DCC for levels that are in the mip tail.
1599 *
1600 * There are two issues that this is intended to
1601 * address:
1602 *
1603 * 1. Multiple mip levels may share a cache line. This
1604 * can lead to corruption when switching between
1605 * rendering to different mip levels because the
1606 * RBs don't maintain coherency.
1607 *
1608 * 2. Texturing with metadata after rendering sometimes
1609 * fails with corruption, probably for a similar
1610 * reason.
1611 *
1612 * Working around these issues for all levels in the
1613 * mip tail may be overly conservative, but it's what
1614 * Vulkan does.
1615 *
1616 * Alternative solutions that also work but are worse:
1617 * - Disable DCC entirely.
1618 * - Flush TC L2 after rendering.
1619 */
1620 for (unsigned i = 0; i < in->numMipLevels; i++) {
1621 if (meta_mip_info[i].inMiptail) {
1622 /* GFX10 can only compress the first level
1623 * in the mip tail.
1624 *
1625 * TODO: Try to do the same thing for gfx9
1626 * if there are no regressions.
1627 */
1628 if (info->chip_class >= GFX10)
1629 surf->num_dcc_levels = i + 1;
1630 else
1631 surf->num_dcc_levels = i;
1632 break;
1633 }
1634 }
1635
1636 if (!surf->num_dcc_levels)
1637 surf->dcc_size = 0;
1638
1639 surf->u.gfx9.display_dcc_size = surf->dcc_size;
1640 surf->u.gfx9.display_dcc_alignment = surf->dcc_alignment;
1641 surf->u.gfx9.display_dcc_pitch_max = dout.pitch - 1;
1642
1643 /* Compute displayable DCC. */
1644 if (in->flags.display &&
1645 surf->num_dcc_levels &&
1646 info->use_display_dcc_with_retile_blit) {
1647 /* Compute displayable DCC info. */
1648 din.dccKeyFlags.pipeAligned = 0;
1649 din.dccKeyFlags.rbAligned = 0;
1650
1651 assert(din.numSlices == 1);
1652 assert(din.numMipLevels == 1);
1653 assert(din.numFrags == 1);
1654 assert(surf->tile_swizzle == 0);
1655 assert(surf->u.gfx9.dcc.pipe_aligned ||
1656 surf->u.gfx9.dcc.rb_aligned);
1657
1658 ret = Addr2ComputeDccInfo(addrlib->handle, &din, &dout);
1659 if (ret != ADDR_OK)
1660 return ret;
1661
1662 surf->u.gfx9.display_dcc_size = dout.dccRamSize;
1663 surf->u.gfx9.display_dcc_alignment = dout.dccRamBaseAlign;
1664 surf->u.gfx9.display_dcc_pitch_max = dout.pitch - 1;
1665 assert(surf->u.gfx9.display_dcc_size <= surf->dcc_size);
1666
1667 surf->u.gfx9.dcc_retile_use_uint16 =
1668 surf->u.gfx9.display_dcc_size <= UINT16_MAX + 1 &&
1669 surf->dcc_size <= UINT16_MAX + 1;
1670
1671 /* Align the retile map size to get more hash table hits and
1672 * decrease the maximum memory footprint when all retile maps
1673 * are cached in the hash table.
1674 */
1675 unsigned retile_dim[2] = {in->width, in->height};
1676
1677 for (unsigned i = 0; i < 2; i++) {
1678 /* Increase the alignment as the size increases.
1679 * Greater alignment increases retile compute work,
1680 * but decreases maximum memory footprint for the cache.
1681 *
1682 * With this alignment, the worst case memory footprint of
1683 * the cache is:
1684 * 1920x1080: 55 MB
1685 * 2560x1440: 99 MB
1686 * 3840x2160: 305 MB
1687 *
1688 * The worst case size in MB can be computed in Haskell as follows:
1689 * (sum (map get_retile_size (map get_dcc_size (deduplicate (map align_pair
1690 * [(i*16,j*16) | i <- [1..maxwidth`div`16], j <- [1..maxheight`div`16]]))))) `div` 1024^2
1691 * where
1692 * alignment x = if x <= 512 then 16 else if x <= 1024 then 32 else if x <= 2048 then 64 else 128
1693 * align x = (x + (alignment x) - 1) `div` (alignment x) * (alignment x)
1694 * align_pair e = (align (fst e), align (snd e))
1695 * deduplicate = map head . groupBy (\ a b -> ((fst a) == (fst b)) && ((snd a) == (snd b))) . sortBy compare
1696 * get_dcc_size e = ((fst e) * (snd e) * bpp) `div` 256
1697 * get_retile_size dcc_size = dcc_size * 2 * (if dcc_size <= 2^16 then 2 else 4)
1698 * bpp = 4; maxwidth = 3840; maxheight = 2160
1699 */
1700 if (retile_dim[i] <= 512)
1701 retile_dim[i] = align(retile_dim[i], 16);
1702 else if (retile_dim[i] <= 1024)
1703 retile_dim[i] = align(retile_dim[i], 32);
1704 else if (retile_dim[i] <= 2048)
1705 retile_dim[i] = align(retile_dim[i], 64);
1706 else
1707 retile_dim[i] = align(retile_dim[i], 128);
1708
1709 /* Don't align more than the DCC pixel alignment. */
1710 assert(dout.metaBlkWidth >= 128 && dout.metaBlkHeight >= 128);
1711 }
1712
1713 surf->u.gfx9.dcc_retile_num_elements =
1714 DIV_ROUND_UP(retile_dim[0], dout.compressBlkWidth) *
1715 DIV_ROUND_UP(retile_dim[1], dout.compressBlkHeight) * 2;
1716 /* Align the size to 4 (for the compute shader). */
1717 surf->u.gfx9.dcc_retile_num_elements =
1718 align(surf->u.gfx9.dcc_retile_num_elements, 4);
1719
1720 if (!(surf->flags & RADEON_SURF_IMPORTED)) {
1721 /* Compute address mapping from non-displayable to displayable DCC. */
1722 ADDR2_COMPUTE_DCC_ADDRFROMCOORD_INPUT addrin;
1723 memset(&addrin, 0, sizeof(addrin));
1724 addrin.size = sizeof(addrin);
1725 addrin.swizzleMode = din.swizzleMode;
1726 addrin.resourceType = din.resourceType;
1727 addrin.bpp = din.bpp;
1728 addrin.numSlices = 1;
1729 addrin.numMipLevels = 1;
1730 addrin.numFrags = 1;
1731 addrin.pitch = dout.pitch;
1732 addrin.height = dout.height;
1733 addrin.compressBlkWidth = dout.compressBlkWidth;
1734 addrin.compressBlkHeight = dout.compressBlkHeight;
1735 addrin.compressBlkDepth = dout.compressBlkDepth;
1736 addrin.metaBlkWidth = dout.metaBlkWidth;
1737 addrin.metaBlkHeight = dout.metaBlkHeight;
1738 addrin.metaBlkDepth = dout.metaBlkDepth;
1739 addrin.dccRamSliceSize = 0; /* Don't care for non-layered images. */
1740
1741 surf->u.gfx9.dcc_retile_map =
1742 ac_compute_dcc_retile_map(addrlib, info,
1743 retile_dim[0], retile_dim[1],
1744 surf->u.gfx9.dcc.rb_aligned,
1745 surf->u.gfx9.dcc.pipe_aligned,
1746 surf->u.gfx9.dcc_retile_use_uint16,
1747 surf->u.gfx9.dcc_retile_num_elements,
1748 &addrin);
1749 if (!surf->u.gfx9.dcc_retile_map)
1750 return ADDR_OUTOFMEMORY;
1751 }
1752 }
1753 }
1754
1755 /* FMASK */
1756 if (in->numSamples > 1 && info->has_graphics &&
1757 !(surf->flags & RADEON_SURF_NO_FMASK)) {
1758 ADDR2_COMPUTE_FMASK_INFO_INPUT fin = {0};
1759 ADDR2_COMPUTE_FMASK_INFO_OUTPUT fout = {0};
1760
1761 fin.size = sizeof(ADDR2_COMPUTE_FMASK_INFO_INPUT);
1762 fout.size = sizeof(ADDR2_COMPUTE_FMASK_INFO_OUTPUT);
1763
1764 ret = gfx9_get_preferred_swizzle_mode(addrlib->handle, surf, in,
1765 true, &fin.swizzleMode);
1766 if (ret != ADDR_OK)
1767 return ret;
1768
1769 fin.unalignedWidth = in->width;
1770 fin.unalignedHeight = in->height;
1771 fin.numSlices = in->numSlices;
1772 fin.numSamples = in->numSamples;
1773 fin.numFrags = in->numFrags;
1774
1775 ret = Addr2ComputeFmaskInfo(addrlib->handle, &fin, &fout);
1776 if (ret != ADDR_OK)
1777 return ret;
1778
1779 surf->u.gfx9.fmask.swizzle_mode = fin.swizzleMode;
1780 surf->u.gfx9.fmask.epitch = fout.pitch - 1;
1781 surf->fmask_size = fout.fmaskBytes;
1782 surf->fmask_alignment = fout.baseAlign;
1783
1784 /* Compute tile swizzle for the FMASK surface. */
1785 if (config->info.fmask_surf_index &&
1786 fin.swizzleMode >= ADDR_SW_64KB_Z_T &&
1787 !(surf->flags & RADEON_SURF_SHAREABLE)) {
1788 ADDR2_COMPUTE_PIPEBANKXOR_INPUT xin = {0};
1789 ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT xout = {0};
1790
1791 xin.size = sizeof(ADDR2_COMPUTE_PIPEBANKXOR_INPUT);
1792 xout.size = sizeof(ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT);
1793
1794 /* This counter starts from 1 instead of 0. */
1795 xin.surfIndex = p_atomic_inc_return(config->info.fmask_surf_index);
1796 xin.flags = in->flags;
1797 xin.swizzleMode = fin.swizzleMode;
1798 xin.resourceType = in->resourceType;
1799 xin.format = in->format;
1800 xin.numSamples = in->numSamples;
1801 xin.numFrags = in->numFrags;
1802
1803 ret = Addr2ComputePipeBankXor(addrlib->handle, &xin, &xout);
1804 if (ret != ADDR_OK)
1805 return ret;
1806
1807 assert(xout.pipeBankXor <=
1808 u_bit_consecutive(0, sizeof(surf->fmask_tile_swizzle) * 8));
1809 surf->fmask_tile_swizzle = xout.pipeBankXor;
1810 }
1811 }
1812
1813 /* CMASK -- on GFX10 only for FMASK */
1814 if (in->swizzleMode != ADDR_SW_LINEAR &&
1815 in->resourceType == ADDR_RSRC_TEX_2D &&
1816 ((info->chip_class <= GFX9 &&
1817 in->numSamples == 1 &&
1818 in->flags.metaPipeUnaligned == 0 &&
1819 in->flags.metaRbUnaligned == 0) ||
1820 (surf->fmask_size && in->numSamples >= 2))) {
1821 ADDR2_COMPUTE_CMASK_INFO_INPUT cin = {0};
1822 ADDR2_COMPUTE_CMASK_INFO_OUTPUT cout = {0};
1823
1824 cin.size = sizeof(ADDR2_COMPUTE_CMASK_INFO_INPUT);
1825 cout.size = sizeof(ADDR2_COMPUTE_CMASK_INFO_OUTPUT);
1826
1827 assert(in->flags.metaPipeUnaligned == 0);
1828 assert(in->flags.metaRbUnaligned == 0);
1829
1830 cin.cMaskFlags.pipeAligned = 1;
1831 cin.cMaskFlags.rbAligned = 1;
1832 cin.resourceType = in->resourceType;
1833 cin.unalignedWidth = in->width;
1834 cin.unalignedHeight = in->height;
1835 cin.numSlices = in->numSlices;
1836
1837 if (in->numSamples > 1)
1838 cin.swizzleMode = surf->u.gfx9.fmask.swizzle_mode;
1839 else
1840 cin.swizzleMode = in->swizzleMode;
1841
1842 ret = Addr2ComputeCmaskInfo(addrlib->handle, &cin, &cout);
1843 if (ret != ADDR_OK)
1844 return ret;
1845
1846 surf->cmask_size = cout.cmaskBytes;
1847 surf->cmask_alignment = cout.baseAlign;
1848 }
1849 }
1850
1851 return 0;
1852 }
1853
1854 static int gfx9_compute_surface(struct ac_addrlib *addrlib,
1855 const struct radeon_info *info,
1856 const struct ac_surf_config *config,
1857 enum radeon_surf_mode mode,
1858 struct radeon_surf *surf)
1859 {
1860 bool compressed;
1861 ADDR2_COMPUTE_SURFACE_INFO_INPUT AddrSurfInfoIn = {0};
1862 int r;
1863
1864 AddrSurfInfoIn.size = sizeof(ADDR2_COMPUTE_SURFACE_INFO_INPUT);
1865
1866 compressed = surf->blk_w == 4 && surf->blk_h == 4;
1867
1868 /* The format must be set correctly for the allocation of compressed
1869 * textures to work. In other cases, setting the bpp is sufficient. */
1870 if (compressed) {
1871 switch (surf->bpe) {
1872 case 8:
1873 AddrSurfInfoIn.format = ADDR_FMT_BC1;
1874 break;
1875 case 16:
1876 AddrSurfInfoIn.format = ADDR_FMT_BC3;
1877 break;
1878 default:
1879 assert(0);
1880 }
1881 } else {
1882 switch (surf->bpe) {
1883 case 1:
1884 assert(!(surf->flags & RADEON_SURF_ZBUFFER));
1885 AddrSurfInfoIn.format = ADDR_FMT_8;
1886 break;
1887 case 2:
1888 assert(surf->flags & RADEON_SURF_ZBUFFER ||
1889 !(surf->flags & RADEON_SURF_SBUFFER));
1890 AddrSurfInfoIn.format = ADDR_FMT_16;
1891 break;
1892 case 4:
1893 assert(surf->flags & RADEON_SURF_ZBUFFER ||
1894 !(surf->flags & RADEON_SURF_SBUFFER));
1895 AddrSurfInfoIn.format = ADDR_FMT_32;
1896 break;
1897 case 8:
1898 assert(!(surf->flags & RADEON_SURF_Z_OR_SBUFFER));
1899 AddrSurfInfoIn.format = ADDR_FMT_32_32;
1900 break;
1901 case 12:
1902 assert(!(surf->flags & RADEON_SURF_Z_OR_SBUFFER));
1903 AddrSurfInfoIn.format = ADDR_FMT_32_32_32;
1904 break;
1905 case 16:
1906 assert(!(surf->flags & RADEON_SURF_Z_OR_SBUFFER));
1907 AddrSurfInfoIn.format = ADDR_FMT_32_32_32_32;
1908 break;
1909 default:
1910 assert(0);
1911 }
1912 AddrSurfInfoIn.bpp = surf->bpe * 8;
1913 }
1914
1915 bool is_color_surface = !(surf->flags & RADEON_SURF_Z_OR_SBUFFER);
1916 AddrSurfInfoIn.flags.color = is_color_surface &&
1917 !(surf->flags & RADEON_SURF_NO_RENDER_TARGET);
1918 AddrSurfInfoIn.flags.depth = (surf->flags & RADEON_SURF_ZBUFFER) != 0;
1919 AddrSurfInfoIn.flags.display = get_display_flag(config, surf);
1920 /* flags.texture currently refers to TC-compatible HTILE */
1921 AddrSurfInfoIn.flags.texture = is_color_surface ||
1922 surf->flags & RADEON_SURF_TC_COMPATIBLE_HTILE;
1923 AddrSurfInfoIn.flags.opt4space = 1;
1924
1925 AddrSurfInfoIn.numMipLevels = config->info.levels;
1926 AddrSurfInfoIn.numSamples = MAX2(1, config->info.samples);
1927 AddrSurfInfoIn.numFrags = AddrSurfInfoIn.numSamples;
1928
1929 if (!(surf->flags & RADEON_SURF_Z_OR_SBUFFER))
1930 AddrSurfInfoIn.numFrags = MAX2(1, config->info.storage_samples);
1931
1932 /* GFX9 doesn't support 1D depth textures, so allocate all 1D textures
1933 * as 2D to avoid having shader variants for 1D vs 2D, so all shaders
1934 * must sample 1D textures as 2D. */
1935 if (config->is_3d)
1936 AddrSurfInfoIn.resourceType = ADDR_RSRC_TEX_3D;
1937 else if (info->chip_class != GFX9 && config->is_1d)
1938 AddrSurfInfoIn.resourceType = ADDR_RSRC_TEX_1D;
1939 else
1940 AddrSurfInfoIn.resourceType = ADDR_RSRC_TEX_2D;
1941
1942 AddrSurfInfoIn.width = config->info.width;
1943 AddrSurfInfoIn.height = config->info.height;
1944
1945 if (config->is_3d)
1946 AddrSurfInfoIn.numSlices = config->info.depth;
1947 else if (config->is_cube)
1948 AddrSurfInfoIn.numSlices = 6;
1949 else
1950 AddrSurfInfoIn.numSlices = config->info.array_size;
1951
1952 /* This is propagated to DCC. It must be 0 for HTILE and CMASK. */
1953 AddrSurfInfoIn.flags.metaPipeUnaligned = 0;
1954 AddrSurfInfoIn.flags.metaRbUnaligned = 0;
1955
1956 /* Optimal values for the L2 cache. */
1957 if (info->chip_class == GFX9) {
1958 surf->u.gfx9.dcc.independent_64B_blocks = 1;
1959 surf->u.gfx9.dcc.independent_128B_blocks = 0;
1960 surf->u.gfx9.dcc.max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
1961 } else if (info->chip_class >= GFX10) {
1962 surf->u.gfx9.dcc.independent_64B_blocks = 0;
1963 surf->u.gfx9.dcc.independent_128B_blocks = 1;
1964 surf->u.gfx9.dcc.max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
1965 }
1966
1967 if (AddrSurfInfoIn.flags.display) {
1968 /* The display hardware can only read DCC with RB_ALIGNED=0 and
1969 * PIPE_ALIGNED=0. PIPE_ALIGNED really means L2CACHE_ALIGNED.
1970 *
1971 * The CB block requires RB_ALIGNED=1 except 1 RB chips.
1972 * PIPE_ALIGNED is optional, but PIPE_ALIGNED=0 requires L2 flushes
1973 * after rendering, so PIPE_ALIGNED=1 is recommended.
1974 */
1975 if (info->use_display_dcc_unaligned) {
1976 AddrSurfInfoIn.flags.metaPipeUnaligned = 1;
1977 AddrSurfInfoIn.flags.metaRbUnaligned = 1;
1978 }
1979
1980 /* Adjust DCC settings to meet DCN requirements. */
1981 if (info->use_display_dcc_unaligned ||
1982 info->use_display_dcc_with_retile_blit) {
1983 /* Only Navi12/14 support independent 64B blocks in L2,
1984 * but without DCC image stores.
1985 */
1986 if (info->family == CHIP_NAVI12 ||
1987 info->family == CHIP_NAVI14) {
1988 surf->u.gfx9.dcc.independent_64B_blocks = 1;
1989 surf->u.gfx9.dcc.independent_128B_blocks = 0;
1990 surf->u.gfx9.dcc.max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
1991 }
1992
1993 if (info->chip_class >= GFX10_3) {
1994 surf->u.gfx9.dcc.independent_64B_blocks = 1;
1995 surf->u.gfx9.dcc.independent_128B_blocks = 1;
1996 surf->u.gfx9.dcc.max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
1997 }
1998 }
1999 }
2000
2001 switch (mode) {
2002 case RADEON_SURF_MODE_LINEAR_ALIGNED:
2003 assert(config->info.samples <= 1);
2004 assert(!(surf->flags & RADEON_SURF_Z_OR_SBUFFER));
2005 AddrSurfInfoIn.swizzleMode = ADDR_SW_LINEAR;
2006 break;
2007
2008 case RADEON_SURF_MODE_1D:
2009 case RADEON_SURF_MODE_2D:
2010 if (surf->flags & RADEON_SURF_IMPORTED ||
2011 (info->chip_class >= GFX10 &&
2012 surf->flags & RADEON_SURF_FORCE_SWIZZLE_MODE)) {
2013 AddrSurfInfoIn.swizzleMode = surf->u.gfx9.surf.swizzle_mode;
2014 break;
2015 }
2016
2017 r = gfx9_get_preferred_swizzle_mode(addrlib->handle, surf, &AddrSurfInfoIn,
2018 false, &AddrSurfInfoIn.swizzleMode);
2019 if (r)
2020 return r;
2021 break;
2022
2023 default:
2024 assert(0);
2025 }
2026
2027 surf->u.gfx9.resource_type = AddrSurfInfoIn.resourceType;
2028 surf->has_stencil = !!(surf->flags & RADEON_SURF_SBUFFER);
2029
2030 surf->num_dcc_levels = 0;
2031 surf->surf_size = 0;
2032 surf->fmask_size = 0;
2033 surf->dcc_size = 0;
2034 surf->htile_size = 0;
2035 surf->htile_slice_size = 0;
2036 surf->u.gfx9.surf_offset = 0;
2037 surf->u.gfx9.stencil_offset = 0;
2038 surf->cmask_size = 0;
2039 surf->u.gfx9.dcc_retile_use_uint16 = false;
2040 surf->u.gfx9.dcc_retile_num_elements = 0;
2041 surf->u.gfx9.dcc_retile_map = NULL;
2042
2043 /* Calculate texture layout information. */
2044 r = gfx9_compute_miptree(addrlib, info, config, surf, compressed,
2045 &AddrSurfInfoIn);
2046 if (r)
2047 return r;
2048
2049 /* Calculate texture layout information for stencil. */
2050 if (surf->flags & RADEON_SURF_SBUFFER) {
2051 AddrSurfInfoIn.flags.stencil = 1;
2052 AddrSurfInfoIn.bpp = 8;
2053 AddrSurfInfoIn.format = ADDR_FMT_8;
2054
2055 if (!AddrSurfInfoIn.flags.depth) {
2056 r = gfx9_get_preferred_swizzle_mode(addrlib->handle, surf, &AddrSurfInfoIn,
2057 false, &AddrSurfInfoIn.swizzleMode);
2058 if (r)
2059 return r;
2060 } else
2061 AddrSurfInfoIn.flags.depth = 0;
2062
2063 r = gfx9_compute_miptree(addrlib, info, config, surf, compressed,
2064 &AddrSurfInfoIn);
2065 if (r)
2066 return r;
2067 }
2068
2069 surf->is_linear = surf->u.gfx9.surf.swizzle_mode == ADDR_SW_LINEAR;
2070
2071 /* Query whether the surface is displayable. */
2072 /* This is only useful for surfaces that are allocated without SCANOUT. */
2073 bool displayable = false;
2074 if (!config->is_3d && !config->is_cube) {
2075 r = Addr2IsValidDisplaySwizzleMode(addrlib->handle, surf->u.gfx9.surf.swizzle_mode,
2076 surf->bpe * 8, &displayable);
2077 if (r)
2078 return r;
2079
2080 /* Display needs unaligned DCC. */
2081 if (surf->num_dcc_levels &&
2082 (!is_dcc_supported_by_DCN(info, config, surf,
2083 surf->u.gfx9.dcc.rb_aligned,
2084 surf->u.gfx9.dcc.pipe_aligned) ||
2085 /* Don't set is_displayable if displayable DCC is missing. */
2086 (info->use_display_dcc_with_retile_blit &&
2087 !surf->u.gfx9.dcc_retile_num_elements)))
2088 displayable = false;
2089 }
2090 surf->is_displayable = displayable;
2091
2092 /* Validate that we allocated a displayable surface if requested. */
2093 assert(!AddrSurfInfoIn.flags.display || surf->is_displayable);
2094
2095 /* Validate that DCC is set up correctly. */
2096 if (surf->num_dcc_levels) {
2097 assert(is_dcc_supported_by_L2(info, surf));
2098 if (AddrSurfInfoIn.flags.color)
2099 assert(is_dcc_supported_by_CB(info, surf->u.gfx9.surf.swizzle_mode));
2100 if (AddrSurfInfoIn.flags.display) {
2101 assert(is_dcc_supported_by_DCN(info, config, surf,
2102 surf->u.gfx9.dcc.rb_aligned,
2103 surf->u.gfx9.dcc.pipe_aligned));
2104 }
2105 }
2106
2107 if (info->has_graphics &&
2108 !compressed &&
2109 !config->is_3d &&
2110 config->info.levels == 1 &&
2111 AddrSurfInfoIn.flags.color &&
2112 !surf->is_linear &&
2113 surf->surf_alignment >= 64 * 1024 && /* 64KB tiling */
2114 !(surf->flags & (RADEON_SURF_DISABLE_DCC |
2115 RADEON_SURF_FORCE_SWIZZLE_MODE |
2116 RADEON_SURF_FORCE_MICRO_TILE_MODE))) {
2117 /* Validate that DCC is enabled if DCN can do it. */
2118 if ((info->use_display_dcc_unaligned ||
2119 info->use_display_dcc_with_retile_blit) &&
2120 AddrSurfInfoIn.flags.display &&
2121 surf->bpe == 4) {
2122 assert(surf->num_dcc_levels);
2123 }
2124
2125 /* Validate that non-scanout DCC is always enabled. */
2126 if (!AddrSurfInfoIn.flags.display)
2127 assert(surf->num_dcc_levels);
2128 }
2129
2130 if (!surf->htile_size) {
2131 /* Unset this if HTILE is not present. */
2132 surf->flags &= ~RADEON_SURF_TC_COMPATIBLE_HTILE;
2133 }
2134
2135 switch (surf->u.gfx9.surf.swizzle_mode) {
2136 /* S = standard. */
2137 case ADDR_SW_256B_S:
2138 case ADDR_SW_4KB_S:
2139 case ADDR_SW_64KB_S:
2140 case ADDR_SW_64KB_S_T:
2141 case ADDR_SW_4KB_S_X:
2142 case ADDR_SW_64KB_S_X:
2143 surf->micro_tile_mode = RADEON_MICRO_MODE_STANDARD;
2144 break;
2145
2146 /* D = display. */
2147 case ADDR_SW_LINEAR:
2148 case ADDR_SW_256B_D:
2149 case ADDR_SW_4KB_D:
2150 case ADDR_SW_64KB_D:
2151 case ADDR_SW_64KB_D_T:
2152 case ADDR_SW_4KB_D_X:
2153 case ADDR_SW_64KB_D_X:
2154 surf->micro_tile_mode = RADEON_MICRO_MODE_DISPLAY;
2155 break;
2156
2157 /* R = rotated (gfx9), render target (gfx10). */
2158 case ADDR_SW_256B_R:
2159 case ADDR_SW_4KB_R:
2160 case ADDR_SW_64KB_R:
2161 case ADDR_SW_64KB_R_T:
2162 case ADDR_SW_4KB_R_X:
2163 case ADDR_SW_64KB_R_X:
2164 case ADDR_SW_VAR_R_X:
2165 /* The rotated micro tile mode doesn't work if both CMASK and RB+ are
2166 * used at the same time. We currently do not use rotated
2167 * in gfx9.
2168 */
2169 assert(info->chip_class >= GFX10 ||
2170 !"rotate micro tile mode is unsupported");
2171 surf->micro_tile_mode = RADEON_MICRO_MODE_RENDER;
2172 break;
2173
2174 /* Z = depth. */
2175 case ADDR_SW_4KB_Z:
2176 case ADDR_SW_64KB_Z:
2177 case ADDR_SW_64KB_Z_T:
2178 case ADDR_SW_4KB_Z_X:
2179 case ADDR_SW_64KB_Z_X:
2180 case ADDR_SW_VAR_Z_X:
2181 surf->micro_tile_mode = RADEON_MICRO_MODE_DEPTH;
2182 break;
2183
2184 default:
2185 assert(0);
2186 }
2187
2188 return 0;
2189 }
2190
2191 int ac_compute_surface(struct ac_addrlib *addrlib, const struct radeon_info *info,
2192 const struct ac_surf_config *config,
2193 enum radeon_surf_mode mode,
2194 struct radeon_surf *surf)
2195 {
2196 int r;
2197
2198 r = surf_config_sanity(config, surf->flags);
2199 if (r)
2200 return r;
2201
2202 if (info->chip_class >= GFX9)
2203 r = gfx9_compute_surface(addrlib, info, config, mode, surf);
2204 else
2205 r = gfx6_compute_surface(addrlib->handle, info, config, mode, surf);
2206
2207 if (r)
2208 return r;
2209
2210 /* Determine the memory layout of multiple allocations in one buffer. */
2211 surf->total_size = surf->surf_size;
2212 surf->alignment = surf->surf_alignment;
2213
2214 if (surf->htile_size) {
2215 surf->htile_offset = align64(surf->total_size, surf->htile_alignment);
2216 surf->total_size = surf->htile_offset + surf->htile_size;
2217 surf->alignment = MAX2(surf->alignment, surf->htile_alignment);
2218 }
2219
2220 if (surf->fmask_size) {
2221 assert(config->info.samples >= 2);
2222 surf->fmask_offset = align64(surf->total_size, surf->fmask_alignment);
2223 surf->total_size = surf->fmask_offset + surf->fmask_size;
2224 surf->alignment = MAX2(surf->alignment, surf->fmask_alignment);
2225 }
2226
2227 /* Single-sample CMASK is in a separate buffer. */
2228 if (surf->cmask_size && config->info.samples >= 2) {
2229 surf->cmask_offset = align64(surf->total_size, surf->cmask_alignment);
2230 surf->total_size = surf->cmask_offset + surf->cmask_size;
2231 surf->alignment = MAX2(surf->alignment, surf->cmask_alignment);
2232 }
2233
2234 if (surf->is_displayable)
2235 surf->flags |= RADEON_SURF_SCANOUT;
2236
2237 if (surf->dcc_size &&
2238 /* dcc_size is computed on GFX9+ only if it's displayable. */
2239 (info->chip_class >= GFX9 || !get_display_flag(config, surf))) {
2240 /* It's better when displayable DCC is immediately after
2241 * the image due to hw-specific reasons.
2242 */
2243 if (info->chip_class >= GFX9 &&
2244 surf->u.gfx9.dcc_retile_num_elements) {
2245 /* Add space for the displayable DCC buffer. */
2246 surf->display_dcc_offset =
2247 align64(surf->total_size, surf->u.gfx9.display_dcc_alignment);
2248 surf->total_size = surf->display_dcc_offset +
2249 surf->u.gfx9.display_dcc_size;
2250
2251 /* Add space for the DCC retile buffer. (16-bit or 32-bit elements) */
2252 surf->dcc_retile_map_offset =
2253 align64(surf->total_size, info->tcc_cache_line_size);
2254
2255 if (surf->u.gfx9.dcc_retile_use_uint16) {
2256 surf->total_size = surf->dcc_retile_map_offset +
2257 surf->u.gfx9.dcc_retile_num_elements * 2;
2258 } else {
2259 surf->total_size = surf->dcc_retile_map_offset +
2260 surf->u.gfx9.dcc_retile_num_elements * 4;
2261 }
2262 }
2263
2264 surf->dcc_offset = align64(surf->total_size, surf->dcc_alignment);
2265 surf->total_size = surf->dcc_offset + surf->dcc_size;
2266 surf->alignment = MAX2(surf->alignment, surf->dcc_alignment);
2267 }
2268
2269 return 0;
2270 }
2271
2272 /* This is meant to be used for disabling DCC. */
2273 void ac_surface_zero_dcc_fields(struct radeon_surf *surf)
2274 {
2275 surf->dcc_offset = 0;
2276 surf->display_dcc_offset = 0;
2277 surf->dcc_retile_map_offset = 0;
2278 }
2279
2280 static unsigned eg_tile_split(unsigned tile_split)
2281 {
2282 switch (tile_split) {
2283 case 0: tile_split = 64; break;
2284 case 1: tile_split = 128; break;
2285 case 2: tile_split = 256; break;
2286 case 3: tile_split = 512; break;
2287 default:
2288 case 4: tile_split = 1024; break;
2289 case 5: tile_split = 2048; break;
2290 case 6: tile_split = 4096; break;
2291 }
2292 return tile_split;
2293 }
2294
2295 static unsigned eg_tile_split_rev(unsigned eg_tile_split)
2296 {
2297 switch (eg_tile_split) {
2298 case 64: return 0;
2299 case 128: return 1;
2300 case 256: return 2;
2301 case 512: return 3;
2302 default:
2303 case 1024: return 4;
2304 case 2048: return 5;
2305 case 4096: return 6;
2306 }
2307 }
2308
2309 #define AMDGPU_TILING_DCC_MAX_COMPRESSED_BLOCK_SIZE_SHIFT 45
2310 #define AMDGPU_TILING_DCC_MAX_COMPRESSED_BLOCK_SIZE_MASK 0x3
2311
2312 /* This should be called before ac_compute_surface. */
2313 void ac_surface_set_bo_metadata(const struct radeon_info *info,
2314 struct radeon_surf *surf, uint64_t tiling_flags,
2315 enum radeon_surf_mode *mode)
2316 {
2317 bool scanout;
2318
2319 if (info->chip_class >= GFX9) {
2320 surf->u.gfx9.surf.swizzle_mode = AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
2321 surf->u.gfx9.dcc.independent_64B_blocks = AMDGPU_TILING_GET(tiling_flags, DCC_INDEPENDENT_64B);
2322 surf->u.gfx9.dcc.independent_128B_blocks = AMDGPU_TILING_GET(tiling_flags, DCC_INDEPENDENT_128B);
2323 surf->u.gfx9.dcc.max_compressed_block_size = AMDGPU_TILING_GET(tiling_flags, DCC_MAX_COMPRESSED_BLOCK_SIZE);
2324 surf->u.gfx9.display_dcc_pitch_max = AMDGPU_TILING_GET(tiling_flags, DCC_PITCH_MAX);
2325 scanout = AMDGPU_TILING_GET(tiling_flags, SCANOUT);
2326 *mode = surf->u.gfx9.surf.swizzle_mode > 0 ? RADEON_SURF_MODE_2D : RADEON_SURF_MODE_LINEAR_ALIGNED;
2327 } else {
2328 surf->u.legacy.pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2329 surf->u.legacy.bankw = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2330 surf->u.legacy.bankh = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2331 surf->u.legacy.tile_split = eg_tile_split(AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT));
2332 surf->u.legacy.mtilea = 1 << AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2333 surf->u.legacy.num_banks = 2 << AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2334 scanout = AMDGPU_TILING_GET(tiling_flags, MICRO_TILE_MODE) == 0; /* DISPLAY */
2335
2336 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 4) /* 2D_TILED_THIN1 */
2337 *mode = RADEON_SURF_MODE_2D;
2338 else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 2) /* 1D_TILED_THIN1 */
2339 *mode = RADEON_SURF_MODE_1D;
2340 else
2341 *mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
2342 }
2343
2344 if (scanout)
2345 surf->flags |= RADEON_SURF_SCANOUT;
2346 else
2347 surf->flags &= ~RADEON_SURF_SCANOUT;
2348 }
2349
2350 void ac_surface_get_bo_metadata(const struct radeon_info *info,
2351 struct radeon_surf *surf, uint64_t *tiling_flags)
2352 {
2353 *tiling_flags = 0;
2354
2355 if (info->chip_class >= GFX9) {
2356 uint64_t dcc_offset = 0;
2357
2358 if (surf->dcc_offset) {
2359 dcc_offset = surf->display_dcc_offset ? surf->display_dcc_offset
2360 : surf->dcc_offset;
2361 assert((dcc_offset >> 8) != 0 && (dcc_offset >> 8) < (1 << 24));
2362 }
2363
2364 *tiling_flags |= AMDGPU_TILING_SET(SWIZZLE_MODE, surf->u.gfx9.surf.swizzle_mode);
2365 *tiling_flags |= AMDGPU_TILING_SET(DCC_OFFSET_256B, dcc_offset >> 8);
2366 *tiling_flags |= AMDGPU_TILING_SET(DCC_PITCH_MAX, surf->u.gfx9.display_dcc_pitch_max);
2367 *tiling_flags |= AMDGPU_TILING_SET(DCC_INDEPENDENT_64B, surf->u.gfx9.dcc.independent_64B_blocks);
2368 *tiling_flags |= AMDGPU_TILING_SET(DCC_INDEPENDENT_128B, surf->u.gfx9.dcc.independent_128B_blocks);
2369 *tiling_flags |= AMDGPU_TILING_SET(DCC_MAX_COMPRESSED_BLOCK_SIZE, surf->u.gfx9.dcc.max_compressed_block_size);
2370 *tiling_flags |= AMDGPU_TILING_SET(SCANOUT, (surf->flags & RADEON_SURF_SCANOUT) != 0);
2371 } else {
2372 if (surf->u.legacy.level[0].mode >= RADEON_SURF_MODE_2D)
2373 *tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 4); /* 2D_TILED_THIN1 */
2374 else if (surf->u.legacy.level[0].mode >= RADEON_SURF_MODE_1D)
2375 *tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 2); /* 1D_TILED_THIN1 */
2376 else
2377 *tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 1); /* LINEAR_ALIGNED */
2378
2379 *tiling_flags |= AMDGPU_TILING_SET(PIPE_CONFIG, surf->u.legacy.pipe_config);
2380 *tiling_flags |= AMDGPU_TILING_SET(BANK_WIDTH, util_logbase2(surf->u.legacy.bankw));
2381 *tiling_flags |= AMDGPU_TILING_SET(BANK_HEIGHT, util_logbase2(surf->u.legacy.bankh));
2382 if (surf->u.legacy.tile_split)
2383 *tiling_flags |= AMDGPU_TILING_SET(TILE_SPLIT, eg_tile_split_rev(surf->u.legacy.tile_split));
2384 *tiling_flags |= AMDGPU_TILING_SET(MACRO_TILE_ASPECT, util_logbase2(surf->u.legacy.mtilea));
2385 *tiling_flags |= AMDGPU_TILING_SET(NUM_BANKS, util_logbase2(surf->u.legacy.num_banks)-1);
2386
2387 if (surf->flags & RADEON_SURF_SCANOUT)
2388 *tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 0); /* DISPLAY_MICRO_TILING */
2389 else
2390 *tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 1); /* THIN_MICRO_TILING */
2391 }
2392 }
2393
2394 static uint32_t ac_get_umd_metadata_word1(const struct radeon_info *info)
2395 {
2396 return (ATI_VENDOR_ID << 16) | info->pci_id;
2397 }
2398
2399 /* This should be called after ac_compute_surface. */
2400 bool ac_surface_set_umd_metadata(const struct radeon_info *info,
2401 struct radeon_surf *surf,
2402 unsigned num_storage_samples,
2403 unsigned num_mipmap_levels,
2404 unsigned size_metadata,
2405 uint32_t metadata[64])
2406 {
2407 uint32_t *desc = &metadata[2];
2408 uint64_t offset;
2409
2410 if (info->chip_class >= GFX9)
2411 offset = surf->u.gfx9.surf_offset;
2412 else
2413 offset = surf->u.legacy.level[0].offset;
2414
2415 if (offset || /* Non-zero planes ignore metadata. */
2416 size_metadata < 10 * 4 || /* at least 2(header) + 8(desc) dwords */
2417 metadata[0] == 0 || /* invalid version number */
2418 metadata[1] != ac_get_umd_metadata_word1(info)) /* invalid PCI ID */ {
2419 /* Disable DCC because it might not be enabled. */
2420 ac_surface_zero_dcc_fields(surf);
2421
2422 /* Don't report an error if the texture comes from an incompatible driver,
2423 * but this might not work.
2424 */
2425 return true;
2426 }
2427
2428 /* Validate that sample counts and the number of mipmap levels match. */
2429 unsigned desc_last_level = G_008F1C_LAST_LEVEL(desc[3]);
2430 unsigned type = G_008F1C_TYPE(desc[3]);
2431
2432 if (type == V_008F1C_SQ_RSRC_IMG_2D_MSAA || type == V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY) {
2433 unsigned log_samples = util_logbase2(MAX2(1, num_storage_samples));
2434
2435 if (desc_last_level != log_samples) {
2436 fprintf(stderr,
2437 "amdgpu: invalid MSAA texture import, "
2438 "metadata has log2(samples) = %u, the caller set %u\n",
2439 desc_last_level, log_samples);
2440 return false;
2441 }
2442 } else {
2443 if (desc_last_level != num_mipmap_levels - 1) {
2444 fprintf(stderr,
2445 "amdgpu: invalid mipmapped texture import, "
2446 "metadata has last_level = %u, the caller set %u\n",
2447 desc_last_level, num_mipmap_levels - 1);
2448 return false;
2449 }
2450 }
2451
2452 if (info->chip_class >= GFX8 && G_008F28_COMPRESSION_EN(desc[6])) {
2453 /* Read DCC information. */
2454 switch (info->chip_class) {
2455 case GFX8:
2456 surf->dcc_offset = (uint64_t)desc[7] << 8;
2457 break;
2458
2459 case GFX9:
2460 surf->dcc_offset =
2461 ((uint64_t)desc[7] << 8) | ((uint64_t)G_008F24_META_DATA_ADDRESS(desc[5]) << 40);
2462 surf->u.gfx9.dcc.pipe_aligned = G_008F24_META_PIPE_ALIGNED(desc[5]);
2463 surf->u.gfx9.dcc.rb_aligned = G_008F24_META_RB_ALIGNED(desc[5]);
2464
2465 /* If DCC is unaligned, this can only be a displayable image. */
2466 if (!surf->u.gfx9.dcc.pipe_aligned && !surf->u.gfx9.dcc.rb_aligned)
2467 assert(surf->is_displayable);
2468 break;
2469
2470 case GFX10:
2471 case GFX10_3:
2472 surf->dcc_offset =
2473 ((uint64_t)G_00A018_META_DATA_ADDRESS_LO(desc[6]) << 8) | ((uint64_t)desc[7] << 16);
2474 surf->u.gfx9.dcc.pipe_aligned = G_00A018_META_PIPE_ALIGNED(desc[6]);
2475 break;
2476
2477 default:
2478 assert(0);
2479 return false;
2480 }
2481 } else {
2482 /* Disable DCC. dcc_offset is always set by texture_from_handle
2483 * and must be cleared here.
2484 */
2485 ac_surface_zero_dcc_fields(surf);
2486 }
2487
2488 return true;
2489 }
2490
2491 void ac_surface_get_umd_metadata(const struct radeon_info *info,
2492 struct radeon_surf *surf,
2493 unsigned num_mipmap_levels,
2494 uint32_t desc[8],
2495 unsigned *size_metadata, uint32_t metadata[64])
2496 {
2497 /* Clear the base address and set the relative DCC offset. */
2498 desc[0] = 0;
2499 desc[1] &= C_008F14_BASE_ADDRESS_HI;
2500
2501 switch (info->chip_class) {
2502 case GFX6:
2503 case GFX7:
2504 break;
2505 case GFX8:
2506 desc[7] = surf->dcc_offset >> 8;
2507 break;
2508 case GFX9:
2509 desc[7] = surf->dcc_offset >> 8;
2510 desc[5] &= C_008F24_META_DATA_ADDRESS;
2511 desc[5] |= S_008F24_META_DATA_ADDRESS(surf->dcc_offset >> 40);
2512 break;
2513 case GFX10:
2514 case GFX10_3:
2515 desc[6] &= C_00A018_META_DATA_ADDRESS_LO;
2516 desc[6] |= S_00A018_META_DATA_ADDRESS_LO(surf->dcc_offset >> 8);
2517 desc[7] = surf->dcc_offset >> 16;
2518 break;
2519 default:
2520 assert(0);
2521 }
2522
2523 /* Metadata image format format version 1:
2524 * [0] = 1 (metadata format identifier)
2525 * [1] = (VENDOR_ID << 16) | PCI_ID
2526 * [2:9] = image descriptor for the whole resource
2527 * [2] is always 0, because the base address is cleared
2528 * [9] is the DCC offset bits [39:8] from the beginning of
2529 * the buffer
2530 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
2531 */
2532
2533 metadata[0] = 1; /* metadata image format version 1 */
2534
2535 /* Tiling modes are ambiguous without a PCI ID. */
2536 metadata[1] = ac_get_umd_metadata_word1(info);
2537
2538 /* Dwords [2:9] contain the image descriptor. */
2539 memcpy(&metadata[2], desc, 8 * 4);
2540 *size_metadata = 10 * 4;
2541
2542 /* Dwords [10:..] contain the mipmap level offsets. */
2543 if (info->chip_class <= GFX8) {
2544 for (unsigned i = 0; i < num_mipmap_levels; i++)
2545 metadata[10 + i] = surf->u.legacy.level[i].offset >> 8;
2546
2547 *size_metadata += num_mipmap_levels * 4;
2548 }
2549 }
2550
2551 void ac_surface_override_offset_stride(const struct radeon_info *info,
2552 struct radeon_surf *surf,
2553 unsigned num_mipmap_levels,
2554 uint64_t offset, unsigned pitch)
2555 {
2556 if (info->chip_class >= GFX9) {
2557 if (pitch) {
2558 surf->u.gfx9.surf_pitch = pitch;
2559 if (num_mipmap_levels == 1)
2560 surf->u.gfx9.surf.epitch = pitch - 1;
2561 surf->u.gfx9.surf_slice_size =
2562 (uint64_t)pitch * surf->u.gfx9.surf_height * surf->bpe;
2563 }
2564 surf->u.gfx9.surf_offset = offset;
2565 if (surf->u.gfx9.stencil_offset)
2566 surf->u.gfx9.stencil_offset += offset;
2567 } else {
2568 if (pitch) {
2569 surf->u.legacy.level[0].nblk_x = pitch;
2570 surf->u.legacy.level[0].slice_size_dw =
2571 ((uint64_t)pitch * surf->u.legacy.level[0].nblk_y * surf->bpe) / 4;
2572 }
2573
2574 if (offset) {
2575 for (unsigned i = 0; i < ARRAY_SIZE(surf->u.legacy.level); ++i)
2576 surf->u.legacy.level[i].offset += offset;
2577 }
2578 }
2579
2580 if (surf->htile_offset)
2581 surf->htile_offset += offset;
2582 if (surf->fmask_offset)
2583 surf->fmask_offset += offset;
2584 if (surf->cmask_offset)
2585 surf->cmask_offset += offset;
2586 if (surf->dcc_offset)
2587 surf->dcc_offset += offset;
2588 if (surf->display_dcc_offset)
2589 surf->display_dcc_offset += offset;
2590 if (surf->dcc_retile_map_offset)
2591 surf->dcc_retile_map_offset += offset;
2592 }