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