Debian Codegraph @

src/asahi/vulkan/hk_shader.h

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #pragma once
9
10 #include "asahi/compiler/agx_compile.h"
11 #include "poly/nir/poly_nir.h"
12 #include "util/macros.h"
13 #include "agx_linker.h"
14 #include "agx_nir_lower_vbo.h"
15 #include "agx_pack.h"
16 #include "agx_usc.h"
17 #include "agx_uvs.h"
18
19 #include "hk_device.h"
20 #include "hk_device_memory.h"
21 #include "hk_private.h"
22
23 #include "nir_xfb_info.h"
24 #include "shader_enums.h"
25 #include "vk_pipeline_cache.h"
26
27 #include "nir_defines.h"
28
29 #include "vk_shader.h"
30
31 struct hk_physical_device;
32 struct hk_pipeline_compilation_ctx;
33 struct vk_descriptor_set_layout;
34 struct vk_graphics_pipeline_state;
35 struct vk_pipeline_cache;
36 struct vk_pipeline_layout;
37 struct vk_pipeline_robustness_state;
38 struct vk_shader_module;
39
40 struct hk_tess_info {
41 enum tess_primitive_mode mode : 8;
42 enum gl_tess_spacing spacing : 8;
43 bool points;
44 bool ccw;
45 };
46 static_assert(sizeof(struct hk_tess_info) == 4, "packed");
47
48 static struct hk_tess_info
49 hk_tess_info_merge(struct hk_tess_info a, struct hk_tess_info b)
50 {
51 static_assert(TESS_PRIMITIVE_UNSPECIFIED == 0, "zero state");
52 static_assert(TESS_SPACING_UNSPECIFIED == 0, "zero state");
53
54 /* Just merge by OR'ing the raw bits */
55 uint32_t x, y;
56 memcpy(&x, &a, sizeof(x));
57 memcpy(&y, &b, sizeof(y));
58
59 x |= y;
60
61 struct hk_tess_info out;
62 memcpy(&out, &x, sizeof(out));
63 return out;
64 }
65
66 struct hk_shader_info {
67 union {
68 struct {
69 uint32_t attribs_read;
70 BITSET_DECLARE(attrib_components_read, AGX_MAX_ATTRIBS * 4);
71 bool use_prolog;
72 uint8_t _pad[7];
73 } vs;
74
75 struct {
76 struct agx_interp_info interp;
77 struct agx_fs_epilog_link_info epilog_key;
78
79 bool reads_sample_mask;
80 bool post_depth_coverage;
81 bool uses_sample_shading;
82 bool early_fragment_tests;
83 bool writes_memory;
84
85 uint8_t _pad[7];
86 } fs;
87
88 struct {
89 uint64_t tcs_per_vertex_outputs;
90 uint32_t tcs_output_stride;
91 uint8_t tcs_output_patch_size;
92 uint8_t tcs_nr_patch_outputs;
93
94 struct hk_tess_info info;
95 } tess;
96
97 struct poly_gs_info gs;
98
99 /* Used to initialize the union for other stages */
100 uint8_t _pad[32];
101 };
102
103 struct agx_unlinked_uvs_layout uvs;
104
105 /* Transform feedback buffer strides */
106 uint8_t xfb_stride[MAX_XFB_BUFFERS];
107
108 mesa_shader_stage stage : 8;
109 uint8_t clip_distance_array_size;
110 uint8_t cull_distance_array_size;
111 uint8_t set_count;
112
113 /* XXX: is there a less goofy way to do this? I really don't want dynamic
114 * allocation here.
115 */
116 nir_xfb_info xfb_info;
117 nir_xfb_output_info xfb_outputs[64];
118 };
119
120 /*
121 * Hash table keys for fast-linked shader variants. These contain the entire
122 * prolog/epilog key so we only do 1 hash table lookup instead of 2 in the
123 * general case where the linked shader is already ready.
124 */
125 struct hk_fast_link_key_vs {
126 struct agx_vs_prolog_key prolog;
127 };
128
129 struct hk_fast_link_key_fs {
130 unsigned nr_samples_shaded;
131 struct agx_fs_prolog_key prolog;
132 struct agx_fs_epilog_key epilog;
133 };
134
135 struct hk_shader {
136 struct agx_shader_part b;
137
138 struct hk_shader_info info;
139 struct agx_fragment_face_2_packed frag_face;
140 struct agx_counts_packed counts;
141
142 const void *code_ptr;
143 uint32_t code_size;
144
145 const void *data_ptr;
146 uint32_t data_size;
147
148 /* BO for any uploaded shader part */
149 struct agx_bo *bo;
150
151 /* Cache of fast linked variants */
152 struct {
153 simple_mtx_t lock;
154 struct hash_table *ht;
155 } linked;
156
157 /* If there's only a single possibly linked variant, direct pointer. TODO:
158 * Union with the cache to save some space?
159 */
160 struct hk_linked_shader *only_linked;
161
162 /* Address to the uploaded preamble section. Preambles are uploaded
163 * separately from fast-linked main shaders.
164 */
165 uint64_t preamble_addr;
166
167 /* Address of the start of the shader data section */
168 uint64_t data_addr;
169 };
170
171 enum hk_vs_variant {
172 /* Hardware vertex shader, when next stage is fragment */
173 HK_VS_VARIANT_HW,
174
175 /* Hardware compute shader, when next is geometry/tessellation */
176 HK_VS_VARIANT_SW,
177
178 HK_VS_VARIANTS,
179 };
180
181 enum hk_gs_variant {
182 /* Hardware vertex shader used for rasterization */
183 HK_GS_VARIANT_RAST,
184
185 /* Main compute shader */
186 HK_GS_VARIANT_MAIN,
187
188 /* Count compute shader */
189 HK_GS_VARIANT_COUNT,
190
191 /* Pre-GS compute shader */
192 HK_GS_VARIANT_PRE,
193
194 HK_GS_VARIANTS,
195 };
196
197 /* clang-format off */
198 static const char *hk_gs_variant_name[] = {
199 [HK_GS_VARIANT_RAST] = "Rasterization",
200 [HK_GS_VARIANT_MAIN] = "Main",
201 [HK_GS_VARIANT_COUNT] = "Count",
202 [HK_GS_VARIANT_PRE] = "Pre-GS",
203 };
204 /* clang-format on */
205
206 static inline unsigned
207 hk_num_variants(mesa_shader_stage stage)
208 {
209 switch (stage) {
210 case MESA_SHADER_VERTEX:
211 case MESA_SHADER_TESS_EVAL:
212 return HK_VS_VARIANTS;
213
214 case MESA_SHADER_GEOMETRY:
215 return HK_GS_VARIANTS;
216
217 default:
218 return 1;
219 }
220 }
221
222 /*
223 * An hk_api shader maps 1:1 to a VkShader object. An hk_api_shader may contain
224 * multiple hardware hk_shader's, built at shader compile time. This complexity
225 * is required to efficiently implement the legacy geometry pipeline.
226 */
227 struct hk_api_shader {
228 struct vk_shader vk;
229
230 /* Is this an internal passthrough geometry shader? */
231 bool is_passthrough;
232
233 struct hk_shader variants[];
234 };
235
236 #define hk_foreach_variant(api_shader, var) \
237 for (struct hk_shader *var = api_shader->variants; \
238 var < api_shader->variants + hk_num_variants(api_shader->vk.stage); \
239 ++var)
240
241 static const char *
242 hk_variant_name(struct hk_api_shader *obj, struct hk_shader *variant)
243 {
244 unsigned i = variant - obj->variants;
245 assert(i < hk_num_variants(obj->vk.stage));
246
247 if (hk_num_variants(obj->vk.stage) == 1) {
248 return NULL;
249 } else if (obj->vk.stage == MESA_SHADER_GEOMETRY) {
250 assert(i < ARRAY_SIZE(hk_gs_variant_name));
251 return hk_gs_variant_name[i];
252 } else {
253 assert(i < 2);
254 return i == HK_VS_VARIANT_SW ? "Software" : "Hardware";
255 }
256 }
257
258 static struct hk_shader *
259 hk_only_variant(struct hk_api_shader *obj)
260 {
261 if (!obj)
262 return NULL;
263
264 assert(hk_num_variants(obj->vk.stage) == 1);
265 return &obj->variants[0];
266 }
267
268 static struct hk_shader *
269 hk_any_variant(struct hk_api_shader *obj)
270 {
271 if (!obj)
272 return NULL;
273
274 return &obj->variants[0];
275 }
276
277 static struct hk_shader *
278 hk_main_gs_variant(struct hk_api_shader *obj)
279 {
280 return &obj->variants[HK_GS_VARIANT_MAIN];
281 }
282
283 static struct hk_shader *
284 hk_count_gs_variant(struct hk_api_shader *obj)
285 {
286 return &obj->variants[HK_GS_VARIANT_COUNT];
287 }
288
289 static struct hk_shader *
290 hk_pre_gs_variant(struct hk_api_shader *obj)
291 {
292 return &obj->variants[HK_GS_VARIANT_PRE];
293 }
294
295 #define HK_MAX_LINKED_USC_SIZE \
296 (AGX_USC_PRESHADER_LENGTH + AGX_USC_FRAGMENT_PROPERTIES_LENGTH + \
297 AGX_USC_REGISTERS_LENGTH + AGX_USC_SHADER_LENGTH + AGX_USC_SHARED_LENGTH + \
298 AGX_USC_SAMPLER_LENGTH + (AGX_USC_UNIFORM_LENGTH * 9))
299
300 struct hk_linked_shader {
301 struct agx_linked_shader b;
302
303 /* True if the VS prolog uses software indexing, either for geom/tess or
304 * adjacency primitives.
305 */
306 bool sw_indexing;
307
308 /* Distinct from hk_shader::counts due to addition of cf_binding_count, which
309 * is delayed since it depends on cull distance.
310 */
311 struct agx_fragment_shader_word_0_packed fs_counts;
312
313 /* Baked USC words to bind this linked shader */
314 struct {
315 uint8_t data[HK_MAX_LINKED_USC_SIZE];
316 size_t size;
317 } usc;
318 };
319
320 struct hk_linked_shader *hk_fast_link(struct hk_device *dev, bool fragment,
321 struct hk_shader *main,
322 struct agx_shader_part *prolog,
323 struct agx_shader_part *epilog,
324 unsigned nr_samples_shaded);
325
326 extern const struct vk_device_shader_ops hk_device_shader_ops;
327
328 uint64_t
329 hk_physical_device_compiler_flags(const struct hk_physical_device *pdev);
330
331 static inline nir_address_format
332 hk_buffer_addr_format(VkPipelineRobustnessBufferBehaviorEXT robustness)
333 {
334 switch (robustness) {
335 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT:
336 return nir_address_format_64bit_global_32bit_offset;
337 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_EXT:
338 case VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_2_EXT:
339 return nir_address_format_64bit_bounded_global;
340 default:
341 UNREACHABLE("Invalid robust buffer access behavior");
342 }
343 }
344
345 bool hk_lower_uvs_index(nir_shader *s, mesa_shader_stage sw_stage,
346 unsigned nr_vbos);
347
348 bool
349 hk_nir_lower_descriptors(nir_shader *nir,
350 const struct vk_pipeline_robustness_state *rs,
351 uint32_t set_layout_count,
352 struct vk_descriptor_set_layout *const *set_layouts);
353
354 VkResult hk_compile_shader(struct hk_device *dev,
355 struct vk_shader_compile_info *info,
356 const struct vk_graphics_pipeline_state *state,
357 const struct vk_features *features,
358 const VkAllocationCallbacks *pAllocator,
359 struct hk_api_shader **shader_out);
360
361 void hk_preprocess_nir_internal(struct vk_physical_device *vk_pdev,
362 nir_shader *nir);
363
364 void hk_api_shader_destroy(struct vk_device *vk_dev,
365 struct vk_shader *vk_shader,
366 const VkAllocationCallbacks *pAllocator);
367
368 const nir_shader_compiler_options *
369 hk_get_nir_options(struct vk_physical_device *vk_pdev, mesa_shader_stage stage,
370 UNUSED const struct vk_pipeline_robustness_state *rs);
371
372 struct hk_api_shader *hk_meta_shader(struct hk_device *dev,
373 hk_internal_builder_t builder, void *data,
374 size_t data_size);
375
376 struct hk_passthrough_gs_key {
377 /* Bit mask of outputs written by the VS/TES, to be passed through */
378 uint64_t outputs;
379
380 /* Clip/cull sizes, implies clip/cull written in output */
381 uint8_t clip_distance_array_size;
382 uint8_t cull_distance_array_size;
383
384 /* Transform feedback buffer strides */
385 uint8_t xfb_stride[MAX_XFB_BUFFERS];
386
387 /* Decomposed primitive */
388 enum mesa_prim prim;
389
390 /* Transform feedback info. Must use hk_passthrough_gs_key_size to get the
391 * key size */
392 nir_xfb_info xfb_info;
393 };
394
395 static inline size_t
396 hk_passthrough_gs_key_size(uint16_t output_count)
397 {
398 return (sizeof(struct hk_passthrough_gs_key) - sizeof(nir_xfb_info)) +
399 nir_xfb_info_size(output_count);
400 }
401
402 void hk_nir_passthrough_gs(struct nir_builder *b, const void *key_);