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