image_compress_cvtt.cpp 12 KB

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