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 "tgsi/tgsi_scan.h"
75 struct radeon_shader_binary
;
76 struct radeon_shader_reloc
;
78 #define SI_MAX_VS_OUTPUTS 40
80 /* SGPR user data indices */
82 SI_SGPR_RW_BUFFERS
, /* rings (& stream-out, VS only) */
83 SI_SGPR_RW_BUFFERS_HI
,
84 SI_SGPR_CONST_BUFFERS
,
85 SI_SGPR_CONST_BUFFERS_HI
,
86 SI_SGPR_SAMPLERS
, /* images & sampler states interleaved */
90 SI_SGPR_SHADER_BUFFERS
,
91 SI_SGPR_SHADER_BUFFERS_HI
,
92 SI_NUM_RESOURCE_SGPRS
,
95 SI_SGPR_VERTEX_BUFFERS
= SI_NUM_RESOURCE_SGPRS
,
96 SI_SGPR_VERTEX_BUFFERS_HI
,
98 SI_SGPR_START_INSTANCE
,
102 SI_SGPR_VS_STATE_BITS
= SI_ES_NUM_USER_SGPR
,
106 SI_SGPR_LS_OUT_LAYOUT
= SI_ES_NUM_USER_SGPR
,
109 /* both TCS and TES */
110 SI_SGPR_TCS_OFFCHIP_LAYOUT
= SI_NUM_RESOURCE_SGPRS
,
111 SI_TES_NUM_USER_SGPR
,
114 SI_SGPR_TCS_OUT_OFFSETS
= SI_TES_NUM_USER_SGPR
,
115 SI_SGPR_TCS_OUT_LAYOUT
,
116 SI_SGPR_TCS_IN_LAYOUT
,
117 SI_TCS_NUM_USER_SGPR
,
120 SI_GS_NUM_USER_SGPR
= SI_NUM_RESOURCE_SGPRS
,
121 SI_GSCOPY_NUM_USER_SGPR
= SI_SGPR_RW_BUFFERS_HI
+ 1,
124 SI_SGPR_ALPHA_REF
= SI_NUM_RESOURCE_SGPRS
,
128 SI_SGPR_GRID_SIZE
= SI_NUM_RESOURCE_SGPRS
,
129 SI_CS_NUM_USER_SGPR
= SI_SGPR_GRID_SIZE
+ 3
132 /* LLVM function parameter indices */
135 SI_PARAM_CONST_BUFFERS
,
138 SI_PARAM_SHADER_BUFFERS
,
139 SI_NUM_RESOURCE_PARAMS
,
141 /* VS only parameters */
142 SI_PARAM_VERTEX_BUFFERS
= SI_NUM_RESOURCE_PARAMS
,
143 SI_PARAM_BASE_VERTEX
,
144 SI_PARAM_START_INSTANCE
,
145 /* [0] = clamp vertex color, VS as VS only */
146 SI_PARAM_VS_STATE_BITS
,
147 /* same value as TCS_IN_LAYOUT, VS as LS only */
148 SI_PARAM_LS_OUT_LAYOUT
= SI_PARAM_START_INSTANCE
+ 1,
149 /* the other VS parameters are assigned dynamically */
151 /* Layout of TCS outputs in the offchip buffer
152 * [0:8] = the number of patches per threadgroup.
153 * [9:15] = the number of output vertices per patch.
154 * [16:31] = the offset of per patch attributes in the buffer in bytes.
156 SI_PARAM_TCS_OFFCHIP_LAYOUT
= SI_NUM_RESOURCE_PARAMS
, /* for TCS & TES */
158 /* TCS only parameters. */
160 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
161 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
162 * [16:31] = TCS output patch0 offset for per-patch / 16, max = NUM_PATCHES*32*32* + 32*32
164 SI_PARAM_TCS_OUT_OFFSETS
,
166 /* Layout of TCS outputs / TES inputs:
167 * [0:12] = stride between output patches in dwords, num_outputs * num_vertices * 4, max = 32*32*4
168 * [13:20] = stride between output vertices in dwords = num_inputs * 4, max = 32*4
169 * [26:31] = gl_PatchVerticesIn, max = 32
171 SI_PARAM_TCS_OUT_LAYOUT
,
173 /* Layout of LS outputs / TCS inputs
174 * [0:12] = stride between patches in dwords = num_inputs * num_vertices * 4, max = 32*32*4
175 * [13:20] = stride between vertices in dwords = num_inputs * 4, max = 32*4
177 SI_PARAM_TCS_IN_LAYOUT
,
180 SI_PARAM_TESS_FACTOR_OFFSET
,
184 /* GS only parameters */
185 SI_PARAM_GS2VS_OFFSET
= SI_NUM_RESOURCE_PARAMS
,
187 SI_PARAM_VTX0_OFFSET
,
188 SI_PARAM_VTX1_OFFSET
,
189 SI_PARAM_PRIMITIVE_ID
,
190 SI_PARAM_VTX2_OFFSET
,
191 SI_PARAM_VTX3_OFFSET
,
192 SI_PARAM_VTX4_OFFSET
,
193 SI_PARAM_VTX5_OFFSET
,
194 SI_PARAM_GS_INSTANCE_ID
,
196 /* PS only parameters */
197 SI_PARAM_ALPHA_REF
= SI_NUM_RESOURCE_PARAMS
,
199 SI_PARAM_PERSP_SAMPLE
,
200 SI_PARAM_PERSP_CENTER
,
201 SI_PARAM_PERSP_CENTROID
,
202 SI_PARAM_PERSP_PULL_MODEL
,
203 SI_PARAM_LINEAR_SAMPLE
,
204 SI_PARAM_LINEAR_CENTER
,
205 SI_PARAM_LINEAR_CENTROID
,
206 SI_PARAM_LINE_STIPPLE_TEX
,
207 SI_PARAM_POS_X_FLOAT
,
208 SI_PARAM_POS_Y_FLOAT
,
209 SI_PARAM_POS_Z_FLOAT
,
210 SI_PARAM_POS_W_FLOAT
,
213 SI_PARAM_SAMPLE_COVERAGE
,
214 SI_PARAM_POS_FIXED_PT
,
216 /* CS only parameters */
217 SI_PARAM_GRID_SIZE
= SI_NUM_RESOURCE_PARAMS
,
221 SI_NUM_PARAMS
= SI_PARAM_POS_FIXED_PT
+ 9, /* +8 for COLOR[0..1] */
224 /* SI-specific system values. */
226 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
= TGSI_SEMANTIC_COUNT
,
227 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
,
232 /* A shader selector is a gallium CSO and contains shader variants and
233 * binaries for one TGSI program. This can be shared by multiple contexts.
235 struct si_shader_selector
{
237 struct si_shader
*first_variant
; /* immutable after the first variant */
238 struct si_shader
*last_variant
; /* mutable */
240 /* The compiled TGSI shader expecting a prolog and/or epilog (not
241 * uploaded to a buffer).
243 struct si_shader
*main_shader_part
;
245 struct tgsi_token
*tokens
;
246 struct pipe_stream_output_info so
;
247 struct tgsi_shader_info info
;
249 /* PIPE_SHADER_[VERTEX|FRAGMENT|...] */
253 unsigned esgs_itemsize
;
254 unsigned gs_input_verts_per_prim
;
255 unsigned gs_output_prim
;
256 unsigned gs_max_out_vertices
;
257 unsigned gs_num_invocations
;
258 unsigned max_gs_stream
; /* count - 1 */
259 unsigned gsvs_vertex_size
;
260 unsigned max_gsvs_emit_size
;
263 unsigned color_attr_index
[2];
264 unsigned db_shader_control
;
265 /* Set 0xf or 0x0 (4 bits) per each written output.
266 * ANDed with spi_shader_col_format.
268 unsigned colors_written_4bit
;
273 /* masks of "get_unique_index" bits */
274 uint64_t outputs_written
;
275 uint32_t patch_outputs_written
;
278 /* Valid shader configurations:
280 * API shaders VS | TCS | TES | GS |pass| PS
281 * are compiled as: | | | |thru|
283 * Only VS & PS: VS | -- | -- | -- | -- | PS
284 * With GS: ES | -- | -- | GS | VS | PS
285 * With Tessel.: LS | HS | VS | -- | -- | PS
286 * With both: LS | HS | ES | GS | VS | PS
289 /* Common VS bits between the shader key and the prolog key. */
290 struct si_vs_prolog_bits
{
291 unsigned instance_divisors
[SI_NUM_VERTEX_BUFFERS
];
294 /* Common VS bits between the shader key and the epilog key. */
295 struct si_vs_epilog_bits
{
296 unsigned export_prim_id
:1; /* when PS needs it and GS is disabled */
298 * - skip clipdist, culldist (including clipvertex code) exports based
299 * on which clip_plane_enable bits are set
300 * - skip layer, viewport, clipdist, and culldist parameter exports
301 * if PS doesn't read them
305 /* Common TCS bits between the shader key and the epilog key. */
306 struct si_tcs_epilog_bits
{
307 unsigned prim_mode
:3;
308 uint64_t inputs_to_copy
;
311 /* Common PS bits between the shader key and the prolog key. */
312 struct si_ps_prolog_bits
{
313 unsigned color_two_side
:1;
314 /* TODO: add a flatshade bit that skips interpolation for colors */
315 unsigned poly_stipple
:1;
316 unsigned force_persample_interp
:1;
318 * - add force_center_interp if MSAA is disabled and centroid or
320 * - add force_center_interp_bc_optimize to force center interpolation
321 * based on the bc_optimize SGPR bit if MSAA is enabled, centroid is
322 * present and sample isn't present.
326 /* Common PS bits between the shader key and the epilog key. */
327 struct si_ps_epilog_bits
{
328 unsigned spi_shader_col_format
;
329 unsigned color_is_int8
:8;
330 unsigned last_cbuf
:3;
331 unsigned alpha_func
:3;
332 unsigned alpha_to_one
:1;
333 unsigned poly_line_smoothing
:1;
334 unsigned clamp_color
:1;
337 union si_shader_part_key
{
339 struct si_vs_prolog_bits states
;
340 unsigned num_input_sgprs
:5;
341 unsigned last_input
:4;
344 struct si_vs_epilog_bits states
;
345 unsigned prim_id_param_offset
:5;
348 struct si_tcs_epilog_bits states
;
351 struct si_ps_prolog_bits states
;
352 unsigned num_input_sgprs
:5;
353 unsigned num_input_vgprs
:5;
354 /* Color interpolation and two-side color selection. */
355 unsigned colors_read
:8; /* color input components read */
356 unsigned num_interp_inputs
:5; /* BCOLOR is at this location */
357 unsigned face_vgpr_index
:5;
359 char color_attr_index
[2];
360 char color_interp_vgpr_index
[2]; /* -1 == constant */
363 struct si_ps_epilog_bits states
;
364 unsigned colors_written
:8;
366 unsigned writes_stencil
:1;
367 unsigned writes_samplemask
:1;
371 union si_shader_key
{
373 struct si_ps_prolog_bits prolog
;
374 struct si_ps_epilog_bits epilog
;
377 struct si_vs_prolog_bits prolog
;
378 struct si_vs_epilog_bits epilog
;
379 unsigned as_es
:1; /* export shader */
380 unsigned as_ls
:1; /* local shader */
383 struct si_tcs_epilog_bits epilog
;
384 } tcs
; /* tessellation control shader */
386 struct si_vs_epilog_bits epilog
; /* same as VS */
387 unsigned as_es
:1; /* export shader */
388 } tes
; /* tessellation evaluation shader */
391 struct si_shader_config
{
395 unsigned spi_ps_input_ena
;
396 unsigned spi_ps_input_addr
;
398 unsigned scratch_bytes_per_wave
;
403 /* GCN-specific shader info. */
404 struct si_shader_info
{
405 ubyte vs_output_param_offset
[SI_MAX_VS_OUTPUTS
];
406 ubyte num_input_sgprs
;
407 ubyte num_input_vgprs
;
408 char face_vgpr_index
;
409 bool uses_instanceid
;
410 ubyte nr_pos_exports
;
411 ubyte nr_param_exports
;
415 struct si_shader_selector
*selector
;
416 struct si_shader
*next_variant
;
418 struct si_shader_part
*prolog
;
419 struct si_shader_part
*epilog
;
421 struct si_shader
*gs_copy_shader
;
422 struct si_pm4_state
*pm4
;
423 struct r600_resource
*bo
;
424 struct r600_resource
*scratch_bo
;
425 union si_shader_key key
;
426 bool is_binary_shared
;
429 /* The following data is all that's needed for binary shaders. */
430 struct radeon_shader_binary binary
;
431 struct si_shader_config config
;
432 struct si_shader_info info
;
435 struct si_shader_part
{
436 struct si_shader_part
*next
;
437 union si_shader_part_key key
;
438 struct radeon_shader_binary binary
;
439 struct si_shader_config config
;
442 static inline struct tgsi_shader_info
*si_get_vs_info(struct si_context
*sctx
)
444 if (sctx
->gs_shader
.cso
)
445 return &sctx
->gs_shader
.cso
->info
;
446 else if (sctx
->tes_shader
.cso
)
447 return &sctx
->tes_shader
.cso
->info
;
448 else if (sctx
->vs_shader
.cso
)
449 return &sctx
->vs_shader
.cso
->info
;
454 static inline struct si_shader
* si_get_vs_state(struct si_context
*sctx
)
456 if (sctx
->gs_shader
.current
)
457 return sctx
->gs_shader
.current
->gs_copy_shader
;
458 else if (sctx
->tes_shader
.current
)
459 return sctx
->tes_shader
.current
;
461 return sctx
->vs_shader
.current
;
464 static inline bool si_vs_exports_prim_id(struct si_shader
*shader
)
466 if (shader
->selector
->type
== PIPE_SHADER_VERTEX
)
467 return shader
->key
.vs
.epilog
.export_prim_id
;
468 else if (shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
)
469 return shader
->key
.tes
.epilog
.export_prim_id
;
475 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
476 LLVMTargetMachineRef tm
,
477 struct si_shader
*shader
,
479 struct pipe_debug_callback
*debug
);
480 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
481 struct si_shader
*shader
,
482 struct pipe_debug_callback
*debug
);
483 void si_dump_shader_key(unsigned shader
, union si_shader_key
*key
, FILE *f
);
484 int si_compile_llvm(struct si_screen
*sscreen
,
485 struct radeon_shader_binary
*binary
,
486 struct si_shader_config
*conf
,
487 LLVMTargetMachineRef tm
,
489 struct pipe_debug_callback
*debug
,
492 void si_shader_destroy(struct si_shader
*shader
);
493 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
);
494 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
);
495 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
496 struct pipe_debug_callback
*debug
, unsigned processor
,
498 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
499 struct si_shader
*shader
,
500 struct si_shader_config
*config
,
501 uint64_t scratch_va
);
502 void si_shader_binary_read_config(struct radeon_shader_binary
*binary
,
503 struct si_shader_config
*conf
,
504 unsigned symbol_offset
);