medfall

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

commit e4ee342ba7a62eb62eed04e5aaadb81f32737a01
parent 7c120feccc74f9b042ee423e36d0a2cc070880cf
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Thu Nov  3 19:22:46 +0200

Do UBOs properly

Diffstat:
bsp.cc | 4++--
renderer.cc | 22++++++++++++++++------
renderer.h | 5+++++
3 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/bsp.cc b/bsp.cc @@ -24,7 +24,7 @@ static const GLchar * const vert_src = GLSL( out vec3 frag_colour; - layout( std140 ) uniform uniforms { + layout( std140 ) uniform v_hot { mat4 VP; }; @@ -375,7 +375,7 @@ extern "C" GAME_FRAME( game_frame ) { RenderState render_state = { }; render_state.shader = game->test_shader; - render_state.ubs[ 0 ] = test_ub; + render_state.ubs[ UBO_VS_HOT ] = test_ub; bspr_render( &game->bspr, GLMV3( game->pos ), render_state ); immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); diff --git a/renderer.cc b/renderer.cc @@ -170,6 +170,15 @@ Shader renderer_new_shader( ShaderConfig config ) { glLinkProgram( program ); + const char * ubo_names[] = { "v_hot", "v_cold", "f_hot", "f_cold" }; + const GLuint ubo_bindings[] = { UBO_VS_HOT, UBO_VS_COLD, UBO_FS_HOT, UBO_FS_COLD }; + 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, ubo_bindings[ i ] ); + } + } + glDeleteShader( vs ); glDeleteShader( fs ); if( gs != 0 ) { @@ -325,17 +334,18 @@ void renderer_draw_mesh( const Mesh & mesh, RenderState state ) { // TODO: check vs OpenGLState if these binds are really necessary glUseProgram( state.shader ); glBindVertexArray( mesh.vao ); - // TODO: don't hardcode 0! - // GLuint ubo_index = glGetUniformBlockIndex( shaderA.Program, "uniforms" ); - GLuint ubo_index = 0; - glBindBufferBase( GL_UNIFORM_BUFFER, ubo_index, state.ubs[ 0 ] ); - // TODO: set depth func, etc - for( int i = 0; i < ARRAY_COUNT( state.textures ); i++ ) { + for( size_t i = 0; i < ARRAY_COUNT( state.ubs ); i++ ) { + glBindBufferBase( GL_UNIFORM_BUFFER, checked_cast< GLuint >( i ), state.ubs[ i ] ); + } + + for( size_t i = 0; i < ARRAY_COUNT( state.textures ); i++ ) { glActiveTexture( GL_TEXTURE0 + i ); glBindTexture( GL_TEXTURE_2D, state.textures[ i ] ); } + // TODO: set depth func, etc + GLenum primitive = primitivetype_to_glenum( mesh.primitive_type ); if( mesh.indexed ) { glDrawElements( primitive, mesh.num_vertices, GL_UNSIGNED_INT, 0 ); diff --git a/renderer.h b/renderer.h @@ -16,6 +16,11 @@ typedef u32 VAO; typedef u32 Shader; typedef u32 Texture; +const u32 UBO_VS_HOT = 0; +const u32 UBO_VS_COLD = 1; +const u32 UBO_FS_HOT = 2; +const u32 UBO_FS_COLD = 3; + enum DrawCallType { DRAWCALL_NEW_VB, DRAWCALL_NEW_IB,