From 329828f689e880b825b396c7be5b243aad6ae5ea Mon Sep 17 00:00:00 2001 From: hgn Date: Tue, 11 Jun 2024 19:35:32 +0100 Subject: [PATCH] framebuffer change --- depth_compare.h | 7 +- network.c | 4 +- render.c | 752 ++++++++---------------------------------------- render.h | 68 +---- skaterift.c | 19 +- workshop.c | 14 +- world.h | 2 +- world_gen.c | 2 +- world_render.c | 68 ++--- world_render.h | 6 - world_water.c | 11 +- 11 files changed, 195 insertions(+), 758 deletions(-) diff --git a/depth_compare.h b/depth_compare.h index 592381b..28e9496 100644 --- a/depth_compare.h +++ b/depth_compare.h @@ -1,5 +1,6 @@ #pragma once #include "vg/vg_m.h" +#include "vg/vg_framebuffer.h" #include "skaterift.h" #include "render.h" @@ -10,13 +11,13 @@ static inline void depth_compare_bind( vg_camera *cam ) { uTexSceneDepth( 5 ); - render_fb_bind_texture( gpipeline.fb_main, 2, 5 ); + vg_framebuffer_bind_texture( gpipeline.fb_main, 2, 5 ); v3f inverse; - render_fb_inverse_ratio( gpipeline.fb_main, inverse ); + vg_framebuffer_inverse_ratio( gpipeline.fb_main, inverse ); inverse[2] = skaterift.cam.farz-skaterift.cam.nearz; uInverseRatioDepth( inverse ); - render_fb_inverse_ratio( NULL, inverse ); + vg_framebuffer_inverse_ratio( NULL, inverse ); inverse[2] = cam->farz-cam->nearz; uInverseRatioMain( inverse ); } diff --git a/network.c b/network.c index 27048fb..dd90edd 100644 --- a/network.c +++ b/network.c @@ -372,7 +372,7 @@ void network_status_string( vg_str *str, u32 *colour ) void render_server_status_gui(void) { - render_fb_bind( gpipeline.fb_workshop_preview, 0 ); + vg_framebuffer_bind( gpipeline.fb_network_status, 1.0f ); vg_ui_set_screen( 128, 48 ); ui_context *ctx = &vg_ui.ctx; @@ -397,7 +397,7 @@ void render_server_status_gui(void) ui_flush( ctx, k_ui_shader_colour, NULL ); skaterift.rt_textures[ k_skaterift_rt_server_status ] = - gpipeline.fb_workshop_preview->attachments[0].id; + gpipeline.fb_network_status->attachments[0].id; } static void on_server_connect_status( CallbackMsg_t *msg ){ diff --git a/render.c b/render.c index 9478124..0c6a8f0 100644 --- a/render.c +++ b/render.c @@ -1,471 +1,10 @@ #include "render.h" #include "vg/vg_engine.h" #include "vg/vg_platform.h" - -struct framebuffer framebuffers[] = -{ - { - /* - * The primary draw target - */ - "main", - .link = &gpipeline.fb_main, - .resolution_div = 1, - .attachments = - { - { - "colour", k_framebuffer_attachment_type_texture, - - .internalformat = GL_RGB, - .format = GL_RGB, - .type = GL_UNSIGNED_BYTE, - .attachment = GL_COLOR_ATTACHMENT0 - }, - { - "motion", k_framebuffer_attachment_type_texture, - - .quality = k_framebuffer_quality_high_only, - .internalformat = GL_RG16F, - .format = GL_RG, - .type = GL_FLOAT, - .attachment = GL_COLOR_ATTACHMENT1 - }, - { -#if 0 - "depth_stencil", k_framebuffer_attachment_type_renderbuffer, - - .internalformat = GL_DEPTH24_STENCIL8, -#else - "depth_stencil", k_framebuffer_attachment_type_texture_depth, - .internalformat = GL_DEPTH24_STENCIL8, - .format = GL_DEPTH_STENCIL, - .type = GL_UNSIGNED_INT_24_8, -#endif - .attachment = GL_DEPTH_STENCIL_ATTACHMENT - } - } - }, - { - /* - * Second rendered view from the perspective of the water reflection - */ - "water_reflection", - .link = &gpipeline.fb_water_reflection, - .resolution_div = 2, - .attachments = - { - { - "colour", k_framebuffer_attachment_type_texture, - .internalformat = GL_RGB, - .format = GL_RGB, - .type = GL_UNSIGNED_BYTE, - .attachment = GL_COLOR_ATTACHMENT0 - }, - { - "depth_stencil", k_framebuffer_attachment_type_renderbuffer, - - .internalformat = GL_DEPTH24_STENCIL8, - .attachment = GL_DEPTH_STENCIL_ATTACHMENT - } - } - }, - { - /* - * Thid rendered view from the perspective of the camera, but just - * captures stuff thats under the water - */ - "water_beneath", - .link = &gpipeline.fb_water_beneath, - .resolution_div = 2, - .attachments = - { - { - "colour", k_framebuffer_attachment_type_texture, - .internalformat = GL_RED, - .format = GL_RED, - .type = GL_UNSIGNED_BYTE, - .attachment = GL_COLOR_ATTACHMENT0 - }, - { - "depth_stencil", k_framebuffer_attachment_type_renderbuffer, - - .internalformat = GL_DEPTH24_STENCIL8, - .attachment = GL_DEPTH_STENCIL_ATTACHMENT - } - } - }, - { - "workshop_preview", - .link = &gpipeline.fb_workshop_preview, - .resolution_div = 0, - .fixed_w = WORKSHOP_PREVIEW_WIDTH, .fixed_h = WORKSHOP_PREVIEW_HEIGHT, - .attachments = - { - { - "colour", k_framebuffer_attachment_type_texture, - .internalformat = GL_RGB, - .format = GL_RGB, - .type = GL_UNSIGNED_BYTE, - .attachment = GL_COLOR_ATTACHMENT0 - }, - { - "depth_stencil", k_framebuffer_attachment_type_renderbuffer, - .internalformat = GL_DEPTH24_STENCIL8, - .attachment = GL_DEPTH_STENCIL_ATTACHMENT - } - } - }, - { - "network_status_ui", - .link = &gpipeline.fb_network_status, - .resolution_div = 0, - .fixed_w = 128, .fixed_h = 48, - .attachments = - { - { - "colour", k_framebuffer_attachment_type_texture, - .internalformat = GL_RGB, - .format = GL_RGB, - .type = GL_UNSIGNED_BYTE, - .attachment = GL_COLOR_ATTACHMENT0 - } - } - } -}; - -/* - * Get the current (automatically scaled or fixed) resolution of framebuffer - */ -void render_fb_get_current_res( struct framebuffer *fb, int *x, int *y ) -{ - if( fb->resolution_div ){ - *x = vg.window_x / fb->resolution_div; - *y = vg.window_y / fb->resolution_div; - } - else{ - *x = fb->fixed_w; - *y = fb->fixed_h; - } -} - -void render_fb_inverse_ratio( framebuffer *fb, v2f inverse ) -{ - if( fb ){ - int x, y; - render_fb_get_current_res( fb, &x, &y ); - - v2f render = { fb->render_w, fb->render_h }, - original = { x, y }; - - v2_div( render, original, inverse ); - } - else{ - v2_div( (v2f){1.0f,1.0f}, (v2f){ vg.window_x, vg.window_y }, inverse ); - } -} - -/* - * Bind framebuffer for drawing to - */ -void render_fb_bind( framebuffer *fb, int use_scaling ) -{ - int x, y; - render_fb_get_current_res( fb, &x, &y ); - - if( use_scaling ){ - x = k_render_scale*(float)x; - y = k_render_scale*(float)y; - - x = VG_MAX( 16, x ); - y = VG_MAX( 16, y ); - - fb->render_w = x; - fb->render_h = y; - } - - glBindFramebuffer( GL_FRAMEBUFFER, fb->fb ); - glViewport( 0, 0, x, y ); -} - -/* - * Bind framebuffer attachment's texture - */ -void render_fb_bind_texture( framebuffer *fb, int attachment, int slot ) -{ - struct framebuffer_attachment *at = &fb->attachments[attachment]; - - if( (at->purpose != k_framebuffer_attachment_type_texture) && - (at->purpose != k_framebuffer_attachment_type_texture_depth) ) - { - vg_fatal_error( "illegal operation: bind non-texture framebuffer" - " attachment to texture slot" ); - } - - glActiveTexture( GL_TEXTURE0 + slot ); - glBindTexture( GL_TEXTURE_2D, fb->attachments[attachment].id ); -} - - -/* - * Shaders - */ - -#define FB_FORMAT_STR( E ) { E, #E }, - -/* - * Convert OpenGL attachment ID enum to string - */ -static const char *render_fb_attachment_str( GLenum e ) -{ - struct { GLenum e; const char *str; } - formats[] = - { - FB_FORMAT_STR(GL_COLOR_ATTACHMENT0) - FB_FORMAT_STR(GL_COLOR_ATTACHMENT1) - FB_FORMAT_STR(GL_COLOR_ATTACHMENT2) - FB_FORMAT_STR(GL_COLOR_ATTACHMENT3) - FB_FORMAT_STR(GL_COLOR_ATTACHMENT4) - FB_FORMAT_STR(GL_DEPTH_STENCIL_ATTACHMENT) - }; - - for( int i=0; ipurpose == k_framebuffer_attachment_type_renderbuffer ){ - glBindRenderbuffer( GL_RENDERBUFFER, a->id ); - glRenderbufferStorage( GL_RENDERBUFFER, a->internalformat, rx, ry ); - } - else if( a->purpose == k_framebuffer_attachment_type_texture || - a->purpose == k_framebuffer_attachment_type_texture_depth ) - { - glBindTexture( GL_TEXTURE_2D, a->id ); - glTexImage2D( GL_TEXTURE_2D, 0, a->internalformat, rx, ry, - 0, a->format, a->type, NULL ); - } -} - -/* - * Full allocation of a framebuffer - */ -void render_fb_allocate( struct framebuffer *fb ) -{ - glGenFramebuffers( 1, &fb->fb ); - glBindFramebuffer( GL_FRAMEBUFFER, fb->fb ); - - int rx, ry; - render_fb_get_current_res( fb, &rx, &ry ); - - vg_info( "allocate_framebuffer( %s, %dx%d )\n", fb->display_name, rx, ry ); - vg_info( "{\n" ); - - GLenum colour_attachments[4]; - u32 colour_count = 0; - - for( int j=0; jattachments); j++ ){ - struct framebuffer_attachment *attachment = &fb->attachments[j]; - - if( attachment->purpose == k_framebuffer_attachment_type_none ) - continue; - - vg_info( " %s: %s\n", - render_fb_attachment_str( attachment->attachment ), - render_fb_format_str( attachment->internalformat ) ); - - if( attachment->purpose == k_framebuffer_attachment_type_renderbuffer ){ - glGenRenderbuffers( 1, &attachment->id ); - render_fb_allocate_texture( fb, attachment ); - glFramebufferRenderbuffer( GL_FRAMEBUFFER, - GL_DEPTH_STENCIL_ATTACHMENT, - GL_RENDERBUFFER, attachment->id ); - } - else if( attachment->purpose == k_framebuffer_attachment_type_texture || - attachment->purpose == k_framebuffer_attachment_type_texture_depth ) - { - glGenTextures( 1, &attachment->id ); - render_fb_allocate_texture( fb, attachment ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); - - glFramebufferTexture2D( GL_FRAMEBUFFER, attachment->attachment, - GL_TEXTURE_2D, attachment->id, 0 ); - - if( attachment->purpose == k_framebuffer_attachment_type_texture ) - colour_attachments[ colour_count ++ ] = attachment->attachment; - } - } - - glDrawBuffers( colour_count, colour_attachments ); - - /* - * Check result - */ - GLenum result = glCheckFramebufferStatus( GL_FRAMEBUFFER ); - - if( result == GL_FRAMEBUFFER_COMPLETE ){ - /* - * Attatch to gpipeline - */ - if( fb->link ) - *fb->link = fb; - - vg_success( " status: complete\n" ); - vg_info( "}\n" ); - } - else{ - if( result == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT ) - vg_error( " status: Incomplete attachment" ); - else if( result == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT ) - vg_error( " status: Missing attachment" ); - else if( result == GL_FRAMEBUFFER_UNSUPPORTED ) - vg_error( " status: Unsupported framebuffer format" ); - else - vg_error( " status: Generic Error" ); - - vg_info( "}\n" ); - vg_fatal_error( "Incomplete framebuffer (see logs)" ); - } -} - -/* - * Resize/Update all framebuffers(we know about) - */ -void render_fb_resize(void) -{ - if( !gpipeline.ready ) return; - - for( int i=0; iattachments); j++ ){ - struct framebuffer_attachment *attachment = &fb->attachments[j]; - render_fb_allocate_texture( fb, attachment ); - } - } -} - -static int render_framebuffer_control( int argc, char const *argv[] ); -static void render_framebuffer_poll( int argc, char const *argv[] ); +#include "vg/vg_framebuffer.h" static void async_render_init( void *payload, u32 size ) { - /* - * Complete Framebuffers - */ - for( int i=0; idisplay_name = "main"; + gpipeline.fb_main->resolution_div = 1; + gpipeline.fb_main->attachments[0] = (vg_framebuffer_attachment) + { + "colour", k_framebuffer_attachment_type_texture, + + .internalformat = GL_RGB, + .format = GL_RGB, + .type = GL_UNSIGNED_BYTE, + .attachment = GL_COLOR_ATTACHMENT0 + }; + gpipeline.fb_main->attachments[1] = (vg_framebuffer_attachment) + { + "motion", k_framebuffer_attachment_type_texture, + + .quality = k_framebuffer_quality_high_only, + .internalformat = GL_RG16F, + .format = GL_RG, + .type = GL_FLOAT, + .attachment = GL_COLOR_ATTACHMENT1 + }; + gpipeline.fb_main->attachments[2] = (vg_framebuffer_attachment) + { + "depth_stencil", k_framebuffer_attachment_type_texture_depth, + .internalformat = GL_DEPTH24_STENCIL8, + .format = GL_DEPTH_STENCIL, + .type = GL_UNSIGNED_INT_24_8, + .attachment = GL_DEPTH_STENCIL_ATTACHMENT + }; + vg_framebuffer_create( gpipeline.fb_main ); + + /* + * Water reflection + */ + gpipeline.fb_water_reflection = vg_framebuffer_allocate( alloc, 2, 1 ); + gpipeline.fb_water_reflection->display_name = "water_reflection"; + gpipeline.fb_water_reflection->resolution_div = 2; + gpipeline.fb_water_reflection->attachments[0] = (vg_framebuffer_attachment) + { + "colour", k_framebuffer_attachment_type_texture, + .internalformat = GL_RGB, + .format = GL_RGB, + .type = GL_UNSIGNED_BYTE, + .attachment = GL_COLOR_ATTACHMENT0 + }; + gpipeline.fb_water_reflection->attachments[1] = (vg_framebuffer_attachment) + { + "depth_stencil", k_framebuffer_attachment_type_renderbuffer, + .internalformat = GL_DEPTH24_STENCIL8, + .attachment = GL_DEPTH_STENCIL_ATTACHMENT + }; + vg_framebuffer_create( gpipeline.fb_water_reflection ); + + /* + * Thid rendered view from the perspective of the camera, but just + * captures stuff thats under the water + */ + gpipeline.fb_water_beneath = vg_framebuffer_allocate( alloc, 2, 1 ); + gpipeline.fb_water_beneath->display_name = "water_beneath"; + gpipeline.fb_water_beneath->resolution_div = 2; + gpipeline.fb_water_beneath->attachments[0] = (vg_framebuffer_attachment) + { + "colour", k_framebuffer_attachment_type_texture, + .internalformat = GL_RED, + .format = GL_RED, + .type = GL_UNSIGNED_BYTE, + .attachment = GL_COLOR_ATTACHMENT0 + }; + gpipeline.fb_water_beneath->attachments[1] = (vg_framebuffer_attachment) + { + "depth_stencil", k_framebuffer_attachment_type_renderbuffer, + .internalformat = GL_DEPTH24_STENCIL8, + .attachment = GL_DEPTH_STENCIL_ATTACHMENT + }; + vg_framebuffer_create( gpipeline.fb_water_beneath ); + + /* + * Workshop preview + */ + gpipeline.fb_workshop_preview = vg_framebuffer_allocate( alloc, 2, 1 ); + gpipeline.fb_workshop_preview->display_name = "workshop_preview"; + gpipeline.fb_workshop_preview->resolution_div = 0; + gpipeline.fb_workshop_preview->fixed_w = WORKSHOP_PREVIEW_WIDTH; + gpipeline.fb_workshop_preview->fixed_h = WORKSHOP_PREVIEW_HEIGHT; + gpipeline.fb_workshop_preview->attachments[0] = (vg_framebuffer_attachment) + { + "colour", k_framebuffer_attachment_type_texture, + .internalformat = GL_RGB, + .format = GL_RGB, + .type = GL_UNSIGNED_BYTE, + .attachment = GL_COLOR_ATTACHMENT0 + }; + gpipeline.fb_workshop_preview->attachments[1] = (vg_framebuffer_attachment) + { + "depth_stencil", k_framebuffer_attachment_type_renderbuffer, + .internalformat = GL_DEPTH24_STENCIL8, + .attachment = GL_DEPTH_STENCIL_ATTACHMENT + }; + vg_framebuffer_create( gpipeline.fb_workshop_preview ); + + /* + * Network status + */ + gpipeline.fb_network_status = vg_framebuffer_allocate( alloc, 1, 1 ); + gpipeline.fb_network_status->display_name = "network_status_ui"; + gpipeline.fb_network_status->resolution_div = 0; + gpipeline.fb_network_status->fixed_w = 128; + gpipeline.fb_network_status->fixed_h = 48; + gpipeline.fb_network_status->attachments[0] = (vg_framebuffer_attachment) + { + "colour", k_framebuffer_attachment_type_texture, + .internalformat = GL_RGB, + .format = GL_RGB, + .type = GL_UNSIGNED_BYTE, + .attachment = GL_COLOR_ATTACHMENT0 + }; + vg_framebuffer_create( gpipeline.fb_network_status ); vg_async_call( async_render_init, NULL, 0 ); } @@ -557,167 +217,3 @@ void render_fsquad2(void) glBindVertexArray( gpipeline.fsquad.vao ); glDrawArrays( GL_TRIANGLES, 66+6,6 ); } - -/* - * Call this inside the UI function - */ -void render_view_framebuffer_ui( ui_context *ctx ) -{ - // FIXME10 -#if 0 - int viewing_count = 0; - - glBindVertexArray( gpipeline.fsquad.vao ); - shader_blit_use(); - shader_blit_uTexMain( 0 ); - - v2f identity = { 1.0f, 1.0f }; - shader_blit_uInverseRatio( identity ); - - for( int i=0; iattachments); j++ ){ - struct framebuffer_attachment *at = &fb->attachments[j]; - - if( !at->debug_view ) - continue; - - v2f corner, - window = { vg.window_x, vg.window_y }; - - corner[0] = viewing_count % 3; - corner[1] = 1 + (viewing_count / 3); - v2_mul( corner, window, corner ); - v2_muls( corner, 0.3f, corner ); - corner[1] = vg.window_y - corner[1]; - - ui_text( (ui_rect){ corner[0], corner[1], 0.0f, 0.0f }, - fb->display_name, 2, k_text_align_left ); - ui_text( (ui_rect){ corner[0], corner[1] + 32, 0.0f, 0.0f, }, - at->display_name, 1, k_text_align_left ); - - if( at->purpose == k_framebuffer_attachment_type_renderbuffer ){ - v2f center; - v2_muladds( corner, window, 0.15f, center ); - - ui_text( (ui_rect){ center[0], center[1], 0.0f, 0.0f }, - "", 1, k_text_align_center ); - } - else{ - render_fb_bind_texture( fb, j, 0 ); - - int start = (viewing_count+2) * 6, - count = 6; - glDrawArrays( GL_TRIANGLES, start, count ); - } - - viewing_count ++; - } - } -#endif -} - -static void render_framebuffer_show( struct framebuffer *fb, - struct framebuffer_attachment *at, - int operation ) -{ - at->debug_view = operation; - vg_info( "%s %s:%s\n", (operation?"shown": "hidden"), - fb->display_name, at->display_name ); -} - -/* - * arg0: command "show"/"hide" - * arg1: framebuffer name /"all" - * arg2: subname /none - */ -static int render_framebuffer_control( int argc, char const *argv[] ) -{ - if( argc < 2 ){ - vg_error( "Usage: fb \"show/hide\" /\"all\" /none\n" ); - return 0; - } - - int modify_all = 0, - operation = 0; - - if( !strcmp( argv[0], "show" ) ) - operation = 1; - else if( !strcmp( argv[0], "hide" ) ) - operation = 0; - else{ - vg_error( "Unknown framebuffer operation: '%s'\n", argv[0] ); - return 0; - } - - if( !strcmp( argv[1], "all" ) ) - modify_all = 1; - - for( int i=0; iattachments); j++ ){ - struct framebuffer_attachment *at = &fb->attachments[j]; - - if( at->purpose == k_framebuffer_attachment_type_none ) - continue; - - if( modify_all ){ - render_framebuffer_show( fb, at, operation ); - } - else{ - if( !strcmp( fb->display_name, argv[1] ) ){ - if( argc == 2 ) - render_framebuffer_show( fb, at, operation ); - else if( !strcmp( at->display_name, argv[2] ) ) - render_framebuffer_show( fb, at, operation ); - } - } - } - } - - return 0; -} - -static void render_framebuffer_poll( int argc, char const *argv[] ) -{ - const char *term = argv[argc-1]; - - if( argc == 1 ){ - console_suggest_score_text( "show", term, 0 ); - console_suggest_score_text( "hide", term, 0 ); - } - else if( argc == 2 ){ - console_suggest_score_text( "all", term, 0 ); - - for( int i=0; idisplay_name, term, 0 ); - } - } - else if( argc == 3 ){ - int modify_all = 0; - - if( !strcmp( argv[1], "all" ) ) - modify_all = 1; - - for( int i=0; iattachments); j++ ){ - struct framebuffer_attachment *at = &fb->attachments[j]; - - if( at->purpose == k_framebuffer_attachment_type_none ) - continue; - - if( modify_all ){ - console_suggest_score_text( at->display_name, term, 0 ); - } - else if( !strcmp( fb->display_name, argv[1] ) ){ - console_suggest_score_text( at->display_name, term, 0 ); - } - } - } - } -} diff --git a/render.h b/render.h index 2c4926d..f5b0afd 100644 --- a/render.h +++ b/render.h @@ -6,6 +6,7 @@ #include "model.h" #include "camera.h" #include "shader_props.h" +#include "vg/vg_framebuffer.h" #include "shaders/blit.h" #include "shaders/blitblur.h" @@ -21,74 +22,23 @@ static f32 k_blur_strength = 0.3f; static f32 k_fov = 0.86f; static f32 k_cam_height = 0.8f; -typedef struct framebuffer framebuffer; - /* * All standard buffers used in rendering */ -static struct pipeline{ +struct pipeline +{ glmesh fsquad; - framebuffer *fb_main, - *fb_water_reflection, - *fb_water_beneath, - *fb_workshop_preview, - *fb_network_status; + vg_framebuffer *fb_main, + *fb_water_reflection, + *fb_water_beneath, + *fb_workshop_preview, + *fb_network_status; int ready; } -gpipeline; - -struct framebuffer{ - const char *display_name; - int resolution_div, /* definition */ - fixed_w, - fixed_h, - - render_w, /* runtime */ - render_h; - - struct framebuffer_attachment{ - const char *display_name; - - enum framebuffer_attachment_type{ - k_framebuffer_attachment_type_none, - k_framebuffer_attachment_type_texture, - k_framebuffer_attachment_type_renderbuffer, - k_framebuffer_attachment_type_texture_depth - } - purpose; - - enum framebuffer_quality_profile{ - k_framebuffer_quality_all, - k_framebuffer_quality_high_only - } - quality; - - GLenum internalformat, - format, - type, - attachment; - - GLuint id; - - /* Runtime */ - int debug_view; - } - attachments[5]; - GLuint fb; - framebuffer **link; -} -extern framebuffers[]; +static gpipeline; void render_init(void); void render_fsquad(void); void render_fsquad1(void); void render_fsquad2(void); -void render_view_framebuffer_ui( ui_context *ctx ); -void render_fb_bind_texture( framebuffer *fb, int attachment, int slot ); -void render_fb_inverse_ratio( framebuffer *fb, v2f inverse ); -void render_fb_get_current_res( struct framebuffer *fb, int *x, int *y ); -void render_fb_bind( framebuffer *fb, int use_scaling ); -void render_fb_bind_texture( framebuffer *fb, int attachment, int slot ); -void render_fb_allocate( struct framebuffer *fb ); -void render_fb_resize(void); diff --git a/skaterift.c b/skaterift.c index f4fc3c1..5d4d69c 100644 --- a/skaterift.c +++ b/skaterift.c @@ -392,10 +392,6 @@ void vg_post_update(void) * RENDERING * ---------------------------------------------------------------------------*/ -void vg_framebuffer_resize( int w, int h ){ - render_fb_resize(); -} - static void present_view_with_post_processing(void){ glBindFramebuffer( GL_FRAMEBUFFER, 0 ); glViewport( 0,0, vg.window_x, vg.window_y ); @@ -406,7 +402,7 @@ static void present_view_with_post_processing(void){ glBlendEquation(GL_FUNC_ADD); v2f inverse; - render_fb_inverse_ratio( gpipeline.fb_main, inverse ); + vg_framebuffer_inverse_ratio( gpipeline.fb_main, inverse ); if( k_blur_effect ){ shader_blitblur_use(); @@ -441,14 +437,14 @@ static void present_view_with_post_processing(void){ v2_zero( override ); shader_blitblur_uOverrideDir( override ); - render_fb_bind_texture( gpipeline.fb_main, 0, 0 ); - render_fb_bind_texture( gpipeline.fb_main, 1, 1 ); + vg_framebuffer_bind_texture( gpipeline.fb_main, 0, 0 ); + vg_framebuffer_bind_texture( gpipeline.fb_main, 1, 1 ); } else{ shader_blit_use(); shader_blit_uTexMain( 0 ); shader_blit_uInverseRatio( inverse ); - render_fb_bind_texture( gpipeline.fb_main, 0, 0 ); + vg_framebuffer_bind_texture( gpipeline.fb_main, 0, 0 ); } render_fsquad(); @@ -674,7 +670,7 @@ static void render_main_game(void) } /* variable res target */ - render_fb_bind( gpipeline.fb_main, 1 ); + vg_framebuffer_bind( gpipeline.fb_main, k_render_scale ); glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT ); @@ -692,7 +688,7 @@ static void render_main_game(void) /* continue with variable rate */ if( !global_miniworld.transition && !menu_viewing_map() ) { - render_fb_bind( gpipeline.fb_main, 1 ); + vg_framebuffer_bind( gpipeline.fb_main, k_render_scale ); render_world_gates( get_view_world(), &skaterift.cam ); } @@ -764,7 +760,7 @@ void vg_gui( ui_context *ctx ) imgui_world_light_edit( ctx, world_current_instance() ); vg_ui.tex_bg = gpipeline.fb_main->attachments[0].id; - render_fb_inverse_ratio( gpipeline.fb_main, vg_ui.bg_inverse_ratio ); + vg_framebuffer_inverse_ratio( gpipeline.fb_main, vg_ui.bg_inverse_ratio ); menu_gui( ctx ); player__im_gui( ctx ); @@ -773,7 +769,6 @@ void vg_gui( ui_context *ctx ) world_routes_imgui( ctx, world ); skaterift_replay_imgui( ctx ); workshop_form_gui( ctx ); - render_view_framebuffer_ui( ctx ); remote_player_network_imgui( ctx, vg.pv ); if( menu_viewing_map() ) diff --git a/workshop.c b/workshop.c index 639337e..f8072c1 100644 --- a/workshop.c +++ b/workshop.c @@ -275,13 +275,13 @@ static void workshop_form_async_submit_begin( void *payload, u32 size ) static void workshop_form_async_download_image( void *payload, u32 size ) { int w, h; - render_fb_get_current_res( gpipeline.fb_workshop_preview, &w, &h ); + vg_framebuffer_get_res( gpipeline.fb_workshop_preview, &w, &h ); vg_linear_clear( vg_mem.scratch ); workshop_form.img_buffer = vg_linear_alloc( vg_mem.scratch, w*h*3 ); vg_info( "read framebuffer: glReadPixels( %dx%d )\n", w,h ); - glBindFramebuffer( GL_READ_FRAMEBUFFER, gpipeline.fb_workshop_preview->fb ); + glBindFramebuffer( GL_READ_FRAMEBUFFER, gpipeline.fb_workshop_preview->id ); glReadBuffer( GL_COLOR_ATTACHMENT0 ); glReadPixels( 0,0, w,h, GL_RGB, GL_UNSIGNED_BYTE, workshop_form.img_buffer ); @@ -587,7 +587,7 @@ static void workshop_form_async_imageload( void *data, u32 len ) { if( data ) { - struct framebuffer_attachment *a = + vg_framebuffer_attachment *a = &gpipeline.fb_workshop_preview->attachments[0]; glBindTexture( GL_TEXTURE_2D, a->id ); @@ -725,7 +725,7 @@ static void workshop_op_download_and_view_submission( int result_index ) vg_error( "No metadata was returned with this item.\n" ); } - render_fb_bind( gpipeline.fb_workshop_preview, 0 ); + vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f ); glClearColor( 0.2f, 0.0f, 0.0f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); glBindFramebuffer( GL_FRAMEBUFFER, 0 ); @@ -890,7 +890,7 @@ void workshop_init(void) static void workshop_render_world_preview(void) { - render_fb_bind( gpipeline.fb_workshop_preview, 0 ); + vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f ); glClearColor( 0.0f, 0.0f, 0.3f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); @@ -908,7 +908,7 @@ static void workshop_render_world_preview(void) */ static void workshop_render_player_preview(void) { - render_fb_bind( gpipeline.fb_workshop_preview, 0 ); + vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f ); glClearColor( 0.16f, 0.15f, 0.15f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); glEnable( GL_DEPTH_TEST ); @@ -968,7 +968,7 @@ static void workshop_render_board_preview(void) return; } - render_fb_bind( gpipeline.fb_workshop_preview, 0 ); + vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f ); glClearColor( 0.0f, 0.0f, 0.3f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); diff --git a/world.h b/world.h index efa5f9d..3a067db 100644 --- a/world.h +++ b/world.h @@ -134,7 +134,7 @@ struct world_instance { float probabilities[3]; v3i light_cubes; - struct framebuffer heightmap; + vg_framebuffer *heightmap; /* * Dynamically allocated when world_load is called. diff --git a/world_gen.c b/world_gen.c index 04d6131..91e5b7b 100644 --- a/world_gen.c +++ b/world_gen.c @@ -650,7 +650,7 @@ void async_world_postprocess( void *payload, u32 _size ) glDisable(GL_DEPTH_TEST); glDisable(GL_BLEND); glDisable(GL_CULL_FACE); - render_fb_bind( &world->heightmap, 0 ); + vg_framebuffer_bind( world->heightmap, 1.0f ); shader_blitcolour_use(); shader_blitcolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} ); render_fsquad(); diff --git a/world_render.c b/world_render.c index 4cac23f..5db790a 100644 --- a/world_render.c +++ b/world_render.c @@ -24,9 +24,11 @@ static int ccmd_set_time( int argc, const char *argv[] ){ return 0; } -static void async_world_render_init( void *payload, u32 size ){ +static void async_world_render_init( void *payload, u32 size ) +{ vg_info( "Allocate uniform buffers\n" ); - for( int i=0; iubo_bind_point = i; @@ -38,32 +40,6 @@ static void async_world_render_init( void *payload, u32 size ){ glBindBufferBase( GL_UNIFORM_BUFFER, i, world->ubo_lighting ); VG_CHECK_GL_ERR(); } - - vg_info( "Allocate frame buffers\n" ); - for( int i=0; iheightmap; - - fb->display_name = NULL; - fb->link = NULL; - fb->fixed_w = 1024; - fb->fixed_h = 1024; - fb->resolution_div = 0; - - fb->attachments[0].display_name = NULL; - fb->attachments[0].purpose = k_framebuffer_attachment_type_texture; - fb->attachments[0].internalformat = GL_RG16F; - fb->attachments[0].format = GL_RG; - fb->attachments[0].type = GL_FLOAT; - fb->attachments[0].attachment = GL_COLOR_ATTACHMENT0; - - fb->attachments[1].purpose = k_framebuffer_attachment_type_none; - fb->attachments[2].purpose = k_framebuffer_attachment_type_none; - fb->attachments[3].purpose = k_framebuffer_attachment_type_none; - fb->attachments[4].purpose = k_framebuffer_attachment_type_none; - - render_fb_allocate( fb ); - } } void world_render_init(void) @@ -92,6 +68,26 @@ void world_render_init(void) VG_TEX2D_NEAREST|VG_TEX2D_REPEAT, &world_render.tex_terrain_noise ); + vg_info( "Allocate frame buffers\n" ); + for( int i=0; iheightmap = vg_framebuffer_allocate( vg_mem.rtmemory, 1, 0 ); + world->heightmap->display_name = NULL; + world->heightmap->fixed_w = 1024; + world->heightmap->fixed_h = 1024; + world->heightmap->resolution_div = 0; + world->heightmap->attachments[0] = (vg_framebuffer_attachment) + { + NULL, k_framebuffer_attachment_type_texture, + .internalformat = GL_RG16F, + .format = GL_RG, + .type = GL_FLOAT, + .attachment = GL_COLOR_ATTACHMENT0 + }; + vg_framebuffer_create( world->heightmap ); + } + vg_async_call( async_world_render_init, NULL, 0 ); } @@ -109,7 +105,7 @@ void world_bind_position_texture( world_instance *world, GLuint shader, GLuint location, int slot ) { - render_fb_bind_texture( &world->heightmap, 0, slot ); + vg_framebuffer_bind_texture( world->heightmap, 0, slot ); glUniform1i( location, slot ); } @@ -1105,19 +1101,22 @@ void render_world( world_instance *world, vg_camera *cam, render_world_fxglow( world, world, cam, NULL, 1, 1, 0 ); } - if( with_water ){ + if( with_water ) + { render_water_texture( world, cam ); - render_fb_bind( gpipeline.fb_main, 1 ); + vg_framebuffer_bind( gpipeline.fb_main, k_render_scale ); } - if( stenciled ){ + if( stenciled ) + { glStencilFunc( GL_EQUAL, 1, 0xFF ); glStencilMask( 0x00 ); glEnable( GL_CULL_FACE ); glEnable( GL_STENCIL_TEST ); } - if( with_water ){ + if( with_water ) + { render_water_surface( world, cam ); } @@ -1125,7 +1124,8 @@ void render_world( world_instance *world, vg_camera *cam, render_other_entities( world, cam ); ent_miniworld_render( world, cam ); - if( stenciled ){ + if( stenciled ) + { glStencilMask( 0xFF ); glStencilFunc( GL_ALWAYS, 1, 0xFF ); glDisable( GL_STENCIL_TEST ); diff --git a/world_render.h b/world_render.h index 7645b26..1eb5ab1 100644 --- a/world_render.h +++ b/world_render.h @@ -33,12 +33,6 @@ struct world_render double sky_time, sky_rate, sky_target_rate; - /* water rendering */ - struct{ - struct framebuffer fbreflect, fbdepth; - } - water; - v3f render_gate_pos; struct timer_text{ char text[8]; diff --git a/world_water.c b/world_water.c index 38038bf..664a6e3 100644 --- a/world_water.c +++ b/world_water.c @@ -51,7 +51,7 @@ void render_water_texture( world_instance *world, vg_camera *cam ) return; /* Draw reflection buffa */ - render_fb_bind( gpipeline.fb_water_reflection, 1 ); + vg_framebuffer_bind( gpipeline.fb_water_reflection, k_render_scale ); glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); /* @@ -97,7 +97,7 @@ void render_water_texture( world_instance *world, vg_camera *cam ) * Create beneath view matrix */ vg_camera beneath_cam; - render_fb_bind( gpipeline.fb_water_beneath, 1 ); + vg_framebuffer_bind( gpipeline.fb_water_beneath, k_render_scale ); glClearColor( 1.0f, 0.0f, 0.0f, 0.0f ); glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); @@ -125,11 +125,12 @@ void render_water_surface( world_instance *world, vg_camera *cam ) if( !world->water.enabled ) return; - if( vg.quality_profile == k_quality_profile_high ){ + if( vg.quality_profile == k_quality_profile_high ) + { /* Draw surface */ shader_scene_water_use(); - render_fb_bind_texture( gpipeline.fb_water_reflection, 0, 0 ); + vg_framebuffer_bind_texture( gpipeline.fb_water_reflection, 0, 0 ); shader_scene_water_uTexMain( 0 ); glActiveTexture( GL_TEXTURE1 ); @@ -142,7 +143,7 @@ void render_water_surface( world_instance *world, vg_camera *cam ) WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_water ); - render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 5 ); + vg_framebuffer_bind_texture( gpipeline.fb_water_beneath, 0, 5 ); shader_scene_water_uTexBack( 5 ); shader_scene_water_uTime( world_static.time ); shader_scene_water_uCamera( cam->transform[3] ); -- 2.25.1