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.
file annotate diff log raw
nkeynes@352
     1
/**
nkeynes@352
     2
 * $Id: gl_fbo.c,v 1.1 2007-02-11 10:09:32 nkeynes Exp $
nkeynes@352
     3
 *
nkeynes@352
     4
 * GL framebuffer-based driver shell. This requires the EXT_framebuffer_object
nkeynes@352
     5
 * extension, but is much nicer/faster/etc than pbuffers when it's available.
nkeynes@352
     6
 * This is (optionally) used indirectly by the top-level GLX driver.
nkeynes@352
     7
 *
nkeynes@352
     8
 * Strategy-wise, we maintain 2 framebuffers with up to 4 target colour
nkeynes@352
     9
 * buffers a piece. Effectively this reserves one fb for display output,
nkeynes@352
    10
 * and the other for texture output (each fb has a single size).
nkeynes@352
    11
 *
nkeynes@352
    12
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@352
    13
 *
nkeynes@352
    14
 * This program is free software; you can redistribute it and/or modify
nkeynes@352
    15
 * it under the terms of the GNU General Public License as published by
nkeynes@352
    16
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@352
    17
 * (at your option) any later version.
nkeynes@352
    18
 *
nkeynes@352
    19
 * This program is distributed in the hope that it will be useful,
nkeynes@352
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@352
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@352
    22
 * GNU General Public License for more details.
nkeynes@352
    23
 */
nkeynes@352
    24
nkeynes@352
    25
#include <GL/gl.h>
nkeynes@352
    26
#include "display.h"
nkeynes@352
    27
nkeynes@352
    28
#define MAX_FRAMEBUFFERS 2
nkeynes@352
    29
#define MAX_TEXTURES_PER_FB 4
nkeynes@352
    30
nkeynes@352
    31
static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height );
nkeynes@352
    32
static void gl_fbo_destroy_render_buffer( render_buffer_t buffer );
nkeynes@352
    33
static gboolean gl_fbo_set_render_target( render_buffer_t buffer );
nkeynes@352
    34
static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer );
nkeynes@352
    35
static gboolean gl_fbo_display_frame_buffer( frame_buffer_t buffer );
nkeynes@352
    36
static gboolean gl_fbo_display_blank( uint32_t colour );
nkeynes@352
    37
static gboolean gl_fbo_read_render_buffer( render_buffer_t buffer, char *target );
nkeynes@352
    38
nkeynes@352
    39
extern uint32_t video_width, video_height;
nkeynes@352
    40
nkeynes@352
    41
/**
nkeynes@352
    42
 * Framebuffer info structure
nkeynes@352
    43
 */
nkeynes@352
    44
struct gl_fbo_info {
nkeynes@352
    45
    GLuint fb_id;
nkeynes@352
    46
    GLuint depth_id;
nkeynes@352
    47
    GLuint stencil_id;
nkeynes@352
    48
    GLuint tex_ids[MAX_TEXTURES_PER_FB];
nkeynes@352
    49
    int width, height;
nkeynes@352
    50
};
nkeynes@352
    51
nkeynes@352
    52
static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
nkeynes@352
    53
const static int ATTACHMENT_POINTS[MAX_TEXTURES_PER_FB] = {
nkeynes@352
    54
    GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, 
nkeynes@352
    55
    GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT };
nkeynes@352
    56
static int last_used_fbo;
nkeynes@352
    57
nkeynes@352
    58
gboolean gl_fbo_is_supported()
nkeynes@352
    59
{
nkeynes@352
    60
    return isGLExtensionSupported("GL_EXT_framebuffer_object");
nkeynes@352
    61
}
nkeynes@352
    62
nkeynes@352
    63
/**
nkeynes@352
    64
 * Construct the initial frame buffers and allocate ids for everything.
nkeynes@352
    65
 * The render handling driver methods are set to the fbo versions.
nkeynes@352
    66
 */
nkeynes@352
    67
void gl_fbo_init( display_driver_t driver ) 
nkeynes@352
    68
{
nkeynes@352
    69
    int i,j;
nkeynes@352
    70
    int fbids[MAX_FRAMEBUFFERS];
nkeynes@352
    71
    int rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
nkeynes@352
    72
    
nkeynes@352
    73
    glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids );
