2 * Copyright © 2011 Red Hat All Rights Reserved.
3 * Copyright © 2017 Advanced Micro Devices, Inc.
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:
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.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
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/macros.h"
33 #include "util/u_atomic.h"
34 #include "util/u_math.h"
40 #include <amdgpu_drm.h>
42 #include "addrlib/inc/addrinterface.h"
44 #ifndef CIASICIDGFXENGINE_SOUTHERNISLAND
45 #define CIASICIDGFXENGINE_SOUTHERNISLAND 0x0000000A
48 #ifndef CIASICIDGFXENGINE_ARCTICISLAND
49 #define CIASICIDGFXENGINE_ARCTICISLAND 0x0000000D
52 static void *ADDR_API
allocSysMem(const ADDR_ALLOCSYSMEM_INPUT
* pInput
)
54 return malloc(pInput
->sizeInBytes
);
57 static ADDR_E_RETURNCODE ADDR_API
freeSysMem(const ADDR_FREESYSMEM_INPUT
* pInput
)
59 free(pInput
->pVirtAddr
);
63 ADDR_HANDLE
amdgpu_addr_create(const struct radeon_info
*info
,
64 const struct amdgpu_gpu_info
*amdinfo
,
65 uint64_t *max_alignment
)
67 ADDR_CREATE_INPUT addrCreateInput
= {0};
68 ADDR_CREATE_OUTPUT addrCreateOutput
= {0};
69 ADDR_REGISTER_VALUE regValue
= {0};
70 ADDR_CREATE_FLAGS createFlags
= {{0}};
71 ADDR_GET_MAX_ALINGMENTS_OUTPUT addrGetMaxAlignmentsOutput
= {0};
72 ADDR_E_RETURNCODE addrRet
;
74 addrCreateInput
.size
= sizeof(ADDR_CREATE_INPUT
);
75 addrCreateOutput
.size
= sizeof(ADDR_CREATE_OUTPUT
);
77 regValue
.gbAddrConfig
= amdinfo
->gb_addr_cfg
;
78 createFlags
.value
= 0;
80 addrCreateInput
.chipFamily
= info
->family_id
;
81 addrCreateInput
.chipRevision
= info
->chip_external_rev
;
83 if (addrCreateInput
.chipFamily
== FAMILY_UNKNOWN
)
86 if (addrCreateInput
.chipFamily
>= FAMILY_AI
) {
87 addrCreateInput
.chipEngine
= CIASICIDGFXENGINE_ARCTICISLAND
;
88 regValue
.blockVarSizeLog2
= 0;
90 regValue
.noOfBanks
= amdinfo
->mc_arb_ramcfg
& 0x3;
91 regValue
.noOfRanks
= (amdinfo
->mc_arb_ramcfg
& 0x4) >> 2;
93 regValue
.backendDisables
= amdinfo
->enabled_rb_pipes_mask
;
94 regValue
.pTileConfig
= amdinfo
->gb_tile_mode
;
95 regValue
.noOfEntries
= ARRAY_SIZE(amdinfo
->gb_tile_mode
);
96 if (addrCreateInput
.chipFamily
== FAMILY_SI
) {
97 regValue
.pMacroTileConfig
= NULL
;
98 regValue
.noOfMacroEntries
= 0;
100 regValue
.pMacroTileConfig
= amdinfo
->gb_macro_tile_mode
;
101 regValue
.noOfMacroEntries
= ARRAY_SIZE(amdinfo
->gb_macro_tile_mode
);
104 createFlags
.useTileIndex
= 1;
105 createFlags
.useHtileSliceAlign
= 1;
107 addrCreateInput
.chipEngine
= CIASICIDGFXENGINE_SOUTHERNISLAND
;
110 addrCreateInput
.callbacks
.allocSysMem
= allocSysMem
;
111 addrCreateInput
.callbacks
.freeSysMem
= freeSysMem
;
112 addrCreateInput
.callbacks
.debugPrint
= 0;
113 addrCreateInput
.createFlags
= createFlags
;
114 addrCreateInput
.regValue
= regValue
;
116 addrRet
= AddrCreate(&addrCreateInput
, &addrCreateOutput
);
117 if (addrRet
!= ADDR_OK
)
121 addrRet
= AddrGetMaxAlignments(addrCreateOutput
.hLib
, &addrGetMaxAlignmentsOutput
);
122 if (addrRet
== ADDR_OK
){
123 *max_alignment
= addrGetMaxAlignmentsOutput
.baseAlign
;
126 return addrCreateOutput
.hLib
;
129 static int surf_config_sanity(const struct ac_surf_config
*config
,
132 /* FMASK is allocated together with the color surface and can't be
133 * allocated separately.
135 assert(!(flags
& RADEON_SURF_FMASK
));
136 if (flags
& RADEON_SURF_FMASK
)
139 /* all dimension must be at least 1 ! */
140 if (!config
->info
.width
|| !config
->info
.height
|| !config
->info
.depth
||
141 !config
->info
.array_size
|| !config
->info
.levels
)
144 switch (config
->info
.samples
) {
152 if (flags
& RADEON_SURF_Z_OR_SBUFFER
)
159 if (!(flags
& RADEON_SURF_Z_OR_SBUFFER
)) {
160 switch (config
->info
.storage_samples
) {
172 if (config
->is_3d
&& config
->info
.array_size
> 1)
174 if (config
->is_cube
&& config
->info
.depth
> 1)
180 static int gfx6_compute_level(ADDR_HANDLE addrlib
,
181 const struct ac_surf_config
*config
,
182 struct radeon_surf
*surf
, bool is_stencil
,
183 unsigned level
, bool compressed
,
184 ADDR_COMPUTE_SURFACE_INFO_INPUT
*AddrSurfInfoIn
,
185 ADDR_COMPUTE_SURFACE_INFO_OUTPUT
*AddrSurfInfoOut
,
186 ADDR_COMPUTE_DCCINFO_INPUT
*AddrDccIn
,
187 ADDR_COMPUTE_DCCINFO_OUTPUT
*AddrDccOut
,
188 ADDR_COMPUTE_HTILE_INFO_INPUT
*AddrHtileIn
,
189 ADDR_COMPUTE_HTILE_INFO_OUTPUT
*AddrHtileOut
)
191 struct legacy_surf_level
*surf_level
;
192 ADDR_E_RETURNCODE ret
;
194 AddrSurfInfoIn
->mipLevel
= level
;
195 AddrSurfInfoIn
->width
= u_minify(config
->info
.width
, level
);
196 AddrSurfInfoIn
->height
= u_minify(config
->info
.height
, level
);
198 /* Make GFX6 linear surfaces compatible with GFX9 for hybrid graphics,
199 * because GFX9 needs linear alignment of 256 bytes.
201 if (config
->info
.levels
== 1 &&
202 AddrSurfInfoIn
->tileMode
== ADDR_TM_LINEAR_ALIGNED
&&
203 AddrSurfInfoIn
->bpp
&&
204 util_is_power_of_two_or_zero(AddrSurfInfoIn
->bpp
)) {
205 unsigned alignment
= 256 / (AddrSurfInfoIn
->bpp
/ 8);
207 AddrSurfInfoIn
->width
= align(AddrSurfInfoIn
->width
, alignment
);
211 AddrSurfInfoIn
->numSlices
= u_minify(config
->info
.depth
, level
);
212 else if (config
->is_cube
)
213 AddrSurfInfoIn
->numSlices
= 6;
215 AddrSurfInfoIn
->numSlices
= config
->info
.array_size
;
218 /* Set the base level pitch. This is needed for calculation
219 * of non-zero levels. */
221 AddrSurfInfoIn
->basePitch
= surf
->u
.legacy
.stencil_level
[0].nblk_x
;
223 AddrSurfInfoIn
->basePitch
= surf
->u
.legacy
.level
[0].nblk_x
;
225 /* Convert blocks to pixels for compressed formats. */
227 AddrSurfInfoIn
->basePitch
*= surf
->blk_w
;
230 ret
= AddrComputeSurfaceInfo(addrlib
,
233 if (ret
!= ADDR_OK
) {
237 surf_level
= is_stencil
? &surf
->u
.legacy
.stencil_level
[level
] : &surf
->u
.legacy
.level
[level
];
238 surf_level
->offset
= align64(surf
->surf_size
, AddrSurfInfoOut
->baseAlign
);
239 surf_level
->slice_size_dw
= AddrSurfInfoOut
->sliceSize
/ 4;
240 surf_level
->nblk_x
= AddrSurfInfoOut
->pitch
;
241 surf_level
->nblk_y
= AddrSurfInfoOut
->height
;
243 switch (AddrSurfInfoOut
->tileMode
) {
244 case ADDR_TM_LINEAR_ALIGNED
:
245 surf_level
->mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
247 case ADDR_TM_1D_TILED_THIN1
:
248 surf_level
->mode
= RADEON_SURF_MODE_1D
;
250 case ADDR_TM_2D_TILED_THIN1
:
251 surf_level
->mode
= RADEON_SURF_MODE_2D
;
258 surf
->u
.legacy
.stencil_tiling_index
[level
] = AddrSurfInfoOut
->tileIndex
;
260 surf
->u
.legacy
.tiling_index
[level
] = AddrSurfInfoOut
->tileIndex
;
262 surf
->surf_size
= surf_level
->offset
+ AddrSurfInfoOut
->surfSize
;
264 /* Clear DCC fields at the beginning. */
265 surf_level
->dcc_offset
= 0;
267 /* The previous level's flag tells us if we can use DCC for this level. */
268 if (AddrSurfInfoIn
->flags
.dccCompatible
&&
269 (level
== 0 || AddrDccOut
->subLvlCompressible
)) {
270 bool prev_level_clearable
= level
== 0 ||
271 AddrDccOut
->dccRamSizeAligned
;
273 AddrDccIn
->colorSurfSize
= AddrSurfInfoOut
->surfSize
;
274 AddrDccIn
->tileMode
= AddrSurfInfoOut
->tileMode
;
275 AddrDccIn
->tileInfo
= *AddrSurfInfoOut
->pTileInfo
;
276 AddrDccIn
->tileIndex
= AddrSurfInfoOut
->tileIndex
;
277 AddrDccIn
->macroModeIndex
= AddrSurfInfoOut
->macroModeIndex
;
279 ret
= AddrComputeDccInfo(addrlib
,
283 if (ret
== ADDR_OK
) {
284 surf_level
->dcc_offset
= surf
->dcc_size
;
285 surf
->num_dcc_levels
= level
+ 1;
286 surf
->dcc_size
= surf_level
->dcc_offset
+ AddrDccOut
->dccRamSize
;
287 surf
->dcc_alignment
= MAX2(surf
->dcc_alignment
, AddrDccOut
->dccRamBaseAlign
);
289 /* If the DCC size of a subresource (1 mip level or 1 slice)
290 * is not aligned, the DCC memory layout is not contiguous for
291 * that subresource, which means we can't use fast clear.
293 * We only do fast clears for whole mipmap levels. If we did
294 * per-slice fast clears, the same restriction would apply.
295 * (i.e. only compute the slice size and see if it's aligned)
297 * The last level can be non-contiguous and still be clearable
298 * if it's interleaved with the next level that doesn't exist.
300 if (AddrDccOut
->dccRamSizeAligned
||
301 (prev_level_clearable
&& level
== config
->info
.levels
- 1))
302 surf_level
->dcc_fast_clear_size
= AddrDccOut
->dccFastClearSize
;
304 surf_level
->dcc_fast_clear_size
= 0;
308 /* TC-compatible HTILE. */
310 AddrSurfInfoIn
->flags
.depth
&&
311 surf_level
->mode
== RADEON_SURF_MODE_2D
&&
313 AddrHtileIn
->flags
.tcCompatible
= AddrSurfInfoIn
->flags
.tcCompatible
;
314 AddrHtileIn
->pitch
= AddrSurfInfoOut
->pitch
;
315 AddrHtileIn
->height
= AddrSurfInfoOut
->height
;
316 AddrHtileIn
->numSlices
= AddrSurfInfoOut
->depth
;
317 AddrHtileIn
->blockWidth
= ADDR_HTILE_BLOCKSIZE_8
;
318 AddrHtileIn
->blockHeight
= ADDR_HTILE_BLOCKSIZE_8
;
319 AddrHtileIn
->pTileInfo
= AddrSurfInfoOut
->pTileInfo
;
320 AddrHtileIn
->tileIndex
= AddrSurfInfoOut
->tileIndex
;
321 AddrHtileIn
->macroModeIndex
= AddrSurfInfoOut
->macroModeIndex
;
323 ret
= AddrComputeHtileInfo(addrlib
,
327 if (ret
== ADDR_OK
) {
328 surf
->htile_size
= AddrHtileOut
->htileBytes
;
329 surf
->htile_slice_size
= AddrHtileOut
->sliceSize
;
330 surf
->htile_alignment
= AddrHtileOut
->baseAlign
;
337 #define G_009910_MICRO_TILE_MODE(x) (((x) >> 0) & 0x03)
338 #define V_009910_ADDR_SURF_THICK_MICRO_TILING 0x03
339 #define G_009910_MICRO_TILE_MODE_NEW(x) (((x) >> 22) & 0x07)
341 static void gfx6_set_micro_tile_mode(struct radeon_surf
*surf
,
342 const struct radeon_info
*info
)
344 uint32_t tile_mode
= info
->si_tile_mode_array
[surf
->u
.legacy
.tiling_index
[0]];
346 if (info
->chip_class
>= GFX7
)
347 surf
->micro_tile_mode
= G_009910_MICRO_TILE_MODE_NEW(tile_mode
);
349 surf
->micro_tile_mode
= G_009910_MICRO_TILE_MODE(tile_mode
);
352 static unsigned cik_get_macro_tile_index(struct radeon_surf
*surf
)
354 unsigned index
, tileb
;
356 tileb
= 8 * 8 * surf
->bpe
;
357 tileb
= MIN2(surf
->u
.legacy
.tile_split
, tileb
);
359 for (index
= 0; tileb
> 64; index
++)
366 static bool get_display_flag(const struct ac_surf_config
*config
,
367 const struct radeon_surf
*surf
)
369 unsigned num_channels
= config
->info
.num_channels
;
370 unsigned bpe
= surf
->bpe
;
372 if (!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
) &&
373 surf
->flags
& RADEON_SURF_SCANOUT
&&
374 config
->info
.samples
<= 1 &&
375 surf
->blk_w
<= 2 && surf
->blk_h
== 1) {
377 if (surf
->blk_w
== 2 && surf
->blk_h
== 1)
380 if (/* RGBA8 or RGBA16F */
381 (bpe
>= 4 && bpe
<= 8 && num_channels
== 4) ||
382 /* R5G6B5 or R5G5B5A1 */
383 (bpe
== 2 && num_channels
>= 3) ||
385 (bpe
== 1 && num_channels
== 1))
392 * This must be called after the first level is computed.
394 * Copy surface-global settings like pipe/bank config from level 0 surface
395 * computation, and compute tile swizzle.
397 static int gfx6_surface_settings(ADDR_HANDLE addrlib
,
398 const struct radeon_info
*info
,
399 const struct ac_surf_config
*config
,
400 ADDR_COMPUTE_SURFACE_INFO_OUTPUT
* csio
,
401 struct radeon_surf
*surf
)
403 surf
->surf_alignment
= csio
->baseAlign
;
404 surf
->u
.legacy
.pipe_config
= csio
->pTileInfo
->pipeConfig
- 1;
405 gfx6_set_micro_tile_mode(surf
, info
);
407 /* For 2D modes only. */
408 if (csio
->tileMode
>= ADDR_TM_2D_TILED_THIN1
) {
409 surf
->u
.legacy
.bankw
= csio
->pTileInfo
->bankWidth
;
410 surf
->u
.legacy
.bankh
= csio
->pTileInfo
->bankHeight
;
411 surf
->u
.legacy
.mtilea
= csio
->pTileInfo
->macroAspectRatio
;
412 surf
->u
.legacy
.tile_split
= csio
->pTileInfo
->tileSplitBytes
;
413 surf
->u
.legacy
.num_banks
= csio
->pTileInfo
->banks
;
414 surf
->u
.legacy
.macro_tile_index
= csio
->macroModeIndex
;
416 surf
->u
.legacy
.macro_tile_index
= 0;
419 /* Compute tile swizzle. */
420 /* TODO: fix tile swizzle with mipmapping for GFX6 */
421 if ((info
->chip_class
>= GFX7
|| config
->info
.levels
== 1) &&
422 config
->info
.surf_index
&&
423 surf
->u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_2D
&&
424 !(surf
->flags
& (RADEON_SURF_Z_OR_SBUFFER
| RADEON_SURF_SHAREABLE
)) &&
425 !get_display_flag(config
, surf
)) {
426 ADDR_COMPUTE_BASE_SWIZZLE_INPUT AddrBaseSwizzleIn
= {0};
427 ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT AddrBaseSwizzleOut
= {0};
429 AddrBaseSwizzleIn
.size
= sizeof(ADDR_COMPUTE_BASE_SWIZZLE_INPUT
);
430 AddrBaseSwizzleOut
.size
= sizeof(ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT
);
432 AddrBaseSwizzleIn
.surfIndex
= p_atomic_inc_return(config
->info
.surf_index
) - 1;
433 AddrBaseSwizzleIn
.tileIndex
= csio
->tileIndex
;
434 AddrBaseSwizzleIn
.macroModeIndex
= csio
->macroModeIndex
;
435 AddrBaseSwizzleIn
.pTileInfo
= csio
->pTileInfo
;
436 AddrBaseSwizzleIn
.tileMode
= csio
->tileMode
;
438 int r
= AddrComputeBaseSwizzle(addrlib
, &AddrBaseSwizzleIn
,
439 &AddrBaseSwizzleOut
);
443 assert(AddrBaseSwizzleOut
.tileSwizzle
<=
444 u_bit_consecutive(0, sizeof(surf
->tile_swizzle
) * 8));
445 surf
->tile_swizzle
= AddrBaseSwizzleOut
.tileSwizzle
;
450 static void ac_compute_cmask(const struct radeon_info
*info
,
451 const struct ac_surf_config
*config
,
452 struct radeon_surf
*surf
)
454 unsigned pipe_interleave_bytes
= info
->pipe_interleave_bytes
;
455 unsigned num_pipes
= info
->num_tile_pipes
;
456 unsigned cl_width
, cl_height
;
458 if (surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
)
461 assert(info
->chip_class
<= GFX8
);
476 case 16: /* Hawaii */
485 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
487 unsigned width
= align(surf
->u
.legacy
.level
[0].nblk_x
, cl_width
*8);
488 unsigned height
= align(surf
->u
.legacy
.level
[0].nblk_y
, cl_height
*8);
489 unsigned slice_elements
= (width
* height
) / (8*8);
491 /* Each element of CMASK is a nibble. */
492 unsigned slice_bytes
= slice_elements
/ 2;
494 surf
->u
.legacy
.cmask_slice_tile_max
= (width
* height
) / (128*128);
495 if (surf
->u
.legacy
.cmask_slice_tile_max
)
496 surf
->u
.legacy
.cmask_slice_tile_max
-= 1;
500 num_layers
= config
->info
.depth
;
501 else if (config
->is_cube
)
504 num_layers
= config
->info
.array_size
;
506 surf
->cmask_alignment
= MAX2(256, base_align
);
507 surf
->cmask_size
= align(slice_bytes
, base_align
) * num_layers
;
511 * Fill in the tiling information in \p surf based on the given surface config.
513 * The following fields of \p surf must be initialized by the caller:
514 * blk_w, blk_h, bpe, flags.
516 static int gfx6_compute_surface(ADDR_HANDLE addrlib
,
517 const struct radeon_info
*info
,
518 const struct ac_surf_config
*config
,
519 enum radeon_surf_mode mode
,
520 struct radeon_surf
*surf
)
524 ADDR_COMPUTE_SURFACE_INFO_INPUT AddrSurfInfoIn
= {0};
525 ADDR_COMPUTE_SURFACE_INFO_OUTPUT AddrSurfInfoOut
= {0};
526 ADDR_COMPUTE_DCCINFO_INPUT AddrDccIn
= {0};
527 ADDR_COMPUTE_DCCINFO_OUTPUT AddrDccOut
= {0};
528 ADDR_COMPUTE_HTILE_INFO_INPUT AddrHtileIn
= {0};
529 ADDR_COMPUTE_HTILE_INFO_OUTPUT AddrHtileOut
= {0};
530 ADDR_TILEINFO AddrTileInfoIn
= {0};
531 ADDR_TILEINFO AddrTileInfoOut
= {0};
534 AddrSurfInfoIn
.size
= sizeof(ADDR_COMPUTE_SURFACE_INFO_INPUT
);
535 AddrSurfInfoOut
.size
= sizeof(ADDR_COMPUTE_SURFACE_INFO_OUTPUT
);
536 AddrDccIn
.size
= sizeof(ADDR_COMPUTE_DCCINFO_INPUT
);
537 AddrDccOut
.size
= sizeof(ADDR_COMPUTE_DCCINFO_OUTPUT
);
538 AddrHtileIn
.size
= sizeof(ADDR_COMPUTE_HTILE_INFO_INPUT
);
539 AddrHtileOut
.size
= sizeof(ADDR_COMPUTE_HTILE_INFO_OUTPUT
);
540 AddrSurfInfoOut
.pTileInfo
= &AddrTileInfoOut
;
542 compressed
= surf
->blk_w
== 4 && surf
->blk_h
== 4;
544 /* MSAA requires 2D tiling. */
545 if (config
->info
.samples
> 1)
546 mode
= RADEON_SURF_MODE_2D
;
548 /* DB doesn't support linear layouts. */
549 if (surf
->flags
& (RADEON_SURF_Z_OR_SBUFFER
) &&
550 mode
< RADEON_SURF_MODE_1D
)
551 mode
= RADEON_SURF_MODE_1D
;
553 /* Set the requested tiling mode. */
555 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
556 AddrSurfInfoIn
.tileMode
= ADDR_TM_LINEAR_ALIGNED
;
558 case RADEON_SURF_MODE_1D
:
559 AddrSurfInfoIn
.tileMode
= ADDR_TM_1D_TILED_THIN1
;
561 case RADEON_SURF_MODE_2D
:
562 AddrSurfInfoIn
.tileMode
= ADDR_TM_2D_TILED_THIN1
;
568 /* The format must be set correctly for the allocation of compressed
569 * textures to work. In other cases, setting the bpp is sufficient.
574 AddrSurfInfoIn
.format
= ADDR_FMT_BC1
;
577 AddrSurfInfoIn
.format
= ADDR_FMT_BC3
;
584 AddrDccIn
.bpp
= AddrSurfInfoIn
.bpp
= surf
->bpe
* 8;
587 AddrDccIn
.numSamples
= AddrSurfInfoIn
.numSamples
=
588 MAX2(1, config
->info
.samples
);
589 AddrSurfInfoIn
.tileIndex
= -1;
591 if (!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
)) {
592 AddrDccIn
.numSamples
= AddrSurfInfoIn
.numFrags
=
593 MAX2(1, config
->info
.storage_samples
);
596 /* Set the micro tile type. */
597 if (surf
->flags
& RADEON_SURF_SCANOUT
)
598 AddrSurfInfoIn
.tileType
= ADDR_DISPLAYABLE
;
599 else if (surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
)
600 AddrSurfInfoIn
.tileType
= ADDR_DEPTH_SAMPLE_ORDER
;
602 AddrSurfInfoIn
.tileType
= ADDR_NON_DISPLAYABLE
;
604 AddrSurfInfoIn
.flags
.color
= !(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
);
605 AddrSurfInfoIn
.flags
.depth
= (surf
->flags
& RADEON_SURF_ZBUFFER
) != 0;
606 AddrSurfInfoIn
.flags
.cube
= config
->is_cube
;
607 AddrSurfInfoIn
.flags
.display
= get_display_flag(config
, surf
);
608 AddrSurfInfoIn
.flags
.pow2Pad
= config
->info
.levels
> 1;
609 AddrSurfInfoIn
.flags
.tcCompatible
= (surf
->flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
) != 0;
611 /* Only degrade the tile mode for space if TC-compatible HTILE hasn't been
612 * requested, because TC-compatible HTILE requires 2D tiling.
614 AddrSurfInfoIn
.flags
.opt4Space
= !AddrSurfInfoIn
.flags
.tcCompatible
&&
615 !AddrSurfInfoIn
.flags
.fmask
&&
616 config
->info
.samples
<= 1 &&
617 (surf
->flags
& RADEON_SURF_OPTIMIZE_FOR_SPACE
);
620 * - If we add MSAA support, keep in mind that CB can't decompress 8bpp
622 * - Mipmapped array textures have low performance (discovered by a closed
625 AddrSurfInfoIn
.flags
.dccCompatible
=
626 info
->chip_class
>= GFX8
&&
627 !(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
) &&
628 !(surf
->flags
& RADEON_SURF_DISABLE_DCC
) &&
630 ((config
->info
.array_size
== 1 && config
->info
.depth
== 1) ||
631 config
->info
.levels
== 1);
633 AddrSurfInfoIn
.flags
.noStencil
= (surf
->flags
& RADEON_SURF_SBUFFER
) == 0;
634 AddrSurfInfoIn
.flags
.compressZ
= !!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
);
636 /* On GFX7-GFX8, the DB uses the same pitch and tile mode (except tilesplit)
637 * for Z and stencil. This can cause a number of problems which we work
640 * - a depth part that is incompatible with mipmapped texturing
641 * - at least on Stoney, entirely incompatible Z/S aspects (e.g.
642 * incorrect tiling applied to the stencil part, stencil buffer
643 * memory accesses that go out of bounds) even without mipmapping
645 * Some piglit tests that are prone to different types of related
647 * ./bin/ext_framebuffer_multisample-upsample 2 stencil
648 * ./bin/framebuffer-blit-levels {draw,read} stencil
649 * ./bin/ext_framebuffer_multisample-unaligned-blit N {depth,stencil} {msaa,upsample,downsample}
650 * ./bin/fbo-depth-array fs-writes-{depth,stencil} / {depth,stencil}-{clear,layered-clear,draw}
651 * ./bin/depthstencil-render-miplevels 1024 d=s=z24_s8
653 int stencil_tile_idx
= -1;
655 if (AddrSurfInfoIn
.flags
.depth
&& !AddrSurfInfoIn
.flags
.noStencil
&&
656 (config
->info
.levels
> 1 || info
->family
== CHIP_STONEY
)) {
657 /* Compute stencilTileIdx that is compatible with the (depth)
658 * tileIdx. This degrades the depth surface if necessary to
659 * ensure that a matching stencilTileIdx exists. */
660 AddrSurfInfoIn
.flags
.matchStencilTileCfg
= 1;
662 /* Keep the depth mip-tail compatible with texturing. */
663 AddrSurfInfoIn
.flags
.noStencil
= 1;
666 /* Set preferred macrotile parameters. This is usually required
667 * for shared resources. This is for 2D tiling only. */
668 if (AddrSurfInfoIn
.tileMode
>= ADDR_TM_2D_TILED_THIN1
&&
669 surf
->u
.legacy
.bankw
&& surf
->u
.legacy
.bankh
&&
670 surf
->u
.legacy
.mtilea
&& surf
->u
.legacy
.tile_split
) {
671 /* If any of these parameters are incorrect, the calculation
673 AddrTileInfoIn
.banks
= surf
->u
.legacy
.num_banks
;
674 AddrTileInfoIn
.bankWidth
= surf
->u
.legacy
.bankw
;
675 AddrTileInfoIn
.bankHeight
= surf
->u
.legacy
.bankh
;
676 AddrTileInfoIn
.macroAspectRatio
= surf
->u
.legacy
.mtilea
;
677 AddrTileInfoIn
.tileSplitBytes
= surf
->u
.legacy
.tile_split
;
678 AddrTileInfoIn
.pipeConfig
= surf
->u
.legacy
.pipe_config
+ 1; /* +1 compared to GB_TILE_MODE */
679 AddrSurfInfoIn
.flags
.opt4Space
= 0;
680 AddrSurfInfoIn
.pTileInfo
= &AddrTileInfoIn
;
682 /* If AddrSurfInfoIn.pTileInfo is set, Addrlib doesn't set
683 * the tile index, because we are expected to know it if
684 * we know the other parameters.
686 * This is something that can easily be fixed in Addrlib.
687 * For now, just figure it out here.
688 * Note that only 2D_TILE_THIN1 is handled here.
690 assert(!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
));
691 assert(AddrSurfInfoIn
.tileMode
== ADDR_TM_2D_TILED_THIN1
);
693 if (info
->chip_class
== GFX6
) {
694 if (AddrSurfInfoIn
.tileType
== ADDR_DISPLAYABLE
) {
696 AddrSurfInfoIn
.tileIndex
= 11; /* 16bpp */
698 AddrSurfInfoIn
.tileIndex
= 12; /* 32bpp */
701 AddrSurfInfoIn
.tileIndex
= 14; /* 8bpp */
702 else if (surf
->bpe
== 2)
703 AddrSurfInfoIn
.tileIndex
= 15; /* 16bpp */
704 else if (surf
->bpe
== 4)
705 AddrSurfInfoIn
.tileIndex
= 16; /* 32bpp */
707 AddrSurfInfoIn
.tileIndex
= 17; /* 64bpp (and 128bpp) */
711 if (AddrSurfInfoIn
.tileType
== ADDR_DISPLAYABLE
)
712 AddrSurfInfoIn
.tileIndex
= 10; /* 2D displayable */
714 AddrSurfInfoIn
.tileIndex
= 14; /* 2D non-displayable */
716 /* Addrlib doesn't set this if tileIndex is forced like above. */
717 AddrSurfInfoOut
.macroModeIndex
= cik_get_macro_tile_index(surf
);
721 surf
->has_stencil
= !!(surf
->flags
& RADEON_SURF_SBUFFER
);
722 surf
->num_dcc_levels
= 0;
725 surf
->dcc_alignment
= 1;
726 surf
->htile_size
= 0;
727 surf
->htile_slice_size
= 0;
728 surf
->htile_alignment
= 1;
730 const bool only_stencil
= (surf
->flags
& RADEON_SURF_SBUFFER
) &&
731 !(surf
->flags
& RADEON_SURF_ZBUFFER
);
733 /* Calculate texture layout information. */
735 for (level
= 0; level
< config
->info
.levels
; level
++) {
736 r
= gfx6_compute_level(addrlib
, config
, surf
, false, level
, compressed
,
737 &AddrSurfInfoIn
, &AddrSurfInfoOut
,
738 &AddrDccIn
, &AddrDccOut
, &AddrHtileIn
, &AddrHtileOut
);
745 /* Check that we actually got a TC-compatible HTILE if
746 * we requested it (only for level 0, since we're not
747 * supporting HTILE on higher mip levels anyway). */
748 assert(AddrSurfInfoOut
.tcCompatible
||
749 !AddrSurfInfoIn
.flags
.tcCompatible
||
750 AddrSurfInfoIn
.flags
.matchStencilTileCfg
);
752 if (AddrSurfInfoIn
.flags
.matchStencilTileCfg
) {
753 if (!AddrSurfInfoOut
.tcCompatible
) {
754 AddrSurfInfoIn
.flags
.tcCompatible
= 0;
755 surf
->flags
&= ~RADEON_SURF_TC_COMPATIBLE_HTILE
;
758 AddrSurfInfoIn
.flags
.matchStencilTileCfg
= 0;
759 AddrSurfInfoIn
.tileIndex
= AddrSurfInfoOut
.tileIndex
;
760 stencil_tile_idx
= AddrSurfInfoOut
.stencilTileIdx
;
762 assert(stencil_tile_idx
>= 0);
765 r
= gfx6_surface_settings(addrlib
, info
, config
,
766 &AddrSurfInfoOut
, surf
);
772 /* Calculate texture layout information for stencil. */
773 if (surf
->flags
& RADEON_SURF_SBUFFER
) {
774 AddrSurfInfoIn
.tileIndex
= stencil_tile_idx
;
775 AddrSurfInfoIn
.bpp
= 8;
776 AddrSurfInfoIn
.flags
.depth
= 0;
777 AddrSurfInfoIn
.flags
.stencil
= 1;
778 AddrSurfInfoIn
.flags
.tcCompatible
= 0;
779 /* This will be ignored if AddrSurfInfoIn.pTileInfo is NULL. */
780 AddrTileInfoIn
.tileSplitBytes
= surf
->u
.legacy
.stencil_tile_split
;
782 for (level
= 0; level
< config
->info
.levels
; level
++) {
783 r
= gfx6_compute_level(addrlib
, config
, surf
, true, level
, compressed
,
784 &AddrSurfInfoIn
, &AddrSurfInfoOut
,
785 &AddrDccIn
, &AddrDccOut
,
790 /* DB uses the depth pitch for both stencil and depth. */
792 if (surf
->u
.legacy
.stencil_level
[level
].nblk_x
!=
793 surf
->u
.legacy
.level
[level
].nblk_x
)
794 surf
->u
.legacy
.stencil_adjusted
= true;
796 surf
->u
.legacy
.level
[level
].nblk_x
=
797 surf
->u
.legacy
.stencil_level
[level
].nblk_x
;
802 r
= gfx6_surface_settings(addrlib
, info
, config
,
803 &AddrSurfInfoOut
, surf
);
808 /* For 2D modes only. */
809 if (AddrSurfInfoOut
.tileMode
>= ADDR_TM_2D_TILED_THIN1
) {
810 surf
->u
.legacy
.stencil_tile_split
=
811 AddrSurfInfoOut
.pTileInfo
->tileSplitBytes
;
818 if (config
->info
.samples
>= 2 && AddrSurfInfoIn
.flags
.color
) {
819 ADDR_COMPUTE_FMASK_INFO_INPUT fin
= {0};
820 ADDR_COMPUTE_FMASK_INFO_OUTPUT fout
= {0};
821 ADDR_TILEINFO fmask_tile_info
= {};
823 fin
.size
= sizeof(fin
);
824 fout
.size
= sizeof(fout
);
826 fin
.tileMode
= AddrSurfInfoOut
.tileMode
;
827 fin
.pitch
= AddrSurfInfoOut
.pitch
;
828 fin
.height
= config
->info
.height
;
829 fin
.numSlices
= AddrSurfInfoIn
.numSlices
;
830 fin
.numSamples
= AddrSurfInfoIn
.numSamples
;
831 fin
.numFrags
= AddrSurfInfoIn
.numFrags
;
833 fout
.pTileInfo
= &fmask_tile_info
;
835 r
= AddrComputeFmaskInfo(addrlib
, &fin
, &fout
);
839 surf
->fmask_size
= fout
.fmaskBytes
;
840 surf
->fmask_alignment
= fout
.baseAlign
;
841 surf
->fmask_tile_swizzle
= 0;
843 surf
->u
.legacy
.fmask
.slice_tile_max
=
844 (fout
.pitch
* fout
.height
) / 64;
845 if (surf
->u
.legacy
.fmask
.slice_tile_max
)
846 surf
->u
.legacy
.fmask
.slice_tile_max
-= 1;
848 surf
->u
.legacy
.fmask
.tiling_index
= fout
.tileIndex
;
849 surf
->u
.legacy
.fmask
.bankh
= fout
.pTileInfo
->bankHeight
;
850 surf
->u
.legacy
.fmask
.pitch_in_pixels
= fout
.pitch
;
851 surf
->u
.legacy
.fmask
.slice_size
= fout
.sliceSize
;
853 /* Compute tile swizzle for FMASK. */
854 if (config
->info
.fmask_surf_index
&&
855 !(surf
->flags
& RADEON_SURF_SHAREABLE
)) {
856 ADDR_COMPUTE_BASE_SWIZZLE_INPUT xin
= {0};
857 ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT xout
= {0};
859 xin
.size
= sizeof(ADDR_COMPUTE_BASE_SWIZZLE_INPUT
);
860 xout
.size
= sizeof(ADDR_COMPUTE_BASE_SWIZZLE_OUTPUT
);
862 /* This counter starts from 1 instead of 0. */
863 xin
.surfIndex
= p_atomic_inc_return(config
->info
.fmask_surf_index
);
864 xin
.tileIndex
= fout
.tileIndex
;
865 xin
.macroModeIndex
= fout
.macroModeIndex
;
866 xin
.pTileInfo
= fout
.pTileInfo
;
867 xin
.tileMode
= fin
.tileMode
;
869 int r
= AddrComputeBaseSwizzle(addrlib
, &xin
, &xout
);
873 assert(xout
.tileSwizzle
<=
874 u_bit_consecutive(0, sizeof(surf
->tile_swizzle
) * 8));
875 surf
->fmask_tile_swizzle
= xout
.tileSwizzle
;
879 /* Recalculate the whole DCC miptree size including disabled levels.
880 * This is what addrlib does, but calling addrlib would be a lot more
883 if (surf
->dcc_size
&& config
->info
.levels
> 1) {
884 /* The smallest miplevels that are never compressed by DCC
885 * still read the DCC buffer via TC if the base level uses DCC,
886 * and for some reason the DCC buffer needs to be larger if
887 * the miptree uses non-zero tile_swizzle. Otherwise there are
890 * "dcc_alignment * 4" was determined by trial and error.
892 surf
->dcc_size
= align64(surf
->surf_size
>> 8,
893 surf
->dcc_alignment
* 4);
896 /* Make sure HTILE covers the whole miptree, because the shader reads
897 * TC-compatible HTILE even for levels where it's disabled by DB.
899 if (surf
->htile_size
&& config
->info
.levels
> 1 &&
900 surf
->flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
) {
901 /* MSAA can't occur with levels > 1, so ignore the sample count. */
902 const unsigned total_pixels
= surf
->surf_size
/ surf
->bpe
;
903 const unsigned htile_block_size
= 8 * 8;
904 const unsigned htile_element_size
= 4;
906 surf
->htile_size
= (total_pixels
/ htile_block_size
) *
908 surf
->htile_size
= align(surf
->htile_size
, surf
->htile_alignment
);
911 surf
->is_linear
= surf
->u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_LINEAR_ALIGNED
;
912 surf
->is_displayable
= surf
->is_linear
||
913 surf
->micro_tile_mode
== RADEON_MICRO_MODE_DISPLAY
||
914 surf
->micro_tile_mode
== RADEON_MICRO_MODE_ROTATED
;
916 /* The rotated micro tile mode doesn't work if both CMASK and RB+ are
917 * used at the same time. This case is not currently expected to occur
918 * because we don't use rotated. Enforce this restriction on all chips
919 * to facilitate testing.
921 if (surf
->micro_tile_mode
== RADEON_MICRO_MODE_ROTATED
) {
922 assert(!"rotate micro tile mode is unsupported");
926 ac_compute_cmask(info
, config
, surf
);
930 /* This is only called when expecting a tiled layout. */
932 gfx9_get_preferred_swizzle_mode(ADDR_HANDLE addrlib
,
933 ADDR2_COMPUTE_SURFACE_INFO_INPUT
*in
,
934 bool is_fmask
, AddrSwizzleMode
*swizzle_mode
)
936 ADDR_E_RETURNCODE ret
;
937 ADDR2_GET_PREFERRED_SURF_SETTING_INPUT sin
= {0};
938 ADDR2_GET_PREFERRED_SURF_SETTING_OUTPUT sout
= {0};
940 sin
.size
= sizeof(ADDR2_GET_PREFERRED_SURF_SETTING_INPUT
);
941 sout
.size
= sizeof(ADDR2_GET_PREFERRED_SURF_SETTING_OUTPUT
);
943 sin
.flags
= in
->flags
;
944 sin
.resourceType
= in
->resourceType
;
945 sin
.format
= in
->format
;
946 sin
.resourceLoction
= ADDR_RSRC_LOC_INVIS
;
947 /* TODO: We could allow some of these: */
948 sin
.forbiddenBlock
.micro
= 1; /* don't allow the 256B swizzle modes */
949 sin
.forbiddenBlock
.var
= 1; /* don't allow the variable-sized swizzle modes */
950 sin
.forbiddenBlock
.linear
= 1; /* don't allow linear swizzle modes */
952 sin
.width
= in
->width
;
953 sin
.height
= in
->height
;
954 sin
.numSlices
= in
->numSlices
;
955 sin
.numMipLevels
= in
->numMipLevels
;
956 sin
.numSamples
= in
->numSamples
;
957 sin
.numFrags
= in
->numFrags
;
960 sin
.flags
.display
= 0;
965 ret
= Addr2GetPreferredSurfaceSetting(addrlib
, &sin
, &sout
);
969 *swizzle_mode
= sout
.swizzleMode
;
973 static int gfx9_compute_miptree(ADDR_HANDLE addrlib
,
974 const struct radeon_info
*info
,
975 const struct ac_surf_config
*config
,
976 struct radeon_surf
*surf
, bool compressed
,
977 ADDR2_COMPUTE_SURFACE_INFO_INPUT
*in
)
979 ADDR2_MIP_INFO mip_info
[RADEON_SURF_MAX_LEVELS
] = {};
980 ADDR2_COMPUTE_SURFACE_INFO_OUTPUT out
= {0};
981 ADDR_E_RETURNCODE ret
;
983 out
.size
= sizeof(ADDR2_COMPUTE_SURFACE_INFO_OUTPUT
);
984 out
.pMipInfo
= mip_info
;
986 ret
= Addr2ComputeSurfaceInfo(addrlib
, in
, &out
);
990 if (in
->flags
.stencil
) {
991 surf
->u
.gfx9
.stencil
.swizzle_mode
= in
->swizzleMode
;
992 surf
->u
.gfx9
.stencil
.epitch
= out
.epitchIsHeight
? out
.mipChainHeight
- 1 :
993 out
.mipChainPitch
- 1;
994 surf
->surf_alignment
= MAX2(surf
->surf_alignment
, out
.baseAlign
);
995 surf
->u
.gfx9
.stencil_offset
= align(surf
->surf_size
, out
.baseAlign
);
996 surf
->surf_size
= surf
->u
.gfx9
.stencil_offset
+ out
.surfSize
;
1000 surf
->u
.gfx9
.surf
.swizzle_mode
= in
->swizzleMode
;
1001 surf
->u
.gfx9
.surf
.epitch
= out
.epitchIsHeight
? out
.mipChainHeight
- 1 :
1002 out
.mipChainPitch
- 1;
1004 /* CMASK fast clear uses these even if FMASK isn't allocated.
1005 * FMASK only supports the Z swizzle modes, whose numbers are multiples of 4.
1007 surf
->u
.gfx9
.fmask
.swizzle_mode
= surf
->u
.gfx9
.surf
.swizzle_mode
& ~0x3;
1008 surf
->u
.gfx9
.fmask
.epitch
= surf
->u
.gfx9
.surf
.epitch
;
1010 surf
->u
.gfx9
.surf_slice_size
= out
.sliceSize
;
1011 surf
->u
.gfx9
.surf_pitch
= out
.pitch
;
1012 surf
->u
.gfx9
.surf_height
= out
.height
;
1013 surf
->surf_size
= out
.surfSize
;
1014 surf
->surf_alignment
= out
.baseAlign
;
1016 if (in
->swizzleMode
== ADDR_SW_LINEAR
) {
1017 for (unsigned i
= 0; i
< in
->numMipLevels
; i
++)
1018 surf
->u
.gfx9
.offset
[i
] = mip_info
[i
].offset
;
1021 if (in
->flags
.depth
) {
1022 assert(in
->swizzleMode
!= ADDR_SW_LINEAR
);
1025 ADDR2_COMPUTE_HTILE_INFO_INPUT hin
= {0};
1026 ADDR2_COMPUTE_HTILE_INFO_OUTPUT hout
= {0};
1028 hin
.size
= sizeof(ADDR2_COMPUTE_HTILE_INFO_INPUT
);
1029 hout
.size
= sizeof(ADDR2_COMPUTE_HTILE_INFO_OUTPUT
);
1031 hin
.hTileFlags
.pipeAligned
= !in
->flags
.metaPipeUnaligned
;
1032 hin
.hTileFlags
.rbAligned
= !in
->flags
.metaRbUnaligned
;
1033 hin
.depthFlags
= in
->flags
;
1034 hin
.swizzleMode
= in
->swizzleMode
;
1035 hin
.unalignedWidth
= in
->width
;
1036 hin
.unalignedHeight
= in
->height
;
1037 hin
.numSlices
= in
->numSlices
;
1038 hin
.numMipLevels
= in
->numMipLevels
;
1039 hin
.firstMipIdInTail
= out
.firstMipIdInTail
;
1041 ret
= Addr2ComputeHtileInfo(addrlib
, &hin
, &hout
);
1045 surf
->u
.gfx9
.htile
.rb_aligned
= hin
.hTileFlags
.rbAligned
;
1046 surf
->u
.gfx9
.htile
.pipe_aligned
= hin
.hTileFlags
.pipeAligned
;
1047 surf
->htile_size
= hout
.htileBytes
;
1048 surf
->htile_slice_size
= hout
.sliceSize
;
1049 surf
->htile_alignment
= hout
.baseAlign
;
1051 /* Compute tile swizzle for the color surface.
1052 * All *_X and *_T modes can use the swizzle.
1054 if (config
->info
.surf_index
&&
1055 in
->swizzleMode
>= ADDR_SW_64KB_Z_T
&&
1056 !out
.mipChainInTail
&&
1057 !(surf
->flags
& RADEON_SURF_SHAREABLE
) &&
1058 !in
->flags
.display
) {
1059 ADDR2_COMPUTE_PIPEBANKXOR_INPUT xin
= {0};
1060 ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT xout
= {0};
1062 xin
.size
= sizeof(ADDR2_COMPUTE_PIPEBANKXOR_INPUT
);
1063 xout
.size
= sizeof(ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT
);
1065 xin
.surfIndex
= p_atomic_inc_return(config
->info
.surf_index
) - 1;
1066 xin
.flags
= in
->flags
;
1067 xin
.swizzleMode
= in
->swizzleMode
;
1068 xin
.resourceType
= in
->resourceType
;
1069 xin
.format
= in
->format
;
1070 xin
.numSamples
= in
->numSamples
;
1071 xin
.numFrags
= in
->numFrags
;
1073 ret
= Addr2ComputePipeBankXor(addrlib
, &xin
, &xout
);
1077 assert(xout
.pipeBankXor
<=
1078 u_bit_consecutive(0, sizeof(surf
->tile_swizzle
) * 8));
1079 surf
->tile_swizzle
= xout
.pipeBankXor
;
1083 if (!(surf
->flags
& RADEON_SURF_DISABLE_DCC
) &&
1085 in
->swizzleMode
!= ADDR_SW_LINEAR
) {
1086 ADDR2_COMPUTE_DCCINFO_INPUT din
= {0};
1087 ADDR2_COMPUTE_DCCINFO_OUTPUT dout
= {0};
1088 ADDR2_META_MIP_INFO meta_mip_info
[RADEON_SURF_MAX_LEVELS
] = {};
1090 din
.size
= sizeof(ADDR2_COMPUTE_DCCINFO_INPUT
);
1091 dout
.size
= sizeof(ADDR2_COMPUTE_DCCINFO_OUTPUT
);
1092 dout
.pMipInfo
= meta_mip_info
;
1094 din
.dccKeyFlags
.pipeAligned
= !in
->flags
.metaPipeUnaligned
;
1095 din
.dccKeyFlags
.rbAligned
= !in
->flags
.metaRbUnaligned
;
1096 din
.colorFlags
= in
->flags
;
1097 din
.resourceType
= in
->resourceType
;
1098 din
.swizzleMode
= in
->swizzleMode
;
1100 din
.unalignedWidth
= in
->width
;
1101 din
.unalignedHeight
= in
->height
;
1102 din
.numSlices
= in
->numSlices
;
1103 din
.numFrags
= in
->numFrags
;
1104 din
.numMipLevels
= in
->numMipLevels
;
1105 din
.dataSurfaceSize
= out
.surfSize
;
1106 din
.firstMipIdInTail
= out
.firstMipIdInTail
;
1108 ret
= Addr2ComputeDccInfo(addrlib
, &din
, &dout
);
1112 surf
->u
.gfx9
.dcc
.rb_aligned
= din
.dccKeyFlags
.rbAligned
;
1113 surf
->u
.gfx9
.dcc
.pipe_aligned
= din
.dccKeyFlags
.pipeAligned
;
1114 surf
->dcc_size
= dout
.dccRamSize
;
1115 surf
->dcc_alignment
= dout
.dccRamBaseAlign
;
1116 surf
->num_dcc_levels
= in
->numMipLevels
;
1118 /* Disable DCC for levels that are in the mip tail.
1120 * There are two issues that this is intended to
1123 * 1. Multiple mip levels may share a cache line. This
1124 * can lead to corruption when switching between
1125 * rendering to different mip levels because the
1126 * RBs don't maintain coherency.
1128 * 2. Texturing with metadata after rendering sometimes
1129 * fails with corruption, probably for a similar
1132 * Working around these issues for all levels in the
1133 * mip tail may be overly conservative, but it's what
1136 * Alternative solutions that also work but are worse:
1137 * - Disable DCC entirely.
1138 * - Flush TC L2 after rendering.
1140 for (unsigned i
= 0; i
< in
->numMipLevels
; i
++) {
1141 if (meta_mip_info
[i
].inMiptail
) {
1142 surf
->num_dcc_levels
= i
;
1147 if (!surf
->num_dcc_levels
)
1150 surf
->u
.gfx9
.display_dcc_size
= surf
->dcc_size
;
1151 surf
->u
.gfx9
.display_dcc_alignment
= surf
->dcc_alignment
;
1152 surf
->u
.gfx9
.display_dcc_pitch_max
= dout
.pitch
- 1;
1154 /* Compute displayable DCC. */
1155 if (in
->flags
.display
&&
1156 surf
->num_dcc_levels
&&
1157 info
->use_display_dcc_with_retile_blit
) {
1158 /* Compute displayable DCC info. */
1159 din
.dccKeyFlags
.pipeAligned
= 0;
1160 din
.dccKeyFlags
.rbAligned
= 0;
1162 assert(din
.numSlices
== 1);
1163 assert(din
.numMipLevels
== 1);
1164 assert(din
.numFrags
== 1);
1165 assert(surf
->tile_swizzle
== 0);
1166 assert(surf
->u
.gfx9
.dcc
.pipe_aligned
||
1167 surf
->u
.gfx9
.dcc
.rb_aligned
);
1169 ret
= Addr2ComputeDccInfo(addrlib
, &din
, &dout
);
1173 surf
->u
.gfx9
.display_dcc_size
= dout
.dccRamSize
;
1174 surf
->u
.gfx9
.display_dcc_alignment
= dout
.dccRamBaseAlign
;
1175 surf
->u
.gfx9
.display_dcc_pitch_max
= dout
.pitch
- 1;
1176 assert(surf
->u
.gfx9
.display_dcc_size
<= surf
->dcc_size
);
1178 /* Compute address mapping from non-displayable to displayable DCC. */
1179 ADDR2_COMPUTE_DCC_ADDRFROMCOORD_INPUT addrin
= {};
1180 addrin
.size
= sizeof(addrin
);
1181 addrin
.colorFlags
.color
= 1;
1182 addrin
.swizzleMode
= din
.swizzleMode
;
1183 addrin
.resourceType
= din
.resourceType
;
1184 addrin
.bpp
= din
.bpp
;
1185 addrin
.unalignedWidth
= din
.unalignedWidth
;
1186 addrin
.unalignedHeight
= din
.unalignedHeight
;
1187 addrin
.numSlices
= 1;
1188 addrin
.numMipLevels
= 1;
1189 addrin
.numFrags
= 1;
1191 ADDR2_COMPUTE_DCC_ADDRFROMCOORD_OUTPUT addrout
= {};
1192 addrout
.size
= sizeof(addrout
);
1194 surf
->u
.gfx9
.dcc_retile_num_elements
=
1195 DIV_ROUND_UP(in
->width
, dout
.compressBlkWidth
) *
1196 DIV_ROUND_UP(in
->height
, dout
.compressBlkHeight
) * 2;
1197 /* Align the size to 4 (for the compute shader). */
1198 surf
->u
.gfx9
.dcc_retile_num_elements
=
1199 align(surf
->u
.gfx9
.dcc_retile_num_elements
, 4);
1201 surf
->u
.gfx9
.dcc_retile_map
=
1202 malloc(surf
->u
.gfx9
.dcc_retile_num_elements
* 4);
1203 if (!surf
->u
.gfx9
.dcc_retile_map
)
1204 return ADDR_OUTOFMEMORY
;
1207 surf
->u
.gfx9
.dcc_retile_use_uint16
= true;
1209 for (unsigned y
= 0; y
< in
->height
; y
+= dout
.compressBlkHeight
) {
1212 for (unsigned x
= 0; x
< in
->width
; x
+= dout
.compressBlkWidth
) {
1215 /* Compute src DCC address */
1216 addrin
.dccKeyFlags
.pipeAligned
= surf
->u
.gfx9
.dcc
.pipe_aligned
;
1217 addrin
.dccKeyFlags
.rbAligned
= surf
->u
.gfx9
.dcc
.rb_aligned
;
1220 ret
= Addr2ComputeDccAddrFromCoord(addrlib
, &addrin
, &addrout
);
1224 surf
->u
.gfx9
.dcc_retile_map
[index
* 2] = addrout
.addr
;
1225 if (addrout
.addr
> USHRT_MAX
)
1226 surf
->u
.gfx9
.dcc_retile_use_uint16
= false;
1228 /* Compute dst DCC address */
1229 addrin
.dccKeyFlags
.pipeAligned
= 0;
1230 addrin
.dccKeyFlags
.rbAligned
= 0;
1233 ret
= Addr2ComputeDccAddrFromCoord(addrlib
, &addrin
, &addrout
);
1237 surf
->u
.gfx9
.dcc_retile_map
[index
* 2 + 1] = addrout
.addr
;
1238 if (addrout
.addr
> USHRT_MAX
)
1239 surf
->u
.gfx9
.dcc_retile_use_uint16
= false;
1241 assert(index
* 2 + 1 < surf
->u
.gfx9
.dcc_retile_num_elements
);
1245 /* Fill the remaining pairs with the last one (for the compute shader). */
1246 for (unsigned i
= index
* 2; i
< surf
->u
.gfx9
.dcc_retile_num_elements
; i
++)
1247 surf
->u
.gfx9
.dcc_retile_map
[i
] = surf
->u
.gfx9
.dcc_retile_map
[i
- 2];
1252 if (in
->numSamples
> 1) {
1253 ADDR2_COMPUTE_FMASK_INFO_INPUT fin
= {0};
1254 ADDR2_COMPUTE_FMASK_INFO_OUTPUT fout
= {0};
1256 fin
.size
= sizeof(ADDR2_COMPUTE_FMASK_INFO_INPUT
);
1257 fout
.size
= sizeof(ADDR2_COMPUTE_FMASK_INFO_OUTPUT
);
1259 ret
= gfx9_get_preferred_swizzle_mode(addrlib
, in
,
1260 true, &fin
.swizzleMode
);
1264 fin
.unalignedWidth
= in
->width
;
1265 fin
.unalignedHeight
= in
->height
;
1266 fin
.numSlices
= in
->numSlices
;
1267 fin
.numSamples
= in
->numSamples
;
1268 fin
.numFrags
= in
->numFrags
;
1270 ret
= Addr2ComputeFmaskInfo(addrlib
, &fin
, &fout
);
1274 surf
->u
.gfx9
.fmask
.swizzle_mode
= fin
.swizzleMode
;
1275 surf
->u
.gfx9
.fmask
.epitch
= fout
.pitch
- 1;
1276 surf
->fmask_size
= fout
.fmaskBytes
;
1277 surf
->fmask_alignment
= fout
.baseAlign
;
1279 /* Compute tile swizzle for the FMASK surface. */
1280 if (config
->info
.fmask_surf_index
&&
1281 fin
.swizzleMode
>= ADDR_SW_64KB_Z_T
&&
1282 !(surf
->flags
& RADEON_SURF_SHAREABLE
)) {
1283 ADDR2_COMPUTE_PIPEBANKXOR_INPUT xin
= {0};
1284 ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT xout
= {0};
1286 xin
.size
= sizeof(ADDR2_COMPUTE_PIPEBANKXOR_INPUT
);
1287 xout
.size
= sizeof(ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT
);
1289 /* This counter starts from 1 instead of 0. */
1290 xin
.surfIndex
= p_atomic_inc_return(config
->info
.fmask_surf_index
);
1291 xin
.flags
= in
->flags
;
1292 xin
.swizzleMode
= fin
.swizzleMode
;
1293 xin
.resourceType
= in
->resourceType
;
1294 xin
.format
= in
->format
;
1295 xin
.numSamples
= in
->numSamples
;
1296 xin
.numFrags
= in
->numFrags
;
1298 ret
= Addr2ComputePipeBankXor(addrlib
, &xin
, &xout
);
1302 assert(xout
.pipeBankXor
<=
1303 u_bit_consecutive(0, sizeof(surf
->fmask_tile_swizzle
) * 8));
1304 surf
->fmask_tile_swizzle
= xout
.pipeBankXor
;
1309 if (in
->swizzleMode
!= ADDR_SW_LINEAR
) {
1310 ADDR2_COMPUTE_CMASK_INFO_INPUT cin
= {0};
1311 ADDR2_COMPUTE_CMASK_INFO_OUTPUT cout
= {0};
1313 cin
.size
= sizeof(ADDR2_COMPUTE_CMASK_INFO_INPUT
);
1314 cout
.size
= sizeof(ADDR2_COMPUTE_CMASK_INFO_OUTPUT
);
1316 if (in
->numSamples
> 1) {
1317 /* FMASK is always aligned. */
1318 cin
.cMaskFlags
.pipeAligned
= 1;
1319 cin
.cMaskFlags
.rbAligned
= 1;
1321 cin
.cMaskFlags
.pipeAligned
= !in
->flags
.metaPipeUnaligned
;
1322 cin
.cMaskFlags
.rbAligned
= !in
->flags
.metaRbUnaligned
;
1324 cin
.colorFlags
= in
->flags
;
1325 cin
.resourceType
= in
->resourceType
;
1326 cin
.unalignedWidth
= in
->width
;
1327 cin
.unalignedHeight
= in
->height
;
1328 cin
.numSlices
= in
->numSlices
;
1330 if (in
->numSamples
> 1)
1331 cin
.swizzleMode
= surf
->u
.gfx9
.fmask
.swizzle_mode
;
1333 cin
.swizzleMode
= in
->swizzleMode
;
1335 ret
= Addr2ComputeCmaskInfo(addrlib
, &cin
, &cout
);
1339 surf
->u
.gfx9
.cmask
.rb_aligned
= cin
.cMaskFlags
.rbAligned
;
1340 surf
->u
.gfx9
.cmask
.pipe_aligned
= cin
.cMaskFlags
.pipeAligned
;
1341 surf
->cmask_size
= cout
.cmaskBytes
;
1342 surf
->cmask_alignment
= cout
.baseAlign
;
1349 static int gfx9_compute_surface(ADDR_HANDLE addrlib
,
1350 const struct radeon_info
*info
,
1351 const struct ac_surf_config
*config
,
1352 enum radeon_surf_mode mode
,
1353 struct radeon_surf
*surf
)
1356 ADDR2_COMPUTE_SURFACE_INFO_INPUT AddrSurfInfoIn
= {0};
1359 AddrSurfInfoIn
.size
= sizeof(ADDR2_COMPUTE_SURFACE_INFO_INPUT
);
1361 compressed
= surf
->blk_w
== 4 && surf
->blk_h
== 4;
1363 /* The format must be set correctly for the allocation of compressed
1364 * textures to work. In other cases, setting the bpp is sufficient. */
1366 switch (surf
->bpe
) {
1368 AddrSurfInfoIn
.format
= ADDR_FMT_BC1
;
1371 AddrSurfInfoIn
.format
= ADDR_FMT_BC3
;
1377 switch (surf
->bpe
) {
1379 assert(!(surf
->flags
& RADEON_SURF_ZBUFFER
));
1380 AddrSurfInfoIn
.format
= ADDR_FMT_8
;
1383 assert(surf
->flags
& RADEON_SURF_ZBUFFER
||
1384 !(surf
->flags
& RADEON_SURF_SBUFFER
));
1385 AddrSurfInfoIn
.format
= ADDR_FMT_16
;
1388 assert(surf
->flags
& RADEON_SURF_ZBUFFER
||
1389 !(surf
->flags
& RADEON_SURF_SBUFFER
));
1390 AddrSurfInfoIn
.format
= ADDR_FMT_32
;
1393 assert(!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
));
1394 AddrSurfInfoIn
.format
= ADDR_FMT_32_32
;
1397 assert(!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
));
1398 AddrSurfInfoIn
.format
= ADDR_FMT_32_32_32
;
1401 assert(!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
));
1402 AddrSurfInfoIn
.format
= ADDR_FMT_32_32_32_32
;
1407 AddrSurfInfoIn
.bpp
= surf
->bpe
* 8;
1410 bool is_color_surface
= !(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
);
1411 AddrSurfInfoIn
.flags
.color
= is_color_surface
&&
1412 !(surf
->flags
& RADEON_SURF_NO_RENDER_TARGET
);
1413 AddrSurfInfoIn
.flags
.depth
= (surf
->flags
& RADEON_SURF_ZBUFFER
) != 0;
1414 AddrSurfInfoIn
.flags
.display
= get_display_flag(config
, surf
);
1415 /* flags.texture currently refers to TC-compatible HTILE */
1416 AddrSurfInfoIn
.flags
.texture
= is_color_surface
||
1417 surf
->flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
;
1418 AddrSurfInfoIn
.flags
.opt4space
= 1;
1420 AddrSurfInfoIn
.numMipLevels
= config
->info
.levels
;
1421 AddrSurfInfoIn
.numSamples
= MAX2(1, config
->info
.samples
);
1422 AddrSurfInfoIn
.numFrags
= AddrSurfInfoIn
.numSamples
;
1424 if (!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
))
1425 AddrSurfInfoIn
.numFrags
= MAX2(1, config
->info
.storage_samples
);
1427 /* GFX9 doesn't support 1D depth textures, so allocate all 1D textures
1428 * as 2D to avoid having shader variants for 1D vs 2D, so all shaders
1429 * must sample 1D textures as 2D. */
1431 AddrSurfInfoIn
.resourceType
= ADDR_RSRC_TEX_3D
;
1433 AddrSurfInfoIn
.resourceType
= ADDR_RSRC_TEX_2D
;
1435 AddrSurfInfoIn
.width
= config
->info
.width
;
1436 AddrSurfInfoIn
.height
= config
->info
.height
;
1439 AddrSurfInfoIn
.numSlices
= config
->info
.depth
;
1440 else if (config
->is_cube
)
1441 AddrSurfInfoIn
.numSlices
= 6;
1443 AddrSurfInfoIn
.numSlices
= config
->info
.array_size
;
1445 /* This is propagated to HTILE/DCC/CMASK. */
1446 AddrSurfInfoIn
.flags
.metaPipeUnaligned
= 0;
1447 AddrSurfInfoIn
.flags
.metaRbUnaligned
= 0;
1449 /* The display hardware can only read DCC with RB_ALIGNED=0 and
1450 * PIPE_ALIGNED=0. PIPE_ALIGNED really means L2CACHE_ALIGNED.
1452 * The CB block requires RB_ALIGNED=1 except 1 RB chips.
1453 * PIPE_ALIGNED is optional, but PIPE_ALIGNED=0 requires L2 flushes
1454 * after rendering, so PIPE_ALIGNED=1 is recommended.
1456 if (info
->use_display_dcc_unaligned
&& is_color_surface
&&
1457 AddrSurfInfoIn
.flags
.display
) {
1458 AddrSurfInfoIn
.flags
.metaPipeUnaligned
= 1;
1459 AddrSurfInfoIn
.flags
.metaRbUnaligned
= 1;
1463 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
1464 assert(config
->info
.samples
<= 1);
1465 assert(!(surf
->flags
& RADEON_SURF_Z_OR_SBUFFER
));
1466 AddrSurfInfoIn
.swizzleMode
= ADDR_SW_LINEAR
;
1469 case RADEON_SURF_MODE_1D
:
1470 case RADEON_SURF_MODE_2D
:
1471 if (surf
->flags
& RADEON_SURF_IMPORTED
) {
1472 AddrSurfInfoIn
.swizzleMode
= surf
->u
.gfx9
.surf
.swizzle_mode
;
1476 r
= gfx9_get_preferred_swizzle_mode(addrlib
, &AddrSurfInfoIn
,
1477 false, &AddrSurfInfoIn
.swizzleMode
);
1486 surf
->u
.gfx9
.resource_type
= AddrSurfInfoIn
.resourceType
;
1487 surf
->has_stencil
= !!(surf
->flags
& RADEON_SURF_SBUFFER
);
1489 surf
->num_dcc_levels
= 0;
1490 surf
->surf_size
= 0;
1491 surf
->fmask_size
= 0;
1493 surf
->htile_size
= 0;
1494 surf
->htile_slice_size
= 0;
1495 surf
->u
.gfx9
.surf_offset
= 0;
1496 surf
->u
.gfx9
.stencil_offset
= 0;
1497 surf
->cmask_size
= 0;
1498 surf
->u
.gfx9
.dcc_retile_use_uint16
= false;
1499 surf
->u
.gfx9
.dcc_retile_num_elements
= 0;
1500 surf
->u
.gfx9
.dcc_retile_map
= NULL
;
1502 /* Calculate texture layout information. */
1503 r
= gfx9_compute_miptree(addrlib
, info
, config
, surf
, compressed
,
1508 /* Calculate texture layout information for stencil. */
1509 if (surf
->flags
& RADEON_SURF_SBUFFER
) {
1510 AddrSurfInfoIn
.flags
.stencil
= 1;
1511 AddrSurfInfoIn
.bpp
= 8;
1512 AddrSurfInfoIn
.format
= ADDR_FMT_8
;
1514 if (!AddrSurfInfoIn
.flags
.depth
) {
1515 r
= gfx9_get_preferred_swizzle_mode(addrlib
, &AddrSurfInfoIn
,
1516 false, &AddrSurfInfoIn
.swizzleMode
);
1520 AddrSurfInfoIn
.flags
.depth
= 0;
1522 r
= gfx9_compute_miptree(addrlib
, info
, config
, surf
, compressed
,
1528 surf
->is_linear
= surf
->u
.gfx9
.surf
.swizzle_mode
== ADDR_SW_LINEAR
;
1530 /* Query whether the surface is displayable. */
1531 bool displayable
= false;
1532 if (!config
->is_3d
&& !config
->is_cube
) {
1533 r
= Addr2IsValidDisplaySwizzleMode(addrlib
, surf
->u
.gfx9
.surf
.swizzle_mode
,
1534 surf
->bpe
* 8, &displayable
);
1538 /* Display needs unaligned DCC. */
1539 if (info
->use_display_dcc_unaligned
&&
1540 surf
->num_dcc_levels
&&
1541 (surf
->u
.gfx9
.dcc
.pipe_aligned
||
1542 surf
->u
.gfx9
.dcc
.rb_aligned
))
1543 displayable
= false;
1545 surf
->is_displayable
= displayable
;
1547 switch (surf
->u
.gfx9
.surf
.swizzle_mode
) {
1549 case ADDR_SW_256B_S
:
1551 case ADDR_SW_64KB_S
:
1553 case ADDR_SW_64KB_S_T
:
1554 case ADDR_SW_4KB_S_X
:
1555 case ADDR_SW_64KB_S_X
:
1556 case ADDR_SW_VAR_S_X
:
1557 surf
->micro_tile_mode
= RADEON_MICRO_MODE_THIN
;
1561 case ADDR_SW_LINEAR
:
1562 case ADDR_SW_256B_D
:
1564 case ADDR_SW_64KB_D
:
1566 case ADDR_SW_64KB_D_T
:
1567 case ADDR_SW_4KB_D_X
:
1568 case ADDR_SW_64KB_D_X
:
1569 case ADDR_SW_VAR_D_X
:
1570 surf
->micro_tile_mode
= RADEON_MICRO_MODE_DISPLAY
;
1574 case ADDR_SW_256B_R
:
1576 case ADDR_SW_64KB_R
:
1578 case ADDR_SW_64KB_R_T
:
1579 case ADDR_SW_4KB_R_X
:
1580 case ADDR_SW_64KB_R_X
:
1581 case ADDR_SW_VAR_R_X
:
1582 /* The rotated micro tile mode doesn't work if both CMASK and RB+ are
1583 * used at the same time. This case is not currently expected to occur
1584 * because we don't use rotated. Enforce this restriction on all chips
1585 * to facilitate testing.
1587 assert(!"rotate micro tile mode is unsupported");
1593 case ADDR_SW_64KB_Z
:
1595 case ADDR_SW_64KB_Z_T
:
1596 case ADDR_SW_4KB_Z_X
:
1597 case ADDR_SW_64KB_Z_X
:
1598 case ADDR_SW_VAR_Z_X
:
1599 surf
->micro_tile_mode
= RADEON_MICRO_MODE_DEPTH
;
1609 free(surf
->u
.gfx9
.dcc_retile_map
);
1610 surf
->u
.gfx9
.dcc_retile_map
= NULL
;
1614 int ac_compute_surface(ADDR_HANDLE addrlib
, const struct radeon_info
*info
,
1615 const struct ac_surf_config
*config
,
1616 enum radeon_surf_mode mode
,
1617 struct radeon_surf
*surf
)
1621 r
= surf_config_sanity(config
, surf
->flags
);
1625 if (info
->chip_class
>= GFX9
)
1626 return gfx9_compute_surface(addrlib
, info
, config
, mode
, surf
);
1628 return gfx6_compute_surface(addrlib
, info
, config
, mode
, surf
);