save timing version in replay frame
[carveJwlIkooP6JGAAIwe30JlM.git] / render.h
index 317b30110bc2d6df942816a258f60940871eec82..22c05726ddd0222a7b567e6b47c1c87ddea18540 100644 (file)
--- a/render.h
+++ b/render.h
 
 #include "common.h"
 #include "model.h"
+#include "camera.h"
+#include "world.h"
 
 #include "shaders/blit.h"
-#include "shaders/standard.h"
-#include "shaders/vblend.h"
-
-VG_STATIC void render_water_texture( m4x3f camera );
-VG_STATIC void render_water_surface( m4x4f pv, m4x3f camera );
-VG_STATIC void render_world( m4x4f projection, m4x3f camera );
-VG_STATIC void shader_link_standard_ub( GLuint shader, int texture_id );
-VG_STATIC void render_world_depth( m4x4f projection, m4x3f camera );
+#include "shaders/blitblur.h"
+#include "shaders/blitcolour.h"
+#include "shaders/blit_transition.h"
 
+#define WORKSHOP_PREVIEW_WIDTH  504
+#define WORKSHOP_PREVIEW_HEIGHT 336
 #ifndef RENDER_H
 #define RENDER_H
 
-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;
 
-VG_STATIC struct pipeline
-{
-   float fov;
+/* 
+ * All standard buffers used in rendering
+ */
+static struct pipeline{
    glmesh fsquad;
 
-   GLuint fb_background,
-          rgb_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];
+   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;
 
-   float shadow_spread, shadow_length;
+      enum framebuffer_quality_profile{
+         k_framebuffer_quality_all,
+         k_framebuffer_quality_high_only
+      }
+      quality;
+      
+      GLenum internalformat,
+             format,
+             type,
+             attachment;
 
-   GLuint fb_depthmap, rgb_depthmap;
-   GLuint ubo_world_lighting,
-          ubo_world;
+      GLuint id;
 
-   int ready;
+      /* Runtime */
+      int debug_view;
+   }
+   attachments[5];
+   GLuint fb;
+   framebuffer **link;
 }
-gpipeline =
+framebuffers[] = 
 {
-   .widgets =
    {
+      /*
+       * The primary draw target
+       */
+      "main", 
+      .link = &gpipeline.fb_main,
+      .resolution_div = 1,
+      .attachments = 
       {
-         .enabled = 1,
-         .colour = { 1.36f, 1.35f, 1.01f },
-         .dir = { 0.63f, -0.08f }
-      },
+         {
+            "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 = 
       {
-         .enabled = 1,
-         .colour = { 0.33f, 0.56f, 0.64f },
-         .dir = { -2.60f, -0.13f }
-      },
+         {
+            "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 = 
       {
-         .enabled = 1,
-         .colour = { 0.05f, 0.05f, 0.23f },
-         .dir = { 2.60f, -0.84f }
+         {
+            "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
+         }
       }
    },
-   .shadow_spread = 0.65f,
-   .shadow_length = 9.50f,
-
-   .ub_world_lighting =
    {
-      .g_ambient_colour = { 0.09f, 0.03f, 0.07f }
+      "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
+         }
+      }
    }
 };
 
 /* 
- * Matrix Projections
+ * Get the current (automatically scaled or fixed) resolution of framebuffer
  */
-/* 
- * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf 
- */
-VG_STATIC void plane_clip_projection( m4x4f mat, v4f plane )
+static void render_fb_get_current_res( struct framebuffer *fb, 
+                                          int *x, int *y )
 {
-   v4f c = 
-   {
-      (vg_signf(plane[0]) + mat[2][0]) / mat[0][0],
-      (vg_signf(plane[1]) + mat[2][1]) / mat[1][1],
-      -1.0f,
-      (1.0f + mat[2][2]) / mat[3][2]
-   };
-
-   v4_muls( plane, 2.0f / v4_dot(plane,c), c );
-
-   mat[0][2] = c[0];
-   mat[1][2] = c[1];
-   mat[2][2] = c[2] + 1.0f;
-   mat[3][2] = c[3];
+   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;
+   }
 }
 
