rendering_shader_container.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. static inline uint32_t aligned_to(uint32_t p_size, uint32_t p_alignment) {
  33. if (p_size % p_alignment) {
  34. return p_size + (p_alignment - (p_size % p_alignment));
  35. } else {
  36. return p_size;
  37. }
  38. }
  39. uint32_t RenderingShaderContainer::_from_bytes_header_extra_data(const uint8_t *p_bytes) {
  40. return 0;
  41. }
  42. uint32_t RenderingShaderContainer::_from_bytes_reflection_extra_data(const uint8_t *p_bytes) {
  43. return 0;
  44. }
  45. uint32_t RenderingShaderContainer::_from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) {
  46. return 0;
  47. }
  48. uint32_t RenderingShaderContainer::_from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
  49. return 0;
  50. }
  51. uint32_t RenderingShaderContainer::_from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) {
  52. return 0;
  53. }
  54. uint32_t RenderingShaderContainer::_from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
  55. return 0;
  56. }
  57. uint32_t RenderingShaderContainer::_from_bytes_shader_extra_data_start(const uint8_t *p_bytes) {
  58. return 0;
  59. }
  60. uint32_t RenderingShaderContainer::_from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
  61. return 0;
  62. }
  63. uint32_t RenderingShaderContainer::_from_bytes_footer_extra_data(const uint8_t *p_bytes) {
  64. return 0;
  65. }
  66. uint32_t RenderingShaderContainer::_to_bytes_header_extra_data(uint8_t *) const {
  67. return 0;
  68. }
  69. uint32_t RenderingShaderContainer::_to_bytes_reflection_extra_data(uint8_t *) const {
  70. return 0;
  71. }
  72. uint32_t RenderingShaderContainer::_to_bytes_reflection_binding_uniform_extra_data(uint8_t *, uint32_t) const {
  73. return 0;
  74. }
  75. uint32_t RenderingShaderContainer::_to_bytes_reflection_specialization_extra_data(uint8_t *, uint32_t) const {
  76. return 0;
  77. }
  78. uint32_t RenderingShaderContainer::_to_bytes_shader_extra_data(uint8_t *, uint32_t) const {
  79. return 0;
  80. }
  81. uint32_t RenderingShaderContainer::_to_bytes_footer_extra_data(uint8_t *) const {
  82. return 0;
  83. }
  84. void RenderingShaderContainer::_set_from_shader_reflection_post(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection) {
  85. // Do nothing.
  86. }
  87. void RenderingShaderContainer::set_from_shader_reflection(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection) {
  88. reflection_binding_set_uniforms_count.clear();
  89. reflection_binding_set_uniforms_data.clear();
  90. reflection_specialization_data.clear();
  91. reflection_shader_stages.clear();
  92. shader_name = p_shader_name.utf8();
  93. reflection_data.vertex_input_mask = p_reflection.vertex_input_mask;
  94. reflection_data.fragment_output_mask = p_reflection.fragment_output_mask;
  95. reflection_data.specialization_constants_count = p_reflection.specialization_constants.size();
  96. reflection_data.is_compute = p_reflection.is_compute;
  97. reflection_data.has_multiview = p_reflection.has_multiview;
  98. reflection_data.compute_local_size[0] = p_reflection.compute_local_size[0];
  99. reflection_data.compute_local_size[1] = p_reflection.compute_local_size[1];
  100. reflection_data.compute_local_size[2] = p_reflection.compute_local_size[2];
  101. reflection_data.set_count = p_reflection.uniform_sets.size();
  102. reflection_data.push_constant_size = p_reflection.push_constant_size;
  103. reflection_data.push_constant_stages_mask = uint32_t(p_reflection.push_constant_stages);
  104. reflection_data.shader_name_len = shader_name.length();
  105. ReflectionBindingData binding_data;
  106. for (const Vector<RenderingDeviceCommons::ShaderUniform> &uniform_set : p_reflection.uniform_sets) {
  107. for (const RenderingDeviceCommons::ShaderUniform &uniform : uniform_set) {
  108. binding_data.type = uint32_t(uniform.type);
  109. binding_data.binding = uniform.binding;
  110. binding_data.stages = uint32_t(uniform.stages);
  111. binding_data.length = uniform.length;
  112. binding_data.writable = uint32_t(uniform.writable);
  113. reflection_binding_set_uniforms_data.push_back(binding_data);
  114. }
  115. reflection_binding_set_uniforms_count.push_back(uniform_set.size());
  116. }
  117. ReflectionSpecializationData specialization_data;
  118. for (const RenderingDeviceCommons::ShaderSpecializationConstant &spec : p_reflection.specialization_constants) {
  119. specialization_data.type = uint32_t(spec.type);
  120. specialization_data.constant_id = spec.constant_id;
  121. specialization_data.int_value = spec.int_value;
  122. specialization_data.stage_flags = uint32_t(spec.stages);
  123. reflection_specialization_data.push_back(specialization_data);
  124. }
  125. for (uint32_t i = 0; i < RenderingDeviceCommons::SHADER_STAGE_MAX; i++) {
  126. if (p_reflection.stages_bits.has_flag(RenderingDeviceCommons::ShaderStage(1U << i))) {
  127. reflection_shader_stages.push_back(RenderingDeviceCommons::ShaderStage(i));
  128. }
  129. }
  130. reflection_data.stage_count = reflection_shader_stages.size();
  131. _set_from_shader_reflection_post(p_shader_name, p_reflection);
  132. }
  133. bool RenderingShaderContainer::set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) {
  134. return _set_code_from_spirv(p_spirv);
  135. }
  136. RenderingDeviceCommons::ShaderReflection RenderingShaderContainer::get_shader_reflection() const {
  137. RenderingDeviceCommons::ShaderReflection shader_refl;
  138. shader_refl.push_constant_size = reflection_data.push_constant_size;
  139. shader_refl.push_constant_stages = reflection_data.push_constant_stages_mask;
  140. shader_refl.vertex_input_mask = reflection_data.vertex_input_mask;
  141. shader_refl.fragment_output_mask = reflection_data.fragment_output_mask;
  142. shader_refl.is_compute = reflection_data.is_compute;
  143. shader_refl.has_multiview = reflection_data.has_multiview;
  144. shader_refl.compute_local_size[0] = reflection_data.compute_local_size[0];
  145. shader_refl.compute_local_size[1] = reflection_data.compute_local_size[1];
  146. shader_refl.compute_local_size[2] = reflection_data.compute_local_size[2];
  147. shader_refl.uniform_sets.resize(reflection_data.set_count);
  148. shader_refl.specialization_constants.resize(reflection_data.specialization_constants_count);
  149. shader_refl.stages_vector.resize(reflection_data.stage_count);
  150. 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.");
  151. uint32_t uniform_index = 0;
  152. for (uint32_t i = 0; i < reflection_data.set_count; i++) {
  153. Vector<RenderingDeviceCommons::ShaderUniform> &uniform_set = shader_refl.uniform_sets.ptrw()[i];
  154. uint32_t uniforms_count = reflection_binding_set_uniforms_count[i];
  155. uniform_set.resize(uniforms_count);
  156. for (uint32_t j = 0; j < uniforms_count; j++) {
  157. const ReflectionBindingData &binding = reflection_binding_set_uniforms_data[uniform_index++];
  158. RenderingDeviceCommons::ShaderUniform &uniform = uniform_set.ptrw()[j];
  159. uniform.type = RenderingDeviceCommons::UniformType(binding.type);
  160. uniform.writable = binding.writable;
  161. uniform.length = binding.length;
  162. uniform.binding = binding.binding;
  163. uniform.stages = binding.stages;
  164. }
  165. }
  166. shader_refl.specialization_constants.resize(reflection_data.specialization_constants_count);
  167. for (uint32_t i = 0; i < reflection_data.specialization_constants_count; i++) {
  168. const ReflectionSpecializationData &spec = reflection_specialization_data[i];
  169. RenderingDeviceCommons::ShaderSpecializationConstant &sc = shader_refl.specialization_constants.ptrw()[i];
  170. sc.type = RenderingDeviceCommons::PipelineSpecializationConstantType(spec.type);
  171. sc.constant_id = spec.constant_id;
  172. sc.int_value = spec.int_value;
  173. sc.stages = spec.stage_flags;
  174. }
  175. shader_refl.stages_vector.resize(reflection_data.stage_count);
  176. for (uint32_t i = 0; i < reflection_data.stage_count; i++) {
  177. shader_refl.stages_vector.set(i, reflection_shader_stages[i]);
  178. shader_refl.stages_bits.set_flag(RenderingDeviceCommons::ShaderStage(1U << reflection_shader_stages[i]));
  179. }
  180. return shader_refl;
  181. }
  182. bool RenderingShaderContainer::from_bytes(const PackedByteArray &p_bytes) {
  183. const uint64_t alignment = sizeof(uint32_t);
  184. const uint8_t *bytes_ptr = p_bytes.ptr();
  185. uint64_t bytes_offset = 0;
  186. // Read container header.
  187. 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.");
  188. const ContainerHeader &container_header = *(const ContainerHeader *)(&bytes_ptr[bytes_offset]);
  189. bytes_offset += sizeof(ContainerHeader);
  190. bytes_offset += _from_bytes_header_extra_data(&bytes_ptr[bytes_offset]);
  191. ERR_FAIL_COND_V_MSG(container_header.magic_number != CONTAINER_MAGIC_NUMBER, false, "Incorrect magic number in shader container.");
  192. ERR_FAIL_COND_V_MSG(container_header.version > CONTAINER_VERSION, false, "Unsupported version in shader container.");
  193. ERR_FAIL_COND_V_MSG(container_header.format != _format(), false, "Incorrect format in shader container.");
  194. ERR_FAIL_COND_V_MSG(container_header.format_version > _format_version(), false, "Unsupported format version in shader container.");
  195. // Adjust shaders to the size indicated by the container header.
  196. shaders.resize(container_header.shader_count);
  197. // Read reflection data.
  198. ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ReflectionData)) > p_bytes.size(), false, "Not enough bytes for reflection data in shader container.");
  199. reflection_data = *(const ReflectionData *)(&bytes_ptr[bytes_offset]);
  200. bytes_offset += sizeof(ReflectionData);
  201. bytes_offset += _from_bytes_reflection_extra_data(&bytes_ptr[bytes_offset]);
  202. // Read shader name.
  203. 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.");
  204. if (reflection_data.shader_name_len > 0) {
  205. String shader_name_str;
  206. shader_name_str.append_utf8((const char *)(&bytes_ptr[bytes_offset]), reflection_data.shader_name_len);
  207. shader_name = shader_name_str.utf8();
  208. bytes_offset = aligned_to(bytes_offset + reflection_data.shader_name_len, alignment);
  209. } else {
  210. shader_name = CharString();
  211. }
  212. reflection_binding_set_uniforms_count.resize(reflection_data.set_count);
  213. reflection_binding_set_uniforms_data.clear();
  214. uint32_t uniform_index = 0;
  215. for (uint32_t i = 0; i < reflection_data.set_count; i++) {
  216. 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.");
  217. uint32_t uniforms_count = *(uint32_t *)(&bytes_ptr[bytes_offset]);
  218. reflection_binding_set_uniforms_count.ptrw()[i] = uniforms_count;
  219. bytes_offset += sizeof(uint32_t);
  220. reflection_binding_set_uniforms_data.resize(reflection_binding_set_uniforms_data.size() + uniforms_count);
  221. bytes_offset += _from_bytes_reflection_binding_uniform_extra_data_start(&bytes_ptr[bytes_offset]);
  222. for (uint32_t j = 0; j < uniforms_count; j++) {
  223. ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ReflectionBindingData)) > p_bytes.size(), false, "Not enough bytes for uniform in shader container.");
  224. memcpy(&reflection_binding_set_uniforms_data.ptrw()[uniform_index], &bytes_ptr[bytes_offset], sizeof(ReflectionBindingData));
  225. bytes_offset += sizeof(ReflectionBindingData);
  226. bytes_offset += _from_bytes_reflection_binding_uniform_extra_data(&bytes_ptr[bytes_offset], uniform_index);
  227. uniform_index++;
  228. }
  229. }
  230. reflection_specialization_data.resize(reflection_data.specialization_constants_count);
  231. bytes_offset += _from_bytes_reflection_specialization_extra_data_start(&bytes_ptr[bytes_offset]);
  232. for (uint32_t i = 0; i < reflection_data.specialization_constants_count; i++) {
  233. ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ReflectionSpecializationData)) > p_bytes.size(), false, "Not enough bytes for specialization in shader container.");
  234. memcpy(&reflection_specialization_data.ptrw()[i], &bytes_ptr[bytes_offset], sizeof(ReflectionSpecializationData));
  235. bytes_offset += sizeof(ReflectionSpecializationData);
  236. bytes_offset += _from_bytes_reflection_specialization_extra_data(&bytes_ptr[bytes_offset], i);
  237. }
  238. const uint32_t stage_count = reflection_data.stage_count;
  239. if (stage_count > 0) {
  240. ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + stage_count * sizeof(RenderingDeviceCommons::ShaderStage)) > p_bytes.size(), false, "Not enough bytes for stages in shader container.");
  241. reflection_shader_stages.resize(stage_count);
  242. bytes_offset += _from_bytes_shader_extra_data_start(&bytes_ptr[bytes_offset]);
  243. memcpy(reflection_shader_stages.ptrw(), &bytes_ptr[bytes_offset], stage_count * sizeof(RenderingDeviceCommons::ShaderStage));
  244. bytes_offset += stage_count * sizeof(RenderingDeviceCommons::ShaderStage);
  245. }
  246. // Read shaders.
  247. for (int64_t i = 0; i < shaders.size(); i++) {
  248. ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + sizeof(ShaderHeader)) > p_bytes.size(), false, "Not enough bytes for shader header in shader container.");
  249. const ShaderHeader &header = *(const ShaderHeader *)(&bytes_ptr[bytes_offset]);
  250. bytes_offset += sizeof(ShaderHeader);
  251. 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.");
  252. Shader &shader = shaders.ptrw()[i];
  253. shader.shader_stage = RenderingDeviceCommons::ShaderStage(header.shader_stage);
  254. shader.code_compression_flags = header.code_compression_flags;
  255. shader.code_decompressed_size = header.code_decompressed_size;
  256. shader.code_compressed_bytes.resize(header.code_compressed_size);
  257. memcpy(shader.code_compressed_bytes.ptrw(), &bytes_ptr[bytes_offset], header.code_compressed_size);
  258. bytes_offset = aligned_to(bytes_offset + header.code_compressed_size, alignment);
  259. bytes_offset += _from_bytes_shader_extra_data(&bytes_ptr[bytes_offset], i);
  260. }
  261. bytes_offset += _from_bytes_footer_extra_data(&bytes_ptr[bytes_offset]);
  262. 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.");
  263. return true;
  264. }
  265. PackedByteArray RenderingShaderContainer::to_bytes() const {
  266. // Compute the exact size the container will require for writing everything out.
  267. const uint64_t alignment = sizeof(uint32_t);
  268. uint64_t total_size = 0;
  269. total_size += sizeof(ContainerHeader) + _to_bytes_header_extra_data(nullptr);
  270. total_size += sizeof(ReflectionData) + _to_bytes_reflection_extra_data(nullptr);
  271. total_size += aligned_to(reflection_data.shader_name_len, alignment);
  272. total_size += reflection_binding_set_uniforms_count.size() * sizeof(uint32_t);
  273. total_size += reflection_binding_set_uniforms_data.size() * sizeof(ReflectionBindingData);
  274. total_size += reflection_specialization_data.size() * sizeof(ReflectionSpecializationData);
  275. total_size += reflection_shader_stages.size() * sizeof(RenderingDeviceCommons::ShaderStage);
  276. for (uint32_t i = 0; i < reflection_binding_set_uniforms_data.size(); i++) {
  277. total_size += _to_bytes_reflection_binding_uniform_extra_data(nullptr, i);
  278. }
  279. for (uint32_t i = 0; i < reflection_specialization_data.size(); i++) {
  280. total_size += _to_bytes_reflection_specialization_extra_data(nullptr, i);
  281. }
  282. for (uint32_t i = 0; i < shaders.size(); i++) {
  283. total_size += sizeof(ShaderHeader);
  284. total_size += shaders[i].code_compressed_bytes.size();
  285. total_size = aligned_to(total_size, alignment);
  286. total_size += _to_bytes_shader_extra_data(nullptr, i);
  287. }
  288. total_size += _to_bytes_footer_extra_data(nullptr);
  289. // Create the array that will hold all of the data.
  290. PackedByteArray bytes;
  291. bytes.resize_initialized(total_size);
  292. // Write out the data to the array.
  293. uint64_t bytes_offset = 0;
  294. uint8_t *bytes_ptr = bytes.ptrw();
  295. ContainerHeader &container_header = *(ContainerHeader *)(&bytes_ptr[bytes_offset]);
  296. container_header.magic_number = CONTAINER_MAGIC_NUMBER;
  297. container_header.version = CONTAINER_VERSION;
  298. container_header.format = _format();
  299. container_header.format_version = _format_version();
  300. container_header.shader_count = shaders.size();
  301. bytes_offset += sizeof(ContainerHeader);
  302. bytes_offset += _to_bytes_header_extra_data(&bytes_ptr[bytes_offset]);
  303. memcpy(&bytes_ptr[bytes_offset], &reflection_data, sizeof(ReflectionData));
  304. bytes_offset += sizeof(ReflectionData);
  305. bytes_offset += _to_bytes_reflection_extra_data(&bytes_ptr[bytes_offset]);
  306. if (shader_name.size() > 0) {
  307. memcpy(&bytes_ptr[bytes_offset], shader_name.ptr(), reflection_data.shader_name_len);
  308. bytes_offset = aligned_to(bytes_offset + reflection_data.shader_name_len, alignment);
  309. }
  310. uint32_t uniform_index = 0;
  311. for (uint32_t uniform_count : reflection_binding_set_uniforms_count) {
  312. memcpy(&bytes_ptr[bytes_offset], &uniform_count, sizeof(uniform_count));
  313. bytes_offset += sizeof(uint32_t);
  314. for (uint32_t i = 0; i < uniform_count; i++) {
  315. memcpy(&bytes_ptr[bytes_offset], &reflection_binding_set_uniforms_data[uniform_index], sizeof(ReflectionBindingData));
  316. bytes_offset += sizeof(ReflectionBindingData);
  317. bytes_offset += _to_bytes_reflection_binding_uniform_extra_data(&bytes_ptr[bytes_offset], uniform_index);
  318. uniform_index++;
  319. }
  320. }
  321. for (uint32_t i = 0; i < reflection_specialization_data.size(); i++) {
  322. memcpy(&bytes_ptr[bytes_offset], &reflection_specialization_data.ptr()[i], sizeof(ReflectionSpecializationData));
  323. bytes_offset += sizeof(ReflectionSpecializationData);
  324. bytes_offset += _to_bytes_reflection_specialization_extra_data(&bytes_ptr[bytes_offset], i);
  325. }
  326. if (!reflection_shader_stages.is_empty()) {
  327. uint32_t stage_count = reflection_shader_stages.size();
  328. memcpy(&bytes_ptr[bytes_offset], reflection_shader_stages.ptr(), stage_count * sizeof(RenderingDeviceCommons::ShaderStage));
  329. bytes_offset += stage_count * sizeof(RenderingDeviceCommons::ShaderStage);
  330. }
  331. for (uint32_t i = 0; i < shaders.size(); i++) {
  332. const Shader &shader = shaders[i];
  333. ShaderHeader &header = *(ShaderHeader *)(&bytes.ptr()[bytes_offset]);
  334. header.shader_stage = shader.shader_stage;
  335. header.code_compressed_size = uint32_t(shader.code_compressed_bytes.size());
  336. header.code_compression_flags = shader.code_compression_flags;
  337. header.code_decompressed_size = shader.code_decompressed_size;
  338. bytes_offset += sizeof(ShaderHeader);
  339. memcpy(&bytes.ptrw()[bytes_offset], shader.code_compressed_bytes.ptr(), shader.code_compressed_bytes.size());
  340. bytes_offset = aligned_to(bytes_offset + shader.code_compressed_bytes.size(), alignment);
  341. bytes_offset += _to_bytes_shader_extra_data(&bytes_ptr[bytes_offset], i);
  342. }
  343. bytes_offset += _to_bytes_footer_extra_data(&bytes_ptr[bytes_offset]);
  344. 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.");
  345. return bytes;
  346. }
  347. 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 {
  348. DEV_ASSERT(p_decompressed_bytes != nullptr);
  349. DEV_ASSERT(p_decompressed_size > 0);
  350. DEV_ASSERT(p_compressed_bytes != nullptr);
  351. DEV_ASSERT(r_compressed_size != nullptr);
  352. DEV_ASSERT(r_compressed_flags != nullptr);
  353. *r_compressed_flags = 0;
  354. PackedByteArray zstd_bytes;
  355. const int64_t zstd_max_bytes = Compression::get_max_compressed_buffer_size(p_decompressed_size, Compression::MODE_ZSTD);
  356. zstd_bytes.resize(zstd_max_bytes);
  357. const int64_t zstd_size = Compression::compress(zstd_bytes.ptrw(), p_decompressed_bytes, p_decompressed_size, Compression::MODE_ZSTD);
  358. if (zstd_size > 0 && (uint32_t)(zstd_size) < p_decompressed_size) {
  359. // Only choose Zstd if it results in actual compression.
  360. memcpy(p_compressed_bytes, zstd_bytes.ptr(), zstd_size);
  361. *r_compressed_size = zstd_size;
  362. *r_compressed_flags |= COMPRESSION_FLAG_ZSTD;
  363. } else {
  364. // Just copy the input to the output directly.
  365. memcpy(p_compressed_bytes, p_decompressed_bytes, p_decompressed_size);
  366. *r_compressed_size = p_decompressed_size;
  367. }
  368. return true;
  369. }
  370. 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 {
  371. DEV_ASSERT(p_compressed_bytes != nullptr);
  372. DEV_ASSERT(p_compressed_size > 0);
  373. DEV_ASSERT(p_decompressed_bytes != nullptr);
  374. DEV_ASSERT(p_decompressed_size > 0);
  375. bool uses_zstd = p_compressed_flags & COMPRESSION_FLAG_ZSTD;
  376. if (uses_zstd) {
  377. if (!Compression::decompress(p_decompressed_bytes, p_decompressed_size, p_compressed_bytes, p_compressed_size, Compression::MODE_ZSTD)) {
  378. ERR_FAIL_V_MSG(false, "Malformed zstd input for decompressing shader code.");
  379. }
  380. } else {
  381. memcpy(p_decompressed_bytes, p_compressed_bytes, MIN(p_compressed_size, p_decompressed_size));
  382. }
  383. return true;
  384. }
  385. RenderingShaderContainer::RenderingShaderContainer() {}
  386. RenderingShaderContainer::~RenderingShaderContainer() {}