wgl: WGL_COLOR_BITS_ARB does include alpha.
[wine] / dlls / winex11.drv / opengl.c
1 /*
2  * X11DRV OpenGL functions
3  *
4  * Copyright 2000 Lionel Ulmer
5  * Copyright 2005 Alex Woods
6  * Copyright 2005 Raphael Junqueira
7  * Copyright 2006 Roderick Colenbrander
8  * Copyright 2006 Tomas Carnecky
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "x11drv.h"
33 #include "winternl.h"
34 #include "wine/library.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
38 WINE_DECLARE_DEBUG_CHANNEL(opengl);
39
40 #ifdef SONAME_LIBGL
41
42 #undef APIENTRY
43 #undef CALLBACK
44 #undef WINAPI
45
46 #ifdef HAVE_GL_GL_H
47 # include <GL/gl.h>
48 #endif
49 #ifdef HAVE_GL_GLX_H
50 # include <GL/glx.h>
51 #endif
52 #ifdef HAVE_GL_GLEXT_H
53 # include <GL/glext.h>
54 #endif
55
56 #include "wine/wgl.h"
57
58 #undef APIENTRY
59 #undef CALLBACK
60 #undef WINAPI
61
62 /* Redefines the constants */
63 #define CALLBACK    __stdcall
64 #define WINAPI      __stdcall
65 #define APIENTRY    WINAPI
66
67
68 WINE_DECLARE_DEBUG_CHANNEL(fps);
69
70 typedef struct wine_glextension {
71     const char *extName;
72     struct {
73         const char *funcName;
74         void *funcAddress;
75     } extEntryPoints[8];
76 } WineGLExtension;
77
78 struct WineGLInfo {
79     const char *glVersion;
80     const char *glExtensions;
81
82     int glxVersion[2];
83
84     const char *glxServerVersion;
85     const char *glxServerExtensions;
86
87     const char *glxClientVersion;
88     const char *glxClientExtensions;
89
90     const char *glxExtensions;
91
92     BOOL glxDirect;
93     char wglExtensions[4096];
94 };
95
96 typedef struct wine_glpixelformat {
97     int         iPixelFormat;
98     GLXFBConfig fbconfig;
99     int         fmt_id;
100     int         render_type;
101     BOOL        offscreenOnly;
102 } WineGLPixelFormat;
103
104 typedef struct wine_glcontext {
105     HDC hdc;
106     XVisualInfo *vis;
107     WineGLPixelFormat *fmt;
108     GLXContext ctx;
109     BOOL do_escape;
110     X11DRV_PDEVICE *physDev;
111     RECT viewport;
112     RECT scissor;
113     BOOL scissor_enabled;
114     struct wine_glcontext *next;
115     struct wine_glcontext *prev;
116 } Wine_GLContext;
117
118 typedef struct wine_glpbuffer {
119     Drawable   drawable;
120     Display*   display;
121     WineGLPixelFormat* fmt;
122     int        width;
123     int        height;
124     int*       attribList;
125     HDC        hdc;
126
127     int        use_render_texture; /* This is also the internal texture format */
128     int        texture_bind_target;
129     int        texture_bpp;
130     GLint      texture_format;
131     GLuint     texture_target;
132     GLenum     texture_type;
133     GLuint     texture;
134     int        texture_level;
135 } Wine_GLPBuffer;
136
137 static Wine_GLContext *context_list;
138 static struct WineGLInfo WineGLInfo = { 0 };
139 static int use_render_texture_emulation = 1;
140 static int use_render_texture_ati = 0;
141 static int swap_interval = 1;
142
143 #define MAX_EXTENSIONS 16
144 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
145 static int WineGLExtensionListSize;
146
147 static WineGLPixelFormat *WineGLPixelFormatList;
148 static int WineGLPixelFormatListSize = 0;
149
150 static void X11DRV_WineGL_LoadExtensions(void);
151
152 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
153   TRACE("  - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
154   TRACE("  - dwFlags : ");
155 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
156   TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
157   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
158   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
159   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
160   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
161   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
162   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
163   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
164   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
165   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
166   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
167   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
168   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
169   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
170   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
171   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
172 #undef TEST_AND_DUMP
173   TRACE("\n");
174
175   TRACE("  - iPixelType : ");
176   switch (ppfd->iPixelType) {
177   case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
178   case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
179   }
180   TRACE("\n");
181
182   TRACE("  - Color   : %d\n", ppfd->cColorBits);
183   TRACE("  - Red     : %d\n", ppfd->cRedBits);
184   TRACE("  - Green   : %d\n", ppfd->cGreenBits);
185   TRACE("  - Blue    : %d\n", ppfd->cBlueBits);
186   TRACE("  - Alpha   : %d\n", ppfd->cAlphaBits);
187   TRACE("  - Accum   : %d\n", ppfd->cAccumBits);
188   TRACE("  - Depth   : %d\n", ppfd->cDepthBits);
189   TRACE("  - Stencil : %d\n", ppfd->cStencilBits);
190   TRACE("  - Aux     : %d\n", ppfd->cAuxBuffers);
191
192   TRACE("  - iLayerType : ");
193   switch (ppfd->iLayerType) {
194   case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
195   case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
196   case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
197   }
198   TRACE("\n");
199 }
200
201 #define PUSH1(attribs,att)        do { attribs[nAttribs++] = (att); } while (0)
202 #define PUSH2(attribs,att,value)  do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
203
204 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
205 /* GLX 1.0 */
206 MAKE_FUNCPTR(glXChooseVisual)
207 MAKE_FUNCPTR(glXCreateContext)
208 MAKE_FUNCPTR(glXCreateGLXPixmap)
209 MAKE_FUNCPTR(glXGetCurrentContext)
210 MAKE_FUNCPTR(glXGetCurrentDrawable)
211 MAKE_FUNCPTR(glXDestroyContext)
212 MAKE_FUNCPTR(glXDestroyGLXPixmap)
213 MAKE_FUNCPTR(glXGetConfig)
214 MAKE_FUNCPTR(glXIsDirect)
215 MAKE_FUNCPTR(glXMakeCurrent)
216 MAKE_FUNCPTR(glXSwapBuffers)
217 MAKE_FUNCPTR(glXQueryExtension)
218 MAKE_FUNCPTR(glXQueryVersion)
219 MAKE_FUNCPTR(glXUseXFont)
220
221 /* GLX 1.1 */
222 MAKE_FUNCPTR(glXGetClientString)
223 MAKE_FUNCPTR(glXQueryExtensionsString)
224 MAKE_FUNCPTR(glXQueryServerString)
225
226 /* GLX 1.3 */
227 MAKE_FUNCPTR(glXGetFBConfigs)
228 MAKE_FUNCPTR(glXChooseFBConfig)
229 MAKE_FUNCPTR(glXCreatePbuffer)
230 MAKE_FUNCPTR(glXCreateNewContext)
231 MAKE_FUNCPTR(glXDestroyPbuffer)
232 MAKE_FUNCPTR(glXGetFBConfigAttrib)
233 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
234 MAKE_FUNCPTR(glXMakeContextCurrent)
235 MAKE_FUNCPTR(glXQueryDrawable)
236 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
237
238 /* GLX Extensions */
239 static void* (*pglXGetProcAddressARB)(const GLubyte *);
240 static BOOL  (*pglXBindTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
241 static BOOL  (*pglXReleaseTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
242 static BOOL  (*pglXDrawableAttribARB)(Display *dpy, GLXDrawable draw, const int *attribList);
243 static int   (*pglXSwapIntervalSGI)(int);
244
245 /* NV GLX Extension */
246 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
247 static void  (*pglXFreeMemoryNV)(GLvoid *pointer);
248
249 /* Standard OpenGL */
250 MAKE_FUNCPTR(glBindTexture)
251 MAKE_FUNCPTR(glBitmap)
252 MAKE_FUNCPTR(glCopyTexSubImage1D)
253 MAKE_FUNCPTR(glCopyTexImage2D)
254 MAKE_FUNCPTR(glCopyTexSubImage2D)
255 MAKE_FUNCPTR(glDisable)
256 MAKE_FUNCPTR(glDrawBuffer)
257 MAKE_FUNCPTR(glEnable)
258 MAKE_FUNCPTR(glEndList)
259 MAKE_FUNCPTR(glGetError)
260 MAKE_FUNCPTR(glGetIntegerv)
261 MAKE_FUNCPTR(glGetString)
262 MAKE_FUNCPTR(glIsEnabled)
263 MAKE_FUNCPTR(glNewList)
264 MAKE_FUNCPTR(glPixelStorei)
265 MAKE_FUNCPTR(glReadPixels)
266 MAKE_FUNCPTR(glScissor)
267 MAKE_FUNCPTR(glTexImage2D)
268 MAKE_FUNCPTR(glViewport)
269 #undef MAKE_FUNCPTR
270
271 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
272 {
273     static BOOL infoInitialized = FALSE;
274
275     int screen = DefaultScreen(gdi_display);
276     Window win = RootWindow(gdi_display, screen);
277     Visual *visual;
278     XVisualInfo template;
279     XVisualInfo *vis;
280     int num;
281     GLXContext ctx = NULL;
282
283     if (infoInitialized)
284         return TRUE;
285     infoInitialized = TRUE;
286
287     wine_tsx11_lock();
288
289     visual = DefaultVisual(gdi_display, screen);
290     template.visualid = XVisualIDFromVisual(visual);
291     vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
292     if (vis) {
293         WORD old_fs = wine_get_fs();
294         /* Create a GLX Context. Without one we can't query GL information */
295         ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
296         if (wine_get_fs() != old_fs)
297         {
298             wine_set_fs( old_fs );
299             wine_tsx11_unlock();
300             ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
301             ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
302             return FALSE;
303         }
304     }
305
306     if (ctx) {
307         pglXMakeCurrent(gdi_display, win, ctx);
308     } else {
309         ERR(" couldn't initialize OpenGL, expect problems\n");
310         wine_tsx11_unlock();
311         return FALSE;
312     }
313
314     WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
315     WineGLInfo.glExtensions = strdup((const char *) pglGetString(GL_EXTENSIONS));
316
317     /* Get the common GLX version supported by GLX client and server ( major/minor) */
318     pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
319
320     WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
321     WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
322
323     WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
324     WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
325
326     WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
327     WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
328
329     TRACE("GL version             : %s.\n", WineGLInfo.glVersion);
330     TRACE("GL renderer            : %s.\n", pglGetString(GL_RENDERER));
331     TRACE("GLX version            : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
332     TRACE("Server GLX version     : %s.\n", WineGLInfo.glxServerVersion);
333     TRACE("Server GLX vendor:     : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VENDOR));
334     TRACE("Client GLX version     : %s.\n", WineGLInfo.glxClientVersion);
335     TRACE("Client GLX vendor:     : %s.\n", pglXGetClientString(gdi_display, GLX_VENDOR));
336     TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
337
338     if(vis) XFree(vis);
339     if(ctx) {
340         pglXMakeCurrent(gdi_display, None, NULL);    
341         pglXDestroyContext(gdi_display, ctx);
342     }
343     wine_tsx11_unlock();
344     return TRUE;
345 }
346
347 static BOOL has_opengl(void)
348 {
349     static int init_done;
350     static void *opengl_handle;
351     const char *glx_extensions;
352
353     int error_base, event_base;
354
355     if (init_done) return (opengl_handle != NULL);
356     init_done = 1;
357
358     /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
359        and include all dependencies */
360     opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
361     if (opengl_handle == NULL) return FALSE;
362
363     pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
364     if (pglXGetProcAddressARB == NULL) {
365         ERR("could not find glXGetProcAddressARB in libGL.\n");
366         return FALSE;
367     }
368
369 #define LOAD_FUNCPTR(f) if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) goto sym_not_found;
370 /* GLX 1.0 */
371 LOAD_FUNCPTR(glXChooseVisual)
372 LOAD_FUNCPTR(glXCreateContext)
373 LOAD_FUNCPTR(glXCreateGLXPixmap)
374 LOAD_FUNCPTR(glXGetCurrentContext)
375 LOAD_FUNCPTR(glXGetCurrentDrawable)
376 LOAD_FUNCPTR(glXDestroyContext)
377 LOAD_FUNCPTR(glXDestroyGLXPixmap)
378 LOAD_FUNCPTR(glXGetConfig)
379 LOAD_FUNCPTR(glXIsDirect)
380 LOAD_FUNCPTR(glXMakeCurrent)
381 LOAD_FUNCPTR(glXSwapBuffers)
382 LOAD_FUNCPTR(glXQueryExtension)
383 LOAD_FUNCPTR(glXQueryVersion)
384 LOAD_FUNCPTR(glXUseXFont)
385
386 /* GLX 1.1 */
387 LOAD_FUNCPTR(glXGetClientString)
388 LOAD_FUNCPTR(glXQueryExtensionsString)
389 LOAD_FUNCPTR(glXQueryServerString)
390
391 /* GLX 1.3 */
392 LOAD_FUNCPTR(glXCreatePbuffer)
393 LOAD_FUNCPTR(glXCreateNewContext)
394 LOAD_FUNCPTR(glXDestroyPbuffer)
395 LOAD_FUNCPTR(glXMakeContextCurrent)
396 LOAD_FUNCPTR(glXGetCurrentReadDrawable)
397 LOAD_FUNCPTR(glXGetFBConfigs)
398
399 /* Standard OpenGL calls */
400 LOAD_FUNCPTR(glBindTexture)
401 LOAD_FUNCPTR(glBitmap)
402 LOAD_FUNCPTR(glCopyTexSubImage1D)
403 LOAD_FUNCPTR(glCopyTexImage2D)
404 LOAD_FUNCPTR(glCopyTexSubImage2D)
405 LOAD_FUNCPTR(glDisable)
406 LOAD_FUNCPTR(glDrawBuffer)
407 LOAD_FUNCPTR(glEnable)
408 LOAD_FUNCPTR(glEndList)
409 LOAD_FUNCPTR(glGetError)
410 LOAD_FUNCPTR(glGetIntegerv)
411 LOAD_FUNCPTR(glGetString)
412 LOAD_FUNCPTR(glIsEnabled)
413 LOAD_FUNCPTR(glNewList)
414 LOAD_FUNCPTR(glPixelStorei)
415 LOAD_FUNCPTR(glReadPixels)
416 LOAD_FUNCPTR(glScissor)
417 LOAD_FUNCPTR(glTexImage2D)
418 LOAD_FUNCPTR(glViewport)
419 #undef LOAD_FUNCPTR
420
421 /* It doesn't matter if these fail. They'll only be used if the driver reports
422    the associated extension is available (and if a driver reports the extension
423    is available but fails to provide the functions, it's quite broken) */
424 #define LOAD_FUNCPTR(f) p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f);
425 /* NV GLX Extension */
426 LOAD_FUNCPTR(glXAllocateMemoryNV)
427 LOAD_FUNCPTR(glXFreeMemoryNV)
428 #undef LOAD_FUNCPTR
429
430     if(!X11DRV_WineGL_InitOpenglInfo()) {
431         wine_dlclose(opengl_handle, NULL, 0);
432         opengl_handle = NULL;
433         return FALSE;
434     }
435
436     wine_tsx11_lock();
437     if (pglXQueryExtension(gdi_display, &error_base, &event_base) == True) {
438         TRACE("GLX is up and running error_base = %d\n", error_base);
439     } else {
440         wine_dlclose(opengl_handle, NULL, 0);
441         opengl_handle = NULL;
442     }
443
444     /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
445      * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
446      * rendering is used.
447      *
448      * The main problem for our OpenGL code is that we need certain GLX calls but their presence
449      * depends on the reported GLX client / server version and on the client / server extension list.
450      * Those don't have to be the same.
451      *
452      * In general the server GLX information should be used in case of indirect rendering. When direct
453      * rendering is used, the OpenGL client library is responsible for which GLX calls are available.
454      * Nvidia's OpenGL drivers are the best in terms of GLX features. At the moment of writing their
455      * 8762 drivers support 1.3 for the server and 1.4 for the client and they support lots of extensions.
456      * Unfortunately it is much more complicated for Mesa/DRI-based drivers and ATI's drivers.
457      * Both sets of drivers report a server version of 1.2 and the client version can be 1.3 or 1.4.
458      * Further, in case of at least ATI's drivers, one crucial extension needed for our pixel format code
459      * is only available in the list of server extensions and not in the client list.
460      *
461      * The versioning checks below try to take into account the comments from above.
462      */
463
464     /* Depending on the use of direct or indirect rendering we need either the list of extensions
465      * exported by the client or by the server.
466      */
467     if(WineGLInfo.glxDirect)
468         glx_extensions = WineGLInfo.glxClientExtensions;
469     else
470         glx_extensions = WineGLInfo.glxServerExtensions;
471
472     /* Based on the default opengl context we decide whether direct or indirect rendering is used.
473      * In case of indirect rendering we check if the GLX version of the server is 1.2 and else
474      * the client version is checked.
475      */
476     if ((!WineGLInfo.glxDirect && !strcmp("1.2", WineGLInfo.glxServerVersion)) ||
477         (WineGLInfo.glxDirect && !strcmp("1.2", WineGLInfo.glxClientVersion)))
478     {
479         if (NULL != strstr(glx_extensions, "GLX_SGIX_fbconfig")) {
480             pglXChooseFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
481             pglXGetFBConfigAttrib = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
482             pglXGetVisualFromFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
483         } else {
484             ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxClientVersion);
485         }
486     } else {
487         pglXChooseFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
488         pglXGetFBConfigAttrib = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
489         pglXGetVisualFromFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
490     }
491
492     /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
493      * enable this function when the Xserver understand GLX 1.3 or newer
494      */
495     if (!strcmp("1.2", WineGLInfo.glxServerVersion))
496         pglXQueryDrawable = NULL;
497     else
498         pglXQueryDrawable = wine_dlsym(RTLD_DEFAULT, "glXQueryDrawable", NULL, 0);
499
500     if (NULL != strstr(glx_extensions, "GLX_ATI_render_texture")) {
501         pglXBindTexImageARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageARB");
502         pglXReleaseTexImageARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageARB");
503         pglXDrawableAttribARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribARB");
504     }
505
506     X11DRV_WineGL_LoadExtensions();
507
508     wine_tsx11_unlock();
509     return (opengl_handle != NULL);
510
511 sym_not_found:
512     wine_dlclose(opengl_handle, NULL, 0);
513     opengl_handle = NULL;
514     return FALSE;
515 }
516
517 static inline Wine_GLContext *alloc_context(void)
518 {
519     Wine_GLContext *ret;
520
521     if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
522     {
523         ret->next = context_list;
524         if (context_list) context_list->prev = ret;
525         context_list = ret;
526     }
527     return ret;
528 }
529
530 static inline void free_context(Wine_GLContext *context)
531 {
532     if (context->next != NULL) context->next->prev = context->prev;
533     if (context->prev != NULL) context->prev->next = context->next;
534     else context_list = context->next;
535
536     HeapFree(GetProcessHeap(), 0, context);
537 }
538
539 static inline Wine_GLContext *get_context_from_GLXContext(GLXContext ctx)
540 {
541     Wine_GLContext *ret;
542     if (!ctx) return NULL;
543     for (ret = context_list; ret; ret = ret->next) if (ctx == ret->ctx) break;
544     return ret;
545 }
546
547 /**
548  * get_hdc_from_Drawable (internal)
549  *
550  * For use by wglGetCurrentReadDCARB.
551  */
552 static inline HDC get_hdc_from_Drawable(GLXDrawable d)
553 {
554     Wine_GLContext *ret;
555     for (ret = context_list; ret; ret = ret->next) {
556         if (d == ret->physDev->drawable) {
557             return ret->hdc;
558         }
559     }
560     return NULL;
561 }
562
563 static inline BOOL is_valid_context( Wine_GLContext *ctx )
564 {
565     Wine_GLContext *ptr;
566     for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
567     return (ptr != NULL);
568 }
569
570 static int describeContext(Wine_GLContext* ctx) {
571     int tmp;
572     int ctx_vis_id;
573     TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
574     pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
575     TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
576     pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
577     TRACE(" - VISUAL_ID 0x%x\n", tmp);
578     ctx_vis_id = tmp;
579     return ctx_vis_id;
580 }
581
582 static int describeDrawable(Wine_GLContext* ctx, Drawable drawable) {
583     int tmp;
584     int nElements;
585     int attribList[3] = { GLX_FBCONFIG_ID, 0, None };
586     GLXFBConfig *fbCfgs;
587
588     if (pglXQueryDrawable == NULL)  {
589         /** glXQueryDrawable not available so returns not supported */
590         return -1;
591     }
592
593     TRACE(" Drawable %p have :\n", (void*) drawable);
594     pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &tmp);
595     TRACE(" - WIDTH as %d\n", tmp);
596     pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &tmp);
597     TRACE(" - HEIGHT as %d\n", tmp);
598     pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &tmp);
599     TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
600
601     attribList[1] = tmp;
602     fbCfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribList, &nElements);
603     if (fbCfgs == NULL) {
604         return -1;
605     }
606
607     pglXGetFBConfigAttrib(gdi_display, fbCfgs[0], GLX_VISUAL_ID, &tmp);
608     TRACE(" - VISUAL_ID as 0x%x\n", tmp);
609
610     XFree(fbCfgs);
611
612     return tmp;
613 }
614
615 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
616   int nAttribs = 0;
617   unsigned cur = 0; 
618   int pop;
619   int drawattrib = 0;
620   int nvfloatattrib = GLX_DONT_CARE;
621   int pixelattrib = 0;
622
623   /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
624    * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
625   while (iWGLAttr && 0 != iWGLAttr[cur]) {
626     TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
627
628     switch (iWGLAttr[cur]) {
629     case WGL_COLOR_BITS_ARB:
630       pop = iWGLAttr[++cur];
631       PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
632       TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
633       break;
634     case WGL_BLUE_BITS_ARB:
635       pop = iWGLAttr[++cur];
636       PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
637       TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
638       break;
639     case WGL_RED_BITS_ARB:
640       pop = iWGLAttr[++cur];
641       PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
642       TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
643       break;
644     case WGL_GREEN_BITS_ARB:
645       pop = iWGLAttr[++cur];
646       PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
647       TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
648       break;
649     case WGL_ALPHA_BITS_ARB:
650       pop = iWGLAttr[++cur];
651       PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
652       TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
653       break;
654     case WGL_DEPTH_BITS_ARB:
655       pop = iWGLAttr[++cur];
656       PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
657       TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
658       break;
659     case WGL_STENCIL_BITS_ARB:
660       pop = iWGLAttr[++cur];
661       PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
662       TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
663       break;
664     case WGL_DOUBLE_BUFFER_ARB:
665       pop = iWGLAttr[++cur];
666       PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
667       TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
668       break;
669
670     case WGL_PIXEL_TYPE_ARB:
671       pop = iWGLAttr[++cur];
672       TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
673       switch (pop) {
674       case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
675       case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
676       /* This is the same as WGL_TYPE_RGBA_FLOAT_ATI but the GLX constants differ, only the ARB GLX one is widely supported so use that */
677       case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
678       default:
679         ERR("unexpected PixelType(%x)\n", pop); 
680         pop = 0;
681       }
682       break;
683
684     case WGL_SUPPORT_GDI_ARB:
685       pop = iWGLAttr[++cur];
686       PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
687       TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
688       break;
689
690     case WGL_DRAW_TO_BITMAP_ARB:
691       pop = iWGLAttr[++cur];
692       TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
693       /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
694       if (pop) {
695         drawattrib |= GLX_PIXMAP_BIT;
696       }
697       break;
698
699     case WGL_DRAW_TO_WINDOW_ARB:
700       pop = iWGLAttr[++cur];
701       TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
702       /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
703       if (pop) {
704         drawattrib |= GLX_WINDOW_BIT;
705       }
706       break;
707
708     case WGL_DRAW_TO_PBUFFER_ARB:
709       pop = iWGLAttr[++cur];
710       TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
711       /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
712       if (pop) {
713         drawattrib |= GLX_PBUFFER_BIT;
714       }
715       break;
716
717     case WGL_ACCELERATION_ARB:
718     case WGL_SUPPORT_OPENGL_ARB:
719       pop = iWGLAttr[++cur];
720       /** nothing to do, if we are here, supposing support Accelerated OpenGL */
721       TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
722       break;
723
724     case WGL_PBUFFER_LARGEST_ARB:
725       pop = iWGLAttr[++cur];
726       PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
727       TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
728       break;
729
730     case WGL_SAMPLE_BUFFERS_ARB:
731       pop = iWGLAttr[++cur];
732       PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
733       TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
734       break;
735
736     case WGL_SAMPLES_ARB:
737       pop = iWGLAttr[++cur];
738       PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
739       TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
740       break;
741
742     case WGL_TEXTURE_FORMAT_ARB:
743     case WGL_TEXTURE_TARGET_ARB:
744     case WGL_MIPMAP_TEXTURE_ARB:
745       TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
746       pop = iWGLAttr[++cur];
747       if (NULL == pbuf) {
748         ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
749       }
750       if (use_render_texture_ati) {
751         /** nothing to do here */
752       }
753       else if (!use_render_texture_emulation) {
754         if (WGL_NO_TEXTURE_ARB != pop) {
755           ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
756           return -1; /** error: don't support it */
757         } else {
758           PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
759           drawattrib |= GLX_PBUFFER_BIT;
760         }
761       }
762       break ;
763     case WGL_FLOAT_COMPONENTS_NV:
764       nvfloatattrib = iWGLAttr[++cur];
765       TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
766       break ;
767     case WGL_BIND_TO_TEXTURE_RGB_ARB:
768     case WGL_BIND_TO_TEXTURE_RGBA_ARB:
769     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
770     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
771     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
772     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
773       pop = iWGLAttr[++cur];
774       /** cannot be converted, see direct handling on 
775        *   - wglGetPixelFormatAttribivARB
776        *  TODO: wglChoosePixelFormat
777        */
778       break ;
779
780     default:
781       FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
782       break;
783     }
784     ++cur;
785   }
786
787   /* Apply the OR'd drawable type bitmask now EVEN when WGL_DRAW_TO* is unset.
788    * It is needed in all cases because GLX_DRAWABLE_TYPE default to GLX_WINDOW_BIT. */
789   PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
790   TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
791
792   /* Set GLX_RENDER_TYPE all the time */
793   PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
794   TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
795
796   /* Set GLX_FLOAT_COMPONENTS_NV all the time */
797   if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
798     PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
799     TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
800   }
801
802   return nAttribs;
803 }
804
805 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
806 {
807     int render_type=0, render_type_bit;
808     pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
809     switch(render_type_bit)
810     {
811         case GLX_RGBA_BIT:
812             render_type = GLX_RGBA_TYPE;
813             break;
814         case GLX_COLOR_INDEX_BIT:
815             render_type = GLX_COLOR_INDEX_TYPE;
816             break;
817         case GLX_RGBA_FLOAT_BIT:
818             render_type = GLX_RGBA_FLOAT_TYPE;
819             break;
820         default:
821             ERR("Unknown render_type: %x\n", render_type);
822     }
823     return render_type;
824 }
825
826 static BOOL get_fbconfig_from_visualid(Display *display, Visual *visual, GLXFBConfig *fbconfig, int *fmt_id)
827 {
828     GLXFBConfig* cfgs = NULL;
829     int i;
830     int nCfgs;
831     int tmp_fmt_id;
832     int tmp_vis_id;
833     VisualID visualid;
834
835     if(!display || !visual) {
836         ERR("Invalid display or visual\n");
837         return FALSE;
838     }
839     visualid = XVisualIDFromVisual(visual);
840
841     /* Get a list of all available framebuffer configurations */
842     cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
843     if (NULL == cfgs || 0 == nCfgs) {
844         ERR("glXChooseFBConfig returns NULL\n");
845         if(cfgs != NULL) XFree(cfgs);
846         return FALSE;
847     }
848
849     /* Find the requested offscreen format and count the number of offscreen formats */
850     for(i=0; i<nCfgs; i++) {
851         pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
852         pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &tmp_fmt_id);
853
854         /* We are looking up the GLX index of our main visual and have found it :) */
855         if(visualid == tmp_vis_id) {
856             TRACE("Found FBCONFIG_ID 0x%x at index %d for VISUAL_ID 0x%x\n", tmp_fmt_id, i, tmp_vis_id);
857             *fbconfig = cfgs[i];
858             *fmt_id = tmp_fmt_id;
859             XFree(cfgs);
860             return TRUE;
861         }
862     }
863
864     ERR("No fbconfig found for Wine's main visual (0x%lx), expect problems!\n", visualid);
865     XFree(cfgs);
866     return FALSE;
867 }
868
869 static BOOL init_formats(Display *display, int screen, Visual *visual)
870 {
871     int fmt_id, tmp_vis_id, tmp_fmt_id, nCfgs, i;
872     GLXFBConfig* cfgs;
873     GLXFBConfig fbconfig;
874     int nOffscreenFormats = 0;
875
876     /* Locate the fbconfig correspondig to our main visual */
877     if(!get_fbconfig_from_visualid(display, visual, &fbconfig, &fmt_id)) {
878         ERR("Can't get the FBCONFIG_ID for the main visual, expect problems!\n");
879         return FALSE;
880     }
881
882     /* As mentioned in various parts of the code only the format of the main visual can be used for onscreen rendering.
883     * Next to this format there are also so called offscreen rendering formats (used for pbuffers) which can be supported
884     * because they don't need a visual. Below we use glXGetFBConfigs instead of glXChooseFBConfig to enumerate the fb configurations
885     * because this call lists both types of formats instead of only onscreen ones. */
886     cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
887     if (NULL == cfgs || 0 == nCfgs) {
888         WARN("glXChooseFBConfig returns NULL\n");
889         if(cfgs != NULL) XFree(cfgs);
890
891         /* We only have the single format of Wine's main visual */
892         WineGLPixelFormatList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WineGLPixelFormat));
893         WineGLPixelFormatList[0].iPixelFormat = 1;
894         WineGLPixelFormatList[0].fbconfig = fbconfig;
895         WineGLPixelFormatList[0].fmt_id = fmt_id;
896         WineGLPixelFormatList[0].render_type = get_render_type_from_fbconfig(display, fbconfig);
897         WineGLPixelFormatList[0].offscreenOnly = FALSE;
898         WineGLPixelFormatListSize = 1;
899     }
900     else {
901         /* Count the number of offscreen formats to determine the size for our pixelformat list */
902         for(i=0; i<nCfgs; i++) {
903             pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
904
905             /* We have found an offscreen rendering format :) */
906             if(tmp_vis_id == 0) {
907                 nOffscreenFormats++;
908             }
909         }
910         TRACE("Number of offscreen formats: %d\n", nOffscreenFormats);
911
912         /* Allocate memory for all the offscreen pixelformats and the format of Wine's main visual */
913         WineGLPixelFormatList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (1+nOffscreenFormats)*sizeof(WineGLPixelFormat));
914         WineGLPixelFormatList[0].iPixelFormat = 1;
915         WineGLPixelFormatList[0].fbconfig = fbconfig;
916         WineGLPixelFormatList[0].fmt_id = fmt_id;
917         WineGLPixelFormatList[0].render_type = get_render_type_from_fbconfig(display, fbconfig);
918         WineGLPixelFormatList[0].offscreenOnly = FALSE;
919         WineGLPixelFormatListSize = 1;
920
921         /* Fill the list with offscreen formats */
922         for(i=0; i<nCfgs; i++) {
923             pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
924             pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &tmp_fmt_id);
925
926             /* We have found an offscreen rendering format :) */
927             if(tmp_vis_id == 0) {
928                 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", tmp_fmt_id, WineGLPixelFormatListSize+1, i);
929                 WineGLPixelFormatList[WineGLPixelFormatListSize].iPixelFormat = WineGLPixelFormatListSize+1; /* The index starts at 1 */
930                 WineGLPixelFormatList[WineGLPixelFormatListSize].fbconfig = cfgs[i];
931                 WineGLPixelFormatList[WineGLPixelFormatListSize].fmt_id = tmp_fmt_id;
932                 WineGLPixelFormatList[WineGLPixelFormatListSize].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
933                 WineGLPixelFormatList[WineGLPixelFormatListSize].offscreenOnly = TRUE;
934                 WineGLPixelFormatListSize++;
935             }
936         }
937     }
938
939     if(cfgs != NULL) XFree(cfgs);
940
941     return TRUE;
942 }
943
944 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
945  * In our WGL implementation we only support a subset of these formats namely the format of
946  * Wine's main visual and offscreen formats (if they are available).
947  * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
948  * and it returns the number of supported WGL formats in fmt_count.
949  */
950 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
951 {
952     WineGLPixelFormat *res = NULL;
953
954     /* Init the list of pixel formats when we need it */
955     if(!WineGLPixelFormatListSize)
956         init_formats(display, DefaultScreen(display), visual);
957
958     /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
959      * iPixelFormat in case of probing the number of pixelformats.
960      */
961     if((iPixelFormat <= 0) || (iPixelFormat > WineGLPixelFormatListSize)) {
962         if(AllowOffscreen)
963             *fmt_count = WineGLPixelFormatListSize;
964         else
965             *fmt_count = 1; /* Only show the format of our main visual */
966     }
967     else if(iPixelFormat == 1) {
968         res = &WineGLPixelFormatList[0];
969         *fmt_count = 1; /* Only show the format of our main visual */
970     }
971     else if((WineGLPixelFormatList[iPixelFormat-1].offscreenOnly == TRUE) && AllowOffscreen) {
972         res = &WineGLPixelFormatList[iPixelFormat-1];
973         *fmt_count = WineGLPixelFormatListSize;
974         TRACE("Returning FBConfig=%p for iPixelFormat=%d\n", res->fbconfig, iPixelFormat);
975     }
976
977     TRACE("Number of returned pixelformats=%d\n", *fmt_count);
978
979     return res;
980 }
981
982 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
983 static int ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id)
984 {
985     int i;
986
987     /* Init the list of pixel formats when we need it */
988     if(!WineGLPixelFormatListSize)
989         init_formats(display, DefaultScreen(display), visual);
990
991     for(i=0; i<WineGLPixelFormatListSize; i++) {
992         if(WineGLPixelFormatList[i].fmt_id == fmt_id) {
993             TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", WineGLPixelFormatList[i].iPixelFormat, fmt_id);
994             return WineGLPixelFormatList[i].iPixelFormat;
995         }
996     }
997     TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
998     return 0;
999 }
1000
1001 /**
1002  * X11DRV_ChoosePixelFormat
1003  *
1004  * Equivalent to glXChooseVisual.
1005  */
1006 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev, 
1007                              const PIXELFORMATDESCRIPTOR *ppfd) {
1008   WineGLPixelFormat *fmt;
1009   int ret = 0;
1010   int value = 0;
1011
1012   if (!has_opengl()) {
1013     ERR("No libGL on this box - disabling OpenGL support !\n");
1014     return 0;
1015   }
1016
1017   if (TRACE_ON(opengl)) {
1018     TRACE("(%p,%p)\n", physDev, ppfd);
1019
1020     dump_PIXELFORMATDESCRIPTOR((const PIXELFORMATDESCRIPTOR *) ppfd);
1021   }
1022
1023   wine_tsx11_lock(); 
1024   if(!visual) {
1025     ERR("Can't get an opengl visual!\n");
1026     goto choose_exit;
1027   }
1028
1029   fmt = ConvertPixelFormatWGLtoGLX(gdi_display, 1 /* main visual */, FALSE /* offscreen */, &value);
1030   /* In case an fbconfig was found, check if it matches to the requirements of the ppfd */
1031   if(!fmt) {
1032     ERR("Can't find a matching FBCONFIG_ID for VISUAL_ID 0x%lx!\n", visual->visualid);
1033   } else {
1034     int dwFlags = 0;
1035     int iPixelType = 0;
1036     int value = 0;
1037
1038     /* Pixel type */
1039     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1040     if (value & GLX_RGBA_BIT)
1041       iPixelType = PFD_TYPE_RGBA;
1042     else
1043       iPixelType = PFD_TYPE_COLORINDEX;
1044
1045     if (ppfd->iPixelType != iPixelType) {
1046       TRACE("pixel type mismatch\n");
1047       goto choose_exit;
1048     }
1049
1050     /* Doublebuffer */
1051     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value); if (value) dwFlags |= PFD_DOUBLEBUFFER;
1052     if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) && (ppfd->dwFlags & PFD_DOUBLEBUFFER)) {
1053       if (!(dwFlags & PFD_DOUBLEBUFFER)) {
1054         TRACE("dbl buffer mismatch\n");
1055         goto choose_exit;
1056       }
1057     }
1058
1059     /* Stereo */
1060     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) dwFlags |= PFD_STEREO;
1061     if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE) && (ppfd->dwFlags & PFD_STEREO)) {
1062       if (!(dwFlags & PFD_STEREO)) {
1063         TRACE("stereo mismatch\n");
1064         goto choose_exit;
1065       }
1066     }
1067
1068     /* Alpha bits */
1069     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &value);
1070     if (ppfd->iPixelType==PFD_TYPE_RGBA && ppfd->cAlphaBits && !value) {
1071       TRACE("alpha mismatch\n");
1072       goto choose_exit;
1073     }
1074
1075     /* Depth bits */
1076     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1077     if (ppfd->cDepthBits && !value) {
1078       TRACE("depth mismatch\n");
1079       goto choose_exit;
1080     }
1081
1082     /* Stencil bits */
1083     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1084     if (ppfd->cStencilBits && !value) {
1085       TRACE("stencil mismatch\n");
1086       goto choose_exit;
1087     }
1088
1089     /* Aux buffers */
1090     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1091     if (ppfd->cAuxBuffers && !value) {
1092       TRACE("aux mismatch\n");
1093       goto choose_exit;
1094     }
1095
1096     /* When we pass all the checks we have found a matching format :) */
1097     ret = 1;
1098     TRACE("Successfully found a matching mode, returning index: %d\n", ret);
1099   }
1100
1101 choose_exit:
1102   if(!ret)
1103     TRACE("No matching mode was found returning 0\n");
1104
1105   wine_tsx11_unlock();
1106   return ret;
1107 }
1108
1109 /**
1110  * X11DRV_DescribePixelFormat
1111  *
1112  * Get the pixel-format descriptor associated to the given id
1113  */
1114 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
1115                                int iPixelFormat,
1116                                UINT nBytes,
1117                                PIXELFORMATDESCRIPTOR *ppfd) {
1118   /*XVisualInfo *vis;*/
1119   int value;
1120   int rb,gb,bb,ab;
1121   WineGLPixelFormat *fmt;
1122   int ret = 0;
1123   int fmt_count = 0;
1124
1125   if (!has_opengl()) {
1126     ERR("No libGL on this box - disabling OpenGL support !\n");
1127     return 0;
1128   }
1129   
1130   TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
1131
1132   /* Look for the iPixelFormat in our list of supported formats. If it is supported we get the index in the FBConfig table and the number of supported formats back */
1133   fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1134   if (ppfd == NULL) {
1135       /* The application is only querying the number of pixelformats */
1136       return fmt_count;
1137   } else if(fmt == NULL) {
1138       WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1139       return 0;
1140   }
1141
1142   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1143     ERR("Wrong structure size !\n");
1144     /* Should set error */
1145     return 0;
1146   }
1147
1148   ret = fmt_count;
1149
1150   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1151   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1152   ppfd->nVersion = 1;
1153
1154   /* These flags are always the same... */
1155   ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
1156   /* Now the flags extracted from the Visual */
1157
1158   wine_tsx11_lock();
1159
1160   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_CONFIG_CAVEAT, &value);
1161   if(value == GLX_SLOW_CONFIG)
1162       ppfd->dwFlags |= PFD_GENERIC_ACCELERATED;
1163
1164   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1165   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1166
1167   /* Pixel type */
1168   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1169   if (value & GLX_RGBA_BIT)
1170     ppfd->iPixelType = PFD_TYPE_RGBA;
1171   else
1172     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1173
1174   /* Color bits */
1175   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1176   ppfd->cColorBits = value;
1177
1178   /* Red, green, blue and alpha bits / shifts */
1179   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1180     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1181     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1182     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1183     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1184
1185     ppfd->cRedBits = rb;
1186     ppfd->cRedShift = gb + bb + ab;
1187     ppfd->cBlueBits = bb;
1188     ppfd->cBlueShift = ab;
1189     ppfd->cGreenBits = gb;
1190     ppfd->cGreenShift = bb + ab;
1191     ppfd->cAlphaBits = ab;
1192     ppfd->cAlphaShift = 0;
1193   } else {
1194     ppfd->cRedBits = 0;
1195     ppfd->cRedShift = 0;
1196     ppfd->cBlueBits = 0;
1197     ppfd->cBlueShift = 0;
1198     ppfd->cGreenBits = 0;
1199     ppfd->cGreenShift = 0;
1200     ppfd->cAlphaBits = 0;
1201     ppfd->cAlphaShift = 0;
1202   }
1203
1204   /* Accum RGBA bits */
1205   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1206   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1207   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1208   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1209
1210   ppfd->cAccumBits = rb+gb+bb+ab;
1211   ppfd->cAccumRedBits = rb;
1212   ppfd->cAccumGreenBits = gb;
1213   ppfd->cAccumBlueBits = bb;
1214   ppfd->cAccumAlphaBits = ab;
1215
1216   /* Depth bits */
1217   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1218   ppfd->cDepthBits = value;
1219
1220   /* stencil bits */
1221   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1222   ppfd->cStencilBits = value;
1223
1224   wine_tsx11_unlock();
1225
1226   /* Aux : to do ... */
1227
1228   ppfd->iLayerType = PFD_MAIN_PLANE;
1229
1230   if (TRACE_ON(opengl)) {
1231     dump_PIXELFORMATDESCRIPTOR(ppfd);
1232   }
1233
1234   return ret;
1235 }
1236
1237 /**
1238  * X11DRV_GetPixelFormat
1239  *
1240  * Get the pixel-format id used by this DC
1241  */
1242 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1243   WineGLPixelFormat *fmt;
1244   int tmp;
1245   TRACE("(%p)\n", physDev);
1246
1247   fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE, &tmp);
1248   if(!fmt)
1249   {
1250     /* This happens on HDCs on which SetPixelFormat wasn't called yet */
1251     ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physDev->current_pf);
1252     return 0;
1253   }
1254   else if(fmt->offscreenOnly)
1255   {
1256     /* Offscreen formats can't be used with traditional WGL calls.
1257      * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1258      TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1259     return 1;
1260   }
1261
1262   return physDev->current_pf;
1263   TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1264 }
1265
1266 /**
1267  * X11DRV_SetPixelFormat
1268  *
1269  * Set the pixel-format id used by this DC
1270  */
1271 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1272                            int iPixelFormat,
1273                            const PIXELFORMATDESCRIPTOR *ppfd) {
1274   WineGLPixelFormat *fmt;
1275   int value;
1276
1277   TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1278
1279   if (!has_opengl()) {
1280     ERR("No libGL on this box - disabling OpenGL support !\n");
1281     return 0;
1282   }
1283
1284   /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1285   if(get_glxdrawable(physDev) == root_window)
1286   {
1287     ERR("Invalid operation on root_window\n");
1288     return 0;
1289   }
1290
1291   /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1292   fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1293   if(!fmt) {
1294     ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1295     return 0;
1296   }
1297
1298   physDev->current_pf = iPixelFormat;
1299
1300   if (TRACE_ON(opengl)) {
1301     int gl_test = 0;
1302
1303     /*
1304      * How to test if hdc current drawable is compatible (visual/FBConfig) ?
1305      *
1306      * in case of root window created HDCs we crash here :(
1307      *
1308     Drawable drawable =  get_drawable( physDev->hdc );
1309     TRACE(" drawable (%p,%p) have :\n", drawable, root_window);
1310     pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &value);
1311     TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
1312     pglXQueryDrawable(gdi_display, drawable, GLX_VISUAL_ID, (unsigned int*) &value);
1313     TRACE(" - VISUAL_ID as 0x%x\n", tmp);
1314     pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &value);
1315     TRACE(" - WIDTH as %d\n", tmp);
1316     pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &value);
1317     TRACE(" - HEIGHT as %d\n", tmp);
1318     */
1319     gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1320     if (gl_test) {
1321       ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1322     } else {
1323       TRACE(" FBConfig have :\n");
1324       TRACE(" - FBCONFIG_ID   0x%x\n", value);
1325       pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1326       TRACE(" - VISUAL_ID     0x%x\n", value);
1327       pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1328       TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1329     }
1330   }
1331   return TRUE;
1332 }
1333
1334 /**
1335  * X11DRV_wglCreateContext
1336  *
1337  * For OpenGL32 wglCreateContext.
1338  */
1339 HGLRC X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1340 {
1341     Wine_GLContext *ret;
1342     WineGLPixelFormat *fmt;
1343     int hdcPF = physDev->current_pf;
1344     int fmt_count = 0;
1345     int value = 0;
1346     int gl_test = 0;
1347     HDC hdc = physDev->hdc;
1348
1349     TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1350
1351     if (!has_opengl()) {
1352         ERR("No libGL on this box - disabling OpenGL support !\n");
1353         return 0;
1354     }
1355
1356     /* First, get the visual in use by the X11DRV */
1357     if (!gdi_display) return 0;
1358
1359     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
1360     /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1361      * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1362      * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1363      * If this fails something is very wrong on the system. */
1364     if(!fmt) {
1365         ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
1366         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1367         return NULL;
1368     }
1369
1370     if (fmt_count < hdcPF) {
1371         ERR("(%p): unexpected pixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, hdcPF, fmt_count);
1372         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1373         return NULL;
1374     }
1375
1376     gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1377     if (gl_test) {
1378         ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1379         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1380         return NULL;
1381     }
1382
1383     /* The context will be allocated in the wglMakeCurrent call */
1384     wine_tsx11_lock();
1385     ret = alloc_context();
1386     wine_tsx11_unlock();
1387     ret->hdc = hdc;
1388     ret->physDev = physDev;
1389     ret->fmt = fmt;
1390
1391     /*ret->vis = vis;*/
1392     ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1393
1394     TRACE(" creating context %p (GL context creation delayed)\n", ret);
1395     return (HGLRC) ret;
1396 }
1397
1398 /**
1399  * X11DRV_wglDeleteContext
1400  *
1401  * For OpenGL32 wglDeleteContext.
1402  */
1403 BOOL X11DRV_wglDeleteContext(HGLRC hglrc)
1404 {
1405     Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1406     BOOL ret = TRUE;
1407
1408     TRACE("(%p)\n", hglrc);
1409
1410     if (!has_opengl()) {
1411         ERR("No libGL on this box - disabling OpenGL support !\n");
1412         return 0;
1413     }
1414
1415     wine_tsx11_lock();
1416     /* A game (Half Life not to name it) deletes twice the same context,
1417     * so make sure it is valid first */
1418     if (is_valid_context( ctx ))
1419     {
1420         if (ctx->ctx) pglXDestroyContext(gdi_display, ctx->ctx);
1421         free_context(ctx);
1422     }
1423     else
1424     {
1425         WARN("Error deleting context !\n");
1426         SetLastError(ERROR_INVALID_HANDLE);
1427         ret = FALSE;
1428     }
1429     wine_tsx11_unlock();
1430
1431     return ret;
1432 }
1433
1434 /**
1435  * X11DRV_wglGetCurrentReadDCARB
1436  *
1437  * For OpenGL32 wglGetCurrentReadDCARB.
1438  */
1439 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void) 
1440 {
1441     GLXDrawable gl_d;
1442     HDC ret;
1443
1444     TRACE("()\n");
1445
1446     wine_tsx11_lock();
1447     gl_d = pglXGetCurrentReadDrawable();
1448     ret = get_hdc_from_Drawable(gl_d);
1449     wine_tsx11_unlock();
1450
1451     TRACE(" returning %p (GL drawable %lu)\n", ret, gl_d);
1452     return ret;
1453 }
1454
1455 /**
1456  * X11DRV_wglGetProcAddress
1457  *
1458  * For OpenGL32 wglGetProcAddress.
1459  */
1460 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1461 {
1462     int i, j;
1463     const WineGLExtension *ext;
1464
1465     int padding = 32 - strlen(lpszProc);
1466     if (padding < 0)
1467         padding = 0;
1468
1469     if (!has_opengl()) {
1470         ERR("No libGL on this box - disabling OpenGL support !\n");
1471         return 0;
1472     }
1473
1474     /* Check the table of WGL extensions to see if we need to return a WGL extension
1475      * or a function pointer to a native OpenGL function. */
1476     if(strncmp(lpszProc, "wgl", 3) != 0) {
1477         return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1478     } else {
1479         TRACE("('%s'):%*s", lpszProc, padding, " ");
1480         for (i = 0; i < WineGLExtensionListSize; ++i) {
1481             ext = WineGLExtensionList[i];
1482             for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1483                 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1484                     TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1485                     return ext->extEntryPoints[j].funcAddress;
1486                 }
1487             }
1488         }
1489     }
1490
1491     ERR("(%s) - not found\n", lpszProc);
1492     return NULL;
1493 }
1494
1495 /***********************************************************************
1496  *              sync_current_drawable
1497  *
1498  * Adjust the current viewport and scissor in order to position
1499  * and size the current drawable correctly on the parent window.
1500  */
1501 static void sync_current_drawable(BOOL updatedc)
1502 {
1503     int dy;
1504     int width;
1505     int height;
1506     RECT rc;
1507     Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1508
1509     TRACE("\n");
1510
1511     if (ctx && ctx->physDev)
1512     {
1513         if (updatedc)
1514             GetClipBox(ctx->physDev->hdc, &rc); /* Make sure physDev is up to date */
1515
1516         dy = ctx->physDev->drawable_rect.bottom - ctx->physDev->drawable_rect.top -
1517             ctx->physDev->dc_rect.bottom;
1518         width = ctx->physDev->dc_rect.right - ctx->physDev->dc_rect.left;
1519         height = ctx->physDev->dc_rect.bottom - ctx->physDev->dc_rect.top;
1520
1521         wine_tsx11_lock();
1522
1523         pglViewport(ctx->physDev->dc_rect.left + ctx->viewport.left,
1524             dy + ctx->viewport.top,
1525             ctx->viewport.right ? (ctx->viewport.right - ctx->viewport.left) : width,
1526             ctx->viewport.bottom ? (ctx->viewport.bottom - ctx->viewport.top) : height);
1527
1528         pglEnable(GL_SCISSOR_TEST);
1529
1530         if (ctx->scissor_enabled)
1531             pglScissor(ctx->physDev->dc_rect.left + min(width, max(0, ctx->scissor.left)),
1532                 dy + min(height, max(0, ctx->scissor.top)),
1533                 min(width, max(0, ctx->scissor.right - ctx->scissor.left)),
1534                 min(height, max(0, ctx->scissor.bottom - ctx->scissor.top)));
1535         else
1536             pglScissor(ctx->physDev->dc_rect.left, dy, width, height);
1537
1538         wine_tsx11_unlock();
1539     }
1540 }
1541
1542 /**
1543  * X11DRV_wglMakeCurrent
1544  *
1545  * For OpenGL32 wglMakeCurrent.
1546  */
1547 BOOL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1548     BOOL ret;
1549     HDC hdc = physDev->hdc;
1550     DWORD type = GetObjectType(hdc);
1551
1552     TRACE("(%p,%p)\n", hdc, hglrc);
1553
1554     if (!has_opengl()) {
1555         ERR("No libGL on this box - disabling OpenGL support !\n");
1556         return 0;
1557     }
1558
1559     wine_tsx11_lock();
1560     if (hglrc == NULL) {
1561         ret = pglXMakeCurrent(gdi_display, None, NULL);
1562         NtCurrentTeb()->glContext = NULL;
1563     } else {
1564         Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1565         Drawable drawable = get_glxdrawable(physDev);
1566         if (ctx->ctx == NULL) {
1567             /* The describe lines below are for debugging purposes only */
1568             if (TRACE_ON(wgl)) {
1569                 describeDrawable(ctx, drawable);
1570                 describeContext(ctx);
1571             }
1572
1573             /* Create a GLX context using the same visual as chosen earlier in wglCreateContext.
1574              * We are certain that the drawable and context are compatible as we only allow compatible formats.
1575              */
1576             TRACE(" Creating GLX Context\n");
1577             if(ctx->vis)
1578                 ctx->ctx = pglXCreateContext(gdi_display, ctx->vis, NULL, type == OBJ_MEMDC ? False : True);
1579             else /* Create a GLX Context for a pbuffer */
1580                 ctx->ctx = pglXCreateNewContext(gdi_display, ctx->fmt->fbconfig, ctx->fmt->render_type, NULL, True);
1581
1582             TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1583         }
1584         TRACE(" make current for dis %p, drawable %p, ctx %p\n", gdi_display, (void*) drawable, ctx->ctx);
1585         ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1586         NtCurrentTeb()->glContext = ctx;
1587         if(ret)
1588         {
1589             ctx->physDev = physDev;
1590
1591             if (type == OBJ_MEMDC)
1592             {
1593                 ctx->do_escape = TRUE;
1594                 pglDrawBuffer(GL_FRONT_LEFT);
1595             }
1596             else
1597             {
1598                 sync_current_drawable(FALSE);
1599             }
1600         }
1601     }
1602     wine_tsx11_unlock();
1603     TRACE(" returning %s\n", (ret ? "True" : "False"));
1604     return ret;
1605 }
1606
1607 /**
1608  * X11DRV_wglMakeContextCurrentARB
1609  *
1610  * For OpenGL32 wglMakeContextCurrentARB
1611  */
1612 BOOL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) 
1613 {
1614     BOOL ret;
1615     TRACE("(%p,%p,%p)\n", hDrawDev, hReadDev, hglrc);
1616
1617     if (!has_opengl()) {
1618         ERR("No libGL on this box - disabling OpenGL support !\n");
1619         return 0;
1620     }
1621
1622     wine_tsx11_lock();
1623     if (hglrc == NULL) {
1624         ret = pglXMakeCurrent(gdi_display, None, NULL);
1625         NtCurrentTeb()->glContext = NULL;
1626     } else {
1627         if (NULL == pglXMakeContextCurrent) {
1628             ret = FALSE;
1629         } else {
1630             Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1631             Drawable d_draw = get_glxdrawable(hDrawDev);
1632             Drawable d_read = get_glxdrawable(hReadDev);
1633
1634             if (ctx->ctx == NULL) {
1635                 ctx->ctx = pglXCreateContext(gdi_display, ctx->vis, NULL, GetObjectType(hDrawDev->hdc) == OBJ_MEMDC ? False : True);
1636                 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1637             }
1638             ret = pglXMakeContextCurrent(gdi_display, d_draw, d_read, ctx->ctx);
1639             NtCurrentTeb()->glContext = ctx;
1640         }
1641     }
1642     wine_tsx11_unlock();
1643
1644     TRACE(" returning %s\n", (ret ? "True" : "False"));
1645     return ret;
1646 }
1647
1648 /**
1649  * X11DRV_wglShareLists
1650  *
1651  * For OpenGL32 wglShaderLists.
1652  */
1653 BOOL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1654     Wine_GLContext *org  = (Wine_GLContext *) hglrc1;
1655     Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1656
1657     TRACE("(%p, %p)\n", org, dest);
1658
1659     if (!has_opengl()) {
1660         ERR("No libGL on this box - disabling OpenGL support !\n");
1661         return 0;
1662     }
1663
1664     if (NULL != dest && dest->ctx != NULL) {
1665         ERR("Could not share display lists, context already created !\n");
1666         return FALSE;
1667     } else {
1668         if (org->ctx == NULL) {
1669             wine_tsx11_lock();
1670             describeContext(org);
1671
1672             if(org->vis)
1673                 org->ctx = pglXCreateContext(gdi_display, org->vis, NULL, GetObjectType(org->physDev->hdc) == OBJ_MEMDC ? False : True);
1674             else /* Create a GLX Context for a pbuffer */
1675                 org->ctx = pglXCreateNewContext(gdi_display, org->fmt->fbconfig, org->fmt->render_type, NULL, True);
1676             wine_tsx11_unlock();
1677             TRACE(" created a delayed OpenGL context (%p) for Wine context %p\n", org->ctx, org);
1678         }
1679         if (NULL != dest) {
1680             wine_tsx11_lock();
1681             describeContext(dest);
1682             /* Create the destination context with display lists shared */
1683             if(dest->vis)
1684                 dest->ctx = pglXCreateContext(gdi_display, dest->vis, org->ctx, GetObjectType(org->physDev->hdc) == OBJ_MEMDC ? False : True);
1685             else /* Create a GLX Context for a pbuffer */
1686                 dest->ctx = pglXCreateNewContext(gdi_display, dest->fmt->fbconfig, dest->fmt->render_type, org->ctx, True);
1687             wine_tsx11_unlock();
1688             TRACE(" created a delayed OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1689             return TRUE;
1690         }
1691     }
1692     return FALSE;
1693 }
1694
1695 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1696 {
1697      /* We are running using client-side rendering fonts... */
1698      GLYPHMETRICS gm;
1699      unsigned int glyph;
1700      int size = 0;
1701      void *bitmap = NULL, *gl_bitmap = NULL;
1702      int org_alignment;
1703
1704      wine_tsx11_lock();
1705      pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1706      pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1707      wine_tsx11_unlock();
1708
1709      for (glyph = first; glyph < first + count; glyph++) {
1710          unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, NULL);
1711          int height, width_int;
1712
1713          TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1714          if (needed_size == GDI_ERROR) {
1715              TRACE("  - needed size : %d (GDI_ERROR)\n", needed_size);
1716              goto error;
1717          } else {
1718              TRACE("  - needed size : %d\n", needed_size);
1719          }
1720
1721          if (needed_size > size) {
1722              size = needed_size;
1723              HeapFree(GetProcessHeap(), 0, bitmap);
1724              HeapFree(GetProcessHeap(), 0, gl_bitmap);
1725              bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1726              gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1727          }
1728          if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, NULL) == GDI_ERROR) goto error;
1729          if (TRACE_ON(opengl)) {
1730              unsigned int height, width, bitmask;
1731              unsigned char *bitmap_ = (unsigned char *) bitmap;
1732
1733              TRACE("  - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1734              TRACE("  - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1735              TRACE("  - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1736              if (needed_size != 0) {
1737                  TRACE("  - bitmap :\n");
1738                  for (height = 0; height < gm.gmBlackBoxY; height++) {
1739                      TRACE("      ");
1740                      for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1741                          if (bitmask == 0) {
1742                              bitmap_ += 1;
1743                              bitmask = 0x80;
1744                          }
1745                          if (*bitmap_ & bitmask)
1746                              TRACE("*");
1747                          else
1748                              TRACE(" ");
1749                      }
1750                      bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1751                      TRACE("\n");
1752                  }
1753              }
1754          }
1755
1756          /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1757          * glyph for it to be drawn properly.
1758          */
1759          if (needed_size != 0) {
1760              width_int = (gm.gmBlackBoxX + 31) / 32;
1761              for (height = 0; height < gm.gmBlackBoxY; height++) {
1762                  int width;
1763                  for (width = 0; width < width_int; width++) {
1764                      ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1765                      ((int *) bitmap)[height * width_int + width];
1766                  }
1767              }
1768          }
1769
1770          wine_tsx11_lock();
1771          pglNewList(listBase++, GL_COMPILE);
1772          if (needed_size != 0) {
1773              pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1774                      0 - (int) gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - (int) gm.gmptGlyphOrigin.y,
1775                      gm.gmCellIncX, gm.gmCellIncY,
1776                      gl_bitmap);
1777          } else {
1778              /* This is the case of 'empty' glyphs like the space character */
1779              pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1780          }
1781          pglEndList();
1782          wine_tsx11_unlock();
1783      }
1784
1785      wine_tsx11_lock();
1786      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1787      wine_tsx11_unlock();
1788
1789      HeapFree(GetProcessHeap(), 0, bitmap);
1790      HeapFree(GetProcessHeap(), 0, gl_bitmap);
1791      return TRUE;
1792
1793   error:
1794      wine_tsx11_lock();
1795      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1796      wine_tsx11_unlock();
1797
1798      HeapFree(GetProcessHeap(), 0, bitmap);
1799      HeapFree(GetProcessHeap(), 0, gl_bitmap);
1800      return FALSE;
1801 }
1802
1803 /**
1804  * X11DRV_wglUseFontBitmapsA
1805  *
1806  * For OpenGL32 wglUseFontBitmapsA.
1807  */
1808 BOOL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1809 {
1810      Font fid = physDev->font;
1811
1812      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1813
1814      if (!has_opengl()) {
1815         ERR("No libGL on this box - disabling OpenGL support !\n");
1816         return 0;
1817      }
1818
1819      if (fid == 0) {
1820          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
1821      }
1822
1823      wine_tsx11_lock();
1824      /* I assume that the glyphs are at the same position for X and for Windows */
1825      pglXUseXFont(fid, first, count, listBase);
1826      wine_tsx11_unlock();
1827      return TRUE;
1828 }
1829
1830 /**
1831  * X11DRV_wglUseFontBitmapsW
1832  *
1833  * For OpenGL32 wglUseFontBitmapsW.
1834  */
1835 BOOL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1836 {
1837      Font fid = physDev->font;
1838
1839      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1840
1841      if (!has_opengl()) {
1842         ERR("No libGL on this box - disabling OpenGL support !\n");
1843         return 0;
1844      }
1845
1846      if (fid == 0) {
1847          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
1848      }
1849
1850      WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
1851
1852      wine_tsx11_lock();
1853      /* I assume that the glyphs are at the same position for X and for Windows */
1854      pglXUseXFont(fid, first, count, listBase);
1855      wine_tsx11_unlock();
1856      return TRUE;
1857 }
1858
1859 static void WINAPI X11DRV_wglDisable(GLenum cap)
1860 {
1861     if (cap == GL_SCISSOR_TEST)
1862     {
1863        Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1864
1865        if (ctx)
1866           ctx->scissor_enabled = FALSE;
1867     }
1868     else
1869     {
1870         wine_tsx11_lock();
1871         pglDisable(cap);
1872         wine_tsx11_unlock();
1873     }
1874 }
1875
1876 static void WINAPI X11DRV_wglEnable(GLenum cap)
1877 {
1878     if (cap == GL_SCISSOR_TEST)
1879     {
1880        Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1881
1882        if (ctx)
1883            ctx->scissor_enabled = TRUE;
1884     }
1885     else
1886     {
1887         wine_tsx11_lock();
1888         pglEnable(cap);
1889         wine_tsx11_unlock();
1890     }
1891 }
1892
1893 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
1894 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
1895 {
1896     wine_tsx11_lock();
1897     switch(pname)
1898     {
1899     case GL_DEPTH_BITS:
1900         {
1901             GLXContext gl_ctx = pglXGetCurrentContext();
1902             Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1903
1904             pglGetIntegerv(pname, params);
1905             /**
1906              * if we cannot find a Wine Context
1907              * we only have the default wine desktop context,
1908              * so if we have only a 24 depth say we have 32
1909              */
1910             if (NULL == ret && 24 == *params) {
1911                 *params = 32;
1912             }
1913             TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
1914             break;
1915         }
1916     case GL_ALPHA_BITS:
1917         {
1918             GLXContext gl_ctx = pglXGetCurrentContext();
1919             Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1920
1921             pglXGetFBConfigAttrib(gdi_display, ret->fmt->fbconfig, GLX_ALPHA_SIZE, params);
1922             TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
1923             break;
1924         }
1925     default:
1926         pglGetIntegerv(pname, params);
1927         break;
1928     }
1929     wine_tsx11_unlock();
1930 }
1931
1932 static GLboolean WINAPI X11DRV_wglIsEnabled(GLenum cap)
1933 {
1934     GLboolean enabled = False;
1935
1936     if (cap == GL_SCISSOR_TEST)
1937     {
1938        Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1939
1940        if (ctx)
1941            enabled = ctx->scissor_enabled;
1942     }
1943     else
1944     {
1945         wine_tsx11_lock();
1946         enabled = pglIsEnabled(cap);
1947         wine_tsx11_unlock();
1948     }
1949     return enabled;
1950 }
1951
1952 static void WINAPI X11DRV_wglScissor(GLint x, GLint y, GLsizei width, GLsizei height)
1953 {
1954     Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1955
1956     if (ctx)
1957     {
1958         ctx->scissor.left = x;
1959         ctx->scissor.top = y;
1960         ctx->scissor.right = x + width;
1961         ctx->scissor.bottom = y + height;
1962
1963         sync_current_drawable(TRUE);
1964     }
1965 }
1966
1967 static void WINAPI X11DRV_wglViewport(GLint x, GLint y, GLsizei width, GLsizei height)
1968 {
1969     Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1970
1971     if (ctx)
1972     {
1973         ctx->viewport.left = x;
1974         ctx->viewport.top = y;
1975         ctx->viewport.right = x + width;
1976         ctx->viewport.bottom = y + height;
1977
1978         sync_current_drawable(TRUE);
1979     }
1980 }
1981
1982 /**
1983  * X11DRV_wglGetExtensionsStringARB
1984  *
1985  * WGL_ARB_extensions_string: wglGetExtensionsStringARB
1986  */
1987 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
1988     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
1989     return WineGLInfo.wglExtensions;
1990 }
1991
1992 /**
1993  * X11DRV_wglCreatePbufferARB
1994  *
1995  * WGL_ARB_pbuffer: wglCreatePbufferARB
1996  */
1997 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
1998 {
1999     Wine_GLPBuffer* object = NULL;
2000     WineGLPixelFormat *fmt = NULL;
2001     int nCfgs = 0;
2002     int attribs[256];
2003     int nAttribs = 0;
2004
2005     TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2006
2007     if (0 >= iPixelFormat) {
2008         ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2009         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2010         return NULL; /* unexpected error */
2011     }
2012
2013     /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2014     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2015     if(!fmt) {
2016         ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2017         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2018         goto create_failed; /* unexpected error */
2019     }
2020
2021     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2022     if (NULL == object) {
2023         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2024         goto create_failed; /* unexpected error */
2025     }
2026     object->hdc = hdc;
2027     object->display = gdi_display;
2028     object->width = iWidth;
2029     object->height = iHeight;
2030     object->fmt = fmt;
2031
2032     PUSH2(attribs, GLX_PBUFFER_WIDTH,  iWidth);
2033     PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight); 
2034     while (piAttribList && 0 != *piAttribList) {
2035         int attr_v;
2036         switch (*piAttribList) {
2037             case WGL_PBUFFER_LARGEST_ARB: {
2038                 ++piAttribList;
2039                 attr_v = *piAttribList;
2040                 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2041                 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2042                 break;
2043             }
2044
2045             case WGL_TEXTURE_FORMAT_ARB: {
2046                 ++piAttribList;
2047                 attr_v = *piAttribList;
2048                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2049                 if (use_render_texture_ati) {
2050                     int type = 0;
2051                     switch (attr_v) {
2052                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2053                         case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2054                         case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2055                         default:
2056                             SetLastError(ERROR_INVALID_DATA);
2057                             goto create_failed;
2058                     }
2059                     object->use_render_texture = 1;
2060                     PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2061                 } else {
2062                     if (WGL_NO_TEXTURE_ARB == attr_v) {
2063                         object->use_render_texture = 0;
2064                     } else {
2065                         if (!use_render_texture_emulation) {
2066                             SetLastError(ERROR_INVALID_DATA);
2067                             goto create_failed;
2068                         }
2069                         switch (attr_v) {
2070                             case WGL_TEXTURE_RGB_ARB:
2071                                 object->use_render_texture = GL_RGB;
2072                                 object->texture_bpp = 3;
2073                                 object->texture_format = GL_RGB;
2074                                 object->texture_type = GL_UNSIGNED_BYTE;
2075                                 break;
2076                             case WGL_TEXTURE_RGBA_ARB:
2077                                 object->use_render_texture = GL_RGBA;
2078                                 object->texture_bpp = 4;
2079                                 object->texture_format = GL_RGBA;
2080                                 object->texture_type = GL_UNSIGNED_BYTE;
2081                                 break;
2082
2083                             /* WGL_FLOAT_COMPONENTS_NV */
2084                             case WGL_TEXTURE_FLOAT_R_NV:
2085                                 object->use_render_texture = GL_FLOAT_R_NV;
2086                                 object->texture_bpp = 4;
2087                                 object->texture_format = GL_RED;
2088                                 object->texture_type = GL_FLOAT;
2089                                 break;
2090                             case WGL_TEXTURE_FLOAT_RG_NV:
2091                                 object->use_render_texture = GL_FLOAT_RG_NV;
2092                                 object->texture_bpp = 8;
2093                                 object->texture_format = GL_LUMINANCE_ALPHA;
2094                                 object->texture_type = GL_FLOAT;
2095                                 break;
2096                             case WGL_TEXTURE_FLOAT_RGB_NV:
2097                                 object->use_render_texture = GL_FLOAT_RGB_NV;
2098                                 object->texture_bpp = 12;
2099                                 object->texture_format = GL_RGB;
2100                                 object->texture_type = GL_FLOAT;
2101                                 break;
2102                             case WGL_TEXTURE_FLOAT_RGBA_NV:
2103                                 object->use_render_texture = GL_FLOAT_RGBA_NV;
2104                                 object->texture_bpp = 16;
2105                                 object->texture_format = GL_RGBA;
2106                                 object->texture_type = GL_FLOAT;
2107                                 break;
2108                             default:
2109                                 ERR("Unknown texture format: %x\n", attr_v);
2110                                 SetLastError(ERROR_INVALID_DATA);
2111                                 goto create_failed;
2112                         }
2113                     }
2114                 }
2115                 break;
2116             }
2117
2118             case WGL_TEXTURE_TARGET_ARB: {
2119                 ++piAttribList;
2120                 attr_v = *piAttribList;
2121                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2122                 if (use_render_texture_ati) {
2123                     int type = 0;
2124                     switch (attr_v) {
2125                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2126                         case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2127                         case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2128                         case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2129                         default:
2130                             SetLastError(ERROR_INVALID_DATA);
2131                             goto create_failed;
2132                     }
2133                     PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2134                 } else {
2135                     if (WGL_NO_TEXTURE_ARB == attr_v) {
2136                         object->texture_target = 0;
2137                     } else {
2138                         if (!use_render_texture_emulation) {
2139                             SetLastError(ERROR_INVALID_DATA);
2140                             goto create_failed;
2141                         }
2142                         switch (attr_v) {
2143                             case WGL_TEXTURE_CUBE_MAP_ARB: {
2144                                 if (iWidth != iHeight) {
2145                                     SetLastError(ERROR_INVALID_DATA);
2146                                     goto create_failed;
2147                                 }
2148                                 object->texture_target = GL_TEXTURE_CUBE_MAP;
2149                                 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2150                                break;
2151                             }
2152                             case WGL_TEXTURE_1D_ARB: {
2153                                 if (1 != iHeight) {
2154                                     SetLastError(ERROR_INVALID_DATA);
2155                                     goto create_failed;
2156                                 }
2157                                 object->texture_target = GL_TEXTURE_1D;
2158                                 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2159                                 break;
2160                             }
2161                             case WGL_TEXTURE_2D_ARB: {
2162                                 object->texture_target = GL_TEXTURE_2D;
2163                                 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2164                                 break;
2165                             }
2166                             case WGL_TEXTURE_RECTANGLE_NV: {
2167                                 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2168                                 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2169                                 break;
2170                             }
2171                             default:
2172                                 ERR("Unknown texture target: %x\n", attr_v);
2173                                 SetLastError(ERROR_INVALID_DATA);
2174                                 goto create_failed;
2175                         }
2176                     }
2177                 }
2178                 break;
2179             }
2180
2181             case WGL_MIPMAP_TEXTURE_ARB: {
2182                 ++piAttribList;
2183                 attr_v = *piAttribList;
2184                 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2185                 if (use_render_texture_ati) {
2186                     PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2187                 } else {
2188                     if (!use_render_texture_emulation) {
2189                         SetLastError(ERROR_INVALID_DATA);
2190                         goto create_failed;
2191                     }
2192                 }
2193                 break;
2194             }
2195         }
2196         ++piAttribList;
2197     }
2198
2199     PUSH1(attribs, None);
2200     object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2201     TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
2202     if (!object->drawable) {
2203         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2204         goto create_failed; /* unexpected error */
2205     }
2206     TRACE("->(%p)\n", object);
2207     return (HPBUFFERARB) object;
2208
2209 create_failed:
2210     HeapFree(GetProcessHeap(), 0, object);
2211     TRACE("->(FAILED)\n");
2212     return (HPBUFFERARB) NULL;
2213 }
2214
2215 /**
2216  * X11DRV_wglDestroyPbufferARB
2217  *
2218  * WGL_ARB_pbuffer: wglDestroyPbufferARB
2219  */
2220 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2221 {
2222     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2223     TRACE("(%p)\n", hPbuffer);
2224     if (NULL == object) {
2225         SetLastError(ERROR_INVALID_HANDLE);
2226         return GL_FALSE;
2227     }
2228     pglXDestroyPbuffer(object->display, object->drawable);
2229     HeapFree(GetProcessHeap(), 0, object);
2230     return GL_TRUE;
2231 }
2232
2233 /**
2234  * X11DRV_wglGetPbufferDCARB
2235  *
2236  * WGL_ARB_pbuffer: wglGetPbufferDCARB
2237  * The function wglGetPbufferDCARB returns a device context for a pbuffer.
2238  * Gdi32 implements the part of this function which creates a device context.
2239  * This part associates the physDev with the X drawable of the pbuffer.
2240  */
2241 HDC X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
2242 {
2243     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2244     if (NULL == object) {
2245         SetLastError(ERROR_INVALID_HANDLE);
2246         return NULL;
2247     }
2248
2249     /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
2250      * All formats in our pixelformat list are compatible with each other and the main drawable. */
2251     physDev->current_pf = object->fmt->iPixelFormat;
2252     physDev->drawable = object->drawable;
2253     SetRect( &physDev->drawable_rect, 0, 0, object->width, object->height );
2254     physDev->dc_rect = physDev->drawable_rect;
2255
2256     TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
2257     return physDev->hdc;
2258 }
2259
2260 /**
2261  * X11DRV_wglQueryPbufferARB
2262  *
2263  * WGL_ARB_pbuffer: wglQueryPbufferARB
2264  */
2265 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2266 {
2267     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2268     TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2269     if (NULL == object) {
2270         SetLastError(ERROR_INVALID_HANDLE);
2271         return GL_FALSE;
2272     }
2273     switch (iAttribute) {
2274         case WGL_PBUFFER_WIDTH_ARB:
2275             pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2276             break;
2277         case WGL_PBUFFER_HEIGHT_ARB:
2278             pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2279             break;
2280
2281         case WGL_PBUFFER_LOST_ARB:
2282             FIXME("unsupported WGL_PBUFFER_LOST_ARB (need glXSelectEvent/GLX_DAMAGED work)\n");
2283             break;
2284
2285         case WGL_TEXTURE_FORMAT_ARB:
2286             if (use_render_texture_ati) {
2287                 unsigned int tmp;
2288                 int type = WGL_NO_TEXTURE_ARB;
2289                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2290                 switch (tmp) {
2291                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2292                     case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2293                     case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2294                 }
2295                 *piValue = type;
2296             } else {
2297                 if (!object->use_render_texture) {
2298                     *piValue = WGL_NO_TEXTURE_ARB;
2299                 } else {
2300                     if (!use_render_texture_emulation) {
2301                         SetLastError(ERROR_INVALID_HANDLE);
2302                         return GL_FALSE;
2303                     }
2304                     switch(object->use_render_texture) {
2305                         case GL_RGB:
2306                             *piValue = WGL_TEXTURE_RGB_ARB;
2307                             break;
2308                         case GL_RGBA:
2309                             *piValue = WGL_TEXTURE_RGBA_ARB;
2310                             break;
2311                         /* WGL_FLOAT_COMPONENTS_NV */
2312                         case GL_FLOAT_R_NV:
2313                             *piValue = WGL_TEXTURE_FLOAT_R_NV;
2314                             break;
2315                         case GL_FLOAT_RG_NV:
2316                             *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2317                             break;
2318                         case GL_FLOAT_RGB_NV:
2319                             *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2320                             break;
2321                         case GL_FLOAT_RGBA_NV:
2322                             *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2323                             break;
2324                         default:
2325                             ERR("Unknown texture format: %x\n", object->use_render_texture);
2326                     }
2327                 }
2328             }
2329             break;
2330
2331         case WGL_TEXTURE_TARGET_ARB:
2332             if (use_render_texture_ati) {
2333                 unsigned int tmp;
2334                 int type = WGL_NO_TEXTURE_ARB;
2335                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2336                 switch (tmp) {
2337                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2338                     case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2339                     case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2340                     case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2341                 }
2342                 *piValue = type;
2343             } else {
2344             if (!object->texture_target) {
2345                 *piValue = WGL_NO_TEXTURE_ARB;
2346             } else {
2347                 if (!use_render_texture_emulation) {
2348                     SetLastError(ERROR_INVALID_DATA);      
2349                     return GL_FALSE;
2350                 }
2351                 switch (object->texture_target) {
2352                     case GL_TEXTURE_1D:       *piValue = WGL_TEXTURE_1D_ARB; break;
2353                     case GL_TEXTURE_2D:       *piValue = WGL_TEXTURE_2D_ARB; break;
2354                     case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2355                     case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2356                 }
2357             }
2358         }
2359         break;
2360
2361     case WGL_MIPMAP_TEXTURE_ARB:
2362         if (use_render_texture_ati) {
2363             pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2364         } else {
2365             *piValue = GL_FALSE; /** don't support that */
2366             FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2367         }
2368         break;
2369
2370     default:
2371         FIXME("unexpected attribute %x\n", iAttribute);
2372         break;
2373     }
2374
2375     return GL_TRUE;
2376 }
2377
2378 /**
2379  * X11DRV_wglReleasePbufferDCARB
2380  *
2381  * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2382  */
2383 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2384 {
2385     TRACE("(%p, %p)\n", hPbuffer, hdc);
2386     DeleteDC(hdc);
2387     return 0;
2388 }
2389
2390 /**
2391  * X11DRV_wglSetPbufferAttribARB
2392  *
2393  * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2394  */
2395 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2396 {
2397     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2398     WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2399     if (NULL == object) {
2400         SetLastError(ERROR_INVALID_HANDLE);
2401         return GL_FALSE;
2402     }
2403     if (!object->use_render_texture) {
2404         SetLastError(ERROR_INVALID_HANDLE);
2405         return GL_FALSE;
2406     }
2407     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2408         return GL_TRUE;
2409     }
2410     if (NULL != pglXDrawableAttribARB) {
2411         if (use_render_texture_ati) {
2412             FIXME("Need conversion for GLX_ATI_render_texture\n");
2413         }
2414         return pglXDrawableAttribARB(object->display, object->drawable, piAttribList); 
2415     }
2416     return GL_FALSE;
2417 }
2418
2419 /**
2420  * X11DRV_wglChoosePixelFormatARB
2421  *
2422  * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2423  */
2424 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2425 {
2426     int gl_test = 0;
2427     int attribs[256];
2428     int nAttribs = 0;
2429     GLXFBConfig* cfgs = NULL;
2430     int nCfgs = 0;
2431     UINT it;
2432     int fmt_id;
2433     int fmt = 0;
2434     int pfmt_it = 0;
2435
2436     TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2437     if (NULL != pfAttribFList) {
2438         FIXME("unused pfAttribFList\n");
2439     }
2440
2441     nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2442     if (-1 == nAttribs) {
2443         WARN("Cannot convert WGL to GLX attributes\n");
2444         return GL_FALSE;
2445     }
2446     PUSH1(attribs, None);
2447
2448     /* Search for FB configurations matching the requirements in attribs */
2449     cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2450     if (NULL == cfgs) {
2451         WARN("Compatible Pixel Format not found\n");
2452         return GL_FALSE;
2453     }
2454
2455     /* Loop through all matching formats and check if they are suitable.
2456     * Note that this function should at max return nMaxFormats different formats */
2457     for (it = 0; it < nCfgs; ++it) {
2458         gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2459         if (gl_test) {
2460             ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2461             continue;
2462         }
2463
2464         /* Search for the format in our list of compatible formats */
2465         fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id);
2466         if(!fmt)
2467             continue;
2468
2469         if(pfmt_it < nMaxFormats) {
2470             piFormats[pfmt_it] = fmt;
2471             TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2472         }
2473         pfmt_it++;
2474     }
2475
2476     *nNumFormats = pfmt_it;
2477     /** free list */
2478     XFree(cfgs);
2479     return GL_TRUE;
2480 }
2481
2482 /**
2483  * X11DRV_wglGetPixelFormatAttribivARB
2484  *
2485  * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2486  */
2487 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2488 {
2489     UINT i;
2490     WineGLPixelFormat *fmt = NULL;
2491     int hTest;
2492     int tmp;
2493     int curGLXAttr = 0;
2494     int nWGLFormats = 0;
2495
2496     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2497
2498     if (0 < iLayerPlane) {
2499         FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2500         return GL_FALSE;
2501     }
2502
2503     /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supoprted.
2504     * We don't have to fail yet as a program can specify an invaled iPixelFormat (lets say 0) if it wants to query
2505     * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2506     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2507     if(!fmt) {
2508         WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2509     }
2510
2511     for (i = 0; i < nAttributes; ++i) {
2512         const int curWGLAttr = piAttributes[i];
2513         TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2514
2515         switch (curWGLAttr) {
2516             case WGL_NUMBER_PIXEL_FORMATS_ARB:
2517                 piValues[i] = nWGLFormats; 
2518                 continue;
2519
2520             case WGL_SUPPORT_OPENGL_ARB:
2521                 piValues[i] = GL_TRUE; 
2522                 continue;
2523
2524             case WGL_ACCELERATION_ARB:
2525                 curGLXAttr = GLX_CONFIG_CAVEAT;
2526                 if (!fmt) goto pix_error;
2527                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2528                 if (hTest) goto get_error;
2529                 switch (tmp) {
2530                     case GLX_NONE: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2531                     case GLX_SLOW_CONFIG: piValues[i] = WGL_NO_ACCELERATION_ARB; break;
2532                     case GLX_NON_CONFORMANT_CONFIG: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2533                     default:
2534                         ERR("unexpected Config Caveat(%x)\n", tmp);
2535                         piValues[i] = WGL_NO_ACCELERATION_ARB;
2536                 }
2537                 continue;
2538
2539             case WGL_TRANSPARENT_ARB:
2540                 curGLXAttr = GLX_TRANSPARENT_TYPE;
2541                 if (!fmt) goto pix_error;
2542                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2543                 if (hTest) goto get_error;
2544                     piValues[i] = GL_FALSE;
2545                 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2546                     continue;
2547
2548             case WGL_PIXEL_TYPE_ARB:
2549                 curGLXAttr = GLX_RENDER_TYPE;
2550                 if (!fmt) goto pix_error;
2551                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2552                 if (hTest) goto get_error;
2553                 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2554                 if      (tmp & GLX_RGBA_BIT)           { piValues[i] = WGL_TYPE_RGBA_ARB; }
2555                 else if (tmp & GLX_COLOR_INDEX_BIT)    { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2556                 else if (tmp & GLX_RGBA_FLOAT_BIT)     { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2557                 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2558                 else {
2559                     ERR("unexpected RenderType(%x)\n", tmp);
2560                     piValues[i] = WGL_TYPE_RGBA_ARB;
2561                 }
2562                 continue;
2563
2564             case WGL_COLOR_BITS_ARB:
2565                 curGLXAttr = GLX_BUFFER_SIZE;
2566                 break;
2567
2568             case WGL_BIND_TO_TEXTURE_RGB_ARB:
2569                 if (use_render_texture_ati) {
2570                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2571                     break;
2572                 }
2573             case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2574                 if (use_render_texture_ati) {
2575                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2576                     break;
2577                 }
2578                 if (!use_render_texture_emulation) {
2579                     piValues[i] = GL_FALSE;
2580                     continue;   
2581                 }
2582                 curGLXAttr = GLX_RENDER_TYPE;
2583                 if (!fmt) goto pix_error;
2584                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2585                 if (hTest) goto get_error;
2586                 if (GLX_COLOR_INDEX_BIT == tmp) {
2587                     piValues[i] = GL_FALSE;  
2588                     continue;
2589                 }
2590                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2591                 if (hTest) goto get_error;
2592                     piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2593                 continue;
2594
2595             case WGL_BLUE_BITS_ARB:
2596                 curGLXAttr = GLX_BLUE_SIZE;
2597                 break;
2598             case WGL_RED_BITS_ARB:
2599                 curGLXAttr = GLX_RED_SIZE;
2600                 break;
2601             case WGL_GREEN_BITS_ARB:
2602                 curGLXAttr = GLX_GREEN_SIZE;
2603                 break;
2604             case WGL_ALPHA_BITS_ARB:
2605                 curGLXAttr = GLX_ALPHA_SIZE;
2606                 break;
2607             case WGL_DEPTH_BITS_ARB:
2608                 curGLXAttr = GLX_DEPTH_SIZE;
2609                 break;
2610             case WGL_STENCIL_BITS_ARB:
2611                 curGLXAttr = GLX_STENCIL_SIZE;
2612                 break;
2613             case WGL_DOUBLE_BUFFER_ARB:
2614                 curGLXAttr = GLX_DOUBLEBUFFER;
2615                 break;
2616             case WGL_STEREO_ARB:
2617                 curGLXAttr = GLX_STEREO;
2618                 break;
2619             case WGL_AUX_BUFFERS_ARB:
2620                 curGLXAttr = GLX_AUX_BUFFERS;
2621                 break;
2622
2623             case WGL_SUPPORT_GDI_ARB:
2624                 curGLXAttr = GLX_X_RENDERABLE;
2625                 break;
2626
2627             case WGL_DRAW_TO_WINDOW_ARB:
2628             case WGL_DRAW_TO_BITMAP_ARB:
2629             case WGL_DRAW_TO_PBUFFER_ARB:
2630                 if (!fmt) goto pix_error;
2631                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2632                 if (hTest) goto get_error;
2633                 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2634                    (curWGLAttr == WGL_DRAW_TO_BITMAP_ARB && (tmp&GLX_PIXMAP_BIT)) ||
2635                    (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2636                     piValues[i] = GL_TRUE;
2637                 else
2638                     piValues[i] = GL_FALSE;
2639                 continue;
2640
2641             case WGL_PBUFFER_LARGEST_ARB:
2642                 curGLXAttr = GLX_LARGEST_PBUFFER;
2643                 break;
2644
2645             case WGL_SAMPLE_BUFFERS_ARB:
2646                 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2647                 break;
2648
2649             case WGL_SAMPLES_ARB:
2650                 curGLXAttr = GLX_SAMPLES_ARB;
2651                 break;
2652
2653             case WGL_FLOAT_COMPONENTS_NV:
2654                 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2655                 break;
2656
2657             case WGL_ACCUM_RED_BITS_ARB:
2658                 curGLXAttr = GLX_ACCUM_RED_SIZE;
2659                 break;
2660             case WGL_ACCUM_GREEN_BITS_ARB:
2661                 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2662                 break;
2663             case WGL_ACCUM_BLUE_BITS_ARB:
2664                 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2665                 break;
2666             case WGL_ACCUM_ALPHA_BITS_ARB:
2667                 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2668                 break;
2669             case WGL_ACCUM_BITS_ARB:
2670                 if (!fmt) goto pix_error;
2671                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2672                 if (hTest) goto get_error;
2673                 piValues[i] = tmp;
2674                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2675                 if (hTest) goto get_error;
2676                 piValues[i] += tmp;
2677                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2678                 if (hTest) goto get_error;
2679                 piValues[i] += tmp;
2680                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2681                 if (hTest) goto get_error;
2682                 piValues[i] += tmp;
2683                 continue;
2684
2685             default:
2686                 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2687         }
2688
2689         /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2690          * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2691          * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2692          *
2693          * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2694          *       and check which options can work using iPixelFormat=0 and which not.
2695          *       A problem would be that this function is an extension. This would
2696          *       mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2697          */
2698         if (0 != curGLXAttr && iPixelFormat != 0) {
2699             if (!fmt) goto pix_error;
2700             hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2701             if (hTest) goto get_error;
2702             curGLXAttr = 0;
2703         } else { 
2704             piValues[i] = GL_FALSE; 
2705         }
2706     }
2707     return GL_TRUE;
2708
2709 get_error:
2710     ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2711     return GL_FALSE;
2712
2713 pix_error:
2714     ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
2715     return GL_FALSE;
2716 }
2717
2718 /**
2719  * X11DRV_wglGetPixelFormatAttribfvARB
2720  *
2721  * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2722  */
2723 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
2724 {
2725     int *attr;
2726     int ret;
2727     int i;
2728
2729     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2730
2731     /* Allocate a temporary array to store integer values */
2732     attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2733     if (!attr) {
2734         ERR("couldn't allocate %d array\n", nAttributes);
2735         return GL_FALSE;
2736     }
2737
2738     /* Piggy-back on wglGetPixelFormatAttribivARB */
2739     ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2740     if (ret) {
2741         /* Convert integer values to float. Should also check for attributes
2742            that can give decimal values here */
2743         for (i=0; i<nAttributes;i++) {
2744             pfValues[i] = attr[i];
2745         }
2746     }
2747
2748     HeapFree(GetProcessHeap(), 0, attr);
2749     return ret;
2750 }
2751
2752 /**
2753  * X11DRV_wglBindTexImageARB
2754  *
2755  * WGL_ARB_render_texture: wglBindTexImageARB
2756  */
2757 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2758 {
2759     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2760     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2761     if (NULL == object) {
2762         SetLastError(ERROR_INVALID_HANDLE);
2763         return GL_FALSE;
2764     }
2765     if (!object->use_render_texture) {
2766         SetLastError(ERROR_INVALID_HANDLE);
2767         return GL_FALSE;
2768     }
2769
2770     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2771         static int init = 0;
2772         int prev_binded_texture = 0;
2773         GLXContext prev_context = pglXGetCurrentContext();
2774         Drawable prev_drawable = pglXGetCurrentDrawable();
2775         GLXContext tmp_context;
2776
2777         /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2778            This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2779            isn't ideal performance wise but I wasn't able to get other ways working.
2780         */
2781         if(!init) {
2782             init = 1; /* Only show the FIXME once for performance reasons */
2783             FIXME("partial stub!\n");
2784         }
2785
2786         TRACE("drawable=%p, context=%p\n", (void*)object->drawable, prev_context);
2787         tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2788
2789         pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2790
2791         /* Switch to our pbuffer */
2792         pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2793
2794         /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2795          * After that upload the pbuffer texture data. */
2796         pglBindTexture(object->texture_target, prev_binded_texture);
2797         pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2798
2799         /* Switch back to the original drawable and upload the pbuffer-texture */
2800         pglXMakeCurrent(object->display, prev_drawable, prev_context);
2801         pglXDestroyContext(gdi_display, tmp_context);
2802         return GL_TRUE;
2803     }
2804
2805     if (NULL != pglXBindTexImageARB) {
2806         return pglXBindTexImageARB(object->display, object->drawable, iBuffer);
2807     }
2808     return GL_FALSE;
2809 }
2810
2811 /**
2812  * X11DRV_wglReleaseTexImageARB
2813  *
2814  * WGL_ARB_render_texture: wglReleaseTexImageARB
2815  */
2816 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2817 {
2818     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2819     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2820     if (NULL == object) {
2821         SetLastError(ERROR_INVALID_HANDLE);
2822         return GL_FALSE;
2823     }
2824     if (!object->use_render_texture) {
2825         SetLastError(ERROR_INVALID_HANDLE);
2826         return GL_FALSE;
2827     }
2828     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2829         return GL_TRUE;
2830     }
2831     if (NULL != pglXReleaseTexImageARB) {
2832         return pglXReleaseTexImageARB(object->display, object->drawable, iBuffer);
2833     }
2834     return GL_FALSE;
2835 }
2836
2837 /**
2838  * X11DRV_wglGetExtensionsStringEXT
2839  *
2840  * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2841  */
2842 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
2843     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2844     return WineGLInfo.wglExtensions;
2845 }
2846
2847 /**
2848  * X11DRV_wglGetSwapIntervalEXT
2849  *
2850  * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2851  */
2852 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
2853     FIXME("(),stub!\n");
2854     return swap_interval;
2855 }
2856
2857 /**
2858  * X11DRV_wglSwapIntervalEXT
2859  *
2860  * WGL_EXT_swap_control: wglSwapIntervalEXT
2861  */
2862 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
2863     TRACE("(%d)\n", interval);
2864     swap_interval = interval;
2865     if (NULL != pglXSwapIntervalSGI) {
2866         return 0 == pglXSwapIntervalSGI(interval);
2867     }
2868     WARN("(): GLX_SGI_swap_control extension seems not supported\n");
2869     return TRUE;
2870 }
2871
2872 /**
2873  * X11DRV_wglAllocateMemoryNV
2874  *
2875  * WGL_NV_vertex_array_range: wglAllocateMemoryNV
2876  */
2877 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
2878     TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
2879     if (pglXAllocateMemoryNV == NULL)
2880         return NULL;
2881
2882     return pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
2883 }
2884
2885 /**
2886  * X11DRV_wglFreeMemoryNV
2887  *
2888  * WGL_NV_vertex_array_range: wglFreeMemoryNV
2889  */
2890 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
2891     TRACE("(%p)\n", pointer);
2892     if (pglXFreeMemoryNV == NULL)
2893         return;
2894
2895     pglXFreeMemoryNV(pointer);
2896 }
2897
2898 /**
2899  * glxRequireVersion (internal)
2900  *
2901  * Check if the supported GLX version matches requiredVersion.
2902  */
2903 static BOOL glxRequireVersion(int requiredVersion)
2904 {
2905     /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2906     if(requiredVersion <= WineGLInfo.glxVersion[1])
2907         return TRUE;
2908
2909     return FALSE;
2910 }
2911
2912 static BOOL glxRequireExtension(const char *requiredExtension)
2913 {
2914     if (strstr(WineGLInfo.glxClientExtensions, requiredExtension) == NULL) {
2915         return FALSE;
2916     }
2917
2918     return TRUE;
2919 }
2920
2921 static void register_extension_string(const char *ext)
2922 {
2923     strcat(WineGLInfo.wglExtensions, " ");
2924     strcat(WineGLInfo.wglExtensions, ext);
2925
2926     TRACE("'%s'\n", ext);
2927 }
2928
2929 static BOOL register_extension(const WineGLExtension * ext)
2930 {
2931     int i;
2932
2933     assert( WineGLExtensionListSize < MAX_EXTENSIONS );
2934     WineGLExtensionList[WineGLExtensionListSize++] = ext;
2935
2936     strcat(WineGLInfo.wglExtensions, " ");
2937     strcat(WineGLInfo.wglExtensions, ext->extName);
2938
2939     TRACE("'%s'\n", ext->extName);
2940
2941     for (i = 0; ext->extEntryPoints[i].funcName; ++i)
2942         TRACE("    - '%s'\n", ext->extEntryPoints[i].funcName);
2943
2944     return TRUE;
2945 }
2946
2947 static const WineGLExtension WGL_internal_functions =
2948 {
2949   "",
2950   {
2951     { "wglDisable", X11DRV_wglDisable },
2952     { "wglEnable", X11DRV_wglEnable },
2953     { "wglGetIntegerv", X11DRV_wglGetIntegerv },
2954     { "wglIsEnabled", X11DRV_wglIsEnabled },
2955     { "wglScissor", X11DRV_wglScissor },
2956     { "wglViewport", X11DRV_wglViewport },
2957   }
2958 };
2959
2960
2961 static const WineGLExtension WGL_ARB_extensions_string =
2962 {
2963   "WGL_ARB_extensions_string",
2964   {
2965     { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
2966   }
2967 };
2968
2969 static const WineGLExtension WGL_ARB_make_current_read =
2970 {
2971   "WGL_ARB_make_current_read",
2972   {
2973     { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
2974     { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
2975   }
2976 };
2977
2978 static const WineGLExtension WGL_ARB_multisample =
2979 {
2980   "WGL_ARB_multisample",
2981 };
2982
2983 static const WineGLExtension WGL_ARB_pbuffer =
2984 {
2985   "WGL_ARB_pbuffer",
2986   {
2987     { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
2988     { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
2989     { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
2990     { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
2991     { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
2992     { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
2993   }
2994 };
2995
2996 static const WineGLExtension WGL_ARB_pixel_format =
2997 {
2998   "WGL_ARB_pixel_format",
2999   {
3000     { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3001     { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3002     { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3003   }
3004 };
3005
3006 static const WineGLExtension WGL_ARB_render_texture =
3007 {
3008   "WGL_ARB_render_texture",
3009   {
3010     { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3011     { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3012   }
3013 };
3014
3015 static const WineGLExtension WGL_EXT_extensions_string =
3016 {
3017   "WGL_EXT_extensions_string",
3018   {
3019     { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3020   }
3021 };
3022
3023 static const WineGLExtension WGL_EXT_swap_control =
3024 {
3025   "WGL_EXT_swap_control",
3026   {
3027     { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3028     { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3029   }
3030 };
3031
3032 static const WineGLExtension WGL_NV_vertex_array_range =
3033 {
3034   "WGL_NV_vertex_array_range",
3035   {
3036     { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3037     { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3038   }
3039 };
3040
3041 /**
3042  * X11DRV_WineGL_LoadExtensions
3043  */
3044 static void X11DRV_WineGL_LoadExtensions(void)
3045 {
3046     WineGLInfo.wglExtensions[0] = 0;
3047
3048     /* Load Wine internal functions */
3049     register_extension(&WGL_internal_functions);
3050
3051     /* ARB Extensions */
3052
3053     if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3054     {
3055         register_extension_string("WGL_ARB_pixel_format_float");
3056         register_extension_string("WGL_ATI_pixel_format_float");
3057     }
3058
3059     register_extension(&WGL_ARB_extensions_string);
3060
3061     if (glxRequireVersion(3))
3062         register_extension(&WGL_ARB_make_current_read);
3063
3064     if (glxRequireExtension("GLX_ARB_multisample"))
3065         register_extension(&WGL_ARB_multisample);
3066
3067     /* In general pbuffer functionality requires support in the X-server. The functionality is
3068      * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3069      * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3070      * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3071      *
3072      * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3073      * without support in the X-server (which other Mesa based drivers require).
3074      *
3075      * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3076      * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3077      * is available pbuffers must be available too. */
3078     if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3079         register_extension(&WGL_ARB_pbuffer);
3080
3081     register_extension(&WGL_ARB_pixel_format);
3082
3083     /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3084     if (glxRequireExtension("GLX_ATI_render_texture") ||
3085         glxRequireExtension("GLX_ARB_render_texture") ||
3086         (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3087     {
3088         register_extension(&WGL_ARB_render_texture);
3089
3090         /* The WGL version of GLX_NV_float_buffer requires render_texture */
3091         if(glxRequireExtension("GLX_NV_float_buffer"))
3092             register_extension_string("WGL_NV_float_buffer");
3093
3094         /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3095         if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3096             register_extension_string("WGL_NV_texture_rectangle");
3097     }
3098
3099     /* EXT Extensions */
3100
3101     register_extension(&WGL_EXT_extensions_string);
3102
3103     /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3104      * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3105     register_extension(&WGL_EXT_swap_control);
3106
3107     /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3108     if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3109         register_extension(&WGL_NV_vertex_array_range);
3110 }
3111
3112
3113 static XID create_glxpixmap(X11DRV_PDEVICE *physDev)
3114 {
3115     GLXPixmap ret;
3116     XVisualInfo *vis;
3117     XVisualInfo template;
3118     int num;
3119
3120     wine_tsx11_lock();
3121
3122     /* Retrieve the visualid from our main visual which is the only visual we can use */
3123     template.visualid =  XVisualIDFromVisual(visual);
3124     vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
3125
3126     ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
3127     XFree(vis);
3128     wine_tsx11_unlock(); 
3129     TRACE("return %lx\n", ret);
3130     return ret;
3131 }
3132
3133 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3134 {
3135     Drawable ret;
3136
3137     if(physDev->bitmap)
3138     {
3139         if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
3140             ret = physDev->drawable; /* PBuffer */
3141         else
3142         {
3143             if(!physDev->bitmap->glxpixmap)
3144                 physDev->bitmap->glxpixmap = create_glxpixmap(physDev);
3145             ret = physDev->bitmap->glxpixmap;
3146         }
3147     }
3148     else
3149         ret = physDev->drawable;
3150     return ret;
3151 }
3152
3153 BOOL destroy_glxpixmap(XID glxpixmap)
3154 {
3155     wine_tsx11_lock(); 
3156     pglXDestroyGLXPixmap(gdi_display, glxpixmap);
3157     wine_tsx11_unlock(); 
3158     return TRUE;
3159 }
3160
3161 /**
3162  * X11DRV_SwapBuffers
3163  *
3164  * Swap the buffers of this DC
3165  */
3166 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
3167 {
3168   GLXDrawable drawable;
3169   if (!has_opengl()) {
3170     ERR("No libGL on this box - disabling OpenGL support !\n");
3171     return 0;
3172   }
3173   
3174   TRACE_(opengl)("(%p)\n", physDev);
3175
3176   drawable = get_glxdrawable(physDev);
3177   wine_tsx11_lock();
3178   pglXSwapBuffers(gdi_display, drawable);
3179   wine_tsx11_unlock();
3180
3181   /* FPS support */
3182   if (TRACE_ON(fps))
3183   {
3184       static long prev_time, frames;
3185
3186       DWORD time = GetTickCount();
3187       frames++;
3188       /* every 1.5 seconds */
3189       if (time - prev_time > 1500) {
3190           TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
3191           prev_time = time;
3192           frames = 0;
3193       }
3194   }
3195
3196   return TRUE;
3197 }
3198
3199 /***********************************************************************
3200  *              X11DRV_setup_opengl_visual
3201  *
3202  * Setup the default visual used for OpenGL and Direct3D, and the desktop
3203  * window (if it exists).  If OpenGL isn't available, the visual is simply
3204  * set to the default visual for the display
3205  */
3206 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
3207 {
3208     XVisualInfo *visual = NULL;
3209     int i;
3210
3211     /* In order to support OpenGL or D3D, we require a double-buffered visual and stencil buffer support,
3212      * D3D and some applications can make use of aux buffers.
3213      */
3214     int visualProperties[][11] = {
3215         { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_ALPHA_SIZE, 8, GLX_AUX_BUFFERS, 1, None },
3216         { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_ALPHA_SIZE, 8, None },
3217         { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, None },
3218         { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 16, None },
3219     };
3220
3221     if (!has_opengl())
3222         return NULL;
3223
3224     wine_tsx11_lock();
3225     for (i = 0; i < sizeof(visualProperties)/sizeof(visualProperties[0]); ++i) {
3226         visual = pglXChooseVisual(display, DefaultScreen(display), visualProperties[i]);
3227         if (visual)
3228             break;
3229     }
3230     wine_tsx11_unlock();
3231
3232     if (visual)
3233         TRACE("Visual ID %lx Chosen\n", visual->visualid);
3234     else
3235         WARN("No suitable visual found\n");
3236
3237     return visual;
3238 }
3239
3240 #else  /* no OpenGL includes */
3241
3242 /***********************************************************************
3243  *              ChoosePixelFormat (X11DRV.@)
3244  */
3245 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
3246                              const PIXELFORMATDESCRIPTOR *ppfd) {
3247   ERR("No OpenGL support compiled in.\n");
3248
3249   return 0;
3250 }
3251
3252 /***********************************************************************
3253  *              DescribePixelFormat (X11DRV.@)
3254  */
3255 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
3256                                int iPixelFormat,
3257                                UINT nBytes,
3258                                PIXELFORMATDESCRIPTOR *ppfd) {
3259   ERR("No OpenGL support compiled in.\n");
3260
3261   return 0;
3262 }
3263
3264 /***********************************************************************
3265  *              GetPixelFormat (X11DRV.@)
3266  */
3267 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
3268   ERR("No OpenGL support compiled in.\n");
3269
3270   return 0;
3271 }
3272
3273 /***********************************************************************
3274  *              SetPixelFormat (X11DRV.@)
3275  */
3276 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
3277                            int iPixelFormat,
3278                            const PIXELFORMATDESCRIPTOR *ppfd) {
3279   ERR("No OpenGL support compiled in.\n");
3280
3281   return FALSE;
3282 }
3283
3284 /***********************************************************************
3285  *              SwapBuffers (X11DRV.@)
3286  */
3287 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
3288   ERR_(opengl)("No OpenGL support compiled in.\n");
3289
3290   return FALSE;
3291 }
3292
3293 /**
3294  * X11DRV_wglCreateContext
3295  *
3296  * For OpenGL32 wglCreateContext.
3297  */
3298 HGLRC X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
3299     ERR_(opengl)("No OpenGL support compiled in.\n");
3300     return NULL;
3301 }
3302
3303 /**
3304  * X11DRV_wglDeleteContext
3305  *
3306  * For OpenGL32 wglDeleteContext.
3307  */
3308 BOOL X11DRV_wglDeleteContext(HGLRC hglrc) {
3309     ERR_(opengl)("No OpenGL support compiled in.\n");
3310     return FALSE;
3311 }
3312
3313 /**
3314  * X11DRV_wglGetProcAddress
3315  *
3316  * For OpenGL32 wglGetProcAddress.
3317  */
3318 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
3319     ERR_(opengl)("No OpenGL support compiled in.\n");
3320     return NULL;
3321 }
3322
3323 HDC X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, void *hPbuffer)
3324 {
3325     ERR_(opengl)("No OpenGL support compiled in.\n");
3326     return NULL;
3327 }
3328
3329 BOOL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
3330     ERR_(opengl)("No OpenGL support compiled in.\n");
3331     return FALSE;
3332 }
3333
3334 /**
3335  * X11DRV_wglMakeCurrent
3336  *
3337  * For OpenGL32 wglMakeCurrent.
3338  */
3339 BOOL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
3340     ERR_(opengl)("No OpenGL support compiled in.\n");
3341     return FALSE;
3342 }
3343
3344 /**
3345  * X11DRV_wglShareLists
3346  *
3347  * For OpenGL32 wglShaderLists.
3348  */
3349 BOOL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
3350     ERR_(opengl)("No OpenGL support compiled in.\n");
3351     return FALSE;
3352 }
3353
3354 /**
3355  * X11DRV_wglUseFontBitmapsA
3356  *
3357  * For OpenGL32 wglUseFontBitmapsA.
3358  */
3359 BOOL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3360 {
3361     ERR_(opengl)("No OpenGL support compiled in.\n");
3362     return FALSE;
3363 }
3364
3365 /**
3366  * X11DRV_wglUseFontBitmapsW
3367  *
3368  * For OpenGL32 wglUseFontBitmapsW.
3369  */
3370 BOOL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3371 {
3372     ERR_(opengl)("No OpenGL support compiled in.\n");
3373     return FALSE;
3374 }
3375
3376 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
3377 {
3378   return NULL;
3379 }
3380
3381 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3382 {
3383     return 0;
3384 }
3385
3386 BOOL destroy_glxpixmap(XID glxpixmap)
3387 {
3388     return FALSE;
3389 }
3390
3391 #endif /* defined(HAVE_OPENGL) */