framebuffer formalitites
[carveJwlIkooP6JGAAIwe30JlM.git] / render.h
index 8122b2ae8e41f18effe8dea39a9d5224ae8f2c93..77bb56e1b570eeb7b4bddd0de12e6f82c5e21077 100644 (file)
--- a/render.h
+++ b/render.h
@@ -4,36 +4,36 @@
 
 #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"
 
-static void render_water_texture( m4x3f camera );
-static void render_water_surface( m4x4f pv, m4x3f camera );
-static void render_world( m4x4f projection, m4x3f camera );
-static void shader_link_standard_ub( GLuint shader, int texture_id );
-static void render_world_depth( m4x4f projection, m4x3f camera );
+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 );
 
 #ifndef RENDER_H
 #define RENDER_H
 
-struct framebuffer
-{
-   GLuint fb, colour, rb;
-   int div;
-   GLuint format;
+typedef struct framebuffer framebuffer;
 
-   int allocated;
-};
 
-static struct pipeline
+/* 
+ * All standard buffers used in rendering
+ */
+VG_STATIC struct pipeline
 {
-   float fov;
    glmesh fsquad;
 
-   GLuint fb_background,
-          rgb_background;
+   framebuffer *fb_main,
+               *fb_heightmap,
+               *fb_water_reflection,
+               *fb_water_beneath;
 
    /* STD140 */
    struct ub_world_lighting
@@ -49,6 +49,7 @@ static struct pipeline
       float g_water_fog;
       int g_light_count;
       int g_light_preview;
+      int g_shadow_samples;
    }
    ub_world_lighting;
 
@@ -61,8 +62,6 @@ static struct pipeline
    widgets[3];
 
    float shadow_spread, shadow_length;
-
-   GLuint fb_depthmap, rgb_depthmap;
    GLuint ubo_world_lighting,
           ubo_world;
 
@@ -97,52 +96,214 @@ gpipeline =
    }
 };
 
+struct framebuffer
+{
+   const char *display_name;
+   int         resolution_div,
+               fixed_w,
+               fixed_h;
+
+   struct framebuffer_attachment
+   {
+      const char *display_name;
+      
+      enum framebuffer_attachment_type
+      {
+         k_framebuffer_attachment_type_none,
+         k_framebuffer_attachment_type_colour,
+         k_framebuffer_attachment_type_renderbuffer
+      }
+      purpose;
+
+      enum framebuffer_quality_profile
+      {
+         k_framebuffer_quality_all,
+         k_framebuffer_quality_high_only
+      }
+      quality;
+
+      GLenum internalformat,
+             format,
+             type,
+             attachment;
+
+      GLuint id;
+   }
+   attachments[5];
+   GLuint fb;
+   framebuffer **link;
+}
+framebuffers[] = 
+{
+   {
+      /*
+       * The primary draw target
+       */
+      "Main", 
+      .link = &gpipeline.fb_main,
+      .resolution_div = 1,
+      .attachments = 
+      {
+         {
+            "Colour", k_framebuffer_attachment_type_colour,
+
+            .internalformat = GL_RGB,
+            .format         = GL_RGB,
+            .type           = GL_UNSIGNED_BYTE,
+            .attachment     = GL_COLOR_ATTACHMENT0
+         },
+         {
+            "Motion Vectors", k_framebuffer_attachment_type_colour,
+
+            .quality        = k_framebuffer_quality_high_only,
+            .internalformat = GL_RG16F,
+            .format         = GL_RG,
+            .type           = GL_FLOAT,
+            .attachment     = GL_COLOR_ATTACHMENT1
+         },
+         {
+            "Depth/Stencil", k_framebuffer_attachment_type_renderbuffer,
+
+            .internalformat = GL_DEPTH24_STENCIL8,
+            .attachment     = GL_DEPTH_STENCIL_ATTACHMENT
+         }
+      }
+   },
+   {
+      /*
+       * A ortho projection of the world, used for shadows and ocean colouring.
+       * Note: it does not have a render buffer attachement because it's
+       *       intended to be drawn to in a MAX blending mode
+       */
+      "Heightmap", 
+      .link = &gpipeline.fb_heightmap,
+      .fixed_w = 1024,
+      .fixed_h = 1024,
+
+      .attachments =
+      {
+         {
+            "Depth", k_framebuffer_attachment_type_colour,
+
+            .internalformat = GL_R32F,
+            .format         = GL_RED,
+            .type           = GL_FLOAT,
+            .attachment     = GL_COLOR_ATTACHMENT0
+         }
+      }
+   },
+   {
+      /*
+       * Second rendered view from the perspective of the water reflection
+       */
+      "Water reflection",
+      .link = &gpipeline.fb_water_reflection,
+      .resolution_div = 3,
+      .attachments = 
+      {
+         {
+            "Colour", k_framebuffer_attachment_type_colour,
+            .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 = 4,
+      .attachments = 
+      {
+         {
+            "Colour", k_framebuffer_attachment_type_colour,
+            .internalformat = GL_RGBA,
+            .format         = GL_RGBA,
+            .type           = GL_UNSIGNED_BYTE,
+            .attachment     = GL_COLOR_ATTACHMENT0
+         },
+         {
+            "Depth/Stencil", k_framebuffer_attachment_type_renderbuffer,
+
+            .internalformat = GL_DEPTH24_STENCIL8,
+            .attachment     = GL_DEPTH_STENCIL_ATTACHMENT
+         }
+      }
+   }
+};
+
 /* 
- * Matrix Projections
- */
-/* 
- * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf 
+ * Get the current (automatically scaled or fixed) resolution of framebuffer
  */
-static void plane_clip_projection( m4x4f mat, v4f plane )
+VG_STATIC void render_fb_get_current_res( struct framebuffer *fb, 
+                                          int *x, int *y )
 {
-   v4f c = 
+   if( fb->resolution_div )
    {
-      (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 );
+      *x = vg.window_x / fb->resolution_div;
+      *y = vg.window_y / fb->resolution_div;
+   }
+   else
+   {
+      *x = fb->fixed_w;
+      *y = fb->fixed_h;
+   }
+}
 
-   mat[0][2] = c[0];
-   mat[1][2] = c[1];
-   mat[2][2] = c[2] + 1.0f;
-   mat[3][2] = c[3];
+/*
+ * Bind framebuffer for drawing to
+ */
+VG_STATIC void render_fb_bind( framebuffer *fb )
+{
+   int x, y;
+   render_fb_get_current_res( fb, &x, &y );
+   glBindFramebuffer( GL_FRAMEBUFFER, fb->fb );
+   glViewport( 0, 0, x, y );
 }
 
-static void pipeline_projection( m4x4f mat, float nearz, float farz )
+/*
+ * Bind framebuffer attachment's texture
+ */
+VG_STATIC void render_fb_bind_texture( framebuffer *fb, 
+                                       int attachment, int slot )
 {
-   m4x4_projection( mat,
-         gpipeline.fov,
-         (float)vg.window_x / (float)vg.window_y, 
-         nearz, farz );
+   struct framebuffer_attachment *at = &fb->attachments[attachment];
+
+   if( at->purpose != k_framebuffer_attachment_type_colour )
+   {
+      vg_fatal_exit_loop( "illegal operation: bind non-colour framebuffer"
+                          " attachment to texture slot" );
+   }
+
+   glActiveTexture( GL_TEXTURE0 + slot );
+   glBindTexture( GL_TEXTURE_2D, fb->attachments[attachment].id );
 }
 
+
 /*
  * Shaders
  */
-static void shader_link_standard_ub( GLuint shader, int texture_id )
+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 );
+   render_fb_bind_texture( gpipeline.fb_heightmap, 0, texture_id );
    glUniform1i( glGetUniformLocation( shader, "g_world_depth" ), texture_id );
 }
 