-VG_STATIC void pipeline_projection( m4x4f mat, float nearz, float farz )
+static void render_fb_inverse_ratio( framebuffer *fb, v2f inverse )
 {
-   m4x4_projection( mat,
-         gpipeline.fov,
-         (float)vg.window_x / (float)vg.window_y, 
-         nearz, farz );
+   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 );
+   }
 }
 
 /*
- * Shaders
+ * Bind framebuffer for drawing to
  */
-VG_STATIC void shader_link_standard_ub( GLuint shader, int texture_id )
+static void render_fb_bind( framebuffer *fb, int use_scaling )
 {
-   GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );   
-   glUniformBlockBinding( shader, idx, 0 );
+   int x, y;
+   render_fb_get_current_res( fb, &x, &y );
 
-   glActiveTexture( GL_TEXTURE0 + texture_id );
-   glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_depthmap );
-   glUniform1i( glGetUniformLocation( shader, "g_world_depth" ), texture_id );
+   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 );
 }
 
-VG_STATIC void render_update_lighting_ub(void)
+/*
+ * Bind framebuffer attachment's texture
+ */
+static void render_fb_bind_texture( framebuffer *fb, 
+                                       int attachment, int slot )
 {
-   struct ub_world_lighting *winf = &gpipeline.ub_world_lighting;
-   int c = 0;
+   struct framebuffer_attachment *at = &fb->attachments[attachment];
 
-   for( int i=0; i<3; i++ )
+   if( (at->purpose != k_framebuffer_attachment_type_texture) &&
+       (at->purpose != k_framebuffer_attachment_type_texture_depth) )
    {
-      struct light_widget *lw = &gpipeline.widgets[i];
+      vg_fatal_error( "illegal operation: bind non-texture framebuffer"
+                          " attachment to texture slot" );
+   }
 
-      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] );
+   glActiveTexture( GL_TEXTURE0 + slot );
+   glBindTexture( GL_TEXTURE_2D, fb->attachments[attachment].id );
+}
 
-         c ++;
-      }
-   }
 
-   winf->g_light_count = c;
-   winf->g_light_directions[0][3] = gpipeline.shadow_length;
-   winf->g_light_colours[0][3] = gpipeline.shadow_spread;
+/*
+ * 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)
+   };
 
-   if( vg.quality_profile == k_quality_profile_low )
-      winf->g_shadow_samples = 0;
-   else
-      winf->g_shadow_samples = 8;
+   for( int i=0; i<vg_list_size(formats); i++ )
+      if( formats[i].e == e )
+         return formats[i].str;
 
-   glBindBuffer( GL_UNIFORM_BUFFER, gpipeline.ubo_world_lighting );
-   glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(struct ub_world_lighting),
-         &gpipeline.ub_world_lighting );
+   return "UNDEFINED";
 }
 
-/* 
- * Framebuffers
+/*
+ * Convert OpenGL texture format enums from TexImage2D table 1,2 &
+ * RenderBufferStorage Table 1, into strings
  */
