image_compress_etc.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**************************************************************************/
  2. /* image_compress_etc.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_etc.h"
  31. #include "core/image.h"
  32. #include "core/os/os.h"
  33. #include "core/print_string.h"
  34. #include <Etc.h>
  35. #include <EtcFilter.h>
  36. static Image::Format _get_etc2_mode(Image::DetectChannels format) {
  37. switch (format) {
  38. case Image::DETECTED_R:
  39. return Image::FORMAT_ETC2_R11;
  40. case Image::DETECTED_RG:
  41. return Image::FORMAT_ETC2_RG11;
  42. case Image::DETECTED_RGB:
  43. return Image::FORMAT_ETC2_RGB8;
  44. case Image::DETECTED_RGBA:
  45. return Image::FORMAT_ETC2_RGBA8;
  46. // TODO: would be nice if we could use FORMAT_ETC2_RGB8A1 for FORMAT_RGBA5551
  47. default:
  48. // TODO: Kept for compatibility, but should be investigated whether it's correct or if it should error out
  49. return Image::FORMAT_ETC2_RGBA8;
  50. }
  51. }
  52. static Etc::Image::Format _image_format_to_etc2comp_format(Image::Format format) {
  53. switch (format) {
  54. case Image::FORMAT_ETC:
  55. return Etc::Image::Format::ETC1;
  56. case Image::FORMAT_ETC2_R11:
  57. return Etc::Image::Format::R11;
  58. case Image::FORMAT_ETC2_R11S:
  59. return Etc::Image::Format::SIGNED_R11;
  60. case Image::FORMAT_ETC2_RG11:
  61. return Etc::Image::Format::RG11;
  62. case Image::FORMAT_ETC2_RG11S:
  63. return Etc::Image::Format::SIGNED_RG11;
  64. case Image::FORMAT_ETC2_RGB8:
  65. return Etc::Image::Format::RGB8;
  66. case Image::FORMAT_ETC2_RGBA8:
  67. return Etc::Image::Format::RGBA8;
  68. case Image::FORMAT_ETC2_RGB8A1:
  69. return Etc::Image::Format::RGB8A1;
  70. default:
  71. ERR_FAIL_V(Etc::Image::Format::UNKNOWN);
  72. }
  73. }
  74. static void _compress_etc(Image *p_img, float p_lossy_quality, bool force_etc1_format, Image::CompressSource p_source) {
  75. Image::Format img_format = p_img->get_format();
  76. Image::DetectChannels detected_channels = p_img->get_detected_channels();
  77. if (p_source == Image::COMPRESS_SOURCE_LAYERED) {
  78. //keep what comes in
  79. switch (p_img->get_format()) {
  80. case Image::FORMAT_L8: {
  81. detected_channels = Image::DETECTED_L;
  82. } break;
  83. case Image::FORMAT_LA8: {
  84. detected_channels = Image::DETECTED_LA;
  85. } break;
  86. case Image::FORMAT_R8: {
  87. detected_channels = Image::DETECTED_R;
  88. } break;
  89. case Image::FORMAT_RG8: {
  90. detected_channels = Image::DETECTED_RG;
  91. } break;
  92. case Image::FORMAT_RGB8: {
  93. detected_channels = Image::DETECTED_RGB;
  94. } break;
  95. case Image::FORMAT_RGBA8:
  96. case Image::FORMAT_RGBA4444:
  97. case Image::FORMAT_RGBA5551: {
  98. detected_channels = Image::DETECTED_RGBA;
  99. } break;
  100. default: {
  101. }
  102. }
  103. }
  104. if (p_source == Image::COMPRESS_SOURCE_SRGB && (detected_channels == Image::DETECTED_R || detected_channels == Image::DETECTED_RG)) {
  105. //R and RG do not support SRGB
  106. detected_channels = Image::DETECTED_RGB;
  107. }
  108. if (p_source == Image::COMPRESS_SOURCE_NORMAL) {
  109. //use RG channels only for normal
  110. detected_channels = Image::DETECTED_RG;
  111. }
  112. if (img_format >= Image::FORMAT_DXT1) {
  113. return; //do not compress, already compressed
  114. }
  115. if (img_format > Image::FORMAT_RGBA8) {
  116. // TODO: we should be able to handle FORMAT_RGBA4444 and FORMAT_RGBA5551 eventually
  117. return;
  118. }
  119. if (force_etc1_format) {
  120. // If VRAM compression is using ETC, but image has alpha, convert to RGBA4444 or LA8
  121. // This saves space while maintaining the alpha channel
  122. if (detected_channels == Image::DETECTED_RGBA) {
  123. if (p_img->has_mipmaps()) {
  124. // Image doesn't support mipmaps with RGBA4444 textures
  125. p_img->clear_mipmaps();
  126. }
  127. p_img->convert(Image::FORMAT_RGBA4444);
  128. return;
  129. } else if (detected_channels == Image::DETECTED_LA) {
  130. p_img->convert(Image::FORMAT_LA8);
  131. return;
  132. }
  133. }
  134. uint32_t imgw = p_img->get_width(), imgh = p_img->get_height();
  135. Image::Format etc_format = force_etc1_format ? Image::FORMAT_ETC : _get_etc2_mode(detected_channels);
  136. Ref<Image> img = p_img->duplicate();
  137. if (img->get_format() != Image::FORMAT_RGBA8) {
  138. img->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
  139. }
  140. if (img->has_mipmaps()) {
  141. if (next_power_of_2(imgw) != imgw || next_power_of_2(imgh) != imgh) {
  142. img->resize_to_po2();
  143. imgw = img->get_width();
  144. imgh = img->get_height();
  145. }
  146. } else {
  147. if (imgw % 4 != 0 || imgh % 4 != 0) {
  148. if (imgw % 4) {
  149. imgw += 4 - imgw % 4;
  150. }
  151. if (imgh % 4) {
  152. imgh += 4 - imgh % 4;
  153. }
  154. img->resize(imgw, imgh);
  155. }
  156. }
  157. PoolVector<uint8_t>::Read r = img->get_data().read();
  158. ERR_FAIL_COND(!r.ptr());
  159. unsigned int target_size = Image::get_image_data_size(imgw, imgh, etc_format, p_img->has_mipmaps());
  160. int mmc = 1 + (p_img->has_mipmaps() ? Image::get_image_required_mipmaps(imgw, imgh, etc_format) : 0);
  161. PoolVector<uint8_t> dst_data;
  162. dst_data.resize(target_size);
  163. PoolVector<uint8_t>::Write w = dst_data.write();
  164. // prepare parameters to be passed to etc2comp
  165. int num_cpus = OS::get_singleton()->get_processor_count();
  166. int encoding_time = 0;
  167. float effort = 0.0; //default, reasonable time
  168. if (p_lossy_quality > 0.95) {
  169. effort = 80;
  170. } else if (p_lossy_quality > 0.85) {
  171. effort = 60;
  172. } else if (p_lossy_quality > 0.75) {
  173. effort = 40;
  174. }
  175. Etc::ErrorMetric error_metric = Etc::ErrorMetric::RGBX; // NOTE: we can experiment with other error metrics
  176. Etc::Image::Format etc2comp_etc_format = _image_format_to_etc2comp_format(etc_format);
  177. int wofs = 0;
  178. print_verbose("ETC: Begin encoding, format: " + Image::get_format_name(etc_format));
  179. uint64_t t = OS::get_singleton()->get_ticks_msec();
  180. for (int i = 0; i < mmc; i++) {
  181. // convert source image to internal etc2comp format (which is equivalent to Image::FORMAT_RGBAF)
  182. // NOTE: We can alternatively add a case to Image::convert to handle Image::FORMAT_RGBAF conversion.
  183. int mipmap_ofs = 0, mipmap_size = 0, mipmap_w = 0, mipmap_h = 0;
  184. img->get_mipmap_offset_size_and_dimensions(i, mipmap_ofs, mipmap_size, mipmap_w, mipmap_h);
  185. const uint8_t *src = &r[mipmap_ofs];
  186. Etc::ColorFloatRGBA *src_rgba_f = new Etc::ColorFloatRGBA[mipmap_w * mipmap_h];
  187. for (int j = 0; j < mipmap_w * mipmap_h; j++) {
  188. int si = j * 4; // RGBA8
  189. src_rgba_f[j] = Etc::ColorFloatRGBA::ConvertFromRGBA8(src[si], src[si + 1], src[si + 2], src[si + 3]);
  190. }
  191. unsigned char *etc_data = nullptr;
  192. unsigned int etc_data_len = 0;
  193. unsigned int extended_width = 0, extended_height = 0;
  194. Etc::Encode((float *)src_rgba_f, mipmap_w, mipmap_h, etc2comp_etc_format, error_metric, effort, num_cpus, num_cpus, &etc_data, &etc_data_len, &extended_width, &extended_height, &encoding_time);
  195. CRASH_COND(wofs + etc_data_len > target_size);
  196. memcpy(&w[wofs], etc_data, etc_data_len);
  197. wofs += etc_data_len;
  198. delete[] etc_data;
  199. delete[] src_rgba_f;
  200. }
  201. print_verbose("ETC: Time encoding: " + rtos(OS::get_singleton()->get_ticks_msec() - t));
  202. p_img->create(imgw, imgh, p_img->has_mipmaps(), etc_format, dst_data);
  203. }
  204. static void _compress_etc1(Image *p_img, float p_lossy_quality) {
  205. _compress_etc(p_img, p_lossy_quality, true, Image::COMPRESS_SOURCE_GENERIC);
  206. }
  207. static void _compress_etc2(Image *p_img, float p_lossy_quality, Image::CompressSource p_source) {
  208. _compress_etc(p_img, p_lossy_quality, false, p_source);
  209. }
  210. void _register_etc_compress_func() {
  211. Image::_image_compress_etc1_func = _compress_etc1;
  212. Image::_image_compress_etc2_func = _compress_etc2;
  213. }