nkeynes@352
    74
    glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids );
nkeynes@352
    75
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
    76
	fbo[i].fb_id = fbids[i];
nkeynes@352
    77
	fbo[i].depth_id = rbids[i*2];
nkeynes@352
    78
	fbo[i].stencil_id = rbids[i*2+1];
nkeynes@352
    79
	fbo[i].width = -1;
nkeynes@352
    80
	fbo[i].height = -1;
nkeynes@352
    81
	for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
nkeynes@352
    82
	    fbo[i].tex_ids[j] = -1;
nkeynes@352
    83
	}
nkeynes@352
    84
    }
nkeynes@352
    85
    last_used_fbo = 0;
nkeynes@352
    86
nkeynes@352
    87
    driver->create_render_buffer = gl_fbo_create_render_buffer;
nkeynes@352
    88
    driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
nkeynes@352
    89
    driver->set_render_target = gl_fbo_set_render_target;
nkeynes@352
    90
    driver->display_render_buffer = gl_fbo_display_render_buffer;
nkeynes@352
    91
    driver->display_frame_buffer = gl_fbo_display_frame_buffer;
nkeynes@352
    92
    driver->display_blank = gl_fbo_display_blank;
nkeynes@352
    93
    driver->read_render_buffer = gl_fbo_read_render_buffer;
nkeynes@352
    94
}
nkeynes@352
    95
nkeynes@352
    96
void gl_fbo_shutdown()
nkeynes@352
    97
{
nkeynes@352
    98
    int i;
nkeynes@352
    99
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
nkeynes@352
   100
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
   101
	glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
nkeynes@352
   102
	glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
nkeynes@352
   103
    }
nkeynes@352
   104
}
nkeynes@352
   105
nkeynes@352
   106
void gl_fbo_setup_framebuffer( int bufno, int width, int height )
nkeynes@352
   107
{
nkeynes@352
   108
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
nkeynes@352
   109
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
nkeynes@352
   110
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
nkeynes@352
   111
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
nkeynes@352
   112
				 GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
nkeynes@352
   113
    /* Stencil doesn't work on ATI, and we're not using it at the moment anyway, so...
nkeynes@352
   114
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
nkeynes@352
   115
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height);
nkeynes@352
   116
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
nkeynes@352
   117
				 GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
nkeynes@352
   118
    */
nkeynes@352
   119
    fbo[bufno].width = width;
nkeynes@352
   120
    fbo[bufno].height = height;
nkeynes@352
   121
}
nkeynes@352
   122
nkeynes@352
   123
int gl_fbo_get_framebuffer( int width, int height ) 
nkeynes@352
   124
{
nkeynes@352
   125
    int bufno = -1, i;
nkeynes@352
   126
    /* find a compatible framebuffer context */
nkeynes@352
   127
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
   128
	if( fbo[i].width == -1 && bufno == -1 ) {
nkeynes@352
   129
	    bufno = i;
nkeynes@352
   130
	} else if( fbo[i].width == width && fbo[i].height == height ) {
nkeynes@352
   131
	    bufno = i;
nkeynes@352
   132
	    break;
nkeynes@352
   133
	}
nkeynes@352
   134
    }
nkeynes@352
   135
    if( bufno == -1 ) {
nkeynes@352
   136
	bufno = last_used_fbo + 1;
nkeynes@352
   137
	if( bufno > MAX_FRAMEBUFFERS ) {
nkeynes@352
   138
	    bufno = 0;
nkeynes@352
   139
	}
nkeynes@352
   140
	last_used_fbo = bufno;
nkeynes@352
   141
    }
nkeynes@352
   142
    if( fbo[bufno].width == width && fbo[bufno].height == height ) {
nkeynes@352
   143
	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
nkeynes@352
   144
    } else {
nkeynes@352
   145
	gl_fbo_setup_framebuffer( bufno, width, height );
nkeynes@352
   146
    } 
nkeynes@352
   147
    return bufno;
nkeynes@352
   148
}
nkeynes@352
   149
nkeynes@352
   150
