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