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