image_compress_basisu.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // Copy the source image data with mipmaps into BasisU.
  87. {
  88. const int orig_width = image->get_width();
  89. const int orig_height = image->get_height();
  90. bool is_res_div_4 = (orig_width % 4 == 0) && (orig_height % 4 == 0);
  91. // Image's resolution rounded up to the nearest values divisible by 4.
  92. int next_width = orig_width <= 2 ? orig_width : (orig_width + 3) & ~3;
  93. int next_height = orig_height <= 2 ? orig_height : (orig_height + 3) & ~3;
  94. Vector<uint8_t> image_data = image->get_data();
  95. basisu::vector<basisu::image> basisu_mipmaps;
  96. // Buffer for storing padded mipmap data.
  97. Vector<uint32_t> mip_data_padded;
  98. for (int32_t i = 0; i <= image->get_mipmap_count(); i++) {
  99. int ofs, size, width, height;
  100. image->get_mipmap_offset_size_and_dimensions(i, ofs, size, width, height);
  101. const uint8_t *image_mip_data = image_data.ptr() + ofs;
  102. // Pad the mipmap's data if its resolution isn't divisible by 4.
  103. if (image->has_mipmaps() && !is_res_div_4 && (width > 2 && height > 2) && (width != next_width || height != next_height)) {
  104. // Source mip's data interpreted as 32-bit RGBA blocks to help with copying pixel data.
  105. const uint32_t *mip_src_data = reinterpret_cast<const uint32_t *>(image_mip_data);
  106. // Reserve space in the padded buffer.
  107. mip_data_padded.resize(next_width * next_height);
  108. uint32_t *data_padded_ptr = mip_data_padded.ptrw();
  109. // Pad mipmap to the nearest block by smearing.
  110. int x = 0, y = 0;
  111. for (y = 0; y < height; y++) {
  112. for (x = 0; x < width; x++) {
  113. data_padded_ptr[next_width * y + x] = mip_src_data[width * y + x];
  114. }
  115. // First, smear in x.
  116. for (; x < next_width; x++) {
  117. data_padded_ptr[next_width * y + x] = data_padded_ptr[next_width * y + x - 1];
  118. }
  119. }
  120. // Then, smear in y.
  121. for (; y < next_height; y++) {
  122. for (x = 0; x < next_width; x++) {
  123. data_padded_ptr[next_width * y + x] = data_padded_ptr[next_width * y + x - next_width];
  124. }
  125. }
  126. // Override the image_mip_data pointer with our temporary Vector.
  127. image_mip_data = reinterpret_cast<const uint8_t *>(mip_data_padded.ptr());
  128. // Override the mipmap's properties.
  129. width = next_width;
  130. height = next_height;
  131. size = mip_data_padded.size() * 4;
  132. }
  133. // Get the next mipmap's resolution.
  134. next_width /= 2;
  135. next_height /= 2;
  136. // Copy the source mipmap's data to a BasisU image.
  137. basisu::image basisu_image(width, height);
  138. memcpy(basisu_image.get_ptr(), image_mip_data, size);
  139. if (i == 0) {
  140. params.m_source_images.push_back(basisu_image);
  141. } else {
  142. basisu_mipmaps.push_back(basisu_image);
  143. }
  144. }
  145. params.m_source_mipmap_images.push_back(basisu_mipmaps);
  146. }
  147. // Encode the image data.
  148. Vector<uint8_t> basisu_data;
  149. basisu::basis_compressor compressor;
  150. compressor.init(params);
  151. int basisu_err = compressor.process();
  152. ERR_FAIL_COND_V(basisu_err != basisu::basis_compressor::cECSuccess, basisu_data);
  153. const basisu::uint8_vec &basisu_out = compressor.get_output_basis_file();
  154. basisu_data.resize(basisu_out.size() + 4);
  155. // Copy the encoded data to the buffer.
  156. {
  157. uint8_t *wb = basisu_data.ptrw();
  158. *(uint32_t *)wb = decompress_format;
  159. memcpy(wb + 4, basisu_out.get_ptr(), basisu_out.size());
  160. }
  161. return basisu_data;
  162. }
  163. #endif // TOOLS_ENABLED
  164. Ref<Image> basis_universal_unpacker_ptr(const uint8_t *p_data, int p_size) {
  165. Ref<Image> image;
  166. ERR_FAIL_NULL_V_MSG(p_data, image, "Cannot unpack invalid BasisUniversal data.");
  167. const uint8_t *src_ptr = p_data;
  168. int src_size = p_size;
  169. basist::transcoder_texture_format basisu_format = basist::transcoder_texture_format::cTFTotalTextureFormats;
  170. Image::Format image_format = Image::FORMAT_MAX;
  171. // Get supported compression formats.
  172. bool bptc_supported = RS::get_singleton()->has_os_feature("bptc");
  173. bool astc_supported = RS::get_singleton()->has_os_feature("astc");
  174. bool s3tc_supported = RS::get_singleton()->has_os_feature("s3tc");
  175. bool etc2_supported = RS::get_singleton()->has_os_feature("etc2");
  176. bool needs_ra_rg_swap = false;
  177. switch (*(uint32_t *)(src_ptr)) {
  178. case BASIS_DECOMPRESS_RG: {
  179. // RGTC transcoding is currently performed with RG_AS_RA, fail.
  180. ERR_FAIL_V(image);
  181. } break;
  182. case BASIS_DECOMPRESS_RGB: {
  183. if (bptc_supported) {
  184. basisu_format = basist::transcoder_texture_format::cTFBC7_M6_OPAQUE_ONLY;
  185. image_format = Image::FORMAT_BPTC_RGBA;
  186. } else if (astc_supported) {
  187. basisu_format = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;
  188. image_format = Image::FORMAT_ASTC_4x4;
  189. } else if (s3tc_supported) {
  190. basisu_format = basist::transcoder_texture_format::cTFBC1;
  191. image_format = Image::FORMAT_DXT1;
  192. } else if (etc2_supported) {
  193. basisu_format = basist::transcoder_texture_format::cTFETC1;
  194. image_format = Image::FORMAT_ETC2_RGB8;
  195. } else {
  196. // No supported VRAM compression formats, decompress.
  197. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  198. image_format = Image::FORMAT_RGBA8;
  199. }
  200. } break;
  201. case BASIS_DECOMPRESS_RGBA: {
  202. if (bptc_supported) {
  203. basisu_format = basist::transcoder_texture_format::cTFBC7_M5;
  204. image_format = Image::FORMAT_BPTC_RGBA;
  205. } else if (astc_supported) {
  206. basisu_format = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;
  207. image_format = Image::FORMAT_ASTC_4x4;
  208. } else if (s3tc_supported) {
  209. basisu_format = basist::transcoder_texture_format::cTFBC3;
  210. image_format = Image::FORMAT_DXT5;
  211. } else if (etc2_supported) {
  212. basisu_format = basist::transcoder_texture_format::cTFETC2;
  213. image_format = Image::FORMAT_ETC2_RGBA8;
  214. } else {
  215. // No supported VRAM compression formats, decompress.
  216. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  217. image_format = Image::FORMAT_RGBA8;
  218. }
  219. } break;
  220. case BASIS_DECOMPRESS_RG_AS_RA: {
  221. if (s3tc_supported) {
  222. basisu_format = basist::transcoder_texture_format::cTFBC3;
  223. image_format = Image::FORMAT_DXT5_RA_AS_RG;
  224. } else if (etc2_supported) {
  225. basisu_format = basist::transcoder_texture_format::cTFETC2;
  226. image_format = Image::FORMAT_ETC2_RA_AS_RG;
  227. } else {
  228. // No supported VRAM compression formats, decompress.
  229. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  230. image_format = Image::FORMAT_RGBA8;
  231. needs_ra_rg_swap = true;
  232. }
  233. } break;
  234. }
  235. src_ptr += 4;
  236. src_size -= 4;
  237. basist::basisu_transcoder transcoder;
  238. ERR_FAIL_COND_V(!transcoder.validate_header(src_ptr, src_size), image);
  239. transcoder.start_transcoding(src_ptr, src_size);
  240. basist::basisu_image_info basisu_info;
  241. transcoder.get_image_info(src_ptr, src_size, basisu_info, 0);
  242. // Create the buffer for transcoded/decompressed data.
  243. Vector<uint8_t> out_data;
  244. out_data.resize(Image::get_image_data_size(basisu_info.m_width, basisu_info.m_height, image_format, basisu_info.m_total_levels > 1));
  245. uint8_t *dst = out_data.ptrw();
  246. memset(dst, 0, out_data.size());
  247. for (uint32_t i = 0; i < basisu_info.m_total_levels; i++) {
  248. basist::basisu_image_level_info basisu_level;
  249. transcoder.get_image_level_info(src_ptr, src_size, basisu_level, 0, i);
  250. uint32_t mip_block_or_pixel_count = Image::is_format_compressed(image_format) ? basisu_level.m_total_blocks : basisu_level.m_orig_width * basisu_level.m_orig_height;
  251. int ofs = Image::get_image_mipmap_offset(basisu_info.m_width, basisu_info.m_height, image_format, i);
  252. bool result = transcoder.transcode_image_level(src_ptr, src_size, 0, i, dst + ofs, mip_block_or_pixel_count, basisu_format);
  253. if (!result) {
  254. print_line(vformat("BasisUniversal cannot unpack level %d.", i));
  255. break;
  256. }
  257. }
  258. image = Image::create_from_data(basisu_info.m_width, basisu_info.m_height, basisu_info.m_total_levels > 1, image_format, out_data);
  259. if (needs_ra_rg_swap) {
  260. // Swap uncompressed RA-as-RG texture's color channels.
  261. image->convert_ra_rgba8_to_rg();
  262. }
  263. return image;
  264. }
  265. Ref<Image> basis_universal_unpacker(const Vector<uint8_t> &p_buffer) {
  266. return basis_universal_unpacker_ptr(p_buffer.ptr(), p_buffer.size());
  267. }