medfall

A super great game engine
Log | Files | Refs

commit 9110cc6adc765b7b89d8aa67f6137865be638c4c
parent fac84d570608f5a23083947ae81c3ed38092a5ca
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Tue, 24 Oct 2017 20:55:51 +0300

Texture format overhaul. Merge sRGB into TextureFormat, add RGB/RGBA U8 textures, drop BC3

Diffstat:
clipmap.cc | 3---
obj.cc | 6++----
obj.h | 4++--
renderer.cc | 129+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
renderer.h | 17+++++++++++------
5 files changed, 98 insertions(+), 61 deletions(-)

diff --git a/clipmap.cc b/clipmap.cc @@ -142,7 +142,6 @@ GAME_INIT( game_init ) { texture_config.format = TEXFMT_R_U16; texture_config.wrap = TEXWRAP_BORDER; texture_config.border_colour = v4( 0 ); - texture_config.srgb = false; heightmap = renderer_new_texture( texture_config ); } @@ -154,7 +153,6 @@ GAME_INIT( game_init ) { texture_config.format = TEXFMT_RGB_FLOAT; texture_config.wrap = TEXWRAP_BORDER; texture_config.border_colour = v4( 0, 0, 1, 0 ); - texture_config.srgb = false; normalmap = renderer_new_texture( texture_config ); } @@ -166,7 +164,6 @@ GAME_INIT( game_init ) { texture_config.format = TEXFMT_R_U8; texture_config.wrap = TEXWRAP_BORDER; texture_config.border_colour = v4( 0 ); - texture_config.srgb = false; horizonmap = renderer_new_texture( texture_config ); } diff --git a/obj.cc b/obj.cc @@ -48,7 +48,7 @@ Mesh load_obj( const char * path, MemoryArena * arena ) { return renderer_new_mesh( mesh_config ); } -Texture load_png( const char * path, TextureFormat format, int channels, bool srgb ) { +Texture load_png( const char * path, TextureFormat format, int channels ) { LodePNGColorType color = LCT_RGBA; if( channels == 1 ) color = LCT_GREY; if( channels == 2 ) color = LCT_GREY_ALPHA; @@ -64,7 +64,6 @@ Texture load_png( const char * path, TextureFormat format, int channels, bool sr texture_config.height = h; texture_config.data = data; texture_config.format = format; - texture_config.srgb = srgb; Texture texture = renderer_new_texture( texture_config ); free( data ); @@ -72,7 +71,7 @@ Texture load_png( const char * path, TextureFormat format, int channels, bool sr return texture; } -Texture load_png_memory( const u8 * data, size_t len, TextureFormat format, int channels, bool srgb ) { +Texture load_png_memory( const u8 * data, size_t len, TextureFormat format, int channels ) { LodePNGColorType color = LCT_RGBA; if( channels == 1 ) color = LCT_GREY; if( channels == 2 ) color = LCT_GREY_ALPHA; @@ -88,7 +87,6 @@ Texture load_png_memory( const u8 * data, size_t len, TextureFormat format, int texture_config.height = h; texture_config.data = decoded; texture_config.format = format; - texture_config.srgb = srgb; Texture texture = renderer_new_texture( texture_config ); free( decoded ); diff --git a/obj.h b/obj.h @@ -5,5 +5,5 @@ Mesh load_obj( const char * path, MemoryArena * arena ); -Texture load_png( const char * path, TextureFormat format, int channels, bool srgb ); -Texture load_png_memory( const u8 * data, size_t len, TextureFormat format, int channels, bool srgb ); +Texture load_png( const char * path, TextureFormat format, int channels ); +Texture load_png_memory( const u8 * data, size_t len, TextureFormat format, int channels ); diff --git a/renderer.cc b/renderer.cc @@ -98,7 +98,7 @@ void renderer_init() { glGetIntegerv( GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment ); ubo_offset_alignment = checked_cast< size_t >( alignment ); - blue_noise = load_png_memory( blue_noise_png, blue_noise_png_len, TEXFMT_R_U8NORM, 1, false ); + blue_noise = load_png_memory( blue_noise_png, blue_noise_png_len, TEXFMT_R_U8NORM, 1 ); } void renderer_term() { @@ -444,40 +444,44 @@ static GLenum bufferusage_to_glenum( BufferUsage usage ) { return GL_INVALID_ENUM; } -static GLenum textureformat_to_glenum( TextureFormat format, bool srgb ) { +static GLenum texfmt_to_internal_format( TextureFormat format ) { switch( format ) { case TEXFMT_INVALID: break; - 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_R_U8NORM: - ASSERT( !srgb ); return GL_R8_SNORM; case TEXFMT_R_U16: - ASSERT( !srgb ); return GL_R16; + case TEXFMT_R_FLOAT: + return GL_R32F; + + case TEXFMT_RGB_U8: + return GL_RGB8; + case TEXFMT_RGB_U8_SRGB: + return GL_SRGB8; + case TEXFMT_RGB_FLOAT: + return GL_RGB32F; + + case TEXFMT_RGBA_U8: + return GL_RGBA8; + case TEXFMT_RGBA_U8_SRGB: + return GL_SRGB8_ALPHA8; + case TEXFMT_RGBA_FLOAT: + return GL_RGBA32F; + case TEXFMT_DEPTH: - ASSERT( !srgb ); return GL_DEPTH_COMPONENT24; + 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; + return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + case TEXFMT_BC1_SRGB: + return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT; case TEXFMT_BC4: - ASSERT( !srgb ); return GL_COMPRESSED_RED_RGTC1; case TEXFMT_BC5: - ASSERT( !srgb ); return GL_COMPRESSED_RG_RGTC2; } @@ -517,8 +521,7 @@ static void texturefilter_to_glenum( TextureFilterMode mode, bool mipmaps, GLenu } static bool is_compressed( TextureFormat format ) { - return format == TEXFMT_BC1 || format == TEXFMT_BC3 || - format == TEXFMT_BC4 || format == TEXFMT_BC5; + return format == TEXFMT_BC1 || format == TEXFMT_BC4 || format == TEXFMT_BC5; } VB renderer_new_vb( const void * data, u32 len, BufferUsage usage ) { @@ -569,7 +572,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, false ), tbo ); + glTexBuffer( GL_TEXTURE_BUFFER, texfmt_to_internal_format( format ), tbo ); if( data != NULL ) { glBufferData( GL_TEXTURE_BUFFER, len, data, bufferusage_to_glenum( usage ) ); @@ -775,23 +778,43 @@ static u32 texture_byte_size( u32 width, u32 height, TextureFormat format, bool } switch( format ) { - case TEXFMT_RGBA_FLOAT: - return total_dim * 4 * sizeof( float ); - case TEXFMT_RGB_FLOAT: - return total_dim * 3 * sizeof( float ); - case TEXFMT_R_FLOAT: - return total_dim * 1 * sizeof( float ); + case TEXFMT_INVALID: + case TEXFMT_DEPTH: + break; + case TEXFMT_R_U8: case TEXFMT_R_U8NORM: return total_dim * 1 * sizeof( u8 ); case TEXFMT_R_U16: return total_dim * 1 * sizeof( u16 ); + case TEXFMT_R_FLOAT: + return total_dim * 1 * sizeof( float ); + + case TEXFMT_RGB_U8: + return total_dim * 3 * sizeof( u8 ); + case TEXFMT_RGB_U8_SRGB: + return total_dim * 3 * sizeof( u8 ); + case TEXFMT_RGB_FLOAT: + return total_dim * 3 * sizeof( float ); + + case TEXFMT_RGBA_U8: + return total_dim * 4 * sizeof( u8 ); + case TEXFMT_RGBA_U8_SRGB: + return total_dim * 4 * sizeof( u8 ); + case TEXFMT_RGBA_FLOAT: + return total_dim * 4 * sizeof( float ); + + case TEXFMT_BC1: + case TEXFMT_BC1_SRGB: + return total_dim / 2; case TEXFMT_BC4: return total_dim / 2; - default: - FATAL( "unsupported TextureFormat: {}", format ); - return GL_INVALID_ENUM; + case TEXFMT_BC5: + return total_dim; } + + FATAL( "unsupported TextureFormat: {}", format ); + return GL_INVALID_ENUM; } Texture renderer_new_texture( TextureConfig config ) { @@ -813,10 +836,10 @@ Texture renderer_new_texture( TextureConfig config ) { glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, ( GLfloat * ) &config.border_colour ); } - GLenum internal_format = textureformat_to_glenum( config.format, config.srgb ); + GLenum internal_format = texfmt_to_internal_format( config.format ); if( is_compressed( config.format ) ) { - u32 dxt_size = texture_byte_size( config.width, config.height, TEXFMT_BC4, false ); + u32 dxt_size = texture_byte_size( config.width, config.height, config.format, false ); glCompressedTexImage2D( GL_TEXTURE_2D, 0, internal_format, config.width, config.height, 0, dxt_size, config.data ); } @@ -825,18 +848,6 @@ Texture renderer_new_texture( TextureConfig config ) { GLenum type = GL_INVALID_ENUM; switch( config.format ) { - case TEXFMT_RGBA_FLOAT: - channels = GL_RGBA; - type = GL_FLOAT; - break; - case TEXFMT_RGB_FLOAT: - channels = GL_RGB; - type = GL_FLOAT; - break; - case TEXFMT_R_FLOAT: - channels = GL_RED; - type = GL_FLOAT; - break; case TEXFMT_R_U8: case TEXFMT_R_U8NORM: channels = GL_RED; @@ -846,10 +857,36 @@ Texture renderer_new_texture( TextureConfig config ) { channels = GL_RED; type = GL_UNSIGNED_SHORT; break; + case TEXFMT_R_FLOAT: + channels = GL_RED; + type = GL_FLOAT; + break; + + case TEXFMT_RGB_U8: + case TEXFMT_RGB_U8_SRGB: + channels = GL_RGB; + type = GL_UNSIGNED_BYTE; + break; + case TEXFMT_RGB_FLOAT: + channels = GL_RGB; + type = GL_FLOAT; + break; + + case TEXFMT_RGBA_U8: + case TEXFMT_RGBA_U8_SRGB: + channels = GL_RGBA; + type = GL_UNSIGNED_BYTE; + break; + case TEXFMT_RGBA_FLOAT: + channels = GL_RGBA; + type = GL_FLOAT; + break; + case TEXFMT_DEPTH: channels = GL_DEPTH_COMPONENT; type = GL_FLOAT; break; + default: FATAL( "implement channel/type selection for {}", config.format ); break; diff --git a/renderer.h b/renderer.h @@ -57,17 +57,24 @@ enum PrimitiveType { enum TextureFormat { TEXFMT_INVALID, - TEXFMT_RGBA_FLOAT, - TEXFMT_RGB_FLOAT, - TEXFMT_R_FLOAT, + TEXFMT_R_U8, TEXFMT_R_U8NORM, TEXFMT_R_U16, + TEXFMT_R_FLOAT, + + TEXFMT_RGB_U8, + TEXFMT_RGB_U8_SRGB, + TEXFMT_RGB_FLOAT, + + TEXFMT_RGBA_U8, + TEXFMT_RGBA_U8_SRGB, + TEXFMT_RGBA_FLOAT, TEXFMT_DEPTH, TEXFMT_BC1, - TEXFMT_BC3, + TEXFMT_BC1_SRGB, TEXFMT_BC4, TEXFMT_BC5, }; @@ -183,8 +190,6 @@ struct TextureConfig { bool has_mipmaps = false; v4 border_colour; - bool srgb = false; // TODO: this should be true by default. maybe needs enum TextureColorSpace too - // TODO: or it should be merged with TextureFormat since only some formats make sense with sRGB }; enum ClearColourBool { RENDERER_CLEAR_COLOUR_DONT, RENDERER_CLEAR_COLOUR_DO };