+static const char *render_fb_format_str( GLenum format )
+{
+   struct { GLenum e; const char *str; }
+   formats[] =
+   { 
+      /* Table 1 */
+      FB_FORMAT_STR(GL_DEPTH_COMPONENT)
+      FB_FORMAT_STR(GL_DEPTH_STENCIL)
+      FB_FORMAT_STR(GL_RED)
+      FB_FORMAT_STR(GL_RG)
+      FB_FORMAT_STR(GL_RGB)
+      FB_FORMAT_STR(GL_RGBA)
+
+      /* Render buffer formats */
+      FB_FORMAT_STR(GL_DEPTH_COMPONENT16)
+      FB_FORMAT_STR(GL_DEPTH_COMPONENT24)
+      FB_FORMAT_STR(GL_DEPTH_COMPONENT32F)
+      FB_FORMAT_STR(GL_DEPTH24_STENCIL8)
+      FB_FORMAT_STR(GL_DEPTH32F_STENCIL8)
+      FB_FORMAT_STR(GL_STENCIL_INDEX8)
+
+      /* Table 2 */
+      FB_FORMAT_STR(GL_R8)
+      FB_FORMAT_STR(GL_R8_SNORM)
+      FB_FORMAT_STR(GL_R16)
+      FB_FORMAT_STR(GL_R16_SNORM)
+      FB_FORMAT_STR(GL_RG8)
+      FB_FORMAT_STR(GL_RG8_SNORM)
+      FB_FORMAT_STR(GL_RG16)
+      FB_FORMAT_STR(GL_RG16_SNORM)
+      FB_FORMAT_STR(GL_R3_G3_B2)
+      FB_FORMAT_STR(GL_RGB4)
+      FB_FORMAT_STR(GL_RGB5)
+      FB_FORMAT_STR(GL_RGB8)
+      FB_FORMAT_STR(GL_RGB8_SNORM)
+      FB_FORMAT_STR(GL_RGB10)
+      FB_FORMAT_STR(GL_RGB12)
+      FB_FORMAT_STR(GL_RGB16_SNORM)
+      FB_FORMAT_STR(GL_RGBA2)
+      FB_FORMAT_STR(GL_RGBA4)
+      FB_FORMAT_STR(GL_RGB5_A1)
+      FB_FORMAT_STR(GL_RGBA8)
+      FB_FORMAT_STR(GL_RGBA8_SNORM)
+      FB_FORMAT_STR(GL_RGB10_A2)
+      FB_FORMAT_STR(GL_RGB10_A2UI)
+      FB_FORMAT_STR(GL_RGBA12)
+      FB_FORMAT_STR(GL_RGBA16)
+      FB_FORMAT_STR(GL_SRGB8)
+      FB_FORMAT_STR(GL_SRGB8_ALPHA8)
+      FB_FORMAT_STR(GL_R16F)
+      FB_FORMAT_STR(GL_RG16F)
+      FB_FORMAT_STR(GL_RGB16F)
+      FB_FORMAT_STR(GL_RGBA16F)
+      FB_FORMAT_STR(GL_R32F)
+      FB_FORMAT_STR(GL_RG32F)
+      FB_FORMAT_STR(GL_RGB32F)
+      FB_FORMAT_STR(GL_RGBA32F)
+      FB_FORMAT_STR(GL_R11F_G11F_B10F)
+      FB_FORMAT_STR(GL_RGB9_E5)
+      FB_FORMAT_STR(GL_R8I)
+      FB_FORMAT_STR(GL_R8UI)
+      FB_FORMAT_STR(GL_R16I)
+      FB_FORMAT_STR(GL_R16UI)
+      FB_FORMAT_STR(GL_R32I)
+      FB_FORMAT_STR(GL_R32UI)
+      FB_FORMAT_STR(GL_RG8I)
+      FB_FORMAT_STR(GL_RG8UI)
+      FB_FORMAT_STR(GL_RG16I)
+      FB_FORMAT_STR(GL_RG16UI)
+      FB_FORMAT_STR(GL_RG32I)
+      FB_FORMAT_STR(GL_RG32UI)
+      FB_FORMAT_STR(GL_RGB8I)
+      FB_FORMAT_STR(GL_RGB8UI)
+      FB_FORMAT_STR(GL_RGB16I)
+      FB_FORMAT_STR(GL_RGB16UI)
+      FB_FORMAT_STR(GL_RGB32I)
+      FB_FORMAT_STR(GL_RGB32UI)
+      FB_FORMAT_STR(GL_RGBA8I)
+      FB_FORMAT_STR(GL_RGBA8UI)
+      FB_FORMAT_STR(GL_RGBA16I)
+      FB_FORMAT_STR(GL_RGBA16UI)
+      FB_FORMAT_STR(GL_RGBA32I)
+      FB_FORMAT_STR(GL_RGBA32UI)
+   };
+
+   for( int i=0; i<vg_list_size(formats); i++ )
+      if( formats[i].e == format )
+         return formats[i].str;
 
-VG_STATIC void fb_use( struct framebuffer *fb )
+   return "UNDEFINED";
+}
+
+/*
+ * Bind and allocate texture for framebuffer attachment
+ */
+static void render_fb_allocate_texture( struct framebuffer *fb, 
+                                           struct framebuffer_attachment *a )
 {
-   if( !fb )
-   {
-      glBindFramebuffer( GL_FRAMEBUFFER, 0 );
-      glViewport( 0, 0, vg.window_x, vg.window_y );
+   int rx, ry;
+   render_fb_get_current_res( fb, &rx, &ry );
+
+   if( a->purpose == k_framebuffer_attachment_type_renderbuffer ){
+      glBindRenderbuffer( GL_RENDERBUFFER, a->id );
+      glRenderbufferStorage( GL_RENDERBUFFER, a->internalformat, rx, ry );
    }
-   else
+   else if( a->purpose == k_framebuffer_attachment_type_texture ||
+            a->purpose == k_framebuffer_attachment_type_texture_depth )
    {
-      glBindFramebuffer( GL_FRAMEBUFFER, fb->fb );
-      glViewport( 0, 0, vg.window_x / fb->div, vg.window_y / fb->div );
+      glBindTexture( GL_TEXTURE_2D, a->id );
+      glTexImage2D( GL_TEXTURE_2D, 0, a->internalformat, rx, ry,
+                                   0, a->format, a->type, NULL );
    }
 }
 
-VG_STATIC void fb_init( struct framebuffer *fb )
+/* 
+ * Full allocation of a framebuffer
+ */
+static void render_fb_allocate( 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);
+   int rx, ry;
+   render_fb_get_current_res( fb, &rx, &ry );
 
-   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);
+   vg_info( "allocate_framebuffer( %s, %dx%d )\n", fb->display_name, rx, ry );
+   vg_info( "{\n" );
 
