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 656:031d23fa6d0b
prev635:76c63aac3590
next669:ab344e42bca9
author nkeynes
date Wed Apr 02 01:46:58 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Add configure-time checks for fbo and shader functions in libGL (so linking
doesn't fail on really old libGL implementations)
file annotate diff log raw
nkeynes@352
     1
/**
nkeynes@561
     2
 * $Id$
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@424
    25
#define GL_GLEXT_PROTOTYPES 1
nkeynes@424
    26
nkeynes@477
    27
#include <stdlib.h>
nkeynes@477
    28
#include "lxdream.h"
nkeynes@352
    29
#include "display.h"
nkeynes@635
    30
#include "drivers/video_gl.h"
nkeynes@635
    31
#include "pvr2/glutil.h"
nkeynes@352
    32
nkeynes@656
    33
#ifdef HAVE_OPENGL_FBO
nkeynes@656
    34
nkeynes@352
    35
#define MAX_FRAMEBUFFERS 2
nkeynes@352
    36
#define MAX_TEXTURES_PER_FB 4
nkeynes@352
    37
nkeynes@352
    38
static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height );
nkeynes@352
    39
static void gl_fbo_destroy_render_buffer( render_buffer_t buffer );
nkeynes@352
    40
static gboolean gl_fbo_set_render_target( render_buffer_t buffer );
nkeynes@352
    41
static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer );
nkeynes@477
    42
static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
nkeynes@352
    43
static gboolean gl_fbo_display_blank( uint32_t colour );
nkeynes@477
    44
static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
nkeynes@352
    45
nkeynes@352
    46
extern uint32_t video_width, video_height;
nkeynes@352
    47
nkeynes@352
    48
/**
nkeynes@352
    49
 * Framebuffer info structure
nkeynes@352
    50
 */
nkeynes@352
    51
struct gl_fbo_info {
nkeynes@352
    52
    GLuint fb_id;
nkeynes@352
    53
    GLuint depth_id;
nkeynes@352
    54
    GLuint stencil_id;
nkeynes@352
    55
    GLuint tex_ids[MAX_TEXTURES_PER_FB];
nkeynes@352
    56
    int width, height;
nkeynes@352
    57
};
nkeynes@352
    58
nkeynes@352
    59
static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
nkeynes@352
    60
const static int ATTACHMENT_POINTS[MAX_TEXTURES_PER_FB] = {
nkeynes@352
    61
    GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, 
nkeynes@352
    62
    GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT };
nkeynes@352
    63
static int last_used_fbo;
nkeynes@352
    64
nkeynes@352
    65
gboolean gl_fbo_is_supported()
nkeynes@352
    66
{
nkeynes@352
    67
    return isGLExtensionSupported("GL_EXT_framebuffer_object");
nkeynes@352
    68
}
nkeynes@352
    69
nkeynes@352
    70
/**
nkeynes@352
    71
 * Construct the initial frame buffers and allocate ids for everything.
nkeynes@352
    72
 * The render handling driver methods are set to the fbo versions.
nkeynes@352
    73
 */
nkeynes@352
    74
void gl_fbo_init( display_driver_t driver ) 
nkeynes@352
    75
{
nkeynes@352
    76
    int i,j;
nkeynes@424
    77
    GLuint fbids[MAX_FRAMEBUFFERS];
nkeynes@424
    78
    GLuint rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
nkeynes@352
    79
    
nkeynes@424
    80
    glGenFramebuffersEXT( MAX_FRAMEBUFFERS, &fbids[0] );
nkeynes@424
    81
    glGenRenderbuffersEXT( MAX_FRAMEBUFFERS*2, &rbids[0] );
nkeynes@352
    82
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
    83
	fbo[i].fb_id = fbids[i];
nkeynes@352
    84
	fbo[i].depth_id = rbids[i*2];
nkeynes@352
    85
	fbo[i].stencil_id = rbids[i*2+1];
nkeynes@352
    86
	fbo[i].width = -1;
nkeynes@352
    87
	fbo[i].height = -1;
nkeynes@352
    88
	for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
nkeynes@352
    89
	    fbo[i].tex_ids[j] = -1;
nkeynes@352
    90
	}
nkeynes@352
    91
    }
