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