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 477:9a373f2ff009
prev445:75c668cdfdc0
next540:a3767018a96d
author nkeynes
date Wed Oct 31 09:10:23 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Add save/restore of render buffers in save states
Gzip memory blocks in save states
Move front-buffer management back to pvr2
Add screenshot preview when loading save states
Various minor fixes and cleanups
view annotate diff log raw
     1 /**
     2  * $Id: gl_fbo.c,v 1.7 2007-10-31 09:10:23 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 <stdlib.h>
    30 #include "lxdream.h"
    31 #include "display.h"
    32 #include "drivers/video_x11.h"
    33 #include "drivers/gl_common.h"
    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 );
   245     return TRUE;
   246 }
   248 /**
   249  * Render the texture holding the given buffer to the front window
   250  * buffer.
   251  */
   252 static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer )
   253 {
   254     glFinish();
   255     gl_fbo_detach();
   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     gl_load_frame_buffer( frame, buffer );
   265 }
   267 static gboolean gl_fbo_display_blank( uint32_t colour )
   268 {
   269     glFinish();
   270     gl_fbo_detach();
   271     return gl_display_blank( colour );
   272 }
   274 void gl_fbo_detach()
   275 {
   276     glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
   277     glDrawBuffer( GL_FRONT );
   278     glReadBuffer( GL_FRONT );
   279 }    
   281 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   282 					   int rowstride, int format )
   283 {
   284     int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
   285     gl_fbo_attach_texture( fb, buffer->buf_id );
   286     return gl_read_render_buffer( target, buffer, rowstride, format );
   287 }
.