image_compress_cvtt.cpp 13 KB

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