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