nkeynes@352
    92
    last_used_fbo = 0;
nkeynes@352
    93
nkeynes@352
    94
    driver->create_render_buffer = gl_fbo_create_render_buffer;
nkeynes@352
    95
    driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
nkeynes@352
    96
    driver->set_render_target = gl_fbo_set_render_target;
nkeynes@352
    97
    driver->display_render_buffer = gl_fbo_display_render_buffer;
nkeynes@477
    98
    driver->load_frame_buffer = gl_fbo_load_frame_buffer;
nkeynes@352
    99
    driver->display_blank = gl_fbo_display_blank;
nkeynes@352
   100
    driver->read_render_buffer = gl_fbo_read_render_buffer;
nkeynes@445
   101
nkeynes@445
   102
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
nkeynes@445
   103
    glDrawBuffer(GL_FRONT);
nkeynes@445
   104
    glReadBuffer(GL_FRONT);
nkeynes@352
   105
}
nkeynes@352
   106
nkeynes@352
   107
void gl_fbo_shutdown()
nkeynes@352
   108
{
nkeynes@352
   109
    int i;
nkeynes@352
   110
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
nkeynes@352
   111
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
   112
	glDeleteFramebuffersEXT( 1, &fbo[i].fb_id );
nkeynes@352
   113
	glDeleteRenderbuffersEXT( 2, &fbo[i].depth_id );
nkeynes@352
   114
    }
nkeynes@352
   115
}
nkeynes@352
   116
nkeynes@352
   117
void gl_fbo_setup_framebuffer( int bufno, int width, int height )
nkeynes@352
   118
{
nkeynes@352
   119
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id);
nkeynes@352
   120
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
nkeynes@352
   121
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
nkeynes@352
   122
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
nkeynes@352
   123
				 GL_RENDERBUFFER_EXT, fbo[bufno].depth_id);
nkeynes@352
   124
    /* Stencil doesn't work on ATI, and we're not using it at the moment anyway, so...
nkeynes@352
   125
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
nkeynes@352
   126
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height);
nkeynes@352
   127
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
nkeynes@352
   128
				 GL_RENDERBUFFER_EXT, fbo[bufno].stencil_id);
nkeynes@352
   129
    */
nkeynes@352
   130
    fbo[bufno].width = width;
nkeynes@352
   131
    fbo[bufno].height = height;
nkeynes@352
   132
}
nkeynes@352
   133
nkeynes@352
   134
int gl_fbo_get_framebuffer( int width, int height ) 
nkeynes@352
   135
{
nkeynes@352
   136
    int bufno = -1, i;
nkeynes@352
   137
    /* find a compatible framebuffer context */
nkeynes@352
   138
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
   139
	if( fbo[i].width == -1 && bufno == -1 ) {
nkeynes@352
   140
	    bufno = i;
nkeynes@352
   141
	} else if( fbo[i].width == width && fbo[i].height == height ) {
nkeynes@352
   142
	    bufno = i;
nkeynes@352
   143
	    break;
nkeynes@352
   144
	}
nkeynes@352
   145
    }
nkeynes@352
   146
    if( bufno == -1 ) {
nkeynes@352
   147
	bufno = last_used_fbo + 1;
nkeynes@352
   148
	if( bufno > MAX_FRAMEBUFFERS ) {
nkeynes@352
   149
	    bufno = 0;
nkeynes@352
   150
	}
nkeynes@352
   151
	last_used_fbo = bufno;
nkeynes@352
   152
    }
nkeynes@352
   153
    if( fbo[bufno].width == width && fbo[bufno].height == height ) {
nkeynes@352
   154
	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo[bufno].fb_id );