-static void render_update_lighting_ub(void)
+VG_STATIC void render_update_lighting_ub(void)
 {
    struct ub_world_lighting *winf = &gpipeline.ub_world_lighting;
    int c = 0;
@@ -169,20 +330,270 @@ static void render_update_lighting_ub(void)
    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 );
 }
 
+#define FB_FORMAT_STR( E ) { E, #E },
+
 /* 
- * Framebuffers
+ * Convert OpenGL attachment ID enum to string
+ */
+VG_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; i<vg_list_size(formats); i++ )
+      if( formats[i].e == e )
+         return formats[i].str;
+
+   return "UNDEFINED";
+}
+
+/*
+ * Convert OpenGL texture format enums from TexImage2D table 1,2 &
+ * RenderBufferStorage Table 1, into strings
  */
+VG_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;
 
-static void fb_use( struct framebuffer *fb )
+   return "UNDEFINED";
+}
+
+/*
+ * Bind and allocate texture for framebuffer attachment
+ */
+VG_STATIC void render_fb_allocate_texture( struct framebuffer *fb, 
+                                           struct framebuffer_attachment *a )
+{
+   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, GL_DEPTH24_STENCIL8, rx, ry );
+   }
+   else if( a->purpose == k_framebuffer_attachment_type_colour )
+   {
+      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
+ */
+VG_STATIC 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; j<vg_list_size(fb->attachments); 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_colour )
+      {
+         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 );
+
+         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_exit_loop( "Incomplete framebuffer (see logs)" );
+   }
+}
+
+/*
+ * Resize/Update all framebuffers(we know about)
+ */
+VG_STATIC void render_fb_resize(void)
+{
+   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 );
+      }
+   }
+}
+
+#if 0
+VG_STATIC void fb_use( struct framebuffer *fb )
 {
    if( !fb )
    {
-      glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+      glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_background );
       glViewport( 0, 0, vg.window_x, vg.window_y );
    }
    else
