Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/cvtt/image_compress_cvtt.cpp
10277 views
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
40
struct 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
48
struct CVTTCompressionRowTask {
49
Vector<uint8_t> in_mm;
50
uint8_t *out_mm_bytes = nullptr;
51
int y_start = 0;
52
int width = 0;
53
int height = 0;
54
};
55
56
struct 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
63
static 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.ptr();
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
132
static 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
144
void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels) {
145
uint64_t start_time = OS::get_singleton()->get_ticks_msec();
146
147
if (p_image->is_compressed()) {
148
return; //do not compress, already compressed
149
}
150
151
int w = p_image->get_width();
152
int h = p_image->get_height();
153
154
if (w % 4 != 0 || h % 4 != 0) {
155
w = w <= 2 ? w : (w + 3) & ~3;
156
h = h <= 2 ? h : (h + 3) & ~3;
157
}
158
159
bool is_ldr = (p_image->get_format() <= Image::FORMAT_RGBA8);
160
bool is_hdr = (p_image->get_format() >= Image::FORMAT_RF) && (p_image->get_format() <= Image::FORMAT_RGBE9995);
161
162
if (!is_ldr && !is_hdr) {
163
return; // Not a usable source format
164
}
165
166
cvtt::Options options;
167
uint32_t flags = cvtt::Flags::Default;
168
flags |= cvtt::Flags::BC7_RespectPunchThrough;
169
if (p_channels == Image::USED_CHANNELS_RG) { //guessing this is a normal map
170
flags |= cvtt::Flags::Uniform;
171
}
172
options.flags = flags;
173
174
Image::Format target_format = Image::FORMAT_BPTC_RGBA;
175
176
bool is_signed = false;
177
if (is_hdr) {
178
if (p_image->get_format() != Image::FORMAT_RGBH) {
179
p_image->convert(Image::FORMAT_RGBH);
180
}
181
182
is_signed = p_image->detect_signed();
183
target_format = is_signed ? Image::FORMAT_BPTC_RGBF : Image::FORMAT_BPTC_RGBFU;
184
} else {
185
p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
186
}
187
188
Vector<uint8_t> data;
189
int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
190
int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(w, h, target_format) : 0;
191
data.resize(target_size);
192
int shift = Image::get_format_pixel_rshift(target_format);
193
194
uint8_t *wb = data.ptrw();
195
196
int64_t dst_ofs = 0;
197
198
CVTTCompressionJobQueue job_queue;
199
job_queue.job_params.is_hdr = is_hdr;
200
job_queue.job_params.is_signed = is_signed;
201
job_queue.job_params.options = options;
202
job_queue.job_params.bytes_per_pixel = is_hdr ? 6 : 4;
203
cvtt::Kernels::ConfigureBC7EncodingPlanFromQuality(job_queue.job_params.bc7_plan, 5);
204
205
// Amdahl's law (Wikipedia)
206
// If a program needs 20 hours to complete using a single thread, but a one-hour portion of the program cannot be parallelized,
207
// therefore only the remaining 19 hours (p = 0.95) of execution time can be parallelized, then regardless of how many threads are devoted
208
// to a parallelized execution of this program, the minimum execution time cannot be less than one hour.
209
//
210
// The number of executions with different inputs can be increased while the latency is the same.
211
212
Vector<CVTTCompressionRowTask> tasks;
213
214
for (int i = 0; i <= mm_count; i++) {
215
Vector<uint8_t> in_data;
216
int width, height;
217
Image::get_image_mipmap_offset_and_dimensions(w, h, target_format, i, width, height);
218
219
int bw = width % 4 != 0 ? width + (4 - width % 4) : width;
220
int bh = height % 4 != 0 ? height + (4 - height % 4) : height;
221
222
int64_t src_mip_ofs, src_mip_size;
223
int src_mip_w, src_mip_h;
224
p_image->get_mipmap_offset_size_and_dimensions(i, src_mip_ofs, src_mip_size, src_mip_w, src_mip_h);
225
226
// Pad textures to nearest block by smearing.
227
if (width != src_mip_w || height != src_mip_h) {
228
const uint8_t *src_mip_read = p_image->ptr() + src_mip_ofs;
229
230
// Reserve the buffer for padded image data.
231
int px_size = Image::get_format_pixel_size(p_image->get_format());
232
in_data.resize(width * height * px_size);
233
uint8_t *ptrw = in_data.ptrw();
234
235
int x = 0, y = 0;
236
for (y = 0; y < src_mip_h; y++) {
237
for (x = 0; x < src_mip_w; x++) {
238
memcpy(ptrw + (width * y + x) * px_size, src_mip_read + (src_mip_w * y + x) * px_size, px_size);
239
}
240
241
// First, smear in x.
242
for (; x < width; x++) {
243
memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - 1) * px_size, px_size);
244
}
245
}
246
247
// Then, smear in y.
248
for (; y < height; y++) {
249
for (x = 0; x < width; x++) {
250
memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - width) * px_size, px_size);
251
}
252
}
253
} else {
254
// Create a buffer filled with the source mip layer data.
255
in_data.resize(src_mip_size);
256
memcpy(in_data.ptrw(), p_image->ptr() + src_mip_ofs, src_mip_size);
257
}
258
259
//const uint8_t *in_bytes = &rb[src_ofs];
260
uint8_t *out_bytes = &wb[dst_ofs];
261
262
for (int y_start = 0; y_start < height; y_start += 4) {
263
CVTTCompressionRowTask row_task;
264
row_task.width = width;
265
row_task.height = height;
266
row_task.y_start = y_start;
267
row_task.in_mm = in_data;
268
row_task.out_mm_bytes = out_bytes;
269
270
tasks.push_back(row_task);
271
272
out_bytes += 16 * (bw / 4);
273
}
274
275
dst_ofs += (MAX(4, bw) * MAX(4, bh)) >> shift;
276
}
277
278
const CVTTCompressionRowTask *tasks_rb = tasks.ptr();
279
280
job_queue.job_tasks = &tasks_rb[0];
281
job_queue.num_tasks = static_cast<uint32_t>(tasks.size());
282
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"));
283
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
284
285
p_image->set_data(w, h, p_image->has_mipmaps(), target_format, data);
286
287
print_verbose(vformat("CVTT: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
288
}
289
290
void image_decompress_cvtt(Image *p_image) {
291
Image::Format target_format;
292
bool is_signed = false;
293
bool is_hdr = false;
294
295
Image::Format input_format = p_image->get_format();
296
297
switch (input_format) {
298
case Image::FORMAT_BPTC_RGBA:
299
target_format = Image::FORMAT_RGBA8;
300
break;
301
case Image::FORMAT_BPTC_RGBF:
302
case Image::FORMAT_BPTC_RGBFU:
303
target_format = Image::FORMAT_RGBH;
304
is_signed = (input_format == Image::FORMAT_BPTC_RGBF);
305
is_hdr = true;
306
break;
307
default:
308
return; // Invalid input format
309
};
310
311
int w = p_image->get_width();
312
int h = p_image->get_height();
313
314
const uint8_t *rb = p_image->get_data().ptr();
315
316
Vector<uint8_t> data;
317
int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
318
int mm_count = p_image->get_mipmap_count();
319
data.resize(target_size);
320
321
uint8_t *wb = data.ptrw();
322
323
int bytes_per_pixel = is_hdr ? 6 : 4;
324
325
int64_t dst_ofs = 0;
326
327
for (int i = 0; i <= mm_count; i++) {
328
int64_t src_ofs = p_image->get_mipmap_offset(i);
329
330
const uint8_t *in_bytes = &rb[src_ofs];
331
uint8_t *out_bytes = &wb[dst_ofs];
332
333
cvtt::PixelBlockU8 output_blocks_ldr[cvtt::NumParallelBlocks];
334
cvtt::PixelBlockF16 output_blocks_hdr[cvtt::NumParallelBlocks];
335
336
for (int y_start = 0; y_start < h; y_start += 4) {
337
int y_end = y_start + 4;
338
339
for (int x_start = 0; x_start < w; x_start += 4 * cvtt::NumParallelBlocks) {
340
uint8_t input_blocks[16 * cvtt::NumParallelBlocks];
341
memset(input_blocks, 0, sizeof(input_blocks));
342
343
unsigned int num_real_blocks = ((w - x_start) + 3) / 4;
344
if (num_real_blocks > cvtt::NumParallelBlocks) {
345
num_real_blocks = cvtt::NumParallelBlocks;
346
}
347
348
memcpy(input_blocks, in_bytes, 16 * num_real_blocks);
349
in_bytes += 16 * num_real_blocks;
350
351
int x_end = x_start + 4 * num_real_blocks;
352
353
if (is_hdr) {
354
if (is_signed) {
355
cvtt::Kernels::DecodeBC6HS(output_blocks_hdr, input_blocks);
356
} else {
357
cvtt::Kernels::DecodeBC6HU(output_blocks_hdr, input_blocks);
358
}
359
} else {
360
cvtt::Kernels::DecodeBC7(output_blocks_ldr, input_blocks);
361
}
362
363
for (int y = y_start; y < y_end; y++) {
364
int first_input_element = (y - y_start) * 4;
365
uint8_t *row_start;
366
if (y >= h) {
367
row_start = out_bytes + (h - 1) * (w * bytes_per_pixel);
368
} else {
369
row_start = out_bytes + y * (w * bytes_per_pixel);
370
}
371
372
for (int x = x_start; x < x_end; x++) {
373
uint8_t *pixel_start;
374
if (x >= w) {
375
pixel_start = row_start + (w - 1) * bytes_per_pixel;
376
} else {
377
pixel_start = row_start + x * bytes_per_pixel;
378
}
379
380
int block_index = (x - x_start) / 4;
381
int block_element = (x - x_start) % 4 + first_input_element;
382
if (is_hdr) {
383
memcpy(pixel_start, output_blocks_hdr[block_index].m_pixels[block_element], bytes_per_pixel);
384
} else {
385
memcpy(pixel_start, output_blocks_ldr[block_index].m_pixels[block_element], bytes_per_pixel);
386
}
387
}
388
}
389
}
390
}
391
392
dst_ofs += w * h * bytes_per_pixel;
393
w >>= 1;
394
h >>= 1;
395
}
396
p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
397
}
398
399