medfall

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

commit 185341238a2ccb5d515c602cf7304a9dfc676a44
parent b34ea80f37609a3c53254971f061e98cb1dd5ce1
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Nov  5 12:45:02 +0200

Add texture buffer support to the renderer

Diffstat:
renderer.cc | 48++++++++++++++++++++++++++++++++++++++++++++++++
renderer.h | 12++++++++++++
2 files changed, 60 insertions(+), 0 deletions(-)
diff --git a/renderer.cc b/renderer.cc @@ -11,6 +11,7 @@ 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 const GLuint ATTR_POSITION = 0; static const GLuint ATTR_NORMAL = 1; @@ -114,11 +115,35 @@ UB renderer_new_ub( const void * data, size_t len ) { return checked_cast< UB >( ubo ); } +TB renderer_new_tb( TextureFormat format, const void * data, size_t len, BufferUsage usage ) { + GLuint tbo, texture; + glGenBuffers( 1, &tbo ); + glGenTextures( 1, &texture ); + + glBindBuffer( GL_TEXTURE_BUFFER, tbo ); + glBindTexture( GL_TEXTURE_BUFFER, texture ); + glTexBuffer( GL_TEXTURE_BUFFER, textureformat_to_glenum( format ), tbo ); + + if( data != NULL ) { + glBufferData( GL_TEXTURE_BUFFER, len, data, bufferusage_to_glenum( usage ) ); + } + + TB tb = { }; + tb.texture = texture; + tb.tb = checked_cast< TextureBuffer >( tbo ); + return tb; +} + void renderer_ub_data( UB ub, const void * data, size_t len ) { 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 ) { + 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 ); @@ -134,6 +159,14 @@ void renderer_delete_ub( UB ub ) { glDeleteBuffers( 1, &ubo ); } +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 ); +} + static GLuint new_gl_shader( GLenum type, const char * src ) { GLuint shader = glCreateShader( type ); glShaderSource( shader, 1, &src, NULL ); @@ -232,6 +265,16 @@ Shader renderer_new_shader( ShaderConfig config ) { glUniform1i( uniform, i ); } } + + for( size_t i = 0; i < RENDERER_MAX_TEXTURE_BUFFERS; i++ ) { + if( config.texture_buffer_uniform_names[ i ] != NULL ) { + GLuint uniform = glGetUniformLocation( program, config.texture_buffer_uniform_names[ i ] ); + if( uniform == GLuint( -1 ) ) { + FATAL( "couldn't find texturebuffer uniform: %s", config.texture_buffer_uniform_names[ i ] ); + } + glUniform1i( uniform, i + RENDERER_MAX_TEXTURES ); + } + } glUseProgram( 0 ); return checked_cast< Shader >( program ); @@ -373,6 +416,11 @@ void renderer_draw_mesh( const Mesh & mesh, RenderState state ) { glBindTexture( GL_TEXTURE_2D, checked_cast< GLuint >( state.textures[ i ] ) ); } + for( size_t 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 ) ); + } + // TODO: set depth func, etc GLenum primitive = primitivetype_to_glenum( mesh.primitive_type ); diff --git a/renderer.h b/renderer.h @@ -15,6 +15,7 @@ typedef u32 UB; typedef u32 VAO; typedef u32 Shader; typedef u32 Texture; +typedef u32 TextureBuffer; const u32 UB_VS_HOT = 0; const u32 UB_VS_COLD = 1; @@ -43,11 +44,18 @@ enum BufferUsage { BUFFERUSAGE_STREAM, }; +struct TB { + Texture texture; + u32 tb; +}; + #define RENDERER_MAX_TEXTURES 4 +#define RENDERER_MAX_TEXTURE_BUFFERS 4 #define RENDERER_MAX_UBS 4 struct RenderState { Texture textures[ RENDERER_MAX_TEXTURES ]; + TB tbs[ RENDERER_MAX_TEXTURE_BUFFERS ]; UB ubs[ RENDERER_MAX_UBS ]; Shader shader; DepthFunc depth_func; @@ -84,6 +92,7 @@ struct ShaderConfig { const char * fragment_src; const char * geometry_src; const char * texture_uniform_names[ RENDERER_MAX_TEXTURES ]; + const char * texture_buffer_uniform_names[ RENDERER_MAX_TEXTURE_BUFFERS ]; }; enum TextureFormat { @@ -110,12 +119,15 @@ void renderer_begin_frame( ClearColourBool clear_colour = CLEARCOLOUR_DO, ClearD VB renderer_new_vb( const void * data = NULL, size_t len = 0, BufferUsage usage = BUFFERUSAGE_STATIC ); IB renderer_new_ib( const void * data = NULL, size_t len = 0, BufferUsage usage = BUFFERUSAGE_STATIC ); UB renderer_new_ub( const void * data = NULL, size_t len = 0 ); +TB renderer_new_tb( TextureFormat format, const void * data = NULL, size_t len = 0, BufferUsage usage = BUFFERUSAGE_DYNAMIC ); void renderer_delete_vb( VB vb ); void renderer_delete_ib( IB ib ); void renderer_delete_ub( UB ub ); +void renderer_delete_tb( TB ub ); void renderer_ub_data( UB ub, const void * data, size_t len ); +void renderer_tb_data( TB tb, const void * data, size_t len, BufferUsage usage = BUFFERUSAGE_DYNAMIC ); Shader renderer_new_shader( ShaderConfig config ); Shader renderer_new_shader( const char * vertex_src, const char * fragment_src );