@@ -192,7 +603,7 @@ static void fb_use( struct framebuffer *fb )
    }
 }
 
-static void fb_init( struct framebuffer *fb )
+VG_STATIC void fb_init( struct framebuffer *fb )
 {
    i32 ix = vg.window_x / fb->div,
        iy = vg.window_y / fb->div;
@@ -222,19 +633,36 @@ static void fb_init( struct framebuffer *fb )
    fb->allocated = 1;
 }
 
-static void fb_free( struct framebuffer *fb )
+VG_STATIC void _fb_glTexImage2D( GLsizei x, GLsizei y, GLint internalformat, 
+                                 GLenum format, GLenum type, const void *data )
+{
+   glTexImage2D( GL_TEXTURE_2D, 0, internalformat, x,y, 0, 
+                                   format, type, data );
+
+   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 );
+}
+
+#define fb_tex2d( X,Y, INTERNAL_FORMAT, FORMAT, TYPE, DATA ) \
+   _fb_glTexImage2D( X,Y, INTERNAL_FORMAT, FORMAT, TYPE, DATA ); \
+   vg_info( "texture( %dx%d, internal: %s, format: %s, type: %s )\n", \
+            X,Y, #INTERNAL_FORMAT, #FORMAT, #TYPE );
+
+VG_STATIC void fb_free( struct framebuffer *fb )
 {
    glDeleteTextures( 1, &fb->colour );
    glDeleteFramebuffers( 1, &fb->fb );
 }
 
-static void fb_bindtex( struct framebuffer *fb, int texture )
+VG_STATIC void fb_bindtex( struct framebuffer *fb, int texture )
 {
    glActiveTexture( GL_TEXTURE0 + texture );
    glBindTexture( GL_TEXTURE_2D, fb->colour );
 }
 
-static void fb_resize( struct framebuffer *fb )
+VG_STATIC void fb_resize( struct framebuffer *fb )
 {
    if( !fb->allocated )
       return;
@@ -249,42 +677,60 @@ static void fb_resize( struct framebuffer *fb )
    glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
    glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
 }
+#endif
 
-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 );
-   }
-}
-
-/* used for drawing player onto */
-static void render_init_temp_buffer(void)
+#if 0
+VG_STATIC void render_init_temp_buffer(void)
 {
-   vg_info( "[render] Allocate temporary framebuffer\n" );
+   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);
+   fb_tex2d( vg.window_x, vg.window_y, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, NULL );
+   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_RG16F, vg.window_x, vg.window_y, 
+                                0, GL_RG,    GL_FLOAT, 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, 
+       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.rgb_background, 0);
+         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 );
+   
+   render_check_framebuffer_complete();
 
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    VG_CHECK_GL_ERR();
 }
 
 /* used for drawing world depth from the top view, used in our water and
  * lighting calculations */
-static void render_init_depthmap_buffer(void)
+VG_STATIC void render_init_depthmap_buffer(void)
 {
    vg_info( "[render] Allocate depth map buffer\n" );
 
@@ -303,15 +749,20 @@ static void render_init_depthmap_buffer(void)
          GL_TEXTURE_2D, 
          gpipeline.rgb_depthmap, 0);
 
+   render_check_framebuffer_complete();
    VG_CHECK_GL_ERR();
 }
+#endif
 
-static void render_init_fs_quad(void)
+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.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 );
@@ -326,7 +777,7 @@ static void render_init_fs_quad(void)
    VG_CHECK_GL_ERR();
 }
 
-static void render_init_uniform_buffers(void)
+VG_STATIC void render_init_uniform_buffers(void)
 {
    vg_info( "[render] Allocate uniform buffer\n" );
 
@@ -341,16 +792,24 @@ static void render_init_uniform_buffers(void)
    VG_CHECK_GL_ERR();
 }
 
-static void render_init(void)
+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();
+      /* 
+       * Complete Framebuffers
+       */
+      for( int i=0; i<vg_list_size(framebuffers); i++ )
+      {
+         struct framebuffer *fb = &framebuffers[i];
+         render_fb_allocate( fb );
+      }
+
       render_init_fs_quad();
       render_init_uniform_buffers();
 
@@ -361,27 +820,19 @@ static void render_init(void)
    vg_release_thread_sync();
 }
 
-static void render_free(void *_)
-{
-   glDeleteBuffers( 1, &gpipeline.ubo_world_lighting );
-
-   glDeleteVertexArrays( 1, &gpipeline.fsquad.vao );
-   glDeleteBuffers( 1, &gpipeline.fsquad.vbo );
-
-   glDeleteFramebuffers( 1, &gpipeline.fb_depthmap );
-   glDeleteTextures( 1, &gpipeline.rgb_depthmap );
-
-   glDeleteFramebuffers( 1, &gpipeline.fb_background );
-   glDeleteTextures( 1, &gpipeline.rgb_background );
-}
-
 /*
  * Utility
  */
-static void render_fsquad(void)
+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 */