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 877:8331f4aa3616
prev868:c5b4ed31d819
next1076:18c164e8aec4
author nkeynes
date Thu Oct 16 12:06:24 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Fix bound check on number of framebuffers allocated
Fix repurposing framebuffers to clear out previous attachments to prevent
dimension erros.
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_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    47 extern uint32_t video_width, video_height;
    49 /**
    50  * Framebuffer info structure
    51  */
    52 struct gl_fbo_info {
    53     GLuint fb_id;
    54     GLuint depth_id;
    55     GLuint stencil_id;
    56     GLuint tex_ids[MAX_TEXTURES_PER_FB];
    57     int width, height;
    58 };
    60 static GLint gl_fbo_max_attachments = 0;
    61 static gboolean gl_fbo_have_packed_stencil = FALSE;
    62 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
    64 #define ATTACHMENT_POINT(n) (GL_COLOR_ATTACHMENT0_EXT+(n))
    65 static int last_used_fbo;
    67 gboolean gl_fbo_is_supported()
    68 {
    69     return isGLExtensionSupported("GL_EXT_framebuffer_object");
    70 }
    72 /**
    73  * Construct the initial frame buffers and allocate ids for everything.
    74  * The render handling driver methods are set to the fbo versions.
    75  */
    76 void gl_fbo_init( display_driver_t driver ) 
    77 {
    78     int i,j;
    79     GLuint fbids[MAX_FRAMEBUFFERS];
    80     GLuint rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
    82     glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &gl_fbo_max_attachments);
    83     glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids[0] );
    84     glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids[0] );
    85     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
    86         fbo[i].fb_id = fbids[i];
    87         fbo[i].depth_id = rbids[i*2];
    88         fbo[i].stencil_id = rbids[i*2+1];
    89         fbo[i].width = -1;
    90         fbo[i].height = -1;
    91         for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
    92             fbo[i].tex_ids[j] = -1;
    93         }
    94     }
    95     last_used_fbo = 0;
    97     if( isGLExtensionSupported("GL_EXT_packed_depth_stencil" ) ) {
    98         driver->capabilities.stencil_bits = 8;
    99         gl_fbo_have_packed_stencil = TRUE;
   100     } else {
   101         driver->capabilities.stencil_bits = 0;
   102         gl_fbo_have_packed_stencil = FALSE;
   103         WARN( "Packed depth stencil not available - disabling shadow volumes" );
   104     }
   106     driver->create_render_buffer = gl_fbo_create_render_buffer;
   107     driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
   108     driver->set_render_target = gl_fbo_set_render_target;
   109     driver->finish_render = gl_fbo_finish_render;
   110     driver->display_render_buffer = gl_fbo_display_render_buffer;
   111     driver->load_frame_buffer = gl_fbo_load_frame_buffer;
   112     driver->display_blank = gl_fbo_display_blank;
   113     driver->read_render_buffer = gl_fbo_read_render_buffer;
   115     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
   116     glDrawBuffer(GL_FRONT);
   117     glReadBuffer(GL_FRONT);
   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)); 
   220     GLint status = glGetError();
   221     if( status != GL_NO_ERROR ) {
   222         ERROR( "GL error setting render target (%x)!", status );
   223     }
   224     status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   225     if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
   226         ERROR( "Framebuffer failure: %x", status );
   227         exit(1);
   228     }
   230     return ATTACHMENT_POINT(attach);
   231 }
   233 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   234 {
   235     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   236     buffer->width = width;
   237     buffer->height = height;
   238     buffer->tex_id = tex_id;
   239     if( tex_id == 0 ) {
   240         GLuint tex;
   241         glGenTextures( 1, &tex );
   242         buffer->buf_id = tex;
   243     } else {
   244         buffer->buf_id = tex_id;
   245         glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   246     }
   247     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
   248     glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
   249     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
   250     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
   251     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   252     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   253     return buffer;
   254 }
   256 /**
   257  * Ensure the texture in the given render buffer is not attached to a 
   258  * framebuffer (ie, so we can safely use it as a texture during the rendering
   259  * cycle, or before deletion).
   260  */
   261 static void gl_fbo_detach_render_buffer( render_buffer_t buffer )
   262 {
   263     int i,j;
   264     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   265         if( fbo[i].width == buffer->width && fbo[i].height == buffer->height ) {
   266             for( j=0; j<gl_fbo_max_attachments; j++ ) {
   267                 if( fbo[i].tex_ids[j] == buffer->buf_id ) {
   268                     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[i].fb_id);
   269                     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINT(j), 
   270                                               GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
   271                     fbo[i].tex_ids[j] = -1;
   272                     return;
   273                 }                    
   274             }
   275             break;
   276         }
   277     }
   278 }
   280 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
   281 {
   282     int i,j;
   284     gl_fbo_detach_render_buffer( buffer );
   286     if( buffer->buf_id != buffer->tex_id ) {
   287         // If tex_id was set at buffer creation, we don't own the texture.
   288         // Otherwise, delete it now.
   289         GLuint tex = buffer->buf_id; 
   290         glDeleteTextures( 1, &tex );
   291     }
   292     buffer->buf_id = 0;
   293     buffer->tex_id = 0;
   294     free( buffer );
   295 }
   297 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   298 {
   299     glFinish();
   300     glGetError();
   301     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   302     gl_fbo_attach_texture( fb, buffer->buf_id );
   303     /* setup the gl context */
   304     glViewport( 0, 0, buffer->width, buffer->height );
   306     return TRUE;
   307 }
   309 static void gl_fbo_finish_render( render_buffer_t buffer )
   310 {
   311     glFinish();
   312     glGetError();
   313     gl_fbo_detach_render_buffer(buffer);
   314 }
   316 /**
   317  * Render the texture holding the given buffer to the front window
   318  * buffer.
   319  */
   320 static void gl_fbo_display_render_buffer( render_buffer_t buffer )
   321 {
   322     glFinish();
   323     gl_fbo_detach();
   324     gl_display_render_buffer( buffer );
   325 }
   327 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   328 {
   329     glFinish();
   330     gl_fbo_detach();
   331     gl_load_frame_buffer( frame, buffer->buf_id );
   332 }
   334 static void gl_fbo_display_blank( uint32_t colour )
   335 {
   336     glFinish();
   337     gl_fbo_detach();
   338     gl_display_blank( colour );
   339 }
   341 void gl_fbo_detach()
   342 {
   343     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   344     glDrawBuffer( GL_FRONT );
   345     glReadBuffer( GL_FRONT );
   346 }    
   348 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   349                                            int rowstride, int format )
   350 {
   351     glGetError();
   352     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   353     gl_fbo_attach_texture( fb, buffer->buf_id );
   354     return gl_read_render_buffer( target, buffer, rowstride, format );
   355 }
   357 #else
   358 gboolean gl_fbo_is_supported()
   359 {
   360     return FALSE;
   361 }
   363 void gl_fbo_init( display_driver_t driver ) 
   364 {
   365 }
   367 #endif
.