jscript: Use prototype for builtin Date properties.
[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-2009 Roderick Colenbrander
8  * Copyright 2006 Tomas Carnecky
9  * Copyright 2012 Alexandre Julliard
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_SYS_UN_H
37 #include <sys/un.h>
38 #endif
39 #ifdef HAVE_GL_GL_H
40 # include <GL/gl.h>
41 #endif
42 #ifdef HAVE_GL_GLX_H
43 # include <GL/glx.h>
44 #endif
45 #undef APIENTRY
46 #undef GLAPI
47 #undef WINGDIAPI
48
49 #include "x11drv.h"
50 #include "winternl.h"
51 #include "wine/library.h"
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
55
56 #ifdef SONAME_LIBGL
57
58 WINE_DECLARE_DEBUG_CHANNEL(winediag);
59 WINE_DECLARE_DEBUG_CHANNEL(fps);
60
61 #include "wine/wgl_driver.h"
62 #include "wine/wglext.h"
63
64 /* For compatibility with old Mesa headers */
65 #ifndef GLX_SAMPLE_BUFFERS_ARB
66 # define GLX_SAMPLE_BUFFERS_ARB           100000
67 #endif
68 #ifndef GLX_SAMPLES_ARB
69 # define GLX_SAMPLES_ARB                  100001
70 #endif
71 #ifndef GL_TEXTURE_CUBE_MAP
72 # define GL_TEXTURE_CUBE_MAP              0x8513
73 #endif
74 #ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT
75 # define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
76 #endif
77 #ifndef GLX_EXT_fbconfig_packed_float
78 # define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
79 # define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT  0x00000008
80 #endif
81 #ifndef GLX_ARB_create_context
82 # define GLX_CONTEXT_MAJOR_VERSION_ARB    0x2091
83 # define GLX_CONTEXT_MINOR_VERSION_ARB    0x2092
84 # define GLX_CONTEXT_FLAGS_ARB            0x2094
85 #endif
86 #ifndef GLX_ARB_create_context_profile
87 # define GLX_CONTEXT_PROFILE_MASK_ARB     0x9126
88 #endif
89 /** GLX_ATI_pixel_format_float */
90 #define GLX_RGBA_FLOAT_ATI_BIT            0x00000100
91 /** GLX_ARB_pixel_format_float */
92 #define GLX_RGBA_FLOAT_BIT                0x00000004
93 #define GLX_RGBA_FLOAT_TYPE               0x20B9
94 /** GL_NV_float_buffer */
95 #define GL_FLOAT_R_NV                     0x8880
96 #define GL_FLOAT_RG_NV                    0x8881
97 #define GL_FLOAT_RGB_NV                   0x8882
98 #define GL_FLOAT_RGBA_NV                  0x8883
99 #define GL_FLOAT_R16_NV                   0x8884
100 #define GL_FLOAT_R32_NV                   0x8885
101 #define GL_FLOAT_RG16_NV                  0x8886
102 #define GL_FLOAT_RG32_NV                  0x8887
103 #define GL_FLOAT_RGB16_NV                 0x8888
104 #define GL_FLOAT_RGB32_NV                 0x8889
105 #define GL_FLOAT_RGBA16_NV                0x888A
106 #define GL_FLOAT_RGBA32_NV                0x888B
107 #define GL_TEXTURE_FLOAT_COMPONENTS_NV    0x888C
108 #define GL_FLOAT_CLEAR_COLOR_VALUE_NV     0x888D
109 #define GL_FLOAT_RGBA_MODE_NV             0x888E
110 /** GLX_NV_float_buffer */
111 #define GLX_FLOAT_COMPONENTS_NV           0x20B0
112
113
114 struct WineGLInfo {
115     const char *glVersion;
116     char *glExtensions;
117
118     int glxVersion[2];
119
120     const char *glxServerVersion;
121     const char *glxServerVendor;
122     const char *glxServerExtensions;
123
124     const char *glxClientVersion;
125     const char *glxClientVendor;
126     const char *glxClientExtensions;
127
128     const char *glxExtensions;
129
130     BOOL glxDirect;
131     char wglExtensions[4096];
132 };
133
134 typedef struct wine_glpixelformat {
135     int         iPixelFormat;
136     GLXFBConfig fbconfig;
137     int         fmt_id;
138     int         render_type;
139     BOOL        offscreenOnly;
140     DWORD       dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
141 } WineGLPixelFormat;
142
143 struct wgl_context
144 {
145     HDC hdc;
146     BOOL has_been_current;
147     BOOL sharing;
148     BOOL gl3_context;
149     XVisualInfo *vis;
150     WineGLPixelFormat *fmt;
151     int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
152     int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
153     GLXContext ctx;
154     HDC read_hdc;
155     Drawable drawables[2];
156     BOOL refresh_drawables;
157     Pixmap pixmap;            /* pixmap for memory DCs */
158     GLXPixmap glxpixmap;      /* GLX pixmap for memory DCs */
159     SIZE pixmap_size;         /* pixmap size for memory DCs */
160     struct list entry;
161 };
162
163 struct wgl_pbuffer
164 {
165     Drawable   drawable;
166     WineGLPixelFormat* fmt;
167     int        width;
168     int        height;
169     int*       attribList;
170     int        use_render_texture; /* This is also the internal texture format */
171     int        texture_bind_target;
172     int        texture_bpp;
173     GLint      texture_format;
174     GLuint     texture_target;
175     GLenum     texture_type;
176     GLuint     texture;
177     int        texture_level;
178 };
179
180 struct glx_physdev
181 {
182     struct gdi_physdev dev;
183     X11DRV_PDEVICE    *x11dev;
184     enum dc_gl_type    type;          /* type of GL device context */
185     int                pixel_format;
186     Drawable           drawable;
187     Pixmap             pixmap;        /* pixmap for a DL_GL_PIXMAP_WIN drawable */
188 };
189
190 static const struct gdi_dc_funcs glxdrv_funcs;
191
192 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
193 {
194     return (struct glx_physdev *)dev;
195 }
196
197 static struct list context_list = LIST_INIT( context_list );
198 static struct WineGLInfo WineGLInfo = { 0 };
199 static int use_render_texture_emulation = 1;
200 static BOOL has_swap_control;
201 static int swap_interval = 1;
202
203 static struct opengl_funcs opengl_funcs;
204
205 #define USE_GL_FUNC(name) #name,
206 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
207 #undef USE_GL_FUNC
208
209 static void X11DRV_WineGL_LoadExtensions(void);
210 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count);
211 static BOOL glxRequireVersion(int requiredVersion);
212 static BOOL glxRequireExtension(const char *requiredExtension);
213
214 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
215   TRACE("  - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
216   TRACE("  - dwFlags : ");
217 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
218   TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
219   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
220   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
221   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
222   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
223   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
224   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
225   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
226   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
227   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
228   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
229   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
230   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
231   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
232   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
233   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
234   /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
235    * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
236   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
237 #undef TEST_AND_DUMP
238   TRACE("\n");
239
240   TRACE("  - iPixelType : ");
241   switch (ppfd->iPixelType) {
242   case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
243   case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
244   }
245   TRACE("\n");
246
247   TRACE("  - Color   : %d\n", ppfd->cColorBits);
248   TRACE("  - Red     : %d\n", ppfd->cRedBits);
249   TRACE("  - Green   : %d\n", ppfd->cGreenBits);
250   TRACE("  - Blue    : %d\n", ppfd->cBlueBits);
251   TRACE("  - Alpha   : %d\n", ppfd->cAlphaBits);
252   TRACE("  - Accum   : %d\n", ppfd->cAccumBits);
253   TRACE("  - Depth   : %d\n", ppfd->cDepthBits);
254   TRACE("  - Stencil : %d\n", ppfd->cStencilBits);
255   TRACE("  - Aux     : %d\n", ppfd->cAuxBuffers);
256
257   TRACE("  - iLayerType : ");
258   switch (ppfd->iLayerType) {
259   case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
260   case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
261   case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
262   }
263   TRACE("\n");
264 }
265
266 #define PUSH1(attribs,att)        do { attribs[nAttribs++] = (att); } while (0)
267 #define PUSH2(attribs,att,value)  do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
268
269 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
270 /* GLX 1.0 */
271 MAKE_FUNCPTR(glXChooseVisual)
272 MAKE_FUNCPTR(glXCopyContext)
273 MAKE_FUNCPTR(glXCreateContext)
274 MAKE_FUNCPTR(glXCreateGLXPixmap)
275 MAKE_FUNCPTR(glXGetCurrentContext)
276 MAKE_FUNCPTR(glXGetCurrentDrawable)
277 MAKE_FUNCPTR(glXDestroyContext)
278 MAKE_FUNCPTR(glXDestroyGLXPixmap)
279 MAKE_FUNCPTR(glXGetConfig)
280 MAKE_FUNCPTR(glXIsDirect)
281 MAKE_FUNCPTR(glXMakeCurrent)
282 MAKE_FUNCPTR(glXSwapBuffers)
283 MAKE_FUNCPTR(glXQueryExtension)
284 MAKE_FUNCPTR(glXQueryVersion)
285
286 /* GLX 1.1 */
287 MAKE_FUNCPTR(glXGetClientString)
288 MAKE_FUNCPTR(glXQueryExtensionsString)
289 MAKE_FUNCPTR(glXQueryServerString)
290
291 /* GLX 1.3 */
292 MAKE_FUNCPTR(glXGetFBConfigs)
293 MAKE_FUNCPTR(glXChooseFBConfig)
294 MAKE_FUNCPTR(glXCreatePbuffer)
295 MAKE_FUNCPTR(glXCreateNewContext)
296 MAKE_FUNCPTR(glXDestroyPbuffer)
297 MAKE_FUNCPTR(glXGetFBConfigAttrib)
298 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
299 MAKE_FUNCPTR(glXMakeContextCurrent)
300 MAKE_FUNCPTR(glXQueryDrawable)
301 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
302 #undef MAKE_FUNCPTR
303
304 /* GLX Extensions */
305 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
306 static void* (*pglXGetProcAddressARB)(const GLubyte *);
307 static int   (*pglXSwapIntervalSGI)(int);
308
309 /* NV GLX Extension */
310 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
311 static void  (*pglXFreeMemoryNV)(GLvoid *pointer);
312
313 /* MESA GLX Extensions */
314 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
315
316 /* Standard OpenGL */
317 static void (*pglFinish)(void);
318 static void (*pglFlush)(void);
319
320 static void wglFinish(void);
321 static void wglFlush(void);
322
323 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
324 {
325     /* In the future we might want to find the exact X or GLX error to report back to the app */
326     return 1;
327 }
328
329 static BOOL infoInitialized = FALSE;
330 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
331 {
332     int screen = DefaultScreen(gdi_display);
333     Window win = 0, root = 0;
334     const char *gl_renderer;
335     const char* str;
336     XVisualInfo *vis;
337     GLXContext ctx = NULL;
338     XSetWindowAttributes attr;
339     BOOL ret = FALSE;
340     int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
341
342     if (infoInitialized)
343         return TRUE;
344     infoInitialized = TRUE;
345
346     attr.override_redirect = True;
347     attr.colormap = None;
348     attr.border_pixel = 0;
349
350     wine_tsx11_lock();
351
352     vis = pglXChooseVisual(gdi_display, screen, attribList);
353     if (vis) {
354 #ifdef __i386__
355         WORD old_fs = wine_get_fs();
356         /* Create a GLX Context. Without one we can't query GL information */
357         ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
358         if (wine_get_fs() != old_fs)
359         {
360             wine_set_fs( old_fs );
361             ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
362             ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
363             goto done;
364         }
365 #else
366         ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
367 #endif
368     }
369     if (!ctx) goto done;
370
371     root = RootWindow( gdi_display, vis->screen );
372     if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
373         attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
374     if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
375                               vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
376         XMapWindow( gdi_display, win );
377     else
378         win = root;
379
380     if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
381     {
382         ERR_(winediag)( "Unable to activate OpenGL context, most likely your OpenGL drivers haven't been installed correctly\n" );
383         goto done;
384     }
385     gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
386     WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
387     str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
388     WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
389     strcpy(WineGLInfo.glExtensions, str);
390
391     /* Get the common GLX version supported by GLX client and server ( major/minor) */
392     pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
393
394     WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
395     WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
396     WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
397
398     WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
399     WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
400     WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
401
402     WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
403     WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
404
405     TRACE("GL version             : %s.\n", WineGLInfo.glVersion);
406     TRACE("GL renderer            : %s.\n", gl_renderer);
407     TRACE("GLX version            : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
408     TRACE("Server GLX version     : %s.\n", WineGLInfo.glxServerVersion);
409     TRACE("Server GLX vendor:     : %s.\n", WineGLInfo.glxServerVendor);
410     TRACE("Client GLX version     : %s.\n", WineGLInfo.glxClientVersion);
411     TRACE("Client GLX vendor:     : %s.\n", WineGLInfo.glxClientVendor);
412     TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
413
414     if(!WineGLInfo.glxDirect)
415     {
416         int fd = ConnectionNumber(gdi_display);
417         struct sockaddr_un uaddr;
418         unsigned int uaddrlen = sizeof(struct sockaddr_un);
419
420         /* In general indirect rendering on a local X11 server indicates a driver problem.
421          * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
422          */
423         if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
424             ERR_(winediag)("Direct rendering is disabled, most likely your OpenGL drivers "
425                            "haven't been installed correctly (using GL renderer %s, version %s).\n",
426                            debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
427     }
428     else
429     {
430         /* In general you would expect that if direct rendering is returned, that you receive hardware
431          * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
432          * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
433          * software rendering, it shows direct rendering.
434          *
435          * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
436          * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
437          * it shows 'Mesa X11'.
438          */
439         if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
440             ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your OpenGL "
441                            "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
442                            debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
443     }
444     ret = TRUE;
445
446 done:
447     if(vis) XFree(vis);
448     if(ctx) {
449         pglXMakeCurrent(gdi_display, None, NULL);    
450         pglXDestroyContext(gdi_display, ctx);
451     }
452     if (win != root) XDestroyWindow( gdi_display, win );
453     if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
454     wine_tsx11_unlock();
455     if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
456     return ret;
457 }
458
459 static BOOL has_opengl(void)
460 {
461     static int init_done;
462     static void *opengl_handle;
463
464     char buffer[200];
465     int error_base, event_base;
466     unsigned int i;
467
468     if (init_done) return (opengl_handle != NULL);
469     init_done = 1;
470
471     /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
472        and include all dependencies */
473     opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
474     if (opengl_handle == NULL)
475     {
476         ERR( "Failed to load libGL: %s\n", buffer );
477         ERR( "OpenGL support is disabled.\n");
478         return FALSE;
479     }
480
481     for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
482     {
483         if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
484         {
485             ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
486             goto failed;
487         }
488     }
489
490     /* redirect some standard OpenGL functions */
491 #define REDIRECT(func) \
492     do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
493     REDIRECT( glFinish );
494     REDIRECT( glFlush );
495 #undef REDIRECT
496
497     pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
498     if (pglXGetProcAddressARB == NULL) {
499         ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
500         goto failed;
501     }
502
503 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
504     { \
505         ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
506         goto failed; \
507     } while(0)
508
509     /* GLX 1.0 */
510     LOAD_FUNCPTR(glXChooseVisual);
511     LOAD_FUNCPTR(glXCopyContext);
512     LOAD_FUNCPTR(glXCreateContext);
513     LOAD_FUNCPTR(glXCreateGLXPixmap);
514     LOAD_FUNCPTR(glXGetCurrentContext);
515     LOAD_FUNCPTR(glXGetCurrentDrawable);
516     LOAD_FUNCPTR(glXDestroyContext);
517     LOAD_FUNCPTR(glXDestroyGLXPixmap);
518     LOAD_FUNCPTR(glXGetConfig);
519     LOAD_FUNCPTR(glXIsDirect);
520     LOAD_FUNCPTR(glXMakeCurrent);
521     LOAD_FUNCPTR(glXSwapBuffers);
522     LOAD_FUNCPTR(glXQueryExtension);
523     LOAD_FUNCPTR(glXQueryVersion);
524
525     /* GLX 1.1 */
526     LOAD_FUNCPTR(glXGetClientString);
527     LOAD_FUNCPTR(glXQueryExtensionsString);
528     LOAD_FUNCPTR(glXQueryServerString);
529
530     /* GLX 1.3 */
531     LOAD_FUNCPTR(glXCreatePbuffer);
532     LOAD_FUNCPTR(glXCreateNewContext);
533     LOAD_FUNCPTR(glXDestroyPbuffer);
534     LOAD_FUNCPTR(glXMakeContextCurrent);
535     LOAD_FUNCPTR(glXGetCurrentReadDrawable);
536     LOAD_FUNCPTR(glXGetFBConfigs);
537 #undef LOAD_FUNCPTR
538
539 /* It doesn't matter if these fail. They'll only be used if the driver reports
540    the associated extension is available (and if a driver reports the extension
541    is available but fails to provide the functions, it's quite broken) */
542 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
543     /* ARB GLX Extension */
544     LOAD_FUNCPTR(glXCreateContextAttribsARB);
545     /* SGI GLX Extension */
546     LOAD_FUNCPTR(glXSwapIntervalSGI);
547     /* NV GLX Extension */
548     LOAD_FUNCPTR(glXAllocateMemoryNV);
549     LOAD_FUNCPTR(glXFreeMemoryNV);
550 #undef LOAD_FUNCPTR
551
552     if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
553
554     wine_tsx11_lock();
555     if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
556         TRACE("GLX is up and running error_base = %d\n", error_base);
557     } else {
558         wine_tsx11_unlock();
559         ERR( "GLX extension is missing, disabling OpenGL.\n" );
560         goto failed;
561     }
562
563     /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
564      * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
565      * rendering is used.
566      *
567      * The main problem for our OpenGL code is that we need certain GLX calls but their presence
568      * depends on the reported GLX client / server version and on the client / server extension list.
569      * Those don't have to be the same.
570      *
571      * In general the server GLX information lists the capabilities in case of indirect rendering.
572      * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
573      * available and in that case the client GLX informat can be used.
574      * OpenGL programs should use the 'intersection' of both sets of information which is advertised
575      * in the GLX version/extension list. When a program does this it works for certain for both
576      * direct and indirect rendering.
577      *
578      * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
579      * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
580      * extension list which causes this extension not to be listed. (Wine requires this extension).
581      * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
582      * pbuffers is around.
583      *
584      * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
585      * the ATI drivers and from then on use GLX client information for them.
586      */
587
588     if(glxRequireVersion(3)) {
589         pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
590         pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
591         pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
592         pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
593     } else if(glxRequireExtension("GLX_SGIX_fbconfig")) {
594         pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
595         pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
596         pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
597
598         /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
599          * enable this function when the Xserver understand GLX 1.3 or newer
600          */
601         pglXQueryDrawable = NULL;
602      } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
603         TRACE("Overriding ATI GLX capabilities!\n");
604         pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
605         pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
606         pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
607         pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
608
609         /* Use client GLX information in case of the ATI drivers. We override the
610          * capabilities over here and not somewhere else as ATI might better their
611          * life in the future. In case they release proper drivers this block of
612          * code won't be called. */
613         WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
614     } else {
615          ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
616     }
617
618     if(glxRequireExtension("GLX_MESA_copy_sub_buffer")) {
619         pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
620     }
621
622     X11DRV_WineGL_LoadExtensions();
623
624     wine_tsx11_unlock();
625     return TRUE;
626
627 failed:
628     wine_dlclose(opengl_handle, NULL, 0);
629     opengl_handle = NULL;
630     return FALSE;
631 }
632
633 static int describeContext( struct wgl_context *ctx ) {
634     int tmp;
635     int ctx_vis_id;
636     TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
637     pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
638     TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
639     pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
640     TRACE(" - VISUAL_ID 0x%x\n", tmp);
641     ctx_vis_id = tmp;
642     return ctx_vis_id;
643 }
644
645 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
646   int nAttribs = 0;
647   unsigned cur = 0; 
648   int pop;
649   int drawattrib = 0;
650   int nvfloatattrib = GLX_DONT_CARE;
651   int pixelattrib = GLX_DONT_CARE;
652
653   /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
654    * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
655   while (iWGLAttr && 0 != iWGLAttr[cur]) {
656     TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
657
658     switch (iWGLAttr[cur]) {
659     case WGL_AUX_BUFFERS_ARB:
660       pop = iWGLAttr[++cur];
661       PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
662       TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
663       break;
664     case WGL_COLOR_BITS_ARB:
665       pop = iWGLAttr[++cur];
666       PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
667       TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
668       break;
669     case WGL_BLUE_BITS_ARB:
670       pop = iWGLAttr[++cur];
671       PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
672       TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
673       break;
674     case WGL_RED_BITS_ARB:
675       pop = iWGLAttr[++cur];
676       PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
677       TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
678       break;
679     case WGL_GREEN_BITS_ARB:
680       pop = iWGLAttr[++cur];
681       PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
682       TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
683       break;
684     case WGL_ALPHA_BITS_ARB:
685       pop = iWGLAttr[++cur];
686       PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
687       TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
688       break;
689     case WGL_DEPTH_BITS_ARB:
690       pop = iWGLAttr[++cur];
691       PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
692       TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
693       break;
694     case WGL_STENCIL_BITS_ARB:
695       pop = iWGLAttr[++cur];
696       PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
697       TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
698       break;
699     case WGL_DOUBLE_BUFFER_ARB:
700       pop = iWGLAttr[++cur];
701       PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
702       TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
703       break;
704     case WGL_STEREO_ARB:
705       pop = iWGLAttr[++cur];
706       PUSH2(oGLXAttr, GLX_STEREO, pop);
707       TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
708       break;
709
710     case WGL_PIXEL_TYPE_ARB:
711       pop = iWGLAttr[++cur];
712       TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
713       switch (pop) {
714       case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
715       case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
716       /* 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 */
717       case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
718       case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
719       default:
720         ERR("unexpected PixelType(%x)\n", pop); 
721         pop = 0;
722       }
723       break;
724
725     case WGL_SUPPORT_GDI_ARB:
726       /* This flag is set in a WineGLPixelFormat */
727       pop = iWGLAttr[++cur];
728       TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
729       break;
730
731     case WGL_DRAW_TO_BITMAP_ARB:
732       /* This flag is set in a WineGLPixelFormat */
733       pop = iWGLAttr[++cur];
734       TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
735       break;
736
737     case WGL_DRAW_TO_WINDOW_ARB:
738       pop = iWGLAttr[++cur];
739       TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
740       /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
741       if (pop) {
742         drawattrib |= GLX_WINDOW_BIT;
743       }
744       break;
745
746     case WGL_DRAW_TO_PBUFFER_ARB:
747       pop = iWGLAttr[++cur];
748       TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
749       /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
750       if (pop) {
751         drawattrib |= GLX_PBUFFER_BIT;
752       }
753       break;
754
755     case WGL_ACCELERATION_ARB:
756       /* This flag is set in a WineGLPixelFormat */
757       pop = iWGLAttr[++cur];
758       TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
759       break;
760
761     case WGL_SUPPORT_OPENGL_ARB:
762       pop = iWGLAttr[++cur];
763       /** nothing to do, if we are here, supposing support Accelerated OpenGL */
764       TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
765       break;
766
767     case WGL_SWAP_METHOD_ARB:
768       pop = iWGLAttr[++cur];
769       /* For now we ignore this and just return SWAP_EXCHANGE */
770       TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
771       break;
772
773     case WGL_PBUFFER_LARGEST_ARB:
774       pop = iWGLAttr[++cur];
775       PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
776       TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
777       break;
778
779     case WGL_SAMPLE_BUFFERS_ARB:
780       pop = iWGLAttr[++cur];
781       PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
782       TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
783       break;
784
785     case WGL_SAMPLES_ARB:
786       pop = iWGLAttr[++cur];
787       PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
788       TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
789       break;
790
791     case WGL_TEXTURE_FORMAT_ARB:
792     case WGL_TEXTURE_TARGET_ARB:
793     case WGL_MIPMAP_TEXTURE_ARB:
794       TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
795       pop = iWGLAttr[++cur];
796       if (NULL == pbuf) {
797         ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
798       }
799       if (!use_render_texture_emulation) {
800         if (WGL_NO_TEXTURE_ARB != pop) {
801           ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
802           return -1; /** error: don't support it */
803         } else {
804           drawattrib |= GLX_PBUFFER_BIT;
805         }
806       }
807       break ;
808     case WGL_FLOAT_COMPONENTS_NV:
809       nvfloatattrib = iWGLAttr[++cur];
810       TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
811       break ;
812     case WGL_BIND_TO_TEXTURE_DEPTH_NV:
813     case WGL_BIND_TO_TEXTURE_RGB_ARB:
814     case WGL_BIND_TO_TEXTURE_RGBA_ARB:
815     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
816     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
817     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
818     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
819       pop = iWGLAttr[++cur];
820       /** cannot be converted, see direct handling on 
821        *   - wglGetPixelFormatAttribivARB
822        *  TODO: wglChoosePixelFormat
823        */
824       break ;
825     case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
826       pop = iWGLAttr[++cur];
827       PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
828       TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
829       break ;
830
831     case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
832       pop = iWGLAttr[++cur];
833       PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
834       TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
835       break ;
836     default:
837       FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
838       break;
839     }
840     ++cur;
841   }
842
843   /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
844    * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
845    * pixmap and pbuffer formats appear as well. */
846   if (!drawattrib) drawattrib = GLX_DONT_CARE;
847   PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
848   TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
849
850   /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
851    * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
852    * GLX_DONT_CARE unless it is overridden. */
853   PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
854   TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
855
856   /* Set GLX_FLOAT_COMPONENTS_NV all the time */
857   if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
858     PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
859     TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
860   }
861
862   return nAttribs;
863 }
864
865 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
866 {
867     int render_type=0, render_type_bit;
868     pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
869     switch(render_type_bit)
870     {
871         case GLX_RGBA_BIT:
872             render_type = GLX_RGBA_TYPE;
873             break;
874         case GLX_COLOR_INDEX_BIT:
875             render_type = GLX_COLOR_INDEX_TYPE;
876             break;
877         case GLX_RGBA_FLOAT_BIT:
878             render_type = GLX_RGBA_FLOAT_TYPE;
879             break;
880         case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
881             render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
882             break;
883         default:
884             ERR("Unknown render_type: %x\n", render_type_bit);
885     }
886     return render_type;
887 }
888
889 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
890 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
891 {
892     int dbuf, value;
893     pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
894     pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
895
896     /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
897      * the GLX_PIXMAP_BIT set. */
898     return !dbuf && (value & GLX_PIXMAP_BIT);
899 }
900
901 static WineGLPixelFormat *get_formats(Display *display, int *size_ret, int *onscreen_size_ret)
902 {
903     static WineGLPixelFormat *list;
904     static int size, onscreen_size;
905
906     int fmt_id, nCfgs, i, run, bmp_formats;
907     GLXFBConfig* cfgs;
908     XVisualInfo *visinfo;
909
910     wine_tsx11_lock();
911     if (list) goto done;
912
913     cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
914     if (NULL == cfgs || 0 == nCfgs) {
915         if(cfgs != NULL) XFree(cfgs);
916         wine_tsx11_unlock();
917         ERR("glXChooseFBConfig returns NULL\n");
918         return NULL;
919     }
920
921     /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
922      * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
923      * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
924      * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
925      *
926      * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
927      */
928     for(i=0, bmp_formats=0; i<nCfgs; i++)
929     {
930         if(check_fbconfig_bitmap_capability(display, cfgs[i]))
931             bmp_formats++;
932     }
933     TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
934
935     list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats)*sizeof(WineGLPixelFormat));
936
937     /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
938      * Do this as GLX doesn't guarantee that the list is sorted */
939     for(run=0; run < 2; run++)
940     {
941         for(i=0; i<nCfgs; i++) {
942             pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
943             visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
944
945             /* The first run we only add onscreen formats (ones which have an associated X Visual).
946              * The second run we only set offscreen formats. */
947             if(!run && visinfo)
948             {
949                 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
950                  * The contents is copied to the destination using XCopyArea. For the copying to work
951                  * the depth of the source and destination window should be the same. In general this should
952                  * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
953                  * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
954                  * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
955                  * list formats with the same depth. */
956                 if(visinfo->depth != screen_depth)
957                 {
958                     XFree(visinfo);
959                     continue;
960                 }
961
962                 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
963                 list[size].iPixelFormat = size+1; /* The index starts at 1 */
964                 list[size].fbconfig = cfgs[i];
965                 list[size].fmt_id = fmt_id;
966                 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
967                 list[size].offscreenOnly = FALSE;
968                 list[size].dwFlags = 0;
969                 size++;
970                 onscreen_size++;
971
972                 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
973                 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
974                 {
975                     TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
976                     list[size].iPixelFormat = size+1; /* The index starts at 1 */
977                     list[size].fbconfig = cfgs[i];
978                     list[size].fmt_id = fmt_id;
979                     list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
980                     list[size].offscreenOnly = FALSE;
981                     list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
982                     size++;
983                     onscreen_size++;
984                 }
985             } else if(run && !visinfo) {
986                 int window_drawable=0;
987                 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
988
989                 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
990                  * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
991                  * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
992                  * likely make our child window opengl rendering more complicated since likely you can't use
993                  * XCopyArea on a GLX Window.
994                  * For now ignore fbconfigs which are window drawable but lack a visual. */
995                 if(window_drawable & GLX_WINDOW_BIT)
996                 {
997                     TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
998                     continue;
999                 }
1000
1001                 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1002                 list[size].iPixelFormat = size+1; /* The index starts at 1 */
1003                 list[size].fbconfig = cfgs[i];
1004                 list[size].fmt_id = fmt_id;
1005                 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1006                 list[size].offscreenOnly = TRUE;
1007                 list[size].dwFlags = 0;
1008                 size++;
1009             }
1010
1011             if (visinfo) XFree(visinfo);
1012         }
1013     }
1014
1015     XFree(cfgs);
1016
1017 done:
1018     if (size_ret) *size_ret = size;
1019     if (onscreen_size_ret) *onscreen_size_ret = onscreen_size;
1020     wine_tsx11_unlock();
1021     return list;
1022 }
1023
1024 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1025  * In our WGL implementation we only support a subset of these formats namely the format of
1026  * Wine's main visual and offscreen formats (if they are available).
1027  * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
1028  * and it returns the number of supported WGL formats in fmt_count.
1029  */
1030 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
1031 {
1032     WineGLPixelFormat *list, *res = NULL;
1033     int size, onscreen_size;
1034
1035     if (!(list = get_formats(display, &size, &onscreen_size ))) return NULL;
1036
1037     /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1038      * iPixelFormat in case of probing the number of pixelformats.
1039      */
1040     if((iPixelFormat > 0) && (iPixelFormat <= size) &&
1041        (!list[iPixelFormat-1].offscreenOnly || AllowOffscreen)) {
1042         res = &list[iPixelFormat-1];
1043         TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n", res->fmt_id, iPixelFormat);
1044     }
1045
1046     if(AllowOffscreen)
1047         *fmt_count = size;
1048     else
1049         *fmt_count = onscreen_size;
1050
1051     TRACE("Number of returned pixelformats=%d\n", *fmt_count);
1052
1053     return res;
1054 }
1055
1056 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
1057 static WineGLPixelFormat* ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id, DWORD dwFlags)
1058 {
1059     WineGLPixelFormat *list;
1060     int i, size;
1061
1062     if (!(list = get_formats(display, &size, NULL ))) return NULL;
1063
1064     for(i=0; i<size; i++) {
1065         /* A GLX format can appear multiple times in the pixel format list due to fake formats for bitmap rendering.
1066          * Fake formats might get selected when the user passes the proper flags using the dwFlags parameter. */
1067         if( (list[i].fmt_id == fmt_id) && ((list[i].dwFlags & dwFlags) == dwFlags) ) {
1068             TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", list[i].iPixelFormat, fmt_id);
1069             return &list[i];
1070         }
1071     }
1072     TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1073     return NULL;
1074 }
1075
1076 static int pixelformat_from_fbconfig_id(XID fbconfig_id)
1077 {
1078     WineGLPixelFormat *fmt;
1079
1080     if (!fbconfig_id) return 0;
1081
1082     fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
1083     if(fmt)
1084         return fmt->iPixelFormat;
1085     /* This will happen on hwnds without a pixel format set; it's ok */
1086     return 0;
1087 }
1088
1089
1090 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1091 void mark_drawable_dirty(Drawable old, Drawable new)
1092 {
1093     struct wgl_context *ctx;
1094     LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1095     {
1096         if (old == ctx->drawables[0]) {
1097             ctx->drawables[0] = new;
1098             ctx->refresh_drawables = TRUE;
1099         }
1100         if (old == ctx->drawables[1]) {
1101             ctx->drawables[1] = new;
1102             ctx->refresh_drawables = TRUE;
1103         }
1104     }
1105 }
1106
1107 /* Given the current context, make sure its drawable is sync'd */
1108 static inline void sync_context(struct wgl_context *context)
1109 {
1110     if(context && context->refresh_drawables) {
1111         if (glxRequireVersion(3))
1112             pglXMakeContextCurrent(gdi_display, context->drawables[0],
1113                                    context->drawables[1], context->ctx);
1114         else
1115             pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1116         context->refresh_drawables = FALSE;
1117     }
1118 }
1119
1120
1121 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1122 {
1123     GLXContext ctx;
1124
1125     /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1126     BOOL indirect = (context->fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? FALSE : TRUE;
1127
1128     if(context->gl3_context)
1129     {
1130         if(context->numAttribs)
1131             ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1132         else
1133             ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1134     }
1135     else if(context->vis)
1136         ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1137     else /* Create a GLX Context for a pbuffer */
1138         ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1139
1140     return ctx;
1141 }
1142
1143
1144 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
1145 {
1146     return pglXCreateGLXPixmap(display, vis, parent);
1147 }
1148
1149
1150 /**
1151  * glxdrv_DescribePixelFormat
1152  *
1153  * Get the pixel-format descriptor associated to the given id
1154  */
1155 static int glxdrv_DescribePixelFormat(PHYSDEV dev, int iPixelFormat,
1156                                       UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1157 {
1158   /*XVisualInfo *vis;*/
1159   int value;
1160   int rb,gb,bb,ab;
1161   WineGLPixelFormat *fmt;
1162   int ret = 0;
1163   int fmt_count = 0;
1164
1165   if (!has_opengl()) return 0;
1166
1167   TRACE("(%p,%d,%d,%p)\n", dev->hdc, iPixelFormat, nBytes, ppfd);
1168
1169   /* 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 */
1170   fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1171   if (ppfd == NULL) {
1172       /* The application is only querying the number of pixelformats */
1173       return fmt_count;
1174   } else if(fmt == NULL) {
1175       WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1176       return 0;
1177   }
1178
1179   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1180     ERR("Wrong structure size !\n");
1181     /* Should set error */
1182     return 0;
1183   }
1184
1185   ret = fmt_count;
1186
1187   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1188   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1189   ppfd->nVersion = 1;
1190
1191   /* These flags are always the same... */
1192   ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1193   /* Now the flags extracted from the Visual */
1194
1195   wine_tsx11_lock();
1196
1197   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1198   if(value & GLX_WINDOW_BIT)
1199       ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1200
1201   /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1202    * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1203    * Radeon 9000 indicated that all bitmap formats have PFD_SUPPORT_GDI. Except for 2 formats on the Radeon 9000 none of the hw accelerated formats
1204    * offered the GDI bit either. */
1205   ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1206
1207   /* PFD_GENERIC_FORMAT - gdi software rendering
1208    * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1209    * none set - full hardware accelerated by a ICD
1210    *
1211    * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do  */
1212   ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1213
1214   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1215   if (value) {
1216       ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1217       ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1218   }
1219   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1220
1221   /* Pixel type */
1222   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1223   if (value & GLX_RGBA_BIT)
1224     ppfd->iPixelType = PFD_TYPE_RGBA;
1225   else
1226     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1227
1228   /* Color bits */
1229   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1230   ppfd->cColorBits = value;
1231
1232   /* Red, green, blue and alpha bits / shifts */
1233   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1234     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1235     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1236     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1237     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1238
1239     ppfd->cRedBits = rb;
1240     ppfd->cRedShift = gb + bb + ab;
1241     ppfd->cBlueBits = bb;
1242     ppfd->cBlueShift = ab;
1243     ppfd->cGreenBits = gb;
1244     ppfd->cGreenShift = bb + ab;
1245     ppfd->cAlphaBits = ab;
1246     ppfd->cAlphaShift = 0;
1247   } else {
1248     ppfd->cRedBits = 0;
1249     ppfd->cRedShift = 0;
1250     ppfd->cBlueBits = 0;
1251     ppfd->cBlueShift = 0;
1252     ppfd->cGreenBits = 0;
1253     ppfd->cGreenShift = 0;
1254     ppfd->cAlphaBits = 0;
1255     ppfd->cAlphaShift = 0;
1256   }
1257
1258   /* Accum RGBA bits */
1259   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1260   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1261   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1262   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1263
1264   ppfd->cAccumBits = rb+gb+bb+ab;
1265   ppfd->cAccumRedBits = rb;
1266   ppfd->cAccumGreenBits = gb;
1267   ppfd->cAccumBlueBits = bb;
1268   ppfd->cAccumAlphaBits = ab;
1269
1270   /* Aux bits */
1271   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1272   ppfd->cAuxBuffers = value;
1273
1274   /* Depth bits */
1275   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1276   ppfd->cDepthBits = value;
1277
1278   /* stencil bits */
1279   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1280   ppfd->cStencilBits = value;
1281
1282   wine_tsx11_unlock();
1283
1284   ppfd->iLayerType = PFD_MAIN_PLANE;
1285
1286   if (TRACE_ON(wgl)) {
1287     dump_PIXELFORMATDESCRIPTOR(ppfd);
1288   }
1289
1290   return ret;
1291 }
1292
1293 /***********************************************************************
1294  *              glxdrv_wglGetPixelFormat
1295  */
1296 static int glxdrv_wglGetPixelFormat( HDC hdc )
1297 {
1298     struct x11drv_escape_get_drawable escape;
1299     WineGLPixelFormat *fmt;
1300     int tmp;
1301
1302     TRACE( "(%p)\n", hdc );
1303
1304     escape.code = X11DRV_GET_DRAWABLE;
1305     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape.code), (LPCSTR)&escape.code,
1306                     sizeof(escape), (LPSTR)&escape ))
1307         return 0;
1308
1309     if (!escape.pixel_format) return 0;  /* not set yet */
1310
1311     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, escape.pixel_format, TRUE, &tmp);
1312     if (!fmt)
1313     {
1314         ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", escape.pixel_format);
1315         return 0;
1316     }
1317     if (fmt->offscreenOnly)
1318     {
1319         /* Offscreen formats can't be used with traditional WGL calls.
1320          * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1321         TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1322         return 1;
1323     }
1324     TRACE("(%p): returns %d\n", hdc, escape.pixel_format);
1325     return escape.pixel_format;
1326 }
1327
1328 /***********************************************************************
1329  *              glxdrv_SetPixelFormat
1330  */
1331 static BOOL glxdrv_SetPixelFormat(PHYSDEV dev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
1332 {
1333     struct glx_physdev *physdev = get_glxdrv_dev( dev );
1334     WineGLPixelFormat *fmt;
1335     int value;
1336     HWND hwnd;
1337
1338     TRACE("(%p,%d,%p)\n", dev->hdc, iPixelFormat, ppfd);
1339
1340     if (!has_opengl()) return FALSE;
1341
1342     if(physdev->pixel_format)  /* cannot change it if already set */
1343         return (physdev->pixel_format == iPixelFormat);
1344
1345     /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1346     if(physdev->x11dev->drawable == root_window)
1347     {
1348         ERR("Invalid operation on root_window\n");
1349         return FALSE;
1350     }
1351
1352     /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1353     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1354     if(!fmt) {
1355         ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1356         return FALSE;
1357     }
1358
1359     wine_tsx11_lock();
1360     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1361     wine_tsx11_unlock();
1362
1363     hwnd = WindowFromDC(physdev->dev.hdc);
1364     if(hwnd) {
1365         if(!(value&GLX_WINDOW_BIT)) {
1366             WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1367             return FALSE;
1368         }
1369
1370         if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, fmt->fmt_id, 0)) {
1371             ERR("Couldn't set format of the window, returning failure\n");
1372             return FALSE;
1373         }
1374         /* physDev->current_pf will be set by the DCE update */
1375     }
1376     else if (GetObjectType( physdev->dev.hdc ) == OBJ_MEMDC) {
1377         if(!(value&GLX_PIXMAP_BIT)) {
1378             WARN("Pixel format %d is not compatible for bitmap rendering\n", iPixelFormat);
1379             return FALSE;
1380         }
1381
1382         physdev->pixel_format = iPixelFormat;
1383         physdev->type = DC_GL_BITMAP;
1384     }
1385     else {
1386         FIXME("called on a non-window, non-bitmap object?\n");
1387     }
1388
1389     if (TRACE_ON(wgl)) {
1390         int gl_test = 0;
1391
1392         wine_tsx11_lock();
1393         gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1394         if (gl_test) {
1395            ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1396         } else {
1397             TRACE(" FBConfig have :\n");
1398             TRACE(" - FBCONFIG_ID   0x%x\n", value);
1399             pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1400             TRACE(" - VISUAL_ID     0x%x\n", value);
1401             pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1402             TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1403         }
1404         wine_tsx11_unlock();
1405     }
1406     return TRUE;
1407 }
1408
1409 /***********************************************************************
1410  *              glxdrv_wglCopyContext
1411  */
1412 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1413 {
1414     TRACE("%p -> %p mask %#x\n", src, dst, mask);
1415
1416     wine_tsx11_lock();
1417     pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1418     wine_tsx11_unlock();
1419
1420     /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1421     return TRUE;
1422 }
1423
1424 /***********************************************************************
1425  *              glxdrv_wglCreateContext
1426  */
1427 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1428 {
1429     struct x11drv_escape_get_drawable escape;
1430     struct wgl_context *ret;
1431     WineGLPixelFormat *fmt;
1432     int fmt_count = 0;
1433
1434     TRACE( "(%p)\n", hdc );
1435
1436     escape.code = X11DRV_GET_DRAWABLE;
1437     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape.code), (LPCSTR)&escape.code,
1438                     sizeof(escape), (LPSTR)&escape ))
1439         return 0;
1440
1441     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, escape.pixel_format, TRUE /* Offscreen */, &fmt_count);
1442     /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1443      * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1444      * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1445      * If this fails something is very wrong on the system. */
1446     if(!fmt) {
1447         ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", escape.pixel_format);
1448         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1449         return NULL;
1450     }
1451
1452     if (!(ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret)))) return 0;
1453
1454     ret->hdc = hdc;
1455     ret->fmt = fmt;
1456     ret->has_been_current = FALSE;
1457     ret->sharing = FALSE;
1458
1459     wine_tsx11_lock();
1460     ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1461     ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1462     list_add_head( &context_list, &ret->entry );
1463     wine_tsx11_unlock();
1464
1465     TRACE(" creating context %p (GL context creation delayed)\n", ret);
1466     return ret;
1467 }
1468
1469 /***********************************************************************
1470  *              glxdrv_wglDeleteContext
1471  */
1472 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1473 {
1474     TRACE("(%p)\n", ctx);
1475
1476     wine_tsx11_lock();
1477     list_remove( &ctx->entry );
1478     if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1479     if (ctx->glxpixmap) pglXDestroyGLXPixmap( gdi_display, ctx->glxpixmap );
1480     if (ctx->pixmap) XFreePixmap( gdi_display, ctx->pixmap );
1481     if (ctx->vis) XFree( ctx->vis );
1482     wine_tsx11_unlock();
1483
1484     HeapFree( GetProcessHeap(), 0, ctx );
1485 }
1486
1487 /**
1488  * X11DRV_wglGetCurrentReadDCARB
1489  *
1490  * For OpenGL32 wglGetCurrentReadDCARB.
1491  */
1492 static HDC X11DRV_wglGetCurrentReadDCARB(void)
1493 {
1494     HDC ret = 0;
1495     struct wgl_context *ctx = NtCurrentTeb()->glContext;
1496
1497     if (ctx) ret = ctx->read_hdc;
1498
1499     TRACE(" returning %p (GL drawable %lu)\n", ret, ctx ? ctx->drawables[1] : 0);
1500     return ret;
1501 }
1502
1503 /***********************************************************************
1504  *              glxdrv_wglGetProcAddress
1505  */
1506 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1507 {
1508     if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1509     return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1510 }
1511
1512 static GLXPixmap get_context_pixmap( HDC hdc, struct wgl_context *ctx )
1513 {
1514     if (!ctx->pixmap)
1515     {
1516         BITMAP bmp;
1517
1518         GetObjectW( GetCurrentObject( hdc, OBJ_BITMAP ), sizeof(bmp), &bmp );
1519
1520         wine_tsx11_lock();
1521         ctx->pixmap = XCreatePixmap( gdi_display, root_window,
1522                                      bmp.bmWidth, bmp.bmHeight, ctx->vis->depth );
1523         ctx->glxpixmap = pglXCreateGLXPixmap( gdi_display, ctx->vis, ctx->pixmap );
1524         wine_tsx11_unlock();
1525         ctx->pixmap_size.cx = bmp.bmWidth;
1526         ctx->pixmap_size.cy = bmp.bmHeight;
1527     }
1528     return ctx->glxpixmap;
1529 }
1530
1531 /***********************************************************************
1532  *              glxdrv_wglMakeCurrent
1533  */
1534 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1535 {
1536     BOOL ret;
1537     struct x11drv_escape_get_drawable escape;
1538
1539     TRACE("(%p,%p)\n", hdc, ctx);
1540
1541     if (!ctx)
1542     {
1543         wine_tsx11_lock();
1544         ret = pglXMakeCurrent(gdi_display, None, NULL);
1545         wine_tsx11_unlock();
1546         NtCurrentTeb()->glContext = NULL;
1547         return TRUE;
1548     }
1549
1550     escape.code = X11DRV_GET_DRAWABLE;
1551     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape.code), (LPCSTR)&escape.code,
1552                     sizeof(escape), (LPSTR)&escape ))
1553         return FALSE;
1554
1555     if (!escape.pixel_format)
1556     {
1557         WARN("Trying to use an invalid drawable\n");
1558         SetLastError(ERROR_INVALID_HANDLE);
1559         return FALSE;
1560     }
1561     if (ctx->fmt->iPixelFormat != escape.pixel_format)
1562     {
1563         WARN( "mismatched pixel format hdc %p %u ctx %p %u\n",
1564               hdc, escape.pixel_format, ctx, ctx->fmt->iPixelFormat );
1565         SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1566         return FALSE;
1567     }
1568     else
1569     {
1570         if (escape.gl_type == DC_GL_BITMAP) escape.gl_drawable = get_context_pixmap( hdc, ctx );
1571
1572         wine_tsx11_lock();
1573
1574         if (TRACE_ON(wgl)) {
1575             int vis_id;
1576             pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &vis_id);
1577             describeContext(ctx);
1578             TRACE("hdc %p drawable %lx fmt %u vis %x ctx %p\n", hdc,
1579                   escape.gl_drawable, escape.pixel_format, vis_id, ctx->ctx);
1580         }
1581
1582         ret = pglXMakeCurrent(gdi_display, escape.gl_drawable, ctx->ctx);
1583
1584         if (ret)
1585         {
1586             NtCurrentTeb()->glContext = ctx;
1587
1588             ctx->has_been_current = TRUE;
1589             ctx->hdc = hdc;
1590             ctx->read_hdc = hdc;
1591             ctx->drawables[0] = escape.gl_drawable;
1592             ctx->drawables[1] = escape.gl_drawable;
1593             ctx->refresh_drawables = FALSE;
1594
1595             if (escape.gl_type == DC_GL_BITMAP) opengl_funcs.gl.p_glDrawBuffer(GL_FRONT_LEFT);
1596         }
1597         else
1598             SetLastError(ERROR_INVALID_HANDLE);
1599         wine_tsx11_unlock();
1600     }
1601     TRACE(" returning %s\n", (ret ? "True" : "False"));
1602     return ret;
1603 }
1604
1605 /***********************************************************************
1606  *              X11DRV_wglMakeContextCurrentARB
1607  */
1608 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1609 {
1610     struct x11drv_escape_get_drawable escape_draw, escape_read;
1611     BOOL ret;
1612
1613     TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1614
1615     if (!ctx)
1616     {
1617         wine_tsx11_lock();
1618         ret = pglXMakeCurrent(gdi_display, None, NULL);
1619         wine_tsx11_unlock();
1620         NtCurrentTeb()->glContext = NULL;
1621         return TRUE;
1622     }
1623
1624     escape_draw.code = X11DRV_GET_DRAWABLE;
1625     if (!ExtEscape( draw_hdc, X11DRV_ESCAPE, sizeof(escape_draw.code), (LPCSTR)&escape_draw.code,
1626                     sizeof(escape_draw), (LPSTR)&escape_draw ))
1627         return FALSE;
1628
1629     escape_read.code = X11DRV_GET_DRAWABLE;
1630     if (!ExtEscape( read_hdc, X11DRV_ESCAPE, sizeof(escape_read.code), (LPCSTR)&escape_read.code,
1631                     sizeof(escape_read), (LPSTR)&escape_read ))
1632         return FALSE;
1633
1634     if (!escape_draw.pixel_format)
1635     {
1636         WARN("Trying to use an invalid drawable\n");
1637         SetLastError(ERROR_INVALID_HANDLE);
1638         return FALSE;
1639     }
1640     else
1641     {
1642         if (!pglXMakeContextCurrent) return FALSE;
1643
1644         if (escape_draw.gl_type == DC_GL_BITMAP) escape_draw.gl_drawable = get_context_pixmap( draw_hdc, ctx );
1645         if (escape_read.gl_type == DC_GL_BITMAP) escape_read.gl_drawable = get_context_pixmap( read_hdc, ctx );
1646
1647         wine_tsx11_lock();
1648         ret = pglXMakeContextCurrent(gdi_display, escape_draw.gl_drawable, escape_read.gl_drawable, ctx->ctx);
1649         if (ret)
1650         {
1651             ctx->has_been_current = TRUE;
1652             ctx->hdc = draw_hdc;
1653             ctx->read_hdc = read_hdc;
1654             ctx->drawables[0] = escape_draw.gl_drawable;
1655             ctx->drawables[1] = escape_read.gl_drawable;
1656             ctx->refresh_drawables = FALSE;
1657             NtCurrentTeb()->glContext = ctx;
1658         }
1659         else
1660             SetLastError(ERROR_INVALID_HANDLE);
1661         wine_tsx11_unlock();
1662     }
1663
1664     TRACE(" returning %s\n", (ret ? "True" : "False"));
1665     return ret;
1666 }
1667
1668 /***********************************************************************
1669  *              glxdrv_wglShareLists
1670  */
1671 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1672 {
1673     TRACE("(%p, %p)\n", org, dest);
1674
1675     /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1676      * at context creation time but in case of WGL it is done using wglShareLists.
1677      * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1678      * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1679      * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1680      * so there delaying context creation doesn't work.
1681      *
1682      * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1683      * and when a program requests sharing we recreate the destination context if it hasn't been made
1684      * current or when it hasn't shared display lists before.
1685      */
1686
1687     if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1688     {
1689         ERR("Could not share display lists, one of the contexts has been current already !\n");
1690         return FALSE;
1691     }
1692     else if(dest->sharing)
1693     {
1694         ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1695         return FALSE;
1696     }
1697     else
1698     {
1699         if((GetObjectType(org->hdc) == OBJ_MEMDC) ^ (GetObjectType(dest->hdc) == OBJ_MEMDC))
1700         {
1701             WARN("Attempting to share a context between a direct and indirect rendering context, expect issues!\n");
1702         }
1703
1704         wine_tsx11_lock();
1705         describeContext(org);
1706         describeContext(dest);
1707
1708         /* Re-create the GLX context and share display lists */
1709         pglXDestroyContext(gdi_display, dest->ctx);
1710         dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1711         wine_tsx11_unlock();
1712         TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1713
1714         org->sharing = TRUE;
1715         dest->sharing = TRUE;
1716         return TRUE;
1717     }
1718     return FALSE;
1719 }
1720
1721 /***********************************************************************
1722  *              glxdrv_wglGetCurrentDC
1723  */
1724 static HDC glxdrv_wglGetCurrentDC( struct wgl_context *ctx )
1725 {
1726     TRACE("hdc %p\n", ctx->hdc);
1727     return ctx->hdc;
1728 }
1729
1730 static void flush_pixmap( struct wgl_context *ctx )
1731 {
1732     char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1733     BITMAPINFO *info = (BITMAPINFO *)buffer;
1734     struct gdi_image_bits bits;
1735
1736     if (!get_pixmap_image( ctx->pixmap, ctx->pixmap_size.cx, ctx->pixmap_size.cy, ctx->vis, info, &bits ))
1737     {
1738         HBITMAP bitmap = GetCurrentObject( ctx->hdc, OBJ_BITMAP );
1739         SetDIBits( 0, bitmap, 0, ctx->pixmap_size.cy, bits.ptr, info, DIB_RGB_COLORS );
1740         if (bits.free) bits.free( &bits );
1741     }
1742 }
1743
1744 static void flush_gl_drawable( struct glx_physdev *physdev )
1745 {
1746     RECT rect;
1747     int w = physdev->x11dev->dc_rect.right - physdev->x11dev->dc_rect.left;
1748     int h = physdev->x11dev->dc_rect.bottom - physdev->x11dev->dc_rect.top;
1749     Drawable src = physdev->drawable;
1750
1751     if (w <= 0 || h <= 0) return;
1752
1753     switch (physdev->type)
1754     {
1755     case DC_GL_PIXMAP_WIN:
1756         src = physdev->pixmap;
1757         /* fall through */
1758     case DC_GL_CHILD_WIN:
1759         /* The GL drawable may be lagged behind if we don't flush first, so
1760          * flush the display make sure we copy up-to-date data */
1761         wine_tsx11_lock();
1762         XFlush(gdi_display);
1763         XSetFunction(gdi_display, physdev->x11dev->gc, GXcopy);
1764         XCopyArea(gdi_display, src, physdev->x11dev->drawable, physdev->x11dev->gc, 0, 0, w, h,
1765                   physdev->x11dev->dc_rect.left, physdev->x11dev->dc_rect.top);
1766         wine_tsx11_unlock();
1767         SetRect( &rect, 0, 0, w, h );
1768         add_device_bounds( physdev->x11dev, &rect );
1769     default:
1770         break;
1771     }
1772 }
1773
1774
1775 static void wglFinish(void)
1776 {
1777     struct wgl_context *ctx = NtCurrentTeb()->glContext;
1778     enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
1779
1780     wine_tsx11_lock();
1781     sync_context(ctx);
1782     pglFinish();
1783     wine_tsx11_unlock();
1784     if (ctx)
1785     {
1786         if (ctx->pixmap) flush_pixmap( ctx );
1787         else ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1788     }
1789 }
1790
1791 static void wglFlush(void)
1792 {
1793     struct wgl_context *ctx = NtCurrentTeb()->glContext;
1794     enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
1795
1796     wine_tsx11_lock();
1797     sync_context(ctx);
1798     pglFlush();
1799     wine_tsx11_unlock();
1800     if (ctx)
1801     {
1802         if (ctx->pixmap) flush_pixmap( ctx );
1803         else ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1804     }
1805 }
1806
1807 /***********************************************************************
1808  *              X11DRV_wglCreateContextAttribsARB
1809  */
1810 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
1811                                                               const int* attribList )
1812 {
1813     struct x11drv_escape_get_drawable escape;
1814     struct wgl_context *ret;
1815     WineGLPixelFormat *fmt;
1816     int fmt_count = 0;
1817
1818     TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
1819
1820     escape.code = X11DRV_GET_DRAWABLE;
1821     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape.code), (LPCSTR)&escape.code,
1822                     sizeof(escape), (LPSTR)&escape ))
1823         return 0;
1824
1825     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, escape.pixel_format, TRUE /* Offscreen */, &fmt_count);
1826     /* wglCreateContextAttribsARB supports ALL pixel formats, so also offscreen ones.
1827      * If this fails something is very wrong on the system. */
1828     if(!fmt)
1829     {
1830         ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", escape.pixel_format);
1831         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1832         return NULL;
1833     }
1834
1835     if (!(ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret)))) return 0;
1836
1837     ret->hdc = hdc;
1838     ret->fmt = fmt;
1839     ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
1840     ret->gl3_context = TRUE;
1841
1842     ret->numAttribs = 0;
1843     if(attribList)
1844     {
1845         int *pAttribList = (int*)attribList;
1846         int *pContextAttribList = &ret->attribList[0];
1847         /* attribList consists of pairs {token, value] terminated with 0 */
1848         while(pAttribList[0] != 0)
1849         {
1850             TRACE("%#x %#x\n", pAttribList[0], pAttribList[1]);
1851             switch(pAttribList[0])
1852             {
1853                 case WGL_CONTEXT_MAJOR_VERSION_ARB:
1854                     pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
1855                     pContextAttribList[1] = pAttribList[1];
1856                     break;
1857                 case WGL_CONTEXT_MINOR_VERSION_ARB:
1858                     pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
1859                     pContextAttribList[1] = pAttribList[1];
1860                     break;
1861                 case WGL_CONTEXT_LAYER_PLANE_ARB:
1862                     break;
1863                 case WGL_CONTEXT_FLAGS_ARB:
1864                     pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
1865                     pContextAttribList[1] = pAttribList[1];
1866                     break;
1867                 case WGL_CONTEXT_PROFILE_MASK_ARB:
1868                     pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
1869                     pContextAttribList[1] = pAttribList[1];
1870                     break;
1871                 default:
1872                     ERR("Unhandled attribList pair: %#x %#x\n", pAttribList[0], pAttribList[1]);
1873             }
1874
1875             ret->numAttribs++;
1876             pAttribList += 2;
1877             pContextAttribList += 2;
1878         }
1879     }
1880
1881     wine_tsx11_lock();
1882     X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1883     ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1884
1885     XSync(gdi_display, False);
1886     if(X11DRV_check_error() || !ret->ctx)
1887     {
1888         /* In the future we should convert the GLX error to a win32 one here if needed */
1889         ERR("Context creation failed\n");
1890         HeapFree( GetProcessHeap(), 0, ret );
1891         wine_tsx11_unlock();
1892         return NULL;
1893     }
1894
1895     list_add_head( &context_list, &ret->entry );
1896     wine_tsx11_unlock();
1897     TRACE(" creating context %p\n", ret);
1898     return ret;
1899 }
1900
1901 /**
1902  * X11DRV_wglGetExtensionsStringARB
1903  *
1904  * WGL_ARB_extensions_string: wglGetExtensionsStringARB
1905  */
1906 static const GLubyte *X11DRV_wglGetExtensionsStringARB(HDC hdc)
1907 {
1908     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
1909     return (const GLubyte *)WineGLInfo.wglExtensions;
1910 }
1911
1912 /**
1913  * X11DRV_wglCreatePbufferARB
1914  *
1915  * WGL_ARB_pbuffer: wglCreatePbufferARB
1916  */
1917 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
1918                                                        const int *piAttribList )
1919 {
1920     struct wgl_pbuffer* object = NULL;
1921     WineGLPixelFormat *fmt = NULL;
1922     int nCfgs = 0;
1923     int attribs[256];
1924     int nAttribs = 0;
1925
1926     TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
1927
1928     /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
1929     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
1930     if(!fmt) {
1931         ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
1932         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1933         return NULL;
1934     }
1935
1936     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1937     if (NULL == object) {
1938         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
1939         return NULL;
1940     }
1941     object->width = iWidth;
1942     object->height = iHeight;
1943     object->fmt = fmt;
1944
1945     PUSH2(attribs, GLX_PBUFFER_WIDTH,  iWidth);
1946     PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight); 
1947     while (piAttribList && 0 != *piAttribList) {
1948         int attr_v;
1949         switch (*piAttribList) {
1950             case WGL_PBUFFER_LARGEST_ARB: {
1951                 ++piAttribList;
1952                 attr_v = *piAttribList;
1953                 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
1954                 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
1955                 break;
1956             }
1957
1958             case WGL_TEXTURE_FORMAT_ARB: {
1959                 ++piAttribList;
1960                 attr_v = *piAttribList;
1961                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
1962                 if (WGL_NO_TEXTURE_ARB == attr_v) {
1963                     object->use_render_texture = 0;
1964                 } else {
1965                     if (!use_render_texture_emulation) {
1966                         SetLastError(ERROR_INVALID_DATA);
1967                         goto create_failed;
1968                     }
1969                     switch (attr_v) {
1970                         case WGL_TEXTURE_RGB_ARB:
1971                             object->use_render_texture = GL_RGB;
1972                             object->texture_bpp = 3;
1973                             object->texture_format = GL_RGB;
1974                             object->texture_type = GL_UNSIGNED_BYTE;
1975                             break;
1976                         case WGL_TEXTURE_RGBA_ARB:
1977                             object->use_render_texture = GL_RGBA;
1978                             object->texture_bpp = 4;
1979                             object->texture_format = GL_RGBA;
1980                             object->texture_type = GL_UNSIGNED_BYTE;
1981                             break;
1982
1983                         /* WGL_FLOAT_COMPONENTS_NV */
1984                         case WGL_TEXTURE_FLOAT_R_NV:
1985                             object->use_render_texture = GL_FLOAT_R_NV;
1986                             object->texture_bpp = 4;
1987                             object->texture_format = GL_RED;
1988                             object->texture_type = GL_FLOAT;
1989                             break;
1990                         case WGL_TEXTURE_FLOAT_RG_NV:
1991                             object->use_render_texture = GL_FLOAT_RG_NV;
1992                             object->texture_bpp = 8;
1993                             object->texture_format = GL_LUMINANCE_ALPHA;
1994                             object->texture_type = GL_FLOAT;
1995                             break;
1996                         case WGL_TEXTURE_FLOAT_RGB_NV:
1997                             object->use_render_texture = GL_FLOAT_RGB_NV;
1998                             object->texture_bpp = 12;
1999                             object->texture_format = GL_RGB;
2000                             object->texture_type = GL_FLOAT;
2001                             break;
2002                         case WGL_TEXTURE_FLOAT_RGBA_NV:
2003                             object->use_render_texture = GL_FLOAT_RGBA_NV;
2004                             object->texture_bpp = 16;
2005                             object->texture_format = GL_RGBA;
2006                             object->texture_type = GL_FLOAT;
2007                             break;
2008                         default:
2009                             ERR("Unknown texture format: %x\n", attr_v);
2010                             SetLastError(ERROR_INVALID_DATA);
2011                             goto create_failed;
2012                     }
2013                 }
2014                 break;
2015             }
2016
2017             case WGL_TEXTURE_TARGET_ARB: {
2018                 ++piAttribList;
2019                 attr_v = *piAttribList;
2020                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2021                 if (WGL_NO_TEXTURE_ARB == attr_v) {
2022                     object->texture_target = 0;
2023                 } else {
2024                     if (!use_render_texture_emulation) {
2025                         SetLastError(ERROR_INVALID_DATA);
2026                         goto create_failed;
2027                     }
2028                     switch (attr_v) {
2029                         case WGL_TEXTURE_CUBE_MAP_ARB: {
2030                             if (iWidth != iHeight) {
2031                                 SetLastError(ERROR_INVALID_DATA);
2032                                 goto create_failed;
2033                             }
2034                             object->texture_target = GL_TEXTURE_CUBE_MAP;
2035                             object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2036                            break;
2037                         }
2038                         case WGL_TEXTURE_1D_ARB: {
2039                             if (1 != iHeight) {
2040                                 SetLastError(ERROR_INVALID_DATA);
2041                                 goto create_failed;
2042                             }
2043                             object->texture_target = GL_TEXTURE_1D;
2044                             object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2045                             break;
2046                         }
2047                         case WGL_TEXTURE_2D_ARB: {
2048                             object->texture_target = GL_TEXTURE_2D;
2049                             object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2050                             break;
2051                         }
2052                         case WGL_TEXTURE_RECTANGLE_NV: {
2053                             object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2054                             object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2055                             break;
2056                         }
2057                         default:
2058                             ERR("Unknown texture target: %x\n", attr_v);
2059                             SetLastError(ERROR_INVALID_DATA);
2060                             goto create_failed;
2061                     }
2062                 }
2063                 break;
2064             }
2065
2066             case WGL_MIPMAP_TEXTURE_ARB: {
2067                 ++piAttribList;
2068                 attr_v = *piAttribList;
2069                 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2070                 if (!use_render_texture_emulation) {
2071                     SetLastError(ERROR_INVALID_DATA);
2072                     goto create_failed;
2073                 }
2074                 break;
2075             }
2076         }
2077         ++piAttribList;
2078     }
2079
2080     PUSH1(attribs, None);
2081     wine_tsx11_lock();
2082     object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2083     wine_tsx11_unlock();
2084     TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2085     if (!object->drawable) {
2086         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2087         goto create_failed; /* unexpected error */
2088     }
2089     TRACE("->(%p)\n", object);
2090     return object;
2091
2092 create_failed:
2093     HeapFree(GetProcessHeap(), 0, object);
2094     TRACE("->(FAILED)\n");
2095     return NULL;
2096 }
2097
2098 /**
2099  * X11DRV_wglDestroyPbufferARB
2100  *
2101  * WGL_ARB_pbuffer: wglDestroyPbufferARB
2102  */
2103 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2104 {
2105     TRACE("(%p)\n", object);
2106
2107     wine_tsx11_lock();
2108     pglXDestroyPbuffer(gdi_display, object->drawable);
2109     wine_tsx11_unlock();
2110     HeapFree(GetProcessHeap(), 0, object);
2111     return GL_TRUE;
2112 }
2113
2114 /**
2115  * X11DRV_wglGetPbufferDCARB
2116  *
2117  * WGL_ARB_pbuffer: wglGetPbufferDCARB
2118  */
2119 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2120 {
2121     struct x11drv_escape_set_drawable escape;
2122     HDC hdc;
2123
2124     hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2125     if (!hdc) return 0;
2126
2127     escape.code = X11DRV_SET_DRAWABLE;
2128     escape.drawable = object->drawable;
2129     escape.mode = IncludeInferiors;
2130     SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2131     escape.fbconfig_id = object->fmt->fmt_id;
2132     escape.gl_drawable = object->drawable;
2133     escape.pixmap = 0;
2134     escape.gl_type = DC_GL_PBUFFER;
2135     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2136
2137     TRACE( "(%p)->(%p)\n", object, hdc );
2138     return hdc;
2139 }
2140
2141 /**
2142  * X11DRV_wglQueryPbufferARB
2143  *
2144  * WGL_ARB_pbuffer: wglQueryPbufferARB
2145  */
2146 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2147 {
2148     TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2149
2150     switch (iAttribute) {
2151         case WGL_PBUFFER_WIDTH_ARB:
2152             wine_tsx11_lock();
2153             pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2154             wine_tsx11_unlock();
2155             break;
2156         case WGL_PBUFFER_HEIGHT_ARB:
2157             wine_tsx11_lock();
2158             pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2159             wine_tsx11_unlock();
2160             break;
2161
2162         case WGL_PBUFFER_LOST_ARB:
2163             /* GLX Pbuffers cannot be lost by default. We can support this by
2164              * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2165              * to receive pixel buffer clobber events, however that may or may
2166              * not give any benefit */
2167             *piValue = GL_FALSE;
2168             break;
2169
2170         case WGL_TEXTURE_FORMAT_ARB:
2171             if (!object->use_render_texture) {
2172                 *piValue = WGL_NO_TEXTURE_ARB;
2173             } else {
2174                 if (!use_render_texture_emulation) {
2175                     SetLastError(ERROR_INVALID_HANDLE);
2176                     return GL_FALSE;
2177                 }
2178                 switch(object->use_render_texture) {
2179                     case GL_RGB:
2180                         *piValue = WGL_TEXTURE_RGB_ARB;
2181                         break;
2182                     case GL_RGBA:
2183                         *piValue = WGL_TEXTURE_RGBA_ARB;
2184                         break;
2185                     /* WGL_FLOAT_COMPONENTS_NV */
2186                     case GL_FLOAT_R_NV:
2187                         *piValue = WGL_TEXTURE_FLOAT_R_NV;
2188                         break;
2189                     case GL_FLOAT_RG_NV:
2190                         *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2191                         break;
2192                     case GL_FLOAT_RGB_NV:
2193                         *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2194                         break;
2195                     case GL_FLOAT_RGBA_NV:
2196                         *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2197                         break;
2198                     default:
2199                         ERR("Unknown texture format: %x\n", object->use_render_texture);
2200                 }
2201             }
2202             break;
2203
2204         case WGL_TEXTURE_TARGET_ARB:
2205             if (!object->texture_target){
2206                 *piValue = WGL_NO_TEXTURE_ARB;
2207             } else {
2208                 if (!use_render_texture_emulation) {
2209                     SetLastError(ERROR_INVALID_DATA);
2210                     return GL_FALSE;
2211                 }
2212                 switch (object->texture_target) {
2213                     case GL_TEXTURE_1D:       *piValue = WGL_TEXTURE_1D_ARB; break;
2214                     case GL_TEXTURE_2D:       *piValue = WGL_TEXTURE_2D_ARB; break;
2215                     case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2216                     case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2217                 }
2218             }
2219             break;
2220
2221         case WGL_MIPMAP_TEXTURE_ARB:
2222             *piValue = GL_FALSE; /** don't support that */
2223             FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2224             break;
2225
2226         default:
2227             FIXME("unexpected attribute %x\n", iAttribute);
2228             break;
2229     }
2230
2231     return GL_TRUE;
2232 }
2233
2234 /**
2235  * X11DRV_wglReleasePbufferDCARB
2236  *
2237  * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2238  */
2239 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2240 {
2241     TRACE("(%p, %p)\n", object, hdc);
2242     return DeleteDC(hdc);
2243 }
2244
2245 /**
2246  * X11DRV_wglSetPbufferAttribARB
2247  *
2248  * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2249  */
2250 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2251 {
2252     GLboolean ret = GL_FALSE;
2253
2254     WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2255
2256     if (!object->use_render_texture) {
2257         SetLastError(ERROR_INVALID_HANDLE);
2258         return GL_FALSE;
2259     }
2260     if (1 == use_render_texture_emulation) {
2261         return GL_TRUE;
2262     }
2263     return ret;
2264 }
2265
2266 /**
2267  * X11DRV_wglChoosePixelFormatARB
2268  *
2269  * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2270  */
2271 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2272                                             UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2273 {
2274     int gl_test = 0;
2275     int attribs[256];
2276     int nAttribs = 0;
2277     GLXFBConfig* cfgs = NULL;
2278     int nCfgs = 0;
2279     int it;
2280     int fmt_id;
2281     WineGLPixelFormat *fmt;
2282     UINT pfmt_it = 0;
2283     int run;
2284     int i;
2285     DWORD dwFlags = 0;
2286
2287     TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2288     if (NULL != pfAttribFList) {
2289         FIXME("unused pfAttribFList\n");
2290     }
2291
2292     nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2293     if (-1 == nAttribs) {
2294         WARN("Cannot convert WGL to GLX attributes\n");
2295         return GL_FALSE;
2296     }
2297     PUSH1(attribs, None);
2298
2299     /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2300      * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the WineGLPixelFormat. We don't query these attributes
2301      * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on by passing a dwFlags to 'ConvertPixelFormatGLXtoWGL'. */
2302     for(i=0; piAttribIList[i] != 0; i+=2)
2303     {
2304         switch(piAttribIList[i])
2305         {
2306             case WGL_DRAW_TO_BITMAP_ARB:
2307                 if(piAttribIList[i+1])
2308                     dwFlags |= PFD_DRAW_TO_BITMAP;
2309                 break;
2310             case WGL_ACCELERATION_ARB:
2311                 switch(piAttribIList[i+1])
2312                 {
2313                     case WGL_NO_ACCELERATION_ARB:
2314                         dwFlags |= PFD_GENERIC_FORMAT;
2315                         break;
2316                     case WGL_GENERIC_ACCELERATION_ARB:
2317                         dwFlags |= PFD_GENERIC_ACCELERATED;
2318                         break;
2319                     case WGL_FULL_ACCELERATION_ARB:
2320                         /* Nothing to do */
2321                         break;
2322                 }
2323                 break;
2324             case WGL_SUPPORT_GDI_ARB:
2325                 if(piAttribIList[i+1])
2326                     dwFlags |= PFD_SUPPORT_GDI;
2327                 break;
2328         }
2329     }
2330
2331     /* Search for FB configurations matching the requirements in attribs */
2332     wine_tsx11_lock();
2333     cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2334     if (NULL == cfgs) {
2335         wine_tsx11_unlock();
2336         WARN("Compatible Pixel Format not found\n");
2337         return GL_FALSE;
2338     }
2339
2340     /* Loop through all matching formats and check if they are suitable.
2341      * Note that this function should at max return nMaxFormats different formats */
2342     for(run=0; run < 2; run++)
2343     {
2344         for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2345         {
2346             gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2347             if (gl_test) {
2348                 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2349                 continue;
2350             }
2351
2352             /* Search for the format in our list of compatible formats */
2353             fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id, dwFlags);
2354             if(!fmt)
2355                 continue;
2356
2357             /* During the first run we only want onscreen formats and during the second only offscreen 'XOR' */
2358             if( ((run == 0) && fmt->offscreenOnly) || ((run == 1) && !fmt->offscreenOnly) )
2359                 continue;
2360
2361             piFormats[pfmt_it] = fmt->iPixelFormat;
2362             TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2363             pfmt_it++;
2364         }
2365     }
2366
2367     *nNumFormats = pfmt_it;
2368     /** free list */
2369     XFree(cfgs);
2370     wine_tsx11_unlock();
2371     return GL_TRUE;
2372 }
2373
2374 /**
2375  * X11DRV_wglGetPixelFormatAttribivARB
2376  *
2377  * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2378  */
2379 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2380                                                  UINT nAttributes, const int *piAttributes, int *piValues )
2381 {
2382     UINT i;
2383     WineGLPixelFormat *fmt = NULL;
2384     int hTest;
2385     int tmp;
2386     int curGLXAttr = 0;
2387     int nWGLFormats = 0;
2388
2389     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2390
2391     if (0 < iLayerPlane) {
2392         FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2393         return GL_FALSE;
2394     }
2395
2396     /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2397     * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2398     * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2399     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2400     if(!fmt) {
2401         WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2402     }
2403
2404     wine_tsx11_lock();
2405     for (i = 0; i < nAttributes; ++i) {
2406         const int curWGLAttr = piAttributes[i];
2407         TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2408
2409         switch (curWGLAttr) {
2410             case WGL_NUMBER_PIXEL_FORMATS_ARB:
2411                 piValues[i] = nWGLFormats; 
2412                 continue;
2413
2414             case WGL_SUPPORT_OPENGL_ARB:
2415                 piValues[i] = GL_TRUE; 
2416                 continue;
2417
2418             case WGL_ACCELERATION_ARB:
2419                 curGLXAttr = GLX_CONFIG_CAVEAT;
2420                 if (!fmt) goto pix_error;
2421                 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2422                     piValues[i] = WGL_NO_ACCELERATION_ARB;
2423                 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2424                     piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2425                 else
2426                     piValues[i] = WGL_FULL_ACCELERATION_ARB;
2427                 continue;
2428
2429             case WGL_TRANSPARENT_ARB:
2430                 curGLXAttr = GLX_TRANSPARENT_TYPE;
2431                 if (!fmt) goto pix_error;
2432                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2433                 if (hTest) goto get_error;
2434                     piValues[i] = GL_FALSE;
2435                 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2436                     continue;
2437
2438             case WGL_PIXEL_TYPE_ARB:
2439                 curGLXAttr = GLX_RENDER_TYPE;
2440                 if (!fmt) goto pix_error;
2441                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2442                 if (hTest) goto get_error;
2443                 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2444                 if      (tmp & GLX_RGBA_BIT)           { piValues[i] = WGL_TYPE_RGBA_ARB; }
2445                 else if (tmp & GLX_COLOR_INDEX_BIT)    { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2446                 else if (tmp & GLX_RGBA_FLOAT_BIT)     { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2447                 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2448                 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2449                 else {
2450                     ERR("unexpected RenderType(%x)\n", tmp);
2451                     piValues[i] = WGL_TYPE_RGBA_ARB;
2452                 }
2453                 continue;
2454
2455             case WGL_COLOR_BITS_ARB:
2456                 curGLXAttr = GLX_BUFFER_SIZE;
2457                 break;
2458
2459             case WGL_BIND_TO_TEXTURE_RGB_ARB:
2460             case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2461                 if (!use_render_texture_emulation) {
2462                     piValues[i] = GL_FALSE;
2463                     continue;   
2464                 }
2465                 curGLXAttr = GLX_RENDER_TYPE;
2466                 if (!fmt) goto pix_error;
2467                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2468                 if (hTest) goto get_error;
2469                 if (GLX_COLOR_INDEX_BIT == tmp) {
2470                     piValues[i] = GL_FALSE;  
2471                     continue;
2472                 }
2473                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2474                 if (hTest) goto get_error;
2475                 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2476                 continue;
2477
2478             case WGL_BLUE_BITS_ARB:
2479                 curGLXAttr = GLX_BLUE_SIZE;
2480                 break;
2481             case WGL_RED_BITS_ARB:
2482                 curGLXAttr = GLX_RED_SIZE;
2483                 break;
2484             case WGL_GREEN_BITS_ARB:
2485                 curGLXAttr = GLX_GREEN_SIZE;
2486                 break;
2487             case WGL_ALPHA_BITS_ARB:
2488                 curGLXAttr = GLX_ALPHA_SIZE;
2489                 break;
2490             case WGL_DEPTH_BITS_ARB:
2491                 curGLXAttr = GLX_DEPTH_SIZE;
2492                 break;
2493             case WGL_STENCIL_BITS_ARB:
2494                 curGLXAttr = GLX_STENCIL_SIZE;
2495                 break;
2496             case WGL_DOUBLE_BUFFER_ARB:
2497                 curGLXAttr = GLX_DOUBLEBUFFER;
2498                 break;
2499             case WGL_STEREO_ARB:
2500                 curGLXAttr = GLX_STEREO;
2501                 break;
2502             case WGL_AUX_BUFFERS_ARB:
2503                 curGLXAttr = GLX_AUX_BUFFERS;
2504                 break;
2505
2506             case WGL_SUPPORT_GDI_ARB:
2507                 if (!fmt) goto pix_error;
2508                 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) ? TRUE : FALSE;
2509                 continue;
2510
2511             case WGL_DRAW_TO_BITMAP_ARB:
2512                 if (!fmt) goto pix_error;
2513                 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? TRUE : FALSE;
2514                 continue;
2515
2516             case WGL_DRAW_TO_WINDOW_ARB:
2517             case WGL_DRAW_TO_PBUFFER_ARB:
2518                 if (!fmt) goto pix_error;
2519                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2520                 if (hTest) goto get_error;
2521                 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2522                    (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2523                     piValues[i] = GL_TRUE;
2524                 else
2525                     piValues[i] = GL_FALSE;
2526                 continue;
2527
2528             case WGL_SWAP_METHOD_ARB:
2529                 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2530                  * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2531                  * point only ATI offers this.
2532                  */
2533                 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2534                 break;
2535
2536             case WGL_PBUFFER_LARGEST_ARB:
2537                 curGLXAttr = GLX_LARGEST_PBUFFER;
2538                 break;
2539
2540             case WGL_SAMPLE_BUFFERS_ARB:
2541                 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2542                 break;
2543
2544             case WGL_SAMPLES_ARB:
2545                 curGLXAttr = GLX_SAMPLES_ARB;
2546                 break;
2547
2548             case WGL_FLOAT_COMPONENTS_NV:
2549                 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2550                 break;
2551
2552             case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2553                 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2554                 break;
2555
2556             case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2557                 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2558                 break;
2559
2560             case WGL_ACCUM_RED_BITS_ARB:
2561                 curGLXAttr = GLX_ACCUM_RED_SIZE;
2562                 break;
2563             case WGL_ACCUM_GREEN_BITS_ARB:
2564                 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2565                 break;
2566             case WGL_ACCUM_BLUE_BITS_ARB:
2567                 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2568                 break;
2569             case WGL_ACCUM_ALPHA_BITS_ARB:
2570                 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2571                 break;
2572             case WGL_ACCUM_BITS_ARB:
2573                 if (!fmt) goto pix_error;
2574                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2575                 if (hTest) goto get_error;
2576                 piValues[i] = tmp;
2577                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2578                 if (hTest) goto get_error;
2579                 piValues[i] += tmp;
2580                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2581                 if (hTest) goto get_error;
2582                 piValues[i] += tmp;
2583                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2584                 if (hTest) goto get_error;
2585                 piValues[i] += tmp;
2586                 continue;
2587
2588             default:
2589                 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2590         }
2591
2592         /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2593          * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2594          * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2595          *
2596          * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2597          *       and check which options can work using iPixelFormat=0 and which not.
2598          *       A problem would be that this function is an extension. This would
2599          *       mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2600          */
2601         if (0 != curGLXAttr && iPixelFormat != 0) {
2602             if (!fmt) goto pix_error;
2603             hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2604             if (hTest) goto get_error;
2605             curGLXAttr = 0;
2606         } else { 
2607             piValues[i] = GL_FALSE; 
2608         }
2609     }
2610     wine_tsx11_unlock();
2611     return GL_TRUE;
2612
2613 get_error:
2614     wine_tsx11_unlock();
2615     ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2616     return GL_FALSE;
2617
2618 pix_error:
2619     wine_tsx11_unlock();
2620     ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
2621     return GL_FALSE;
2622 }
2623
2624 /**
2625  * X11DRV_wglGetPixelFormatAttribfvARB
2626  *
2627  * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2628  */
2629 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2630                                                  UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2631 {
2632     int *attr;
2633     int ret;
2634     UINT i;
2635
2636     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2637
2638     /* Allocate a temporary array to store integer values */
2639     attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2640     if (!attr) {
2641         ERR("couldn't allocate %d array\n", nAttributes);
2642         return GL_FALSE;
2643     }
2644
2645     /* Piggy-back on wglGetPixelFormatAttribivARB */
2646     ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2647     if (ret) {
2648         /* Convert integer values to float. Should also check for attributes
2649            that can give decimal values here */
2650         for (i=0; i<nAttributes;i++) {
2651             pfValues[i] = attr[i];
2652         }
2653     }
2654
2655     HeapFree(GetProcessHeap(), 0, attr);
2656     return ret;
2657 }
2658
2659 /**
2660  * X11DRV_wglBindTexImageARB
2661  *
2662  * WGL_ARB_render_texture: wglBindTexImageARB
2663  */
2664 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2665 {
2666     GLboolean ret = GL_FALSE;
2667
2668     TRACE("(%p, %d)\n", object, iBuffer);
2669
2670     if (!object->use_render_texture) {
2671         SetLastError(ERROR_INVALID_HANDLE);
2672         return GL_FALSE;
2673     }
2674
2675     if (1 == use_render_texture_emulation) {
2676         static int init = 0;
2677         int prev_binded_texture = 0;
2678         GLXContext prev_context;
2679         Drawable prev_drawable;
2680         GLXContext tmp_context;
2681
2682         wine_tsx11_lock();
2683         prev_context = pglXGetCurrentContext();
2684         prev_drawable = pglXGetCurrentDrawable();
2685
2686         /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2687            This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2688            isn't ideal performance wise but I wasn't able to get other ways working.
2689         */
2690         if(!init) {
2691             init = 1; /* Only show the FIXME once for performance reasons */
2692             FIXME("partial stub!\n");
2693         }
2694
2695         TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2696         tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2697
2698         opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2699
2700         /* Switch to our pbuffer */
2701         pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2702
2703         /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2704          * After that upload the pbuffer texture data. */
2705         opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2706         opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2707
2708         /* Switch back to the original drawable and upload the pbuffer-texture */
2709         pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2710         pglXDestroyContext(gdi_display, tmp_context);
2711         wine_tsx11_unlock();
2712         return GL_TRUE;
2713     }
2714
2715     return ret;
2716 }
2717
2718 /**
2719  * X11DRV_wglReleaseTexImageARB
2720  *
2721  * WGL_ARB_render_texture: wglReleaseTexImageARB
2722  */
2723 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2724 {
2725     GLboolean ret = GL_FALSE;
2726
2727     TRACE("(%p, %d)\n", object, iBuffer);
2728
2729     if (!object->use_render_texture) {
2730         SetLastError(ERROR_INVALID_HANDLE);
2731         return GL_FALSE;
2732     }
2733     if (1 == use_render_texture_emulation) {
2734         return GL_TRUE;
2735     }
2736     return ret;
2737 }
2738
2739 /**
2740  * X11DRV_wglGetExtensionsStringEXT
2741  *
2742  * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2743  */
2744 static const GLubyte *X11DRV_wglGetExtensionsStringEXT(void)
2745 {
2746     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2747     return (const GLubyte *)WineGLInfo.wglExtensions;
2748 }
2749
2750 /**
2751  * X11DRV_wglGetSwapIntervalEXT
2752  *
2753  * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2754  */
2755 static int X11DRV_wglGetSwapIntervalEXT(void)
2756 {
2757     /* GLX_SGI_swap_control doesn't have any provisions for getting the swap
2758      * interval, so the swap interval has to be tracked. */
2759     TRACE("()\n");
2760     return swap_interval;
2761 }
2762
2763 /**
2764  * X11DRV_wglSwapIntervalEXT
2765  *
2766  * WGL_EXT_swap_control: wglSwapIntervalEXT
2767  */
2768 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
2769 {
2770     BOOL ret = TRUE;
2771
2772     TRACE("(%d)\n", interval);
2773
2774     if (interval < 0)
2775     {
2776         SetLastError(ERROR_INVALID_DATA);
2777         return FALSE;
2778     }
2779     else if (!has_swap_control && interval == 0)
2780     {
2781         /* wglSwapIntervalEXT considers an interval value of zero to mean that
2782          * vsync should be disabled, but glXSwapIntervalSGI considers such a
2783          * value to be an error. Just silently ignore the request for now. */
2784         WARN("Request to disable vertical sync is not handled\n");
2785         swap_interval = 0;
2786     }
2787     else
2788     {
2789         if (pglXSwapIntervalSGI)
2790         {
2791             wine_tsx11_lock();
2792             ret = !pglXSwapIntervalSGI(interval);
2793             wine_tsx11_unlock();
2794         }
2795         else
2796             WARN("GLX_SGI_swap_control extension is not available\n");
2797
2798         if (ret)
2799             swap_interval = interval;
2800         else
2801             SetLastError(ERROR_DC_NOT_FOUND);
2802     }
2803
2804     return ret;
2805 }
2806
2807 /**
2808  * X11DRV_wglSetPixelFormatWINE
2809  *
2810  * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
2811  * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
2812  */
2813 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
2814 {
2815     WineGLPixelFormat *fmt;
2816     int value;
2817     HWND hwnd;
2818
2819     TRACE("(%p,%d)\n", hdc, format);
2820
2821     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, format, FALSE /* Offscreen */, &value);
2822     if (!fmt)
2823     {
2824         ERR( "Invalid format %d\n", format );
2825         return FALSE;
2826     }
2827
2828     hwnd = WindowFromDC( hdc );
2829     if (!hwnd || hwnd == GetDesktopWindow())
2830     {
2831         ERR( "not a valid window DC %p\n", hdc );
2832         return FALSE;
2833     }
2834
2835     wine_tsx11_lock();
2836     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
2837     wine_tsx11_unlock();
2838
2839     if (!(value & GLX_WINDOW_BIT))
2840     {
2841         WARN( "Pixel format %d is not compatible for window rendering\n", format );
2842         return FALSE;
2843     }
2844
2845     return SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, fmt->fmt_id, 0);
2846     /* DC pixel format will be set by the DCE update */
2847 }
2848
2849 /**
2850  * glxRequireVersion (internal)
2851  *
2852  * Check if the supported GLX version matches requiredVersion.
2853  */
2854 static BOOL glxRequireVersion(int requiredVersion)
2855 {
2856     /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2857     if(requiredVersion <= WineGLInfo.glxVersion[1])
2858         return TRUE;
2859
2860     return FALSE;
2861 }
2862
2863 static BOOL glxRequireExtension(const char *requiredExtension)
2864 {
2865     if (strstr(WineGLInfo.glxExtensions, requiredExtension) == NULL) {
2866         return FALSE;
2867     }
2868
2869     return TRUE;
2870 }
2871
2872 static void register_extension(const char *ext)
2873 {
2874     if (WineGLInfo.wglExtensions[0])
2875         strcat(WineGLInfo.wglExtensions, " ");
2876     strcat(WineGLInfo.wglExtensions, ext);
2877
2878     TRACE("'%s'\n", ext);
2879 }
2880
2881 /**
2882  * X11DRV_WineGL_LoadExtensions
2883  */
2884 static void X11DRV_WineGL_LoadExtensions(void)
2885 {
2886     WineGLInfo.wglExtensions[0] = 0;
2887
2888     /* ARB Extensions */
2889
2890     if(glxRequireExtension("GLX_ARB_create_context"))
2891     {
2892         register_extension( "WGL_ARB_create_context" );
2893         opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
2894
2895         if(glxRequireExtension("GLX_ARB_create_context_profile"))
2896             register_extension("WGL_ARB_create_context_profile");
2897     }
2898
2899     if(glxRequireExtension("GLX_ARB_fbconfig_float"))
2900     {
2901         register_extension("WGL_ARB_pixel_format_float");
2902         register_extension("WGL_ATI_pixel_format_float");
2903     }
2904
2905     register_extension( "WGL_ARB_extensions_string" );
2906     opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
2907
2908     if (glxRequireVersion(3))
2909     {
2910         register_extension( "WGL_ARB_make_current_read" );
2911         opengl_funcs.ext.p_wglGetCurrentReadDCARB   = X11DRV_wglGetCurrentReadDCARB;
2912         opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
2913     }
2914
2915     if (glxRequireExtension("GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
2916
2917     /* In general pbuffer functionality requires support in the X-server. The functionality is
2918      * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
2919      */
2920     if ( glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") )
2921     {
2922         register_extension( "WGL_ARB_pbuffer" );
2923         opengl_funcs.ext.p_wglCreatePbufferARB    = X11DRV_wglCreatePbufferARB;
2924         opengl_funcs.ext.p_wglDestroyPbufferARB   = X11DRV_wglDestroyPbufferARB;
2925         opengl_funcs.ext.p_wglGetPbufferDCARB     = X11DRV_wglGetPbufferDCARB;
2926         opengl_funcs.ext.p_wglQueryPbufferARB     = X11DRV_wglQueryPbufferARB;
2927         opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
2928         opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
2929     }
2930
2931     register_extension( "WGL_ARB_pixel_format" );
2932     opengl_funcs.ext.p_wglChoosePixelFormatARB      = X11DRV_wglChoosePixelFormatARB;
2933     opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
2934     opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
2935
2936     /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
2937     if (glxRequireExtension("GLX_ARB_render_texture") ||
2938         (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
2939     {
2940         register_extension( "WGL_ARB_render_texture" );
2941         opengl_funcs.ext.p_wglBindTexImageARB    = X11DRV_wglBindTexImageARB;
2942         opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
2943
2944         /* The WGL version of GLX_NV_float_buffer requires render_texture */
2945         if(glxRequireExtension("GLX_NV_float_buffer"))
2946             register_extension("WGL_NV_float_buffer");
2947
2948         /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
2949         if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
2950             register_extension("WGL_NV_texture_rectangle");
2951     }
2952
2953     /* EXT Extensions */
2954
2955     register_extension( "WGL_EXT_extensions_string" );
2956     opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
2957
2958     /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
2959      * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
2960     register_extension( "WGL_EXT_swap_control" );
2961     opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
2962     opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
2963
2964     if(glxRequireExtension("GLX_EXT_framebuffer_sRGB"))
2965         register_extension("WGL_EXT_framebuffer_sRGB");
2966
2967     if(glxRequireExtension("GLX_EXT_fbconfig_packed_float"))
2968         register_extension("WGL_EXT_pixel_format_packed_float");
2969
2970     if (glxRequireExtension("GLX_EXT_swap_control"))
2971         has_swap_control = TRUE;
2972
2973     /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
2974     if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
2975     {
2976         register_extension( "WGL_NV_vertex_array_range" );
2977         opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
2978         opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
2979     }
2980
2981     /* WINE-specific WGL Extensions */
2982
2983     /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
2984      * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
2985      */
2986     register_extension( "WGL_WINE_pixel_format_passthrough" );
2987     opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
2988 }
2989
2990
2991 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
2992 {
2993     wine_tsx11_lock(); 
2994     pglXDestroyGLXPixmap(display, glxpixmap);
2995     wine_tsx11_unlock(); 
2996     return TRUE;
2997 }
2998
2999 /**
3000  * glxdrv_SwapBuffers
3001  *
3002  * Swap the buffers of this DC
3003  */
3004 static BOOL glxdrv_SwapBuffers(PHYSDEV dev)
3005 {
3006   struct glx_physdev *physdev = get_glxdrv_dev( dev );
3007   struct wgl_context *ctx = NtCurrentTeb()->glContext;
3008
3009   TRACE("(%p)\n", dev->hdc);
3010
3011   if (!ctx)
3012   {
3013       WARN("Using a NULL context, skipping\n");
3014       SetLastError(ERROR_INVALID_HANDLE);
3015       return FALSE;
3016   }
3017
3018   if (!physdev->drawable)
3019   {
3020       WARN("Using an invalid drawable, skipping\n");
3021       SetLastError(ERROR_INVALID_HANDLE);
3022       return FALSE;
3023   }
3024
3025   wine_tsx11_lock();
3026   sync_context(ctx);
3027   switch (physdev->type)
3028   {
3029   case DC_GL_PIXMAP_WIN:
3030       if(pglXCopySubBufferMESA) {
3031           int w = physdev->x11dev->dc_rect.right - physdev->x11dev->dc_rect.left;
3032           int h = physdev->x11dev->dc_rect.bottom - physdev->x11dev->dc_rect.top;
3033
3034           /* (glX)SwapBuffers has an implicit glFlush effect, however
3035            * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3036            * copying */
3037           pglFlush();
3038           if(w > 0 && h > 0)
3039               pglXCopySubBufferMESA(gdi_display, physdev->drawable, 0, 0, w, h);
3040           break;
3041       }
3042       /* fall through */
3043   default:
3044       pglXSwapBuffers(gdi_display, physdev->drawable);
3045       break;
3046   }
3047
3048   flush_gl_drawable( physdev );
3049   wine_tsx11_unlock();
3050
3051   /* FPS support */
3052   if (TRACE_ON(fps))
3053   {
3054       static long prev_time, start_time;
3055       static unsigned long frames, frames_total;
3056
3057       DWORD time = GetTickCount();
3058       frames++;
3059       frames_total++;
3060       /* every 1.5 seconds */
3061       if (time - prev_time > 1500) {
3062           TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
3063                       1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
3064           prev_time = time;
3065           frames = 0;
3066           if(start_time == 0) start_time = time;
3067       }
3068   }
3069
3070   return TRUE;
3071 }
3072
3073 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3074 {
3075     WineGLPixelFormat *fmt;
3076     XVisualInfo *ret;
3077
3078     fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
3079     if(fmt == NULL)
3080         return NULL;
3081
3082     wine_tsx11_lock();
3083     ret = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
3084     wine_tsx11_unlock();
3085     return ret;
3086 }
3087
3088 static BOOL create_glx_dc( PHYSDEV *pdev )
3089 {
3090     /* assume that only the main x11 device implements GetDeviceCaps */
3091     X11DRV_PDEVICE *x11dev = get_x11drv_dev( GET_NEXT_PHYSDEV( *pdev, pGetDeviceCaps ));
3092     struct glx_physdev *physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) );
3093
3094     if (!physdev) return FALSE;
3095     physdev->x11dev = x11dev;
3096     push_dc_driver( pdev, &physdev->dev, &glxdrv_funcs );
3097     return TRUE;
3098 }
3099
3100 /**********************************************************************
3101  *           glxdrv_CreateDC
3102  */
3103 static BOOL glxdrv_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
3104                              LPCWSTR output, const DEVMODEW* initData )
3105 {
3106     return create_glx_dc( pdev );
3107 }
3108
3109 /**********************************************************************
3110  *           glxdrv_CreateCompatibleDC
3111  */
3112 static BOOL glxdrv_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
3113 {
3114     if (orig)  /* chain to next driver first */
3115     {
3116         orig = GET_NEXT_PHYSDEV( orig, pCreateCompatibleDC );
3117         if (!orig->funcs->pCreateCompatibleDC( orig, pdev )) return FALSE;
3118     }
3119     /* otherwise we have been called by x11drv */
3120     return create_glx_dc( pdev );
3121 }
3122
3123 /**********************************************************************
3124  *           glxdrv_DeleteDC
3125  */
3126 static BOOL glxdrv_DeleteDC( PHYSDEV dev )
3127 {
3128     struct glx_physdev *physdev = get_glxdrv_dev( dev );
3129     HeapFree( GetProcessHeap(), 0, physdev );
3130     return TRUE;
3131 }
3132
3133 /**********************************************************************
3134  *           glxdrv_ExtEscape
3135  */
3136 static INT glxdrv_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
3137                              INT out_count, LPVOID out_data )
3138 {
3139     struct glx_physdev *physdev = get_glxdrv_dev( dev );
3140
3141     dev = GET_NEXT_PHYSDEV( dev, pExtEscape );
3142
3143     if (escape == X11DRV_ESCAPE && in_data && in_count >= sizeof(enum x11drv_escape_codes))
3144     {
3145         switch (*(const enum x11drv_escape_codes *)in_data)
3146         {
3147         case X11DRV_SET_DRAWABLE:
3148             if (in_count >= sizeof(struct x11drv_escape_set_drawable))
3149             {
3150                 const struct x11drv_escape_set_drawable *data = in_data;
3151                 physdev->pixel_format = pixelformat_from_fbconfig_id( data->fbconfig_id );
3152                 physdev->type         = data->gl_type;
3153                 physdev->drawable     = data->gl_drawable;
3154                 physdev->pixmap       = data->pixmap;
3155                 TRACE( "SET_DRAWABLE hdc %p drawable %lx pf %u type %u\n",
3156                        dev->hdc, physdev->drawable, physdev->pixel_format, physdev->type );
3157             }
3158             break;
3159         case X11DRV_GET_DRAWABLE:
3160             if (out_count >= sizeof(struct x11drv_escape_get_drawable))
3161             {
3162                 struct x11drv_escape_get_drawable *data = out_data;
3163                 data->pixel_format = physdev->pixel_format;
3164                 data->gl_type      = physdev->type;
3165                 data->gl_drawable  = physdev->drawable;
3166                 data->pixmap       = physdev->pixmap;
3167             }
3168             break;
3169         case X11DRV_FLUSH_GL_DRAWABLE:
3170             flush_gl_drawable( physdev );
3171             return TRUE;
3172         default:
3173             break;
3174         }
3175     }
3176     return dev->funcs->pExtEscape( dev, escape, in_count, in_data, out_count, out_data );
3177 }
3178
3179 /**********************************************************************
3180  *           glxdrv_wine_get_wgl_driver
3181  */
3182 static struct opengl_funcs * glxdrv_wine_get_wgl_driver( PHYSDEV dev, UINT version )
3183 {
3184     if (version != WINE_WGL_DRIVER_VERSION)
3185     {
3186         ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3187         return NULL;
3188     }
3189
3190     if (has_opengl()) return &opengl_funcs;
3191
3192     dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
3193     return dev->funcs->wine_get_wgl_driver( dev, version );
3194 }
3195
3196 static const struct gdi_dc_funcs glxdrv_funcs =
3197 {
3198     NULL,                               /* pAbortDoc */
3199     NULL,                               /* pAbortPath */
3200     NULL,                               /* pAlphaBlend */
3201     NULL,                               /* pAngleArc */
3202     NULL,                               /* pArc */
3203     NULL,                               /* pArcTo */
3204     NULL,                               /* pBeginPath */
3205     NULL,                               /* pBlendImage */
3206     NULL,                               /* pChord */
3207     NULL,                               /* pCloseFigure */
3208     glxdrv_CreateCompatibleDC,          /* pCreateCompatibleDC */
3209     glxdrv_CreateDC,                    /* pCreateDC */
3210     glxdrv_DeleteDC,                    /* pDeleteDC */
3211     NULL,                               /* pDeleteObject */
3212     glxdrv_DescribePixelFormat,         /* pDescribePixelFormat */
3213     NULL,                               /* pDeviceCapabilities */
3214     NULL,                               /* pEllipse */
3215     NULL,                               /* pEndDoc */
3216     NULL,                               /* pEndPage */
3217     NULL,                               /* pEndPath */
3218     NULL,                               /* pEnumFonts */
3219     NULL,                               /* pEnumICMProfiles */
3220     NULL,                               /* pExcludeClipRect */
3221     NULL,                               /* pExtDeviceMode */
3222     glxdrv_ExtEscape,                   /* pExtEscape */
3223     NULL,                               /* pExtFloodFill */
3224     NULL,                               /* pExtSelectClipRgn */
3225     NULL,                               /* pExtTextOut */
3226     NULL,                               /* pFillPath */
3227     NULL,                               /* pFillRgn */
3228     NULL,                               /* pFlattenPath */
3229     NULL,                               /* pFontIsLinked */
3230     NULL,                               /* pFrameRgn */
3231     NULL,                               /* pGdiComment */
3232     NULL,                               /* pGdiRealizationInfo */
3233     NULL,                               /* pGetBoundsRect */
3234     NULL,                               /* pGetCharABCWidths */
3235     NULL,                               /* pGetCharABCWidthsI */
3236     NULL,                               /* pGetCharWidth */
3237     NULL,                               /* pGetDeviceCaps */
3238     NULL,                               /* pGetDeviceGammaRamp */
3239     NULL,                               /* pGetFontData */
3240     NULL,                               /* pGetFontUnicodeRanges */
3241     NULL,                               /* pGetGlyphIndices */
3242     NULL,                               /* pGetGlyphOutline */
3243     NULL,                               /* pGetICMProfile */
3244     NULL,                               /* pGetImage */
3245     NULL,                               /* pGetKerningPairs */
3246     NULL,                               /* pGetNearestColor */
3247     NULL,                               /* pGetOutlineTextMetrics */
3248     NULL,                               /* pGetPixel */
3249     NULL,                               /* pGetSystemPaletteEntries */
3250     NULL,                               /* pGetTextCharsetInfo */
3251     NULL,                               /* pGetTextExtentExPoint */
3252     NULL,                               /* pGetTextExtentExPointI */
3253     NULL,                               /* pGetTextFace */
3254     NULL,                               /* pGetTextMetrics */
3255     NULL,                               /* pGradientFill */
3256     NULL,                               /* pIntersectClipRect */
3257     NULL,                               /* pInvertRgn */
3258     NULL,                               /* pLineTo */
3259     NULL,                               /* pModifyWorldTransform */
3260     NULL,                               /* pMoveTo */
3261     NULL,                               /* pOffsetClipRgn */
3262     NULL,                               /* pOffsetViewportOrg */
3263     NULL,                               /* pOffsetWindowOrg */
3264     NULL,                               /* pPaintRgn */
3265     NULL,                               /* pPatBlt */
3266     NULL,                               /* pPie */
3267     NULL,                               /* pPolyBezier */
3268     NULL,                               /* pPolyBezierTo */
3269     NULL,                               /* pPolyDraw */
3270     NULL,                               /* pPolyPolygon */
3271     NULL,                               /* pPolyPolyline */
3272     NULL,                               /* pPolygon */
3273     NULL,                               /* pPolyline */
3274     NULL,                               /* pPolylineTo */
3275     NULL,                               /* pPutImage */
3276     NULL,                               /* pRealizeDefaultPalette */
3277     NULL,                               /* pRealizePalette */
3278     NULL,                               /* pRectangle */
3279     NULL,                               /* pResetDC */
3280     NULL,                               /* pRestoreDC */
3281     NULL,                               /* pRoundRect */
3282     NULL,                               /* pSaveDC */
3283     NULL,                               /* pScaleViewportExt */
3284     NULL,                               /* pScaleWindowExt */
3285     NULL,                               /* pSelectBitmap */
3286     NULL,                               /* pSelectBrush */
3287     NULL,                               /* pSelectClipPath */
3288     NULL,                               /* pSelectFont */
3289     NULL,                               /* pSelectPalette */
3290     NULL,                               /* pSelectPen */
3291     NULL,                               /* pSetArcDirection */
3292     NULL,                               /* pSetBkColor */
3293     NULL,                               /* pSetBkMode */
3294     NULL,                               /* pSetBoundsRect */
3295     NULL,                               /* pSetDCBrushColor */
3296     NULL,                               /* pSetDCPenColor */
3297     NULL,                               /* pSetDIBitsToDevice */
3298     NULL,                               /* pSetDeviceClipping */
3299     NULL,                               /* pSetDeviceGammaRamp */
3300     NULL,                               /* pSetLayout */
3301     NULL,                               /* pSetMapMode */
3302     NULL,                               /* pSetMapperFlags */
3303     NULL,                               /* pSetPixel */
3304     glxdrv_SetPixelFormat,              /* pSetPixelFormat */
3305     NULL,                               /* pSetPolyFillMode */
3306     NULL,                               /* pSetROP2 */
3307     NULL,                               /* pSetRelAbs */
3308     NULL,                               /* pSetStretchBltMode */
3309     NULL,                               /* pSetTextAlign */
3310     NULL,                               /* pSetTextCharacterExtra */
3311     NULL,                               /* pSetTextColor */
3312     NULL,                               /* pSetTextJustification */
3313     NULL,                               /* pSetViewportExt */
3314     NULL,                               /* pSetViewportOrg */
3315     NULL,                               /* pSetWindowExt */
3316     NULL,                               /* pSetWindowOrg */
3317     NULL,                               /* pSetWorldTransform */
3318     NULL,                               /* pStartDoc */
3319     NULL,                               /* pStartPage */
3320     NULL,                               /* pStretchBlt */
3321     NULL,                               /* pStretchDIBits */
3322     NULL,                               /* pStrokeAndFillPath */
3323     NULL,                               /* pStrokePath */
3324     glxdrv_SwapBuffers,                 /* pSwapBuffers */
3325     NULL,                               /* pUnrealizePalette */
3326     NULL,                               /* pWidenPath */
3327     glxdrv_wine_get_wgl_driver,         /* wine_get_wgl_driver */
3328     GDI_PRIORITY_GRAPHICS_DRV + 20      /* priority */
3329 };
3330
3331 static struct opengl_funcs opengl_funcs =
3332 {
3333     {
3334         glxdrv_wglCopyContext,              /* p_wglCopyContext */
3335         glxdrv_wglCreateContext,            /* p_wglCreateContext */
3336         glxdrv_wglDeleteContext,            /* p_wglDeleteContext */
3337         glxdrv_wglGetCurrentDC,             /* p_wglGetCurrentDC */
3338         glxdrv_wglGetPixelFormat,           /* p_wglGetPixelFormat */
3339         glxdrv_wglGetProcAddress,           /* p_wglGetProcAddress */
3340         glxdrv_wglMakeCurrent,              /* p_wglMakeCurrent */
3341         glxdrv_wglShareLists,               /* p_wglShareLists */
3342     }
3343 };
3344
3345 const struct gdi_dc_funcs *get_glx_driver(void)
3346 {
3347     return &glxdrv_funcs;
3348 }
3349
3350 #else  /* no OpenGL includes */
3351
3352 const struct gdi_dc_funcs *get_glx_driver(void)
3353 {
3354     return NULL;
3355 }
3356
3357 void mark_drawable_dirty(Drawable old, Drawable new)
3358 {
3359 }
3360
3361 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
3362 {
3363     return 0;
3364 }
3365
3366
3367 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3368 {
3369     return FALSE;
3370 }
3371
3372 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3373 {
3374     return NULL;
3375 }
3376
3377 #endif /* defined(SONAME_LIBGL) */