filename | src/drivers/video_glx.c |
changeset | 736:a02d1475ccfd |
prev | 703:585c66692be7 |
next | 805:b355f7b3ff2e |
author | nkeynes |
date | Sun Aug 03 02:55:27 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Check the MAX_COLOR_ATTACHMENTS value rather than assuming it's at least 4, since it may in fact be less. |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Shared functions for all X11-based display drivers.
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
19 #include <string.h>
20 #include "display.h"
21 #include <X11/Xlib.h>
22 #include <GL/glx.h>
23 #include <string.h>
24 #include "pvr2/pvr2.h"
25 #include "pvr2/glutil.h"
26 #include "drivers/video_glx.h"
27 #include "drivers/video_gl.h"
29 /**
30 * General X11 parameters. The front-end driver is expected to set this up
31 * by calling video_glx_init after initializing itself.
32 */
33 Display *video_x11_display = NULL;
34 Window video_x11_window = 0;
36 static int glx_version = 100;
37 static XVisualInfo *glx_visual = NULL;
38 static GLXFBConfig glx_fbconfig;
39 static GLXContext glx_context = NULL;
40 static gboolean glx_is_initialized = FALSE;
41 static gboolean glx_fbconfig_supported = FALSE;
42 static gboolean glx_pbuffer_supported = FALSE;
43 static GLuint glx_pbuffer_texture = 0;
45 /* Prototypes for pbuffer support methods */
46 static void glx_pbuffer_init( display_driver_t driver );
47 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height );
48 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer );
49 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer );
50 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer );
51 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
52 static void glx_pbuffer_display_blank( uint32_t colour );
53 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
55 /**
56 * Test if a specific extension is supported. From opengl.org
57 * @param extension extension name to check for
58 * @return TRUE if supported, otherwise FALSE.
59 */
60 gboolean isServerGLXExtensionSupported( Display *display, int screen,
61 const char *extension )
62 {
63 const char *extensions = NULL;
64 const char *start;
65 char *where, *terminator;
67 /* Extension names should not have spaces. */
68 where = strchr(extension, ' ');
69 if (where || *extension == '\0')
70 return 0;
71 extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
72 start = extensions;
73 for (;;) {
74 where = strstr((const char *) start, extension);
75 if (!where)
76 break;
77 terminator = where + strlen(extension);
78 if (where == start || *(where - 1) == ' ')
79 if (*terminator == ' ' || *terminator == '\0')
80 return TRUE;
81 start = terminator;
82 }
83 return FALSE;
84 }
86 gboolean video_glx_init( Display *display, int screen )
87 {
88 int major, minor;
90 if( glx_is_initialized ) {
91 return TRUE;
92 }
94 Bool result = glXQueryVersion( display, &major, &minor );
95 if( result != False ) {
96 glx_version = (major*100) + minor;
97 }
99 glx_fbconfig_supported = (glx_version >= 103 ||
100 isServerGLXExtensionSupported(display, screen,
101 "GLX_SGIX_fbconfig") );
102 glx_pbuffer_supported = (glx_version >= 103 ||
103 isServerGLXExtensionSupported(display, screen,
104 "GLX_SGIX_pbuffer") );
105 if( glx_fbconfig_supported ) {
106 int nelem;
107 int fb_attribs[] = { GLX_DRAWABLE_TYPE,
108 GLX_PBUFFER_BIT|GLX_WINDOW_BIT,
109 GLX_RENDER_TYPE, GLX_RGBA_BIT,
110 GLX_DEPTH_SIZE, 24, 0 };
111 GLXFBConfig *configs = glXChooseFBConfig( display, screen,
112 fb_attribs, &nelem );
114 if( configs == NULL || nelem == 0 ) {
115 /* Try a 16-bit depth buffer and see if it helps */
116 fb_attribs[5] = 16;
117 configs = glXChooseFBConfig( display, screen, fb_attribs, &nelem );
118 if( nelem > 0 ) {
119 WARN( "Using a 16-bit depth buffer - expect video glitches" );
120 }
122 }
123 if( configs == NULL || nelem == 0 ) {
124 /* Still didn't work. Fallback to 1.2 methods */
125 glx_fbconfig_supported = FALSE;
126 glx_pbuffer_supported = FALSE;
127 } else {
128 glx_fbconfig = configs[0];
129 glx_visual = glXGetVisualFromFBConfig(display, glx_fbconfig);
130 XFree(configs);
131 }
132 }
134 if( !glx_fbconfig_supported ) {
135 int attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, 0 };
136 glx_visual = glXChooseVisual( display, screen, attribs );
137 if( glx_visual == NULL ) {
138 /* Try the 16-bit fallback here too */
139 attribs[2] = 16;
140 glx_visual = glXChooseVisual( display, screen, attribs );
141 if( glx_visual != NULL ) {
142 WARN( "Using a 16-bit depth buffer - expect video glitches" );
143 }
144 }
145 }
147 if( glx_visual == NULL ) {
148 return FALSE;
149 }
151 glx_is_initialized = TRUE;
152 return TRUE;
153 }
155 gboolean video_glx_init_context( Display *display, Window window )
156 {
157 if( glx_fbconfig_supported ) {
158 glx_context = glXCreateNewContext( display, glx_fbconfig,
159 GLX_RGBA_TYPE, NULL, True );
160 if( glx_context == NULL ) {
161 ERROR( "Unable to create a GLX Context.");
162 return FALSE;
163 }
165 if( glXMakeContextCurrent( display, window, window,
166 glx_context ) == False ) {
167 ERROR( "Unable to prepare GLX context for drawing" );
168 glXDestroyContext( display, glx_context );
169 return FALSE;
170 }
171 } else {
172 glx_context = glXCreateContext( display, glx_visual, None, True );
173 if( glx_context == NULL ) {
174 ERROR( "Unable to create a GLX Context.");
175 return FALSE;
176 }
178 if( glXMakeCurrent( display, window, glx_context ) == False ) {
179 ERROR( "Unable to prepare GLX context for drawing" );
180 glXDestroyContext( display, glx_context );
181 return FALSE;
182 }
183 }
185 if( !glXIsDirect(display, glx_context) ) {
186 WARN( "Not using direct rendering - this is likely to be slow" );
187 }
189 video_x11_display = display;
190 video_x11_window = window;
192 return TRUE;
193 }
195 gboolean video_glx_init_driver( display_driver_t driver )
196 {
197 if( gl_fbo_is_supported() ) { // First preference
198 gl_fbo_init(driver);
199 } else if( glx_pbuffer_supported ) {
200 glx_pbuffer_init(driver);
201 } else {
202 ERROR( "Unable to create render buffers (requires either EXT_framebuffer_object or GLX 1.3+)" );
203 video_glx_shutdown();
204 return FALSE;
205 }
206 return TRUE;
207 }
210 void video_glx_shutdown()
211 {
212 // texcache_gl_shutdown();
213 glx_is_initialized = FALSE;
214 if( glx_context != NULL ) {
215 glXDestroyContext( video_x11_display, glx_context );
216 glx_context = NULL;
217 }
218 if( glx_visual != NULL ) {
219 XFree(glx_visual);
220 glx_visual = NULL;
221 }
222 }
225 XVisualInfo *video_glx_get_visual()
226 {
227 return glx_visual;
228 }
231 int video_glx_load_font( const gchar *font_name )
232 {
233 int lists;
234 XFontStruct *font = XLoadQueryFont(video_x11_display, font_name );
235 if (font == NULL)
236 return -1;
238 lists = glGenLists(96);
239 glXUseXFont(font->fid, 32, 96, lists);
240 XFreeFont(video_x11_display, font);
241 return lists;
242 }
245 void video_glx_swap_buffers( void )
246 {
247 glXSwapBuffers( video_x11_display, video_x11_window );
248 }
250 void video_glx_make_window_current( void )
251 {
252 glXMakeCurrent( video_x11_display, video_x11_window, glx_context );
253 }
256 // Pbuffer support
258 /**
259 * Construct the initial frame buffers and allocate ids for everything.
260 * The render handling driver methods are set to the fbo versions.
261 */
262 static void glx_pbuffer_init( display_driver_t driver )
263 {
264 glGenTextures( 1, &glx_pbuffer_texture );
265 driver->create_render_buffer = glx_pbuffer_create_render_buffer;
266 driver->destroy_render_buffer = glx_pbuffer_destroy_render_buffer;
267 driver->set_render_target = glx_pbuffer_set_render_target;
268 driver->display_render_buffer = glx_pbuffer_display_render_buffer;
269 driver->load_frame_buffer = glx_pbuffer_load_frame_buffer;
270 driver->display_blank = glx_pbuffer_display_blank;
271 driver->read_render_buffer = glx_pbuffer_read_render_buffer;
272 }
274 void glx_pbuffer_shutdown()
275 {
276 glDeleteTextures( 1, &glx_pbuffer_texture );
277 }
279 static render_buffer_t glx_pbuffer_create_render_buffer( uint32_t width, uint32_t height )
280 {
281 int attribs[] = { GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height,
282 GLX_PRESERVED_CONTENTS, True, 0 };
283 GLXPbuffer pb = glXCreatePbuffer( video_x11_display, glx_fbconfig, attribs );
284 if( pb == (GLXPbuffer)NULL ) {
285 ERROR( "Unable to create pbuffer" );
286 return NULL;
287 }
288 render_buffer_t buffer = calloc( sizeof(struct render_buffer), 1 );
289 buffer->width = width;
290 buffer->height = height;
291 buffer->buf_id = pb;
292 return buffer;
293 }
295 static void glx_pbuffer_destroy_render_buffer( render_buffer_t buffer )
296 {
297 glXDestroyPbuffer( video_x11_display, (GLXPbuffer)buffer->buf_id );
298 buffer->buf_id = 0;
299 free( buffer );
300 }
302 static gboolean glx_pbuffer_set_render_target( render_buffer_t buffer )
303 {
304 glFinish();
305 if( glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context ) == False ) {
306 ERROR( "Make context current (pbuffer) failed!" );
307 }
308 /* setup the gl context */
309 glViewport( 0, 0, buffer->width, buffer->height );
310 glDrawBuffer(GL_FRONT);
312 return TRUE;
313 }
315 /**
316 * Render the texture holding the given buffer to the front window
317 * buffer.
318 */
319 static void glx_pbuffer_display_render_buffer( render_buffer_t buffer )
320 {
321 glFinish();
322 glReadBuffer( GL_FRONT );
323 glDrawBuffer( GL_FRONT );
324 glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
325 glBindTexture( GL_TEXTURE_RECTANGLE_ARB, glx_pbuffer_texture );
326 glCopyTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, 0, 0, buffer->width, buffer->height, 0 );
327 video_glx_make_window_current();
328 gl_texture_window( buffer->width, buffer->height, glx_pbuffer_texture, buffer->inverted );
329 }
331 static void glx_pbuffer_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
332 {
333 glFinish();
334 glXMakeContextCurrent( video_x11_display, (GLXPbuffer)buffer->buf_id, (GLXPbuffer)buffer->buf_id, glx_context );
335 GLenum type = colour_formats[frame->colour_format].type;
336 GLenum format = colour_formats[frame->colour_format].format;
337 int bpp = colour_formats[frame->colour_format].bpp;
338 int rowstride = (frame->rowstride / bpp) - frame->width;
340 gl_reset_state();
341 glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
342 glRasterPos2f(0.375, frame->height-0.375);
343 glPixelZoom( 1.0, 1.0 );
344 glDrawPixels( frame->width, frame->height, format, type, frame->data );
345 glFlush();
346 }
348 static void glx_pbuffer_display_blank( uint32_t colour )
349 {
350 glFinish();
351 video_glx_make_window_current();
352 gl_display_blank( colour );
353 }
355 static gboolean glx_pbuffer_read_render_buffer( unsigned char *target, render_buffer_t buffer,
356 int rowstride, int format )
357 {
358 glXMakeCurrent( video_x11_display, (GLXDrawable)buffer->buf_id, glx_context );
359 glReadBuffer( GL_FRONT );
360 return gl_read_render_buffer( target, buffer, rowstride, format );
361 }
.