Debian Codegraph @

servers/rendering/rendering_shader_container.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* rendering_shader_container.cpp */
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 #include "rendering_shader_container.h"
32
33 #include "core/io/compression.h"
34
35 #include "servers/rendering/renderer_rd/shader_rd.h"
36 #include "thirdparty/spirv-reflect/spirv_reflect.h"
37
38 static inline uint32_t aligned_to(uint32_t p_size, uint32_t p_alignment) {
39 if (p_size % p_alignment) {
40 return p_size + (p_alignment - (p_size % p_alignment));
41 } else {
42 return p_size;
43 }
44 }
45
46 template <class T>
47 const T &RenderingShaderContainer::ReflectSymbol<T>::get_spv_reflect(RDC::ShaderStage p_stage) const {
48 const T *info = _spv_reflect[get_index_for_stage(p_stage)];
49 DEV_ASSERT(info != nullptr); // Caller is expected to specify valid shader stages
50 return *info;
51 }
52
53 template <class T>
54 void RenderingShaderContainer::ReflectSymbol<T>::set_spv_reflect(RDC::ShaderStage p_stage, const T *p_spv) {
55 stages.set_flag(1 << p_stage);
56 _spv_reflect[get_index_for_stage(p_stage)] = p_spv;
57 }
58
59 RenderingShaderContainer::ReflectShaderStage::ReflectShaderStage() {
60 _module = memnew(SpvReflectShaderModule);
61 memset(_module, 0, sizeof(SpvReflectShaderModule));
62 }
63
64 RenderingShaderContainer::ReflectShaderStage::~ReflectShaderStage() {
65 spvReflectDestroyShaderModule(_module);
66 memdelete(_module);
67 _module = nullptr;
68 }
69
70 const SpvReflectShaderModule &RenderingShaderContainer::ReflectShaderStage::module() const {
71 return *_module;
72 }
73
74 const Span<uint32_t> RenderingShaderContainer::ReflectShaderStage::spirv() const {
75 return _spirv_data.span().reinterpret<uint32_t>();
76 }
77
78 uint32_t RenderingShaderContainer::_from_bytes_header_extra_data(const uint8_t *p_bytes) {
79 return 0;
80 }
81
82 uint32_t RenderingShaderContainer::_from_bytes_reflection_extra_data(const uint8_t *p_bytes) {
83 return 0;
84 }
85
86 uint32_t RenderingShaderContainer::_from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) {
87 return 0;
88 }
89
90 uint32_t RenderingShaderContainer::_from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
91 return 0;
92 }
93
94 uint32_t RenderingShaderContainer::_from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) {
95 return 0;
96 }
97
98 uint32_t RenderingShaderContainer::_from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
99 return 0;
100 }
101
102 uint32_t RenderingShaderContainer::_from_bytes_shader_extra_data_start(const uint8_t *p_bytes) {
103 return 0;
104 }
105
106 uint32_t RenderingShaderContainer::_from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
107 return 0;
108 }
109
110 uint32_t RenderingShaderContainer::_from_bytes_footer_extra_data(const uint8_t *p_bytes) {
111 return 0;
112 }
113
114 uint32_t RenderingShaderContainer::_to_bytes_header_extra_data(uint8_t *) const {
115 return 0;
116 }
117
118 uint32_t RenderingShaderContainer::_to_bytes_reflection_extra_data(uint8_t *) const {
119 return 0;
120 }
121
122 uint32_t RenderingShaderContainer::_to_bytes_reflection_binding_uniform_extra_data(uint8_t *, uint32_t) const {
123 return 0;
124 }
125
126 uint32_t RenderingShaderContainer::_to_bytes_reflection_specialization_extra_data(uint8_t *, uint32_t) const {
127 return 0;
128 }
129
130 uint32_t RenderingShaderContainer::_to_bytes_shader_extra_data(uint8_t *, uint32_t) const {
131 return 0;
132 }
133
134 uint32_t RenderingShaderContainer::_to_bytes_footer_extra_data(uint8_t *) const {
135 return 0;
136 }
137
138 void RenderingShaderContainer::_set_from_shader_reflection_post(const ReflectShader &p_shader) {
139 // Do nothing.
140 }
141
142 static RenderingDeviceCommons::DataFormat spv_image_format_to_data_format(const SpvImageFormat p_format) {
143 using RDC = RenderingDeviceCommons;
144 switch (p_format) {
145 case SpvImageFormatUnknown:
146 return RDC::DATA_FORMAT_MAX;
147 case SpvImageFormatRgba32f:
148 return RDC::DATA_FORMAT_R32G32B32A32_SFLOAT;
149 case SpvImageFormatRgba16f:
150 return RDC::DATA_FORMAT_R16G16B16A16_SFLOAT;
151 case SpvImageFormatR32f:
152 return RDC::DATA_FORMAT_R32_SFLOAT;
153 case SpvImageFormatRgba8:
154 return RDC::DATA_FORMAT_R8G8B8A8_UNORM;
155 case SpvImageFormatRgba8Snorm:
156 return RDC::DATA_FORMAT_R8G8B8A8_SNORM;
157 case SpvImageFormatRg32f:
158 return RDC::DATA_FORMAT_R32G32_SFLOAT;
159 case SpvImageFormatRg16f:
160 return RDC::DATA_FORMAT_R16G16_SFLOAT;
161 case SpvImageFormatR11fG11fB10f:
162 return RDC::DATA_FORMAT_B10G11R11_UFLOAT_PACK32;
163 case SpvImageFormatR16f:
164 return RDC::DATA_FORMAT_R16_SFLOAT;
165 case SpvImageFormatRgba16:
166 return RDC::DATA_FORMAT_R16G16B16A16_UNORM;
167 case SpvImageFormatRgb10A2:
168 return RDC::DATA_FORMAT_A2B10G10R10_UNORM_PACK32;
169 case SpvImageFormatRg16:
170 return RDC::DATA_FORMAT_R16G16_UNORM;
171 case SpvImageFormatRg8:
172 return RDC::DATA_FORMAT_R8G8_UNORM;
173 case SpvImageFormatR16:
174 return RDC::DATA_FORMAT_R16_UNORM;
175 case SpvImageFormatR8:
176 return RDC::DATA_FORMAT_R8_UNORM;
177 case SpvImageFormatRgba16Snorm:
178 return RDC::DATA_FORMAT_R16G16B16A16_SNORM;
179 case SpvImageFormatRg16Snorm:
180 return RDC::DATA_FORMAT_R16G16_SNORM;
181 case SpvImageFormatRg8Snorm:
182 return RDC::DATA_FORMAT_R8G8_SNORM;
183 case SpvImageFormatR16Snorm:
184 return RDC::DATA_FORMAT_R16_SNORM;
185 case SpvImageFormatR8Snorm:
186 return RDC::DATA_FORMAT_R8_SNORM;
187 case SpvImageFormatRgba32i:
188 return RDC::DATA_FORMAT_R32G32B32A32_SINT;
189 case SpvImageFormatRgba16i:
190 return RDC::DATA_FORMAT_R16G16B16A16_SINT;
191 case SpvImageFormatRgba8i:
192 return RDC::DATA_FORMAT_R8G8B8A8_SINT;
193 case SpvImageFormatR32i:
194 return RDC::DATA_FORMAT_R32_SINT;
195 case SpvImageFormatRg32i:
196 return RDC::DATA_FORMAT_R32G32_SINT;
197 case SpvImageFormatRg16i:
198 return RDC::DATA_FORMAT_R16G16_SINT;
199 case SpvImageFormatRg8i:
200 return RDC::DATA_FORMAT_R8G8_SINT;
201 case SpvImageFormatR16i:
202 return RDC::DATA_FORMAT_R16_SINT;
203 case SpvImageFormatR8i:
204 return RDC::DATA_FORMAT_R8_SINT;
205 case SpvImageFormatRgba32ui:
206 return RDC::DATA_FORMAT_R32G32B32A32_UINT;
207 case SpvImageFormatRgba16ui:
208 return RDC::DATA_FORMAT_R16G16B16A16_UINT;
209 case SpvImageFormatRgba8ui:
210 return RDC::DATA_FORMAT_R8G8B8A8_UINT;
211 case SpvImageFormatR32ui:
212 return RDC::DATA_FORMAT_R32_UINT;
213 case SpvImageFormatRgb10a2ui:
214 return RDC::DATA_FORMAT_A2B10G10R10_UINT_PACK32;
215 case SpvImageFormatRg32ui:
216 return RDC::DATA_FORMAT_R32G32_UINT;
217 case SpvImageFormatRg16ui:
218 return RDC::DATA_FORMAT_R16G16_UINT;
219 case SpvImageFormatRg8ui:
220 return RDC::DATA_FORMAT_R8G8_UINT;
221 case SpvImageFormatR16ui:
222 return RDC::DATA_FORMAT_R16_UINT;
223 case SpvImageFormatR8ui:
224 return RDC::DATA_FORMAT_R8_UINT;
225 case SpvImageFormatR64ui:
226 return RDC::DATA_FORMAT_R64_UINT;
227 case SpvImageFormatR64i:
228 return RDC::DATA_FORMAT_R64_SINT;
229 case SpvImageFormatMax:
230 return RDC::DATA_FORMAT_MAX;
231 }
232 return RDC::DATA_FORMAT_MAX;
233 }
234
235 Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv, ReflectShader &r_shader) {
236 ReflectShader &reflection = r_shader;
237
238 shader_name = p_shader_name.utf8();
239
240 const uint32_t spirv_size = p_spirv.size() + 0;
241
242 LocalVector<ReflectShaderStage> &r_refl = r_shader.shader_stages;
243 r_refl.resize(spirv_size);
244
245 for (uint32_t i = 0; i < spirv_size; i++) {
246 RDC::ShaderStage stage = p_spirv[i].shader_stage;
247 RDC::ShaderStage stage_flag = (RDC::ShaderStage)(1 << stage);
248 r_refl[i].shader_stage = stage;
249 r_refl[i]._spirv_data = p_spirv[i].spirv;
250
251 const Vector<uint64_t> &dynamic_buffers = p_spirv[i].dynamic_buffers;
252
253 if (stage == RDC::SHADER_STAGE_COMPUTE) {
254 ERR_FAIL_COND_V_MSG(spirv_size != 1, FAILED,
255 "Compute shaders can only receive one stage, dedicated to compute.");
256 }
257 ERR_FAIL_COND_V_MSG(reflection.stages_bits.has_flag(stage_flag), FAILED,
258 "Stage " + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + " submitted more than once.");
259 reflection.stages_bits.set_flag(stage_flag);
260
261 {
262 SpvReflectShaderModule &module = *r_refl.ptr()[i]._module;
263 const uint8_t *spirv = p_spirv[i].spirv.ptr();
264 SpvReflectResult result = spvReflectCreateShaderModule2(SPV_REFLECT_MODULE_FLAG_NO_COPY, p_spirv[i].spirv.size(), spirv, &module);
265 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
266 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed parsing shader.");
267
268 for (uint32_t j = 0; j < module.capability_count; j++) {
269 if (module.capabilities[j].value == SpvCapabilityMultiView) {
270 reflection.has_multiview = true;
271 break;
272 }
273 }
274
275 if (reflection.is_compute()) {
276 reflection.compute_local_size[0] = module.entry_points->local_size.x;
277 reflection.compute_local_size[1] = module.entry_points->local_size.y;
278 reflection.compute_local_size[2] = module.entry_points->local_size.z;
279 }
280 uint32_t binding_count = 0;
281 result = spvReflectEnumerateDescriptorBindings(&module, &binding_count, nullptr);
282 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
283 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating descriptor bindings.");
284
285 if (binding_count > 0) {
286 // Parse bindings.
287
288 Vector<SpvReflectDescriptorBinding *> bindings;
289 bindings.resize(binding_count);
290 result = spvReflectEnumerateDescriptorBindings(&module, &binding_count, bindings.ptrw());
291
292 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
293 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed getting descriptor bindings.");
294
295 for (uint32_t j = 0; j < binding_count; j++) {
296 const SpvReflectDescriptorBinding &binding = *bindings[j];
297
298 ReflectUniform uniform;
299 uniform.set_spv_reflect(stage, &binding);
300
301 bool need_array_dimensions = false;
302 bool need_block_size = false;
303 bool may_be_writable = false;
304 bool is_image = false;
305
306 switch (binding.descriptor_type) {
307 case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLER: {
308 uniform.type = RDC::UNIFORM_TYPE_SAMPLER;
309 need_array_dimensions = true;
310 } break;
311 case SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
312 uniform.type = RDC::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
313 need_array_dimensions = true;
314 is_image = true;
315 } break;
316 case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
317 uniform.type = RDC::UNIFORM_TYPE_TEXTURE;
318 need_array_dimensions = true;
319 is_image = true;
320 } break;
321 case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
322 uniform.type = RDC::UNIFORM_TYPE_IMAGE;
323 need_array_dimensions = true;
324 may_be_writable = true;
325 is_image = true;
326 } break;
327 case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: {
328 uniform.type = RDC::UNIFORM_TYPE_TEXTURE_BUFFER;
329 need_array_dimensions = true;
330 is_image = true;
331 } break;
332 case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
333 uniform.type = RDC::UNIFORM_TYPE_IMAGE_BUFFER;
334 need_array_dimensions = true;
335 may_be_writable = true;
336 is_image = true;
337 } break;
338 case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER: {
339 const uint64_t key = ShaderRD::DynamicBuffer::encode(binding.set, binding.binding);
340 if (dynamic_buffers.has(key)) {
341 uniform.type = RDC::UNIFORM_TYPE_UNIFORM_BUFFER_DYNAMIC;
342 reflection.has_dynamic_buffers = true;
343 } else {
344 uniform.type = RDC::UNIFORM_TYPE_UNIFORM_BUFFER;
345 }
346 need_block_size = true;
347 } break;
348 case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER: {
349 const uint64_t key = ShaderRD::DynamicBuffer::encode(binding.set, binding.binding);
350 if (dynamic_buffers.has(key)) {
351 uniform.type = RDC::UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC;
352 reflection.has_dynamic_buffers = true;
353 } else {
354 uniform.type = RDC::UNIFORM_TYPE_STORAGE_BUFFER;
355 }
356 need_block_size = true;
357 may_be_writable = true;
358 } break;
359 case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
360 ERR_PRINT("Dynamic uniform buffer not supported.");
361 continue;
362 } break;
363 case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
364 ERR_PRINT("Dynamic storage buffer not supported.");
365 continue;
366 } break;
367 case SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
368 uniform.type = RDC::UNIFORM_TYPE_INPUT_ATTACHMENT;
369 need_array_dimensions = true;
370 is_image = true;
371 } break;
372 case SPV_REFLECT_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR: {
373 ERR_PRINT("Acceleration structure not supported.");
374 continue;
375 } break;
376 }
377
378 if (need_array_dimensions) {
379 uniform.length = 1;
380 for (uint32_t k = 0; k < binding.array.dims_count; k++) {
381 uniform.length *= binding.array.dims[k];
382 }
383 } else if (need_block_size) {
384 uniform.length = binding.block.size;
385 } else {
386 uniform.length = 0;
387 }
388
389 if (may_be_writable) {
390 if (binding.descriptor_type == SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
391 uniform.writable = !(binding.decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE);
392 } else {
393 uniform.writable = !(binding.decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE) && !(binding.block.decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE);
394 }
395 } else {
396 uniform.writable = false;
397 }
398
399 if (is_image) {
400 uniform.image.format = spv_image_format_to_data_format(binding.image.image_format);
401 }
402
403 uniform.binding = binding.binding;
404 uint32_t set = binding.set;
405
406 ERR_FAIL_COND_V_MSG(set >= RDC::MAX_UNIFORM_SETS, FAILED,
407 "On shader stage '" + String(RDC::SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' uses a set (" + itos(set) + ") index larger than what is supported (" + itos(RDC::MAX_UNIFORM_SETS) + ").");
408
409 if (set < (uint32_t)reflection.uniform_sets.size()) {
410 // Check if this already exists.
411 bool exists = false;
412 for (uint32_t k = 0; k < reflection.uniform_sets[set].size(); k++) {
413 if (reflection.uniform_sets[set][k].binding == uniform.binding) {
414 // Already exists, verify that it's the same type.
415 ERR_FAIL_COND_V_MSG(reflection.uniform_sets[set][k].type != uniform.type, FAILED,
416 "On shader stage '" + String(RDC::SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' trying to reuse location for set=" + itos(set) + ", binding=" + itos(uniform.binding) + " with different uniform type.");
417
418 // Also, verify that it's the same size.
419 ERR_FAIL_COND_V_MSG(reflection.uniform_sets[set][k].length != uniform.length, FAILED,
420 "On shader stage '" + String(RDC::SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' trying to reuse location for set=" + itos(set) + ", binding=" + itos(uniform.binding) + " with different uniform size.");
421
422 // Also, verify that it has the same writability.
423 ERR_FAIL_COND_V_MSG(reflection.uniform_sets[set][k].writable != uniform.writable, FAILED,
424 "On shader stage '" + String(RDC::SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' trying to reuse location for set=" + itos(set) + ", binding=" + itos(uniform.binding) + " with different writability.");
425
426 // Just append stage mask and return.
427 reflection.uniform_sets[set][k].stages.set_flag(stage_flag);
428 exists = true;
429 break;
430 }
431 }
432
433 if (exists) {
434 continue; // Merged.
435 }
436 }
437
438 uniform.stages.set_flag(stage_flag);
439
440 if (set >= (uint32_t)reflection.uniform_sets.size()) {
441 reflection.uniform_sets.resize(set + 1);
442 }
443
444 reflection.uniform_sets[set].push_back(uniform);
445 }
446 }
447
448 {
449 // Specialization constants.
450
451 uint32_t sc_count = 0;
452 result = spvReflectEnumerateSpecializationConstants(&module, &sc_count, nullptr);
453 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
454 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating specialization constants.");
455
456 if (sc_count) {
457 Vector<SpvReflectSpecializationConstant *> spec_constants;
458 spec_constants.resize(sc_count);
459
460 result = spvReflectEnumerateSpecializationConstants(&module, &sc_count, spec_constants.ptrw());
461 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
462 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining specialization constants.");
463
464 for (uint32_t j = 0; j < sc_count; j++) {
465 int32_t existing = -1;
466 ReflectSpecializationConstant sconst;
467 SpvReflectSpecializationConstant *spc = spec_constants[j];
468 sconst.set_spv_reflect(stage, spc);
469
470 sconst.constant_id = spc->constant_id;
471 sconst.int_value = 0; // Clear previous value JIC.
472 switch (spc->constant_type) {
473 case SPV_REFLECT_SPECIALIZATION_CONSTANT_BOOL: {
474 sconst.type = RDC::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
475 sconst.bool_value = spc->default_value.int_bool_value != 0;
476 } break;
477 case SPV_REFLECT_SPECIALIZATION_CONSTANT_INT: {
478 sconst.type = RDC::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT;
479 sconst.int_value = spc->default_value.int_bool_value;
480 } break;
481 case SPV_REFLECT_SPECIALIZATION_CONSTANT_FLOAT: {
482 sconst.type = RDC::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT;
483 sconst.float_value = spc->default_value.float_value;
484 } break;
485 }
486 sconst.stages.set_flag(stage_flag);
487
488 for (uint32_t k = 0; k < reflection.specialization_constants.size(); k++) {
489 if (reflection.specialization_constants[k].constant_id == sconst.constant_id) {
490 ERR_FAIL_COND_V_MSG(reflection.specialization_constants[k].type != sconst.type, FAILED, "More than one specialization constant used for id (" + itos(sconst.constant_id) + "), but their types differ.");
491 ERR_FAIL_COND_V_MSG(reflection.specialization_constants[k].int_value != sconst.int_value, FAILED, "More than one specialization constant used for id (" + itos(sconst.constant_id) + "), but their default values differ.");
492 existing = k;
493 break;
494 }
495 }
496
497 if (existing >= 0) {
498 reflection.specialization_constants[existing].stages.set_flag(stage_flag);
499 } else {
500 reflection.specialization_constants.push_back(sconst);
501 }
502 }
503
504 reflection.specialization_constants.sort();
505 }
506 }
507
508 if (stage == RDC::SHADER_STAGE_VERTEX || stage == RDC::SHADER_STAGE_FRAGMENT) {
509 uint32_t iv_count = 0;
510 result = spvReflectEnumerateInputVariables(&module, &iv_count, nullptr);
511 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
512 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating input variables.");
513
514 if (iv_count) {
515 Vector<SpvReflectInterfaceVariable *> input_vars;
516 input_vars.resize(iv_count);
517
518 result = spvReflectEnumerateInputVariables(&module, &iv_count, input_vars.ptrw());
519 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
520 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining input variables.");
521
522 for (const SpvReflectInterfaceVariable *v : input_vars) {
523 if (!v) {
524 continue;
525 }
526 if (stage == RDC::SHADER_STAGE_VERTEX) {
527 if (v->decoration_flags == 0) { // Regular input.
528 reflection.vertex_input_mask |= (((uint64_t)1) << v->location);
529 }
530 }
531 if (v->built_in == SpvBuiltInViewIndex) {
532 reflection.has_multiview = true;
533 }
534 }
535 }
536 }
537
538 if (stage == RDC::SHADER_STAGE_FRAGMENT) {
539 uint32_t ov_count = 0;
540 result = spvReflectEnumerateOutputVariables(&module, &ov_count, nullptr);
541 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
542 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating output variables.");
543
544 if (ov_count) {
545 Vector<SpvReflectInterfaceVariable *> output_vars;
546 output_vars.resize(ov_count);
547
548 result = spvReflectEnumerateOutputVariables(&module, &ov_count, output_vars.ptrw());
549 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
550 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining output variables.");
551
552 for (const SpvReflectInterfaceVariable *refvar : output_vars) {
553 if (!refvar) {
554 continue;
555 }
556 if (refvar->built_in != SpvBuiltInFragDepth) {
557 reflection.fragment_output_mask |= 1 << refvar->location;
558 }
559 }
560 }
561 }
562
563 uint32_t pc_count = 0;
564 result = spvReflectEnumeratePushConstantBlocks(&module, &pc_count, nullptr);
565 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
566 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating push constants.");
567
568 if (pc_count) {
569 ERR_FAIL_COND_V_MSG(pc_count > 1, FAILED,
570 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "': Only one push constant is supported, which should be the same across shader stages.");
571
572 Vector<SpvReflectBlockVariable *> pconstants;
573 pconstants.resize(pc_count);
574 result = spvReflectEnumeratePushConstantBlocks(&module, &pc_count, pconstants.ptrw());
575 ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
576 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining push constants.");
577 #if 0
578 if (pconstants[0] == nullptr) {
579 Ref<FileAccess> f = FileAccess::open("res://popo.spv", FileAccess::WRITE);
580 f->store_buffer((const uint8_t *)&SpirV[0], SpirV.size() * sizeof(uint32_t));
581 }
582 #endif
583
584 ERR_FAIL_COND_V_MSG(reflection.push_constant_size && reflection.push_constant_size != pconstants[0]->size, FAILED,
585 "Reflection of SPIR-V shader stage '" + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "': Push constant block must be the same across shader stages.");
586
587 reflection.push_constant_size = pconstants[0]->size;
588 reflection.push_constant_stages.set_flag(stage_flag);
589
590 //print_line("Stage: " + String(RDC::SHADER_STAGE_NAMES[stage]) + " push constant of size=" + itos(push_constant.push_constant_size));
591 }
592 }
593 }
594
595 // Sort all uniform_sets by binding.
596 for (uint32_t i = 0; i < reflection.uniform_sets.size(); i++) {
597 reflection.uniform_sets[i].sort();
598 }
599
600 set_from_shader_reflection(reflection);
601
602 return OK;
603 }
604
605 void RenderingShaderContainer::set_from_shader_reflection(const ReflectShader &p_reflection) {
606 reflection_binding_set_uniforms_count.clear();
607 reflection_binding_set_uniforms_data.clear();
608 reflection_specialization_data.clear();
609 reflection_shader_stages.clear();
610
611 reflection_data.vertex_input_mask = p_reflection.vertex_input_mask;
612 reflection_data.fragment_output_mask = p_reflection.fragment_output_mask;
613 reflection_data.specialization_constants_count = p_reflection.specialization_constants.size();
614 reflection_data.is_compute = p_reflection.is_compute();
615 reflection_data.has_multiview = p_reflection.has_multiview;
616 reflection_data.has_dynamic_buffers = p_reflection.has_dynamic_buffers;
617 reflection_data.compute_local_size[0] = p_reflection.compute_local_size[0];
618 reflection_data.compute_local_size[1] = p_reflection.compute_local_size[1];
619 reflection_data.compute_local_size[2] = p_reflection.compute_local_size[2];
620 reflection_data.set_count = p_reflection.uniform_sets.size();
621 reflection_data.push_constant_size = p_reflection.push_constant_size;
622 reflection_data.push_constant_stages_mask = uint32_t(p_reflection.push_constant_stages);
623 reflection_data.shader_name_len = shader_name.length();
624
625 ReflectionBindingData binding_data;
626 for (const ReflectDescriptorSet &uniform_set : p_reflection.uniform_sets) {
627 for (const ReflectUniform &uniform : uniform_set) {
628 binding_data.type = uint32_t(uniform.type);
629 binding_data.binding = uniform.binding;
630 binding_data.stages = uint32_t(uniform.stages);
631 binding_data.length = uniform.length;
632 binding_data.writable = uint32_t(uniform.writable);
633 reflection_binding_set_uniforms_data.push_back(binding_data);
634 }
635
636 reflection_binding_set_uniforms_count.push_back(uniform_set.size());
637 }
638
639 ReflectionSpecializationData specialization_data;
640 for (const ReflectSpecializationConstant &spec : p_reflection.specialization_constants) {
641 specialization_data.type = uint32_t(spec.type);
642 specialization_data.constant_id = spec.constant_id;
643 specialization_data.int_value = spec.int_value;
644 specialization_data.stage_flags = uint32_t(spec.stages);
645 reflection_specialization_data.push_back(specialization_data);
646 }
647
648 for (uint32_t i = 0; i < RDC::SHADER_STAGE_MAX; i++) {
649 if (p_reflection.stages_bits.has_flag(RDC::ShaderStage(1U << i))) {
650 reflection_shader_stages.push_back(RDC::ShaderStage(i));
651 }
652 }
653
654 reflection_data.stage_count = reflection_shader_stages.size();
655
656 _set_from_shader_reflection_post(p_reflection);
657 }
658
659 bool RenderingShaderContainer::set_code_from_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv) {
660 ReflectShader shader;
661 ERR_FAIL_COND_V(reflect_spirv(p_shader_name, p_spirv, shader) != OK, false);
662 return _set_code_from_spirv(shader);
663 }
664
665 RenderingDeviceCommons::ShaderReflection RenderingShaderContainer::get_shader_reflection() const {
666 RDC::ShaderReflection shader_refl;
667 shader_refl.push_constant_size = reflection_data.push_constant_size;
668 shader_refl.push_constant_stages = reflection_data.push_constant_stages_mask;
669 shader_refl.vertex_input_mask = reflection_data.vertex_input_mask;
670 shader_refl.fragment_output_mask = reflection_data.fragment_output_mask;
671 shader_refl.is_compute = reflection_data.is_compute;
672 shader_refl.has_multiview = reflection_data.has_multiview;
673 shader_refl.has_dynamic_buffers = reflection_data.has_dynamic_buffers;
674 shader_refl.compute_local_size[0] = reflection_data.compute_local_size[0];
675 shader_refl.compute_local_size[1] = reflection_data.compute_local_size[1];
676 shader_refl.compute_local_size[2] = reflection_data.compute_local_size[2];
677 shader_refl.uniform_sets.resize(reflection_data.set_count);
678 shader_refl.specialization_constants.resize(reflection_data.specialization_constants_count);
679 shader_refl.stages_vector.resize(reflection_data.stage_count);
680
681 DEV_ASSERT(reflection_binding_set_uniforms_count.size() == reflection_data.set_count && "The amount of elements in the reflection and the shader container can't be different.");
682 uint32_t uniform_index = 0;
683 for (uint32_t i = 0; i < reflection_data.set_count; i++) {
684 Vector<RDC::ShaderUniform> &uniform_set = shader_refl.uniform_sets.ptrw()[i];
685 uint32_t uniforms_count = reflection_binding_set_uniforms_count[i];
686 uniform_set.resize(uniforms_count);
687 for (uint32_t j = 0; j < uniforms_count; j++) {
688 const ReflectionBindingData &binding = reflection_binding_set_uniforms_data[uniform_index++];
689 RDC::ShaderUniform &uniform = uniform_set.ptrw()[j];
690 uniform.type = RDC::UniformType(binding.type);
691 uniform.writable = binding.writable;
692 uniform.length = binding.length;
693 uniform.binding = binding.binding;
694 uniform.stages = binding.stages;
695 }
696 }
697
698 shader_refl.specialization_constants.resize(reflection_data.specialization_constants_count);
699 for (uint32_t i = 0; i < reflection_data.specialization_constants_count; i++) {
700 const ReflectionSpecializationData &spec = reflection_specialization_data[i];
701 RDC::ShaderSpecializationConstant &sc = shader_refl.specialization_constants.ptrw()[i];
702 sc.type = RDC::PipelineSpecializationConstantType(spec.type);
703 sc.constant_id = spec.constant_id;
704 sc.int_value = spec.int_value;
705 sc.stages = spec.stage_flags;
706 }
707
708 shader_refl.stages_vector.resize(reflection_data.stage_count);
709 for (uint32_t i = 0; i < reflection_data.stage_count; i++) {
710 shader_refl.stages_vector.set(i, reflection_shader_stages[i]);
711 shader_refl.stages_bits.set_flag(RDC::ShaderStage(1U << reflection_shader_stages[i]));
712 }
713
714 return shader_refl;
715 }
716
717 bool RenderingShaderContainer::from_bytes(const PackedByteArray &p_bytes) {
718 const uint64_t alignment = sizeof(uint32_t);
719 const uint8_t *bytes_ptr = p_bytes.ptr();
720 uint64_t bytes_offset = 0;
721
722 // Read container header.
723 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ContainerHeader)) > p_bytes.size(), false, "Not enough bytes for a container header in shader container.");
724 const ContainerHeader &container_header = *(const ContainerHeader *)(&bytes_ptr[bytes_offset]);
725 bytes_offset += sizeof(ContainerHeader);
726 bytes_offset += _from_bytes_header_extra_data(&bytes_ptr[bytes_offset]);
727
728 ERR_FAIL_COND_V_MSG(container_header.magic_number != CONTAINER_MAGIC_NUMBER, false, "Incorrect magic number in shader container.");
729 ERR_FAIL_COND_V_MSG(container_header.version > CONTAINER_VERSION, false, "Unsupported version in shader container.");
730 ERR_FAIL_COND_V_MSG(container_header.format != _format(), false, "Incorrect format in shader container.");
731 ERR_FAIL_COND_V_MSG(container_header.format_version > _format_version(), false, "Unsupported format version in shader container.");
732
733 // Adjust shaders to the size indicated by the container header.
734 shaders.resize(container_header.shader_count);
735
736 // Read reflection data.
737 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ReflectionData)) > p_bytes.size(), false, "Not enough bytes for reflection data in shader container.");
738 reflection_data = *(const ReflectionData *)(&bytes_ptr[bytes_offset]);
739 bytes_offset += sizeof(ReflectionData);
740 bytes_offset += _from_bytes_reflection_extra_data(&bytes_ptr[bytes_offset]);
741
742 // Read shader name.
743 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + reflection_data.shader_name_len) > p_bytes.size(), false, "Not enough bytes for shader name in shader container.");
744 if (reflection_data.shader_name_len > 0) {
745 String shader_name_str;
746 shader_name_str.append_utf8((const char *)(&bytes_ptr[bytes_offset]), reflection_data.shader_name_len);
747 shader_name = shader_name_str.utf8();
748 bytes_offset = aligned_to(bytes_offset + reflection_data.shader_name_len, alignment);
749 } else {
750 shader_name = CharString();
751 }
752
753 reflection_binding_set_uniforms_count.resize(reflection_data.set_count);
754 reflection_binding_set_uniforms_data.clear();
755
756 uint32_t uniform_index = 0;
757 for (uint32_t i = 0; i < reflection_data.set_count; i++) {
758 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(uint32_t)) > p_bytes.size(), false, "Not enough bytes for uniform set count in shader container.");
759 uint32_t uniforms_count = *(uint32_t *)(&bytes_ptr[bytes_offset]);
760 reflection_binding_set_uniforms_count.ptrw()[i] = uniforms_count;
761 bytes_offset += sizeof(uint32_t);
762
763 reflection_binding_set_uniforms_data.resize(reflection_binding_set_uniforms_data.size() + uniforms_count);
764 bytes_offset += _from_bytes_reflection_binding_uniform_extra_data_start(&bytes_ptr[bytes_offset]);
765
766 for (uint32_t j = 0; j < uniforms_count; j++) {
767 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ReflectionBindingData)) > p_bytes.size(), false, "Not enough bytes for uniform in shader container.");
768 memcpy(&reflection_binding_set_uniforms_data.ptrw()[uniform_index], &bytes_ptr[bytes_offset], sizeof(ReflectionBindingData));
769 bytes_offset += sizeof(ReflectionBindingData);
770 bytes_offset += _from_bytes_reflection_binding_uniform_extra_data(&bytes_ptr[bytes_offset], uniform_index);
771 uniform_index++;
772 }
773 }
774
775 reflection_specialization_data.resize(reflection_data.specialization_constants_count);
776 bytes_offset += _from_bytes_reflection_specialization_extra_data_start(&bytes_ptr[bytes_offset]);
777
778 for (uint32_t i = 0; i < reflection_data.specialization_constants_count; i++) {
779 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ReflectionSpecializationData)) > p_bytes.size(), false, "Not enough bytes for specialization in shader container.");
780 memcpy(&reflection_specialization_data.ptrw()[i], &bytes_ptr[bytes_offset], sizeof(ReflectionSpecializationData));
781 bytes_offset += sizeof(ReflectionSpecializationData);
782 bytes_offset += _from_bytes_reflection_specialization_extra_data(&bytes_ptr[bytes_offset], i);
783 }
784
785 const uint32_t stage_count = reflection_data.stage_count;
786 if (stage_count > 0) {
787 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + stage_count * sizeof(RDC::ShaderStage)) > p_bytes.size(), false, "Not enough bytes for stages in shader container.");
788 reflection_shader_stages.resize(stage_count);
789 bytes_offset += _from_bytes_shader_extra_data_start(&bytes_ptr[bytes_offset]);
790 memcpy(reflection_shader_stages.ptrw(), &bytes_ptr[bytes_offset], stage_count * sizeof(RDC::ShaderStage));
791 bytes_offset += stage_count * sizeof(RDC::ShaderStage);
792 }
793
794 // Read shaders.
795 for (int64_t i = 0; i < shaders.size(); i++) {
796 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ShaderHeader)) > p_bytes.size(), false, "Not enough bytes for shader header in shader container.");
797 const ShaderHeader &header = *(const ShaderHeader *)(&bytes_ptr[bytes_offset]);
798 bytes_offset += sizeof(ShaderHeader);
799
800 ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + header.code_compressed_size) > p_bytes.size(), false, "Not enough bytes for a shader in shader container.");
801 Shader &shader = shaders.ptrw()[i];
802 shader.shader_stage = RDC::ShaderStage(header.shader_stage);
803 shader.code_compression_flags = header.code_compression_flags;
804 shader.code_decompressed_size = header.code_decompressed_size;
805 shader.code_compressed_bytes.resize(header.code_compressed_size);
806 memcpy(shader.code_compressed_bytes.ptrw(), &bytes_ptr[bytes_offset], header.code_compressed_size);
807 bytes_offset = aligned_to(bytes_offset + header.code_compressed_size, alignment);
808 bytes_offset += _from_bytes_shader_extra_data(&bytes_ptr[bytes_offset], i);
809 }
810
811 bytes_offset += _from_bytes_footer_extra_data(&bytes_ptr[bytes_offset]);
812
813 ERR_FAIL_COND_V_MSG(bytes_offset != (uint64_t)p_bytes.size(), false, "Amount of bytes in the container does not match the amount of bytes read.");
814 return true;
815 }
816
817 PackedByteArray RenderingShaderContainer::to_bytes() const {
818 // Compute the exact size the container will require for writing everything out.
819 const uint64_t alignment = sizeof(uint32_t);
820 uint64_t total_size = 0;
821 total_size += sizeof(ContainerHeader) + _to_bytes_header_extra_data(nullptr);
822 total_size += sizeof(ReflectionData) + _to_bytes_reflection_extra_data(nullptr);
823 total_size += aligned_to(reflection_data.shader_name_len, alignment);
824 total_size += reflection_binding_set_uniforms_count.size() * sizeof(uint32_t);
825 total_size += reflection_binding_set_uniforms_data.size() * sizeof(ReflectionBindingData);
826 total_size += reflection_specialization_data.size() * sizeof(ReflectionSpecializationData);
827 total_size += reflection_shader_stages.size() * sizeof(RDC::ShaderStage);
828
829 for (uint32_t i = 0; i < reflection_binding_set_uniforms_data.size(); i++) {
830 total_size += _to_bytes_reflection_binding_uniform_extra_data(nullptr, i);
831 }
832
833 for (uint32_t i = 0; i < reflection_specialization_data.size(); i++) {
834 total_size += _to_bytes_reflection_specialization_extra_data(nullptr, i);
835 }
836
837 for (uint32_t i = 0; i < shaders.size(); i++) {
838 total_size += sizeof(ShaderHeader);
839 total_size += shaders[i].code_compressed_bytes.size();
840 total_size = aligned_to(total_size, alignment);
841 total_size += _to_bytes_shader_extra_data(nullptr, i);
842 }
843
844 total_size += _to_bytes_footer_extra_data(nullptr);
845
846 // Create the array that will hold all of the data.
847 PackedByteArray bytes;
848 bytes.resize_initialized(total_size);
849
850 // Write out the data to the array.
851 uint64_t bytes_offset = 0;
852 uint8_t *bytes_ptr = bytes.ptrw();
853 ContainerHeader &container_header = *(ContainerHeader *)(&bytes_ptr[bytes_offset]);
854 container_header.magic_number = CONTAINER_MAGIC_NUMBER;
855 container_header.version = CONTAINER_VERSION;
856 container_header.format = _format();
857 container_header.format_version = _format_version();
858 container_header.shader_count = shaders.size();
859 bytes_offset += sizeof(ContainerHeader);
860 bytes_offset += _to_bytes_header_extra_data(&bytes_ptr[bytes_offset]);
861
862 memcpy(&bytes_ptr[bytes_offset], &reflection_data, sizeof(ReflectionData));
863 bytes_offset += sizeof(ReflectionData);
864 bytes_offset += _to_bytes_reflection_extra_data(&bytes_ptr[bytes_offset]);
865
866 if (shader_name.size() > 0) {
867 memcpy(&bytes_ptr[bytes_offset], shader_name.ptr(), reflection_data.shader_name_len);
868 bytes_offset = aligned_to(bytes_offset + reflection_data.shader_name_len, alignment);
869 }
870
871 uint32_t uniform_index = 0;
872 for (uint32_t uniform_count : reflection_binding_set_uniforms_count) {
873 memcpy(&bytes_ptr[bytes_offset], &uniform_count, sizeof(uniform_count));
874 bytes_offset += sizeof(uint32_t);
875
876 for (uint32_t i = 0; i < uniform_count; i++) {
877 memcpy(&bytes_ptr[bytes_offset], &reflection_binding_set_uniforms_data[uniform_index], sizeof(ReflectionBindingData));
878 bytes_offset += sizeof(ReflectionBindingData);
879 bytes_offset += _to_bytes_reflection_binding_uniform_extra_data(&bytes_ptr[bytes_offset], uniform_index);
880 uniform_index++;
881 }
882 }
883
884 for (uint32_t i = 0; i < reflection_specialization_data.size(); i++) {
885 memcpy(&bytes_ptr[bytes_offset], &reflection_specialization_data.ptr()[i], sizeof(ReflectionSpecializationData));
886 bytes_offset += sizeof(ReflectionSpecializationData);
887 bytes_offset += _to_bytes_reflection_specialization_extra_data(&bytes_ptr[bytes_offset], i);
888 }
889
890 if (!reflection_shader_stages.is_empty()) {
891 uint32_t stage_count = reflection_shader_stages.size();
892 memcpy(&bytes_ptr[bytes_offset], reflection_shader_stages.ptr(), stage_count * sizeof(RDC::ShaderStage));
893 bytes_offset += stage_count * sizeof(RDC::ShaderStage);
894 }
895
896 for (uint32_t i = 0; i < shaders.size(); i++) {
897 const Shader &shader = shaders[i];
898 ShaderHeader &header = *(ShaderHeader *)(&bytes.ptr()[bytes_offset]);
899 header.shader_stage = shader.shader_stage;
900 header.code_compressed_size = uint32_t(shader.code_compressed_bytes.size());
901 header.code_compression_flags = shader.code_compression_flags;
902 header.code_decompressed_size = shader.code_decompressed_size;
903 bytes_offset += sizeof(ShaderHeader);
904 memcpy(&bytes.ptrw()[bytes_offset], shader.code_compressed_bytes.ptr(), shader.code_compressed_bytes.size());
905 bytes_offset = aligned_to(bytes_offset + shader.code_compressed_bytes.size(), alignment);
906 bytes_offset += _to_bytes_shader_extra_data(&bytes_ptr[bytes_offset], i);
907 }
908
909 bytes_offset += _to_bytes_footer_extra_data(&bytes_ptr[bytes_offset]);
910
911 ERR_FAIL_COND_V_MSG(bytes_offset != total_size, PackedByteArray(), "Amount of bytes written does not match the amount of bytes reserved for the container.");
912 return bytes;
913 }
914
915 bool RenderingShaderContainer::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 {
916 DEV_ASSERT(p_decompressed_bytes != nullptr);
917 DEV_ASSERT(p_decompressed_size > 0);
918 DEV_ASSERT(p_compressed_bytes != nullptr);
919 DEV_ASSERT(r_compressed_size != nullptr);
920 DEV_ASSERT(r_compressed_flags != nullptr);
921
922 *r_compressed_flags = 0;
923
924 PackedByteArray zstd_bytes;
925 const int64_t zstd_max_bytes = Compression::get_max_compressed_buffer_size(p_decompressed_size, Compression::MODE_ZSTD);
926 zstd_bytes.resize(zstd_max_bytes);
927
928 const int64_t zstd_size = Compression::compress(zstd_bytes.ptrw(), p_decompressed_bytes, p_decompressed_size, Compression::MODE_ZSTD);
929 if (zstd_size > 0 && (uint32_t)(zstd_size) < p_decompressed_size) {
930 // Only choose Zstd if it results in actual compression.
931 memcpy(p_compressed_bytes, zstd_bytes.ptr(), zstd_size);
932 *r_compressed_size = zstd_size;
933 *r_compressed_flags |= COMPRESSION_FLAG_ZSTD;
934 } else {
935 // Just copy the input to the output directly.
936 memcpy(p_compressed_bytes, p_decompressed_bytes, p_decompressed_size);
937 *r_compressed_size = p_decompressed_size;
938 }
939
940 return true;
941 }
942
943 bool RenderingShaderContainer::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 {
944 DEV_ASSERT(p_compressed_bytes != nullptr);
945 DEV_ASSERT(p_compressed_size > 0);
946 DEV_ASSERT(p_decompressed_bytes != nullptr);
947 DEV_ASSERT(p_decompressed_size > 0);
948
949 bool uses_zstd = p_compressed_flags & COMPRESSION_FLAG_ZSTD;
950 if (uses_zstd) {
951 if (!Compression::decompress(p_decompressed_bytes, p_decompressed_size, p_compressed_bytes, p_compressed_size, Compression::MODE_ZSTD)) {
952 ERR_FAIL_V_MSG(false, "Malformed zstd input for decompressing shader code.");
953 }
954 } else {
955 memcpy(p_decompressed_bytes, p_compressed_bytes, MIN(p_compressed_size, p_decompressed_size));
956 }
957
958 return true;
959 }
960
961 RenderingShaderContainer::RenderingShaderContainer() {}
962
963 RenderingShaderContainer::~RenderingShaderContainer() {}