filename | src/drivers/gl_fbo.c |
changeset | 1251:b8ab59d39756 |
prev | 1245:01e0020adf88 |
next | 1298:d0eb2307b847 |
author | nkeynes |
date | Sat Mar 03 16:11:28 2012 +1000 (12 years ago) |
permissions | -rw-r--r-- |
last change | Support depth component 16 as well as 24 (add capability flag for the available bits) Put remaining TODOs inside HAVE_OPENGL_FIXEDFUNC blocks Add swap-buffer calls for EGL (does not appear to support rendering directly to front-buffer) |
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/video_gl.h"
31 #include "pvr2/glutil.h"
33 #if defined(HAVE_OPENGL_FBO) || defined(HAVE_OPENGL_FBO_EXT)
35 #define MAX_FRAMEBUFFERS 2
36 #define MAX_TEXTURES_PER_FB 16
38 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id );
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 void gl_fbo_finish_render( render_buffer_t buffer );
42 static void gl_fbo_display_render_buffer( render_buffer_t buffer );
43 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
44 static void gl_fbo_display_blank( uint32_t colour );
45 static gboolean gl_fbo_test_framebuffer( );
46 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
48 extern uint32_t video_width, video_height;
50 /**
51 * Framebuffer info structure
52 */
53 struct gl_fbo_info {
54 GLuint fb_id;
55 GLuint depth_id;
56 GLuint stencil_id;
57 GLuint tex_ids[MAX_TEXTURES_PER_FB];
58 int width, height;
59 };
61 static GLint gl_fbo_max_attachments = 0;
62 static GLint gl_fbo_depth_component;
63 static gboolean gl_fbo_have_packed_stencil = FALSE;
64 static struct gl_fbo_info fbo[MAX_FRAMEBUFFERS];
66 #define ATTACHMENT_POINT(n) (GL_COLOR_ATTACHMENT0+(n))
67 static int last_used_fbo;
69 gboolean gl_fbo_is_supported()
70 {
71 return isGLExtensionSupported("GL_EXT_framebuffer_object") || isOpenGLES2();
72 }
74 /**
75 * Construct the initial frame buffers and allocate ids for everything.
76 * The render handling driver methods are set to the fbo versions.
77 */
78 void gl_fbo_init( display_driver_t driver )
79 {
80 int i,j;
81 GLuint fbids[MAX_FRAMEBUFFERS];
82 GLuint rbids[MAX_FRAMEBUFFERS*2]; /* depth buffer, stencil buffer per fb */
84 gl_fbo_max_attachments = glGetMaxColourAttachments();
85 glGenFramebuffers( MAX_FRAMEBUFFERS, &fbids[0] );
86 glGenRenderbuffers( MAX_FRAMEBUFFERS*2, &rbids[0] );
87 for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
88 fbo[i].fb_id = fbids[i];
89 fbo[i].depth_id = rbids[i*2];
90 fbo[i].stencil_id = rbids[i*2+1];
91 fbo[i].width = -1;
92 fbo[i].height = -1;
93 for( j=0; j<MAX_TEXTURES_PER_FB; j++ ) {
94 fbo[i].tex_ids[j] = -1;
95 }
96 }
97 last_used_fbo = 0;
99 if( isGLExtensionSupported("GL_EXT_packed_depth_stencil" ) ) {
100 driver->capabilities.stencil_bits = 8;
101 gl_fbo_have_packed_stencil = TRUE;
102 } else {
103 driver->capabilities.stencil_bits = 0;
104 gl_fbo_have_packed_stencil = FALSE;
105 WARN( "Packed depth stencil not available - disabling shadow volumes" );
106 }
107 if( driver->capabilities.depth_bits >= 24 ) {
108 gl_fbo_depth_component = GL_DEPTH_COMPONENT24;
109 } else {
110 gl_fbo_depth_component = GL_DEPTH_COMPONENT16;
111 }
113 driver->create_render_buffer = gl_fbo_create_render_buffer;
114 driver->destroy_render_buffer = gl_fbo_destroy_render_buffer;
115 driver->set_render_target = gl_fbo_set_render_target;
116 driver->finish_render = gl_fbo_finish_render;
117 driver->display_render_buffer = gl_fbo_display_render_buffer;
118 driver->load_frame_buffer = gl_fbo_load_frame_buffer;
119 driver->display_blank = gl_fbo_display_blank;
120 driver->read_render_buffer = gl_fbo_read_render_buffer;
122 gl_fbo_test_framebuffer();
123 glBindFramebuffer(GL_FRAMEBUFFER, 0);
124 }
126 void gl_fbo_shutdown()
127 {
128 int i;
129 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
130 for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
131 glDeleteFramebuffers( 1, &fbo[i].fb_id );
132 glDeleteRenderbuffers( 2, &fbo[i].depth_id );
133 }
134 }
136 static void gl_fbo_setup_framebuffer( int bufno, int width, int height )
137 {
138 int i;
139 glBindFramebuffer(GL_FRAMEBUFFER, fbo[bufno].fb_id);
141 /* Clear out any existing texture attachments */
142 for( i=0; i<gl_fbo_max_attachments; i++ ) {
143 if( fbo[bufno].tex_ids[i] != -1 ) {
144 glFramebufferTexture2D(GL_FRAMEBUFFER, ATTACHMENT_POINT(i),
145 GL_TEXTURE_2D, 0, 0);
146 fbo[bufno].tex_ids[i] = -1;
147 }
148 }
150 /* Setup the renderbuffers */
151 if( gl_fbo_have_packed_stencil ) {
152 glBindRenderbuffer(GL_RENDERBUFFER, fbo[bufno].depth_id);
153 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
154 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
155 GL_RENDERBUFFER, fbo[bufno].depth_id);
156 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
157 GL_RENDERBUFFER, fbo[bufno].depth_id);
158 } else {
159 glBindRenderbuffer(GL_RENDERBUFFER, fbo[bufno].depth_id);
160 glRenderbufferStorage(GL_RENDERBUFFER, gl_fbo_depth_component, width, height);
161 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
162 GL_RENDERBUFFER, fbo[bufno].depth_id);
163 /* In theory you could attach a separate stencil buffer. In practice this
164 * isn't actually supported by any hardware I've had access to, so we're
165 * stencil-less.
166 */
167 }
168 fbo[bufno].width = width;
169 fbo[bufno].height = height;
170 }
172 static int gl_fbo_get_framebuffer( int width, int height )
173 {
174 int bufno = -1, i;
175 /* find a compatible framebuffer context */
176 for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
177 if( fbo[i].width == -1 && bufno == -1 ) {
178 bufno = i;
179 } else if( fbo[i].width == width && fbo[i].height == height ) {
180 bufno = i;
181 break;
182 }
183 }
184 if( bufno == -1 ) {
185 bufno = last_used_fbo + 1;
186 if( bufno >= MAX_FRAMEBUFFERS ) {
187 bufno = 0;
188 }
189 last_used_fbo = bufno;
190 }
191 if( fbo[bufno].width == width && fbo[bufno].height == height ) {
192 glBindFramebuffer( GL_FRAMEBUFFER, fbo[bufno].fb_id );
193 } else {
194 gl_fbo_setup_framebuffer( bufno, width, height );
195 }
196 return bufno;
197 }
199 /**
200 * Attach a texture to the framebuffer. The texture must already be initialized
201 * to the correct dimensions etc.
202 */
203 static GLint gl_fbo_attach_texture( int fbo_no, GLint tex_id ) {
204 int attach = -1, i;
205 for( i=0; i<gl_fbo_max_attachments; i++ ) {
206 if( fbo[fbo_no].tex_ids[i] == tex_id ) {
207 #ifdef HAVE_OPENGL_DRAW_BUFFER
208 glDrawBuffer(ATTACHMENT_POINT(i));
209 glReadBuffer(ATTACHMENT_POINT(i));
210 #endif
211 return ATTACHMENT_POINT(i); // already attached
212 } else if( fbo[fbo_no].tex_ids[i] == -1 && attach == -1 ) {
213 attach = i;
214 }
215 }
216 if( attach == -1 ) {
217 attach = 0;
218 }
219 fbo[fbo_no].tex_ids[attach] = tex_id;
220 glBindTexture( GL_TEXTURE_2D, 0 ); // Ensure the output texture is unbound
221 glFramebufferTexture2D(GL_FRAMEBUFFER, ATTACHMENT_POINT(attach),
222 GL_TEXTURE_2D, tex_id, 0 );
223 /* Set draw/read buffers by default */
224 #ifdef HAVE_OPENGL_DRAW_BUFFER
225 glDrawBuffer(ATTACHMENT_POINT(attach));
226 glReadBuffer(ATTACHMENT_POINT(attach));
227 #endif
229 return ATTACHMENT_POINT(attach);
230 }
232 static gboolean gl_fbo_test_framebuffer( )
233 {
234 gboolean result = TRUE;
235 glGetError(); /* Clear error state just in case */
236 render_buffer_t buffer = gl_fbo_create_render_buffer( 640, 480, 0 );
237 gl_fbo_set_render_target(buffer);
239 GLint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
240 if( status != GL_FRAMEBUFFER_COMPLETE ) {
241 ERROR( "Framebuffer failure: %x", status );
242 result = FALSE;
243 }
244 if( result ) {
245 result = gl_check_error( "Setting up framebuffer" );
246 }
248 gl_fbo_destroy_render_buffer( buffer );
249 return result;
250 }
252 static render_buffer_t gl_fbo_create_render_buffer( uint32_t width, uint32_t height, GLuint tex_id )
253 {
254 render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
255 buffer->width = width;
256 buffer->height = height;
257 buffer->tex_id = tex_id;
258 if( tex_id == 0 ) {
259 GLuint tex;
260 glGenTextures( 1, &tex );
261 buffer->buf_id = tex;
262 } else {
263 buffer->buf_id = tex_id;
264 glBindTexture( GL_TEXTURE_2D, tex_id );
265 }
266 glBindTexture( GL_TEXTURE_2D, buffer->buf_id );
267 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
268 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
269 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
270 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
271 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
272 return buffer;
273 }
275 /**
276 * Ensure the texture in the given render buffer is not attached to a
277 * framebuffer (ie, so we can safely use it as a texture during the rendering
278 * cycle, or before deletion).
279 */
280 static void gl_fbo_detach_render_buffer( render_buffer_t buffer )
281 {
282 int i,j;
283 for( i=0; i<MAX_FRAMEBUFFERS; i++ ) {
284 if( fbo[i].width == buffer->width && fbo[i].height == buffer->height ) {
285 for( j=0; j<gl_fbo_max_attachments; j++ ) {
286 if( fbo[i].tex_ids[j] == buffer->buf_id ) {
287 glBindFramebuffer(GL_FRAMEBUFFER, fbo[i].fb_id);
288 glFramebufferTexture2D(GL_FRAMEBUFFER, ATTACHMENT_POINT(j),
289 GL_TEXTURE_2D, GL_NONE, 0 );
290 fbo[i].tex_ids[j] = -1;
291 return;
292 }
293 }
294 break;
295 }
296 }
297 }
299 static void gl_fbo_destroy_render_buffer( render_buffer_t buffer )
300 {
301 int i,j;
303 gl_fbo_detach_render_buffer( buffer );
305 if( buffer->buf_id != buffer->tex_id ) {
306 // If tex_id was set at buffer creation, we don't own the texture.
307 // Otherwise, delete it now.
308 GLuint tex = buffer->buf_id;
309 glDeleteTextures( 1, &tex );
310 }
311 buffer->buf_id = 0;
312 buffer->tex_id = 0;
313 free( buffer );
314 }
316 static gboolean gl_fbo_set_render_target( render_buffer_t buffer )
317 {
318 int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
319 gl_fbo_attach_texture( fb, buffer->buf_id );
320 /* setup the gl context */
321 glViewport( 0, 0, buffer->width, buffer->height );
323 return TRUE;
324 }
326 static void gl_fbo_finish_render( render_buffer_t buffer )
327 {
328 glFinish();
329 gl_fbo_detach_render_buffer(buffer);
330 }
332 /**
333 * Render the texture holding the given buffer to the front window
334 * buffer.
335 */
336 static void gl_fbo_display_render_buffer( render_buffer_t buffer )
337 {
338 gl_fbo_detach();
339 gl_display_render_buffer( buffer );
340 if( display_driver->swap_buffers )
341 display_driver->swap_buffers();
342 }
344 static void gl_fbo_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
345 {
346 gl_fbo_detach();
347 gl_load_frame_buffer( frame, buffer->buf_id );
348 }
350 static void gl_fbo_display_blank( uint32_t colour )
351 {
352 gl_fbo_detach();
353 gl_display_blank( colour );
354 if( display_driver->swap_buffers )
355 display_driver->swap_buffers();
356 }
358 void gl_fbo_detach()
359 {
360 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
361 /* Make sure texture attachment is not a current draw/read buffer */
362 #ifdef HAVE_OPENGL_DRAW_BUFFER
363 glDrawBuffer( GL_FRONT );
364 glReadBuffer( GL_FRONT );
365 #endif
366 }
368 static gboolean gl_fbo_read_render_buffer( unsigned char *target, render_buffer_t buffer,
369 int rowstride, int format )
370 {
371 int fb = gl_fbo_get_framebuffer( buffer->width, buffer->height );
372 gl_fbo_attach_texture( fb, buffer->buf_id );
373 return gl_read_render_buffer( target, buffer, rowstride, format );
374 }
376 #else
377 gboolean gl_fbo_is_supported()
378 {
379 return FALSE;
380 }
382 void gl_fbo_init( display_driver_t driver )
383 {
384 }
386 #endif
.