nkeynes@352
   155
    } else {
nkeynes@352
   156
	gl_fbo_setup_framebuffer( bufno, width, height );
nkeynes@352
   157
    } 
nkeynes@352
   158
    return bufno;
nkeynes@352
   159
}
nkeynes@352
   160
nkeynes@352
   161
/**
nkeynes@352
   162
 * Attach a texture to the framebuffer. The texture must already be initialized
nkeynes@352
   163
 * to the correct dimensions etc.
nkeynes@352
   164
 */
nkeynes@352
   165
static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
nkeynes@352
   166
    int attach = -1, i;
nkeynes@352
   167
    for( i=0; i<MAX_TEXTURES_PER_FB; i++ ) {
nkeynes@352
   168
	if( fbo[fbo_no].tex_ids[i] == tex_id ) {
nkeynes@352
   169
	    glDrawBuffer(ATTACHMENT_POINTS[i]);
nkeynes@352
   170
	    glReadBuffer(ATTACHMENT_POINTS[i]); 
nkeynes@352
   171
	    return ATTACHMENT_POINTS[i]; // already attached
nkeynes@352
   172
	} else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
nkeynes@352
   173
	    attach = i;
nkeynes@352
   174
	}
nkeynes@352
   175
    }
nkeynes@352
   176
    if( attach == -1 ) {
nkeynes@352
   177
	/* should never happen */
nkeynes@352
   178
	attach = 0;
nkeynes@352
   179
    }
nkeynes@352
   180
    fbo[fbo_no].tex_ids[attach] = tex_id;
nkeynes@352
   181
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, 0 ); // Ensure the output texture is unbound
nkeynes@352
   182
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[attach], 
nkeynes@352
   183
			      GL_TEXTURE_RECTANGLE_ARB, tex_id, 0 );
nkeynes@352
   184
    /* Set draw/read buffers by default */
nkeynes@352
   185
    glDrawBuffer(ATTACHMENT_POINTS[attach]);
nkeynes@352
   186
    glReadBuffer(ATTACHMENT_POINTS[attach]); 
nkeynes@352
   187
nkeynes@352
   188
nkeynes@352
   189
    GLint status = glGetError();
nkeynes@352
   190
    if( status != GL_NO_ERROR ) {
nkeynes@352
   191
	ERROR( "GL error setting render target (%x)!", status );
nkeynes@352
   192
    }
nkeynes@352
   193
    status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
nkeynes@352
   194
    if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) {
nkeynes@352
   195
	ERROR( "Framebuffer failure: %x", status );
nkeynes@352
   196
	exit(1);
nkeynes@352
   197
    }
nkeynes@352
   198
nkeynes@352
   199
    return ATTACHMENT_POINTS[attach];
nkeynes@352
   200
}
nkeynes@352
   201
nkeynes@352
   202
static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height )
nkeynes@352
   203
{
nkeynes@352
   204
    render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
nkeynes@352
   205
    buffer->width = width;
nkeynes@352
   206
    buffer->height = height;
nkeynes@352
   207
    glGenTextures( 1, &buffer->buf_id );
nkeynes@352
   208
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
nkeynes@352
   209
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@352
   210
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
nkeynes@352
   211
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
nkeynes@352
   212
    glTexParameteri( GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
nkeynes@352
   213
    glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
nkeynes@352
   214
    return buffer;
nkeynes@352
   215
}
nkeynes@352
   216
nkeynes@352
   217
static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
nkeynes@352
   218
{
nkeynes@352
   219
    int i,j;
nkeynes@352
   220
    for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
nkeynes@352
   221
	for( j=0; j < MAX_TEXTURES_PER_FB; j++ ) {
nkeynes@352
   222
	    if( fbo[i].tex_ids[j] == buffer->buf_id ) {
nkeynes@477
   223
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[i].fb_id);
nkeynes@352
   224
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ATTACHMENT_POINTS[j], 
nkeynes@352
   225
					  GL_TEXTURE_RECTANGLE_ARB, GL_NONE, 0 );
nkeynes@352
   226
		fbo[i].tex_ids[j] = -1;
nkeynes@352
   227
	    }
nkeynes@352
   228
	}
