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