image_decompress_bcdec.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**************************************************************************/
  2. /* image_decompress_bcdec.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_decompress_bcdec.h"
  31. #include "core/os/os.h"
  32. #include "core/string/print_string.h"
  33. #define BCDEC_IMPLEMENTATION
  34. #include "thirdparty/misc/bcdec.h"
  35. inline void bcdec_bc6h_half_s(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
  36. bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, true);
  37. }
  38. inline void bcdec_bc6h_half_u(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
  39. bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, false);
  40. }
  41. template <void (*decompress_func)(const void *, void *, int), int block_size, int pixel_size, int component_size>
  42. static inline void _safe_decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {
  43. // A stack-allocated output buffer large enough to contain an entire uncompressed block.
  44. uint8_t temp_buf[4 * 4 * pixel_size];
  45. // The amount of misaligned pixels on each axis.
  46. const int width_diff = width - (width & ~0x03);
  47. const int height_diff = height - (height & ~0x03);
  48. // The amount of uncompressed blocks on each axis.
  49. const int width_blocks = (width & ~0x03) / 4;
  50. const int height_blocks = (height & ~0x03) / 4;
  51. // The pitch of the image in bytes.
  52. const int image_pitch = width * pixel_size;
  53. // The pitch of a block in bytes.
  54. const int block_pitch = 4 * pixel_size;
  55. // The pitch of the last block in bytes.
  56. const int odd_pitch = width_diff * pixel_size;
  57. size_t src_pos = 0;
  58. size_t dst_pos = 0;
  59. // Decompress the blocks, starting from the top.
  60. for (int y = 0; y < height_blocks; y += 1) {
  61. // Decompress the blocks, starting from the left.
  62. for (int x = 0; x < width_blocks; x += 1) {
  63. decompress_func(&src[src_pos], &dst[dst_pos], image_pitch / component_size);
  64. src_pos += block_size;
  65. dst_pos += block_pitch;
  66. }
  67. // Decompress the block on the right.
  68. if (width_diff > 0) {
  69. decompress_func(&src[src_pos], temp_buf, block_pitch / component_size);
  70. // Copy the data from the temporary buffer to the output.
  71. for (int i = 0; i < 4; i++) {
  72. memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);
  73. }
  74. src_pos += block_size;
  75. dst_pos += odd_pitch;
  76. }
  77. // Skip to the next row of blocks, the current one has already been filled.
  78. dst_pos += 3 * image_pitch;
  79. }
  80. // Decompress the blocks at the bottom of the image.
  81. if (height_diff > 0) {
  82. // Decompress the blocks at the bottom.
  83. for (int x = 0; x < width_blocks; x += 1) {
  84. decompress_func(&src[src_pos], temp_buf, block_pitch / component_size);
  85. // Copy the data from the temporary buffer to the output.
  86. for (int i = 0; i < height_diff; i++) {
  87. memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], block_pitch);
  88. }
  89. src_pos += block_size;
  90. dst_pos += block_pitch;
  91. }
  92. // Decompress the block in the lower-right corner.
  93. if (width_diff > 0) {
  94. decompress_func(&src[src_pos], temp_buf, block_pitch / component_size);
  95. // Copy the data from the temporary buffer to the output.
  96. for (int i = 0; i < height_diff; i++) {
  97. memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);
  98. }
  99. src_pos += block_size;
  100. dst_pos += odd_pitch;
  101. }
  102. }
  103. }
  104. template <void (*decompress_func)(const void *, void *, int), int block_size, int pixel_size, int component_size>
  105. static inline void _decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {
  106. size_t src_pos = 0;
  107. size_t dst_pos = 0;
  108. // The size of a single block in bytes.
  109. const int block_pitch = 4 * pixel_size;
  110. // The pitch of the image in bytes.
  111. const int image_pitch = width * pixel_size;
  112. for (int y = 0; y < height; y += 4) {
  113. for (int x = 0; x < width; x += 4) {
  114. decompress_func(&src[src_pos], &dst[dst_pos], image_pitch / component_size);
  115. src_pos += block_size;
  116. dst_pos += block_pitch;
  117. }
  118. // Skip to the next row of blocks, the current one has already been filled.
  119. dst_pos += 3 * image_pitch;
  120. }
  121. }
  122. static void decompress_image(BCdecFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {
  123. const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);
  124. uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);
  125. const uint64_t aligned_width = (width + 3) & ~0x03;
  126. const uint64_t aligned_height = (height + 3) & ~0x03;
  127. if (width != aligned_width || height != aligned_height) {
  128. // Decompress the mipmap in a 'safe' way, which involves starting from the top left.
  129. // For each block row, decompress all of the 'full' blocks, then the misaligned one (on the x axis).
  130. // Then, decompress the final misaligned block row at the bottom.
  131. // Finally, decompress the misaligned block at the bottom right.
  132. switch (format) {
  133. case BCdec_BC1: {
  134. _safe_decompress_mipmap<bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  135. } break;
  136. case BCdec_BC2: {
  137. _safe_decompress_mipmap<bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  138. } break;
  139. case BCdec_BC3: {
  140. _safe_decompress_mipmap<bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  141. } break;
  142. case BCdec_BC4: {
  143. _safe_decompress_mipmap<bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1>(width, height, src_blocks, dec_blocks);
  144. } break;
  145. case BCdec_BC5: {
  146. _safe_decompress_mipmap<bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 1>(width, height, src_blocks, dec_blocks);
  147. } break;
  148. case BCdec_BC6U: {
  149. _safe_decompress_mipmap<bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
  150. } break;
  151. case BCdec_BC6S: {
  152. _safe_decompress_mipmap<bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
  153. } break;
  154. case BCdec_BC7: {
  155. _safe_decompress_mipmap<bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  156. } break;
  157. }
  158. } else {
  159. // Just decompress as usual, as fast as possible.
  160. switch (format) {
  161. case BCdec_BC1: {
  162. _decompress_mipmap<bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  163. } break;
  164. case BCdec_BC2: {
  165. _decompress_mipmap<bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  166. } break;
  167. case BCdec_BC3: {
  168. _decompress_mipmap<bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  169. } break;
  170. case BCdec_BC4: {
  171. _decompress_mipmap<bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1>(width, height, src_blocks, dec_blocks);
  172. } break;
  173. case BCdec_BC5: {
  174. _decompress_mipmap<bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 1>(width, height, src_blocks, dec_blocks);
  175. } break;
  176. case BCdec_BC6U: {
  177. _decompress_mipmap<bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
  178. } break;
  179. case BCdec_BC6S: {
  180. _decompress_mipmap<bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
  181. } break;
  182. case BCdec_BC7: {
  183. _decompress_mipmap<bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
  184. } break;
  185. }
  186. }
  187. }
  188. void image_decompress_bcdec(Image *p_image) {
  189. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  190. int width = p_image->get_width();
  191. int height = p_image->get_height();
  192. Image::Format source_format = p_image->get_format();
  193. Image::Format target_format = Image::FORMAT_MAX;
  194. BCdecFormat bcdec_format = BCdec_BC1;
  195. switch (source_format) {
  196. case Image::FORMAT_DXT1:
  197. bcdec_format = BCdec_BC1;
  198. target_format = Image::FORMAT_RGBA8;
  199. break;
  200. case Image::FORMAT_DXT3:
  201. bcdec_format = BCdec_BC2;
  202. target_format = Image::FORMAT_RGBA8;
  203. break;
  204. case Image::FORMAT_DXT5:
  205. case Image::FORMAT_DXT5_RA_AS_RG:
  206. bcdec_format = BCdec_BC3;
  207. target_format = Image::FORMAT_RGBA8;
  208. break;
  209. case Image::FORMAT_RGTC_R:
  210. bcdec_format = BCdec_BC4;
  211. target_format = Image::FORMAT_R8;
  212. break;
  213. case Image::FORMAT_RGTC_RG:
  214. bcdec_format = BCdec_BC5;
  215. target_format = Image::FORMAT_RG8;
  216. break;
  217. case Image::FORMAT_BPTC_RGBFU:
  218. bcdec_format = BCdec_BC6U;
  219. target_format = Image::FORMAT_RGBH;
  220. break;
  221. case Image::FORMAT_BPTC_RGBF:
  222. bcdec_format = BCdec_BC6S;
  223. target_format = Image::FORMAT_RGBH;
  224. break;
  225. case Image::FORMAT_BPTC_RGBA:
  226. bcdec_format = BCdec_BC7;
  227. target_format = Image::FORMAT_RGBA8;
  228. break;
  229. default:
  230. ERR_FAIL_MSG("bcdec: Can't decompress unknown format: " + Image::get_format_name(source_format) + ".");
  231. break;
  232. }
  233. int mm_count = p_image->get_mipmap_count();
  234. int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());
  235. // Decompressed data.
  236. Vector<uint8_t> data;
  237. data.resize(target_size);
  238. uint8_t *wb = data.ptrw();
  239. // Source data.
  240. const uint8_t *rb = p_image->get_data().ptr();
  241. // Decompress mipmaps.
  242. for (int i = 0; i <= mm_count; i++) {
  243. int mipmap_w = 0, mipmap_h = 0;
  244. int64_t src_ofs = Image::get_image_mipmap_offset(width, height, source_format, i);
  245. int64_t dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, mipmap_w, mipmap_h);
  246. decompress_image(bcdec_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);
  247. }
  248. p_image->set_data(width, height, p_image->has_mipmaps(), target_format, data);
  249. // Swap channels if the format is using a channel swizzle.
  250. if (source_format == Image::FORMAT_DXT5_RA_AS_RG) {
  251. p_image->convert_ra_rgba8_to_rg();
  252. }
  253. print_verbose(vformat("bcdec: Decompression of a %dx%d %s image with %d mipmaps took %d ms.",
  254. p_image->get_width(), p_image->get_height(), Image::get_format_name(source_format), p_image->get_mipmap_count(), OS::get_singleton()->get_ticks_msec() - start_time));
  255. }