Path: blob/21.2-virgl/src/amd/common/ac_surface.h
7132 views
/*1* Copyright © 2017 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sub license, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,12* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES13* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND14* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS15* AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,17* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE18* USE OR OTHER DEALINGS IN THE SOFTWARE.19*20* The above copyright notice and this permission notice (including the21* next paragraph) shall be included in all copies or substantial portions22* of the Software.23*/2425#ifndef AC_SURFACE_H26#define AC_SURFACE_H2728#include "amd_family.h"29#include "util/format/u_format.h"3031/* NIR is optional. Some components don't want to include NIR with ac_surface.h. */32#ifdef AC_SURFACE_INCLUDE_NIR33#include "compiler/nir/nir_builder.h"34#endif3536#include <stdbool.h>37#include <stdint.h>38#include <stdio.h>3940#ifdef __cplusplus41extern "C" {42#endif4344/* Forward declarations. */45struct ac_addrlib;4647struct amdgpu_gpu_info;48struct radeon_info;4950#define RADEON_SURF_MAX_LEVELS 155152enum radeon_surf_mode53{54RADEON_SURF_MODE_LINEAR_ALIGNED = 1,55RADEON_SURF_MODE_1D = 2,56RADEON_SURF_MODE_2D = 3,57};5859/* This describes D/S/Z/R swizzle modes.60* Defined in the GB_TILE_MODEn.MICRO_TILE_MODE_NEW order.61*/62enum radeon_micro_mode63{64RADEON_MICRO_MODE_DISPLAY = 0,65RADEON_MICRO_MODE_STANDARD = 1,66RADEON_MICRO_MODE_DEPTH = 2,67RADEON_MICRO_MODE_RENDER = 3, /* gfx9 and older: rotated */68};6970/* the first 16 bits are reserved for libdrm_radeon, don't use them */71#define RADEON_SURF_SCANOUT (1 << 16)72#define RADEON_SURF_ZBUFFER (1 << 17)73#define RADEON_SURF_SBUFFER (1 << 18)74#define RADEON_SURF_Z_OR_SBUFFER (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)75/* bits 19 and 20 are reserved for libdrm_radeon, don't use them */76#define RADEON_SURF_FMASK (1 << 21)77#define RADEON_SURF_DISABLE_DCC (1ull << 22)78#define RADEON_SURF_TC_COMPATIBLE_HTILE (1ull << 23)79#define RADEON_SURF_IMPORTED (1ull << 24)80#define RADEON_SURF_CONTIGUOUS_DCC_LAYERS (1ull << 25)81#define RADEON_SURF_SHAREABLE (1ull << 26)82#define RADEON_SURF_NO_RENDER_TARGET (1ull << 27)83/* Force a swizzle mode (gfx9+) or tile mode (gfx6-8).84* If this is not set, optimize for space. */85#define RADEON_SURF_FORCE_SWIZZLE_MODE (1ull << 28)86#define RADEON_SURF_NO_FMASK (1ull << 29)87#define RADEON_SURF_NO_HTILE (1ull << 30)88#define RADEON_SURF_FORCE_MICRO_TILE_MODE (1ull << 31)89#define RADEON_SURF_PRT (1ull << 32)9091struct legacy_surf_level {92uint32_t offset_256B; /* divided by 256, the hw can only do 40-bit addresses */93uint32_t slice_size_dw; /* in dwords; max = 4GB / 4. */94unsigned nblk_x : 15;95unsigned nblk_y : 15;96enum radeon_surf_mode mode : 2;97};9899struct legacy_surf_dcc_level {100uint32_t dcc_offset; /* relative offset within DCC mip tree */101uint32_t dcc_fast_clear_size;102uint32_t dcc_slice_fast_clear_size;103};104105struct legacy_surf_fmask {106unsigned slice_tile_max; /* max 4M */107uint8_t tiling_index; /* max 31 */108uint8_t bankh; /* max 8 */109uint16_t pitch_in_pixels;110};111112struct legacy_surf_layout {113unsigned bankw : 4; /* max 8 */114unsigned bankh : 4; /* max 8 */115unsigned mtilea : 4; /* max 8 */116unsigned tile_split : 13; /* max 4K */117unsigned stencil_tile_split : 13; /* max 4K */118unsigned pipe_config : 5; /* max 17 */119unsigned num_banks : 5; /* max 16 */120unsigned macro_tile_index : 4; /* max 15 */121122/* Whether the depth miptree or stencil miptree as used by the DB are123* adjusted from their TC compatible form to ensure depth/stencil124* compatibility. If either is true, the corresponding plane cannot be125* sampled from.126*/127unsigned depth_adjusted : 1;128unsigned stencil_adjusted : 1;129130struct legacy_surf_level level[RADEON_SURF_MAX_LEVELS];131uint8_t tiling_index[RADEON_SURF_MAX_LEVELS];132133union {134/* Color layout */135struct {136struct legacy_surf_dcc_level dcc_level[RADEON_SURF_MAX_LEVELS];137struct legacy_surf_fmask fmask;138unsigned cmask_slice_tile_max;139} color;140141/* Z/S layout */142struct {143struct legacy_surf_level stencil_level[RADEON_SURF_MAX_LEVELS];144uint8_t stencil_tiling_index[RADEON_SURF_MAX_LEVELS];145} zs;146};147};148149/* Same as addrlib - AddrResourceType. */150enum gfx9_resource_type151{152RADEON_RESOURCE_1D = 0,153RADEON_RESOURCE_2D,154RADEON_RESOURCE_3D,155};156157struct gfx9_surf_meta_flags {158uint8_t rb_aligned : 1; /* optimal for RBs */159uint8_t pipe_aligned : 1; /* optimal for TC */160uint8_t independent_64B_blocks : 1;161uint8_t independent_128B_blocks : 1;162uint8_t max_compressed_block_size : 2;163uint8_t display_equation_valid : 1;164};165166struct gfx9_surf_level {167unsigned offset;168unsigned size; /* the size of one level in one layer (the image is an array of layers169* where each layer has an array of levels) */170};171172/**173* Meta address equation.174*175* DCC/HTILE address equation for doing DCC/HTILE address computations in shaders.176*177* ac_surface_meta_address_test.c contains the reference implementation.178* ac_nir_{dcc,htile}_addr_from_coord is the NIR implementation.179*180* For DCC:181* The gfx9 equation doesn't support mipmapping.182* The gfx10 equation doesn't support mipmapping and MSAA.183* (those are also limitations of Addr2ComputeDccAddrFromCoord)184*185* For HTILE:186* The gfx9 equation isn't implemented.187* The gfx10 equation doesn't support mipmapping.188*/189struct gfx9_meta_equation {190uint16_t meta_block_width;191uint16_t meta_block_height;192uint16_t meta_block_depth;193194union {195/* The gfx9 DCC equation is chip-specific, and it varies with:196* - resource type197* - swizzle_mode198* - bpp199* - number of samples200* - number of fragments201* - pipe_aligned202* - rb_aligned203*/204struct {205uint8_t num_bits;206uint8_t num_pipe_bits;207208struct {209struct {210uint8_t dim:3; /* 0..4 */211uint8_t ord:5; /* 0..31 */212} coord[5]; /* 0..num_coords-1 */213} bit[20]; /* 0..num_bits-1 */214} gfx9;215216/* The gfx10 DCC equation is chip-specific, it requires 64KB_R_X, and it varies with:217* - bpp218* - number of samples219* - number of fragments220* - pipe_aligned221*222* The gfx10 HTILE equation is chip-specific, it requires 64KB_Z_X, and it varies with:223* - number of samples224*/225uint16_t gfx10_bits[60];226} u;227};228229struct gfx9_surf_layout {230uint16_t epitch; /* gfx9 only, not on gfx10 */231uint8_t swizzle_mode; /* color or depth */232233enum gfx9_resource_type resource_type:8; /* 1D, 2D or 3D */234uint16_t surf_pitch; /* in blocks */235uint16_t surf_height;236237uint64_t surf_offset; /* 0 unless imported with an offset */238/* The size of the 2D plane containing all mipmap levels. */239uint64_t surf_slice_size;240/* Mipmap level offset within the slice in bytes. Only valid for LINEAR. */241uint32_t offset[RADEON_SURF_MAX_LEVELS];242/* Mipmap level pitch in elements. Only valid for LINEAR. */243uint16_t pitch[RADEON_SURF_MAX_LEVELS];244245uint16_t base_mip_width;246uint16_t base_mip_height;247248/* Pitch of level in blocks, only valid for prt images. */249uint16_t prt_level_pitch[RADEON_SURF_MAX_LEVELS];250/* Offset within slice in bytes, only valid for prt images. */251uint32_t prt_level_offset[RADEON_SURF_MAX_LEVELS];252253/* DCC or HTILE level info */254struct gfx9_surf_level meta_levels[RADEON_SURF_MAX_LEVELS];255256union {257/* Color */258struct {259struct gfx9_surf_meta_flags dcc; /* metadata of color */260uint8_t fmask_swizzle_mode;261uint16_t fmask_epitch; /* gfx9 only, not on gfx10 */262263uint16_t dcc_pitch_max;264uint16_t dcc_height;265266uint8_t dcc_block_width;267uint8_t dcc_block_height;268uint8_t dcc_block_depth;269270/* Displayable DCC. This is always rb_aligned=0 and pipe_aligned=0.271* The 3D engine doesn't support that layout except for chips with 1 RB.272* All other chips must set rb_aligned=1.273* A compute shader needs to convert from aligned DCC to unaligned.274*/275uint8_t display_dcc_alignment_log2;276uint32_t display_dcc_size;277uint16_t display_dcc_pitch_max; /* (mip chain pitch - 1) */278uint16_t display_dcc_height;279bool dcc_retile_use_uint16; /* if all values fit into uint16_t */280uint32_t dcc_retile_num_elements;281void *dcc_retile_map;282283/* CMASK level info (only level 0) */284struct gfx9_surf_level cmask_level0;285286/* For DCC retiling. */287struct gfx9_meta_equation dcc_equation; /* 2D only */288struct gfx9_meta_equation display_dcc_equation;289} color;290291/* Z/S */292struct {293uint64_t stencil_offset; /* separate stencil */294uint16_t stencil_epitch; /* gfx9 only, not on gfx10 */295uint8_t stencil_swizzle_mode;296297/* For HTILE VRS. */298struct gfx9_meta_equation htile_equation;299} zs;300};301};302303struct radeon_surf {304/* Format properties. */305uint8_t blk_w : 4;306uint8_t blk_h : 4;307uint8_t bpe : 5;308/* Display, standard(thin), depth, render(rotated). AKA D,S,Z,R swizzle modes. */309uint8_t micro_tile_mode : 3;310/* Number of mipmap levels where DCC or HTILE is enabled starting from level 0.311* Non-zero levels may be disabled due to alignment constraints, but not312* the first level.313*/314uint8_t num_meta_levels : 4;315uint8_t is_linear : 1;316uint8_t has_stencil : 1;317/* This might be true even if micro_tile_mode isn't displayable or rotated. */318uint8_t is_displayable : 1;319uint8_t first_mip_tail_level : 4;320321/* These are return values. Some of them can be set by the caller, but322* they will be treated as hints (e.g. bankw, bankh) and might be323* changed by the calculator.324*/325326/* Not supported yet for depth + stencil. */327uint16_t prt_tile_width;328uint16_t prt_tile_height;329330/* Tile swizzle can be OR'd with low bits of the BASE_256B address.331* The value is the same for all mipmap levels. Supported tile modes:332* - GFX6: Only macro tiling.333* - GFX9: Only *_X and *_T swizzle modes. Level 0 must not be in the mip334* tail.335*336* Only these surfaces are allowed to set it:337* - color (if it doesn't have to be displayable)338* - DCC (same tile swizzle as color)339* - FMASK340* - CMASK if it's TC-compatible or if the gen is GFX9341* - depth/stencil if HTILE is not TC-compatible and if the gen is not GFX9342*/343uint8_t tile_swizzle;344uint8_t fmask_tile_swizzle;345346/* Use (1 << log2) to compute the alignment. */347uint8_t surf_alignment_log2;348uint8_t fmask_alignment_log2;349uint8_t meta_alignment_log2; /* DCC or HTILE */350uint8_t cmask_alignment_log2;351uint8_t alignment_log2;352353/* DRM format modifier. Set to DRM_FORMAT_MOD_INVALID to have addrlib354* select tiling parameters instead.355*/356uint64_t modifier;357uint64_t flags;358359uint64_t surf_size;360uint64_t fmask_size;361uint32_t fmask_slice_size; /* max 2^31 (16K * 16K * 8) */362363/* DCC and HTILE (they are very small) */364uint32_t meta_size;365uint32_t meta_slice_size;366uint32_t meta_pitch;367368uint32_t cmask_size;369uint32_t cmask_slice_size;370371/* All buffers combined. */372uint64_t meta_offset; /* DCC or HTILE */373uint64_t fmask_offset;374uint64_t cmask_offset;375uint64_t display_dcc_offset;376uint64_t total_size;377378union {379/* Return values for GFX8 and older.380*381* Some of them can be set by the caller if certain parameters are382* desirable. The allocator will try to obey them.383*/384struct legacy_surf_layout legacy;385386/* GFX9+ return values. */387struct gfx9_surf_layout gfx9;388} u;389};390391struct ac_surf_info {392uint32_t width;393uint32_t height;394uint32_t depth;395uint8_t samples; /* For Z/S: samples; For color: FMASK coverage samples */396uint8_t storage_samples; /* For color: allocated samples */397uint8_t levels;398uint8_t num_channels; /* heuristic for displayability */399uint16_t array_size;400uint32_t *surf_index; /* Set a monotonic counter for tile swizzling. */401uint32_t *fmask_surf_index;402};403404struct ac_surf_config {405struct ac_surf_info info;406unsigned is_1d : 1;407unsigned is_3d : 1;408unsigned is_cube : 1;409};410411struct ac_addrlib *ac_addrlib_create(const struct radeon_info *info, uint64_t *max_alignment);412void ac_addrlib_destroy(struct ac_addrlib *addrlib);413void *ac_addrlib_get_handle(struct ac_addrlib *addrlib);414415int ac_compute_surface(struct ac_addrlib *addrlib, const struct radeon_info *info,416const struct ac_surf_config *config, enum radeon_surf_mode mode,417struct radeon_surf *surf);418void ac_surface_zero_dcc_fields(struct radeon_surf *surf);419420void ac_surface_set_bo_metadata(const struct radeon_info *info, struct radeon_surf *surf,421uint64_t tiling_flags, enum radeon_surf_mode *mode);422void ac_surface_get_bo_metadata(const struct radeon_info *info, struct radeon_surf *surf,423uint64_t *tiling_flags);424425bool ac_surface_set_umd_metadata(const struct radeon_info *info, struct radeon_surf *surf,426unsigned num_storage_samples, unsigned num_mipmap_levels,427unsigned size_metadata, const uint32_t metadata[64]);428void ac_surface_get_umd_metadata(const struct radeon_info *info, struct radeon_surf *surf,429unsigned num_mipmap_levels, uint32_t desc[8],430unsigned *size_metadata, uint32_t metadata[64]);431432bool ac_surface_override_offset_stride(const struct radeon_info *info, struct radeon_surf *surf,433unsigned num_mipmap_levels, uint64_t offset, unsigned pitch);434435struct ac_modifier_options {436bool dcc; /* Whether to allow DCC. */437bool dcc_retile; /* Whether to allow use of a DCC retile map. */438};439440bool ac_is_modifier_supported(const struct radeon_info *info,441const struct ac_modifier_options *options,442enum pipe_format format,443uint64_t modifier);444bool ac_get_supported_modifiers(const struct radeon_info *info,445const struct ac_modifier_options *options,446enum pipe_format format,447unsigned *mod_count,448uint64_t *mods);449bool ac_modifier_has_dcc(uint64_t modifier);450bool ac_modifier_has_dcc_retile(uint64_t modifier);451452unsigned ac_surface_get_nplanes(const struct radeon_surf *surf);453uint64_t ac_surface_get_plane_offset(enum chip_class chip_class,454const struct radeon_surf *surf,455unsigned plane, unsigned layer);456uint64_t ac_surface_get_plane_stride(enum chip_class chip_class,457const struct radeon_surf *surf,458unsigned plane);459/* Of the whole miplevel, not an individual layer */460uint64_t ac_surface_get_plane_size(const struct radeon_surf *surf,461unsigned plane);462463void ac_surface_print_info(FILE *out, const struct radeon_info *info,464const struct radeon_surf *surf);465466#ifdef AC_SURFACE_INCLUDE_NIR467nir_ssa_def *ac_nir_dcc_addr_from_coord(nir_builder *b, const struct radeon_info *info,468unsigned bpe, struct gfx9_meta_equation *equation,469nir_ssa_def *dcc_pitch, nir_ssa_def *dcc_height,470nir_ssa_def *dcc_slice_size,471nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *z,472nir_ssa_def *sample, nir_ssa_def *pipe_xor);473474nir_ssa_def *ac_nir_htile_addr_from_coord(nir_builder *b, const struct radeon_info *info,475struct gfx9_meta_equation *equation,476nir_ssa_def *htile_pitch,477nir_ssa_def *htile_slice_size,478nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *z,479nir_ssa_def *pipe_xor);480#endif481482#ifdef __cplusplus483}484#endif485486#endif /* AC_SURFACE_H */487488489