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