medfall

A super great game engine
Log | Files | Refs

commit f1dfaca068a2640c86150dd3bc83491143341505
parent 572ec91c4bd3862b12520d8b99e3772e0730965d
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon May 15 03:19:31 +0300

Some renderer cleanup

Diffstat:
renderer.cc | 89++++++++++++++++++++++++++++++++++++++++---------------------------------------
renderer.h | 21+++++++++++----------
2 files changed, 56 insertions(+), 54 deletions(-)
diff --git a/renderer.cc b/renderer.cc @@ -3,13 +3,14 @@ #include "intrinsics.h" #include "renderer.h" #include "log.h" -// #include "nonblocking_fixed_spsc_queue.h" +#include "linear_algebra.h" STATIC_ASSERT( SAME_TYPE( VB, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( IB, GLuint ) ); STATIC_ASSERT( SAME_TYPE( UB, GLuint ) ); STATIC_ASSERT( SAME_TYPE( Shader, GLuint ) ); STATIC_ASSERT( SAME_TYPE( Texture, GLuint ) ); -STATIC_ASSERT( SAME_TYPE( TextureBuffer, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( TextureBufferObject, GLuint ) ); STATIC_ASSERT( SAME_TYPE( u32, GLuint ) ); static const GLuint ATTR_POSITION = 0; @@ -40,11 +41,13 @@ static GLenum bufferusage_to_glenum( BufferUsage usage ) { } FATAL( "unsupported BufferUsage: {}", usage ); - return 0; + return GL_INVALID_ENUM; } static GLenum textureformat_to_glenum( TextureFormat format, bool srgb ) { switch( format ) { + case TEXFMT_INVALID: + break; case TEXFMT_RGBA_FLOAT: ASSERT( !srgb ); return GL_RGBA32F; @@ -67,10 +70,10 @@ static GLenum textureformat_to_glenum( TextureFormat format, bool srgb ) { case TEXFMT_BC5: ASSERT( !srgb ); return GL_COMPRESSED_RG_RGTC2; - default: - FATAL( "unsupported TextureFormat: {}", format ); - return 0; } + + FATAL( "unsupported TextureFormat: {}", format ); + return GL_INVALID_ENUM; } static bool is_compressed( TextureFormat format ) { @@ -94,6 +97,10 @@ VB renderer_new_vb( const void * data, u32 len, BufferUsage usage ) { return vbo; } +void renderer_delete_vb( VB vb ) { + glDeleteBuffers( 1, &vb ); +} + IB renderer_new_ib( const void * data, u32 len, BufferUsage usage ) { ASSERT( len < S32_MAX ); @@ -108,6 +115,10 @@ IB renderer_new_ib( const void * data, u32 len, BufferUsage usage ) { return ebo; } +void renderer_delete_ib( IB ib ) { + glDeleteBuffers( 1, &ib ); +} + UB renderer_new_ub( const void * data, u32 len ) { ASSERT( len < S32_MAX ); @@ -122,6 +133,17 @@ UB renderer_new_ub( const void * data, u32 len ) { return ubo; } +void renderer_ub_data( UB ub, const void * data, u32 len ) { + ASSERT( len < S32_MAX ); + + glBindBuffer( GL_UNIFORM_BUFFER, ub ); + glBufferData( GL_UNIFORM_BUFFER, len, data, GL_DYNAMIC_DRAW ); +} + +void renderer_delete_ub( UB ub ) { + glDeleteBuffers( 1, &ub ); +} + TB renderer_new_tb( TextureFormat format, const void * data, u32 len, BufferUsage usage ) { ASSERT( len < S32_MAX ); ASSERT( !is_compressed( format ) ); @@ -139,39 +161,20 @@ TB renderer_new_tb( TextureFormat format, const void * data, u32 len, BufferUsag } TB tb = { }; + tb.tbo = tbo; tb.texture = texture; - tb.tb = tbo; return tb; } -void renderer_ub_data( UB ub, const void * data, u32 len ) { - ASSERT( len < S32_MAX ); - - glBindBuffer( GL_UNIFORM_BUFFER, ub ); - glBufferData( GL_UNIFORM_BUFFER, len, data, GL_DYNAMIC_DRAW ); -} - void renderer_tb_data( TB tb, const void * data, u32 len, BufferUsage usage ) { ASSERT( len < S32_MAX ); - glBindBuffer( GL_TEXTURE_BUFFER, tb.tb ); + glBindBuffer( GL_TEXTURE_BUFFER, tb.tbo ); glBufferData( GL_TEXTURE_BUFFER, len, data, bufferusage_to_glenum( usage ) ); } -void renderer_delete_vb( VB vb ) { - glDeleteBuffers( 1, &vb ); -} - -void renderer_delete_ib( IB ib ) { - glDeleteBuffers( 1, &ib ); -} - -void renderer_delete_ub( UB ub ) { - glDeleteBuffers( 1, &ub ); -} - void renderer_delete_tb( TB tb ) { - glDeleteBuffers( 1, &tb.tb ); + glDeleteBuffers( 1, &tb.tbo ); glDeleteTextures( 1, &tb.texture ); } @@ -285,12 +288,15 @@ void renderer_delete_shader( Shader shader ) { glDeleteProgram( shader ); } -static int mipmap_levels( u32 width, u32 height ) { - int levels = 1; - while( width > 1 || height > 1 ) { +static u32 mipmap_dim( u32 dim, u32 level ) { + return max( dim >> level, u32( 1 ) ); +} + +// TODO: use clz +static u32 mipmap_levels( u32 width, u32 height ) { + u32 levels = 1; + while( mipmap_dim( width, levels ) > 1 || mipmap_dim( height, levels ) > 1 ) { levels++; - width = max( width / 2, u32( 1 ) ); - height = max( height / 2, u32( 1 ) ); } return levels; } @@ -298,10 +304,8 @@ static int mipmap_levels( u32 width, u32 height ) { static u32 texture_byte_size( u32 width, u32 height, TextureFormat format, bool mipmaps ) { u32 total_dim = width * height; if( mipmaps ) { - for( int i = 1; i < mipmap_levels( width, height ); i++ ) { - width = max( width / 2, u32( 1 ) ); - height = max( height / 2, u32( 1 ) ); - total_dim += width * height; + for( u32 i = 1; i < mipmap_levels( width, height ); i++ ) { + total_dim += mipmap_dim( width, i ) * mipmap_dim( height, i ); } } @@ -318,7 +322,7 @@ static u32 texture_byte_size( u32 width, u32 height, TextureFormat format, bool return total_dim / 2; default: FATAL( "unsupported TextureFormat: {}", format ); - return 0; + return GL_INVALID_ENUM; } } @@ -392,14 +396,11 @@ Mesh renderer_new_mesh( MeshConfig config ) { case PRIMITIVETYPE_TRIANGLES: ASSERT( config.num_vertices % 3 == 0 ); break; - case PRIMITIVETYPE_TRIANGLE_STRIP: - ASSERT( config.num_vertices % 3 == 2 ); + ASSERT( config.num_vertices >= 3 ); break; - case PRIMITIVETYPE_POINTS: break; - case PRIMITIVETYPE_LINES: ASSERT( config.num_vertices % 2 == 0 ); break; @@ -512,7 +513,7 @@ static GLenum depthfunc_to_glenum( DepthFunc depth_func ) { } FATAL( "unsupported DepthFunc: {}", depth_func ); - return 0; + return GL_INVALID_ENUM; } static GLenum primitivetype_to_glenum( PrimitiveType primitive_type ) { @@ -524,7 +525,7 @@ static GLenum primitivetype_to_glenum( PrimitiveType primitive_type ) { } FATAL( "unsupported PrimitiveType: {}", primitive_type ); - return 0; + return GL_INVALID_ENUM; } static RenderState previous_render_state; diff --git a/renderer.h b/renderer.h @@ -13,7 +13,7 @@ typedef u32 IB; typedef u32 UB; typedef u32 Shader; typedef u32 Texture; -typedef u32 TextureBuffer; +typedef u32 TextureBufferObject; const u32 UB_VS_HOT = 0; const u32 UB_VS_COLD = 1; @@ -42,8 +42,8 @@ enum BufferUsage { }; struct TB { + TextureBufferObject tbo; Texture texture; - u32 tb; }; #define RENDERER_MAX_TEXTURES 4 @@ -51,10 +51,10 @@ struct TB { #define RENDERER_MAX_UBS 4 struct RenderState { + UB ubs[ RENDERER_MAX_UBS ] = { }; Texture textures[ RENDERER_MAX_TEXTURES ] = { }; TB tbs[ RENDERER_MAX_TEXTURE_BUFFERS ] = { }; - UB ubs[ RENDERER_MAX_UBS ] = { }; - Shader shader = 0; + Shader shader = INVALID_SHADER; DepthFunc depth_func = DEPTHFUNC_LESS; CullFace cull_face = CULLFACE_BACK; bool disable_depth_writes = false; @@ -149,17 +149,18 @@ enum ClearDepthBool { CLEARDEPTH_DONT, CLEARDEPTH_DO }; void renderer_begin_frame( ClearColourBool clear_colour = CLEARCOLOUR_DO, ClearDepthBool clear_depth = CLEARDEPTH_DO ); VB renderer_new_vb( const void * data = NULL, u32 len = 0, BufferUsage usage = BUFFERUSAGE_STATIC ); -IB renderer_new_ib( const void * data = NULL, u32 len = 0, BufferUsage usage = BUFFERUSAGE_STATIC ); -UB renderer_new_ub( const void * data = NULL, u32 len = 0 ); -TB renderer_new_tb( TextureFormat format, const void * data = NULL, u32 len = 0, BufferUsage usage = BUFFERUSAGE_DYNAMIC ); - void renderer_delete_vb( VB vb ); + +IB renderer_new_ib( const void * data = NULL, u32 len = 0, BufferUsage usage = BUFFERUSAGE_STATIC ); void renderer_delete_ib( IB ib ); -void renderer_delete_ub( UB ub ); -void renderer_delete_tb( TB ub ); +UB renderer_new_ub( const void * data = NULL, u32 len = 0 ); void renderer_ub_data( UB ub, const void * data, u32 len ); +void renderer_delete_ub( UB ub ); + +TB renderer_new_tb( TextureFormat format, const void * data = NULL, u32 len = 0, BufferUsage usage = BUFFERUSAGE_DYNAMIC ); void renderer_tb_data( TB tb, const void * data, u32 len, BufferUsage usage = BUFFERUSAGE_DYNAMIC ); +void renderer_delete_tb( TB ub ); Shader renderer_new_shader( ShaderConfig config ); Shader renderer_new_shader( const char * src );