nkeynes@352
   229
    }
nkeynes@352
   230
    
nkeynes@352
   231
    glDeleteTextures( 1, &buffer->buf_id );
nkeynes@352
   232
    buffer->buf_id = 0;
nkeynes@352
   233
    free( buffer );
nkeynes@352
   234
}
nkeynes@352
   235
nkeynes@352
   236
static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
nkeynes@352
   237
{
nkeynes@477
   238
    glFinish();
nkeynes@352
   239
    glGetError();
nkeynes@352
   240
    int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
nkeynes@424
   241
    gl_fbo_attach_texture( fb, buffer->buf_id );
nkeynes@352
   242
    /* setup the gl context */
nkeynes@352
   243
    glViewport( 0, 0, buffer->width, buffer->height );
nkeynes@635
   244
    glsl_enable_shader(TRUE);
nkeynes@352
   245
    
nkeynes@352
   246
    return TRUE;
nkeynes@352
   247
}
nkeynes@352
   248
nkeynes@352
   249
/**
nkeynes@352
   250
 * Render the texture holding the given buffer to the front window
nkeynes@352
   251
 * buffer.
nkeynes@352
   252
 */
nkeynes@352
   253
static gboolean gl_fbo_display_render_buffer( render_buffer_t buffer )
nkeynes@352
   254
{
nkeynes@352
   255
    glFinish();
nkeynes@477
   256
    gl_fbo_detach();
nkeynes@635
   257
    glsl_enable_shader(FALSE);
nkeynes@477
   258
    gl_display_render_buffer( buffer );
nkeynes@352
   259
    return TRUE;
nkeynes@352
   260
}
nkeynes@352
   261
nkeynes@477
   262
static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
nkeynes@477
   263
{
nkeynes@477
   264
    glFinish();
nkeynes@477
   265
    gl_fbo_detach();
nkeynes@635
   266
    glsl_enable_shader(FALSE);
nkeynes@545
   267
    gl_load_frame_buffer( frame, buffer->buf_id );
nkeynes@477
   268
}
nkeynes@477
   269
nkeynes@477
   270
static gboolean gl_fbo_display_blank( uint32_t colour )
nkeynes@477
   271
{
nkeynes@477
   272
    glFinish();
nkeynes@477
   273
    gl_fbo_detach();
nkeynes@635
   274
    glsl_enable_shader(FALSE);
nkeynes@477
   275
    return gl_display_blank( colour );
nkeynes@477
   276
}
nkeynes@477
   277
nkeynes@477
   278
void gl_fbo_detach()
nkeynes@352
   279
{
nkeynes@352
   280
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
nkeynes@352
   281
    glDrawBuffer( GL_FRONT );
nkeynes@352
   282
    glReadBuffer( GL_FRONT );
nkeynes@477
   283
}    
nkeynes@352
   284
nkeynes@477
   285
static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@477
   286
					   int rowstride, int format )
nkeynes@352
   287
{
nkeynes@352
   288
    int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
nkeynes@424
   289
    gl_fbo_attach_texture( fb, buffer->buf_id );
nkeynes@477
   290
    return gl_read_render_buffer( target, buffer, rowstride, format );
nkeynes@352
   291
}
nkeynes@352
   292
nkeynes@656
   293
#else
nkeynes@656
   294
gboolean gl_fbo_is_supported()
nkeynes@656
   295
{
nkeynes@656
   296
    return FALSE;
nkeynes@656
   297
}
nkeynes@656
   298
nkeynes@656
   299
void gl_fbo_init( display_driver_t driver ) 
nkeynes@656
   300
{
nkeynes@656
   301
}
nkeynes@656
   302
nkeynes@656
   303
#endif
.