Skip to content

Commit

Permalink
-Added EXR supprot for HDR (no BC6 compression yet though)
Browse files Browse the repository at this point in the history
-Improvements to texture importer
-Proper detection of S3TC compression modes, and added all modes to Image
-Fixes to non-power of 2 compressed textures, which should all be supported by GLES3
  • Loading branch information
reduz committed May 27, 2017
1 parent 41918f3 commit f896419
Show file tree
Hide file tree
Showing 18 changed files with 13,359 additions and 109 deletions.
512 changes: 481 additions & 31 deletions core/image.cpp

Large diffs are not rendered by default.

40 changes: 30 additions & 10 deletions core/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class Image : public Resource {
FORMAT_RG8,
FORMAT_RGB8,
FORMAT_RGBA8,
FORMAT_RGB565, //16 bit
FORMAT_RGBA4444,
FORMAT_RGBA5551,
FORMAT_RF, //float
Expand All @@ -77,14 +76,17 @@ class Image : public Resource {
FORMAT_RGH,
FORMAT_RGBH,
FORMAT_RGBAH,
FORMAT_RGBE9995,
FORMAT_DXT1, //s3tc bc1
FORMAT_DXT3, //bc2
FORMAT_DXT5, //bc3
FORMAT_ATI1, //bc4
FORMAT_ATI2, //bc5
FORMAT_BPTC_RGBA, //btpc bc6h
FORMAT_BPTC_RGBF, //float /
FORMAT_BPTC_RGBFU, //unsigned float
FORMAT_LATC_L,
FORMAT_LATC_LA,
FORMAT_RGTC_R,
FORMAT_RGTC_RG,
FORMAT_BPTC_RGBA, //btpc bc7
FORMAT_BPTC_RGBF, //float bc6h
FORMAT_BPTC_RGBFU, //unsigned float bc6hu
FORMAT_PVRTC2, //pvrtc
FORMAT_PVRTC2A,
FORMAT_PVRTC4,
Expand Down Expand Up @@ -125,13 +127,13 @@ class Image : public Resource {
static void (*_image_decompress_etc)(Image *);
static void (*_image_decompress_etc2)(Image *);

Error _decompress_bc();

static PoolVector<uint8_t> (*lossy_packer)(const Ref<Image> &p_image, float p_quality);
static Ref<Image> (*lossy_unpacker)(const PoolVector<uint8_t> &p_buffer);
static PoolVector<uint8_t> (*lossless_packer)(const Ref<Image> &p_image);
static Ref<Image> (*lossless_unpacker)(const PoolVector<uint8_t> &p_buffer);

PoolVector<uint8_t>::Write write_lock;

protected:
static void _bind_methods();

Expand Down Expand Up @@ -253,18 +255,18 @@ class Image : public Resource {

static int get_format_pixel_size(Format p_format);
static int get_format_pixel_rshift(Format p_format);
static int get_format_block_size(Format p_format);
static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);

static int get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps = 0);
static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);

enum CompressMode {
COMPRESS_16BIT,
COMPRESS_S3TC,
COMPRESS_PVRTC2,
COMPRESS_PVRTC4,
COMPRESS_ETC,
COMPRESS_ETC2
COMPRESS_ETC2,
};

Error compress(CompressMode p_mode = COMPRESS_S3TC);
Expand All @@ -289,6 +291,24 @@ class Image : public Resource {

virtual Ref<Resource> duplicate(bool p_subresources = false) const;

void lock();
void unlock();

//this is used for compression
enum DetectChannels {
DETECTED_L,
DETECTED_LA,
DETECTED_R,
DETECTED_RG,
DETECTED_RGB,
DETECTED_RGBA,
};

DetectChannels get_detected_channels();

Color get_pixel(int p_x, int p_y);
void put_pixel(int p_x, int p_y, const Color &p_color);

void copy_internals_from(const Ref<Image> &p_image) {
ERR_FAIL_COND(p_image.is_null());
format = p_image->format;
Expand Down
4 changes: 4 additions & 0 deletions core/math/math_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ class Math {
return u.f32;
}

static _ALWAYS_INLINE_ float half_to_float(const uint16_t h) {
return halfptr_to_float(&h);
}

static _ALWAYS_INLINE_ uint16_t make_half_float(float f) {

union {
Expand Down
69 changes: 53 additions & 16 deletions drivers/gles3/rasterizer_storage_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,6 @@ Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_
r_gl_type = GL_UNSIGNED_BYTE;
srgb = true;

} break;
case Image::FORMAT_RGB565: {
#ifndef GLES_OVER_GL
r_gl_internal_format = GL_RGB565;
#else
//#warning TODO: Convert tod 555 if 565 is not supported (GLES3.3-)
r_gl_internal_format = GL_RGB5;
#endif
//r_gl_internal_format=GL_RGB565;
r_gl_format = GL_RGB;
r_gl_type = GL_UNSIGNED_SHORT_5_6_5;

} break;
case Image::FORMAT_RGBA4444: {

Expand Down Expand Up @@ -240,6 +228,12 @@ Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_
r_gl_format = GL_RGBA;
r_gl_type = GL_HALF_FLOAT;

} break;
case Image::FORMAT_RGBE9995: {
r_gl_internal_format = GL_RGB9_E5;
r_gl_format = GL_RGB;
r_gl_type = GL_UNSIGNED_INT_5_9_9_9_REV;

} break;
case Image::FORMAT_DXT1: {

Expand Down Expand Up @@ -289,7 +283,7 @@ Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_
}

} break;
case Image::FORMAT_ATI1: {
case Image::FORMAT_LATC_L: {

if (config.latc_supported) {

Expand All @@ -305,7 +299,7 @@ Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_
}

} break;
case Image::FORMAT_ATI2: {
case Image::FORMAT_LATC_LA: {

if (config.latc_supported) {

Expand All @@ -318,6 +312,36 @@ Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_
need_decompress = true;
}

} break;
case Image::FORMAT_RGTC_R: {

if (config.rgtc_supported) {

r_gl_internal_format = _EXT_COMPRESSED_RED_RGTC1_EXT;
r_gl_format = GL_RGBA;
r_gl_type = GL_UNSIGNED_BYTE;
r_compressed = true;
srgb = true;

} else {

need_decompress = true;
}

} break;
case Image::FORMAT_RGTC_RG: {

if (config.rgtc_supported) {

r_gl_internal_format = _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT;
r_gl_format = GL_RGBA;
r_gl_type = GL_UNSIGNED_BYTE;
r_compressed = true;
} else {

need_decompress = true;
}

} break;
case Image::FORMAT_BPTC_RGBA: {

Expand Down Expand Up @@ -662,7 +686,7 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p
if (texture->alloc_width == img->get_width() / 2 && texture->alloc_height == img->get_height() / 2) {

img->shrink_x2();
} else if (img->get_format() <= Image::FORMAT_RGB565) {
} else if (img->get_format() <= Image::FORMAT_RGBA8) {

img->resize(texture->alloc_width, texture->alloc_height, Image::INTERPOLATE_BILINEAR);
}
Expand Down Expand Up @@ -768,6 +792,9 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p
int h = img->get_height();

int tsize = 0;

int block = Image::get_format_block_size(img->get_format());

for (int i = 0; i < mipmaps; i++) {

int size, ofs;
Expand All @@ -777,7 +804,16 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p

if (texture->compressed) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glCompressedTexImage2D(blit_target, i, internal_format, w, h, 0, size, &read[ofs]);

//this is not needed, as compressed takes the regular size, even if blocks extend it
//int bw = (w % block != 0) ? w + (block - w % block) : w;
//int bh = (h % block != 0) ? h + (block - h % block) : h;

int bw = w;
int bh = h;

glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
print_line("format: " + Image::get_format_name(texture->format) + " size: " + Vector2(bw, bh) + " block: " + itos(block));

} else {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Expand Down Expand Up @@ -6358,6 +6394,7 @@ void RasterizerStorageGLES3::initialize() {
config.s3tc_supported = config.extensions.has("GL_EXT_texture_compression_dxt1") || config.extensions.has("GL_EXT_texture_compression_s3tc") || config.extensions.has("WEBGL_compressed_texture_s3tc");
config.etc_supported = config.extensions.has("GL_OES_compressed_ETC1_RGB8_texture");
config.latc_supported = config.extensions.has("GL_EXT_texture_compression_latc");
config.rgtc_supported = config.extensions.has("GL_EXT_texture_compression_rgtc");
config.bptc_supported = config.extensions.has("GL_ARB_texture_compression_bptc");
#ifdef GLES_OVER_GL
config.hdr_supported = true;
Expand Down
1 change: 1 addition & 0 deletions drivers/gles3/rasterizer_storage_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {

bool s3tc_supported;
bool latc_supported;
bool rgtc_supported;
bool bptc_supported;
bool etc_supported;
bool etc2_supported;
Expand Down
21 changes: 16 additions & 5 deletions editor/import/resource_importer_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ void ResourceImporterTexture::get_import_options(List<ImportOption> *r_options,

r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/mode", PROPERTY_HINT_ENUM, "Lossless,Lossy,Video RAM,Uncompressed", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), p_preset == PRESET_3D ? 2 : 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "compress/lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.7));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/hdr_mode", PROPERTY_HINT_ENUM, "Compress,Force RGBE"), 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "flags/repeat", PROPERTY_HINT_ENUM, "Disabled,Enabled,Mirrored"), p_preset == PRESET_3D ? 1 : 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "flags/filter"), p_preset == PRESET_2D_PIXEL ? false : true));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "flags/mipmaps"), p_preset == PRESET_3D ? true : false));
Expand All @@ -181,7 +182,7 @@ void ResourceImporterTexture::get_import_options(List<ImportOption> *r_options,
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "detect_3d"), p_preset == PRESET_DETECT));
}

void ResourceImporterTexture::_save_stex(const Ref<Image> &p_image, const String &p_to_path, int p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, int p_texture_flags, bool p_streamable, bool p_detect_3d, bool p_detect_srgb) {
void ResourceImporterTexture::_save_stex(const Ref<Image> &p_image, const String &p_to_path, int p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, int p_texture_flags, bool p_streamable, bool p_detect_3d, bool p_detect_srgb, bool p_force_rgbe) {

FileAccess *f = FileAccess::open(p_to_path, FileAccess::WRITE);
f->store_8('G');
Expand All @@ -204,6 +205,10 @@ void ResourceImporterTexture::_save_stex(const Ref<Image> &p_image, const String
if (p_detect_srgb)
format |= StreamTexture::FORMAT_BIT_DETECT_SRGB;

if ((p_compress_mode == COMPRESS_LOSSLESS || p_compress_mode == COMPRESS_LOSSY) && p_image->get_format() > Image::FORMAT_RGBA8) {
p_compress_mode == COMPRESS_UNCOMPRESSED; //these can't go as lossy
}

switch (p_compress_mode) {
case COMPRESS_LOSSLESS: {

Expand Down Expand Up @@ -267,7 +272,12 @@ void ResourceImporterTexture::_save_stex(const Ref<Image> &p_image, const String

Ref<Image> image = p_image->duplicate();
image->generate_mipmaps();
image->compress(p_vram_compression);

if (p_force_rgbe && image->get_format() >= Image::FORMAT_R8 && image->get_format() <= Image::FORMAT_RGBE9995) {
image->convert(Image::FORMAT_RGBE9995);
} else {
image->compress(p_vram_compression);
}

format |= image->get_format();

Expand Down Expand Up @@ -316,6 +326,7 @@ Error ResourceImporterTexture::import(const String &p_source_file, const String
bool premult_alpha = p_options["process/premult_alpha"];
bool stream = p_options["stream"];
int size_limit = p_options["size_limit"];
bool force_rgbe = int(p_options["compress/hdr_mode"]) == 1;

Ref<Image> image;
image.instance();
Expand Down Expand Up @@ -367,16 +378,16 @@ Error ResourceImporterTexture::import(const String &p_source_file, const String
if (compress_mode == COMPRESS_VIDEO_RAM) {
//must import in all formats
//Android, GLES 2.x
_save_stex(image, p_save_path + ".etc.stex", compress_mode, lossy, Image::COMPRESS_ETC, mipmaps, tex_flags, stream, detect_3d, detect_srgb);
_save_stex(image, p_save_path + ".etc.stex", compress_mode, lossy, Image::COMPRESS_ETC, mipmaps, tex_flags, stream, detect_3d, detect_srgb, force_rgbe);
r_platform_variants->push_back("etc");
//_save_stex(image,p_save_path+".etc2.stex",compress_mode,lossy,Image::COMPRESS_ETC2,mipmaps,tex_flags,stream);
//r_platform_variants->push_back("etc2");
_save_stex(image, p_save_path + ".s3tc.stex", compress_mode, lossy, Image::COMPRESS_S3TC, mipmaps, tex_flags, stream, detect_3d, detect_srgb);
_save_stex(image, p_save_path + ".s3tc.stex", compress_mode, lossy, Image::COMPRESS_S3TC, mipmaps, tex_flags, stream, detect_3d, detect_srgb, force_rgbe);
r_platform_variants->push_back("s3tc");

} else {
//import normally
_save_stex(image, p_save_path + ".stex", compress_mode, lossy, Image::COMPRESS_16BIT /*this is ignored */, mipmaps, tex_flags, stream, detect_3d, detect_srgb);
_save_stex(image, p_save_path + ".stex", compress_mode, lossy, Image::COMPRESS_S3TC /*this is ignored */, mipmaps, tex_flags, stream, detect_3d, detect_srgb, force_rgbe);
}

return OK;
Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ResourceImporterTexture : public ResourceImporter {
virtual void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const;
virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const;

void _save_stex(const Ref<Image> &p_image, const String &p_to_path, int p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, int p_texture_flags, bool p_streamable, bool p_detect_3d, bool p_detect_srgb);
void _save_stex(const Ref<Image> &p_image, const String &p_to_path, int p_compress_mode, float p_lossy_quality, Image::CompressMode p_vram_compression, bool p_mipmaps, int p_texture_flags, bool p_streamable, bool p_detect_3d, bool p_detect_srgb, bool p_force_rgbe);

virtual Error import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = NULL);

Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/texture_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void TextureEditor::_notification(int p_what) {
String format;
if (texture->cast_to<ImageTexture>()) {
format = Image::get_format_name(texture->cast_to<ImageTexture>()->get_format());
} else if (texture->cast_to<StreamTexture>()) {
format = Image::get_format_name(texture->cast_to<StreamTexture>()->get_format());
} else {
format = texture->get_class();
}
Expand Down
4 changes: 2 additions & 2 deletions modules/dds/texture_loader_dds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ static const DDSFormatInfo dds_format_info[DDS_MAX] = {
{ "DXT1", true, false, 4, 8, Image::FORMAT_DXT1 },
{ "DXT3", true, false, 4, 16, Image::FORMAT_DXT3 },
{ "DXT5", true, false, 4, 16, Image::FORMAT_DXT5 },
{ "ATI1", true, false, 4, 8, Image::FORMAT_ATI1 },
{ "ATI2", true, false, 4, 16, Image::FORMAT_ATI2 },
{ "ATI1", true, false, 4, 8, Image::FORMAT_LATC_L },
{ "ATI2", true, false, 4, 16, Image::FORMAT_LATC_LA },
{ "BGRA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
{ "BGR8", false, false, 1, 3, Image::FORMAT_RGB8 },
{ "RGBA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
Expand Down
Loading

0 comments on commit f896419

Please sign in to comment.