Path: blob/21.2-virgl/src/amd/vulkan/radv_pipeline.c
7263 views
/*1* Copyright © 2016 Red Hat.2* Copyright © 2016 Bas Nieuwenhuizen3*4* based in part on anv driver which is:5* Copyright © 2015 Intel Corporation6*7* Permission is hereby granted, free of charge, to any person obtaining a8* copy of this software and associated documentation files (the "Software"),9* to deal in the Software without restriction, including without limitation10* the rights to use, copy, modify, merge, publish, distribute, sublicense,11* and/or sell copies of the Software, and to permit persons to whom the12* Software is furnished to do so, subject to the following conditions:13*14* The above copyright notice and this permission notice (including the next15* paragraph) shall be included in all copies or substantial portions of the16* Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL21* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER22* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING23* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS24* IN THE SOFTWARE.25*/2627#include "nir/nir.h"28#include "nir/nir_builder.h"29#include "nir/nir_xfb_info.h"30#include "spirv/nir_spirv.h"31#include "util/disk_cache.h"32#include "util/mesa-sha1.h"33#include "util/u_atomic.h"34#include "radv_cs.h"35#include "radv_debug.h"36#include "radv_private.h"37#include "radv_shader.h"38#include "vk_util.h"3940#include "util/debug.h"41#include "ac_binary.h"42#include "ac_exp_param.h"43#include "ac_nir.h"44#include "ac_shader_util.h"45#include "aco_interface.h"46#include "sid.h"47#include "vk_format.h"4849struct radv_blend_state {50uint32_t blend_enable_4bit;51uint32_t need_src_alpha;5253uint32_t cb_target_mask;54uint32_t cb_target_enabled_4bit;55uint32_t sx_mrt_blend_opt[8];56uint32_t cb_blend_control[8];5758uint32_t spi_shader_col_format;59uint32_t col_format_is_int8;60uint32_t col_format_is_int10;61uint32_t cb_shader_mask;62uint32_t db_alpha_to_mask;6364uint32_t commutative_4bit;6566bool single_cb_enable;67bool mrt0_is_dual_src;68};6970struct radv_dsa_order_invariance {71/* Whether the final result in Z/S buffers is guaranteed to be72* invariant under changes to the order in which fragments arrive.73*/74bool zs;7576/* Whether the set of fragments that pass the combined Z/S test is77* guaranteed to be invariant under changes to the order in which78* fragments arrive.79*/80bool pass_set;81};8283static bool84radv_is_state_dynamic(const VkGraphicsPipelineCreateInfo *pCreateInfo, VkDynamicState state)85{86if (pCreateInfo->pDynamicState) {87uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;88for (uint32_t i = 0; i < count; i++) {89if (pCreateInfo->pDynamicState->pDynamicStates[i] == state)90return true;91}92}9394return false;95}9697static const VkPipelineMultisampleStateCreateInfo *98radv_pipeline_get_multisample_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)99{100if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable ||101radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT))102return pCreateInfo->pMultisampleState;103return NULL;104}105106static const VkPipelineTessellationStateCreateInfo *107radv_pipeline_get_tessellation_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)108{109for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {110if (pCreateInfo->pStages[i].stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||111pCreateInfo->pStages[i].stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {112return pCreateInfo->pTessellationState;113}114}115return NULL;116}117118static const VkPipelineDepthStencilStateCreateInfo *119radv_pipeline_get_depth_stencil_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)120{121RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);122struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;123124if ((!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&125subpass->depth_stencil_attachment) ||126radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT))127return pCreateInfo->pDepthStencilState;128return NULL;129}130131static const VkPipelineColorBlendStateCreateInfo *132radv_pipeline_get_color_blend_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)133{134RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);135struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;136137if ((!pCreateInfo->pRasterizationState->rasterizerDiscardEnable && subpass->has_color_att) ||138radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT))139return pCreateInfo->pColorBlendState;140return NULL;141}142143static bool144radv_pipeline_has_ngg(const struct radv_pipeline *pipeline)145{146if (pipeline->graphics.last_vgt_api_stage == MESA_SHADER_NONE)147return false;148149struct radv_shader_variant *variant =150pipeline->shaders[pipeline->graphics.last_vgt_api_stage];151152return variant->info.is_ngg;153}154155bool156radv_pipeline_has_ngg_passthrough(const struct radv_pipeline *pipeline)157{158if (pipeline->graphics.last_vgt_api_stage == MESA_SHADER_NONE)159return false;160161assert(radv_pipeline_has_ngg(pipeline));162163struct radv_shader_variant *variant =164pipeline->shaders[pipeline->graphics.last_vgt_api_stage];165166return variant->info.is_ngg_passthrough;167}168169bool170radv_pipeline_has_gs_copy_shader(const struct radv_pipeline *pipeline)171{172return !!pipeline->gs_copy_shader;173}174175static void176radv_pipeline_destroy(struct radv_device *device, struct radv_pipeline *pipeline,177const VkAllocationCallbacks *allocator)178{179for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i)180if (pipeline->shaders[i])181radv_shader_variant_destroy(device, pipeline->shaders[i]);182183if (pipeline->gs_copy_shader)184radv_shader_variant_destroy(device, pipeline->gs_copy_shader);185186if (pipeline->cs.buf)187free(pipeline->cs.buf);188189vk_object_base_finish(&pipeline->base);190vk_free2(&device->vk.alloc, allocator, pipeline);191}192193void194radv_DestroyPipeline(VkDevice _device, VkPipeline _pipeline,195const VkAllocationCallbacks *pAllocator)196{197RADV_FROM_HANDLE(radv_device, device, _device);198RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);199200if (!_pipeline)201return;202203radv_pipeline_destroy(device, pipeline, pAllocator);204}205206static uint32_t207get_hash_flags(const struct radv_device *device, bool stats)208{209uint32_t hash_flags = 0;210211if (device->instance->debug_flags & RADV_DEBUG_NO_NGG)212hash_flags |= RADV_HASH_SHADER_NO_NGG;213if (device->instance->perftest_flags & RADV_PERFTEST_NGGC)214hash_flags |= RADV_HASH_SHADER_FORCE_NGG_CULLING;215if (device->physical_device->cs_wave_size == 32)216hash_flags |= RADV_HASH_SHADER_CS_WAVE32;217if (device->physical_device->ps_wave_size == 32)218hash_flags |= RADV_HASH_SHADER_PS_WAVE32;219if (device->physical_device->ge_wave_size == 32)220hash_flags |= RADV_HASH_SHADER_GE_WAVE32;221if (device->physical_device->use_llvm)222hash_flags |= RADV_HASH_SHADER_LLVM;223if (device->instance->debug_flags & RADV_DEBUG_DISCARD_TO_DEMOTE)224hash_flags |= RADV_HASH_SHADER_DISCARD_TO_DEMOTE;225if (device->instance->enable_mrt_output_nan_fixup)226hash_flags |= RADV_HASH_SHADER_MRT_NAN_FIXUP;227if (device->instance->debug_flags & RADV_DEBUG_INVARIANT_GEOM)228hash_flags |= RADV_HASH_SHADER_INVARIANT_GEOM;229if (stats)230hash_flags |= RADV_HASH_SHADER_KEEP_STATISTICS;231if (device->force_vrs != RADV_FORCE_VRS_2x2)232hash_flags |= RADV_HASH_SHADER_FORCE_VRS_2x2;233if (device->force_vrs != RADV_FORCE_VRS_2x1)234hash_flags |= RADV_HASH_SHADER_FORCE_VRS_2x1;235if (device->force_vrs != RADV_FORCE_VRS_1x2)236hash_flags |= RADV_HASH_SHADER_FORCE_VRS_1x2;237return hash_flags;238}239240static void241radv_pipeline_init_scratch(const struct radv_device *device, struct radv_pipeline *pipeline)242{243unsigned scratch_bytes_per_wave = 0;244unsigned max_waves = 0;245unsigned min_waves = 1;246247for (int i = 0; i < MESA_SHADER_STAGES; ++i) {248if (pipeline->shaders[i] && pipeline->shaders[i]->config.scratch_bytes_per_wave) {249unsigned max_stage_waves = device->scratch_waves;250251scratch_bytes_per_wave =252MAX2(scratch_bytes_per_wave, pipeline->shaders[i]->config.scratch_bytes_per_wave);253254max_stage_waves =255MIN2(max_stage_waves, 4 * device->physical_device->rad_info.num_good_compute_units *256(256 / pipeline->shaders[i]->config.num_vgprs));257max_waves = MAX2(max_waves, max_stage_waves);258}259}260261if (pipeline->shaders[MESA_SHADER_COMPUTE]) {262unsigned group_size = pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[0] *263pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[1] *264pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[2];265min_waves = MAX2(min_waves, round_up_u32(group_size, 64));266}267268pipeline->scratch_bytes_per_wave = scratch_bytes_per_wave;269pipeline->max_waves = max_waves;270}271272static uint32_t273si_translate_blend_function(VkBlendOp op)274{275switch (op) {276case VK_BLEND_OP_ADD:277return V_028780_COMB_DST_PLUS_SRC;278case VK_BLEND_OP_SUBTRACT:279return V_028780_COMB_SRC_MINUS_DST;280case VK_BLEND_OP_REVERSE_SUBTRACT:281return V_028780_COMB_DST_MINUS_SRC;282case VK_BLEND_OP_MIN:283return V_028780_COMB_MIN_DST_SRC;284case VK_BLEND_OP_MAX:285return V_028780_COMB_MAX_DST_SRC;286default:287return 0;288}289}290291static uint32_t292si_translate_blend_factor(VkBlendFactor factor)293{294switch (factor) {295case VK_BLEND_FACTOR_ZERO:296return V_028780_BLEND_ZERO;297case VK_BLEND_FACTOR_ONE:298return V_028780_BLEND_ONE;299case VK_BLEND_FACTOR_SRC_COLOR:300return V_028780_BLEND_SRC_COLOR;301case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:302return V_028780_BLEND_ONE_MINUS_SRC_COLOR;303case VK_BLEND_FACTOR_DST_COLOR:304return V_028780_BLEND_DST_COLOR;305case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR:306return V_028780_BLEND_ONE_MINUS_DST_COLOR;307case VK_BLEND_FACTOR_SRC_ALPHA:308return V_028780_BLEND_SRC_ALPHA;309case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:310return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;311case VK_BLEND_FACTOR_DST_ALPHA:312return V_028780_BLEND_DST_ALPHA;313case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA:314return V_028780_BLEND_ONE_MINUS_DST_ALPHA;315case VK_BLEND_FACTOR_CONSTANT_COLOR:316return V_028780_BLEND_CONSTANT_COLOR;317case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR:318return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;319case VK_BLEND_FACTOR_CONSTANT_ALPHA:320return V_028780_BLEND_CONSTANT_ALPHA;321case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA:322return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;323case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:324return V_028780_BLEND_SRC_ALPHA_SATURATE;325case VK_BLEND_FACTOR_SRC1_COLOR:326return V_028780_BLEND_SRC1_COLOR;327case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:328return V_028780_BLEND_INV_SRC1_COLOR;329case VK_BLEND_FACTOR_SRC1_ALPHA:330return V_028780_BLEND_SRC1_ALPHA;331case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:332return V_028780_BLEND_INV_SRC1_ALPHA;333default:334return 0;335}336}337338static uint32_t339si_translate_blend_opt_function(VkBlendOp op)340{341switch (op) {342case VK_BLEND_OP_ADD:343return V_028760_OPT_COMB_ADD;344case VK_BLEND_OP_SUBTRACT:345return V_028760_OPT_COMB_SUBTRACT;346case VK_BLEND_OP_REVERSE_SUBTRACT:347return V_028760_OPT_COMB_REVSUBTRACT;348case VK_BLEND_OP_MIN:349return V_028760_OPT_COMB_MIN;350case VK_BLEND_OP_MAX:351return V_028760_OPT_COMB_MAX;352default:353return V_028760_OPT_COMB_BLEND_DISABLED;354}355}356357static uint32_t358si_translate_blend_opt_factor(VkBlendFactor factor, bool is_alpha)359{360switch (factor) {361case VK_BLEND_FACTOR_ZERO:362return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;363case VK_BLEND_FACTOR_ONE:364return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;365case VK_BLEND_FACTOR_SRC_COLOR:366return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0367: V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;368case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:369return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1370: V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;371case VK_BLEND_FACTOR_SRC_ALPHA:372return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;373case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:374return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;375case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:376return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE377: V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;378default:379return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;380}381}382383/**384* Get rid of DST in the blend factors by commuting the operands:385* func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)386*/387static void388si_blend_remove_dst(VkBlendOp *func, VkBlendFactor *src_factor, VkBlendFactor *dst_factor,389VkBlendFactor expected_dst, VkBlendFactor replacement_src)390{391if (*src_factor == expected_dst && *dst_factor == VK_BLEND_FACTOR_ZERO) {392*src_factor = VK_BLEND_FACTOR_ZERO;393*dst_factor = replacement_src;394395/* Commuting the operands requires reversing subtractions. */396if (*func == VK_BLEND_OP_SUBTRACT)397*func = VK_BLEND_OP_REVERSE_SUBTRACT;398else if (*func == VK_BLEND_OP_REVERSE_SUBTRACT)399*func = VK_BLEND_OP_SUBTRACT;400}401}402403static bool404si_blend_factor_uses_dst(VkBlendFactor factor)405{406return factor == VK_BLEND_FACTOR_DST_COLOR || factor == VK_BLEND_FACTOR_DST_ALPHA ||407factor == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||408factor == VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA ||409factor == VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;410}411412static bool413is_dual_src(VkBlendFactor factor)414{415switch (factor) {416case VK_BLEND_FACTOR_SRC1_COLOR:417case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:418case VK_BLEND_FACTOR_SRC1_ALPHA:419case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:420return true;421default:422return false;423}424}425426static unsigned427radv_choose_spi_color_format(const struct radv_device *device, VkFormat vk_format,428bool blend_enable, bool blend_need_alpha)429{430const struct util_format_description *desc = vk_format_description(vk_format);431bool use_rbplus = device->physical_device->rad_info.rbplus_allowed;432struct ac_spi_color_formats formats = {0};433unsigned format, ntype, swap;434435format = radv_translate_colorformat(vk_format);436ntype = radv_translate_color_numformat(vk_format, desc,437vk_format_get_first_non_void_channel(vk_format));438swap = radv_translate_colorswap(vk_format, false);439440ac_choose_spi_color_formats(format, swap, ntype, false, use_rbplus, &formats);441442if (blend_enable && blend_need_alpha)443return formats.blend_alpha;444else if (blend_need_alpha)445return formats.alpha;446else if (blend_enable)447return formats.blend;448else449return formats.normal;450}451452static bool453format_is_int8(VkFormat format)454{455const struct util_format_description *desc = vk_format_description(format);456int channel = vk_format_get_first_non_void_channel(format);457458return channel >= 0 && desc->channel[channel].pure_integer && desc->channel[channel].size == 8;459}460461static bool462format_is_int10(VkFormat format)463{464const struct util_format_description *desc = vk_format_description(format);465466if (desc->nr_channels != 4)467return false;468for (unsigned i = 0; i < 4; i++) {469if (desc->channel[i].pure_integer && desc->channel[i].size == 10)470return true;471}472return false;473}474475static void476radv_pipeline_compute_spi_color_formats(const struct radv_pipeline *pipeline,477const VkGraphicsPipelineCreateInfo *pCreateInfo,478struct radv_blend_state *blend)479{480RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);481struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;482unsigned col_format = 0, is_int8 = 0, is_int10 = 0;483unsigned num_targets;484485for (unsigned i = 0; i < (blend->single_cb_enable ? 1 : subpass->color_count); ++i) {486unsigned cf;487488if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED ||489!(blend->cb_target_mask & (0xfu << (i * 4)))) {490cf = V_028714_SPI_SHADER_ZERO;491} else {492struct radv_render_pass_attachment *attachment =493pass->attachments + subpass->color_attachments[i].attachment;494bool blend_enable = blend->blend_enable_4bit & (0xfu << (i * 4));495496cf = radv_choose_spi_color_format(pipeline->device, attachment->format, blend_enable,497blend->need_src_alpha & (1 << i));498499if (format_is_int8(attachment->format))500is_int8 |= 1 << i;501if (format_is_int10(attachment->format))502is_int10 |= 1 << i;503}504505col_format |= cf << (4 * i);506}507508if (!(col_format & 0xf) && blend->need_src_alpha & (1 << 0)) {509/* When a subpass doesn't have any color attachments, write the510* alpha channel of MRT0 when alpha coverage is enabled because511* the depth attachment needs it.512*/513col_format |= V_028714_SPI_SHADER_32_AR;514}515516/* If the i-th target format is set, all previous target formats must517* be non-zero to avoid hangs.518*/519num_targets = (util_last_bit(col_format) + 3) / 4;520for (unsigned i = 0; i < num_targets; i++) {521if (!(col_format & (0xfu << (i * 4)))) {522col_format |= V_028714_SPI_SHADER_32_R << (i * 4);523}524}525526/* The output for dual source blending should have the same format as527* the first output.528*/529if (blend->mrt0_is_dual_src) {530assert(!(col_format >> 4));531col_format |= (col_format & 0xf) << 4;532}533534blend->cb_shader_mask = ac_get_cb_shader_mask(col_format);535blend->spi_shader_col_format = col_format;536blend->col_format_is_int8 = is_int8;537blend->col_format_is_int10 = is_int10;538}539540/*541* Ordered so that for each i,542* radv_format_meta_fs_key(radv_fs_key_format_exemplars[i]) == i.543*/544const VkFormat radv_fs_key_format_exemplars[NUM_META_FS_KEYS] = {545VK_FORMAT_R32_SFLOAT,546VK_FORMAT_R32G32_SFLOAT,547VK_FORMAT_R8G8B8A8_UNORM,548VK_FORMAT_R16G16B16A16_UNORM,549VK_FORMAT_R16G16B16A16_SNORM,550VK_FORMAT_R16G16B16A16_UINT,551VK_FORMAT_R16G16B16A16_SINT,552VK_FORMAT_R32G32B32A32_SFLOAT,553VK_FORMAT_R8G8B8A8_UINT,554VK_FORMAT_R8G8B8A8_SINT,555VK_FORMAT_A2R10G10B10_UINT_PACK32,556VK_FORMAT_A2R10G10B10_SINT_PACK32,557};558559unsigned560radv_format_meta_fs_key(struct radv_device *device, VkFormat format)561{562unsigned col_format = radv_choose_spi_color_format(device, format, false, false);563assert(col_format != V_028714_SPI_SHADER_32_AR);564565bool is_int8 = format_is_int8(format);566bool is_int10 = format_is_int10(format);567568if (col_format == V_028714_SPI_SHADER_UINT16_ABGR && is_int8)569return 8;570else if (col_format == V_028714_SPI_SHADER_SINT16_ABGR && is_int8)571return 9;572else if (col_format == V_028714_SPI_SHADER_UINT16_ABGR && is_int10)573return 10;574else if (col_format == V_028714_SPI_SHADER_SINT16_ABGR && is_int10)575return 11;576else {577if (col_format >= V_028714_SPI_SHADER_32_AR)578--col_format; /* Skip V_028714_SPI_SHADER_32_AR since there is no such VkFormat */579580--col_format; /* Skip V_028714_SPI_SHADER_ZERO */581return col_format;582}583}584585static void586radv_blend_check_commutativity(struct radv_blend_state *blend, VkBlendOp op, VkBlendFactor src,587VkBlendFactor dst, unsigned chanmask)588{589/* Src factor is allowed when it does not depend on Dst. */590static const uint32_t src_allowed =591(1u << VK_BLEND_FACTOR_ONE) | (1u << VK_BLEND_FACTOR_SRC_COLOR) |592(1u << VK_BLEND_FACTOR_SRC_ALPHA) | (1u << VK_BLEND_FACTOR_SRC_ALPHA_SATURATE) |593(1u << VK_BLEND_FACTOR_CONSTANT_COLOR) | (1u << VK_BLEND_FACTOR_CONSTANT_ALPHA) |594(1u << VK_BLEND_FACTOR_SRC1_COLOR) | (1u << VK_BLEND_FACTOR_SRC1_ALPHA) |595(1u << VK_BLEND_FACTOR_ZERO) | (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR) |596(1u << VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA) |597(1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR) |598(1u << VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA) |599(1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR) | (1u << VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA);600601if (dst == VK_BLEND_FACTOR_ONE && (src_allowed & (1u << src))) {602/* Addition is commutative, but floating point addition isn't603* associative: subtle changes can be introduced via different604* rounding. Be conservative, only enable for min and max.605*/606if (op == VK_BLEND_OP_MAX || op == VK_BLEND_OP_MIN)607blend->commutative_4bit |= chanmask;608}609}610611static struct radv_blend_state612radv_pipeline_init_blend_state(struct radv_pipeline *pipeline,613const VkGraphicsPipelineCreateInfo *pCreateInfo,614const struct radv_graphics_pipeline_create_info *extra)615{616const VkPipelineColorBlendStateCreateInfo *vkblend =617radv_pipeline_get_color_blend_state(pCreateInfo);618const VkPipelineMultisampleStateCreateInfo *vkms =619radv_pipeline_get_multisample_state(pCreateInfo);620struct radv_blend_state blend = {0};621unsigned mode = V_028808_CB_NORMAL;622unsigned cb_color_control = 0;623int i;624625if (extra && extra->custom_blend_mode) {626blend.single_cb_enable = true;627mode = extra->custom_blend_mode;628}629630if (vkblend) {631if (vkblend->logicOpEnable)632cb_color_control |= S_028808_ROP3(si_translate_blend_logic_op(vkblend->logicOp));633else634cb_color_control |= S_028808_ROP3(V_028808_ROP3_COPY);635}636637blend.db_alpha_to_mask = S_028B70_ALPHA_TO_MASK_OFFSET0(3) | S_028B70_ALPHA_TO_MASK_OFFSET1(1) |638S_028B70_ALPHA_TO_MASK_OFFSET2(0) | S_028B70_ALPHA_TO_MASK_OFFSET3(2) |639S_028B70_OFFSET_ROUND(1);640641if (vkms && vkms->alphaToCoverageEnable) {642blend.db_alpha_to_mask |= S_028B70_ALPHA_TO_MASK_ENABLE(1);643blend.need_src_alpha |= 0x1;644}645646blend.cb_target_mask = 0;647if (vkblend) {648for (i = 0; i < vkblend->attachmentCount; i++) {649const VkPipelineColorBlendAttachmentState *att = &vkblend->pAttachments[i];650unsigned blend_cntl = 0;651unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;652VkBlendOp eqRGB = att->colorBlendOp;653VkBlendFactor srcRGB = att->srcColorBlendFactor;654VkBlendFactor dstRGB = att->dstColorBlendFactor;655VkBlendOp eqA = att->alphaBlendOp;656VkBlendFactor srcA = att->srcAlphaBlendFactor;657VkBlendFactor dstA = att->dstAlphaBlendFactor;658659blend.sx_mrt_blend_opt[i] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) |660S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);661662if (!att->colorWriteMask)663continue;664665/* Ignore other blend targets if dual-source blending666* is enabled to prevent wrong behaviour.667*/668if (blend.mrt0_is_dual_src)669continue;670671blend.cb_target_mask |= (unsigned)att->colorWriteMask << (4 * i);672blend.cb_target_enabled_4bit |= 0xfu << (4 * i);673if (!att->blendEnable) {674blend.cb_blend_control[i] = blend_cntl;675continue;676}677678if (is_dual_src(srcRGB) || is_dual_src(dstRGB) || is_dual_src(srcA) || is_dual_src(dstA))679if (i == 0)680blend.mrt0_is_dual_src = true;681682if (eqRGB == VK_BLEND_OP_MIN || eqRGB == VK_BLEND_OP_MAX) {683srcRGB = VK_BLEND_FACTOR_ONE;684dstRGB = VK_BLEND_FACTOR_ONE;685}686if (eqA == VK_BLEND_OP_MIN || eqA == VK_BLEND_OP_MAX) {687srcA = VK_BLEND_FACTOR_ONE;688dstA = VK_BLEND_FACTOR_ONE;689}690691radv_blend_check_commutativity(&blend, eqRGB, srcRGB, dstRGB, 0x7u << (4 * i));692radv_blend_check_commutativity(&blend, eqA, srcA, dstA, 0x8u << (4 * i));693694/* Blending optimizations for RB+.695* These transformations don't change the behavior.696*697* First, get rid of DST in the blend factors:698* func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)699*/700si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB, VK_BLEND_FACTOR_DST_COLOR,701VK_BLEND_FACTOR_SRC_COLOR);702703si_blend_remove_dst(&eqA, &srcA, &dstA, VK_BLEND_FACTOR_DST_COLOR,704VK_BLEND_FACTOR_SRC_COLOR);705706si_blend_remove_dst(&eqA, &srcA, &dstA, VK_BLEND_FACTOR_DST_ALPHA,707VK_BLEND_FACTOR_SRC_ALPHA);708709/* Look up the ideal settings from tables. */710srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);711dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);712srcA_opt = si_translate_blend_opt_factor(srcA, true);713dstA_opt = si_translate_blend_opt_factor(dstA, true);714715/* Handle interdependencies. */716if (si_blend_factor_uses_dst(srcRGB))717dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;718if (si_blend_factor_uses_dst(srcA))719dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;720721if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE &&722(dstRGB == VK_BLEND_FACTOR_ZERO || dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||723dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE))724dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;725726/* Set the final value. */727blend.sx_mrt_blend_opt[i] =728S_028760_COLOR_SRC_OPT(srcRGB_opt) | S_028760_COLOR_DST_OPT(dstRGB_opt) |729S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |730S_028760_ALPHA_SRC_OPT(srcA_opt) | S_028760_ALPHA_DST_OPT(dstA_opt) |731S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));732blend_cntl |= S_028780_ENABLE(1);733734blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));735blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));736blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));737if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {738blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);739blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));740blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));741blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));742}743blend.cb_blend_control[i] = blend_cntl;744745blend.blend_enable_4bit |= 0xfu << (i * 4);746747if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA || dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||748srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||749dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||750srcRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA ||751dstRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA)752blend.need_src_alpha |= 1 << i;753}754for (i = vkblend->attachmentCount; i < 8; i++) {755blend.cb_blend_control[i] = 0;756blend.sx_mrt_blend_opt[i] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) |757S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);758}759}760761if (pipeline->device->physical_device->rad_info.has_rbplus) {762/* Disable RB+ blend optimizations for dual source blending. */763if (blend.mrt0_is_dual_src) {764for (i = 0; i < 8; i++) {765blend.sx_mrt_blend_opt[i] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE) |766S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE);767}768}769770/* RB+ doesn't work with dual source blending, logic op and771* RESOLVE.772*/773if (blend.mrt0_is_dual_src || (vkblend && vkblend->logicOpEnable) ||774mode == V_028808_CB_RESOLVE)775cb_color_control |= S_028808_DISABLE_DUAL_QUAD(1);776}777778if (blend.cb_target_mask)779cb_color_control |= S_028808_MODE(mode);780else781cb_color_control |= S_028808_MODE(V_028808_CB_DISABLE);782783radv_pipeline_compute_spi_color_formats(pipeline, pCreateInfo, &blend);784785pipeline->graphics.cb_color_control = cb_color_control;786787return blend;788}789790static uint32_t791si_translate_fill(VkPolygonMode func)792{793switch (func) {794case VK_POLYGON_MODE_FILL:795return V_028814_X_DRAW_TRIANGLES;796case VK_POLYGON_MODE_LINE:797return V_028814_X_DRAW_LINES;798case VK_POLYGON_MODE_POINT:799return V_028814_X_DRAW_POINTS;800default:801assert(0);802return V_028814_X_DRAW_POINTS;803}804}805806static uint8_t807radv_pipeline_get_ps_iter_samples(const VkGraphicsPipelineCreateInfo *pCreateInfo)808{809const VkPipelineMultisampleStateCreateInfo *vkms = pCreateInfo->pMultisampleState;810RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);811struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];812uint32_t ps_iter_samples = 1;813uint32_t num_samples;814815/* From the Vulkan 1.1.129 spec, 26.7. Sample Shading:816*817* "If the VK_AMD_mixed_attachment_samples extension is enabled and the818* subpass uses color attachments, totalSamples is the number of819* samples of the color attachments. Otherwise, totalSamples is the820* value of VkPipelineMultisampleStateCreateInfo::rasterizationSamples821* specified at pipeline creation time."822*/823if (subpass->has_color_att) {824num_samples = subpass->color_sample_count;825} else {826num_samples = vkms->rasterizationSamples;827}828829if (vkms->sampleShadingEnable) {830ps_iter_samples = ceilf(vkms->minSampleShading * num_samples);831ps_iter_samples = util_next_power_of_two(ps_iter_samples);832}833return ps_iter_samples;834}835836static bool837radv_is_depth_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)838{839return pCreateInfo->depthTestEnable && pCreateInfo->depthWriteEnable &&840pCreateInfo->depthCompareOp != VK_COMPARE_OP_NEVER;841}842843static bool844radv_writes_stencil(const VkStencilOpState *state)845{846return state->writeMask &&847(state->failOp != VK_STENCIL_OP_KEEP || state->passOp != VK_STENCIL_OP_KEEP ||848state->depthFailOp != VK_STENCIL_OP_KEEP);849}850851static bool852radv_is_stencil_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)853{854return pCreateInfo->stencilTestEnable &&855(radv_writes_stencil(&pCreateInfo->front) || radv_writes_stencil(&pCreateInfo->back));856}857858static bool859radv_is_ds_write_enabled(const VkPipelineDepthStencilStateCreateInfo *pCreateInfo)860{861return radv_is_depth_write_enabled(pCreateInfo) || radv_is_stencil_write_enabled(pCreateInfo);862}863864static bool865radv_order_invariant_stencil_op(VkStencilOp op)866{867/* REPLACE is normally order invariant, except when the stencil868* reference value is written by the fragment shader. Tracking this869* interaction does not seem worth the effort, so be conservative.870*/871return op != VK_STENCIL_OP_INCREMENT_AND_CLAMP && op != VK_STENCIL_OP_DECREMENT_AND_CLAMP &&872op != VK_STENCIL_OP_REPLACE;873}874875static bool876radv_order_invariant_stencil_state(const VkStencilOpState *state)877{878/* Compute whether, assuming Z writes are disabled, this stencil state879* is order invariant in the sense that the set of passing fragments as880* well as the final stencil buffer result does not depend on the order881* of fragments.882*/883return !state->writeMask ||884/* The following assumes that Z writes are disabled. */885(state->compareOp == VK_COMPARE_OP_ALWAYS &&886radv_order_invariant_stencil_op(state->passOp) &&887radv_order_invariant_stencil_op(state->depthFailOp)) ||888(state->compareOp == VK_COMPARE_OP_NEVER &&889radv_order_invariant_stencil_op(state->failOp));890}891892static bool893radv_pipeline_has_dynamic_ds_states(const VkGraphicsPipelineCreateInfo *pCreateInfo)894{895VkDynamicState ds_states[] = {896VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT, VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT,897VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT, VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT,898VK_DYNAMIC_STATE_STENCIL_OP_EXT,899};900901for (uint32_t i = 0; i < ARRAY_SIZE(ds_states); i++) {902if (radv_is_state_dynamic(pCreateInfo, ds_states[i]))903return true;904}905906return false;907}908909static bool910radv_pipeline_out_of_order_rast(struct radv_pipeline *pipeline,911const struct radv_blend_state *blend,912const VkGraphicsPipelineCreateInfo *pCreateInfo)913{914RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);915struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;916const VkPipelineDepthStencilStateCreateInfo *vkds =917radv_pipeline_get_depth_stencil_state(pCreateInfo);918const VkPipelineColorBlendStateCreateInfo *vkblend =919radv_pipeline_get_color_blend_state(pCreateInfo);920unsigned colormask = blend->cb_target_enabled_4bit;921922if (!pipeline->device->physical_device->out_of_order_rast_allowed)923return false;924925/* Be conservative if a logic operation is enabled with color buffers. */926if (colormask && vkblend && vkblend->logicOpEnable)927return false;928929/* Be conservative if an extended dynamic depth/stencil state is930* enabled because the driver can't update out-of-order rasterization931* dynamically.932*/933if (radv_pipeline_has_dynamic_ds_states(pCreateInfo))934return false;935936/* Default depth/stencil invariance when no attachment is bound. */937struct radv_dsa_order_invariance dsa_order_invariant = {.zs = true, .pass_set = true};938939if (vkds) {940struct radv_render_pass_attachment *attachment =941pass->attachments + subpass->depth_stencil_attachment->attachment;942bool has_stencil = vk_format_has_stencil(attachment->format);943struct radv_dsa_order_invariance order_invariance[2];944struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];945946/* Compute depth/stencil order invariance in order to know if947* it's safe to enable out-of-order.948*/949bool zfunc_is_ordered = vkds->depthCompareOp == VK_COMPARE_OP_NEVER ||950vkds->depthCompareOp == VK_COMPARE_OP_LESS ||951vkds->depthCompareOp == VK_COMPARE_OP_LESS_OR_EQUAL ||952vkds->depthCompareOp == VK_COMPARE_OP_GREATER ||953vkds->depthCompareOp == VK_COMPARE_OP_GREATER_OR_EQUAL;954955bool nozwrite_and_order_invariant_stencil =956!radv_is_ds_write_enabled(vkds) ||957(!radv_is_depth_write_enabled(vkds) && radv_order_invariant_stencil_state(&vkds->front) &&958radv_order_invariant_stencil_state(&vkds->back));959960order_invariance[1].zs = nozwrite_and_order_invariant_stencil ||961(!radv_is_stencil_write_enabled(vkds) && zfunc_is_ordered);962order_invariance[0].zs = !radv_is_depth_write_enabled(vkds) || zfunc_is_ordered;963964order_invariance[1].pass_set =965nozwrite_and_order_invariant_stencil ||966(!radv_is_stencil_write_enabled(vkds) && (vkds->depthCompareOp == VK_COMPARE_OP_ALWAYS ||967vkds->depthCompareOp == VK_COMPARE_OP_NEVER));968order_invariance[0].pass_set =969!radv_is_depth_write_enabled(vkds) || (vkds->depthCompareOp == VK_COMPARE_OP_ALWAYS ||970vkds->depthCompareOp == VK_COMPARE_OP_NEVER);971972dsa_order_invariant = order_invariance[has_stencil];973if (!dsa_order_invariant.zs)974return false;975976/* The set of PS invocations is always order invariant,977* except when early Z/S tests are requested.978*/979if (ps && ps->info.ps.writes_memory && ps->info.ps.early_fragment_test &&980!dsa_order_invariant.pass_set)981return false;982983/* Determine if out-of-order rasterization should be disabled984* when occlusion queries are used.985*/986pipeline->graphics.disable_out_of_order_rast_for_occlusion = !dsa_order_invariant.pass_set;987}988989/* No color buffers are enabled for writing. */990if (!colormask)991return true;992993unsigned blendmask = colormask & blend->blend_enable_4bit;994995if (blendmask) {996/* Only commutative blending. */997if (blendmask & ~blend->commutative_4bit)998return false;9991000if (!dsa_order_invariant.pass_set)1001return false;1002}10031004if (colormask & ~blendmask)1005return false;10061007return true;1008}10091010static const VkConservativeRasterizationModeEXT1011radv_get_conservative_raster_mode(const VkPipelineRasterizationStateCreateInfo *pCreateInfo)1012{1013const VkPipelineRasterizationConservativeStateCreateInfoEXT *conservative_raster =1014vk_find_struct_const(pCreateInfo->pNext,1015PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT);10161017if (!conservative_raster)1018return VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT;1019return conservative_raster->conservativeRasterizationMode;1020}10211022static void1023radv_pipeline_init_multisample_state(struct radv_pipeline *pipeline,1024const struct radv_blend_state *blend,1025const VkGraphicsPipelineCreateInfo *pCreateInfo)1026{1027const VkPipelineMultisampleStateCreateInfo *vkms =1028radv_pipeline_get_multisample_state(pCreateInfo);1029struct radv_multisample_state *ms = &pipeline->graphics.ms;1030unsigned num_tile_pipes = pipeline->device->physical_device->rad_info.num_tile_pipes;1031const VkConservativeRasterizationModeEXT mode =1032radv_get_conservative_raster_mode(pCreateInfo->pRasterizationState);1033bool out_of_order_rast = false;1034int ps_iter_samples = 1;1035uint32_t mask = 0xffff;10361037if (vkms) {1038ms->num_samples = vkms->rasterizationSamples;10391040/* From the Vulkan 1.1.129 spec, 26.7. Sample Shading:1041*1042* "Sample shading is enabled for a graphics pipeline:1043*1044* - If the interface of the fragment shader entry point of the1045* graphics pipeline includes an input variable decorated1046* with SampleId or SamplePosition. In this case1047* minSampleShadingFactor takes the value 1.0.1048* - Else if the sampleShadingEnable member of the1049* VkPipelineMultisampleStateCreateInfo structure specified1050* when creating the graphics pipeline is set to VK_TRUE. In1051* this case minSampleShadingFactor takes the value of1052* VkPipelineMultisampleStateCreateInfo::minSampleShading.1053*1054* Otherwise, sample shading is considered disabled."1055*/1056if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.ps.uses_sample_shading) {1057ps_iter_samples = ms->num_samples;1058} else {1059ps_iter_samples = radv_pipeline_get_ps_iter_samples(pCreateInfo);1060}1061} else {1062ms->num_samples = 1;1063}10641065const struct VkPipelineRasterizationStateRasterizationOrderAMD *raster_order =1066vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,1067PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD);1068if (raster_order && raster_order->rasterizationOrder == VK_RASTERIZATION_ORDER_RELAXED_AMD) {1069/* Out-of-order rasterization is explicitly enabled by the1070* application.1071*/1072out_of_order_rast = true;1073} else {1074/* Determine if the driver can enable out-of-order1075* rasterization internally.1076*/1077out_of_order_rast = radv_pipeline_out_of_order_rast(pipeline, blend, pCreateInfo);1078}10791080ms->pa_sc_aa_config = 0;1081ms->db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) | S_028804_INCOHERENT_EQAA_READS(1) |1082S_028804_INTERPOLATE_COMP_Z(1) | S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);10831084/* Adjust MSAA state if conservative rasterization is enabled. */1085if (mode != VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT) {1086ms->pa_sc_aa_config |= S_028BE0_AA_MASK_CENTROID_DTMN(1);10871088ms->db_eqaa |=1089S_028804_ENABLE_POSTZ_OVERRASTERIZATION(1) | S_028804_OVERRASTERIZATION_AMOUNT(4);1090}10911092ms->pa_sc_mode_cntl_1 =1093S_028A4C_WALK_FENCE_ENABLE(1) | // TODO linear dst fixes1094S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |1095S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast) |1096S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |1097/* always 1: */1098S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) | S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |1099S_028A4C_TILE_WALK_ORDER_ENABLE(1) | S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |1100S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) | S_028A4C_FORCE_EOV_REZ_ENABLE(1);1101ms->pa_sc_mode_cntl_0 = S_028A48_ALTERNATE_RBS_PER_TILE(1102pipeline->device->physical_device->rad_info.chip_class >= GFX9) |1103S_028A48_VPORT_SCISSOR_ENABLE(1);11041105const VkPipelineRasterizationLineStateCreateInfoEXT *rast_line = vk_find_struct_const(1106pCreateInfo->pRasterizationState->pNext, PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);1107if (rast_line) {1108ms->pa_sc_mode_cntl_0 |= S_028A48_LINE_STIPPLE_ENABLE(rast_line->stippledLineEnable);1109if (rast_line->lineRasterizationMode == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT) {1110/* From the Vulkan spec 1.1.129:1111*1112* "When VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT lines1113* are being rasterized, sample locations may all be1114* treated as being at the pixel center (this may1115* affect attribute and depth interpolation)."1116*/1117ms->num_samples = 1;1118}1119}11201121if (ms->num_samples > 1) {1122RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);1123struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];1124uint32_t z_samples =1125subpass->depth_stencil_attachment ? subpass->depth_sample_count : ms->num_samples;1126unsigned log_samples = util_logbase2(ms->num_samples);1127unsigned log_z_samples = util_logbase2(z_samples);1128unsigned log_ps_iter_samples = util_logbase2(ps_iter_samples);1129ms->pa_sc_mode_cntl_0 |= S_028A48_MSAA_ENABLE(1);1130ms->db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_z_samples) |1131S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |1132S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |1133S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);1134ms->pa_sc_aa_config |=1135S_028BE0_MSAA_NUM_SAMPLES(log_samples) |1136S_028BE0_MAX_SAMPLE_DIST(radv_get_default_max_sample_dist(log_samples)) |1137S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples) | /* CM_R_028BE0_PA_SC_AA_CONFIG */1138S_028BE0_COVERED_CENTROID_IS_CENTER(1139pipeline->device->physical_device->rad_info.chip_class >= GFX10_3);1140ms->pa_sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);1141if (ps_iter_samples > 1)1142pipeline->graphics.spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(2);1143}11441145if (vkms && vkms->pSampleMask) {1146mask = vkms->pSampleMask[0] & 0xffff;1147}11481149ms->pa_sc_aa_mask[0] = mask | (mask << 16);1150ms->pa_sc_aa_mask[1] = mask | (mask << 16);1151}11521153static void1154gfx103_pipeline_init_vrs_state(struct radv_pipeline *pipeline,1155const VkGraphicsPipelineCreateInfo *pCreateInfo)1156{1157const VkPipelineMultisampleStateCreateInfo *vkms =1158radv_pipeline_get_multisample_state(pCreateInfo);1159struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];1160struct radv_multisample_state *ms = &pipeline->graphics.ms;1161struct radv_vrs_state *vrs = &pipeline->graphics.vrs;11621163if (vkms && (vkms->sampleShadingEnable || ps->info.ps.uses_sample_shading ||1164ps->info.ps.reads_sample_mask_in)) {1165/* Disable VRS and use the rates from PS_ITER_SAMPLES if:1166*1167* 1) sample shading is enabled or per-sample interpolation is1168* used by the fragment shader1169* 2) the fragment shader reads gl_SampleMaskIn because the1170* 16-bit sample coverage mask isn't enough for MSAA8x and1171* 2x2 coarse shading isn't enough.1172*/1173vrs->pa_cl_vrs_cntl = S_028848_SAMPLE_ITER_COMBINER_MODE(V_028848_VRS_COMB_MODE_OVERRIDE);11741175/* Make sure sample shading is enabled even if only MSAA1x is1176* used because the SAMPLE_ITER combiner is in passthrough1177* mode if PS_ITER_SAMPLE is 0, and it uses the per-draw rate.1178* The default VRS rate when sample shading is enabled is 1x1.1179*/1180if (!G_028A4C_PS_ITER_SAMPLE(ms->pa_sc_mode_cntl_1))1181ms->pa_sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(1);1182} else {1183vrs->pa_cl_vrs_cntl = S_028848_SAMPLE_ITER_COMBINER_MODE(V_028848_VRS_COMB_MODE_PASSTHRU);1184}11851186/* The primitive combiner is always passthrough. */1187vrs->pa_cl_vrs_cntl |= S_028848_PRIMITIVE_RATE_COMBINER_MODE(V_028848_VRS_COMB_MODE_PASSTHRU);1188}11891190static bool1191radv_prim_can_use_guardband(enum VkPrimitiveTopology topology)1192{1193switch (topology) {1194case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:1195case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:1196case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:1197case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:1198case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:1199return false;1200case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:1201case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:1202case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:1203case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:1204case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:1205case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:1206return true;1207default:1208unreachable("unhandled primitive type");1209}1210}12111212static uint32_t1213si_conv_gl_prim_to_gs_out(unsigned gl_prim)1214{1215switch (gl_prim) {1216case 0: /* GL_POINTS */1217return V_028A6C_POINTLIST;1218case 1: /* GL_LINES */1219case 3: /* GL_LINE_STRIP */1220case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */1221case 0x8E7A: /* GL_ISOLINES */1222return V_028A6C_LINESTRIP;12231224case 4: /* GL_TRIANGLES */1225case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */1226case 5: /* GL_TRIANGLE_STRIP */1227case 7: /* GL_QUADS */1228return V_028A6C_TRISTRIP;1229default:1230assert(0);1231return 0;1232}1233}12341235static uint32_t1236si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology)1237{1238switch (topology) {1239case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:1240case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:1241return V_028A6C_POINTLIST;1242case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:1243case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:1244case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:1245case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:1246return V_028A6C_LINESTRIP;1247case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:1248case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:1249case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:1250case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:1251case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:1252return V_028A6C_TRISTRIP;1253default:1254assert(0);1255return 0;1256}1257}12581259static uint64_t1260radv_dynamic_state_mask(VkDynamicState state)1261{1262switch (state) {1263case VK_DYNAMIC_STATE_VIEWPORT:1264case VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT:1265return RADV_DYNAMIC_VIEWPORT;1266case VK_DYNAMIC_STATE_SCISSOR:1267case VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT:1268return RADV_DYNAMIC_SCISSOR;1269case VK_DYNAMIC_STATE_LINE_WIDTH:1270return RADV_DYNAMIC_LINE_WIDTH;1271case VK_DYNAMIC_STATE_DEPTH_BIAS:1272return RADV_DYNAMIC_DEPTH_BIAS;1273case VK_DYNAMIC_STATE_BLEND_CONSTANTS:1274return RADV_DYNAMIC_BLEND_CONSTANTS;1275case VK_DYNAMIC_STATE_DEPTH_BOUNDS:1276return RADV_DYNAMIC_DEPTH_BOUNDS;1277case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:1278return RADV_DYNAMIC_STENCIL_COMPARE_MASK;1279case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:1280return RADV_DYNAMIC_STENCIL_WRITE_MASK;1281case VK_DYNAMIC_STATE_STENCIL_REFERENCE:1282return RADV_DYNAMIC_STENCIL_REFERENCE;1283case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:1284return RADV_DYNAMIC_DISCARD_RECTANGLE;1285case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT:1286return RADV_DYNAMIC_SAMPLE_LOCATIONS;1287case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT:1288return RADV_DYNAMIC_LINE_STIPPLE;1289case VK_DYNAMIC_STATE_CULL_MODE_EXT:1290return RADV_DYNAMIC_CULL_MODE;1291case VK_DYNAMIC_STATE_FRONT_FACE_EXT:1292return RADV_DYNAMIC_FRONT_FACE;1293case VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT:1294return RADV_DYNAMIC_PRIMITIVE_TOPOLOGY;1295case VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT:1296return RADV_DYNAMIC_DEPTH_TEST_ENABLE;1297case VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT:1298return RADV_DYNAMIC_DEPTH_WRITE_ENABLE;1299case VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT:1300return RADV_DYNAMIC_DEPTH_COMPARE_OP;1301case VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT:1302return RADV_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE;1303case VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT:1304return RADV_DYNAMIC_STENCIL_TEST_ENABLE;1305case VK_DYNAMIC_STATE_STENCIL_OP_EXT:1306return RADV_DYNAMIC_STENCIL_OP;1307case VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT:1308return RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE;1309case VK_DYNAMIC_STATE_FRAGMENT_SHADING_RATE_KHR:1310return RADV_DYNAMIC_FRAGMENT_SHADING_RATE;1311case VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT:1312return RADV_DYNAMIC_PATCH_CONTROL_POINTS;1313case VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT:1314return RADV_DYNAMIC_RASTERIZER_DISCARD_ENABLE;1315case VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT:1316return RADV_DYNAMIC_DEPTH_BIAS_ENABLE;1317case VK_DYNAMIC_STATE_LOGIC_OP_EXT:1318return RADV_DYNAMIC_LOGIC_OP;1319case VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT:1320return RADV_DYNAMIC_PRIMITIVE_RESTART_ENABLE;1321case VK_DYNAMIC_STATE_COLOR_WRITE_ENABLE_EXT:1322return RADV_DYNAMIC_COLOR_WRITE_ENABLE;1323default:1324unreachable("Unhandled dynamic state");1325}1326}13271328static bool1329radv_pipeline_is_blend_enabled(const VkGraphicsPipelineCreateInfo *pCreateInfo)1330{1331const VkPipelineColorBlendStateCreateInfo *vkblend =1332radv_pipeline_get_color_blend_state(pCreateInfo);13331334assert(vkblend);13351336for (uint32_t i = 0; i < vkblend->attachmentCount; i++) {1337const VkPipelineColorBlendAttachmentState *att = &vkblend->pAttachments[i];1338if (att->colorWriteMask && att->blendEnable)1339return true;1340}1341return false;1342}13431344static uint64_t1345radv_pipeline_needed_dynamic_state(const VkGraphicsPipelineCreateInfo *pCreateInfo)1346{1347RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);1348struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];1349uint64_t states = RADV_DYNAMIC_ALL;13501351/* If rasterization is disabled we do not care about any of the1352* dynamic states, since they are all rasterization related only,1353* except primitive topology, primitive restart enable, vertex1354* binding stride and rasterization discard itself.1355*/1356if (pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&1357!radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT)) {1358return RADV_DYNAMIC_PRIMITIVE_TOPOLOGY | RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE |1359RADV_DYNAMIC_PRIMITIVE_RESTART_ENABLE | RADV_DYNAMIC_RASTERIZER_DISCARD_ENABLE;1360}13611362if (!pCreateInfo->pRasterizationState->depthBiasEnable &&1363!radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT))1364states &= ~RADV_DYNAMIC_DEPTH_BIAS;13651366if (!pCreateInfo->pDepthStencilState ||1367(!pCreateInfo->pDepthStencilState->depthBoundsTestEnable &&1368!radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT)))1369states &= ~RADV_DYNAMIC_DEPTH_BOUNDS;13701371if (!pCreateInfo->pDepthStencilState ||1372(!pCreateInfo->pDepthStencilState->stencilTestEnable &&1373!radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT)))1374states &= ~(RADV_DYNAMIC_STENCIL_COMPARE_MASK | RADV_DYNAMIC_STENCIL_WRITE_MASK |1375RADV_DYNAMIC_STENCIL_REFERENCE);13761377if (!vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT))1378states &= ~RADV_DYNAMIC_DISCARD_RECTANGLE;13791380if (!pCreateInfo->pMultisampleState ||1381!vk_find_struct_const(pCreateInfo->pMultisampleState->pNext,1382PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT))1383states &= ~RADV_DYNAMIC_SAMPLE_LOCATIONS;13841385if (!pCreateInfo->pRasterizationState ||1386!vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,1387PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT))1388states &= ~RADV_DYNAMIC_LINE_STIPPLE;13891390if (!vk_find_struct_const(pCreateInfo->pNext,1391PIPELINE_FRAGMENT_SHADING_RATE_STATE_CREATE_INFO_KHR) &&1392!radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_FRAGMENT_SHADING_RATE_KHR))1393states &= ~RADV_DYNAMIC_FRAGMENT_SHADING_RATE;13941395if (!subpass->has_color_att ||1396!radv_pipeline_is_blend_enabled(pCreateInfo))1397states &= ~RADV_DYNAMIC_BLEND_CONSTANTS;13981399if (!subpass->has_color_att)1400states &= ~RADV_DYNAMIC_COLOR_WRITE_ENABLE;14011402return states;1403}14041405static struct radv_ia_multi_vgt_param_helpers1406radv_compute_ia_multi_vgt_param_helpers(struct radv_pipeline *pipeline)1407{1408struct radv_ia_multi_vgt_param_helpers ia_multi_vgt_param = {0};1409const struct radv_device *device = pipeline->device;14101411if (radv_pipeline_has_tess(pipeline))1412ia_multi_vgt_param.primgroup_size =1413pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.num_tess_patches;1414else if (radv_pipeline_has_gs(pipeline))1415ia_multi_vgt_param.primgroup_size = 64;1416else1417ia_multi_vgt_param.primgroup_size = 128; /* recommended without a GS */14181419/* GS requirement. */1420ia_multi_vgt_param.partial_es_wave = false;1421if (radv_pipeline_has_gs(pipeline) && device->physical_device->rad_info.chip_class <= GFX8)1422if (SI_GS_PER_ES / ia_multi_vgt_param.primgroup_size >= pipeline->device->gs_table_depth - 3)1423ia_multi_vgt_param.partial_es_wave = true;14241425ia_multi_vgt_param.ia_switch_on_eoi = false;1426if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.ps.prim_id_input)1427ia_multi_vgt_param.ia_switch_on_eoi = true;1428if (radv_pipeline_has_gs(pipeline) && pipeline->shaders[MESA_SHADER_GEOMETRY]->info.uses_prim_id)1429ia_multi_vgt_param.ia_switch_on_eoi = true;1430if (radv_pipeline_has_tess(pipeline)) {1431/* SWITCH_ON_EOI must be set if PrimID is used. */1432if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.uses_prim_id ||1433radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.uses_prim_id)1434ia_multi_vgt_param.ia_switch_on_eoi = true;1435}14361437ia_multi_vgt_param.partial_vs_wave = false;1438if (radv_pipeline_has_tess(pipeline)) {1439/* Bug with tessellation and GS on Bonaire and older 2 SE chips. */1440if ((device->physical_device->rad_info.family == CHIP_TAHITI ||1441device->physical_device->rad_info.family == CHIP_PITCAIRN ||1442device->physical_device->rad_info.family == CHIP_BONAIRE) &&1443radv_pipeline_has_gs(pipeline))1444ia_multi_vgt_param.partial_vs_wave = true;1445/* Needed for 028B6C_DISTRIBUTION_MODE != 0 */1446if (device->physical_device->rad_info.has_distributed_tess) {1447if (radv_pipeline_has_gs(pipeline)) {1448if (device->physical_device->rad_info.chip_class <= GFX8)1449ia_multi_vgt_param.partial_es_wave = true;1450} else {1451ia_multi_vgt_param.partial_vs_wave = true;1452}1453}1454}14551456if (radv_pipeline_has_gs(pipeline)) {1457/* On these chips there is the possibility of a hang if the1458* pipeline uses a GS and partial_vs_wave is not set.1459*1460* This mostly does not hit 4-SE chips, as those typically set1461* ia_switch_on_eoi and then partial_vs_wave is set for pipelines1462* with GS due to another workaround.1463*1464* Reproducer: https://bugs.freedesktop.org/show_bug.cgi?id=1092421465*/1466if (device->physical_device->rad_info.family == CHIP_TONGA ||1467device->physical_device->rad_info.family == CHIP_FIJI ||1468device->physical_device->rad_info.family == CHIP_POLARIS10 ||1469device->physical_device->rad_info.family == CHIP_POLARIS11 ||1470device->physical_device->rad_info.family == CHIP_POLARIS12 ||1471device->physical_device->rad_info.family == CHIP_VEGAM) {1472ia_multi_vgt_param.partial_vs_wave = true;1473}1474}14751476ia_multi_vgt_param.base =1477S_028AA8_PRIMGROUP_SIZE(ia_multi_vgt_param.primgroup_size - 1) |1478/* The following field was moved to VGT_SHADER_STAGES_EN in GFX9. */1479S_028AA8_MAX_PRIMGRP_IN_WAVE(device->physical_device->rad_info.chip_class == GFX8 ? 2 : 0) |1480S_030960_EN_INST_OPT_BASIC(device->physical_device->rad_info.chip_class >= GFX9) |1481S_030960_EN_INST_OPT_ADV(device->physical_device->rad_info.chip_class >= GFX9);14821483return ia_multi_vgt_param;1484}14851486static void1487radv_pipeline_init_input_assembly_state(struct radv_pipeline *pipeline,1488const VkGraphicsPipelineCreateInfo *pCreateInfo,1489const struct radv_graphics_pipeline_create_info *extra)1490{1491const VkPipelineInputAssemblyStateCreateInfo *ia_state = pCreateInfo->pInputAssemblyState;1492struct radv_shader_variant *tes = pipeline->shaders[MESA_SHADER_TESS_EVAL];1493struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];14941495pipeline->graphics.can_use_guardband = radv_prim_can_use_guardband(ia_state->topology);14961497if (radv_pipeline_has_gs(pipeline)) {1498if (si_conv_gl_prim_to_gs_out(gs->info.gs.output_prim) == V_028A6C_TRISTRIP)1499pipeline->graphics.can_use_guardband = true;1500} else if (radv_pipeline_has_tess(pipeline)) {1501if (!tes->info.tes.point_mode &&1502si_conv_gl_prim_to_gs_out(tes->info.tes.primitive_mode) == V_028A6C_TRISTRIP)1503pipeline->graphics.can_use_guardband = true;1504}15051506if (extra && extra->use_rectlist) {1507pipeline->graphics.can_use_guardband = true;1508}15091510pipeline->graphics.ia_multi_vgt_param = radv_compute_ia_multi_vgt_param_helpers(pipeline);1511}15121513static void1514radv_pipeline_init_dynamic_state(struct radv_pipeline *pipeline,1515const VkGraphicsPipelineCreateInfo *pCreateInfo,1516const struct radv_graphics_pipeline_create_info *extra)1517{1518uint64_t needed_states = radv_pipeline_needed_dynamic_state(pCreateInfo);1519uint64_t states = needed_states;1520RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);1521struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];15221523pipeline->dynamic_state = default_dynamic_state;1524pipeline->graphics.needed_dynamic_state = needed_states;15251526if (pCreateInfo->pDynamicState) {1527/* Remove all of the states that are marked as dynamic */1528uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;1529for (uint32_t s = 0; s < count; s++)1530states &= ~radv_dynamic_state_mask(pCreateInfo->pDynamicState->pDynamicStates[s]);1531}15321533struct radv_dynamic_state *dynamic = &pipeline->dynamic_state;15341535if (needed_states & RADV_DYNAMIC_VIEWPORT) {1536assert(pCreateInfo->pViewportState);15371538dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;1539if (states & RADV_DYNAMIC_VIEWPORT) {1540typed_memcpy(dynamic->viewport.viewports, pCreateInfo->pViewportState->pViewports,1541pCreateInfo->pViewportState->viewportCount);1542}1543}15441545if (needed_states & RADV_DYNAMIC_SCISSOR) {1546dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;1547if (states & RADV_DYNAMIC_SCISSOR) {1548typed_memcpy(dynamic->scissor.scissors, pCreateInfo->pViewportState->pScissors,1549pCreateInfo->pViewportState->scissorCount);1550}1551}15521553if (states & RADV_DYNAMIC_LINE_WIDTH) {1554assert(pCreateInfo->pRasterizationState);1555dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;1556}15571558if (states & RADV_DYNAMIC_DEPTH_BIAS) {1559assert(pCreateInfo->pRasterizationState);1560dynamic->depth_bias.bias = pCreateInfo->pRasterizationState->depthBiasConstantFactor;1561dynamic->depth_bias.clamp = pCreateInfo->pRasterizationState->depthBiasClamp;1562dynamic->depth_bias.slope = pCreateInfo->pRasterizationState->depthBiasSlopeFactor;1563}15641565/* Section 9.2 of the Vulkan 1.0.15 spec says:1566*1567* pColorBlendState is [...] NULL if the pipeline has rasterization1568* disabled or if the subpass of the render pass the pipeline is1569* created against does not use any color attachments.1570*/1571if (states & RADV_DYNAMIC_BLEND_CONSTANTS) {1572assert(pCreateInfo->pColorBlendState);1573typed_memcpy(dynamic->blend_constants, pCreateInfo->pColorBlendState->blendConstants, 4);1574}15751576if (states & RADV_DYNAMIC_CULL_MODE) {1577dynamic->cull_mode = pCreateInfo->pRasterizationState->cullMode;1578}15791580if (states & RADV_DYNAMIC_FRONT_FACE) {1581dynamic->front_face = pCreateInfo->pRasterizationState->frontFace;1582}15831584if (states & RADV_DYNAMIC_PRIMITIVE_TOPOLOGY) {1585dynamic->primitive_topology = si_translate_prim(pCreateInfo->pInputAssemblyState->topology);1586if (extra && extra->use_rectlist) {1587dynamic->primitive_topology = V_008958_DI_PT_RECTLIST;1588}1589}15901591/* If there is no depthstencil attachment, then don't read1592* pDepthStencilState. The Vulkan spec states that pDepthStencilState may1593* be NULL in this case. Even if pDepthStencilState is non-NULL, there is1594* no need to override the depthstencil defaults in1595* radv_pipeline::dynamic_state when there is no depthstencil attachment.1596*1597* Section 9.2 of the Vulkan 1.0.15 spec says:1598*1599* pDepthStencilState is [...] NULL if the pipeline has rasterization1600* disabled or if the subpass of the render pass the pipeline is created1601* against does not use a depth/stencil attachment.1602*/1603if (needed_states && subpass->depth_stencil_attachment) {1604if (states & RADV_DYNAMIC_DEPTH_BOUNDS) {1605dynamic->depth_bounds.min = pCreateInfo->pDepthStencilState->minDepthBounds;1606dynamic->depth_bounds.max = pCreateInfo->pDepthStencilState->maxDepthBounds;1607}16081609if (states & RADV_DYNAMIC_STENCIL_COMPARE_MASK) {1610dynamic->stencil_compare_mask.front = pCreateInfo->pDepthStencilState->front.compareMask;1611dynamic->stencil_compare_mask.back = pCreateInfo->pDepthStencilState->back.compareMask;1612}16131614if (states & RADV_DYNAMIC_STENCIL_WRITE_MASK) {1615dynamic->stencil_write_mask.front = pCreateInfo->pDepthStencilState->front.writeMask;1616dynamic->stencil_write_mask.back = pCreateInfo->pDepthStencilState->back.writeMask;1617}16181619if (states & RADV_DYNAMIC_STENCIL_REFERENCE) {1620dynamic->stencil_reference.front = pCreateInfo->pDepthStencilState->front.reference;1621dynamic->stencil_reference.back = pCreateInfo->pDepthStencilState->back.reference;1622}16231624if (states & RADV_DYNAMIC_DEPTH_TEST_ENABLE) {1625dynamic->depth_test_enable = pCreateInfo->pDepthStencilState->depthTestEnable;1626}16271628if (states & RADV_DYNAMIC_DEPTH_WRITE_ENABLE) {1629dynamic->depth_write_enable = pCreateInfo->pDepthStencilState->depthWriteEnable;1630}16311632if (states & RADV_DYNAMIC_DEPTH_COMPARE_OP) {1633dynamic->depth_compare_op = pCreateInfo->pDepthStencilState->depthCompareOp;1634}16351636if (states & RADV_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE) {1637dynamic->depth_bounds_test_enable = pCreateInfo->pDepthStencilState->depthBoundsTestEnable;1638}16391640if (states & RADV_DYNAMIC_STENCIL_TEST_ENABLE) {1641dynamic->stencil_test_enable = pCreateInfo->pDepthStencilState->stencilTestEnable;1642}16431644if (states & RADV_DYNAMIC_STENCIL_OP) {1645dynamic->stencil_op.front.compare_op = pCreateInfo->pDepthStencilState->front.compareOp;1646dynamic->stencil_op.front.fail_op = pCreateInfo->pDepthStencilState->front.failOp;1647dynamic->stencil_op.front.pass_op = pCreateInfo->pDepthStencilState->front.passOp;1648dynamic->stencil_op.front.depth_fail_op =1649pCreateInfo->pDepthStencilState->front.depthFailOp;16501651dynamic->stencil_op.back.compare_op = pCreateInfo->pDepthStencilState->back.compareOp;1652dynamic->stencil_op.back.fail_op = pCreateInfo->pDepthStencilState->back.failOp;1653dynamic->stencil_op.back.pass_op = pCreateInfo->pDepthStencilState->back.passOp;1654dynamic->stencil_op.back.depth_fail_op = pCreateInfo->pDepthStencilState->back.depthFailOp;1655}1656}16571658const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =1659vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);1660if (needed_states & RADV_DYNAMIC_DISCARD_RECTANGLE) {1661dynamic->discard_rectangle.count = discard_rectangle_info->discardRectangleCount;1662if (states & RADV_DYNAMIC_DISCARD_RECTANGLE) {1663typed_memcpy(dynamic->discard_rectangle.rectangles,1664discard_rectangle_info->pDiscardRectangles,1665discard_rectangle_info->discardRectangleCount);1666}1667}16681669if (needed_states & RADV_DYNAMIC_SAMPLE_LOCATIONS) {1670const VkPipelineSampleLocationsStateCreateInfoEXT *sample_location_info =1671vk_find_struct_const(pCreateInfo->pMultisampleState->pNext,1672PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT);1673/* If sampleLocationsEnable is VK_FALSE, the default sample1674* locations are used and the values specified in1675* sampleLocationsInfo are ignored.1676*/1677if (sample_location_info->sampleLocationsEnable) {1678const VkSampleLocationsInfoEXT *pSampleLocationsInfo =1679&sample_location_info->sampleLocationsInfo;16801681assert(pSampleLocationsInfo->sampleLocationsCount <= MAX_SAMPLE_LOCATIONS);16821683dynamic->sample_location.per_pixel = pSampleLocationsInfo->sampleLocationsPerPixel;1684dynamic->sample_location.grid_size = pSampleLocationsInfo->sampleLocationGridSize;1685dynamic->sample_location.count = pSampleLocationsInfo->sampleLocationsCount;1686typed_memcpy(&dynamic->sample_location.locations[0],1687pSampleLocationsInfo->pSampleLocations,1688pSampleLocationsInfo->sampleLocationsCount);1689}1690}16911692const VkPipelineRasterizationLineStateCreateInfoEXT *rast_line_info = vk_find_struct_const(1693pCreateInfo->pRasterizationState->pNext, PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);1694if (needed_states & RADV_DYNAMIC_LINE_STIPPLE) {1695dynamic->line_stipple.factor = rast_line_info->lineStippleFactor;1696dynamic->line_stipple.pattern = rast_line_info->lineStipplePattern;1697}16981699if (!(states & RADV_DYNAMIC_VERTEX_INPUT_BINDING_STRIDE))1700pipeline->graphics.uses_dynamic_stride = true;17011702const VkPipelineFragmentShadingRateStateCreateInfoKHR *shading_rate = vk_find_struct_const(1703pCreateInfo->pNext, PIPELINE_FRAGMENT_SHADING_RATE_STATE_CREATE_INFO_KHR);1704if (states & RADV_DYNAMIC_FRAGMENT_SHADING_RATE) {1705dynamic->fragment_shading_rate.size = shading_rate->fragmentSize;1706for (int i = 0; i < 2; i++)1707dynamic->fragment_shading_rate.combiner_ops[i] = shading_rate->combinerOps[i];1708}17091710if (states & RADV_DYNAMIC_DEPTH_BIAS_ENABLE) {1711dynamic->depth_bias_enable = pCreateInfo->pRasterizationState->depthBiasEnable;1712}17131714if (states & RADV_DYNAMIC_PRIMITIVE_RESTART_ENABLE) {1715dynamic->primitive_restart_enable =1716!!pCreateInfo->pInputAssemblyState->primitiveRestartEnable;1717}17181719if (states & RADV_DYNAMIC_RASTERIZER_DISCARD_ENABLE) {1720dynamic->rasterizer_discard_enable =1721pCreateInfo->pRasterizationState->rasterizerDiscardEnable;1722}17231724if (subpass->has_color_att && states & RADV_DYNAMIC_LOGIC_OP) {1725if (pCreateInfo->pColorBlendState->logicOpEnable) {1726dynamic->logic_op = si_translate_blend_logic_op(pCreateInfo->pColorBlendState->logicOp);1727} else {1728dynamic->logic_op = V_028808_ROP3_COPY;1729}1730}17311732if (states & RADV_DYNAMIC_COLOR_WRITE_ENABLE) {1733const VkPipelineColorWriteCreateInfoEXT *color_write_info = vk_find_struct_const(1734pCreateInfo->pColorBlendState->pNext, PIPELINE_COLOR_WRITE_CREATE_INFO_EXT);1735if (color_write_info) {1736dynamic->color_write_enable = 0;1737for (uint32_t i = 0; i < color_write_info->attachmentCount; i++) {1738dynamic->color_write_enable |=1739color_write_info->pColorWriteEnables[i] ? (0xfu << (i * 4)) : 0;1740}1741}1742}17431744pipeline->dynamic_state.mask = states;1745}17461747static void1748radv_pipeline_init_raster_state(struct radv_pipeline *pipeline,1749const VkGraphicsPipelineCreateInfo *pCreateInfo)1750{1751const VkPipelineRasterizationStateCreateInfo *raster_info = pCreateInfo->pRasterizationState;1752const VkPipelineRasterizationProvokingVertexStateCreateInfoEXT *provoking_vtx_info =1753vk_find_struct_const(raster_info->pNext,1754PIPELINE_RASTERIZATION_PROVOKING_VERTEX_STATE_CREATE_INFO_EXT);1755bool provoking_vtx_last = false;17561757if (provoking_vtx_info &&1758provoking_vtx_info->provokingVertexMode == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT) {1759provoking_vtx_last = true;1760}17611762pipeline->graphics.pa_su_sc_mode_cntl =1763S_028814_FACE(raster_info->frontFace) |1764S_028814_CULL_FRONT(!!(raster_info->cullMode & VK_CULL_MODE_FRONT_BIT)) |1765S_028814_CULL_BACK(!!(raster_info->cullMode & VK_CULL_MODE_BACK_BIT)) |1766S_028814_POLY_MODE(raster_info->polygonMode != VK_POLYGON_MODE_FILL) |1767S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(raster_info->polygonMode)) |1768S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(raster_info->polygonMode)) |1769S_028814_POLY_OFFSET_FRONT_ENABLE(raster_info->depthBiasEnable ? 1 : 0) |1770S_028814_POLY_OFFSET_BACK_ENABLE(raster_info->depthBiasEnable ? 1 : 0) |1771S_028814_POLY_OFFSET_PARA_ENABLE(raster_info->depthBiasEnable ? 1 : 0) |1772S_028814_PROVOKING_VTX_LAST(provoking_vtx_last);17731774if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {1775/* It should also be set if PERPENDICULAR_ENDCAP_ENA is set. */1776pipeline->graphics.pa_su_sc_mode_cntl |=1777S_028814_KEEP_TOGETHER_ENABLE(raster_info->polygonMode != VK_POLYGON_MODE_FILL);1778}17791780bool depth_clip_disable = raster_info->depthClampEnable;1781const VkPipelineRasterizationDepthClipStateCreateInfoEXT *depth_clip_state =1782vk_find_struct_const(raster_info->pNext,1783PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT);1784if (depth_clip_state) {1785depth_clip_disable = !depth_clip_state->depthClipEnable;1786}17871788pipeline->graphics.pa_cl_clip_cntl =1789S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.1790S_028810_ZCLIP_NEAR_DISABLE(depth_clip_disable ? 1 : 0) |1791S_028810_ZCLIP_FAR_DISABLE(depth_clip_disable ? 1 : 0) |1792S_028810_DX_RASTERIZATION_KILL(raster_info->rasterizerDiscardEnable ? 1 : 0) |1793S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);17941795pipeline->graphics.uses_conservative_overestimate =1796radv_get_conservative_raster_mode(pCreateInfo->pRasterizationState) ==1797VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT;1798}17991800static void1801radv_pipeline_init_depth_stencil_state(struct radv_pipeline *pipeline,1802const VkGraphicsPipelineCreateInfo *pCreateInfo)1803{1804const VkPipelineDepthStencilStateCreateInfo *ds_info =1805radv_pipeline_get_depth_stencil_state(pCreateInfo);1806RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);1807struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;1808struct radv_render_pass_attachment *attachment = NULL;1809uint32_t db_depth_control = 0;18101811if (subpass->depth_stencil_attachment)1812attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;18131814bool has_depth_attachment = attachment && vk_format_has_depth(attachment->format);1815bool has_stencil_attachment = attachment && vk_format_has_stencil(attachment->format);18161817if (ds_info) {1818if (has_depth_attachment) {1819db_depth_control = S_028800_Z_ENABLE(ds_info->depthTestEnable ? 1 : 0) |1820S_028800_Z_WRITE_ENABLE(ds_info->depthWriteEnable ? 1 : 0) |1821S_028800_ZFUNC(ds_info->depthCompareOp) |1822S_028800_DEPTH_BOUNDS_ENABLE(ds_info->depthBoundsTestEnable ? 1 : 0);1823}18241825if (has_stencil_attachment && ds_info->stencilTestEnable) {1826db_depth_control |= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);1827db_depth_control |= S_028800_STENCILFUNC(ds_info->front.compareOp);1828db_depth_control |= S_028800_STENCILFUNC_BF(ds_info->back.compareOp);1829}1830}18311832pipeline->graphics.db_depth_control = db_depth_control;1833}18341835static void1836gfx9_get_gs_info(const struct radv_pipeline_key *key, const struct radv_pipeline *pipeline,1837nir_shader **nir, struct radv_shader_info *infos, struct gfx9_gs_info *out)1838{1839struct radv_shader_info *gs_info = &infos[MESA_SHADER_GEOMETRY];1840struct radv_es_output_info *es_info;1841if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)1842es_info = nir[MESA_SHADER_TESS_CTRL] ? &gs_info->tes.es_info : &gs_info->vs.es_info;1843else1844es_info = nir[MESA_SHADER_TESS_CTRL] ? &infos[MESA_SHADER_TESS_EVAL].tes.es_info1845: &infos[MESA_SHADER_VERTEX].vs.es_info;18461847unsigned gs_num_invocations = MAX2(gs_info->gs.invocations, 1);1848bool uses_adjacency;1849switch (key->topology) {1850case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:1851case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:1852case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:1853case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:1854uses_adjacency = true;1855break;1856default:1857uses_adjacency = false;1858break;1859}18601861/* All these are in dwords: */1862/* We can't allow using the whole LDS, because GS waves compete with1863* other shader stages for LDS space. */1864const unsigned max_lds_size = 8 * 1024;1865const unsigned esgs_itemsize = es_info->esgs_itemsize / 4;1866unsigned esgs_lds_size;18671868/* All these are per subgroup: */1869const unsigned max_out_prims = 32 * 1024;1870const unsigned max_es_verts = 255;1871const unsigned ideal_gs_prims = 64;1872unsigned max_gs_prims, gs_prims;1873unsigned min_es_verts, es_verts, worst_case_es_verts;18741875if (uses_adjacency || gs_num_invocations > 1)1876max_gs_prims = 127 / gs_num_invocations;1877else1878max_gs_prims = 255;18791880/* MAX_PRIMS_PER_SUBGROUP = gs_prims * max_vert_out * gs_invocations.1881* Make sure we don't go over the maximum value.1882*/1883if (gs_info->gs.vertices_out > 0) {1884max_gs_prims =1885MIN2(max_gs_prims, max_out_prims / (gs_info->gs.vertices_out * gs_num_invocations));1886}1887assert(max_gs_prims > 0);18881889/* If the primitive has adjacency, halve the number of vertices1890* that will be reused in multiple primitives.1891*/1892min_es_verts = gs_info->gs.vertices_in / (uses_adjacency ? 2 : 1);18931894gs_prims = MIN2(ideal_gs_prims, max_gs_prims);1895worst_case_es_verts = MIN2(min_es_verts * gs_prims, max_es_verts);18961897/* Compute ESGS LDS size based on the worst case number of ES vertices1898* needed to create the target number of GS prims per subgroup.1899*/1900esgs_lds_size = esgs_itemsize * worst_case_es_verts;19011902/* If total LDS usage is too big, refactor partitions based on ratio1903* of ESGS item sizes.1904*/1905if (esgs_lds_size > max_lds_size) {1906/* Our target GS Prims Per Subgroup was too large. Calculate1907* the maximum number of GS Prims Per Subgroup that will fit1908* into LDS, capped by the maximum that the hardware can support.1909*/1910gs_prims = MIN2((max_lds_size / (esgs_itemsize * min_es_verts)), max_gs_prims);1911assert(gs_prims > 0);1912worst_case_es_verts = MIN2(min_es_verts * gs_prims, max_es_verts);19131914esgs_lds_size = esgs_itemsize * worst_case_es_verts;1915assert(esgs_lds_size <= max_lds_size);1916}19171918/* Now calculate remaining ESGS information. */1919if (esgs_lds_size)1920es_verts = MIN2(esgs_lds_size / esgs_itemsize, max_es_verts);1921else1922es_verts = max_es_verts;19231924/* Vertices for adjacency primitives are not always reused, so restore1925* it for ES_VERTS_PER_SUBGRP.1926*/1927min_es_verts = gs_info->gs.vertices_in;19281929/* For normal primitives, the VGT only checks if they are past the ES1930* verts per subgroup after allocating a full GS primitive and if they1931* are, kick off a new subgroup. But if those additional ES verts are1932* unique (e.g. not reused) we need to make sure there is enough LDS1933* space to account for those ES verts beyond ES_VERTS_PER_SUBGRP.1934*/1935es_verts -= min_es_verts - 1;19361937uint32_t es_verts_per_subgroup = es_verts;1938uint32_t gs_prims_per_subgroup = gs_prims;1939uint32_t gs_inst_prims_in_subgroup = gs_prims * gs_num_invocations;1940uint32_t max_prims_per_subgroup = gs_inst_prims_in_subgroup * gs_info->gs.vertices_out;1941out->lds_size = align(esgs_lds_size, 128) / 128;1942out->vgt_gs_onchip_cntl = S_028A44_ES_VERTS_PER_SUBGRP(es_verts_per_subgroup) |1943S_028A44_GS_PRIMS_PER_SUBGRP(gs_prims_per_subgroup) |1944S_028A44_GS_INST_PRIMS_IN_SUBGRP(gs_inst_prims_in_subgroup);1945out->vgt_gs_max_prims_per_subgroup = S_028A94_MAX_PRIMS_PER_SUBGROUP(max_prims_per_subgroup);1946out->vgt_esgs_ring_itemsize = esgs_itemsize;1947assert(max_prims_per_subgroup <= max_out_prims);1948}19491950static void1951clamp_gsprims_to_esverts(unsigned *max_gsprims, unsigned max_esverts, unsigned min_verts_per_prim,1952bool use_adjacency)1953{1954unsigned max_reuse = max_esverts - min_verts_per_prim;1955if (use_adjacency)1956max_reuse /= 2;1957*max_gsprims = MIN2(*max_gsprims, 1 + max_reuse);1958}19591960static unsigned1961radv_get_num_input_vertices(nir_shader **nir)1962{1963if (nir[MESA_SHADER_GEOMETRY]) {1964nir_shader *gs = nir[MESA_SHADER_GEOMETRY];19651966return gs->info.gs.vertices_in;1967}19681969if (nir[MESA_SHADER_TESS_CTRL]) {1970nir_shader *tes = nir[MESA_SHADER_TESS_EVAL];19711972if (tes->info.tess.point_mode)1973return 1;1974if (tes->info.tess.primitive_mode == GL_ISOLINES)1975return 2;1976return 3;1977}19781979return 3;1980}19811982static void1983gfx10_get_ngg_info(const struct radv_pipeline_key *key, struct radv_pipeline *pipeline,1984nir_shader **nir, struct radv_shader_info *infos, struct gfx10_ngg_info *ngg)1985{1986struct radv_shader_info *gs_info = &infos[MESA_SHADER_GEOMETRY];1987struct radv_es_output_info *es_info =1988nir[MESA_SHADER_TESS_CTRL] ? &gs_info->tes.es_info : &gs_info->vs.es_info;1989unsigned gs_type = nir[MESA_SHADER_GEOMETRY] ? MESA_SHADER_GEOMETRY : MESA_SHADER_VERTEX;1990unsigned max_verts_per_prim = radv_get_num_input_vertices(nir);1991unsigned min_verts_per_prim = gs_type == MESA_SHADER_GEOMETRY ? max_verts_per_prim : 1;1992unsigned gs_num_invocations = nir[MESA_SHADER_GEOMETRY] ? MAX2(gs_info->gs.invocations, 1) : 1;1993bool uses_adjacency;1994switch (key->topology) {1995case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:1996case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:1997case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:1998case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:1999uses_adjacency = true;2000break;2001default:2002uses_adjacency = false;2003break;2004}20052006/* All these are in dwords: */2007/* We can't allow using the whole LDS, because GS waves compete with2008* other shader stages for LDS space.2009*2010* TODO: We should really take the shader's internal LDS use into2011* account. The linker will fail if the size is greater than2012* 8K dwords.2013*/2014const unsigned max_lds_size = 8 * 1024 - 768;2015const unsigned target_lds_size = max_lds_size;2016unsigned esvert_lds_size = 0;2017unsigned gsprim_lds_size = 0;20182019/* All these are per subgroup: */2020const unsigned min_esverts =2021pipeline->device->physical_device->rad_info.chip_class >= GFX10_3 ? 29 : 24;2022bool max_vert_out_per_gs_instance = false;2023unsigned max_esverts_base = 256;2024unsigned max_gsprims_base = 128; /* default prim group size clamp */20252026/* Hardware has the following non-natural restrictions on the value2027* of GE_CNTL.VERT_GRP_SIZE based on based on the primitive type of2028* the draw:2029* - at most 252 for any line input primitive type2030* - at most 251 for any quad input primitive type2031* - at most 251 for triangle strips with adjacency (this happens to2032* be the natural limit for triangle *lists* with adjacency)2033*/2034max_esverts_base = MIN2(max_esverts_base, 251 + max_verts_per_prim - 1);20352036if (gs_type == MESA_SHADER_GEOMETRY) {2037unsigned max_out_verts_per_gsprim = gs_info->gs.vertices_out * gs_num_invocations;20382039if (max_out_verts_per_gsprim <= 256) {2040if (max_out_verts_per_gsprim) {2041max_gsprims_base = MIN2(max_gsprims_base, 256 / max_out_verts_per_gsprim);2042}2043} else {2044/* Use special multi-cycling mode in which each GS2045* instance gets its own subgroup. Does not work with2046* tessellation. */2047max_vert_out_per_gs_instance = true;2048max_gsprims_base = 1;2049max_out_verts_per_gsprim = gs_info->gs.vertices_out;2050}20512052esvert_lds_size = es_info->esgs_itemsize / 4;2053gsprim_lds_size = (gs_info->gs.gsvs_vertex_size / 4 + 1) * max_out_verts_per_gsprim;2054} else {2055/* VS and TES. */2056/* LDS size for passing data from GS to ES. */2057struct radv_streamout_info *so_info = nir[MESA_SHADER_TESS_CTRL]2058? &infos[MESA_SHADER_TESS_EVAL].so2059: &infos[MESA_SHADER_VERTEX].so;20602061if (so_info->num_outputs)2062esvert_lds_size = 4 * so_info->num_outputs + 1;20632064/* GS stores Primitive IDs (one DWORD) into LDS at the address2065* corresponding to the ES thread of the provoking vertex. All2066* ES threads load and export PrimitiveID for their thread.2067*/2068if (!nir[MESA_SHADER_TESS_CTRL] && infos[MESA_SHADER_VERTEX].vs.outinfo.export_prim_id)2069esvert_lds_size = MAX2(esvert_lds_size, 1);2070}20712072unsigned max_gsprims = max_gsprims_base;2073unsigned max_esverts = max_esverts_base;20742075if (esvert_lds_size)2076max_esverts = MIN2(max_esverts, target_lds_size / esvert_lds_size);2077if (gsprim_lds_size)2078max_gsprims = MIN2(max_gsprims, target_lds_size / gsprim_lds_size);20792080max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);2081clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, uses_adjacency);2082assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);20832084if (esvert_lds_size || gsprim_lds_size) {2085/* Now that we have a rough proportionality between esverts2086* and gsprims based on the primitive type, scale both of them2087* down simultaneously based on required LDS space.2088*2089* We could be smarter about this if we knew how much vertex2090* reuse to expect.2091*/2092unsigned lds_total = max_esverts * esvert_lds_size + max_gsprims * gsprim_lds_size;2093if (lds_total > target_lds_size) {2094max_esverts = max_esverts * target_lds_size / lds_total;2095max_gsprims = max_gsprims * target_lds_size / lds_total;20962097max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);2098clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, uses_adjacency);2099assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);2100}2101}21022103/* Round up towards full wave sizes for better ALU utilization. */2104if (!max_vert_out_per_gs_instance) {2105unsigned orig_max_esverts;2106unsigned orig_max_gsprims;2107unsigned wavesize;21082109if (gs_type == MESA_SHADER_GEOMETRY) {2110wavesize = gs_info->wave_size;2111} else {2112wavesize = nir[MESA_SHADER_TESS_CTRL] ? infos[MESA_SHADER_TESS_EVAL].wave_size2113: infos[MESA_SHADER_VERTEX].wave_size;2114}21152116do {2117orig_max_esverts = max_esverts;2118orig_max_gsprims = max_gsprims;21192120max_esverts = align(max_esverts, wavesize);2121max_esverts = MIN2(max_esverts, max_esverts_base);2122if (esvert_lds_size)2123max_esverts =2124MIN2(max_esverts, (max_lds_size - max_gsprims * gsprim_lds_size) / esvert_lds_size);2125max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);21262127/* Hardware restriction: minimum value of max_esverts */2128if (pipeline->device->physical_device->rad_info.chip_class == GFX10)2129max_esverts = MAX2(max_esverts, min_esverts - 1 + max_verts_per_prim);2130else2131max_esverts = MAX2(max_esverts, min_esverts);21322133max_gsprims = align(max_gsprims, wavesize);2134max_gsprims = MIN2(max_gsprims, max_gsprims_base);2135if (gsprim_lds_size) {2136/* Don't count unusable vertices to the LDS2137* size. Those are vertices above the maximum2138* number of vertices that can occur in the2139* workgroup, which is e.g. max_gsprims * 32140* for triangles.2141*/2142unsigned usable_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);2143max_gsprims = MIN2(max_gsprims,2144(max_lds_size - usable_esverts * esvert_lds_size) / gsprim_lds_size);2145}2146clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, uses_adjacency);2147assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);2148} while (orig_max_esverts != max_esverts || orig_max_gsprims != max_gsprims);21492150/* Verify the restriction. */2151if (pipeline->device->physical_device->rad_info.chip_class == GFX10)2152assert(max_esverts >= min_esverts - 1 + max_verts_per_prim);2153else2154assert(max_esverts >= min_esverts);2155} else {2156/* Hardware restriction: minimum value of max_esverts */2157if (pipeline->device->physical_device->rad_info.chip_class == GFX10)2158max_esverts = MAX2(max_esverts, min_esverts - 1 + max_verts_per_prim);2159else2160max_esverts = MAX2(max_esverts, min_esverts);2161}21622163unsigned max_out_vertices = max_vert_out_per_gs_instance ? gs_info->gs.vertices_out2164: gs_type == MESA_SHADER_GEOMETRY2165? max_gsprims * gs_num_invocations * gs_info->gs.vertices_out2166: max_esverts;2167assert(max_out_vertices <= 256);21682169unsigned prim_amp_factor = 1;2170if (gs_type == MESA_SHADER_GEOMETRY) {2171/* Number of output primitives per GS input primitive after2172* GS instancing. */2173prim_amp_factor = gs_info->gs.vertices_out;2174}21752176/* On Gfx10, the GE only checks against the maximum number of ES verts2177* after allocating a full GS primitive. So we need to ensure that2178* whenever this check passes, there is enough space for a full2179* primitive without vertex reuse.2180*/2181if (pipeline->device->physical_device->rad_info.chip_class == GFX10)2182ngg->hw_max_esverts = max_esverts - max_verts_per_prim + 1;2183else2184ngg->hw_max_esverts = max_esverts;21852186ngg->max_gsprims = max_gsprims;2187ngg->max_out_verts = max_out_vertices;2188ngg->prim_amp_factor = prim_amp_factor;2189ngg->max_vert_out_per_gs_instance = max_vert_out_per_gs_instance;2190ngg->ngg_emit_size = max_gsprims * gsprim_lds_size;2191ngg->enable_vertex_grouping = false;21922193/* Don't count unusable vertices. */2194ngg->esgs_ring_size = MIN2(max_esverts, max_gsprims * max_verts_per_prim) * esvert_lds_size * 4;21952196if (gs_type == MESA_SHADER_GEOMETRY) {2197ngg->vgt_esgs_ring_itemsize = es_info->esgs_itemsize / 4;2198} else {2199ngg->vgt_esgs_ring_itemsize = 1;2200}22012202assert(ngg->hw_max_esverts >= min_esverts); /* HW limitation */2203}22042205static void2206radv_pipeline_init_gs_ring_state(struct radv_pipeline *pipeline, const struct gfx9_gs_info *gs)2207{2208struct radv_device *device = pipeline->device;2209unsigned num_se = device->physical_device->rad_info.max_se;2210unsigned wave_size = 64;2211unsigned max_gs_waves = 32 * num_se; /* max 32 per SE on GCN */2212/* On GFX6-GFX7, the value comes from VGT_GS_VERTEX_REUSE = 16.2213* On GFX8+, the value comes from VGT_VERTEX_REUSE_BLOCK_CNTL = 30 (+2).2214*/2215unsigned gs_vertex_reuse =2216(device->physical_device->rad_info.chip_class >= GFX8 ? 32 : 16) * num_se;2217unsigned alignment = 256 * num_se;2218/* The maximum size is 63.999 MB per SE. */2219unsigned max_size = ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se;2220struct radv_shader_info *gs_info = &pipeline->shaders[MESA_SHADER_GEOMETRY]->info;22212222/* Calculate the minimum size. */2223unsigned min_esgs_ring_size =2224align(gs->vgt_esgs_ring_itemsize * 4 * gs_vertex_reuse * wave_size, alignment);2225/* These are recommended sizes, not minimum sizes. */2226unsigned esgs_ring_size =2227max_gs_waves * 2 * wave_size * gs->vgt_esgs_ring_itemsize * 4 * gs_info->gs.vertices_in;2228unsigned gsvs_ring_size = max_gs_waves * 2 * wave_size * gs_info->gs.max_gsvs_emit_size;22292230min_esgs_ring_size = align(min_esgs_ring_size, alignment);2231esgs_ring_size = align(esgs_ring_size, alignment);2232gsvs_ring_size = align(gsvs_ring_size, alignment);22332234if (pipeline->device->physical_device->rad_info.chip_class <= GFX8)2235pipeline->graphics.esgs_ring_size = CLAMP(esgs_ring_size, min_esgs_ring_size, max_size);22362237pipeline->graphics.gsvs_ring_size = MIN2(gsvs_ring_size, max_size);2238}22392240struct radv_shader_variant *2241radv_get_shader(const struct radv_pipeline *pipeline, gl_shader_stage stage)2242{2243if (stage == MESA_SHADER_VERTEX) {2244if (pipeline->shaders[MESA_SHADER_VERTEX])2245return pipeline->shaders[MESA_SHADER_VERTEX];2246if (pipeline->shaders[MESA_SHADER_TESS_CTRL])2247return pipeline->shaders[MESA_SHADER_TESS_CTRL];2248if (pipeline->shaders[MESA_SHADER_GEOMETRY])2249return pipeline->shaders[MESA_SHADER_GEOMETRY];2250} else if (stage == MESA_SHADER_TESS_EVAL) {2251if (!radv_pipeline_has_tess(pipeline))2252return NULL;2253if (pipeline->shaders[MESA_SHADER_TESS_EVAL])2254return pipeline->shaders[MESA_SHADER_TESS_EVAL];2255if (pipeline->shaders[MESA_SHADER_GEOMETRY])2256return pipeline->shaders[MESA_SHADER_GEOMETRY];2257}2258return pipeline->shaders[stage];2259}22602261static const struct radv_vs_output_info *2262get_vs_output_info(const struct radv_pipeline *pipeline)2263{2264if (radv_pipeline_has_gs(pipeline))2265if (radv_pipeline_has_ngg(pipeline))2266return &pipeline->shaders[MESA_SHADER_GEOMETRY]->info.vs.outinfo;2267else2268return &pipeline->gs_copy_shader->info.vs.outinfo;2269else if (radv_pipeline_has_tess(pipeline))2270return &pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.outinfo;2271else2272return &pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.outinfo;2273}22742275static void2276radv_link_shaders(struct radv_pipeline *pipeline, nir_shader **shaders,2277bool optimize_conservatively)2278{2279nir_shader *ordered_shaders[MESA_SHADER_STAGES];2280int shader_count = 0;22812282if (shaders[MESA_SHADER_FRAGMENT]) {2283ordered_shaders[shader_count++] = shaders[MESA_SHADER_FRAGMENT];2284}2285if (shaders[MESA_SHADER_GEOMETRY]) {2286ordered_shaders[shader_count++] = shaders[MESA_SHADER_GEOMETRY];2287}2288if (shaders[MESA_SHADER_TESS_EVAL]) {2289ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_EVAL];2290}2291if (shaders[MESA_SHADER_TESS_CTRL]) {2292ordered_shaders[shader_count++] = shaders[MESA_SHADER_TESS_CTRL];2293}2294if (shaders[MESA_SHADER_VERTEX]) {2295ordered_shaders[shader_count++] = shaders[MESA_SHADER_VERTEX];2296}2297if (shaders[MESA_SHADER_COMPUTE]) {2298ordered_shaders[shader_count++] = shaders[MESA_SHADER_COMPUTE];2299}23002301bool has_geom_tess = shaders[MESA_SHADER_GEOMETRY] || shaders[MESA_SHADER_TESS_CTRL];2302bool merged_gs = shaders[MESA_SHADER_GEOMETRY] &&2303pipeline->device->physical_device->rad_info.chip_class >= GFX9;23042305if (!optimize_conservatively && shader_count > 1) {2306unsigned first = ordered_shaders[shader_count - 1]->info.stage;2307unsigned last = ordered_shaders[0]->info.stage;23082309if (ordered_shaders[0]->info.stage == MESA_SHADER_FRAGMENT &&2310ordered_shaders[1]->info.has_transform_feedback_varyings)2311nir_link_xfb_varyings(ordered_shaders[1], ordered_shaders[0]);23122313for (int i = 1; i < shader_count; ++i) {2314nir_lower_io_arrays_to_elements(ordered_shaders[i], ordered_shaders[i - 1]);2315}23162317for (int i = 0; i < shader_count; ++i) {2318nir_variable_mode mask = 0;23192320if (ordered_shaders[i]->info.stage != first)2321mask = mask | nir_var_shader_in;23222323if (ordered_shaders[i]->info.stage != last)2324mask = mask | nir_var_shader_out;23252326if (nir_lower_io_to_scalar_early(ordered_shaders[i], mask)) {2327/* Optimize the new vector code and then remove dead vars */2328nir_copy_prop(ordered_shaders[i]);2329nir_opt_shrink_vectors(ordered_shaders[i],2330!pipeline->device->instance->disable_shrink_image_store);23312332if (ordered_shaders[i]->info.stage != last) {2333/* Optimize swizzled movs of load_const for2334* nir_link_opt_varyings's constant propagation2335*/2336nir_opt_constant_folding(ordered_shaders[i]);2337/* For nir_link_opt_varyings's duplicate input opt */2338nir_opt_cse(ordered_shaders[i]);2339}23402341/* Run copy-propagation to help remove dead2342* output variables (some shaders have useless2343* copies to/from an output), so compaction2344* later will be more effective.2345*2346* This will have been done earlier but it might2347* not have worked because the outputs were vector.2348*/2349if (ordered_shaders[i]->info.stage == MESA_SHADER_TESS_CTRL)2350nir_opt_copy_prop_vars(ordered_shaders[i]);23512352nir_opt_dce(ordered_shaders[i]);2353nir_remove_dead_variables(2354ordered_shaders[i], nir_var_function_temp | nir_var_shader_in | nir_var_shader_out,2355NULL);2356}2357}2358}23592360for (int i = 1; !optimize_conservatively && (i < shader_count); ++i) {2361if (nir_link_opt_varyings(ordered_shaders[i], ordered_shaders[i - 1])) {2362nir_opt_constant_folding(ordered_shaders[i - 1]);2363nir_opt_algebraic(ordered_shaders[i - 1]);2364nir_opt_dce(ordered_shaders[i - 1]);2365}23662367nir_remove_dead_variables(ordered_shaders[i], nir_var_shader_out, NULL);2368nir_remove_dead_variables(ordered_shaders[i - 1], nir_var_shader_in, NULL);23692370bool progress = nir_remove_unused_varyings(ordered_shaders[i], ordered_shaders[i - 1]);23712372nir_compact_varyings(ordered_shaders[i], ordered_shaders[i - 1], true);23732374if (ordered_shaders[i]->info.stage == MESA_SHADER_TESS_CTRL ||2375(ordered_shaders[i]->info.stage == MESA_SHADER_VERTEX && has_geom_tess) ||2376(ordered_shaders[i]->info.stage == MESA_SHADER_TESS_EVAL && merged_gs)) {2377nir_lower_io_to_vector(ordered_shaders[i], nir_var_shader_out);2378if (ordered_shaders[i]->info.stage == MESA_SHADER_TESS_CTRL)2379nir_vectorize_tess_levels(ordered_shaders[i]);2380nir_opt_combine_stores(ordered_shaders[i], nir_var_shader_out);2381}2382if (ordered_shaders[i - 1]->info.stage == MESA_SHADER_GEOMETRY ||2383ordered_shaders[i - 1]->info.stage == MESA_SHADER_TESS_CTRL ||2384ordered_shaders[i - 1]->info.stage == MESA_SHADER_TESS_EVAL) {2385nir_lower_io_to_vector(ordered_shaders[i - 1], nir_var_shader_in);2386}23872388if (progress) {2389if (nir_lower_global_vars_to_local(ordered_shaders[i])) {2390ac_nir_lower_indirect_derefs(ordered_shaders[i],2391pipeline->device->physical_device->rad_info.chip_class);2392/* remove dead writes, which can remove input loads */2393nir_lower_vars_to_ssa(ordered_shaders[i]);2394nir_opt_dce(ordered_shaders[i]);2395}23962397if (nir_lower_global_vars_to_local(ordered_shaders[i - 1])) {2398ac_nir_lower_indirect_derefs(ordered_shaders[i - 1],2399pipeline->device->physical_device->rad_info.chip_class);2400}2401}2402}2403}24042405static void2406radv_set_driver_locations(struct radv_pipeline *pipeline, nir_shader **shaders,2407struct radv_shader_info infos[MESA_SHADER_STAGES])2408{2409if (shaders[MESA_SHADER_FRAGMENT]) {2410nir_foreach_shader_out_variable(var, shaders[MESA_SHADER_FRAGMENT])2411{2412var->data.driver_location = var->data.location + var->data.index;2413}2414}24152416if (!shaders[MESA_SHADER_VERTEX])2417return;24182419bool has_tess = shaders[MESA_SHADER_TESS_CTRL];2420bool has_gs = shaders[MESA_SHADER_GEOMETRY];24212422/* Merged stage for VS and TES */2423unsigned vs_info_idx = MESA_SHADER_VERTEX;2424unsigned tes_info_idx = MESA_SHADER_TESS_EVAL;24252426if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {2427/* These are merged into the next stage */2428vs_info_idx = has_tess ? MESA_SHADER_TESS_CTRL : MESA_SHADER_GEOMETRY;2429tes_info_idx = has_gs ? MESA_SHADER_GEOMETRY : MESA_SHADER_TESS_EVAL;2430}24312432nir_foreach_shader_in_variable (var, shaders[MESA_SHADER_VERTEX]) {2433var->data.driver_location = var->data.location;2434}24352436if (has_tess) {2437nir_linked_io_var_info vs2tcs = nir_assign_linked_io_var_locations(2438shaders[MESA_SHADER_VERTEX], shaders[MESA_SHADER_TESS_CTRL]);2439nir_linked_io_var_info tcs2tes = nir_assign_linked_io_var_locations(2440shaders[MESA_SHADER_TESS_CTRL], shaders[MESA_SHADER_TESS_EVAL]);24412442infos[MESA_SHADER_VERTEX].vs.num_linked_outputs = vs2tcs.num_linked_io_vars;2443infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_inputs = vs2tcs.num_linked_io_vars;2444infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_outputs = tcs2tes.num_linked_io_vars;2445infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_patch_outputs = tcs2tes.num_linked_patch_io_vars;2446infos[MESA_SHADER_TESS_EVAL].tes.num_linked_inputs = tcs2tes.num_linked_io_vars;2447infos[MESA_SHADER_TESS_EVAL].tes.num_linked_patch_inputs = tcs2tes.num_linked_patch_io_vars;24482449/* Copy data to merged stage */2450infos[vs_info_idx].vs.num_linked_outputs = vs2tcs.num_linked_io_vars;2451infos[tes_info_idx].tes.num_linked_inputs = tcs2tes.num_linked_io_vars;2452infos[tes_info_idx].tes.num_linked_patch_inputs = tcs2tes.num_linked_patch_io_vars;24532454if (has_gs) {2455nir_linked_io_var_info tes2gs = nir_assign_linked_io_var_locations(2456shaders[MESA_SHADER_TESS_EVAL], shaders[MESA_SHADER_GEOMETRY]);24572458infos[MESA_SHADER_TESS_EVAL].tes.num_linked_outputs = tes2gs.num_linked_io_vars;2459infos[MESA_SHADER_GEOMETRY].gs.num_linked_inputs = tes2gs.num_linked_io_vars;24602461/* Copy data to merged stage */2462infos[tes_info_idx].tes.num_linked_outputs = tes2gs.num_linked_io_vars;2463}2464} else if (has_gs) {2465nir_linked_io_var_info vs2gs = nir_assign_linked_io_var_locations(2466shaders[MESA_SHADER_VERTEX], shaders[MESA_SHADER_GEOMETRY]);24672468infos[MESA_SHADER_VERTEX].vs.num_linked_outputs = vs2gs.num_linked_io_vars;2469infos[MESA_SHADER_GEOMETRY].gs.num_linked_inputs = vs2gs.num_linked_io_vars;24702471/* Copy data to merged stage */2472infos[vs_info_idx].vs.num_linked_outputs = vs2gs.num_linked_io_vars;2473}24742475assert(pipeline->graphics.last_vgt_api_stage != MESA_SHADER_NONE);2476nir_foreach_shader_out_variable(var, shaders[pipeline->graphics.last_vgt_api_stage])2477{2478var->data.driver_location = var->data.location;2479}2480}24812482static uint32_t2483radv_get_attrib_stride(const VkPipelineVertexInputStateCreateInfo *input_state,2484uint32_t attrib_binding)2485{2486for (uint32_t i = 0; i < input_state->vertexBindingDescriptionCount; i++) {2487const VkVertexInputBindingDescription *input_binding =2488&input_state->pVertexBindingDescriptions[i];24892490if (input_binding->binding == attrib_binding)2491return input_binding->stride;2492}24932494return 0;2495}24962497static struct radv_pipeline_key2498radv_generate_graphics_pipeline_key(const struct radv_pipeline *pipeline,2499const VkGraphicsPipelineCreateInfo *pCreateInfo,2500const struct radv_blend_state *blend)2501{2502RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);2503struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;2504const VkPipelineVertexInputStateCreateInfo *input_state = pCreateInfo->pVertexInputState;2505const VkPipelineVertexInputDivisorStateCreateInfoEXT *divisor_state =2506vk_find_struct_const(input_state->pNext, PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT);2507bool uses_dynamic_stride = false;25082509struct radv_pipeline_key key;2510memset(&key, 0, sizeof(key));25112512if (pCreateInfo->flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT)2513key.optimisations_disabled = 1;25142515key.has_multiview_view_index = !!subpass->view_mask;25162517uint32_t binding_input_rate = 0;2518uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];2519for (unsigned i = 0; i < input_state->vertexBindingDescriptionCount; ++i) {2520if (input_state->pVertexBindingDescriptions[i].inputRate) {2521unsigned binding = input_state->pVertexBindingDescriptions[i].binding;2522binding_input_rate |= 1u << binding;2523instance_rate_divisors[binding] = 1;2524}2525}2526if (divisor_state) {2527for (unsigned i = 0; i < divisor_state->vertexBindingDivisorCount; ++i) {2528instance_rate_divisors[divisor_state->pVertexBindingDivisors[i].binding] =2529divisor_state->pVertexBindingDivisors[i].divisor;2530}2531}25322533if (pCreateInfo->pDynamicState) {2534uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;2535for (uint32_t i = 0; i < count; i++) {2536if (pCreateInfo->pDynamicState->pDynamicStates[i] ==2537VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT) {2538uses_dynamic_stride = true;2539break;2540}2541}2542}25432544for (unsigned i = 0; i < input_state->vertexAttributeDescriptionCount; ++i) {2545const VkVertexInputAttributeDescription *desc = &input_state->pVertexAttributeDescriptions[i];2546const struct util_format_description *format_desc;2547unsigned location = desc->location;2548unsigned binding = desc->binding;2549unsigned num_format, data_format;2550int first_non_void;25512552if (binding_input_rate & (1u << binding)) {2553key.instance_rate_inputs |= 1u << location;2554key.instance_rate_divisors[location] = instance_rate_divisors[binding];2555}25562557format_desc = vk_format_description(desc->format);2558first_non_void = vk_format_get_first_non_void_channel(desc->format);25592560num_format = radv_translate_buffer_numformat(format_desc, first_non_void);2561data_format = radv_translate_buffer_dataformat(format_desc, first_non_void);25622563key.vertex_attribute_formats[location] = data_format | (num_format << 4);2564key.vertex_attribute_bindings[location] = desc->binding;2565key.vertex_attribute_offsets[location] = desc->offset;25662567const struct ac_data_format_info *dfmt_info = ac_get_data_format_info(data_format);2568unsigned attrib_align =2569dfmt_info->chan_byte_size ? dfmt_info->chan_byte_size : dfmt_info->element_size;25702571/* If desc->offset is misaligned, then the buffer offset must be too. Just2572* skip updating vertex_binding_align in this case.2573*/2574if (desc->offset % attrib_align == 0)2575key.vertex_binding_align[desc->binding] =2576MAX2(key.vertex_binding_align[desc->binding], attrib_align);25772578if (!uses_dynamic_stride) {2579/* From the Vulkan spec 1.2.157:2580*2581* "If the bound pipeline state object was created2582* with the2583* VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT2584* dynamic state enabled then pStrides[i] specifies2585* the distance in bytes between two consecutive2586* elements within the corresponding buffer. In this2587* case the VkVertexInputBindingDescription::stride2588* state from the pipeline state object is ignored."2589*2590* Make sure the vertex attribute stride is zero to2591* avoid computing a wrong offset if it's initialized2592* to something else than zero.2593*/2594key.vertex_attribute_strides[location] =2595radv_get_attrib_stride(input_state, desc->binding);2596}25972598enum ac_fetch_format adjust = AC_FETCH_FORMAT_NONE;2599if (pipeline->device->physical_device->rad_info.chip_class <= GFX8 &&2600pipeline->device->physical_device->rad_info.family != CHIP_STONEY) {2601VkFormat format = input_state->pVertexAttributeDescriptions[i].format;2602switch (format) {2603case VK_FORMAT_A2R10G10B10_SNORM_PACK32:2604case VK_FORMAT_A2B10G10R10_SNORM_PACK32:2605adjust = AC_FETCH_FORMAT_SNORM;2606break;2607case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:2608case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:2609adjust = AC_FETCH_FORMAT_SSCALED;2610break;2611case VK_FORMAT_A2R10G10B10_SINT_PACK32:2612case VK_FORMAT_A2B10G10R10_SINT_PACK32:2613adjust = AC_FETCH_FORMAT_SINT;2614break;2615default:2616break;2617}2618}2619key.vertex_alpha_adjust[location] = adjust;26202621switch (desc->format) {2622case VK_FORMAT_B8G8R8A8_UNORM:2623case VK_FORMAT_B8G8R8A8_SNORM:2624case VK_FORMAT_B8G8R8A8_USCALED:2625case VK_FORMAT_B8G8R8A8_SSCALED:2626case VK_FORMAT_B8G8R8A8_UINT:2627case VK_FORMAT_B8G8R8A8_SINT:2628case VK_FORMAT_B8G8R8A8_SRGB:2629case VK_FORMAT_A2R10G10B10_UNORM_PACK32:2630case VK_FORMAT_A2R10G10B10_SNORM_PACK32:2631case VK_FORMAT_A2R10G10B10_USCALED_PACK32:2632case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:2633case VK_FORMAT_A2R10G10B10_UINT_PACK32:2634case VK_FORMAT_A2R10G10B10_SINT_PACK32:2635key.vertex_post_shuffle |= 1 << location;2636break;2637default:2638break;2639}2640}26412642const VkPipelineTessellationStateCreateInfo *tess =2643radv_pipeline_get_tessellation_state(pCreateInfo);2644if (tess)2645key.tess_input_vertices = tess->patchControlPoints;26462647const VkPipelineMultisampleStateCreateInfo *vkms =2648radv_pipeline_get_multisample_state(pCreateInfo);2649if (vkms && vkms->rasterizationSamples > 1) {2650uint32_t num_samples = vkms->rasterizationSamples;2651uint32_t ps_iter_samples = radv_pipeline_get_ps_iter_samples(pCreateInfo);2652key.num_samples = num_samples;2653key.log2_ps_iter_samples = util_logbase2(ps_iter_samples);2654}26552656key.col_format = blend->spi_shader_col_format;2657if (pipeline->device->physical_device->rad_info.chip_class < GFX8) {2658key.is_int8 = blend->col_format_is_int8;2659key.is_int10 = blend->col_format_is_int10;2660}26612662if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {2663key.topology = pCreateInfo->pInputAssemblyState->topology;26642665const VkPipelineRasterizationStateCreateInfo *raster_info = pCreateInfo->pRasterizationState;2666const VkPipelineRasterizationProvokingVertexStateCreateInfoEXT *provoking_vtx_info =2667vk_find_struct_const(raster_info->pNext,2668PIPELINE_RASTERIZATION_PROVOKING_VERTEX_STATE_CREATE_INFO_EXT);2669if (provoking_vtx_info &&2670provoking_vtx_info->provokingVertexMode == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT) {2671key.provoking_vtx_last = true;2672}2673}2674return key;2675}26762677static bool2678radv_nir_stage_uses_xfb(const nir_shader *nir)2679{2680nir_xfb_info *xfb = nir_gather_xfb_info(nir, NULL);2681bool uses_xfb = !!xfb;26822683ralloc_free(xfb);2684return uses_xfb;2685}26862687static void2688radv_fill_shader_keys(struct radv_device *device, struct radv_shader_variant_key *keys,2689const struct radv_pipeline_key *key, nir_shader **nir)2690{2691keys[MESA_SHADER_VERTEX].vs.instance_rate_inputs = key->instance_rate_inputs;2692keys[MESA_SHADER_VERTEX].vs.post_shuffle = key->vertex_post_shuffle;2693for (unsigned i = 0; i < MAX_VERTEX_ATTRIBS; ++i) {2694keys[MESA_SHADER_VERTEX].vs.instance_rate_divisors[i] = key->instance_rate_divisors[i];2695keys[MESA_SHADER_VERTEX].vs.vertex_attribute_formats[i] = key->vertex_attribute_formats[i];2696keys[MESA_SHADER_VERTEX].vs.vertex_attribute_bindings[i] = key->vertex_attribute_bindings[i];2697keys[MESA_SHADER_VERTEX].vs.vertex_attribute_offsets[i] = key->vertex_attribute_offsets[i];2698keys[MESA_SHADER_VERTEX].vs.vertex_attribute_strides[i] = key->vertex_attribute_strides[i];2699keys[MESA_SHADER_VERTEX].vs.alpha_adjust[i] = key->vertex_alpha_adjust[i];2700}2701for (unsigned i = 0; i < MAX_VBS; ++i)2702keys[MESA_SHADER_VERTEX].vs.vertex_binding_align[i] = key->vertex_binding_align[i];2703keys[MESA_SHADER_VERTEX].vs.outprim = si_conv_prim_to_gs_out(key->topology);2704keys[MESA_SHADER_VERTEX].vs.provoking_vtx_last = key->provoking_vtx_last;27052706if (nir[MESA_SHADER_TESS_CTRL]) {2707keys[MESA_SHADER_VERTEX].vs_common_out.as_ls = true;2708keys[MESA_SHADER_TESS_CTRL].tcs.input_vertices = key->tess_input_vertices;2709keys[MESA_SHADER_TESS_CTRL].tcs.primitive_mode =2710nir[MESA_SHADER_TESS_EVAL]->info.tess.primitive_mode;2711}27122713if (nir[MESA_SHADER_GEOMETRY]) {2714if (nir[MESA_SHADER_TESS_CTRL])2715keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_es = true;2716else2717keys[MESA_SHADER_VERTEX].vs_common_out.as_es = true;2718}27192720if (device->physical_device->use_ngg) {2721if (nir[MESA_SHADER_TESS_CTRL]) {2722keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = true;2723} else {2724keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = true;2725}27262727if (nir[MESA_SHADER_TESS_CTRL] && nir[MESA_SHADER_GEOMETRY] &&2728nir[MESA_SHADER_GEOMETRY]->info.gs.invocations *2729nir[MESA_SHADER_GEOMETRY]->info.gs.vertices_out >2730256) {2731/* Fallback to the legacy path if tessellation is2732* enabled with extreme geometry because2733* EN_MAX_VERT_OUT_PER_GS_INSTANCE doesn't work and it2734* might hang.2735*/2736keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;2737}27382739gl_shader_stage last_xfb_stage = MESA_SHADER_VERTEX;27402741for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {2742if (nir[i])2743last_xfb_stage = i;2744}27452746bool uses_xfb = nir[last_xfb_stage] && radv_nir_stage_uses_xfb(nir[last_xfb_stage]);27472748if (!device->physical_device->use_ngg_streamout && uses_xfb) {2749if (nir[MESA_SHADER_TESS_CTRL])2750keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg = false;2751else2752keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg = false;2753}27542755/* Determine if the pipeline is eligible for the NGG passthrough2756* mode. It can't be enabled for geometry shaders, for NGG2757* streamout or for vertex shaders that export the primitive ID2758* (this is checked later because we don't have the info here.)2759*/2760if (!nir[MESA_SHADER_GEOMETRY] && !uses_xfb) {2761if (nir[MESA_SHADER_TESS_CTRL] && keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg) {2762keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg_passthrough = true;2763} else if (nir[MESA_SHADER_VERTEX] && keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) {2764keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = true;2765}2766}2767}27682769for (int i = 0; i < MESA_SHADER_STAGES; ++i)2770keys[i].has_multiview_view_index = key->has_multiview_view_index;27712772keys[MESA_SHADER_FRAGMENT].fs.col_format = key->col_format;2773keys[MESA_SHADER_FRAGMENT].fs.is_int8 = key->is_int8;2774keys[MESA_SHADER_FRAGMENT].fs.is_int10 = key->is_int10;2775keys[MESA_SHADER_FRAGMENT].fs.log2_ps_iter_samples = key->log2_ps_iter_samples;2776keys[MESA_SHADER_FRAGMENT].fs.num_samples = key->num_samples;27772778if (nir[MESA_SHADER_COMPUTE]) {2779unsigned subgroup_size = key->compute_subgroup_size;2780unsigned req_subgroup_size = subgroup_size;2781bool require_full_subgroups = key->require_full_subgroups;27822783if (!subgroup_size)2784subgroup_size = device->physical_device->cs_wave_size;27852786unsigned local_size = nir[MESA_SHADER_COMPUTE]->info.workgroup_size[0] *2787nir[MESA_SHADER_COMPUTE]->info.workgroup_size[1] *2788nir[MESA_SHADER_COMPUTE]->info.workgroup_size[2];27892790/* Games don't always request full subgroups when they should,2791* which can cause bugs if cswave32 is enabled.2792*/2793if (device->physical_device->cs_wave_size == 32 &&2794nir[MESA_SHADER_COMPUTE]->info.cs.uses_wide_subgroup_intrinsics && !req_subgroup_size &&2795local_size % RADV_SUBGROUP_SIZE == 0)2796require_full_subgroups = true;27972798if (require_full_subgroups && !req_subgroup_size) {2799/* don't use wave32 pretending to be wave64 */2800subgroup_size = RADV_SUBGROUP_SIZE;2801}28022803keys[MESA_SHADER_COMPUTE].cs.subgroup_size = subgroup_size;2804}2805}28062807static uint8_t2808radv_get_wave_size(struct radv_device *device, const VkPipelineShaderStageCreateInfo *pStage,2809gl_shader_stage stage, const struct radv_shader_variant_key *key,2810const struct radv_shader_info *info)2811{2812if (stage == MESA_SHADER_GEOMETRY && !info->is_ngg)2813return 64;2814else if (stage == MESA_SHADER_COMPUTE) {2815return key->cs.subgroup_size;2816} else if (stage == MESA_SHADER_FRAGMENT)2817return device->physical_device->ps_wave_size;2818else2819return device->physical_device->ge_wave_size;2820}28212822static uint8_t2823radv_get_ballot_bit_size(struct radv_device *device, const VkPipelineShaderStageCreateInfo *pStage,2824gl_shader_stage stage, const struct radv_shader_variant_key *key)2825{2826if (stage == MESA_SHADER_COMPUTE && key->cs.subgroup_size)2827return key->cs.subgroup_size;2828return 64;2829}28302831static void2832radv_fill_shader_info(struct radv_pipeline *pipeline,2833const VkPipelineShaderStageCreateInfo **pStages,2834struct radv_shader_variant_key *keys, struct radv_shader_info *infos,2835nir_shader **nir)2836{2837unsigned active_stages = 0;2838unsigned filled_stages = 0;28392840for (int i = 0; i < MESA_SHADER_STAGES; i++) {2841if (nir[i])2842active_stages |= (1 << i);2843}28442845if (nir[MESA_SHADER_FRAGMENT]) {2846radv_nir_shader_info_init(&infos[MESA_SHADER_FRAGMENT]);2847radv_nir_shader_info_pass(pipeline->device, nir[MESA_SHADER_FRAGMENT], pipeline->layout,2848&keys[MESA_SHADER_FRAGMENT], &infos[MESA_SHADER_FRAGMENT]);28492850/* TODO: These are no longer used as keys we should refactor this */2851keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id =2852infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;2853keys[MESA_SHADER_VERTEX].vs_common_out.export_layer_id =2854infos[MESA_SHADER_FRAGMENT].ps.layer_input;2855keys[MESA_SHADER_VERTEX].vs_common_out.export_clip_dists =2856!!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;2857keys[MESA_SHADER_VERTEX].vs_common_out.export_viewport_index =2858infos[MESA_SHADER_FRAGMENT].ps.viewport_index_input;2859keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_prim_id =2860infos[MESA_SHADER_FRAGMENT].ps.prim_id_input;2861keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_layer_id =2862infos[MESA_SHADER_FRAGMENT].ps.layer_input;2863keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_clip_dists =2864!!infos[MESA_SHADER_FRAGMENT].ps.num_input_clips_culls;2865keys[MESA_SHADER_TESS_EVAL].vs_common_out.export_viewport_index =2866infos[MESA_SHADER_FRAGMENT].ps.viewport_index_input;28672868/* NGG passthrough mode can't be enabled for vertex shaders2869* that export the primitive ID.2870*2871* TODO: I should really refactor the keys logic.2872*/2873if (nir[MESA_SHADER_VERTEX] && keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id) {2874keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg_passthrough = false;2875}28762877filled_stages |= (1 << MESA_SHADER_FRAGMENT);2878}28792880if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&2881nir[MESA_SHADER_TESS_CTRL]) {2882struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};2883struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];2884key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;28852886radv_nir_shader_info_init(&infos[MESA_SHADER_TESS_CTRL]);28872888for (int i = 0; i < 2; i++) {2889radv_nir_shader_info_pass(pipeline->device, combined_nir[i], pipeline->layout, &key,2890&infos[MESA_SHADER_TESS_CTRL]);2891}28922893filled_stages |= (1 << MESA_SHADER_VERTEX);2894filled_stages |= (1 << MESA_SHADER_TESS_CTRL);2895}28962897if (pipeline->device->physical_device->rad_info.chip_class >= GFX9 &&2898nir[MESA_SHADER_GEOMETRY]) {2899gl_shader_stage pre_stage =2900nir[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;2901struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};29022903radv_nir_shader_info_init(&infos[MESA_SHADER_GEOMETRY]);29042905for (int i = 0; i < 2; i++) {2906radv_nir_shader_info_pass(pipeline->device, combined_nir[i], pipeline->layout,2907&keys[pre_stage], &infos[MESA_SHADER_GEOMETRY]);2908}29092910filled_stages |= (1 << pre_stage);2911filled_stages |= (1 << MESA_SHADER_GEOMETRY);2912}29132914active_stages ^= filled_stages;2915while (active_stages) {2916int i = u_bit_scan(&active_stages);2917radv_nir_shader_info_init(&infos[i]);2918radv_nir_shader_info_pass(pipeline->device, nir[i], pipeline->layout, &keys[i], &infos[i]);2919}29202921for (int i = 0; i < MESA_SHADER_STAGES; i++) {2922if (nir[i]) {2923infos[i].wave_size = radv_get_wave_size(pipeline->device, pStages[i], i, &keys[i], &infos[i]);2924infos[i].ballot_bit_size =2925radv_get_ballot_bit_size(pipeline->device, pStages[i], i, &keys[i]);2926}2927}2928}29292930static void2931merge_tess_info(struct shader_info *tes_info, struct shader_info *tcs_info)2932{2933/* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:2934*2935* "PointMode. Controls generation of points rather than triangles2936* or lines. This functionality defaults to disabled, and is2937* enabled if either shader stage includes the execution mode.2938*2939* and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,2940* PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,2941* and OutputVertices, it says:2942*2943* "One mode must be set in at least one of the tessellation2944* shader stages."2945*2946* So, the fields can be set in either the TCS or TES, but they must2947* agree if set in both. Our backend looks at TES, so bitwise-or in2948* the values from the TCS.2949*/2950assert(tcs_info->tess.tcs_vertices_out == 0 || tes_info->tess.tcs_vertices_out == 0 ||2951tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);2952tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;29532954assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||2955tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||2956tcs_info->tess.spacing == tes_info->tess.spacing);2957tes_info->tess.spacing |= tcs_info->tess.spacing;29582959assert(tcs_info->tess.primitive_mode == 0 || tes_info->tess.primitive_mode == 0 ||2960tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);2961tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;2962tes_info->tess.ccw |= tcs_info->tess.ccw;2963tes_info->tess.point_mode |= tcs_info->tess.point_mode;29642965/* Copy the merged info back to the TCS */2966tcs_info->tess.tcs_vertices_out = tes_info->tess.tcs_vertices_out;2967tcs_info->tess.spacing = tes_info->tess.spacing;2968tcs_info->tess.primitive_mode = tes_info->tess.primitive_mode;2969tcs_info->tess.ccw = tes_info->tess.ccw;2970tcs_info->tess.point_mode = tes_info->tess.point_mode;2971}29722973static void2974gather_tess_info(struct radv_device *device, nir_shader **nir, struct radv_shader_info *infos,2975const struct radv_pipeline_key *pipeline_key)2976{2977merge_tess_info(&nir[MESA_SHADER_TESS_EVAL]->info, &nir[MESA_SHADER_TESS_CTRL]->info);29782979/* Number of tessellation patches per workgroup processed by the current pipeline. */2980unsigned num_patches = get_tcs_num_patches(2981pipeline_key->tess_input_vertices, nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out,2982infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_inputs,2983infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_outputs,2984infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_patch_outputs, device->tess_offchip_block_dw_size,2985device->physical_device->rad_info.chip_class, device->physical_device->rad_info.family);29862987/* LDS size used by VS+TCS for storing TCS inputs and outputs. */2988unsigned tcs_lds_size = calculate_tess_lds_size(2989device->physical_device->rad_info.chip_class, pipeline_key->tess_input_vertices,2990nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out,2991infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_inputs, num_patches,2992infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_outputs,2993infos[MESA_SHADER_TESS_CTRL].tcs.num_linked_patch_outputs);29942995infos[MESA_SHADER_TESS_CTRL].num_tess_patches = num_patches;2996infos[MESA_SHADER_TESS_CTRL].tcs.num_lds_blocks = tcs_lds_size;2997infos[MESA_SHADER_TESS_CTRL].tcs.tes_reads_tess_factors =2998!!(nir[MESA_SHADER_TESS_EVAL]->info.inputs_read &2999(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER));3000infos[MESA_SHADER_TESS_CTRL].tcs.tes_inputs_read = nir[MESA_SHADER_TESS_EVAL]->info.inputs_read;3001infos[MESA_SHADER_TESS_CTRL].tcs.tes_patch_inputs_read =3002nir[MESA_SHADER_TESS_EVAL]->info.patch_inputs_read;30033004infos[MESA_SHADER_TESS_EVAL].num_tess_patches = num_patches;3005infos[MESA_SHADER_GEOMETRY].num_tess_patches = num_patches;30063007if (!radv_use_llvm_for_stage(device, MESA_SHADER_VERTEX)) {3008/* When the number of TCS input and output vertices are the same (typically 3):3009* - There is an equal amount of LS and HS invocations3010* - In case of merged LSHS shaders, the LS and HS halves of the shader3011* always process the exact same vertex. We can use this knowledge to optimize them.3012*3013* We don't set tcs_in_out_eq if the float controls differ because that might3014* involve different float modes for the same block and our optimizer3015* doesn't handle a instruction dominating another with a different mode.3016*/3017infos[MESA_SHADER_VERTEX].vs.tcs_in_out_eq =3018device->physical_device->rad_info.chip_class >= GFX9 &&3019pipeline_key->tess_input_vertices ==3020nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out &&3021nir[MESA_SHADER_VERTEX]->info.float_controls_execution_mode ==3022nir[MESA_SHADER_TESS_CTRL]->info.float_controls_execution_mode;30233024if (infos[MESA_SHADER_VERTEX].vs.tcs_in_out_eq)3025infos[MESA_SHADER_VERTEX].vs.tcs_temp_only_input_mask =3026nir[MESA_SHADER_TESS_CTRL]->info.inputs_read &3027nir[MESA_SHADER_VERTEX]->info.outputs_written &3028~nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_cross_invocation_inputs_read &3029~nir[MESA_SHADER_TESS_CTRL]->info.inputs_read_indirectly &3030~nir[MESA_SHADER_VERTEX]->info.outputs_accessed_indirectly;30313032/* Copy data to TCS so it can be accessed by the backend if they are merged. */3033infos[MESA_SHADER_TESS_CTRL].vs.tcs_in_out_eq = infos[MESA_SHADER_VERTEX].vs.tcs_in_out_eq;3034infos[MESA_SHADER_TESS_CTRL].vs.tcs_temp_only_input_mask =3035infos[MESA_SHADER_VERTEX].vs.tcs_temp_only_input_mask;3036}3037}30383039static void3040radv_init_feedback(const VkPipelineCreationFeedbackCreateInfoEXT *ext)3041{3042if (!ext)3043return;30443045if (ext->pPipelineCreationFeedback) {3046ext->pPipelineCreationFeedback->flags = 0;3047ext->pPipelineCreationFeedback->duration = 0;3048}30493050for (unsigned i = 0; i < ext->pipelineStageCreationFeedbackCount; ++i) {3051ext->pPipelineStageCreationFeedbacks[i].flags = 0;3052ext->pPipelineStageCreationFeedbacks[i].duration = 0;3053}3054}30553056static void3057radv_start_feedback(VkPipelineCreationFeedbackEXT *feedback)3058{3059if (!feedback)3060return;30613062feedback->duration -= radv_get_current_time();3063feedback->flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT;3064}30653066static void3067radv_stop_feedback(VkPipelineCreationFeedbackEXT *feedback, bool cache_hit)3068{3069if (!feedback)3070return;30713072feedback->duration += radv_get_current_time();3073feedback->flags =3074VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT |3075(cache_hit ? VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT : 0);3076}30773078static bool3079mem_vectorize_callback(unsigned align_mul, unsigned align_offset, unsigned bit_size,3080unsigned num_components, nir_intrinsic_instr *low, nir_intrinsic_instr *high,3081void *data)3082{3083if (num_components > 4)3084return false;30853086/* >128 bit loads are split except with SMEM */3087if (bit_size * num_components > 128)3088return false;30893090uint32_t align;3091if (align_offset)3092align = 1 << (ffs(align_offset) - 1);3093else3094align = align_mul;30953096switch (low->intrinsic) {3097case nir_intrinsic_load_global:3098case nir_intrinsic_store_global:3099case nir_intrinsic_store_ssbo:3100case nir_intrinsic_load_ssbo:3101case nir_intrinsic_load_ubo:3102case nir_intrinsic_load_push_constant: {3103unsigned max_components;3104if (align % 4 == 0)3105max_components = NIR_MAX_VEC_COMPONENTS;3106else if (align % 2 == 0)3107max_components = 16u / bit_size;3108else3109max_components = 8u / bit_size;3110return (align % (bit_size / 8u)) == 0 && num_components <= max_components;3111}3112case nir_intrinsic_load_deref:3113case nir_intrinsic_store_deref:3114assert(nir_deref_mode_is(nir_src_as_deref(low->src[0]), nir_var_mem_shared));3115FALLTHROUGH;3116case nir_intrinsic_load_shared:3117case nir_intrinsic_store_shared:3118if (bit_size * num_components ==311996) { /* 96 bit loads require 128 bit alignment and are split otherwise */3120return align % 16 == 0;3121} else if (bit_size == 16 && (align % 4)) {3122/* AMD hardware can't do 2-byte aligned f16vec2 loads, but they are useful for ALU3123* vectorization, because our vectorizer requires the scalar IR to already contain vectors.3124*/3125return (align % 2 == 0) && num_components <= 2;3126} else {3127if (num_components == 3) {3128/* AMD hardware can't do 3-component loads except for 96-bit loads, handled above. */3129return false;3130}3131unsigned req = bit_size * num_components;3132if (req == 64 || req == 128) /* 64-bit and 128-bit loads can use ds_read2_b{32,64} */3133req /= 2u;3134return align % (req / 8u) == 0;3135}3136default:3137return false;3138}3139return false;3140}31413142static unsigned3143lower_bit_size_callback(const nir_instr *instr, void *_)3144{3145struct radv_device *device = _;3146enum chip_class chip = device->physical_device->rad_info.chip_class;31473148if (instr->type != nir_instr_type_alu)3149return 0;3150nir_alu_instr *alu = nir_instr_as_alu(instr);31513152if (alu->dest.dest.ssa.bit_size & (8 | 16)) {3153unsigned bit_size = alu->dest.dest.ssa.bit_size;3154switch (alu->op) {3155case nir_op_iabs:3156case nir_op_bitfield_select:3157case nir_op_imul_high:3158case nir_op_umul_high:3159case nir_op_ineg:3160case nir_op_isign:3161return 32;3162case nir_op_imax:3163case nir_op_umax:3164case nir_op_imin:3165case nir_op_umin:3166case nir_op_ishr:3167case nir_op_ushr:3168case nir_op_ishl:3169case nir_op_uadd_sat:3170return (bit_size == 8 || !(chip >= GFX8 && nir_dest_is_divergent(alu->dest.dest))) ? 323171: 0;3172default:3173return 0;3174}3175}31763177if (nir_src_bit_size(alu->src[0].src) & (8 | 16)) {3178unsigned bit_size = nir_src_bit_size(alu->src[0].src);3179switch (alu->op) {3180case nir_op_bit_count:3181case nir_op_find_lsb:3182case nir_op_ufind_msb:3183case nir_op_i2b1:3184return 32;3185case nir_op_ilt:3186case nir_op_ige:3187case nir_op_ieq:3188case nir_op_ine:3189case nir_op_ult:3190case nir_op_uge:3191return (bit_size == 8 || !(chip >= GFX8 && nir_dest_is_divergent(alu->dest.dest))) ? 323192: 0;3193default:3194return 0;3195}3196}31973198return 0;3199}32003201static bool3202opt_vectorize_callback(const nir_instr *instr, void *_)3203{3204assert(instr->type == nir_instr_type_alu);3205nir_alu_instr *alu = nir_instr_as_alu(instr);3206unsigned bit_size = alu->dest.dest.ssa.bit_size;3207if (bit_size != 16)3208return false;32093210switch (alu->op) {3211case nir_op_fadd:3212case nir_op_fsub:3213case nir_op_fmul:3214case nir_op_fneg:3215case nir_op_fsat:3216case nir_op_fmin:3217case nir_op_fmax:3218case nir_op_iadd:3219case nir_op_isub:3220case nir_op_imul:3221case nir_op_imin:3222case nir_op_imax:3223case nir_op_umin:3224case nir_op_umax:3225return true;3226case nir_op_ishl: /* TODO: in NIR, these have 32bit shift operands */3227case nir_op_ishr: /* while Radeon needs 16bit operands when vectorized */3228case nir_op_ushr:3229default:3230return false;3231}3232}32333234static nir_component_mask_t3235non_uniform_access_callback(const nir_src *src, void *_)3236{3237if (src->ssa->num_components == 1)3238return 0x1;3239return nir_chase_binding(*src).success ? 0x2 : 0x3;3240}32413242VkResult3243radv_create_shaders(struct radv_pipeline *pipeline, struct radv_device *device,3244struct radv_pipeline_cache *cache, const struct radv_pipeline_key *pipeline_key,3245const VkPipelineShaderStageCreateInfo **pStages,3246const VkPipelineCreateFlags flags,3247VkPipelineCreationFeedbackEXT *pipeline_feedback,3248VkPipelineCreationFeedbackEXT **stage_feedbacks)3249{3250struct vk_shader_module fs_m = {0};3251struct vk_shader_module *modules[MESA_SHADER_STAGES] = {32520,3253};3254nir_shader *nir[MESA_SHADER_STAGES] = {0};3255struct radv_shader_binary *binaries[MESA_SHADER_STAGES] = {NULL};3256struct radv_shader_variant_key keys[MESA_SHADER_STAGES] = {{{{{0}}}}};3257struct radv_shader_info infos[MESA_SHADER_STAGES] = {0};3258unsigned char hash[20], gs_copy_hash[20];3259bool keep_executable_info =3260(flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR) ||3261device->keep_shader_info;3262bool keep_statistic_info = (flags & VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR) ||3263(device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) ||3264device->keep_shader_info;3265bool disable_optimizations = flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;32663267radv_start_feedback(pipeline_feedback);32683269for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {3270if (pStages[i]) {3271modules[i] = vk_shader_module_from_handle(pStages[i]->module);3272if (modules[i]->nir)3273_mesa_sha1_compute(modules[i]->nir->info.name, strlen(modules[i]->nir->info.name),3274modules[i]->sha1);32753276pipeline->active_stages |= mesa_to_vk_shader_stage(i);3277if (i < MESA_SHADER_FRAGMENT)3278pipeline->graphics.last_vgt_api_stage = i;3279}3280}32813282radv_hash_shaders(hash, pStages, pipeline->layout, pipeline_key,3283get_hash_flags(device, keep_statistic_info));3284memcpy(gs_copy_hash, hash, 20);3285gs_copy_hash[0] ^= 1;32863287pipeline->pipeline_hash = *(uint64_t *)hash;32883289bool found_in_application_cache = true;3290if (modules[MESA_SHADER_GEOMETRY] && !keep_executable_info) {3291struct radv_shader_variant *variants[MESA_SHADER_STAGES] = {0};3292radv_create_shader_variants_from_pipeline_cache(device, cache, gs_copy_hash, variants,3293&found_in_application_cache);3294pipeline->gs_copy_shader = variants[MESA_SHADER_GEOMETRY];3295}32963297if (!keep_executable_info &&3298radv_create_shader_variants_from_pipeline_cache(device, cache, hash, pipeline->shaders,3299&found_in_application_cache) &&3300(!modules[MESA_SHADER_GEOMETRY] || pipeline->gs_copy_shader)) {3301radv_stop_feedback(pipeline_feedback, found_in_application_cache);3302return VK_SUCCESS;3303}33043305if (flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT) {3306radv_stop_feedback(pipeline_feedback, found_in_application_cache);3307return VK_PIPELINE_COMPILE_REQUIRED_EXT;3308}33093310if (!modules[MESA_SHADER_FRAGMENT] && !modules[MESA_SHADER_COMPUTE]) {3311nir_builder fs_b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, NULL, "noop_fs");3312fs_m = vk_shader_module_from_nir(fs_b.shader);3313modules[MESA_SHADER_FRAGMENT] = &fs_m;3314}33153316for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {3317const VkPipelineShaderStageCreateInfo *stage = pStages[i];33183319if (!modules[i])3320continue;33213322radv_start_feedback(stage_feedbacks[i]);33233324nir[i] = radv_shader_compile_to_nir(device, modules[i], stage ? stage->pName : "main", i,3325stage ? stage->pSpecializationInfo : NULL, flags,3326pipeline->layout, pipeline_key);33273328/* We don't want to alter meta shaders IR directly so clone it3329* first.3330*/3331if (nir[i]->info.name) {3332nir[i] = nir_shader_clone(NULL, nir[i]);3333}33343335radv_stop_feedback(stage_feedbacks[i], false);3336}33373338bool optimize_conservatively = flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;33393340radv_link_shaders(pipeline, nir, optimize_conservatively);3341radv_set_driver_locations(pipeline, nir, infos);33423343for (int i = 0; i < MESA_SHADER_STAGES; ++i) {3344if (nir[i]) {3345radv_start_feedback(stage_feedbacks[i]);3346radv_optimize_nir(device, nir[i], optimize_conservatively, false);33473348/* Gather info again, information such as outputs_read can be out-of-date. */3349nir_shader_gather_info(nir[i], nir_shader_get_entrypoint(nir[i]));3350radv_lower_io(device, nir[i]);33513352radv_stop_feedback(stage_feedbacks[i], false);3353}3354}33553356infos[MESA_SHADER_VERTEX].vs.as_ls = !!nir[MESA_SHADER_TESS_CTRL];3357infos[MESA_SHADER_VERTEX].vs.as_es = !!nir[MESA_SHADER_GEOMETRY] && !nir[MESA_SHADER_TESS_CTRL];3358infos[MESA_SHADER_TESS_EVAL].tes.as_es =3359!!nir[MESA_SHADER_GEOMETRY] && !!nir[MESA_SHADER_TESS_CTRL];33603361if (nir[MESA_SHADER_TESS_CTRL]) {3362nir_lower_patch_vertices(nir[MESA_SHADER_TESS_EVAL],3363nir[MESA_SHADER_TESS_CTRL]->info.tess.tcs_vertices_out, NULL);3364gather_tess_info(device, nir, infos, pipeline_key);3365}33663367radv_fill_shader_keys(device, keys, pipeline_key, nir);3368radv_fill_shader_info(pipeline, pStages, keys, infos, nir);33693370bool pipeline_has_ngg = (nir[MESA_SHADER_VERTEX] && keys[MESA_SHADER_VERTEX].vs_common_out.as_ngg) ||3371(nir[MESA_SHADER_TESS_EVAL] && keys[MESA_SHADER_TESS_EVAL].vs_common_out.as_ngg);33723373if (pipeline_has_ngg) {3374struct gfx10_ngg_info *ngg_info;33753376if (nir[MESA_SHADER_GEOMETRY])3377ngg_info = &infos[MESA_SHADER_GEOMETRY].ngg_info;3378else if (nir[MESA_SHADER_TESS_CTRL])3379ngg_info = &infos[MESA_SHADER_TESS_EVAL].ngg_info;3380else3381ngg_info = &infos[MESA_SHADER_VERTEX].ngg_info;33823383gfx10_get_ngg_info(pipeline_key, pipeline, nir, infos, ngg_info);3384} else if (nir[MESA_SHADER_GEOMETRY]) {3385struct gfx9_gs_info *gs_info = &infos[MESA_SHADER_GEOMETRY].gs_ring_info;33863387gfx9_get_gs_info(pipeline_key, pipeline, nir, infos, gs_info);3388}33893390for (int i = 0; i < MESA_SHADER_STAGES; ++i) {3391if (nir[i]) {3392radv_start_feedback(stage_feedbacks[i]);33933394if (!radv_use_llvm_for_stage(device, i)) {3395nir_lower_non_uniform_access_options options = {3396.types = nir_lower_non_uniform_ubo_access | nir_lower_non_uniform_ssbo_access |3397nir_lower_non_uniform_texture_access | nir_lower_non_uniform_image_access,3398.callback = &non_uniform_access_callback,3399.callback_data = NULL,3400};3401NIR_PASS_V(nir[i], nir_lower_non_uniform_access, &options);3402}3403NIR_PASS_V(nir[i], nir_lower_memory_model);34043405bool lower_to_scalar = false;34063407nir_load_store_vectorize_options vectorize_opts = {3408.modes = nir_var_mem_ssbo | nir_var_mem_ubo | nir_var_mem_push_const |3409nir_var_mem_shared | nir_var_mem_global,3410.callback = mem_vectorize_callback,3411.robust_modes = 0,3412};34133414if (device->robust_buffer_access2) {3415vectorize_opts.robust_modes =3416nir_var_mem_ubo | nir_var_mem_ssbo | nir_var_mem_global | nir_var_mem_push_const;3417}34183419if (nir_opt_load_store_vectorize(nir[i], &vectorize_opts)) {3420NIR_PASS_V(nir[i], nir_copy_prop);3421lower_to_scalar = true;34223423/* Gather info again, to update whether 8/16-bit are used. */3424nir_shader_gather_info(nir[i], nir_shader_get_entrypoint(nir[i]));3425}34263427lower_to_scalar |=3428nir_opt_shrink_vectors(nir[i], !device->instance->disable_shrink_image_store);34293430if (lower_to_scalar)3431nir_lower_alu_to_scalar(nir[i], NULL, NULL);34323433/* lower ALU operations */3434/* TODO: Some 64-bit tests crash inside LLVM. */3435if (!radv_use_llvm_for_stage(device, i))3436nir_lower_int64(nir[i]);34373438/* TODO: Implement nir_op_uadd_sat with LLVM. */3439if (!radv_use_llvm_for_stage(device, i))3440nir_opt_idiv_const(nir[i], 8);34413442nir_lower_idiv(nir[i],3443&(nir_lower_idiv_options){3444.imprecise_32bit_lowering = false,3445.allow_fp16 = device->physical_device->rad_info.chip_class >= GFX9,3446});34473448nir_opt_sink(nir[i], nir_move_load_input | nir_move_const_undef | nir_move_copies);3449nir_opt_move(nir[i], nir_move_load_input | nir_move_const_undef | nir_move_copies);34503451/* Lower I/O intrinsics to memory instructions. */3452bool io_to_mem = radv_lower_io_to_mem(device, nir[i], &infos[i], pipeline_key);3453bool lowered_ngg = pipeline_has_ngg && i == pipeline->graphics.last_vgt_api_stage &&3454!radv_use_llvm_for_stage(device, i);3455if (lowered_ngg) {3456uint64_t ps_inputs_read = nir[MESA_SHADER_FRAGMENT] ? nir[MESA_SHADER_FRAGMENT]->info.inputs_read : 0;3457bool consider_culling = radv_consider_culling(device, nir[i], ps_inputs_read);3458radv_lower_ngg(device, nir[i], &infos[i], pipeline_key, &keys[i], consider_culling);3459}34603461radv_optimize_nir_algebraic(nir[i], io_to_mem || lowered_ngg || i == MESA_SHADER_COMPUTE);34623463if (nir[i]->info.bit_sizes_int & (8 | 16)) {3464if (device->physical_device->rad_info.chip_class >= GFX8) {3465nir_convert_to_lcssa(nir[i], true, true);3466nir_divergence_analysis(nir[i]);3467}34683469if (nir_lower_bit_size(nir[i], lower_bit_size_callback, device)) {3470NIR_PASS_V(nir[i], nir_opt_constant_folding);3471NIR_PASS_V(nir[i], nir_opt_dce);3472}34733474if (device->physical_device->rad_info.chip_class >= GFX8)3475nir_opt_remove_phis(nir[i]); /* cleanup LCSSA phis */3476}3477if (((nir[i]->info.bit_sizes_int | nir[i]->info.bit_sizes_float) & 16) &&3478device->physical_device->rad_info.chip_class >= GFX9)3479NIR_PASS_V(nir[i], nir_opt_vectorize, opt_vectorize_callback, NULL);34803481/* cleanup passes */3482nir_lower_load_const_to_scalar(nir[i]);3483nir_move_options move_opts = nir_move_const_undef | nir_move_load_ubo |3484nir_move_load_input | nir_move_comparisons | nir_move_copies;3485nir_opt_sink(nir[i], move_opts | nir_move_load_ssbo);3486nir_opt_move(nir[i], move_opts);34873488radv_stop_feedback(stage_feedbacks[i], false);3489}3490}34913492for (int i = 0; i < MESA_SHADER_STAGES; ++i) {3493if (radv_can_dump_shader(device, modules[i], false))3494nir_print_shader(nir[i], stderr);3495}34963497if (modules[MESA_SHADER_GEOMETRY]) {3498struct radv_shader_binary *gs_copy_binary = NULL;3499if (!pipeline_has_ngg) {3500struct radv_shader_info info = {0};3501struct radv_shader_variant_key key = {0};35023503key.has_multiview_view_index = keys[MESA_SHADER_GEOMETRY].has_multiview_view_index;35043505radv_nir_shader_info_pass(device, nir[MESA_SHADER_GEOMETRY], pipeline->layout, &key,3506&info);3507info.wave_size = 64; /* Wave32 not supported. */3508info.ballot_bit_size = 64;35093510pipeline->gs_copy_shader = radv_create_gs_copy_shader(3511device, nir[MESA_SHADER_GEOMETRY], &info, &gs_copy_binary, keep_executable_info,3512keep_statistic_info, keys[MESA_SHADER_GEOMETRY].has_multiview_view_index,3513disable_optimizations);3514}35153516if (!keep_executable_info && pipeline->gs_copy_shader) {3517struct radv_shader_binary *gs_binaries[MESA_SHADER_STAGES] = {NULL};3518struct radv_shader_variant *gs_variants[MESA_SHADER_STAGES] = {0};35193520gs_binaries[MESA_SHADER_GEOMETRY] = gs_copy_binary;3521gs_variants[MESA_SHADER_GEOMETRY] = pipeline->gs_copy_shader;35223523radv_pipeline_cache_insert_shaders(device, cache, gs_copy_hash, gs_variants, gs_binaries);35243525pipeline->gs_copy_shader = gs_variants[MESA_SHADER_GEOMETRY];3526}3527free(gs_copy_binary);3528}35293530if (nir[MESA_SHADER_FRAGMENT]) {3531if (!pipeline->shaders[MESA_SHADER_FRAGMENT]) {3532radv_start_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT]);35333534pipeline->shaders[MESA_SHADER_FRAGMENT] = radv_shader_variant_compile(3535device, modules[MESA_SHADER_FRAGMENT], &nir[MESA_SHADER_FRAGMENT], 1, pipeline->layout,3536keys + MESA_SHADER_FRAGMENT, infos + MESA_SHADER_FRAGMENT, keep_executable_info,3537keep_statistic_info, disable_optimizations, &binaries[MESA_SHADER_FRAGMENT]);35383539radv_stop_feedback(stage_feedbacks[MESA_SHADER_FRAGMENT], false);3540}3541}35423543if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_TESS_CTRL]) {3544if (!pipeline->shaders[MESA_SHADER_TESS_CTRL]) {3545struct nir_shader *combined_nir[] = {nir[MESA_SHADER_VERTEX], nir[MESA_SHADER_TESS_CTRL]};3546struct radv_shader_variant_key key = keys[MESA_SHADER_TESS_CTRL];3547key.tcs.vs_key = keys[MESA_SHADER_VERTEX].vs;35483549radv_start_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL]);35503551pipeline->shaders[MESA_SHADER_TESS_CTRL] = radv_shader_variant_compile(3552device, modules[MESA_SHADER_TESS_CTRL], combined_nir, 2, pipeline->layout, &key,3553&infos[MESA_SHADER_TESS_CTRL], keep_executable_info, keep_statistic_info,3554disable_optimizations, &binaries[MESA_SHADER_TESS_CTRL]);35553556radv_stop_feedback(stage_feedbacks[MESA_SHADER_TESS_CTRL], false);3557}3558modules[MESA_SHADER_VERTEX] = NULL;3559}35603561if (device->physical_device->rad_info.chip_class >= GFX9 && modules[MESA_SHADER_GEOMETRY]) {3562gl_shader_stage pre_stage =3563modules[MESA_SHADER_TESS_EVAL] ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;3564if (!pipeline->shaders[MESA_SHADER_GEOMETRY]) {3565struct nir_shader *combined_nir[] = {nir[pre_stage], nir[MESA_SHADER_GEOMETRY]};35663567radv_start_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY]);35683569pipeline->shaders[MESA_SHADER_GEOMETRY] = radv_shader_variant_compile(3570device, modules[MESA_SHADER_GEOMETRY], combined_nir, 2, pipeline->layout,3571&keys[pre_stage], &infos[MESA_SHADER_GEOMETRY], keep_executable_info,3572keep_statistic_info, disable_optimizations, &binaries[MESA_SHADER_GEOMETRY]);35733574radv_stop_feedback(stage_feedbacks[MESA_SHADER_GEOMETRY], false);3575}3576modules[pre_stage] = NULL;3577}35783579for (int i = 0; i < MESA_SHADER_STAGES; ++i) {3580if (modules[i] && !pipeline->shaders[i]) {3581radv_start_feedback(stage_feedbacks[i]);35823583pipeline->shaders[i] = radv_shader_variant_compile(3584device, modules[i], &nir[i], 1, pipeline->layout, keys + i, infos + i,3585keep_executable_info, keep_statistic_info, disable_optimizations, &binaries[i]);35863587radv_stop_feedback(stage_feedbacks[i], false);3588}3589}35903591if (!keep_executable_info) {3592radv_pipeline_cache_insert_shaders(device, cache, hash, pipeline->shaders, binaries);3593}35943595for (int i = 0; i < MESA_SHADER_STAGES; ++i) {3596free(binaries[i]);3597if (nir[i]) {3598ralloc_free(nir[i]);35993600if (radv_can_dump_shader_stats(device, modules[i])) {3601radv_dump_shader_stats(device, pipeline, i, stderr);3602}3603}3604}36053606if (fs_m.nir)3607ralloc_free(fs_m.nir);36083609radv_stop_feedback(pipeline_feedback, false);3610return VK_SUCCESS;3611}36123613static uint32_t3614radv_pipeline_stage_to_user_data_0(struct radv_pipeline *pipeline, gl_shader_stage stage,3615enum chip_class chip_class)3616{3617bool has_gs = radv_pipeline_has_gs(pipeline);3618bool has_tess = radv_pipeline_has_tess(pipeline);3619bool has_ngg = radv_pipeline_has_ngg(pipeline);36203621switch (stage) {3622case MESA_SHADER_FRAGMENT:3623return R_00B030_SPI_SHADER_USER_DATA_PS_0;3624case MESA_SHADER_VERTEX:3625if (has_tess) {3626if (chip_class >= GFX10) {3627return R_00B430_SPI_SHADER_USER_DATA_HS_0;3628} else if (chip_class == GFX9) {3629return R_00B430_SPI_SHADER_USER_DATA_LS_0;3630} else {3631return R_00B530_SPI_SHADER_USER_DATA_LS_0;3632}3633}36343635if (has_gs) {3636if (chip_class >= GFX10) {3637return R_00B230_SPI_SHADER_USER_DATA_GS_0;3638} else {3639return R_00B330_SPI_SHADER_USER_DATA_ES_0;3640}3641}36423643if (has_ngg)3644return R_00B230_SPI_SHADER_USER_DATA_GS_0;36453646return R_00B130_SPI_SHADER_USER_DATA_VS_0;3647case MESA_SHADER_GEOMETRY:3648return chip_class == GFX9 ? R_00B330_SPI_SHADER_USER_DATA_ES_03649: R_00B230_SPI_SHADER_USER_DATA_GS_0;3650case MESA_SHADER_COMPUTE:3651return R_00B900_COMPUTE_USER_DATA_0;3652case MESA_SHADER_TESS_CTRL:3653return chip_class == GFX9 ? R_00B430_SPI_SHADER_USER_DATA_LS_03654: R_00B430_SPI_SHADER_USER_DATA_HS_0;3655case MESA_SHADER_TESS_EVAL:3656if (has_gs) {3657return chip_class >= GFX10 ? R_00B230_SPI_SHADER_USER_DATA_GS_03658: R_00B330_SPI_SHADER_USER_DATA_ES_0;3659} else if (has_ngg) {3660return R_00B230_SPI_SHADER_USER_DATA_GS_0;3661} else {3662return R_00B130_SPI_SHADER_USER_DATA_VS_0;3663}3664default:3665unreachable("unknown shader");3666}3667}36683669struct radv_bin_size_entry {3670unsigned bpp;3671VkExtent2D extent;3672};36733674static VkExtent2D3675radv_gfx9_compute_bin_size(const struct radv_pipeline *pipeline,3676const VkGraphicsPipelineCreateInfo *pCreateInfo)3677{3678static const struct radv_bin_size_entry color_size_table[][3][9] = {3679{3680/* One RB / SE */3681{3682/* One shader engine */3683{0, {128, 128}},3684{1, {64, 128}},3685{2, {32, 128}},3686{3, {16, 128}},3687{17, {0, 0}},3688{UINT_MAX, {0, 0}},3689},3690{3691/* Two shader engines */3692{0, {128, 128}},3693{2, {64, 128}},3694{3, {32, 128}},3695{5, {16, 128}},3696{17, {0, 0}},3697{UINT_MAX, {0, 0}},3698},3699{3700/* Four shader engines */3701{0, {128, 128}},3702{3, {64, 128}},3703{5, {16, 128}},3704{17, {0, 0}},3705{UINT_MAX, {0, 0}},3706},3707},3708{3709/* Two RB / SE */3710{3711/* One shader engine */3712{0, {128, 128}},3713{2, {64, 128}},3714{3, {32, 128}},3715{5, {16, 128}},3716{33, {0, 0}},3717{UINT_MAX, {0, 0}},3718},3719{3720/* Two shader engines */3721{0, {128, 128}},3722{3, {64, 128}},3723{5, {32, 128}},3724{9, {16, 128}},3725{33, {0, 0}},3726{UINT_MAX, {0, 0}},3727},3728{3729/* Four shader engines */3730{0, {256, 256}},3731{2, {128, 256}},3732{3, {128, 128}},3733{5, {64, 128}},3734{9, {16, 128}},3735{33, {0, 0}},3736{UINT_MAX, {0, 0}},3737},3738},3739{3740/* Four RB / SE */3741{3742/* One shader engine */3743{0, {128, 256}},3744{2, {128, 128}},3745{3, {64, 128}},3746{5, {32, 128}},3747{9, {16, 128}},3748{33, {0, 0}},3749{UINT_MAX, {0, 0}},3750},3751{3752/* Two shader engines */3753{0, {256, 256}},3754{2, {128, 256}},3755{3, {128, 128}},3756{5, {64, 128}},3757{9, {32, 128}},3758{17, {16, 128}},3759{33, {0, 0}},3760{UINT_MAX, {0, 0}},3761},3762{3763/* Four shader engines */3764{0, {256, 512}},3765{2, {256, 256}},3766{3, {128, 256}},3767{5, {128, 128}},3768{9, {64, 128}},3769{17, {16, 128}},3770{33, {0, 0}},3771{UINT_MAX, {0, 0}},3772},3773},3774};3775static const struct radv_bin_size_entry ds_size_table[][3][9] = {3776{3777// One RB / SE3778{3779// One shader engine3780{0, {128, 256}},3781{2, {128, 128}},3782{4, {64, 128}},3783{7, {32, 128}},3784{13, {16, 128}},3785{49, {0, 0}},3786{UINT_MAX, {0, 0}},3787},3788{3789// Two shader engines3790{0, {256, 256}},3791{2, {128, 256}},3792{4, {128, 128}},3793{7, {64, 128}},3794{13, {32, 128}},3795{25, {16, 128}},3796{49, {0, 0}},3797{UINT_MAX, {0, 0}},3798},3799{3800// Four shader engines3801{0, {256, 512}},3802{2, {256, 256}},3803{4, {128, 256}},3804{7, {128, 128}},3805{13, {64, 128}},3806{25, {16, 128}},3807{49, {0, 0}},3808{UINT_MAX, {0, 0}},3809},3810},3811{3812// Two RB / SE3813{3814// One shader engine3815{0, {256, 256}},3816{2, {128, 256}},3817{4, {128, 128}},3818{7, {64, 128}},3819{13, {32, 128}},3820{25, {16, 128}},3821{97, {0, 0}},3822{UINT_MAX, {0, 0}},3823},3824{3825// Two shader engines3826{0, {256, 512}},3827{2, {256, 256}},3828{4, {128, 256}},3829{7, {128, 128}},3830{13, {64, 128}},3831{25, {32, 128}},3832{49, {16, 128}},3833{97, {0, 0}},3834{UINT_MAX, {0, 0}},3835},3836{3837// Four shader engines3838{0, {512, 512}},3839{2, {256, 512}},3840{4, {256, 256}},3841{7, {128, 256}},3842{13, {128, 128}},3843{25, {64, 128}},3844{49, {16, 128}},3845{97, {0, 0}},3846{UINT_MAX, {0, 0}},3847},3848},3849{3850// Four RB / SE3851{3852// One shader engine3853{0, {256, 512}},3854{2, {256, 256}},3855{4, {128, 256}},3856{7, {128, 128}},3857{13, {64, 128}},3858{25, {32, 128}},3859{49, {16, 128}},3860{UINT_MAX, {0, 0}},3861},3862{3863// Two shader engines3864{0, {512, 512}},3865{2, {256, 512}},3866{4, {256, 256}},3867{7, {128, 256}},3868{13, {128, 128}},3869{25, {64, 128}},3870{49, {32, 128}},3871{97, {16, 128}},3872{UINT_MAX, {0, 0}},3873},3874{3875// Four shader engines3876{0, {512, 512}},3877{4, {256, 512}},3878{7, {256, 256}},3879{13, {128, 256}},3880{25, {128, 128}},3881{49, {64, 128}},3882{97, {16, 128}},3883{UINT_MAX, {0, 0}},3884},3885},3886};38873888RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);3889struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;3890VkExtent2D extent = {512, 512};38913892unsigned log_num_rb_per_se =3893util_logbase2_ceil(pipeline->device->physical_device->rad_info.max_render_backends /3894pipeline->device->physical_device->rad_info.max_se);3895unsigned log_num_se = util_logbase2_ceil(pipeline->device->physical_device->rad_info.max_se);38963897unsigned total_samples = 1u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_aa_config);3898unsigned ps_iter_samples = 1u << G_028804_PS_ITER_SAMPLES(pipeline->graphics.ms.db_eqaa);3899unsigned effective_samples = total_samples;3900unsigned color_bytes_per_pixel = 0;39013902const VkPipelineColorBlendStateCreateInfo *vkblend =3903radv_pipeline_get_color_blend_state(pCreateInfo);3904if (vkblend) {3905for (unsigned i = 0; i < subpass->color_count; i++) {3906if (!vkblend->pAttachments[i].colorWriteMask)3907continue;39083909if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)3910continue;39113912VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;3913color_bytes_per_pixel += vk_format_get_blocksize(format);3914}39153916/* MSAA images typically don't use all samples all the time. */3917if (effective_samples >= 2 && ps_iter_samples <= 1)3918effective_samples = 2;3919color_bytes_per_pixel *= effective_samples;3920}39213922const struct radv_bin_size_entry *color_entry = color_size_table[log_num_rb_per_se][log_num_se];3923while (color_entry[1].bpp <= color_bytes_per_pixel)3924++color_entry;39253926extent = color_entry->extent;39273928if (subpass->depth_stencil_attachment) {3929struct radv_render_pass_attachment *attachment =3930pass->attachments + subpass->depth_stencil_attachment->attachment;39313932/* Coefficients taken from AMDVLK */3933unsigned depth_coeff = vk_format_has_depth(attachment->format) ? 5 : 0;3934unsigned stencil_coeff = vk_format_has_stencil(attachment->format) ? 1 : 0;3935unsigned ds_bytes_per_pixel = 4 * (depth_coeff + stencil_coeff) * total_samples;39363937const struct radv_bin_size_entry *ds_entry = ds_size_table[log_num_rb_per_se][log_num_se];3938while (ds_entry[1].bpp <= ds_bytes_per_pixel)3939++ds_entry;39403941if (ds_entry->extent.width * ds_entry->extent.height < extent.width * extent.height)3942extent = ds_entry->extent;3943}39443945return extent;3946}39473948static VkExtent2D3949radv_gfx10_compute_bin_size(const struct radv_pipeline *pipeline,3950const VkGraphicsPipelineCreateInfo *pCreateInfo)3951{3952RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);3953struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;3954VkExtent2D extent = {512, 512};39553956const unsigned db_tag_size = 64;3957const unsigned db_tag_count = 312;3958const unsigned color_tag_size = 1024;3959const unsigned color_tag_count = 31;3960const unsigned fmask_tag_size = 256;3961const unsigned fmask_tag_count = 44;39623963const unsigned rb_count = pipeline->device->physical_device->rad_info.max_render_backends;3964const unsigned pipe_count =3965MAX2(rb_count, pipeline->device->physical_device->rad_info.num_tcc_blocks);39663967const unsigned db_tag_part = (db_tag_count * rb_count / pipe_count) * db_tag_size * pipe_count;3968const unsigned color_tag_part =3969(color_tag_count * rb_count / pipe_count) * color_tag_size * pipe_count;3970const unsigned fmask_tag_part =3971(fmask_tag_count * rb_count / pipe_count) * fmask_tag_size * pipe_count;39723973const unsigned total_samples =39741u << G_028BE0_MSAA_NUM_SAMPLES(pipeline->graphics.ms.pa_sc_aa_config);3975const unsigned samples_log = util_logbase2_ceil(total_samples);39763977unsigned color_bytes_per_pixel = 0;3978unsigned fmask_bytes_per_pixel = 0;39793980const VkPipelineColorBlendStateCreateInfo *vkblend =3981radv_pipeline_get_color_blend_state(pCreateInfo);3982if (vkblend) {3983for (unsigned i = 0; i < subpass->color_count; i++) {3984if (!vkblend->pAttachments[i].colorWriteMask)3985continue;39863987if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)3988continue;39893990VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;3991color_bytes_per_pixel += vk_format_get_blocksize(format);39923993if (total_samples > 1) {3994assert(samples_log <= 3);3995const unsigned fmask_array[] = {0, 1, 1, 4};3996fmask_bytes_per_pixel += fmask_array[samples_log];3997}3998}39994000color_bytes_per_pixel *= total_samples;4001}4002color_bytes_per_pixel = MAX2(color_bytes_per_pixel, 1);40034004const unsigned color_pixel_count_log = util_logbase2(color_tag_part / color_bytes_per_pixel);4005extent.width = 1ull << ((color_pixel_count_log + 1) / 2);4006extent.height = 1ull << (color_pixel_count_log / 2);40074008if (fmask_bytes_per_pixel) {4009const unsigned fmask_pixel_count_log = util_logbase2(fmask_tag_part / fmask_bytes_per_pixel);40104011const VkExtent2D fmask_extent =4012(VkExtent2D){.width = 1ull << ((fmask_pixel_count_log + 1) / 2),4013.height = 1ull << (color_pixel_count_log / 2)};40144015if (fmask_extent.width * fmask_extent.height < extent.width * extent.height)4016extent = fmask_extent;4017}40184019if (subpass->depth_stencil_attachment) {4020struct radv_render_pass_attachment *attachment =4021pass->attachments + subpass->depth_stencil_attachment->attachment;40224023/* Coefficients taken from AMDVLK */4024unsigned depth_coeff = vk_format_has_depth(attachment->format) ? 5 : 0;4025unsigned stencil_coeff = vk_format_has_stencil(attachment->format) ? 1 : 0;4026unsigned db_bytes_per_pixel = (depth_coeff + stencil_coeff) * total_samples;40274028const unsigned db_pixel_count_log = util_logbase2(db_tag_part / db_bytes_per_pixel);40294030const VkExtent2D db_extent = (VkExtent2D){.width = 1ull << ((db_pixel_count_log + 1) / 2),4031.height = 1ull << (color_pixel_count_log / 2)};40324033if (db_extent.width * db_extent.height < extent.width * extent.height)4034extent = db_extent;4035}40364037extent.width = MAX2(extent.width, 128);4038extent.height = MAX2(extent.width, 64);40394040return extent;4041}40424043static void4044radv_pipeline_init_disabled_binning_state(struct radv_pipeline *pipeline,4045const VkGraphicsPipelineCreateInfo *pCreateInfo)4046{4047uint32_t pa_sc_binner_cntl_0 = S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC) |4048S_028C44_DISABLE_START_OF_PRIM(1);40494050if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {4051RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);4052struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;4053const VkPipelineColorBlendStateCreateInfo *vkblend =4054radv_pipeline_get_color_blend_state(pCreateInfo);4055unsigned min_bytes_per_pixel = 0;40564057if (vkblend) {4058for (unsigned i = 0; i < subpass->color_count; i++) {4059if (!vkblend->pAttachments[i].colorWriteMask)4060continue;40614062if (subpass->color_attachments[i].attachment == VK_ATTACHMENT_UNUSED)4063continue;40644065VkFormat format = pass->attachments[subpass->color_attachments[i].attachment].format;4066unsigned bytes = vk_format_get_blocksize(format);4067if (!min_bytes_per_pixel || bytes < min_bytes_per_pixel)4068min_bytes_per_pixel = bytes;4069}4070}40714072pa_sc_binner_cntl_0 =4073S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_NEW_SC) | S_028C44_BIN_SIZE_X(0) |4074S_028C44_BIN_SIZE_Y(0) | S_028C44_BIN_SIZE_X_EXTEND(2) | /* 128 */4075S_028C44_BIN_SIZE_Y_EXTEND(min_bytes_per_pixel <= 4 ? 2 : 1) | /* 128 or 64 */4076S_028C44_DISABLE_START_OF_PRIM(1);4077}40784079pipeline->graphics.binning.pa_sc_binner_cntl_0 = pa_sc_binner_cntl_0;4080}40814082struct radv_binning_settings4083radv_get_binning_settings(const struct radv_physical_device *pdev)4084{4085struct radv_binning_settings settings;4086if (pdev->rad_info.has_dedicated_vram) {4087if (pdev->rad_info.max_render_backends > 4) {4088settings.context_states_per_bin = 1;4089settings.persistent_states_per_bin = 1;4090} else {4091settings.context_states_per_bin = 3;4092settings.persistent_states_per_bin = 8;4093}4094settings.fpovs_per_batch = 63;4095} else {4096/* The context states are affected by the scissor bug. */4097settings.context_states_per_bin = 6;4098/* 32 causes hangs for RAVEN. */4099settings.persistent_states_per_bin = 16;4100settings.fpovs_per_batch = 63;4101}41024103if (pdev->rad_info.has_gfx9_scissor_bug)4104settings.context_states_per_bin = 1;41054106return settings;4107}41084109static void4110radv_pipeline_init_binning_state(struct radv_pipeline *pipeline,4111const VkGraphicsPipelineCreateInfo *pCreateInfo,4112const struct radv_blend_state *blend)4113{4114if (pipeline->device->physical_device->rad_info.chip_class < GFX9)4115return;41164117VkExtent2D bin_size;4118if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {4119bin_size = radv_gfx10_compute_bin_size(pipeline, pCreateInfo);4120} else if (pipeline->device->physical_device->rad_info.chip_class == GFX9) {4121bin_size = radv_gfx9_compute_bin_size(pipeline, pCreateInfo);4122} else4123unreachable("Unhandled generation for binning bin size calculation");41244125if (pipeline->device->pbb_allowed && bin_size.width && bin_size.height) {4126struct radv_binning_settings settings =4127radv_get_binning_settings(pipeline->device->physical_device);41284129const uint32_t pa_sc_binner_cntl_0 =4130S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED) |4131S_028C44_BIN_SIZE_X(bin_size.width == 16) | S_028C44_BIN_SIZE_Y(bin_size.height == 16) |4132S_028C44_BIN_SIZE_X_EXTEND(util_logbase2(MAX2(bin_size.width, 32)) - 5) |4133S_028C44_BIN_SIZE_Y_EXTEND(util_logbase2(MAX2(bin_size.height, 32)) - 5) |4134S_028C44_CONTEXT_STATES_PER_BIN(settings.context_states_per_bin - 1) |4135S_028C44_PERSISTENT_STATES_PER_BIN(settings.persistent_states_per_bin - 1) |4136S_028C44_DISABLE_START_OF_PRIM(1) |4137S_028C44_FPOVS_PER_BATCH(settings.fpovs_per_batch) | S_028C44_OPTIMAL_BIN_SELECTION(1);41384139pipeline->graphics.binning.pa_sc_binner_cntl_0 = pa_sc_binner_cntl_0;4140} else4141radv_pipeline_init_disabled_binning_state(pipeline, pCreateInfo);4142}41434144static void4145radv_pipeline_generate_depth_stencil_state(struct radeon_cmdbuf *ctx_cs,4146const struct radv_pipeline *pipeline,4147const VkGraphicsPipelineCreateInfo *pCreateInfo,4148const struct radv_graphics_pipeline_create_info *extra)4149{4150const VkPipelineDepthStencilStateCreateInfo *vkds =4151radv_pipeline_get_depth_stencil_state(pCreateInfo);4152RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);4153struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;4154struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];4155struct radv_render_pass_attachment *attachment = NULL;4156uint32_t db_render_control = 0, db_render_override2 = 0;4157uint32_t db_render_override = 0;41584159if (subpass->depth_stencil_attachment)4160attachment = pass->attachments + subpass->depth_stencil_attachment->attachment;41614162bool has_depth_attachment = attachment && vk_format_has_depth(attachment->format);41634164if (vkds && has_depth_attachment) {4165/* from amdvlk: For 4xAA and 8xAA need to decompress on flush for better performance */4166db_render_override2 |= S_028010_DECOMPRESS_Z_ON_FLUSH(attachment->samples > 2);41674168if (pipeline->device->physical_device->rad_info.chip_class >= GFX10_3)4169db_render_override2 |= S_028010_CENTROID_COMPUTATION_MODE(1);4170}41714172if (attachment && extra) {4173db_render_control |= S_028000_DEPTH_CLEAR_ENABLE(extra->db_depth_clear);4174db_render_control |= S_028000_STENCIL_CLEAR_ENABLE(extra->db_stencil_clear);41754176db_render_control |= S_028000_RESUMMARIZE_ENABLE(extra->resummarize_enable);4177db_render_control |= S_028000_DEPTH_COMPRESS_DISABLE(extra->depth_compress_disable);4178db_render_control |= S_028000_STENCIL_COMPRESS_DISABLE(extra->stencil_compress_disable);4179db_render_override2 |=4180S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra->db_depth_disable_expclear);4181db_render_override2 |=4182S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra->db_stencil_disable_expclear);4183}41844185db_render_override |= S_02800C_FORCE_HIS_ENABLE0(V_02800C_FORCE_DISABLE) |4186S_02800C_FORCE_HIS_ENABLE1(V_02800C_FORCE_DISABLE);41874188if (!pCreateInfo->pRasterizationState->depthClampEnable && ps->info.ps.writes_z) {4189/* From VK_EXT_depth_range_unrestricted spec:4190*4191* "The behavior described in Primitive Clipping still applies.4192* If depth clamping is disabled the depth values are still4193* clipped to 0 ≤ zc ≤ wc before the viewport transform. If4194* depth clamping is enabled the above equation is ignored and4195* the depth values are instead clamped to the VkViewport4196* minDepth and maxDepth values, which in the case of this4197* extension can be outside of the 0.0 to 1.0 range."4198*/4199db_render_override |= S_02800C_DISABLE_VIEWPORT_CLAMP(1);4200}42014202radeon_set_context_reg(ctx_cs, R_028000_DB_RENDER_CONTROL, db_render_control);4203radeon_set_context_reg(ctx_cs, R_02800C_DB_RENDER_OVERRIDE, db_render_override);4204radeon_set_context_reg(ctx_cs, R_028010_DB_RENDER_OVERRIDE2, db_render_override2);4205}42064207static void4208radv_pipeline_generate_blend_state(struct radeon_cmdbuf *ctx_cs,4209const struct radv_pipeline *pipeline,4210const struct radv_blend_state *blend)4211{4212radeon_set_context_reg_seq(ctx_cs, R_028780_CB_BLEND0_CONTROL, 8);4213radeon_emit_array(ctx_cs, blend->cb_blend_control, 8);4214radeon_set_context_reg(ctx_cs, R_028B70_DB_ALPHA_TO_MASK, blend->db_alpha_to_mask);42154216if (pipeline->device->physical_device->rad_info.has_rbplus) {42174218radeon_set_context_reg_seq(ctx_cs, R_028760_SX_MRT0_BLEND_OPT, 8);4219radeon_emit_array(ctx_cs, blend->sx_mrt_blend_opt, 8);4220}42214222radeon_set_context_reg(ctx_cs, R_028714_SPI_SHADER_COL_FORMAT, blend->spi_shader_col_format);42234224radeon_set_context_reg(ctx_cs, R_02823C_CB_SHADER_MASK, blend->cb_shader_mask);4225}42264227static void4228radv_pipeline_generate_raster_state(struct radeon_cmdbuf *ctx_cs,4229const struct radv_pipeline *pipeline,4230const VkGraphicsPipelineCreateInfo *pCreateInfo)4231{4232const VkPipelineRasterizationStateCreateInfo *vkraster = pCreateInfo->pRasterizationState;4233const VkConservativeRasterizationModeEXT mode = radv_get_conservative_raster_mode(vkraster);4234uint32_t pa_sc_conservative_rast = S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1);42354236radeon_set_context_reg(ctx_cs, R_028BDC_PA_SC_LINE_CNTL, S_028BDC_DX10_DIAMOND_TEST_ENA(1));42374238if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {4239/* Conservative rasterization. */4240if (mode != VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT) {4241pa_sc_conservative_rast = S_028C4C_PREZ_AA_MASK_ENABLE(1) | S_028C4C_POSTZ_AA_MASK_ENABLE(1) |4242S_028C4C_CENTROID_SAMPLE_OVERRIDE(1);42434244if (mode == VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT) {4245pa_sc_conservative_rast |=4246S_028C4C_OVER_RAST_ENABLE(1) | S_028C4C_OVER_RAST_SAMPLE_SELECT(0) |4247S_028C4C_UNDER_RAST_ENABLE(0) | S_028C4C_UNDER_RAST_SAMPLE_SELECT(1) |4248S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(1);4249} else {4250assert(mode == VK_CONSERVATIVE_RASTERIZATION_MODE_UNDERESTIMATE_EXT);4251pa_sc_conservative_rast |=4252S_028C4C_OVER_RAST_ENABLE(0) | S_028C4C_OVER_RAST_SAMPLE_SELECT(1) |4253S_028C4C_UNDER_RAST_ENABLE(1) | S_028C4C_UNDER_RAST_SAMPLE_SELECT(0) |4254S_028C4C_PBB_UNCERTAINTY_REGION_ENABLE(0);4255}4256}42574258radeon_set_context_reg(ctx_cs, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL,4259pa_sc_conservative_rast);4260}4261}42624263static void4264radv_pipeline_generate_multisample_state(struct radeon_cmdbuf *ctx_cs,4265const struct radv_pipeline *pipeline)4266{4267const struct radv_multisample_state *ms = &pipeline->graphics.ms;42684269radeon_set_context_reg_seq(ctx_cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);4270radeon_emit(ctx_cs, ms->pa_sc_aa_mask[0]);4271radeon_emit(ctx_cs, ms->pa_sc_aa_mask[1]);42724273radeon_set_context_reg(ctx_cs, R_028804_DB_EQAA, ms->db_eqaa);4274radeon_set_context_reg(ctx_cs, R_028A48_PA_SC_MODE_CNTL_0, ms->pa_sc_mode_cntl_0);4275radeon_set_context_reg(ctx_cs, R_028A4C_PA_SC_MODE_CNTL_1, ms->pa_sc_mode_cntl_1);4276radeon_set_context_reg(ctx_cs, R_028BE0_PA_SC_AA_CONFIG, ms->pa_sc_aa_config);42774278/* The exclusion bits can be set to improve rasterization efficiency4279* if no sample lies on the pixel boundary (-8 sample offset). It's4280* currently always TRUE because the driver doesn't support 16 samples.4281*/4282bool exclusion = pipeline->device->physical_device->rad_info.chip_class >= GFX7;4283radeon_set_context_reg(4284ctx_cs, R_02882C_PA_SU_PRIM_FILTER_CNTL,4285S_02882C_XMAX_RIGHT_EXCLUSION(exclusion) | S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion));4286}42874288static void4289radv_pipeline_generate_vgt_gs_mode(struct radeon_cmdbuf *ctx_cs,4290const struct radv_pipeline *pipeline)4291{4292const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);4293const struct radv_shader_variant *vs = pipeline->shaders[MESA_SHADER_TESS_EVAL]4294? pipeline->shaders[MESA_SHADER_TESS_EVAL]4295: pipeline->shaders[MESA_SHADER_VERTEX];4296unsigned vgt_primitiveid_en = 0;4297uint32_t vgt_gs_mode = 0;42984299if (radv_pipeline_has_ngg(pipeline))4300return;43014302if (radv_pipeline_has_gs(pipeline)) {4303const struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];43044305vgt_gs_mode = ac_vgt_gs_mode(gs->info.gs.vertices_out,4306pipeline->device->physical_device->rad_info.chip_class);4307} else if (outinfo->export_prim_id || vs->info.uses_prim_id) {4308vgt_gs_mode = S_028A40_MODE(V_028A40_GS_SCENARIO_A);4309vgt_primitiveid_en |= S_028A84_PRIMITIVEID_EN(1);4310}43114312radeon_set_context_reg(ctx_cs, R_028A84_VGT_PRIMITIVEID_EN, vgt_primitiveid_en);4313radeon_set_context_reg(ctx_cs, R_028A40_VGT_GS_MODE, vgt_gs_mode);4314}43154316static void4317radv_pipeline_generate_hw_vs(struct radeon_cmdbuf *ctx_cs, struct radeon_cmdbuf *cs,4318const struct radv_pipeline *pipeline,4319const struct radv_shader_variant *shader)4320{4321uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;43224323radeon_set_sh_reg_seq(cs, R_00B120_SPI_SHADER_PGM_LO_VS, 4);4324radeon_emit(cs, va >> 8);4325radeon_emit(cs, S_00B124_MEM_BASE(va >> 40));4326radeon_emit(cs, shader->config.rsrc1);4327radeon_emit(cs, shader->config.rsrc2);43284329const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);4330unsigned clip_dist_mask, cull_dist_mask, total_mask;4331clip_dist_mask = outinfo->clip_dist_mask;4332cull_dist_mask = outinfo->cull_dist_mask;4333total_mask = clip_dist_mask | cull_dist_mask;43344335bool writes_primitive_shading_rate =4336outinfo->writes_primitive_shading_rate || pipeline->device->force_vrs != RADV_FORCE_VRS_NONE;4337bool misc_vec_ena = outinfo->writes_pointsize || outinfo->writes_layer ||4338outinfo->writes_viewport_index || writes_primitive_shading_rate;4339unsigned spi_vs_out_config, nparams;43404341/* VS is required to export at least one param. */4342nparams = MAX2(outinfo->param_exports, 1);4343spi_vs_out_config = S_0286C4_VS_EXPORT_COUNT(nparams - 1);43444345if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {4346spi_vs_out_config |= S_0286C4_NO_PC_EXPORT(outinfo->param_exports == 0);4347}43484349radeon_set_context_reg(ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG, spi_vs_out_config);43504351radeon_set_context_reg(4352ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,4353S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |4354S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ? V_02870C_SPI_SHADER_4COMP4355: V_02870C_SPI_SHADER_NONE) |4356S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ? V_02870C_SPI_SHADER_4COMP4357: V_02870C_SPI_SHADER_NONE) |4358S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ? V_02870C_SPI_SHADER_4COMP4359: V_02870C_SPI_SHADER_NONE));43604361radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,4362S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |4363S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |4364S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |4365S_02881C_USE_VTX_VRS_RATE(writes_primitive_shading_rate) |4366S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |4367S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |4368S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |4369S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |4370cull_dist_mask << 8 | clip_dist_mask);43714372if (pipeline->device->physical_device->rad_info.chip_class <= GFX8)4373radeon_set_context_reg(ctx_cs, R_028AB4_VGT_REUSE_OFF, outinfo->writes_viewport_index);4374}43754376static void4377radv_pipeline_generate_hw_es(struct radeon_cmdbuf *cs, const struct radv_pipeline *pipeline,4378const struct radv_shader_variant *shader)4379{4380uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;43814382radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 4);4383radeon_emit(cs, va >> 8);4384radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));4385radeon_emit(cs, shader->config.rsrc1);4386radeon_emit(cs, shader->config.rsrc2);4387}43884389static void4390radv_pipeline_generate_hw_ls(struct radeon_cmdbuf *cs, const struct radv_pipeline *pipeline,4391const struct radv_shader_variant *shader)4392{4393unsigned num_lds_blocks = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.num_lds_blocks;4394uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;4395uint32_t rsrc2 = shader->config.rsrc2;43964397radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);4398radeon_emit(cs, va >> 8);4399radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));44004401rsrc2 |= S_00B52C_LDS_SIZE(num_lds_blocks);4402if (pipeline->device->physical_device->rad_info.chip_class == GFX7 &&4403pipeline->device->physical_device->rad_info.family != CHIP_HAWAII)4404radeon_set_sh_reg(cs, R_00B52C_SPI_SHADER_PGM_RSRC2_LS, rsrc2);44054406radeon_set_sh_reg_seq(cs, R_00B528_SPI_SHADER_PGM_RSRC1_LS, 2);4407radeon_emit(cs, shader->config.rsrc1);4408radeon_emit(cs, rsrc2);4409}44104411static void4412radv_pipeline_generate_hw_ngg(struct radeon_cmdbuf *ctx_cs, struct radeon_cmdbuf *cs,4413const struct radv_pipeline *pipeline,4414const struct radv_shader_variant *shader)4415{4416uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;4417gl_shader_stage es_type =4418radv_pipeline_has_tess(pipeline) ? MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;4419struct radv_shader_variant *es = es_type == MESA_SHADER_TESS_EVAL4420? pipeline->shaders[MESA_SHADER_TESS_EVAL]4421: pipeline->shaders[MESA_SHADER_VERTEX];4422const struct gfx10_ngg_info *ngg_state = &shader->info.ngg_info;44234424radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);4425radeon_emit(cs, va >> 8);4426radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));4427radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);4428radeon_emit(cs, shader->config.rsrc1);4429radeon_emit(cs, shader->config.rsrc2);44304431const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);4432unsigned clip_dist_mask, cull_dist_mask, total_mask;4433clip_dist_mask = outinfo->clip_dist_mask;4434cull_dist_mask = outinfo->cull_dist_mask;4435total_mask = clip_dist_mask | cull_dist_mask;44364437bool writes_primitive_shading_rate =4438outinfo->writes_primitive_shading_rate || pipeline->device->force_vrs != RADV_FORCE_VRS_NONE;4439bool misc_vec_ena = outinfo->writes_pointsize || outinfo->writes_layer ||4440outinfo->writes_viewport_index || writes_primitive_shading_rate;4441bool es_enable_prim_id = outinfo->export_prim_id || (es && es->info.uses_prim_id);4442bool break_wave_at_eoi = false;4443unsigned ge_cntl;4444unsigned nparams;44454446if (es_type == MESA_SHADER_TESS_EVAL) {4447struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];44484449if (es_enable_prim_id || (gs && gs->info.uses_prim_id))4450break_wave_at_eoi = true;4451}44524453nparams = MAX2(outinfo->param_exports, 1);4454radeon_set_context_reg(4455ctx_cs, R_0286C4_SPI_VS_OUT_CONFIG,4456S_0286C4_VS_EXPORT_COUNT(nparams - 1) | S_0286C4_NO_PC_EXPORT(outinfo->param_exports == 0));44574458radeon_set_context_reg(ctx_cs, R_028708_SPI_SHADER_IDX_FORMAT,4459S_028708_IDX0_EXPORT_FORMAT(V_028708_SPI_SHADER_1COMP));4460radeon_set_context_reg(4461ctx_cs, R_02870C_SPI_SHADER_POS_FORMAT,4462S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |4463S_02870C_POS1_EXPORT_FORMAT(outinfo->pos_exports > 1 ? V_02870C_SPI_SHADER_4COMP4464: V_02870C_SPI_SHADER_NONE) |4465S_02870C_POS2_EXPORT_FORMAT(outinfo->pos_exports > 2 ? V_02870C_SPI_SHADER_4COMP4466: V_02870C_SPI_SHADER_NONE) |4467S_02870C_POS3_EXPORT_FORMAT(outinfo->pos_exports > 3 ? V_02870C_SPI_SHADER_4COMP4468: V_02870C_SPI_SHADER_NONE));44694470radeon_set_context_reg(ctx_cs, R_02881C_PA_CL_VS_OUT_CNTL,4471S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |4472S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |4473S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |4474S_02881C_USE_VTX_VRS_RATE(writes_primitive_shading_rate) |4475S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |4476S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |4477S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |4478S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |4479cull_dist_mask << 8 | clip_dist_mask);44804481radeon_set_context_reg(ctx_cs, R_028A84_VGT_PRIMITIVEID_EN,4482S_028A84_PRIMITIVEID_EN(es_enable_prim_id) |4483S_028A84_NGG_DISABLE_PROVOK_REUSE(outinfo->export_prim_id));44844485radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,4486ngg_state->vgt_esgs_ring_itemsize);44874488/* NGG specific registers. */4489struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];4490uint32_t gs_num_invocations = gs ? gs->info.gs.invocations : 1;44914492radeon_set_context_reg(4493ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL,4494S_028A44_ES_VERTS_PER_SUBGRP(ngg_state->hw_max_esverts) |4495S_028A44_GS_PRIMS_PER_SUBGRP(ngg_state->max_gsprims) |4496S_028A44_GS_INST_PRIMS_IN_SUBGRP(ngg_state->max_gsprims * gs_num_invocations));4497radeon_set_context_reg(ctx_cs, R_0287FC_GE_MAX_OUTPUT_PER_SUBGROUP,4498S_0287FC_MAX_VERTS_PER_SUBGROUP(ngg_state->max_out_verts));4499radeon_set_context_reg(ctx_cs, R_028B4C_GE_NGG_SUBGRP_CNTL,4500S_028B4C_PRIM_AMP_FACTOR(ngg_state->prim_amp_factor) |4501S_028B4C_THDS_PER_SUBGRP(0)); /* for fast launch */4502radeon_set_context_reg(4503ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,4504S_028B90_CNT(gs_num_invocations) | S_028B90_ENABLE(gs_num_invocations > 1) |4505S_028B90_EN_MAX_VERT_OUT_PER_GS_INSTANCE(ngg_state->max_vert_out_per_gs_instance));45064507/* User edge flags are set by the pos exports. If user edge flags are4508* not used, we must use hw-generated edge flags and pass them via4509* the prim export to prevent drawing lines on internal edges of4510* decomposed primitives (such as quads) with polygon mode = lines.4511*4512* TODO: We should combine hw-generated edge flags with user edge4513* flags in the shader.4514*/4515radeon_set_context_reg(4516ctx_cs, R_028838_PA_CL_NGG_CNTL,4517S_028838_INDEX_BUF_EDGE_FLAG_ENA(!radv_pipeline_has_tess(pipeline) &&4518!radv_pipeline_has_gs(pipeline)) |4519/* Reuse for NGG. */4520S_028838_VERTEX_REUSE_DEPTH(4521pipeline->device->physical_device->rad_info.chip_class >= GFX10_3 ? 30 : 0));45224523ge_cntl = S_03096C_PRIM_GRP_SIZE(ngg_state->max_gsprims) |4524S_03096C_VERT_GRP_SIZE(ngg_state->enable_vertex_grouping ? ngg_state->hw_max_esverts : 256) | /* 256 = disable vertex grouping */4525S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi);45264527/* Bug workaround for a possible hang with non-tessellation cases.4528* Tessellation always sets GE_CNTL.VERT_GRP_SIZE = 04529*4530* Requirement: GE_CNTL.VERT_GRP_SIZE = VGT_GS_ONCHIP_CNTL.ES_VERTS_PER_SUBGRP - 54531*/4532if (pipeline->device->physical_device->rad_info.chip_class == GFX10 &&4533!radv_pipeline_has_tess(pipeline) && ngg_state->hw_max_esverts != 256) {4534ge_cntl &= C_03096C_VERT_GRP_SIZE;45354536if (ngg_state->hw_max_esverts > 5) {4537ge_cntl |= S_03096C_VERT_GRP_SIZE(ngg_state->hw_max_esverts - 5);4538}4539}45404541radeon_set_uconfig_reg(ctx_cs, R_03096C_GE_CNTL, ge_cntl);4542}45434544static void4545radv_pipeline_generate_hw_hs(struct radeon_cmdbuf *cs, const struct radv_pipeline *pipeline,4546const struct radv_shader_variant *shader)4547{4548uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;45494550if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {4551if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {4552radeon_set_sh_reg_seq(cs, R_00B520_SPI_SHADER_PGM_LO_LS, 2);4553radeon_emit(cs, va >> 8);4554radeon_emit(cs, S_00B524_MEM_BASE(va >> 40));4555} else {4556radeon_set_sh_reg_seq(cs, R_00B410_SPI_SHADER_PGM_LO_LS, 2);4557radeon_emit(cs, va >> 8);4558radeon_emit(cs, S_00B414_MEM_BASE(va >> 40));4559}45604561radeon_set_sh_reg_seq(cs, R_00B428_SPI_SHADER_PGM_RSRC1_HS, 2);4562radeon_emit(cs, shader->config.rsrc1);4563radeon_emit(cs, shader->config.rsrc2);4564} else {4565radeon_set_sh_reg_seq(cs, R_00B420_SPI_SHADER_PGM_LO_HS, 4);4566radeon_emit(cs, va >> 8);4567radeon_emit(cs, S_00B424_MEM_BASE(va >> 40));4568radeon_emit(cs, shader->config.rsrc1);4569radeon_emit(cs, shader->config.rsrc2);4570}4571}45724573static void4574radv_pipeline_generate_vertex_shader(struct radeon_cmdbuf *ctx_cs, struct radeon_cmdbuf *cs,4575const struct radv_pipeline *pipeline)4576{4577struct radv_shader_variant *vs;45784579/* Skip shaders merged into HS/GS */4580vs = pipeline->shaders[MESA_SHADER_VERTEX];4581if (!vs)4582return;45834584if (vs->info.vs.as_ls)4585radv_pipeline_generate_hw_ls(cs, pipeline, vs);4586else if (vs->info.vs.as_es)4587radv_pipeline_generate_hw_es(cs, pipeline, vs);4588else if (vs->info.is_ngg)4589radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, vs);4590else4591radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, vs);4592}45934594static void4595radv_pipeline_generate_tess_shaders(struct radeon_cmdbuf *ctx_cs, struct radeon_cmdbuf *cs,4596const struct radv_pipeline *pipeline)4597{4598struct radv_shader_variant *tes, *tcs;45994600tcs = pipeline->shaders[MESA_SHADER_TESS_CTRL];4601tes = pipeline->shaders[MESA_SHADER_TESS_EVAL];46024603if (tes) {4604if (tes->info.is_ngg) {4605radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, tes);4606} else if (tes->info.tes.as_es)4607radv_pipeline_generate_hw_es(cs, pipeline, tes);4608else4609radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, tes);4610}46114612radv_pipeline_generate_hw_hs(cs, pipeline, tcs);46134614if (pipeline->device->physical_device->rad_info.chip_class >= GFX10 &&4615!radv_pipeline_has_gs(pipeline) && !radv_pipeline_has_ngg(pipeline)) {4616radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL,4617S_028A44_ES_VERTS_PER_SUBGRP(250) | S_028A44_GS_PRIMS_PER_SUBGRP(126) |4618S_028A44_GS_INST_PRIMS_IN_SUBGRP(126));4619}4620}46214622static void4623radv_pipeline_generate_tess_state(struct radeon_cmdbuf *ctx_cs,4624const struct radv_pipeline *pipeline,4625const VkGraphicsPipelineCreateInfo *pCreateInfo)4626{4627struct radv_shader_variant *tes = radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL);4628unsigned type = 0, partitioning = 0, topology = 0, distribution_mode = 0;4629unsigned num_tcs_input_cp, num_tcs_output_cp, num_patches;4630unsigned ls_hs_config;46314632num_tcs_input_cp = pCreateInfo->pTessellationState->patchControlPoints;4633num_tcs_output_cp =4634pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.tcs_vertices_out; // TCS VERTICES OUT4635num_patches = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.num_tess_patches;46364637ls_hs_config = S_028B58_NUM_PATCHES(num_patches) | S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |4638S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);46394640if (pipeline->device->physical_device->rad_info.chip_class >= GFX7) {4641radeon_set_context_reg_idx(ctx_cs, R_028B58_VGT_LS_HS_CONFIG, 2, ls_hs_config);4642} else {4643radeon_set_context_reg(ctx_cs, R_028B58_VGT_LS_HS_CONFIG, ls_hs_config);4644}46454646switch (tes->info.tes.primitive_mode) {4647case GL_TRIANGLES:4648type = V_028B6C_TESS_TRIANGLE;4649break;4650case GL_QUADS:4651type = V_028B6C_TESS_QUAD;4652break;4653case GL_ISOLINES:4654type = V_028B6C_TESS_ISOLINE;4655break;4656}46574658switch (tes->info.tes.spacing) {4659case TESS_SPACING_EQUAL:4660partitioning = V_028B6C_PART_INTEGER;4661break;4662case TESS_SPACING_FRACTIONAL_ODD:4663partitioning = V_028B6C_PART_FRAC_ODD;4664break;4665case TESS_SPACING_FRACTIONAL_EVEN:4666partitioning = V_028B6C_PART_FRAC_EVEN;4667break;4668default:4669break;4670}46714672bool ccw = tes->info.tes.ccw;4673const VkPipelineTessellationDomainOriginStateCreateInfo *domain_origin_state =4674vk_find_struct_const(pCreateInfo->pTessellationState,4675PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO);46764677if (domain_origin_state &&4678domain_origin_state->domainOrigin != VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT)4679ccw = !ccw;46804681if (tes->info.tes.point_mode)4682topology = V_028B6C_OUTPUT_POINT;4683else if (tes->info.tes.primitive_mode == GL_ISOLINES)4684topology = V_028B6C_OUTPUT_LINE;4685else if (ccw)4686topology = V_028B6C_OUTPUT_TRIANGLE_CCW;4687else4688topology = V_028B6C_OUTPUT_TRIANGLE_CW;46894690if (pipeline->device->physical_device->rad_info.has_distributed_tess) {4691if (pipeline->device->physical_device->rad_info.family == CHIP_FIJI ||4692pipeline->device->physical_device->rad_info.family >= CHIP_POLARIS10)4693distribution_mode = V_028B6C_TRAPEZOIDS;4694else4695distribution_mode = V_028B6C_DONUTS;4696} else4697distribution_mode = V_028B6C_NO_DIST;46984699radeon_set_context_reg(ctx_cs, R_028B6C_VGT_TF_PARAM,4700S_028B6C_TYPE(type) | S_028B6C_PARTITIONING(partitioning) |4701S_028B6C_TOPOLOGY(topology) |4702S_028B6C_DISTRIBUTION_MODE(distribution_mode));4703}47044705static void4706radv_pipeline_generate_hw_gs(struct radeon_cmdbuf *ctx_cs, struct radeon_cmdbuf *cs,4707const struct radv_pipeline *pipeline,4708const struct radv_shader_variant *gs)4709{4710const struct gfx9_gs_info *gs_state = &gs->info.gs_ring_info;4711unsigned gs_max_out_vertices;4712const uint8_t *num_components;4713uint8_t max_stream;4714unsigned offset;4715uint64_t va;47164717gs_max_out_vertices = gs->info.gs.vertices_out;4718max_stream = gs->info.gs.max_stream;4719num_components = gs->info.gs.num_stream_output_components;47204721offset = num_components[0] * gs_max_out_vertices;47224723radeon_set_context_reg_seq(ctx_cs, R_028A60_VGT_GSVS_RING_OFFSET_1, 3);4724radeon_emit(ctx_cs, offset);4725if (max_stream >= 1)4726offset += num_components[1] * gs_max_out_vertices;4727radeon_emit(ctx_cs, offset);4728if (max_stream >= 2)4729offset += num_components[2] * gs_max_out_vertices;4730radeon_emit(ctx_cs, offset);4731if (max_stream >= 3)4732offset += num_components[3] * gs_max_out_vertices;4733radeon_set_context_reg(ctx_cs, R_028AB0_VGT_GSVS_RING_ITEMSIZE, offset);47344735radeon_set_context_reg_seq(ctx_cs, R_028B5C_VGT_GS_VERT_ITEMSIZE, 4);4736radeon_emit(ctx_cs, num_components[0]);4737radeon_emit(ctx_cs, (max_stream >= 1) ? num_components[1] : 0);4738radeon_emit(ctx_cs, (max_stream >= 2) ? num_components[2] : 0);4739radeon_emit(ctx_cs, (max_stream >= 3) ? num_components[3] : 0);47404741uint32_t gs_num_invocations = gs->info.gs.invocations;4742radeon_set_context_reg(4743ctx_cs, R_028B90_VGT_GS_INSTANCE_CNT,4744S_028B90_CNT(MIN2(gs_num_invocations, 127)) | S_028B90_ENABLE(gs_num_invocations > 0));47454746radeon_set_context_reg(ctx_cs, R_028AAC_VGT_ESGS_RING_ITEMSIZE,4747gs_state->vgt_esgs_ring_itemsize);47484749va = radv_buffer_get_va(gs->bo) + gs->bo_offset;47504751if (pipeline->device->physical_device->rad_info.chip_class >= GFX9) {4752if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {4753radeon_set_sh_reg_seq(cs, R_00B320_SPI_SHADER_PGM_LO_ES, 2);4754radeon_emit(cs, va >> 8);4755radeon_emit(cs, S_00B324_MEM_BASE(va >> 40));4756} else {4757radeon_set_sh_reg_seq(cs, R_00B210_SPI_SHADER_PGM_LO_ES, 2);4758radeon_emit(cs, va >> 8);4759radeon_emit(cs, S_00B214_MEM_BASE(va >> 40));4760}47614762radeon_set_sh_reg_seq(cs, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 2);4763radeon_emit(cs, gs->config.rsrc1);4764radeon_emit(cs, gs->config.rsrc2 | S_00B22C_LDS_SIZE(gs_state->lds_size));47654766radeon_set_context_reg(ctx_cs, R_028A44_VGT_GS_ONCHIP_CNTL, gs_state->vgt_gs_onchip_cntl);4767radeon_set_context_reg(ctx_cs, R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP,4768gs_state->vgt_gs_max_prims_per_subgroup);4769} else {4770radeon_set_sh_reg_seq(cs, R_00B220_SPI_SHADER_PGM_LO_GS, 4);4771radeon_emit(cs, va >> 8);4772radeon_emit(cs, S_00B224_MEM_BASE(va >> 40));4773radeon_emit(cs, gs->config.rsrc1);4774radeon_emit(cs, gs->config.rsrc2);4775}47764777radv_pipeline_generate_hw_vs(ctx_cs, cs, pipeline, pipeline->gs_copy_shader);4778}47794780static void4781radv_pipeline_generate_geometry_shader(struct radeon_cmdbuf *ctx_cs, struct radeon_cmdbuf *cs,4782const struct radv_pipeline *pipeline)4783{4784struct radv_shader_variant *gs;47854786gs = pipeline->shaders[MESA_SHADER_GEOMETRY];4787if (!gs)4788return;47894790if (gs->info.is_ngg)4791radv_pipeline_generate_hw_ngg(ctx_cs, cs, pipeline, gs);4792else4793radv_pipeline_generate_hw_gs(ctx_cs, cs, pipeline, gs);47944795radeon_set_context_reg(ctx_cs, R_028B38_VGT_GS_MAX_VERT_OUT, gs->info.gs.vertices_out);4796}47974798static uint32_t4799offset_to_ps_input(uint32_t offset, bool flat_shade, bool explicit, bool float16)4800{4801uint32_t ps_input_cntl;4802if (offset <= AC_EXP_PARAM_OFFSET_31) {4803ps_input_cntl = S_028644_OFFSET(offset);4804if (flat_shade || explicit)4805ps_input_cntl |= S_028644_FLAT_SHADE(1);4806if (explicit) {4807/* Force parameter cache to be read in passthrough4808* mode.4809*/4810ps_input_cntl |= S_028644_OFFSET(1 << 5);4811}4812if (float16) {4813ps_input_cntl |= S_028644_FP16_INTERP_MODE(1) | S_028644_ATTR0_VALID(1);4814}4815} else {4816/* The input is a DEFAULT_VAL constant. */4817assert(offset >= AC_EXP_PARAM_DEFAULT_VAL_0000 && offset <= AC_EXP_PARAM_DEFAULT_VAL_1111);4818offset -= AC_EXP_PARAM_DEFAULT_VAL_0000;4819ps_input_cntl = S_028644_OFFSET(0x20) | S_028644_DEFAULT_VAL(offset);4820}4821return ps_input_cntl;4822}48234824static void4825radv_pipeline_generate_ps_inputs(struct radeon_cmdbuf *ctx_cs, const struct radv_pipeline *pipeline)4826{4827struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];4828const struct radv_vs_output_info *outinfo = get_vs_output_info(pipeline);4829uint32_t ps_input_cntl[32];48304831unsigned ps_offset = 0;48324833if (ps->info.ps.prim_id_input) {4834unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID];4835if (vs_offset != AC_EXP_PARAM_UNDEFINED) {4836ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);4837++ps_offset;4838}4839}48404841if (ps->info.ps.layer_input || ps->info.needs_multiview_view_index) {4842unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_LAYER];4843if (vs_offset != AC_EXP_PARAM_UNDEFINED)4844ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);4845else4846ps_input_cntl[ps_offset] =4847offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000, true, false, false);4848++ps_offset;4849}48504851if (ps->info.ps.viewport_index_input) {4852unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VIEWPORT];4853if (vs_offset != AC_EXP_PARAM_UNDEFINED)4854ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true, false, false);4855else4856ps_input_cntl[ps_offset] =4857offset_to_ps_input(AC_EXP_PARAM_DEFAULT_VAL_0000, true, false, false);4858++ps_offset;4859}48604861if (ps->info.ps.has_pcoord) {4862unsigned val;4863val = S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);4864ps_input_cntl[ps_offset] = val;4865ps_offset++;4866}48674868if (ps->info.ps.num_input_clips_culls) {4869unsigned vs_offset;48704871vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST0];4872if (vs_offset != AC_EXP_PARAM_UNDEFINED) {4873ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false, false);4874++ps_offset;4875}48764877vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_CLIP_DIST1];4878if (vs_offset != AC_EXP_PARAM_UNDEFINED && ps->info.ps.num_input_clips_culls > 4) {4879ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, false, false, false);4880++ps_offset;4881}4882}48834884for (unsigned i = 0; i < 32 && (1u << i) <= ps->info.ps.input_mask; ++i) {4885unsigned vs_offset;4886bool flat_shade;4887bool explicit;4888bool float16;4889if (!(ps->info.ps.input_mask & (1u << i)))4890continue;48914892vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VAR0 + i];4893if (vs_offset == AC_EXP_PARAM_UNDEFINED) {4894ps_input_cntl[ps_offset] = S_028644_OFFSET(0x20);4895++ps_offset;4896continue;4897}48984899flat_shade = !!(ps->info.ps.flat_shaded_mask & (1u << ps_offset));4900explicit = !!(ps->info.ps.explicit_shaded_mask & (1u << ps_offset));4901float16 = !!(ps->info.ps.float16_shaded_mask & (1u << ps_offset));49024903ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, flat_shade, explicit, float16);4904++ps_offset;4905}49064907if (ps_offset) {4908radeon_set_context_reg_seq(ctx_cs, R_028644_SPI_PS_INPUT_CNTL_0, ps_offset);4909for (unsigned i = 0; i < ps_offset; i++) {4910radeon_emit(ctx_cs, ps_input_cntl[i]);4911}4912}4913}49144915static uint32_t4916radv_compute_db_shader_control(const struct radv_device *device,4917const struct radv_pipeline *pipeline,4918const struct radv_shader_variant *ps)4919{4920unsigned conservative_z_export = V_02880C_EXPORT_ANY_Z;4921unsigned z_order;4922if (ps->info.ps.early_fragment_test || !ps->info.ps.writes_memory)4923z_order = V_02880C_EARLY_Z_THEN_LATE_Z;4924else4925z_order = V_02880C_LATE_Z;49264927if (ps->info.ps.depth_layout == FRAG_DEPTH_LAYOUT_GREATER)4928conservative_z_export = V_02880C_EXPORT_GREATER_THAN_Z;4929else if (ps->info.ps.depth_layout == FRAG_DEPTH_LAYOUT_LESS)4930conservative_z_export = V_02880C_EXPORT_LESS_THAN_Z;49314932bool disable_rbplus = device->physical_device->rad_info.has_rbplus &&4933!device->physical_device->rad_info.rbplus_allowed;49344935/* It shouldn't be needed to export gl_SampleMask when MSAA is disabled4936* but this appears to break Project Cars (DXVK). See4937* https://bugs.freedesktop.org/show_bug.cgi?id=1094014938*/4939bool mask_export_enable = ps->info.ps.writes_sample_mask;49404941return S_02880C_Z_EXPORT_ENABLE(ps->info.ps.writes_z) |4942S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps->info.ps.writes_stencil) |4943S_02880C_KILL_ENABLE(!!ps->info.ps.can_discard) |4944S_02880C_MASK_EXPORT_ENABLE(mask_export_enable) |4945S_02880C_CONSERVATIVE_Z_EXPORT(conservative_z_export) | S_02880C_Z_ORDER(z_order) |4946S_02880C_DEPTH_BEFORE_SHADER(ps->info.ps.early_fragment_test) |4947S_02880C_PRE_SHADER_DEPTH_COVERAGE_ENABLE(ps->info.ps.post_depth_coverage) |4948S_02880C_EXEC_ON_HIER_FAIL(ps->info.ps.writes_memory) |4949S_02880C_EXEC_ON_NOOP(ps->info.ps.writes_memory) |4950S_02880C_DUAL_QUAD_DISABLE(disable_rbplus);4951}49524953static void4954radv_pipeline_generate_fragment_shader(struct radeon_cmdbuf *ctx_cs, struct radeon_cmdbuf *cs,4955struct radv_pipeline *pipeline)4956{4957struct radv_shader_variant *ps;4958uint64_t va;4959assert(pipeline->shaders[MESA_SHADER_FRAGMENT]);49604961ps = pipeline->shaders[MESA_SHADER_FRAGMENT];4962va = radv_buffer_get_va(ps->bo) + ps->bo_offset;49634964radeon_set_sh_reg_seq(cs, R_00B020_SPI_SHADER_PGM_LO_PS, 4);4965radeon_emit(cs, va >> 8);4966radeon_emit(cs, S_00B024_MEM_BASE(va >> 40));4967radeon_emit(cs, ps->config.rsrc1);4968radeon_emit(cs, ps->config.rsrc2);49694970radeon_set_context_reg(ctx_cs, R_02880C_DB_SHADER_CONTROL,4971radv_compute_db_shader_control(pipeline->device, pipeline, ps));49724973radeon_set_context_reg(ctx_cs, R_0286CC_SPI_PS_INPUT_ENA, ps->config.spi_ps_input_ena);49744975radeon_set_context_reg(ctx_cs, R_0286D0_SPI_PS_INPUT_ADDR, ps->config.spi_ps_input_addr);49764977radeon_set_context_reg(4978ctx_cs, R_0286D8_SPI_PS_IN_CONTROL,4979S_0286D8_NUM_INTERP(ps->info.ps.num_interp) | S_0286D8_PS_W32_EN(ps->info.wave_size == 32));49804981radeon_set_context_reg(ctx_cs, R_0286E0_SPI_BARYC_CNTL, pipeline->graphics.spi_baryc_cntl);49824983radeon_set_context_reg(4984ctx_cs, R_028710_SPI_SHADER_Z_FORMAT,4985ac_get_spi_shader_z_format(ps->info.ps.writes_z, ps->info.ps.writes_stencil,4986ps->info.ps.writes_sample_mask));4987}49884989static void4990radv_pipeline_generate_vgt_vertex_reuse(struct radeon_cmdbuf *ctx_cs,4991const struct radv_pipeline *pipeline)4992{4993if (pipeline->device->physical_device->rad_info.family < CHIP_POLARIS10 ||4994pipeline->device->physical_device->rad_info.chip_class >= GFX10)4995return;49964997unsigned vtx_reuse_depth = 30;4998if (radv_pipeline_has_tess(pipeline) &&4999radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.tes.spacing ==5000TESS_SPACING_FRACTIONAL_ODD) {5001vtx_reuse_depth = 14;5002}5003radeon_set_context_reg(ctx_cs, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL,5004S_028C58_VTX_REUSE_DEPTH(vtx_reuse_depth));5005}50065007static void5008radv_pipeline_generate_vgt_shader_config(struct radeon_cmdbuf *ctx_cs,5009const struct radv_pipeline *pipeline)5010{5011uint32_t stages = 0;5012if (radv_pipeline_has_tess(pipeline)) {5013stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) | S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);50145015if (radv_pipeline_has_gs(pipeline))5016stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) | S_028B54_GS_EN(1);5017else if (radv_pipeline_has_ngg(pipeline))5018stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS);5019else5020stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);5021} else if (radv_pipeline_has_gs(pipeline)) {5022stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) | S_028B54_GS_EN(1);5023} else if (radv_pipeline_has_ngg(pipeline)) {5024stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL);5025}50265027if (radv_pipeline_has_ngg(pipeline)) {5028stages |= S_028B54_PRIMGEN_EN(1);5029if (pipeline->streamout_shader)5030stages |= S_028B54_NGG_WAVE_ID_EN(1);5031if (radv_pipeline_has_ngg_passthrough(pipeline))5032stages |= S_028B54_PRIMGEN_PASSTHRU_EN(1);5033} else if (radv_pipeline_has_gs(pipeline)) {5034stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);5035}50365037if (pipeline->device->physical_device->rad_info.chip_class >= GFX9)5038stages |= S_028B54_MAX_PRIMGRP_IN_WAVE(2);50395040if (pipeline->device->physical_device->rad_info.chip_class >= GFX10) {5041uint8_t hs_size = 64, gs_size = 64, vs_size = 64;50425043if (radv_pipeline_has_tess(pipeline))5044hs_size = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.wave_size;50455046if (pipeline->shaders[MESA_SHADER_GEOMETRY]) {5047vs_size = gs_size = pipeline->shaders[MESA_SHADER_GEOMETRY]->info.wave_size;5048if (radv_pipeline_has_gs_copy_shader(pipeline))5049vs_size = pipeline->gs_copy_shader->info.wave_size;5050} else if (pipeline->shaders[MESA_SHADER_TESS_EVAL])5051vs_size = pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.wave_size;5052else if (pipeline->shaders[MESA_SHADER_VERTEX])5053vs_size = pipeline->shaders[MESA_SHADER_VERTEX]->info.wave_size;50545055if (radv_pipeline_has_ngg(pipeline)) {5056assert(!radv_pipeline_has_gs_copy_shader(pipeline));5057gs_size = vs_size;5058}50595060/* legacy GS only supports Wave64 */5061stages |= S_028B54_HS_W32_EN(hs_size == 32 ? 1 : 0) |5062S_028B54_GS_W32_EN(gs_size == 32 ? 1 : 0) |5063S_028B54_VS_W32_EN(vs_size == 32 ? 1 : 0);5064}50655066radeon_set_context_reg(ctx_cs, R_028B54_VGT_SHADER_STAGES_EN, stages);5067}50685069static void5070radv_pipeline_generate_cliprect_rule(struct radeon_cmdbuf *ctx_cs,5071const VkGraphicsPipelineCreateInfo *pCreateInfo)5072{5073const VkPipelineDiscardRectangleStateCreateInfoEXT *discard_rectangle_info =5074vk_find_struct_const(pCreateInfo->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);5075uint32_t cliprect_rule = 0;50765077if (!discard_rectangle_info) {5078cliprect_rule = 0xffff;5079} else {5080for (unsigned i = 0; i < (1u << MAX_DISCARD_RECTANGLES); ++i) {5081/* Interpret i as a bitmask, and then set the bit in5082* the mask if that combination of rectangles in which5083* the pixel is contained should pass the cliprect5084* test.5085*/5086unsigned relevant_subset = i & ((1u << discard_rectangle_info->discardRectangleCount) - 1);50875088if (discard_rectangle_info->discardRectangleMode ==5089VK_DISCARD_RECTANGLE_MODE_INCLUSIVE_EXT &&5090!relevant_subset)5091continue;50925093if (discard_rectangle_info->discardRectangleMode ==5094VK_DISCARD_RECTANGLE_MODE_EXCLUSIVE_EXT &&5095relevant_subset)5096continue;50975098cliprect_rule |= 1u << i;5099}5100}51015102radeon_set_context_reg(ctx_cs, R_02820C_PA_SC_CLIPRECT_RULE, cliprect_rule);5103}51045105static void5106gfx10_pipeline_generate_ge_cntl(struct radeon_cmdbuf *ctx_cs, struct radv_pipeline *pipeline)5107{5108bool break_wave_at_eoi = false;5109unsigned primgroup_size;5110unsigned vertgroup_size = 256; /* 256 = disable vertex grouping */51115112if (radv_pipeline_has_tess(pipeline)) {5113primgroup_size = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.num_tess_patches;5114} else if (radv_pipeline_has_gs(pipeline)) {5115const struct gfx9_gs_info *gs_state =5116&pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs_ring_info;5117unsigned vgt_gs_onchip_cntl = gs_state->vgt_gs_onchip_cntl;5118primgroup_size = G_028A44_GS_PRIMS_PER_SUBGRP(vgt_gs_onchip_cntl);5119} else {5120primgroup_size = 128; /* recommended without a GS and tess */5121}51225123if (radv_pipeline_has_tess(pipeline)) {5124if (pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.uses_prim_id ||5125radv_get_shader(pipeline, MESA_SHADER_TESS_EVAL)->info.uses_prim_id)5126break_wave_at_eoi = true;5127}51285129radeon_set_uconfig_reg(ctx_cs, R_03096C_GE_CNTL,5130S_03096C_PRIM_GRP_SIZE(primgroup_size) |5131S_03096C_VERT_GRP_SIZE(vertgroup_size) |5132S_03096C_PACKET_TO_ONE_PA(0) /* line stipple */ |5133S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi));5134}51355136static void5137radv_pipeline_generate_vgt_gs_out(struct radeon_cmdbuf *ctx_cs,5138const struct radv_pipeline *pipeline,5139const VkGraphicsPipelineCreateInfo *pCreateInfo,5140const struct radv_graphics_pipeline_create_info *extra)5141{5142uint32_t gs_out;51435144if (radv_pipeline_has_gs(pipeline)) {5145gs_out =5146si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs.output_prim);5147} else if (radv_pipeline_has_tess(pipeline)) {5148if (pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.point_mode) {5149gs_out = V_028A6C_POINTLIST;5150} else {5151gs_out = si_conv_gl_prim_to_gs_out(5152pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.primitive_mode);5153}5154} else {5155gs_out = si_conv_prim_to_gs_out(pCreateInfo->pInputAssemblyState->topology);5156}51575158if (extra && extra->use_rectlist) {5159gs_out = V_028A6C_TRISTRIP;5160if (radv_pipeline_has_ngg(pipeline))5161gs_out = V_028A6C_RECTLIST;5162}51635164radeon_set_context_reg(ctx_cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out);5165}51665167static bool5168gfx103_pipeline_vrs_coarse_shading(const struct radv_pipeline *pipeline)5169{5170struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];5171struct radv_device *device = pipeline->device;51725173if (device->instance->debug_flags & RADV_DEBUG_NO_VRS_FLAT_SHADING)5174return false;51755176if (!ps->info.ps.allow_flat_shading)5177return false;51785179return true;5180}51815182static void5183gfx103_pipeline_generate_vrs_state(struct radeon_cmdbuf *ctx_cs,5184const struct radv_pipeline *pipeline,5185const VkGraphicsPipelineCreateInfo *pCreateInfo)5186{5187uint32_t mode = V_028064_VRS_COMB_MODE_PASSTHRU;5188uint8_t rate_x = 0, rate_y = 0;5189bool enable_vrs = false;51905191if (vk_find_struct_const(pCreateInfo->pNext,5192PIPELINE_FRAGMENT_SHADING_RATE_STATE_CREATE_INFO_KHR) ||5193radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_FRAGMENT_SHADING_RATE_KHR)) {5194/* Enable draw call VRS because it's explicitly requested. */5195enable_vrs = true;5196} else if (gfx103_pipeline_vrs_coarse_shading(pipeline)) {5197/* Enable VRS coarse shading 2x2 if the driver determined that5198* it's safe to enable.5199*/5200mode = V_028064_VRS_COMB_MODE_OVERRIDE;5201rate_x = rate_y = 1;5202} else if (pipeline->device->force_vrs != RADV_FORCE_VRS_NONE) {5203/* Force enable vertex VRS if requested by the user. */5204radeon_set_context_reg(5205ctx_cs, R_028848_PA_CL_VRS_CNTL,5206S_028848_SAMPLE_ITER_COMBINER_MODE(V_028848_VRS_COMB_MODE_OVERRIDE) |5207S_028848_VERTEX_RATE_COMBINER_MODE(V_028848_VRS_COMB_MODE_OVERRIDE));52085209/* If the shader is using discard, turn off coarse shading5210* because discard at 2x2 pixel granularity degrades quality5211* too much. MIN allows sample shading but not coarse shading.5212*/5213struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];52145215mode = ps->info.ps.can_discard ? V_028064_VRS_COMB_MODE_MIN : V_028064_VRS_COMB_MODE_PASSTHRU;5216}52175218radeon_set_context_reg(ctx_cs, R_028A98_VGT_DRAW_PAYLOAD_CNTL, S_028A98_EN_VRS_RATE(enable_vrs));52195220radeon_set_context_reg(ctx_cs, R_028064_DB_VRS_OVERRIDE_CNTL,5221S_028064_VRS_OVERRIDE_RATE_COMBINER_MODE(mode) |5222S_028064_VRS_OVERRIDE_RATE_X(rate_x) |5223S_028064_VRS_OVERRIDE_RATE_Y(rate_y));5224}52255226static void5227radv_pipeline_generate_pm4(struct radv_pipeline *pipeline,5228const VkGraphicsPipelineCreateInfo *pCreateInfo,5229const struct radv_graphics_pipeline_create_info *extra,5230const struct radv_blend_state *blend)5231{5232struct radeon_cmdbuf *ctx_cs = &pipeline->ctx_cs;5233struct radeon_cmdbuf *cs = &pipeline->cs;52345235cs->max_dw = 64;5236ctx_cs->max_dw = 256;5237cs->buf = malloc(4 * (cs->max_dw + ctx_cs->max_dw));5238ctx_cs->buf = cs->buf + cs->max_dw;52395240radv_pipeline_generate_depth_stencil_state(ctx_cs, pipeline, pCreateInfo, extra);5241radv_pipeline_generate_blend_state(ctx_cs, pipeline, blend);5242radv_pipeline_generate_raster_state(ctx_cs, pipeline, pCreateInfo);5243radv_pipeline_generate_multisample_state(ctx_cs, pipeline);5244radv_pipeline_generate_vgt_gs_mode(ctx_cs, pipeline);5245radv_pipeline_generate_vertex_shader(ctx_cs, cs, pipeline);52465247if (radv_pipeline_has_tess(pipeline)) {5248radv_pipeline_generate_tess_shaders(ctx_cs, cs, pipeline);5249radv_pipeline_generate_tess_state(ctx_cs, pipeline, pCreateInfo);5250}52515252radv_pipeline_generate_geometry_shader(ctx_cs, cs, pipeline);5253radv_pipeline_generate_fragment_shader(ctx_cs, cs, pipeline);5254radv_pipeline_generate_ps_inputs(ctx_cs, pipeline);5255radv_pipeline_generate_vgt_vertex_reuse(ctx_cs, pipeline);5256radv_pipeline_generate_vgt_shader_config(ctx_cs, pipeline);5257radv_pipeline_generate_cliprect_rule(ctx_cs, pCreateInfo);5258radv_pipeline_generate_vgt_gs_out(ctx_cs, pipeline, pCreateInfo, extra);52595260if (pipeline->device->physical_device->rad_info.chip_class >= GFX10 &&5261!radv_pipeline_has_ngg(pipeline))5262gfx10_pipeline_generate_ge_cntl(ctx_cs, pipeline);52635264if (pipeline->device->physical_device->rad_info.chip_class >= GFX10_3)5265gfx103_pipeline_generate_vrs_state(ctx_cs, pipeline, pCreateInfo);52665267pipeline->ctx_cs_hash = _mesa_hash_data(ctx_cs->buf, ctx_cs->cdw * 4);52685269assert(ctx_cs->cdw <= ctx_cs->max_dw);5270assert(cs->cdw <= cs->max_dw);5271}52725273static void5274radv_pipeline_init_vertex_input_state(struct radv_pipeline *pipeline,5275const VkGraphicsPipelineCreateInfo *pCreateInfo)5276{5277const struct radv_shader_info *info = &radv_get_shader(pipeline, MESA_SHADER_VERTEX)->info;5278const VkPipelineVertexInputStateCreateInfo *vi_info = pCreateInfo->pVertexInputState;52795280for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {5281const VkVertexInputBindingDescription *desc = &vi_info->pVertexBindingDescriptions[i];52825283pipeline->binding_stride[desc->binding] = desc->stride;5284}52855286for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {5287const VkVertexInputAttributeDescription *desc = &vi_info->pVertexAttributeDescriptions[i];52885289uint32_t end = desc->offset + vk_format_get_blocksize(desc->format);5290pipeline->attrib_ends[desc->location] = end;5291if (pipeline->binding_stride[desc->binding])5292pipeline->attrib_index_offset[desc->location] =5293desc->offset / pipeline->binding_stride[desc->binding];5294pipeline->attrib_bindings[desc->location] = desc->binding;5295}52965297pipeline->use_per_attribute_vb_descs = info->vs.use_per_attribute_vb_descs;5298pipeline->vb_desc_usage_mask = info->vs.vb_desc_usage_mask;5299pipeline->vb_desc_alloc_size = util_bitcount(pipeline->vb_desc_usage_mask) * 16;5300}53015302static struct radv_shader_variant *5303radv_pipeline_get_streamout_shader(struct radv_pipeline *pipeline)5304{5305int i;53065307for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {5308struct radv_shader_variant *shader = radv_get_shader(pipeline, i);53095310if (shader && shader->info.so.num_outputs > 0)5311return shader;5312}53135314return NULL;5315}53165317static void5318radv_pipeline_init_shader_stages_state(struct radv_pipeline *pipeline)5319{5320struct radv_device *device = pipeline->device;53215322for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {5323pipeline->user_data_0[i] = radv_pipeline_stage_to_user_data_0(5324pipeline, i, device->physical_device->rad_info.chip_class);53255326if (pipeline->shaders[i]) {5327pipeline->need_indirect_descriptor_sets |=5328pipeline->shaders[i]->info.need_indirect_descriptor_sets;5329}5330}53315332struct radv_userdata_info *loc =5333radv_lookup_user_sgpr(pipeline, MESA_SHADER_VERTEX, AC_UD_VS_BASE_VERTEX_START_INSTANCE);5334if (loc->sgpr_idx != -1) {5335pipeline->graphics.vtx_base_sgpr = pipeline->user_data_0[MESA_SHADER_VERTEX];5336pipeline->graphics.vtx_base_sgpr += loc->sgpr_idx * 4;5337pipeline->graphics.vtx_emit_num = loc->num_sgprs;5338pipeline->graphics.uses_drawid =5339radv_get_shader(pipeline, MESA_SHADER_VERTEX)->info.vs.needs_draw_id;5340pipeline->graphics.uses_baseinstance =5341radv_get_shader(pipeline, MESA_SHADER_VERTEX)->info.vs.needs_base_instance;5342}5343}53445345static VkResult5346radv_pipeline_init(struct radv_pipeline *pipeline, struct radv_device *device,5347struct radv_pipeline_cache *cache,5348const VkGraphicsPipelineCreateInfo *pCreateInfo,5349const struct radv_graphics_pipeline_create_info *extra)5350{5351VkResult result;53525353pipeline->device = device;5354pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);5355pipeline->graphics.last_vgt_api_stage = MESA_SHADER_NONE;5356assert(pipeline->layout);53575358struct radv_blend_state blend = radv_pipeline_init_blend_state(pipeline, pCreateInfo, extra);53595360const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =5361vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);5362radv_init_feedback(creation_feedback);53635364VkPipelineCreationFeedbackEXT *pipeline_feedback =5365creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;53665367const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = {53680,5369};5370VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = {0};5371for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {5372gl_shader_stage stage = ffs(pCreateInfo->pStages[i].stage) - 1;5373pStages[stage] = &pCreateInfo->pStages[i];5374if (creation_feedback)5375stage_feedbacks[stage] = &creation_feedback->pPipelineStageCreationFeedbacks[i];5376}53775378struct radv_pipeline_key key =5379radv_generate_graphics_pipeline_key(pipeline, pCreateInfo, &blend);53805381result = radv_create_shaders(pipeline, device, cache, &key, pStages, pCreateInfo->flags,5382pipeline_feedback, stage_feedbacks);5383if (result != VK_SUCCESS)5384return result;53855386pipeline->graphics.spi_baryc_cntl = S_0286E0_FRONT_FACE_ALL_BITS(1);5387radv_pipeline_init_multisample_state(pipeline, &blend, pCreateInfo);5388radv_pipeline_init_input_assembly_state(pipeline, pCreateInfo, extra);5389radv_pipeline_init_dynamic_state(pipeline, pCreateInfo, extra);5390radv_pipeline_init_raster_state(pipeline, pCreateInfo);5391radv_pipeline_init_depth_stencil_state(pipeline, pCreateInfo);53925393if (pipeline->device->physical_device->rad_info.chip_class >= GFX10_3)5394gfx103_pipeline_init_vrs_state(pipeline, pCreateInfo);53955396/* Ensure that some export memory is always allocated, for two reasons:5397*5398* 1) Correctness: The hardware ignores the EXEC mask if no export5399* memory is allocated, so KILL and alpha test do not work correctly5400* without this.5401* 2) Performance: Every shader needs at least a NULL export, even when5402* it writes no color/depth output. The NULL export instruction5403* stalls without this setting.5404*5405* Don't add this to CB_SHADER_MASK.5406*5407* GFX10 supports pixel shaders without exports by setting both the5408* color and Z formats to SPI_SHADER_ZERO. The hw will skip export5409* instructions if any are present.5410*/5411struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];5412if ((pipeline->device->physical_device->rad_info.chip_class <= GFX9 ||5413ps->info.ps.can_discard) &&5414!blend.spi_shader_col_format) {5415if (!ps->info.ps.writes_z && !ps->info.ps.writes_stencil && !ps->info.ps.writes_sample_mask)5416blend.spi_shader_col_format = V_028714_SPI_SHADER_32_R;5417}54185419if (extra && (extra->custom_blend_mode == V_028808_CB_ELIMINATE_FAST_CLEAR ||5420extra->custom_blend_mode == V_028808_CB_FMASK_DECOMPRESS ||5421extra->custom_blend_mode == V_028808_CB_DCC_DECOMPRESS ||5422extra->custom_blend_mode == V_028808_CB_RESOLVE)) {5423/* According to the CB spec states, CB_SHADER_MASK should be5424* set to enable writes to all four channels of MRT0.5425*/5426blend.cb_shader_mask = 0xf;5427}54285429pipeline->graphics.col_format = blend.spi_shader_col_format;5430pipeline->graphics.cb_target_mask = blend.cb_target_mask;54315432if (radv_pipeline_has_gs(pipeline) && !radv_pipeline_has_ngg(pipeline)) {5433struct radv_shader_variant *gs = pipeline->shaders[MESA_SHADER_GEOMETRY];54345435radv_pipeline_init_gs_ring_state(pipeline, &gs->info.gs_ring_info);5436}54375438if (radv_pipeline_has_tess(pipeline)) {5439pipeline->graphics.tess_patch_control_points =5440pCreateInfo->pTessellationState->patchControlPoints;5441}54425443radv_pipeline_init_vertex_input_state(pipeline, pCreateInfo);5444radv_pipeline_init_binning_state(pipeline, pCreateInfo, &blend);5445radv_pipeline_init_shader_stages_state(pipeline);5446radv_pipeline_init_scratch(device, pipeline);54475448/* Find the last vertex shader stage that eventually uses streamout. */5449pipeline->streamout_shader = radv_pipeline_get_streamout_shader(pipeline);54505451pipeline->graphics.is_ngg = radv_pipeline_has_ngg(pipeline);5452pipeline->graphics.has_ngg_culling =5453pipeline->graphics.is_ngg &&5454pipeline->shaders[pipeline->graphics.last_vgt_api_stage]->info.has_ngg_culling;54555456radv_pipeline_generate_pm4(pipeline, pCreateInfo, extra, &blend);54575458return result;5459}54605461VkResult5462radv_graphics_pipeline_create(VkDevice _device, VkPipelineCache _cache,5463const VkGraphicsPipelineCreateInfo *pCreateInfo,5464const struct radv_graphics_pipeline_create_info *extra,5465const VkAllocationCallbacks *pAllocator, VkPipeline *pPipeline)5466{5467RADV_FROM_HANDLE(radv_device, device, _device);5468RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);5469struct radv_pipeline *pipeline;5470VkResult result;54715472pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,5473VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);5474if (pipeline == NULL)5475return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);54765477vk_object_base_init(&device->vk, &pipeline->base, VK_OBJECT_TYPE_PIPELINE);54785479result = radv_pipeline_init(pipeline, device, cache, pCreateInfo, extra);5480if (result != VK_SUCCESS) {5481radv_pipeline_destroy(device, pipeline, pAllocator);5482return result;5483}54845485*pPipeline = radv_pipeline_to_handle(pipeline);54865487return VK_SUCCESS;5488}54895490VkResult5491radv_CreateGraphicsPipelines(VkDevice _device, VkPipelineCache pipelineCache, uint32_t count,5492const VkGraphicsPipelineCreateInfo *pCreateInfos,5493const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines)5494{5495VkResult result = VK_SUCCESS;5496unsigned i = 0;54975498for (; i < count; i++) {5499VkResult r;5500r = radv_graphics_pipeline_create(_device, pipelineCache, &pCreateInfos[i], NULL, pAllocator,5501&pPipelines[i]);5502if (r != VK_SUCCESS) {5503result = r;5504pPipelines[i] = VK_NULL_HANDLE;55055506if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)5507break;5508}5509}55105511for (; i < count; ++i)5512pPipelines[i] = VK_NULL_HANDLE;55135514return result;5515}55165517static void5518radv_pipeline_generate_hw_cs(struct radeon_cmdbuf *cs, const struct radv_pipeline *pipeline)5519{5520struct radv_shader_variant *shader = pipeline->shaders[MESA_SHADER_COMPUTE];5521uint64_t va = radv_buffer_get_va(shader->bo) + shader->bo_offset;5522struct radv_device *device = pipeline->device;55235524radeon_set_sh_reg_seq(cs, R_00B830_COMPUTE_PGM_LO, 2);5525radeon_emit(cs, va >> 8);5526radeon_emit(cs, S_00B834_DATA(va >> 40));55275528radeon_set_sh_reg_seq(cs, R_00B848_COMPUTE_PGM_RSRC1, 2);5529radeon_emit(cs, shader->config.rsrc1);5530radeon_emit(cs, shader->config.rsrc2);5531if (device->physical_device->rad_info.chip_class >= GFX10) {5532radeon_set_sh_reg(cs, R_00B8A0_COMPUTE_PGM_RSRC3, shader->config.rsrc3);5533}5534}55355536static void5537radv_pipeline_generate_compute_state(struct radeon_cmdbuf *cs, const struct radv_pipeline *pipeline)5538{5539struct radv_shader_variant *shader = pipeline->shaders[MESA_SHADER_COMPUTE];5540struct radv_device *device = pipeline->device;5541unsigned threads_per_threadgroup;5542unsigned threadgroups_per_cu = 1;5543unsigned waves_per_threadgroup;5544unsigned max_waves_per_sh = 0;55455546/* Calculate best compute resource limits. */5547threads_per_threadgroup =5548shader->info.cs.block_size[0] * shader->info.cs.block_size[1] * shader->info.cs.block_size[2];5549waves_per_threadgroup = DIV_ROUND_UP(threads_per_threadgroup, shader->info.wave_size);55505551if (device->physical_device->rad_info.chip_class >= GFX10 && waves_per_threadgroup == 1)5552threadgroups_per_cu = 2;55535554radeon_set_sh_reg(5555cs, R_00B854_COMPUTE_RESOURCE_LIMITS,5556ac_get_compute_resource_limits(&device->physical_device->rad_info, waves_per_threadgroup,5557max_waves_per_sh, threadgroups_per_cu));55585559radeon_set_sh_reg_seq(cs, R_00B81C_COMPUTE_NUM_THREAD_X, 3);5560radeon_emit(cs, S_00B81C_NUM_THREAD_FULL(shader->info.cs.block_size[0]));5561radeon_emit(cs, S_00B81C_NUM_THREAD_FULL(shader->info.cs.block_size[1]));5562radeon_emit(cs, S_00B81C_NUM_THREAD_FULL(shader->info.cs.block_size[2]));5563}55645565static void5566radv_compute_generate_pm4(struct radv_pipeline *pipeline)5567{5568struct radv_device *device = pipeline->device;5569struct radeon_cmdbuf *cs = &pipeline->cs;55705571cs->max_dw = device->physical_device->rad_info.chip_class >= GFX10 ? 19 : 16;5572cs->buf = malloc(cs->max_dw * 4);55735574radv_pipeline_generate_hw_cs(cs, pipeline);5575radv_pipeline_generate_compute_state(cs, pipeline);55765577assert(pipeline->cs.cdw <= pipeline->cs.max_dw);5578}55795580static struct radv_pipeline_key5581radv_generate_compute_pipeline_key(struct radv_pipeline *pipeline,5582const VkComputePipelineCreateInfo *pCreateInfo)5583{5584const VkPipelineShaderStageCreateInfo *stage = &pCreateInfo->stage;5585struct radv_pipeline_key key;5586memset(&key, 0, sizeof(key));55875588if (pCreateInfo->flags & VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT)5589key.optimisations_disabled = 1;55905591const VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT *subgroup_size =5592vk_find_struct_const(stage->pNext,5593PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO_EXT);55945595if (subgroup_size) {5596assert(subgroup_size->requiredSubgroupSize == 32 ||5597subgroup_size->requiredSubgroupSize == 64);5598key.compute_subgroup_size = subgroup_size->requiredSubgroupSize;5599} else if (stage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) {5600key.require_full_subgroups = true;5601}56025603return key;5604}56055606static VkResult5607radv_compute_pipeline_create(VkDevice _device, VkPipelineCache _cache,5608const VkComputePipelineCreateInfo *pCreateInfo,5609const VkAllocationCallbacks *pAllocator, VkPipeline *pPipeline)5610{5611RADV_FROM_HANDLE(radv_device, device, _device);5612RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);5613const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = {56140,5615};5616VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = {0};5617struct radv_pipeline *pipeline;5618VkResult result;56195620pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,5621VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);5622if (pipeline == NULL)5623return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);56245625vk_object_base_init(&device->vk, &pipeline->base, VK_OBJECT_TYPE_PIPELINE);56265627pipeline->device = device;5628pipeline->graphics.last_vgt_api_stage = MESA_SHADER_NONE;5629pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);5630assert(pipeline->layout);56315632const VkPipelineCreationFeedbackCreateInfoEXT *creation_feedback =5633vk_find_struct_const(pCreateInfo->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);5634radv_init_feedback(creation_feedback);56355636VkPipelineCreationFeedbackEXT *pipeline_feedback =5637creation_feedback ? creation_feedback->pPipelineCreationFeedback : NULL;5638if (creation_feedback)5639stage_feedbacks[MESA_SHADER_COMPUTE] = &creation_feedback->pPipelineStageCreationFeedbacks[0];56405641pStages[MESA_SHADER_COMPUTE] = &pCreateInfo->stage;56425643struct radv_pipeline_key key = radv_generate_compute_pipeline_key(pipeline, pCreateInfo);56445645result = radv_create_shaders(pipeline, device, cache, &key, pStages, pCreateInfo->flags,5646pipeline_feedback, stage_feedbacks);5647if (result != VK_SUCCESS) {5648radv_pipeline_destroy(device, pipeline, pAllocator);5649return result;5650}56515652pipeline->user_data_0[MESA_SHADER_COMPUTE] = radv_pipeline_stage_to_user_data_0(5653pipeline, MESA_SHADER_COMPUTE, device->physical_device->rad_info.chip_class);5654pipeline->need_indirect_descriptor_sets |=5655pipeline->shaders[MESA_SHADER_COMPUTE]->info.need_indirect_descriptor_sets;5656radv_pipeline_init_scratch(device, pipeline);56575658radv_compute_generate_pm4(pipeline);56595660*pPipeline = radv_pipeline_to_handle(pipeline);56615662return VK_SUCCESS;5663}56645665VkResult5666radv_CreateComputePipelines(VkDevice _device, VkPipelineCache pipelineCache, uint32_t count,5667const VkComputePipelineCreateInfo *pCreateInfos,5668const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines)5669{5670VkResult result = VK_SUCCESS;56715672unsigned i = 0;5673for (; i < count; i++) {5674VkResult r;5675r = radv_compute_pipeline_create(_device, pipelineCache, &pCreateInfos[i], pAllocator,5676&pPipelines[i]);5677if (r != VK_SUCCESS) {5678result = r;5679pPipelines[i] = VK_NULL_HANDLE;56805681if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)5682break;5683}5684}56855686for (; i < count; ++i)5687pPipelines[i] = VK_NULL_HANDLE;56885689return result;5690}56915692static uint32_t5693radv_get_executable_count(const struct radv_pipeline *pipeline)5694{5695uint32_t ret = 0;5696for (int i = 0; i < MESA_SHADER_STAGES; ++i) {5697if (!pipeline->shaders[i])5698continue;56995700if (i == MESA_SHADER_GEOMETRY && !radv_pipeline_has_ngg(pipeline)) {5701ret += 2u;5702} else {5703ret += 1u;5704}5705}5706return ret;5707}57085709static struct radv_shader_variant *5710radv_get_shader_from_executable_index(const struct radv_pipeline *pipeline, int index,5711gl_shader_stage *stage)5712{5713for (int i = 0; i < MESA_SHADER_STAGES; ++i) {5714if (!pipeline->shaders[i])5715continue;5716if (!index) {5717*stage = i;5718return pipeline->shaders[i];5719}57205721--index;57225723if (i == MESA_SHADER_GEOMETRY && !radv_pipeline_has_ngg(pipeline)) {5724if (!index) {5725*stage = i;5726return pipeline->gs_copy_shader;5727}5728--index;5729}5730}57315732*stage = -1;5733return NULL;5734}57355736/* Basically strlcpy (which does not exist on linux) specialized for5737* descriptions. */5738static void5739desc_copy(char *desc, const char *src)5740{5741int len = strlen(src);5742assert(len < VK_MAX_DESCRIPTION_SIZE);5743memcpy(desc, src, len);5744memset(desc + len, 0, VK_MAX_DESCRIPTION_SIZE - len);5745}57465747VkResult5748radv_GetPipelineExecutablePropertiesKHR(VkDevice _device, const VkPipelineInfoKHR *pPipelineInfo,5749uint32_t *pExecutableCount,5750VkPipelineExecutablePropertiesKHR *pProperties)5751{5752RADV_FROM_HANDLE(radv_pipeline, pipeline, pPipelineInfo->pipeline);5753const uint32_t total_count = radv_get_executable_count(pipeline);57545755if (!pProperties) {5756*pExecutableCount = total_count;5757return VK_SUCCESS;5758}57595760const uint32_t count = MIN2(total_count, *pExecutableCount);5761for (unsigned i = 0, executable_idx = 0; i < MESA_SHADER_STAGES && executable_idx < count; ++i) {5762if (!pipeline->shaders[i])5763continue;5764pProperties[executable_idx].stages = mesa_to_vk_shader_stage(i);5765const char *name = NULL;5766const char *description = NULL;5767switch (i) {5768case MESA_SHADER_VERTEX:5769name = "Vertex Shader";5770description = "Vulkan Vertex Shader";5771break;5772case MESA_SHADER_TESS_CTRL:5773if (!pipeline->shaders[MESA_SHADER_VERTEX]) {5774pProperties[executable_idx].stages |= VK_SHADER_STAGE_VERTEX_BIT;5775name = "Vertex + Tessellation Control Shaders";5776description = "Combined Vulkan Vertex and Tessellation Control Shaders";5777} else {5778name = "Tessellation Control Shader";5779description = "Vulkan Tessellation Control Shader";5780}5781break;5782case MESA_SHADER_TESS_EVAL:5783name = "Tessellation Evaluation Shader";5784description = "Vulkan Tessellation Evaluation Shader";5785break;5786case MESA_SHADER_GEOMETRY:5787if (radv_pipeline_has_tess(pipeline) && !pipeline->shaders[MESA_SHADER_TESS_EVAL]) {5788pProperties[executable_idx].stages |= VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;5789name = "Tessellation Evaluation + Geometry Shaders";5790description = "Combined Vulkan Tessellation Evaluation and Geometry Shaders";5791} else if (!radv_pipeline_has_tess(pipeline) && !pipeline->shaders[MESA_SHADER_VERTEX]) {5792pProperties[executable_idx].stages |= VK_SHADER_STAGE_VERTEX_BIT;5793name = "Vertex + Geometry Shader";5794description = "Combined Vulkan Vertex and Geometry Shaders";5795} else {5796name = "Geometry Shader";5797description = "Vulkan Geometry Shader";5798}5799break;5800case MESA_SHADER_FRAGMENT:5801name = "Fragment Shader";5802description = "Vulkan Fragment Shader";5803break;5804case MESA_SHADER_COMPUTE:5805name = "Compute Shader";5806description = "Vulkan Compute Shader";5807break;5808}58095810pProperties[executable_idx].subgroupSize = pipeline->shaders[i]->info.wave_size;5811desc_copy(pProperties[executable_idx].name, name);5812desc_copy(pProperties[executable_idx].description, description);58135814++executable_idx;5815if (i == MESA_SHADER_GEOMETRY && !radv_pipeline_has_ngg(pipeline)) {5816assert(pipeline->gs_copy_shader);5817if (executable_idx >= count)5818break;58195820pProperties[executable_idx].stages = VK_SHADER_STAGE_GEOMETRY_BIT;5821pProperties[executable_idx].subgroupSize = 64;5822desc_copy(pProperties[executable_idx].name, "GS Copy Shader");5823desc_copy(pProperties[executable_idx].description,5824"Extra shader stage that loads the GS output ringbuffer into the rasterizer");58255826++executable_idx;5827}5828}58295830VkResult result = *pExecutableCount < total_count ? VK_INCOMPLETE : VK_SUCCESS;5831*pExecutableCount = count;5832return result;5833}58345835VkResult5836radv_GetPipelineExecutableStatisticsKHR(VkDevice _device,5837const VkPipelineExecutableInfoKHR *pExecutableInfo,5838uint32_t *pStatisticCount,5839VkPipelineExecutableStatisticKHR *pStatistics)5840{5841RADV_FROM_HANDLE(radv_device, device, _device);5842RADV_FROM_HANDLE(radv_pipeline, pipeline, pExecutableInfo->pipeline);5843gl_shader_stage stage;5844struct radv_shader_variant *shader =5845radv_get_shader_from_executable_index(pipeline, pExecutableInfo->executableIndex, &stage);58465847enum chip_class chip_class = device->physical_device->rad_info.chip_class;5848unsigned lds_increment = chip_class >= GFX7 ? 512 : 256;5849unsigned max_waves = radv_get_max_waves(device, shader, stage);58505851VkPipelineExecutableStatisticKHR *s = pStatistics;5852VkPipelineExecutableStatisticKHR *end = s + (pStatistics ? *pStatisticCount : 0);5853VkResult result = VK_SUCCESS;58545855if (s < end) {5856desc_copy(s->name, "SGPRs");5857desc_copy(s->description, "Number of SGPR registers allocated per subgroup");5858s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5859s->value.u64 = shader->config.num_sgprs;5860}5861++s;58625863if (s < end) {5864desc_copy(s->name, "VGPRs");5865desc_copy(s->description, "Number of VGPR registers allocated per subgroup");5866s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5867s->value.u64 = shader->config.num_vgprs;5868}5869++s;58705871if (s < end) {5872desc_copy(s->name, "Spilled SGPRs");5873desc_copy(s->description, "Number of SGPR registers spilled per subgroup");5874s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5875s->value.u64 = shader->config.spilled_sgprs;5876}5877++s;58785879if (s < end) {5880desc_copy(s->name, "Spilled VGPRs");5881desc_copy(s->description, "Number of VGPR registers spilled per subgroup");5882s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5883s->value.u64 = shader->config.spilled_vgprs;5884}5885++s;58865887if (s < end) {5888desc_copy(s->name, "PrivMem VGPRs");5889desc_copy(s->description, "Number of VGPRs stored in private memory per subgroup");5890s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5891s->value.u64 = shader->info.private_mem_vgprs;5892}5893++s;58945895if (s < end) {5896desc_copy(s->name, "Code size");5897desc_copy(s->description, "Code size in bytes");5898s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5899s->value.u64 = shader->exec_size;5900}5901++s;59025903if (s < end) {5904desc_copy(s->name, "LDS size");5905desc_copy(s->description, "LDS size in bytes per workgroup");5906s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5907s->value.u64 = shader->config.lds_size * lds_increment;5908}5909++s;59105911if (s < end) {5912desc_copy(s->name, "Scratch size");5913desc_copy(s->description, "Private memory in bytes per subgroup");5914s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5915s->value.u64 = shader->config.scratch_bytes_per_wave;5916}5917++s;59185919if (s < end) {5920desc_copy(s->name, "Subgroups per SIMD");5921desc_copy(s->description, "The maximum number of subgroups in flight on a SIMD unit");5922s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5923s->value.u64 = max_waves;5924}5925++s;59265927if (shader->statistics) {5928for (unsigned i = 0; i < aco_num_statistics; i++) {5929const struct aco_compiler_statistic_info *info = &aco_statistic_infos[i];5930if (s < end) {5931desc_copy(s->name, info->name);5932desc_copy(s->description, info->desc);5933s->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;5934s->value.u64 = shader->statistics[i];5935}5936++s;5937}5938}59395940if (!pStatistics)5941*pStatisticCount = s - pStatistics;5942else if (s > end) {5943*pStatisticCount = end - pStatistics;5944result = VK_INCOMPLETE;5945} else {5946*pStatisticCount = s - pStatistics;5947}59485949return result;5950}59515952static VkResult5953radv_copy_representation(void *data, size_t *data_size, const char *src)5954{5955size_t total_size = strlen(src) + 1;59565957if (!data) {5958*data_size = total_size;5959return VK_SUCCESS;5960}59615962size_t size = MIN2(total_size, *data_size);59635964memcpy(data, src, size);5965if (size)5966*((char *)data + size - 1) = 0;5967return size < total_size ? VK_INCOMPLETE : VK_SUCCESS;5968}59695970VkResult5971radv_GetPipelineExecutableInternalRepresentationsKHR(5972VkDevice device, const VkPipelineExecutableInfoKHR *pExecutableInfo,5973uint32_t *pInternalRepresentationCount,5974VkPipelineExecutableInternalRepresentationKHR *pInternalRepresentations)5975{5976RADV_FROM_HANDLE(radv_pipeline, pipeline, pExecutableInfo->pipeline);5977gl_shader_stage stage;5978struct radv_shader_variant *shader =5979radv_get_shader_from_executable_index(pipeline, pExecutableInfo->executableIndex, &stage);59805981VkPipelineExecutableInternalRepresentationKHR *p = pInternalRepresentations;5982VkPipelineExecutableInternalRepresentationKHR *end =5983p + (pInternalRepresentations ? *pInternalRepresentationCount : 0);5984VkResult result = VK_SUCCESS;5985/* optimized NIR */5986if (p < end) {5987p->isText = true;5988desc_copy(p->name, "NIR Shader(s)");5989desc_copy(p->description, "The optimized NIR shader(s)");5990if (radv_copy_representation(p->pData, &p->dataSize, shader->nir_string) != VK_SUCCESS)5991result = VK_INCOMPLETE;5992}5993++p;59945995/* backend IR */5996if (p < end) {5997p->isText = true;5998if (radv_use_llvm_for_stage(pipeline->device, stage)) {5999desc_copy(p->name, "LLVM IR");6000desc_copy(p->description, "The LLVM IR after some optimizations");6001} else {6002desc_copy(p->name, "ACO IR");6003desc_copy(p->description, "The ACO IR after some optimizations");6004}6005if (radv_copy_representation(p->pData, &p->dataSize, shader->ir_string) != VK_SUCCESS)6006result = VK_INCOMPLETE;6007}6008++p;60096010/* Disassembler */6011if (p < end) {6012p->isText = true;6013desc_copy(p->name, "Assembly");6014desc_copy(p->description, "Final Assembly");6015if (radv_copy_representation(p->pData, &p->dataSize, shader->disasm_string) != VK_SUCCESS)6016result = VK_INCOMPLETE;6017}6018++p;60196020if (!pInternalRepresentations)6021*pInternalRepresentationCount = p - pInternalRepresentations;6022else if (p > end) {6023result = VK_INCOMPLETE;6024*pInternalRepresentationCount = end - pInternalRepresentations;6025} else {6026*pInternalRepresentationCount = p - pInternalRepresentations;6027}60286029return result;6030}603160326033