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