radv: Combine vs and tes output keys parts.
[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 export_prim_id:1;
69 uint32_t export_layer_id:1;
70 uint32_t export_clip_dists:1;
71 };
72
73 struct radv_vs_variant_key {
74 struct radv_vs_out_key out;
75
76 uint32_t instance_rate_inputs;
77 uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];
78 uint8_t vertex_attribute_formats[MAX_VERTEX_ATTRIBS];
79 uint32_t vertex_attribute_bindings[MAX_VERTEX_ATTRIBS];
80 uint32_t vertex_attribute_offsets[MAX_VERTEX_ATTRIBS];
81 uint32_t vertex_attribute_strides[MAX_VERTEX_ATTRIBS];
82
83 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
84 * so we may need to fix it up. */
85 uint64_t alpha_adjust;
86
87 /* For some formats the channels have to be shuffled. */
88 uint32_t post_shuffle;
89 };
90
91 struct radv_tes_variant_key {
92 struct radv_vs_out_key out;
93
94 uint8_t num_patches;
95 uint8_t tcs_num_outputs;
96 };
97
98 struct radv_tcs_variant_key {
99 struct radv_vs_variant_key vs_key;
100 unsigned primitive_mode;
101 unsigned input_vertices;
102 unsigned num_inputs;
103 uint32_t tes_reads_tess_factors:1;
104 };
105
106 struct radv_fs_variant_key {
107 uint32_t col_format;
108 uint8_t log2_ps_iter_samples;
109 uint8_t num_samples;
110 uint32_t is_int8;
111 uint32_t is_int10;
112 };
113
114 struct radv_shader_variant_key {
115 union {
116 struct radv_vs_variant_key vs;
117 struct radv_fs_variant_key fs;
118 struct radv_tes_variant_key tes;
119 struct radv_tcs_variant_key tcs;
120 };
121 bool has_multiview_view_index;
122 };
123
124 struct radv_nir_compiler_options {
125 struct radv_pipeline_layout *layout;
126 struct radv_shader_variant_key key;
127 bool unsafe_math;
128 bool supports_spill;
129 bool clamp_shadow_reference;
130 bool dump_shader;
131 bool dump_preoptir;
132 bool record_llvm_ir;
133 bool check_ir;
134 enum radeon_family family;
135 enum chip_class chip_class;
136 uint32_t tess_offchip_block_dw_size;
137 uint32_t address32_hi;
138 };
139
140 enum radv_ud_index {
141 AC_UD_SCRATCH_RING_OFFSETS = 0,
142 AC_UD_PUSH_CONSTANTS = 1,
143 AC_UD_INLINE_PUSH_CONSTANTS = 2,
144 AC_UD_INDIRECT_DESCRIPTOR_SETS = 3,
145 AC_UD_VIEW_INDEX = 4,
146 AC_UD_STREAMOUT_BUFFERS = 5,
147 AC_UD_SHADER_START = 6,
148 AC_UD_VS_VERTEX_BUFFERS = AC_UD_SHADER_START,
149 AC_UD_VS_BASE_VERTEX_START_INSTANCE,
150 AC_UD_VS_MAX_UD,
151 AC_UD_PS_MAX_UD,
152 AC_UD_CS_GRID_SIZE = AC_UD_SHADER_START,
153 AC_UD_CS_MAX_UD,
154 AC_UD_GS_MAX_UD,
155 AC_UD_TCS_MAX_UD,
156 AC_UD_TES_MAX_UD,
157 AC_UD_MAX_UD = AC_UD_TCS_MAX_UD,
158 };
159
160 struct radv_stream_output {
161 uint8_t location;
162 uint8_t buffer;
163 uint16_t offset;
164 uint8_t component_mask;
165 uint8_t stream;
166 };
167
168 struct radv_streamout_info {
169 uint16_t num_outputs;
170 struct radv_stream_output outputs[MAX_SO_OUTPUTS];
171 uint16_t strides[MAX_SO_BUFFERS];
172 uint32_t enabled_stream_buffers_mask;
173 };
174
175 struct radv_shader_info {
176 bool loads_push_constants;
177 bool loads_dynamic_offsets;
178 uint8_t min_push_constant_used;
179 uint8_t max_push_constant_used;
180 bool has_only_32bit_push_constants;
181 bool has_indirect_push_constants;
182 uint8_t num_inline_push_consts;
183 uint8_t base_inline_push_consts;
184 uint32_t desc_set_used_mask;
185 bool needs_multiview_view_index;
186 bool uses_invocation_id;
187 bool uses_prim_id;
188 struct {
189 uint64_t ls_outputs_written;
190 uint8_t input_usage_mask[VERT_ATTRIB_MAX];
191 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
192 bool has_vertex_buffers; /* needs vertex buffers and base/start */
193 bool needs_draw_id;
194 bool needs_instance_id;
195 } vs;
196 struct {
197 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
198 uint8_t num_stream_output_components[4];
199 uint8_t output_streams[VARYING_SLOT_VAR31 + 1];
200 uint8_t max_stream;
201 } gs;
202 struct {
203 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
204 } tes;
205 struct {
206 bool force_persample;
207 bool needs_sample_positions;
208 bool uses_input_attachments;
209 bool writes_memory;
210 bool writes_z;
211 bool writes_stencil;
212 bool writes_sample_mask;
213 bool has_pcoord;
214 bool prim_id_input;
215 bool layer_input;
216 uint8_t num_input_clips_culls;
217 } ps;
218 struct {
219 bool uses_grid_size;
220 bool uses_block_id[3];
221 bool uses_thread_id[3];
222 bool uses_local_invocation_idx;
223 } cs;
224 struct {
225 uint64_t outputs_written;
226 uint64_t patch_outputs_written;
227 } tcs;
228
229 struct radv_streamout_info so;
230 };
231
232 struct radv_userdata_info {
233 int8_t sgpr_idx;
234 uint8_t num_sgprs;
235 };
236
237 struct radv_userdata_locations {
238 struct radv_userdata_info descriptor_sets[RADV_UD_MAX_SETS];
239 struct radv_userdata_info shader_data[AC_UD_MAX_UD];
240 uint32_t descriptor_sets_enabled;
241 };
242
243 struct radv_vs_output_info {
244 uint8_t vs_output_param_offset[VARYING_SLOT_MAX];
245 uint8_t clip_dist_mask;
246 uint8_t cull_dist_mask;
247 uint8_t param_exports;
248 bool writes_pointsize;
249 bool writes_layer;
250 bool writes_viewport_index;
251 bool export_prim_id;
252 unsigned pos_exports;
253 };
254
255 struct radv_es_output_info {
256 uint32_t esgs_itemsize;
257 };
258
259 struct radv_shader_variant_info {
260 struct radv_userdata_locations user_sgprs_locs;
261 struct radv_shader_info info;
262 unsigned num_user_sgprs;
263 unsigned num_input_sgprs;
264 unsigned num_input_vgprs;
265 unsigned private_mem_vgprs;
266 bool need_indirect_descriptor_sets;
267 struct {
268 struct {
269 struct radv_vs_output_info outinfo;
270 struct radv_es_output_info es_info;
271 bool as_es;
272 bool as_ls;
273 bool export_prim_id;
274 } vs;
275 struct {
276 unsigned num_interp;
277 uint32_t input_mask;
278 uint32_t flat_shaded_mask;
279 uint32_t float16_shaded_mask;
280 bool can_discard;
281 bool early_fragment_test;
282 } fs;
283 struct {
284 unsigned block_size[3];
285 } cs;
286 struct {
287 unsigned vertices_in;
288 unsigned vertices_out;
289 unsigned output_prim;
290 unsigned invocations;
291 unsigned gsvs_vertex_size;
292 unsigned max_gsvs_emit_size;
293 unsigned es_type; /* GFX9: VS or TES */
294 } gs;
295 struct {
296 unsigned tcs_vertices_out;
297 uint32_t num_patches;
298 uint32_t lds_size;
299 } tcs;
300 struct {
301 struct radv_vs_output_info outinfo;
302 struct radv_es_output_info es_info;
303 bool as_es;
304 unsigned primitive_mode;
305 enum gl_tess_spacing spacing;
306 bool ccw;
307 bool point_mode;
308 bool export_prim_id;
309 } tes;
310 };
311 };
312
313 enum radv_shader_binary_type {
314 RADV_BINARY_TYPE_LEGACY,
315 RADV_BINARY_TYPE_RTLD
316 };
317
318 struct radv_shader_binary {
319 enum radv_shader_binary_type type;
320 gl_shader_stage stage;
321 bool is_gs_copy_shader;
322
323 struct radv_shader_variant_info variant_info;
324
325 /* Self-referential size so we avoid consistency issues. */
326 uint32_t total_size;
327 };
328
329 struct radv_shader_binary_legacy {
330 struct radv_shader_binary base;
331 struct ac_shader_config config;
332 unsigned code_size;
333 unsigned llvm_ir_size;
334 unsigned disasm_size;
335
336 /* data has size of code_size + llvm_ir_size + disasm_size + 2, where
337 * the +2 is for 0 of the ir strings. */
338 uint8_t data[0];
339 };
340
341 struct radv_shader_binary_rtld {
342 struct radv_shader_binary base;
343 unsigned elf_size;
344 unsigned llvm_ir_size;
345 uint8_t data[0];
346 };
347
348 struct radv_shader_variant {
349 uint32_t ref_count;
350
351 struct radeon_winsys_bo *bo;
352 uint64_t bo_offset;
353 struct ac_shader_config config;
354 uint32_t code_size;
355 struct radv_shader_variant_info info;
356
357 /* debug only */
358 uint32_t *spirv;
359 uint32_t spirv_size;
360 struct nir_shader *nir;
361 char *disasm_string;
362 char *llvm_ir_string;
363
364 struct list_head slab_list;
365 };
366
367 struct radv_shader_slab {
368 struct list_head slabs;
369 struct list_head shaders;
370 struct radeon_winsys_bo *bo;
371 uint64_t size;
372 char *ptr;
373 };
374
375 void
376 radv_optimize_nir(struct nir_shader *shader, bool optimize_conservatively,
377 bool allow_copies);
378 bool
379 radv_nir_lower_ycbcr_textures(nir_shader *shader,
380 const struct radv_pipeline_layout *layout);
381
382 nir_shader *
383 radv_shader_compile_to_nir(struct radv_device *device,
384 struct radv_shader_module *module,
385 const char *entrypoint_name,
386 gl_shader_stage stage,
387 const VkSpecializationInfo *spec_info,
388 const VkPipelineCreateFlags flags,
389 const struct radv_pipeline_layout *layout);
390
391 void *
392 radv_alloc_shader_memory(struct radv_device *device,
393 struct radv_shader_variant *shader);
394
395 void
396 radv_destroy_shader_slabs(struct radv_device *device);
397
398 struct radv_shader_variant *
399 radv_shader_variant_create(struct radv_device *device,
400 const struct radv_shader_binary *binary);
401 struct radv_shader_variant *
402 radv_shader_variant_compile(struct radv_device *device,
403 struct radv_shader_module *module,
404 struct nir_shader *const *shaders,
405 int shader_count,
406 struct radv_pipeline_layout *layout,
407 const struct radv_shader_variant_key *key,
408 struct radv_shader_binary **binary_out);
409
410 struct radv_shader_variant *
411 radv_create_gs_copy_shader(struct radv_device *device, struct nir_shader *nir,
412 struct radv_shader_binary **binary_out,
413 bool multiview);
414
415 void
416 radv_shader_variant_destroy(struct radv_device *device,
417 struct radv_shader_variant *variant);
418
419 const char *
420 radv_get_shader_name(struct radv_shader_variant *var, gl_shader_stage stage);
421
422 void
423 radv_shader_dump_stats(struct radv_device *device,
424 struct radv_shader_variant *variant,
425 gl_shader_stage stage,
426 FILE *file);
427
428 static inline bool
429 radv_can_dump_shader(struct radv_device *device,
430 struct radv_shader_module *module,
431 bool is_gs_copy_shader)
432 {
433 if (!(device->instance->debug_flags & RADV_DEBUG_DUMP_SHADERS))
434 return false;
435
436 /* Only dump non-meta shaders, useful for debugging purposes. */
437 return (module && !module->nir) || is_gs_copy_shader;
438 }
439
440 static inline bool
441 radv_can_dump_shader_stats(struct radv_device *device,
442 struct radv_shader_module *module)
443 {
444 /* Only dump non-meta shader stats. */
445 return device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS &&
446 module && !module->nir;
447 }
448
449 static inline unsigned shader_io_get_unique_index(gl_varying_slot slot)
450 {
451 /* handle patch indices separate */
452 if (slot == VARYING_SLOT_TESS_LEVEL_OUTER)
453 return 0;
454 if (slot == VARYING_SLOT_TESS_LEVEL_INNER)
455 return 1;
456 if (slot >= VARYING_SLOT_PATCH0 && slot <= VARYING_SLOT_TESS_MAX)
457 return 2 + (slot - VARYING_SLOT_PATCH0);
458 if (slot == VARYING_SLOT_POS)
459 return 0;
460 if (slot == VARYING_SLOT_PSIZ)
461 return 1;
462 if (slot == VARYING_SLOT_CLIP_DIST0)
463 return 2;
464 if (slot == VARYING_SLOT_CLIP_DIST1)
465 return 3;
466 /* 3 is reserved for clip dist as well */
467 if (slot >= VARYING_SLOT_VAR0 && slot <= VARYING_SLOT_VAR31)
468 return 4 + (slot - VARYING_SLOT_VAR0);
469 unreachable("illegal slot in get unique index\n");
470 }
471
472 #endif