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