image_compress_etc.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*************************************************************************/
  2. /* image_compress_etc.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/copymem.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include <Etc.h>
  36. #include <EtcFilter.h>
  37. static Image::Format _get_etc2_mode(Image::DetectChannels format) {
  38. switch (format) {
  39. case Image::DETECTED_R:
  40. return Image::FORMAT_ETC2_R11;
  41. case Image::DETECTED_RG:
  42. return Image::FORMAT_ETC2_RG11;
  43. case Image::DETECTED_RGB:
  44. return Image::FORMAT_ETC2_RGB8;
  45. case Image::DETECTED_RGBA:
  46. return Image::FORMAT_ETC2_RGBA8;
  47. // TODO: would be nice if we could use FORMAT_ETC2_RGB8A1 for FORMAT_RGBA5551
  48. default:
  49. // TODO: Kept for compatibility, but should be investigated whether it's correct or if it should error out
  50. return Image::FORMAT_ETC2_RGBA8;
  51. }
  52. }
  53. static Etc::Image::Format _image_format_to_etc2comp_format(Image::Format format) {
  54. switch (format) {
  55. case Image::FORMAT_ETC:
  56. return Etc::Image::Format::ETC1;
  57. case Image::FORMAT_ETC2_R11:
  58. return Etc::Image::Format::R11;
  59. case Image::FORMAT_ETC2_R11S:
  60. return Etc::Image::Format::SIGNED_R11;
  61. case Image::FORMAT_ETC2_RG11:
  62. return Etc::Image::Format::RG11;
  63. case Image::FORMAT_ETC2_RG11S:
  64. return Etc::Image::Format::SIGNED_RG11;
  65. case Image::FORMAT_ETC2_RGB8:
  66. return Etc::Image::Format::RGB8;
  67. case Image::FORMAT_ETC2_RGBA8:
  68. return Etc::Image::Format::RGBA8;
  69. case Image::FORMAT_ETC2_RGB8A1:
  70. return Etc::Image::Format::RGB8A1;
  71. default:
  72. ERR_FAIL_V(Etc::Image::Format::UNKNOWN);
  73. }
  74. }
  75. static void _compress_etc(Image *p_img, float p_lossy_quality, bool force_etc1_format, Image::CompressSource p_source) {
  76. Image::Format img_format = p_img->get_format();
  77. Image::DetectChannels detected_channels = p_img->get_detected_channels();
  78. if (p_source == Image::COMPRESS_SOURCE_LAYERED) {
  79. //keep what comes in
  80. switch (p_img->get_format()) {
  81. case Image::FORMAT_L8: {
  82. detected_channels = Image::DETECTED_L;
  83. } break;
  84. case Image::FORMAT_LA8: {
  85. detected_channels = Image::DETECTED_LA;
  86. } break;
  87. case Image::FORMAT_R8: {
  88. detected_channels = Image::DETECTED_R;
  89. } break;
  90. case Image::FORMAT_RG8: {
  91. detected_channels = Image::DETECTED_RG;
  92. } break;
  93. case Image::FORMAT_RGB8: {
  94. detected_channels = Image::DETECTED_RGB;
  95. } break;
  96. case Image::FORMAT_RGBA8:
  97. case Image::FORMAT_RGBA4444:
  98. case Image::FORMAT_RGBA5551: {
  99. detected_channels = Image::DETECTED_RGBA;
  100. } break;
  101. default: {
  102. }
  103. }
  104. }
  105. if (p_source == Image::COMPRESS_SOURCE_SRGB && (detected_channels == Image::DETECTED_R || detected_channels == Image::DETECTED_RG)) {
  106. //R and RG do not support SRGB
  107. detected_channels = Image::DETECTED_RGB;
  108. }
  109. if (p_source == Image::COMPRESS_SOURCE_NORMAL) {
  110. //use RG channels only for normal
  111. detected_channels = Image::DETECTED_RG;
  112. }
  113. if (img_format >= Image::FORMAT_DXT1) {
  114. return; //do not compress, already compressed
  115. }
  116. if (img_format > Image::FORMAT_RGBA8) {
  117. // TODO: we should be able to handle FORMAT_RGBA4444 and FORMAT_RGBA5551 eventually
  118. return;
  119. }
  120. if (force_etc1_format) {
  121. // If VRAM compression is using ETC, but image has alpha, convert to RGBA4444 or LA8
  122. // This saves space while maintaining the alpha channel
  123. if (detected_channels == Image::DETECTED_RGBA) {
  124. if (p_img->has_mipmaps()) {
  125. // Image doesn't support mipmaps with RGBA4444 textures
  126. p_img->clear_mipmaps();
  127. }
  128. p_img->convert(Image::FORMAT_RGBA4444);
  129. return;
  130. } else if (detected_channels == Image::DETECTED_LA) {
  131. p_img->convert(Image::FORMAT_LA8);
  132. return;
  133. }
  134. }
  135. uint32_t imgw = p_img->get_width(), imgh = p_img->get_height();
  136. Image::Format etc_format = force_etc1_format ? Image::FORMAT_ETC : _get_etc2_mode(detected_channels);
  137. Ref<Image> img = p_img->duplicate();
  138. if (img->get_format() != Image::FORMAT_RGBA8)
  139. img->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
  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. Etc::ErrorMetric error_metric = Etc::ErrorMetric::RGBX; // NOTE: we can experiment with other error metrics
  175. Etc::Image::Format etc2comp_etc_format = _image_format_to_etc2comp_format(etc_format);
  176. int wofs = 0;
  177. print_verbose("ETC: Begin encoding, format: " + Image::get_format_name(etc_format));
  178. uint64_t t = OS::get_singleton()->get_ticks_msec();
  179. for (int i = 0; i < mmc; i++) {
  180. // convert source image to internal etc2comp format (which is equivalent to Image::FORMAT_RGBAF)
  181. // NOTE: We can alternatively add a case to Image::convert to handle Image::FORMAT_RGBAF conversion.
  182. int mipmap_ofs = 0, mipmap_size = 0, mipmap_w = 0, mipmap_h = 0;
  183. img->get_mipmap_offset_size_and_dimensions(i, mipmap_ofs, mipmap_size, mipmap_w, mipmap_h);
  184. const uint8_t *src = &r[mipmap_ofs];
  185. Etc::ColorFloatRGBA *src_rgba_f = new Etc::ColorFloatRGBA[mipmap_w * mipmap_h];
  186. for (int j = 0; j < mipmap_w * mipmap_h; j++) {
  187. int si = j * 4; // RGBA8
  188. src_rgba_f[j] = Etc::ColorFloatRGBA::ConvertFromRGBA8(src[si], src[si + 1], src[si + 2], src[si + 3]);
  189. }
  190. unsigned char *etc_data = NULL;
  191. unsigned int etc_data_len = 0;
  192. unsigned int extended_width = 0, extended_height = 0;
  193. 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);
  194. CRASH_COND(wofs + etc_data_len > target_size);
  195. memcpy(&w[wofs], etc_data, etc_data_len);
  196. wofs += etc_data_len;
  197. delete[] etc_data;
  198. delete[] src_rgba_f;
  199. }
  200. print_verbose("ETC: Time encoding: " + rtos(OS::get_singleton()->get_ticks_msec() - t));
  201. p_img->create(imgw, imgh, p_img->has_mipmaps(), etc_format, dst_data);
  202. }
  203. static void _compress_etc1(Image *p_img, float p_lossy_quality) {
  204. _compress_etc(p_img, p_lossy_quality, true, Image::COMPRESS_SOURCE_GENERIC);
  205. }
  206. static void _compress_etc2(Image *p_img, float p_lossy_quality, Image::CompressSource p_source) {
  207. _compress_etc(p_img, p_lossy_quality, false, p_source);
  208. }
  209. void _register_etc_compress_func() {
  210. Image::_image_compress_etc1_func = _compress_etc1;
  211. Image::_image_compress_etc2_func = _compress_etc2;
  212. }