1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Copyright © 2024 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef VK_SHADER_H
#define VK_SHADER_H
#include "compiler/spirv/nir_spirv.h"
#include "vk_internal_exts.h"
#include "vk_limits.h"
#include "vk_pipeline_cache.h"
#include "util/mesa-blake3.h"
#ifdef __cplusplus
extern "C" {
#endif
struct blob;
struct nir_shader;
struct vk_command_buffer;
struct vk_device;
struct vk_descriptor_set_layout;
struct vk_dynamic_graphics_state;
struct vk_graphics_pipeline_state;
struct vk_features;
struct vk_physical_device;
struct vk_pipeline;
struct vk_pipeline_robustness_state;
int vk_shader_cmp_graphics_stages(mesa_shader_stage a, mesa_shader_stage b);
int vk_shader_cmp_rt_stages(mesa_shader_stage a, mesa_shader_stage b);
#define MESA_VK_PIPELINE_RAY_TRACING_FLAGS ( \
VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_BUILT_IN_PRIMITIVES_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_ALLOW_SPHERES_AND_LINEAR_SWEPT_SPHERES_BIT_NV | \
VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_AABBS_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_ANY_HIT_SHADERS_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_CLOSEST_HIT_SHADERS_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_MISS_SHADERS_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_INTERSECTION_SHADERS_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR | \
VK_PIPELINE_CREATE_2_RAY_TRACING_ALLOW_MOTION_BIT_NV | \
VK_PIPELINE_CREATE_2_RAY_TRACING_OPACITY_MICROMAP_BIT_EXT | \
VK_PIPELINE_CREATE_2_RAY_TRACING_DISPLACEMENT_MICROMAP_BIT_NV | \
VK_PIPELINE_CREATE_2_DISALLOW_OPACITY_MICROMAP_BIT_ARM)
struct vk_shader_compile_info {
mesa_shader_stage stage;
VkShaderCreateFlagsEXT flags;
/* RT flags only includes :
* - VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_BUILT_IN_PRIMITIVES_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_ALLOW_SPHERES_AND_LINEAR_SWEPT_SPHERES_BIT_NV
* - VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_AABBS_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_ANY_HIT_SHADERS_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_CLOSEST_HIT_SHADERS_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_MISS_SHADERS_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_NO_NULL_INTERSECTION_SHADERS_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR
* - VK_PIPELINE_CREATE_2_RAY_TRACING_ALLOW_MOTION_BIT_NV
* - VK_PIPELINE_CREATE_2_RAY_TRACING_OPACITY_MICROMAP_BIT_EXT
* - VK_PIPELINE_CREATE_2_RAY_TRACING_DISPLACEMENT_MICROMAP_BIT_NV
* - VK_PIPELINE_CREATE_2_DISALLOW_OPACITY_MICROMAP_BIT_ARM
*/
VkPipelineCreateFlags2KHR rt_flags;
VkShaderStageFlags next_stage_mask;
struct nir_shader *nir;
const struct vk_pipeline_robustness_state *robustness;
uint32_t set_layout_count;
struct vk_descriptor_set_layout * const *set_layouts;
uint32_t push_constant_range_count;
const VkPushConstantRange *push_constant_ranges;
};
struct vk_shader_ops;
struct vk_shader {
struct vk_object_base base;
const struct vk_shader_ops *ops;
mesa_shader_stage stage;
/* Stack size for RT shaders */
VkDeviceSize stack_size;
/* Scratch size used by the shader */
VkDeviceSize scratch_size;
/* Number of ray queries used by the shader (useful to avoid going through
* all of the shaders in the RT pipelines)
*/
uint32_t ray_queries;
/* Used for the generic VkPipeline implementation */
struct {
struct vk_pipeline_cache_object cache_obj;
blake3_hash cache_key;
} pipeline;
};
VK_DEFINE_NONDISP_HANDLE_CASTS(vk_shader, base, VkShaderEXT,
VK_OBJECT_TYPE_SHADER_EXT);
struct vk_shader_ops {
/** Destroy a vk_shader_object */
void (*destroy)(struct vk_device *device,
struct vk_shader *shader,
const VkAllocationCallbacks* pAllocator);
/** Serialize a vk_shader_object to a blob
*
* This function shouldn't need to do any validation of the blob data
* beyond basic sanity checking. The common implementation of
* vkGetShaderBinaryEXT verifies the blobUUID and version of input data as
* well as a size and checksum to ensure integrity. This callback is only
* invoked after validation of the input binary data.
*/
bool (*serialize)(struct vk_device *device,
const struct vk_shader *shader,
struct blob *blob);
/** Returns executable properties for this shader
*
* This is equivalent to vkGetPipelineExecutableProperties(), only for a
* single vk_shader.
*/
VkResult (*get_executable_properties)(struct vk_device *device,
const struct vk_shader *shader,
uint32_t *executable_count,
VkPipelineExecutablePropertiesKHR *properties);
/** Returns executable statistics for this shader
*
* This is equivalent to vkGetPipelineExecutableStatistics(), only for a
* single vk_shader.
*/
VkResult (*get_executable_statistics)(struct vk_device *device,
const struct vk_shader *shader,
uint32_t executable_index,
uint32_t *statistic_count,
VkPipelineExecutableStatisticKHR *statistics);
/** Returns executable internal representations for this shader
*
* This is equivalent to vkGetPipelineExecutableInternalRepresentations(),
* only for a single vk_shader.
*/
VkResult (*get_executable_internal_representations)(
struct vk_device *device,
const struct vk_shader *shader,
uint32_t executable_index,
uint32_t *internal_representation_count,
VkPipelineExecutableInternalRepresentationKHR *internal_representations);
};
void *vk_shader_zalloc(struct vk_device *device,
const struct vk_shader_ops *ops,
mesa_shader_stage stage,
const VkAllocationCallbacks *alloc,
size_t size);
void *vk_shader_multizalloc(struct vk_device *device,
struct vk_multialloc *ma,
const struct vk_shader_ops *ops,
mesa_shader_stage stage,
const VkAllocationCallbacks *alloc);
void vk_shader_free(struct vk_device *device,
const VkAllocationCallbacks *alloc,
struct vk_shader *shader);
static inline void
vk_shader_destroy(struct vk_device *device,
struct vk_shader *shader,
const VkAllocationCallbacks *alloc)
{
shader->ops->destroy(device, shader, alloc);
}
struct vk_device_shader_ops {
/** Retrieves a NIR compiler options struct
*
* NIR compiler options are only allowed to vary based on physical device,
* stage, and robustness state.
*/
const struct nir_shader_compiler_options *(*get_nir_options)(
struct vk_physical_device *device,
mesa_shader_stage stage,
const struct vk_pipeline_robustness_state *rs);
/** Retrieves a SPIR-V options struct
*
* SPIR-V options are only allowed to vary based on physical device, stage,
* and robustness state.
*/
struct spirv_to_nir_options (*get_spirv_options)(
struct vk_physical_device *device,
mesa_shader_stage stage,
const struct vk_pipeline_robustness_state *rs);
/** Preprocesses a NIR shader
*
* This callback is optional.
*
* If non-NULL, this callback is invoked after the SPIR-V is parsed into
* NIR and before it is handed to compile(). The driver should do as much
* generic optimization and lowering as it can here. Importantly, the
* preprocess step only knows about the NIR input and the physical device,
* not any enabled device features or pipeline state. This allows us to
* potentially cache this shader and re-use it across pipelines.
*/
void (*preprocess_nir)(
struct vk_physical_device *device,
nir_shader *nir,
const struct vk_pipeline_robustness_state *rs);
/** Return shader stages that should be linked together in a group
*
* The driver should return 0 if it does not require any linking, otherwise
* it should return the stages that need to be linked together. The return
* value should have more than one shader stage and be included in the
* stages given as parameter.
*/
VkShaderStageFlags (*get_rt_group_linking)(struct vk_physical_device *device,
VkShaderStageFlags stages);
/** Hash a vk_graphics_state object and a vk_features object.
*
* This callback hashes whatever bits of vk_graphics_pipeline_state might
* be used to compile a shader in one of the given stages.
*
* state may be null, indicating that all state is dynamic. enabled_features
* is always non-NULL.
*/
void (*hash_state)(struct vk_physical_device *device,
const struct vk_graphics_pipeline_state *state,
const struct vk_features *enabled_features,
VkShaderStageFlags stages,
blake3_hash blake3_out);
/** Compile (and potentially link) a set of shaders
*
* Unlike vkCreateShadersEXT, this callback will only ever be called with
* multiple shaders if VK_SHADER_CREATE_LINK_STAGE_BIT_EXT is set on all of
* them. We also guarantee that the shaders occur in the call in Vulkan
* pipeline stage order as dictated by vk_shader_cmp_graphics_stages().
*
* If state/enabled_features is NULL, the driver must conservatively assume
* all state is dynamic / all features are enabled respectively.
*
* This callback consumes all input NIR shaders, regardless of whether or
* not it was successful.
*/
VkResult (*compile)(struct vk_device *device,
uint32_t shader_count,
struct vk_shader_compile_info *infos,
const struct vk_graphics_pipeline_state *state,
const struct vk_features *enabled_features,
const VkAllocationCallbacks* pAllocator,
struct vk_shader **shaders_out);
/** Create a vk_shader from a binary blob */
VkResult (*deserialize)(struct vk_device *device,
struct blob_reader *blob,
uint32_t binary_version,
const VkAllocationCallbacks* pAllocator,
struct vk_shader **shader_out);
/** Writes a HW shader record from a shader group */
void (*write_rt_shader_group)(struct vk_device *device,
VkRayTracingShaderGroupTypeKHR type,
const struct vk_shader **shaders,
uint32_t shader_count,
void *output);
/** Writes a group replay handle for a shader group */
void (*write_rt_shader_group_replay_handle)(struct vk_device *device,
const struct vk_shader **shaders,
uint32_t shader_count,
void *output);
/** Bind a set of shaders
*
* This is roughly equivalent to vkCmdBindShadersEXT()
*/
void (*cmd_bind_shaders)(struct vk_command_buffer *cmd_buffer,
uint32_t stage_count,
const mesa_shader_stage *stages,
struct vk_shader ** const shaders);
/** Sets dynamic state */
void (*cmd_set_dynamic_graphics_state)(struct vk_command_buffer *cmd_buffer,
const struct vk_dynamic_graphics_state *state);
/** Sets scratch size & ray query count for RT pipelines */
void (*cmd_set_rt_state)(struct vk_command_buffer *cmd_buffer,
VkDeviceSize scratch_size,
uint32_t ray_queries);
/** Sets stack size for RT pipelines */
void (*cmd_set_stack_size)(struct vk_command_buffer *cmd_buffer,
VkDeviceSize stack_size);
};
extern const struct vk_pipeline_robustness_state vk_robustness_disabled;
#ifdef __cplusplus
}
#endif
#endif /* VK_SHADER_H */
|