radeonsi: make SI_SGPR_VERTEX_BUFFERS the last user SGPR input
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.h
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /* The compiler middle-end architecture: Explaining (non-)monolithic shaders
25 * -------------------------------------------------------------------------
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 in
29 * the driver.
30 *
31 * The problem with that is that we also have to emulate some API states
32 * (e.g. alpha-test, and many others) in shaders too. The two obvious ways
33 * 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 shader
36 * cache for good performance
37 * - patch shaders at the binary level
38 *
39 * This driver uses something completely different. The emulated states are
40 * usually implemented at the beginning or end of shaders. Therefore, we can
41 * 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 are
47 * concatenated. This type of shader is called non-monolithic, because it
48 * consists of multiple independent binaries. Creating a new shader variant
49 * is therefore only a concatenation of shader parts (binaries) and doesn't
50 * involve any compilation. The main shader parts are the only parts that are
51 * compiled when applications create shader objects. The prolog and epilog
52 * parts are compiled on the first use and saved, so that their binaries can
53 * be reused by many other shaders.
54 *
55 * One of the roles of the prolog part is to compute vertex buffer addresses
56 * for vertex shaders. A few of the roles of the epilog part are color buffer
57 * format conversions in pixel shaders that we have to do manually, and write
58 * tessellation factors in tessellation control shaders. The prolog and epilog
59 * 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 LLVM
63 * compilation, and the whole thing is compiled and optimized as one unit with
64 * one binary on the output. The result is the same as the non-monolithic
65 * shader, but the final code can be better, because LLVM can optimize across
66 * all shader parts. Monolithic shaders aren't usually used except for these
67 * special cases:
68 *
69 * 1) Some rarely-used states require modification of the main shader part
70 * itself, and in such cases, only the monolithic shader variant is
71 * compiled, and that's always done on the first use.
72 *
73 * 2) When we do cross-stage optimizations for separate shader objects and
74 * e.g. eliminate unused shader varyings, the resulting optimized shader
75 * variants are always compiled as monolithic shaders, and always
76 * asynchronously (i.e. not stalling ongoing rendering). We call them
77 * "optimized monolithic" shaders. The important property here is that
78 * the non-monolithic unoptimized shader variant is always available for use
79 * when the asynchronous compilation of the optimized shader is not done
80 * yet.
81 *
82 * Starting with GFX9 chips, some shader stages are merged, and the number of
83 * shader parts per shader increased. The complete new list of shader parts is:
84 * - 1st shader: prolog part
85 * - 1st shader: main part
86 * - 2nd shader: prolog part
87 * - 2nd shader: main part
88 * - 2nd shader: epilog part
89 */
90
91 /* How linking shader inputs and outputs between vertex, tessellation, and
92 * geometry shaders works.
93 *
94 * Inputs and outputs between shaders are stored in a buffer. This buffer
95 * lives in LDS (typical case for tessellation), but it can also live
96 * 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 Location
103 *
104 * POSITION 0
105 * PSIZE 1
106 * CLIPDIST0..1 2..3
107 * CULLDIST0..1 (not implemented)
108 * GENERIC0..31 4..35
109 *
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 Location
115 *
116 * TESSOUTER 0
117 * TESSINNER 1
118 * PATCH0..29 2..31
119 *
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 and
124 * output addresses like the vertex stride, the patch stride, and the offsets
125 * where per-vertex and per-patch data start, is passed to the shader via
126 * user data SGPRs. The offsets and strides are calculated at draw time and
127 * aren't available at compile time.
128 */
129
130 #ifndef SI_SHADER_H
131 #define SI_SHADER_H
132
133 #include <llvm-c/Core.h> /* LLVMModuleRef */
134 #include <llvm-c/TargetMachine.h>
135 #include "tgsi/tgsi_scan.h"
136 #include "util/u_queue.h"
137
138 #include "ac_binary.h"
139 #include "ac_llvm_build.h"
140 #include "si_state.h"
141
142 struct nir_shader;
143
144 #define SI_MAX_VS_OUTPUTS 40
145
146 /* Shader IO unique indices are supported for TGSI_SEMANTIC_GENERIC with an
147 * index smaller than this.
148 */
149 #define SI_MAX_IO_GENERIC 46
150
151 /* SGPR user data indices */
152 enum {
153 SI_SGPR_RW_BUFFERS, /* rings (& stream-out, VS only) */
154 #if !HAVE_32BIT_POINTERS
155 SI_SGPR_RW_BUFFERS_HI,
156 #endif
157 SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES,
158 #if !HAVE_32BIT_POINTERS
159 SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES_HI,
160 #endif
161 SI_SGPR_CONST_AND_SHADER_BUFFERS, /* or just a constant buffer 0 pointer */
162 #if !HAVE_32BIT_POINTERS
163 SI_SGPR_CONST_AND_SHADER_BUFFERS_HI,
164 #endif
165 SI_SGPR_SAMPLERS_AND_IMAGES,
166 #if !HAVE_32BIT_POINTERS
167 SI_SGPR_SAMPLERS_AND_IMAGES_HI,
168 #endif
169 SI_NUM_RESOURCE_SGPRS,
170
171 /* all VS variants */
172 SI_SGPR_BASE_VERTEX = SI_NUM_RESOURCE_SGPRS,
173 SI_SGPR_START_INSTANCE,
174 SI_SGPR_DRAWID,
175 SI_SGPR_VS_STATE_BITS,
176 SI_VS_NUM_USER_SGPR,
177
178 SI_SGPR_VS_BLIT_DATA = SI_SGPR_CONST_AND_SHADER_BUFFERS,
179
180 /* TES */
181 SI_SGPR_TES_OFFCHIP_LAYOUT = SI_NUM_RESOURCE_SGPRS,
182 SI_SGPR_TES_OFFCHIP_ADDR,
183 SI_TES_NUM_USER_SGPR,
184
185 /* GFX6-8: TCS only */
186 GFX6_SGPR_TCS_OFFCHIP_LAYOUT = SI_NUM_RESOURCE_SGPRS,
187 GFX6_SGPR_TCS_OUT_OFFSETS,
188 GFX6_SGPR_TCS_OUT_LAYOUT,
189 GFX6_SGPR_TCS_IN_LAYOUT,
190 GFX6_TCS_NUM_USER_SGPR,
191
192 /* GFX9: Merged shaders. */
193 #if HAVE_32BIT_POINTERS
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). */
196 GFX9_MERGED_NUM_USER_SGPR = SI_VS_NUM_USER_SGPR,
197 #else
198 /* 2ND_CONST_AND_SHADER_BUFFERS is set in USER_DATA_ADDR_LO/HI (SGPR[0:1]). */
199 GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES = SI_VS_NUM_USER_SGPR,
200 GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES_HI,
201 GFX9_MERGED_NUM_USER_SGPR,
202 #endif
203
204 /* GFX9: Merged LS-HS (VS-TCS) only. */
205 GFX9_SGPR_TCS_OFFCHIP_LAYOUT = GFX9_MERGED_NUM_USER_SGPR,
206 GFX9_SGPR_TCS_OUT_OFFSETS,
207 GFX9_SGPR_TCS_OUT_LAYOUT,
208 #if !HAVE_32BIT_POINTERS
209 GFX9_SGPR_align_for_vb_pointer,
210 #endif
211 GFX9_TCS_NUM_USER_SGPR,
212
213 /* GS limits */
214 GFX6_GS_NUM_USER_SGPR = SI_NUM_RESOURCE_SGPRS,
215 GFX9_GS_NUM_USER_SGPR = GFX9_MERGED_NUM_USER_SGPR,
216 SI_GSCOPY_NUM_USER_SGPR = SI_SGPR_RW_BUFFERS + (HAVE_32BIT_POINTERS ? 1 : 2),
217
218 /* PS only */
219 SI_SGPR_ALPHA_REF = SI_NUM_RESOURCE_SGPRS,
220 SI_PS_NUM_USER_SGPR,
221 };
222
223 /* LLVM function parameter indices */
224 enum {
225 SI_NUM_RESOURCE_PARAMS = 4,
226
227 /* PS only parameters */
228 SI_PARAM_ALPHA_REF = SI_NUM_RESOURCE_PARAMS,
229 SI_PARAM_PRIM_MASK,
230 SI_PARAM_PERSP_SAMPLE,
231 SI_PARAM_PERSP_CENTER,
232 SI_PARAM_PERSP_CENTROID,
233 SI_PARAM_PERSP_PULL_MODEL,
234 SI_PARAM_LINEAR_SAMPLE,
235 SI_PARAM_LINEAR_CENTER,
236 SI_PARAM_LINEAR_CENTROID,
237 SI_PARAM_LINE_STIPPLE_TEX,
238 SI_PARAM_POS_X_FLOAT,
239 SI_PARAM_POS_Y_FLOAT,
240 SI_PARAM_POS_Z_FLOAT,
241 SI_PARAM_POS_W_FLOAT,
242 SI_PARAM_FRONT_FACE,
243 SI_PARAM_ANCILLARY,
244 SI_PARAM_SAMPLE_COVERAGE,
245 SI_PARAM_POS_FIXED_PT,
246
247 SI_NUM_PARAMS = SI_PARAM_POS_FIXED_PT + 9, /* +8 for COLOR[0..1] */
248 };
249
250 /* Fields of driver-defined VS state SGPR. */
251 /* Clamp vertex color output (only used in VS as VS). */
252 #define S_VS_STATE_CLAMP_VERTEX_COLOR(x) (((unsigned)(x) & 0x1) << 0)
253 #define C_VS_STATE_CLAMP_VERTEX_COLOR 0xFFFFFFFE
254 #define S_VS_STATE_INDEXED(x) (((unsigned)(x) & 0x1) << 1)
255 #define C_VS_STATE_INDEXED 0xFFFFFFFD
256 #define S_VS_STATE_LS_OUT_PATCH_SIZE(x) (((unsigned)(x) & 0x1FFF) << 8)
257 #define C_VS_STATE_LS_OUT_PATCH_SIZE 0xFFE000FF
258 #define S_VS_STATE_LS_OUT_VERTEX_SIZE(x) (((unsigned)(x) & 0xFF) << 24)
259 #define C_VS_STATE_LS_OUT_VERTEX_SIZE 0x00FFFFFF
260
261 /* SI-specific system values. */
262 enum {
263 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI = TGSI_SEMANTIC_COUNT,
264 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI,
265 };
266
267 enum {
268 /* Use a property enum that VS wouldn't use. */
269 TGSI_PROPERTY_VS_BLIT_SGPRS = TGSI_PROPERTY_FS_COORD_ORIGIN,
270
271 /* These represent the number of SGPRs the shader uses. */
272 SI_VS_BLIT_SGPRS_POS = 3,
273 SI_VS_BLIT_SGPRS_POS_COLOR = 7,
274 SI_VS_BLIT_SGPRS_POS_TEXCOORD = 9,
275 };
276
277 /* For VS shader key fix_fetch. */
278 enum {
279 SI_FIX_FETCH_NONE = 0,
280 SI_FIX_FETCH_A2_SNORM,
281 SI_FIX_FETCH_A2_SSCALED,
282 SI_FIX_FETCH_A2_SINT,
283 SI_FIX_FETCH_RGBA_32_UNORM,
284 SI_FIX_FETCH_RGBX_32_UNORM,
285 SI_FIX_FETCH_RGBA_32_SNORM,
286 SI_FIX_FETCH_RGBX_32_SNORM,
287 SI_FIX_FETCH_RGBA_32_USCALED,
288 SI_FIX_FETCH_RGBA_32_SSCALED,
289 SI_FIX_FETCH_RGBA_32_FIXED,
290 SI_FIX_FETCH_RGBX_32_FIXED,
291 SI_FIX_FETCH_RG_64_FLOAT,
292 SI_FIX_FETCH_RGB_64_FLOAT,
293 SI_FIX_FETCH_RGBA_64_FLOAT,
294 SI_FIX_FETCH_RGB_8, /* A = 1.0 */
295 SI_FIX_FETCH_RGB_8_INT, /* A = 1 */
296 SI_FIX_FETCH_RGB_16,
297 SI_FIX_FETCH_RGB_16_INT,
298 };
299
300 struct si_shader;
301
302 /* State of the context creating the shader object. */
303 struct si_compiler_ctx_state {
304 /* Should only be used by si_init_shader_selector_async and
305 * si_build_shader_variant if thread_index == -1 (non-threaded). */
306 LLVMTargetMachineRef tm;
307
308 /* Used if thread_index == -1 or if debug.async is true. */
309 struct pipe_debug_callback debug;
310
311 /* Used for creating the log string for gallium/ddebug. */
312 bool is_debug_context;
313 };
314
315 /* A shader selector is a gallium CSO and contains shader variants and
316 * binaries for one TGSI program. This can be shared by multiple contexts.
317 */
318 struct si_shader_selector {
319 struct pipe_reference reference;
320 struct si_screen *screen;
321 struct util_queue_fence ready;
322 struct si_compiler_ctx_state compiler_ctx_state;
323
324 mtx_t mutex;
325 struct si_shader *first_variant; /* immutable after the first variant */
326 struct si_shader *last_variant; /* mutable */
327
328 /* The compiled TGSI shader expecting a prolog and/or epilog (not
329 * uploaded to a buffer).
330 */
331 struct si_shader *main_shader_part;
332 struct si_shader *main_shader_part_ls; /* as_ls is set in the key */
333 struct si_shader *main_shader_part_es; /* as_es is set in the key */
334
335 struct si_shader *gs_copy_shader;
336
337 struct tgsi_token *tokens;
338 struct nir_shader *nir;
339 struct pipe_stream_output_info so;
340 struct tgsi_shader_info info;
341 struct tgsi_tessctrl_info tcs_info;
342
343 /* PIPE_SHADER_[VERTEX|FRAGMENT|...] */
344 unsigned type;
345 bool vs_needs_prolog;
346 bool force_correct_derivs_after_kill;
347 unsigned pa_cl_vs_out_cntl;
348 ubyte clipdist_mask;
349 ubyte culldist_mask;
350
351 /* ES parameters. */
352 unsigned esgs_itemsize;
353
354 /* GS parameters. */
355 unsigned gs_input_verts_per_prim;
356 unsigned gs_output_prim;
357 unsigned gs_max_out_vertices;
358 unsigned gs_num_invocations;
359 unsigned max_gs_stream; /* count - 1 */
360 unsigned gsvs_vertex_size;
361 unsigned max_gsvs_emit_size;
362 unsigned enabled_streamout_buffer_mask;
363
364 /* PS parameters. */
365 unsigned color_attr_index[2];
366 unsigned db_shader_control;
367 /* Set 0xf or 0x0 (4 bits) per each written output.
368 * ANDed with spi_shader_col_format.
369 */
370 unsigned colors_written_4bit;
371
372 /* CS parameters */
373 unsigned local_size;
374
375 uint64_t outputs_written; /* "get_unique_index" bits */
376 uint32_t patch_outputs_written; /* "get_unique_index_patch" bits */
377
378 uint64_t inputs_read; /* "get_unique_index" bits */
379
380 /* bitmasks of used descriptor slots */
381 uint32_t active_const_and_shader_buffers;
382 uint64_t active_samplers_and_images;
383 };
384
385 /* Valid shader configurations:
386 *
387 * API shaders VS | TCS | TES | GS |pass| PS
388 * are compiled as: | | | |thru|
389 * | | | | |
390 * Only VS & PS: VS | | | | | PS
391 * GFX6 - with GS: ES | | | GS | VS | PS
392 * - with tess: LS | HS | VS | | | PS
393 * - with both: LS | HS | ES | GS | VS | PS
394 * GFX9 - with GS: -> | | | GS | VS | PS
395 * - with tess: -> | HS | VS | | | PS
396 * - with both: -> | HS | -> | GS | VS | PS
397 *
398 * -> = merged with the next stage
399 */
400
401 /* Use the byte alignment for all following structure members for optimal
402 * shader key memory footprint.
403 */
404 #pragma pack(push, 1)
405
406 /* Common VS bits between the shader key and the prolog key. */
407 struct si_vs_prolog_bits {
408 /* - If neither "is_one" nor "is_fetched" has a bit set, the instance
409 * divisor is 0.
410 * - If "is_one" has a bit set, the instance divisor is 1.
411 * - If "is_fetched" has a bit set, the instance divisor will be loaded
412 * from the constant buffer.
413 */
414 uint16_t instance_divisor_is_one; /* bitmask of inputs */
415 uint16_t instance_divisor_is_fetched; /* bitmask of inputs */
416 unsigned ls_vgpr_fix:1;
417 };
418
419 /* Common TCS bits between the shader key and the epilog key. */
420 struct si_tcs_epilog_bits {
421 unsigned prim_mode:3;
422 unsigned invoc0_tess_factors_are_def:1;
423 unsigned tes_reads_tess_factors:1;
424 };
425
426 struct si_gs_prolog_bits {
427 unsigned tri_strip_adj_fix:1;
428 };
429
430 /* Common PS bits between the shader key and the prolog key. */
431 struct si_ps_prolog_bits {
432 unsigned color_two_side:1;
433 unsigned flatshade_colors:1;
434 unsigned poly_stipple:1;
435 unsigned force_persp_sample_interp:1;
436 unsigned force_linear_sample_interp:1;
437 unsigned force_persp_center_interp:1;
438 unsigned force_linear_center_interp:1;
439 unsigned bc_optimize_for_persp:1;
440 unsigned bc_optimize_for_linear:1;
441 unsigned samplemask_log_ps_iter:3;
442 };
443
444 /* Common PS bits between the shader key and the epilog key. */
445 struct si_ps_epilog_bits {
446 unsigned spi_shader_col_format;
447 unsigned color_is_int8:8;
448 unsigned color_is_int10:8;
449 unsigned last_cbuf:3;
450 unsigned alpha_func:3;
451 unsigned alpha_to_one:1;
452 unsigned poly_line_smoothing:1;
453 unsigned clamp_color:1;
454 };
455
456 union si_shader_part_key {
457 struct {
458 struct si_vs_prolog_bits states;
459 unsigned num_input_sgprs:6;
460 /* For merged stages such as LS-HS, HS input VGPRs are first. */
461 unsigned num_merged_next_stage_vgprs:3;
462 unsigned last_input:4;
463 unsigned as_ls:1;
464 unsigned as_es:1;
465 /* Prologs for monolithic shaders shouldn't set EXEC. */
466 unsigned is_monolithic:1;
467 } vs_prolog;
468 struct {
469 struct si_tcs_epilog_bits states;
470 } tcs_epilog;
471 struct {
472 struct si_gs_prolog_bits states;
473 /* Prologs of monolithic shaders shouldn't set EXEC. */
474 unsigned is_monolithic:1;
475 } gs_prolog;
476 struct {
477 struct si_ps_prolog_bits states;
478 unsigned num_input_sgprs:6;
479 unsigned num_input_vgprs:5;
480 /* Color interpolation and two-side color selection. */
481 unsigned colors_read:8; /* color input components read */
482 unsigned num_interp_inputs:5; /* BCOLOR is at this location */
483 unsigned face_vgpr_index:5;
484 unsigned ancillary_vgpr_index:5;
485 unsigned wqm:1;
486 char color_attr_index[2];
487 char color_interp_vgpr_index[2]; /* -1 == constant */
488 } ps_prolog;
489 struct {
490 struct si_ps_epilog_bits states;
491 unsigned colors_written:8;
492 unsigned writes_z:1;
493 unsigned writes_stencil:1;
494 unsigned writes_samplemask:1;
495 } ps_epilog;
496 };
497
498 struct si_shader_key {
499 /* Prolog and epilog flags. */
500 union {
501 struct {
502 struct si_vs_prolog_bits prolog;
503 } vs;
504 struct {
505 struct si_vs_prolog_bits ls_prolog; /* for merged LS-HS */
506 struct si_shader_selector *ls; /* for merged LS-HS */
507 struct si_tcs_epilog_bits epilog;
508 } tcs; /* tessellation control shader */
509 struct {
510 struct si_vs_prolog_bits vs_prolog; /* for merged ES-GS */
511 struct si_shader_selector *es; /* for merged ES-GS */
512 struct si_gs_prolog_bits prolog;
513 } gs;
514 struct {
515 struct si_ps_prolog_bits prolog;
516 struct si_ps_epilog_bits epilog;
517 } ps;
518 } part;
519
520 /* These two are initially set according to the NEXT_SHADER property,
521 * or guessed if the property doesn't seem correct.
522 */
523 unsigned as_es:1; /* export shader, which precedes GS */
524 unsigned as_ls:1; /* local shader, which precedes TCS */
525
526 /* Flags for monolithic compilation only. */
527 struct {
528 /* One byte for every input: SI_FIX_FETCH_* enums. */
529 uint8_t vs_fix_fetch[SI_MAX_ATTRIBS];
530
531 union {
532 uint64_t ff_tcs_inputs_to_copy; /* for fixed-func TCS */
533 /* When PS needs PrimID and GS is disabled. */
534 unsigned vs_export_prim_id:1;
535 struct {
536 unsigned interpolate_at_sample_force_center:1;
537 } ps;
538 } u;
539 } mono;
540
541 /* Optimization flags for asynchronous compilation only. */
542 struct {
543 /* For HW VS (it can be VS, TES, GS) */
544 uint64_t kill_outputs; /* "get_unique_index" bits */
545 unsigned clip_disable:1;
546
547 /* For shaders where monolithic variants have better code.
548 *
549 * This is a flag that has no effect on code generation,
550 * but forces monolithic shaders to be used as soon as
551 * possible, because it's in the "opt" group.
552 */
553 unsigned prefer_mono:1;
554 } opt;
555 };
556
557 /* Restore the pack alignment to default. */
558 #pragma pack(pop)
559
560 struct si_shader_config {
561 unsigned num_sgprs;
562 unsigned num_vgprs;
563 unsigned spilled_sgprs;
564 unsigned spilled_vgprs;
565 unsigned private_mem_vgprs;
566 unsigned lds_size;
567 unsigned max_simd_waves;
568 unsigned spi_ps_input_ena;
569 unsigned spi_ps_input_addr;
570 unsigned float_mode;
571 unsigned scratch_bytes_per_wave;
572 unsigned rsrc1;
573 unsigned rsrc2;
574 };
575
576 /* GCN-specific shader info. */
577 struct si_shader_info {
578 ubyte vs_output_param_offset[SI_MAX_VS_OUTPUTS];
579 ubyte num_input_sgprs;
580 ubyte num_input_vgprs;
581 signed char face_vgpr_index;
582 signed char ancillary_vgpr_index;
583 bool uses_instanceid;
584 ubyte nr_pos_exports;
585 ubyte nr_param_exports;
586 };
587
588 struct si_shader {
589 struct si_compiler_ctx_state compiler_ctx_state;
590
591 struct si_shader_selector *selector;
592 struct si_shader_selector *previous_stage_sel; /* for refcounting */
593 struct si_shader *next_variant;
594
595 struct si_shader_part *prolog;
596 struct si_shader *previous_stage; /* for GFX9 */
597 struct si_shader_part *prolog2;
598 struct si_shader_part *epilog;
599
600 struct si_pm4_state *pm4;
601 struct r600_resource *bo;
602 struct r600_resource *scratch_bo;
603 struct si_shader_key key;
604 struct util_queue_fence ready;
605 bool compilation_failed;
606 bool is_monolithic;
607 bool is_optimized;
608 bool is_binary_shared;
609 bool is_gs_copy_shader;
610
611 /* The following data is all that's needed for binary shaders. */
612 struct ac_shader_binary binary;
613 struct si_shader_config config;
614 struct si_shader_info info;
615
616 /* Shader key + LLVM IR + disassembly + statistics.
617 * Generated for debug contexts only.
618 */
619 char *shader_log;
620 size_t shader_log_size;
621 };
622
623 struct si_shader_part {
624 struct si_shader_part *next;
625 union si_shader_part_key key;
626 struct ac_shader_binary binary;
627 struct si_shader_config config;
628 };
629
630 /* si_shader.c */
631 struct si_shader *
632 si_generate_gs_copy_shader(struct si_screen *sscreen,
633 LLVMTargetMachineRef tm,
634 struct si_shader_selector *gs_selector,
635 struct pipe_debug_callback *debug);
636 int si_compile_tgsi_shader(struct si_screen *sscreen,
637 LLVMTargetMachineRef tm,
638 struct si_shader *shader,
639 bool is_monolithic,
640 struct pipe_debug_callback *debug);
641 int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
642 struct si_shader *shader,
643 struct pipe_debug_callback *debug);
644 void si_shader_destroy(struct si_shader *shader);
645 unsigned si_shader_io_get_unique_index_patch(unsigned semantic_name, unsigned index);
646 unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index);
647 int si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader);
648 void si_shader_dump(struct si_screen *sscreen, const struct si_shader *shader,
649 struct pipe_debug_callback *debug, unsigned processor,
650 FILE *f, bool check_debug_option);
651 void si_shader_dump_stats_for_shader_db(const struct si_shader *shader,
652 struct pipe_debug_callback *debug);
653 void si_multiwave_lds_size_workaround(struct si_screen *sscreen,
654 unsigned *lds_size);
655 void si_shader_apply_scratch_relocs(struct si_shader *shader,
656 uint64_t scratch_va);
657 void si_shader_binary_read_config(struct ac_shader_binary *binary,
658 struct si_shader_config *conf,
659 unsigned symbol_offset);
660 const char *si_get_shader_name(const struct si_shader *shader, unsigned processor);
661
662 /* si_shader_nir.c */
663 void si_nir_scan_shader(const struct nir_shader *nir,
664 struct tgsi_shader_info *info);
665 void si_nir_scan_tess_ctrl(const struct nir_shader *nir,
666 const struct tgsi_shader_info *info,
667 struct tgsi_tessctrl_info *out);
668 void si_lower_nir(struct si_shader_selector *sel);
669
670 /* Inline helpers. */
671
672 /* Return the pointer to the main shader part's pointer. */
673 static inline struct si_shader **
674 si_get_main_shader_part(struct si_shader_selector *sel,
675 struct si_shader_key *key)
676 {
677 if (key->as_ls)
678 return &sel->main_shader_part_ls;
679 if (key->as_es)
680 return &sel->main_shader_part_es;
681 return &sel->main_shader_part;
682 }
683
684 static inline bool
685 si_shader_uses_bindless_samplers(struct si_shader_selector *selector)
686 {
687 return selector ? selector->info.uses_bindless_samplers : false;
688 }
689
690 static inline bool
691 si_shader_uses_bindless_images(struct si_shader_selector *selector)
692 {
693 return selector ? selector->info.uses_bindless_images : false;
694 }
695
696 void si_destroy_shader_selector(struct si_context *sctx,
697 struct si_shader_selector *sel);
698
699 static inline void
700 si_shader_selector_reference(struct si_context *sctx,
701 struct si_shader_selector **dst,
702 struct si_shader_selector *src)
703 {
704 if (pipe_reference(&(*dst)->reference, &src->reference))
705 si_destroy_shader_selector(sctx, *dst);
706
707 *dst = src;
708 }
709
710 #endif