Revert "winex11: Refuse to set the pixel format for HWND_MESSAGE windows."
[wine.git] / dlls / winex11.drv / opengl.c
blob61aff532efda7e1db9e15b5b751132e10e360609
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
5 * Copyright 2005 Alex Woods
6 * Copyright 2005 Raphael Junqueira
7 * Copyright 2006-2009 Roderick Colenbrander
8 * Copyright 2006 Tomas Carnecky
9 * Copyright 2012 Alexandre Julliard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include "wine/port.h"
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_SYS_UN_H
37 #include <sys/un.h>
38 #endif
39 #ifdef HAVE_GL_GL_H
40 # include <GL/gl.h>
41 #endif
42 #ifdef HAVE_GL_GLX_H
43 # include <GL/glx.h>
44 #endif
45 #undef APIENTRY
46 #undef GLAPI
47 #undef WINGDIAPI
49 #include "x11drv.h"
50 #include "xcomposite.h"
51 #include "winternl.h"
52 #include "wine/library.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
57 #ifdef SONAME_LIBGL
59 WINE_DECLARE_DEBUG_CHANNEL(winediag);
61 #include "wine/wgl_driver.h"
62 #include "wine/wglext.h"
64 /* For compatibility with old Mesa headers */
65 #ifndef GLX_SAMPLE_BUFFERS_ARB
66 # define GLX_SAMPLE_BUFFERS_ARB 100000
67 #endif
68 #ifndef GLX_SAMPLES_ARB
69 # define GLX_SAMPLES_ARB 100001
70 #endif
71 #ifndef GL_TEXTURE_CUBE_MAP
72 # define GL_TEXTURE_CUBE_MAP 0x8513
73 #endif
74 #ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT
75 # define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
76 #endif
77 #ifndef GLX_EXT_fbconfig_packed_float
78 # define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
79 # define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT 0x00000008
80 #endif
81 #ifndef GLX_ARB_create_context
82 # define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
83 # define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
84 # define GLX_CONTEXT_FLAGS_ARB 0x2094
85 #endif
86 #ifndef GLX_ARB_create_context_profile
87 # define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
88 #endif
89 /** GLX_ATI_pixel_format_float */
90 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
91 /** GLX_ARB_pixel_format_float */
92 #define GLX_RGBA_FLOAT_BIT 0x00000004
93 #define GLX_RGBA_FLOAT_TYPE 0x20B9
94 /** GL_NV_float_buffer */
95 #define GL_FLOAT_R_NV 0x8880
96 #define GL_FLOAT_RG_NV 0x8881
97 #define GL_FLOAT_RGB_NV 0x8882
98 #define GL_FLOAT_RGBA_NV 0x8883
99 #define GL_FLOAT_R16_NV 0x8884
100 #define GL_FLOAT_R32_NV 0x8885
101 #define GL_FLOAT_RG16_NV 0x8886
102 #define GL_FLOAT_RG32_NV 0x8887
103 #define GL_FLOAT_RGB16_NV 0x8888
104 #define GL_FLOAT_RGB32_NV 0x8889
105 #define GL_FLOAT_RGBA16_NV 0x888A
106 #define GL_FLOAT_RGBA32_NV 0x888B
107 #define GL_TEXTURE_FLOAT_COMPONENTS_NV 0x888C
108 #define GL_FLOAT_CLEAR_COLOR_VALUE_NV 0x888D
109 #define GL_FLOAT_RGBA_MODE_NV 0x888E
110 /** GLX_NV_float_buffer */
111 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
114 struct WineGLInfo {
115 const char *glVersion;
116 char *glExtensions;
118 int glxVersion[2];
120 const char *glxServerVersion;
121 const char *glxServerVendor;
122 const char *glxServerExtensions;
124 const char *glxClientVersion;
125 const char *glxClientVendor;
126 const char *glxClientExtensions;
128 const char *glxExtensions;
130 BOOL glxDirect;
131 char wglExtensions[4096];
134 struct wgl_pixel_format
136 GLXFBConfig fbconfig;
137 int fmt_id;
138 int render_type;
139 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
142 struct wgl_context
144 HDC hdc;
145 BOOL has_been_current;
146 BOOL sharing;
147 BOOL gl3_context;
148 XVisualInfo *vis;
149 const struct wgl_pixel_format *fmt;
150 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
151 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
152 GLXContext ctx;
153 Drawable drawables[2];
154 BOOL refresh_drawables;
155 struct list entry;
158 struct wgl_pbuffer
160 Drawable drawable;
161 const struct wgl_pixel_format* fmt;
162 int width;
163 int height;
164 int* attribList;
165 int use_render_texture; /* This is also the internal texture format */
166 int texture_bind_target;
167 int texture_bpp;
168 GLint texture_format;
169 GLuint texture_target;
170 GLenum texture_type;
171 GLuint texture;
172 int texture_level;
175 enum dc_gl_type
177 DC_GL_NONE, /* no GL support (pixel format not set yet) */
178 DC_GL_WINDOW, /* normal top-level window */
179 DC_GL_CHILD_WIN, /* child window using XComposite */
180 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
181 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
184 struct gl_drawable
186 enum dc_gl_type type; /* type of GL surface */
187 Drawable drawable; /* drawable for rendering to the client area */
188 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
189 Colormap colormap; /* colormap used for the drawable */
190 const struct wgl_pixel_format *format; /* pixel format for the drawable */
191 XVisualInfo *visual; /* information about the GL visual */
192 RECT rect; /* drawable rect, relative to whole window drawable */
195 /* X context to associate a struct gl_drawable to an hwnd */
196 static XContext gl_hwnd_context;
197 /* X context to associate a struct gl_drawable to a pbuffer hdc */
198 static XContext gl_pbuffer_context;
200 static const struct gdi_dc_funcs glxdrv_funcs;
202 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
204 return (struct glx_physdev *)dev;
207 static struct list context_list = LIST_INIT( context_list );
208 static struct WineGLInfo WineGLInfo = { 0 };
209 static struct wgl_pixel_format *pixel_formats;
210 static int nb_pixel_formats, nb_onscreen_formats;
211 static int use_render_texture_emulation = 1;
212 static BOOL has_swap_control;
213 static int swap_interval = 1;
215 static CRITICAL_SECTION context_section;
216 static CRITICAL_SECTION_DEBUG critsect_debug =
218 0, 0, &context_section,
219 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
220 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
222 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
224 static struct opengl_funcs opengl_funcs;
226 #define USE_GL_FUNC(name) #name,
227 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
228 #undef USE_GL_FUNC
230 static void X11DRV_WineGL_LoadExtensions(void);
231 static void init_pixel_formats( Display *display );
232 static BOOL glxRequireVersion(int requiredVersion);
234 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
235 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
236 TRACE(" - dwFlags : ");
237 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
238 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
239 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
240 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
241 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
242 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
243 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
244 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
245 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
246 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
247 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
248 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
249 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
250 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
251 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
252 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
253 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
254 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
255 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
256 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
257 #undef TEST_AND_DUMP
258 TRACE("\n");
260 TRACE(" - iPixelType : ");
261 switch (ppfd->iPixelType) {
262 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
263 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
265 TRACE("\n");
267 TRACE(" - Color : %d\n", ppfd->cColorBits);
268 TRACE(" - Red : %d\n", ppfd->cRedBits);
269 TRACE(" - Green : %d\n", ppfd->cGreenBits);
270 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
271 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
272 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
273 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
274 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
275 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
277 TRACE(" - iLayerType : ");
278 switch (ppfd->iLayerType) {
279 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
280 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
281 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
283 TRACE("\n");
286 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
287 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
289 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
290 /* GLX 1.0 */
291 MAKE_FUNCPTR(glXChooseVisual)
292 MAKE_FUNCPTR(glXCopyContext)
293 MAKE_FUNCPTR(glXCreateContext)
294 MAKE_FUNCPTR(glXCreateGLXPixmap)
295 MAKE_FUNCPTR(glXGetCurrentContext)
296 MAKE_FUNCPTR(glXGetCurrentDrawable)
297 MAKE_FUNCPTR(glXDestroyContext)
298 MAKE_FUNCPTR(glXDestroyGLXPixmap)
299 MAKE_FUNCPTR(glXGetConfig)
300 MAKE_FUNCPTR(glXIsDirect)
301 MAKE_FUNCPTR(glXMakeCurrent)
302 MAKE_FUNCPTR(glXSwapBuffers)
303 MAKE_FUNCPTR(glXQueryExtension)
304 MAKE_FUNCPTR(glXQueryVersion)
306 /* GLX 1.1 */
307 MAKE_FUNCPTR(glXGetClientString)
308 MAKE_FUNCPTR(glXQueryExtensionsString)
309 MAKE_FUNCPTR(glXQueryServerString)
311 /* GLX 1.3 */
312 MAKE_FUNCPTR(glXGetFBConfigs)
313 MAKE_FUNCPTR(glXChooseFBConfig)
314 MAKE_FUNCPTR(glXCreatePbuffer)
315 MAKE_FUNCPTR(glXCreateNewContext)
316 MAKE_FUNCPTR(glXDestroyPbuffer)
317 MAKE_FUNCPTR(glXGetFBConfigAttrib)
318 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
319 MAKE_FUNCPTR(glXMakeContextCurrent)
320 MAKE_FUNCPTR(glXQueryDrawable)
321 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
322 #undef MAKE_FUNCPTR
324 /* GLX Extensions */
325 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
326 static void* (*pglXGetProcAddressARB)(const GLubyte *);
327 static int (*pglXSwapIntervalSGI)(int);
329 /* NV GLX Extension */
330 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
331 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
333 /* MESA GLX Extensions */
334 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
336 /* Standard OpenGL */
337 static void (*pglFinish)(void);
338 static void (*pglFlush)(void);
340 static void wglFinish(void);
341 static void wglFlush(void);
343 /* check if the extension is present in the list */
344 static BOOL has_extension( const char *list, const char *ext )
346 size_t len = strlen( ext );
348 while (list)
350 while (*list == ' ') list++;
351 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
352 list = strchr( list, ' ' );
354 return FALSE;
357 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
359 /* In the future we might want to find the exact X or GLX error to report back to the app */
360 return 1;
363 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
365 int screen = DefaultScreen(gdi_display);
366 Window win = 0, root = 0;
367 const char *gl_renderer;
368 const char* str;
369 XVisualInfo *vis;
370 GLXContext ctx = NULL;
371 XSetWindowAttributes attr;
372 BOOL ret = FALSE;
373 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
375 attr.override_redirect = True;
376 attr.colormap = None;
377 attr.border_pixel = 0;
379 vis = pglXChooseVisual(gdi_display, screen, attribList);
380 if (vis) {
381 #ifdef __i386__
382 WORD old_fs = wine_get_fs();
383 /* Create a GLX Context. Without one we can't query GL information */
384 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
385 if (wine_get_fs() != old_fs)
387 wine_set_fs( old_fs );
388 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
389 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
390 goto done;
392 #else
393 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
394 #endif
396 if (!ctx) goto done;
398 root = RootWindow( gdi_display, vis->screen );
399 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
400 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
401 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
402 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
403 XMapWindow( gdi_display, win );
404 else
405 win = root;
407 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
409 ERR_(winediag)( "Unable to activate OpenGL context, most likely your OpenGL drivers haven't been installed correctly\n" );
410 goto done;
412 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
413 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
414 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
415 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
416 strcpy(WineGLInfo.glExtensions, str);
418 /* Get the common GLX version supported by GLX client and server ( major/minor) */
419 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
421 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
422 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
423 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
425 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
426 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
427 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
429 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
430 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
432 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
433 TRACE("GL renderer : %s.\n", gl_renderer);
434 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
435 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
436 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
437 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
438 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
439 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
441 if(!WineGLInfo.glxDirect)
443 int fd = ConnectionNumber(gdi_display);
444 struct sockaddr_un uaddr;
445 unsigned int uaddrlen = sizeof(struct sockaddr_un);
447 /* In general indirect rendering on a local X11 server indicates a driver problem.
448 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
450 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
451 ERR_(winediag)("Direct rendering is disabled, most likely your OpenGL drivers "
452 "haven't been installed correctly (using GL renderer %s, version %s).\n",
453 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
455 else
457 /* In general you would expect that if direct rendering is returned, that you receive hardware
458 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
459 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
460 * software rendering, it shows direct rendering.
462 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
463 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
464 * it shows 'Mesa X11'.
466 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
467 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your OpenGL "
468 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
469 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
471 ret = TRUE;
473 done:
474 if(vis) XFree(vis);
475 if(ctx) {
476 pglXMakeCurrent(gdi_display, None, NULL);
477 pglXDestroyContext(gdi_display, ctx);
479 if (win != root) XDestroyWindow( gdi_display, win );
480 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
481 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
482 return ret;
485 static BOOL has_opengl(void)
487 static int init_done;
488 static void *opengl_handle;
490 char buffer[200];
491 int error_base, event_base;
492 unsigned int i;
494 if (init_done) return (opengl_handle != NULL);
495 init_done = 1;
497 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
498 and include all dependencies */
499 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
500 if (opengl_handle == NULL)
502 ERR( "Failed to load libGL: %s\n", buffer );
503 ERR( "OpenGL support is disabled.\n");
504 return FALSE;
507 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
509 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
511 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
512 goto failed;
516 /* redirect some standard OpenGL functions */
517 #define REDIRECT(func) \
518 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
519 REDIRECT( glFinish );
520 REDIRECT( glFlush );
521 #undef REDIRECT
523 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
524 if (pglXGetProcAddressARB == NULL) {
525 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
526 goto failed;
529 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
531 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
532 goto failed; \
533 } while(0)
535 /* GLX 1.0 */
536 LOAD_FUNCPTR(glXChooseVisual);
537 LOAD_FUNCPTR(glXCopyContext);
538 LOAD_FUNCPTR(glXCreateContext);
539 LOAD_FUNCPTR(glXCreateGLXPixmap);
540 LOAD_FUNCPTR(glXGetCurrentContext);
541 LOAD_FUNCPTR(glXGetCurrentDrawable);
542 LOAD_FUNCPTR(glXDestroyContext);
543 LOAD_FUNCPTR(glXDestroyGLXPixmap);
544 LOAD_FUNCPTR(glXGetConfig);
545 LOAD_FUNCPTR(glXIsDirect);
546 LOAD_FUNCPTR(glXMakeCurrent);
547 LOAD_FUNCPTR(glXSwapBuffers);
548 LOAD_FUNCPTR(glXQueryExtension);
549 LOAD_FUNCPTR(glXQueryVersion);
551 /* GLX 1.1 */
552 LOAD_FUNCPTR(glXGetClientString);
553 LOAD_FUNCPTR(glXQueryExtensionsString);
554 LOAD_FUNCPTR(glXQueryServerString);
556 /* GLX 1.3 */
557 LOAD_FUNCPTR(glXCreatePbuffer);
558 LOAD_FUNCPTR(glXCreateNewContext);
559 LOAD_FUNCPTR(glXDestroyPbuffer);
560 LOAD_FUNCPTR(glXMakeContextCurrent);
561 LOAD_FUNCPTR(glXGetCurrentReadDrawable);
562 LOAD_FUNCPTR(glXGetFBConfigs);
563 #undef LOAD_FUNCPTR
565 /* It doesn't matter if these fail. They'll only be used if the driver reports
566 the associated extension is available (and if a driver reports the extension
567 is available but fails to provide the functions, it's quite broken) */
568 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
569 /* ARB GLX Extension */
570 LOAD_FUNCPTR(glXCreateContextAttribsARB);
571 /* SGI GLX Extension */
572 LOAD_FUNCPTR(glXSwapIntervalSGI);
573 /* NV GLX Extension */
574 LOAD_FUNCPTR(glXAllocateMemoryNV);
575 LOAD_FUNCPTR(glXFreeMemoryNV);
576 #undef LOAD_FUNCPTR
578 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
580 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
581 TRACE("GLX is up and running error_base = %d\n", error_base);
582 } else {
583 ERR( "GLX extension is missing, disabling OpenGL.\n" );
584 goto failed;
586 gl_hwnd_context = XUniqueContext();
587 gl_pbuffer_context = XUniqueContext();
589 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
590 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
591 * rendering is used.
593 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
594 * depends on the reported GLX client / server version and on the client / server extension list.
595 * Those don't have to be the same.
597 * In general the server GLX information lists the capabilities in case of indirect rendering.
598 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
599 * available and in that case the client GLX informat can be used.
600 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
601 * in the GLX version/extension list. When a program does this it works for certain for both
602 * direct and indirect rendering.
604 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
605 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
606 * extension list which causes this extension not to be listed. (Wine requires this extension).
607 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
608 * pbuffers is around.
610 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
611 * the ATI drivers and from then on use GLX client information for them.
614 if(glxRequireVersion(3)) {
615 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
616 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
617 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
618 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
619 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
620 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
621 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
622 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
624 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
625 * enable this function when the Xserver understand GLX 1.3 or newer
627 pglXQueryDrawable = NULL;
628 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
629 TRACE("Overriding ATI GLX capabilities!\n");
630 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
631 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
632 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
633 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
635 /* Use client GLX information in case of the ATI drivers. We override the
636 * capabilities over here and not somewhere else as ATI might better their
637 * life in the future. In case they release proper drivers this block of
638 * code won't be called. */
639 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
640 } else {
641 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
644 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
645 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
648 X11DRV_WineGL_LoadExtensions();
649 init_pixel_formats( gdi_display );
650 return TRUE;
652 failed:
653 wine_dlclose(opengl_handle, NULL, 0);
654 opengl_handle = NULL;
655 return FALSE;
658 static int describeContext( struct wgl_context *ctx ) {
659 int tmp;
660 int ctx_vis_id;
661 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
662 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
663 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
664 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
665 TRACE(" - VISUAL_ID 0x%x\n", tmp);
666 ctx_vis_id = tmp;
667 return ctx_vis_id;
670 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
671 int nAttribs = 0;
672 unsigned cur = 0;
673 int pop;
674 int drawattrib = 0;
675 int nvfloatattrib = GLX_DONT_CARE;
676 int pixelattrib = GLX_DONT_CARE;
678 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
679 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
680 while (iWGLAttr && 0 != iWGLAttr[cur]) {
681 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
683 switch (iWGLAttr[cur]) {
684 case WGL_AUX_BUFFERS_ARB:
685 pop = iWGLAttr[++cur];
686 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
687 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
688 break;
689 case WGL_COLOR_BITS_ARB:
690 pop = iWGLAttr[++cur];
691 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
692 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
693 break;
694 case WGL_BLUE_BITS_ARB:
695 pop = iWGLAttr[++cur];
696 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
697 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
698 break;
699 case WGL_RED_BITS_ARB:
700 pop = iWGLAttr[++cur];
701 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
702 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
703 break;
704 case WGL_GREEN_BITS_ARB:
705 pop = iWGLAttr[++cur];
706 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
707 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
708 break;
709 case WGL_ALPHA_BITS_ARB:
710 pop = iWGLAttr[++cur];
711 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
712 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
713 break;
714 case WGL_DEPTH_BITS_ARB:
715 pop = iWGLAttr[++cur];
716 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
717 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
718 break;
719 case WGL_STENCIL_BITS_ARB:
720 pop = iWGLAttr[++cur];
721 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
722 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
723 break;
724 case WGL_DOUBLE_BUFFER_ARB:
725 pop = iWGLAttr[++cur];
726 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
727 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
728 break;
729 case WGL_STEREO_ARB:
730 pop = iWGLAttr[++cur];
731 PUSH2(oGLXAttr, GLX_STEREO, pop);
732 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
733 break;
735 case WGL_PIXEL_TYPE_ARB:
736 pop = iWGLAttr[++cur];
737 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
738 switch (pop) {
739 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
740 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
741 /* 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 */
742 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
743 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
744 default:
745 ERR("unexpected PixelType(%x)\n", pop);
746 pop = 0;
748 break;
750 case WGL_SUPPORT_GDI_ARB:
751 /* This flag is set in a pixel format */
752 pop = iWGLAttr[++cur];
753 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
754 break;
756 case WGL_DRAW_TO_BITMAP_ARB:
757 /* This flag is set in a pixel format */
758 pop = iWGLAttr[++cur];
759 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
760 break;
762 case WGL_DRAW_TO_WINDOW_ARB:
763 pop = iWGLAttr[++cur];
764 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
765 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
766 if (pop) {
767 drawattrib |= GLX_WINDOW_BIT;
769 break;
771 case WGL_DRAW_TO_PBUFFER_ARB:
772 pop = iWGLAttr[++cur];
773 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
774 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
775 if (pop) {
776 drawattrib |= GLX_PBUFFER_BIT;
778 break;
780 case WGL_ACCELERATION_ARB:
781 /* This flag is set in a pixel format */
782 pop = iWGLAttr[++cur];
783 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
784 break;
786 case WGL_SUPPORT_OPENGL_ARB:
787 pop = iWGLAttr[++cur];
788 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
789 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
790 break;
792 case WGL_SWAP_METHOD_ARB:
793 pop = iWGLAttr[++cur];
794 /* For now we ignore this and just return SWAP_EXCHANGE */
795 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
796 break;
798 case WGL_PBUFFER_LARGEST_ARB:
799 pop = iWGLAttr[++cur];
800 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
801 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
802 break;
804 case WGL_SAMPLE_BUFFERS_ARB:
805 pop = iWGLAttr[++cur];
806 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
807 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
808 break;
810 case WGL_SAMPLES_ARB:
811 pop = iWGLAttr[++cur];
812 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
813 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
814 break;
816 case WGL_TEXTURE_FORMAT_ARB:
817 case WGL_TEXTURE_TARGET_ARB:
818 case WGL_MIPMAP_TEXTURE_ARB:
819 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
820 pop = iWGLAttr[++cur];
821 if (NULL == pbuf) {
822 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
824 if (!use_render_texture_emulation) {
825 if (WGL_NO_TEXTURE_ARB != pop) {
826 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
827 return -1; /** error: don't support it */
828 } else {
829 drawattrib |= GLX_PBUFFER_BIT;
832 break ;
833 case WGL_FLOAT_COMPONENTS_NV:
834 nvfloatattrib = iWGLAttr[++cur];
835 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
836 break ;
837 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
838 case WGL_BIND_TO_TEXTURE_RGB_ARB:
839 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
840 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
841 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
842 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
843 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
844 pop = iWGLAttr[++cur];
845 /** cannot be converted, see direct handling on
846 * - wglGetPixelFormatAttribivARB
847 * TODO: wglChoosePixelFormat
849 break ;
850 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
851 pop = iWGLAttr[++cur];
852 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
853 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
854 break ;
856 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
857 pop = iWGLAttr[++cur];
858 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
859 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
860 break ;
861 default:
862 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
863 break;
865 ++cur;
868 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
869 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
870 * pixmap and pbuffer formats appear as well. */
871 if (!drawattrib) drawattrib = GLX_DONT_CARE;
872 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
873 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
875 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
876 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
877 * GLX_DONT_CARE unless it is overridden. */
878 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
879 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
881 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
882 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
883 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
884 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
887 return nAttribs;
890 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
892 int render_type=0, render_type_bit;
893 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
894 switch(render_type_bit)
896 case GLX_RGBA_BIT:
897 render_type = GLX_RGBA_TYPE;
898 break;
899 case GLX_COLOR_INDEX_BIT:
900 render_type = GLX_COLOR_INDEX_TYPE;
901 break;
902 case GLX_RGBA_FLOAT_BIT:
903 render_type = GLX_RGBA_FLOAT_TYPE;
904 break;
905 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
906 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
907 break;
908 default:
909 ERR("Unknown render_type: %x\n", render_type_bit);
911 return render_type;
914 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
915 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
917 int dbuf, value;
918 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
919 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
921 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
922 * the GLX_PIXMAP_BIT set. */
923 return !dbuf && (value & GLX_PIXMAP_BIT);
926 static void init_pixel_formats( Display *display )
928 struct wgl_pixel_format *list;
929 int size = 0, onscreen_size = 0;
930 int fmt_id, nCfgs, i, run, bmp_formats;
931 GLXFBConfig* cfgs;
932 XVisualInfo *visinfo;
934 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
935 if (NULL == cfgs || 0 == nCfgs) {
936 if(cfgs != NULL) XFree(cfgs);
937 ERR("glXChooseFBConfig returns NULL\n");
938 return;
941 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
942 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
943 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
944 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
946 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
948 for(i=0, bmp_formats=0; i<nCfgs; i++)
950 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
951 bmp_formats++;
953 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
955 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
957 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
958 * Do this as GLX doesn't guarantee that the list is sorted */
959 for(run=0; run < 2; run++)
961 for(i=0; i<nCfgs; i++) {
962 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
963 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
965 /* The first run we only add onscreen formats (ones which have an associated X Visual).
966 * The second run we only set offscreen formats. */
967 if(!run && visinfo)
969 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
970 * The contents is copied to the destination using XCopyArea. For the copying to work
971 * the depth of the source and destination window should be the same. In general this should
972 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
973 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
974 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
975 * list formats with the same depth. */
976 if(visinfo->depth != default_visual.depth)
978 XFree(visinfo);
979 continue;
982 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
983 list[size].fbconfig = cfgs[i];
984 list[size].fmt_id = fmt_id;
985 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
986 list[size].dwFlags = 0;
987 size++;
988 onscreen_size++;
990 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
991 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
993 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
994 list[size].fbconfig = cfgs[i];
995 list[size].fmt_id = fmt_id;
996 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
997 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
998 size++;
999 onscreen_size++;
1001 } else if(run && !visinfo) {
1002 int window_drawable=0;
1003 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1005 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1006 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1007 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1008 * likely make our child window opengl rendering more complicated since likely you can't use
1009 * XCopyArea on a GLX Window.
1010 * For now ignore fbconfigs which are window drawable but lack a visual. */
1011 if(window_drawable & GLX_WINDOW_BIT)
1013 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1014 continue;
1017 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1018 list[size].fbconfig = cfgs[i];
1019 list[size].fmt_id = fmt_id;
1020 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1021 list[size].dwFlags = 0;
1022 size++;
1025 if (visinfo) XFree(visinfo);
1029 XFree(cfgs);
1031 pixel_formats = list;
1032 nb_pixel_formats = size;
1033 nb_onscreen_formats = onscreen_size;
1036 static inline BOOL is_valid_pixel_format( int format )
1038 return format > 0 && format <= nb_pixel_formats;
1041 static inline BOOL is_onscreen_pixel_format( int format )
1043 return format > 0 && format <= nb_onscreen_formats;
1046 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1048 return format - pixel_formats + 1;
1051 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1052 * In our WGL implementation we only support a subset of these formats namely the format of
1053 * Wine's main visual and offscreen formats (if they are available).
1054 * This function converts a WGL format to its corresponding GLX one.
1056 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1058 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1059 * iPixelFormat in case of probing the number of pixelformats.
1061 if (is_valid_pixel_format( iPixelFormat ) &&
1062 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1063 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1064 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1065 return &pixel_formats[iPixelFormat-1];
1067 return NULL;
1070 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1071 static void mark_drawable_dirty(Drawable old, Drawable new)
1073 struct wgl_context *ctx;
1075 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1077 if (old == ctx->drawables[0]) {
1078 ctx->drawables[0] = new;
1079 ctx->refresh_drawables = TRUE;
1081 if (old == ctx->drawables[1]) {
1082 ctx->drawables[1] = new;
1083 ctx->refresh_drawables = TRUE;
1088 /* Given the current context, make sure its drawable is sync'd */
1089 static inline void sync_context(struct wgl_context *context)
1091 if (context->refresh_drawables) {
1092 if (glxRequireVersion(3))
1093 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1094 context->drawables[1], context->ctx);
1095 else
1096 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1097 context->refresh_drawables = FALSE;
1101 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1103 struct gl_drawable *gl;
1105 EnterCriticalSection( &context_section );
1106 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1107 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1108 LeaveCriticalSection( &context_section );
1109 return NULL;
1112 static void release_gl_drawable( struct gl_drawable *gl )
1114 if (gl) LeaveCriticalSection( &context_section );
1117 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1119 GLXContext ctx;
1121 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1122 BOOL indirect = !(context->fmt->dwFlags & PFD_DRAW_TO_BITMAP);
1124 if(context->gl3_context)
1126 if(context->numAttribs)
1127 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1128 else
1129 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1131 else if(context->vis)
1132 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1133 else /* Create a GLX Context for a pbuffer */
1134 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1136 return ctx;
1140 /***********************************************************************
1141 * free_gl_drawable
1143 static void free_gl_drawable( struct gl_drawable *gl )
1145 switch (gl->type)
1147 case DC_GL_WINDOW:
1148 case DC_GL_CHILD_WIN:
1149 XDestroyWindow( gdi_display, gl->drawable );
1150 XFreeColormap( gdi_display, gl->colormap );
1151 break;
1152 case DC_GL_PIXMAP_WIN:
1153 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1154 XFreePixmap( gdi_display, gl->pixmap );
1155 break;
1156 default:
1157 break;
1159 if (gl->visual) XFree( gl->visual );
1160 HeapFree( GetProcessHeap(), 0, gl );
1164 /***********************************************************************
1165 * create_gl_drawable
1167 static BOOL create_gl_drawable( HWND hwnd, HWND parent, struct gl_drawable *gl )
1169 XSetWindowAttributes attrib;
1171 gl->drawable = 0;
1173 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1175 Window parent = X11DRV_get_whole_window( hwnd );
1177 gl->type = DC_GL_WINDOW;
1178 gl->colormap = XCreateColormap( gdi_display, root_window, gl->visual->visual,
1179 (gl->visual->class == PseudoColor ||
1180 gl->visual->class == GrayScale ||
1181 gl->visual->class == DirectColor) ? AllocAll : AllocNone );
1182 attrib.colormap = gl->colormap;
1183 attrib.bit_gravity = NorthWestGravity;
1184 attrib.win_gravity = NorthWestGravity;
1185 attrib.backing_store = NotUseful;
1186 /* put the initial rect outside of the window, it will be moved into place by SetWindowPos */
1187 OffsetRect( &gl->rect, gl->rect.right, gl->rect.bottom );
1188 if (parent)
1189 gl->drawable = XCreateWindow( gdi_display, parent, gl->rect.left, gl->rect.top,
1190 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1191 0, default_visual.depth, InputOutput, gl->visual->visual,
1192 CWBitGravity | CWWinGravity | CWBackingStore | CWColormap,
1193 &attrib );
1194 if (gl->drawable)
1195 XMapWindow( gdi_display, gl->drawable );
1196 else
1197 XFreeColormap( gdi_display, gl->colormap );
1199 #ifdef SONAME_LIBXCOMPOSITE
1200 else if(usexcomposite)
1202 static Window dummy_parent;
1204 attrib.override_redirect = True;
1205 if (!dummy_parent)
1207 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1208 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1209 XMapWindow( gdi_display, dummy_parent );
1211 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1212 (gl->visual->class == PseudoColor ||
1213 gl->visual->class == GrayScale ||
1214 gl->visual->class == DirectColor) ?
1215 AllocAll : AllocNone);
1216 attrib.colormap = gl->colormap;
1217 XInstallColormap(gdi_display, attrib.colormap);
1219 gl->type = DC_GL_CHILD_WIN;
1220 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1221 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1222 0, gl->visual->depth, InputOutput, gl->visual->visual,
1223 CWColormap | CWOverrideRedirect, &attrib );
1224 if (gl->drawable)
1226 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1227 XMapWindow(gdi_display, gl->drawable);
1229 else XFreeColormap( gdi_display, gl->colormap );
1231 #endif
1232 else
1234 WARN("XComposite is not available, using GLXPixmap hack\n");
1236 gl->type = DC_GL_PIXMAP_WIN;
1237 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1238 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1239 gl->visual->depth );
1240 if (gl->pixmap)
1242 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1243 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1247 return gl->drawable != 0;
1251 /***********************************************************************
1252 * set_win_format
1254 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1256 HWND parent = GetAncestor( hwnd, GA_PARENT );
1257 struct gl_drawable *gl, *prev;
1259 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1260 gl->format = format;
1261 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1262 if (!gl->visual)
1264 HeapFree( GetProcessHeap(), 0, gl );
1265 return FALSE;
1268 GetClientRect( hwnd, &gl->rect );
1269 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1270 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1272 if (!create_gl_drawable( hwnd, parent, gl ))
1274 XFree( gl->visual );
1275 HeapFree( GetProcessHeap(), 0, gl );
1276 return FALSE;
1279 TRACE("created GL drawable %lx for win %p format %x\n", gl->drawable, hwnd, format->fmt_id );
1281 XFlush( gdi_display );
1283 EnterCriticalSection( &context_section );
1284 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1285 free_gl_drawable( prev );
1286 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1287 LeaveCriticalSection( &context_section );
1289 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1290 return TRUE;
1293 /***********************************************************************
1294 * sync_gl_drawable
1296 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1298 struct gl_drawable *gl;
1299 Drawable glxp;
1300 Pixmap pix;
1301 int mask = 0;
1302 XWindowChanges changes;
1304 changes.x = client_rect->left - visible_rect->left;
1305 changes.y = client_rect->top - visible_rect->top;
1306 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1307 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1309 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1311 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1312 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1314 TRACE( "setting drawable %lx pos %d,%d,%dx%d\n",
1315 gl->drawable, changes.x, changes.y, changes.width, changes.height );
1317 switch (gl->type)
1319 case DC_GL_WINDOW:
1320 if (changes.x != gl->rect.left) mask |= CWX;
1321 if (changes.y != gl->rect.top) mask |= CWY;
1322 /* fallthrough */
1323 case DC_GL_CHILD_WIN:
1324 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1325 break;
1326 case DC_GL_PIXMAP_WIN:
1327 if (!mask) break;
1328 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1329 if (!pix) goto done;
1330 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1331 if (!glxp)
1333 XFreePixmap(gdi_display, pix);
1334 goto done;
1336 mark_drawable_dirty(gl->drawable, glxp);
1337 XFlush( gdi_display );
1339 XFreePixmap(gdi_display, gl->pixmap);
1340 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1341 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1343 gl->pixmap = pix;
1344 gl->drawable = glxp;
1345 break;
1346 default:
1347 break;
1349 SetRect( &gl->rect, changes.x, changes.y, changes.x + changes.width, changes.y + changes.height );
1350 done:
1351 release_gl_drawable( gl );
1355 /***********************************************************************
1356 * set_gl_drawable_parent
1358 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1360 struct gl_drawable *gl;
1361 Drawable old_drawable;
1363 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1365 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1367 old_drawable = gl->drawable;
1368 switch (gl->type)
1370 case DC_GL_WINDOW:
1371 XDestroyWindow( gdi_display, gl->drawable );
1372 XFreeColormap( gdi_display, gl->colormap );
1373 break;
1374 case DC_GL_CHILD_WIN:
1375 if (parent != GetDesktopWindow()) goto done;
1376 XDestroyWindow( gdi_display, gl->drawable );
1377 XFreeColormap( gdi_display, gl->colormap );
1378 break;
1379 case DC_GL_PIXMAP_WIN:
1380 if (parent != GetDesktopWindow()) goto done;
1381 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1382 XFreePixmap( gdi_display, gl->pixmap );
1383 break;
1384 default:
1385 goto done;
1388 if (!create_gl_drawable( hwnd, parent, gl ))
1390 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1391 release_gl_drawable( gl );
1392 XFree( gl->visual );
1393 HeapFree( GetProcessHeap(), 0, gl );
1394 __wine_set_pixel_format( hwnd, 0 );
1395 return;
1397 mark_drawable_dirty( old_drawable, gl->drawable );
1399 done:
1400 release_gl_drawable( gl );
1405 /***********************************************************************
1406 * destroy_gl_drawable
1408 void destroy_gl_drawable( HWND hwnd )
1410 struct gl_drawable *gl;
1412 EnterCriticalSection( &context_section );
1413 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1415 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1416 free_gl_drawable( gl );
1418 LeaveCriticalSection( &context_section );
1423 * glxdrv_DescribePixelFormat
1425 * Get the pixel-format descriptor associated to the given id
1427 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1428 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1430 /*XVisualInfo *vis;*/
1431 int value;
1432 int rb,gb,bb,ab;
1433 const struct wgl_pixel_format *fmt;
1435 if (!has_opengl()) return 0;
1437 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1439 if (!ppfd) return nb_onscreen_formats;
1441 /* 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 */
1442 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1443 if (!fmt) {
1444 WARN("unexpected format %d\n", iPixelFormat);
1445 return 0;
1448 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1449 ERR("Wrong structure size !\n");
1450 /* Should set error */
1451 return 0;
1454 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1455 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1456 ppfd->nVersion = 1;
1458 /* These flags are always the same... */
1459 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1460 /* Now the flags extracted from the Visual */
1462 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1463 if(value & GLX_WINDOW_BIT)
1464 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1466 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1467 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1468 * Radeon 9000 indicated that all bitmap formats have PFD_SUPPORT_GDI. Except for 2 formats on the Radeon 9000 none of the hw accelerated formats
1469 * offered the GDI bit either. */
1470 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1472 /* PFD_GENERIC_FORMAT - gdi software rendering
1473 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1474 * none set - full hardware accelerated by a ICD
1476 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1477 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1479 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1480 if (value) {
1481 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1482 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1484 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1486 /* Pixel type */
1487 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1488 if (value & GLX_RGBA_BIT)
1489 ppfd->iPixelType = PFD_TYPE_RGBA;
1490 else
1491 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1493 /* Color bits */
1494 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1495 ppfd->cColorBits = value;
1497 /* Red, green, blue and alpha bits / shifts */
1498 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1499 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1500 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1501 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1502 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1504 ppfd->cRedBits = rb;
1505 ppfd->cRedShift = gb + bb + ab;
1506 ppfd->cBlueBits = bb;
1507 ppfd->cBlueShift = ab;
1508 ppfd->cGreenBits = gb;
1509 ppfd->cGreenShift = bb + ab;
1510 ppfd->cAlphaBits = ab;
1511 ppfd->cAlphaShift = 0;
1512 } else {
1513 ppfd->cRedBits = 0;
1514 ppfd->cRedShift = 0;
1515 ppfd->cBlueBits = 0;
1516 ppfd->cBlueShift = 0;
1517 ppfd->cGreenBits = 0;
1518 ppfd->cGreenShift = 0;
1519 ppfd->cAlphaBits = 0;
1520 ppfd->cAlphaShift = 0;
1523 /* Accum RGBA bits */
1524 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1525 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1526 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1527 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1529 ppfd->cAccumBits = rb+gb+bb+ab;
1530 ppfd->cAccumRedBits = rb;
1531 ppfd->cAccumGreenBits = gb;
1532 ppfd->cAccumBlueBits = bb;
1533 ppfd->cAccumAlphaBits = ab;
1535 /* Aux bits */
1536 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1537 ppfd->cAuxBuffers = value;
1539 /* Depth bits */
1540 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1541 ppfd->cDepthBits = value;
1543 /* stencil bits */
1544 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1545 ppfd->cStencilBits = value;
1547 ppfd->iLayerType = PFD_MAIN_PLANE;
1549 if (TRACE_ON(wgl)) {
1550 dump_PIXELFORMATDESCRIPTOR(ppfd);
1553 return nb_onscreen_formats;
1556 /***********************************************************************
1557 * glxdrv_wglGetPixelFormat
1559 static int glxdrv_wglGetPixelFormat( HDC hdc )
1561 struct gl_drawable *gl;
1562 int ret = 0;
1564 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1566 ret = pixel_format_index( gl->format );
1567 /* Offscreen formats can't be used with traditional WGL calls.
1568 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1569 if (!is_onscreen_pixel_format( ret )) ret = 1;
1570 release_gl_drawable( gl );
1572 TRACE( "%p -> %d\n", hdc, ret );
1573 return ret;
1576 /***********************************************************************
1577 * glxdrv_wglSetPixelFormat
1579 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1581 const struct wgl_pixel_format *fmt;
1582 int value;
1583 struct gl_drawable *gl;
1584 HWND hwnd = WindowFromDC( hdc );
1586 TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
1588 if (!hwnd || hwnd == GetDesktopWindow())
1590 WARN( "not a proper window DC %p/%p\n", hdc, hwnd );
1591 return FALSE;
1594 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1595 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1596 if(!fmt) {
1597 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1598 return FALSE;
1601 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1602 if (!(value & GLX_WINDOW_BIT))
1604 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1605 return FALSE;
1608 if ((gl = get_gl_drawable( hwnd, hdc )))
1610 int prev = pixel_format_index( gl->format );
1611 release_gl_drawable( gl );
1612 return prev == iPixelFormat; /* cannot change it if already set */
1615 if (TRACE_ON(wgl)) {
1616 int gl_test = 0;
1618 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1619 if (gl_test) {
1620 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1621 } else {
1622 TRACE(" FBConfig have :\n");
1623 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1624 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1625 TRACE(" - VISUAL_ID 0x%x\n", value);
1626 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1627 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1631 return set_win_format( hwnd, fmt );
1634 /***********************************************************************
1635 * glxdrv_wglCopyContext
1637 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1639 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1641 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1643 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1644 return TRUE;
1647 /***********************************************************************
1648 * glxdrv_wglCreateContext
1650 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1652 struct wgl_context *ret = NULL;
1653 struct gl_drawable *gl;
1655 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1657 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1658 return NULL;
1661 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1663 ret->hdc = hdc;
1664 ret->fmt = gl->format;
1665 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1666 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1667 list_add_head( &context_list, &ret->entry );
1669 release_gl_drawable( gl );
1670 TRACE( "%p -> %p\n", hdc, ret );
1671 return ret;
1674 /***********************************************************************
1675 * glxdrv_wglDeleteContext
1677 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1679 TRACE("(%p)\n", ctx);
1681 EnterCriticalSection( &context_section );
1682 list_remove( &ctx->entry );
1683 LeaveCriticalSection( &context_section );
1685 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1686 if (ctx->vis) XFree( ctx->vis );
1687 HeapFree( GetProcessHeap(), 0, ctx );
1690 /***********************************************************************
1691 * glxdrv_wglGetProcAddress
1693 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1695 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1696 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1699 /***********************************************************************
1700 * glxdrv_wglMakeCurrent
1702 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1704 BOOL ret = FALSE;
1705 struct gl_drawable *gl;
1707 TRACE("(%p,%p)\n", hdc, ctx);
1709 if (!ctx)
1711 pglXMakeCurrent(gdi_display, None, NULL);
1712 NtCurrentTeb()->glContext = NULL;
1713 return TRUE;
1716 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1718 if (ctx->fmt != gl->format)
1720 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1721 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1722 goto done;
1725 if (TRACE_ON(wgl)) {
1726 describeContext(ctx);
1727 TRACE("hdc %p drawable %lx fmt %p ctx %p\n", hdc, gl->drawable, gl->format, ctx->ctx );
1730 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1731 if (ret)
1733 NtCurrentTeb()->glContext = ctx;
1734 ctx->has_been_current = TRUE;
1735 ctx->hdc = hdc;
1736 ctx->drawables[0] = gl->drawable;
1737 ctx->drawables[1] = gl->drawable;
1738 ctx->refresh_drawables = FALSE;
1739 goto done;
1742 SetLastError( ERROR_INVALID_HANDLE );
1744 done:
1745 release_gl_drawable( gl );
1746 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1747 return ret;
1750 /***********************************************************************
1751 * X11DRV_wglMakeContextCurrentARB
1753 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1755 BOOL ret = FALSE;
1756 struct gl_drawable *draw_gl, *read_gl = NULL;
1758 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1760 if (!ctx)
1762 pglXMakeCurrent(gdi_display, None, NULL);
1763 NtCurrentTeb()->glContext = NULL;
1764 return TRUE;
1767 if (!pglXMakeContextCurrent) return FALSE;
1769 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1771 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1772 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1773 read_gl ? read_gl->drawable : 0, ctx->ctx);
1774 if (ret)
1776 ctx->has_been_current = TRUE;
1777 ctx->hdc = draw_hdc;
1778 ctx->drawables[0] = draw_gl->drawable;
1779 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1780 ctx->refresh_drawables = FALSE;
1781 NtCurrentTeb()->glContext = ctx;
1782 goto done;
1785 SetLastError( ERROR_INVALID_HANDLE );
1786 done:
1787 release_gl_drawable( read_gl );
1788 release_gl_drawable( draw_gl );
1789 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1790 return ret;
1793 /***********************************************************************
1794 * glxdrv_wglShareLists
1796 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1798 TRACE("(%p, %p)\n", org, dest);
1800 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1801 * at context creation time but in case of WGL it is done using wglShareLists.
1802 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1803 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1804 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1805 * so there delaying context creation doesn't work.
1807 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1808 * and when a program requests sharing we recreate the destination context if it hasn't been made
1809 * current or when it hasn't shared display lists before.
1812 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1814 ERR("Could not share display lists, one of the contexts has been current already !\n");
1815 return FALSE;
1817 else if(dest->sharing)
1819 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1820 return FALSE;
1822 else
1824 describeContext(org);
1825 describeContext(dest);
1827 /* Re-create the GLX context and share display lists */
1828 pglXDestroyContext(gdi_display, dest->ctx);
1829 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1830 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1832 org->sharing = TRUE;
1833 dest->sharing = TRUE;
1834 return TRUE;
1836 return FALSE;
1839 static void wglFinish(void)
1841 struct x11drv_escape_flush_gl_drawable escape;
1842 struct gl_drawable *gl;
1843 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1845 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1846 escape.gl_drawable = 0;
1848 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1850 switch (gl->type)
1852 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1853 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1854 default: break;
1856 sync_context(ctx);
1857 release_gl_drawable( gl );
1860 pglFinish();
1861 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1864 static void wglFlush(void)
1866 struct x11drv_escape_flush_gl_drawable escape;
1867 struct gl_drawable *gl;
1868 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1870 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1871 escape.gl_drawable = 0;
1873 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1875 switch (gl->type)
1877 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1878 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1879 default: break;
1881 sync_context(ctx);
1882 release_gl_drawable( gl );
1885 pglFlush();
1886 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1889 /***********************************************************************
1890 * X11DRV_wglCreateContextAttribsARB
1892 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
1893 const int* attribList )
1895 struct wgl_context *ret = NULL;
1896 struct gl_drawable *gl;
1898 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
1900 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1902 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1903 return NULL;
1906 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1908 ret->hdc = hdc;
1909 ret->fmt = gl->format;
1910 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
1911 ret->gl3_context = TRUE;
1912 ret->numAttribs = 0;
1913 if (attribList)
1915 int *pContextAttribList = &ret->attribList[0];
1916 /* attribList consists of pairs {token, value] terminated with 0 */
1917 while(attribList[0] != 0)
1919 TRACE("%#x %#x\n", attribList[0], attribList[1]);
1920 switch(attribList[0])
1922 case WGL_CONTEXT_MAJOR_VERSION_ARB:
1923 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
1924 pContextAttribList[1] = attribList[1];
1925 break;
1926 case WGL_CONTEXT_MINOR_VERSION_ARB:
1927 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
1928 pContextAttribList[1] = attribList[1];
1929 break;
1930 case WGL_CONTEXT_LAYER_PLANE_ARB:
1931 break;
1932 case WGL_CONTEXT_FLAGS_ARB:
1933 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
1934 pContextAttribList[1] = attribList[1];
1935 break;
1936 case WGL_CONTEXT_PROFILE_MASK_ARB:
1937 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
1938 pContextAttribList[1] = attribList[1];
1939 break;
1940 default:
1941 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
1943 ret->numAttribs++;
1944 attribList += 2;
1945 pContextAttribList += 2;
1949 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1950 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1951 XSync(gdi_display, False);
1952 if (X11DRV_check_error() || !ret->ctx)
1954 /* In the future we should convert the GLX error to a win32 one here if needed */
1955 ERR("Context creation failed\n");
1956 HeapFree( GetProcessHeap(), 0, ret );
1957 ret = NULL;
1959 else list_add_head( &context_list, &ret->entry );
1962 release_gl_drawable( gl );
1963 TRACE( "%p -> %p\n", hdc, ret );
1964 return ret;
1968 * X11DRV_wglGetExtensionsStringARB
1970 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
1972 static const GLubyte *X11DRV_wglGetExtensionsStringARB(HDC hdc)
1974 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
1975 return (const GLubyte *)WineGLInfo.wglExtensions;
1979 * X11DRV_wglCreatePbufferARB
1981 * WGL_ARB_pbuffer: wglCreatePbufferARB
1983 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
1984 const int *piAttribList )
1986 struct wgl_pbuffer* object = NULL;
1987 const struct wgl_pixel_format *fmt = NULL;
1988 int attribs[256];
1989 int nAttribs = 0;
1991 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
1993 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
1994 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
1995 if(!fmt) {
1996 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
1997 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1998 return NULL;
2001 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2002 if (NULL == object) {
2003 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2004 return NULL;
2006 object->width = iWidth;
2007 object->height = iHeight;
2008 object->fmt = fmt;
2010 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2011 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2012 while (piAttribList && 0 != *piAttribList) {
2013 int attr_v;
2014 switch (*piAttribList) {
2015 case WGL_PBUFFER_LARGEST_ARB: {
2016 ++piAttribList;
2017 attr_v = *piAttribList;
2018 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2019 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2020 break;
2023 case WGL_TEXTURE_FORMAT_ARB: {
2024 ++piAttribList;
2025 attr_v = *piAttribList;
2026 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2027 if (WGL_NO_TEXTURE_ARB == attr_v) {
2028 object->use_render_texture = 0;
2029 } else {
2030 if (!use_render_texture_emulation) {
2031 SetLastError(ERROR_INVALID_DATA);
2032 goto create_failed;
2034 switch (attr_v) {
2035 case WGL_TEXTURE_RGB_ARB:
2036 object->use_render_texture = GL_RGB;
2037 object->texture_bpp = 3;
2038 object->texture_format = GL_RGB;
2039 object->texture_type = GL_UNSIGNED_BYTE;
2040 break;
2041 case WGL_TEXTURE_RGBA_ARB:
2042 object->use_render_texture = GL_RGBA;
2043 object->texture_bpp = 4;
2044 object->texture_format = GL_RGBA;
2045 object->texture_type = GL_UNSIGNED_BYTE;
2046 break;
2048 /* WGL_FLOAT_COMPONENTS_NV */
2049 case WGL_TEXTURE_FLOAT_R_NV:
2050 object->use_render_texture = GL_FLOAT_R_NV;
2051 object->texture_bpp = 4;
2052 object->texture_format = GL_RED;
2053 object->texture_type = GL_FLOAT;
2054 break;
2055 case WGL_TEXTURE_FLOAT_RG_NV:
2056 object->use_render_texture = GL_FLOAT_RG_NV;
2057 object->texture_bpp = 8;
2058 object->texture_format = GL_LUMINANCE_ALPHA;
2059 object->texture_type = GL_FLOAT;
2060 break;
2061 case WGL_TEXTURE_FLOAT_RGB_NV:
2062 object->use_render_texture = GL_FLOAT_RGB_NV;
2063 object->texture_bpp = 12;
2064 object->texture_format = GL_RGB;
2065 object->texture_type = GL_FLOAT;
2066 break;
2067 case WGL_TEXTURE_FLOAT_RGBA_NV:
2068 object->use_render_texture = GL_FLOAT_RGBA_NV;
2069 object->texture_bpp = 16;
2070 object->texture_format = GL_RGBA;
2071 object->texture_type = GL_FLOAT;
2072 break;
2073 default:
2074 ERR("Unknown texture format: %x\n", attr_v);
2075 SetLastError(ERROR_INVALID_DATA);
2076 goto create_failed;
2079 break;
2082 case WGL_TEXTURE_TARGET_ARB: {
2083 ++piAttribList;
2084 attr_v = *piAttribList;
2085 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2086 if (WGL_NO_TEXTURE_ARB == attr_v) {
2087 object->texture_target = 0;
2088 } else {
2089 if (!use_render_texture_emulation) {
2090 SetLastError(ERROR_INVALID_DATA);
2091 goto create_failed;
2093 switch (attr_v) {
2094 case WGL_TEXTURE_CUBE_MAP_ARB: {
2095 if (iWidth != iHeight) {
2096 SetLastError(ERROR_INVALID_DATA);
2097 goto create_failed;
2099 object->texture_target = GL_TEXTURE_CUBE_MAP;
2100 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2101 break;
2103 case WGL_TEXTURE_1D_ARB: {
2104 if (1 != iHeight) {
2105 SetLastError(ERROR_INVALID_DATA);
2106 goto create_failed;
2108 object->texture_target = GL_TEXTURE_1D;
2109 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2110 break;
2112 case WGL_TEXTURE_2D_ARB: {
2113 object->texture_target = GL_TEXTURE_2D;
2114 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2115 break;
2117 case WGL_TEXTURE_RECTANGLE_NV: {
2118 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2119 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2120 break;
2122 default:
2123 ERR("Unknown texture target: %x\n", attr_v);
2124 SetLastError(ERROR_INVALID_DATA);
2125 goto create_failed;
2128 break;
2131 case WGL_MIPMAP_TEXTURE_ARB: {
2132 ++piAttribList;
2133 attr_v = *piAttribList;
2134 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2135 if (!use_render_texture_emulation) {
2136 SetLastError(ERROR_INVALID_DATA);
2137 goto create_failed;
2139 break;
2142 ++piAttribList;
2145 PUSH1(attribs, None);
2146 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2147 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2148 if (!object->drawable) {
2149 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2150 goto create_failed; /* unexpected error */
2152 TRACE("->(%p)\n", object);
2153 return object;
2155 create_failed:
2156 HeapFree(GetProcessHeap(), 0, object);
2157 TRACE("->(FAILED)\n");
2158 return NULL;
2162 * X11DRV_wglDestroyPbufferARB
2164 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2166 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2168 TRACE("(%p)\n", object);
2170 pglXDestroyPbuffer(gdi_display, object->drawable);
2171 HeapFree(GetProcessHeap(), 0, object);
2172 return GL_TRUE;
2176 * X11DRV_wglGetPbufferDCARB
2178 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2180 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2182 struct x11drv_escape_set_drawable escape;
2183 struct gl_drawable *gl, *prev;
2184 HDC hdc;
2186 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2187 if (!hdc) return 0;
2189 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2191 DeleteDC( hdc );
2192 return 0;
2194 gl->type = DC_GL_PBUFFER;
2195 gl->drawable = object->drawable;
2196 gl->format = object->fmt;
2198 EnterCriticalSection( &context_section );
2199 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2200 free_gl_drawable( prev );
2201 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2202 LeaveCriticalSection( &context_section );
2204 escape.code = X11DRV_SET_DRAWABLE;
2205 escape.hwnd = 0;
2206 escape.drawable = object->drawable;
2207 escape.mode = IncludeInferiors;
2208 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2209 escape.fbconfig_id = object->fmt->fmt_id;
2210 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2212 TRACE( "(%p)->(%p)\n", object, hdc );
2213 return hdc;
2217 * X11DRV_wglQueryPbufferARB
2219 * WGL_ARB_pbuffer: wglQueryPbufferARB
2221 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2223 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2225 switch (iAttribute) {
2226 case WGL_PBUFFER_WIDTH_ARB:
2227 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2228 break;
2229 case WGL_PBUFFER_HEIGHT_ARB:
2230 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2231 break;
2233 case WGL_PBUFFER_LOST_ARB:
2234 /* GLX Pbuffers cannot be lost by default. We can support this by
2235 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2236 * to receive pixel buffer clobber events, however that may or may
2237 * not give any benefit */
2238 *piValue = GL_FALSE;
2239 break;
2241 case WGL_TEXTURE_FORMAT_ARB:
2242 if (!object->use_render_texture) {
2243 *piValue = WGL_NO_TEXTURE_ARB;
2244 } else {
2245 if (!use_render_texture_emulation) {
2246 SetLastError(ERROR_INVALID_HANDLE);
2247 return GL_FALSE;
2249 switch(object->use_render_texture) {
2250 case GL_RGB:
2251 *piValue = WGL_TEXTURE_RGB_ARB;
2252 break;
2253 case GL_RGBA:
2254 *piValue = WGL_TEXTURE_RGBA_ARB;
2255 break;
2256 /* WGL_FLOAT_COMPONENTS_NV */
2257 case GL_FLOAT_R_NV:
2258 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2259 break;
2260 case GL_FLOAT_RG_NV:
2261 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2262 break;
2263 case GL_FLOAT_RGB_NV:
2264 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2265 break;
2266 case GL_FLOAT_RGBA_NV:
2267 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2268 break;
2269 default:
2270 ERR("Unknown texture format: %x\n", object->use_render_texture);
2273 break;
2275 case WGL_TEXTURE_TARGET_ARB:
2276 if (!object->texture_target){
2277 *piValue = WGL_NO_TEXTURE_ARB;
2278 } else {
2279 if (!use_render_texture_emulation) {
2280 SetLastError(ERROR_INVALID_DATA);
2281 return GL_FALSE;
2283 switch (object->texture_target) {
2284 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2285 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2286 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2287 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2290 break;
2292 case WGL_MIPMAP_TEXTURE_ARB:
2293 *piValue = GL_FALSE; /** don't support that */
2294 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2295 break;
2297 default:
2298 FIXME("unexpected attribute %x\n", iAttribute);
2299 break;
2302 return GL_TRUE;
2306 * X11DRV_wglReleasePbufferDCARB
2308 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2310 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2312 struct gl_drawable *gl;
2314 TRACE("(%p, %p)\n", object, hdc);
2316 EnterCriticalSection( &context_section );
2318 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2320 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2321 free_gl_drawable( gl );
2323 else hdc = 0;
2325 LeaveCriticalSection( &context_section );
2327 return hdc && DeleteDC(hdc);
2331 * X11DRV_wglSetPbufferAttribARB
2333 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2335 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2337 GLboolean ret = GL_FALSE;
2339 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2341 if (!object->use_render_texture) {
2342 SetLastError(ERROR_INVALID_HANDLE);
2343 return GL_FALSE;
2345 if (1 == use_render_texture_emulation) {
2346 return GL_TRUE;
2348 return ret;
2352 * X11DRV_wglChoosePixelFormatARB
2354 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2356 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2357 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2359 int attribs[256];
2360 int nAttribs = 0;
2361 GLXFBConfig* cfgs = NULL;
2362 int nCfgs = 0;
2363 int it;
2364 int fmt_id;
2365 int start, end;
2366 UINT pfmt_it = 0;
2367 int run;
2368 int i;
2369 DWORD dwFlags = 0;
2371 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2372 if (NULL != pfAttribFList) {
2373 FIXME("unused pfAttribFList\n");
2376 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2377 if (-1 == nAttribs) {
2378 WARN("Cannot convert WGL to GLX attributes\n");
2379 return GL_FALSE;
2381 PUSH1(attribs, None);
2383 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2384 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2385 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2387 for(i=0; piAttribIList[i] != 0; i+=2)
2389 switch(piAttribIList[i])
2391 case WGL_DRAW_TO_BITMAP_ARB:
2392 if(piAttribIList[i+1])
2393 dwFlags |= PFD_DRAW_TO_BITMAP;
2394 break;
2395 case WGL_ACCELERATION_ARB:
2396 switch(piAttribIList[i+1])
2398 case WGL_NO_ACCELERATION_ARB:
2399 dwFlags |= PFD_GENERIC_FORMAT;
2400 break;
2401 case WGL_GENERIC_ACCELERATION_ARB:
2402 dwFlags |= PFD_GENERIC_ACCELERATED;
2403 break;
2404 case WGL_FULL_ACCELERATION_ARB:
2405 /* Nothing to do */
2406 break;
2408 break;
2409 case WGL_SUPPORT_GDI_ARB:
2410 if(piAttribIList[i+1])
2411 dwFlags |= PFD_SUPPORT_GDI;
2412 break;
2416 /* Search for FB configurations matching the requirements in attribs */
2417 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2418 if (NULL == cfgs) {
2419 WARN("Compatible Pixel Format not found\n");
2420 return GL_FALSE;
2423 /* Loop through all matching formats and check if they are suitable.
2424 * Note that this function should at max return nMaxFormats different formats */
2425 for(run=0; run < 2; run++)
2427 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2429 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2431 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2432 continue;
2435 /* During the first run we only want onscreen formats and during the second only offscreen */
2436 start = run == 1 ? nb_onscreen_formats : 0;
2437 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2439 for (i = start; i < end; i++)
2441 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2443 piFormats[pfmt_it++] = i + 1;
2444 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2445 it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2446 break;
2452 *nNumFormats = pfmt_it;
2453 /** free list */
2454 XFree(cfgs);
2455 return GL_TRUE;
2459 * X11DRV_wglGetPixelFormatAttribivARB
2461 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2463 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2464 UINT nAttributes, const int *piAttributes, int *piValues )
2466 UINT i;
2467 const struct wgl_pixel_format *fmt = NULL;
2468 int hTest;
2469 int tmp;
2470 int curGLXAttr = 0;
2472 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2474 if (0 < iLayerPlane) {
2475 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2476 return GL_FALSE;
2479 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2480 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2481 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2482 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2483 if(!fmt) {
2484 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2487 for (i = 0; i < nAttributes; ++i) {
2488 const int curWGLAttr = piAttributes[i];
2489 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2491 switch (curWGLAttr) {
2492 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2493 piValues[i] = nb_pixel_formats;
2494 continue;
2496 case WGL_SUPPORT_OPENGL_ARB:
2497 piValues[i] = GL_TRUE;
2498 continue;
2500 case WGL_ACCELERATION_ARB:
2501 curGLXAttr = GLX_CONFIG_CAVEAT;
2502 if (!fmt) goto pix_error;
2503 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2504 piValues[i] = WGL_NO_ACCELERATION_ARB;
2505 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2506 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2507 else
2508 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2509 continue;
2511 case WGL_TRANSPARENT_ARB:
2512 curGLXAttr = GLX_TRANSPARENT_TYPE;
2513 if (!fmt) goto pix_error;
2514 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2515 if (hTest) goto get_error;
2516 piValues[i] = GL_FALSE;
2517 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2518 continue;
2520 case WGL_PIXEL_TYPE_ARB:
2521 curGLXAttr = GLX_RENDER_TYPE;
2522 if (!fmt) goto pix_error;
2523 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2524 if (hTest) goto get_error;
2525 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2526 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2527 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2528 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2529 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2530 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2531 else {
2532 ERR("unexpected RenderType(%x)\n", tmp);
2533 piValues[i] = WGL_TYPE_RGBA_ARB;
2535 continue;
2537 case WGL_COLOR_BITS_ARB:
2538 curGLXAttr = GLX_BUFFER_SIZE;
2539 break;
2541 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2542 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2543 if (!use_render_texture_emulation) {
2544 piValues[i] = GL_FALSE;
2545 continue;
2547 curGLXAttr = GLX_RENDER_TYPE;
2548 if (!fmt) goto pix_error;
2549 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2550 if (hTest) goto get_error;
2551 if (GLX_COLOR_INDEX_BIT == tmp) {
2552 piValues[i] = GL_FALSE;
2553 continue;
2555 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2556 if (hTest) goto get_error;
2557 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2558 continue;
2560 case WGL_BLUE_BITS_ARB:
2561 curGLXAttr = GLX_BLUE_SIZE;
2562 break;
2563 case WGL_RED_BITS_ARB:
2564 curGLXAttr = GLX_RED_SIZE;
2565 break;
2566 case WGL_GREEN_BITS_ARB:
2567 curGLXAttr = GLX_GREEN_SIZE;
2568 break;
2569 case WGL_ALPHA_BITS_ARB:
2570 curGLXAttr = GLX_ALPHA_SIZE;
2571 break;
2572 case WGL_DEPTH_BITS_ARB:
2573 curGLXAttr = GLX_DEPTH_SIZE;
2574 break;
2575 case WGL_STENCIL_BITS_ARB:
2576 curGLXAttr = GLX_STENCIL_SIZE;
2577 break;
2578 case WGL_DOUBLE_BUFFER_ARB:
2579 curGLXAttr = GLX_DOUBLEBUFFER;
2580 break;
2581 case WGL_STEREO_ARB:
2582 curGLXAttr = GLX_STEREO;
2583 break;
2584 case WGL_AUX_BUFFERS_ARB:
2585 curGLXAttr = GLX_AUX_BUFFERS;
2586 break;
2588 case WGL_SUPPORT_GDI_ARB:
2589 if (!fmt) goto pix_error;
2590 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2591 continue;
2593 case WGL_DRAW_TO_BITMAP_ARB:
2594 if (!fmt) goto pix_error;
2595 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2596 continue;
2598 case WGL_DRAW_TO_WINDOW_ARB:
2599 case WGL_DRAW_TO_PBUFFER_ARB:
2600 if (!fmt) goto pix_error;
2601 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2602 if (hTest) goto get_error;
2603 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2604 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2605 piValues[i] = GL_TRUE;
2606 else
2607 piValues[i] = GL_FALSE;
2608 continue;
2610 case WGL_SWAP_METHOD_ARB:
2611 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2612 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2613 * point only ATI offers this.
2615 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2616 break;
2618 case WGL_PBUFFER_LARGEST_ARB:
2619 curGLXAttr = GLX_LARGEST_PBUFFER;
2620 break;
2622 case WGL_SAMPLE_BUFFERS_ARB:
2623 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2624 break;
2626 case WGL_SAMPLES_ARB:
2627 curGLXAttr = GLX_SAMPLES_ARB;
2628 break;
2630 case WGL_FLOAT_COMPONENTS_NV:
2631 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2632 break;
2634 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2635 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2636 break;
2638 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2639 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2640 break;
2642 case WGL_ACCUM_RED_BITS_ARB:
2643 curGLXAttr = GLX_ACCUM_RED_SIZE;
2644 break;
2645 case WGL_ACCUM_GREEN_BITS_ARB:
2646 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2647 break;
2648 case WGL_ACCUM_BLUE_BITS_ARB:
2649 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2650 break;
2651 case WGL_ACCUM_ALPHA_BITS_ARB:
2652 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2653 break;
2654 case WGL_ACCUM_BITS_ARB:
2655 if (!fmt) goto pix_error;
2656 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2657 if (hTest) goto get_error;
2658 piValues[i] = tmp;
2659 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2660 if (hTest) goto get_error;
2661 piValues[i] += tmp;
2662 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2663 if (hTest) goto get_error;
2664 piValues[i] += tmp;
2665 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2666 if (hTest) goto get_error;
2667 piValues[i] += tmp;
2668 continue;
2670 default:
2671 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2674 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2675 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2676 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2678 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2679 * and check which options can work using iPixelFormat=0 and which not.
2680 * A problem would be that this function is an extension. This would
2681 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2683 if (0 != curGLXAttr && iPixelFormat != 0) {
2684 if (!fmt) goto pix_error;
2685 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2686 if (hTest) goto get_error;
2687 curGLXAttr = 0;
2688 } else {
2689 piValues[i] = GL_FALSE;
2692 return GL_TRUE;
2694 get_error:
2695 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2696 return GL_FALSE;
2698 pix_error:
2699 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2700 return GL_FALSE;
2704 * X11DRV_wglGetPixelFormatAttribfvARB
2706 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2708 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2709 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2711 int *attr;
2712 int ret;
2713 UINT i;
2715 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2717 /* Allocate a temporary array to store integer values */
2718 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2719 if (!attr) {
2720 ERR("couldn't allocate %d array\n", nAttributes);
2721 return GL_FALSE;
2724 /* Piggy-back on wglGetPixelFormatAttribivARB */
2725 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2726 if (ret) {
2727 /* Convert integer values to float. Should also check for attributes
2728 that can give decimal values here */
2729 for (i=0; i<nAttributes;i++) {
2730 pfValues[i] = attr[i];
2734 HeapFree(GetProcessHeap(), 0, attr);
2735 return ret;
2739 * X11DRV_wglBindTexImageARB
2741 * WGL_ARB_render_texture: wglBindTexImageARB
2743 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2745 GLboolean ret = GL_FALSE;
2747 TRACE("(%p, %d)\n", object, iBuffer);
2749 if (!object->use_render_texture) {
2750 SetLastError(ERROR_INVALID_HANDLE);
2751 return GL_FALSE;
2754 if (1 == use_render_texture_emulation) {
2755 static int init = 0;
2756 int prev_binded_texture = 0;
2757 GLXContext prev_context;
2758 Drawable prev_drawable;
2759 GLXContext tmp_context;
2761 prev_context = pglXGetCurrentContext();
2762 prev_drawable = pglXGetCurrentDrawable();
2764 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2765 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2766 isn't ideal performance wise but I wasn't able to get other ways working.
2768 if(!init) {
2769 init = 1; /* Only show the FIXME once for performance reasons */
2770 FIXME("partial stub!\n");
2773 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2774 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2776 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2778 /* Switch to our pbuffer */
2779 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2781 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2782 * After that upload the pbuffer texture data. */
2783 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2784 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2786 /* Switch back to the original drawable and upload the pbuffer-texture */
2787 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2788 pglXDestroyContext(gdi_display, tmp_context);
2789 return GL_TRUE;
2792 return ret;
2796 * X11DRV_wglReleaseTexImageARB
2798 * WGL_ARB_render_texture: wglReleaseTexImageARB
2800 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2802 GLboolean ret = GL_FALSE;
2804 TRACE("(%p, %d)\n", object, iBuffer);
2806 if (!object->use_render_texture) {
2807 SetLastError(ERROR_INVALID_HANDLE);
2808 return GL_FALSE;
2810 if (1 == use_render_texture_emulation) {
2811 return GL_TRUE;
2813 return ret;
2817 * X11DRV_wglGetExtensionsStringEXT
2819 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2821 static const GLubyte *X11DRV_wglGetExtensionsStringEXT(void)
2823 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2824 return (const GLubyte *)WineGLInfo.wglExtensions;
2828 * X11DRV_wglGetSwapIntervalEXT
2830 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2832 static int X11DRV_wglGetSwapIntervalEXT(void)
2834 /* GLX_SGI_swap_control doesn't have any provisions for getting the swap
2835 * interval, so the swap interval has to be tracked. */
2836 TRACE("()\n");
2837 return swap_interval;
2841 * X11DRV_wglSwapIntervalEXT
2843 * WGL_EXT_swap_control: wglSwapIntervalEXT
2845 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
2847 BOOL ret = TRUE;
2849 TRACE("(%d)\n", interval);
2851 if (interval < 0)
2853 SetLastError(ERROR_INVALID_DATA);
2854 return FALSE;
2856 else if (!has_swap_control && interval == 0)
2858 /* wglSwapIntervalEXT considers an interval value of zero to mean that
2859 * vsync should be disabled, but glXSwapIntervalSGI considers such a
2860 * value to be an error. Just silently ignore the request for now. */
2861 WARN("Request to disable vertical sync is not handled\n");
2862 swap_interval = 0;
2864 else
2866 if (pglXSwapIntervalSGI)
2867 ret = !pglXSwapIntervalSGI(interval);
2868 else
2869 WARN("GLX_SGI_swap_control extension is not available\n");
2871 if (ret)
2872 swap_interval = interval;
2873 else
2874 SetLastError(ERROR_DC_NOT_FOUND);
2877 return ret;
2881 * X11DRV_wglSetPixelFormatWINE
2883 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
2884 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
2886 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
2888 const struct wgl_pixel_format *fmt;
2889 int value;
2890 HWND hwnd;
2892 TRACE("(%p,%d)\n", hdc, format);
2894 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
2895 if (!fmt)
2897 ERR( "Invalid format %d\n", format );
2898 return FALSE;
2901 hwnd = WindowFromDC( hdc );
2902 if (!hwnd || hwnd == GetDesktopWindow())
2904 ERR( "not a valid window DC %p\n", hdc );
2905 return FALSE;
2908 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
2909 if (!(value & GLX_WINDOW_BIT))
2911 WARN( "Pixel format %d is not compatible for window rendering\n", format );
2912 return FALSE;
2915 return set_win_format( hwnd, fmt );
2919 * glxRequireVersion (internal)
2921 * Check if the supported GLX version matches requiredVersion.
2923 static BOOL glxRequireVersion(int requiredVersion)
2925 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2926 if(requiredVersion <= WineGLInfo.glxVersion[1])
2927 return TRUE;
2929 return FALSE;
2932 static void register_extension(const char *ext)
2934 if (WineGLInfo.wglExtensions[0])
2935 strcat(WineGLInfo.wglExtensions, " ");
2936 strcat(WineGLInfo.wglExtensions, ext);
2938 TRACE("'%s'\n", ext);
2942 * X11DRV_WineGL_LoadExtensions
2944 static void X11DRV_WineGL_LoadExtensions(void)
2946 WineGLInfo.wglExtensions[0] = 0;
2948 /* ARB Extensions */
2950 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
2952 register_extension( "WGL_ARB_create_context" );
2953 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
2955 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
2956 register_extension("WGL_ARB_create_context_profile");
2959 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
2961 register_extension("WGL_ARB_pixel_format_float");
2962 register_extension("WGL_ATI_pixel_format_float");
2965 register_extension( "WGL_ARB_extensions_string" );
2966 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
2968 if (glxRequireVersion(3))
2970 register_extension( "WGL_ARB_make_current_read" );
2971 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
2972 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
2975 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
2977 /* In general pbuffer functionality requires support in the X-server. The functionality is
2978 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
2980 if ( glxRequireVersion(3) && has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_pbuffer") )
2982 register_extension( "WGL_ARB_pbuffer" );
2983 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
2984 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
2985 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
2986 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
2987 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
2988 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
2991 register_extension( "WGL_ARB_pixel_format" );
2992 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
2993 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
2994 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
2996 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
2997 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
2998 (glxRequireVersion(3) && has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_pbuffer") && use_render_texture_emulation))
3000 register_extension( "WGL_ARB_render_texture" );
3001 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3002 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3004 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3005 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3006 register_extension("WGL_NV_float_buffer");
3008 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3009 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3010 register_extension("WGL_NV_render_texture_rectangle");
3013 /* EXT Extensions */
3015 register_extension( "WGL_EXT_extensions_string" );
3016 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3018 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3019 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3020 register_extension( "WGL_EXT_swap_control" );
3021 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3022 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3024 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3025 register_extension("WGL_EXT_framebuffer_sRGB");
3027 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3028 register_extension("WGL_EXT_pixel_format_packed_float");
3030 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3031 has_swap_control = TRUE;
3033 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3034 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3036 register_extension( "WGL_NV_vertex_array_range" );
3037 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3038 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3041 /* WINE-specific WGL Extensions */
3043 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3044 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3046 register_extension( "WGL_WINE_pixel_format_passthrough" );
3047 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3052 * glxdrv_SwapBuffers
3054 * Swap the buffers of this DC
3056 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3058 struct x11drv_escape_flush_gl_drawable escape;
3059 struct gl_drawable *gl;
3060 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3062 TRACE("(%p)\n", hdc);
3064 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3065 escape.gl_drawable = 0;
3067 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3069 SetLastError( ERROR_INVALID_HANDLE );
3070 return FALSE;
3073 switch (gl->type)
3075 case DC_GL_PIXMAP_WIN:
3076 if (ctx) sync_context( ctx );
3077 escape.gl_drawable = gl->pixmap;
3078 if (pglXCopySubBufferMESA) {
3079 /* (glX)SwapBuffers has an implicit glFlush effect, however
3080 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3081 * copying */
3082 pglFlush();
3083 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3084 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3085 break;
3087 pglXSwapBuffers(gdi_display, gl->drawable);
3088 break;
3089 case DC_GL_CHILD_WIN:
3090 escape.gl_drawable = gl->drawable;
3091 /* fall through */
3092 default:
3093 pglXSwapBuffers(gdi_display, gl->drawable);
3094 break;
3097 release_gl_drawable( gl );
3099 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3100 return TRUE;
3103 static struct opengl_funcs opengl_funcs =
3106 glxdrv_wglCopyContext, /* p_wglCopyContext */
3107 glxdrv_wglCreateContext, /* p_wglCreateContext */
3108 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3109 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3110 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3111 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3112 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3113 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3114 glxdrv_wglShareLists, /* p_wglShareLists */
3115 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3119 struct opengl_funcs *get_glx_driver( UINT version )
3121 if (version != WINE_WGL_DRIVER_VERSION)
3123 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3124 return NULL;
3126 if (has_opengl()) return &opengl_funcs;
3127 return NULL;
3130 #else /* no OpenGL includes */
3132 struct opengl_funcs *get_glx_driver( UINT version )
3134 return NULL;
3137 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3141 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3145 void destroy_gl_drawable( HWND hwnd )
3149 #endif /* defined(SONAME_LIBGL) */