radeonsi: Store inputs to memory when not using a TCS.
[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 "tgsi/tgsi_scan.h"
73 #include "si_state.h"
74
75 struct radeon_shader_binary;
76 struct radeon_shader_reloc;
77
78 #define SI_MAX_VS_OUTPUTS 40
79
80 /* SGPR user data indices */
81 enum {
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 */
87 SI_SGPR_SAMPLERS_HI,
88 SI_SGPR_IMAGES,
89 SI_SGPR_IMAGES_HI,
90 SI_SGPR_SHADER_BUFFERS,
91 SI_SGPR_SHADER_BUFFERS_HI,
92 SI_NUM_RESOURCE_SGPRS,
93
94 /* all VS variants */
95 SI_SGPR_VERTEX_BUFFERS = SI_NUM_RESOURCE_SGPRS,
96 SI_SGPR_VERTEX_BUFFERS_HI,
97 SI_SGPR_BASE_VERTEX,
98 SI_SGPR_START_INSTANCE,
99 SI_ES_NUM_USER_SGPR,
100
101 /* hw VS only */
102 SI_SGPR_VS_STATE_BITS = SI_ES_NUM_USER_SGPR,
103 SI_VS_NUM_USER_SGPR,
104
105 /* hw LS only */
106 SI_SGPR_LS_OUT_LAYOUT = SI_ES_NUM_USER_SGPR,
107 SI_LS_NUM_USER_SGPR,
108
109 /* both TCS and TES */
110 SI_SGPR_TCS_OFFCHIP_LAYOUT = SI_NUM_RESOURCE_SGPRS,
111 SI_SGPR_TCS_OUT_OFFSETS,
112 SI_SGPR_TCS_OUT_LAYOUT,
113 SI_TES_NUM_USER_SGPR,
114
115 /* TCS only */
116 SI_SGPR_TCS_IN_LAYOUT = SI_TES_NUM_USER_SGPR,
117 SI_TCS_NUM_USER_SGPR,
118
119 /* GS limits */
120 SI_GS_NUM_USER_SGPR = SI_NUM_RESOURCE_SGPRS,
121 SI_GSCOPY_NUM_USER_SGPR = SI_SGPR_RW_BUFFERS_HI + 1,
122
123 /* PS only */
124 SI_SGPR_ALPHA_REF = SI_NUM_RESOURCE_SGPRS,
125 SI_PS_NUM_USER_SGPR,
126
127 /* CS only */
128 SI_SGPR_GRID_SIZE = SI_NUM_RESOURCE_SGPRS,
129 SI_CS_NUM_USER_SGPR = SI_SGPR_GRID_SIZE + 3
130 };
131
132 /* LLVM function parameter indices */
133 enum {
134 SI_PARAM_RW_BUFFERS,
135 SI_PARAM_CONST_BUFFERS,
136 SI_PARAM_SAMPLERS,
137 SI_PARAM_IMAGES,
138 SI_PARAM_SHADER_BUFFERS,
139 SI_NUM_RESOURCE_PARAMS,
140
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 */
150
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.
155 */
156 SI_PARAM_TCS_OFFCHIP_LAYOUT = SI_NUM_RESOURCE_PARAMS, /* for TCS & TES */
157
158 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
159 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
160 * [16:31] = TCS output patch0 offset for per-patch / 16, max = NUM_PATCHES*32*32* + 32*32
161 */
162 SI_PARAM_TCS_OUT_OFFSETS, /* for TCS & TES */
163
164 /* Layout of TCS outputs / TES inputs:
165 * [0:12] = stride between output patches in dwords, num_outputs * num_vertices * 4, max = 32*32*4
166 * [13:20] = stride between output vertices in dwords = num_inputs * 4, max = 32*4
167 * [26:31] = gl_PatchVerticesIn, max = 32
168 */
169 SI_PARAM_TCS_OUT_LAYOUT, /* for TCS & TES */
170
171 /* Layout of LS outputs / TCS inputs
172 * [0:12] = stride between patches in dwords = num_inputs * num_vertices * 4, max = 32*32*4
173 * [13:20] = stride between vertices in dwords = num_inputs * 4, max = 32*4
174 */
175 SI_PARAM_TCS_IN_LAYOUT, /* TCS only */
176
177 /* TCS only parameters. */
178 SI_PARAM_TCS_OC_LDS,
179 SI_PARAM_TESS_FACTOR_OFFSET,
180 SI_PARAM_PATCH_ID,
181 SI_PARAM_REL_IDS,
182
183 /* GS only parameters */
184 SI_PARAM_GS2VS_OFFSET = SI_NUM_RESOURCE_PARAMS,
185 SI_PARAM_GS_WAVE_ID,
186 SI_PARAM_VTX0_OFFSET,
187 SI_PARAM_VTX1_OFFSET,
188 SI_PARAM_PRIMITIVE_ID,
189 SI_PARAM_VTX2_OFFSET,
190 SI_PARAM_VTX3_OFFSET,
191 SI_PARAM_VTX4_OFFSET,
192 SI_PARAM_VTX5_OFFSET,
193 SI_PARAM_GS_INSTANCE_ID,
194
195 /* PS only parameters */
196 SI_PARAM_ALPHA_REF = SI_NUM_RESOURCE_PARAMS,
197 SI_PARAM_PRIM_MASK,
198 SI_PARAM_PERSP_SAMPLE,
199 SI_PARAM_PERSP_CENTER,
200 SI_PARAM_PERSP_CENTROID,
201 SI_PARAM_PERSP_PULL_MODEL,
202 SI_PARAM_LINEAR_SAMPLE,
203 SI_PARAM_LINEAR_CENTER,
204 SI_PARAM_LINEAR_CENTROID,
205 SI_PARAM_LINE_STIPPLE_TEX,
206 SI_PARAM_POS_X_FLOAT,
207 SI_PARAM_POS_Y_FLOAT,
208 SI_PARAM_POS_Z_FLOAT,
209 SI_PARAM_POS_W_FLOAT,
210 SI_PARAM_FRONT_FACE,
211 SI_PARAM_ANCILLARY,
212 SI_PARAM_SAMPLE_COVERAGE,
213 SI_PARAM_POS_FIXED_PT,
214
215 /* CS only parameters */
216 SI_PARAM_GRID_SIZE = SI_NUM_RESOURCE_PARAMS,
217 SI_PARAM_BLOCK_ID,
218 SI_PARAM_THREAD_ID,
219
220 SI_NUM_PARAMS = SI_PARAM_POS_FIXED_PT + 9, /* +8 for COLOR[0..1] */
221 };
222
223 /* SI-specific system values. */
224 enum {
225 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI = TGSI_SEMANTIC_COUNT,
226 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI,
227 };
228
229 struct si_shader;
230
231 /* A shader selector is a gallium CSO and contains shader variants and
232 * binaries for one TGSI program. This can be shared by multiple contexts.
233 */
234 struct si_shader_selector {
235 pipe_mutex mutex;
236 struct si_shader *first_variant; /* immutable after the first variant */
237 struct si_shader *last_variant; /* mutable */
238
239 /* The compiled TGSI shader expecting a prolog and/or epilog (not
240 * uploaded to a buffer).
241 */
242 struct si_shader *main_shader_part;
243
244 struct tgsi_token *tokens;
245 struct pipe_stream_output_info so;
246 struct tgsi_shader_info info;
247
248 /* PIPE_SHADER_[VERTEX|FRAGMENT|...] */
249 unsigned type;
250
251 /* GS parameters. */
252 unsigned esgs_itemsize;
253 unsigned gs_input_verts_per_prim;
254 unsigned gs_output_prim;
255 unsigned gs_max_out_vertices;
256 unsigned gs_num_invocations;
257 unsigned max_gs_stream; /* count - 1 */
258 unsigned gsvs_vertex_size;
259 unsigned max_gsvs_emit_size;
260
261 /* PS parameters. */
262 unsigned color_attr_index[2];
263 unsigned db_shader_control;
264 /* Set 0xf or 0x0 (4 bits) per each written output.
265 * ANDed with spi_shader_col_format.
266 */
267 unsigned colors_written_4bit;
268
269 /* CS parameters */
270 unsigned local_size;
271
272 /* masks of "get_unique_index" bits */
273 uint64_t outputs_written;
274 uint32_t patch_outputs_written;
275 };
276
277 /* Valid shader configurations:
278 *
279 * API shaders VS | TCS | TES | GS |pass| PS
280 * are compiled as: | | | |thru|
281 * | | | | |
282 * Only VS & PS: VS | -- | -- | -- | -- | PS
283 * With GS: ES | -- | -- | GS | VS | PS
284 * With Tessel.: LS | HS | VS | -- | -- | PS
285 * With both: LS | HS | ES | GS | VS | PS
286 */
287
288 /* Common VS bits between the shader key and the prolog key. */
289 struct si_vs_prolog_bits {
290 unsigned instance_divisors[SI_NUM_VERTEX_BUFFERS];
291 };
292
293 /* Common VS bits between the shader key and the epilog key. */
294 struct si_vs_epilog_bits {
295 unsigned export_prim_id:1; /* when PS needs it and GS is disabled */
296 /* TODO:
297 * - skip clipdist, culldist (including clipvertex code) exports based
298 * on which clip_plane_enable bits are set
299 * - skip layer, viewport, clipdist, and culldist parameter exports
300 * if PS doesn't read them
301 */
302 };
303
304 /* Common TCS bits between the shader key and the epilog key. */
305 struct si_tcs_epilog_bits {
306 unsigned prim_mode:3;
307 uint64_t inputs_to_copy;
308 };
309
310 /* Common PS bits between the shader key and the prolog key. */
311 struct si_ps_prolog_bits {
312 unsigned color_two_side:1;
313 /* TODO: add a flatshade bit that skips interpolation for colors */
314 unsigned poly_stipple:1;
315 unsigned force_persample_interp:1;
316 /* TODO:
317 * - add force_center_interp if MSAA is disabled and centroid or
318 * sample are present
319 * - add force_center_interp_bc_optimize to force center interpolation
320 * based on the bc_optimize SGPR bit if MSAA is enabled, centroid is
321 * present and sample isn't present.
322 */
323 };
324
325 /* Common PS bits between the shader key and the epilog key. */
326 struct si_ps_epilog_bits {
327 unsigned spi_shader_col_format;
328 unsigned color_is_int8:8;
329 unsigned last_cbuf:3;
330 unsigned alpha_func:3;
331 unsigned alpha_to_one:1;
332 unsigned poly_line_smoothing:1;
333 unsigned clamp_color:1;
334 };
335
336 union si_shader_part_key {
337 struct {
338 struct si_vs_prolog_bits states;
339 unsigned num_input_sgprs:5;
340 unsigned last_input:4;
341 } vs_prolog;
342 struct {
343 struct si_vs_epilog_bits states;
344 unsigned prim_id_param_offset:5;
345 } vs_epilog;
346 struct {
347 struct si_tcs_epilog_bits states;
348 } tcs_epilog;
349 struct {
350 struct si_ps_prolog_bits states;
351 unsigned num_input_sgprs:5;
352 unsigned num_input_vgprs:5;
353 /* Color interpolation and two-side color selection. */
354 unsigned colors_read:8; /* color input components read */
355 unsigned num_interp_inputs:5; /* BCOLOR is at this location */
356 unsigned face_vgpr_index:5;
357 char color_attr_index[2];
358 char color_interp_vgpr_index[2]; /* -1 == constant */
359 } ps_prolog;
360 struct {
361 struct si_ps_epilog_bits states;
362 unsigned colors_written:8;
363 unsigned writes_z:1;
364 unsigned writes_stencil:1;
365 unsigned writes_samplemask:1;
366 } ps_epilog;
367 };
368
369 union si_shader_key {
370 struct {
371 struct si_ps_prolog_bits prolog;
372 struct si_ps_epilog_bits epilog;
373 } ps;
374 struct {
375 struct si_vs_prolog_bits prolog;
376 struct si_vs_epilog_bits epilog;
377 unsigned as_es:1; /* export shader */
378 unsigned as_ls:1; /* local shader */
379 } vs;
380 struct {
381 struct si_tcs_epilog_bits epilog;
382 } tcs; /* tessellation control shader */
383 struct {
384 struct si_vs_epilog_bits epilog; /* same as VS */
385 unsigned as_es:1; /* export shader */
386 } tes; /* tessellation evaluation shader */
387 };
388
389 struct si_shader_config {
390 unsigned num_sgprs;
391 unsigned num_vgprs;
392 unsigned lds_size;
393 unsigned spi_ps_input_ena;
394 unsigned spi_ps_input_addr;
395 unsigned float_mode;
396 unsigned scratch_bytes_per_wave;
397 unsigned rsrc1;
398 unsigned rsrc2;
399 };
400
401 /* GCN-specific shader info. */
402 struct si_shader_info {
403 ubyte vs_output_param_offset[SI_MAX_VS_OUTPUTS];
404 ubyte num_input_sgprs;
405 ubyte num_input_vgprs;
406 char face_vgpr_index;
407 bool uses_instanceid;
408 ubyte nr_pos_exports;
409 ubyte nr_param_exports;
410 };
411
412 struct si_shader {
413 struct si_shader_selector *selector;
414 struct si_shader *next_variant;
415
416 struct si_shader_part *prolog;
417 struct si_shader_part *epilog;
418
419 struct si_shader *gs_copy_shader;
420 struct si_pm4_state *pm4;
421 struct r600_resource *bo;
422 struct r600_resource *scratch_bo;
423 union si_shader_key key;
424 bool is_binary_shared;
425 unsigned z_order;
426
427 /* The following data is all that's needed for binary shaders. */
428 struct radeon_shader_binary binary;
429 struct si_shader_config config;
430 struct si_shader_info info;
431 };
432
433 struct si_shader_part {
434 struct si_shader_part *next;
435 union si_shader_part_key key;
436 struct radeon_shader_binary binary;
437 struct si_shader_config config;
438 };
439
440 static inline struct tgsi_shader_info *si_get_vs_info(struct si_context *sctx)
441 {
442 if (sctx->gs_shader.cso)
443 return &sctx->gs_shader.cso->info;
444 else if (sctx->tes_shader.cso)
445 return &sctx->tes_shader.cso->info;
446 else if (sctx->vs_shader.cso)
447 return &sctx->vs_shader.cso->info;
448 else
449 return NULL;
450 }
451
452 static inline struct si_shader* si_get_vs_state(struct si_context *sctx)
453 {
454 if (sctx->gs_shader.current)
455 return sctx->gs_shader.current->gs_copy_shader;
456 else if (sctx->tes_shader.current)
457 return sctx->tes_shader.current;
458 else
459 return sctx->vs_shader.current;
460 }
461
462 static inline bool si_vs_exports_prim_id(struct si_shader *shader)
463 {
464 if (shader->selector->type == PIPE_SHADER_VERTEX)
465 return shader->key.vs.epilog.export_prim_id;
466 else if (shader->selector->type == PIPE_SHADER_TESS_EVAL)
467 return shader->key.tes.epilog.export_prim_id;
468 else
469 return false;
470 }
471
472 /* si_shader.c */
473 int si_compile_tgsi_shader(struct si_screen *sscreen,
474 LLVMTargetMachineRef tm,
475 struct si_shader *shader,
476 bool is_monolithic,
477 struct pipe_debug_callback *debug);
478 int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
479 struct si_shader *shader,
480 struct pipe_debug_callback *debug);
481 void si_dump_shader_key(unsigned shader, union si_shader_key *key, FILE *f);
482 int si_compile_llvm(struct si_screen *sscreen,
483 struct radeon_shader_binary *binary,
484 struct si_shader_config *conf,
485 LLVMTargetMachineRef tm,
486 LLVMModuleRef mod,
487 struct pipe_debug_callback *debug,
488 unsigned processor,
489 const char *name);
490 void si_shader_destroy(struct si_shader *shader);
491 unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index);
492 int si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader);
493 void si_shader_dump(struct si_screen *sscreen, struct si_shader *shader,
494 struct pipe_debug_callback *debug, unsigned processor,
495 FILE *f);
496 void si_shader_apply_scratch_relocs(struct si_context *sctx,
497 struct si_shader *shader,
498 struct si_shader_config *config,
499 uint64_t scratch_va);
500 void si_shader_binary_read_config(struct radeon_shader_binary *binary,
501 struct si_shader_config *conf,
502 unsigned symbol_offset);
503
504 #endif