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