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 1228:8168aa94e6d7
prev1223:61684ca88599
next1245:01e0020adf88
author nkeynes
date Thu Feb 23 20:16:37 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Merge emu sign-extension fix to top
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 #if defined(HAVE_OPENGL_FBO) || defined(HAVE_OPENGL_FBO_EXT)
    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+(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     gl_fbo_max_attachments = glGetMaxColourAttachments();
    84     glGenFramebuffers( MAX_FRAMEBUFFERS, &fbids[0] );
    85     glGenRenderbuffers( 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     glBindFramebuffer(GL_FRAMEBUFFER, 0);
   118 }
   120 void gl_fbo_shutdown()
   121 {
   122     int i;
   123     glBindFramebuffer( GL_FRAMEBUFFER, 0 );
   124     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   125         glDeleteFramebuffers( 1, &fbo[i].fb_id );
   126         glDeleteRenderbuffers( 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     glBindFramebuffer(GL_FRAMEBUFFER, 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             glFramebufferTexture2D(GL_FRAMEBUFFER, ATTACHMENT_POINT(i),
   139                     GL_TEXTURE_2D, 0, 0);
   140             fbo[bufno].tex_ids[i] = -1;
   141         }
   142     }
   144     /* Setup the renderbuffers */
   145     if( gl_fbo_have_packed_stencil ) {
   146         glBindRenderbuffer(GL_RENDERBUFFER, fbo[bufno].depth_id);
   147         glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
   148         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
   149                                      GL_RENDERBUFFER, fbo[bufno].depth_id);
   150         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
   151                                      GL_RENDERBUFFER, fbo[bufno].depth_id);
   152     } else {
   153         glBindRenderbuffer(GL_RENDERBUFFER, fbo[bufno].depth_id);
   154         glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
   155         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
   156                                      GL_RENDERBUFFER, 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         glBindFramebuffer( GL_FRAMEBUFFER, 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 #ifdef HAVE_OPENGL_DRAW_BUFFER
   202             glDrawBuffer(ATTACHMENT_POINT(i));
   203             glReadBuffer(ATTACHMENT_POINT(i)); 
   204 #endif
   205             return ATTACHMENT_POINT(i); // already attached
   206         } else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
   207             attach = i;
   208         }
   209     }
   210     if( attach == -1 ) {
   211         attach = 0;
   212     }
   213     fbo[fbo_no].tex_ids[attach] = tex_id;
   214     glBindTexture( GL_TEXTURE_2D, 0 ); // Ensure the output texture is unbound
   215     glFramebufferTexture2D(GL_FRAMEBUFFER, ATTACHMENT_POINT(attach), 
   216                               GL_TEXTURE_2D, tex_id, 0 );
   217     /* Set draw/read buffers by default */
   218 #ifdef HAVE_OPENGL_DRAW_BUFFER    
   219     glDrawBuffer(ATTACHMENT_POINT(attach));
   220     glReadBuffer(ATTACHMENT_POINT(attach)); 
   221 #endif
   223     return ATTACHMENT_POINT(attach);
   224 }
   226 static gboolean gl_fbo_test_framebuffer( )
   227 {
   228     gboolean result = TRUE;
   229     glGetError(); /* Clear error state just in case */
   230     render_buffer_t buffer = gl_fbo_create_render_buffer( 640, 480, 0 );
   231     gl_fbo_set_render_target(buffer);
   233     GLint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
   234     if( status != GL_FRAMEBUFFER_COMPLETE ) {
   235         ERROR( "Framebuffer failure: %x", status );
   236         result = FALSE;
   237     }
   238     if( result ) {
   239         result = gl_check_error( "Setting up framebuffer" );
   240     }
   242     gl_fbo_destroy_render_buffer( buffer );
   243     return result;
   244 }
   246 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
   247 {
   248     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   249     buffer->width = width;
   250     buffer->height = height;
   251     buffer->tex_id = tex_id;
   252     if( tex_id == 0 ) {
   253         GLuint tex;
   254         glGenTextures( 1, &tex );
   255         buffer->buf_id = tex;
   256     } else {
   257         buffer->buf_id = tex_id;
   258         glBindTexture( GL_TEXTURE_2D, tex_id );
   259     }
   260     glBindTexture( GL_TEXTURE_2D, buffer->buf_id );
   261     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
   262     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
   263     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
   264     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   265     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   266     return buffer;
   267 }
   269 /**
   270  * Ensure the texture in the given render buffer is not attached to a 
   271  * framebuffer (ie, so we can safely use it as a texture during the rendering
   272  * cycle, or before deletion).
   273  */
   274 static void gl_fbo_detach_render_buffer( render_buffer_t buffer )
   275 {
   276     int i,j;
   277     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   278         if( fbo[i].width == buffer->width && fbo[i].height == buffer->height ) {
   279             for( j=0; j<gl_fbo_max_attachments; j++ ) {
   280                 if( fbo[i].tex_ids[j] == buffer->buf_id ) {
   281                     glBindFramebuffer(GL_FRAMEBUFFER, fbo[i].fb_id);
   282                     glFramebufferTexture2D(GL_FRAMEBUFFER, ATTACHMENT_POINT(j), 
   283                                               GL_TEXTURE_2D, GL_NONE, 0 );
   284                     fbo[i].tex_ids[j] = -1;
   285                     return;
   286                 }                    
   287             }
   288             break;
   289         }
   290     }
   291 }
   293 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
   294 {
   295     int i,j;
   297     gl_fbo_detach_render_buffer( buffer );
   299     if( buffer->buf_id != buffer->tex_id ) {
   300         // If tex_id was set at buffer creation, we don't own the texture.
   301         // Otherwise, delete it now.
   302         GLuint tex = buffer->buf_id; 
   303         glDeleteTextures( 1, &tex );
   304     }
   305     buffer->buf_id = 0;
   306     buffer->tex_id = 0;
   307     free( buffer );
   308 }
   310 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   311 {
   312     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   313     gl_fbo_attach_texture( fb, buffer->buf_id );
   314     /* setup the gl context */
   315     glViewport( 0, 0, buffer->width, buffer->height );
   317     return TRUE;
   318 }
   320 static void gl_fbo_finish_render( render_buffer_t buffer )
   321 {
   322     glFinish();
   323     gl_fbo_detach_render_buffer(buffer);
   324 }
   326 /**
   327  * Render the texture holding the given buffer to the front window
   328  * buffer.
   329  */
   330 static void gl_fbo_display_render_buffer( render_buffer_t buffer )
   331 {
   332     gl_fbo_detach();
   333     gl_display_render_buffer( buffer );
   334 }
   336 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   337 {
   338     gl_fbo_detach();
   339     gl_load_frame_buffer( frame, buffer->buf_id );
   340 }
   342 static void gl_fbo_display_blank( uint32_t colour )
   343 {
   344     gl_fbo_detach();
   345     gl_display_blank( colour );
   346 }
   348 void gl_fbo_detach()
   349 {
   350     glBindFramebuffer( GL_FRAMEBUFFER, 0 );
   351     /* Make sure texture attachment is not a current draw/read buffer */
   352 #ifdef HAVE_OPENGL_DRAW_BUFFER
   353     glDrawBuffer( GL_FRONT );
   354     glReadBuffer( GL_FRONT );
   355 #endif
   356     display_driver->swap_buffers();
   357 }    
   359 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   360                                            int rowstride, int format )
   361 {
   362     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   363     gl_fbo_attach_texture( fb, buffer->buf_id );
   364     return gl_read_render_buffer( target, buffer, rowstride, format );
   365 }
   367 #else
   368 gboolean gl_fbo_is_supported()
   369 {
   370     return FALSE;
   371 }
   373 void gl_fbo_init( display_driver_t driver ) 
   374 {
   375 }
   377 #endif
.