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