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