-   glGenRenderbuffers( 1, &fb->rb );
-   glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
-   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
+   GLenum colour_attachments[4];
+   u32    colour_count = 0;
 
-   glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
-         GL_RENDERBUFFER, fb->rb );
-   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   for( int j=0; j<vg_list_size(fb->attachments); j++ ){
+      struct framebuffer_attachment *attachment = &fb->attachments[j];
 
-   VG_CHECK_GL_ERR();
-   fb->allocated = 1;
-}
+      if( attachment->purpose == k_framebuffer_attachment_type_none )
+         continue;
 
-VG_STATIC void fb_free( struct framebuffer *fb )
-{
-   glDeleteTextures( 1, &fb->colour );
-   glDeleteFramebuffers( 1, &fb->fb );
-}
+      vg_info( "  %s: %s\n", 
+                  render_fb_attachment_str( attachment->attachment ),
+                  render_fb_format_str( attachment->internalformat ) );
 
-VG_STATIC void fb_bindtex( struct framebuffer *fb, int texture )
-{
-   glActiveTexture( GL_TEXTURE0 + texture );
-   glBindTexture( GL_TEXTURE_2D, fb->colour );
-}
+      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;
+      }
+   }
 
-VG_STATIC void fb_resize( struct framebuffer *fb )
-{
-   if( !fb->allocated )
-      return;
+   glDrawBuffers( colour_count, colour_attachments );
 
-   i32 ix = vg.window_x / fb->div,
-       iy = vg.window_y / fb->div;
+   /* 
+    * Check result 
+    */
+   GLenum result = glCheckFramebufferStatus( GL_FRAMEBUFFER );
 
-   glBindTexture( GL_TEXTURE_2D, fb->colour );
-   glTexImage2D( GL_TEXTURE_2D, 0, fb->format, ix, iy, 0,
-         fb->format, GL_UNSIGNED_BYTE, NULL );
+   if( result == GL_FRAMEBUFFER_COMPLETE ){
+      /* 
+       * Attatch to gpipeline
+       */
+      if( fb->link )
+         *fb->link = fb;
 
-   glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
-   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
+      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)" );
+   }
 }
 
-VG_STATIC void render_fb_resize(void)
+/*
+ * Resize/Update all framebuffers(we know about)
+ */
+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 );
+   if( !gpipeline.ready ) return;
+
+   for( int i=0; i<vg_list_size(framebuffers); i++ ){
+      struct framebuffer *fb = &framebuffers[i];
+      for( int j=0; j<vg_list_size(fb->attachments); j++ ){
+         struct framebuffer_attachment *attachment = &fb->attachments[j];
+         render_fb_allocate_texture( fb, attachment );
+      }
    }
 }
 
