radv: tidy up radv_get_shader_name() and add NGG stages
[mesa.git] / src / amd / vulkan / radv_shader.h
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #ifndef RADV_SHADER_H
29 #define RADV_SHADER_H
30
31 #include "radv_debug.h"
32 #include "radv_private.h"
33
34 #include "nir/nir.h"
35
36 /* descriptor index into scratch ring offsets */
37 #define RING_SCRATCH 0
38 #define RING_ESGS_VS 1
39 #define RING_ESGS_GS 2
40 #define RING_GSVS_VS 3
41 #define RING_GSVS_GS 4
42 #define RING_HS_TESS_FACTOR 5
43 #define RING_HS_TESS_OFFCHIP 6
44 #define RING_PS_SAMPLE_POSITIONS 7
45
46 // Match MAX_SETS from radv_descriptor_set.h
47 #define RADV_UD_MAX_SETS MAX_SETS
48
49 #define RADV_NUM_PHYSICAL_VGPRS 256
50
51 struct radv_shader_module {
52 struct nir_shader *nir;
53 unsigned char sha1[20];
54 uint32_t size;
55 char data[0];
56 };
57
58 enum {
59 RADV_ALPHA_ADJUST_NONE = 0,
60 RADV_ALPHA_ADJUST_SNORM = 1,
61 RADV_ALPHA_ADJUST_SINT = 2,
62 RADV_ALPHA_ADJUST_SSCALED = 3,
63 };
64
65 struct radv_vs_out_key {
66 uint32_t as_es:1;
67 uint32_t as_ls:1;
68 uint32_t as_ngg:1;
69 uint32_t export_prim_id:1;
70 uint32_t export_layer_id:1;
71 uint32_t export_clip_dists:1;
72 };
73
74 struct radv_vs_variant_key {
75 struct radv_vs_out_key out;
76
77 uint32_t instance_rate_inputs;
78 uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];
79 uint8_t vertex_attribute_formats[MAX_VERTEX_ATTRIBS];
80 uint32_t vertex_attribute_bindings[MAX_VERTEX_ATTRIBS];
81 uint32_t vertex_attribute_offsets[MAX_VERTEX_ATTRIBS];
82 uint32_t vertex_attribute_strides[MAX_VERTEX_ATTRIBS];
83
84 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
85 * so we may need to fix it up. */
86 uint64_t alpha_adjust;
87
88 /* For some formats the channels have to be shuffled. */
89 uint32_t post_shuffle;
90 };
91
92 struct radv_tes_variant_key {
93 struct radv_vs_out_key out;
94
95 uint8_t num_patches;
96 uint8_t tcs_num_outputs;
97 };
98
99 struct radv_tcs_variant_key {
100 struct radv_vs_variant_key vs_key;
101 unsigned primitive_mode;
102 unsigned input_vertices;
103 unsigned num_inputs;
104 uint32_t tes_reads_tess_factors:1;
105 };
106
107 struct radv_fs_variant_key {
108 uint32_t col_format;
109 uint8_t log2_ps_iter_samples;
110 uint8_t num_samples;
111 uint32_t is_int8;
112 uint32_t is_int10;
113 };
114
115 struct radv_shader_variant_key {
116 union {
117 struct radv_vs_variant_key vs;
118 struct radv_fs_variant_key fs;
119 struct radv_tes_variant_key tes;
120 struct radv_tcs_variant_key tcs;
121
122 /* A common prefix of the vs and tes keys. */
123 struct radv_vs_out_key vs_common_out;
124 };
125 bool has_multiview_view_index;
126 };
127
128 struct radv_nir_compiler_options {
129 struct radv_pipeline_layout *layout;
130 struct radv_shader_variant_key key;
131 bool unsafe_math;
132 bool supports_spill;
133 bool clamp_shadow_reference;
134 bool dump_shader;
135 bool dump_preoptir;
136 bool record_llvm_ir;
137 bool check_ir;
138 enum radeon_family family;
139 enum chip_class chip_class;
140 uint32_t tess_offchip_block_dw_size;
141 uint32_t address32_hi;
142 };
143
144 enum radv_ud_index {
145 AC_UD_SCRATCH_RING_OFFSETS = 0,
146 AC_UD_PUSH_CONSTANTS = 1,
147 AC_UD_INLINE_PUSH_CONSTANTS = 2,
148 AC_UD_INDIRECT_DESCRIPTOR_SETS = 3,
149 AC_UD_VIEW_INDEX = 4,
150 AC_UD_STREAMOUT_BUFFERS = 5,
151 AC_UD_SHADER_START = 6,
152 AC_UD_VS_VERTEX_BUFFERS = AC_UD_SHADER_START,
153 AC_UD_VS_BASE_VERTEX_START_INSTANCE,
154 AC_UD_VS_MAX_UD,
155 AC_UD_PS_MAX_UD,
156 AC_UD_CS_GRID_SIZE = AC_UD_SHADER_START,
157 AC_UD_CS_MAX_UD,
158 AC_UD_GS_MAX_UD,
159 AC_UD_TCS_MAX_UD,
160 AC_UD_TES_MAX_UD,
161 AC_UD_MAX_UD = AC_UD_TCS_MAX_UD,
162 };
163
164 struct radv_stream_output {
165 uint8_t location;
166 uint8_t buffer;
167 uint16_t offset;
168 uint8_t component_mask;
169 uint8_t stream;
170 };
171
172 struct radv_streamout_info {
173 uint16_t num_outputs;
174 struct radv_stream_output outputs[MAX_SO_OUTPUTS];
175 uint16_t strides[MAX_SO_BUFFERS];
176 uint32_t enabled_stream_buffers_mask;
177 };
178
179 struct radv_shader_info {
180 bool loads_push_constants;
181 bool loads_dynamic_offsets;
182 uint8_t min_push_constant_used;
183 uint8_t max_push_constant_used;
184 bool has_only_32bit_push_constants;
185 bool has_indirect_push_constants;
186 uint8_t num_inline_push_consts;
187 uint8_t base_inline_push_consts;
188 uint32_t desc_set_used_mask;
189 bool needs_multiview_view_index;
190 bool uses_invocation_id;
191 bool uses_prim_id;
192 struct {
193 uint64_t ls_outputs_written;
194 uint8_t input_usage_mask[VERT_ATTRIB_MAX];
195 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
196 bool has_vertex_buffers; /* needs vertex buffers and base/start */
197 bool needs_draw_id;
198 bool needs_instance_id;
199 } vs;
200 struct {
201 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
202 uint8_t num_stream_output_components[4];
203 uint8_t output_streams[VARYING_SLOT_VAR31 + 1];
204 uint8_t max_stream;
205 } gs;
206 struct {
207 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
208 } tes;
209 struct {
210 bool force_persample;
211 bool needs_sample_positions;
212 bool writes_memory;
213 bool writes_z;
214 bool writes_stencil;
215 bool writes_sample_mask;
216 bool has_pcoord;
217 bool prim_id_input;
218 bool layer_input;
219 uint8_t num_input_clips_culls;
220 } ps;
221 struct {
222 bool uses_grid_size;
223 bool uses_block_id[3];
224 bool uses_thread_id[3];
225 bool uses_local_invocation_idx;
226 } cs;
227 struct {
228 uint64_t outputs_written;
229 uint64_t patch_outputs_written;
230 } tcs;
231
232 struct radv_streamout_info so;
233 };
234
235 struct radv_userdata_info {
236 int8_t sgpr_idx;
237 uint8_t num_sgprs;
238 };
239
240 struct radv_userdata_locations {
241 struct radv_userdata_info descriptor_sets[RADV_UD_MAX_SETS];
242 struct radv_userdata_info shader_data[AC_UD_MAX_UD];
243 uint32_t descriptor_sets_enabled;
244 };
245
246 struct radv_vs_output_info {
247 uint8_t vs_output_param_offset[VARYING_SLOT_MAX];
248 uint8_t clip_dist_mask;
249 uint8_t cull_dist_mask;
250 uint8_t param_exports;
251 bool writes_pointsize;
252 bool writes_layer;
253 bool writes_viewport_index;
254 bool export_prim_id;
255 unsigned pos_exports;
256 };
257
258 struct radv_es_output_info {
259 uint32_t esgs_itemsize;
260 };
261
262 struct radv_shader_variant_info {
263 struct radv_userdata_locations user_sgprs_locs;
264 struct radv_shader_info info;
265 unsigned num_user_sgprs;
266 unsigned num_input_sgprs;
267 unsigned num_input_vgprs;
268 unsigned private_mem_vgprs;
269 bool need_indirect_descriptor_sets;
270 bool is_ngg;
271 struct {
272 struct {
273 struct radv_vs_output_info outinfo;
274 struct radv_es_output_info es_info;
275 bool as_es;
276 bool as_ls;
277 bool export_prim_id;
278 } vs;
279 struct {
280 unsigned num_interp;
281 uint32_t input_mask;
282 uint32_t flat_shaded_mask;
283 uint32_t float16_shaded_mask;
284 bool can_discard;
285 bool early_fragment_test;
286 } fs;
287 struct {
288 unsigned block_size[3];
289 } cs;
290 struct {
291 unsigned vertices_in;
292 unsigned vertices_out;
293 unsigned output_prim;
294 unsigned invocations;
295 unsigned gsvs_vertex_size;
296 unsigned max_gsvs_emit_size;
297 unsigned es_type; /* GFX9: VS or TES */
298 } gs;
299 struct {
300 unsigned tcs_vertices_out;
301 uint32_t num_patches;
302 uint32_t lds_size;
303 } tcs;
304 struct {
305 struct radv_vs_output_info outinfo;
306 struct radv_es_output_info es_info;
307 bool as_es;
308 unsigned primitive_mode;
309 enum gl_tess_spacing spacing;
310 bool ccw;
311 bool point_mode;
312 bool export_prim_id;
313 } tes;
314 };
315 };
316
317 enum radv_shader_binary_type {
318 RADV_BINARY_TYPE_LEGACY,
319 RADV_BINARY_TYPE_RTLD
320 };
321
322 struct radv_shader_binary {
323 enum radv_shader_binary_type type;
324 gl_shader_stage stage;
325 bool is_gs_copy_shader;
326
327 struct radv_shader_variant_info variant_info;
328
329 /* Self-referential size so we avoid consistency issues. */
330 uint32_t total_size;
331 };
332
333 struct radv_shader_binary_legacy {
334 struct radv_shader_binary base;
335 struct ac_shader_config config;
336 unsigned code_size;
337 unsigned llvm_ir_size;
338 unsigned disasm_size;
339
340 /* data has size of code_size + llvm_ir_size + disasm_size + 2, where
341 * the +2 is for 0 of the ir strings. */
342 uint8_t data[0];
343 };
344
345 struct radv_shader_binary_rtld {
346 struct radv_shader_binary base;
347 unsigned elf_size;
348 unsigned llvm_ir_size;
349 uint8_t data[0];
350 };
351
352 struct radv_shader_variant {
353 uint32_t ref_count;
354
355 struct radeon_winsys_bo *bo;
356 uint64_t bo_offset;
357 struct ac_shader_config config;
358 uint32_t code_size;
359 struct radv_shader_variant_info info;
360
361 /* debug only */
362 uint32_t *spirv;
363 uint32_t spirv_size;
364 struct nir_shader *nir;
365 char *disasm_string;
366 char *llvm_ir_string;
367
368 struct list_head slab_list;
369 };
370
371 struct radv_shader_slab {
372 struct list_head slabs;
373 struct list_head shaders;
374 struct radeon_winsys_bo *bo;
375 uint64_t size;
376 char *ptr;
377 };
378
379 void
380 radv_optimize_nir(struct nir_shader *shader, bool optimize_conservatively,
381 bool allow_copies);
382 bool
383 radv_nir_lower_ycbcr_textures(nir_shader *shader,
384 const struct radv_pipeline_layout *layout);
385
386 nir_shader *
387 radv_shader_compile_to_nir(struct radv_device *device,
388 struct radv_shader_module *module,
389 const char *entrypoint_name,
390 gl_shader_stage stage,
391 const VkSpecializationInfo *spec_info,
392 const VkPipelineCreateFlags flags,
393 const struct radv_pipeline_layout *layout);
394
395 void *
396 radv_alloc_shader_memory(struct radv_device *device,
397 struct radv_shader_variant *shader);
398
399 void
400 radv_destroy_shader_slabs(struct radv_device *device);
401
402 struct radv_shader_variant *
403 radv_shader_variant_create(struct radv_device *device,
404 const struct radv_shader_binary *binary);
405 struct radv_shader_variant *
406 radv_shader_variant_compile(struct radv_device *device,
407 struct radv_shader_module *module,
408 struct nir_shader *const *shaders,
409 int shader_count,
410 struct radv_pipeline_layout *layout,
411 const struct radv_shader_variant_key *key,
412 struct radv_shader_binary **binary_out);
413
414 struct radv_shader_variant *
415 radv_create_gs_copy_shader(struct radv_device *device, struct nir_shader *nir,
416 struct radv_shader_binary **binary_out,
417 bool multiview);
418
419 void
420 radv_shader_variant_destroy(struct radv_device *device,
421 struct radv_shader_variant *variant);
422
423 const char *
424 radv_get_shader_name(struct radv_shader_variant_info *info,
425 gl_shader_stage stage);
426
427 void
428 radv_shader_dump_stats(struct radv_device *device,
429 struct radv_shader_variant *variant,
430 gl_shader_stage stage,
431 FILE *file);
432
433 static inline bool
434 radv_can_dump_shader(struct radv_device *device,
435 struct radv_shader_module *module,
436 bool is_gs_copy_shader)
437 {
438 if (!(device->instance->debug_flags & RADV_DEBUG_DUMP_SHADERS))
439 return false;
440
441 /* Only dump non-meta shaders, useful for debugging purposes. */
442 return (module && !module->nir) || is_gs_copy_shader;
443 }
444
445 static inline bool
446 radv_can_dump_shader_stats(struct radv_device *device,
447 struct radv_shader_module *module)
448 {
449 /* Only dump non-meta shader stats. */
450 return device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS &&
451 module && !module->nir;
452 }
453
454 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot)
455 {
456 /* handle patch indices separate */
457 if (slot == VARYING_SLOT_TESS_LEVEL_OUTER)
458 return 0;
459 if (slot == VARYING_SLOT_TESS_LEVEL_INNER)
460 return 1;
461 if (slot >= VARYING_SLOT_PATCH0 && slot <= VARYING_SLOT_TESS_MAX)
462 return 2 + (slot - VARYING_SLOT_PATCH0);
463 if (slot == VARYING_SLOT_POS)
464 return 0;
465 if (slot == VARYING_SLOT_PSIZ)
466 return 1;
467 if (slot == VARYING_SLOT_CLIP_DIST0)
468 return 2;
469 if (slot == VARYING_SLOT_CLIP_DIST1)
470 return 3;
471 /* 3 is reserved for clip dist as well */
472 if (slot >= VARYING_SLOT_VAR0 && slot <= VARYING_SLOT_VAR31)
473 return 4 + (slot - VARYING_SLOT_VAR0);
474 unreachable("illegal slot in get unique index\n");
475 }
476
477 #endif