winex11.drv: Offset X11 coordinates with the primary monitor position.
[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 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 WINE_DECLARE_DEBUG_CHANNEL(opengl);
39
40 #if defined(HAVE_GL_GL_H) && defined(HAVE_GL_GLX_H)
41
42 #undef APIENTRY
43 #undef CALLBACK
44 #undef WINAPI
45
46 #ifdef HAVE_GL_GL_H
47 # include <GL/gl.h>
48 #endif
49 #ifdef HAVE_GL_GLX_H
50 # include <GL/glx.h>
51 #endif
52 #ifdef HAVE_GL_GLEXT_H
53 # include <GL/glext.h>
54 #endif
55
56 #include "wine/wgl.h"
57
58 #undef APIENTRY
59 #undef CALLBACK
60 #undef WINAPI
61
62 /* Redefines the constants */
63 #define CALLBACK    __stdcall
64 #define WINAPI      __stdcall
65 #define APIENTRY    WINAPI
66
67
68 WINE_DECLARE_DEBUG_CHANNEL(fps);
69
70 typedef struct wine_glcontext {
71     HDC hdc;
72     Display *display;
73     XVisualInfo *vis;
74     GLXFBConfig fb_conf;
75     GLXContext ctx;
76     BOOL do_escape;
77     struct wine_glcontext *next;
78     struct wine_glcontext *prev;
79 } Wine_GLContext;
80
81 typedef struct wine_glpbuffer {
82     Drawable   drawable;
83     Display*   display;
84     int        pixelFormat;
85     int        width;
86     int        height;
87     int*       attribList;
88     HDC        hdc;
89
90     int        use_render_texture;
91     GLuint     texture_target;
92     GLuint     texture_bind_target;
93     GLuint     texture;
94     int        texture_level;
95     HDC        prev_hdc;
96     HGLRC      prev_ctx;
97     HDC        render_hdc;
98     HGLRC      render_ctx;
99 } Wine_GLPBuffer;
100
101 typedef struct wine_glextension {
102     const char *extName;
103     struct {
104         const char *funcName;
105         void *funcAddress;
106     } extEntryPoints[8];
107 } WineGLExtension;
108
109 struct WineGLInfo {
110     const char *glVersion;
111     const char *glExtensions;
112
113     int glxVersion[2];
114
115     const char *glxServerVersion;
116     const char *glxServerExtensions;
117
118     const char *glxClientVersion;
119     const char *glxClientExtensions;
120
121     const char *glxExtensions;
122
123     BOOL glxDirect;
124     char wglExtensions[4096];
125 };
126
127 static Wine_GLContext *context_list;
128 static struct WineGLInfo WineGLInfo = { 0 };
129 static int use_render_texture_emulation = 0;
130 static int use_render_texture_ati = 0;
131 static int swap_interval = 1;
132
133 #define MAX_EXTENSIONS 16
134 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
135 static int WineGLExtensionListSize;
136
137 static void X11DRV_WineGL_LoadExtensions(void);
138
139 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
140   TRACE("  - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
141   TRACE("  - dwFlags : ");
142 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
143   TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
144   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
145   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
146   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
147   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
148   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
149   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
150   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
151   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
152   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
153   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
154   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
155   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
156   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
157   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
158   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
159 #undef TEST_AND_DUMP
160   TRACE("\n");
161
162   TRACE("  - iPixelType : ");
163   switch (ppfd->iPixelType) {
164   case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
165   case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
166   }
167   TRACE("\n");
168
169   TRACE("  - Color   : %d\n", ppfd->cColorBits);
170   TRACE("  - Red     : %d\n", ppfd->cRedBits);
171   TRACE("  - Green   : %d\n", ppfd->cGreenBits);
172   TRACE("  - Blue    : %d\n", ppfd->cBlueBits);
173   TRACE("  - Alpha   : %d\n", ppfd->cAlphaBits);
174   TRACE("  - Accum   : %d\n", ppfd->cAccumBits);
175   TRACE("  - Depth   : %d\n", ppfd->cDepthBits);
176   TRACE("  - Stencil : %d\n", ppfd->cStencilBits);
177   TRACE("  - Aux     : %d\n", ppfd->cAuxBuffers);
178
179   TRACE("  - iLayerType : ");
180   switch (ppfd->iLayerType) {
181   case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
182   case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
183   case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
184   }
185   TRACE("\n");
186 }
187
188 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient and
189    include all dependencies
190 */
191 #ifndef SONAME_LIBGL
192 #define SONAME_LIBGL "libGL.so"
193 #endif
194
195 #define PUSH1(attribs,att)        do { attribs[nAttribs++] = (att); } while (0)
196 #define PUSH2(attribs,att,value)  do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
197
198 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
199 /* GLX 1.0 */
200 MAKE_FUNCPTR(glXChooseVisual)
201 MAKE_FUNCPTR(glXCreateContext)
202 MAKE_FUNCPTR(glXCreateGLXPixmap)
203 MAKE_FUNCPTR(glXGetCurrentContext)
204 MAKE_FUNCPTR(glXDestroyContext)
205 MAKE_FUNCPTR(glXDestroyGLXPixmap)
206 MAKE_FUNCPTR(glXGetConfig)
207 MAKE_FUNCPTR(glXIsDirect)
208 MAKE_FUNCPTR(glXMakeCurrent)
209 MAKE_FUNCPTR(glXSwapBuffers)
210 MAKE_FUNCPTR(glXQueryExtension)
211 MAKE_FUNCPTR(glXQueryVersion)
212 MAKE_FUNCPTR(glXUseXFont)
213
214 /* GLX 1.1 */
215 MAKE_FUNCPTR(glXGetClientString)
216 MAKE_FUNCPTR(glXQueryExtensionsString)
217 MAKE_FUNCPTR(glXQueryServerString)
218
219 /* GLX 1.3 */
220 MAKE_FUNCPTR(glXGetFBConfigs)
221 MAKE_FUNCPTR(glXChooseFBConfig)
222 MAKE_FUNCPTR(glXCreatePbuffer)
223 MAKE_FUNCPTR(glXDestroyPbuffer)
224 MAKE_FUNCPTR(glXGetFBConfigAttrib)
225 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
226 MAKE_FUNCPTR(glXMakeContextCurrent)
227 MAKE_FUNCPTR(glXQueryDrawable)
228 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
229
230 /* GLX Extensions */
231 static void* (*pglXGetProcAddressARB)(const GLubyte *);
232 static BOOL  (*pglXBindTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
233 static BOOL  (*pglXReleaseTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
234 static BOOL  (*pglXDrawableAttribARB)(Display *dpy, GLXDrawable draw, const int *attribList);
235 static int   (*pglXSwapIntervalSGI)(int);
236
237 /* Standard OpenGL */
238 MAKE_FUNCPTR(glBindTexture)
239 MAKE_FUNCPTR(glBitmap)
240 MAKE_FUNCPTR(glCopyTexSubImage1D)
241 MAKE_FUNCPTR(glCopyTexSubImage2D)
242 MAKE_FUNCPTR(glDrawBuffer)
243 MAKE_FUNCPTR(glEndList)
244 MAKE_FUNCPTR(glGetError)
245 MAKE_FUNCPTR(glGetIntegerv)
246 MAKE_FUNCPTR(glGetString)
247 MAKE_FUNCPTR(glNewList)
248 MAKE_FUNCPTR(glPixelStorei)
249 #undef MAKE_FUNCPTR
250
251 BOOL X11DRV_WineGL_InitOpenglInfo()
252 {
253     static BOOL infoInitialized = FALSE;
254
255     int screen = DefaultScreen(gdi_display);
256     Window win = RootWindow(gdi_display, screen);
257     Visual *visual;
258     XVisualInfo template;
259     XVisualInfo *vis;
260     int num;
261     GLXContext ctx = NULL;
262
263     if (infoInitialized)
264         return TRUE;
265     infoInitialized = TRUE;
266
267     wine_tsx11_lock();
268
269     visual = DefaultVisual(gdi_display, screen);
270     template.visualid = XVisualIDFromVisual(visual);
271     vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
272     if (vis) {
273         /* Create a GLX Context. Without one we can't query GL information */
274         ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
275     }
276
277     if (ctx) {
278         pglXMakeCurrent(gdi_display, win, ctx);
279     } else {
280         ERR(" couldn't initialize OpenGL, expect problems\n");
281         wine_tsx11_unlock();
282         return FALSE;
283     }
284
285     WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
286     WineGLInfo.glExtensions = (const char *) pglGetString(GL_EXTENSIONS);
287
288     /* Get the common GLX version supported by GLX client and server ( major/minor) */
289     pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
290
291     WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
292     WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
293
294     WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
295     WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
296
297     WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
298     WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
299
300     TRACE("GL version             : %s.\n", WineGLInfo.glVersion);
301     TRACE("GLX version            : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
302     TRACE("Server GLX version     : %s.\n", WineGLInfo.glxServerVersion);
303     TRACE("Client GLX version     : %s.\n", WineGLInfo.glxClientVersion);
304     TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
305
306     if(vis) XFree(vis);
307     if(ctx) {
308         pglXMakeCurrent(gdi_display, None, NULL);    
309         pglXDestroyContext(gdi_display, ctx);
310     }
311     wine_tsx11_unlock();
312     return TRUE;
313 }
314
315 static BOOL has_opengl(void)
316 {
317     static int init_done;
318     static void *opengl_handle;
319     const char *glx_extensions;
320
321     int error_base, event_base;
322
323     if (init_done) return (opengl_handle != NULL);
324     init_done = 1;
325
326     opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
327     if (opengl_handle == NULL) return FALSE;
328
329     pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
330     if (pglXGetProcAddressARB == NULL) {
331         ERR("could not find glXGetProcAddressARB in libGL.\n");
332         return FALSE;
333     }
334
335 #define LOAD_FUNCPTR(f) if((p##f = (void*)pglXGetProcAddressARB((unsigned char*)#f)) == NULL) goto sym_not_found;
336 /* GLX 1.0 */
337 LOAD_FUNCPTR(glXChooseVisual)
338 LOAD_FUNCPTR(glXCreateContext)
339 LOAD_FUNCPTR(glXCreateGLXPixmap)
340 LOAD_FUNCPTR(glXGetCurrentContext)
341 LOAD_FUNCPTR(glXDestroyContext)
342 LOAD_FUNCPTR(glXDestroyGLXPixmap)
343 LOAD_FUNCPTR(glXGetConfig)
344 LOAD_FUNCPTR(glXIsDirect)
345 LOAD_FUNCPTR(glXMakeCurrent)
346 LOAD_FUNCPTR(glXSwapBuffers)
347 LOAD_FUNCPTR(glXQueryExtension)
348 LOAD_FUNCPTR(glXQueryVersion)
349 LOAD_FUNCPTR(glXUseXFont)
350
351 /* GLX 1.1 */
352 LOAD_FUNCPTR(glXGetClientString)
353 LOAD_FUNCPTR(glXQueryExtensionsString)
354 LOAD_FUNCPTR(glXQueryServerString)
355
356 /* GLX 1.3 */
357 LOAD_FUNCPTR(glXCreatePbuffer)
358 LOAD_FUNCPTR(glXDestroyPbuffer)
359 LOAD_FUNCPTR(glXMakeContextCurrent)
360 LOAD_FUNCPTR(glXGetCurrentReadDrawable)
361 LOAD_FUNCPTR(glXGetFBConfigs)
362
363 /* Standard OpenGL calls */
364 LOAD_FUNCPTR(glBindTexture)
365 LOAD_FUNCPTR(glBitmap)
366 LOAD_FUNCPTR(glEndList)
367 LOAD_FUNCPTR(glCopyTexSubImage1D)
368 LOAD_FUNCPTR(glCopyTexSubImage2D)
369 LOAD_FUNCPTR(glDrawBuffer)
370 LOAD_FUNCPTR(glGetError)
371 LOAD_FUNCPTR(glGetIntegerv)
372 LOAD_FUNCPTR(glGetString)
373 LOAD_FUNCPTR(glNewList)
374 LOAD_FUNCPTR(glPixelStorei)
375 #undef LOAD_FUNCPTR
376
377     if(!X11DRV_WineGL_InitOpenglInfo()) {
378         ERR("Intialization of OpenGL info failed, disabling OpenGL!\n");
379         wine_dlclose(opengl_handle, NULL, 0);
380         opengl_handle = NULL;
381         return FALSE;
382     }
383
384     wine_tsx11_lock();
385     if (pglXQueryExtension(gdi_display, &error_base, &event_base) == True) {
386         TRACE("GLX is up and running error_base = %d\n", error_base);
387     } else {
388         wine_dlclose(opengl_handle, NULL, 0);
389         opengl_handle = NULL;
390     }
391
392     /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
393      * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
394      * rendering is used.
395      *
396      * The main problem for our OpenGL code is that we need certain GLX calls but their presence
397      * depends on the reported GLX client / server version and on the client / server extension list.
398      * Those don't have to be the same.
399      *
400      * In general the server GLX information should be used in case of indirect rendering. When direct
401      * rendering is used, the OpenGL client library is responsible for which GLX calls are available.
402      * Nvidia's OpenGL drivers are the best in terms of GLX features. At the moment of writing their
403      * 8762 drivers support 1.3 for the server and 1.4 for the client and they support lots of extensions.
404      * Unfortunately it is much more complicated for Mesa/DRI-based drivers and ATI's drivers.
405      * Both sets of drivers report a server version of 1.2 and the client version can be 1.3 or 1.4.
406      * Further, in case of at least ATI's drivers, one crucial extension needed for our pixel format code
407      * is only available in the list of server extensions and not in the client list.
408      *
409      * The versioning checks below try to take into account the comments from above.
410      */
411
412     /* Depending on the use of direct or indirect rendering we need either the list of extensions
413      * exported by the client or by the server.
414      */
415     if(WineGLInfo.glxDirect)
416         glx_extensions = WineGLInfo.glxClientExtensions;
417     else
418         glx_extensions = WineGLInfo.glxServerExtensions;
419
420     /* Based on the default opengl context we decide whether direct or indirect rendering is used.
421      * In case of indirect rendering we check if the GLX version of the server is 1.2 and else
422      * the client version is checked.
423      */
424     if ((!WineGLInfo.glxDirect && !strcmp("1.2", WineGLInfo.glxServerVersion)) ||
425         (WineGLInfo.glxDirect && !strcmp("1.2", WineGLInfo.glxClientVersion)))
426     {
427         if (NULL != strstr(glx_extensions, "GLX_SGIX_fbconfig")) {
428             pglXChooseFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
429             pglXGetFBConfigAttrib = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
430             pglXGetVisualFromFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
431         } else {
432             ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxClientVersion);
433         }
434     } else {
435         pglXChooseFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
436         pglXGetFBConfigAttrib = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
437         pglXGetVisualFromFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
438     }
439
440     /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
441      * enable this function when the Xserver understand GLX 1.3 or newer
442      */
443     if (!strcmp("1.2", WineGLInfo.glxServerVersion))
444         pglXQueryDrawable = NULL;
445     else
446         pglXQueryDrawable = wine_dlsym(RTLD_DEFAULT, "glXQueryDrawable", NULL, 0);
447
448     if (NULL != strstr(glx_extensions, "GLX_ATI_render_texture")) {
449         pglXBindTexImageARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageARB");
450         pglXReleaseTexImageARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageARB");
451         pglXDrawableAttribARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribARB");
452     }
453
454     X11DRV_WineGL_LoadExtensions();
455
456     wine_tsx11_unlock();
457     return (opengl_handle != NULL);
458
459 sym_not_found:
460     wine_dlclose(opengl_handle, NULL, 0);
461     opengl_handle = NULL;
462     return FALSE;
463 }
464
465 static inline Wine_GLContext *alloc_context(void)
466 {
467     Wine_GLContext *ret;
468
469     if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
470     {
471         ret->next = context_list;
472         if (context_list) context_list->prev = ret;
473         context_list = ret;
474     }
475     return ret;
476 }
477
478 static inline void free_context(Wine_GLContext *context)
479 {
480     if (context->next != NULL) context->next->prev = context->prev;
481     if (context->prev != NULL) context->prev->next = context->next;
482     else context_list = context->next;
483
484     HeapFree(GetProcessHeap(), 0, context);
485 }
486
487 static inline Wine_GLContext *get_context_from_GLXContext(GLXContext ctx)
488 {
489     Wine_GLContext *ret;
490     if (!ctx) return NULL;
491     for (ret = context_list; ret; ret = ret->next) if (ctx == ret->ctx) break;
492     return ret;
493 }
494
495 /* retrieve the GLX drawable to use on a given DC */
496 inline static Drawable get_drawable( HDC hdc )
497 {
498     GLXDrawable drawable;
499     enum x11drv_escape_codes escape = X11DRV_GET_GLX_DRAWABLE;
500
501     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
502                     sizeof(drawable), (LPSTR)&drawable )) drawable = 0;
503     return drawable;
504 }
505
506 inline static void set_drawable( HDC hdc, Drawable drawable )
507 {
508     struct x11drv_escape_set_drawable escape;
509
510     escape.code = X11DRV_SET_DRAWABLE;
511     escape.drawable = drawable;
512     escape.mode = IncludeInferiors;
513     escape.org.x = escape.org.y = 0;
514     escape.drawable_org.x = escape.drawable_org.y = 0;
515
516     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape, 0, NULL );
517 }
518
519 /** for use of wglGetCurrentReadDCARB */
520 inline static HDC get_hdc_from_Drawable(GLXDrawable d)
521 {
522     Wine_GLContext *ret;
523     for (ret = context_list; ret; ret = ret->next) {
524         if (d == get_drawable( ret->hdc )) {
525             return ret->hdc;
526         }
527     }
528     return NULL;
529 }
530
531 inline static BOOL is_valid_context( Wine_GLContext *ctx )
532 {
533     Wine_GLContext *ptr;
534     for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
535     return (ptr != NULL);
536 }
537
538 static int describeContext(Wine_GLContext* ctx) {
539     int tmp;
540     int ctx_vis_id;
541     TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
542     pglXGetFBConfigAttrib(ctx->display, ctx->fb_conf, GLX_FBCONFIG_ID, &tmp);
543     TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
544     pglXGetFBConfigAttrib(ctx->display, ctx->fb_conf, GLX_VISUAL_ID, &tmp);
545     TRACE(" - VISUAL_ID 0x%x\n", tmp);
546     ctx_vis_id = tmp;
547     return ctx_vis_id;
548 }
549
550 static int describeDrawable(Wine_GLContext* ctx, Drawable drawable) {
551     int tmp;
552     int nElements;
553     int attribList[3] = { GLX_FBCONFIG_ID, 0, None };
554     GLXFBConfig *fbCfgs;
555
556     if (pglXQueryDrawable == NULL)  {
557         /** glXQueryDrawable not available so returns not supported */
558         return -1;
559     }
560
561     TRACE(" Drawable %p have :\n", (void*) drawable);
562     pglXQueryDrawable(ctx->display, drawable, GLX_WIDTH, (unsigned int*) &tmp);
563     TRACE(" - WIDTH as %d\n", tmp);
564     pglXQueryDrawable(ctx->display, drawable, GLX_HEIGHT, (unsigned int*) &tmp);
565     TRACE(" - HEIGHT as %d\n", tmp);
566     pglXQueryDrawable(ctx->display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &tmp);
567     TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
568
569     attribList[1] = tmp;
570     fbCfgs = pglXChooseFBConfig(ctx->display, DefaultScreen(ctx->display), attribList, &nElements);
571     if (fbCfgs == NULL) {
572         return -1;
573     }
574
575     pglXGetFBConfigAttrib(ctx->display, fbCfgs[0], GLX_VISUAL_ID, &tmp);
576     TRACE(" - VISUAL_ID as 0x%x\n", tmp);
577
578     XFree(fbCfgs);
579
580     return tmp;
581 }
582
583 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
584   int nAttribs = 0;
585   unsigned cur = 0; 
586   int pop;
587   int isColor = 0;
588   int wantColorBits = 0;
589   int sz_alpha = 0;
590
591   while (0 != iWGLAttr[cur]) {
592     TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
593
594     switch (iWGLAttr[cur]) {
595     case WGL_COLOR_BITS_ARB:
596       pop = iWGLAttr[++cur];
597       wantColorBits = pop; /** see end */
598       break;
599     case WGL_BLUE_BITS_ARB:
600       pop = iWGLAttr[++cur];
601       PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
602       TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
603       break;
604     case WGL_RED_BITS_ARB:
605       pop = iWGLAttr[++cur];
606       PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
607       TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
608       break;
609     case WGL_GREEN_BITS_ARB:
610       pop = iWGLAttr[++cur];
611       PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
612       TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
613       break;
614     case WGL_ALPHA_BITS_ARB:
615       pop = iWGLAttr[++cur];
616       sz_alpha = pop;
617       PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
618       TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
619       break;
620     case WGL_DEPTH_BITS_ARB:
621       pop = iWGLAttr[++cur];
622       PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
623       TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
624       break;
625     case WGL_STENCIL_BITS_ARB:
626       pop = iWGLAttr[++cur];
627       PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
628       TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
629       break;
630     case WGL_DOUBLE_BUFFER_ARB:
631       pop = iWGLAttr[++cur];
632       PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
633       TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
634       break;
635
636     case WGL_PIXEL_TYPE_ARB:
637       pop = iWGLAttr[++cur];
638       switch (pop) {
639       case WGL_TYPE_COLORINDEX_ARB: pop = GLX_COLOR_INDEX_BIT; isColor = 1; break ;
640       case WGL_TYPE_RGBA_ARB: pop = GLX_RGBA_BIT; break ;
641       case WGL_TYPE_RGBA_FLOAT_ATI: pop = GLX_RGBA_FLOAT_ATI_BIT; break ;
642       default:
643         ERR("unexpected PixelType(%x)\n", pop); 
644         pop = 0;
645       }
646       PUSH2(oGLXAttr, GLX_RENDER_TYPE, pop);
647       TRACE("pAttr[%d] = GLX_RENDER_TYPE: %d\n", cur, pop);
648       break;
649
650     case WGL_SUPPORT_GDI_ARB:
651       pop = iWGLAttr[++cur];
652       PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
653       TRACE("pAttr[%d] = GLX_RENDERABLE: %d\n", cur, pop);
654       break;
655
656     case WGL_DRAW_TO_BITMAP_ARB:
657       pop = iWGLAttr[++cur];
658       PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
659       TRACE("pAttr[%d] = GLX_RENDERABLE: %d\n", cur, pop);
660       if (pop) {
661         PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT);
662         TRACE("pAttr[%d] = GLX_DRAWABLE_TYPE: GLX_PIXMAP_BIT\n", cur);
663       }
664       break;
665
666     case WGL_DRAW_TO_WINDOW_ARB:
667       pop = iWGLAttr[++cur];
668       if (pop) {
669         PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
670         TRACE("pAttr[%d] = GLX_DRAWABLE_TYPE: GLX_WINDOW_BIT\n", cur);
671       }
672       break;
673
674     case WGL_DRAW_TO_PBUFFER_ARB:
675       pop = iWGLAttr[++cur];
676       PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
677       TRACE("pAttr[%d] = GLX_RENDERABLE: %d\n", cur, pop);
678       if (pop) {
679         PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT);
680         TRACE("pAttr[%d] = GLX_DRAWABLE_TYPE: GLX_PBUFFER_BIT\n", cur);
681       }
682       break;
683
684     case WGL_ACCELERATION_ARB:
685     case WGL_SUPPORT_OPENGL_ARB:
686       pop = iWGLAttr[++cur];
687       /** nothing to do, if we are here, supposing support Accelerated OpenGL */
688       TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
689       break;
690
691     case WGL_PBUFFER_LARGEST_ARB:
692       pop = iWGLAttr[++cur];
693       PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
694       TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
695       break;
696
697     case WGL_SAMPLE_BUFFERS_ARB:
698       pop = iWGLAttr[++cur];
699       PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
700       TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
701       break;
702
703     case WGL_SAMPLES_ARB:
704       pop = iWGLAttr[++cur];
705       PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
706       TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
707       break;
708
709     case WGL_TEXTURE_FORMAT_ARB:
710     case WGL_TEXTURE_TARGET_ARB:
711     case WGL_MIPMAP_TEXTURE_ARB:
712       TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
713       pop = iWGLAttr[++cur];
714       if (NULL == pbuf) {
715         ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
716       }
717       if (use_render_texture_ati) {
718         /** nothing to do here */
719       }
720       else if (!use_render_texture_emulation) {
721         if (WGL_NO_TEXTURE_ARB != pop) {
722           ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
723           return -1; /** error: don't support it */
724         } else {
725           PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
726           PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT);
727         }
728       }
729       break ;
730
731     case WGL_BIND_TO_TEXTURE_RGB_ARB:
732     case WGL_BIND_TO_TEXTURE_RGBA_ARB:
733       pop = iWGLAttr[++cur];
734       /** cannot be converted, see direct handling on 
735        *   - wglGetPixelFormatAttribivARB
736        *  TODO: wglChoosePixelFormat
737        */
738       break ;
739
740     default:
741       FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
742       break;
743     }
744     ++cur;
745   }
746
747   /**
748    * Trick as WGL_COLOR_BITS_ARB != GLX_BUFFER_SIZE
749    *    WGL_COLOR_BITS_ARB + WGL_ALPHA_BITS_ARB == GLX_BUFFER_SIZE
750    *
751    *  WGL_COLOR_BITS_ARB
752    *     The number of color bitplanes in each color buffer. For RGBA
753    *     pixel types, it is the size of the color buffer, excluding the
754    *     alpha bitplanes. For color-index pixels, it is the size of the
755    *     color index buffer.
756    *
757    *  GLX_BUFFER_SIZE   
758    *     This attribute defines the number of bits per color buffer. 
759    *     For GLX FBConfigs that correspond to a PseudoColor or StaticColor visual, 
760    *     this is equal to the depth value reported in the X11 visual. 
761    *     For GLX FBConfigs that correspond to TrueColor or DirectColor visual, 
762    *     this is the sum of GLX_RED_SIZE, GLX_GREEN_SIZE, GLX_BLUE_SIZE, and GLX_ALPHA_SIZE.
763    * 
764    */
765   if (0 < wantColorBits) {
766     if (!isColor) { 
767       wantColorBits += sz_alpha; 
768     }
769     if (32 < wantColorBits) {
770       ERR("buggy %d GLX_BUFFER_SIZE default to 32\n", wantColorBits);
771       wantColorBits = 32;
772     }
773     PUSH2(oGLXAttr, GLX_BUFFER_SIZE, wantColorBits);
774     TRACE("pAttr[%d] = WGL_COLOR_BITS_ARB: %d\n", cur, wantColorBits);
775   }
776
777   return nAttribs;
778 }
779
780 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
781  * In our WGL implementation we only support a subset of these formats namely the format of
782  * Wine's main visual and offscreen formats (if they are available).
783  * This function converts a WGL format to its corresponding GLX one. It returns the index (zero-based)
784  * into the GLX FB config table and it returns the number of supported WGL formats in fmt_count.
785  */
786 static BOOL ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, int *fmt_index, int *fmt_count)
787 {
788   int res = FALSE;
789   int i = 0;
790   GLXFBConfig* cfgs = NULL;
791   int nCfgs = 0;
792   int tmp_fmt_id = 0;
793   int tmp_vis_id = 0;
794   int nFormats = 1; /* Start at 1 as we always have a main visual */
795   VisualID visualid = 0;
796
797   /* Request to look up the format of the main visual when iPixelFormat = 1 */
798   if(iPixelFormat == 1) visualid = XVisualIDFromVisual(visual);
799
800   /* As mentioned in various parts of the code only the format of the main visual can be used for onscreen rendering.
801    * Next to this format there are also so called offscreen rendering formats (used for pbuffers) which can be supported
802    * because they don't need a visual. Below we use glXGetFBConfigs instead of glXChooseFBConfig to enumerate the fb configurations
803    * bas this call lists both types of formats instead of only onscreen ones. */
804   cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
805   if (NULL == cfgs || 0 == nCfgs) {
806     ERR("glXChooseFBConfig returns NULL\n");
807     if(cfgs != NULL) XFree(cfgs);
808     return FALSE;
809   }
810
811   /* Find the requested offscreen format and count the number of offscreen formats */
812   for(i=0; i<nCfgs; i++) {
813     pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
814     pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &tmp_fmt_id);
815
816     /* We are looking up the GLX index of our main visual and have found it :) */
817     if(iPixelFormat == 1 && visualid == tmp_vis_id) {
818       *fmt_index = i;
819       TRACE("Found FBCONFIG_ID 0x%x at index %d for VISUAL_ID 0x%x\n", tmp_fmt_id, *fmt_index, tmp_vis_id);
820       res = TRUE;
821     }
822     /* We found an offscreen rendering format :) */
823     else if(tmp_vis_id == 0) {
824       nFormats++;
825       TRACE("Checking offscreen format FBCONFIG_ID 0x%x at index %d\n", tmp_fmt_id, i);
826
827       if(iPixelFormat == nFormats) {
828         *fmt_index = i;
829         TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", tmp_fmt_id, iPixelFormat, i);
830         res = TRUE;
831       }
832     }
833   }
834   *fmt_count = nFormats;
835   TRACE("Number of offscreen formats: %d; returning index: %d\n", *fmt_count, *fmt_index);
836
837   if(cfgs != NULL) XFree(cfgs);
838
839   if(res == FALSE && iPixelFormat == 1)
840     ERR("Can't find a matching FBCONFIG_ID for VISUAL_ID 0x%lx!\n", visualid);
841
842   return res;
843 }
844
845 /**
846  * X11DRV_ChoosePixelFormat
847  *
848  * Equivalent of glXChooseVisual
849  */
850 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev, 
851                              const PIXELFORMATDESCRIPTOR *ppfd) {
852   GLXFBConfig* cfgs = NULL;
853   int ret = 0;
854   int nCfgs = 0;
855   int value = 0;
856   int fmt_index = 0;
857
858   if (!has_opengl()) {
859     ERR("No libGL on this box - disabling OpenGL support !\n");
860     return 0;
861   }
862
863   if (TRACE_ON(opengl)) {
864     TRACE("(%p,%p)\n", physDev, ppfd);
865
866     dump_PIXELFORMATDESCRIPTOR((const PIXELFORMATDESCRIPTOR *) ppfd);
867   }
868
869   wine_tsx11_lock(); 
870   if(!visual) {
871     ERR("Can't get an opengl visual!\n");
872     goto choose_exit;
873   }
874
875   /* Get a list containing all supported FB configurations */
876   cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), NULL, &nCfgs);
877   if (NULL == cfgs || 0 == nCfgs) {
878     ERR("glXChooseFBConfig returns NULL (glError: %d)\n", pglGetError());
879     goto choose_exit;
880   }
881
882   /* In case an fbconfig was found, check if it matches to the requirements of the ppfd */
883   if(!ConvertPixelFormatWGLtoGLX(gdi_display, 1 /* main visual */, &fmt_index, &value)) {
884     ERR("Can't find a matching FBCONFIG_ID for VISUAL_ID 0x%lx!\n", visual->visualid);
885   } else {
886     int dwFlags = 0;
887     int iPixelType = 0;
888     int value = 0;
889
890     /* Pixel type */
891     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_RENDER_TYPE, &value);
892     if (value & GLX_RGBA_BIT)
893       iPixelType = PFD_TYPE_RGBA;
894     else
895       iPixelType = PFD_TYPE_COLORINDEX;
896
897     if (ppfd->iPixelType != iPixelType) {
898       goto choose_exit;
899     }
900
901     /* Doublebuffer */
902     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_DOUBLEBUFFER, &value); if (value) dwFlags |= PFD_DOUBLEBUFFER;
903     if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE)) {
904       if ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != (dwFlags & PFD_DOUBLEBUFFER)) {
905         goto choose_exit;
906       }
907     }
908
909     /* Stereo */
910     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_STEREO, &value); if (value) dwFlags |= PFD_STEREO;
911     if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE)) {
912       if ((ppfd->dwFlags & PFD_STEREO) != (dwFlags & PFD_STEREO)) {
913         goto choose_exit;
914       }
915     }
916
917     /* Alpha bits */
918     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_ALPHA_SIZE, &value);
919     if (ppfd->iPixelType==PFD_TYPE_RGBA && ppfd->cAlphaBits && !value) {
920       goto choose_exit;
921     }
922
923     /* Depth bits */
924     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_DEPTH_SIZE, &value);
925     if (ppfd->cDepthBits && !value) {
926       goto choose_exit;
927     }
928
929     /* Stencil bits */
930     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_STENCIL_SIZE, &value);
931     if (ppfd->cStencilBits && !value) {
932       goto choose_exit;
933     }
934
935     /* Aux buffers */
936     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_AUX_BUFFERS, &value);
937     if (ppfd->cAuxBuffers && !value) {
938       goto choose_exit;
939     }
940
941     /* When we pass all the checks we have found a matching format :) */
942     ret = 1;
943     TRACE("Successfully found a matching mode, returning index: %d\n", ret);
944   }
945
946 choose_exit:
947   if(!ret)
948     TRACE("No matching mode was found returning 0\n");
949
950   if (NULL != cfgs) XFree(cfgs);
951   wine_tsx11_unlock();
952   return ret;
953 }
954
955 /**
956  * X11DRV_DescribePixelFormat
957  *
958  * Get the pixel-format descriptor associated to the given id
959  */
960 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
961                                int iPixelFormat,
962                                UINT nBytes,
963                                PIXELFORMATDESCRIPTOR *ppfd) {
964   /*XVisualInfo *vis;*/
965   int value;
966   int rb,gb,bb,ab;
967
968   GLXFBConfig* cfgs = NULL;
969   GLXFBConfig cur;
970   int nCfgs = 0;
971   int ret = 0;
972   int fmt_index = 0;
973
974   if (!has_opengl()) {
975     ERR("No libGL on this box - disabling OpenGL support !\n");
976     return 0;
977   }
978   
979   TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
980
981   wine_tsx11_lock();
982   cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
983   wine_tsx11_unlock();
984
985   if (NULL == cfgs || 0 == nCfgs) {
986     ERR("unexpected iPixelFormat(%d), returns NULL\n", iPixelFormat);
987     return 0; /* unespected error */
988   }
989
990   /* This function always reports the total number of supported pixel formats.
991    * At the moment we only support the pixel format corresponding to the main
992    * visual which got created at x11drv initialization. More formats could be
993    * supported if there was a way to recreate x11 windows in x11drv. 
994    * Because we only support one format nCfgs needs to be set to 1.
995    */
996   nCfgs = 1;
997
998   if (ppfd == NULL) {
999     /* The application is only querying the number of visuals */
1000     wine_tsx11_lock();
1001     if (NULL != cfgs) XFree(cfgs);
1002     wine_tsx11_unlock();
1003     return nCfgs;
1004   }
1005
1006   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1007     ERR("Wrong structure size !\n");
1008     /* Should set error */
1009     return 0;
1010   }
1011
1012   if (nCfgs < iPixelFormat || 1 > iPixelFormat) {
1013     WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL\n", iPixelFormat, nCfgs);
1014     return 0;
1015   }
1016
1017   /* Retrieve the index in the FBConfig table corresponding to the visual ID from the main visual */
1018   if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &value)) {
1019       ERR("Can't find a valid pixel format index from the main visual, expect problems!\n");
1020       return 0;
1021   }
1022
1023   ret = nCfgs;
1024   cur = cfgs[fmt_index];
1025
1026   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1027   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1028   ppfd->nVersion = 1;
1029
1030   /* These flags are always the same... */
1031   ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
1032   /* Now the flags extracted from the Visual */
1033
1034   wine_tsx11_lock();
1035
1036   pglXGetFBConfigAttrib(gdi_display, cur, GLX_CONFIG_CAVEAT, &value);
1037   if(value == GLX_SLOW_CONFIG)
1038       ppfd->dwFlags |= PFD_GENERIC_ACCELERATED;
1039
1040   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1041   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1042
1043   /* Pixel type */
1044   pglXGetFBConfigAttrib(gdi_display, cur, GLX_RENDER_TYPE, &value);
1045   if (value & GLX_RGBA_BIT)
1046     ppfd->iPixelType = PFD_TYPE_RGBA;
1047   else
1048     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1049
1050   /* Color bits */
1051   pglXGetFBConfigAttrib(gdi_display, cur, GLX_BUFFER_SIZE, &value);
1052   ppfd->cColorBits = value;
1053
1054   /* Red, green, blue and alpha bits / shifts */
1055   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1056     pglXGetFBConfigAttrib(gdi_display, cur, GLX_RED_SIZE, &rb);
1057     pglXGetFBConfigAttrib(gdi_display, cur, GLX_GREEN_SIZE, &gb);
1058     pglXGetFBConfigAttrib(gdi_display, cur, GLX_BLUE_SIZE, &bb);
1059     pglXGetFBConfigAttrib(gdi_display, cur, GLX_ALPHA_SIZE, &ab);
1060
1061     ppfd->cRedBits = rb;
1062     ppfd->cRedShift = gb + bb + ab;
1063     ppfd->cBlueBits = bb;
1064     ppfd->cBlueShift = ab;
1065     ppfd->cGreenBits = gb;
1066     ppfd->cGreenShift = bb + ab;
1067     ppfd->cAlphaBits = ab;
1068     ppfd->cAlphaShift = 0;
1069   } else {
1070     ppfd->cRedBits = 0;
1071     ppfd->cRedShift = 0;
1072     ppfd->cBlueBits = 0;
1073     ppfd->cBlueShift = 0;
1074     ppfd->cGreenBits = 0;
1075     ppfd->cGreenShift = 0;
1076     ppfd->cAlphaBits = 0;
1077     ppfd->cAlphaShift = 0;
1078   }
1079   /* Accums : to do ... */
1080
1081   /* Depth bits */
1082   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DEPTH_SIZE, &value);
1083   ppfd->cDepthBits = value;
1084
1085   /* stencil bits */
1086   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STENCIL_SIZE, &value);
1087   ppfd->cStencilBits = value;
1088
1089   wine_tsx11_unlock();
1090
1091   /* Aux : to do ... */
1092
1093   ppfd->iLayerType = PFD_MAIN_PLANE;
1094
1095   if (TRACE_ON(opengl)) {
1096     dump_PIXELFORMATDESCRIPTOR(ppfd);
1097   }
1098
1099   wine_tsx11_lock();
1100   if (NULL != cfgs) XFree(cfgs);
1101   wine_tsx11_unlock();
1102
1103   return ret;
1104 }
1105
1106 /**
1107  * X11DRV_GetPixelFormat
1108  *
1109  * Get the pixel-format id used by this DC
1110  */
1111 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1112   TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1113
1114   return physDev->current_pf;
1115 }
1116
1117 /**
1118  * X11DRV_SetPixelFormat
1119  *
1120  * Set the pixel-format id used by this DC
1121  */
1122 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1123                            int iPixelFormat,
1124                            const PIXELFORMATDESCRIPTOR *ppfd) {
1125   TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1126
1127   /* At the moment we only support the pixelformat corresponding to the main
1128    * x11drv visual which got created at x11drv initialization. More formats
1129    * can be supported if there was a way to recreate x11 windows in x11drv
1130    */
1131   if(iPixelFormat != 1) {
1132     TRACE("Invalid iPixelFormat: %d\n", iPixelFormat);
1133     return 0;
1134   }
1135
1136   physDev->current_pf = iPixelFormat;
1137
1138   if (TRACE_ON(opengl)) {
1139     int nCfgs_fmt = 0;
1140     GLXFBConfig* cfgs_fmt = NULL;
1141     GLXFBConfig cur_cfg;
1142     int value;
1143     int gl_test = 0;
1144     int fmt_index = 0;
1145
1146     if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &value)) {
1147       ERR("Can't find a valid pixel format index from the main visual, expect problems!\n");
1148       return TRUE; /* Return true because the SetPixelFormat stuff itself passed */
1149     }
1150
1151     /*
1152      * How to test if hdc current drawable is compatible (visual/FBConfig) ?
1153      *
1154      * in case of root window created HDCs we crash here :(
1155      *
1156     Drawable drawable =  get_drawable( physDev->hdc );
1157     TRACE(" drawable (%p,%p) have :\n", drawable, root_window);
1158     pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &value);
1159     TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
1160     pglXQueryDrawable(gdi_display, drawable, GLX_VISUAL_ID, (unsigned int*) &value);
1161     TRACE(" - VISUAL_ID as 0x%x\n", tmp);
1162     pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &value);
1163     TRACE(" - WIDTH as %d\n", tmp);
1164     pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &value);
1165     TRACE(" - HEIGHT as %d\n", tmp);
1166     */
1167     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
1168     cur_cfg = cfgs_fmt[fmt_index];
1169     gl_test = pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_FBCONFIG_ID, &value);
1170     if (gl_test) {
1171       ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1172     } else {
1173       TRACE(" FBConfig have :\n");
1174       TRACE(" - FBCONFIG_ID   0x%x\n", value);
1175       pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_VISUAL_ID, &value);
1176       TRACE(" - VISUAL_ID     0x%x\n", value);
1177       pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_DRAWABLE_TYPE, &value);
1178       TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1179     }
1180     XFree(cfgs_fmt);
1181   }
1182   return TRUE;
1183 }
1184
1185 /* OpenGL32 wglCreateContext */
1186 HGLRC X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1187 {
1188     Wine_GLContext *ret;
1189     GLXFBConfig* cfgs_fmt = NULL;
1190     GLXFBConfig cur_cfg;
1191     int hdcPF = 1; /* We can only use the Wine's main visual which has an index of 1 */
1192     int tmp = 0;
1193     int fmt_index = 0;
1194     int nCfgs_fmt = 0;
1195     int value = 0;
1196     int gl_test = 0;
1197     HDC hdc = physDev->hdc;
1198
1199     TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1200
1201     /* First, get the visual in use by the X11DRV */
1202     if (!gdi_display) return 0;
1203
1204     /* We can only render using the iPixelFormat (1) of Wine's Main visual, we need to get the correspondig GLX format.
1205     * If this fails something is very wrong on the system. */
1206     if(!ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, &fmt_index, &tmp)) {
1207         ERR("Cannot get FB Config for main iPixelFormat 1, expect problems!\n");
1208         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1209         return NULL;
1210     }
1211
1212     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
1213     if (NULL == cfgs_fmt || 0 == nCfgs_fmt) {
1214         ERR("Cannot get FB Configs, expect problems.\n");
1215         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1216         return NULL;
1217     }
1218
1219     if (nCfgs_fmt < fmt_index) {
1220         ERR("(%p): unexpected pixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, fmt_index, nCfgs_fmt);
1221         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1222         return NULL;
1223     }
1224
1225     cur_cfg = cfgs_fmt[fmt_index];
1226     gl_test = pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_FBCONFIG_ID, &value);
1227     if (gl_test) {
1228         ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1229         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1230         return NULL;
1231     }
1232     XFree(cfgs_fmt);
1233
1234     /* The context will be allocated in the wglMakeCurrent call */
1235     wine_tsx11_lock();
1236     ret = alloc_context();
1237     wine_tsx11_unlock();
1238     ret->hdc = hdc;
1239     ret->display = gdi_display;
1240     ret->fb_conf = cur_cfg;
1241     /*ret->vis = vis;*/
1242     ret->vis = pglXGetVisualFromFBConfig(gdi_display, cur_cfg);
1243
1244     TRACE(" creating context %p (GL context creation delayed)\n", ret);
1245     return (HGLRC) ret;
1246 }
1247
1248 /* OpenGL32 wglDeleteContext */
1249 BOOL WINAPI X11DRV_wglDeleteContext(HGLRC hglrc)
1250 {
1251     Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1252     BOOL ret = TRUE;
1253
1254     TRACE("(%p)\n", hglrc);
1255
1256     wine_tsx11_lock();
1257     /* A game (Half Life not to name it) deletes twice the same context,
1258     * so make sure it is valid first */
1259     if (is_valid_context( ctx ))
1260     {
1261         if (ctx->ctx) pglXDestroyContext(ctx->display, ctx->ctx);
1262         free_context(ctx);
1263     }
1264     else
1265     {
1266         WARN("Error deleting context !\n");
1267         SetLastError(ERROR_INVALID_HANDLE);
1268         ret = FALSE;
1269     }
1270     wine_tsx11_unlock();
1271
1272     return ret;
1273 }
1274
1275 /* OpenGL32 wglGetCurrentReadDCARB */
1276 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void) 
1277 {
1278     GLXDrawable gl_d;
1279     HDC ret;
1280
1281     TRACE("()\n");
1282
1283     wine_tsx11_lock();
1284     gl_d = pglXGetCurrentReadDrawable();
1285     ret = get_hdc_from_Drawable(gl_d);
1286     wine_tsx11_unlock();
1287
1288     TRACE(" returning %p (GL drawable %lu)\n", ret, gl_d);
1289     return ret;
1290 }
1291
1292 /* OpenGL32: wglGetProcAddress */
1293 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1294 {
1295     int i, j;
1296     const WineGLExtension *ext;
1297
1298     int padding = 32 - strlen(lpszProc);
1299     if (padding < 0)
1300         padding = 0;
1301
1302     TRACE("('%s'):%*s", lpszProc, padding, " ");
1303     for (i = 0; i < WineGLExtensionListSize; ++i) {
1304         ext = WineGLExtensionList[i];
1305         for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1306             if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1307                 TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1308                 return ext->extEntryPoints[j].funcAddress;
1309             }
1310         }
1311     }
1312
1313     ERR("(%s) - not found\n", lpszProc);
1314
1315     return NULL;
1316 }
1317
1318
1319 /* OpenGL32 wglMakeCurrent */
1320 BOOL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1321     BOOL ret;
1322     HDC hdc = physDev->hdc;
1323     DWORD type = GetObjectType(hdc);
1324
1325     TRACE("(%p,%p)\n", hdc, hglrc);
1326
1327     wine_tsx11_lock();
1328     if (hglrc == NULL) {
1329         ret = pglXMakeCurrent(gdi_display, None, NULL);
1330         NtCurrentTeb()->glContext = NULL;
1331     } else {
1332         Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1333         Drawable drawable = physDev->drawable;
1334         if (ctx->ctx == NULL) {
1335             int draw_vis_id, ctx_vis_id;
1336             VisualID visualid = (VisualID)GetPropA( GetDesktopWindow(), "__wine_x11_visual_id" );
1337             TRACE(" Wine desktop VISUAL_ID is 0x%x\n", (unsigned int) visualid);
1338             draw_vis_id = describeDrawable(ctx, drawable);
1339             ctx_vis_id = describeContext(ctx);
1340
1341             if (-1 == draw_vis_id || (draw_vis_id == visualid && draw_vis_id != ctx_vis_id)) {
1342                 /**
1343                 * Inherits from root window so reuse desktop visual
1344                 */
1345                 XVisualInfo template;
1346                 XVisualInfo *vis;
1347                 int num;
1348                 template.visualid = visualid;
1349                 vis = XGetVisualInfo(ctx->display, VisualIDMask, &template, &num);
1350
1351                 TRACE(" Creating GLX Context\n");
1352                 ctx->ctx = pglXCreateContext(ctx->display, vis, NULL, type == OBJ_MEMDC ? False : True);
1353             } else {
1354                 TRACE(" Creating GLX Context\n");
1355                 ctx->ctx = pglXCreateContext(ctx->display, ctx->vis, NULL, type == OBJ_MEMDC ? False : True);
1356             }
1357             TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1358         }
1359         TRACE(" make current for dis %p, drawable %p, ctx %p\n", ctx->display, (void*) drawable, ctx->ctx);
1360         ret = pglXMakeCurrent(ctx->display, drawable, ctx->ctx);
1361         NtCurrentTeb()->glContext = ctx;
1362         if(ret && type == OBJ_MEMDC)
1363         {
1364             ctx->do_escape = TRUE;
1365             pglDrawBuffer(GL_FRONT_LEFT);
1366         }
1367     }
1368     wine_tsx11_unlock();
1369     TRACE(" returning %s\n", (ret ? "True" : "False"));
1370     return ret;
1371 }
1372
1373 /* OpenGL32 wglMakeContextCurrentARB */
1374 static BOOL WINAPI X11DRV_wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc) 
1375 {
1376     BOOL ret;
1377     TRACE("(%p,%p,%p)\n", hDrawDC, hReadDC, hglrc);
1378
1379     wine_tsx11_lock();
1380     if (hglrc == NULL) {
1381         ret = pglXMakeCurrent(gdi_display, None, NULL);
1382         NtCurrentTeb()->glContext = NULL;
1383     } else {
1384         if (NULL == pglXMakeContextCurrent) {
1385             ret = FALSE;
1386         } else {
1387             Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1388             Drawable d_draw = get_drawable( hDrawDC );
1389             Drawable d_read = get_drawable( hReadDC );
1390
1391             if (ctx->ctx == NULL) {
1392                 ctx->ctx = pglXCreateContext(ctx->display, ctx->vis, NULL, GetObjectType(hDrawDC) == OBJ_MEMDC ? False : True);
1393                 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1394             }
1395             ret = pglXMakeContextCurrent(ctx->display, d_draw, d_read, ctx->ctx);
1396             NtCurrentTeb()->glContext = ctx;
1397         }
1398     }
1399     wine_tsx11_unlock();
1400
1401     TRACE(" returning %s\n", (ret ? "True" : "False"));
1402     return ret;
1403 }
1404
1405 /* OpenGL32 wglShaderLists */
1406 BOOL WINAPI X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1407     Wine_GLContext *org  = (Wine_GLContext *) hglrc1;
1408     Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1409
1410     TRACE("(%p, %p)\n", org, dest);
1411
1412     if (NULL != dest && dest->ctx != NULL) {
1413         ERR("Could not share display lists, context already created !\n");
1414         return FALSE;
1415     } else {
1416         if (org->ctx == NULL) {
1417             wine_tsx11_lock();
1418             describeContext(org);
1419             org->ctx = pglXCreateContext(org->display, org->vis, NULL, GetObjectType(org->hdc) == OBJ_MEMDC ? False : True);
1420             wine_tsx11_unlock();
1421             TRACE(" created a delayed OpenGL context (%p) for Wine context %p\n", org->ctx, org);
1422         }
1423         if (NULL != dest) {
1424             wine_tsx11_lock();
1425             describeContext(dest);
1426             /* Create the destination context with display lists shared */
1427             dest->ctx = pglXCreateContext(org->display, dest->vis, org->ctx, GetObjectType(org->hdc) == OBJ_MEMDC ? False : True);
1428             wine_tsx11_unlock();
1429             TRACE(" created a delayed OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1430             return TRUE;
1431         }
1432     }
1433     return FALSE;
1434 }
1435
1436 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1437 {
1438      /* We are running using client-side rendering fonts... */
1439      GLYPHMETRICS gm;
1440      unsigned int glyph;
1441      int size = 0;
1442      void *bitmap = NULL, *gl_bitmap = NULL;
1443      int org_alignment;
1444
1445      wine_tsx11_lock();
1446      pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1447      pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1448      wine_tsx11_unlock();
1449
1450      for (glyph = first; glyph < first + count; glyph++) {
1451          unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, NULL);
1452          int height, width_int;
1453
1454          TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1455          if (needed_size == GDI_ERROR) {
1456              TRACE("  - needed size : %d (GDI_ERROR)\n", needed_size);
1457              goto error;
1458          } else {
1459              TRACE("  - needed size : %d\n", needed_size);
1460          }
1461
1462          if (needed_size > size) {
1463              size = needed_size;
1464              HeapFree(GetProcessHeap(), 0, bitmap);
1465              HeapFree(GetProcessHeap(), 0, gl_bitmap);
1466              bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1467              gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1468          }
1469          if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, NULL) == GDI_ERROR) goto error;
1470          if (TRACE_ON(opengl)) {
1471              unsigned int height, width, bitmask;
1472              unsigned char *bitmap_ = (unsigned char *) bitmap;
1473
1474              TRACE("  - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1475              TRACE("  - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1476              TRACE("  - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1477              if (needed_size != 0) {
1478                  TRACE("  - bitmap :\n");
1479                  for (height = 0; height < gm.gmBlackBoxY; height++) {
1480                      TRACE("      ");
1481                      for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1482                          if (bitmask == 0) {
1483                              bitmap_ += 1;
1484                              bitmask = 0x80;
1485                          }
1486                          if (*bitmap_ & bitmask)
1487                              TRACE("*");
1488                          else
1489                              TRACE(" ");
1490                      }
1491                      bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1492                      TRACE("\n");
1493                  }
1494              }
1495          }
1496
1497          /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1498          * glyph for it to be drawn properly.
1499          */
1500          if (needed_size != 0) {
1501              width_int = (gm.gmBlackBoxX + 31) / 32;
1502              for (height = 0; height < gm.gmBlackBoxY; height++) {
1503                  int width;
1504                  for (width = 0; width < width_int; width++) {
1505                      ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1506                      ((int *) bitmap)[height * width_int + width];
1507                  }
1508              }
1509          }
1510
1511          wine_tsx11_lock();
1512          pglNewList(listBase++, GL_COMPILE);
1513          if (needed_size != 0) {
1514              pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1515                      0 - (int) gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - (int) gm.gmptGlyphOrigin.y,
1516                      gm.gmCellIncX, gm.gmCellIncY,
1517                      gl_bitmap);
1518          } else {
1519              /* This is the case of 'empty' glyphs like the space character */
1520              pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1521          }
1522          pglEndList();
1523          wine_tsx11_unlock();
1524      }
1525
1526      wine_tsx11_lock();
1527      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1528      wine_tsx11_unlock();
1529
1530      HeapFree(GetProcessHeap(), 0, bitmap);
1531      HeapFree(GetProcessHeap(), 0, gl_bitmap);
1532      return TRUE;
1533
1534   error:
1535      wine_tsx11_lock();
1536      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1537      wine_tsx11_unlock();
1538
1539      HeapFree(GetProcessHeap(), 0, bitmap);
1540      HeapFree(GetProcessHeap(), 0, gl_bitmap);
1541      return FALSE;
1542 }
1543
1544 /* OpenGL32 wglUseFontBitmapsA */
1545 BOOL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1546 {
1547      Font fid = physDev->font;
1548
1549      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1550
1551      if (fid == 0) {
1552          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
1553      }
1554
1555      wine_tsx11_lock();
1556      /* I assume that the glyphs are at the same position for X and for Windows */
1557      pglXUseXFont(fid, first, count, listBase);
1558      wine_tsx11_unlock();
1559      return TRUE;
1560 }
1561
1562 /* OpenGL32 wglUseFontBitmapsW */
1563 BOOL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1564 {
1565      Font fid = physDev->font;
1566
1567      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1568
1569      if (fid == 0) {
1570          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
1571      }
1572
1573      WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
1574
1575      wine_tsx11_lock();
1576      /* I assume that the glyphs are at the same position for X and for Windows */
1577      pglXUseXFont(fid, first, count, listBase);
1578      wine_tsx11_unlock();
1579      return TRUE;
1580 }
1581
1582 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */ 
1583 void X11DRV_wglGetIntegerv(GLenum pname, GLint* params) {
1584     TRACE("pname: 0x%x, params: %p\n", pname, params);
1585     if (pname == GL_DEPTH_BITS) { 
1586         GLXContext gl_ctx = pglXGetCurrentContext();
1587         Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1588         /*TRACE("returns Wine Ctx as %p\n", ret);*/
1589         /** 
1590         * if we cannot find a Wine Context
1591         * we only have the default wine desktop context, 
1592         * so if we have only a 24 depth say we have 32
1593         */
1594         if (NULL == ret && 24 == *params) { 
1595             *params = 32;
1596         }
1597         TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
1598     }
1599     if (pname == GL_ALPHA_BITS) {
1600         GLint tmp;
1601         GLXContext gl_ctx = pglXGetCurrentContext();
1602         Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1603         pglXGetFBConfigAttrib(ret->display, ret->fb_conf, GLX_ALPHA_SIZE, &tmp);
1604         TRACE("returns GL_ALPHA_BITS as '%d'\n", tmp);
1605         *params = tmp;
1606     }
1607 }
1608
1609 /* WGL_ARB_extensions_string: wglGetExtensionsStringARB */
1610 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
1611     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
1612     return WineGLInfo.wglExtensions;
1613 }
1614
1615 /* WGL_ARB_pbuffer: wglCreatePbufferARB */
1616 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
1617 {
1618     Wine_GLPBuffer* object = NULL;
1619     GLXFBConfig* cfgs = NULL;
1620     int nCfgs = 0;
1621     int attribs[256];
1622     unsigned nAttribs = 0;
1623     int fmt_index = 0;
1624
1625     TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
1626
1627     if (0 >= iPixelFormat) {
1628         ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
1629         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1630         return NULL; /* unexpected error */
1631     }
1632
1633     cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
1634
1635     if (NULL == cfgs || 0 == nCfgs) {
1636         ERR("(%p): Cannot get FB Configs for iPixelFormat(%d), returns NULL\n", hdc, iPixelFormat);
1637         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1638         return NULL; /* unexpected error */
1639     }
1640
1641     /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
1642     if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &nCfgs)) {
1643         ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
1644         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1645         goto create_failed; /* unexpected error */
1646     }
1647
1648     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
1649     if (NULL == object) {
1650         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
1651         goto create_failed; /* unexpected error */
1652     }
1653     object->hdc = hdc;
1654     object->display = gdi_display;
1655     object->width = iWidth;
1656     object->height = iHeight;
1657
1658     nAttribs = ConvertAttribWGLtoGLX(piAttribList, attribs, object);
1659     if (-1 == nAttribs) {
1660         WARN("Cannot convert WGL to GLX attributes\n");
1661         goto create_failed;
1662     }
1663     PUSH2(attribs, GLX_PBUFFER_WIDTH,  iWidth);
1664     PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight); 
1665     while (0 != *piAttribList) {
1666         int attr_v;
1667         switch (*piAttribList) {
1668             case WGL_TEXTURE_FORMAT_ARB: {
1669                 ++piAttribList;
1670                 attr_v = *piAttribList;
1671                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
1672                 if (use_render_texture_ati) {
1673                     int type = 0;
1674                     switch (attr_v) {
1675                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
1676                         case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
1677                         case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
1678                         default:
1679                             SetLastError(ERROR_INVALID_DATA);
1680                             goto create_failed;
1681                     }
1682                     object->use_render_texture = 1;
1683                     PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
1684                 } else {
1685                     if (WGL_NO_TEXTURE_ARB == attr_v) {
1686                         object->use_render_texture = 0;
1687                     } else {
1688                         if (!use_render_texture_emulation) {
1689                             SetLastError(ERROR_INVALID_DATA);
1690                             goto create_failed;
1691                         }
1692                         switch (attr_v) {
1693                             case WGL_TEXTURE_RGB_ARB:
1694                                 object->use_render_texture = GL_RGB;
1695                                 break;
1696                             case WGL_TEXTURE_RGBA_ARB:
1697                                 object->use_render_texture = GL_RGBA;
1698                                 break;
1699                             default:
1700                                 SetLastError(ERROR_INVALID_DATA);
1701                                 goto create_failed;
1702                         }
1703                     }
1704                 }
1705                 break;
1706             }
1707
1708             case WGL_TEXTURE_TARGET_ARB: {
1709                 ++piAttribList;
1710                 attr_v = *piAttribList;
1711                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
1712                 if (use_render_texture_ati) {
1713                     int type = 0;
1714                     switch (attr_v) {
1715                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
1716                         case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
1717                         case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
1718                         case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
1719                         default:
1720                             SetLastError(ERROR_INVALID_DATA);
1721                             goto create_failed;
1722                     }
1723                     PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
1724                 } else {
1725                     if (WGL_NO_TEXTURE_ARB == attr_v) {
1726                         object->texture_target = 0;
1727                     } else {
1728                         if (!use_render_texture_emulation) {
1729                             SetLastError(ERROR_INVALID_DATA);
1730                             goto create_failed;
1731                         }
1732                         switch (attr_v) {
1733                             case WGL_TEXTURE_CUBE_MAP_ARB: {
1734                                 if (iWidth != iHeight) {
1735                                     SetLastError(ERROR_INVALID_DATA);
1736                                     goto create_failed;
1737                                 }
1738                                 object->texture_target = GL_TEXTURE_CUBE_MAP;
1739                                 object->texture_bind_target = GL_TEXTURE_CUBE_MAP;
1740                                break;
1741                             }
1742                             case WGL_TEXTURE_1D_ARB: {
1743                                 if (1 != iHeight) {
1744                                     SetLastError(ERROR_INVALID_DATA);
1745                                     goto create_failed;
1746                                 }
1747                                 object->texture_target = GL_TEXTURE_1D;
1748                                 object->texture_bind_target = GL_TEXTURE_1D;
1749                                 break;
1750                             }
1751                             case WGL_TEXTURE_2D_ARB: {
1752                                 object->texture_target = GL_TEXTURE_2D;
1753                                 object->texture_bind_target = GL_TEXTURE_2D;
1754                                 break;
1755                             }
1756                             default:
1757                                 SetLastError(ERROR_INVALID_DATA);
1758                                 goto create_failed;
1759                         }
1760                     }
1761                 }
1762                 break;
1763             }
1764
1765             case WGL_MIPMAP_TEXTURE_ARB: {
1766                 ++piAttribList;
1767                 attr_v = *piAttribList;
1768                 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
1769                 if (use_render_texture_ati) {
1770                     PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
1771                 } else {
1772                     if (!use_render_texture_emulation) {
1773                         SetLastError(ERROR_INVALID_DATA);
1774                         goto create_failed;
1775                     }
1776                 }
1777                 break;
1778             }
1779         }
1780         ++piAttribList;
1781     }
1782
1783     PUSH1(attribs, None);
1784     object->drawable = pglXCreatePbuffer(gdi_display, cfgs[fmt_index], attribs);
1785     TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
1786     if (!object->drawable) {
1787         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
1788         goto create_failed; /* unexpected error */
1789     }
1790     TRACE("->(%p)\n", object);
1791
1792     /** free list */
1793     XFree(cfgs);
1794     return (HPBUFFERARB) object;
1795
1796 create_failed:
1797     if (NULL != cfgs) XFree(cfgs);
1798     HeapFree(GetProcessHeap(), 0, object);
1799     TRACE("->(FAILED)\n");
1800     return (HPBUFFERARB) NULL;
1801 }
1802
1803 /* WGL_ARB_pbuffer: wglDestroyPbufferARB */
1804 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
1805 {
1806     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1807     TRACE("(%p)\n", hPbuffer);
1808     if (NULL == object) {
1809         SetLastError(ERROR_INVALID_HANDLE);
1810         return GL_FALSE;
1811     }
1812     pglXDestroyPbuffer(object->display, object->drawable);
1813     HeapFree(GetProcessHeap(), 0, object);
1814     return GL_TRUE;
1815 }
1816
1817 /* WGL_ARB_pbuffer: wglGetPbufferDCARB */
1818 static HDC WINAPI X11DRV_wglGetPbufferDCARB(HPBUFFERARB hPbuffer)
1819 {
1820     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1821     HDC hDC;
1822     if (NULL == object) {
1823         SetLastError(ERROR_INVALID_HANDLE);
1824         return NULL;
1825     }
1826     hDC = CreateCompatibleDC(object->hdc);
1827
1828     /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
1829      * We only support one onscreen rendering format (the one from the main visual), so use that. */
1830     SetPixelFormat(hDC, 1, NULL);
1831     set_drawable(hDC, object->drawable); /* works ?? */
1832     TRACE("(%p)->(%p)\n", hPbuffer, hDC);
1833     return hDC;
1834 }
1835
1836 /* WGL_ARB_pbuffer: wglQueryPbufferARB */
1837 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
1838 {
1839     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1840     TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
1841     if (NULL == object) {
1842         SetLastError(ERROR_INVALID_HANDLE);
1843         return GL_FALSE;
1844     }
1845     switch (iAttribute) {
1846         case WGL_PBUFFER_WIDTH_ARB:
1847             pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
1848             break;
1849         case WGL_PBUFFER_HEIGHT_ARB:
1850             pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
1851             break;
1852
1853         case WGL_PBUFFER_LOST_ARB:
1854             FIXME("unsupported WGL_PBUFFER_LOST_ARB (need glXSelectEvent/GLX_DAMAGED work)\n");
1855             break;
1856
1857         case WGL_TEXTURE_FORMAT_ARB:
1858             if (use_render_texture_ati) {
1859                 unsigned int tmp;
1860                 int type = WGL_NO_TEXTURE_ARB;
1861                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
1862                 switch (tmp) {
1863                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
1864                     case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
1865                     case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
1866                 }
1867                 *piValue = type;
1868             } else {
1869                 if (!object->use_render_texture) {
1870                     *piValue = WGL_NO_TEXTURE_ARB;
1871                 } else {
1872                     if (!use_render_texture_emulation) {
1873                         SetLastError(ERROR_INVALID_HANDLE);
1874                         return GL_FALSE;
1875                     }
1876                     if (GL_RGBA == object->use_render_texture) {
1877                         *piValue = WGL_TEXTURE_RGBA_ARB;
1878                     } else {
1879                         *piValue = WGL_TEXTURE_RGB_ARB;
1880                     }
1881                 }
1882             }
1883             break;
1884
1885         case WGL_TEXTURE_TARGET_ARB:
1886             if (use_render_texture_ati) {
1887                 unsigned int tmp;
1888                 int type = WGL_NO_TEXTURE_ARB;
1889                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
1890                 switch (tmp) {
1891                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
1892                     case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
1893                     case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
1894                     case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
1895                 }
1896                 *piValue = type;
1897             } else {
1898             if (!object->texture_target) {
1899                 *piValue = WGL_NO_TEXTURE_ARB;
1900             } else {
1901                 if (!use_render_texture_emulation) {
1902                     SetLastError(ERROR_INVALID_DATA);      
1903                     return GL_FALSE;
1904                 }
1905                 switch (object->texture_target) {
1906                     case GL_TEXTURE_1D:       *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
1907                     case GL_TEXTURE_2D:       *piValue = WGL_TEXTURE_1D_ARB; break;
1908                     case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_2D_ARB; break;
1909                 }
1910             }
1911         }
1912         break;
1913
1914     case WGL_MIPMAP_TEXTURE_ARB:
1915         if (use_render_texture_ati) {
1916             pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
1917         } else {
1918             *piValue = GL_FALSE; /** don't support that */
1919             FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
1920         }
1921         break;
1922
1923     default:
1924         FIXME("unexpected attribute %x\n", iAttribute);
1925         break;
1926     }
1927
1928     return GL_TRUE;
1929 }
1930
1931 /* WGL_ARB_pbuffer: wglReleasePbufferDCARB */
1932 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
1933 {
1934     TRACE("(%p, %p)\n", hPbuffer, hdc);
1935     DeleteDC(hdc);
1936     return 0;
1937 }
1938
1939 /* WGL_ARB_pbuffer: wglSetPbufferAttribARB */
1940 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
1941 {
1942     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1943     WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
1944     if (NULL == object) {
1945         SetLastError(ERROR_INVALID_HANDLE);
1946         return GL_FALSE;
1947     }
1948     if (!object->use_render_texture) {
1949         SetLastError(ERROR_INVALID_HANDLE);
1950         return GL_FALSE;
1951     }
1952     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
1953         return GL_TRUE;
1954     }
1955     if (NULL != pglXDrawableAttribARB) {
1956         if (use_render_texture_ati) {
1957             FIXME("Need conversion for GLX_ATI_render_texture\n");
1958         }
1959         return pglXDrawableAttribARB(object->display, object->drawable, piAttribList); 
1960     }
1961     return GL_FALSE;
1962 }
1963
1964 /* WGL_ARB_pixel_format: wglChoosePixelFormatARB */
1965 GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
1966 {
1967     int gl_test = 0;
1968     int attribs[256];
1969     int nAttribs = 0;
1970     GLboolean res = FALSE;
1971
1972     /* We need the visualid to check if the format is suitable */
1973     VisualID visualid = XVisualIDFromVisual(visual);
1974
1975     GLXFBConfig* cfgs = NULL;
1976     int nCfgs = 0;
1977     UINT it;
1978     int fmt_id;
1979
1980     GLXFBConfig* cfgs_fmt = NULL;
1981     int nCfgs_fmt = 0;
1982     UINT it_fmt;
1983     int tmp_fmt_id;
1984     int tmp_vis_id;
1985
1986     int pfmt_it = 0;
1987     int offscreen_index = 1; /* Start at one because we always have a main visual at iPixelFormat=1 */
1988
1989     TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
1990     if (NULL != pfAttribFList) {
1991         FIXME("unused pfAttribFList\n");
1992     }
1993
1994     nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
1995     if (-1 == nAttribs) {
1996         WARN("Cannot convert WGL to GLX attributes\n");
1997         return GL_FALSE;
1998     }
1999     PUSH1(attribs, None);
2000
2001     /* Search for FB configurations matching the requirements in attribs */
2002     cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2003     if (NULL == cfgs) {
2004         WARN("Compatible Pixel Format not found\n");
2005         return GL_FALSE;
2006     }
2007
2008     /* Get a list of all FB configurations */
2009     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
2010     if (NULL == cfgs_fmt) {
2011         ERR("Failed to get All FB Configs\n");
2012         XFree(cfgs);
2013         return GL_FALSE;
2014     }
2015
2016     /* Loop through all matching formats and check if they are suitable.
2017     * Note that this function should at max return nMaxFormats different formats */
2018     for (it = 0; pfmt_it < nMaxFormats && it < nCfgs; ++it) {
2019         gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2020         if (gl_test) {
2021             ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2022             continue;
2023         }
2024
2025         gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_VISUAL_ID, &tmp_vis_id);
2026         if (gl_test) {
2027             ERR("Failed to retrieve VISUAL_ID from GLXFBConfig, expect problems.\n");
2028             continue;
2029         }
2030
2031         /* When the visualid of the GLXFBConfig matches the one of the main visual we have found our
2032         * only supported onscreen rendering format. This format has a WGL index of 1. */
2033         if(tmp_vis_id == visualid) {
2034             piFormats[pfmt_it] = 1;
2035             ++pfmt_it;
2036             res = GL_TRUE;
2037             TRACE("Found compatible GLXFBConfig 0x%x with WGL index 1\n", fmt_id);
2038             continue;
2039         }
2040         /* Only continue with this loop for offscreen rendering formats (visualid = 0) */
2041         else if(tmp_vis_id != 0) {
2042             TRACE("Discarded GLXFBConfig %0x with VisualID %x because the visualid is not the same as our main visual (%lx)\n", fmt_id, tmp_vis_id, visualid);
2043             continue;
2044         }
2045
2046         /* Find the index of the found format in the whole format table */
2047         for (it_fmt = 0; it_fmt < nCfgs_fmt; ++it_fmt) {
2048             gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs_fmt[it_fmt], GLX_FBCONFIG_ID, &tmp_fmt_id);
2049             if (gl_test) {
2050                 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2051                 continue;
2052             }
2053             gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_VISUAL_ID, &tmp_vis_id);
2054             if (gl_test) {
2055                 ERR("Failed to retrieve VISUAL_ID from GLXFBConfig, expect problems.\n");
2056                 continue;
2057             }
2058             /* The format of Wine's main visual is stored at index 1 of our WGL format table.
2059             * At higher indices we store offscreen rendering formats (visualid=0). Below we calculate
2060             * the index of the offscreen format. We do this by counting the number of offscreen formats
2061             * which we see until we reach our target format. */ 
2062             if(tmp_vis_id == 0)
2063                 offscreen_index++;
2064
2065             /* We have found the format in the table (note the format is offscreen) */
2066             if (fmt_id == tmp_fmt_id) {
2067                 int tmp;
2068
2069                 piFormats[pfmt_it] = offscreen_index + 1; /* Add 1 to get a one-based index */ 
2070                 ++pfmt_it;
2071                 pglXGetFBConfigAttrib(gdi_display, cfgs_fmt[it_fmt], GLX_ALPHA_SIZE, &tmp);
2072                 TRACE("ALPHA_SIZE of FBCONFIG_ID(%d/%d) found as '%d'\n", it_fmt + 1, nCfgs_fmt, tmp);
2073                 break;
2074             }
2075         }
2076         if (it_fmt == nCfgs_fmt) {
2077             ERR("Failed to get valid fmt for %d. Try next.\n", it);
2078             continue;
2079         }
2080         TRACE("at %d/%d found FBCONFIG_ID(%d/%d)\n", it + 1, nCfgs, piFormats[it], nCfgs_fmt);
2081     }
2082
2083     *nNumFormats = pfmt_it;
2084     /** free list */
2085     XFree(cfgs);
2086     XFree(cfgs_fmt);
2087     return GL_TRUE;
2088 }
2089
2090 /* WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB */
2091 GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
2092 {
2093     FIXME("(%p, %d, %d, %d, %p, %p): stub\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2094     return GL_FALSE;
2095 }
2096
2097 /* WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB */
2098 GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2099 {
2100     UINT i;
2101     GLXFBConfig* cfgs = NULL;
2102     GLXFBConfig  curCfg = NULL;
2103     int nCfgs = 0;
2104     int hTest;
2105     int tmp;
2106     int curGLXAttr = 0;
2107     int nWGLFormats = 0;
2108     int fmt_index = 0;
2109
2110     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2111
2112     if (0 < iLayerPlane) {
2113         FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2114         return GL_FALSE;
2115     }
2116
2117     cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
2118     if (NULL == cfgs) {
2119         ERR("no FB Configs found for display(%p)\n", gdi_display);
2120         return GL_FALSE;
2121     }
2122
2123     /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supoprted.
2124     * We don't have to fail yet as a program can specify an invaled iPixelFormat (lets say 0) if it wants to query
2125     * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2126     if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &nWGLFormats)) {
2127         ERR("Unable to convert iPixelFormat %d to a GLX one, expect problems!\n", iPixelFormat);
2128     }
2129
2130     for (i = 0; i < nAttributes; ++i) {
2131         const int curWGLAttr = piAttributes[i];
2132         TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2133
2134         switch (curWGLAttr) {
2135             case WGL_NUMBER_PIXEL_FORMATS_ARB:
2136                 piValues[i] = nWGLFormats; 
2137                 continue;
2138
2139             case WGL_SUPPORT_OPENGL_ARB:
2140                 piValues[i] = GL_TRUE; 
2141                 continue;
2142
2143             case WGL_ACCELERATION_ARB:
2144                 curGLXAttr = GLX_CONFIG_CAVEAT;
2145
2146                 if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
2147                     curCfg = cfgs[iPixelFormat - 1];
2148
2149                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2150                 if (hTest) goto get_error;
2151                 switch (tmp) {
2152                     case GLX_NONE: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2153                     case GLX_SLOW_CONFIG: piValues[i] = WGL_NO_ACCELERATION_ARB; break;
2154                     case GLX_NON_CONFORMANT_CONFIG: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2155                     default:
2156                         ERR("unexpected Config Caveat(%x)\n", tmp);
2157                         piValues[i] = WGL_NO_ACCELERATION_ARB;
2158                 }
2159                 continue;
2160
2161             case WGL_TRANSPARENT_ARB:
2162                 curGLXAttr = GLX_TRANSPARENT_TYPE;
2163                     /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2164                      * supported WGLFormats and also check if the GLX fmt_index is valid. */
2165                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2166                     curCfg = cfgs[fmt_index];
2167                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2168                 if (hTest) goto get_error;
2169                     piValues[i] = GL_FALSE;
2170                 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2171                     continue;
2172
2173             case WGL_PIXEL_TYPE_ARB:
2174                 curGLXAttr = GLX_RENDER_TYPE;
2175                 /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2176                 * supported WGLFormats and also check if the GLX fmt_index is valid. */
2177                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2178                     curCfg = cfgs[fmt_index];
2179                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2180                 if (hTest) goto get_error;
2181                 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2182                 if      (tmp & GLX_RGBA_BIT)           { piValues[i] = WGL_TYPE_RGBA_ARB; }
2183                 else if (tmp & GLX_COLOR_INDEX_BIT)    { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2184                 else if (tmp & GLX_RGBA_FLOAT_BIT)     { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2185                 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2186                 else {
2187                     ERR("unexpected RenderType(%x)\n", tmp);
2188                     piValues[i] = WGL_TYPE_RGBA_ARB;
2189                 }
2190                 continue;
2191
2192             case WGL_COLOR_BITS_ARB:
2193                 /** see ConvertAttribWGLtoGLX for explain */
2194                 /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2195                 * supported WGLFormats and also check if the GLX fmt_index is valid. */
2196                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2197                     curCfg = cfgs[fmt_index];
2198                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, GLX_BUFFER_SIZE, piValues + i);
2199                 if (hTest) goto get_error;
2200                 TRACE("WGL_COLOR_BITS_ARB: GLX_BUFFER_SIZE = %d\n", piValues[i]);
2201                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, GLX_ALPHA_SIZE, &tmp);
2202                 if (hTest) goto get_error;
2203                 TRACE("WGL_COLOR_BITS_ARB: GLX_ALPHA_SIZE = %d\n", tmp);
2204                 piValues[i] = piValues[i] - tmp;
2205                 continue;
2206
2207             case WGL_BIND_TO_TEXTURE_RGB_ARB:
2208                 if (use_render_texture_ati) {
2209                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2210                     break;
2211                 }
2212             case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2213                 if (use_render_texture_ati) {
2214                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2215                     break;
2216                 }
2217                 if (!use_render_texture_emulation) {
2218                     piValues[i] = GL_FALSE;
2219                     continue;   
2220                 }
2221                 curGLXAttr = GLX_RENDER_TYPE;
2222                 /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2223                  * supported WGLFormats and also check if the GLX fmt_index is valid. */
2224                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2225                     curCfg = cfgs[fmt_index];
2226                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2227                 if (hTest) goto get_error;
2228                 if (GLX_COLOR_INDEX_BIT == tmp) {
2229                     piValues[i] = GL_FALSE;  
2230                     continue;
2231                 }
2232                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, GLX_DRAWABLE_TYPE, &tmp);
2233                 if (hTest) goto get_error;
2234                     piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2235                 continue;
2236
2237             case WGL_BLUE_BITS_ARB:
2238                 curGLXAttr = GLX_BLUE_SIZE;
2239                 break;
2240             case WGL_RED_BITS_ARB:
2241                 curGLXAttr = GLX_RED_SIZE;
2242                 break;
2243             case WGL_GREEN_BITS_ARB:
2244                 curGLXAttr = GLX_GREEN_SIZE;
2245                 break;
2246             case WGL_ALPHA_BITS_ARB:
2247                 curGLXAttr = GLX_ALPHA_SIZE;
2248                 break;
2249             case WGL_DEPTH_BITS_ARB:
2250                 curGLXAttr = GLX_DEPTH_SIZE;
2251                 break;
2252             case WGL_STENCIL_BITS_ARB:
2253                 curGLXAttr = GLX_STENCIL_SIZE;
2254                 break;
2255             case WGL_DOUBLE_BUFFER_ARB:
2256                 curGLXAttr = GLX_DOUBLEBUFFER;
2257                 break;
2258             case WGL_STEREO_ARB:
2259                 curGLXAttr = GLX_STEREO;
2260                 break;
2261             case WGL_AUX_BUFFERS_ARB:
2262                 curGLXAttr = GLX_AUX_BUFFERS;
2263                 break;
2264
2265             case WGL_SUPPORT_GDI_ARB:
2266             case WGL_DRAW_TO_WINDOW_ARB:
2267             case WGL_DRAW_TO_BITMAP_ARB:
2268             case WGL_DRAW_TO_PBUFFER_ARB:
2269                 curGLXAttr = GLX_X_RENDERABLE;
2270                 break;
2271
2272             case WGL_PBUFFER_LARGEST_ARB:
2273                 curGLXAttr = GLX_LARGEST_PBUFFER;
2274                 break;
2275
2276             case WGL_SAMPLE_BUFFERS_ARB:
2277                 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2278                 break;
2279
2280             case WGL_SAMPLES_ARB:
2281                 curGLXAttr = GLX_SAMPLES_ARB;
2282                 break;
2283
2284             default:
2285                 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2286         }
2287
2288         if (0 != curGLXAttr) {
2289             /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2290             * supported WGLFormats and also check if the GLX fmt_index is valid. */
2291             if((iPixelFormat > 0) && ((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs))) goto pix_error;
2292                 curCfg = cfgs[fmt_index];
2293             hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, piValues + i);
2294             if (hTest) goto get_error;
2295         } else { 
2296             piValues[i] = GL_FALSE; 
2297         }
2298     }
2299     return GL_TRUE;
2300
2301 get_error:
2302     ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2303     XFree(cfgs);
2304     return GL_FALSE;
2305
2306 pix_error:
2307     ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nCfgs);
2308     XFree(cfgs);
2309     return GL_FALSE;
2310 }
2311
2312 /* WGL_ARB_render_texture: wglBindTexImageARB */
2313 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2314 {
2315     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2316     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2317     if (NULL == object) {
2318         SetLastError(ERROR_INVALID_HANDLE);
2319         return GL_FALSE;
2320     }
2321     if (!object->use_render_texture) {
2322         SetLastError(ERROR_INVALID_HANDLE);
2323         return GL_FALSE;
2324     }
2325     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2326         int do_init = 0;
2327         GLint prev_binded_tex;
2328         pglGetIntegerv(object->texture_target, &prev_binded_tex);
2329         if (NULL == object->render_ctx) {
2330             object->render_hdc = X11DRV_wglGetPbufferDCARB(hPbuffer);
2331             /* FIXME: This is routed through gdi32.dll to winex11.drv, replace this with GLX calls */
2332             object->render_ctx = wglCreateContext(object->render_hdc);
2333             do_init = 1;
2334         }
2335         object->prev_hdc = wglGetCurrentDC();
2336         object->prev_ctx = wglGetCurrentContext();
2337         /* FIXME: This is routed through gdi32.dll to winex11.drv, replace this with GLX calls */
2338         wglMakeCurrent(object->render_hdc, object->render_ctx);
2339         /*
2340         if (do_init) {
2341             glBindTexture(object->texture_target, object->texture);
2342             if (GL_RGBA == object->use_render_texture) {
2343                 glTexImage2D(object->texture_target, 0, GL_RGBA8, object->width, object->height, 0, GL_RGBA, GL_FLOAT, NULL);
2344             } else {
2345                 glTexImage2D(object->texture_target, 0, GL_RGB8, object->width, object->height, 0, GL_RGB, GL_FLOAT, NULL);
2346             }
2347         }
2348         */
2349         object->texture = prev_binded_tex;
2350         return GL_TRUE;
2351     }
2352     if (NULL != pglXBindTexImageARB) {
2353         return pglXBindTexImageARB(object->display, object->drawable, iBuffer);
2354     }
2355     return GL_FALSE;
2356 }
2357
2358 /* WGL_ARB_render_texture: wglReleaseTexImageARB */
2359 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2360 {
2361     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2362     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2363     if (NULL == object) {
2364         SetLastError(ERROR_INVALID_HANDLE);
2365         return GL_FALSE;
2366     }
2367     if (!object->use_render_texture) {
2368         SetLastError(ERROR_INVALID_HANDLE);
2369         return GL_FALSE;
2370     }
2371     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2372     /*
2373         GLint prev_binded_tex;
2374         glGetIntegerv(object->texture_target, &prev_binded_tex);    
2375         if (GL_TEXTURE_1D == object->texture_target) {
2376             glCopyTexSubImage1D(object->texture_target, object->texture_level, 0, 0, 0, object->width);
2377         } else {
2378             glCopyTexSubImage2D(object->texture_target, object->texture_level, 0, 0, 0, 0, object->width,       object->height);
2379         }
2380         glBindTexture(object->texture_target, prev_binded_tex);
2381         SwapBuffers(object->render_hdc);
2382         */
2383         pglBindTexture(object->texture_target, object->texture);
2384         if (GL_TEXTURE_1D == object->texture_target) {
2385             pglCopyTexSubImage1D(object->texture_target, object->texture_level, 0, 0, 0, object->width);
2386         } else {
2387             pglCopyTexSubImage2D(object->texture_target, object->texture_level, 0, 0, 0, 0, object->width, object->height);
2388         }
2389
2390         /* FIXME: This is routed through gdi32.dll to winex11.drv, replace this with GLX calls */
2391         wglMakeCurrent(object->prev_hdc, object->prev_ctx);
2392         return GL_TRUE;
2393     }
2394     if (NULL != pglXReleaseTexImageARB) {
2395         return pglXReleaseTexImageARB(object->display, object->drawable, iBuffer);
2396     }
2397     return GL_FALSE;
2398 }
2399
2400 /* WGL_EXT_extensions_string: wglGetExtensionsStringEXT */
2401 const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
2402     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2403     return WineGLInfo.wglExtensions;
2404 }
2405
2406 /* WGL_EXT_swap_control: wglGetSwapIntervalEXT */
2407 int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
2408     FIXME("(),stub!\n");
2409     return swap_interval;
2410 }
2411
2412 /* WGL_EXT_swap_control: wglSwapIntervalEXT */
2413 BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
2414     TRACE("(%d)\n", interval);
2415     swap_interval = interval;
2416     if (NULL != pglXSwapIntervalSGI) {
2417         return 0 == pglXSwapIntervalSGI(interval);
2418     }
2419     WARN("(): GLX_SGI_swap_control extension seems not supported\n");
2420     return TRUE;
2421 }
2422
2423 /* Check if the supported GLX version matches requiredVersion */
2424 static BOOL glxRequireVersion(int requiredVersion)
2425 {
2426     /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2427     if(requiredVersion <= WineGLInfo.glxVersion[1])
2428         return TRUE;
2429
2430     return FALSE;
2431 }
2432
2433 static BOOL glxRequireExtension(const char *requiredExtension)
2434 {
2435     if (strstr(WineGLInfo.glxClientExtensions, requiredExtension) == NULL) {
2436         return FALSE;
2437     }
2438
2439     return TRUE;
2440 }
2441
2442 static BOOL register_extension(const WineGLExtension * ext)
2443 {
2444     int i;
2445
2446     assert( WineGLExtensionListSize < MAX_EXTENSIONS );
2447     WineGLExtensionList[WineGLExtensionListSize++] = ext;
2448
2449     strcat(WineGLInfo.wglExtensions, " ");
2450     strcat(WineGLInfo.wglExtensions, ext->extName);
2451
2452     TRACE("'%s'\n", ext->extName);
2453
2454     for (i = 0; ext->extEntryPoints[i].funcName; ++i)
2455         TRACE("    - '%s'\n", ext->extEntryPoints[i].funcName);
2456
2457     return TRUE;
2458 }
2459
2460 static const WineGLExtension WGL_ARB_extensions_string =
2461 {
2462   "WGL_ARB_extensions_string",
2463   {
2464     { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
2465   }
2466 };
2467
2468 static const WineGLExtension WGL_ARB_make_current_read =
2469 {
2470   "WGL_ARB_make_current_read",
2471   {
2472     { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
2473     { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
2474   }
2475 };
2476
2477 static const WineGLExtension WGL_ARB_multisample =
2478 {
2479   "WGL_ARB_multisample",
2480 };
2481
2482 static const WineGLExtension WGL_ARB_pbuffer =
2483 {
2484   "WGL_ARB_pbuffer",
2485   {
2486     { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
2487     { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
2488     { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
2489     { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
2490     { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
2491     { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
2492   }
2493 };
2494
2495 static const WineGLExtension WGL_ARB_pixel_format =
2496 {
2497   "WGL_ARB_pixel_format",
2498   {
2499     { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
2500     { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
2501     { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
2502   }
2503 };
2504
2505 static const WineGLExtension WGL_ARB_render_texture =
2506 {
2507   "WGL_ARB_render_texture",
2508   {
2509     { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
2510     { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
2511   }
2512 };
2513
2514 static const WineGLExtension WGL_EXT_extensions_string =
2515 {
2516   "WGL_EXT_extensions_string",
2517   {
2518     { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
2519   }
2520 };
2521
2522 static const WineGLExtension WGL_EXT_swap_control =
2523 {
2524   "WGL_EXT_swap_control",
2525   {
2526     { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
2527     { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
2528   }
2529 };
2530
2531
2532 /**
2533  * X11DRV_WineGL_LoadExtensions
2534  */
2535 static void X11DRV_WineGL_LoadExtensions(void)
2536 {
2537     WineGLInfo.wglExtensions[0] = 0;
2538
2539     /* ARB Extensions */
2540
2541     register_extension(&WGL_ARB_extensions_string);
2542
2543     if (glxRequireVersion(3))
2544         register_extension(&WGL_ARB_make_current_read);
2545
2546     if (glxRequireExtension("GLX_ARB_multisample"))
2547         register_extension(&WGL_ARB_multisample);
2548
2549     if (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer"))
2550         register_extension(&WGL_ARB_pbuffer);
2551
2552     register_extension(&WGL_ARB_pixel_format);
2553
2554     if (glxRequireExtension("GLX_ATI_render_texture") ||
2555         glxRequireExtension("GLX_ARB_render_texture"))
2556         register_extension(&WGL_ARB_render_texture);
2557
2558     /* EXT Extensions */
2559
2560     register_extension(&WGL_EXT_extensions_string);
2561
2562     if (glxRequireExtension("GLX_SGI_swap_control"))
2563         register_extension(&WGL_EXT_swap_control);
2564 }
2565
2566
2567 static XID create_glxpixmap(X11DRV_PDEVICE *physDev)
2568 {
2569     GLXPixmap ret;
2570     XVisualInfo *vis;
2571     XVisualInfo template;
2572     int num;
2573
2574     wine_tsx11_lock();
2575
2576     /* Retrieve the visualid from our main visual which is the only visual we can use */
2577     template.visualid =  XVisualIDFromVisual(visual);
2578     vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
2579
2580     ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
2581     XFree(vis);
2582     wine_tsx11_unlock(); 
2583     TRACE("return %lx\n", ret);
2584     return ret;
2585 }
2586
2587 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
2588 {
2589     Drawable ret;
2590
2591     if(physDev->bitmap)
2592     {
2593         if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
2594             ret = physDev->drawable; /* PBuffer */
2595         else
2596         {
2597             if(!physDev->bitmap->glxpixmap)
2598                 physDev->bitmap->glxpixmap = create_glxpixmap(physDev);
2599             ret = physDev->bitmap->glxpixmap;
2600         }
2601     }
2602     else
2603         ret = physDev->drawable;
2604     return ret;
2605 }
2606
2607 BOOL destroy_glxpixmap(XID glxpixmap)
2608 {
2609     wine_tsx11_lock(); 
2610     pglXDestroyGLXPixmap(gdi_display, glxpixmap);
2611     wine_tsx11_unlock(); 
2612     return TRUE;
2613 }
2614
2615 /**
2616  * X11DRV_SwapBuffers
2617  *
2618  * Swap the buffers of this DC
2619  */
2620 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
2621 {
2622   GLXDrawable drawable;
2623   if (!has_opengl()) {
2624     ERR("No libGL on this box - disabling OpenGL support !\n");
2625     return 0;
2626   }
2627   
2628   TRACE_(opengl)("(%p)\n", physDev);
2629
2630   drawable = get_glxdrawable(physDev);
2631   wine_tsx11_lock();
2632   pglXSwapBuffers(gdi_display, drawable);
2633   wine_tsx11_unlock();
2634
2635   /* FPS support */
2636   if (TRACE_ON(fps))
2637   {
2638       static long prev_time, frames;
2639
2640       DWORD time = GetTickCount();
2641       frames++;
2642       /* every 1.5 seconds */
2643       if (time - prev_time > 1500) {
2644           TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
2645           prev_time = time;
2646           frames = 0;
2647       }
2648   }
2649
2650   return TRUE;
2651 }
2652
2653 /***********************************************************************
2654  *              X11DRV_setup_opengl_visual
2655  *
2656  * Setup the default visual used for OpenGL and Direct3D, and the desktop
2657  * window (if it exists).  If OpenGL isn't available, the visual is simply
2658  * set to the default visual for the display
2659  */
2660 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
2661 {
2662     XVisualInfo *visual = NULL;
2663     /* In order to support OpenGL or D3D, we require a double-buffered visual and stencil buffer support, */
2664     int dblBuf[] = {GLX_RGBA,GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_ALPHA_SIZE, 8, GLX_DOUBLEBUFFER, None};
2665     if (!has_opengl()) return NULL;
2666
2667     wine_tsx11_lock();
2668     visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf);
2669     wine_tsx11_unlock();
2670     if (visual == NULL) {
2671         /* fallback to 16 bits depth, no alpha */
2672         int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, GLX_DOUBLEBUFFER, None};
2673         WARN("Failed to get a visual with at least 24 bits depth\n");
2674
2675         wine_tsx11_lock();
2676         visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
2677         wine_tsx11_unlock();
2678         if (visual == NULL) {
2679             /* fallback to no stencil */
2680             int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
2681             WARN("Failed to get a visual with at least 8 bits of stencil\n");
2682
2683             wine_tsx11_lock();
2684             visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
2685             wine_tsx11_unlock();
2686             if (visual == NULL) {
2687                 /* This should only happen if we cannot find a match with a depth size 16 */
2688                 FIXME("Failed to find a suitable visual\n");
2689                 return visual;
2690             }
2691         }
2692     }
2693     TRACE("Visual ID %lx Chosen\n",visual->visualid);
2694     return visual;
2695 }
2696
2697 #else  /* no OpenGL includes */
2698
2699 void X11DRV_OpenGL_Init(Display *display)
2700 {
2701 }
2702
2703 /***********************************************************************
2704  *              ChoosePixelFormat (X11DRV.@)
2705  */
2706 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
2707                              const PIXELFORMATDESCRIPTOR *ppfd) {
2708   ERR("No OpenGL support compiled in.\n");
2709
2710   return 0;
2711 }
2712
2713 /***********************************************************************
2714  *              DescribePixelFormat (X11DRV.@)
2715  */
2716 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
2717                                int iPixelFormat,
2718                                UINT nBytes,
2719                                PIXELFORMATDESCRIPTOR *ppfd) {
2720   ERR("No OpenGL support compiled in.\n");
2721
2722   return 0;
2723 }
2724
2725 /***********************************************************************
2726  *              GetPixelFormat (X11DRV.@)
2727  */
2728 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
2729   ERR("No OpenGL support compiled in.\n");
2730
2731   return 0;
2732 }
2733
2734 /***********************************************************************
2735  *              SetPixelFormat (X11DRV.@)
2736  */
2737 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
2738                            int iPixelFormat,
2739                            const PIXELFORMATDESCRIPTOR *ppfd) {
2740   ERR("No OpenGL support compiled in.\n");
2741
2742   return FALSE;
2743 }
2744
2745 /***********************************************************************
2746  *              SwapBuffers (X11DRV.@)
2747  */
2748 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
2749   ERR_(opengl)("No OpenGL support compiled in.\n");
2750
2751   return FALSE;
2752 }
2753
2754 /* OpenGL32 wglCreateContext */
2755 HGLRC WINAPI X11DRV_wglCreateContext(HDC hdc) {
2756     ERR_(opengl)("No OpenGL support compiled in.\n");
2757     return NULL;
2758 }
2759
2760 /* OpenGL32 wglDeleteContext */
2761 BOOL WINAPI X11DRV_wglDeleteContext(HGLRC hglrc) {
2762     ERR_(opengl)("No OpenGL support compiled in.\n");
2763     return FALSE;
2764 }
2765
2766 /* OpenGL32: wglGetProcAddress */
2767 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
2768     ERR_(opengl)("No OpenGL support compiled in.\n");
2769     return NULL;
2770 }
2771
2772 /* OpenGL32 wglMakeCurrent */
2773 BOOL WINAPI X11DRV_wglMakeCurrent(HDC hdc, HGLRC hglrc) {
2774     ERR_(opengl)("No OpenGL support compiled in.\n");
2775     return FALSE;
2776 }
2777
2778 /* OpenGL32 wglShaderLists */
2779 BOOL WINAPI X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
2780     ERR_(opengl)("No OpenGL support compiled in.\n");
2781     return FALSE;
2782 }
2783
2784 /* OpenGL32 wglUseFontBitmapsA */
2785 BOOL WINAPI X11DRV_wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
2786 {
2787     ERR_(opengl)("No OpenGL support compiled in.\n");
2788     return FALSE;
2789 }
2790
2791 /* OpenGL32 wglUseFontBitmapsW */
2792 BOOL WINAPI X11DRV_wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
2793 {
2794     ERR_(opengl)("No OpenGL support compiled in.\n");
2795     return FALSE;
2796 }
2797
2798 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */ 
2799 void X11DRV_wglGetIntegerv(int pname, int* params) {
2800     ERR_(opengl)("No OpenGL support compiled in.\n");
2801 }
2802
2803 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
2804 {
2805   return NULL;
2806 }
2807
2808 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
2809 {
2810     return 0;
2811 }
2812
2813 BOOL destroy_glxpixmap(XID glxpixmap)
2814 {
2815     return FALSE;
2816 }
2817
2818 #endif /* defined(HAVE_OPENGL) */