image_compress_cvtt.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. Vector<uint8_t> in_mm;
  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.ptr();
  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. if (w % 4 != 0 || h % 4 != 0) {
  131. w = w <= 2 ? w : (w + 3) & ~3;
  132. h = h <= 2 ? h : (h + 3) & ~3;
  133. }
  134. bool is_ldr = (p_image->get_format() <= Image::FORMAT_RGBA8);
  135. bool is_hdr = (p_image->get_format() >= Image::FORMAT_RF) && (p_image->get_format() <= Image::FORMAT_RGBE9995);
  136. if (!is_ldr && !is_hdr) {
  137. return; // Not a usable source format
  138. }
  139. cvtt::Options options;
  140. uint32_t flags = cvtt::Flags::Default;
  141. flags |= cvtt::Flags::BC7_RespectPunchThrough;
  142. if (p_channels == Image::USED_CHANNELS_RG) { //guessing this is a normal map
  143. flags |= cvtt::Flags::Uniform;
  144. }
  145. options.flags = flags;
  146. Image::Format target_format = Image::FORMAT_BPTC_RGBA;
  147. bool is_signed = false;
  148. if (is_hdr) {
  149. if (p_image->get_format() != Image::FORMAT_RGBH) {
  150. p_image->convert(Image::FORMAT_RGBH);
  151. }
  152. is_signed = p_image->detect_signed();
  153. target_format = is_signed ? Image::FORMAT_BPTC_RGBF : Image::FORMAT_BPTC_RGBFU;
  154. } else {
  155. p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
  156. }
  157. Vector<uint8_t> data;
  158. int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  159. int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(w, h, target_format) : 0;
  160. data.resize(target_size);
  161. int shift = Image::get_format_pixel_rshift(target_format);
  162. uint8_t *wb = data.ptrw();
  163. int64_t dst_ofs = 0;
  164. CVTTCompressionJobQueue job_queue;
  165. job_queue.job_params.is_hdr = is_hdr;
  166. job_queue.job_params.is_signed = is_signed;
  167. job_queue.job_params.options = options;
  168. job_queue.job_params.bytes_per_pixel = is_hdr ? 6 : 4;
  169. cvtt::Kernels::ConfigureBC7EncodingPlanFromQuality(job_queue.job_params.bc7_plan, 5);
  170. // Amdahl's law (Wikipedia)
  171. // If a program needs 20 hours to complete using a single thread, but a one-hour portion of the program cannot be parallelized,
  172. // therefore only the remaining 19 hours (p = 0.95) of execution time can be parallelized, then regardless of how many threads are devoted
  173. // to a parallelized execution of this program, the minimum execution time cannot be less than one hour.
  174. //
  175. // The number of executions with different inputs can be increased while the latency is the same.
  176. Vector<CVTTCompressionRowTask> tasks;
  177. for (int i = 0; i <= mm_count; i++) {
  178. Vector<uint8_t> in_data;
  179. int width, height;
  180. Image::get_image_mipmap_offset_and_dimensions(w, h, target_format, i, width, height);
  181. int bw = width % 4 != 0 ? width + (4 - width % 4) : width;
  182. int bh = height % 4 != 0 ? height + (4 - height % 4) : height;
  183. int64_t src_mip_ofs, src_mip_size;
  184. int src_mip_w, src_mip_h;
  185. p_image->get_mipmap_offset_size_and_dimensions(i, src_mip_ofs, src_mip_size, src_mip_w, src_mip_h);
  186. // Pad textures to nearest block by smearing.
  187. if (width != src_mip_w || height != src_mip_h) {
  188. const uint8_t *src_mip_read = p_image->ptr() + src_mip_ofs;
  189. // Reserve the buffer for padded image data.
  190. int px_size = Image::get_format_pixel_size(p_image->get_format());
  191. in_data.resize(width * height * px_size);
  192. uint8_t *ptrw = in_data.ptrw();
  193. int x = 0, y = 0;
  194. for (y = 0; y < src_mip_h; y++) {
  195. for (x = 0; x < src_mip_w; x++) {
  196. memcpy(ptrw + (width * y + x) * px_size, src_mip_read + (src_mip_w * y + x) * px_size, px_size);
  197. }
  198. // First, smear in x.
  199. for (; x < width; x++) {
  200. memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - 1) * px_size, px_size);
  201. }
  202. }
  203. // Then, smear in y.
  204. for (; y < height; y++) {
  205. for (x = 0; x < width; x++) {
  206. memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - width) * px_size, px_size);
  207. }
  208. }
  209. } else {
  210. // Create a buffer filled with the source mip layer data.
  211. in_data.resize(src_mip_size);
  212. memcpy(in_data.ptrw(), p_image->ptr() + src_mip_ofs, src_mip_size);
  213. }
  214. //const uint8_t *in_bytes = &rb[src_ofs];
  215. uint8_t *out_bytes = &wb[dst_ofs];
  216. for (int y_start = 0; y_start < height; y_start += 4) {
  217. CVTTCompressionRowTask row_task;
  218. row_task.width = width;
  219. row_task.height = height;
  220. row_task.y_start = y_start;
  221. row_task.in_mm = in_data;
  222. row_task.out_mm_bytes = out_bytes;
  223. tasks.push_back(row_task);
  224. out_bytes += 16 * (bw / 4);
  225. }
  226. dst_ofs += (MAX(4, bw) * MAX(4, bh)) >> shift;
  227. }
  228. const CVTTCompressionRowTask *tasks_rb = tasks.ptr();
  229. job_queue.job_tasks = &tasks_rb[0];
  230. job_queue.num_tasks = static_cast<uint32_t>(tasks.size());
  231. 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"));
  232. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  233. p_image->set_data(w, h, p_image->has_mipmaps(), target_format, data);
  234. print_verbose(vformat("CVTT: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
  235. }
  236. void image_decompress_cvtt(Image *p_image) {
  237. Image::Format target_format;
  238. bool is_signed = false;
  239. bool is_hdr = false;
  240. Image::Format input_format = p_image->get_format();
  241. switch (input_format) {
  242. case Image::FORMAT_BPTC_RGBA:
  243. target_format = Image::FORMAT_RGBA8;
  244. break;
  245. case Image::FORMAT_BPTC_RGBF:
  246. case Image::FORMAT_BPTC_RGBFU:
  247. target_format = Image::FORMAT_RGBH;
  248. is_signed = (input_format == Image::FORMAT_BPTC_RGBF);
  249. is_hdr = true;
  250. break;
  251. default:
  252. return; // Invalid input format
  253. };
  254. int w = p_image->get_width();
  255. int h = p_image->get_height();
  256. const uint8_t *rb = p_image->get_data().ptr();
  257. Vector<uint8_t> data;
  258. int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  259. int mm_count = p_image->get_mipmap_count();
  260. data.resize(target_size);
  261. uint8_t *wb = data.ptrw();
  262. int bytes_per_pixel = is_hdr ? 6 : 4;
  263. int64_t dst_ofs = 0;
  264. for (int i = 0; i <= mm_count; i++) {
  265. int64_t src_ofs = p_image->get_mipmap_offset(i);
  266. const uint8_t *in_bytes = &rb[src_ofs];
  267. uint8_t *out_bytes = &wb[dst_ofs];
  268. cvtt::PixelBlockU8 output_blocks_ldr[cvtt::NumParallelBlocks];
  269. cvtt::PixelBlockF16 output_blocks_hdr[cvtt::NumParallelBlocks];
  270. for (int y_start = 0; y_start < h; y_start += 4) {
  271. int y_end = y_start + 4;
  272. for (int x_start = 0; x_start < w; x_start += 4 * cvtt::NumParallelBlocks) {
  273. uint8_t input_blocks[16 * cvtt::NumParallelBlocks];
  274. memset(input_blocks, 0, sizeof(input_blocks));
  275. unsigned int num_real_blocks = ((w - x_start) + 3) / 4;
  276. if (num_real_blocks > cvtt::NumParallelBlocks) {
  277. num_real_blocks = cvtt::NumParallelBlocks;
  278. }
  279. memcpy(input_blocks, in_bytes, 16 * num_real_blocks);
  280. in_bytes += 16 * num_real_blocks;
  281. int x_end = x_start + 4 * num_real_blocks;
  282. if (is_hdr) {
  283. if (is_signed) {
  284. cvtt::Kernels::DecodeBC6HS(output_blocks_hdr, input_blocks);
  285. } else {
  286. cvtt::Kernels::DecodeBC6HU(output_blocks_hdr, input_blocks);
  287. }
  288. } else {
  289. cvtt::Kernels::DecodeBC7(output_blocks_ldr, input_blocks);
  290. }
  291. for (int y = y_start; y < y_end; y++) {
  292. int first_input_element = (y - y_start) * 4;
  293. uint8_t *row_start;
  294. if (y >= h) {
  295. row_start = out_bytes + (h - 1) * (w * bytes_per_pixel);
  296. } else {
  297. row_start = out_bytes + y * (w * bytes_per_pixel);
  298. }
  299. for (int x = x_start; x < x_end; x++) {
  300. uint8_t *pixel_start;
  301. if (x >= w) {
  302. pixel_start = row_start + (w - 1) * bytes_per_pixel;
  303. } else {
  304. pixel_start = row_start + x * bytes_per_pixel;
  305. }
  306. int block_index = (x - x_start) / 4;
  307. int block_element = (x - x_start) % 4 + first_input_element;
  308. if (is_hdr) {
  309. memcpy(pixel_start, output_blocks_hdr[block_index].m_pixels[block_element], bytes_per_pixel);
  310. } else {
  311. memcpy(pixel_start, output_blocks_ldr[block_index].m_pixels[block_element], bytes_per_pixel);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. dst_ofs += w * h * bytes_per_pixel;
  318. w >>= 1;
  319. h >>= 1;
  320. }
  321. p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  322. }