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