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