Skip to content

Commit

Permalink
Backport implement loading DDS textures at run-time
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinn authored and akien-mga committed Feb 17, 2023
1 parent 50b45d6 commit 22468ea
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 360 deletions.
10 changes: 10 additions & 0 deletions core/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,7 @@ ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_dds_mem_loader_func = nullptr;

void (*Image::_image_compress_bc_func)(Image *, float, Image::CompressSource) = nullptr;
void (*Image::_image_compress_bptc_func)(Image *, float, Image::CompressSource) = nullptr;
Expand Down Expand Up @@ -2971,6 +2972,7 @@ void Image::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer);
ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer);
ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer);
ClassDB::bind_method(D_METHOD("load_dds_from_buffer", "buffer"), &Image::load_dds_from_buffer);

ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");

Expand Down Expand Up @@ -3301,6 +3303,14 @@ Error Image::load_bmp_from_buffer(const PoolVector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _bmp_mem_loader_func);
}

Error Image::load_dds_from_buffer(const PoolVector<uint8_t> &p_array) {
ERR_FAIL_NULL_V_MSG(
_dds_mem_loader_func,
ERR_UNAVAILABLE,
"The DDS module isn't enabled. Recompile the Godot editor or export template binary with the `module_dds_enabled=yes` SCons option.");
return _load_from_buffer(p_array, _dds_mem_loader_func);
}

Error Image::_load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
int buffer_size = p_array.size();

Expand Down
2 changes: 2 additions & 0 deletions core/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Image : public Resource {
static ImageMemLoadFunc _webp_mem_loader_func;
static ImageMemLoadFunc _tga_mem_loader_func;
static ImageMemLoadFunc _bmp_mem_loader_func;
static ImageMemLoadFunc _dds_mem_loader_func;

static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
Expand Down Expand Up @@ -343,6 +344,7 @@ class Image : public Resource {
Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_tga_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_bmp_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_dds_from_buffer(const PoolVector<uint8_t> &p_array);

Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
Image(const char **p_xpm);
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/Image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@
[b]Note:[/b] Godot's BMP module doesn't support 16-bit per pixel images. Only 1-bit, 4-bit, 8-bit, 24-bit, and 32-bit per pixel images are supported.
</description>
</method>
<method name="load_dds_from_buffer">
<return type="int" enum="Error" />
<argument index="0" name="buffer" type="PoolByteArray" />
<description>
Loads an image from the binary contents of a DDS file.
</description>
</method>
<method name="load_jpg_from_buffer">
<return type="int" enum="Error" />
<argument index="0" name="buffer" type="PoolByteArray" />
Expand Down
Loading

0 comments on commit 22468ea

Please sign in to comment.