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