OpenGL: Support FORMAT_{ETC2,DXT5}_RA_AS_RG

This commit adds support for FORMAT_EXT2_RA_AS_RG and
FORMAT_DXT5_RA_AS_RG with OpenGL, fixing VRAM compression with the
normal map option enabled.
This commit is contained in:
rsjtdrjgfuzkfg 2023-01-11 22:08:49 +01:00
parent f73cb4c13d
commit ebe1123532

View File

@ -300,6 +300,7 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
r_real_format = p_format;
bool need_decompress = false;
bool decompress_ra_to_rg = false;
switch (p_format) {
case Image::FORMAT_L8: {
@ -565,6 +566,28 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
need_decompress = true;
}
} break;
case Image::FORMAT_ETC2_RA_AS_RG: {
if (config->etc2_supported) {
r_gl_internal_format = _EXT_COMPRESSED_RGBA8_ETC2_EAC;
r_gl_format = GL_RGBA;
r_gl_type = GL_UNSIGNED_BYTE;
r_compressed = true;
} else {
need_decompress = true;
}
decompress_ra_to_rg = true;
} break;
case Image::FORMAT_DXT5_RA_AS_RG: {
if (config->s3tc_supported) {
r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
r_gl_format = GL_RGBA;
r_gl_type = GL_UNSIGNED_BYTE;
r_compressed = true;
} else {
need_decompress = true;
}
decompress_ra_to_rg = true;
} break;
default: {
ERR_FAIL_V_MSG(Ref<Image>(), "Image Format: " + itos(p_format) + " is not supported by the OpenGL3 Renderer");
}
@ -575,7 +598,18 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
image = image->duplicate();
image->decompress();
ERR_FAIL_COND_V(image->is_compressed(), image);
if (decompress_ra_to_rg) {
image->convert_ra_rgba8_to_rg();
image->convert(Image::FORMAT_RG8);
}
switch (image->get_format()) {
case Image::FORMAT_RG8: {
r_gl_format = GL_RG;
r_gl_internal_format = GL_RG8;
r_gl_type = GL_UNSIGNED_BYTE;
r_real_format = Image::FORMAT_RG8;
r_compressed = false;
} break;
case Image::FORMAT_RGB8: {
r_gl_format = GL_RGB;
r_gl_internal_format = GL_RGB;
@ -597,7 +631,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
r_gl_type = GL_UNSIGNED_BYTE;
r_real_format = Image::FORMAT_RGBA8;
r_compressed = false;
} break;
}
}
@ -1104,15 +1137,13 @@ void TextureStorage::texture_set_data(RID p_texture, const Ref<Image> &p_image,
texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
texture->gl_set_repeat(RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
//set swizle for older format compatibility
#ifdef GLES_OVER_GL
switch (texture->format) {
#ifdef GLES_OVER_GL
case Image::FORMAT_L8: {
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_RED);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
} break;
case Image::FORMAT_LA8: {
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
@ -1120,15 +1151,27 @@ void TextureStorage::texture_set_data(RID p_texture, const Ref<Image> &p_image,
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
} break;
#endif
case Image::FORMAT_ETC2_RA_AS_RG:
case Image::FORMAT_DXT5_RA_AS_RG: {
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
if (texture->format == real_format) {
// Swizzle RA from compressed texture into RG
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_ALPHA);
} else {
// Converted textures are already in RG, leave as-is
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
}
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_ZERO);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
} break;
default: {
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
} break;
}
#endif
int mipmaps = img->has_mipmaps() ? img->get_mipmap_count() + 1 : 1;