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