medfall

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

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

Use UBOs/TBOs in the terrain renderer

Diffstat:
terrain_manager.cc | 98++++++++++++++++++++++++++++++++++++-------------------------------------------
terrain_manager.h | 32+++++---------------------------
2 files changed, 50 insertions(+), 80 deletions(-)
diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -1,8 +1,6 @@ #include <stdio.h> -#include "glad.h" #include <glm/glm.hpp> -#include <glm/gtc/type_ptr.hpp> #include <GLFW/glfw3.h> #include "intrinsics.h" @@ -24,8 +22,10 @@ static const GLchar * const vert_src = GLSL( out vec4 vpos; out vec3 smooth_position; - uniform mat4 v; - uniform mat4 vp; + layout( std140 ) uniform v_hot { + mat4 v; + mat4 vp; + }; void main() { gl_Position = vp * vec4( position, 1.0 ); @@ -40,10 +40,13 @@ static const GLchar * frag_src = GLSL( out vec4 colour; - uniform float sun; + layout( std140 ) uniform f_hot { + vec2 dimensions; + float sun; + }; + uniform sampler2D normals; uniform sampler2D horizons; - uniform vec2 dimensions; // uniform usamplerBuffer point_light_counts_and_offsets; // uniform usamplerBuffer point_light_indices; @@ -252,29 +255,21 @@ void terrain_init( } } - tm->shader = renderer_new_shader( vert_src, frag_src ); - - tm->at_position = glGetAttribLocation( tm->shader, "position" ); - tm->un_V = glGetUniformLocation( tm->shader, "v" ); - tm->un_VP = glGetUniformLocation( tm->shader, "vp" ); - tm->un_sun = glGetUniformLocation( tm->shader, "sun" ); - tm->un_normals = glGetUniformLocation( tm->shader, "normals" ); - tm->un_horizons = glGetUniformLocation( tm->shader, "horizons" ); - tm->un_dimensions = glGetUniformLocation( tm->shader, "dimensions" ); - - // tm->un_point_light_counts_and_offsets = glGetUniformLocation( tm->shader, "point_light_counts_and_offsets" ); - // tm->un_point_light_indices = glGetUniformLocation( tm->shader, "point_light_indices" ); - tm->un_point_light_origins = glGetUniformLocation( tm->shader, "point_light_origins" ); - tm->un_point_light_colours = glGetUniformLocation( tm->shader, "point_light_colours" ); - - // glGenBuffers( 1, &tm->tbo_point_light_counts_and_offsets ); - // glGenBuffers( 1, &tm->tbo_point_light_indices ); - glGenBuffers( 1, &tm->tbo_point_light_origins ); - glGenBuffers( 1, &tm->tbo_point_light_colours ); - // glGenTextures( 1, &tm->tex_point_light_counts_and_offsets ); - // glGenTextures( 1, &tm->tex_point_light_indices ); - glGenTextures( 1, &tm->tex_point_light_origins ); - glGenTextures( 1, &tm->tex_point_light_colours ); + { + ShaderConfig config = { }; + config.vertex_src = vert_src; + config.fragment_src = frag_src; + config.texture_uniform_names[ 0 ] = "normals"; + config.texture_uniform_names[ 1 ] = "horizons"; + config.texture_buffer_uniform_names[ 0 ] = "point_light_origins"; + config.texture_buffer_uniform_names[ 1 ] = "point_light_colours"; + tm->shader = renderer_new_shader( config ); + } + + tm->vertex_uniforms = renderer_new_ub(); + tm->fragment_uniforms = renderer_new_ub(); + tm->point_light_origins = renderer_new_tb( TEXFMT_RGB_FLOAT ); + tm->point_light_colours = renderer_new_tb( TEXFMT_RGB_FLOAT ); tm->first_teleport = true; } @@ -380,6 +375,16 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { } } +struct VSData { + glm::mat4 v; + glm::mat4 vp; +}; + +struct FSData { + glm::vec2 dimensions; + float sun; +}; + void terrain_render( TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun ) { u32 inited = 0; for( u16 vy = 0; vy < VIEW_SIZE; vy++ ) { @@ -396,7 +401,6 @@ void terrain_render( TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun ) // TODO: move this into terrain_prepare_render? const Heightmap * hm = &tm->decompressed_tiles[ tx ][ ty ].heightmap; const OffsetHeightmap ohm = { *hm, tx * TILE_SIZE, ty * TILE_SIZE }; - // printf( "gpubtt_init at %d %d %p %p\n", tx, ty, tm->btts[ tx ][ ty ].left_root, tm->btts[ tx ][ ty ].right_root ); gpubtt_init( tm->arena, &tm->gpubtts[ tx ][ ty ], @@ -413,14 +417,11 @@ void terrain_render( TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun ) } } - glUseProgram( tm->shader ); + VSData vsdata = { V, VP }; + FSData fsdata = { glm::vec2( TILE_SIZE, TILE_SIZE ), sun }; - glUniformMatrix4fv( tm->un_V, 1, GL_FALSE, glm::value_ptr( V ) ); - glUniformMatrix4fv( tm->un_VP, 1, GL_FALSE, glm::value_ptr( VP ) ); - glUniform1f( tm->un_sun, sun ); - glUniform2f( tm->un_dimensions, TILE_SIZE, TILE_SIZE ); - glUniform1i( tm->un_normals, 0 ); - glUniform1i( tm->un_horizons, 1 ); + renderer_ub_data( tm->vertex_uniforms, &vsdata, sizeof( vsdata ) ); + renderer_ub_data( tm->fragment_uniforms, &fsdata, sizeof( fsdata ) ); /* start lighting */ float tbo1[ 15 ] = { @@ -430,13 +431,7 @@ void terrain_render( TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun ) 800, 1500 + 500 * sin( glfwGetTime() + 3.14 * 4 / 5 ), 15, 900, 1500 + 500 * sin( glfwGetTime() + 3.14 * 5 / 5 ), 15, }; - glBindBuffer( GL_TEXTURE_BUFFER, tm->tbo_point_light_origins ); - glBufferData( GL_TEXTURE_BUFFER, sizeof( tbo1 ), tbo1, GL_DYNAMIC_DRAW ); - glBindBuffer( GL_TEXTURE_BUFFER, 0 ); - glActiveTexture( GL_TEXTURE0 + 2 ); - glBindTexture( GL_TEXTURE_BUFFER, tm->tex_point_light_origins ); - glTexBuffer( GL_TEXTURE_BUFFER, GL_RGB32UI, tm->tbo_point_light_origins ); - glUniform1i( tm->un_point_light_origins, 2 ); + renderer_tb_data( tm->point_light_origins, tbo1, sizeof( tbo1 ) ); float tbo2[ 15 ] = { 15, 0, 0, @@ -445,17 +440,16 @@ void terrain_render( TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun ) 0, 15, 15, 0, 0, 15, }; - glBindBuffer( GL_TEXTURE_BUFFER, tm->tbo_point_light_colours ); - glBufferData( GL_TEXTURE_BUFFER, sizeof( tbo2 ), tbo2, GL_DYNAMIC_DRAW ); - glBindBuffer( GL_TEXTURE_BUFFER, 0 ); - glActiveTexture( GL_TEXTURE0 + 3 ); - glBindTexture( GL_TEXTURE_BUFFER, tm->tex_point_light_colours ); - glTexBuffer( GL_TEXTURE_BUFFER, GL_RGB32F, tm->tbo_point_light_colours ); - glUniform1i( tm->un_point_light_colours, 3 ); + renderer_tb_data( tm->point_light_colours, tbo2, sizeof( tbo2 ) ); /* end lighting */ RenderState render_state = { }; render_state.shader = tm->shader; + render_state.ubs[ UB_VS_HOT ] = tm->vertex_uniforms; + render_state.ubs[ UB_FS_HOT ] = tm->fragment_uniforms; + render_state.tbs[ 0 ] = tm->point_light_origins; + render_state.tbs[ 1 ] = tm->point_light_colours; + for( u16 vy = 0; vy < VIEW_SIZE; vy++ ) { for( u16 vx = 0; vx < VIEW_SIZE; vx++ ) { s32 tx = vx + tm->tile_x - VIEW_HALF; @@ -469,8 +463,6 @@ void terrain_render( TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun ) } } } - - glUseProgram( 0 ); } float terrain_height( const TerrainManager * tm, glm::vec3 position ) { diff --git a/terrain_manager.h b/terrain_manager.h @@ -54,33 +54,11 @@ struct TerrainManager { u32 width, height; - GLuint shader; - - GLint at_position; - GLint at_colour; - GLint un_V; - GLint un_VP; - - GLint at_normal; - GLint at_lit; - GLint un_sun; - GLint un_normals; - GLint un_horizons; - GLint un_dimensions; - - // clustered shading stuff - // GLint un_point_light_counts_and_offsets; - // GLint un_point_light_indices; - GLint un_point_light_origins; - GLint un_point_light_colours; - // GLuint tbo_point_light_counts_and_offsets; - // GLuint tbo_point_light_indices; - GLuint tbo_point_light_origins; - GLuint tbo_point_light_colours; - // GLuint tex_point_light_counts_and_offsets; - // GLuint tex_point_light_indices; - GLuint tex_point_light_origins; - GLuint tex_point_light_colours; + Shader shader; + UB vertex_uniforms; + UB fragment_uniforms; + TB point_light_origins; + TB point_light_colours; bool first_teleport; // tile_x and tile_y are the coordinates of the tile we are centered on