Search
lxdream.org :: lxdream/src/drivers/gl_fbo.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/gl_fbo.c
changeset 1166:f405d42a9786
prev1141:dc60a0987db9
next1210:79bbd30363f4
author nkeynes
date Fri Jan 28 16:14:57 2011 +1000 (8 years ago)
permissions -rw-r--r--
last change Perform the sanity checks for FBOs and shaders up front rather than on every
use, and remove surplus glGetError() calls
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * GL framebuffer-based driver shell. This requires the EXT_framebuffer_object
     5  * extension, but is much nicer/faster/etc than pbuffers when it's available.
     6  * This is (optionally) used indirectly by the top-level GLX driver.
     7  *
     8  * Strategy-wise, we maintain 2 framebuffers with up to 4 target colour
     9  * buffers a piece. Effectively this reserves one fb for display output,
    10  * and the other for texture output (each fb has a single size).
    11  *
    12  * Copyright (c) 2005 Nathan Keynes.
    13  *
    14  * This program is free software; you can redistribute it and/or modify
    15  * it under the terms of the GNU General Public License as published by
    16  * the Free Software Foundation; either version 2 of the License, or
    17  * (at your option) any later version.
    18  *
    19  * This program is distributed in the hope that it will be useful,
    20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    22  * GNU General Public License for more details.
    23  */
    25 #define GL_GLEXT_PROTOTYPES 1
    27 #include <stdlib.h>
    28 #include "lxdream.h"
    29 #include "display.h"
    30 #include "drivers/video_gl.h"
    31 #include "pvr2/glutil.h"
    33 #ifdef HAVE_OPENGL_FBO
    35 #define MAX_FRAMEBUFFERS 2
    36 #define MAX_TEXTURES_PER_FB 16
    38 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
    39 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer );
    40 static gboolean gl_fbo_set_render_target( render_buffer_t buffer );
    41 static void gl_fbo_finish_render( render_buffer_t buffer );
    42 static void gl_fbo_display_render_buffer( render_buffer_t buffer );
    43 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    44 static void gl_fbo_display_blank( uint32_t colour );
    45 static gboolean gl_fbo_test_framebuffer( );
    46 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    48 extern uint32_t video_width, video_height;
    50 /**
    51  * Framebuffer info structure
    52  */
    53 struct gl_fbo_info {
    54     GLuint fb_id;
    55     GLuint depth_id;
    56     GLuint stencil_id;
    57     GLuint tex_ids[MAX_TEXTURES_PER_FB];
    58     int width, height;
    59 };
    61 static GLint gl_fbo_max_attachments = 0;
    62 static gboolean gl_fbo_have_packed_stencil = FALSE;
    63 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
    65 #define ATTACHMENT_POINT(n) (GL_COLOR_ATTACHMENT0_EXT+(n))
    66 static int last_used_fbo;
    68 gboolean gl_fbo_is_supported()
    69 {
    70     return isGLExtensionSupported("GL_EXT_framebuffer_object");
    71 }
    73 /**
    74  * Construct the initial frame buffers and allocate ids for everything.
    75  * The render handling driver methods are set to the fbo versions.
    76  */
    77 void gl_fbo_init( display_driver_t driver ) 
    78 {
    79     int i,j;
    80     GLuint fbids[MAX_FRAMEBUFFERS];
    81     GLuint rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
    83     glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &gl_fbo_max_attachments);
    84     glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids[0] );
    85     glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids[0] );
    86     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
    87         fbo[i].fb_id = fbids[i];
    88         fbo[i].depth_id = rbids[i*2];
    89         fbo[i].stencil_id = rbids[i*2+1];
    90         fbo[i].width = -1;
    91         fbo[i].height = -1;
    92         for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
    93             fbo[i].tex_ids[j] = -1;
    94         }
    95     }
    96     last_used_fbo = 0;
    98     if( isGLExtensionSupported("GL_EXT_packed_depth_stencil" ) ) {
    99         driver->capabilities.stencil_bits = 8;
   100         gl_fbo_have_packed_stencil = TRUE;
   101     } else {
   102         driver->capabilities.stencil_bits = 0;
   103         gl_fbo_have_packed_stencil = FALSE;
   104         WARN( "Packed depth stencil not available - disabling shadow volumes" );
   105     }
   107     driver->create_render_buffer = gl_fbo_create_render_buffer;
   108     driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
   109     driver->set_render_target = gl_fbo_set_render_target;
   110     driver->finish_render = gl_fbo_finish_render;
   111     driver->display_render_buffer = gl_fbo_display_render_buffer;
   112     driver->load_frame_buffer = gl_fbo_load_frame_buffer;
   113     driver->display_blank = gl_fbo_display_blank;
   114     driver->read_render_buffer = gl_fbo_read_render_buffer;
   116     gl_fbo_test_framebuffer();
   117     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
   118 }
   120 void gl_fbo_shutdown()
   121 {
   122     int i;
   123     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   124     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   125         glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
   126         glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
   127     }
   128 }
   130 static void gl_fbo_setup_framebuffer( int bufno, int width, int height )
   131 {
   132     int i;
   133     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
   135     /* Clear out any existing texture attachments */
   136     for( i=0; i<gl_fbo_max_attachments; i++ ) {
   137         if( fbo[bufno].tex_ids[i] != -1 ) {
   138             glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINT(i),
   139                     GL_TEXTURE_RECTANGLE_ARB, 0, 0);
   140             fbo[bufno].tex_ids[i] = -1;
   141         }
   142     }
   144     /* Setup the renderbuffers */
   145     if( gl_fbo_have_packed_stencil ) {
   146         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   147         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT, width, height);
   148         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
   149                                      GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   150         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
   151                                      GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   152     } else {
   153         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   154         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
   155         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
   156                                      GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   157         /* In theory you could attach a separate stencil buffer. In practice this 
   158          * isn't actually supported by any hardware I've had access to, so we're
   159          * stencil-less.
   160          */
   161     }
   162     fbo[bufno].width = width;
   163     fbo[bufno].height = height;
   164 }
   166 static int gl_fbo_get_framebuffer( int width, int height ) 
   167 {
   168     int bufno = -1, i;
   169     /* find a compatible framebuffer context */
   170     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   171         if( fbo[i].width == -1 && bufno == -1 ) {
   172             bufno = i;
   173         } else if( fbo[i].width == width && fbo[i].height == height ) {
   174             bufno = i;
   175             break;
   176         }
   177     }
   178     if( bufno == -1 ) {
   179         bufno = last_used_fbo + 1;
   180         if( bufno >= MAX_FRAMEBUFFERS ) {
   181             bufno = 0;
   182         }
   183         last_used_fbo = bufno;
   184     }
   185     if( fbo[bufno].width == width && fbo[bufno].height == height ) {
   186         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
   187     } else {
   188         gl_fbo_setup_framebuffer( bufno, width, height );
   189     } 
   190     return bufno;
   191 }
   193 /**
   194  * Attach a texture to the framebuffer. The texture must already be initialized
   195  * to the correct dimensions etc.
   196  */
   197 static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
   198     int attach = -1, i;
   199     for( i=0; i<gl_fbo_max_attachments; i++ ) {
   200         if( fbo[fbo_no].tex_ids[i] == tex_id ) {
   201             glDrawBuffer(ATTACHMENT_POINT(i));
   202             glReadBuffer(ATTACHMENT_POINT(i)); 
   203             return ATTACHMENT_POINT(i); // already attached
   204         } else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
   205             attach = i;
   206         }
   207     }
   208     if( attach == -1 ) {
   209         attach = 0;
   210     }
   211     fbo[fbo_no].tex_ids[attach] = tex_id;
   212     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, 0 ); // Ensure the output texture is unbound
   213     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINT(attach), 
   214                               GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
   215     /* Set draw/read buffers by default */
   216     glDrawBuffer(ATTACHMENT_POINT(attach));
   217     glReadBuffer(ATTACHMENT_POINT(attach)); 
   219     return ATTACHMENT_POINT(attach);
   220 }
   222 static gboolean gl_fbo_test_framebuffer( )
   223 {
   224     gboolean result = TRUE;
   225     glGetError(); /* Clear error state just in case */
   226     render_buffer_t buffer = gl_fbo_create_render_buffer( 640, 480, 0 );
   227     gl_fbo_set_render_target(buffer);
   229     GLint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   230     if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
   231         ERROR( "Framebuffer failure: %x", status );
   232         result = FALSE;
   233     }
   234     if( result ) {
   235         result = gl_check_error( "Setting up framebuffer" );
   236     }
   238     gl_fbo_destroy_render_buffer( buffer );
   239     return result;
   240 }
   242 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   243 {
   244     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   245     buffer->width = width;
   246     buffer->height = height;
   247     buffer->tex_id = tex_id;
   248     if( tex_id == 0 ) {
   249         GLuint tex;
   250         glGenTextures( 1, &tex );
   251         buffer->buf_id = tex;
   252     } else {
   253         buffer->buf_id = tex_id;
   254         glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   255     }
   256     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
   257     glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
   258     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
   259     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
   260     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   261     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   262     return buffer;
   263 }
   265 /**
   266  * Ensure the texture in the given render buffer is not attached to a 
   267  * framebuffer (ie, so we can safely use it as a texture during the rendering
   268  * cycle, or before deletion).
   269  */
   270 static void gl_fbo_detach_render_buffer( render_buffer_t buffer )
   271 {
   272     int i,j;
   273     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   274         if( fbo[i].width == buffer->width && fbo[i].height == buffer->height ) {
   275             for( j=0; j<gl_fbo_max_attachments; j++ ) {
   276                 if( fbo[i].tex_ids[j] == buffer->buf_id ) {
   277                     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[i].fb_id);
   278                     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINT(j), 
   279                                               GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
   280                     fbo[i].tex_ids[j] = -1;
   281                     return;
   282                 }                    
   283             }
   284             break;
   285         }
   286     }
   287 }
   289 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
   290 {
   291     int i,j;
   293     gl_fbo_detach_render_buffer( buffer );
   295     if( buffer->buf_id != buffer->tex_id ) {
   296         // If tex_id was set at buffer creation, we don't own the texture.
   297         // Otherwise, delete it now.
   298         GLuint tex = buffer->buf_id; 
   299         glDeleteTextures( 1, &tex );
   300     }
   301     buffer->buf_id = 0;
   302     buffer->tex_id = 0;
   303     free( buffer );
   304 }
   306 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   307 {
   308     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   309     gl_fbo_attach_texture( fb, buffer->buf_id );
   310     /* setup the gl context */
   311     glViewport( 0, 0, buffer->width, buffer->height );
   313     return TRUE;
   314 }
   316 static void gl_fbo_finish_render( render_buffer_t buffer )
   317 {
   318     glFinish();
   319     gl_fbo_detach_render_buffer(buffer);
   320 }
   322 /**
   323  * Render the texture holding the given buffer to the front window
   324  * buffer.
   325  */
   326 static void gl_fbo_display_render_buffer( render_buffer_t buffer )
   327 {
   328     gl_fbo_detach();
   329     gl_display_render_buffer( buffer );
   330 }
   332 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   333 {
   334     gl_fbo_detach();
   335     gl_load_frame_buffer( frame, buffer->buf_id );
   336 }
   338 static void gl_fbo_display_blank( uint32_t colour )
   339 {
   340     gl_fbo_detach();
   341     gl_display_blank( colour );
   342 }
   344 void gl_fbo_detach()
   345 {
   346     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   347     /* Make sure texture attachment is not a current draw/read buffer */
   348     glDrawBuffer( GL_FRONT );
   349     glReadBuffer( GL_FRONT );
   350     display_driver->swap_buffers();
   351 }    
   353 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   354                                            int rowstride, int format )
   355 {
   356     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   357     gl_fbo_attach_texture( fb, buffer->buf_id );
   358     return gl_read_render_buffer( target, buffer, rowstride, format );
   359 }
   361 #else
   362 gboolean gl_fbo_is_supported()
   363 {
   364     return FALSE;
   365 }
   367 void gl_fbo_init( display_driver_t driver ) 
   368 {
   369 }
   371 #endif
.