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