Skip to content

Commit bee5de6

Browse files
committedJan 13, 2025
Merge pull request godotengine#100365 from BlueCube3310/etc-decompress
Add support for decompressing ETC2
2 parents d19147e + c4fd9f9 commit bee5de6

10 files changed

+1067
-1
lines changed
 

‎modules/etcpak/SCsub

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ thirdparty_obj = []
1212

1313
thirdparty_dir = "#thirdparty/etcpak/"
1414
thirdparty_sources = [
15+
"DecodeRGB.cpp",
1516
"Dither.cpp",
1617
"ProcessDxtc.cpp",
1718
"ProcessRGB.cpp",

‎modules/etcpak/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def can_build(env, platform):
2-
return env.editor_build
2+
return True
33

44

55
def configure(env):

‎modules/etcpak/image_compress_etcpak.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#include "image_compress_etcpak.h"
3232

33+
#ifdef TOOLS_ENABLED
34+
3335
#include "core/os/os.h"
3436
#include "core/string/print_string.h"
3537

@@ -303,3 +305,4 @@ void _compress_etcpak(EtcpakType p_compress_type, Image *r_img) {
303305

304306
print_verbose(vformat("etcpak: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
305307
}
308+
#endif // TOOLS_ENABLED

‎modules/etcpak/image_compress_etcpak.h

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#ifndef IMAGE_COMPRESS_ETCPAK_H
3232
#define IMAGE_COMPRESS_ETCPAK_H
3333

34+
#ifdef TOOLS_ENABLED
35+
3436
#include "core/io/image.h"
3537

3638
enum class EtcpakType {
@@ -53,4 +55,6 @@ void _compress_bc(Image *r_img, Image::UsedChannels p_channels);
5355

5456
void _compress_etcpak(EtcpakType p_compress_type, Image *r_img);
5557

58+
#endif // TOOLS_ENABLED
59+
5660
#endif // IMAGE_COMPRESS_ETCPAK_H
+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/**************************************************************************/
2+
/* image_decompress_etcpak.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_etcpak.h"
32+
33+
#include "core/os/os.h"
34+
#include "core/string/print_string.h"
35+
36+
#include <DecodeRGB.hpp>
37+
38+
#define ETCPAK_R_BLOCK_SIZE 8
39+
#define ETCPAK_RG_BLOCK_SIZE 16
40+
#define ETCPAK_RGB_BLOCK_SIZE 8
41+
#define ETCPAK_RGBA_BLOCK_SIZE 16
42+
43+
static void decompress_image(EtcpakFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {
44+
const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);
45+
uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);
46+
47+
#define DECOMPRESS_LOOP(m_func, m_block_size, m_color_bytesize) \
48+
for (uint64_t y = 0; y < height; y += 4) { \
49+
for (uint64_t x = 0; x < width; x += 4) { \
50+
m_func(&src_blocks[src_pos], &dec_blocks[dst_pos], width); \
51+
src_pos += m_block_size; \
52+
dst_pos += 4 * m_color_bytesize; \
53+
} \
54+
dst_pos += 3 * width * m_color_bytesize; \
55+
}
56+
57+
#define DECOMPRESS_LOOP_SAFE(m_func, m_block_size, m_color_bytesize, m_output) \
58+
for (uint64_t y = 0; y < height; y += 4) { \
59+
for (uint64_t x = 0; x < width; x += 4) { \
60+
const uint32_t yblock = MIN(height - y, 4ul); \
61+
const uint32_t xblock = MIN(width - x, 4ul); \
62+
\
63+
const bool incomplete = yblock < 4 && xblock < 4; \
64+
uint8_t *dec_out = incomplete ? m_output : &dec_blocks[y * 4 * width + x * m_color_bytesize]; \
65+
\
66+
m_func(&src_blocks[src_pos], dec_out, incomplete ? 4 : width); \
67+
src_pos += m_block_size; \
68+
\
69+
if (incomplete) { \
70+
for (uint32_t cy = 0; cy < yblock; cy++) { \
71+
for (uint32_t cx = 0; cx < xblock; cx++) { \
72+
memcpy(&dec_blocks[(y + cy) * 4 * width + (x + cx) * m_color_bytesize], &m_output[cy * 4 + cx * m_color_bytesize], m_color_bytesize); \
73+
} \
74+
} \
75+
} \
76+
} \
77+
}
78+
79+
if (width % 4 != 0 || height % 4 != 0) {
80+
uint64_t src_pos = 0;
81+
82+
uint8_t rgba8_output[4 * 4 * 4];
83+
84+
switch (format) {
85+
case Etcpak_R: {
86+
DECOMPRESS_LOOP_SAFE(DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4, rgba8_output)
87+
} break;
88+
case Etcpak_RG: {
89+
DECOMPRESS_LOOP_SAFE(DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4, rgba8_output)
90+
} break;
91+
case Etcpak_RGB: {
92+
DECOMPRESS_LOOP_SAFE(DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4, rgba8_output)
93+
} break;
94+
case Etcpak_RGBA: {
95+
DECOMPRESS_LOOP_SAFE(DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4, rgba8_output)
96+
} break;
97+
}
98+
99+
} else {
100+
uint64_t src_pos = 0, dst_pos = 0;
101+
102+
switch (format) {
103+
case Etcpak_R: {
104+
DECOMPRESS_LOOP(DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4)
105+
} break;
106+
case Etcpak_RG: {
107+
DECOMPRESS_LOOP(DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4)
108+
} break;
109+
case Etcpak_RGB: {
110+
DECOMPRESS_LOOP(DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4)
111+
} break;
112+
case Etcpak_RGBA: {
113+
DECOMPRESS_LOOP(DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4)
114+
} break;
115+
}
116+
}
117+
118+
#undef DECOMPRESS_LOOP
119+
#undef DECOMPRESS_LOOP_SAFE
120+
}
121+
122+
void _decompress_etc(Image *p_image) {
123+
uint64_t start_time = OS::get_singleton()->get_ticks_msec();
124+
125+
int width = p_image->get_width();
126+
int height = p_image->get_height();
127+
128+
// Compressed images' dimensions should be padded to the upper multiple of 4.
129+
// If they aren't, they need to be realigned (the actual data is correctly padded though).
130+
if (width % 4 != 0 || height % 4 != 0) {
131+
int new_width = width + (4 - (width % 4));
132+
int new_height = height + (4 - (height % 4));
133+
134+
print_verbose(vformat("Compressed image (%s) has dimensions are not multiples of 4 (%dx%d), aligning to (%dx%d)", p_image->get_path(), width, height, new_width, new_height));
135+
136+
width = new_width;
137+
height = new_height;
138+
}
139+
140+
Image::Format source_format = p_image->get_format();
141+
Image::Format target_format = Image::FORMAT_RGBA8;
142+
143+
EtcpakFormat etcpak_format = Etcpak_R;
144+
145+
switch (source_format) {
146+
case Image::FORMAT_ETC:
147+
case Image::FORMAT_ETC2_RGB8:
148+
etcpak_format = Etcpak_RGB;
149+
break;
150+
151+
case Image::FORMAT_ETC2_RGBA8:
152+
case Image::FORMAT_ETC2_RA_AS_RG:
153+
etcpak_format = Etcpak_RGBA;
154+
break;
155+
156+
case Image::FORMAT_ETC2_R11:
157+
etcpak_format = Etcpak_R;
158+
break;
159+
160+
case Image::FORMAT_ETC2_RG11:
161+
etcpak_format = Etcpak_RG;
162+
break;
163+
164+
default:
165+
ERR_FAIL_MSG(vformat("etcpak: Can't decompress image %s with an unknown format: %s.", p_image->get_path(), Image::get_format_name(source_format)));
166+
break;
167+
}
168+
169+
int mm_count = p_image->get_mipmap_count();
170+
int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());
171+
172+
// Decompressed data.
173+
Vector<uint8_t> data;
174+
data.resize(target_size);
175+
uint8_t *wb = data.ptrw();
176+
177+
// Source data.
178+
const uint8_t *rb = p_image->ptr();
179+
180+
// Decompress mipmaps.
181+
for (int i = 0; i <= mm_count; i++) {
182+
int mipmap_w = 0, mipmap_h = 0;
183+
int64_t src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, source_format, i, mipmap_w, mipmap_h);
184+
int64_t dst_ofs = Image::get_image_mipmap_offset(width, height, target_format, i);
185+
decompress_image(etcpak_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);
186+
}
187+
188+
p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
189+
190+
// Swap channels if the format is using a channel swizzle.
191+
if (source_format == Image::FORMAT_ETC2_RA_AS_RG) {
192+
p_image->convert_ra_rgba8_to_rg();
193+
}
194+
195+
print_verbose(vformat("etcpak: Decompression of %dx%d %s image %s with %d mipmaps took %d ms.",
196+
p_image->get_width(), p_image->get_height(), Image::get_format_name(source_format), p_image->get_path(), p_image->get_mipmap_count(), OS::get_singleton()->get_ticks_msec() - start_time));
197+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**************************************************************************/
2+
/* image_decompress_etcpak.h */
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+
#ifndef IMAGE_DECOMPRESS_ETCPAK_H
32+
#define IMAGE_DECOMPRESS_ETCPAK_H
33+
34+
#include "core/io/image.h"
35+
36+
enum EtcpakFormat {
37+
Etcpak_R,
38+
Etcpak_RG,
39+
Etcpak_RGB,
40+
Etcpak_RGBA,
41+
};
42+
43+
void _decompress_etc(Image *p_image);
44+
45+
#endif // IMAGE_DECOMPRESS_ETCPAK_H

‎modules/etcpak/register_types.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,21 @@
3131
#include "register_types.h"
3232

3333
#include "image_compress_etcpak.h"
34+
#include "image_decompress_etcpak.h"
3435

3536
void initialize_etcpak_module(ModuleInitializationLevel p_level) {
3637
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
3738
return;
3839
}
3940

41+
#ifdef TOOLS_ENABLED
4042
Image::_image_compress_etc1_func = _compress_etc1;
4143
Image::_image_compress_etc2_func = _compress_etc2;
4244
Image::_image_compress_bc_func = _compress_bc;
45+
#endif
46+
47+
Image::_image_decompress_etc1 = _decompress_etc;
48+
Image::_image_decompress_etc2 = _decompress_etc;
4349
}
4450

4551
void uninitialize_etcpak_module(ModuleInitializationLevel p_level) {

‎thirdparty/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Files extracted from upstream source:
245245
Dither.{cpp,hpp} ForceInline.hpp Math.hpp ProcessCommon.hpp ProcessRGB.{cpp,hpp}
246246
ProcessDxtc.{cpp,hpp} Tables.{cpp,hpp} Vector.hpp
247247
```
248+
- The files `DecodeRGB.{cpp.hpp}` are based on the code from the original repository.
248249
- `AUTHORS.txt` and `LICENSE.txt`
249250

250251
## fonts

‎thirdparty/etcpak/DecodeRGB.cpp

+797
Large diffs are not rendered by default.

‎thirdparty/etcpak/DecodeRGB.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __DECODERGB_HPP__
2+
#define __DECODERGB_HPP__
3+
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
void DecodeRBlock( const void* src, void* dst, size_t width );
8+
void DecodeRGBlock( const void* src, void* dst, size_t width );
9+
void DecodeRGBBlock( const void* src, void* dst, size_t width );
10+
void DecodeRGBABlock( const void* src, void* dst, size_t width );
11+
12+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.