Skip to content

Commit

Permalink
Replace squish with bcdec for BC decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueCube3310 committed Sep 26, 2024
1 parent a0d1ba4 commit 2167157
Show file tree
Hide file tree
Showing 40 changed files with 1,550 additions and 5,138 deletions.
1 change: 0 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ opts.Add(BoolVariable("builtin_pcre2_with_jit", "Use JIT compiler for the built-
opts.Add(BoolVariable("builtin_recastnavigation", "Use the built-in Recast navigation library", True))
opts.Add(BoolVariable("builtin_rvo2_2d", "Use the built-in RVO2 2D library", True))
opts.Add(BoolVariable("builtin_rvo2_3d", "Use the built-in RVO2 3D library", True))
opts.Add(BoolVariable("builtin_squish", "Use the built-in squish library", True))
opts.Add(BoolVariable("builtin_xatlas", "Use the built-in xatlas library", True))
opts.Add(BoolVariable("builtin_zlib", "Use the built-in zlib library", True))
opts.Add(BoolVariable("builtin_zstd", "Use the built-in Zstd library", True))
Expand Down
10 changes: 10 additions & 0 deletions modules/bcdec/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *

Import("env")
Import("env_modules")

env_bcdec = env_modules.Clone()

# Godot source files
env_bcdec.add_source_files(env.modules_sources, "*.cpp")
File renamed without changes.
181 changes: 181 additions & 0 deletions modules/bcdec/image_decompress_bcdec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**************************************************************************/
/* image_decompress_bcdec.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_decompress_bcdec.h"

#include "core/os/os.h"
#include "core/string/print_string.h"

#define BCDEC_IMPLEMENTATION
#include "thirdparty/misc/bcdec.h"

inline void bcdec_bc6h_half_s(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, true);
}

inline void bcdec_bc6h_half_u(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, false);
}

static void decompress_image(BCdecFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {
const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);
uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);
uint64_t src_pos = 0, dst_pos = 0;

#define DECOMPRESS_LOOP(func, block_size, color_bytesize, color_components) \
for (uint64_t y = 0; y < height; y += 4) { \
for (uint64_t x = 0; x < width; x += 4) { \
func(&src_blocks[src_pos], &dec_blocks[dst_pos], width *color_components); \
src_pos += block_size; \
dst_pos += 4 * color_bytesize; \
} \
dst_pos += 3 * width * color_bytesize; \
}

switch (format) {
case BCdec_BC1: {
DECOMPRESS_LOOP(bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 4)
} break;
case BCdec_BC2: {
DECOMPRESS_LOOP(bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 4)
} break;
case BCdec_BC3: {
DECOMPRESS_LOOP(bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 4)
} break;
case BCdec_BC4: {
DECOMPRESS_LOOP(bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1)
} break;
case BCdec_BC5: {
DECOMPRESS_LOOP(bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 2)
} break;
case BCdec_BC6U: {
DECOMPRESS_LOOP(bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 3)
} break;
case BCdec_BC6S: {
DECOMPRESS_LOOP(bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 3)
} break;
case BCdec_BC7: {
DECOMPRESS_LOOP(bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 4)
} break;
}

#undef DECOMPRESS_LOOP
}

void image_decompress_bcdec(Image *p_image) {
uint64_t start_time = OS::get_singleton()->get_ticks_msec();

int w = p_image->get_width();
int h = p_image->get_height();

Image::Format source_format = p_image->get_format();
Image::Format target_format = Image::FORMAT_MAX;

BCdecFormat bcdec_format = BCdec_BC1;

switch (source_format) {
case Image::FORMAT_DXT1:
bcdec_format = BCdec_BC1;
target_format = Image::FORMAT_RGBA8;
break;

case Image::FORMAT_DXT3:
bcdec_format = BCdec_BC2;
target_format = Image::FORMAT_RGBA8;
break;

case Image::FORMAT_DXT5:
case Image::FORMAT_DXT5_RA_AS_RG:
bcdec_format = BCdec_BC3;
target_format = Image::FORMAT_RGBA8;
break;

case Image::FORMAT_RGTC_R:
bcdec_format = BCdec_BC4;
target_format = Image::FORMAT_R8;
break;

case Image::FORMAT_RGTC_RG:
bcdec_format = BCdec_BC5;
target_format = Image::FORMAT_RG8;
break;

case Image::FORMAT_BPTC_RGBFU:
bcdec_format = BCdec_BC6U;
target_format = Image::FORMAT_RGBH;
break;

case Image::FORMAT_BPTC_RGBF:
bcdec_format = BCdec_BC6S;
target_format = Image::FORMAT_RGBH;
break;

case Image::FORMAT_BPTC_RGBA:
bcdec_format = BCdec_BC7;
target_format = Image::FORMAT_RGBA8;
break;

default:
ERR_FAIL_MSG("bcdec: Can't decompress unknown format: " + Image::get_format_name(source_format) + ".");
break;
}

int mm_count = p_image->get_mipmap_count();
int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());

Vector<uint8_t> data;
data.resize(target_size);

const uint8_t *rb = p_image->get_data().ptr();
uint8_t *wb = data.ptrw();

// Decompress mipmaps.
for (int i = 0; i <= mm_count; i++) {
int64_t src_ofs = 0, mipmap_size = 0;
int mipmap_w = 0, mipmap_h = 0;
p_image->get_mipmap_offset_size_and_dimensions(i, src_ofs, mipmap_size, mipmap_w, mipmap_h);

int64_t dst_ofs = Image::get_image_mipmap_offset(p_image->get_width(), p_image->get_height(), target_format, i);
decompress_image(bcdec_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);

w >>= 1;
h >>= 1;
}

p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);

// Swap channels if necessary.
if (source_format == Image::FORMAT_DXT5_RA_AS_RG) {
p_image->convert_ra_rgba8_to_rg();
}

print_verbose(vformat("bcdec: Decompression of a %dx%d %s image with %d mipmaps took %d ms.",
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));
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**************************************************************************/
/* image_decompress_squish.h */
/* image_decompress_bcdec.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
Expand Down Expand Up @@ -28,11 +28,22 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef IMAGE_DECOMPRESS_SQUISH_H
#define IMAGE_DECOMPRESS_SQUISH_H
#ifndef IMAGE_DECOMPRESS_BCDEC_H
#define IMAGE_DECOMPRESS_BCDEC_H

#include "core/io/image.h"

void image_decompress_squish(Image *p_image);
enum BCdecFormat {
BCdec_BC1,
BCdec_BC2,
BCdec_BC3,
BCdec_BC4,
BCdec_BC5,
BCdec_BC6S,
BCdec_BC6U,
BCdec_BC7,
};

#endif // IMAGE_DECOMPRESS_SQUISH_H
void image_decompress_bcdec(Image *p_image);

#endif // IMAGE_DECOMPRESS_BCDEC_H
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@

#include "register_types.h"

#include "image_decompress_squish.h"
#include "image_decompress_bcdec.h"

void initialize_squish_module(ModuleInitializationLevel p_level) {
void initialize_bcdec_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}

Image::_image_decompress_bc = image_decompress_squish;
Image::_image_decompress_bc = image_decompress_bcdec;
Image::_image_decompress_bptc = image_decompress_bcdec;
}

void uninitialize_squish_module(ModuleInitializationLevel p_level) {
void uninitialize_bcdec_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef SQUISH_REGISTER_TYPES_H
#define SQUISH_REGISTER_TYPES_H
#ifndef BCDEC_REGISTER_TYPES_H
#define BCDEC_REGISTER_TYPES_H

#include "modules/register_module_types.h"

void initialize_squish_module(ModuleInitializationLevel p_level);
void uninitialize_squish_module(ModuleInitializationLevel p_level);
void initialize_bcdec_module(ModuleInitializationLevel p_level);
void uninitialize_bcdec_module(ModuleInitializationLevel p_level);

#endif // SQUISH_REGISTER_TYPES_H
#endif // BCDEC_REGISTER_TYPES_H
1 change: 0 additions & 1 deletion modules/cvtt/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void initialize_cvtt_module(ModuleInitializationLevel p_level) {
}

Image::set_compress_bptc_func(image_compress_cvtt);
Image::_image_decompress_bptc = image_decompress_cvtt;
}

void uninitialize_cvtt_module(ModuleInitializationLevel p_level) {
Expand Down
45 changes: 0 additions & 45 deletions modules/squish/SCsub

This file was deleted.

Loading

0 comments on commit 2167157

Please sign in to comment.