X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=render.c;h=aa0cfab94382879a76431ac31b3d1858413f754c;hb=refs%2Fheads%2Fmenu2;hp=0050f943f4773cf0ea42b311f8e991bac3ba3956;hpb=1a194c3888293733939b2dd944251ae1b6c398ce;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/render.c b/render.c index 0050f94..aa0cfab 100644 --- a/render.c +++ b/render.c @@ -1,469 +1,10 @@ #include "render.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_engine.h" +#include "vg/vg_platform.h" +#include "vg/vg_framebuffer.h" static void async_render_init( void *payload, u32 size ) { - /* - * Complete Framebuffers - */ - for( int i=0; idisplay_name = "main"; + g_render.fb_main->resolution_div = 1; + g_render.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 + }; + g_render.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 + }; + g_render.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( g_render.fb_main ); + + /* + * Water reflection + */ + g_render.fb_water_reflection = vg_framebuffer_allocate( alloc, 2, 1 ); + g_render.fb_water_reflection->display_name = "water_reflection"; + g_render.fb_water_reflection->resolution_div = 2; + g_render.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 + }; + g_render.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( g_render.fb_water_reflection ); + + /* + * Thid rendered view from the perspective of the camera, but just + * captures stuff thats under the water + */ + g_render.fb_water_beneath = vg_framebuffer_allocate( alloc, 2, 1 ); + g_render.fb_water_beneath->display_name = "water_beneath"; + g_render.fb_water_beneath->resolution_div = 2; + g_render.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 + }; + g_render.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( g_render.fb_water_beneath ); + + /* + * Workshop preview + */ + g_render.fb_workshop_preview = vg_framebuffer_allocate( alloc, 2, 1 ); + g_render.fb_workshop_preview->display_name = "workshop_preview"; + g_render.fb_workshop_preview->resolution_div = 0; + g_render.fb_workshop_preview->fixed_w = WORKSHOP_PREVIEW_WIDTH; + g_render.fb_workshop_preview->fixed_h = WORKSHOP_PREVIEW_HEIGHT; + g_render.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 + }; + g_render.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( g_render.fb_workshop_preview ); + + /* + * Network status + */ + g_render.fb_network_status = vg_framebuffer_allocate( alloc, 1, 1 ); + g_render.fb_network_status->display_name = "network_status_ui"; + g_render.fb_network_status->resolution_div = 0; + g_render.fb_network_status->fixed_w = 128; + g_render.fb_network_status->fixed_h = 48; + g_render.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( g_render.fb_network_status ); vg_async_call( async_render_init, NULL, 0 ); } @@ -540,181 +202,59 @@ void render_init(void) */ void render_fsquad(void) { - glBindVertexArray( gpipeline.fsquad.vao ); + glBindVertexArray( g_render.fsquad.vao ); glDrawArrays( GL_TRIANGLES, 0, 6 ); } void render_fsquad1(void) { - glBindVertexArray( gpipeline.fsquad.vao ); + glBindVertexArray( g_render.fsquad.vao ); glDrawArrays( GL_TRIANGLES, 6, 6+6 ); } void render_fsquad2(void) { - glBindVertexArray( gpipeline.fsquad.vao ); + glBindVertexArray( g_render.fsquad.vao ); glDrawArrays( GL_TRIANGLES, 66+6,6 ); } -/* - * Call this inside the UI function - */ -void render_view_framebuffer_ui(void) -{ -#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 ) +void postprocess_to_screen( vg_framebuffer *fb ) { - 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 ); - } - } - } - } + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); + glViewport( 0,0, vg.window_x, vg.window_y ); - return 0; -} + glEnable(GL_BLEND); + glDisable(GL_DEPTH_TEST); + glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA); + glBlendEquation(GL_FUNC_ADD); -static void render_framebuffer_poll( int argc, char const *argv[] ) -{ - const char *term = argv[argc-1]; + v2f inverse; + vg_framebuffer_inverse_ratio( fb, inverse ); - if( argc == 1 ){ - console_suggest_score_text( "show", term, 0 ); - console_suggest_score_text( "hide", term, 0 ); + if( k_blur_effect ) + { + shader_blitblur_use(); + shader_blitblur_uTexMain( 0 ); + shader_blitblur_uTexMotion( 1 ); + shader_blitblur_uBlurStrength( k_blur_strength / + (vg.time_frame_delta*60.0) ); + shader_blitblur_uInverseRatio( inverse ); + + inverse[0] -= 0.0001f; + inverse[1] -= 0.0001f; + shader_blitblur_uClampUv( inverse ); + shader_blitblur_uOverrideDir( g_render.blur_override ); + + vg_framebuffer_bind_texture( fb, 0, 0 ); + vg_framebuffer_bind_texture( fb, 1, 1 ); } - else if( argc == 2 ){ - console_suggest_score_text( "all", term, 0 ); - - for( int i=0; idisplay_name, term, 0 ); - } + else + { + shader_blit_use(); + shader_blit_uTexMain( 0 ); + shader_blit_uInverseRatio( inverse ); + vg_framebuffer_bind_texture( fb, 0, 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 ); - } - } - } - } + render_fsquad(); }