/**
nkeynes@352
   151
 * Attach a texture to the framebuffer. The texture must already be initialized
nkeynes@352
   152
 * to the correct dimensions etc.
nkeynes@352
   153
 */
nkeynes@352
   154
static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
nkeynes@352
   155
    int attach = -1, i;
nkeynes@352
   156
    for( i=0; i<MAX_TEXTURES_PER_FB; i++ ) {
nkeynes@352
   157
	if( fbo[fbo_no].tex_ids[i] == tex_id ) {
nkeynes@352
   158
	    glDrawBuffer(ATTACHMENT_POINTS[i]);
nkeynes@352
   159
	    glReadBuffer(ATTACHMENT_POINTS[i]); 
nkeynes@352
   160
	    return ATTACHMENT_POINTS[i]; // already attached
nkeynes@352
   161
	} else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
nkeynes@352
   162
	    attach = i;
nkeynes@352
   163
	}
nkeynes@352
   164
    }
nkeynes@352
   165
    if( attach == -1 ) {
nkeynes@352
   166
	/* should never happen */
nkeynes@352
   167
	attach = 0;
nkeynes@352
   168
    }
nkeynes@352
   169
    fbo[fbo_no].tex_ids[attach] = tex_id;
nkeynes@352
   170
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, 0 ); // Ensure the output texture is unbound
nkeynes@352
   171
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[attach], 
nkeynes@352
   172
			      GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
nkeynes@352
   173
    /* Set draw/read buffers by default */
nkeynes@352
   174
    glDrawBuffer(ATTACHMENT_POINTS[attach]);
nkeynes@352
   175
    glReadBuffer(ATTACHMENT_POINTS[attach]); 
nkeynes@352
   176
nkeynes@352
   177
nkeynes@352
   178
    GLint status = glGetError();
nkeynes@352
   179
    if( status != GL_NO_ERROR ) {
nkeynes@352
   180
	ERROR( "GL error setting render target (%x)!", status );
nkeynes@352
   181
    }
nkeynes@352
   182
    status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
nkeynes@352
   183
    if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
nkeynes@352
   184
	ERROR( "Framebuffer failure: %x", status );
nkeynes@352
   185
	exit(1);
nkeynes@352
   186
    }
nkeynes@352
   187
nkeynes@352
   188
    return ATTACHMENT_POINTS[attach];
nkeynes@352
   189
}
nkeynes@352
   190
nkeynes@352
   191
static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height )
nkeynes@352
   192
{
nkeynes@352
   193
    render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
nkeynes@352
   194
    buffer->width = width;
nkeynes@352
   195
    buffer->height = height;
nkeynes@352
   196
    glGenTextures( 1, &buffer->buf_id );
nkeynes@352
   197
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
nkeynes@352
   198
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@352
   199
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
nkeynes@352
   200
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
nkeynes@352
   201
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
nkeynes@352
   202
    glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
nkeynes@352
   203
    return buffer;
nkeynes@352
   204
}
nkeynes@352
   205
nkeynes@352
   206
static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
nkeynes@352
   207
{
nkeynes@352
   208
    int i,j;
nkeynes@352
   209
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
   210
	for( j=0; j < MAX_TEXTURES_PER_FB; j++ ) {
nkeynes@352
   211
	    if( fbo[i].tex_ids[j] == buffer->buf_id ) {
nkeynes@352
   212
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[j], 
nkeynes@352
   213
					  GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
nkeynes@352
   214
		fbo[i].tex_ids[j] = -1;
nkeynes@352
   215
	    }
nkeynes@352
   216
	}
nkeynes@352
   217
    }
nkeynes@352
   218
    
nkeynes@352
   219
    glDeleteTextures( 1, &buffer->buf_id );
nkeynes@352
   220
    buffer->buf_id = 0;
nkeynes@352
   221
    free( buffer );
nkeynes@352
   222
}
nkeynes@352
   223
nkeynes@352
   224
static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
nkeynes@352
   225
{
nkeynes@352
   226
    glGetError();
nkeynes@352
   227
    int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
nkeynes@352
   228
    GLint attach = gl_fbo_attach_texture( fb, buffer->buf_id );
nkeynes@352
   229
    /* setup the gl context */
nkeynes@352
   230
    glViewport( 0, 0, buffer->width, buffer->height );
nkeynes@352
   231
    
nkeynes@352
   232
    return TRUE;
nkeynes@352
   233
}
nkeynes@352
   234
