Path: blob/21.2-virgl/src/amd/common/ac_shader_util.c
7233 views
/*1* Copyright 2012 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "ac_shader_util.h"24#include "ac_gpu_info.h"2526#include "sid.h"2728#include <assert.h>29#include <stdlib.h>30#include <string.h>3132unsigned ac_get_spi_shader_z_format(bool writes_z, bool writes_stencil, bool writes_samplemask)33{34if (writes_z) {35/* Z needs 32 bits. */36if (writes_samplemask)37return V_028710_SPI_SHADER_32_ABGR;38else if (writes_stencil)39return V_028710_SPI_SHADER_32_GR;40else41return V_028710_SPI_SHADER_32_R;42} else if (writes_stencil || writes_samplemask) {43/* Both stencil and sample mask need only 16 bits. */44return V_028710_SPI_SHADER_UINT16_ABGR;45} else {46return V_028710_SPI_SHADER_ZERO;47}48}4950unsigned ac_get_cb_shader_mask(unsigned spi_shader_col_format)51{52unsigned i, cb_shader_mask = 0;5354for (i = 0; i < 8; i++) {55switch ((spi_shader_col_format >> (i * 4)) & 0xf) {56case V_028714_SPI_SHADER_ZERO:57break;58case V_028714_SPI_SHADER_32_R:59cb_shader_mask |= 0x1 << (i * 4);60break;61case V_028714_SPI_SHADER_32_GR:62cb_shader_mask |= 0x3 << (i * 4);63break;64case V_028714_SPI_SHADER_32_AR:65cb_shader_mask |= 0x9u << (i * 4);66break;67case V_028714_SPI_SHADER_FP16_ABGR:68case V_028714_SPI_SHADER_UNORM16_ABGR:69case V_028714_SPI_SHADER_SNORM16_ABGR:70case V_028714_SPI_SHADER_UINT16_ABGR:71case V_028714_SPI_SHADER_SINT16_ABGR:72case V_028714_SPI_SHADER_32_ABGR:73cb_shader_mask |= 0xfu << (i * 4);74break;75default:76assert(0);77}78}79return cb_shader_mask;80}8182/**83* Calculate the appropriate setting of VGT_GS_MODE when \p shader is a84* geometry shader.85*/86uint32_t ac_vgt_gs_mode(unsigned gs_max_vert_out, enum chip_class chip_class)87{88unsigned cut_mode;8990if (gs_max_vert_out <= 128) {91cut_mode = V_028A40_GS_CUT_128;92} else if (gs_max_vert_out <= 256) {93cut_mode = V_028A40_GS_CUT_256;94} else if (gs_max_vert_out <= 512) {95cut_mode = V_028A40_GS_CUT_512;96} else {97assert(gs_max_vert_out <= 1024);98cut_mode = V_028A40_GS_CUT_1024;99}100101return S_028A40_MODE(V_028A40_GS_SCENARIO_G) | S_028A40_CUT_MODE(cut_mode) |102S_028A40_ES_WRITE_OPTIMIZE(chip_class <= GFX8) | S_028A40_GS_WRITE_OPTIMIZE(1) |103S_028A40_ONCHIP(chip_class >= GFX9 ? 1 : 0);104}105106/// Translate a (dfmt, nfmt) pair into a chip-appropriate combined format107/// value for LLVM8+ tbuffer intrinsics.108unsigned ac_get_tbuffer_format(enum chip_class chip_class, unsigned dfmt, unsigned nfmt)109{110// Some games try to access vertex buffers without a valid format.111// This is a game bug, but we should still handle it gracefully.112if (dfmt == V_008F0C_GFX10_FORMAT_INVALID)113return V_008F0C_GFX10_FORMAT_INVALID;114115if (chip_class >= GFX10) {116unsigned format;117switch (dfmt) {118default:119unreachable("bad dfmt");120case V_008F0C_BUF_DATA_FORMAT_INVALID:121format = V_008F0C_GFX10_FORMAT_INVALID;122break;123case V_008F0C_BUF_DATA_FORMAT_8:124format = V_008F0C_GFX10_FORMAT_8_UINT;125break;126case V_008F0C_BUF_DATA_FORMAT_8_8:127format = V_008F0C_GFX10_FORMAT_8_8_UINT;128break;129case V_008F0C_BUF_DATA_FORMAT_8_8_8_8:130format = V_008F0C_GFX10_FORMAT_8_8_8_8_UINT;131break;132case V_008F0C_BUF_DATA_FORMAT_16:133format = V_008F0C_GFX10_FORMAT_16_UINT;134break;135case V_008F0C_BUF_DATA_FORMAT_16_16:136format = V_008F0C_GFX10_FORMAT_16_16_UINT;137break;138case V_008F0C_BUF_DATA_FORMAT_16_16_16_16:139format = V_008F0C_GFX10_FORMAT_16_16_16_16_UINT;140break;141case V_008F0C_BUF_DATA_FORMAT_32:142format = V_008F0C_GFX10_FORMAT_32_UINT;143break;144case V_008F0C_BUF_DATA_FORMAT_32_32:145format = V_008F0C_GFX10_FORMAT_32_32_UINT;146break;147case V_008F0C_BUF_DATA_FORMAT_32_32_32:148format = V_008F0C_GFX10_FORMAT_32_32_32_UINT;149break;150case V_008F0C_BUF_DATA_FORMAT_32_32_32_32:151format = V_008F0C_GFX10_FORMAT_32_32_32_32_UINT;152break;153case V_008F0C_BUF_DATA_FORMAT_2_10_10_10:154format = V_008F0C_GFX10_FORMAT_2_10_10_10_UINT;155break;156case V_008F0C_BUF_DATA_FORMAT_10_11_11:157format = V_008F0C_GFX10_FORMAT_10_11_11_UINT;158break;159}160161// Use the regularity properties of the combined format enum.162//163// Note: float is incompatible with 8-bit data formats,164// [us]{norm,scaled} are incomparible with 32-bit data formats.165// [us]scaled are not writable.166switch (nfmt) {167case V_008F0C_BUF_NUM_FORMAT_UNORM:168format -= 4;169break;170case V_008F0C_BUF_NUM_FORMAT_SNORM:171format -= 3;172break;173case V_008F0C_BUF_NUM_FORMAT_USCALED:174format -= 2;175break;176case V_008F0C_BUF_NUM_FORMAT_SSCALED:177format -= 1;178break;179default:180unreachable("bad nfmt");181case V_008F0C_BUF_NUM_FORMAT_UINT:182break;183case V_008F0C_BUF_NUM_FORMAT_SINT:184format += 1;185break;186case V_008F0C_BUF_NUM_FORMAT_FLOAT:187format += 2;188break;189}190191return format;192} else {193return dfmt | (nfmt << 4);194}195}196197static const struct ac_data_format_info data_format_table[] = {198[V_008F0C_BUF_DATA_FORMAT_INVALID] = {0, 4, 0, V_008F0C_BUF_DATA_FORMAT_INVALID},199[V_008F0C_BUF_DATA_FORMAT_8] = {1, 1, 1, V_008F0C_BUF_DATA_FORMAT_8},200[V_008F0C_BUF_DATA_FORMAT_16] = {2, 1, 2, V_008F0C_BUF_DATA_FORMAT_16},201[V_008F0C_BUF_DATA_FORMAT_8_8] = {2, 2, 1, V_008F0C_BUF_DATA_FORMAT_8},202[V_008F0C_BUF_DATA_FORMAT_32] = {4, 1, 4, V_008F0C_BUF_DATA_FORMAT_32},203[V_008F0C_BUF_DATA_FORMAT_16_16] = {4, 2, 2, V_008F0C_BUF_DATA_FORMAT_16},204[V_008F0C_BUF_DATA_FORMAT_10_11_11] = {4, 3, 0, V_008F0C_BUF_DATA_FORMAT_10_11_11},205[V_008F0C_BUF_DATA_FORMAT_11_11_10] = {4, 3, 0, V_008F0C_BUF_DATA_FORMAT_11_11_10},206[V_008F0C_BUF_DATA_FORMAT_10_10_10_2] = {4, 4, 0, V_008F0C_BUF_DATA_FORMAT_10_10_10_2},207[V_008F0C_BUF_DATA_FORMAT_2_10_10_10] = {4, 4, 0, V_008F0C_BUF_DATA_FORMAT_2_10_10_10},208[V_008F0C_BUF_DATA_FORMAT_8_8_8_8] = {4, 4, 1, V_008F0C_BUF_DATA_FORMAT_8},209[V_008F0C_BUF_DATA_FORMAT_32_32] = {8, 2, 4, V_008F0C_BUF_DATA_FORMAT_32},210[V_008F0C_BUF_DATA_FORMAT_16_16_16_16] = {8, 4, 2, V_008F0C_BUF_DATA_FORMAT_16},211[V_008F0C_BUF_DATA_FORMAT_32_32_32] = {12, 3, 4, V_008F0C_BUF_DATA_FORMAT_32},212[V_008F0C_BUF_DATA_FORMAT_32_32_32_32] = {16, 4, 4, V_008F0C_BUF_DATA_FORMAT_32},213};214215const struct ac_data_format_info *ac_get_data_format_info(unsigned dfmt)216{217assert(dfmt < ARRAY_SIZE(data_format_table));218return &data_format_table[dfmt];219}220221enum ac_image_dim ac_get_sampler_dim(enum chip_class chip_class, enum glsl_sampler_dim dim,222bool is_array)223{224switch (dim) {225case GLSL_SAMPLER_DIM_1D:226if (chip_class == GFX9)227return is_array ? ac_image_2darray : ac_image_2d;228return is_array ? ac_image_1darray : ac_image_1d;229case GLSL_SAMPLER_DIM_2D:230case GLSL_SAMPLER_DIM_RECT:231case GLSL_SAMPLER_DIM_EXTERNAL:232return is_array ? ac_image_2darray : ac_image_2d;233case GLSL_SAMPLER_DIM_3D:234return ac_image_3d;235case GLSL_SAMPLER_DIM_CUBE:236return ac_image_cube;237case GLSL_SAMPLER_DIM_MS:238return is_array ? ac_image_2darraymsaa : ac_image_2dmsaa;239case GLSL_SAMPLER_DIM_SUBPASS:240return ac_image_2darray;241case GLSL_SAMPLER_DIM_SUBPASS_MS:242return ac_image_2darraymsaa;243default:244unreachable("bad sampler dim");245}246}247248enum ac_image_dim ac_get_image_dim(enum chip_class chip_class, enum glsl_sampler_dim sdim,249bool is_array)250{251enum ac_image_dim dim = ac_get_sampler_dim(chip_class, sdim, is_array);252253/* Match the resource type set in the descriptor. */254if (dim == ac_image_cube || (chip_class <= GFX8 && dim == ac_image_3d))255dim = ac_image_2darray;256else if (sdim == GLSL_SAMPLER_DIM_2D && !is_array && chip_class == GFX9) {257/* When a single layer of a 3D texture is bound, the shader258* will refer to a 2D target, but the descriptor has a 3D type.259* Since the HW ignores BASE_ARRAY in this case, we need to260* send 3 coordinates. This doesn't hurt when the underlying261* texture is non-3D.262*/263dim = ac_image_3d;264}265266return dim;267}268269unsigned ac_get_fs_input_vgpr_cnt(const struct ac_shader_config *config,270signed char *face_vgpr_index_ptr,271signed char *ancillary_vgpr_index_ptr)272{273unsigned num_input_vgprs = 0;274signed char face_vgpr_index = -1;275signed char ancillary_vgpr_index = -1;276277if (G_0286CC_PERSP_SAMPLE_ENA(config->spi_ps_input_addr))278num_input_vgprs += 2;279if (G_0286CC_PERSP_CENTER_ENA(config->spi_ps_input_addr))280num_input_vgprs += 2;281if (G_0286CC_PERSP_CENTROID_ENA(config->spi_ps_input_addr))282num_input_vgprs += 2;283if (G_0286CC_PERSP_PULL_MODEL_ENA(config->spi_ps_input_addr))284num_input_vgprs += 3;285if (G_0286CC_LINEAR_SAMPLE_ENA(config->spi_ps_input_addr))286num_input_vgprs += 2;287if (G_0286CC_LINEAR_CENTER_ENA(config->spi_ps_input_addr))288num_input_vgprs += 2;289if (G_0286CC_LINEAR_CENTROID_ENA(config->spi_ps_input_addr))290num_input_vgprs += 2;291if (G_0286CC_LINE_STIPPLE_TEX_ENA(config->spi_ps_input_addr))292num_input_vgprs += 1;293if (G_0286CC_POS_X_FLOAT_ENA(config->spi_ps_input_addr))294num_input_vgprs += 1;295if (G_0286CC_POS_Y_FLOAT_ENA(config->spi_ps_input_addr))296num_input_vgprs += 1;297if (G_0286CC_POS_Z_FLOAT_ENA(config->spi_ps_input_addr))298num_input_vgprs += 1;299if (G_0286CC_POS_W_FLOAT_ENA(config->spi_ps_input_addr))300num_input_vgprs += 1;301if (G_0286CC_FRONT_FACE_ENA(config->spi_ps_input_addr)) {302face_vgpr_index = num_input_vgprs;303num_input_vgprs += 1;304}305if (G_0286CC_ANCILLARY_ENA(config->spi_ps_input_addr)) {306ancillary_vgpr_index = num_input_vgprs;307num_input_vgprs += 1;308}309if (G_0286CC_SAMPLE_COVERAGE_ENA(config->spi_ps_input_addr))310num_input_vgprs += 1;311if (G_0286CC_POS_FIXED_PT_ENA(config->spi_ps_input_addr))312num_input_vgprs += 1;313314if (face_vgpr_index_ptr)315*face_vgpr_index_ptr = face_vgpr_index;316if (ancillary_vgpr_index_ptr)317*ancillary_vgpr_index_ptr = ancillary_vgpr_index;318319return num_input_vgprs;320}321322void ac_choose_spi_color_formats(unsigned format, unsigned swap, unsigned ntype,323bool is_depth, bool use_rbplus,324struct ac_spi_color_formats *formats)325{326/* Alpha is needed for alpha-to-coverage.327* Blending may be with or without alpha.328*/329unsigned normal = 0; /* most optimal, may not support blending or export alpha */330unsigned alpha = 0; /* exports alpha, but may not support blending */331unsigned blend = 0; /* supports blending, but may not export alpha */332unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */333334/* Choose the SPI color formats. These are required values for RB+.335* Other chips have multiple choices, though they are not necessarily better.336*/337switch (format) {338case V_028C70_COLOR_5_6_5:339case V_028C70_COLOR_1_5_5_5:340case V_028C70_COLOR_5_5_5_1:341case V_028C70_COLOR_4_4_4_4:342case V_028C70_COLOR_10_11_11:343case V_028C70_COLOR_11_11_10:344case V_028C70_COLOR_5_9_9_9:345case V_028C70_COLOR_8:346case V_028C70_COLOR_8_8:347case V_028C70_COLOR_8_8_8_8:348case V_028C70_COLOR_10_10_10_2:349case V_028C70_COLOR_2_10_10_10:350if (ntype == V_028C70_NUMBER_UINT)351alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;352else if (ntype == V_028C70_NUMBER_SINT)353alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;354else355alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;356357if (!use_rbplus && format == V_028C70_COLOR_8 &&358ntype != V_028C70_NUMBER_SRGB && swap == V_028C70_SWAP_STD) /* R */ {359/* When RB+ is enabled, R8_UNORM should use FP16_ABGR for 2x360* exporting performance. Otherwise, use 32_R to remove useless361* instructions needed for 16-bit compressed exports.362*/363blend = normal = V_028714_SPI_SHADER_32_R;364}365break;366367case V_028C70_COLOR_16:368case V_028C70_COLOR_16_16:369case V_028C70_COLOR_16_16_16_16:370if (ntype == V_028C70_NUMBER_UNORM || ntype == V_028C70_NUMBER_SNORM) {371/* UNORM16 and SNORM16 don't support blending */372if (ntype == V_028C70_NUMBER_UNORM)373normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;374else375normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;376377/* Use 32 bits per channel for blending. */378if (format == V_028C70_COLOR_16) {379if (swap == V_028C70_SWAP_STD) { /* R */380blend = V_028714_SPI_SHADER_32_R;381blend_alpha = V_028714_SPI_SHADER_32_AR;382} else if (swap == V_028C70_SWAP_ALT_REV) /* A */383blend = blend_alpha = V_028714_SPI_SHADER_32_AR;384else385assert(0);386} else if (format == V_028C70_COLOR_16_16) {387if (swap == V_028C70_SWAP_STD) { /* RG */388blend = V_028714_SPI_SHADER_32_GR;389blend_alpha = V_028714_SPI_SHADER_32_ABGR;390} else if (swap == V_028C70_SWAP_ALT) /* RA */391blend = blend_alpha = V_028714_SPI_SHADER_32_AR;392else393assert(0);394} else /* 16_16_16_16 */395blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;396} else if (ntype == V_028C70_NUMBER_UINT)397alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;398else if (ntype == V_028C70_NUMBER_SINT)399alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;400else if (ntype == V_028C70_NUMBER_FLOAT)401alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;402else403assert(0);404break;405406case V_028C70_COLOR_32:407if (swap == V_028C70_SWAP_STD) { /* R */408blend = normal = V_028714_SPI_SHADER_32_R;409alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;410} else if (swap == V_028C70_SWAP_ALT_REV) /* A */411alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;412else413assert(0);414break;415416case V_028C70_COLOR_32_32:417if (swap == V_028C70_SWAP_STD) { /* RG */418blend = normal = V_028714_SPI_SHADER_32_GR;419alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;420} else if (swap == V_028C70_SWAP_ALT) /* RA */421alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;422else423assert(0);424break;425426case V_028C70_COLOR_32_32_32_32:427case V_028C70_COLOR_8_24:428case V_028C70_COLOR_24_8:429case V_028C70_COLOR_X24_8_32_FLOAT:430alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;431break;432433default:434assert(0);435return;436}437438/* The DB->CB copy needs 32_ABGR. */439if (is_depth)440alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;441442formats->normal = normal;443formats->alpha = alpha;444formats->blend = blend;445formats->blend_alpha = blend_alpha;446}447448void ac_compute_late_alloc(const struct radeon_info *info, bool ngg, bool ngg_culling,449bool uses_scratch, unsigned *late_alloc_wave64, unsigned *cu_mask)450{451*late_alloc_wave64 = 0; /* The limit is per SA. */452*cu_mask = 0xffff;453454/* CU masking can decrease performance and cause a hang with <= 2 CUs per SA. */455if (info->min_good_cu_per_sa <= 2)456return;457458/* If scratch is used with late alloc, the GPU could deadlock if PS uses scratch too. A more459* complicated computation is needed to enable late alloc with scratch (see PAL).460*/461if (uses_scratch)462return;463464/* Late alloc is not used for NGG on Navi14 due to a hw bug. */465if (ngg && info->family == CHIP_NAVI14)466return;467468if (info->chip_class >= GFX10) {469/* For Wave32, the hw will launch twice the number of late alloc waves, so 1 == 2x wave32.470* These limits are estimated because they are all safe but they vary in performance.471*/472if (ngg_culling)473*late_alloc_wave64 = info->min_good_cu_per_sa * 10;474else475*late_alloc_wave64 = info->min_good_cu_per_sa * 4;476477/* Limit LATE_ALLOC_GS to prevent a hang (hw bug) on gfx10. */478if (info->chip_class == GFX10 && ngg)479*late_alloc_wave64 = MIN2(*late_alloc_wave64, 64);480481/* Gfx10: CU2 & CU3 must be disabled to prevent a hw deadlock.482* Others: CU1 must be disabled to prevent a hw deadlock.483*484* The deadlock is caused by late alloc, which usually increases performance.485*/486*cu_mask &= info->chip_class == GFX10 ? ~BITFIELD_RANGE(2, 2) :487~BITFIELD_RANGE(1, 1);488} else {489if (info->min_good_cu_per_sa <= 4) {490/* Too few available compute units per SA. Disallowing VS to run on one CU could hurt us491* more than late VS allocation would help.492*493* 2 is the highest safe number that allows us to keep all CUs enabled.494*/495*late_alloc_wave64 = 2;496} else {497/* This is a good initial value, allowing 1 late_alloc wave per SIMD on num_cu - 2.498*/499*late_alloc_wave64 = (info->min_good_cu_per_sa - 2) * 4;500}501502/* VS can't execute on one CU if the limit is > 2. */503if (*late_alloc_wave64 > 2)504*cu_mask = 0xfffe; /* 1 CU disabled */505}506507/* Max number that fits into the register field. */508if (ngg) /* GS */509*late_alloc_wave64 = MIN2(*late_alloc_wave64, G_00B204_SPI_SHADER_LATE_ALLOC_GS_GFX10(~0u));510else /* VS */511*late_alloc_wave64 = MIN2(*late_alloc_wave64, G_00B11C_LIMIT(~0u));512}513514515