fea0d1c8df17ae7c01ed6cc5fcc2f86b013b3b63
[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 bool post_depth_coverage;
287 } fs;
288 struct {
289 unsigned block_size[3];
290 } cs;
291 struct {
292 unsigned vertices_in;
293 unsigned vertices_out;
294 unsigned output_prim;
295 unsigned invocations;
296 unsigned gsvs_vertex_size;
297 unsigned max_gsvs_emit_size;
298 unsigned es_type; /* GFX9: VS or TES */
299 } gs;
300 struct {
301 unsigned tcs_vertices_out;
302 uint32_t num_patches;
303 uint32_t lds_size;
304 } tcs;
305 struct {
306 struct radv_vs_output_info outinfo;
307 struct radv_es_output_info es_info;
308 bool as_es;
309 unsigned primitive_mode;
310 enum gl_tess_spacing spacing;
311 bool ccw;
312 bool point_mode;
313 bool export_prim_id;
314 } tes;
315 };
316 };
317
318 enum radv_shader_binary_type {
319 RADV_BINARY_TYPE_LEGACY,
320 RADV_BINARY_TYPE_RTLD
321 };
322
323 struct radv_shader_binary {
324 enum radv_shader_binary_type type;
325 gl_shader_stage stage;
326 bool is_gs_copy_shader;
327
328 struct radv_shader_variant_info variant_info;
329
330 /* Self-referential size so we avoid consistency issues. */
331 uint32_t total_size;
332 };
333
334 struct radv_shader_binary_legacy {
335 struct radv_shader_binary base;
336 struct ac_shader_config config;
337 unsigned code_size;
338 unsigned llvm_ir_size;
339 unsigned disasm_size;
340
341 /* data has size of code_size + llvm_ir_size + disasm_size + 2, where
342 * the +2 is for 0 of the ir strings. */
343 uint8_t data[0];
344 };
345
346 struct radv_shader_binary_rtld {
347 struct radv_shader_binary base;
348 unsigned elf_size;
349 unsigned llvm_ir_size;
350 uint8_t data[0];
351 };
352
353 struct radv_shader_variant {
354 uint32_t ref_count;
355
356 struct radeon_winsys_bo *bo;
357 uint64_t bo_offset;
358 struct ac_shader_config config;
359 uint32_t code_size;
360 struct radv_shader_variant_info info;
361
362 /* debug only */
363 uint32_t *spirv;
364 uint32_t spirv_size;
365 struct nir_shader *nir;
366 char *disasm_string;
367 char *llvm_ir_string;
368
369 struct list_head slab_list;
370 };
371
372 struct radv_shader_slab {
373 struct list_head slabs;
374 struct list_head shaders;
375 struct radeon_winsys_bo *bo;
376 uint64_t size;
377 char *ptr;
378 };
379
380 void
381 radv_optimize_nir(struct nir_shader *shader, bool optimize_conservatively,
382 bool allow_copies);
383 bool
384 radv_nir_lower_ycbcr_textures(nir_shader *shader,
385 const struct radv_pipeline_layout *layout);
386
387 nir_shader *
388 radv_shader_compile_to_nir(struct radv_device *device,
389 struct radv_shader_module *module,
390 const char *entrypoint_name,
391 gl_shader_stage stage,
392 const VkSpecializationInfo *spec_info,
393 const VkPipelineCreateFlags flags,
394 const struct radv_pipeline_layout *layout);
395
396 void *
397 radv_alloc_shader_memory(struct radv_device *device,
398 struct radv_shader_variant *shader);
399
400 void
401 radv_destroy_shader_slabs(struct radv_device *device);
402
403 struct radv_shader_variant *
404 radv_shader_variant_create(struct radv_device *device,
405 const struct radv_shader_binary *binary);
406 struct radv_shader_variant *
407 radv_shader_variant_compile(struct radv_device *device,
408 struct radv_shader_module *module,
409 struct nir_shader *const *shaders,
410 int shader_count,
411 struct radv_pipeline_layout *layout,
412 const struct radv_shader_variant_key *key,
413 struct radv_shader_binary **binary_out);
414
415 struct radv_shader_variant *
416 radv_create_gs_copy_shader(struct radv_device *device, struct nir_shader *nir,
417 struct radv_shader_binary **binary_out,
418 bool multiview);
419
420 void
421 radv_shader_variant_destroy(struct radv_device *device,
422 struct radv_shader_variant *variant);
423
424 const char *
425 radv_get_shader_name(struct radv_shader_variant_info *info,
426 gl_shader_stage stage);
427
428 void
429 radv_shader_dump_stats(struct radv_device *device,
430 struct radv_shader_variant *variant,
431 gl_shader_stage stage,
432 FILE *file);
433
434 static inline bool
435 radv_can_dump_shader(struct radv_device *device,
436 struct radv_shader_module *module,
437 bool is_gs_copy_shader)
438 {
439 if (!(device->instance->debug_flags & RADV_DEBUG_DUMP_SHADERS))
440 return false;
441
442 /* Only dump non-meta shaders, useful for debugging purposes. */
443 return (module && !module->nir) || is_gs_copy_shader;
444 }
445
446 static inline bool
447 radv_can_dump_shader_stats(struct radv_device *device,
448 struct radv_shader_module *module)
449 {
450 /* Only dump non-meta shader stats. */
451 return device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS &&
452 module && !module->nir;
453 }
454
455 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot)
456 {
457 /* handle patch indices separate */
458 if (slot == VARYING_SLOT_TESS_LEVEL_OUTER)
459 return 0;
460 if (slot == VARYING_SLOT_TESS_LEVEL_INNER)
461 return 1;
462 if (slot >= VARYING_SLOT_PATCH0 && slot <= VARYING_SLOT_TESS_MAX)
463 return 2 + (slot - VARYING_SLOT_PATCH0);
464 if (slot == VARYING_SLOT_POS)
465 return 0;
466 if (slot == VARYING_SLOT_PSIZ)
467 return 1;
468 if (slot == VARYING_SLOT_CLIP_DIST0)
469 return 2;
470 if (slot == VARYING_SLOT_CLIP_DIST1)
471 return 3;
472 /* 3 is reserved for clip dist as well */
473 if (slot >= VARYING_SLOT_VAR0 && slot <= VARYING_SLOT_VAR31)
474 return 4 + (slot - VARYING_SLOT_VAR0);
475 unreachable("illegal slot in get unique index\n");
476 }
477
478 #endif