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