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 445:75c668cdfdc0
prev443:1163eb5c0590
next477:9a373f2ff009
author nkeynes
date Tue Oct 16 12:27:28 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Unbind the framebuffer after initialization
view annotate diff log raw
     1 /**
     2  * $Id: gl_fbo.c,v 1.6 2007-10-16 12:27:28 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 #define GL_GLEXT_PROTOTYPES 1
    27 #include <GL/gl.h>
    28 #include <GL/glext.h>
    29 #include "display.h"
    30 #include "drivers/video_x11.h"
    31 #include "drivers/gl_common.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 gboolean gl_fbo_display_frame_buffer( frame_buffer_t buffer );
    41 static gboolean gl_fbo_display_blank( uint32_t colour );
    42 static gboolean gl_fbo_read_render_buffer( render_buffer_t buffer, unsigned char *target );
    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->display_frame_buffer = gl_fbo_display_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 		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[j], 
   222 					  GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
   223 		fbo[i].tex_ids[j] = -1;
   224 	    }
   225 	}
   226     }
   228     glDeleteTextures( 1, &buffer->buf_id );
   229     buffer->buf_id = 0;
   230     free( buffer );
   231 }
   233 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
   234 {
   235     glGetError();
   236     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   237     gl_fbo_attach_texture( fb, buffer->buf_id );
   238     /* setup the gl context */
   239     glViewport( 0, 0, buffer->width, buffer->height );
   241     return TRUE;
   242 }
   244 /**
   245  * Render the texture holding the given buffer to the front window
   246  * buffer.
   247  */
   248 static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer )
   249 {
   250     glFinish();
   251     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ); // real window
   252     glDrawBuffer( GL_FRONT );
   253     glReadBuffer( GL_FRONT );
   254     gl_display_tex_rectangle( buffer->buf_id, buffer->width, buffer->height, TRUE );
   255     /*
   256     glViewport( 0, 0, video_width, video_height );
   257     glEnable( GL_TEXTURE_RECTANGLE_ARB );
   258     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
   259     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   260     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
   261     glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
   262     glDisable( GL_ALPHA_TEST );
   263     glDisable( GL_DEPTH_TEST );
   264     glDisable( GL_SCISSOR_TEST );
   265     glDisable( GL_CULL_FACE );
   267     glMatrixMode(GL_PROJECTION);
   268     glLoadIdentity();
   269     glOrtho( 0, buffer->width, buffer->height, 0, 0, -65535 );
   270     glMatrixMode(GL_MODELVIEW);
   271     glLoadIdentity();
   272     glEnable( GL_BLEND );
   273     glBlendFunc( GL_ONE, GL_ZERO );
   274     glDisable( GL_DEPTH_TEST );
   275     glBegin( GL_QUADS );
   276     glTexCoord2i( 0.5, buffer->height-0.5 );
   277     glVertex2f( 0.0, 0.0 );
   278     glTexCoord2i( buffer->width-0.5, buffer->height-0.5 );
   279     glVertex2f( buffer->width, 0.0 );
   280     glTexCoord2i( buffer->width-0.5, 0 );
   281     glVertex2f( buffer->width, buffer->height );
   282     glTexCoord2i( 0.5, 0.5 );
   283     glVertex2f( 0.0, buffer->height );
   284     glEnd();
   285     glDisable( GL_TEXTURE_RECTANGLE_ARB );
   286     glFlush();
   287     */
   288     return TRUE;
   289 }
   291 static gboolean gl_fbo_display_frame_buffer( frame_buffer_t buffer )
   292 {
   293     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   294     glDrawBuffer( GL_FRONT );
   295     glReadBuffer( GL_FRONT );
   296     return gl_display_frame_buffer( buffer );
   297 }
   299 static gboolean gl_fbo_display_blank( uint32_t colour )
   300 {
   301     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   302     glDrawBuffer( GL_FRONT );
   303     glReadBuffer( GL_FRONT );
   304     return gl_display_blank( colour );
   305 }
   307 static gboolean gl_fbo_read_render_buffer( render_buffer_t buffer, unsigned char *target )
   308 {
   309     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   310     gl_fbo_attach_texture( fb, buffer->buf_id );
   311     return gl_read_render_buffer( buffer, target );
   312 }
.