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