1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/**************************************************************************/
/* image_compress_betsy.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "image_compress_betsy.h"
#include "servers/rendering/rendering_device_binds.h"
#include "servers/rendering/rendering_server_default.h"
#if defined(VULKAN_ENABLED)
#include "drivers/vulkan/rendering_context_driver_vulkan.h"
#endif
#include "bc6h.glsl.gen.h"
struct BC6PushConstant {
float sizeX;
float sizeY;
uint32_t padding[2];
};
static int get_next_multiple(int n, int m) {
return n + (m - (n % m));
}
static bool is_image_signed(const Image *r_img) {
if (r_img->get_format() >= Image::FORMAT_RH && r_img->get_format() <= Image::FORMAT_RGBAH) {
const uint16_t *img_data = reinterpret_cast<const uint16_t *>(r_img->ptr());
const uint64_t img_size = r_img->get_data_size() / 2;
for (uint64_t i = 0; i < img_size; i++) {
if ((img_data[i] & 0x8000) != 0 && (img_data[i] & 0x7fff) != 0) {
return true;
}
}
} else if (r_img->get_format() >= Image::FORMAT_RF && r_img->get_format() <= Image::FORMAT_RGBAF) {
const uint32_t *img_data = reinterpret_cast<const uint32_t *>(r_img->ptr());
const uint64_t img_size = r_img->get_data_size() / 4;
for (uint64_t i = 0; i < img_size; i++) {
if ((img_data[i] & 0x80000000) != 0 && (img_data[i] & 0x7fffffff) != 0) {
return true;
}
}
}
return false;
}
Error _compress_betsy(BetsyFormat p_format, Image *r_img) {
uint64_t start_time = OS::get_singleton()->get_ticks_msec();
if (r_img->is_compressed()) {
return ERR_INVALID_DATA;
}
ERR_FAIL_COND_V_MSG(r_img->get_format() < Image::FORMAT_RF || r_img->get_format() > Image::FORMAT_RGBE9995, ERR_INVALID_DATA, "Image is not an HDR image.");
Error err = OK;
// Create local RD.
RenderingContextDriver *rcd = nullptr;
RenderingDevice *rd = RenderingServer::get_singleton()->create_local_rendering_device();
if (rd == nullptr) {
#if defined(RD_ENABLED)
#if defined(VULKAN_ENABLED)
rcd = memnew(RenderingContextDriverVulkan);
rd = memnew(RenderingDevice);
#endif
#endif
if (rcd != nullptr && rd != nullptr) {
err = rcd->initialize();
if (err == OK) {
err = rd->initialize(rcd);
}
if (err != OK) {
memdelete(rd);
memdelete(rcd);
rd = nullptr;
rcd = nullptr;
}
}
}
ERR_FAIL_NULL_V_MSG(rd, err, "Unable to create a local RenderingDevice.");
Ref<RDShaderFile> compute_shader;
compute_shader.instantiate();
// Destination format.
Image::Format dest_format = Image::FORMAT_MAX;
String version = "";
switch (p_format) {
case BETSY_FORMAT_BC6: {
err = compute_shader->parse_versions_from_text(bc6h_shader_glsl);
if (is_image_signed(r_img)) {
dest_format = Image::FORMAT_BPTC_RGBF;
version = "signed";
} else {
dest_format = Image::FORMAT_BPTC_RGBFU;
version = "unsigned";
}
} break;
default:
err = ERR_INVALID_PARAMETER;
break;
}
if (err != OK) {
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return err;
}
// Compile the shader, return early if invalid.
RID shader = rd->shader_create_from_spirv(compute_shader->get_spirv_stages(version));
if (shader.is_null()) {
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return err;
}
RID pipeline = rd->compute_pipeline_create(shader);
// src_texture format information.
RD::TextureFormat src_texture_format;
{
src_texture_format.array_layers = 1;
src_texture_format.depth = 1;
src_texture_format.mipmaps = 1;
src_texture_format.texture_type = RD::TEXTURE_TYPE_2D;
src_texture_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
}
switch (r_img->get_format()) {
case Image::FORMAT_RH:
src_texture_format.format = RD::DATA_FORMAT_R16_SFLOAT;
break;
case Image::FORMAT_RGH:
src_texture_format.format = RD::DATA_FORMAT_R16G16_SFLOAT;
break;
case Image::FORMAT_RGBH:
r_img->convert(Image::FORMAT_RGBAH);
src_texture_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
break;
case Image::FORMAT_RGBAH:
src_texture_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
break;
case Image::FORMAT_RF:
src_texture_format.format = RD::DATA_FORMAT_R32_SFLOAT;
break;
case Image::FORMAT_RGF:
src_texture_format.format = RD::DATA_FORMAT_R32G32_SFLOAT;
break;
case Image::FORMAT_RGBF:
r_img->convert(Image::FORMAT_RGBAF);
src_texture_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
break;
case Image::FORMAT_RGBAF:
src_texture_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
break;
case Image::FORMAT_RGBE9995:
src_texture_format.format = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
break;
default: {
rd->free(shader);
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return err;
}
}
// Create the sampler state.
RD::SamplerState src_sampler_state;
{
src_sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
src_sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
src_sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
src_sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
src_sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
}
RID src_sampler = rd->sampler_create(src_sampler_state);
// For the destination format just copy the source format and change the usage bits.
RD::TextureFormat dst_texture_format = src_texture_format;
dst_texture_format.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
dst_texture_format.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
const int mip_count = r_img->get_mipmap_count() + 1;
// Container for the compressed data.
Vector<uint8_t> dst_data;
dst_data.resize(Image::get_image_data_size(r_img->get_width(), r_img->get_height(), dest_format, r_img->has_mipmaps()));
uint8_t *dst_data_ptr = dst_data.ptrw();
Vector<Vector<uint8_t>> src_images;
src_images.push_back(Vector<uint8_t>());
Vector<uint8_t> *src_image_ptr = src_images.ptrw();
// Compress each mipmap.
for (int i = 0; i < mip_count; i++) {
int64_t ofs, size;
int width, height;
r_img->get_mipmap_offset_size_and_dimensions(i, ofs, size, width, height);
// Set the source texture width and size.
src_texture_format.height = height;
src_texture_format.width = width;
// Set the destination texture width and size.
dst_texture_format.height = (height + 3) >> 2;
dst_texture_format.width = (width + 3) >> 2;
// Create a buffer filled with the source mip layer data.
src_image_ptr[0].resize(size);
memcpy(src_image_ptr[0].ptrw(), r_img->ptr() + ofs, size);
// Create the textures on the GPU.
RID src_texture = rd->texture_create(src_texture_format, RD::TextureView(), src_images);
RID dst_texture = rd->texture_create(dst_texture_format, RD::TextureView());
if (dest_format == Image::FORMAT_BPTC_RGBFU || dest_format == Image::FORMAT_BPTC_RGBF) {
BC6PushConstant push_constant;
push_constant.sizeX = 1.0f / width;
push_constant.sizeY = 1.0f / height;
push_constant.padding[0] = 0;
push_constant.padding[1] = 0;
Vector<RD::Uniform> uniforms;
{
{
RD::Uniform u;
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
u.binding = 0;
u.append_id(src_sampler);
u.append_id(src_texture);
uniforms.push_back(u);
}
{
RD::Uniform u;
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
u.binding = 1;
u.append_id(dst_texture);
uniforms.push_back(u);
}
}
RID uniform_set = rd->uniform_set_create(uniforms, shader, 0);
RD::ComputeListID compute_list = rd->compute_list_begin();
rd->compute_list_bind_compute_pipeline(compute_list, pipeline);
rd->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(BC6PushConstant));
rd->compute_list_dispatch(compute_list, get_next_multiple(width, 32) / 32, get_next_multiple(height, 32) / 32, 1);
rd->compute_list_end();
}
rd->submit();
rd->sync();
// Copy data from the GPU to the buffer.
const Vector<uint8_t> texture_data = rd->texture_get_data(dst_texture, 0);
int64_t dst_ofs = Image::get_image_mipmap_offset(r_img->get_width(), r_img->get_height(), dest_format, i);
memcpy(dst_data_ptr + dst_ofs, texture_data.ptr(), texture_data.size());
// Free the source and dest texture.
rd->free(dst_texture);
rd->free(src_texture);
}
src_images.clear();
// Set the compressed data to the image.
r_img->set_data(r_img->get_width(), r_img->get_height(), r_img->has_mipmaps(), dest_format, dst_data);
// Free the shader (dependencies will be cleared automatically).
rd->free(src_sampler);
rd->free(shader);
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
print_verbose(vformat("Betsy: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
return OK;
}
Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels) {
Image::Format format = r_img->get_format();
if (format >= Image::FORMAT_RF && format <= Image::FORMAT_RGBE9995) {
return _compress_betsy(BETSY_FORMAT_BC6, r_img);
}
return ERR_UNAVAILABLE;
}
|