radeonsi: clean up lucky #include dependencies
[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 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
29 /* How linking shader inputs and outputs between vertex, tessellation, and
30 * geometry shaders works.
31 *
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.
36 *
37 * Since GS and tessellation are only possible in the OpenGL core profile,
38 * only these semantics are valid for per-vertex data:
39 *
40 * Name Location
41 *
42 * POSITION 0
43 * PSIZE 1
44 * CLIPDIST0..1 2..3
45 * CULLDIST0..1 (not implemented)
46 * GENERIC0..31 4..35
47 *
48 * For example, a shader only writing GENERIC0 has the output stride of 5.
49 *
50 * Only these semantics are valid for per-patch data:
51 *
52 * Name Location
53 *
54 * TESSOUTER 0
55 * TESSINNER 1
56 * PATCH0..29 2..31
57 *
58 * That's how independent shaders agree on input and output locations.
59 * The si_shader_io_get_unique_index function assigns the locations.
60 *
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.
66 */
67
68 #ifndef SI_SHADER_H
69 #define SI_SHADER_H
70
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"
75 #include "si_state.h"
76
77 struct radeon_shader_binary;
78 struct radeon_shader_reloc;
79
80 #define SI_MAX_VS_OUTPUTS 40
81
82 /* SGPR user data indices */
83 enum {
84 SI_SGPR_RW_BUFFERS, /* rings (& stream-out, VS only) */
85 SI_SGPR_RW_BUFFERS_HI,
86 SI_SGPR_CONST_BUFFERS,
87 SI_SGPR_CONST_BUFFERS_HI,
88 SI_SGPR_SAMPLERS, /* images & sampler states interleaved */
89 SI_SGPR_SAMPLERS_HI,
90 SI_SGPR_IMAGES,
91 SI_SGPR_IMAGES_HI,
92 SI_SGPR_SHADER_BUFFERS,
93 SI_SGPR_SHADER_BUFFERS_HI,
94 SI_NUM_RESOURCE_SGPRS,
95
96 /* all VS variants */
97 SI_SGPR_VERTEX_BUFFERS = SI_NUM_RESOURCE_SGPRS,
98 SI_SGPR_VERTEX_BUFFERS_HI,
99 SI_SGPR_BASE_VERTEX,
100 SI_SGPR_START_INSTANCE,
101 SI_SGPR_DRAWID,
102 SI_ES_NUM_USER_SGPR,
103
104 /* hw VS only */
105 SI_SGPR_VS_STATE_BITS = SI_ES_NUM_USER_SGPR,
106 SI_VS_NUM_USER_SGPR,
107
108 /* hw LS only */
109 SI_SGPR_LS_OUT_LAYOUT = SI_ES_NUM_USER_SGPR,
110 SI_LS_NUM_USER_SGPR,
111
112 /* both TCS and TES */
113 SI_SGPR_TCS_OFFCHIP_LAYOUT = SI_NUM_RESOURCE_SGPRS,
114 SI_TES_NUM_USER_SGPR,
115
116 /* TCS only */
117 SI_SGPR_TCS_OUT_OFFSETS = SI_TES_NUM_USER_SGPR,
118 SI_SGPR_TCS_OUT_LAYOUT,
119 SI_SGPR_TCS_IN_LAYOUT,
120 SI_TCS_NUM_USER_SGPR,
121
122 /* GS limits */
123 SI_GS_NUM_USER_SGPR = SI_NUM_RESOURCE_SGPRS,
124 SI_GSCOPY_NUM_USER_SGPR = SI_SGPR_RW_BUFFERS_HI + 1,
125
126 /* PS only */
127 SI_SGPR_ALPHA_REF = SI_NUM_RESOURCE_SGPRS,
128 SI_PS_NUM_USER_SGPR,
129
130 /* CS only */
131 SI_SGPR_GRID_SIZE = SI_NUM_RESOURCE_SGPRS,
132 SI_CS_NUM_USER_SGPR = SI_SGPR_GRID_SIZE + 3
133 };
134
135 /* LLVM function parameter indices */
136 enum {
137 SI_PARAM_RW_BUFFERS,
138 SI_PARAM_CONST_BUFFERS,
139 SI_PARAM_SAMPLERS,
140 SI_PARAM_IMAGES,
141 SI_PARAM_SHADER_BUFFERS,
142 SI_NUM_RESOURCE_PARAMS,
143
144 /* VS only parameters */
145 SI_PARAM_VERTEX_BUFFERS = SI_NUM_RESOURCE_PARAMS,
146 SI_PARAM_BASE_VERTEX,
147 SI_PARAM_START_INSTANCE,
148 SI_PARAM_DRAWID,
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 */
154
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.
159 */
160 SI_PARAM_TCS_OFFCHIP_LAYOUT = SI_NUM_RESOURCE_PARAMS, /* for TCS & TES */
161
162 /* TCS only parameters. */
163
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
167 */
168 SI_PARAM_TCS_OUT_OFFSETS,
169
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
174 */
175 SI_PARAM_TCS_OUT_LAYOUT,
176
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
180 */
181 SI_PARAM_TCS_IN_LAYOUT,
182
183 SI_PARAM_TCS_OC_LDS,
184 SI_PARAM_TESS_FACTOR_OFFSET,
185 SI_PARAM_PATCH_ID,
186 SI_PARAM_REL_IDS,
187
188 /* GS only parameters */
189 SI_PARAM_GS2VS_OFFSET = SI_NUM_RESOURCE_PARAMS,
190 SI_PARAM_GS_WAVE_ID,
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,
199
200 /* PS only parameters */
201 SI_PARAM_ALPHA_REF = SI_NUM_RESOURCE_PARAMS,
202 SI_PARAM_PRIM_MASK,
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,
215 SI_PARAM_FRONT_FACE,
216 SI_PARAM_ANCILLARY,
217 SI_PARAM_SAMPLE_COVERAGE,
218 SI_PARAM_POS_FIXED_PT,
219
220 /* CS only parameters */
221 SI_PARAM_GRID_SIZE = SI_NUM_RESOURCE_PARAMS,
222 SI_PARAM_BLOCK_ID,
223 SI_PARAM_THREAD_ID,
224
225 SI_NUM_PARAMS = SI_PARAM_POS_FIXED_PT + 9, /* +8 for COLOR[0..1] */
226 };
227
228 /* SI-specific system values. */
229 enum {
230 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI = TGSI_SEMANTIC_COUNT,
231 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI,
232 };
233
234 struct si_shader;
235
236 /* A shader selector is a gallium CSO and contains shader variants and
237 * binaries for one TGSI program. This can be shared by multiple contexts.
238 */
239 struct si_shader_selector {
240 struct si_screen *screen;
241 struct util_queue_fence ready;
242
243 /* Should only be used by si_init_shader_selector_async
244 * if thread_index == -1 (non-threaded). */
245 LLVMTargetMachineRef tm;
246 struct pipe_debug_callback debug;
247 bool is_debug_context;
248
249 pipe_mutex mutex;
250 struct si_shader *first_variant; /* immutable after the first variant */
251 struct si_shader *last_variant; /* mutable */
252
253 /* The compiled TGSI shader expecting a prolog and/or epilog (not
254 * uploaded to a buffer).
255 */
256 struct si_shader *main_shader_part;
257
258 struct tgsi_token *tokens;
259 struct pipe_stream_output_info so;
260 struct tgsi_shader_info info;
261
262 /* PIPE_SHADER_[VERTEX|FRAGMENT|...] */
263 unsigned type;
264
265 /* GS parameters. */
266 unsigned esgs_itemsize;
267 unsigned gs_input_verts_per_prim;
268 unsigned gs_output_prim;
269 unsigned gs_max_out_vertices;
270 unsigned gs_num_invocations;
271 unsigned max_gs_stream; /* count - 1 */
272 unsigned gsvs_vertex_size;
273 unsigned max_gsvs_emit_size;
274
275 /* PS parameters. */
276 unsigned color_attr_index[2];
277 unsigned db_shader_control;
278 /* Set 0xf or 0x0 (4 bits) per each written output.
279 * ANDed with spi_shader_col_format.
280 */
281 unsigned colors_written_4bit;
282
283 /* CS parameters */
284 unsigned local_size;
285
286 /* masks of "get_unique_index" bits */
287 uint64_t outputs_written;
288 uint32_t patch_outputs_written;
289 };
290
291 /* Valid shader configurations:
292 *
293 * API shaders VS | TCS | TES | GS |pass| PS
294 * are compiled as: | | | |thru|
295 * | | | | |
296 * Only VS & PS: VS | -- | -- | -- | -- | PS
297 * With GS: ES | -- | -- | GS | VS | PS
298 * With Tessel.: LS | HS | VS | -- | -- | PS
299 * With both: LS | HS | ES | GS | VS | PS
300 */
301
302 /* Common VS bits between the shader key and the prolog key. */
303 struct si_vs_prolog_bits {
304 unsigned instance_divisors[SI_NUM_VERTEX_BUFFERS];
305 };
306
307 /* Common VS bits between the shader key and the epilog key. */
308 struct si_vs_epilog_bits {
309 unsigned export_prim_id:1; /* when PS needs it and GS is disabled */
310 /* TODO:
311 * - skip clipdist, culldist (including clipvertex code) exports based
312 * on which clip_plane_enable bits are set
313 * - skip layer, viewport, clipdist, and culldist parameter exports
314 * if PS doesn't read them
315 */
316 };
317
318 /* Common TCS bits between the shader key and the epilog key. */
319 struct si_tcs_epilog_bits {
320 unsigned prim_mode:3;
321 uint64_t inputs_to_copy;
322 };
323
324 /* Common PS bits between the shader key and the prolog key. */
325 struct si_ps_prolog_bits {
326 unsigned color_two_side:1;
327 unsigned flatshade_colors:1;
328 unsigned poly_stipple:1;
329 unsigned force_persp_sample_interp:1;
330 unsigned force_linear_sample_interp:1;
331 unsigned force_persp_center_interp:1;
332 unsigned force_linear_center_interp:1;
333 unsigned bc_optimize_for_persp:1;
334 unsigned bc_optimize_for_linear:1;
335 };
336
337 /* Common PS bits between the shader key and the epilog key. */
338 struct si_ps_epilog_bits {
339 unsigned spi_shader_col_format;
340 unsigned color_is_int8:8;
341 unsigned last_cbuf:3;
342 unsigned alpha_func:3;
343 unsigned alpha_to_one:1;
344 unsigned poly_line_smoothing:1;
345 unsigned clamp_color:1;
346 };
347
348 union si_shader_part_key {
349 struct {
350 struct si_vs_prolog_bits states;
351 unsigned num_input_sgprs:5;
352 unsigned last_input:4;
353 } vs_prolog;
354 struct {
355 struct si_vs_epilog_bits states;
356 unsigned prim_id_param_offset:5;
357 } vs_epilog;
358 struct {
359 struct si_tcs_epilog_bits states;
360 } tcs_epilog;
361 struct {
362 struct si_ps_prolog_bits states;
363 unsigned num_input_sgprs:5;
364 unsigned num_input_vgprs:5;
365 /* Color interpolation and two-side color selection. */
366 unsigned colors_read:8; /* color input components read */
367 unsigned num_interp_inputs:5; /* BCOLOR is at this location */
368 unsigned face_vgpr_index:5;
369 unsigned wqm:1;
370 char color_attr_index[2];
371 char color_interp_vgpr_index[2]; /* -1 == constant */
372 } ps_prolog;
373 struct {
374 struct si_ps_epilog_bits states;
375 unsigned colors_written:8;
376 unsigned writes_z:1;
377 unsigned writes_stencil:1;
378 unsigned writes_samplemask:1;
379 } ps_epilog;
380 };
381
382 union si_shader_key {
383 struct {
384 struct si_ps_prolog_bits prolog;
385 struct si_ps_epilog_bits epilog;
386 } ps;
387 struct {
388 struct si_vs_prolog_bits prolog;
389 struct si_vs_epilog_bits epilog;
390 unsigned as_es:1; /* export shader */
391 unsigned as_ls:1; /* local shader */
392 } vs;
393 struct {
394 struct si_tcs_epilog_bits epilog;
395 } tcs; /* tessellation control shader */
396 struct {
397 struct si_vs_epilog_bits epilog; /* same as VS */
398 unsigned as_es:1; /* export shader */
399 } tes; /* tessellation evaluation shader */
400 };
401
402 struct si_shader_config {
403 unsigned num_sgprs;
404 unsigned num_vgprs;
405 unsigned spilled_sgprs;
406 unsigned spilled_vgprs;
407 unsigned lds_size;
408 unsigned spi_ps_input_ena;
409 unsigned spi_ps_input_addr;
410 unsigned float_mode;
411 unsigned scratch_bytes_per_wave;
412 unsigned rsrc1;
413 unsigned rsrc2;
414 };
415
416 /* GCN-specific shader info. */
417 struct si_shader_info {
418 ubyte vs_output_param_offset[SI_MAX_VS_OUTPUTS];
419 ubyte num_input_sgprs;
420 ubyte num_input_vgprs;
421 char face_vgpr_index;
422 bool uses_instanceid;
423 ubyte nr_pos_exports;
424 ubyte nr_param_exports;
425 };
426
427 struct si_shader {
428 struct si_shader_selector *selector;
429 struct si_shader *next_variant;
430
431 struct si_shader_part *prolog;
432 struct si_shader_part *epilog;
433
434 struct si_shader *gs_copy_shader;
435 struct si_pm4_state *pm4;
436 struct r600_resource *bo;
437 struct r600_resource *scratch_bo;
438 union si_shader_key key;
439 bool is_binary_shared;
440 unsigned z_order;
441
442 /* The following data is all that's needed for binary shaders. */
443 struct radeon_shader_binary binary;
444 struct si_shader_config config;
445 struct si_shader_info info;
446
447 /* Shader key + LLVM IR + disassembly + statistics.
448 * Generated for debug contexts only.
449 */
450 char *shader_log;
451 size_t shader_log_size;
452 };
453
454 struct si_shader_part {
455 struct si_shader_part *next;
456 union si_shader_part_key key;
457 struct radeon_shader_binary binary;
458 struct si_shader_config config;
459 };
460
461 /* si_shader.c */
462 int si_compile_tgsi_shader(struct si_screen *sscreen,
463 LLVMTargetMachineRef tm,
464 struct si_shader *shader,
465 bool is_monolithic,
466 struct pipe_debug_callback *debug);
467 int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
468 struct si_shader *shader,
469 struct pipe_debug_callback *debug);
470 int si_compile_llvm(struct si_screen *sscreen,
471 struct radeon_shader_binary *binary,
472 struct si_shader_config *conf,
473 LLVMTargetMachineRef tm,
474 LLVMModuleRef mod,
475 struct pipe_debug_callback *debug,
476 unsigned processor,
477 const char *name);
478 void si_shader_destroy(struct si_shader *shader);
479 unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index);
480 int si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader);
481 void si_shader_dump(struct si_screen *sscreen, struct si_shader *shader,
482 struct pipe_debug_callback *debug, unsigned processor,
483 FILE *f);
484 void si_shader_apply_scratch_relocs(struct si_context *sctx,
485 struct si_shader *shader,
486 struct si_shader_config *config,
487 uint64_t scratch_va);
488 void si_shader_binary_read_config(struct radeon_shader_binary *binary,
489 struct si_shader_config *conf,
490 unsigned symbol_offset);
491 unsigned si_get_spi_shader_z_format(bool writes_z, bool writes_stencil,
492 bool writes_samplemask);
493
494 #endif