image_compress_cvtt.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /**************************************************************************/
  2. /* image_compress_cvtt.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_cvtt.h"
  31. #include "core/os/os.h"
  32. #include "core/os/thread.h"
  33. #include "core/print_string.h"
  34. #include "core/safe_refcount.h"
  35. #include <ConvectionKernels.h>
  36. struct CVTTCompressionJobParams {
  37. bool is_hdr;
  38. bool is_signed;
  39. int bytes_per_pixel;
  40. cvtt::Options options;
  41. };
  42. struct CVTTCompressionRowTask {
  43. const uint8_t *in_mm_bytes;
  44. uint8_t *out_mm_bytes;
  45. int y_start;
  46. int width;
  47. int height;
  48. };
  49. struct CVTTCompressionJobQueue {
  50. CVTTCompressionJobParams job_params;
  51. const CVTTCompressionRowTask *job_tasks;
  52. uint32_t num_tasks;
  53. SafeNumeric<uint32_t> current_task;
  54. };
  55. static void _digest_row_task(const CVTTCompressionJobParams &p_job_params, const CVTTCompressionRowTask &p_row_task) {
  56. const uint8_t *in_bytes = p_row_task.in_mm_bytes;
  57. uint8_t *out_bytes = p_row_task.out_mm_bytes;
  58. int w = p_row_task.width;
  59. int h = p_row_task.height;
  60. int y_start = p_row_task.y_start;
  61. int y_end = y_start + 4;
  62. int bytes_per_pixel = p_job_params.bytes_per_pixel;
  63. bool is_hdr = p_job_params.is_hdr;
  64. bool is_signed = p_job_params.is_signed;
  65. cvtt::PixelBlockU8 input_blocks_ldr[cvtt::NumParallelBlocks];
  66. cvtt::PixelBlockF16 input_blocks_hdr[cvtt::NumParallelBlocks];
  67. for (int x_start = 0; x_start < w; x_start += 4 * cvtt::NumParallelBlocks) {
  68. int x_end = x_start + 4 * cvtt::NumParallelBlocks;
  69. for (int y = y_start; y < y_end; y++) {
  70. int first_input_element = (y - y_start) * 4;
  71. const uint8_t *row_start;
  72. if (y >= h) {
  73. row_start = in_bytes + (h - 1) * (w * bytes_per_pixel);
  74. } else {
  75. row_start = in_bytes + y * (w * bytes_per_pixel);
  76. }
  77. for (int x = x_start; x < x_end; x++) {
  78. const uint8_t *pixel_start;
  79. if (x >= w) {
  80. pixel_start = row_start + (w - 1) * bytes_per_pixel;
  81. } else {
  82. pixel_start = row_start + x * bytes_per_pixel;
  83. }
  84. int block_index = (x - x_start) / 4;
  85. int block_element = (x - x_start) % 4 + first_input_element;
  86. if (is_hdr) {
  87. memcpy(input_blocks_hdr[block_index].m_pixels[block_element], pixel_start, bytes_per_pixel);
  88. input_blocks_hdr[block_index].m_pixels[block_element][3] = 0x3c00; // 1.0 (unused)
  89. } else {
  90. memcpy(input_blocks_ldr[block_index].m_pixels[block_element], pixel_start, bytes_per_pixel);
  91. }
  92. }
  93. }
  94. uint8_t output_blocks[16 * cvtt::NumParallelBlocks];
  95. if (is_hdr) {
  96. if (is_signed) {
  97. cvtt::Kernels::EncodeBC6HS(output_blocks, input_blocks_hdr, p_job_params.options);
  98. } else {
  99. cvtt::Kernels::EncodeBC6HU(output_blocks, input_blocks_hdr, p_job_params.options);
  100. }
  101. } else {
  102. cvtt::Kernels::EncodeBC7(output_blocks, input_blocks_ldr, p_job_params.options);
  103. }
  104. unsigned int num_real_blocks = ((w - x_start) + 3) / 4;
  105. if (num_real_blocks > cvtt::NumParallelBlocks) {
  106. num_real_blocks = cvtt::NumParallelBlocks;
  107. }
  108. memcpy(out_bytes, output_blocks, 16 * num_real_blocks);
  109. out_bytes += 16 * num_real_blocks;
  110. }
  111. }
  112. static void _digest_job_queue(void *p_job_queue) {
  113. CVTTCompressionJobQueue *job_queue = static_cast<CVTTCompressionJobQueue *>(p_job_queue);
  114. for (uint32_t next_task = job_queue->current_task.increment(); next_task <= job_queue->num_tasks; next_task = job_queue->current_task.increment()) {
  115. _digest_row_task(job_queue->job_params, job_queue->job_tasks[next_task - 1]);
  116. }
  117. }
  118. void image_compress_cvtt(Image *p_image, float p_lossy_quality, Image::CompressSource p_source) {
  119. if (p_image->get_format() >= Image::FORMAT_BPTC_RGBA) {
  120. return; //do not compress, already compressed
  121. }
  122. int w = p_image->get_width();
  123. int h = p_image->get_height();
  124. bool is_ldr = (p_image->get_format() <= Image::FORMAT_RGBA8);
  125. bool is_hdr = (p_image->get_format() >= Image::FORMAT_RH) && (p_image->get_format() <= Image::FORMAT_RGBE9995);
  126. if (!is_ldr && !is_hdr) {
  127. return; // Not a usable source format
  128. }
  129. cvtt::Options options;
  130. uint32_t flags = cvtt::Flags::Fastest;
  131. if (p_lossy_quality > 0.85) {
  132. flags = cvtt::Flags::Ultra;
  133. } else if (p_lossy_quality > 0.75) {
  134. flags = cvtt::Flags::Better;
  135. } else if (p_lossy_quality > 0.55) {
  136. flags = cvtt::Flags::Default;
  137. } else if (p_lossy_quality > 0.35) {
  138. flags = cvtt::Flags::Fast;
  139. } else if (p_lossy_quality > 0.15) {
  140. flags = cvtt::Flags::Faster;
  141. }
  142. flags |= cvtt::Flags::BC7_RespectPunchThrough;
  143. if (p_source == Image::COMPRESS_SOURCE_NORMAL) {
  144. flags |= cvtt::Flags::Uniform;
  145. }
  146. options.flags = flags;
  147. Image::Format target_format = Image::FORMAT_BPTC_RGBA;
  148. bool is_signed = false;
  149. if (is_hdr) {
  150. if (p_image->get_format() != Image::FORMAT_RGBH) {
  151. p_image->convert(Image::FORMAT_RGBH);
  152. }
  153. PoolVector<uint8_t>::Read rb = p_image->get_data().read();
  154. const uint16_t *source_data = reinterpret_cast<const uint16_t *>(&rb[0]);
  155. int pixel_element_count = w * h * 3;
  156. for (int i = 0; i < pixel_element_count; i++) {
  157. if ((source_data[i] & 0x8000) != 0 && (source_data[i] & 0x7fff) != 0) {
  158. is_signed = true;
  159. break;
  160. }
  161. }
  162. target_format = is_signed ? Image::FORMAT_BPTC_RGBF : Image::FORMAT_BPTC_RGBFU;
  163. } else {
  164. p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
  165. }
  166. PoolVector<uint8_t>::Read rb = p_image->get_data().read();
  167. PoolVector<uint8_t> data;
  168. int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  169. int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(w, h, target_format) : 0;
  170. data.resize(target_size);
  171. int shift = Image::get_format_pixel_rshift(target_format);
  172. PoolVector<uint8_t>::Write wb = data.write();
  173. int dst_ofs = 0;
  174. CVTTCompressionJobQueue job_queue;
  175. job_queue.job_params.is_hdr = is_hdr;
  176. job_queue.job_params.is_signed = is_signed;
  177. job_queue.job_params.options = options;
  178. job_queue.job_params.bytes_per_pixel = is_hdr ? 6 : 4;
  179. #ifdef NO_THREADS
  180. int num_job_threads = 0;
  181. #else
  182. int num_job_threads = OS::get_singleton()->can_use_threads() ? (OS::get_singleton()->get_processor_count() - 1) : 0;
  183. #endif
  184. PoolVector<CVTTCompressionRowTask> tasks;
  185. for (int i = 0; i <= mm_count; i++) {
  186. int bw = w % 4 != 0 ? w + (4 - w % 4) : w;
  187. int bh = h % 4 != 0 ? h + (4 - h % 4) : h;
  188. int src_ofs = p_image->get_mipmap_offset(i);
  189. const uint8_t *in_bytes = &rb[src_ofs];
  190. uint8_t *out_bytes = &wb[dst_ofs];
  191. for (int y_start = 0; y_start < h; y_start += 4) {
  192. CVTTCompressionRowTask row_task;
  193. row_task.width = w;
  194. row_task.height = h;
  195. row_task.y_start = y_start;
  196. row_task.in_mm_bytes = in_bytes;
  197. row_task.out_mm_bytes = out_bytes;
  198. if (num_job_threads > 0) {
  199. tasks.push_back(row_task);
  200. } else {
  201. _digest_row_task(job_queue.job_params, row_task);
  202. }
  203. out_bytes += 16 * (bw / 4);
  204. }
  205. dst_ofs += (MAX(4, bw) * MAX(4, bh)) >> shift;
  206. w = MAX(w / 2, 1);
  207. h = MAX(h / 2, 1);
  208. }
  209. if (num_job_threads > 0) {
  210. PoolVector<Thread *> threads;
  211. threads.resize(num_job_threads);
  212. PoolVector<Thread *>::Write threads_wb = threads.write();
  213. PoolVector<CVTTCompressionRowTask>::Read tasks_rb = tasks.read();
  214. job_queue.job_tasks = &tasks_rb[0];
  215. job_queue.current_task.set(0);
  216. job_queue.num_tasks = static_cast<uint32_t>(tasks.size());
  217. for (int i = 0; i < num_job_threads; i++) {
  218. threads_wb[i] = memnew(Thread);
  219. threads_wb[i]->start(_digest_job_queue, &job_queue);
  220. }
  221. _digest_job_queue(&job_queue);
  222. for (int i = 0; i < num_job_threads; i++) {
  223. threads_wb[i]->wait_to_finish();
  224. memdelete(threads_wb[i]);
  225. }
  226. }
  227. p_image->create(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  228. }
  229. void image_decompress_cvtt(Image *p_image) {
  230. Image::Format target_format;
  231. bool is_signed = false;
  232. bool is_hdr = false;
  233. Image::Format input_format = p_image->get_format();
  234. switch (input_format) {
  235. case Image::FORMAT_BPTC_RGBA:
  236. target_format = Image::FORMAT_RGBA8;
  237. break;
  238. case Image::FORMAT_BPTC_RGBF:
  239. case Image::FORMAT_BPTC_RGBFU:
  240. target_format = Image::FORMAT_RGBH;
  241. is_signed = (input_format == Image::FORMAT_BPTC_RGBF);
  242. is_hdr = true;
  243. break;
  244. default:
  245. return; // Invalid input format
  246. };
  247. int w = p_image->get_width();
  248. int h = p_image->get_height();
  249. PoolVector<uint8_t>::Read rb = p_image->get_data().read();
  250. PoolVector<uint8_t> data;
  251. int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  252. int mm_count = p_image->get_mipmap_count();
  253. data.resize(target_size);
  254. PoolVector<uint8_t>::Write wb = data.write();
  255. int bytes_per_pixel = is_hdr ? 6 : 4;
  256. int dst_ofs = 0;
  257. for (int i = 0; i <= mm_count; i++) {
  258. int src_ofs = p_image->get_mipmap_offset(i);
  259. const uint8_t *in_bytes = &rb[src_ofs];
  260. uint8_t *out_bytes = &wb[dst_ofs];
  261. cvtt::PixelBlockU8 output_blocks_ldr[cvtt::NumParallelBlocks];
  262. cvtt::PixelBlockF16 output_blocks_hdr[cvtt::NumParallelBlocks];
  263. for (int y_start = 0; y_start < h; y_start += 4) {
  264. int y_end = y_start + 4;
  265. for (int x_start = 0; x_start < w; x_start += 4 * cvtt::NumParallelBlocks) {
  266. int x_end = x_start + 4 * cvtt::NumParallelBlocks;
  267. uint8_t input_blocks[16 * cvtt::NumParallelBlocks];
  268. memset(input_blocks, 0, sizeof(input_blocks));
  269. unsigned int num_real_blocks = ((w - x_start) + 3) / 4;
  270. if (num_real_blocks > cvtt::NumParallelBlocks) {
  271. num_real_blocks = cvtt::NumParallelBlocks;
  272. }
  273. memcpy(input_blocks, in_bytes, 16 * num_real_blocks);
  274. in_bytes += 16 * num_real_blocks;
  275. if (is_hdr) {
  276. if (is_signed) {
  277. cvtt::Kernels::DecodeBC6HS(output_blocks_hdr, input_blocks);
  278. } else {
  279. cvtt::Kernels::DecodeBC6HU(output_blocks_hdr, input_blocks);
  280. }
  281. } else {
  282. cvtt::Kernels::DecodeBC7(output_blocks_ldr, input_blocks);
  283. }
  284. for (int y = y_start; y < y_end; y++) {
  285. int first_input_element = (y - y_start) * 4;
  286. uint8_t *row_start;
  287. if (y >= h) {
  288. row_start = out_bytes + (h - 1) * (w * bytes_per_pixel);
  289. } else {
  290. row_start = out_bytes + y * (w * bytes_per_pixel);
  291. }
  292. for (int x = x_start; x < x_end; x++) {
  293. uint8_t *pixel_start;
  294. if (x >= w) {
  295. pixel_start = row_start + (w - 1) * bytes_per_pixel;
  296. } else {
  297. pixel_start = row_start + x * bytes_per_pixel;
  298. }
  299. int block_index = (x - x_start) / 4;
  300. int block_element = (x - x_start) % 4 + first_input_element;
  301. if (is_hdr) {
  302. memcpy(pixel_start, output_blocks_hdr[block_index].m_pixels[block_element], bytes_per_pixel);
  303. } else {
  304. memcpy(pixel_start, output_blocks_ldr[block_index].m_pixels[block_element], bytes_per_pixel);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. dst_ofs += w * h * bytes_per_pixel;
  311. w >>= 1;
  312. h >>= 1;
  313. }
  314. rb.release();
  315. wb.release();
  316. p_image->create(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  317. }