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