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