image_compress_basisu.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**************************************************************************/
  2. /* image_compress_basisu.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 "image_compress_basisu.h"
  31. #include "servers/rendering_server.h"
  32. #include <transcoder/basisu_transcoder.h>
  33. #ifdef TOOLS_ENABLED
  34. #include <encoder/basisu_comp.h>
  35. #endif
  36. void basis_universal_init() {
  37. #ifdef TOOLS_ENABLED
  38. basisu::basisu_encoder_init();
  39. #endif
  40. basist::basisu_transcoder_init();
  41. }
  42. #ifdef TOOLS_ENABLED
  43. Vector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedChannels p_channels) {
  44. Ref<Image> image = p_image->duplicate();
  45. image->convert(Image::FORMAT_RGBA8);
  46. basisu::basis_compressor_params params;
  47. params.m_uastc = true;
  48. params.m_quality_level = basisu::BASISU_QUALITY_MIN;
  49. params.m_pack_uastc_flags &= ~basisu::cPackUASTCLevelMask;
  50. params.m_pack_uastc_flags |= basisu::cPackUASTCLevelFastest;
  51. params.m_rdo_uastc = 0.0f;
  52. params.m_rdo_uastc_quality_scalar = 0.0f;
  53. params.m_rdo_uastc_dict_size = 1024;
  54. params.m_mip_fast = true;
  55. params.m_multithreading = true;
  56. params.m_check_for_alpha = false;
  57. basisu::job_pool job_pool(OS::get_singleton()->get_processor_count());
  58. params.m_pJob_pool = &job_pool;
  59. BasisDecompressFormat decompress_format = BASIS_DECOMPRESS_RG;
  60. switch (p_channels) {
  61. case Image::USED_CHANNELS_L: {
  62. decompress_format = BASIS_DECOMPRESS_RGB;
  63. } break;
  64. case Image::USED_CHANNELS_LA: {
  65. params.m_force_alpha = true;
  66. decompress_format = BASIS_DECOMPRESS_RGBA;
  67. } break;
  68. case Image::USED_CHANNELS_R: {
  69. decompress_format = BASIS_DECOMPRESS_RGB;
  70. } break;
  71. case Image::USED_CHANNELS_RG: {
  72. // Currently RG textures are compressed as DXT5/ETC2_RGBA8 with a RA -> RG swizzle,
  73. // as BasisUniversal didn't use to support ETC2_RG11 transcoding.
  74. params.m_force_alpha = true;
  75. image->convert_rg_to_ra_rgba8();
  76. decompress_format = BASIS_DECOMPRESS_RG_AS_RA;
  77. } break;
  78. case Image::USED_CHANNELS_RGB: {
  79. decompress_format = BASIS_DECOMPRESS_RGB;
  80. } break;
  81. case Image::USED_CHANNELS_RGBA: {
  82. params.m_force_alpha = true;
  83. decompress_format = BASIS_DECOMPRESS_RGBA;
  84. } break;
  85. }
  86. {
  87. // Encode the image with mipmaps.
  88. Vector<uint8_t> image_data = image->get_data();
  89. basisu::vector<basisu::image> basisu_mipmaps;
  90. for (int32_t i = 0; i <= image->get_mipmap_count(); i++) {
  91. int ofs, size, width, height;
  92. image->get_mipmap_offset_size_and_dimensions(i, ofs, size, width, height);
  93. basisu::image basisu_image(width, height);
  94. memcpy(basisu_image.get_ptr(), image_data.ptr() + ofs, size);
  95. if (i == 0) {
  96. params.m_source_images.push_back(basisu_image);
  97. } else {
  98. basisu_mipmaps.push_back(basisu_image);
  99. }
  100. }
  101. params.m_source_mipmap_images.push_back(basisu_mipmaps);
  102. }
  103. // Encode the image data.
  104. Vector<uint8_t> basisu_data;
  105. basisu::basis_compressor compressor;
  106. compressor.init(params);
  107. int basisu_err = compressor.process();
  108. ERR_FAIL_COND_V(basisu_err != basisu::basis_compressor::cECSuccess, basisu_data);
  109. const basisu::uint8_vec &basisu_out = compressor.get_output_basis_file();
  110. basisu_data.resize(basisu_out.size() + 4);
  111. // Copy the encoded data to the buffer.
  112. {
  113. uint8_t *w = basisu_data.ptrw();
  114. *(uint32_t *)w = decompress_format;
  115. memcpy(w + 4, basisu_out.get_ptr(), basisu_out.size());
  116. }
  117. return basisu_data;
  118. }
  119. #endif // TOOLS_ENABLED
  120. Ref<Image> basis_universal_unpacker_ptr(const uint8_t *p_data, int p_size) {
  121. Ref<Image> image;
  122. ERR_FAIL_NULL_V_MSG(p_data, image, "Cannot unpack invalid BasisUniversal data.");
  123. const uint8_t *src_ptr = p_data;
  124. int src_size = p_size;
  125. basist::transcoder_texture_format basisu_format = basist::transcoder_texture_format::cTFTotalTextureFormats;
  126. Image::Format image_format = Image::FORMAT_MAX;
  127. // Get supported compression formats.
  128. bool bptc_supported = RS::get_singleton()->has_os_feature("bptc");
  129. bool astc_supported = RS::get_singleton()->has_os_feature("astc");
  130. bool s3tc_supported = RS::get_singleton()->has_os_feature("s3tc");
  131. bool etc2_supported = RS::get_singleton()->has_os_feature("etc2");
  132. switch (*(uint32_t *)(src_ptr)) {
  133. case BASIS_DECOMPRESS_RG: {
  134. // RGTC transcoding is currently performed with RG_AS_RA, fail.
  135. ERR_FAIL_V(image);
  136. } break;
  137. case BASIS_DECOMPRESS_RGB: {
  138. if (bptc_supported) {
  139. basisu_format = basist::transcoder_texture_format::cTFBC7_M6_OPAQUE_ONLY;
  140. image_format = Image::FORMAT_BPTC_RGBA;
  141. } else if (astc_supported) {
  142. basisu_format = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;
  143. image_format = Image::FORMAT_ASTC_4x4;
  144. } else if (s3tc_supported) {
  145. basisu_format = basist::transcoder_texture_format::cTFBC1;
  146. image_format = Image::FORMAT_DXT1;
  147. } else if (etc2_supported) {
  148. basisu_format = basist::transcoder_texture_format::cTFETC1;
  149. image_format = Image::FORMAT_ETC2_RGB8;
  150. } else {
  151. // No supported VRAM compression formats, decompress.
  152. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  153. image_format = Image::FORMAT_RGBA8;
  154. }
  155. } break;
  156. case BASIS_DECOMPRESS_RGBA: {
  157. if (bptc_supported) {
  158. basisu_format = basist::transcoder_texture_format::cTFBC7_M5;
  159. image_format = Image::FORMAT_BPTC_RGBA;
  160. } else if (astc_supported) {
  161. basisu_format = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;
  162. image_format = Image::FORMAT_ASTC_4x4;
  163. } else if (s3tc_supported) {
  164. basisu_format = basist::transcoder_texture_format::cTFBC3;
  165. image_format = Image::FORMAT_DXT5;
  166. } else if (etc2_supported) {
  167. basisu_format = basist::transcoder_texture_format::cTFETC2;
  168. image_format = Image::FORMAT_ETC2_RGBA8;
  169. } else {
  170. // No supported VRAM compression formats, decompress.
  171. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  172. image_format = Image::FORMAT_RGBA8;
  173. }
  174. } break;
  175. case BASIS_DECOMPRESS_RG_AS_RA: {
  176. if (s3tc_supported) {
  177. basisu_format = basist::transcoder_texture_format::cTFBC3;
  178. image_format = Image::FORMAT_DXT5_RA_AS_RG;
  179. } else if (etc2_supported) {
  180. basisu_format = basist::transcoder_texture_format::cTFETC2;
  181. image_format = Image::FORMAT_ETC2_RA_AS_RG;
  182. } else {
  183. // No supported VRAM compression formats, decompress.
  184. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  185. image_format = Image::FORMAT_RGBA8;
  186. }
  187. } break;
  188. }
  189. src_ptr += 4;
  190. src_size -= 4;
  191. basist::basisu_transcoder transcoder;
  192. ERR_FAIL_COND_V(!transcoder.validate_header(src_ptr, src_size), image);
  193. transcoder.start_transcoding(src_ptr, src_size);
  194. basist::basisu_image_info basisu_info;
  195. transcoder.get_image_info(src_ptr, src_size, basisu_info, 0);
  196. Vector<uint8_t> out_data;
  197. out_data.resize(Image::get_image_data_size(basisu_info.m_width, basisu_info.m_height, image_format, basisu_info.m_total_levels > 1));
  198. uint8_t *dst = out_data.ptrw();
  199. memset(dst, 0, out_data.size());
  200. uint32_t mip_count = Image::get_image_required_mipmaps(basisu_info.m_orig_width, basisu_info.m_orig_height, image_format);
  201. for (uint32_t i = 0; i <= mip_count; i++) {
  202. basist::basisu_image_level_info basisu_level;
  203. transcoder.get_image_level_info(src_ptr, src_size, basisu_level, 0, i);
  204. int ofs = Image::get_image_mipmap_offset(basisu_info.m_width, basisu_info.m_height, image_format, i);
  205. bool result = transcoder.transcode_image_level(src_ptr, src_size, 0, i, dst + ofs, basisu_level.m_total_blocks, basisu_format);
  206. if (!result) {
  207. print_line(vformat("BasisUniversal cannot unpack level %d.", i));
  208. break;
  209. }
  210. }
  211. image = Image::create_from_data(basisu_info.m_width, basisu_info.m_height, basisu_info.m_total_levels > 1, image_format, out_data);
  212. return image;
  213. }
  214. Ref<Image> basis_universal_unpacker(const Vector<uint8_t> &p_buffer) {
  215. return basis_universal_unpacker_ptr(p_buffer.ptr(), p_buffer.size());
  216. }