Debian Codegraph @

servers/rendering/rendering_shader_container.h

raw symbols SCIP
1 /**************************************************************************/
2 /* rendering_shader_container.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/object/ref_counted.h"
34 #include "servers/rendering/rendering_device_commons.h"
35
36 struct SpvReflectShaderModule;
37 struct SpvReflectDescriptorBinding;
38 struct SpvReflectSpecializationConstant;
39
40 class RenderingShaderContainer : public RefCounted {
41 GDSOFTCLASS(RenderingShaderContainer, RefCounted);
42
43 public:
44 static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;
45 static const uint32_t CONTAINER_VERSION = 2;
46
47 protected:
48 using RDC = RenderingDeviceCommons;
49
50 struct ContainerHeader {
51 uint32_t magic_number = 0;
52 uint32_t version = 0;
53 uint32_t format = 0;
54 uint32_t format_version = 0;
55 uint32_t shader_count = 0;
56 };
57
58 struct ReflectionData {
59 uint64_t vertex_input_mask = 0;
60 uint32_t fragment_output_mask = 0;
61 uint32_t specialization_constants_count = 0;
62 uint32_t is_compute = 0;
63 uint32_t has_multiview = 0;
64 uint32_t has_dynamic_buffers = 0;
65 uint32_t compute_local_size[3] = {};
66 uint32_t set_count = 0;
67 uint32_t push_constant_size = 0;
68 uint32_t push_constant_stages_mask = 0;
69 uint32_t stage_count = 0;
70 uint32_t shader_name_len = 0;
71 };
72
73 struct ReflectionBindingData {
74 uint32_t type = 0;
75 uint32_t binding = 0;
76 uint32_t stages = 0;
77 uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).
78 uint32_t writable = 0;
79
80 bool operator<(const ReflectionBindingData &p_other) const {
81 return binding < p_other.binding;
82 }
83 };
84
85 struct ReflectionSpecializationData {
86 uint32_t type = 0;
87 uint32_t constant_id = 0;
88 uint32_t int_value = 0;
89 uint32_t stage_flags = 0;
90 };
91
92 struct ShaderHeader {
93 uint32_t shader_stage = 0;
94 uint32_t code_compressed_size = 0;
95 uint32_t code_compression_flags = 0;
96 uint32_t code_decompressed_size = 0;
97 };
98
99 ReflectionData reflection_data;
100 Vector<uint32_t> reflection_binding_set_uniforms_count;
101 Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;
102 Vector<ReflectionSpecializationData> reflection_specialization_data;
103 Vector<RDC::ShaderStage> reflection_shader_stages;
104
105 virtual uint32_t _format() const = 0;
106 virtual uint32_t _format_version() const = 0;
107
108 // These methods will always be called with a valid pointer.
109 virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);
110 virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);
111 virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);
112 virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);
113 virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);
114 virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);
115 virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);
116 virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);
117 virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);
118
119 // These methods will be called with a nullptr to retrieve the size of the data.
120 virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;
121 virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;
122 virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
123 virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
124 virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
125 virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;
126
127 template <class T>
128 struct ReflectSymbol {
129 static constexpr uint32_t STAGE_INDEX[RDC::SHADER_STAGE_MAX] = {
130 0, // SHADER_STAGE_VERTEX
131 1, // SHADER_STAGE_FRAGMENT
132 0, // SHADER_STAGE_TESSELATION_CONTROL
133 1, // SHADER_STAGE_TESSELATION_EVALUATION
134 0, // SHADER_STAGE_COMPUTE
135 };
136
137 BitField<RDC::ShaderStage> stages = {};
138
139 private:
140 const T *_spv_reflect[2] = { nullptr };
141
142 public:
143 _FORCE_INLINE_ constexpr uint32_t get_index_for_stage(RDC::ShaderStage p_stage) const {
144 DEV_ASSERT(stages.has_flag((1 << p_stage)));
145 return STAGE_INDEX[p_stage];
146 }
147
148 const T &get_spv_reflect(RDC::ShaderStage p_stage) const;
149
150 /*! Returns the first valid stage if multiple stages are set.
151 *
152 * Crashes if no stages are set.
153 */
154 const T &get_spv_reflect() const {
155 for (const T *d : _spv_reflect) {
156 if (d != nullptr) {
157 return *d;
158 }
159 }
160 CRASH_NOW_MSG("No stages set in ReflectSymbol");
161 }
162 void set_spv_reflect(RDC::ShaderStage p_stage, const T *p_spv);
163 };
164
165 struct ReflectImageTraits {
166 RDC::DataFormat format = RDC::DATA_FORMAT_MAX;
167 };
168
169 struct ReflectUniform : ReflectSymbol<SpvReflectDescriptorBinding> {
170 RDC::UniformType type = RDC::UniformType::UNIFORM_TYPE_MAX;
171 uint32_t binding = 0;
172
173 ReflectImageTraits image;
174
175 uint32_t length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).
176 bool writable = false;
177
178 bool operator<(const ReflectUniform &p_other) const {
179 if (binding != p_other.binding) {
180 return binding < p_other.binding;
181 }
182 if (type != p_other.type) {
183 return type < p_other.type;
184 }
185 if (writable != p_other.writable) {
186 return writable < p_other.writable;
187 }
188 if (stages != p_other.stages) {
189 return stages < p_other.stages;
190 }
191 if (length != p_other.length) {
192 return length < p_other.length;
193 }
194 return false;
195 }
196 };
197
198 struct ReflectSpecializationConstant : ReflectSymbol<SpvReflectSpecializationConstant> {
199 RDC::PipelineSpecializationConstantType type = {};
200 uint32_t constant_id = 0xffffffff;
201 union {
202 uint32_t int_value = 0;
203 float float_value;
204 bool bool_value;
205 };
206
207 bool operator<(const ReflectSpecializationConstant &p_other) const { return constant_id < p_other.constant_id; }
208 };
209
210 class ReflectShaderStage {
211 friend class RenderingShaderContainer;
212
213 Vector<uint8_t> _spirv_data;
214 SpvReflectShaderModule *_module = nullptr;
215
216 public:
217 RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;
218 const SpvReflectShaderModule &module() const;
219 const Span<uint32_t> spirv() const;
220 const Vector<uint8_t> spirv_data() const { return _spirv_data; }
221
222 ReflectShaderStage();
223 ~ReflectShaderStage();
224 };
225
226 typedef LocalVector<ReflectUniform> ReflectDescriptorSet;
227
228 struct ReflectShader {
229 uint64_t vertex_input_mask = 0;
230 uint32_t fragment_output_mask = 0;
231 uint32_t compute_local_size[3] = {};
232 uint32_t push_constant_size = 0;
233 bool has_multiview = false;
234 bool has_dynamic_buffers = false;
235
236 LocalVector<ReflectShaderStage> shader_stages;
237 LocalVector<ReflectDescriptorSet> uniform_sets;
238 LocalVector<ReflectSymbol<SpvReflectDescriptorBinding>> reflect_uniforms;
239 LocalVector<ReflectSpecializationConstant> specialization_constants;
240 LocalVector<ReflectSymbol<SpvReflectSpecializationConstant>> reflect_specialization_constants;
241 LocalVector<RDC::ShaderStage> stages_vector;
242 BitField<RDC::ShaderStage> stages_bits = {};
243 BitField<RDC::ShaderStage> push_constant_stages = {};
244
245 _FORCE_INLINE_ bool is_compute() const {
246 return stages_bits.has_flag(RDC::SHADER_STAGE_COMPUTE_BIT);
247 }
248
249 /*! Returns the uniform at the specified global index.
250 *
251 * This is a flattened view of all uniform sets.
252 */
253 ReflectUniform &uniform_at(uint32_t p_index) {
254 for (LocalVector<ReflectUniform> &set : uniform_sets) {
255 if (p_index < set.size()) {
256 return set[p_index];
257 }
258 p_index -= set.size();
259 }
260 CRASH_NOW_MSG(vformat("Uniform index %d out of range (total %d)", p_index, uniform_count()));
261 }
262
263 uint32_t uniform_count() const {
264 uint32_t count = 0;
265 for (const LocalVector<ReflectUniform> &set : uniform_sets) {
266 count += set.size();
267 }
268 return count;
269 }
270 };
271
272 // This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.
273 virtual void _set_from_shader_reflection_post(const ReflectShader &p_shader);
274
275 // This method will be called when set_code_from_spirv() is called.
276 virtual bool _set_code_from_spirv(const ReflectShader &p_shader) = 0;
277
278 void set_from_shader_reflection(const ReflectShader &p_reflection);
279 Error reflect_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv, ReflectShader &r_shader);
280
281 public:
282 enum CompressionFlags {
283 COMPRESSION_FLAG_ZSTD = 0x1,
284 };
285
286 struct Shader {
287 RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;
288 PackedByteArray code_compressed_bytes;
289 uint32_t code_compression_flags = 0;
290 uint32_t code_decompressed_size = 0;
291 };
292
293 CharString shader_name;
294 Vector<Shader> shaders;
295
296 bool set_code_from_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv);
297 RDC::ShaderReflection get_shader_reflection() const;
298 bool from_bytes(const PackedByteArray &p_bytes);
299 PackedByteArray to_bytes() const;
300 bool compress_code(const uint8_t *p_decompressed_bytes, uint32_t p_decompressed_size, uint8_t *p_compressed_bytes, uint32_t *r_compressed_size, uint32_t *r_compressed_flags) const;
301 bool decompress_code(const uint8_t *p_compressed_bytes, uint32_t p_compressed_size, uint32_t p_compressed_flags, uint8_t *p_decompressed_bytes, uint32_t p_decompressed_size) const;
302 RenderingShaderContainer();
303 virtual ~RenderingShaderContainer();
304 };
305
306 class RenderingShaderContainerFormat : public RenderingDeviceCommons {
307 GDSOFTCLASS(RenderingShaderContainerFormat, RenderingDeviceCommons);
308
309 public:
310 virtual Ref<RenderingShaderContainer> create_container() const = 0;
311 virtual ShaderLanguageVersion get_shader_language_version() const = 0;
312 virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;
313 };