image_compress_etcpak.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**************************************************************************/
  2. /* image_compress_etcpak.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_etcpak.h"
  31. #include "core/os/os.h"
  32. #include "core/string/print_string.h"
  33. #include <ProcessDxtc.hpp>
  34. #include <ProcessRGB.hpp>
  35. EtcpakType _determine_etc_type(Image::UsedChannels p_channels) {
  36. switch (p_channels) {
  37. case Image::USED_CHANNELS_L:
  38. return EtcpakType::ETCPAK_TYPE_ETC2;
  39. case Image::USED_CHANNELS_LA:
  40. return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;
  41. case Image::USED_CHANNELS_R:
  42. return EtcpakType::ETCPAK_TYPE_ETC2_R;
  43. case Image::USED_CHANNELS_RG:
  44. return EtcpakType::ETCPAK_TYPE_ETC2_RG;
  45. case Image::USED_CHANNELS_RGB:
  46. return EtcpakType::ETCPAK_TYPE_ETC2;
  47. case Image::USED_CHANNELS_RGBA:
  48. return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;
  49. default:
  50. return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;
  51. }
  52. }
  53. EtcpakType _determine_dxt_type(Image::UsedChannels p_channels) {
  54. switch (p_channels) {
  55. case Image::USED_CHANNELS_L:
  56. return EtcpakType::ETCPAK_TYPE_DXT1;
  57. case Image::USED_CHANNELS_LA:
  58. return EtcpakType::ETCPAK_TYPE_DXT5;
  59. case Image::USED_CHANNELS_R:
  60. return EtcpakType::ETCPAK_TYPE_RGTC_R;
  61. case Image::USED_CHANNELS_RG:
  62. return EtcpakType::ETCPAK_TYPE_RGTC_RG;
  63. case Image::USED_CHANNELS_RGB:
  64. return EtcpakType::ETCPAK_TYPE_DXT1;
  65. case Image::USED_CHANNELS_RGBA:
  66. return EtcpakType::ETCPAK_TYPE_DXT5;
  67. default:
  68. return EtcpakType::ETCPAK_TYPE_DXT5;
  69. }
  70. }
  71. void _compress_etc1(Image *r_img) {
  72. _compress_etcpak(EtcpakType::ETCPAK_TYPE_ETC1, r_img);
  73. }
  74. void _compress_etc2(Image *r_img, Image::UsedChannels p_channels) {
  75. EtcpakType type = _determine_etc_type(p_channels);
  76. _compress_etcpak(type, r_img);
  77. }
  78. void _compress_bc(Image *r_img, Image::UsedChannels p_channels) {
  79. EtcpakType type = _determine_dxt_type(p_channels);
  80. _compress_etcpak(type, r_img);
  81. }
  82. void _compress_etcpak(EtcpakType p_compresstype, Image *r_img) {
  83. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  84. Image::Format img_format = r_img->get_format();
  85. if (Image::is_format_compressed(img_format)) {
  86. return; // Do not compress, already compressed.
  87. }
  88. if (img_format > Image::FORMAT_RGBA8) {
  89. // TODO: we should be able to handle FORMAT_RGBA4444 and FORMAT_RGBA5551 eventually
  90. return;
  91. }
  92. // Use RGBA8 to convert.
  93. if (img_format != Image::FORMAT_RGBA8) {
  94. r_img->convert(Image::FORMAT_RGBA8);
  95. }
  96. // Determine output format based on Etcpak type.
  97. Image::Format target_format = Image::FORMAT_RGBA8;
  98. if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC1) {
  99. target_format = Image::FORMAT_ETC;
  100. r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC.
  101. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2) {
  102. target_format = Image::FORMAT_ETC2_RGB8;
  103. r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC.
  104. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_R) {
  105. target_format = Image::FORMAT_ETC2_R11;
  106. r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC.
  107. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_RG) {
  108. target_format = Image::FORMAT_ETC2_RG11;
  109. r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC.
  110. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG) {
  111. target_format = Image::FORMAT_ETC2_RA_AS_RG;
  112. r_img->convert_rg_to_ra_rgba8();
  113. r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC.
  114. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_ALPHA) {
  115. target_format = Image::FORMAT_ETC2_RGBA8;
  116. r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC.
  117. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT1) {
  118. target_format = Image::FORMAT_DXT1;
  119. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG) {
  120. target_format = Image::FORMAT_DXT5_RA_AS_RG;
  121. r_img->convert_rg_to_ra_rgba8();
  122. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT5) {
  123. target_format = Image::FORMAT_DXT5;
  124. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_RGTC_R) {
  125. target_format = Image::FORMAT_RGTC_R;
  126. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_RGTC_RG) {
  127. target_format = Image::FORMAT_RGTC_RG;
  128. } else {
  129. ERR_FAIL_MSG("Invalid or unsupported etcpak compression format, not ETC or DXT.");
  130. }
  131. // Compress image data and (if required) mipmaps.
  132. const bool mipmaps = r_img->has_mipmaps();
  133. int width = r_img->get_width();
  134. int height = r_img->get_height();
  135. /*
  136. The first mipmap level of a compressed texture must be a multiple of 4. Quote from D3D11.3 spec:
  137. BC format surfaces are always multiples of full blocks, each block representing 4x4 pixels.
  138. For mipmaps, the top level map is required to be a multiple of 4 size in all dimensions.
  139. The sizes for the lower level maps are computed as they are for all mipmapped surfaces,
  140. and thus may not be a multiple of 4, for example a top level map of 20 results in a second level
  141. map size of 10. For these cases, there is a differing 'physical' size and a 'virtual' size.
  142. The virtual size is that computed for each mip level without adjustment, which is 10 for the example.
  143. The physical size is the virtual size rounded up to the next multiple of 4, which is 12 for the example,
  144. and this represents the actual memory size. The sampling hardware will apply texture address
  145. processing based on the virtual size (using, for example, border color if specified for accesses
  146. beyond 10), and thus for the example case will not access the 11th and 12th row of the resource.
  147. So for mipmap chains when an axis becomes < 4 in size, only texels 'a','b','e','f'
  148. are used for a 2x2 map, and texel 'a' is used for 1x1. Note that this is similar to, but distinct from,
  149. the surface pitch, which can encompass additional padding beyond the physical surface size.
  150. */
  151. int next_width = width <= 2 ? width : (width + 3) & ~3;
  152. int next_height = height <= 2 ? height : (height + 3) & ~3;
  153. if (next_width != width || next_height != height) {
  154. r_img->resize(next_width, next_height, Image::INTERPOLATE_LANCZOS);
  155. width = r_img->get_width();
  156. height = r_img->get_height();
  157. }
  158. // ERR_FAIL_COND(width % 4 != 0 || height % 4 != 0); // FIXME: No longer guaranteed.
  159. // Multiple-of-4 should be guaranteed by above.
  160. // However, power-of-two 3d textures will create Nx2 and Nx1 mipmap levels,
  161. // which are individually compressed Image objects that violate the above rule.
  162. // Hence, we allow Nx1 and Nx2 images through without forcing to multiple-of-4.
  163. const uint8_t *src_read = r_img->get_data().ptr();
  164. print_verbose(vformat("etcpak: Encoding image size %dx%d to format %s%s.", width, height, Image::get_format_name(target_format), mipmaps ? ", with mipmaps" : ""));
  165. int dest_size = Image::get_image_data_size(width, height, target_format, mipmaps);
  166. Vector<uint8_t> dest_data;
  167. dest_data.resize(dest_size);
  168. uint8_t *dest_write = dest_data.ptrw();
  169. int mip_count = mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;
  170. Vector<uint32_t> padded_src;
  171. for (int i = 0; i < mip_count + 1; i++) {
  172. // Get write mip metrics for target image.
  173. int orig_mip_w, orig_mip_h;
  174. int mip_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, orig_mip_w, orig_mip_h);
  175. // Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).
  176. ERR_FAIL_COND(mip_ofs % 8 != 0);
  177. uint64_t *dest_mip_write = (uint64_t *)&dest_write[mip_ofs];
  178. // Block size. Align stride to multiple of 4 (RGBA8).
  179. int mip_w = (orig_mip_w + 3) & ~3;
  180. int mip_h = (orig_mip_h + 3) & ~3;
  181. const uint32_t blocks = mip_w * mip_h / 16;
  182. // Get mip data from source image for reading.
  183. int src_mip_ofs = r_img->get_mipmap_offset(i);
  184. const uint32_t *src_mip_read = (const uint32_t *)&src_read[src_mip_ofs];
  185. // Pad textures to nearest block by smearing.
  186. if (mip_w != orig_mip_w || mip_h != orig_mip_h) {
  187. padded_src.resize(mip_w * mip_h);
  188. uint32_t *ptrw = padded_src.ptrw();
  189. int x = 0, y = 0;
  190. for (y = 0; y < orig_mip_h; y++) {
  191. for (x = 0; x < orig_mip_w; x++) {
  192. ptrw[mip_w * y + x] = src_mip_read[orig_mip_w * y + x];
  193. }
  194. // First, smear in x.
  195. for (; x < mip_w; x++) {
  196. ptrw[mip_w * y + x] = ptrw[mip_w * y + x - 1];
  197. }
  198. }
  199. // Then, smear in y.
  200. for (; y < mip_h; y++) {
  201. for (x = 0; x < mip_w; x++) {
  202. ptrw[mip_w * y + x] = ptrw[mip_w * y + x - mip_w];
  203. }
  204. }
  205. // Override the src_mip_read pointer to our temporary Vector.
  206. src_mip_read = padded_src.ptr();
  207. }
  208. switch (p_compresstype) {
  209. case EtcpakType::ETCPAK_TYPE_ETC1:
  210. CompressEtc1RgbDither(src_mip_read, dest_mip_write, blocks, mip_w);
  211. break;
  212. case EtcpakType::ETCPAK_TYPE_ETC2:
  213. CompressEtc2Rgb(src_mip_read, dest_mip_write, blocks, mip_w, true);
  214. break;
  215. case EtcpakType::ETCPAK_TYPE_ETC2_ALPHA:
  216. case EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG:
  217. CompressEtc2Rgba(src_mip_read, dest_mip_write, blocks, mip_w, true);
  218. break;
  219. case EtcpakType::ETCPAK_TYPE_ETC2_R:
  220. CompressEacR(src_mip_read, dest_mip_write, blocks, mip_w);
  221. break;
  222. case EtcpakType::ETCPAK_TYPE_ETC2_RG:
  223. CompressEacRg(src_mip_read, dest_mip_write, blocks, mip_w);
  224. break;
  225. case EtcpakType::ETCPAK_TYPE_DXT1:
  226. CompressDxt1Dither(src_mip_read, dest_mip_write, blocks, mip_w);
  227. break;
  228. case EtcpakType::ETCPAK_TYPE_DXT5:
  229. case EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG:
  230. CompressDxt5(src_mip_read, dest_mip_write, blocks, mip_w);
  231. break;
  232. case EtcpakType::ETCPAK_TYPE_RGTC_R:
  233. CompressBc4(src_mip_read, dest_mip_write, blocks, mip_w);
  234. break;
  235. case EtcpakType::ETCPAK_TYPE_RGTC_RG:
  236. CompressBc5(src_mip_read, dest_mip_write, blocks, mip_w);
  237. break;
  238. default:
  239. ERR_FAIL_MSG("etcpak: Invalid or unsupported compression format.");
  240. break;
  241. }
  242. }
  243. // Replace original image with compressed one.
  244. r_img->set_data(width, height, mipmaps, target_format, dest_data);
  245. print_verbose(vformat("etcpak: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
  246. }