medfall

A super great game engine
Log | Files | Refs

commit 66c47ee51102c7545be3d0075494afb8a7056526
parent 144986037ea33d291c5817285197341ea204abbc
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Apr 15 00:37:19 +0300

Add DXT1/DXT5/BC5/sRGB texture support

Diffstat:
renderer.cc | 38++++++++++++++++++++++++++++----------
renderer.h | 3+++
2 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/renderer.cc b/renderer.cc @@ -43,14 +43,30 @@ static GLenum bufferusage_to_glenum( BufferUsage usage ) { return 0; } -static GLenum textureformat_to_glenum( TextureFormat format ) { +static GLenum textureformat_to_glenum( TextureFormat format, bool srgb ) { switch( format ) { - case TEXFMT_RGBA_FLOAT: return GL_RGBA32F; - case TEXFMT_RGB_FLOAT: return GL_RGB32F; - case TEXFMT_R_FLOAT: return GL_R32F; - case TEXFMT_R_U8: return GL_R8; - case TEXFMT_BC4: return GL_COMPRESSED_RED_RGTC1; - + case TEXFMT_RGBA_FLOAT: + ASSERT( !srgb ); + return GL_RGBA32F; + case TEXFMT_RGB_FLOAT: + ASSERT( !srgb ); + return GL_RGB32F; + case TEXFMT_R_FLOAT: + ASSERT( !srgb ); + return GL_R32F; + case TEXFMT_R_U8: + ASSERT( !srgb ); + return GL_R8; + case TEXFMT_BC1: + return srgb ? GL_COMPRESSED_SRGB_S3TC_DXT1_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + case TEXFMT_BC3: + return srgb ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT : GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + case TEXFMT_BC4: + ASSERT( !srgb ); + return GL_COMPRESSED_RED_RGTC1; + case TEXFMT_BC5: + ASSERT( !srgb ); + return GL_COMPRESSED_RG_RGTC2; default: FATAL( "unsupported TextureFormat: {}", format ); return 0; @@ -103,6 +119,7 @@ UB renderer_new_ub( const void * data, u32 len ) { TB renderer_new_tb( TextureFormat format, const void * data, u32 len, BufferUsage usage ) { ASSERT( len < S32_MAX ); + ASSERT( !is_compressed( format ) ); GLuint tbo, texture; glGenBuffers( 1, &tbo ); @@ -110,7 +127,7 @@ TB renderer_new_tb( TextureFormat format, const void * data, u32 len, BufferUsag glBindBuffer( GL_TEXTURE_BUFFER, tbo ); glBindTexture( GL_TEXTURE_BUFFER, texture ); - glTexBuffer( GL_TEXTURE_BUFFER, textureformat_to_glenum( format ), tbo ); + glTexBuffer( GL_TEXTURE_BUFFER, textureformat_to_glenum( format, false ), tbo ); if( data != NULL ) { glBufferData( GL_TEXTURE_BUFFER, len, data, bufferusage_to_glenum( usage ) ); @@ -262,7 +279,8 @@ void renderer_delete_shader( Shader shader ) { } static bool is_compressed( TextureFormat format ) { - return format == TEXFMT_BC4; + return format == TEXFMT_BC1 || format == TEXFMT_BC3 || + format == TEXFMT_BC4 || format == TEXFMT_BC5; } static int mipmap_levels( u32 width, u32 height ) { @@ -313,7 +331,7 @@ Texture renderer_new_texture( TextureConfig config ) { glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - GLenum internal_format = textureformat_to_glenum( config.format ); + GLenum internal_format = textureformat_to_glenum( config.format, config.srgb ); GLenum channels = GL_INVALID_ENUM; GLenum type = GL_INVALID_ENUM; diff --git a/renderer.h b/renderer.h @@ -125,7 +125,10 @@ enum TextureFormat { TEXFMT_RGB_FLOAT, TEXFMT_R_FLOAT, TEXFMT_R_U8, + TEXFMT_BC1, + TEXFMT_BC3, TEXFMT_BC4, + TEXFMT_BC5, }; struct TextureConfig {