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