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
,
104 SI_SGPR_VS_STATE_BITS
= SI_ES_NUM_USER_SGPR
,
108 SI_SGPR_LS_OUT_LAYOUT
= SI_ES_NUM_USER_SGPR
,
111 /* both TCS and TES */
112 SI_SGPR_TCS_OFFCHIP_LAYOUT
= SI_NUM_RESOURCE_SGPRS
,
113 SI_TES_NUM_USER_SGPR
,
116 SI_SGPR_TCS_OUT_OFFSETS
= SI_TES_NUM_USER_SGPR
,
117 SI_SGPR_TCS_OUT_LAYOUT
,
118 SI_SGPR_TCS_IN_LAYOUT
,
119 SI_TCS_NUM_USER_SGPR
,
122 SI_GS_NUM_USER_SGPR
= SI_NUM_RESOURCE_SGPRS
,
123 SI_GSCOPY_NUM_USER_SGPR
= SI_SGPR_RW_BUFFERS_HI
+ 1,
126 SI_SGPR_ALPHA_REF
= SI_NUM_RESOURCE_SGPRS
,
130 SI_SGPR_GRID_SIZE
= SI_NUM_RESOURCE_SGPRS
,
131 SI_SGPR_BLOCK_SIZE
= SI_SGPR_GRID_SIZE
+ 3,
132 SI_CS_NUM_USER_SGPR
= SI_SGPR_BLOCK_SIZE
+ 3
135 /* LLVM function parameter indices */
138 SI_PARAM_CONST_BUFFERS
,
141 SI_PARAM_SHADER_BUFFERS
,
142 SI_NUM_RESOURCE_PARAMS
,
144 /* VS only parameters */
145 SI_PARAM_VERTEX_BUFFERS
= SI_NUM_RESOURCE_PARAMS
,
146 SI_PARAM_BASE_VERTEX
,
147 SI_PARAM_START_INSTANCE
,
149 /* [0] = clamp vertex color, VS as VS only */
150 SI_PARAM_VS_STATE_BITS
,
151 /* same value as TCS_IN_LAYOUT, VS as LS only */
152 SI_PARAM_LS_OUT_LAYOUT
= SI_PARAM_DRAWID
+ 1,
153 /* the other VS parameters are assigned dynamically */
155 /* Layout of TCS outputs in the offchip buffer
156 * [0:8] = the number of patches per threadgroup.
157 * [9:15] = the number of output vertices per patch.
158 * [16:31] = the offset of per patch attributes in the buffer in bytes.
160 SI_PARAM_TCS_OFFCHIP_LAYOUT
= SI_NUM_RESOURCE_PARAMS
, /* for TCS & TES */
162 /* TCS only parameters. */
164 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
165 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
166 * [16:31] = TCS output patch0 offset for per-patch / 16, max = NUM_PATCHES*32*32* + 32*32
168 SI_PARAM_TCS_OUT_OFFSETS
,
170 /* Layout of TCS outputs / TES inputs:
171 * [0:12] = stride between output patches in dwords, num_outputs * num_vertices * 4, max = 32*32*4
172 * [13:20] = stride between output vertices in dwords = num_inputs * 4, max = 32*4
173 * [26:31] = gl_PatchVerticesIn, max = 32
175 SI_PARAM_TCS_OUT_LAYOUT
,
177 /* Layout of LS outputs / TCS inputs
178 * [0:12] = stride between patches in dwords = num_inputs * num_vertices * 4, max = 32*32*4
179 * [13:20] = stride between vertices in dwords = num_inputs * 4, max = 32*4
181 SI_PARAM_TCS_IN_LAYOUT
,
184 SI_PARAM_TESS_FACTOR_OFFSET
,
188 /* GS only parameters */
189 SI_PARAM_GS2VS_OFFSET
= SI_NUM_RESOURCE_PARAMS
,
191 SI_PARAM_VTX0_OFFSET
,
192 SI_PARAM_VTX1_OFFSET
,
193 SI_PARAM_PRIMITIVE_ID
,
194 SI_PARAM_VTX2_OFFSET
,
195 SI_PARAM_VTX3_OFFSET
,
196 SI_PARAM_VTX4_OFFSET
,
197 SI_PARAM_VTX5_OFFSET
,
198 SI_PARAM_GS_INSTANCE_ID
,
200 /* PS only parameters */
201 SI_PARAM_ALPHA_REF
= SI_NUM_RESOURCE_PARAMS
,
203 SI_PARAM_PERSP_SAMPLE
,
204 SI_PARAM_PERSP_CENTER
,
205 SI_PARAM_PERSP_CENTROID
,
206 SI_PARAM_PERSP_PULL_MODEL
,
207 SI_PARAM_LINEAR_SAMPLE
,
208 SI_PARAM_LINEAR_CENTER
,
209 SI_PARAM_LINEAR_CENTROID
,
210 SI_PARAM_LINE_STIPPLE_TEX
,
211 SI_PARAM_POS_X_FLOAT
,
212 SI_PARAM_POS_Y_FLOAT
,
213 SI_PARAM_POS_Z_FLOAT
,
214 SI_PARAM_POS_W_FLOAT
,
217 SI_PARAM_SAMPLE_COVERAGE
,
218 SI_PARAM_POS_FIXED_PT
,
220 /* CS only parameters */
221 SI_PARAM_GRID_SIZE
= SI_NUM_RESOURCE_PARAMS
,
226 SI_NUM_PARAMS
= SI_PARAM_POS_FIXED_PT
+ 9, /* +8 for COLOR[0..1] */
229 /* Fields of driver-defined VS state SGPR. */
230 /* Clamp vertex color output (only used in VS as VS). */
231 #define S_VS_STATE_CLAMP_VERTEX_COLOR(x) (((unsigned)(x) & 0x1) << 0)
232 #define C_VS_STATE_CLAMP_VERTEX_COLOR 0xFFFFFFFE
234 /* SI-specific system values. */
236 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
= TGSI_SEMANTIC_COUNT
,
237 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
,
240 /* For VS shader key fix_fetch. */
242 SI_FIX_FETCH_NONE
= 0,
243 SI_FIX_FETCH_A2_SNORM
,
244 SI_FIX_FETCH_A2_SSCALED
,
245 SI_FIX_FETCH_A2_SINT
,
246 SI_FIX_FETCH_RGBA_32_UNORM
,
247 SI_FIX_FETCH_RGBX_32_UNORM
,
248 SI_FIX_FETCH_RGBA_32_SNORM
,
249 SI_FIX_FETCH_RGBX_32_SNORM
,
250 SI_FIX_FETCH_RGBA_32_USCALED
,
251 SI_FIX_FETCH_RGBA_32_SSCALED
,
252 SI_FIX_FETCH_RGBA_32_FIXED
,
253 SI_FIX_FETCH_RGBX_32_FIXED
,
254 SI_FIX_FETCH_RG_64_FLOAT
,
255 SI_FIX_FETCH_RGB_64_FLOAT
,
256 SI_FIX_FETCH_RGBA_64_FLOAT
,
257 SI_FIX_FETCH_RGB_8
, /* A = 1.0 */
258 SI_FIX_FETCH_RGB_8_INT
, /* A = 1 */
260 SI_FIX_FETCH_RGB_16_INT
,
265 /* State of the context creating the shader object. */
266 struct si_compiler_ctx_state
{
267 /* Should only be used by si_init_shader_selector_async and
268 * si_build_shader_variant if thread_index == -1 (non-threaded). */
269 LLVMTargetMachineRef tm
;
271 /* Used if thread_index == -1 or if debug.async is true. */
272 struct pipe_debug_callback debug
;
274 /* Used for creating the log string for gallium/ddebug. */
275 bool is_debug_context
;
278 /* A shader selector is a gallium CSO and contains shader variants and
279 * binaries for one TGSI program. This can be shared by multiple contexts.
281 struct si_shader_selector
{
282 struct si_screen
*screen
;
283 struct util_queue_fence ready
;
284 struct si_compiler_ctx_state compiler_ctx_state
;
287 struct si_shader
*first_variant
; /* immutable after the first variant */
288 struct si_shader
*last_variant
; /* mutable */
290 /* The compiled TGSI shader expecting a prolog and/or epilog (not
291 * uploaded to a buffer).
293 struct si_shader
*main_shader_part
;
294 struct si_shader
*main_shader_part_ls
; /* as_ls is set in the key */
295 struct si_shader
*main_shader_part_es
; /* as_es is set in the key */
297 struct si_shader
*gs_copy_shader
;
299 struct tgsi_token
*tokens
;
300 struct pipe_stream_output_info so
;
301 struct tgsi_shader_info info
;
303 /* PIPE_SHADER_[VERTEX|FRAGMENT|...] */
307 unsigned esgs_itemsize
;
308 unsigned gs_input_verts_per_prim
;
309 unsigned gs_output_prim
;
310 unsigned gs_max_out_vertices
;
311 unsigned gs_num_invocations
;
312 unsigned max_gs_stream
; /* count - 1 */
313 unsigned gsvs_vertex_size
;
314 unsigned max_gsvs_emit_size
;
317 unsigned color_attr_index
[2];
318 unsigned db_shader_control
;
319 /* Set 0xf or 0x0 (4 bits) per each written output.
320 * ANDed with spi_shader_col_format.
322 unsigned colors_written_4bit
;
327 uint64_t outputs_written
; /* "get_unique_index" bits */
328 uint32_t patch_outputs_written
; /* "get_unique_index" bits */
329 uint32_t outputs_written2
; /* "get_unique_index2" bits */
331 uint64_t inputs_read
; /* "get_unique_index" bits */
332 uint32_t inputs_read2
; /* "get_unique_index2" bits */
335 /* Valid shader configurations:
337 * API shaders VS | TCS | TES | GS |pass| PS
338 * are compiled as: | | | |thru|
340 * Only VS & PS: VS | -- | -- | -- | -- | PS
341 * With GS: ES | -- | -- | GS | VS | PS
342 * With Tessel.: LS | HS | VS | -- | -- | PS
343 * With both: LS | HS | ES | GS | VS | PS
346 /* Common VS bits between the shader key and the prolog key. */
347 struct si_vs_prolog_bits
{
348 unsigned instance_divisors
[SI_MAX_ATTRIBS
];
351 /* Common VS bits between the shader key and the epilog key. */
352 struct si_vs_epilog_bits
{
353 unsigned export_prim_id
:1; /* when PS needs it and GS is disabled */
356 /* Common TCS bits between the shader key and the epilog key. */
357 struct si_tcs_epilog_bits
{
358 unsigned prim_mode
:3;
359 unsigned tes_reads_tess_factors
:1;
362 struct si_gs_prolog_bits
{
363 unsigned tri_strip_adj_fix
:1;
366 /* Common PS bits between the shader key and the prolog key. */
367 struct si_ps_prolog_bits
{
368 unsigned color_two_side
:1;
369 unsigned flatshade_colors
:1;
370 unsigned poly_stipple
:1;
371 unsigned force_persp_sample_interp
:1;
372 unsigned force_linear_sample_interp
:1;
373 unsigned force_persp_center_interp
:1;
374 unsigned force_linear_center_interp
:1;
375 unsigned bc_optimize_for_persp
:1;
376 unsigned bc_optimize_for_linear
:1;
379 /* Common PS bits between the shader key and the epilog key. */
380 struct si_ps_epilog_bits
{
381 unsigned spi_shader_col_format
;
382 unsigned color_is_int8
:8;
383 unsigned color_is_int10
:8;
384 unsigned last_cbuf
:3;
385 unsigned alpha_func
:3;
386 unsigned alpha_to_one
:1;
387 unsigned poly_line_smoothing
:1;
388 unsigned clamp_color
:1;
391 union si_shader_part_key
{
393 struct si_vs_prolog_bits states
;
394 unsigned num_input_sgprs
:5;
395 unsigned last_input
:4;
398 struct si_vs_epilog_bits states
;
399 unsigned prim_id_param_offset
:5;
402 struct si_tcs_epilog_bits states
;
405 struct si_gs_prolog_bits states
;
408 struct si_ps_prolog_bits states
;
409 unsigned num_input_sgprs
:5;
410 unsigned num_input_vgprs
:5;
411 /* Color interpolation and two-side color selection. */
412 unsigned colors_read
:8; /* color input components read */
413 unsigned num_interp_inputs
:5; /* BCOLOR is at this location */
414 unsigned face_vgpr_index
:5;
416 char color_attr_index
[2];
417 char color_interp_vgpr_index
[2]; /* -1 == constant */
420 struct si_ps_epilog_bits states
;
421 unsigned colors_written
:8;
423 unsigned writes_stencil
:1;
424 unsigned writes_samplemask
:1;
428 struct si_shader_key
{
429 /* Prolog and epilog flags. */
432 struct si_vs_prolog_bits prolog
;
433 struct si_vs_epilog_bits epilog
;
436 struct si_tcs_epilog_bits epilog
;
437 } tcs
; /* tessellation control shader */
439 struct si_vs_epilog_bits epilog
; /* same as VS */
440 } tes
; /* tessellation evaluation shader */
442 struct si_gs_prolog_bits prolog
;
445 struct si_ps_prolog_bits prolog
;
446 struct si_ps_epilog_bits epilog
;
450 /* These two are initially set according to the NEXT_SHADER property,
451 * or guessed if the property doesn't seem correct.
453 unsigned as_es
:1; /* export shader, which precedes GS */
454 unsigned as_ls
:1; /* local shader, which precedes TCS */
456 /* Flags for monolithic compilation only. */
459 /* One byte for every input: SI_FIX_FETCH_* enums. */
460 uint8_t fix_fetch
[SI_MAX_ATTRIBS
];
463 uint64_t 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
;
493 /* SPI_PS_INPUT_CNTL_i.OFFSET[0:4] */
494 EXP_PARAM_OFFSET_0
= 0,
495 EXP_PARAM_OFFSET_31
= 31,
496 /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL[0:1] */
497 EXP_PARAM_DEFAULT_VAL_0000
= 64,
498 EXP_PARAM_DEFAULT_VAL_0001
,
499 EXP_PARAM_DEFAULT_VAL_1110
,
500 EXP_PARAM_DEFAULT_VAL_1111
,
501 EXP_PARAM_UNDEFINED
= 255,
504 /* GCN-specific shader info. */
505 struct si_shader_info
{
506 ubyte vs_output_param_offset
[SI_MAX_VS_OUTPUTS
];
507 ubyte num_input_sgprs
;
508 ubyte num_input_vgprs
;
509 char face_vgpr_index
;
510 bool uses_instanceid
;
511 ubyte nr_pos_exports
;
512 ubyte nr_param_exports
;
516 struct si_compiler_ctx_state compiler_ctx_state
;
518 struct si_shader_selector
*selector
;
519 struct si_shader
*next_variant
;
521 struct si_shader_part
*prolog
;
522 struct si_shader_part
*epilog
;
524 struct si_pm4_state
*pm4
;
525 struct r600_resource
*bo
;
526 struct r600_resource
*scratch_bo
;
527 struct si_shader_key key
;
528 struct util_queue_fence optimized_ready
;
529 bool compilation_failed
;
532 bool is_binary_shared
;
533 bool is_gs_copy_shader
;
535 /* The following data is all that's needed for binary shaders. */
536 struct ac_shader_binary binary
;
537 struct si_shader_config config
;
538 struct si_shader_info info
;
540 /* Shader key + LLVM IR + disassembly + statistics.
541 * Generated for debug contexts only.
544 size_t shader_log_size
;
547 struct si_shader_part
{
548 struct si_shader_part
*next
;
549 union si_shader_part_key key
;
550 struct ac_shader_binary binary
;
551 struct si_shader_config config
;
556 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
557 LLVMTargetMachineRef tm
,
558 struct si_shader_selector
*gs_selector
,
559 struct pipe_debug_callback
*debug
);
560 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
561 LLVMTargetMachineRef tm
,
562 struct si_shader
*shader
,
564 struct pipe_debug_callback
*debug
);
565 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
566 struct si_shader
*shader
,
567 struct pipe_debug_callback
*debug
);
568 int si_compile_llvm(struct si_screen
*sscreen
,
569 struct ac_shader_binary
*binary
,
570 struct si_shader_config
*conf
,
571 LLVMTargetMachineRef tm
,
573 struct pipe_debug_callback
*debug
,
576 void si_shader_destroy(struct si_shader
*shader
);
577 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
);
578 unsigned si_shader_io_get_unique_index2(unsigned name
, unsigned index
);
579 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
);
580 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
581 struct pipe_debug_callback
*debug
, unsigned processor
,
582 FILE *f
, bool check_debug_option
);
583 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
585 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
586 struct si_shader
*shader
,
587 struct si_shader_config
*config
,
588 uint64_t scratch_va
);
589 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
590 struct si_shader_config
*conf
,
591 unsigned symbol_offset
);
592 unsigned si_get_spi_shader_z_format(bool writes_z
, bool writes_stencil
,
593 bool writes_samplemask
);
594 const char *si_get_shader_name(struct si_shader
*shader
, unsigned processor
);
596 /* Inline helpers. */
598 /* Return the pointer to the main shader part's pointer. */
599 static inline struct si_shader
**
600 si_get_main_shader_part(struct si_shader_selector
*sel
,
601 struct si_shader_key
*key
)
604 return &sel
->main_shader_part_ls
;
606 return &sel
->main_shader_part_es
;
607 return &sel
->main_shader_part
;