2 * Copyright 2012 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
29 /* How linking shader inputs and outputs between vertex, tessellation, and
30 * geometry shaders works.
32 * Inputs and outputs between shaders are stored in a buffer. This buffer
33 * lives in LDS (typical case for tessellation), but it can also live
34 * in memory (ESGS). Each input or output has a fixed location within a vertex.
35 * The highest used input or output determines the stride between vertices.
37 * Since GS and tessellation are only possible in the OpenGL core profile,
38 * only these semantics are valid for per-vertex data:
45 * CULLDIST0..1 (not implemented)
48 * For example, a shader only writing GENERIC0 has the output stride of 5.
50 * Only these semantics are valid for per-patch data:
58 * That's how independent shaders agree on input and output locations.
59 * The si_shader_io_get_unique_index function assigns the locations.
61 * For tessellation, other required information for calculating the input and
62 * output addresses like the vertex stride, the patch stride, and the offsets
63 * where per-vertex and per-patch data start, is passed to the shader via
64 * user data SGPRs. The offsets and strides are calculated at draw time and
65 * aren't available at compile time.
71 #include <llvm-c/Core.h> /* LLVMModuleRef */
72 #include <llvm-c/TargetMachine.h>
73 #include "tgsi/tgsi_scan.h"
74 #include "util/u_queue.h"
77 struct ac_shader_binary
;
79 #define SI_MAX_VS_OUTPUTS 40
81 /* SGPR user data indices */
83 SI_SGPR_RW_BUFFERS
, /* rings (& stream-out, VS only) */
84 SI_SGPR_RW_BUFFERS_HI
,
85 SI_SGPR_CONST_BUFFERS
,
86 SI_SGPR_CONST_BUFFERS_HI
,
87 SI_SGPR_SAMPLERS
, /* images & sampler states interleaved */
91 SI_SGPR_SHADER_BUFFERS
,
92 SI_SGPR_SHADER_BUFFERS_HI
,
93 SI_NUM_RESOURCE_SGPRS
,
96 SI_SGPR_VERTEX_BUFFERS
= SI_NUM_RESOURCE_SGPRS
,
97 SI_SGPR_VERTEX_BUFFERS_HI
,
99 SI_SGPR_START_INSTANCE
,
101 SI_SGPR_VS_STATE_BITS
,
104 /* both TCS and TES */
105 SI_SGPR_TCS_OFFCHIP_LAYOUT
= SI_NUM_RESOURCE_SGPRS
,
106 SI_TES_NUM_USER_SGPR
,
109 SI_SGPR_TCS_OUT_OFFSETS
= SI_TES_NUM_USER_SGPR
,
110 SI_SGPR_TCS_OUT_LAYOUT
,
111 SI_SGPR_TCS_IN_LAYOUT
,
112 SI_TCS_NUM_USER_SGPR
,
115 SI_GS_NUM_USER_SGPR
= SI_NUM_RESOURCE_SGPRS
,
116 SI_GSCOPY_NUM_USER_SGPR
= SI_SGPR_RW_BUFFERS_HI
+ 1,
119 SI_SGPR_ALPHA_REF
= SI_NUM_RESOURCE_SGPRS
,
123 SI_SGPR_GRID_SIZE
= SI_NUM_RESOURCE_SGPRS
,
124 SI_SGPR_BLOCK_SIZE
= SI_SGPR_GRID_SIZE
+ 3,
125 SI_CS_NUM_USER_SGPR
= SI_SGPR_BLOCK_SIZE
+ 3
128 /* LLVM function parameter indices */
131 SI_PARAM_CONST_BUFFERS
,
134 SI_PARAM_SHADER_BUFFERS
,
135 SI_NUM_RESOURCE_PARAMS
,
137 /* VS only parameters */
138 SI_PARAM_VERTEX_BUFFERS
= SI_NUM_RESOURCE_PARAMS
,
139 SI_PARAM_BASE_VERTEX
,
140 SI_PARAM_START_INSTANCE
,
142 SI_PARAM_VS_STATE_BITS
,
144 /* Layout of TCS outputs in the offchip buffer
145 * [0:8] = the number of patches per threadgroup.
146 * [9:15] = the number of output vertices per patch.
147 * [16:31] = the offset of per patch attributes in the buffer in bytes.
149 SI_PARAM_TCS_OFFCHIP_LAYOUT
= SI_NUM_RESOURCE_PARAMS
, /* for TCS & TES */
151 /* TCS only parameters. */
153 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
154 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
155 * [16:31] = TCS output patch0 offset for per-patch / 16, max = NUM_PATCHES*32*32* + 32*32
157 SI_PARAM_TCS_OUT_OFFSETS
,
159 /* Layout of TCS outputs / TES inputs:
160 * [0:12] = stride between output patches in dwords, num_outputs * num_vertices * 4, max = 32*32*4
161 * [13:20] = stride between output vertices in dwords = num_inputs * 4, max = 32*4
162 * [26:31] = gl_PatchVerticesIn, max = 32
164 SI_PARAM_TCS_OUT_LAYOUT
,
166 /* Layout of LS outputs / TCS inputs
167 * [8:20] = stride between patches in dwords = num_inputs * num_vertices * 4, max = 32*32*4
168 * [24:31] = stride between vertices in dwords = num_inputs * 4, max = 32*4
169 * (same layout as SI_PARAM_VS_STATE_BITS)
171 SI_PARAM_TCS_IN_LAYOUT
,
174 SI_PARAM_TESS_FACTOR_OFFSET
,
178 /* GS only parameters */
179 SI_PARAM_GS2VS_OFFSET
= SI_NUM_RESOURCE_PARAMS
,
181 SI_PARAM_VTX0_OFFSET
,
182 SI_PARAM_VTX1_OFFSET
,
183 SI_PARAM_PRIMITIVE_ID
,
184 SI_PARAM_VTX2_OFFSET
,
185 SI_PARAM_VTX3_OFFSET
,
186 SI_PARAM_VTX4_OFFSET
,
187 SI_PARAM_VTX5_OFFSET
,
188 SI_PARAM_GS_INSTANCE_ID
,
190 /* PS only parameters */
191 SI_PARAM_ALPHA_REF
= SI_NUM_RESOURCE_PARAMS
,
193 SI_PARAM_PERSP_SAMPLE
,
194 SI_PARAM_PERSP_CENTER
,
195 SI_PARAM_PERSP_CENTROID
,
196 SI_PARAM_PERSP_PULL_MODEL
,
197 SI_PARAM_LINEAR_SAMPLE
,
198 SI_PARAM_LINEAR_CENTER
,
199 SI_PARAM_LINEAR_CENTROID
,
200 SI_PARAM_LINE_STIPPLE_TEX
,
201 SI_PARAM_POS_X_FLOAT
,
202 SI_PARAM_POS_Y_FLOAT
,
203 SI_PARAM_POS_Z_FLOAT
,
204 SI_PARAM_POS_W_FLOAT
,
207 SI_PARAM_SAMPLE_COVERAGE
,
208 SI_PARAM_POS_FIXED_PT
,
210 /* CS only parameters */
211 SI_PARAM_GRID_SIZE
= SI_NUM_RESOURCE_PARAMS
,
216 SI_NUM_PARAMS
= SI_PARAM_POS_FIXED_PT
+ 9, /* +8 for COLOR[0..1] */
219 /* Fields of driver-defined VS state SGPR. */
220 /* Clamp vertex color output (only used in VS as VS). */
221 #define S_VS_STATE_CLAMP_VERTEX_COLOR(x) (((unsigned)(x) & 0x1) << 0)
222 #define C_VS_STATE_CLAMP_VERTEX_COLOR 0xFFFFFFFE
223 #define S_VS_STATE_INDEXED(x) (((unsigned)(x) & 0x1) << 1)
224 #define C_VS_STATE_INDEXED 0xFFFFFFFD
225 #define S_VS_STATE_LS_OUT_PATCH_SIZE(x) (((unsigned)(x) & 0x1FFF) << 8)
226 #define C_VS_STATE_LS_OUT_PATCH_SIZE 0xFFE000FF
227 #define S_VS_STATE_LS_OUT_VERTEX_SIZE(x) (((unsigned)(x) & 0xFF) << 24)
228 #define C_VS_STATE_LS_OUT_VERTEX_SIZE 0x00FFFFFF
230 /* SI-specific system values. */
232 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
= TGSI_SEMANTIC_COUNT
,
233 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
,
236 /* For VS shader key fix_fetch. */
238 SI_FIX_FETCH_NONE
= 0,
239 SI_FIX_FETCH_A2_SNORM
,
240 SI_FIX_FETCH_A2_SSCALED
,
241 SI_FIX_FETCH_A2_SINT
,
242 SI_FIX_FETCH_RGBA_32_UNORM
,
243 SI_FIX_FETCH_RGBX_32_UNORM
,
244 SI_FIX_FETCH_RGBA_32_SNORM
,
245 SI_FIX_FETCH_RGBX_32_SNORM
,
246 SI_FIX_FETCH_RGBA_32_USCALED
,
247 SI_FIX_FETCH_RGBA_32_SSCALED
,
248 SI_FIX_FETCH_RGBA_32_FIXED
,
249 SI_FIX_FETCH_RGBX_32_FIXED
,
250 SI_FIX_FETCH_RG_64_FLOAT
,
251 SI_FIX_FETCH_RGB_64_FLOAT
,
252 SI_FIX_FETCH_RGBA_64_FLOAT
,
253 SI_FIX_FETCH_RGB_8
, /* A = 1.0 */
254 SI_FIX_FETCH_RGB_8_INT
, /* A = 1 */
256 SI_FIX_FETCH_RGB_16_INT
,
261 /* State of the context creating the shader object. */
262 struct si_compiler_ctx_state
{
263 /* Should only be used by si_init_shader_selector_async and
264 * si_build_shader_variant if thread_index == -1 (non-threaded). */
265 LLVMTargetMachineRef tm
;
267 /* Used if thread_index == -1 or if debug.async is true. */
268 struct pipe_debug_callback debug
;
270 /* Used for creating the log string for gallium/ddebug. */
271 bool is_debug_context
;
274 /* A shader selector is a gallium CSO and contains shader variants and
275 * binaries for one TGSI program. This can be shared by multiple contexts.
277 struct si_shader_selector
{
278 struct si_screen
*screen
;
279 struct util_queue_fence ready
;
280 struct si_compiler_ctx_state compiler_ctx_state
;
283 struct si_shader
*first_variant
; /* immutable after the first variant */
284 struct si_shader
*last_variant
; /* mutable */
286 /* The compiled TGSI shader expecting a prolog and/or epilog (not
287 * uploaded to a buffer).
289 struct si_shader
*main_shader_part
;
290 struct si_shader
*main_shader_part_ls
; /* as_ls is set in the key */
291 struct si_shader
*main_shader_part_es
; /* as_es is set in the key */
293 struct si_shader
*gs_copy_shader
;
295 struct tgsi_token
*tokens
;
296 struct pipe_stream_output_info so
;
297 struct tgsi_shader_info info
;
299 /* PIPE_SHADER_[VERTEX|FRAGMENT|...] */
301 bool vs_needs_prolog
;
304 unsigned esgs_itemsize
;
305 unsigned gs_input_verts_per_prim
;
306 unsigned gs_output_prim
;
307 unsigned gs_max_out_vertices
;
308 unsigned gs_num_invocations
;
309 unsigned max_gs_stream
; /* count - 1 */
310 unsigned gsvs_vertex_size
;
311 unsigned max_gsvs_emit_size
;
314 unsigned color_attr_index
[2];
315 unsigned db_shader_control
;
316 /* Set 0xf or 0x0 (4 bits) per each written output.
317 * ANDed with spi_shader_col_format.
319 unsigned colors_written_4bit
;
324 uint64_t outputs_written
; /* "get_unique_index" bits */
325 uint32_t patch_outputs_written
; /* "get_unique_index" bits */
326 uint32_t outputs_written2
; /* "get_unique_index2" bits */
328 uint64_t inputs_read
; /* "get_unique_index" bits */
329 uint32_t inputs_read2
; /* "get_unique_index2" bits */
332 /* Valid shader configurations:
334 * API shaders VS | TCS | TES | GS |pass| PS
335 * are compiled as: | | | |thru|
337 * Only VS & PS: VS | | | | | PS
338 * GFX6 - with GS: ES | | | GS | VS | PS
339 * - with tess: LS | HS | VS | | | PS
340 * - with both: LS | HS | ES | GS | VS | PS
341 * GFX9 - with GS: -> | | | GS | VS | PS
342 * - with tess: -> | HS | VS | | | PS
343 * - with both: -> | HS | -> | GS | VS | PS
345 * -> = merged with the next stage
348 /* Common VS bits between the shader key and the prolog key. */
349 struct si_vs_prolog_bits
{
350 unsigned instance_divisors
[SI_MAX_ATTRIBS
];
353 /* Common VS bits between the shader key and the epilog key. */
354 struct si_vs_epilog_bits
{
355 unsigned export_prim_id
:1; /* when PS needs it and GS is disabled */
358 /* Common TCS bits between the shader key and the epilog key. */
359 struct si_tcs_epilog_bits
{
360 unsigned prim_mode
:3;
361 unsigned tes_reads_tess_factors
:1;
364 struct si_gs_prolog_bits
{
365 unsigned tri_strip_adj_fix
:1;
368 /* Common PS bits between the shader key and the prolog key. */
369 struct si_ps_prolog_bits
{
370 unsigned color_two_side
:1;
371 unsigned flatshade_colors
:1;
372 unsigned poly_stipple
:1;
373 unsigned force_persp_sample_interp
:1;
374 unsigned force_linear_sample_interp
:1;
375 unsigned force_persp_center_interp
:1;
376 unsigned force_linear_center_interp
:1;
377 unsigned bc_optimize_for_persp
:1;
378 unsigned bc_optimize_for_linear
:1;
381 /* Common PS bits between the shader key and the epilog key. */
382 struct si_ps_epilog_bits
{
383 unsigned spi_shader_col_format
;
384 unsigned color_is_int8
:8;
385 unsigned color_is_int10
:8;
386 unsigned last_cbuf
:3;
387 unsigned alpha_func
:3;
388 unsigned alpha_to_one
:1;
389 unsigned poly_line_smoothing
:1;
390 unsigned clamp_color
:1;
393 union si_shader_part_key
{
395 struct si_vs_prolog_bits states
;
396 unsigned num_input_sgprs
:6;
397 unsigned last_input
:4;
400 struct si_vs_epilog_bits states
;
401 unsigned prim_id_param_offset
:5;
404 struct si_tcs_epilog_bits states
;
407 struct si_gs_prolog_bits states
;
410 struct si_ps_prolog_bits states
;
411 unsigned num_input_sgprs
:6;
412 unsigned num_input_vgprs
:5;
413 /* Color interpolation and two-side color selection. */
414 unsigned colors_read
:8; /* color input components read */
415 unsigned num_interp_inputs
:5; /* BCOLOR is at this location */
416 unsigned face_vgpr_index
:5;
418 char color_attr_index
[2];
419 char color_interp_vgpr_index
[2]; /* -1 == constant */
422 struct si_ps_epilog_bits states
;
423 unsigned colors_written
:8;
425 unsigned writes_stencil
:1;
426 unsigned writes_samplemask
:1;
430 struct si_shader_key
{
431 /* Prolog and epilog flags. */
434 struct si_vs_prolog_bits prolog
;
435 struct si_vs_epilog_bits epilog
;
438 struct si_vs_prolog_bits ls_prolog
; /* for merged LS-HS */
439 struct si_shader_selector
*ls
; /* for merged LS-HS */
440 struct si_tcs_epilog_bits epilog
;
441 } tcs
; /* tessellation control shader */
443 struct si_vs_epilog_bits epilog
; /* same as VS */
444 } tes
; /* tessellation evaluation shader */
446 struct si_gs_prolog_bits prolog
;
449 struct si_ps_prolog_bits prolog
;
450 struct si_ps_epilog_bits epilog
;
454 /* These two are initially set according to the NEXT_SHADER property,
455 * or guessed if the property doesn't seem correct.
457 unsigned as_es
:1; /* export shader, which precedes GS */
458 unsigned as_ls
:1; /* local shader, which precedes TCS */
460 /* Flags for monolithic compilation only. */
462 /* One byte for every input: SI_FIX_FETCH_* enums. */
463 uint8_t vs_fix_fetch
[SI_MAX_ATTRIBS
];
464 uint64_t ff_tcs_inputs_to_copy
; /* for fixed-func TCS */
467 /* Optimization flags for asynchronous compilation only. */
470 uint64_t kill_outputs
; /* "get_unique_index" bits */
471 uint32_t kill_outputs2
; /* "get_unique_index2" bits */
472 unsigned clip_disable
:1;
473 } hw_vs
; /* HW VS (it can be VS, TES, GS) */
477 struct si_shader_config
{
480 unsigned spilled_sgprs
;
481 unsigned spilled_vgprs
;
482 unsigned private_mem_vgprs
;
484 unsigned spi_ps_input_ena
;
485 unsigned spi_ps_input_addr
;
487 unsigned scratch_bytes_per_wave
;
492 /* GCN-specific shader info. */
493 struct si_shader_info
{
494 ubyte vs_output_param_offset
[SI_MAX_VS_OUTPUTS
];
495 ubyte num_input_sgprs
;
496 ubyte num_input_vgprs
;
497 char face_vgpr_index
;
498 bool uses_instanceid
;
499 ubyte nr_pos_exports
;
500 ubyte nr_param_exports
;
504 struct si_compiler_ctx_state compiler_ctx_state
;
506 struct si_shader_selector
*selector
;
507 struct si_shader
*next_variant
;
509 struct si_shader_part
*prolog
;
510 struct si_shader
*previous_stage
; /* for GFX9 */
511 struct si_shader_part
*epilog
;
513 struct si_pm4_state
*pm4
;
514 struct r600_resource
*bo
;
515 struct r600_resource
*scratch_bo
;
516 struct si_shader_key key
;
517 struct util_queue_fence optimized_ready
;
518 bool compilation_failed
;
521 bool is_binary_shared
;
522 bool is_gs_copy_shader
;
524 /* The following data is all that's needed for binary shaders. */
525 struct ac_shader_binary binary
;
526 struct si_shader_config config
;
527 struct si_shader_info info
;
529 /* Shader key + LLVM IR + disassembly + statistics.
530 * Generated for debug contexts only.
533 size_t shader_log_size
;
536 struct si_shader_part
{
537 struct si_shader_part
*next
;
538 union si_shader_part_key key
;
539 struct ac_shader_binary binary
;
540 struct si_shader_config config
;
545 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
546 LLVMTargetMachineRef tm
,
547 struct si_shader_selector
*gs_selector
,
548 struct pipe_debug_callback
*debug
);
549 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
550 LLVMTargetMachineRef tm
,
551 struct si_shader
*shader
,
553 struct pipe_debug_callback
*debug
);
554 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
555 struct si_shader
*shader
,
556 struct pipe_debug_callback
*debug
);
557 int si_compile_llvm(struct si_screen
*sscreen
,
558 struct ac_shader_binary
*binary
,
559 struct si_shader_config
*conf
,
560 LLVMTargetMachineRef tm
,
562 struct pipe_debug_callback
*debug
,
565 void si_shader_destroy(struct si_shader
*shader
);
566 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
);
567 unsigned si_shader_io_get_unique_index2(unsigned name
, unsigned index
);
568 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
);
569 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
570 struct pipe_debug_callback
*debug
, unsigned processor
,
571 FILE *f
, bool check_debug_option
);
572 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
574 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
575 struct si_shader
*shader
,
576 struct si_shader_config
*config
,
577 uint64_t scratch_va
);
578 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
579 struct si_shader_config
*conf
,
580 unsigned symbol_offset
);
581 unsigned si_get_spi_shader_z_format(bool writes_z
, bool writes_stencil
,
582 bool writes_samplemask
);
583 const char *si_get_shader_name(struct si_shader
*shader
, unsigned processor
);
585 /* Inline helpers. */
587 /* Return the pointer to the main shader part's pointer. */
588 static inline struct si_shader
**
589 si_get_main_shader_part(struct si_shader_selector
*sel
,
590 struct si_shader_key
*key
)
593 return &sel
->main_shader_part_ls
;
595 return &sel
->main_shader_part_es
;
596 return &sel
->main_shader_part
;