Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_shader.h
4570 views
/*1* Copyright 2012 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324/* The compiler middle-end architecture: Explaining (non-)monolithic shaders25* -------------------------------------------------------------------------26*27* Typically, there is one-to-one correspondence between API and HW shaders,28* that is, for every API shader, there is exactly one shader binary in29* the driver.30*31* The problem with that is that we also have to emulate some API states32* (e.g. alpha-test, and many others) in shaders too. The two obvious ways33* to deal with it are:34* - each shader has multiple variants for each combination of emulated states,35* and the variants are compiled on demand, possibly relying on a shader36* cache for good performance37* - patch shaders at the binary level38*39* This driver uses something completely different. The emulated states are40* usually implemented at the beginning or end of shaders. Therefore, we can41* split the shader into 3 parts:42* - prolog part (shader code dependent on states)43* - main part (the API shader)44* - epilog part (shader code dependent on states)45*46* Each part is compiled as a separate shader and the final binaries are47* concatenated. This type of shader is called non-monolithic, because it48* consists of multiple independent binaries. Creating a new shader variant49* is therefore only a concatenation of shader parts (binaries) and doesn't50* involve any compilation. The main shader parts are the only parts that are51* compiled when applications create shader objects. The prolog and epilog52* parts are compiled on the first use and saved, so that their binaries can53* be reused by many other shaders.54*55* One of the roles of the prolog part is to compute vertex buffer addresses56* for vertex shaders. A few of the roles of the epilog part are color buffer57* format conversions in pixel shaders that we have to do manually, and write58* tessellation factors in tessellation control shaders. The prolog and epilog59* have many other important responsibilities in various shader stages.60* They don't just "emulate legacy stuff".61*62* Monolithic shaders are shaders where the parts are combined before LLVM63* compilation, and the whole thing is compiled and optimized as one unit with64* one binary on the output. The result is the same as the non-monolithic65* shader, but the final code can be better, because LLVM can optimize across66* all shader parts. Monolithic shaders aren't usually used except for these67* special cases:68*69* 1) Some rarely-used states require modification of the main shader part70* itself, and in such cases, only the monolithic shader variant is71* compiled, and that's always done on the first use.72*73* 2) When we do cross-stage optimizations for separate shader objects and74* e.g. eliminate unused shader varyings, the resulting optimized shader75* variants are always compiled as monolithic shaders, and always76* asynchronously (i.e. not stalling ongoing rendering). We call them77* "optimized monolithic" shaders. The important property here is that78* the non-monolithic unoptimized shader variant is always available for use79* when the asynchronous compilation of the optimized shader is not done80* yet.81*82* Starting with GFX9 chips, some shader stages are merged, and the number of83* shader parts per shader increased. The complete new list of shader parts is:84* - 1st shader: prolog part85* - 1st shader: main part86* - 2nd shader: prolog part87* - 2nd shader: main part88* - 2nd shader: epilog part89*/9091/* How linking shader inputs and outputs between vertex, tessellation, and92* geometry shaders works.93*94* Inputs and outputs between shaders are stored in a buffer. This buffer95* lives in LDS (typical case for tessellation), but it can also live96* in memory (ESGS). Each input or output has a fixed location within a vertex.97* The highest used input or output determines the stride between vertices.98*99* Since GS and tessellation are only possible in the OpenGL core profile,100* only these semantics are valid for per-vertex data:101*102* Name Location103*104* POSITION 0105* PSIZE 1106* CLIPDIST0..1 2..3107* CULLDIST0..1 (not implemented)108* GENERIC0..31 4..35109*110* For example, a shader only writing GENERIC0 has the output stride of 5.111*112* Only these semantics are valid for per-patch data:113*114* Name Location115*116* TESSOUTER 0117* TESSINNER 1118* PATCH0..29 2..31119*120* That's how independent shaders agree on input and output locations.121* The si_shader_io_get_unique_index function assigns the locations.122*123* For tessellation, other required information for calculating the input and124* output addresses like the vertex stride, the patch stride, and the offsets125* where per-vertex and per-patch data start, is passed to the shader via126* user data SGPRs. The offsets and strides are calculated at draw time and127* aren't available at compile time.128*/129130#ifndef SI_SHADER_H131#define SI_SHADER_H132133#include "ac_binary.h"134#include "ac_llvm_build.h"135#include "ac_llvm_util.h"136#include "util/simple_mtx.h"137#include "util/u_inlines.h"138#include "util/u_live_shader_cache.h"139#include "util/u_queue.h"140141#include <stdio.h>142143#ifdef __cplusplus144extern "C" {145#endif146147// Use LDS symbols when supported by LLVM. Can be disabled for testing the old148// path on newer LLVM for now. Should be removed in the long term.149#define USE_LDS_SYMBOLS (true)150151struct nir_shader;152struct si_shader;153struct si_context;154155#define SI_MAX_ATTRIBS 16156#define SI_MAX_VS_OUTPUTS 40157158#define SI_NGG_PRIM_EDGE_FLAG_BITS ((1 << 9) | (1 << 19) | (1 << 29))159160/* SGPR user data indices */161enum162{163SI_SGPR_INTERNAL_BINDINGS,164SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES,165SI_SGPR_CONST_AND_SHADER_BUFFERS, /* or just a constant buffer 0 pointer */166SI_SGPR_SAMPLERS_AND_IMAGES,167SI_NUM_RESOURCE_SGPRS,168169/* API VS, TES without GS, GS copy shader */170SI_SGPR_VS_STATE_BITS = SI_NUM_RESOURCE_SGPRS,171SI_NUM_VS_STATE_RESOURCE_SGPRS,172173/* all VS variants */174SI_SGPR_BASE_VERTEX = SI_NUM_VS_STATE_RESOURCE_SGPRS,175SI_SGPR_DRAWID,176SI_SGPR_START_INSTANCE,177SI_VS_NUM_USER_SGPR,178179SI_SGPR_VS_BLIT_DATA = SI_SGPR_CONST_AND_SHADER_BUFFERS,180181/* TES */182SI_SGPR_TES_OFFCHIP_LAYOUT = SI_NUM_VS_STATE_RESOURCE_SGPRS,183SI_SGPR_TES_OFFCHIP_ADDR,184SI_TES_NUM_USER_SGPR,185186/* GFX6-8: TCS only */187GFX6_SGPR_TCS_OFFCHIP_LAYOUT = SI_NUM_RESOURCE_SGPRS,188GFX6_SGPR_TCS_OUT_OFFSETS,189GFX6_SGPR_TCS_OUT_LAYOUT,190GFX6_SGPR_TCS_IN_LAYOUT,191GFX6_TCS_NUM_USER_SGPR,192193/* GFX9: Merged shaders. */194/* 2ND_CONST_AND_SHADER_BUFFERS is set in USER_DATA_ADDR_LO (SGPR0). */195/* 2ND_SAMPLERS_AND_IMAGES is set in USER_DATA_ADDR_HI (SGPR1). */196GFX9_MERGED_NUM_USER_SGPR = SI_VS_NUM_USER_SGPR,197198/* GFX9: Merged LS-HS (VS-TCS) only. */199GFX9_SGPR_TCS_OFFCHIP_LAYOUT = GFX9_MERGED_NUM_USER_SGPR,200GFX9_SGPR_TCS_OUT_OFFSETS,201GFX9_SGPR_TCS_OUT_LAYOUT,202GFX9_TCS_NUM_USER_SGPR,203204/* GS limits */205GFX6_GS_NUM_USER_SGPR = SI_NUM_RESOURCE_SGPRS,206GFX9_VSGS_NUM_USER_SGPR = SI_VS_NUM_USER_SGPR,207GFX9_TESGS_NUM_USER_SGPR = SI_TES_NUM_USER_SGPR,208SI_GSCOPY_NUM_USER_SGPR = SI_NUM_VS_STATE_RESOURCE_SGPRS,209210/* PS only */211SI_SGPR_ALPHA_REF = SI_NUM_RESOURCE_SGPRS,212SI_PS_NUM_USER_SGPR,213214/* The value has to be 12, because the hw requires that descriptors215* are aligned to 4 SGPRs.216*/217SI_SGPR_VS_VB_DESCRIPTOR_FIRST = 12,218};219220/* LLVM function parameter indices */221enum222{223SI_NUM_RESOURCE_PARAMS = 4,224225/* PS only parameters */226SI_PARAM_ALPHA_REF = SI_NUM_RESOURCE_PARAMS,227SI_PARAM_PRIM_MASK,228SI_PARAM_PERSP_SAMPLE,229SI_PARAM_PERSP_CENTER,230SI_PARAM_PERSP_CENTROID,231SI_PARAM_PERSP_PULL_MODEL,232SI_PARAM_LINEAR_SAMPLE,233SI_PARAM_LINEAR_CENTER,234SI_PARAM_LINEAR_CENTROID,235SI_PARAM_LINE_STIPPLE_TEX,236SI_PARAM_POS_X_FLOAT,237SI_PARAM_POS_Y_FLOAT,238SI_PARAM_POS_Z_FLOAT,239SI_PARAM_POS_W_FLOAT,240SI_PARAM_FRONT_FACE,241SI_PARAM_ANCILLARY,242SI_PARAM_SAMPLE_COVERAGE,243SI_PARAM_POS_FIXED_PT,244245SI_NUM_PARAMS = SI_PARAM_POS_FIXED_PT + 9, /* +8 for COLOR[0..1] */246};247248/* Fields of driver-defined VS state SGPR. */249#define S_VS_STATE_CLAMP_VERTEX_COLOR(x) (((unsigned)(x)&0x1) << 0)250#define C_VS_STATE_CLAMP_VERTEX_COLOR 0xFFFFFFFE251#define S_VS_STATE_INDEXED(x) (((unsigned)(x)&0x1) << 1)252#define C_VS_STATE_INDEXED 0xFFFFFFFD253#define S_VS_STATE_OUTPRIM(x) (((unsigned)(x)&0x3) << 2)254#define C_VS_STATE_OUTPRIM 0xFFFFFFF3255#define S_VS_STATE_PROVOKING_VTX_INDEX(x) (((unsigned)(x)&0x3) << 4)256#define C_VS_STATE_PROVOKING_VTX_INDEX 0xFFFFFFCF257#define S_VS_STATE_STREAMOUT_QUERY_ENABLED(x) (((unsigned)(x)&0x1) << 6)258#define C_VS_STATE_STREAMOUT_QUERY_ENABLED 0xFFFFFFBF259#define S_VS_STATE_SMALL_PRIM_PRECISION(x) (((unsigned)(x)&0xF) << 7)260#define C_VS_STATE_SMALL_PRIM_PRECISION 0xFFFFF87F261#define S_VS_STATE_LS_OUT_PATCH_SIZE(x) (((unsigned)(x)&0x1FFF) << 11)262#define C_VS_STATE_LS_OUT_PATCH_SIZE 0xFF0007FF263#define S_VS_STATE_LS_OUT_VERTEX_SIZE(x) (((unsigned)(x)&0xFF) << 24)264#define C_VS_STATE_LS_OUT_VERTEX_SIZE 0x00FFFFFF265266enum267{268/* These represent the number of SGPRs the shader uses. */269SI_VS_BLIT_SGPRS_POS = 3,270SI_VS_BLIT_SGPRS_POS_COLOR = 7,271SI_VS_BLIT_SGPRS_POS_TEXCOORD = 9,272};273274#define SI_NGG_CULL_VIEW_SMALLPRIMS (1 << 0) /* view.xy + small prims */275#define SI_NGG_CULL_BACK_FACE (1 << 1) /* back faces */276#define SI_NGG_CULL_FRONT_FACE (1 << 2) /* front faces */277#define SI_NGG_CULL_GS_FAST_LAUNCH_TRI_LIST (1 << 3) /* GS fast launch: triangles */278#define SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP (1 << 4) /* GS fast launch: triangle strip */279#define SI_NGG_CULL_GS_FAST_LAUNCH_INDEX_SIZE_PACKED(x) (((x) & 0x3) << 5) /* 0->0, 1->1, 2->2, 3->4 */280#define SI_GET_NGG_CULL_GS_FAST_LAUNCH_INDEX_SIZE_PACKED(x) (((x) >> 5) & 0x3)281#define SI_NGG_CULL_GS_FAST_LAUNCH_ALL (0xf << 3) /* GS fast launch (both prim types) */282283/**284* For VS shader keys, describe any fixups required for vertex fetch.285*286* \ref log_size, \ref format, and the number of channels are interpreted as287* by \ref ac_build_opencoded_load_format.288*289* Note: all bits 0 (size = 1 byte, num channels = 1, format = float) is an290* impossible format and indicates that no fixup is needed (just use291* buffer_load_format_xyzw).292*/293union si_vs_fix_fetch {294struct {295uint8_t log_size : 2; /* 1, 2, 4, 8 or bytes per channel */296uint8_t num_channels_m1 : 2; /* number of channels minus 1 */297uint8_t format : 3; /* AC_FETCH_FORMAT_xxx */298uint8_t reverse : 1; /* reverse XYZ channels */299} u;300uint8_t bits;301};302303struct si_shader;304305/* State of the context creating the shader object. */306struct si_compiler_ctx_state {307/* Should only be used by si_init_shader_selector_async and308* si_build_shader_variant if thread_index == -1 (non-threaded). */309struct ac_llvm_compiler *compiler;310311/* Used if thread_index == -1 or if debug.async is true. */312struct pipe_debug_callback debug;313314/* Used for creating the log string for gallium/ddebug. */315bool is_debug_context;316};317318enum si_color_output_type {319SI_TYPE_ANY32,320SI_TYPE_FLOAT16,321SI_TYPE_INT16,322SI_TYPE_UINT16,323};324325struct si_shader_info {326shader_info base;327328gl_shader_stage stage;329330ubyte num_inputs;331ubyte num_outputs;332ubyte input_semantic[PIPE_MAX_SHADER_INPUTS];333ubyte input_interpolate[PIPE_MAX_SHADER_INPUTS];334ubyte input_usage_mask[PIPE_MAX_SHADER_INPUTS];335ubyte input_fp16_lo_hi_valid[PIPE_MAX_SHADER_INPUTS];336ubyte output_semantic[PIPE_MAX_SHADER_OUTPUTS];337char output_semantic_to_slot[VARYING_SLOT_VAR15_16BIT + 1];338ubyte output_usagemask[PIPE_MAX_SHADER_OUTPUTS];339ubyte output_readmask[PIPE_MAX_SHADER_OUTPUTS];340ubyte output_streams[PIPE_MAX_SHADER_OUTPUTS];341ubyte output_type[PIPE_MAX_SHADER_OUTPUTS]; /* enum nir_alu_type */342343ubyte color_interpolate[2];344ubyte color_interpolate_loc[2];345346int constbuf0_num_slots;347ubyte num_stream_output_components[4];348349uint num_memory_stores;350351ubyte colors_read; /**< which color components are read by the FS */352ubyte colors_written;353uint16_t output_color_types; /**< Each bit pair is enum si_color_output_type */354bool color0_writes_all_cbufs; /**< gl_FragColor */355bool reads_samplemask; /**< does fragment shader read sample mask? */356bool reads_tess_factors; /**< If TES reads TESSINNER or TESSOUTER */357bool writes_z; /**< does fragment shader write Z value? */358bool writes_stencil; /**< does fragment shader write stencil value? */359bool writes_samplemask; /**< does fragment shader write sample mask? */360bool writes_edgeflag; /**< vertex shader outputs edgeflag */361bool uses_interp_color;362bool uses_persp_center_color;363bool uses_persp_centroid_color;364bool uses_persp_sample_color;365bool uses_persp_center;366bool uses_persp_centroid;367bool uses_persp_sample;368bool uses_linear_center;369bool uses_linear_centroid;370bool uses_linear_sample;371bool uses_interp_at_sample;372bool uses_instanceid;373bool uses_base_vertex;374bool uses_base_instance;375bool uses_drawid;376bool uses_primid;377bool uses_frontface;378bool uses_invocationid;379bool uses_thread_id[3];380bool uses_block_id[3];381bool uses_variable_block_size;382bool uses_grid_size;383bool uses_subgroup_info;384bool writes_position;385bool writes_psize;386bool writes_clipvertex;387bool writes_primid;388bool writes_viewport_index;389bool writes_layer;390bool uses_bindless_samplers;391bool uses_bindless_images;392bool uses_indirect_descriptor;393394bool uses_vmem_return_type_sampler_or_bvh;395bool uses_vmem_return_type_other; /* all other VMEM loads and atomics with return */396397/** Whether all codepaths write tess factors in all invocations. */398bool tessfactors_are_def_in_all_invocs;399400/* A flag to check if vrs2x2 can be enabled to reduce number of401* fragment shader invocations if flat shading.402*/403bool allow_flat_shading;404405/* Optimization: if the texture bound to this texunit has been cleared to 1,406* then the draw can be skipped (see si_draw_vbo_skip_noop). Initially the407* value is 0xff (undetermined) and can be later changed to 0 (= false) or408* texunit + 1.409*/410uint8_t writes_1_if_tex_is_1;411};412413/* A shader selector is a gallium CSO and contains shader variants and414* binaries for one NIR program. This can be shared by multiple contexts.415*/416struct si_shader_selector {417struct util_live_shader base;418struct si_screen *screen;419struct util_queue_fence ready;420struct si_compiler_ctx_state compiler_ctx_state;421422simple_mtx_t mutex;423struct si_shader *first_variant; /* immutable after the first variant */424struct si_shader *last_variant; /* mutable */425426/* The compiled NIR shader without a prolog and/or epilog (not427* uploaded to a buffer object).428*/429struct si_shader *main_shader_part;430struct si_shader *main_shader_part_ls; /* as_ls is set in the key */431struct si_shader *main_shader_part_es; /* as_es is set in the key */432struct si_shader *main_shader_part_ngg; /* as_ngg is set in the key */433struct si_shader *main_shader_part_ngg_es; /* for Wave32 TES before legacy GS */434435struct si_shader *gs_copy_shader;436437struct nir_shader *nir;438void *nir_binary;439unsigned nir_size;440441struct pipe_stream_output_info so;442struct si_shader_info info;443444enum pipe_shader_type pipe_shader_type;445ubyte const_and_shader_buf_descriptors_index;446ubyte sampler_and_images_descriptors_index;447bool vs_needs_prolog;448bool prim_discard_cs_allowed;449ubyte cs_shaderbufs_sgpr_index;450ubyte cs_num_shaderbufs_in_user_sgprs;451ubyte cs_images_sgpr_index;452ubyte cs_images_num_sgprs;453ubyte cs_num_images_in_user_sgprs;454ubyte num_vs_inputs;455ubyte num_vbos_in_user_sgprs;456unsigned pa_cl_vs_out_cntl;457unsigned ngg_cull_vert_threshold; /* UINT32_MAX = disabled */458ubyte clipdist_mask;459ubyte culldist_mask;460enum pipe_prim_type rast_prim;461462/* ES parameters. */463uint16_t esgs_itemsize; /* vertex stride */464uint16_t lshs_vertex_stride;465466/* GS parameters. */467uint16_t gsvs_vertex_size;468ubyte gs_input_verts_per_prim;469unsigned max_gsvs_emit_size;470uint16_t enabled_streamout_buffer_mask;471bool tess_turns_off_ngg;472473/* PS parameters. */474ubyte color_attr_index[2];475unsigned db_shader_control;476/* Set 0xf or 0x0 (4 bits) per each written output.477* ANDed with spi_shader_col_format.478*/479unsigned colors_written_4bit;480481uint64_t outputs_written_before_ps; /* "get_unique_index" bits */482uint64_t outputs_written; /* "get_unique_index" bits */483uint32_t patch_outputs_written; /* "get_unique_index_patch" bits */484485uint64_t inputs_read; /* "get_unique_index" bits */486uint64_t tcs_vgpr_only_inputs; /* TCS inputs that are only in VGPRs, not LDS. */487488/* bitmasks of used descriptor slots */489uint64_t active_const_and_shader_buffers;490uint64_t active_samplers_and_images;491};492493/* Valid shader configurations:494*495* API shaders VS | TCS | TES | GS |pass| PS496* are compiled as: | | | |thru|497* | | | | |498* Only VS & PS: VS | | | | | PS499* GFX6 - with GS: ES | | | GS | VS | PS500* - with tess: LS | HS | VS | | | PS501* - with both: LS | HS | ES | GS | VS | PS502* GFX9 - with GS: -> | | | GS | VS | PS503* - with tess: -> | HS | VS | | | PS504* - with both: -> | HS | -> | GS | VS | PS505* | | | | |506* NGG - VS & PS: GS | | | | | PS507* (GFX10+) - with GS: -> | | | GS | | PS508* - with tess: -> | HS | GS | | | PS509* - with both: -> | HS | -> | GS | | PS510*511* -> = merged with the next stage512*/513514/* Use the byte alignment for all following structure members for optimal515* shader key memory footprint.516*/517#pragma pack(push, 1)518519/* Common VS bits between the shader key and the prolog key. */520struct si_vs_prolog_bits {521/* - If neither "is_one" nor "is_fetched" has a bit set, the instance522* divisor is 0.523* - If "is_one" has a bit set, the instance divisor is 1.524* - If "is_fetched" has a bit set, the instance divisor will be loaded525* from the constant buffer.526*/527uint16_t instance_divisor_is_one; /* bitmask of inputs */528uint16_t instance_divisor_is_fetched; /* bitmask of inputs */529unsigned ls_vgpr_fix : 1;530unsigned unpack_instance_id_from_vertex_id : 1;531};532533/* Common TCS bits between the shader key and the epilog key. */534struct si_tcs_epilog_bits {535unsigned prim_mode : 3;536unsigned invoc0_tess_factors_are_def : 1;537unsigned tes_reads_tess_factors : 1;538};539540struct si_gs_prolog_bits {541unsigned tri_strip_adj_fix : 1;542};543544/* Common PS bits between the shader key and the prolog key. */545struct si_ps_prolog_bits {546unsigned color_two_side : 1;547unsigned flatshade_colors : 1;548unsigned poly_stipple : 1;549unsigned force_persp_sample_interp : 1;550unsigned force_linear_sample_interp : 1;551unsigned force_persp_center_interp : 1;552unsigned force_linear_center_interp : 1;553unsigned bc_optimize_for_persp : 1;554unsigned bc_optimize_for_linear : 1;555unsigned samplemask_log_ps_iter : 3;556};557558/* Common PS bits between the shader key and the epilog key. */559struct si_ps_epilog_bits {560unsigned spi_shader_col_format;561unsigned color_is_int8 : 8;562unsigned color_is_int10 : 8;563unsigned last_cbuf : 3;564unsigned alpha_func : 3;565unsigned alpha_to_one : 1;566unsigned poly_line_smoothing : 1;567unsigned clamp_color : 1;568};569570union si_shader_part_key {571struct {572struct si_vs_prolog_bits states;573unsigned num_input_sgprs : 6;574/* For merged stages such as LS-HS, HS input VGPRs are first. */575unsigned num_merged_next_stage_vgprs : 3;576unsigned num_inputs : 5;577unsigned as_ls : 1;578unsigned as_es : 1;579unsigned as_ngg : 1;580unsigned as_prim_discard_cs : 1;581unsigned gs_fast_launch_tri_list : 1; /* for NGG culling */582unsigned gs_fast_launch_tri_strip : 1; /* for NGG culling */583unsigned gs_fast_launch_index_size_packed : 2;584unsigned load_vgprs_after_culling : 1;585/* Prologs for monolithic shaders shouldn't set EXEC. */586unsigned is_monolithic : 1;587} vs_prolog;588struct {589struct si_tcs_epilog_bits states;590} tcs_epilog;591struct {592struct si_gs_prolog_bits states;593unsigned as_ngg : 1;594} gs_prolog;595struct {596struct si_ps_prolog_bits states;597unsigned num_input_sgprs : 6;598unsigned num_input_vgprs : 5;599/* Color interpolation and two-side color selection. */600unsigned colors_read : 8; /* color input components read */601unsigned num_interp_inputs : 5; /* BCOLOR is at this location */602unsigned face_vgpr_index : 5;603unsigned ancillary_vgpr_index : 5;604unsigned wqm : 1;605char color_attr_index[2];606signed char color_interp_vgpr_index[2]; /* -1 == constant */607} ps_prolog;608struct {609struct si_ps_epilog_bits states;610unsigned colors_written : 8;611unsigned color_types : 16;612unsigned writes_z : 1;613unsigned writes_stencil : 1;614unsigned writes_samplemask : 1;615} ps_epilog;616};617618struct si_shader_key {619/* Prolog and epilog flags. */620union {621struct {622struct si_vs_prolog_bits prolog;623} vs;624struct {625struct si_vs_prolog_bits ls_prolog; /* for merged LS-HS */626struct si_shader_selector *ls; /* for merged LS-HS */627struct si_tcs_epilog_bits epilog;628} tcs; /* tessellation control shader */629struct {630struct si_vs_prolog_bits vs_prolog; /* for merged ES-GS */631struct si_shader_selector *es; /* for merged ES-GS */632struct si_gs_prolog_bits prolog;633} gs;634struct {635struct si_ps_prolog_bits prolog;636struct si_ps_epilog_bits epilog;637} ps;638} part;639640/* These three are initially set according to the NEXT_SHADER property,641* or guessed if the property doesn't seem correct.642*/643unsigned as_es : 1; /* export shader, which precedes GS */644unsigned as_ls : 1; /* local shader, which precedes TCS */645unsigned as_ngg : 1; /* VS, TES, or GS compiled as NGG primitive shader */646647/* Flags for monolithic compilation only. */648struct {649/* Whether fetch should be opencoded according to vs_fix_fetch.650* Otherwise, if vs_fix_fetch is non-zero, buffer_load_format_xyzw651* with minimal fixups is used. */652uint16_t vs_fetch_opencode;653union si_vs_fix_fetch vs_fix_fetch[SI_MAX_ATTRIBS];654655union {656uint64_t ff_tcs_inputs_to_copy; /* for fixed-func TCS */657/* When PS needs PrimID and GS is disabled. */658unsigned vs_export_prim_id : 1;659struct {660unsigned interpolate_at_sample_force_center : 1;661unsigned fbfetch_msaa : 1;662unsigned fbfetch_is_1D : 1;663unsigned fbfetch_layered : 1;664} ps;665} u;666} mono;667668/* Optimization flags for asynchronous compilation only. */669struct {670/* For HW VS (it can be VS, TES, GS) */671uint64_t kill_outputs; /* "get_unique_index" bits */672unsigned kill_clip_distances : 8;673unsigned kill_pointsize : 1;674675/* For NGG VS and TES. */676unsigned ngg_culling : 7; /* SI_NGG_CULL_* */677678/* For shaders where monolithic variants have better code.679*680* This is a flag that has no effect on code generation,681* but forces monolithic shaders to be used as soon as682* possible, because it's in the "opt" group.683*/684unsigned prefer_mono : 1;685686/* Primitive discard compute shader. */687unsigned vs_as_prim_discard_cs : 1;688unsigned cs_prim_type : 4;689unsigned cs_indexed : 1;690unsigned cs_instancing : 1;691unsigned cs_provoking_vertex_first : 1;692unsigned cs_cull_front : 1;693unsigned cs_cull_back : 1;694695/* VS and TCS have the same number of patch vertices. */696unsigned same_patch_vertices:1;697698unsigned inline_uniforms:1;699700/* This must be kept last to limit the number of variants701* depending only on the uniform values.702*/703uint32_t inlined_uniform_values[MAX_INLINABLE_UNIFORMS];704} opt;705};706707/* Restore the pack alignment to default. */708#pragma pack(pop)709710/* GCN-specific shader info. */711struct si_shader_binary_info {712ubyte vs_output_param_offset[SI_MAX_VS_OUTPUTS];713ubyte num_input_sgprs;714ubyte num_input_vgprs;715signed char face_vgpr_index;716signed char ancillary_vgpr_index;717bool uses_instanceid;718ubyte nr_pos_exports;719ubyte nr_param_exports;720unsigned private_mem_vgprs;721unsigned max_simd_waves;722};723724struct si_shader_binary {725const char *elf_buffer;726size_t elf_size;727728char *uploaded_code;729size_t uploaded_code_size;730731char *llvm_ir_string;732};733734struct gfx9_gs_info {735unsigned es_verts_per_subgroup;736unsigned gs_prims_per_subgroup;737unsigned gs_inst_prims_in_subgroup;738unsigned max_prims_per_subgroup;739unsigned esgs_ring_size; /* in bytes */740};741742struct si_shader {743struct si_compiler_ctx_state compiler_ctx_state;744745struct si_shader_selector *selector;746struct si_shader_selector *previous_stage_sel; /* for refcounting */747struct si_shader *next_variant;748749struct si_shader_part *prolog;750struct si_shader *previous_stage; /* for GFX9 */751struct si_shader_part *prolog2;752struct si_shader_part *epilog;753754struct si_pm4_state *pm4;755struct si_resource *bo;756struct si_resource *scratch_bo;757struct si_shader_key key;758struct util_queue_fence ready;759bool compilation_failed;760bool is_monolithic;761bool is_optimized;762bool is_binary_shared;763bool is_gs_copy_shader;764765/* The following data is all that's needed for binary shaders. */766struct si_shader_binary binary;767struct ac_shader_config config;768struct si_shader_binary_info info;769770/* SI_SGPR_VS_STATE_BITS */771bool uses_vs_state_provoking_vertex;772bool uses_vs_state_outprim;773774bool uses_base_instance;775776struct {777uint16_t ngg_emit_size; /* in dwords */778uint16_t hw_max_esverts;779uint16_t max_gsprims;780uint16_t max_out_verts;781uint16_t prim_amp_factor;782bool max_vert_out_per_gs_instance;783} ngg;784785/* Shader key + LLVM IR + disassembly + statistics.786* Generated for debug contexts only.787*/788char *shader_log;789size_t shader_log_size;790791struct gfx9_gs_info gs_info;792793/* For save precompute context registers values. */794union {795struct {796unsigned vgt_gsvs_ring_offset_1;797unsigned vgt_gsvs_ring_offset_2;798unsigned vgt_gsvs_ring_offset_3;799unsigned vgt_gsvs_ring_itemsize;800unsigned vgt_gs_max_vert_out;801unsigned vgt_gs_vert_itemsize;802unsigned vgt_gs_vert_itemsize_1;803unsigned vgt_gs_vert_itemsize_2;804unsigned vgt_gs_vert_itemsize_3;805unsigned vgt_gs_instance_cnt;806unsigned vgt_gs_onchip_cntl;807unsigned vgt_gs_max_prims_per_subgroup;808unsigned vgt_esgs_ring_itemsize;809} gs;810811struct {812unsigned ge_max_output_per_subgroup;813unsigned ge_ngg_subgrp_cntl;814unsigned vgt_primitiveid_en;815unsigned vgt_gs_onchip_cntl;816unsigned vgt_gs_instance_cnt;817unsigned vgt_esgs_ring_itemsize;818unsigned spi_vs_out_config;819unsigned spi_shader_idx_format;820unsigned spi_shader_pos_format;821unsigned pa_cl_vte_cntl;822unsigned pa_cl_ngg_cntl;823unsigned vgt_gs_max_vert_out; /* for API GS */824unsigned ge_pc_alloc; /* uconfig register */825} ngg;826827struct {828unsigned vgt_gs_mode;829unsigned vgt_primitiveid_en;830unsigned vgt_reuse_off;831unsigned spi_vs_out_config;832unsigned spi_shader_pos_format;833unsigned pa_cl_vte_cntl;834unsigned ge_pc_alloc; /* uconfig register */835} vs;836837struct {838unsigned spi_ps_input_ena;839unsigned spi_ps_input_addr;840unsigned spi_baryc_cntl;841unsigned spi_ps_in_control;842unsigned spi_shader_z_format;843unsigned spi_shader_col_format;844unsigned cb_shader_mask;845} ps;846} ctx_reg;847848/*For save precompute registers value */849unsigned vgt_tf_param; /* VGT_TF_PARAM */850unsigned vgt_vertex_reuse_block_cntl; /* VGT_VERTEX_REUSE_BLOCK_CNTL */851unsigned pa_cl_vs_out_cntl;852unsigned ge_cntl;853};854855struct si_shader_part {856struct si_shader_part *next;857union si_shader_part_key key;858struct si_shader_binary binary;859struct ac_shader_config config;860};861862/* si_shader.c */863bool si_compile_shader(struct si_screen *sscreen, struct ac_llvm_compiler *compiler,864struct si_shader *shader, struct pipe_debug_callback *debug);865bool si_create_shader_variant(struct si_screen *sscreen, struct ac_llvm_compiler *compiler,866struct si_shader *shader, struct pipe_debug_callback *debug);867void si_shader_destroy(struct si_shader *shader);868unsigned si_shader_io_get_unique_index_patch(unsigned semantic);869unsigned si_shader_io_get_unique_index(unsigned semantic, bool is_varying);870bool si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader,871uint64_t scratch_va);872void si_shader_dump(struct si_screen *sscreen, struct si_shader *shader,873struct pipe_debug_callback *debug, FILE *f, bool check_debug_option);874void si_shader_dump_stats_for_shader_db(struct si_screen *screen, struct si_shader *shader,875struct pipe_debug_callback *debug);876void si_multiwave_lds_size_workaround(struct si_screen *sscreen, unsigned *lds_size);877const char *si_get_shader_name(const struct si_shader *shader);878void si_shader_binary_clean(struct si_shader_binary *binary);879880/* si_shader_llvm_gs.c */881struct si_shader *si_generate_gs_copy_shader(struct si_screen *sscreen,882struct ac_llvm_compiler *compiler,883struct si_shader_selector *gs_selector,884struct pipe_debug_callback *debug);885886/* si_shader_nir.c */887void si_nir_scan_shader(const struct nir_shader *nir, struct si_shader_info *info);888void si_nir_opts(struct si_screen *sscreen, struct nir_shader *nir, bool first);889void si_nir_late_opts(nir_shader *nir);890void si_finalize_nir(struct pipe_screen *screen, void *nirptr, bool optimize);891892/* si_state_shaders.c */893void gfx9_get_gs_info(struct si_shader_selector *es, struct si_shader_selector *gs,894struct gfx9_gs_info *out);895896/* Inline helpers. */897898/* Return the pointer to the main shader part's pointer. */899static inline struct si_shader **si_get_main_shader_part(struct si_shader_selector *sel,900struct si_shader_key *key)901{902if (key->as_ls)903return &sel->main_shader_part_ls;904if (key->as_es && key->as_ngg)905return &sel->main_shader_part_ngg_es;906if (key->as_es)907return &sel->main_shader_part_es;908if (key->as_ngg)909return &sel->main_shader_part_ngg;910return &sel->main_shader_part;911}912913static inline bool gfx10_is_ngg_passthrough(struct si_shader *shader)914{915struct si_shader_selector *sel = shader->selector;916917return sel->info.stage != MESA_SHADER_GEOMETRY && !sel->so.num_outputs && !sel->info.writes_edgeflag &&918!shader->key.opt.ngg_culling &&919(sel->info.stage != MESA_SHADER_VERTEX || !shader->key.mono.u.vs_export_prim_id);920}921922static inline bool si_shader_uses_bindless_samplers(struct si_shader_selector *selector)923{924return selector ? selector->info.uses_bindless_samplers : false;925}926927static inline bool si_shader_uses_bindless_images(struct si_shader_selector *selector)928{929return selector ? selector->info.uses_bindless_images : false;930}931932#ifdef __cplusplus933}934#endif935936#endif937938939