Skip to content

Commit 2167157

Browse files
committed
Replace squish with bcdec for BC decompression
1 parent a0d1ba4 commit 2167157

40 files changed

+1550
-5138
lines changed

SConstruct

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ opts.Add(BoolVariable("builtin_pcre2_with_jit", "Use JIT compiler for the built-
299299
opts.Add(BoolVariable("builtin_recastnavigation", "Use the built-in Recast navigation library", True))
300300
opts.Add(BoolVariable("builtin_rvo2_2d", "Use the built-in RVO2 2D library", True))
301301
opts.Add(BoolVariable("builtin_rvo2_3d", "Use the built-in RVO2 3D library", True))
302-
opts.Add(BoolVariable("builtin_squish", "Use the built-in squish library", True))
303302
opts.Add(BoolVariable("builtin_xatlas", "Use the built-in xatlas library", True))
304303
opts.Add(BoolVariable("builtin_zlib", "Use the built-in zlib library", True))
305304
opts.Add(BoolVariable("builtin_zstd", "Use the built-in Zstd library", True))

modules/bcdec/SCsub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
from misc.utility.scons_hints import *
3+
4+
Import("env")
5+
Import("env_modules")
6+
7+
env_bcdec = env_modules.Clone()
8+
9+
# Godot source files
10+
env_bcdec.add_source_files(env.modules_sources, "*.cpp")
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**************************************************************************/
2+
/* image_decompress_bcdec.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_decompress_bcdec.h"
32+
33+
#include "core/os/os.h"
34+
#include "core/string/print_string.h"
35+
36+
#define BCDEC_IMPLEMENTATION
37+
#include "thirdparty/misc/bcdec.h"
38+
39+
inline void bcdec_bc6h_half_s(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
40+
bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, true);
41+
}
42+
43+
inline void bcdec_bc6h_half_u(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
44+
bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, false);
45+
}
46+
47+
static void decompress_image(BCdecFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {
48+
const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);
49+
uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);
50+
uint64_t src_pos = 0, dst_pos = 0;
51+
52+
#define DECOMPRESS_LOOP(func, block_size, color_bytesize, color_components) \
53+
for (uint64_t y = 0; y < height; y += 4) { \
54+
for (uint64_t x = 0; x < width; x += 4) { \
55+
func(&src_blocks[src_pos], &dec_blocks[dst_pos], width *color_components); \
56+
src_pos += block_size; \
57+
dst_pos += 4 * color_bytesize; \
58+
} \
59+
dst_pos += 3 * width * color_bytesize; \
60+
}
61+
62+
switch (format) {
63+
case BCdec_BC1: {
64+
DECOMPRESS_LOOP(bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 4)
65+
} break;
66+
case BCdec_BC2: {
67+
DECOMPRESS_LOOP(bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 4)
68+
} break;
69+
case BCdec_BC3: {
70+
DECOMPRESS_LOOP(bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 4)
71+
} break;
72+
case BCdec_BC4: {
73+
DECOMPRESS_LOOP(bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1)
74+
} break;
75+
case BCdec_BC5: {
76+
DECOMPRESS_LOOP(bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 2)
77+
} break;
78+
case BCdec_BC6U: {
79+
DECOMPRESS_LOOP(bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 3)
80+
} break;
81+
case BCdec_BC6S: {
82+
DECOMPRESS_LOOP(bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 3)
83+
} break;
84+
case BCdec_BC7: {
85+
DECOMPRESS_LOOP(bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 4)
86+
} break;
87+
}
88+
89+
#undef DECOMPRESS_LOOP
90+
}
91+
92+
void image_decompress_bcdec(Image *p_image) {
93+
uint64_t start_time = OS::get_singleton()->get_ticks_msec();
94+
95+
int w = p_image->get_width();
96+
int h = p_image->get_height();
97+
98+
Image::Format source_format = p_image->get_format();
99+
Image::Format target_format = Image::FORMAT_MAX;
100+
101+
BCdecFormat bcdec_format = BCdec_BC1;
102+
103+
switch (source_format) {
104+
case Image::FORMAT_DXT1:
105+
bcdec_format = BCdec_BC1;
106+
target_format = Image::FORMAT_RGBA8;
107+
break;
108+
109+
case Image::FORMAT_DXT3:
110+
bcdec_format = BCdec_BC2;
111+
target_format = Image::FORMAT_RGBA8;
112+
break;
113+
114+
case Image::FORMAT_DXT5:
115+
case Image::FORMAT_DXT5_RA_AS_RG:
116+
bcdec_format = BCdec_BC3;
117+
target_format = Image::FORMAT_RGBA8;
118+
break;
119+
120+
case Image::FORMAT_RGTC_R:
121+
bcdec_format = BCdec_BC4;
122+
target_format = Image::FORMAT_R8;
123+
break;
124+
125+
case Image::FORMAT_RGTC_RG:
126+
bcdec_format = BCdec_BC5;
127+
target_format = Image::FORMAT_RG8;
128+
break;
129+
130+
case Image::FORMAT_BPTC_RGBFU:
131+
bcdec_format = BCdec_BC6U;
132+
target_format = Image::FORMAT_RGBH;
133+
break;
134+
135+
case Image::FORMAT_BPTC_RGBF:
136+
bcdec_format = BCdec_BC6S;
137+
target_format = Image::FORMAT_RGBH;
138+
break;
139+
140+
case Image::FORMAT_BPTC_RGBA:
141+
bcdec_format = BCdec_BC7;
142+
target_format = Image::FORMAT_RGBA8;
143+
break;
144+
145+
default:
146+
ERR_FAIL_MSG("bcdec: Can't decompress unknown format: " + Image::get_format_name(source_format) + ".");
147+
break;
148+
}
149+
150+
int mm_count = p_image->get_mipmap_count();
151+
int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
152+
153+
Vector<uint8_t> data;
154+
data.resize(target_size);
155+
156+
const uint8_t *rb = p_image->get_data().ptr();
157+
uint8_t *wb = data.ptrw();
158+
159+
// Decompress mipmaps.
160+
for (int i = 0; i <= mm_count; i++) {
161+
int64_t src_ofs = 0, mipmap_size = 0;
162+
int mipmap_w = 0, mipmap_h = 0;
163+
p_image->get_mipmap_offset_size_and_dimensions(i, src_ofs, mipmap_size, mipmap_w, mipmap_h);
164+
165+
int64_t dst_ofs = Image::get_image_mipmap_offset(p_image->get_width(), p_image->get_height(), target_format, i);
166+
decompress_image(bcdec_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);
167+
168+
w >>= 1;
169+
h >>= 1;
170+
}
171+
172+
p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
173+
174+
// Swap channels if necessary.
175+
if (source_format == Image::FORMAT_DXT5_RA_AS_RG) {
176+
p_image->convert_ra_rgba8_to_rg();
177+
}
178+
179+
print_verbose(vformat("bcdec: Decompression of a %dx%d %s image with %d mipmaps took %d ms.",
180+
p_image->get_width(), p_image->get_height(), Image::get_format_name(source_format), p_image->get_mipmap_count(), OS::get_singleton()->get_ticks_msec() - start_time));
181+
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**************************************************************************/
2-
/* image_decompress_squish.h */
2+
/* image_decompress_bcdec.h */
33
/**************************************************************************/
44
/* This file is part of: */
55
/* GODOT ENGINE */
@@ -28,11 +28,22 @@
2828
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2929
/**************************************************************************/
3030

31-
#ifndef IMAGE_DECOMPRESS_SQUISH_H
32-
#define IMAGE_DECOMPRESS_SQUISH_H
31+
#ifndef IMAGE_DECOMPRESS_BCDEC_H
32+
#define IMAGE_DECOMPRESS_BCDEC_H
3333

3434
#include "core/io/image.h"
3535

36-
void image_decompress_squish(Image *p_image);
36+
enum BCdecFormat {
37+
BCdec_BC1,
38+
BCdec_BC2,
39+
BCdec_BC3,
40+
BCdec_BC4,
41+
BCdec_BC5,
42+
BCdec_BC6S,
43+
BCdec_BC6U,
44+
BCdec_BC7,
45+
};
3746

38-
#endif // IMAGE_DECOMPRESS_SQUISH_H
47+
void image_decompress_bcdec(Image *p_image);
48+
49+
#endif // IMAGE_DECOMPRESS_BCDEC_H
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@
3030

3131
#include "register_types.h"
3232

33-
#include "image_decompress_squish.h"
33+
#include "image_decompress_bcdec.h"
3434

35-
void initialize_squish_module(ModuleInitializationLevel p_level) {
35+
void initialize_bcdec_module(ModuleInitializationLevel p_level) {
3636
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
3737
return;
3838
}
3939

40-
Image::_image_decompress_bc = image_decompress_squish;
40+
Image::_image_decompress_bc = image_decompress_bcdec;
41+
Image::_image_decompress_bptc = image_decompress_bcdec;
4142
}
4243

43-
void uninitialize_squish_module(ModuleInitializationLevel p_level) {
44+
void uninitialize_bcdec_module(ModuleInitializationLevel p_level) {
4445
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
4546
return;
4647
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2929
/**************************************************************************/
3030

31-
#ifndef SQUISH_REGISTER_TYPES_H
32-
#define SQUISH_REGISTER_TYPES_H
31+
#ifndef BCDEC_REGISTER_TYPES_H
32+
#define BCDEC_REGISTER_TYPES_H
3333

3434
#include "modules/register_module_types.h"
3535

36-
void initialize_squish_module(ModuleInitializationLevel p_level);
37-
void uninitialize_squish_module(ModuleInitializationLevel p_level);
36+
void initialize_bcdec_module(ModuleInitializationLevel p_level);
37+
void uninitialize_bcdec_module(ModuleInitializationLevel p_level);
3838

39-
#endif // SQUISH_REGISTER_TYPES_H
39+
#endif // BCDEC_REGISTER_TYPES_H

modules/cvtt/register_types.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ void initialize_cvtt_module(ModuleInitializationLevel p_level) {
4040
}
4141

4242
Image::set_compress_bptc_func(image_compress_cvtt);
43-
Image::_image_decompress_bptc = image_decompress_cvtt;
4443
}
4544

4645
void uninitialize_cvtt_module(ModuleInitializationLevel p_level) {

modules/squish/SCsub

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)