image_compress_cvtt.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/object/worker_thread_pool.h"
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include "core/templates/safe_refcount.h"
  35. #include <ConvectionKernels.h>
  36. struct CVTTCompressionJobParams {
  37. bool is_hdr = false;
  38. bool is_signed = false;
  39. int bytes_per_pixel = 0;
  40. cvtt::BC7EncodingPlan bc7_plan;
  41. cvtt::Options options;
  42. };
  43. struct CVTTCompressionRowTask {
  44. const uint8_t *in_mm_bytes = nullptr;
  45. uint8_t *out_mm_bytes = nullptr;
  46. int y_start = 0;
  47. int width = 0;
  48. int height = 0;
  49. };
  50. struct CVTTCompressionJobQueue {
  51. CVTTCompressionJobParams job_params;
  52. const CVTTCompressionRowTask *job_tasks = nullptr;
  53. uint32_t num_tasks = 0;
  54. SafeNumeric<uint32_t> current_task;
  55. };
  56. static void _digest_row_task(const CVTTCompressionJobParams &p_job_params, const CVTTCompressionRowTask &p_row_task) {
  57. const uint8_t *in_bytes = p_row_task.in_mm_bytes;
  58. uint8_t *out_bytes = p_row_task.out_mm_bytes;
  59. int w = p_row_task.width;
  60. int h = p_row_task.height;
  61. int y_start = p_row_task.y_start;
  62. int y_end = y_start + 4;
  63. int bytes_per_pixel = p_job_params.bytes_per_pixel;
  64. bool is_hdr = p_job_params.is_hdr;
  65. bool is_signed = p_job_params.is_signed;
  66. cvtt::PixelBlockU8 input_blocks_ldr[cvtt::NumParallelBlocks];
  67. cvtt::PixelBlockF16 input_blocks_hdr[cvtt::NumParallelBlocks];
  68. for (int x_start = 0; x_start < w; x_start += 4 * cvtt::NumParallelBlocks) {
  69. int x_end = x_start + 4 * cvtt::NumParallelBlocks;
  70. for (int y = y_start; y < y_end; y++) {
  71. int first_input_element = (y - y_start) * 4;
  72. const uint8_t *row_start;
  73. if (y >= h) {
  74. row_start = in_bytes + (h - 1) * (w * bytes_per_pixel);
  75. } else {
  76. row_start = in_bytes + y * (w * bytes_per_pixel);
  77. }
  78. for (int x = x_start; x < x_end; x++) {
  79. const uint8_t *pixel_start;
  80. if (x >= w) {
  81. pixel_start = row_start + (w - 1) * bytes_per_pixel;
  82. } else {
  83. pixel_start = row_start + x * bytes_per_pixel;
  84. }
  85. int block_index = (x - x_start) / 4;
  86. int block_element = (x - x_start) % 4 + first_input_element;
  87. if (is_hdr) {
  88. memcpy(input_blocks_hdr[block_index].m_pixels[block_element], pixel_start, bytes_per_pixel);
  89. input_blocks_hdr[block_index].m_pixels[block_element][3] = 0x3c00; // 1.0 (unused)
  90. } else {
  91. memcpy(input_blocks_ldr[block_index].m_pixels[block_element], pixel_start, bytes_per_pixel);
  92. }
  93. }
  94. }
  95. uint8_t output_blocks[16 * cvtt::NumParallelBlocks];
  96. if (is_hdr) {
  97. if (is_signed) {
  98. cvtt::Kernels::EncodeBC6HS(output_blocks, input_blocks_hdr, p_job_params.options);
  99. } else {
  100. cvtt::Kernels::EncodeBC6HU(output_blocks, input_blocks_hdr, p_job_params.options);
  101. }
  102. } else {
  103. cvtt::Kernels::EncodeBC7(output_blocks, input_blocks_ldr, p_job_params.options, p_job_params.bc7_plan);
  104. }
  105. unsigned int num_real_blocks = ((w - x_start) + 3) / 4;
  106. if (num_real_blocks > cvtt::NumParallelBlocks) {
  107. num_real_blocks = cvtt::NumParallelBlocks;
  108. }
  109. memcpy(out_bytes, output_blocks, 16 * num_real_blocks);
  110. out_bytes += 16 * num_real_blocks;
  111. }
  112. }
  113. static void _digest_job_queue(void *p_job_queue, uint32_t p_index) {
  114. CVTTCompressionJobQueue *job_queue = static_cast<CVTTCompressionJobQueue *>(p_job_queue);
  115. uint32_t num_tasks = job_queue->num_tasks;
  116. uint32_t total_threads = WorkerThreadPool::get_singleton()->get_thread_count();
  117. uint32_t start = p_index * num_tasks / total_threads;
  118. uint32_t end = (p_index + 1 == total_threads) ? num_tasks : ((p_index + 1) * num_tasks / total_threads);
  119. for (uint32_t i = start; i < end; i++) {
  120. _digest_row_task(job_queue->job_params, job_queue->job_tasks[i]);
  121. }
  122. }
  123. void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels) {
  124. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  125. if (p_image->is_compressed()) {
  126. return; //do not compress, already compressed
  127. }
  128. int w = p_image->get_width();
  129. int h = p_image->get_height();
  130. bool is_ldr = (p_image->get_format() <= Image::FORMAT_RGBA8);
  131. bool is_hdr = (p_image->get_format() >= Image::FORMAT_RF) && (p_image->get_format() <= Image::FORMAT_RGBE9995);
  132. if (!is_ldr && !is_hdr) {
  133. return; // Not a usable source format
  134. }
  135. cvtt::Options options;
  136. uint32_t flags = cvtt::Flags::Default;
  137. flags |= cvtt::Flags::BC7_RespectPunchThrough;
  138. if (p_channels == Image::USED_CHANNELS_RG) { //guessing this is a normal map
  139. flags |= cvtt::Flags::Uniform;
  140. }
  141. options.flags = flags;
  142. Image::Format target_format = Image::FORMAT_BPTC_RGBA;
  143. bool is_signed = false;
  144. if (is_hdr) {
  145. if (p_image->get_format() != Image::FORMAT_RGBH) {
  146. p_image->convert(Image::FORMAT_RGBH);
  147. }
  148. is_signed = p_image->detect_signed();
  149. target_format = is_signed ? Image::FORMAT_BPTC_RGBF : Image::FORMAT_BPTC_RGBFU;
  150. } else {
  151. p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
  152. }
  153. const uint8_t *rb = p_image->get_data().ptr();
  154. Vector<uint8_t> data;
  155. int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  156. int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(w, h, target_format) : 0;
  157. data.resize(target_size);
  158. int shift = Image::get_format_pixel_rshift(target_format);
  159. uint8_t *wb = data.ptrw();
  160. int64_t dst_ofs = 0;
  161. CVTTCompressionJobQueue job_queue;
  162. job_queue.job_params.is_hdr = is_hdr;
  163. job_queue.job_params.is_signed = is_signed;
  164. job_queue.job_params.options = options;
  165. job_queue.job_params.bytes_per_pixel = is_hdr ? 6 : 4;
  166. cvtt::Kernels::ConfigureBC7EncodingPlanFromQuality(job_queue.job_params.bc7_plan, 5);
  167. // Amdahl's law (Wikipedia)
  168. // If a program needs 20 hours to complete using a single thread, but a one-hour portion of the program cannot be parallelized,
  169. // therefore only the remaining 19 hours (p = 0.95) of execution time can be parallelized, then regardless of how many threads are devoted
  170. // to a parallelized execution of this program, the minimum execution time cannot be less than one hour.
  171. //
  172. // The number of executions with different inputs can be increased while the latency is the same.
  173. Vector<CVTTCompressionRowTask> tasks;
  174. for (int i = 0; i <= mm_count; i++) {
  175. int bw = w % 4 != 0 ? w + (4 - w % 4) : w;
  176. int bh = h % 4 != 0 ? h + (4 - h % 4) : h;
  177. int64_t src_ofs = p_image->get_mipmap_offset(i);
  178. const uint8_t *in_bytes = &rb[src_ofs];
  179. uint8_t *out_bytes = &wb[dst_ofs];
  180. for (int y_start = 0; y_start < h; y_start += 4) {
  181. CVTTCompressionRowTask row_task;
  182. row_task.width = w;
  183. row_task.height = h;
  184. row_task.y_start = y_start;
  185. row_task.in_mm_bytes = in_bytes;
  186. row_task.out_mm_bytes = out_bytes;
  187. tasks.push_back(row_task);
  188. out_bytes += 16 * (bw / 4);
  189. }
  190. dst_ofs += (MAX(4, bw) * MAX(4, bh)) >> shift;
  191. w = MAX(w / 2, 1);
  192. h = MAX(h / 2, 1);
  193. }
  194. const CVTTCompressionRowTask *tasks_rb = tasks.ptr();
  195. job_queue.job_tasks = &tasks_rb[0];
  196. job_queue.num_tasks = static_cast<uint32_t>(tasks.size());
  197. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&_digest_job_queue, &job_queue, WorkerThreadPool::get_singleton()->get_thread_count(), -1, true, SNAME("CVTT Compress"));
  198. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  199. p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  200. print_verbose(vformat("CVTT: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
  201. }
  202. void image_decompress_cvtt(Image *p_image) {
  203. Image::Format target_format;
  204. bool is_signed = false;
  205. bool is_hdr = false;
  206. Image::Format input_format = p_image->get_format();
  207. switch (input_format) {
  208. case Image::FORMAT_BPTC_RGBA:
  209. target_format = Image::FORMAT_RGBA8;
  210. break;
  211. case Image::FORMAT_BPTC_RGBF:
  212. case Image::FORMAT_BPTC_RGBFU:
  213. target_format = Image::FORMAT_RGBH;
  214. is_signed = (input_format == Image::FORMAT_BPTC_RGBF);
  215. is_hdr = true;
  216. break;
  217. default:
  218. return; // Invalid input format
  219. };
  220. int w = p_image->get_width();
  221. int h = p_image->get_height();
  222. const uint8_t *rb = p_image->get_data().ptr();
  223. Vector<uint8_t> data;
  224. int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  225. int mm_count = p_image->get_mipmap_count();
  226. data.resize(target_size);
  227. uint8_t *wb = data.ptrw();
  228. int bytes_per_pixel = is_hdr ? 6 : 4;
  229. int64_t dst_ofs = 0;
  230. for (int i = 0; i <= mm_count; i++) {
  231. int64_t src_ofs = p_image->get_mipmap_offset(i);
  232. const uint8_t *in_bytes = &rb[src_ofs];
  233. uint8_t *out_bytes = &wb[dst_ofs];
  234. cvtt::PixelBlockU8 output_blocks_ldr[cvtt::NumParallelBlocks];
  235. cvtt::PixelBlockF16 output_blocks_hdr[cvtt::NumParallelBlocks];
  236. for (int y_start = 0; y_start < h; y_start += 4) {
  237. int y_end = y_start + 4;
  238. for (int x_start = 0; x_start < w; x_start += 4 * cvtt::NumParallelBlocks) {
  239. uint8_t input_blocks[16 * cvtt::NumParallelBlocks];
  240. memset(input_blocks, 0, sizeof(input_blocks));
  241. unsigned int num_real_blocks = ((w - x_start) + 3) / 4;
  242. if (num_real_blocks > cvtt::NumParallelBlocks) {
  243. num_real_blocks = cvtt::NumParallelBlocks;
  244. }
  245. memcpy(input_blocks, in_bytes, 16 * num_real_blocks);
  246. in_bytes += 16 * num_real_blocks;
  247. int x_end = x_start + 4 * num_real_blocks;
  248. if (is_hdr) {
  249. if (is_signed) {
  250. cvtt::Kernels::DecodeBC6HS(output_blocks_hdr, input_blocks);
  251. } else {
  252. cvtt::Kernels::DecodeBC6HU(output_blocks_hdr, input_blocks);
  253. }
  254. } else {
  255. cvtt::Kernels::DecodeBC7(output_blocks_ldr, input_blocks);
  256. }
  257. for (int y = y_start; y < y_end; y++) {
  258. int first_input_element = (y - y_start) * 4;
  259. uint8_t *row_start;
  260. if (y >= h) {
  261. row_start = out_bytes + (h - 1) * (w * bytes_per_pixel);
  262. } else {
  263. row_start = out_bytes + y * (w * bytes_per_pixel);
  264. }
  265. for (int x = x_start; x < x_end; x++) {
  266. uint8_t *pixel_start;
  267. if (x >= w) {
  268. pixel_start = row_start + (w - 1) * bytes_per_pixel;
  269. } else {
  270. pixel_start = row_start + x * bytes_per_pixel;
  271. }
  272. int block_index = (x - x_start) / 4;
  273. int block_element = (x - x_start) % 4 + first_input_element;
  274. if (is_hdr) {
  275. memcpy(pixel_start, output_blocks_hdr[block_index].m_pixels[block_element], bytes_per_pixel);
  276. } else {
  277. memcpy(pixel_start, output_blocks_ldr[block_index].m_pixels[block_element], bytes_per_pixel);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. dst_ofs += w * h * bytes_per_pixel;
  284. w >>= 1;
  285. h >>= 1;
  286. }
  287. p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  288. }