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 656:031d23fa6d0b
prev635:76c63aac3590
next669:ab344e42bca9
author nkeynes
date Wed Apr 02 01:46:58 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Add configure-time checks for fbo and shader functions in libGL (so linking
doesn't fail on really old libGL implementations)
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 4
    38 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height );
    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 gboolean gl_fbo_display_render_buffer( render_buffer_t buffer );
    42 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
    43 static gboolean gl_fbo_display_blank( uint32_t colour );
    44 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
    46 extern uint32_t video_width, video_height;
    48 /**
    49  * Framebuffer info structure
    50  */
    51 struct gl_fbo_info {
    52     GLuint fb_id;
    53     GLuint depth_id;
    54     GLuint stencil_id;
    55     GLuint tex_ids[MAX_TEXTURES_PER_FB];
    56     int width, height;
    57 };
    59 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
    60 const static int ATTACHMENT_POINTS[MAX_TEXTURES_PER_FB] = {
    61     GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, 
    62     GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT };
    63 static int last_used_fbo;
    65 gboolean gl_fbo_is_supported()
    66 {
    67     return isGLExtensionSupported("GL_EXT_framebuffer_object");
    68 }
    70 /**
    71  * Construct the initial frame buffers and allocate ids for everything.
    72  * The render handling driver methods are set to the fbo versions.
    73  */
    74 void gl_fbo_init( display_driver_t driver ) 
    75 {
    76     int i,j;
    77     GLuint fbids[MAX_FRAMEBUFFERS];
    78     GLuint rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
    80     glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids[0] );
    81     glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids[0] );
    82     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
    83 	fbo[i].fb_id = fbids[i];
    84 	fbo[i].depth_id = rbids[i*2];
    85 	fbo[i].stencil_id = rbids[i*2+1];
    86 	fbo[i].width = -1;
    87 	fbo[i].height = -1;
    88 	for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
    89 	    fbo[i].tex_ids[j] = -1;
    90 	}
    91     }
    92     last_used_fbo = 0;
    94     driver->create_render_buffer = gl_fbo_create_render_buffer;
    95     driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
    96     driver->set_render_target = gl_fbo_set_render_target;
    97     driver->display_render_buffer = gl_fbo_display_render_buffer;
    98     driver->load_frame_buffer = gl_fbo_load_frame_buffer;
    99     driver->display_blank = gl_fbo_display_blank;
   100     driver->read_render_buffer = gl_fbo_read_render_buffer;
   102     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
   103     glDrawBuffer(GL_FRONT);
   104     glReadBuffer(GL_FRONT);
   105 }
   107 void gl_fbo_shutdown()
   108 {
   109     int i;
   110     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   111     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   112 	glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
   113 	glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
   114     }
   115 }
   117 void gl_fbo_setup_framebuffer( int bufno, int width, int height )
   118 {
   119     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
   120     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   121     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
   122     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
   123 				 GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   124     /* Stencil doesn't work on ATI, and we're not using it at the moment anyway, so...
   125     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   126     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height);
   127     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
   128 				 GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   129     */
   130     fbo[bufno].width = width;
   131     fbo[bufno].height = height;
   132 }
   134 int gl_fbo_get_framebuffer( int width, int height ) 
   135 {
   136     int bufno = -1, i;
   137     /* find a compatible framebuffer context */
   138     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   139 	if( fbo[i].width == -1 && bufno == -1 ) {
   140 	    bufno = i;
   141 	} else if( fbo[i].width == width && fbo[i].height == height ) {
   142 	    bufno = i;
   143 	    break;
   144 	}
   145     }
   146     if( bufno == -1 ) {
   147 	bufno = last_used_fbo + 1;
   148 	if( bufno > MAX_FRAMEBUFFERS ) {
   149 	    bufno = 0;
   150 	}
   151 	last_used_fbo = bufno;
   152     }
   153     if( fbo[bufno].width == width && fbo[bufno].height == height ) {
   154 	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
   155     } else {
   156 	gl_fbo_setup_framebuffer( bufno, width, height );
   157     } 
   158     return bufno;
   159 }
   161 /**
   162  * Attach a texture to the framebuffer. The texture must already be initialized
   163  * to the correct dimensions etc.
   164  */
   165 static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
   166     int attach = -1, i;
   167     for( i=0; i<MAX_TEXTURES_PER_FB; i++ ) {
   168 	if( fbo[fbo_no].tex_ids[i] == tex_id ) {
   169 	    glDrawBuffer(ATTACHMENT_POINTS[i]);
   170 	    glReadBuffer(ATTACHMENT_POINTS[i]); 
   171 	    return ATTACHMENT_POINTS[i]; // already attached
   172 	} else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
   173 	    attach = i;
   174 	}
   175     }
   176     if( attach == -1 ) {
   177 	/* should never happen */
   178 	attach = 0;
   179     }
   180     fbo[fbo_no].tex_ids[attach] = tex_id;
   181     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, 0 ); // Ensure the output texture is unbound
   182     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[attach], 
   183 			      GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
   184     /* Set draw/read buffers by default */
   185     glDrawBuffer(ATTACHMENT_POINTS[attach]);
   186     glReadBuffer(ATTACHMENT_POINTS[attach]); 
   189     GLint status = glGetError();
   190     if( status != GL_NO_ERROR ) {
   191 	ERROR( "GL error setting render target (%x)!", status );
   192     }
   193     status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   194     if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
   195 	ERROR( "Framebuffer failure: %x", status );
   196 	exit(1);
   197     }
   199     return ATTACHMENT_POINTS[attach];
   200 }
   202 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height )
   203 {
   204     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   205     buffer->width = width;
   206     buffer->height = height;
   207     glGenTextures( 1, &buffer->buf_id );
   208     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
   209     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
   210     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
   211     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   212     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   213     glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
   214     return buffer;
   215 }
   217 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
   218 {
   219     int i,j;
   220     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   221 	for( j=0; j < MAX_TEXTURES_PER_FB; j++ ) {
   222 	    if( fbo[i].tex_ids[j] == buffer->buf_id ) {
   223 		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[i].fb_id);
   224 		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[j], 
   225 					  GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
   226 		fbo[i].tex_ids[j] = -1;
   227 	    }
   228 	}
   229     }
   231     glDeleteTextures( 1, &buffer->buf_id );
   232     buffer->buf_id = 0;
   233     free( buffer );
   234 }
   236 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   237 {
   238     glFinish();
   239     glGetError();
   240     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   241     gl_fbo_attach_texture( fb, buffer->buf_id );
   242     /* setup the gl context */
   243     glViewport( 0, 0, buffer->width, buffer->height );
   244     glsl_enable_shader(TRUE);
   246     return TRUE;
   247 }
   249 /**
   250  * Render the texture holding the given buffer to the front window
   251  * buffer.
   252  */
   253 static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer )
   254 {
   255     glFinish();
   256     gl_fbo_detach();
   257     glsl_enable_shader(FALSE);
   258     gl_display_render_buffer( buffer );
   259     return TRUE;
   260 }
   262 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
   263 {
   264     glFinish();
   265     gl_fbo_detach();
   266     glsl_enable_shader(FALSE);
   267     gl_load_frame_buffer( frame, buffer->buf_id );
   268 }
   270 static gboolean gl_fbo_display_blank( uint32_t colour )
   271 {
   272     glFinish();
   273     gl_fbo_detach();
   274     glsl_enable_shader(FALSE);
   275     return gl_display_blank( colour );
   276 }
   278 void gl_fbo_detach()
   279 {
   280     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   281     glDrawBuffer( GL_FRONT );
   282     glReadBuffer( GL_FRONT );
   283 }    
   285 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   286 					   int rowstride, int format )
   287 {
   288     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   289     gl_fbo_attach_texture( fb, buffer->buf_id );
   290     return gl_read_render_buffer( target, buffer, rowstride, format );
   291 }
   293 #else
   294 gboolean gl_fbo_is_supported()
   295 {
   296     return FALSE;
   297 }
   299 void gl_fbo_init( display_driver_t driver ) 
   300 {
   301 }
   303 #endif
.