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