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