riched20: Fix one more memory leak.
[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 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
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       TRACE("pixel type mismatch\n");
899       goto choose_exit;
900     }
901
902     /* Doublebuffer */
903     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_DOUBLEBUFFER, &value); if (value) dwFlags |= PFD_DOUBLEBUFFER;
904     if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) && (ppfd->dwFlags & PFD_DOUBLEBUFFER)) {
905       if (!(dwFlags & PFD_DOUBLEBUFFER)) {
906         TRACE("dbl buffer mismatch\n");
907         goto choose_exit;
908       }
909     }
910
911     /* Stereo */
912     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_STEREO, &value); if (value) dwFlags |= PFD_STEREO;
913     if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE) && (ppfd->dwFlags & PFD_STEREO)) {
914       if (!(dwFlags & PFD_STEREO)) {
915         TRACE("stereo mismatch\n");
916         goto choose_exit;
917       }
918     }
919
920     /* Alpha bits */
921     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_ALPHA_SIZE, &value);
922     if (ppfd->iPixelType==PFD_TYPE_RGBA && ppfd->cAlphaBits && !value) {
923       TRACE("alpha mismatch\n");
924       goto choose_exit;
925     }
926
927     /* Depth bits */
928     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_DEPTH_SIZE, &value);
929     if (ppfd->cDepthBits && !value) {
930       TRACE("depth mismatch\n");
931       goto choose_exit;
932     }
933
934     /* Stencil bits */
935     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_STENCIL_SIZE, &value);
936     if (ppfd->cStencilBits && !value) {
937       TRACE("stencil mismatch\n");
938       goto choose_exit;
939     }
940
941     /* Aux buffers */
942     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_AUX_BUFFERS, &value);
943     if (ppfd->cAuxBuffers && !value) {
944       TRACE("aux mismatch\n");
945       goto choose_exit;
946     }
947
948     /* When we pass all the checks we have found a matching format :) */
949     ret = 1;
950     TRACE("Successfully found a matching mode, returning index: %d\n", ret);
951   }
952
953 choose_exit:
954   if(!ret)
955     TRACE("No matching mode was found returning 0\n");
956
957   if (NULL != cfgs) XFree(cfgs);
958   wine_tsx11_unlock();
959   return ret;
960 }
961
962 /**
963  * X11DRV_DescribePixelFormat
964  *
965  * Get the pixel-format descriptor associated to the given id
966  */
967 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
968                                int iPixelFormat,
969                                UINT nBytes,
970                                PIXELFORMATDESCRIPTOR *ppfd) {
971   /*XVisualInfo *vis;*/
972   int value;
973   int rb,gb,bb,ab;
974
975   GLXFBConfig* cfgs = NULL;
976   GLXFBConfig cur;
977   int nCfgs = 0;
978   int ret = 0;
979   int fmt_index = 0;
980
981   if (!has_opengl()) {
982     ERR("No libGL on this box - disabling OpenGL support !\n");
983     return 0;
984   }
985   
986   TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
987
988   wine_tsx11_lock();
989   cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
990   wine_tsx11_unlock();
991
992   if (NULL == cfgs || 0 == nCfgs) {
993     ERR("unexpected iPixelFormat(%d), returns NULL\n", iPixelFormat);
994     return 0; /* unespected error */
995   }
996
997   /* This function always reports the total number of supported pixel formats.
998    * At the moment we only support the pixel format corresponding to the main
999    * visual which got created at x11drv initialization. More formats could be
1000    * supported if there was a way to recreate x11 windows in x11drv. 
1001    * Because we only support one format nCfgs needs to be set to 1.
1002    */
1003   nCfgs = 1;
1004
1005   if (ppfd == NULL) {
1006     /* The application is only querying the number of visuals */
1007     wine_tsx11_lock();
1008     if (NULL != cfgs) XFree(cfgs);
1009     wine_tsx11_unlock();
1010     return nCfgs;
1011   }
1012
1013   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1014     ERR("Wrong structure size !\n");
1015     /* Should set error */
1016     return 0;
1017   }
1018
1019   if (nCfgs < iPixelFormat || 1 > iPixelFormat) {
1020     WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL\n", iPixelFormat, nCfgs);
1021     return 0;
1022   }
1023
1024   /* Retrieve the index in the FBConfig table corresponding to the visual ID from the main visual */
1025   if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &value)) {
1026       ERR("Can't find a valid pixel format index from the main visual, expect problems!\n");
1027       return 0;
1028   }
1029
1030   ret = nCfgs;
1031   cur = cfgs[fmt_index];
1032
1033   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1034   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1035   ppfd->nVersion = 1;
1036
1037   /* These flags are always the same... */
1038   ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
1039   /* Now the flags extracted from the Visual */
1040
1041   wine_tsx11_lock();
1042
1043   pglXGetFBConfigAttrib(gdi_display, cur, GLX_CONFIG_CAVEAT, &value);
1044   if(value == GLX_SLOW_CONFIG)
1045       ppfd->dwFlags |= PFD_GENERIC_ACCELERATED;
1046
1047   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1048   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1049
1050   /* Pixel type */
1051   pglXGetFBConfigAttrib(gdi_display, cur, GLX_RENDER_TYPE, &value);
1052   if (value & GLX_RGBA_BIT)
1053     ppfd->iPixelType = PFD_TYPE_RGBA;
1054   else
1055     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1056
1057   /* Color bits */
1058   pglXGetFBConfigAttrib(gdi_display, cur, GLX_BUFFER_SIZE, &value);
1059   ppfd->cColorBits = value;
1060
1061   /* Red, green, blue and alpha bits / shifts */
1062   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1063     pglXGetFBConfigAttrib(gdi_display, cur, GLX_RED_SIZE, &rb);
1064     pglXGetFBConfigAttrib(gdi_display, cur, GLX_GREEN_SIZE, &gb);
1065     pglXGetFBConfigAttrib(gdi_display, cur, GLX_BLUE_SIZE, &bb);
1066     pglXGetFBConfigAttrib(gdi_display, cur, GLX_ALPHA_SIZE, &ab);
1067
1068     ppfd->cRedBits = rb;
1069     ppfd->cRedShift = gb + bb + ab;
1070     ppfd->cBlueBits = bb;
1071     ppfd->cBlueShift = ab;
1072     ppfd->cGreenBits = gb;
1073     ppfd->cGreenShift = bb + ab;
1074     ppfd->cAlphaBits = ab;
1075     ppfd->cAlphaShift = 0;
1076   } else {
1077     ppfd->cRedBits = 0;
1078     ppfd->cRedShift = 0;
1079     ppfd->cBlueBits = 0;
1080     ppfd->cBlueShift = 0;
1081     ppfd->cGreenBits = 0;
1082     ppfd->cGreenShift = 0;
1083     ppfd->cAlphaBits = 0;
1084     ppfd->cAlphaShift = 0;
1085   }
1086   /* Accums : to do ... */
1087
1088   /* Depth bits */
1089   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DEPTH_SIZE, &value);
1090   ppfd->cDepthBits = value;
1091
1092   /* stencil bits */
1093   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STENCIL_SIZE, &value);
1094   ppfd->cStencilBits = value;
1095
1096   wine_tsx11_unlock();
1097
1098   /* Aux : to do ... */
1099
1100   ppfd->iLayerType = PFD_MAIN_PLANE;
1101
1102   if (TRACE_ON(opengl)) {
1103     dump_PIXELFORMATDESCRIPTOR(ppfd);
1104   }
1105
1106   wine_tsx11_lock();
1107   if (NULL != cfgs) XFree(cfgs);
1108   wine_tsx11_unlock();
1109
1110   return ret;
1111 }
1112
1113 /**
1114  * X11DRV_GetPixelFormat
1115  *
1116  * Get the pixel-format id used by this DC
1117  */
1118 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1119   TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1120
1121   return physDev->current_pf;
1122 }
1123
1124 /**
1125  * X11DRV_SetPixelFormat
1126  *
1127  * Set the pixel-format id used by this DC
1128  */
1129 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1130                            int iPixelFormat,
1131                            const PIXELFORMATDESCRIPTOR *ppfd) {
1132   TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1133
1134   if (!has_opengl()) {
1135     ERR("No libGL on this box - disabling OpenGL support !\n");
1136     return 0;
1137   }
1138
1139   /* At the moment we only support the pixelformat corresponding to the main
1140    * x11drv visual which got created at x11drv initialization. More formats
1141    * can be supported if there was a way to recreate x11 windows in x11drv
1142    */
1143   if(iPixelFormat != 1) {
1144     TRACE("Invalid iPixelFormat: %d\n", iPixelFormat);
1145     return 0;
1146   }
1147
1148   physDev->current_pf = iPixelFormat;
1149
1150   if (TRACE_ON(opengl)) {
1151     int nCfgs_fmt = 0;
1152     GLXFBConfig* cfgs_fmt = NULL;
1153     GLXFBConfig cur_cfg;
1154     int value;
1155     int gl_test = 0;
1156     int fmt_index = 0;
1157
1158     if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &value)) {
1159       ERR("Can't find a valid pixel format index from the main visual, expect problems!\n");
1160       return TRUE; /* Return true because the SetPixelFormat stuff itself passed */
1161     }
1162
1163     /*
1164      * How to test if hdc current drawable is compatible (visual/FBConfig) ?
1165      *
1166      * in case of root window created HDCs we crash here :(
1167      *
1168     Drawable drawable =  get_drawable( physDev->hdc );
1169     TRACE(" drawable (%p,%p) have :\n", drawable, root_window);
1170     pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &value);
1171     TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
1172     pglXQueryDrawable(gdi_display, drawable, GLX_VISUAL_ID, (unsigned int*) &value);
1173     TRACE(" - VISUAL_ID as 0x%x\n", tmp);
1174     pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &value);
1175     TRACE(" - WIDTH as %d\n", tmp);
1176     pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &value);
1177     TRACE(" - HEIGHT as %d\n", tmp);
1178     */
1179     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
1180     cur_cfg = cfgs_fmt[fmt_index];
1181     gl_test = pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_FBCONFIG_ID, &value);
1182     if (gl_test) {
1183       ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1184     } else {
1185       TRACE(" FBConfig have :\n");
1186       TRACE(" - FBCONFIG_ID   0x%x\n", value);
1187       pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_VISUAL_ID, &value);
1188       TRACE(" - VISUAL_ID     0x%x\n", value);
1189       pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_DRAWABLE_TYPE, &value);
1190       TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1191     }
1192     XFree(cfgs_fmt);
1193   }
1194   return TRUE;
1195 }
1196
1197 /* OpenGL32 wglCreateContext */
1198 HGLRC X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1199 {
1200     Wine_GLContext *ret;
1201     GLXFBConfig* cfgs_fmt = NULL;
1202     GLXFBConfig cur_cfg;
1203     int hdcPF = 1; /* We can only use the Wine's main visual which has an index of 1 */
1204     int tmp = 0;
1205     int fmt_index = 0;
1206     int nCfgs_fmt = 0;
1207     int value = 0;
1208     int gl_test = 0;
1209     HDC hdc = physDev->hdc;
1210
1211     TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1212
1213     if (!has_opengl()) {
1214         ERR("No libGL on this box - disabling OpenGL support !\n");
1215         return 0;
1216     }
1217
1218     /* First, get the visual in use by the X11DRV */
1219     if (!gdi_display) return 0;
1220
1221     /* We can only render using the iPixelFormat (1) of Wine's Main visual, we need to get the correspondig GLX format.
1222     * If this fails something is very wrong on the system. */
1223     if(!ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, &fmt_index, &tmp)) {
1224         ERR("Cannot get FB Config for main iPixelFormat 1, expect problems!\n");
1225         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1226         return NULL;
1227     }
1228
1229     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
1230     if (NULL == cfgs_fmt || 0 == nCfgs_fmt) {
1231         ERR("Cannot get FB Configs, expect problems.\n");
1232         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1233         return NULL;
1234     }
1235
1236     if (nCfgs_fmt < fmt_index) {
1237         ERR("(%p): unexpected pixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, fmt_index, nCfgs_fmt);
1238         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1239         return NULL;
1240     }
1241
1242     cur_cfg = cfgs_fmt[fmt_index];
1243     gl_test = pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_FBCONFIG_ID, &value);
1244     if (gl_test) {
1245         ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1246         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1247         return NULL;
1248     }
1249     XFree(cfgs_fmt);
1250
1251     /* The context will be allocated in the wglMakeCurrent call */
1252     wine_tsx11_lock();
1253     ret = alloc_context();
1254     wine_tsx11_unlock();
1255     ret->hdc = hdc;
1256     ret->display = gdi_display;
1257     ret->fb_conf = cur_cfg;
1258     /*ret->vis = vis;*/
1259     ret->vis = pglXGetVisualFromFBConfig(gdi_display, cur_cfg);
1260
1261     TRACE(" creating context %p (GL context creation delayed)\n", ret);
1262     return (HGLRC) ret;
1263 }
1264
1265 /* OpenGL32 wglDeleteContext */
1266 BOOL X11DRV_wglDeleteContext(HGLRC hglrc)
1267 {
1268     Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1269     BOOL ret = TRUE;
1270
1271     TRACE("(%p)\n", hglrc);
1272
1273     if (!has_opengl()) {
1274         ERR("No libGL on this box - disabling OpenGL support !\n");
1275         return 0;
1276     }
1277
1278     wine_tsx11_lock();
1279     /* A game (Half Life not to name it) deletes twice the same context,
1280     * so make sure it is valid first */
1281     if (is_valid_context( ctx ))
1282     {
1283         if (ctx->ctx) pglXDestroyContext(ctx->display, ctx->ctx);
1284         free_context(ctx);
1285     }
1286     else
1287     {
1288         WARN("Error deleting context !\n");
1289         SetLastError(ERROR_INVALID_HANDLE);
1290         ret = FALSE;
1291     }
1292     wine_tsx11_unlock();
1293
1294     return ret;
1295 }
1296
1297 /* OpenGL32 wglGetCurrentReadDCARB */
1298 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void) 
1299 {
1300     GLXDrawable gl_d;
1301     HDC ret;
1302
1303     TRACE("()\n");
1304
1305     wine_tsx11_lock();
1306     gl_d = pglXGetCurrentReadDrawable();
1307     ret = get_hdc_from_Drawable(gl_d);
1308     wine_tsx11_unlock();
1309
1310     TRACE(" returning %p (GL drawable %lu)\n", ret, gl_d);
1311     return ret;
1312 }
1313
1314 /* OpenGL32: wglGetProcAddress */
1315 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1316 {
1317     int i, j;
1318     const WineGLExtension *ext;
1319
1320     int padding = 32 - strlen(lpszProc);
1321     if (padding < 0)
1322         padding = 0;
1323
1324     if (!has_opengl()) {
1325         ERR("No libGL on this box - disabling OpenGL support !\n");
1326         return 0;
1327     }
1328
1329     /* Check the table of WGL extensions to see if we need to return a WGL extension
1330      * or a function pointer to a native OpenGL function. */
1331     if(strncmp(lpszProc, "wgl", 3) != 0) {
1332         return pglXGetProcAddressARB((GLubyte*)lpszProc);
1333     } else {
1334         TRACE("('%s'):%*s", lpszProc, padding, " ");
1335         for (i = 0; i < WineGLExtensionListSize; ++i) {
1336             ext = WineGLExtensionList[i];
1337             for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1338                 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1339                     TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1340                     return ext->extEntryPoints[j].funcAddress;
1341                 }
1342             }
1343         }
1344     }
1345
1346     ERR("(%s) - not found\n", lpszProc);
1347     return NULL;
1348 }
1349
1350
1351 /* OpenGL32 wglMakeCurrent */
1352 BOOL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1353     BOOL ret;
1354     HDC hdc = physDev->hdc;
1355     DWORD type = GetObjectType(hdc);
1356
1357     TRACE("(%p,%p)\n", hdc, hglrc);
1358
1359     if (!has_opengl()) {
1360         ERR("No libGL on this box - disabling OpenGL support !\n");
1361         return 0;
1362     }
1363
1364     wine_tsx11_lock();
1365     if (hglrc == NULL) {
1366         ret = pglXMakeCurrent(gdi_display, None, NULL);
1367         NtCurrentTeb()->glContext = NULL;
1368     } else {
1369         Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1370         Drawable drawable = physDev->drawable;
1371         if (ctx->ctx == NULL) {
1372             int draw_vis_id, ctx_vis_id;
1373             VisualID visualid = (VisualID)GetPropA( GetDesktopWindow(), "__wine_x11_visual_id" );
1374             TRACE(" Wine desktop VISUAL_ID is 0x%x\n", (unsigned int) visualid);
1375             draw_vis_id = describeDrawable(ctx, drawable);
1376             ctx_vis_id = describeContext(ctx);
1377
1378             if (-1 == draw_vis_id || (draw_vis_id == visualid && draw_vis_id != ctx_vis_id)) {
1379                 /**
1380                 * Inherits from root window so reuse desktop visual
1381                 */
1382                 XVisualInfo template;
1383                 XVisualInfo *vis;
1384                 int num;
1385                 template.visualid = visualid;
1386                 vis = XGetVisualInfo(ctx->display, VisualIDMask, &template, &num);
1387
1388                 TRACE(" Creating GLX Context\n");
1389                 ctx->ctx = pglXCreateContext(ctx->display, vis, NULL, type == OBJ_MEMDC ? False : True);
1390             } else {
1391                 TRACE(" Creating GLX Context\n");
1392                 ctx->ctx = pglXCreateContext(ctx->display, ctx->vis, NULL, type == OBJ_MEMDC ? False : True);
1393             }
1394             TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1395         }
1396         TRACE(" make current for dis %p, drawable %p, ctx %p\n", ctx->display, (void*) drawable, ctx->ctx);
1397         ret = pglXMakeCurrent(ctx->display, drawable, ctx->ctx);
1398         NtCurrentTeb()->glContext = ctx;
1399         if(ret && type == OBJ_MEMDC)
1400         {
1401             ctx->do_escape = TRUE;
1402             pglDrawBuffer(GL_FRONT_LEFT);
1403         }
1404     }
1405     wine_tsx11_unlock();
1406     TRACE(" returning %s\n", (ret ? "True" : "False"));
1407     return ret;
1408 }
1409
1410 /* OpenGL32 wglMakeContextCurrentARB */
1411 BOOL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) 
1412 {
1413     BOOL ret;
1414     TRACE("(%p,%p,%p)\n", hDrawDev, hReadDev, hglrc);
1415
1416     if (!has_opengl()) {
1417         ERR("No libGL on this box - disabling OpenGL support !\n");
1418         return 0;
1419     }
1420
1421     wine_tsx11_lock();
1422     if (hglrc == NULL) {
1423         ret = pglXMakeCurrent(gdi_display, None, NULL);
1424         NtCurrentTeb()->glContext = NULL;
1425     } else {
1426         if (NULL == pglXMakeContextCurrent) {
1427             ret = FALSE;
1428         } else {
1429             Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1430             Drawable d_draw = get_glxdrawable(hDrawDev);
1431             Drawable d_read = get_glxdrawable(hReadDev);
1432
1433             if (ctx->ctx == NULL) {
1434                 ctx->ctx = pglXCreateContext(ctx->display, ctx->vis, NULL, GetObjectType(hDrawDev->hdc) == OBJ_MEMDC ? False : True);
1435                 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1436             }
1437             ret = pglXMakeContextCurrent(ctx->display, d_draw, d_read, ctx->ctx);
1438             NtCurrentTeb()->glContext = ctx;
1439         }
1440     }
1441     wine_tsx11_unlock();
1442
1443     TRACE(" returning %s\n", (ret ? "True" : "False"));
1444     return ret;
1445 }
1446
1447 /* OpenGL32 wglShaderLists */
1448 BOOL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1449     Wine_GLContext *org  = (Wine_GLContext *) hglrc1;
1450     Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1451
1452     TRACE("(%p, %p)\n", org, dest);
1453
1454     if (!has_opengl()) {
1455         ERR("No libGL on this box - disabling OpenGL support !\n");
1456         return 0;
1457     }
1458
1459     if (NULL != dest && dest->ctx != NULL) {
1460         ERR("Could not share display lists, context already created !\n");
1461         return FALSE;
1462     } else {
1463         if (org->ctx == NULL) {
1464             wine_tsx11_lock();
1465             describeContext(org);
1466             org->ctx = pglXCreateContext(org->display, org->vis, NULL, GetObjectType(org->hdc) == OBJ_MEMDC ? False : True);
1467             wine_tsx11_unlock();
1468             TRACE(" created a delayed OpenGL context (%p) for Wine context %p\n", org->ctx, org);
1469         }
1470         if (NULL != dest) {
1471             wine_tsx11_lock();
1472             describeContext(dest);
1473             /* Create the destination context with display lists shared */
1474             dest->ctx = pglXCreateContext(org->display, dest->vis, org->ctx, GetObjectType(org->hdc) == OBJ_MEMDC ? False : True);
1475             wine_tsx11_unlock();
1476             TRACE(" created a delayed OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1477             return TRUE;
1478         }
1479     }
1480     return FALSE;
1481 }
1482
1483 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1484 {
1485      /* We are running using client-side rendering fonts... */
1486      GLYPHMETRICS gm;
1487      unsigned int glyph;
1488      int size = 0;
1489      void *bitmap = NULL, *gl_bitmap = NULL;
1490      int org_alignment;
1491
1492      wine_tsx11_lock();
1493      pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1494      pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1495      wine_tsx11_unlock();
1496
1497      for (glyph = first; glyph < first + count; glyph++) {
1498          unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, NULL);
1499          int height, width_int;
1500
1501          TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1502          if (needed_size == GDI_ERROR) {
1503              TRACE("  - needed size : %d (GDI_ERROR)\n", needed_size);
1504              goto error;
1505          } else {
1506              TRACE("  - needed size : %d\n", needed_size);
1507          }
1508
1509          if (needed_size > size) {
1510              size = needed_size;
1511              HeapFree(GetProcessHeap(), 0, bitmap);
1512              HeapFree(GetProcessHeap(), 0, gl_bitmap);
1513              bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1514              gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1515          }
1516          if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, NULL) == GDI_ERROR) goto error;
1517          if (TRACE_ON(opengl)) {
1518              unsigned int height, width, bitmask;
1519              unsigned char *bitmap_ = (unsigned char *) bitmap;
1520
1521              TRACE("  - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1522              TRACE("  - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1523              TRACE("  - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1524              if (needed_size != 0) {
1525                  TRACE("  - bitmap :\n");
1526                  for (height = 0; height < gm.gmBlackBoxY; height++) {
1527                      TRACE("      ");
1528                      for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1529                          if (bitmask == 0) {
1530                              bitmap_ += 1;
1531                              bitmask = 0x80;
1532                          }
1533                          if (*bitmap_ & bitmask)
1534                              TRACE("*");
1535                          else
1536                              TRACE(" ");
1537                      }
1538                      bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1539                      TRACE("\n");
1540                  }
1541              }
1542          }
1543
1544          /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1545          * glyph for it to be drawn properly.
1546          */
1547          if (needed_size != 0) {
1548              width_int = (gm.gmBlackBoxX + 31) / 32;
1549              for (height = 0; height < gm.gmBlackBoxY; height++) {
1550                  int width;
1551                  for (width = 0; width < width_int; width++) {
1552                      ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1553                      ((int *) bitmap)[height * width_int + width];
1554                  }
1555              }
1556          }
1557
1558          wine_tsx11_lock();
1559          pglNewList(listBase++, GL_COMPILE);
1560          if (needed_size != 0) {
1561              pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1562                      0 - (int) gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - (int) gm.gmptGlyphOrigin.y,
1563                      gm.gmCellIncX, gm.gmCellIncY,
1564                      gl_bitmap);
1565          } else {
1566              /* This is the case of 'empty' glyphs like the space character */
1567              pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1568          }
1569          pglEndList();
1570          wine_tsx11_unlock();
1571      }
1572
1573      wine_tsx11_lock();
1574      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1575      wine_tsx11_unlock();
1576
1577      HeapFree(GetProcessHeap(), 0, bitmap);
1578      HeapFree(GetProcessHeap(), 0, gl_bitmap);
1579      return TRUE;
1580
1581   error:
1582      wine_tsx11_lock();
1583      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1584      wine_tsx11_unlock();
1585
1586      HeapFree(GetProcessHeap(), 0, bitmap);
1587      HeapFree(GetProcessHeap(), 0, gl_bitmap);
1588      return FALSE;
1589 }
1590
1591 /* OpenGL32 wglUseFontBitmapsA */
1592 BOOL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1593 {
1594      Font fid = physDev->font;
1595
1596      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1597
1598      if (!has_opengl()) {
1599         ERR("No libGL on this box - disabling OpenGL support !\n");
1600         return 0;
1601      }
1602
1603      if (fid == 0) {
1604          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
1605      }
1606
1607      wine_tsx11_lock();
1608      /* I assume that the glyphs are at the same position for X and for Windows */
1609      pglXUseXFont(fid, first, count, listBase);
1610      wine_tsx11_unlock();
1611      return TRUE;
1612 }
1613
1614 /* OpenGL32 wglUseFontBitmapsW */
1615 BOOL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1616 {
1617      Font fid = physDev->font;
1618
1619      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1620
1621      if (!has_opengl()) {
1622         ERR("No libGL on this box - disabling OpenGL support !\n");
1623         return 0;
1624      }
1625
1626      if (fid == 0) {
1627          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
1628      }
1629
1630      WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
1631
1632      wine_tsx11_lock();
1633      /* I assume that the glyphs are at the same position for X and for Windows */
1634      pglXUseXFont(fid, first, count, listBase);
1635      wine_tsx11_unlock();
1636      return TRUE;
1637 }
1638
1639 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */ 
1640 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params) {
1641     TRACE("pname: 0x%x, params: %p\n", pname, params);
1642     if (pname == GL_DEPTH_BITS) { 
1643         GLXContext gl_ctx = pglXGetCurrentContext();
1644         Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1645         /*TRACE("returns Wine Ctx as %p\n", ret);*/
1646         /** 
1647         * if we cannot find a Wine Context
1648         * we only have the default wine desktop context, 
1649         * so if we have only a 24 depth say we have 32
1650         */
1651         if (NULL == ret && 24 == *params) { 
1652             *params = 32;
1653         }
1654         TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
1655     }
1656     if (pname == GL_ALPHA_BITS) {
1657         GLint tmp;
1658         GLXContext gl_ctx = pglXGetCurrentContext();
1659         Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1660         pglXGetFBConfigAttrib(ret->display, ret->fb_conf, GLX_ALPHA_SIZE, &tmp);
1661         TRACE("returns GL_ALPHA_BITS as '%d'\n", tmp);
1662         *params = tmp;
1663     }
1664 }
1665
1666 /* WGL_ARB_extensions_string: wglGetExtensionsStringARB */
1667 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
1668     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
1669     return WineGLInfo.wglExtensions;
1670 }
1671
1672 /* WGL_ARB_pbuffer: wglCreatePbufferARB */
1673 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
1674 {
1675     Wine_GLPBuffer* object = NULL;
1676     GLXFBConfig* cfgs = NULL;
1677     int nCfgs = 0;
1678     int attribs[256];
1679     unsigned nAttribs = 0;
1680     int fmt_index = 0;
1681
1682     TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
1683
1684     if (0 >= iPixelFormat) {
1685         ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
1686         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1687         return NULL; /* unexpected error */
1688     }
1689
1690     cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
1691
1692     if (NULL == cfgs || 0 == nCfgs) {
1693         ERR("(%p): Cannot get FB Configs for iPixelFormat(%d), returns NULL\n", hdc, iPixelFormat);
1694         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1695         return NULL; /* unexpected error */
1696     }
1697
1698     /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
1699     if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &nCfgs)) {
1700         ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
1701         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1702         goto create_failed; /* unexpected error */
1703     }
1704
1705     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
1706     if (NULL == object) {
1707         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
1708         goto create_failed; /* unexpected error */
1709     }
1710     object->hdc = hdc;
1711     object->display = gdi_display;
1712     object->width = iWidth;
1713     object->height = iHeight;
1714
1715     nAttribs = ConvertAttribWGLtoGLX(piAttribList, attribs, object);
1716     if (-1 == nAttribs) {
1717         WARN("Cannot convert WGL to GLX attributes\n");
1718         goto create_failed;
1719     }
1720     PUSH2(attribs, GLX_PBUFFER_WIDTH,  iWidth);
1721     PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight); 
1722     while (0 != *piAttribList) {
1723         int attr_v;
1724         switch (*piAttribList) {
1725             case WGL_TEXTURE_FORMAT_ARB: {
1726                 ++piAttribList;
1727                 attr_v = *piAttribList;
1728                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
1729                 if (use_render_texture_ati) {
1730                     int type = 0;
1731                     switch (attr_v) {
1732                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
1733                         case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
1734                         case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
1735                         default:
1736                             SetLastError(ERROR_INVALID_DATA);
1737                             goto create_failed;
1738                     }
1739                     object->use_render_texture = 1;
1740                     PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
1741                 } else {
1742                     if (WGL_NO_TEXTURE_ARB == attr_v) {
1743                         object->use_render_texture = 0;
1744                     } else {
1745                         if (!use_render_texture_emulation) {
1746                             SetLastError(ERROR_INVALID_DATA);
1747                             goto create_failed;
1748                         }
1749                         switch (attr_v) {
1750                             case WGL_TEXTURE_RGB_ARB:
1751                                 object->use_render_texture = GL_RGB;
1752                                 break;
1753                             case WGL_TEXTURE_RGBA_ARB:
1754                                 object->use_render_texture = GL_RGBA;
1755                                 break;
1756                             default:
1757                                 SetLastError(ERROR_INVALID_DATA);
1758                                 goto create_failed;
1759                         }
1760                     }
1761                 }
1762                 break;
1763             }
1764
1765             case WGL_TEXTURE_TARGET_ARB: {
1766                 ++piAttribList;
1767                 attr_v = *piAttribList;
1768                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
1769                 if (use_render_texture_ati) {
1770                     int type = 0;
1771                     switch (attr_v) {
1772                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
1773                         case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
1774                         case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
1775                         case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
1776                         default:
1777                             SetLastError(ERROR_INVALID_DATA);
1778                             goto create_failed;
1779                     }
1780                     PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
1781                 } else {
1782                     if (WGL_NO_TEXTURE_ARB == attr_v) {
1783                         object->texture_target = 0;
1784                     } else {
1785                         if (!use_render_texture_emulation) {
1786                             SetLastError(ERROR_INVALID_DATA);
1787                             goto create_failed;
1788                         }
1789                         switch (attr_v) {
1790                             case WGL_TEXTURE_CUBE_MAP_ARB: {
1791                                 if (iWidth != iHeight) {
1792                                     SetLastError(ERROR_INVALID_DATA);
1793                                     goto create_failed;
1794                                 }
1795                                 object->texture_target = GL_TEXTURE_CUBE_MAP;
1796                                 object->texture_bind_target = GL_TEXTURE_CUBE_MAP;
1797                                break;
1798                             }
1799                             case WGL_TEXTURE_1D_ARB: {
1800                                 if (1 != iHeight) {
1801                                     SetLastError(ERROR_INVALID_DATA);
1802                                     goto create_failed;
1803                                 }
1804                                 object->texture_target = GL_TEXTURE_1D;
1805                                 object->texture_bind_target = GL_TEXTURE_1D;
1806                                 break;
1807                             }
1808                             case WGL_TEXTURE_2D_ARB: {
1809                                 object->texture_target = GL_TEXTURE_2D;
1810                                 object->texture_bind_target = GL_TEXTURE_2D;
1811                                 break;
1812                             }
1813                             default:
1814                                 SetLastError(ERROR_INVALID_DATA);
1815                                 goto create_failed;
1816                         }
1817                     }
1818                 }
1819                 break;
1820             }
1821
1822             case WGL_MIPMAP_TEXTURE_ARB: {
1823                 ++piAttribList;
1824                 attr_v = *piAttribList;
1825                 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
1826                 if (use_render_texture_ati) {
1827                     PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
1828                 } else {
1829                     if (!use_render_texture_emulation) {
1830                         SetLastError(ERROR_INVALID_DATA);
1831                         goto create_failed;
1832                     }
1833                 }
1834                 break;
1835             }
1836         }
1837         ++piAttribList;
1838     }
1839
1840     PUSH1(attribs, None);
1841     object->drawable = pglXCreatePbuffer(gdi_display, cfgs[fmt_index], attribs);
1842     TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
1843     if (!object->drawable) {
1844         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
1845         goto create_failed; /* unexpected error */
1846     }
1847     TRACE("->(%p)\n", object);
1848
1849     /** free list */
1850     XFree(cfgs);
1851     return (HPBUFFERARB) object;
1852
1853 create_failed:
1854     if (NULL != cfgs) XFree(cfgs);
1855     HeapFree(GetProcessHeap(), 0, object);
1856     TRACE("->(FAILED)\n");
1857     return (HPBUFFERARB) NULL;
1858 }
1859
1860 /* WGL_ARB_pbuffer: wglDestroyPbufferARB */
1861 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
1862 {
1863     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1864     TRACE("(%p)\n", hPbuffer);
1865     if (NULL == object) {
1866         SetLastError(ERROR_INVALID_HANDLE);
1867         return GL_FALSE;
1868     }
1869     pglXDestroyPbuffer(object->display, object->drawable);
1870     HeapFree(GetProcessHeap(), 0, object);
1871     return GL_TRUE;
1872 }
1873
1874 /* WGL_ARB_pbuffer: wglGetPbufferDCARB
1875  * The function wglGetPbufferDCARB returns a device context for a pbuffer.
1876  * Gdi32 implements the part of this function which creates a device context.
1877  * This part associates the physDev with the X drawable of the pbuffer.
1878  */
1879 HDC X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
1880 {
1881     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1882     if (NULL == object) {
1883         SetLastError(ERROR_INVALID_HANDLE);
1884         return NULL;
1885     }
1886
1887     /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
1888      * We only support one onscreen rendering format (the one from the main visual), so use that. */
1889     physDev->current_pf = 1;
1890     physDev->drawable = object->drawable;
1891
1892     TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
1893     return physDev->hdc;
1894 }
1895
1896 /* WGL_ARB_pbuffer: wglQueryPbufferARB */
1897 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
1898 {
1899     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1900     TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
1901     if (NULL == object) {
1902         SetLastError(ERROR_INVALID_HANDLE);
1903         return GL_FALSE;
1904     }
1905     switch (iAttribute) {
1906         case WGL_PBUFFER_WIDTH_ARB:
1907             pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
1908             break;
1909         case WGL_PBUFFER_HEIGHT_ARB:
1910             pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
1911             break;
1912
1913         case WGL_PBUFFER_LOST_ARB:
1914             FIXME("unsupported WGL_PBUFFER_LOST_ARB (need glXSelectEvent/GLX_DAMAGED work)\n");
1915             break;
1916
1917         case WGL_TEXTURE_FORMAT_ARB:
1918             if (use_render_texture_ati) {
1919                 unsigned int tmp;
1920                 int type = WGL_NO_TEXTURE_ARB;
1921                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
1922                 switch (tmp) {
1923                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
1924                     case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
1925                     case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
1926                 }
1927                 *piValue = type;
1928             } else {
1929                 if (!object->use_render_texture) {
1930                     *piValue = WGL_NO_TEXTURE_ARB;
1931                 } else {
1932                     if (!use_render_texture_emulation) {
1933                         SetLastError(ERROR_INVALID_HANDLE);
1934                         return GL_FALSE;
1935                     }
1936                     if (GL_RGBA == object->use_render_texture) {
1937                         *piValue = WGL_TEXTURE_RGBA_ARB;
1938                     } else {
1939                         *piValue = WGL_TEXTURE_RGB_ARB;
1940                     }
1941                 }
1942             }
1943             break;
1944
1945         case WGL_TEXTURE_TARGET_ARB:
1946             if (use_render_texture_ati) {
1947                 unsigned int tmp;
1948                 int type = WGL_NO_TEXTURE_ARB;
1949                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
1950                 switch (tmp) {
1951                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
1952                     case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
1953                     case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
1954                     case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
1955                 }
1956                 *piValue = type;
1957             } else {
1958             if (!object->texture_target) {
1959                 *piValue = WGL_NO_TEXTURE_ARB;
1960             } else {
1961                 if (!use_render_texture_emulation) {
1962                     SetLastError(ERROR_INVALID_DATA);      
1963                     return GL_FALSE;
1964                 }
1965                 switch (object->texture_target) {
1966                     case GL_TEXTURE_1D:       *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
1967                     case GL_TEXTURE_2D:       *piValue = WGL_TEXTURE_1D_ARB; break;
1968                     case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_2D_ARB; break;
1969                 }
1970             }
1971         }
1972         break;
1973
1974     case WGL_MIPMAP_TEXTURE_ARB:
1975         if (use_render_texture_ati) {
1976             pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
1977         } else {
1978             *piValue = GL_FALSE; /** don't support that */
1979             FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
1980         }
1981         break;
1982
1983     default:
1984         FIXME("unexpected attribute %x\n", iAttribute);
1985         break;
1986     }
1987
1988     return GL_TRUE;
1989 }
1990
1991 /* WGL_ARB_pbuffer: wglReleasePbufferDCARB */
1992 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
1993 {
1994     TRACE("(%p, %p)\n", hPbuffer, hdc);
1995     DeleteDC(hdc);
1996     return 0;
1997 }
1998
1999 /* WGL_ARB_pbuffer: wglSetPbufferAttribARB */
2000 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2001 {
2002     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2003     WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2004     if (NULL == object) {
2005         SetLastError(ERROR_INVALID_HANDLE);
2006         return GL_FALSE;
2007     }
2008     if (!object->use_render_texture) {
2009         SetLastError(ERROR_INVALID_HANDLE);
2010         return GL_FALSE;
2011     }
2012     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2013         return GL_TRUE;
2014     }
2015     if (NULL != pglXDrawableAttribARB) {
2016         if (use_render_texture_ati) {
2017             FIXME("Need conversion for GLX_ATI_render_texture\n");
2018         }
2019         return pglXDrawableAttribARB(object->display, object->drawable, piAttribList); 
2020     }
2021     return GL_FALSE;
2022 }
2023
2024 /* WGL_ARB_pixel_format: wglChoosePixelFormatARB */
2025 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2026 {
2027     int gl_test = 0;
2028     int attribs[256];
2029     int nAttribs = 0;
2030     GLboolean res = FALSE;
2031
2032     /* We need the visualid to check if the format is suitable */
2033     VisualID visualid = XVisualIDFromVisual(visual);
2034
2035     GLXFBConfig* cfgs = NULL;
2036     int nCfgs = 0;
2037     UINT it;
2038     int fmt_id;
2039
2040     GLXFBConfig* cfgs_fmt = NULL;
2041     int nCfgs_fmt = 0;
2042     UINT it_fmt;
2043     int tmp_fmt_id;
2044     int tmp_vis_id;
2045
2046     int pfmt_it = 0;
2047     int offscreen_index = 1; /* Start at one because we always have a main visual at iPixelFormat=1 */
2048
2049     TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2050     if (NULL != pfAttribFList) {
2051         FIXME("unused pfAttribFList\n");
2052     }
2053
2054     nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2055     if (-1 == nAttribs) {
2056         WARN("Cannot convert WGL to GLX attributes\n");
2057         return GL_FALSE;
2058     }
2059     PUSH1(attribs, None);
2060
2061     /* Search for FB configurations matching the requirements in attribs */
2062     cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2063     if (NULL == cfgs) {
2064         WARN("Compatible Pixel Format not found\n");
2065         return GL_FALSE;
2066     }
2067
2068     /* Get a list of all FB configurations */
2069     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
2070     if (NULL == cfgs_fmt) {
2071         ERR("Failed to get All FB Configs\n");
2072         XFree(cfgs);
2073         return GL_FALSE;
2074     }
2075
2076     /* Loop through all matching formats and check if they are suitable.
2077     * Note that this function should at max return nMaxFormats different formats */
2078     for (it = 0; pfmt_it < nMaxFormats && it < nCfgs; ++it) {
2079         gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2080         if (gl_test) {
2081             ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2082             continue;
2083         }
2084
2085         gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_VISUAL_ID, &tmp_vis_id);
2086         if (gl_test) {
2087             ERR("Failed to retrieve VISUAL_ID from GLXFBConfig, expect problems.\n");
2088             continue;
2089         }
2090
2091         /* When the visualid of the GLXFBConfig matches the one of the main visual we have found our
2092         * only supported onscreen rendering format. This format has a WGL index of 1. */
2093         if(tmp_vis_id == visualid) {
2094             piFormats[pfmt_it] = 1;
2095             ++pfmt_it;
2096             res = GL_TRUE;
2097             TRACE("Found compatible GLXFBConfig 0x%x with WGL index 1\n", fmt_id);
2098             continue;
2099         }
2100         /* Only continue with this loop for offscreen rendering formats (visualid = 0) */
2101         else if(tmp_vis_id != 0) {
2102             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);
2103             continue;
2104         }
2105
2106         /* Find the index of the found format in the whole format table */
2107         for (it_fmt = 0; it_fmt < nCfgs_fmt; ++it_fmt) {
2108             gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs_fmt[it_fmt], GLX_FBCONFIG_ID, &tmp_fmt_id);
2109             if (gl_test) {
2110                 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2111                 continue;
2112             }
2113             gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_VISUAL_ID, &tmp_vis_id);
2114             if (gl_test) {
2115                 ERR("Failed to retrieve VISUAL_ID from GLXFBConfig, expect problems.\n");
2116                 continue;
2117             }
2118             /* The format of Wine's main visual is stored at index 1 of our WGL format table.
2119             * At higher indices we store offscreen rendering formats (visualid=0). Below we calculate
2120             * the index of the offscreen format. We do this by counting the number of offscreen formats
2121             * which we see until we reach our target format. */ 
2122             if(tmp_vis_id == 0)
2123                 offscreen_index++;
2124
2125             /* We have found the format in the table (note the format is offscreen) */
2126             if (fmt_id == tmp_fmt_id) {
2127                 int tmp;
2128
2129                 piFormats[pfmt_it] = offscreen_index + 1; /* Add 1 to get a one-based index */ 
2130                 ++pfmt_it;
2131                 pglXGetFBConfigAttrib(gdi_display, cfgs_fmt[it_fmt], GLX_ALPHA_SIZE, &tmp);
2132                 TRACE("ALPHA_SIZE of FBCONFIG_ID(%d/%d) found as '%d'\n", it_fmt + 1, nCfgs_fmt, tmp);
2133                 break;
2134             }
2135         }
2136         if (it_fmt == nCfgs_fmt) {
2137             ERR("Failed to get valid fmt for %d. Try next.\n", it);
2138             continue;
2139         }
2140         TRACE("at %d/%d found FBCONFIG_ID(%d/%d)\n", it + 1, nCfgs, piFormats[it], nCfgs_fmt);
2141     }
2142
2143     *nNumFormats = pfmt_it;
2144     /** free list */
2145     XFree(cfgs);
2146     XFree(cfgs_fmt);
2147     return GL_TRUE;
2148 }
2149
2150 /* WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB */
2151 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
2152 {
2153     FIXME("(%p, %d, %d, %d, %p, %p): stub\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2154     return GL_FALSE;
2155 }
2156
2157 /* WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB */
2158 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2159 {
2160     UINT i;
2161     GLXFBConfig* cfgs = NULL;
2162     GLXFBConfig  curCfg = NULL;
2163     int nCfgs = 0;
2164     int hTest;
2165     int tmp;
2166     int curGLXAttr = 0;
2167     int nWGLFormats = 0;
2168     int fmt_index = 0;
2169
2170     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2171
2172     if (0 < iLayerPlane) {
2173         FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2174         return GL_FALSE;
2175     }
2176
2177     cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
2178     if (NULL == cfgs) {
2179         ERR("no FB Configs found for display(%p)\n", gdi_display);
2180         return GL_FALSE;
2181     }
2182
2183     /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supoprted.
2184     * We don't have to fail yet as a program can specify an invaled iPixelFormat (lets say 0) if it wants to query
2185     * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2186     if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &nWGLFormats)) {
2187         ERR("Unable to convert iPixelFormat %d to a GLX one, expect problems!\n", iPixelFormat);
2188     }
2189
2190     for (i = 0; i < nAttributes; ++i) {
2191         const int curWGLAttr = piAttributes[i];
2192         TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2193
2194         switch (curWGLAttr) {
2195             case WGL_NUMBER_PIXEL_FORMATS_ARB:
2196                 piValues[i] = nWGLFormats; 
2197                 continue;
2198
2199             case WGL_SUPPORT_OPENGL_ARB:
2200                 piValues[i] = GL_TRUE; 
2201                 continue;
2202
2203             case WGL_ACCELERATION_ARB:
2204                 curGLXAttr = GLX_CONFIG_CAVEAT;
2205
2206                 if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
2207                     curCfg = cfgs[iPixelFormat - 1];
2208
2209                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2210                 if (hTest) goto get_error;
2211                 switch (tmp) {
2212                     case GLX_NONE: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2213                     case GLX_SLOW_CONFIG: piValues[i] = WGL_NO_ACCELERATION_ARB; break;
2214                     case GLX_NON_CONFORMANT_CONFIG: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2215                     default:
2216                         ERR("unexpected Config Caveat(%x)\n", tmp);
2217                         piValues[i] = WGL_NO_ACCELERATION_ARB;
2218                 }
2219                 continue;
2220
2221             case WGL_TRANSPARENT_ARB:
2222                 curGLXAttr = GLX_TRANSPARENT_TYPE;
2223                     /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2224                      * supported WGLFormats and also check if the GLX fmt_index is valid. */
2225                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2226                     curCfg = cfgs[fmt_index];
2227                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2228                 if (hTest) goto get_error;
2229                     piValues[i] = GL_FALSE;
2230                 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2231                     continue;
2232
2233             case WGL_PIXEL_TYPE_ARB:
2234                 curGLXAttr = GLX_RENDER_TYPE;
2235                 /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2236                 * supported WGLFormats and also check if the GLX fmt_index is valid. */
2237                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2238                     curCfg = cfgs[fmt_index];
2239                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2240                 if (hTest) goto get_error;
2241                 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2242                 if      (tmp & GLX_RGBA_BIT)           { piValues[i] = WGL_TYPE_RGBA_ARB; }
2243                 else if (tmp & GLX_COLOR_INDEX_BIT)    { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2244                 else if (tmp & GLX_RGBA_FLOAT_BIT)     { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2245                 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2246                 else {
2247                     ERR("unexpected RenderType(%x)\n", tmp);
2248                     piValues[i] = WGL_TYPE_RGBA_ARB;
2249                 }
2250                 continue;
2251
2252             case WGL_COLOR_BITS_ARB:
2253                 /** see ConvertAttribWGLtoGLX for explain */
2254                 /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2255                 * supported WGLFormats and also check if the GLX fmt_index is valid. */
2256                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2257                     curCfg = cfgs[fmt_index];
2258                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, GLX_BUFFER_SIZE, piValues + i);
2259                 if (hTest) goto get_error;
2260                 TRACE("WGL_COLOR_BITS_ARB: GLX_BUFFER_SIZE = %d\n", piValues[i]);
2261                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, GLX_ALPHA_SIZE, &tmp);
2262                 if (hTest) goto get_error;
2263                 TRACE("WGL_COLOR_BITS_ARB: GLX_ALPHA_SIZE = %d\n", tmp);
2264                 piValues[i] = piValues[i] - tmp;
2265                 continue;
2266
2267             case WGL_BIND_TO_TEXTURE_RGB_ARB:
2268                 if (use_render_texture_ati) {
2269                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2270                     break;
2271                 }
2272             case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2273                 if (use_render_texture_ati) {
2274                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2275                     break;
2276                 }
2277                 if (!use_render_texture_emulation) {
2278                     piValues[i] = GL_FALSE;
2279                     continue;   
2280                 }
2281                 curGLXAttr = GLX_RENDER_TYPE;
2282                 /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2283                  * supported WGLFormats and also check if the GLX fmt_index is valid. */
2284                 if((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs)) goto pix_error;
2285                     curCfg = cfgs[fmt_index];
2286                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, &tmp);
2287                 if (hTest) goto get_error;
2288                 if (GLX_COLOR_INDEX_BIT == tmp) {
2289                     piValues[i] = GL_FALSE;  
2290                     continue;
2291                 }
2292                 hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, GLX_DRAWABLE_TYPE, &tmp);
2293                 if (hTest) goto get_error;
2294                     piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2295                 continue;
2296
2297             case WGL_BLUE_BITS_ARB:
2298                 curGLXAttr = GLX_BLUE_SIZE;
2299                 break;
2300             case WGL_RED_BITS_ARB:
2301                 curGLXAttr = GLX_RED_SIZE;
2302                 break;
2303             case WGL_GREEN_BITS_ARB:
2304                 curGLXAttr = GLX_GREEN_SIZE;
2305                 break;
2306             case WGL_ALPHA_BITS_ARB:
2307                 curGLXAttr = GLX_ALPHA_SIZE;
2308                 break;
2309             case WGL_DEPTH_BITS_ARB:
2310                 curGLXAttr = GLX_DEPTH_SIZE;
2311                 break;
2312             case WGL_STENCIL_BITS_ARB:
2313                 curGLXAttr = GLX_STENCIL_SIZE;
2314                 break;
2315             case WGL_DOUBLE_BUFFER_ARB:
2316                 curGLXAttr = GLX_DOUBLEBUFFER;
2317                 break;
2318             case WGL_STEREO_ARB:
2319                 curGLXAttr = GLX_STEREO;
2320                 break;
2321             case WGL_AUX_BUFFERS_ARB:
2322                 curGLXAttr = GLX_AUX_BUFFERS;
2323                 break;
2324
2325             case WGL_SUPPORT_GDI_ARB:
2326             case WGL_DRAW_TO_WINDOW_ARB:
2327             case WGL_DRAW_TO_BITMAP_ARB:
2328             case WGL_DRAW_TO_PBUFFER_ARB:
2329                 curGLXAttr = GLX_X_RENDERABLE;
2330                 break;
2331
2332             case WGL_PBUFFER_LARGEST_ARB:
2333                 curGLXAttr = GLX_LARGEST_PBUFFER;
2334                 break;
2335
2336             case WGL_SAMPLE_BUFFERS_ARB:
2337                 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2338                 break;
2339
2340             case WGL_SAMPLES_ARB:
2341                 curGLXAttr = GLX_SAMPLES_ARB;
2342                 break;
2343
2344             default:
2345                 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2346         }
2347
2348         if (0 != curGLXAttr) {
2349             /* Check if the format is supported by checking if iPixelFormat isn't larger than the max number of 
2350             * supported WGLFormats and also check if the GLX fmt_index is valid. */
2351             if((iPixelFormat > 0) && ((iPixelFormat > nWGLFormats) || (fmt_index > nCfgs))) goto pix_error;
2352                 curCfg = cfgs[fmt_index];
2353             hTest = pglXGetFBConfigAttrib(gdi_display, curCfg, curGLXAttr, piValues + i);
2354             if (hTest) goto get_error;
2355         } else { 
2356             piValues[i] = GL_FALSE; 
2357         }
2358     }
2359     return GL_TRUE;
2360
2361 get_error:
2362     ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2363     XFree(cfgs);
2364     return GL_FALSE;
2365
2366 pix_error:
2367     ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nCfgs);
2368     XFree(cfgs);
2369     return GL_FALSE;
2370 }
2371
2372 /* WGL_ARB_render_texture: wglBindTexImageARB */
2373 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2374 {
2375     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2376     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2377     if (NULL == object) {
2378         SetLastError(ERROR_INVALID_HANDLE);
2379         return GL_FALSE;
2380     }
2381     if (!object->use_render_texture) {
2382         SetLastError(ERROR_INVALID_HANDLE);
2383         return GL_FALSE;
2384     }
2385 /* Disable WGL_ARB_render_texture support untill it is implemented properly
2386  * using pbuffers or FBOs */
2387 #if 0
2388     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2389         int do_init = 0;
2390         GLint prev_binded_tex;
2391         pglGetIntegerv(object->texture_target, &prev_binded_tex);
2392         if (NULL == object->render_ctx) {
2393             object->render_hdc = X11DRV_wglGetPbufferDCARB(hPbuffer);
2394             /* FIXME: This is routed through gdi32.dll to winex11.drv, replace this with GLX calls */
2395             object->render_ctx = wglCreateContext(object->render_hdc);
2396             do_init = 1;
2397         }
2398         object->prev_hdc = wglGetCurrentDC();
2399         object->prev_ctx = wglGetCurrentContext();
2400         /* FIXME: This is routed through gdi32.dll to winex11.drv, replace this with GLX calls */
2401         wglMakeCurrent(object->render_hdc, object->render_ctx);
2402         /*
2403         if (do_init) {
2404             glBindTexture(object->texture_target, object->texture);
2405             if (GL_RGBA == object->use_render_texture) {
2406                 glTexImage2D(object->texture_target, 0, GL_RGBA8, object->width, object->height, 0, GL_RGBA, GL_FLOAT, NULL);
2407             } else {
2408                 glTexImage2D(object->texture_target, 0, GL_RGB8, object->width, object->height, 0, GL_RGB, GL_FLOAT, NULL);
2409             }
2410         }
2411         */
2412         object->texture = prev_binded_tex;
2413         return GL_TRUE;
2414     }
2415 #endif
2416     if (NULL != pglXBindTexImageARB) {
2417         return pglXBindTexImageARB(object->display, object->drawable, iBuffer);
2418     }
2419     return GL_FALSE;
2420 }
2421
2422 /* WGL_ARB_render_texture: wglReleaseTexImageARB */
2423 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2424 {
2425     Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2426     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2427     if (NULL == object) {
2428         SetLastError(ERROR_INVALID_HANDLE);
2429         return GL_FALSE;
2430     }
2431     if (!object->use_render_texture) {
2432         SetLastError(ERROR_INVALID_HANDLE);
2433         return GL_FALSE;
2434     }
2435     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2436     /*
2437         GLint prev_binded_tex;
2438         glGetIntegerv(object->texture_target, &prev_binded_tex);    
2439         if (GL_TEXTURE_1D == object->texture_target) {
2440             glCopyTexSubImage1D(object->texture_target, object->texture_level, 0, 0, 0, object->width);
2441         } else {
2442             glCopyTexSubImage2D(object->texture_target, object->texture_level, 0, 0, 0, 0, object->width,       object->height);
2443         }
2444         glBindTexture(object->texture_target, prev_binded_tex);
2445         SwapBuffers(object->render_hdc);
2446         */
2447         pglBindTexture(object->texture_target, object->texture);
2448         if (GL_TEXTURE_1D == object->texture_target) {
2449             pglCopyTexSubImage1D(object->texture_target, object->texture_level, 0, 0, 0, object->width);
2450         } else {
2451             pglCopyTexSubImage2D(object->texture_target, object->texture_level, 0, 0, 0, 0, object->width, object->height);
2452         }
2453
2454         /* FIXME: This is routed through gdi32.dll to winex11.drv, replace this with GLX calls */
2455         wglMakeCurrent(object->prev_hdc, object->prev_ctx);
2456         return GL_TRUE;
2457     }
2458     if (NULL != pglXReleaseTexImageARB) {
2459         return pglXReleaseTexImageARB(object->display, object->drawable, iBuffer);
2460     }
2461     return GL_FALSE;
2462 }
2463
2464 /* WGL_EXT_extensions_string: wglGetExtensionsStringEXT */
2465 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
2466     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2467     return WineGLInfo.wglExtensions;
2468 }
2469
2470 /* WGL_EXT_swap_control: wglGetSwapIntervalEXT */
2471 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
2472     FIXME("(),stub!\n");
2473     return swap_interval;
2474 }
2475
2476 /* WGL_EXT_swap_control: wglSwapIntervalEXT */
2477 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
2478     TRACE("(%d)\n", interval);
2479     swap_interval = interval;
2480     if (NULL != pglXSwapIntervalSGI) {
2481         return 0 == pglXSwapIntervalSGI(interval);
2482     }
2483     WARN("(): GLX_SGI_swap_control extension seems not supported\n");
2484     return TRUE;
2485 }
2486
2487 /* Check if the supported GLX version matches requiredVersion */
2488 static BOOL glxRequireVersion(int requiredVersion)
2489 {
2490     /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2491     if(requiredVersion <= WineGLInfo.glxVersion[1])
2492         return TRUE;
2493
2494     return FALSE;
2495 }
2496
2497 static BOOL glxRequireExtension(const char *requiredExtension)
2498 {
2499     if (strstr(WineGLInfo.glxClientExtensions, requiredExtension) == NULL) {
2500         return FALSE;
2501     }
2502
2503     return TRUE;
2504 }
2505
2506 static BOOL register_extension(const WineGLExtension * ext)
2507 {
2508     int i;
2509
2510     assert( WineGLExtensionListSize < MAX_EXTENSIONS );
2511     WineGLExtensionList[WineGLExtensionListSize++] = ext;
2512
2513     strcat(WineGLInfo.wglExtensions, " ");
2514     strcat(WineGLInfo.wglExtensions, ext->extName);
2515
2516     TRACE("'%s'\n", ext->extName);
2517
2518     for (i = 0; ext->extEntryPoints[i].funcName; ++i)
2519         TRACE("    - '%s'\n", ext->extEntryPoints[i].funcName);
2520
2521     return TRUE;
2522 }
2523
2524 static const WineGLExtension WGL_internal_functions =
2525 {
2526   "",
2527   {
2528     { "wglGetIntegerv", X11DRV_wglGetIntegerv },
2529   }
2530 };
2531
2532
2533 static const WineGLExtension WGL_ARB_extensions_string =
2534 {
2535   "WGL_ARB_extensions_string",
2536   {
2537     { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
2538   }
2539 };
2540
2541 static const WineGLExtension WGL_ARB_make_current_read =
2542 {
2543   "WGL_ARB_make_current_read",
2544   {
2545     { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
2546     { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
2547   }
2548 };
2549
2550 static const WineGLExtension WGL_ARB_multisample =
2551 {
2552   "WGL_ARB_multisample",
2553 };
2554
2555 static const WineGLExtension WGL_ARB_pbuffer =
2556 {
2557   "WGL_ARB_pbuffer",
2558   {
2559     { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
2560     { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
2561     { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
2562     { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
2563     { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
2564     { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
2565   }
2566 };
2567
2568 static const WineGLExtension WGL_ARB_pixel_format =
2569 {
2570   "WGL_ARB_pixel_format",
2571   {
2572     { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
2573     { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
2574     { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
2575   }
2576 };
2577
2578 static const WineGLExtension WGL_ARB_render_texture =
2579 {
2580   "WGL_ARB_render_texture",
2581   {
2582     { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
2583     { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
2584   }
2585 };
2586
2587 static const WineGLExtension WGL_EXT_extensions_string =
2588 {
2589   "WGL_EXT_extensions_string",
2590   {
2591     { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
2592   }
2593 };
2594
2595 static const WineGLExtension WGL_EXT_swap_control =
2596 {
2597   "WGL_EXT_swap_control",
2598   {
2599     { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
2600     { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
2601   }
2602 };
2603
2604
2605 /**
2606  * X11DRV_WineGL_LoadExtensions
2607  */
2608 static void X11DRV_WineGL_LoadExtensions(void)
2609 {
2610     WineGLInfo.wglExtensions[0] = 0;
2611
2612     /* Load Wine internal functions */
2613     register_extension(&WGL_internal_functions);
2614
2615     /* ARB Extensions */
2616
2617     register_extension(&WGL_ARB_extensions_string);
2618
2619     if (glxRequireVersion(3))
2620         register_extension(&WGL_ARB_make_current_read);
2621
2622     if (glxRequireExtension("GLX_ARB_multisample"))
2623         register_extension(&WGL_ARB_multisample);
2624
2625     if (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer"))
2626         register_extension(&WGL_ARB_pbuffer);
2627
2628     register_extension(&WGL_ARB_pixel_format);
2629
2630     if (glxRequireExtension("GLX_ATI_render_texture") ||
2631         glxRequireExtension("GLX_ARB_render_texture"))
2632         register_extension(&WGL_ARB_render_texture);
2633
2634     /* EXT Extensions */
2635
2636     register_extension(&WGL_EXT_extensions_string);
2637
2638     if (glxRequireExtension("GLX_SGI_swap_control"))
2639         register_extension(&WGL_EXT_swap_control);
2640 }
2641
2642
2643 static XID create_glxpixmap(X11DRV_PDEVICE *physDev)
2644 {
2645     GLXPixmap ret;
2646     XVisualInfo *vis;
2647     XVisualInfo template;
2648     int num;
2649
2650     wine_tsx11_lock();
2651
2652     /* Retrieve the visualid from our main visual which is the only visual we can use */
2653     template.visualid =  XVisualIDFromVisual(visual);
2654     vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
2655
2656     ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
2657     XFree(vis);
2658     wine_tsx11_unlock(); 
2659     TRACE("return %lx\n", ret);
2660     return ret;
2661 }
2662
2663 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
2664 {
2665     Drawable ret;
2666
2667     if(physDev->bitmap)
2668     {
2669         if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
2670             ret = physDev->drawable; /* PBuffer */
2671         else
2672         {
2673             if(!physDev->bitmap->glxpixmap)
2674                 physDev->bitmap->glxpixmap = create_glxpixmap(physDev);
2675             ret = physDev->bitmap->glxpixmap;
2676         }
2677     }
2678     else
2679         ret = physDev->drawable;
2680     return ret;
2681 }
2682
2683 BOOL destroy_glxpixmap(XID glxpixmap)
2684 {
2685     wine_tsx11_lock(); 
2686     pglXDestroyGLXPixmap(gdi_display, glxpixmap);
2687     wine_tsx11_unlock(); 
2688     return TRUE;
2689 }
2690
2691 /**
2692  * X11DRV_SwapBuffers
2693  *
2694  * Swap the buffers of this DC
2695  */
2696 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
2697 {
2698   GLXDrawable drawable;
2699   if (!has_opengl()) {
2700     ERR("No libGL on this box - disabling OpenGL support !\n");
2701     return 0;
2702   }
2703   
2704   TRACE_(opengl)("(%p)\n", physDev);
2705
2706   drawable = get_glxdrawable(physDev);
2707   wine_tsx11_lock();
2708   pglXSwapBuffers(gdi_display, drawable);
2709   wine_tsx11_unlock();
2710
2711   /* FPS support */
2712   if (TRACE_ON(fps))
2713   {
2714       static long prev_time, frames;
2715
2716       DWORD time = GetTickCount();
2717       frames++;
2718       /* every 1.5 seconds */
2719       if (time - prev_time > 1500) {
2720           TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
2721           prev_time = time;
2722           frames = 0;
2723       }
2724   }
2725
2726   return TRUE;
2727 }
2728
2729 /***********************************************************************
2730  *              X11DRV_setup_opengl_visual
2731  *
2732  * Setup the default visual used for OpenGL and Direct3D, and the desktop
2733  * window (if it exists).  If OpenGL isn't available, the visual is simply
2734  * set to the default visual for the display
2735  */
2736 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
2737 {
2738     XVisualInfo *visual = NULL;
2739     /* In order to support OpenGL or D3D, we require a double-buffered visual and stencil buffer support, */
2740     int dblBuf[] = {GLX_RGBA,GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_ALPHA_SIZE, 8, GLX_DOUBLEBUFFER, None};
2741     if (!has_opengl()) return NULL;
2742
2743     wine_tsx11_lock();
2744     visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf);
2745     wine_tsx11_unlock();
2746     if (visual == NULL) {
2747         /* fallback to 16 bits depth, no alpha */
2748         int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, GLX_DOUBLEBUFFER, None};
2749         WARN("Failed to get a visual with at least 24 bits depth\n");
2750
2751         wine_tsx11_lock();
2752         visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
2753         wine_tsx11_unlock();
2754         if (visual == NULL) {
2755             /* fallback to no stencil */
2756             int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
2757             WARN("Failed to get a visual with at least 8 bits of stencil\n");
2758
2759             wine_tsx11_lock();
2760             visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
2761             wine_tsx11_unlock();
2762             if (visual == NULL) {
2763                 /* This should only happen if we cannot find a match with a depth size 16 */
2764                 FIXME("Failed to find a suitable visual\n");
2765                 return visual;
2766             }
2767         }
2768     }
2769     TRACE("Visual ID %lx Chosen\n",visual->visualid);
2770     return visual;
2771 }
2772
2773 #else  /* no OpenGL includes */
2774
2775 /***********************************************************************
2776  *              ChoosePixelFormat (X11DRV.@)
2777  */
2778 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
2779                              const PIXELFORMATDESCRIPTOR *ppfd) {
2780   ERR("No OpenGL support compiled in.\n");
2781
2782   return 0;
2783 }
2784
2785 /***********************************************************************
2786  *              DescribePixelFormat (X11DRV.@)
2787  */
2788 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
2789                                int iPixelFormat,
2790                                UINT nBytes,
2791                                PIXELFORMATDESCRIPTOR *ppfd) {
2792   ERR("No OpenGL support compiled in.\n");
2793
2794   return 0;
2795 }
2796
2797 /***********************************************************************
2798  *              GetPixelFormat (X11DRV.@)
2799  */
2800 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
2801   ERR("No OpenGL support compiled in.\n");
2802
2803   return 0;
2804 }
2805
2806 /***********************************************************************
2807  *              SetPixelFormat (X11DRV.@)
2808  */
2809 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
2810                            int iPixelFormat,
2811                            const PIXELFORMATDESCRIPTOR *ppfd) {
2812   ERR("No OpenGL support compiled in.\n");
2813
2814   return FALSE;
2815 }
2816
2817 /***********************************************************************
2818  *              SwapBuffers (X11DRV.@)
2819  */
2820 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
2821   ERR_(opengl)("No OpenGL support compiled in.\n");
2822
2823   return FALSE;
2824 }
2825
2826 /* OpenGL32 wglCreateContext */
2827 HGLRC X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
2828     ERR_(opengl)("No OpenGL support compiled in.\n");
2829     return NULL;
2830 }
2831
2832 /* OpenGL32 wglDeleteContext */
2833 BOOL X11DRV_wglDeleteContext(HGLRC hglrc) {
2834     ERR_(opengl)("No OpenGL support compiled in.\n");
2835     return FALSE;
2836 }
2837
2838 /* OpenGL32: wglGetProcAddress */
2839 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
2840     ERR_(opengl)("No OpenGL support compiled in.\n");
2841     return NULL;
2842 }
2843
2844 HDC X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, HPBUFFERARB hPbuffer)
2845 {
2846     ERR_(opengl)("No OpenGL support compiled in.\n");
2847     return NULL;
2848 }
2849
2850 BOOL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
2851     ERR_(opengl)("No OpenGL support compiled in.\n");
2852     return FALSE;
2853 }
2854
2855 /* OpenGL32 wglMakeCurrent */
2856 BOOL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
2857     ERR_(opengl)("No OpenGL support compiled in.\n");
2858     return FALSE;
2859 }
2860
2861 /* OpenGL32 wglShaderLists */
2862 BOOL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
2863     ERR_(opengl)("No OpenGL support compiled in.\n");
2864     return FALSE;
2865 }
2866
2867 /* OpenGL32 wglUseFontBitmapsA */
2868 BOOL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2869 {
2870     ERR_(opengl)("No OpenGL support compiled in.\n");
2871     return FALSE;
2872 }
2873
2874 /* OpenGL32 wglUseFontBitmapsW */
2875 BOOL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2876 {
2877     ERR_(opengl)("No OpenGL support compiled in.\n");
2878     return FALSE;
2879 }
2880
2881 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
2882 {
2883   return NULL;
2884 }
2885
2886 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
2887 {
2888     return 0;
2889 }
2890
2891 BOOL destroy_glxpixmap(XID glxpixmap)
2892 {
2893     return FALSE;
2894 }
2895
2896 #endif /* defined(HAVE_OPENGL) */