-/* used for drawing player onto */
-VG_STATIC void render_init_temp_buffer(void)
-{
-   vg_info( "[render] Allocate temporary framebuffer\n" );
+static int render_framebuffer_control( int argc, char const *argv[] );
+static void render_framebuffer_poll( int argc, char const *argv[] );
 
-   glGenFramebuffers( 1, &gpipeline.fb_background );
-   glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_background );
+static void async_render_init( void *payload, u32 size )
+{
+   /* 
+    * Complete Framebuffers
+    */
+   for( int i=0; i<vg_list_size(framebuffers); i++ ){
+      struct framebuffer *fb = &framebuffers[i];
+      render_fb_allocate( fb );
+   }
 
-   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);
+   f32 rh = 0x1p-4f, ih = 0.3f;
+
+   float quad[] = { 
+      0.00f,0.00f, 1.00f,1.00f, 0.00f,1.00f,    /* fsquad */
+      0.00f,0.00f, 1.00f,0.00f, 1.00f,1.00f,    
+
+      0.00f,0.00f, 1.00f,rh,     0.00f,rh,    /* fsquad1 */
+      0.00f,0.00f, 1.00f,0.00f,  1.00f,rh,
+
+      /* 9x9 debug grid */
+      /* row0 */
+      0.00f,0.00f, 0.30f,0.30f, 0.00f,0.30f,
+      0.00f,0.00f, 0.30f,0.00f, 0.30f,0.30f,
+      0.30f,0.00f, 0.60f,0.30f, 0.30f,0.30f,
+      0.30f,0.00f, 0.60f,0.00f, 0.60f,0.30f,
+      0.60f,0.00f, 0.90f,0.30f, 0.60f,0.30f,
+      0.60f,0.00f, 0.90f,0.00f, 0.90f,0.30f,
+      /* row1 */
+      0.00f,0.30f, 0.30f,0.60f, 0.00f,0.60f,
+      0.00f,0.30f, 0.30f,0.30f, 0.30f,0.60f,
+      0.30f,0.30f, 0.60f,0.60f, 0.30f,0.60f,
+      0.30f,0.30f, 0.60f,0.30f, 0.60f,0.60f,
+      0.60f,0.30f, 0.90f,0.60f, 0.60f,0.60f,
+      0.60f,0.30f, 0.90f,0.30f, 0.90f,0.60f,
+      /* row2 */
+      0.00f,0.60f, 0.30f,0.90f, 0.00f,0.90f,
+      0.00f,0.60f, 0.30f,0.60f, 0.30f,0.90f,
+      0.30f,0.60f, 0.60f,0.90f, 0.30f,0.90f,
+      0.30f,0.60f, 0.60f,0.60f, 0.60f,0.90f,
+      0.60f,0.60f, 0.90f,0.90f, 0.60f,0.90f,
+      0.60f,0.60f, 0.90f,0.60f, 0.90f,0.90f,
+
+      0.00f,ih, 1.00f,ih+rh, 0.00f,ih+rh,    /* fsquad2 */
+      0.00f,ih, 1.00f,ih,    1.00f,ih+rh,
+   };
 
-   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, 
-         gpipeline.rgb_background, 0);
+   vg_console_reg_cmd( "fb", render_framebuffer_control, 
+                             render_framebuffer_poll );
+   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();
+
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   gpipeline.ready = 1;
 }
 
-/* used for drawing world depth from the top view, used in our water and
- * lighting calculations */
-VG_STATIC void render_init_depthmap_buffer(void)
+static void render_init(void)
 {
-   vg_info( "[render] Allocate depth map buffer\n" );
-
-   glGenFramebuffers( 1, &gpipeline.fb_depthmap );
-   glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
+   vg_console_reg_var( "blur_strength", &k_blur_strength, k_var_dtype_f32, 0 );
+   vg_console_reg_var( "render_scale", &k_render_scale,
+                       k_var_dtype_f32, VG_VAR_PERSISTENT );
+   vg_console_reg_var( "fov", &k_fov, k_var_dtype_f32, VG_VAR_PERSISTENT );
+   vg_console_reg_var( "cam_height", &k_cam_height, 
+                        k_var_dtype_f32, VG_VAR_PERSISTENT );
+   vg_console_reg_var( "blur_effect", &k_blur_effect, 
+                        k_var_dtype_i32, VG_VAR_PERSISTENT );
 
-   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);
+   shader_blit_register();
+   shader_blitblur_register();
+   shader_blitcolour_register();
+   shader_blit_transition_register();
 
-   VG_CHECK_GL_ERR();
+   vg_async_call( async_render_init, NULL, 0 );
 }
 
