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 352:f0df7a6d4703
next424:421d68e78c46
author nkeynes
date Sun Feb 11 10:09:32 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Bug 27: Implement opengl framebuffer objects
Rewrite much of the final video output stage. Now uses generic "render
buffers", implemented on GL using framebuffer objects + textures.
view annotate diff log raw
     1 /**
     2  * $Id: gl_fbo.c,v 1.1 2007-02-11 10:09:32 nkeynes Exp $
     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 #include <GL/gl.h>
    26 #include "display.h"
    28 #define MAX_FRAMEBUFFERS 2
    29 #define MAX_TEXTURES_PER_FB 4
    31 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height );
    32 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer );
    33 static gboolean gl_fbo_set_render_target( render_buffer_t buffer );
    34 static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer );
    35 static gboolean gl_fbo_display_frame_buffer( frame_buffer_t buffer );
    36 static gboolean gl_fbo_display_blank( uint32_t colour );
    37 static gboolean gl_fbo_read_render_buffer( render_buffer_t buffer, char *target );
    39 extern uint32_t video_width, video_height;
    41 /**
    42  * Framebuffer info structure
    43  */
    44 struct gl_fbo_info {
    45     GLuint fb_id;
    46     GLuint depth_id;
    47     GLuint stencil_id;
    48     GLuint tex_ids[MAX_TEXTURES_PER_FB];
    49     int width, height;
    50 };
    52 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
    53 const static int ATTACHMENT_POINTS[MAX_TEXTURES_PER_FB] = {
    54     GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, 
    55     GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT };
    56 static int last_used_fbo;
    58 gboolean gl_fbo_is_supported()
    59 {
    60     return isGLExtensionSupported("GL_EXT_framebuffer_object");
    61 }
    63 /**
    64  * Construct the initial frame buffers and allocate ids for everything.
    65  * The render handling driver methods are set to the fbo versions.
    66  */
    67 void gl_fbo_init( display_driver_t driver ) 
    68 {
    69     int i,j;
    70     int fbids[MAX_FRAMEBUFFERS];
    71     int rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
    73     glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids );
    74     glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids );
    75     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
    76 	fbo[i].fb_id = fbids[i];
    77 	fbo[i].depth_id = rbids[i*2];
    78 	fbo[i].stencil_id = rbids[i*2+1];
    79 	fbo[i].width = -1;
    80 	fbo[i].height = -1;
    81 	for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
    82 	    fbo[i].tex_ids[j] = -1;
    83 	}
    84     }
    85     last_used_fbo = 0;
    87     driver->create_render_buffer = gl_fbo_create_render_buffer;
    88     driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
    89     driver->set_render_target = gl_fbo_set_render_target;
    90     driver->display_render_buffer = gl_fbo_display_render_buffer;
    91     driver->display_frame_buffer = gl_fbo_display_frame_buffer;
    92     driver->display_blank = gl_fbo_display_blank;
    93     driver->read_render_buffer = gl_fbo_read_render_buffer;
    94 }
    96 void gl_fbo_shutdown()
    97 {
    98     int i;
    99     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   100     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   101 	glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
   102 	glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
   103     }
   104 }
   106 void gl_fbo_setup_framebuffer( int bufno, int width, int height )
   107 {
   108     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
   109     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   110     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
   111     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
   112 				 GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
   113     /* Stencil doesn't work on ATI, and we're not using it at the moment anyway, so...
   114     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   115     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height);
   116     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
   117 				 GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
   118     */
   119     fbo[bufno].width = width;
   120     fbo[bufno].height = height;
   121 }
   123 int gl_fbo_get_framebuffer( int width, int height ) 
   124 {
   125     int bufno = -1, i;
   126     /* find a compatible framebuffer context */
   127     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   128 	if( fbo[i].width == -1 && bufno == -1 ) {
   129 	    bufno = i;
   130 	} else if( fbo[i].width == width && fbo[i].height == height ) {
   131 	    bufno = i;
   132 	    break;
   133 	}
   134     }
   135     if( bufno == -1 ) {
   136 	bufno = last_used_fbo + 1;
   137 	if( bufno > MAX_FRAMEBUFFERS ) {
   138 	    bufno = 0;
   139 	}
   140 	last_used_fbo = bufno;
   141     }
   142     if( fbo[bufno].width == width && fbo[bufno].height == height ) {
   143 	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
   144     } else {
   145 	gl_fbo_setup_framebuffer( bufno, width, height );
   146     } 
   147     return bufno;
   148 }
   150 /**
   151  * Attach a texture to the framebuffer. The texture must already be initialized
   152  * to the correct dimensions etc.
   153  */
   154 static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
   155     int attach = -1, i;
   156     for( i=0; i<MAX_TEXTURES_PER_FB; i++ ) {
   157 	if( fbo[fbo_no].tex_ids[i] == tex_id ) {
   158 	    glDrawBuffer(ATTACHMENT_POINTS[i]);
   159 	    glReadBuffer(ATTACHMENT_POINTS[i]); 
   160 	    return ATTACHMENT_POINTS[i]; // already attached
   161 	} else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
   162 	    attach = i;
   163 	}
   164     }
   165     if( attach == -1 ) {
   166 	/* should never happen */
   167 	attach = 0;
   168     }
   169     fbo[fbo_no].tex_ids[attach] = tex_id;
   170     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, 0 ); // Ensure the output texture is unbound
   171     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[attach], 
   172 			      GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
   173     /* Set draw/read buffers by default */
   174     glDrawBuffer(ATTACHMENT_POINTS[attach]);
   175     glReadBuffer(ATTACHMENT_POINTS[attach]); 
   178     GLint status = glGetError();
   179     if( status != GL_NO_ERROR ) {
   180 	ERROR( "GL error setting render target (%x)!", status );
   181     }
   182     status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   183     if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
   184 	ERROR( "Framebuffer failure: %x", status );
   185 	exit(1);
   186     }
   188     return ATTACHMENT_POINTS[attach];
   189 }
   191 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height )
   192 {
   193     render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
   194     buffer->width = width;
   195     buffer->height = height;
   196     glGenTextures( 1, &buffer->buf_id );
   197     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
   198     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
   199     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
   200     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   201     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   202     glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
   203     return buffer;
   204 }
   206 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
   207 {
   208     int i,j;
   209     for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
   210 	for( j=0; j < MAX_TEXTURES_PER_FB; j++ ) {
   211 	    if( fbo[i].tex_ids[j] == buffer->buf_id ) {
   212 		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[j], 
   213 					  GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
   214 		fbo[i].tex_ids[j] = -1;
   215 	    }
   216 	}
   217     }
   219     glDeleteTextures( 1, &buffer->buf_id );
   220     buffer->buf_id = 0;
   221     free( buffer );
   222 }
   224 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   225 {
   226     glGetError();
   227     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   228     GLint attach = gl_fbo_attach_texture( fb, buffer->buf_id );
   229     /* setup the gl context */
   230     glViewport( 0, 0, buffer->width, buffer->height );
   232     return TRUE;
   233 }
   235 /**
   236  * Render the texture holding the given buffer to the front window
   237  * buffer.
   238  */
   239 static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer )
   240 {
   241     glFinish();
   242     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ); // real window
   243     glViewport( 0, 0, video_width, video_height );
   244     glEnable( GL_TEXTURE_RECTANGLE_ARB );
   245     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
   246     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   247     glDrawBuffer( GL_FRONT );
   248     glReadBuffer( GL_FRONT );
   249     glDisable( GL_ALPHA_TEST );
   250     glDisable( GL_DEPTH_TEST );
   251     glDisable( GL_SCISSOR_TEST );
   252     glDisable( GL_CULL_FACE );
   254     glMatrixMode(GL_PROJECTION);
   255     glLoadIdentity();
   256     glOrtho( 0, buffer->width, buffer->height, 0, 0, -65535 );
   257     glMatrixMode(GL_MODELVIEW);
   258     glLoadIdentity();
   259     glEnable( GL_BLEND );
   260     glBlendFunc( GL_ONE, GL_ZERO );
   261     glDisable( GL_DEPTH_TEST );
   262     glBegin( GL_QUADS );
   263     glTexCoord2i( 0, buffer->height );
   264     glVertex2f( 0.0, 0.0 );
   265     glTexCoord2i( buffer->width, buffer->height );
   266     glVertex2f( buffer->width, 0.0 );
   267     glTexCoord2i( buffer->width, 0 );
   268     glVertex2f( buffer->width, buffer->height );
   269     glTexCoord2i( 0, 0 );
   270     glVertex2f( 0.0, buffer->height );
   271     glEnd();
   272     glDisable( GL_TEXTURE_RECTANGLE_ARB );
   273     glFlush();
   274     return TRUE;
   275 }
   277 static gboolean gl_fbo_display_frame_buffer( frame_buffer_t buffer )
   278 {
   279     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   280     glDrawBuffer( GL_FRONT );
   281     glReadBuffer( GL_FRONT );
   282     return gl_display_frame_buffer( buffer );
   283 }
   285 static gboolean gl_fbo_display_blank( uint32_t colour )
   286 {
   287     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   288     glDrawBuffer( GL_FRONT );
   289     glReadBuffer( GL_FRONT );
   290     return gl_display_blank( colour );
   291 }
   293 static gboolean gl_fbo_read_render_buffer( render_buffer_t buffer, char *target )
   294 {
   295     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   296     GLint attach = gl_fbo_attach_texture( fb, buffer->buf_id );
   297     return gl_read_render_buffer( buffer, target );
   298 }
.