medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 9c4e1158a7efa0ee6f45a056f051dcdb2e190ba3
parent 1a316c8c9ac1f476f56ce3c19b311e9cdc539ad4
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Nov 28 18:45:37 +0200

Assert VB/IB/etc are the same type as GLuint

Diffstat:
intrinsics.h | 11+++++++++++
renderer.cc | 88+++++++++++++++++++++++++++++++++++++------------------------------------------
2 files changed, 52 insertions(+), 47 deletions(-)
diff --git a/intrinsics.h b/intrinsics.h @@ -150,6 +150,17 @@ ScopeExit< F > MakeScopeExit( F f ) { }; #define SCOPE_EXIT( code ) auto CONCAT( SCOPE_EXIT_, __COUNTER__ ) = MakeScopeExit( [=]() { code; } ) +template< typename S, typename T > +struct SameType { + enum { value = false }; +}; +template< typename T > +struct SameType< T, T > { + enum { value = true }; +}; + +#define SAME_TYPE( S, T ) SameType< S, T >::value + template< typename To, typename From > To checked_cast( const From & from ) { To result = To( from ); diff --git a/renderer.cc b/renderer.cc @@ -5,14 +5,13 @@ #include "log.h" // #include "nonblocking_fixed_spsc_queue.h" -STATIC_ASSERT( sizeof( VB ) == sizeof( GLuint ) ); -STATIC_ASSERT( sizeof( IB ) == sizeof( GLuint ) ); -STATIC_ASSERT( sizeof( UB ) == sizeof( GLuint ) ); -STATIC_ASSERT( sizeof( VAO ) == sizeof( GLuint ) ); -STATIC_ASSERT( sizeof( Shader ) == sizeof( GLuint ) ); -STATIC_ASSERT( sizeof( Texture ) == sizeof( GLuint ) ); -STATIC_ASSERT( sizeof( TextureBuffer ) == sizeof( GLuint ) ); -STATIC_ASSERT( sizeof( u32 ) == sizeof( GL_UNSIGNED_INT ) ); // for indexed rendering +STATIC_ASSERT( SAME_TYPE( VB, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( UB, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( VAO, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( Shader, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( Texture, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( TextureBuffer, GLuint ) ); +STATIC_ASSERT( SAME_TYPE( u32, GLuint ) ); // for indexed rendering static const GLuint ATTR_POSITION = 0; static const GLuint ATTR_NORMAL = 1; @@ -51,6 +50,11 @@ void renderer_begin_frame( ClearColourBool clear_colour, ClearDepthBool clear_de clear_mask |= clear_depth == CLEARDEPTH_DO ? GL_DEPTH_BUFFER_BIT : 0; glClear( clear_mask ); } + + GLenum error = glGetError(); + if( error != GL_NO_ERROR ) { + printf( "glGetError: %d\n", error ); + } } static GLenum bufferusage_to_glenum( BufferUsage usage ) { @@ -85,7 +89,7 @@ VB renderer_new_vb( const void * data, size_t len, BufferUsage usage ) { glBufferData( GL_ARRAY_BUFFER, len, data, bufferusage_to_glenum( usage ) ); } - return checked_cast< VB >( vbo ); + return vbo; } IB renderer_new_ib( const void * data, size_t len, BufferUsage usage ) { @@ -97,7 +101,7 @@ IB renderer_new_ib( const void * data, size_t len, BufferUsage usage ) { glBufferData( GL_ELEMENT_ARRAY_BUFFER, len, data, bufferusage_to_glenum( usage ) ); } - return checked_cast< IB >( ebo ); + return ebo; } UB renderer_new_ub( const void * data, size_t len ) { @@ -109,7 +113,7 @@ UB renderer_new_ub( const void * data, size_t len ) { glBufferData( GL_UNIFORM_BUFFER, len, data, GL_DYNAMIC_DRAW ); } - return checked_cast< UB >( ubo ); + return ubo; } TB renderer_new_tb( TextureFormat format, const void * data, size_t len, BufferUsage usage ) { @@ -127,43 +131,35 @@ TB renderer_new_tb( TextureFormat format, const void * data, size_t len, BufferU TB tb = { }; tb.texture = texture; - tb.tb = checked_cast< TextureBuffer >( tbo ); + tb.tb = tbo; return tb; } void renderer_ub_data( UB ub, const void * data, size_t len ) { - GLuint ubo = checked_cast< GLuint >( ub ); - glBindBuffer( GL_UNIFORM_BUFFER, ubo ); + glBindBuffer( GL_UNIFORM_BUFFER, ub ); glBufferData( GL_UNIFORM_BUFFER, len, data, GL_DYNAMIC_DRAW ); } void renderer_tb_data( TB tb, const void * data, size_t len, BufferUsage usage ) { - GLuint tbo = checked_cast< GLuint >( tb.tb ); - glBindBuffer( GL_TEXTURE_BUFFER, tbo ); + glBindBuffer( GL_TEXTURE_BUFFER, tb.tb ); glBufferData( GL_TEXTURE_BUFFER, len, data, bufferusage_to_glenum( usage ) ); } void renderer_delete_vb( VB vb ) { - GLuint vbo = checked_cast< GLuint >( vb ); - glDeleteBuffers( 1, &vbo ); + glDeleteBuffers( 1, &vb ); } void renderer_delete_ib( IB ib ) { - GLuint ebo = checked_cast< GLuint >( ib ); - glDeleteBuffers( 1, &ebo ); + glDeleteBuffers( 1, &ib ); } void renderer_delete_ub( UB ub ) { - GLuint ubo = checked_cast< GLuint >( ub ); - glDeleteBuffers( 1, &ubo ); + glDeleteBuffers( 1, &ub ); } void renderer_delete_tb( TB tb ) { - GLuint tbo = checked_cast< GLuint >( tb.tb ); - glDeleteBuffers( 1, &tbo ); - - GLuint texture = checked_cast< GLuint >( tb.texture ); - glDeleteTextures( 1, &texture ); + glDeleteBuffers( 1, &tb.tb ); + glDeleteTextures( 1, &tb.texture ); } static GLuint new_gl_shader( GLenum type, const char * src ) { @@ -250,7 +246,7 @@ Shader renderer_new_shader( ShaderConfig config ) { for( size_t i = 0; i < ARRAY_COUNT( ubo_names ); i++ ) { GLuint idx = glGetUniformBlockIndex( program, ubo_names[ i ] ); if( idx != GL_INVALID_INDEX ) { - glUniformBlockBinding( program, idx, checked_cast< GLuint >( ubo_bindings[ i ] ) ); + glUniformBlockBinding( program, idx, ubo_bindings[ i ] ); } } @@ -276,7 +272,7 @@ Shader renderer_new_shader( ShaderConfig config ) { } glUseProgram( 0 ); - return checked_cast< Shader >( program ); + return program; } Shader renderer_new_shader( const char * vertex_src, const char * fragment_src ) { @@ -324,15 +320,14 @@ Texture renderer_new_texture( TextureConfig config ) { break; } - glTexImage2D( GL_TEXTURE_2D, 0, internal_format, + glTexImage2D( GL_TEXTURE_2D, 0, internal_format, config.width, config.height, 0, channels, type, config.data ); return static_cast< Texture >( texture ); } void renderer_delete_texture( Texture texture ) { - GLuint gl_texture = checked_cast< GLuint >( texture ); - glDeleteTextures( 1, &gl_texture ); + glDeleteTextures( 1, &texture ); } static GLvoid * offset_to_glvoidp( u32 offset ) { @@ -348,36 +343,36 @@ Mesh renderer_new_mesh( MeshConfig config ) { if( config.unified_buffer == 0 ) { ASSERT( config.positions != 0 ); - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.positions ) ); + glBindBuffer( GL_ARRAY_BUFFER, config.positions ); glEnableVertexAttribArray( ATTR_POSITION ); glVertexAttribPointer( ATTR_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0 ); if( config.normals != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.normals ) ); + glBindBuffer( GL_ARRAY_BUFFER, config.normals ); glEnableVertexAttribArray( ATTR_NORMAL ); glVertexAttribPointer( ATTR_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0 ); } if( config.tex_coords0 != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.tex_coords0 ) ); + glBindBuffer( GL_ARRAY_BUFFER, config.tex_coords0 ); glEnableVertexAttribArray( ATTR_TEX_COORD0 ); glVertexAttribPointer( ATTR_TEX_COORD0, 2, GL_FLOAT, GL_FALSE, 0, 0 ); } if( config.tex_coords1 != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.tex_coords1 ) ); + glBindBuffer( GL_ARRAY_BUFFER, config.tex_coords1 ); glEnableVertexAttribArray( ATTR_TEX_COORD1 ); glVertexAttribPointer( ATTR_TEX_COORD1, 2, GL_FLOAT, GL_FALSE, 0, 0 ); } if( config.colours != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.colours ) ); + glBindBuffer( GL_ARRAY_BUFFER, config.colours ); glEnableVertexAttribArray( ATTR_COLOUR ); glVertexAttribPointer( ATTR_COLOUR, 3, GL_FLOAT, GL_FALSE, 0, 0 ); } } else { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.unified_buffer ) ); + glBindBuffer( GL_ARRAY_BUFFER, config.unified_buffer ); glEnableVertexAttribArray( ATTR_POSITION ); glVertexAttribPointer( ATTR_POSITION, 3, GL_FLOAT, GL_FALSE, config.stride, offset_to_glvoidp( config.positions_offset ) ); @@ -404,7 +399,7 @@ Mesh renderer_new_mesh( MeshConfig config ) { } if( config.indices != 0 ) { - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, checked_cast< GLuint >( config.indices ) ); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, config.indices ); } glBindVertexArray( 0 ); @@ -418,8 +413,7 @@ Mesh renderer_new_mesh( MeshConfig config ) { } void renderer_delete_mesh( const Mesh & mesh ) { - GLuint vao = checked_cast< GLuint >( mesh.vao ); - glDeleteVertexArrays( 1, &vao ); + glDeleteVertexArrays( 1, &mesh.vao ); } static GLenum depthfunc_to_glenum( DepthFunc depth_func ) { @@ -452,20 +446,20 @@ void renderer_draw_mesh( const Mesh & mesh, RenderState state ) { glBindVertexArray( mesh.vao ); // uniforms - for( size_t i = 0; i < RENDERER_MAX_UBS; i++ ) { - glBindBufferBase( GL_UNIFORM_BUFFER, checked_cast< GLuint >( i ), state.ubs[ i ] ); + for( GLuint i = 0; i < RENDERER_MAX_UBS; i++ ) { + glBindBufferBase( GL_UNIFORM_BUFFER, i, state.ubs[ i ] ); } // textures - for( size_t i = 0; i < RENDERER_MAX_TEXTURES; i++ ) { + for( GLuint i = 0; i < RENDERER_MAX_TEXTURES; i++ ) { glActiveTexture( GL_TEXTURE0 + i ); - glBindTexture( GL_TEXTURE_2D, checked_cast< GLuint >( state.textures[ i ] ) ); + glBindTexture( GL_TEXTURE_2D, state.textures[ i ] ); } // texture buffers - for( size_t i = 0; i < RENDERER_MAX_TEXTURE_BUFFERS; i++ ) { + for( GLuint i = 0; i < RENDERER_MAX_TEXTURE_BUFFERS; i++ ) { glActiveTexture( GL_TEXTURE0 + i + RENDERER_MAX_TEXTURES ); - glBindTexture( GL_TEXTURE_BUFFER, checked_cast< GLuint >( state.tbs[ i ].texture ) ); + glBindTexture( GL_TEXTURE_BUFFER, state.tbs[ i ].texture ); } // depth writing