-VG_STATIC void render_init_fs_quad(void)
+/*
+ * Utility
+ */
+static void render_fsquad(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 };
-
-   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 );
+   glDrawArrays( GL_TRIANGLES, 0, 6 );
+}
+
+static void render_fsquad1(void)
+{
    glBindVertexArray( gpipeline.fsquad.vao );
-   glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 
-         sizeof(float)*2, (void*)0 );
-   glEnableVertexAttribArray( 0 );
+   glDrawArrays( GL_TRIANGLES, 6, 6 );
+}
 
-   VG_CHECK_GL_ERR();
+static void render_fsquad2(void)
+{
+   glBindVertexArray( gpipeline.fsquad.vao );
+   glDrawArrays( GL_TRIANGLES, 66,6 );
 }
 
-VG_STATIC void render_init_uniform_buffers(void)
+/*
+ * Call this inside the UI function
+ */
+static void render_view_framebuffer_ui(void)
 {
-   vg_info( "[render] Allocate uniform buffer\n" );
+#if 0
+   int viewing_count = 0;
 
-   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 );
+   glBindVertexArray( gpipeline.fsquad.vao );
+   shader_blit_use();
+   shader_blit_uTexMain( 0 );
+   
+   v2f identity = { 1.0f, 1.0f };
+   shader_blit_uInverseRatio( identity );
 
-   render_update_lighting_ub();
-   glBindBufferBase( GL_UNIFORM_BUFFER, 0, gpipeline.ubo_world_lighting );
+   for( int i=0; i<vg_list_size(framebuffers); i++ ){
+      struct framebuffer *fb = &framebuffers[i];
 
-   VG_CHECK_GL_ERR();
+      for( int j=0; j<vg_list_size(fb->attachments); 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 },
+                     "<hardware texture>", 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
 }
 
-VG_STATIC void render_init(void)
+static void render_framebuffer_show( struct framebuffer *fb,
+                                        struct framebuffer_attachment *at,
+                                        int operation )
 {
-   shader_blit_register();
-   shader_standard_register();
-   shader_vblend_register();
+   at->debug_view = operation;
+   vg_info( "%s %s:%s\n", (operation?"shown": "hidden"), 
+               fb->display_name, at->display_name );
+}
 
-   vg_acquire_thread_sync();
-   {
-      render_init_temp_buffer();
-      render_init_depthmap_buffer();
-      render_init_fs_quad();
-      render_init_uniform_buffers();
+/* 
+ * arg0: command           "show"/"hide"
+ * arg1: framebuffer name  <name>/"all"
+ * arg2: subname           <name>/none
+ */
+static int render_framebuffer_control( int argc, char const *argv[] )
+{
+   if( argc < 2 ){
+      vg_error( "Usage: fb \"show/hide\" <name>/\"all\" <name>/none\n" );
+      return 0;
+   }
+
+   int modify_all = 0,
+       operation  = 0;
 
-      glBindFramebuffer( GL_FRAMEBUFFER, 0 );
-      gpipeline.ready = 1;
+   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; i<vg_list_size(framebuffers); i++ ){
+      struct framebuffer *fb = &framebuffers[i];
+      
+      for( int j=0; j<vg_list_size(fb->attachments); 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 );
+            }
+         }
+      }
    }
 
-   vg_release_thread_sync();
+   return 0;
 }
 
-/*
- * Utility
- */
-VG_STATIC void render_fsquad(void)
+static void render_framebuffer_poll( int argc, char const *argv[] )
 {
-   glBindVertexArray( gpipeline.fsquad.vao );
-   glDrawArrays( GL_TRIANGLES, 0, 6 );
+   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; i<vg_list_size(framebuffers); i++ ){
+         struct framebuffer *fb = &framebuffers[i];
+         console_suggest_score_text( fb->display_name, term, 0 );
+      }
+   }
+   else if( argc == 3 ){
+      int modify_all = 0;
+
+      if( !strcmp( argv[1], "all" ) )
+         modify_all = 1;
+
+      for( int i=0; i<vg_list_size(framebuffers); i++ ){
+         struct framebuffer *fb = &framebuffers[i];
+         
+         for( int j=0; j<vg_list_size(fb->attachments); 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 );
+            }
+         }
+      }
+   }
 }
 
 #endif /* RENDER_H */