rendering_shader_container.cpp 42 KB

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