radv: adjust CB_SHADER_MASK for dual-source blending in the shader info pass
[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 "ac_binary.h"
32 #include "amd_family.h"
33 #include "radv_constants.h"
34
35 #include "nir/nir.h"
36 #include "vulkan/vulkan.h"
37 #include "vulkan/util/vk_object.h"
38
39 struct radv_device;
40
41 struct radv_shader_module {
42 struct vk_object_base base;
43 struct nir_shader *nir;
44 unsigned char sha1[20];
45 uint32_t size;
46 char data[0];
47 };
48
49 enum {
50 RADV_ALPHA_ADJUST_NONE = 0,
51 RADV_ALPHA_ADJUST_SNORM = 1,
52 RADV_ALPHA_ADJUST_SINT = 2,
53 RADV_ALPHA_ADJUST_SSCALED = 3,
54 };
55
56 struct radv_vs_out_key {
57 uint32_t as_es:1;
58 uint32_t as_ls:1;
59 uint32_t as_ngg:1;
60 uint32_t as_ngg_passthrough:1;
61 uint32_t export_prim_id:1;
62 uint32_t export_layer_id:1;
63 uint32_t export_clip_dists:1;
64 uint32_t export_viewport_index:1;
65 };
66
67 struct radv_vs_variant_key {
68 struct radv_vs_out_key out;
69
70 uint32_t instance_rate_inputs;
71 uint32_t instance_rate_divisors[MAX_VERTEX_ATTRIBS];
72 uint8_t vertex_attribute_formats[MAX_VERTEX_ATTRIBS];
73 uint32_t vertex_attribute_bindings[MAX_VERTEX_ATTRIBS];
74 uint32_t vertex_attribute_offsets[MAX_VERTEX_ATTRIBS];
75 uint32_t vertex_attribute_strides[MAX_VERTEX_ATTRIBS];
76
77 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
78 * so we may need to fix it up. */
79 uint64_t alpha_adjust;
80
81 /* For some formats the channels have to be shuffled. */
82 uint32_t post_shuffle;
83
84 /* Output primitive type. */
85 uint8_t outprim;
86 };
87
88 struct radv_tes_variant_key {
89 struct radv_vs_out_key out;
90
91 uint8_t num_patches;
92 uint8_t tcs_num_outputs;
93 };
94
95 struct radv_tcs_variant_key {
96 struct radv_vs_variant_key vs_key;
97 unsigned primitive_mode;
98 unsigned input_vertices;
99 unsigned num_inputs;
100 uint32_t tes_reads_tess_factors:1;
101 };
102
103 struct radv_fs_variant_key {
104 uint32_t col_format;
105 uint8_t log2_ps_iter_samples;
106 uint8_t num_samples;
107 uint32_t is_int8;
108 uint32_t is_int10;
109 bool is_dual_src;
110 };
111
112 struct radv_cs_variant_key {
113 uint8_t subgroup_size;
114 };
115
116 struct radv_shader_variant_key {
117 union {
118 struct radv_vs_variant_key vs;
119 struct radv_fs_variant_key fs;
120 struct radv_tes_variant_key tes;
121 struct radv_tcs_variant_key tcs;
122 struct radv_cs_variant_key cs;
123
124 /* A common prefix of the vs and tes keys. */
125 struct radv_vs_out_key vs_common_out;
126 };
127 bool has_multiview_view_index;
128 };
129
130 struct radv_nir_compiler_options {
131 struct radv_pipeline_layout *layout;
132 struct radv_shader_variant_key key;
133 bool explicit_scratch_args;
134 bool clamp_shadow_reference;
135 bool robust_buffer_access;
136 bool dump_shader;
137 bool dump_preoptir;
138 bool record_ir;
139 bool record_stats;
140 bool check_ir;
141 bool has_ls_vgpr_init_bug;
142 bool use_ngg_streamout;
143 bool enable_mrt_output_nan_fixup;
144 enum radeon_family family;
145 enum chip_class chip_class;
146 uint32_t tess_offchip_block_dw_size;
147 uint32_t address32_hi;
148 };
149
150 enum radv_ud_index {
151 AC_UD_SCRATCH_RING_OFFSETS = 0,
152 AC_UD_PUSH_CONSTANTS = 1,
153 AC_UD_INLINE_PUSH_CONSTANTS = 2,
154 AC_UD_INDIRECT_DESCRIPTOR_SETS = 3,
155 AC_UD_VIEW_INDEX = 4,
156 AC_UD_STREAMOUT_BUFFERS = 5,
157 AC_UD_NGG_GS_STATE = 6,
158 AC_UD_SHADER_START = 7,
159 AC_UD_VS_VERTEX_BUFFERS = AC_UD_SHADER_START,
160 AC_UD_VS_BASE_VERTEX_START_INSTANCE,
161 AC_UD_VS_MAX_UD,
162 AC_UD_PS_MAX_UD,
163 AC_UD_CS_GRID_SIZE = AC_UD_SHADER_START,
164 AC_UD_CS_MAX_UD,
165 AC_UD_GS_MAX_UD,
166 AC_UD_TCS_MAX_UD,
167 AC_UD_TES_MAX_UD,
168 AC_UD_MAX_UD = AC_UD_TCS_MAX_UD,
169 };
170
171 struct radv_stream_output {
172 uint8_t location;
173 uint8_t buffer;
174 uint16_t offset;
175 uint8_t component_mask;
176 uint8_t stream;
177 };
178
179 struct radv_streamout_info {
180 uint16_t num_outputs;
181 struct radv_stream_output outputs[MAX_SO_OUTPUTS];
182 uint16_t strides[MAX_SO_BUFFERS];
183 uint32_t enabled_stream_buffers_mask;
184 };
185
186 struct radv_userdata_info {
187 int8_t sgpr_idx;
188 uint8_t num_sgprs;
189 };
190
191 struct radv_userdata_locations {
192 struct radv_userdata_info descriptor_sets[MAX_SETS];
193 struct radv_userdata_info shader_data[AC_UD_MAX_UD];
194 uint32_t descriptor_sets_enabled;
195 };
196
197 struct radv_vs_output_info {
198 uint8_t vs_output_param_offset[VARYING_SLOT_MAX];
199 uint8_t clip_dist_mask;
200 uint8_t cull_dist_mask;
201 uint8_t param_exports;
202 bool writes_pointsize;
203 bool writes_layer;
204 bool writes_viewport_index;
205 bool export_prim_id;
206 unsigned pos_exports;
207 };
208
209 struct radv_es_output_info {
210 uint32_t esgs_itemsize;
211 };
212
213 struct gfx9_gs_info {
214 uint32_t vgt_gs_onchip_cntl;
215 uint32_t vgt_gs_max_prims_per_subgroup;
216 uint32_t vgt_esgs_ring_itemsize;
217 uint32_t lds_size;
218 };
219
220 struct gfx10_ngg_info {
221 uint16_t ngg_emit_size; /* in dwords */
222 uint32_t hw_max_esverts;
223 uint32_t max_gsprims;
224 uint32_t max_out_verts;
225 uint32_t prim_amp_factor;
226 uint32_t vgt_esgs_ring_itemsize;
227 uint32_t esgs_ring_size;
228 bool max_vert_out_per_gs_instance;
229 };
230
231 struct radv_shader_info {
232 bool loads_push_constants;
233 bool loads_dynamic_offsets;
234 uint8_t min_push_constant_used;
235 uint8_t max_push_constant_used;
236 bool has_only_32bit_push_constants;
237 bool has_indirect_push_constants;
238 uint8_t num_inline_push_consts;
239 uint8_t base_inline_push_consts;
240 uint32_t desc_set_used_mask;
241 bool needs_multiview_view_index;
242 bool uses_invocation_id;
243 bool uses_prim_id;
244 uint8_t wave_size;
245 uint8_t ballot_bit_size;
246 struct radv_userdata_locations user_sgprs_locs;
247 unsigned num_user_sgprs;
248 unsigned num_input_sgprs;
249 unsigned num_input_vgprs;
250 unsigned private_mem_vgprs;
251 bool need_indirect_descriptor_sets;
252 bool is_ngg;
253 bool is_ngg_passthrough;
254 struct {
255 uint64_t ls_outputs_written;
256 uint8_t input_usage_mask[VERT_ATTRIB_MAX];
257 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
258 bool has_vertex_buffers; /* needs vertex buffers and base/start */
259 bool needs_draw_id;
260 bool needs_instance_id;
261 struct radv_vs_output_info outinfo;
262 struct radv_es_output_info es_info;
263 bool as_es;
264 bool as_ls;
265 bool export_prim_id;
266 uint8_t num_linked_outputs;
267 } vs;
268 struct {
269 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
270 uint8_t num_stream_output_components[4];
271 uint8_t output_streams[VARYING_SLOT_VAR31 + 1];
272 uint8_t max_stream;
273 bool writes_memory;
274 unsigned gsvs_vertex_size;
275 unsigned max_gsvs_emit_size;
276 unsigned vertices_in;
277 unsigned vertices_out;
278 unsigned output_prim;
279 unsigned invocations;
280 unsigned es_type; /* GFX9: VS or TES */
281 uint8_t num_linked_inputs;
282 } gs;
283 struct {
284 uint8_t output_usage_mask[VARYING_SLOT_VAR31 + 1];
285 struct radv_vs_output_info outinfo;
286 struct radv_es_output_info es_info;
287 bool as_es;
288 unsigned primitive_mode;
289 enum gl_tess_spacing spacing;
290 bool ccw;
291 bool point_mode;
292 bool export_prim_id;
293 uint8_t num_linked_inputs;
294 uint8_t num_linked_patch_inputs;
295 uint8_t num_linked_outputs;
296 } tes;
297 struct {
298 bool force_persample;
299 bool needs_sample_positions;
300 bool writes_memory;
301 bool writes_z;
302 bool writes_stencil;
303 bool writes_sample_mask;
304 bool has_pcoord;
305 bool prim_id_input;
306 bool layer_input;
307 bool viewport_index_input;
308 uint8_t num_input_clips_culls;
309 uint32_t input_mask;
310 uint32_t flat_shaded_mask;
311 uint32_t explicit_shaded_mask;
312 uint32_t float16_shaded_mask;
313 uint32_t num_interp;
314 uint32_t cb_shader_mask;
315 bool can_discard;
316 bool early_fragment_test;
317 bool post_depth_coverage;
318 uint8_t depth_layout;
319 } ps;
320 struct {
321 bool uses_grid_size;
322 bool uses_block_id[3];
323 bool uses_thread_id[3];
324 bool uses_local_invocation_idx;
325 unsigned block_size[3];
326 } cs;
327 struct {
328 uint64_t outputs_written;
329 uint64_t patch_outputs_written;
330 uint64_t tes_inputs_read;
331 uint64_t tes_patch_inputs_read;
332 unsigned tcs_vertices_out;
333 uint32_t num_patches;
334 uint32_t lds_size;
335 uint8_t num_linked_inputs;
336 uint8_t num_linked_outputs;
337 uint8_t num_linked_patch_outputs;
338 } tcs;
339
340 struct radv_streamout_info so;
341
342 struct gfx9_gs_info gs_ring_info;
343 struct gfx10_ngg_info ngg_info;
344
345 unsigned float_controls_mode;
346 };
347
348 enum radv_shader_binary_type {
349 RADV_BINARY_TYPE_LEGACY,
350 RADV_BINARY_TYPE_RTLD
351 };
352
353 struct radv_shader_binary {
354 enum radv_shader_binary_type type;
355 gl_shader_stage stage;
356 bool is_gs_copy_shader;
357
358 struct radv_shader_info info;
359
360 /* Self-referential size so we avoid consistency issues. */
361 uint32_t total_size;
362 };
363
364 struct radv_shader_binary_legacy {
365 struct radv_shader_binary base;
366 struct ac_shader_config config;
367 unsigned code_size;
368 unsigned exec_size;
369 unsigned ir_size;
370 unsigned disasm_size;
371 unsigned stats_size;
372
373 /* data has size of stats_size + code_size + ir_size + disasm_size + 2,
374 * where the +2 is for 0 of the ir strings. */
375 uint8_t data[0];
376 };
377
378 struct radv_shader_binary_rtld {
379 struct radv_shader_binary base;
380 unsigned elf_size;
381 unsigned llvm_ir_size;
382 uint8_t data[0];
383 };
384
385 struct radv_compiler_statistic_info {
386 char name[32];
387 char desc[64];
388 };
389
390 struct radv_compiler_statistics {
391 unsigned count;
392 struct radv_compiler_statistic_info *infos;
393 uint32_t values[];
394 };
395
396 struct radv_shader_variant {
397 uint32_t ref_count;
398
399 struct radeon_winsys_bo *bo;
400 uint64_t bo_offset;
401 struct ac_shader_config config;
402 uint32_t code_size;
403 uint32_t exec_size;
404 struct radv_shader_info info;
405
406 /* debug only */
407 char *spirv;
408 uint32_t spirv_size;
409 char *nir_string;
410 char *disasm_string;
411 char *ir_string;
412 struct radv_compiler_statistics *statistics;
413
414 struct list_head slab_list;
415 };
416
417 struct radv_shader_slab {
418 struct list_head slabs;
419 struct list_head shaders;
420 struct radeon_winsys_bo *bo;
421 uint64_t size;
422 char *ptr;
423 };
424
425 void
426 radv_optimize_nir(struct nir_shader *shader, bool optimize_conservatively,
427 bool allow_copies);
428 bool
429 radv_nir_lower_ycbcr_textures(nir_shader *shader,
430 const struct radv_pipeline_layout *layout);
431
432 nir_shader *
433 radv_shader_compile_to_nir(struct radv_device *device,
434 struct radv_shader_module *module,
435 const char *entrypoint_name,
436 gl_shader_stage stage,
437 const VkSpecializationInfo *spec_info,
438 const VkPipelineCreateFlags flags,
439 const struct radv_pipeline_layout *layout,
440 unsigned subgroup_size, unsigned ballot_bit_size);
441
442 void
443 radv_destroy_shader_slabs(struct radv_device *device);
444
445 VkResult
446 radv_create_shaders(struct radv_pipeline *pipeline,
447 struct radv_device *device,
448 struct radv_pipeline_cache *cache,
449 const struct radv_pipeline_key *key,
450 const VkPipelineShaderStageCreateInfo **pStages,
451 const VkPipelineCreateFlags flags,
452 VkPipelineCreationFeedbackEXT *pipeline_feedback,
453 VkPipelineCreationFeedbackEXT **stage_feedbacks);
454
455 struct radv_shader_variant *
456 radv_shader_variant_create(struct radv_device *device,
457 const struct radv_shader_binary *binary,
458 bool keep_shader_info);
459 struct radv_shader_variant *
460 radv_shader_variant_compile(struct radv_device *device,
461 struct radv_shader_module *module,
462 struct nir_shader *const *shaders,
463 int shader_count,
464 struct radv_pipeline_layout *layout,
465 const struct radv_shader_variant_key *key,
466 struct radv_shader_info *info,
467 bool keep_shader_info, bool keep_statistic_info,
468 struct radv_shader_binary **binary_out);
469
470 struct radv_shader_variant *
471 radv_create_gs_copy_shader(struct radv_device *device, struct nir_shader *nir,
472 struct radv_shader_info *info,
473 struct radv_shader_binary **binary_out,
474 bool multiview, bool keep_shader_info,
475 bool keep_statistic_info);
476
477 void
478 radv_shader_variant_destroy(struct radv_device *device,
479 struct radv_shader_variant *variant);
480
481
482 unsigned
483 radv_get_max_waves(struct radv_device *device,
484 struct radv_shader_variant *variant,
485 gl_shader_stage stage);
486
487 unsigned
488 radv_get_max_workgroup_size(enum chip_class chip_class,
489 gl_shader_stage stage,
490 const unsigned *sizes);
491
492 const char *
493 radv_get_shader_name(struct radv_shader_info *info,
494 gl_shader_stage stage);
495
496 void
497 radv_shader_dump_stats(struct radv_device *device,
498 struct radv_shader_variant *variant,
499 gl_shader_stage stage,
500 FILE *file);
501
502 bool
503 radv_can_dump_shader(struct radv_device *device,
504 struct radv_shader_module *module,
505 bool is_gs_copy_shader);
506
507 bool
508 radv_can_dump_shader_stats(struct radv_device *device,
509 struct radv_shader_module *module);
510
511 static inline unsigned
512 shader_io_get_unique_index(gl_varying_slot slot)
513 {
514 /* handle patch indices separate */
515 if (slot == VARYING_SLOT_TESS_LEVEL_OUTER)
516 return 0;
517 if (slot == VARYING_SLOT_TESS_LEVEL_INNER)
518 return 1;
519 if (slot >= VARYING_SLOT_PATCH0 && slot <= VARYING_SLOT_TESS_MAX)
520 return 2 + (slot - VARYING_SLOT_PATCH0);
521 if (slot == VARYING_SLOT_POS)
522 return 0;
523 if (slot == VARYING_SLOT_PSIZ)
524 return 1;
525 if (slot == VARYING_SLOT_CLIP_DIST0)
526 return 2;
527 if (slot == VARYING_SLOT_CLIP_DIST1)
528 return 3;
529 /* 3 is reserved for clip dist as well */
530 if (slot >= VARYING_SLOT_VAR0 && slot <= VARYING_SLOT_VAR31)
531 return 4 + (slot - VARYING_SLOT_VAR0);
532 unreachable("illegal slot in get unique index\n");
533 }
534
535 static inline unsigned
536 calculate_tess_lds_size(unsigned tcs_num_input_vertices,
537 unsigned tcs_num_output_vertices,
538 unsigned tcs_num_inputs,
539 unsigned tcs_num_patches,
540 unsigned tcs_num_outputs,
541 unsigned tcs_num_patch_outputs)
542 {
543 unsigned input_vertex_size = tcs_num_inputs * 16;
544 unsigned output_vertex_size = tcs_num_outputs * 16;
545
546 unsigned input_patch_size = tcs_num_input_vertices * input_vertex_size;
547
548 unsigned pervertex_output_patch_size = tcs_num_output_vertices * output_vertex_size;
549 unsigned output_patch_size = pervertex_output_patch_size + tcs_num_patch_outputs * 16;
550
551 unsigned output_patch0_offset = input_patch_size * tcs_num_patches;
552
553 return output_patch0_offset + output_patch_size * tcs_num_patches;
554 }
555
556 static inline unsigned
557 get_tcs_num_patches(unsigned tcs_num_input_vertices,
558 unsigned tcs_num_output_vertices,
559 unsigned tcs_num_inputs,
560 unsigned tcs_num_outputs,
561 unsigned tcs_num_patch_outputs,
562 unsigned tess_offchip_block_dw_size,
563 enum chip_class chip_class,
564 enum radeon_family family)
565 {
566 uint32_t input_vertex_size = tcs_num_inputs * 16;
567 uint32_t input_patch_size = tcs_num_input_vertices * input_vertex_size;
568 uint32_t output_vertex_size = tcs_num_outputs * 16;
569 uint32_t pervertex_output_patch_size = tcs_num_output_vertices * output_vertex_size;
570 uint32_t output_patch_size = pervertex_output_patch_size + tcs_num_patch_outputs * 16;
571
572 /* Ensure that we only need one wave per SIMD so we don't need to check
573 * resource usage. Also ensures that the number of tcs in and out
574 * vertices per threadgroup are at most 256.
575 */
576 unsigned num_patches = 64 / MAX2(tcs_num_input_vertices, tcs_num_output_vertices) * 4;
577 /* Make sure that the data fits in LDS. This assumes the shaders only
578 * use LDS for the inputs and outputs.
579 */
580 unsigned hardware_lds_size = 32768;
581
582 /* Looks like STONEY hangs if we use more than 32 KiB LDS in a single
583 * threadgroup, even though there is more than 32 KiB LDS.
584 *
585 * Test: dEQP-VK.tessellation.shader_input_output.barrier
586 */
587 if (chip_class >= GFX7 && family != CHIP_STONEY)
588 hardware_lds_size = 65536;
589
590 num_patches = MIN2(num_patches, hardware_lds_size / (input_patch_size + output_patch_size));
591 /* Make sure the output data fits in the offchip buffer */
592 num_patches = MIN2(num_patches, (tess_offchip_block_dw_size * 4) / output_patch_size);
593 /* Not necessary for correctness, but improves performance. The
594 * specific value is taken from the proprietary driver.
595 */
596 num_patches = MIN2(num_patches, 40);
597
598 /* GFX6 bug workaround - limit LS-HS threadgroups to only one wave. */
599 if (chip_class == GFX6) {
600 unsigned one_wave = 64 / MAX2(tcs_num_input_vertices, tcs_num_output_vertices);
601 num_patches = MIN2(num_patches, one_wave);
602 }
603 return num_patches;
604 }
605
606 void
607 radv_lower_fs_io(nir_shader *nir);
608
609 #endif