/*
* Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
*/
-
+#pragma once
#include "common.h"
#include "model.h"
#include "camera.h"
#include "shaders/blit.h"
#include "shaders/blitblur.h"
-#include "shaders/standard.h"
-#include "shaders/vblend.h"
-
-VG_STATIC void render_water_texture( camera *cam );
-VG_STATIC void render_water_surface( camera *cam );
-VG_STATIC void render_world( camera *cam );
-VG_STATIC void shader_link_standard_ub( GLuint shader, int texture_id );
-VG_STATIC void render_world_depth( camera *cam );
+#include "shaders/blitcolour.h"
+#include "shaders/blit_transition.h"
-#ifndef RENDER_H
-#define RENDER_H
+#define WORKSHOP_PREVIEW_WIDTH 504
+#define WORKSHOP_PREVIEW_HEIGHT 336
-struct framebuffer
-{
- GLuint fb, colour, rb;
- int div;
- GLuint format;
+static f32 k_render_scale = 1.0f;
+static i32 k_blur_effect = 1;
+static f32 k_blur_strength = 0.3f;
+static f32 k_fov = 0.86f;
+static f32 k_cam_height = 0.8f;
- int allocated;
-};
+typedef struct framebuffer framebuffer;
/*
* All standard buffers used in rendering
*/
-VG_STATIC struct pipeline
-{
-#if 0
- float fov;
-#endif
+static struct pipeline{
glmesh fsquad;
- GLuint fb_background,
- rgb_background,
- mv_background,
- rb_background;
-
- /* STD140 */
- struct ub_world_lighting
- {
- /* v3f (padded) */
- v4f g_light_colours[3],
- g_light_directions[3],
- g_ambient_colour;
-
- v4f g_water_plane,
- g_depth_bounds;
-
- float g_water_fog;
- int g_light_count;
- int g_light_preview;
- int g_shadow_samples;
- }
- ub_world_lighting;
-
- struct light_widget
- {
- int enabled;
- v2f dir;
- v3f colour;
- }
- widgets[3];
-
- float shadow_spread, shadow_length;
-
- GLuint fb_depthmap, rgb_depthmap;
- GLuint ubo_world_lighting,
- ubo_world;
-
+ framebuffer *fb_main,
+ *fb_water_reflection,
+ *fb_water_beneath,
+ *fb_workshop_preview,
+ *fb_network_status;
int ready;
}
-gpipeline =
-{
- .widgets =
- {
- {
- .enabled = 1,
- .colour = { 1.36f, 1.35f, 1.01f },
- .dir = { 0.63f, -0.08f }
- },
- {
- .enabled = 1,
- .colour = { 0.33f, 0.56f, 0.64f },
- .dir = { -2.60f, -0.13f }
- },
- {
- .enabled = 1,
- .colour = { 0.05f, 0.05f, 0.23f },
- .dir = { 2.60f, -0.84f }
+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
}
- },
- .shadow_spread = 0.65f,
- .shadow_length = 9.50f,
-
- .ub_world_lighting =
- {
- .g_ambient_colour = { 0.09f, 0.03f, 0.07f }
- }
-};
+ purpose;
-/*
- * Shaders
- */
-VG_STATIC void shader_link_standard_ub( GLuint shader, int texture_id )
-{
- GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );
- glUniformBlockBinding( shader, idx, 0 );
-
- glActiveTexture( GL_TEXTURE0 + texture_id );
- glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_depthmap );
- glUniform1i( glGetUniformLocation( shader, "g_world_depth" ), texture_id );
-}
-
-VG_STATIC void render_update_lighting_ub(void)
-{
- struct ub_world_lighting *winf = &gpipeline.ub_world_lighting;
- int c = 0;
-
- for( int i=0; i<3; i++ )
- {
- struct light_widget *lw = &gpipeline.widgets[i];
-
- if( lw->enabled )
- {
- float pitch = lw->dir[0],
- yaw = lw->dir[1],
- xz = cosf( pitch );
-
- v3_copy( (v3f){ xz*cosf(yaw), sinf(pitch), xz*sinf(yaw) },
- winf->g_light_directions[c] );
- v3_copy( lw->colour, winf->g_light_colours[c] );
-
- c ++;
+ enum framebuffer_quality_profile{
+ k_framebuffer_quality_all,
+ k_framebuffer_quality_high_only
}
- }
-
- winf->g_light_count = c;
- winf->g_light_directions[0][3] = gpipeline.shadow_length;
- winf->g_light_colours[0][3] = gpipeline.shadow_spread;
-
- if( vg.quality_profile == k_quality_profile_low )
- winf->g_shadow_samples = 0;
- else
- winf->g_shadow_samples = 8;
-
- glBindBuffer( GL_UNIFORM_BUFFER, gpipeline.ubo_world_lighting );
- glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(struct ub_world_lighting),
- &gpipeline.ub_world_lighting );
-}
-
-/*
- * Framebuffers
- */
-
-VG_STATIC void fb_use( struct framebuffer *fb )
-{
- if( !fb )
- {
- glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_background );
- glViewport( 0, 0, vg.window_x, vg.window_y );
- }
- else
- {
- glBindFramebuffer( GL_FRAMEBUFFER, fb->fb );
- glViewport( 0, 0, vg.window_x / fb->div, vg.window_y / fb->div );
- }
-}
-
-VG_STATIC void fb_init( struct framebuffer *fb )
-{
- i32 ix = vg.window_x / fb->div,
- iy = vg.window_y / fb->div;
-
- glGenFramebuffers( 1, &fb->fb );
- glBindFramebuffer( GL_FRAMEBUFFER, fb->fb );
-
- glGenTextures( 1, &fb->colour );
- glBindTexture( GL_TEXTURE_2D, fb->colour );
- glTexImage2D( GL_TEXTURE_2D, 0, fb->format, ix, iy,
- 0, fb->format, GL_UNSIGNED_BYTE, NULL);
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D, fb->colour, 0);
-
- glGenRenderbuffers( 1, &fb->rb );
- glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
- glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
-
- glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
- GL_RENDERBUFFER, fb->rb );
- glBindFramebuffer( GL_FRAMEBUFFER, 0 );
-
- VG_CHECK_GL_ERR();
- fb->allocated = 1;
-}
-
-VG_STATIC void fb_free( struct framebuffer *fb )
-{
- glDeleteTextures( 1, &fb->colour );
- glDeleteFramebuffers( 1, &fb->fb );
-}
-
-VG_STATIC void fb_bindtex( struct framebuffer *fb, int texture )
-{
- glActiveTexture( GL_TEXTURE0 + texture );
- glBindTexture( GL_TEXTURE_2D, fb->colour );
-}
-
-VG_STATIC void fb_resize( struct framebuffer *fb )
-{
- if( !fb->allocated )
- return;
-
- i32 ix = vg.window_x / fb->div,
- iy = vg.window_y / fb->div;
+ quality;
+
+ GLenum internalformat,
+ format,
+ type,
+ attachment;
- glBindTexture( GL_TEXTURE_2D, fb->colour );
- glTexImage2D( GL_TEXTURE_2D, 0, fb->format, ix, iy, 0,
- fb->format, GL_UNSIGNED_BYTE, NULL );
+ GLuint id;
- glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
- glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
-}
-
-VG_STATIC void render_fb_resize(void)
-{
- if( gpipeline.ready )
- {
- glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg.window_x, vg.window_y, 0,
- GL_RGB, GL_UNSIGNED_BYTE, NULL );
-
- /* FIXME: Resizeother textures and rb */
- }
-}
-
-VG_STATIC void render_init_temp_buffer(void)
-{
- vg_info( "[render] Allocate framebuffer\n" );
-
- glGenFramebuffers( 1, &gpipeline.fb_background );
- glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_background );
-
- glGenTextures( 1, &gpipeline.rgb_background );
- glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg.window_x, vg.window_y,
- 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
- 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, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D,
- gpipeline.rgb_background, 0 );
-
- glGenTextures( 1, &gpipeline.mv_background );
- glBindTexture( GL_TEXTURE_2D, gpipeline.mv_background );
-#if 0
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, vg.window_x, vg.window_y,
- 0, GL_RG, GL_FLOAT, NULL);
-#endif
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA16F, vg.window_x, vg.window_y,
- 0, GL_RGBA, GL_FLOAT, NULL);
-
- 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, GL_COLOR_ATTACHMENT1,
- GL_TEXTURE_2D,
- gpipeline.mv_background, 0 );
-
- /* render buffer */
- glGenRenderbuffers( 1, &gpipeline.rb_background );
- glBindRenderbuffer( GL_RENDERBUFFER, gpipeline.rb_background );
- glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8,
- vg.window_x, vg.window_y );
- glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
- GL_RENDERBUFFER, gpipeline.rb_background );
-
- GLuint attachments[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
- glDrawBuffers( 2, attachments );
-
- GLenum result = glCheckFramebufferStatus( GL_FRAMEBUFFER );
-
- if( result != GL_FRAMEBUFFER_COMPLETE )
- {
- if( result == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT )
- vg_fatal_exit_loop( "Main RT: Incomplete attachment" );
- else if( result == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT )
- vg_fatal_exit_loop( "Main RT: Missing attachment" );
- else if( result == GL_FRAMEBUFFER_UNSUPPORTED )
- vg_fatal_exit_loop( "Main RT: Unsupported framebuffer format" );
- else
- vg_fatal_exit_loop( "Main RT: Generic Error" );
- }
-
- glBindFramebuffer( GL_FRAMEBUFFER, 0 );
- VG_CHECK_GL_ERR();
-}
-
-/* used for drawing world depth from the top view, used in our water and
- * lighting calculations */
-VG_STATIC void render_init_depthmap_buffer(void)
-{
- vg_info( "[render] Allocate depth map buffer\n" );
-
- glGenFramebuffers( 1, &gpipeline.fb_depthmap );
- glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
-
- glGenTextures( 1, &gpipeline.rgb_depthmap );
- glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_depthmap );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_R32F, 1024, 1024, 0,
- GL_RED, GL_FLOAT, NULL );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- vg_tex2d_clamp();
-
- glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D,
- gpipeline.rgb_depthmap, 0);
-
- VG_CHECK_GL_ERR();
-}
-
-VG_STATIC void render_init_fs_quad(void)
-{
- vg_info( "[render] Allocate quad\n" );
-
- float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-
- 0.2f, 0.0f, 0.8f, 1.0f, 0.2f, 1.0f,
- 0.2f, 0.0f, 0.8f, 0.0f, 0.8f, 1.0f};
-
- glGenVertexArrays( 1, &gpipeline.fsquad.vao );
- glGenBuffers( 1, &gpipeline.fsquad.vbo );
- glBindVertexArray( gpipeline.fsquad.vao );
- glBindBuffer( GL_ARRAY_BUFFER, gpipeline.fsquad.vbo );
- glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
- glBindVertexArray( gpipeline.fsquad.vao );
- glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE,
- sizeof(float)*2, (void*)0 );
- glEnableVertexAttribArray( 0 );
-
- VG_CHECK_GL_ERR();
-}
-
-VG_STATIC void render_init_uniform_buffers(void)
-{
- vg_info( "[render] Allocate uniform buffer\n" );
-
- glGenBuffers( 1, &gpipeline.ubo_world_lighting );
- glBindBuffer( GL_UNIFORM_BUFFER, gpipeline.ubo_world_lighting );
- glBufferData( GL_UNIFORM_BUFFER, sizeof(struct ub_world_lighting),
- NULL, GL_DYNAMIC_DRAW );
-
- render_update_lighting_ub();
- glBindBufferBase( GL_UNIFORM_BUFFER, 0, gpipeline.ubo_world_lighting );
-
- VG_CHECK_GL_ERR();
-}
-
-VG_STATIC void render_init(void)
-{
- shader_blit_register();
- shader_blitblur_register();
- shader_standard_register();
- shader_vblend_register();
-
- vg_acquire_thread_sync();
- {
- render_init_temp_buffer();
- render_init_depthmap_buffer();
- render_init_fs_quad();
- render_init_uniform_buffers();
-
- glBindFramebuffer( GL_FRAMEBUFFER, 0 );
- gpipeline.ready = 1;
+ /* Runtime */
+ int debug_view;
}
-
- vg_release_thread_sync();
-}
-
-/*
- * Utility
- */
-VG_STATIC void render_fsquad(void)
-{
- glBindVertexArray( gpipeline.fsquad.vao );
- glDrawArrays( GL_TRIANGLES, 0, 6 );
-}
-
-VG_STATIC void render_fsquad1(void)
-{
- glBindVertexArray( gpipeline.fsquad.vao );
- glDrawArrays( GL_TRIANGLES, 6, 6 );
-}
-
-#endif /* RENDER_H */
+ attachments[5];
+ GLuint fb;
+ framebuffer **link;
+}
+extern framebuffers[];
+
+void render_init(void);
+void render_fsquad(void);
+void render_fsquad1(void);
+void render_fsquad2(void);
+void render_view_framebuffer_ui(void);
+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);