nkeynes@352
   235
/**
nkeynes@352
   236
 * Render the texture holding the given buffer to the front window
nkeynes@352
   237
 * buffer.
nkeynes@352
   238
 */
nkeynes@352
   239
static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer )
nkeynes@352
   240
{
nkeynes@352
   241
    glFinish();
nkeynes@352
   242
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ); // real window
nkeynes@352
   243
    glViewport( 0, 0, video_width, video_height );
nkeynes@352
   244
    glEnable( GL_TEXTURE_RECTANGLE_ARB );
nkeynes@352
   245
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
nkeynes@352
   246
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
nkeynes@352
   247
    glDrawBuffer( GL_FRONT );
nkeynes@352
   248
    glReadBuffer( GL_FRONT );
nkeynes@352
   249
    glDisable( GL_ALPHA_TEST );
nkeynes@352
   250
    glDisable( GL_DEPTH_TEST );
nkeynes@352
   251
    glDisable( GL_SCISSOR_TEST );
nkeynes@352
   252
    glDisable( GL_CULL_FACE );
nkeynes@352
   253
nkeynes@352
   254
    glMatrixMode(GL_PROJECTION);
nkeynes@352
   255
    glLoadIdentity();
nkeynes@352
   256
    glOrtho( 0, buffer->width, buffer->height, 0, 0, -65535 );
nkeynes@352
   257
    glMatrixMode(GL_MODELVIEW);
nkeynes@352
   258
    glLoadIdentity();
nkeynes@352
   259
    glEnable( GL_BLEND );
nkeynes@352
   260
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@352
   261
    glDisable( GL_DEPTH_TEST );
nkeynes@352
   262
    glBegin( GL_QUADS );
nkeynes@352
   263
    glTexCoord2i( 0, buffer->height );
nkeynes@352
   264
    glVertex2f( 0.0, 0.0 );
nkeynes@352
   265
    glTexCoord2i( buffer->width, buffer->height );
nkeynes@352
   266
    glVertex2f( buffer->width, 0.0 );
nkeynes@352
   267
    glTexCoord2i( buffer->width, 0 );
nkeynes@352
   268
    glVertex2f( buffer->width, buffer->height );
nkeynes@352
   269
    glTexCoord2i( 0, 0 );
nkeynes@352
   270
    glVertex2f( 0.0, buffer->height );
nkeynes@352
   271
    glEnd();
nkeynes@352
   272
    glDisable( GL_TEXTURE_RECTANGLE_ARB );
nkeynes@352
   273
    glFlush();
nkeynes@352
   274
    return TRUE;
nkeynes@352
   275
}
nkeynes@352
   276
nkeynes@352
   277
static gboolean gl_fbo_display_frame_buffer( frame_buffer_t buffer )
nkeynes@352
   278
{
nkeynes@352
   279
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
nkeynes@352
   280
    glDrawBuffer( GL_FRONT );
nkeynes@352
   281
    glReadBuffer( GL_FRONT );
nkeynes@352
   282
    return gl_display_frame_buffer( buffer );
nkeynes@352
   283
}
nkeynes@352
   284
nkeynes@352
   285
static gboolean gl_fbo_display_blank( uint32_t colour )
nkeynes@352
   286
{
nkeynes@352
   287
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
nkeynes@352
   288
    glDrawBuffer( GL_FRONT );
nkeynes@352
   289
    glReadBuffer( GL_FRONT );
nkeynes@352
   290
    return gl_display_blank( colour );
nkeynes@352
   291
}
nkeynes@352
   292
nkeynes@352
   293
static gboolean gl_fbo_read_render_buffer( render_buffer_t buffer, char *target )
nkeynes@352
   294
{
nkeynes@352
   295
    int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
nkeynes@352
   296
    GLint attach = gl_fbo_attach_texture( fb, buffer->buf_id );
nkeynes@352
   297
    return gl_read_render_buffer( buffer, target );
nkeynes@352
   298
}
nkeynes@352
   299
.