winex11: Move the FLUSH_GL_DRAWABLE implementation to the main ExtEscape routine.
[wine.git] / dlls / winex11.drv / opengl.c
blob4b6cb1ff01a111272c589a191c097798592b4901
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 glx_physdev
186 struct gdi_physdev dev;
187 X11DRV_PDEVICE *x11dev;
188 enum dc_gl_type type; /* type of GL device context */
189 const struct wgl_pixel_format *format;
190 Drawable drawable;
191 Pixmap pixmap; /* pixmap for a DL_GL_PIXMAP_WIN drawable */
194 struct gl_drawable
196 enum dc_gl_type type; /* type of GL surface */
197 Drawable drawable; /* drawable for rendering to the client area */
198 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
199 Colormap colormap; /* colormap used for the drawable */
200 const struct wgl_pixel_format *format; /* pixel format for the drawable */
201 XVisualInfo *visual; /* information about the GL visual */
202 RECT rect; /* drawable rect, relative to whole window drawable */
205 /* X context to associate a struct gl_drawable to an hwnd */
206 static XContext gl_hwnd_context;
207 /* X context to associate a struct gl_drawable to a pbuffer hdc */
208 static XContext gl_pbuffer_context;
210 static const struct gdi_dc_funcs glxdrv_funcs;
212 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
214 return (struct glx_physdev *)dev;
217 static struct list context_list = LIST_INIT( context_list );
218 static struct WineGLInfo WineGLInfo = { 0 };
219 static struct wgl_pixel_format *pixel_formats;
220 static int nb_pixel_formats, nb_onscreen_formats;
221 static int use_render_texture_emulation = 1;
222 static BOOL has_swap_control;
223 static int swap_interval = 1;
225 static CRITICAL_SECTION context_section;
226 static CRITICAL_SECTION_DEBUG critsect_debug =
228 0, 0, &context_section,
229 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
230 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
232 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
234 static struct opengl_funcs opengl_funcs;
236 #define USE_GL_FUNC(name) #name,
237 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
238 #undef USE_GL_FUNC
240 static void X11DRV_WineGL_LoadExtensions(void);
241 static void init_pixel_formats( Display *display );
242 static BOOL glxRequireVersion(int requiredVersion);
244 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
245 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
246 TRACE(" - dwFlags : ");
247 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
248 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
249 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
250 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
251 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
252 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
253 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
254 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
255 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
256 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
257 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
258 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
259 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
260 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
261 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
262 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
263 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
264 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
265 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
266 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
267 #undef TEST_AND_DUMP
268 TRACE("\n");
270 TRACE(" - iPixelType : ");
271 switch (ppfd->iPixelType) {
272 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
273 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
275 TRACE("\n");
277 TRACE(" - Color : %d\n", ppfd->cColorBits);
278 TRACE(" - Red : %d\n", ppfd->cRedBits);
279 TRACE(" - Green : %d\n", ppfd->cGreenBits);
280 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
281 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
282 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
283 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
284 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
285 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
287 TRACE(" - iLayerType : ");
288 switch (ppfd->iLayerType) {
289 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
290 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
291 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
293 TRACE("\n");
296 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
297 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
299 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
300 /* GLX 1.0 */
301 MAKE_FUNCPTR(glXChooseVisual)
302 MAKE_FUNCPTR(glXCopyContext)
303 MAKE_FUNCPTR(glXCreateContext)
304 MAKE_FUNCPTR(glXCreateGLXPixmap)
305 MAKE_FUNCPTR(glXGetCurrentContext)
306 MAKE_FUNCPTR(glXGetCurrentDrawable)
307 MAKE_FUNCPTR(glXDestroyContext)
308 MAKE_FUNCPTR(glXDestroyGLXPixmap)
309 MAKE_FUNCPTR(glXGetConfig)
310 MAKE_FUNCPTR(glXIsDirect)
311 MAKE_FUNCPTR(glXMakeCurrent)
312 MAKE_FUNCPTR(glXSwapBuffers)
313 MAKE_FUNCPTR(glXQueryExtension)
314 MAKE_FUNCPTR(glXQueryVersion)
316 /* GLX 1.1 */
317 MAKE_FUNCPTR(glXGetClientString)
318 MAKE_FUNCPTR(glXQueryExtensionsString)
319 MAKE_FUNCPTR(glXQueryServerString)
321 /* GLX 1.3 */
322 MAKE_FUNCPTR(glXGetFBConfigs)
323 MAKE_FUNCPTR(glXChooseFBConfig)
324 MAKE_FUNCPTR(glXCreatePbuffer)
325 MAKE_FUNCPTR(glXCreateNewContext)
326 MAKE_FUNCPTR(glXDestroyPbuffer)
327 MAKE_FUNCPTR(glXGetFBConfigAttrib)
328 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
329 MAKE_FUNCPTR(glXMakeContextCurrent)
330 MAKE_FUNCPTR(glXQueryDrawable)
331 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
332 #undef MAKE_FUNCPTR
334 /* GLX Extensions */
335 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
336 static void* (*pglXGetProcAddressARB)(const GLubyte *);
337 static int (*pglXSwapIntervalSGI)(int);
339 /* NV GLX Extension */
340 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
341 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
343 /* MESA GLX Extensions */
344 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
346 /* Standard OpenGL */
347 static void (*pglFinish)(void);
348 static void (*pglFlush)(void);
350 static void wglFinish(void);
351 static void wglFlush(void);
353 /* check if the extension is present in the list */
354 static BOOL has_extension( const char *list, const char *ext )
356 size_t len = strlen( ext );
358 while (list)
360 while (*list == ' ') list++;
361 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
362 list = strchr( list, ' ' );
364 return FALSE;
367 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
369 /* In the future we might want to find the exact X or GLX error to report back to the app */
370 return 1;
373 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
375 int screen = DefaultScreen(gdi_display);
376 Window win = 0, root = 0;
377 const char *gl_renderer;
378 const char* str;
379 XVisualInfo *vis;
380 GLXContext ctx = NULL;
381 XSetWindowAttributes attr;
382 BOOL ret = FALSE;
383 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
385 attr.override_redirect = True;
386 attr.colormap = None;
387 attr.border_pixel = 0;
389 vis = pglXChooseVisual(gdi_display, screen, attribList);
390 if (vis) {
391 #ifdef __i386__
392 WORD old_fs = wine_get_fs();
393 /* Create a GLX Context. Without one we can't query GL information */
394 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
395 if (wine_get_fs() != old_fs)
397 wine_set_fs( old_fs );
398 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
399 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
400 goto done;
402 #else
403 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
404 #endif
406 if (!ctx) goto done;
408 root = RootWindow( gdi_display, vis->screen );
409 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
410 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
411 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
412 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
413 XMapWindow( gdi_display, win );
414 else
415 win = root;
417 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
419 ERR_(winediag)( "Unable to activate OpenGL context, most likely your OpenGL drivers haven't been installed correctly\n" );
420 goto done;
422 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
423 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
424 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
425 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
426 strcpy(WineGLInfo.glExtensions, str);
428 /* Get the common GLX version supported by GLX client and server ( major/minor) */
429 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
431 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
432 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
433 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
435 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
436 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
437 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
439 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
440 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
442 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
443 TRACE("GL renderer : %s.\n", gl_renderer);
444 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
445 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
446 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
447 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
448 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
449 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
451 if(!WineGLInfo.glxDirect)
453 int fd = ConnectionNumber(gdi_display);
454 struct sockaddr_un uaddr;
455 unsigned int uaddrlen = sizeof(struct sockaddr_un);
457 /* In general indirect rendering on a local X11 server indicates a driver problem.
458 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
460 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
461 ERR_(winediag)("Direct rendering is disabled, most likely your OpenGL drivers "
462 "haven't been installed correctly (using GL renderer %s, version %s).\n",
463 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
465 else
467 /* In general you would expect that if direct rendering is returned, that you receive hardware
468 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
469 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
470 * software rendering, it shows direct rendering.
472 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
473 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
474 * it shows 'Mesa X11'.
476 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
477 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your OpenGL "
478 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
479 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
481 ret = TRUE;
483 done:
484 if(vis) XFree(vis);
485 if(ctx) {
486 pglXMakeCurrent(gdi_display, None, NULL);
487 pglXDestroyContext(gdi_display, ctx);
489 if (win != root) XDestroyWindow( gdi_display, win );
490 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
491 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
492 return ret;
495 static BOOL has_opengl(void)
497 static int init_done;
498 static void *opengl_handle;
500 char buffer[200];
501 int error_base, event_base;
502 unsigned int i;
504 if (init_done) return (opengl_handle != NULL);
505 init_done = 1;
507 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
508 and include all dependencies */
509 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
510 if (opengl_handle == NULL)
512 ERR( "Failed to load libGL: %s\n", buffer );
513 ERR( "OpenGL support is disabled.\n");
514 return FALSE;
517 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
519 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
521 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
522 goto failed;
526 /* redirect some standard OpenGL functions */
527 #define REDIRECT(func) \
528 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
529 REDIRECT( glFinish );
530 REDIRECT( glFlush );
531 #undef REDIRECT
533 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
534 if (pglXGetProcAddressARB == NULL) {
535 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
536 goto failed;
539 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
541 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
542 goto failed; \
543 } while(0)
545 /* GLX 1.0 */
546 LOAD_FUNCPTR(glXChooseVisual);
547 LOAD_FUNCPTR(glXCopyContext);
548 LOAD_FUNCPTR(glXCreateContext);
549 LOAD_FUNCPTR(glXCreateGLXPixmap);
550 LOAD_FUNCPTR(glXGetCurrentContext);
551 LOAD_FUNCPTR(glXGetCurrentDrawable);
552 LOAD_FUNCPTR(glXDestroyContext);
553 LOAD_FUNCPTR(glXDestroyGLXPixmap);
554 LOAD_FUNCPTR(glXGetConfig);
555 LOAD_FUNCPTR(glXIsDirect);
556 LOAD_FUNCPTR(glXMakeCurrent);
557 LOAD_FUNCPTR(glXSwapBuffers);
558 LOAD_FUNCPTR(glXQueryExtension);
559 LOAD_FUNCPTR(glXQueryVersion);
561 /* GLX 1.1 */
562 LOAD_FUNCPTR(glXGetClientString);
563 LOAD_FUNCPTR(glXQueryExtensionsString);
564 LOAD_FUNCPTR(glXQueryServerString);
566 /* GLX 1.3 */
567 LOAD_FUNCPTR(glXCreatePbuffer);
568 LOAD_FUNCPTR(glXCreateNewContext);
569 LOAD_FUNCPTR(glXDestroyPbuffer);
570 LOAD_FUNCPTR(glXMakeContextCurrent);
571 LOAD_FUNCPTR(glXGetCurrentReadDrawable);
572 LOAD_FUNCPTR(glXGetFBConfigs);
573 #undef LOAD_FUNCPTR
575 /* It doesn't matter if these fail. They'll only be used if the driver reports
576 the associated extension is available (and if a driver reports the extension
577 is available but fails to provide the functions, it's quite broken) */
578 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
579 /* ARB GLX Extension */
580 LOAD_FUNCPTR(glXCreateContextAttribsARB);
581 /* SGI GLX Extension */
582 LOAD_FUNCPTR(glXSwapIntervalSGI);
583 /* NV GLX Extension */
584 LOAD_FUNCPTR(glXAllocateMemoryNV);
585 LOAD_FUNCPTR(glXFreeMemoryNV);
586 #undef LOAD_FUNCPTR
588 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
590 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
591 TRACE("GLX is up and running error_base = %d\n", error_base);
592 } else {
593 ERR( "GLX extension is missing, disabling OpenGL.\n" );
594 goto failed;
596 gl_hwnd_context = XUniqueContext();
597 gl_pbuffer_context = XUniqueContext();
599 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
600 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
601 * rendering is used.
603 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
604 * depends on the reported GLX client / server version and on the client / server extension list.
605 * Those don't have to be the same.
607 * In general the server GLX information lists the capabilities in case of indirect rendering.
608 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
609 * available and in that case the client GLX informat can be used.
610 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
611 * in the GLX version/extension list. When a program does this it works for certain for both
612 * direct and indirect rendering.
614 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
615 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
616 * extension list which causes this extension not to be listed. (Wine requires this extension).
617 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
618 * pbuffers is around.
620 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
621 * the ATI drivers and from then on use GLX client information for them.
624 if(glxRequireVersion(3)) {
625 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
626 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
627 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
628 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
629 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
630 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
631 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
632 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
634 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
635 * enable this function when the Xserver understand GLX 1.3 or newer
637 pglXQueryDrawable = NULL;
638 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
639 TRACE("Overriding ATI GLX capabilities!\n");
640 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
641 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
642 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
643 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
645 /* Use client GLX information in case of the ATI drivers. We override the
646 * capabilities over here and not somewhere else as ATI might better their
647 * life in the future. In case they release proper drivers this block of
648 * code won't be called. */
649 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
650 } else {
651 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
654 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
655 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
658 X11DRV_WineGL_LoadExtensions();
659 init_pixel_formats( gdi_display );
660 return TRUE;
662 failed:
663 wine_dlclose(opengl_handle, NULL, 0);
664 opengl_handle = NULL;
665 return FALSE;
668 static int describeContext( struct wgl_context *ctx ) {
669 int tmp;
670 int ctx_vis_id;
671 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
672 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
673 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
674 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
675 TRACE(" - VISUAL_ID 0x%x\n", tmp);
676 ctx_vis_id = tmp;
677 return ctx_vis_id;
680 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
681 int nAttribs = 0;
682 unsigned cur = 0;
683 int pop;
684 int drawattrib = 0;
685 int nvfloatattrib = GLX_DONT_CARE;
686 int pixelattrib = GLX_DONT_CARE;
688 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
689 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
690 while (iWGLAttr && 0 != iWGLAttr[cur]) {
691 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
693 switch (iWGLAttr[cur]) {
694 case WGL_AUX_BUFFERS_ARB:
695 pop = iWGLAttr[++cur];
696 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
697 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
698 break;
699 case WGL_COLOR_BITS_ARB:
700 pop = iWGLAttr[++cur];
701 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
702 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
703 break;
704 case WGL_BLUE_BITS_ARB:
705 pop = iWGLAttr[++cur];
706 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
707 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
708 break;
709 case WGL_RED_BITS_ARB:
710 pop = iWGLAttr[++cur];
711 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
712 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
713 break;
714 case WGL_GREEN_BITS_ARB:
715 pop = iWGLAttr[++cur];
716 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
717 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
718 break;
719 case WGL_ALPHA_BITS_ARB:
720 pop = iWGLAttr[++cur];
721 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
722 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
723 break;
724 case WGL_DEPTH_BITS_ARB:
725 pop = iWGLAttr[++cur];
726 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
727 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
728 break;
729 case WGL_STENCIL_BITS_ARB:
730 pop = iWGLAttr[++cur];
731 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
732 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
733 break;
734 case WGL_DOUBLE_BUFFER_ARB:
735 pop = iWGLAttr[++cur];
736 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
737 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
738 break;
739 case WGL_STEREO_ARB:
740 pop = iWGLAttr[++cur];
741 PUSH2(oGLXAttr, GLX_STEREO, pop);
742 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
743 break;
745 case WGL_PIXEL_TYPE_ARB:
746 pop = iWGLAttr[++cur];
747 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
748 switch (pop) {
749 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
750 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
751 /* 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 */
752 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
753 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
754 default:
755 ERR("unexpected PixelType(%x)\n", pop);
756 pop = 0;
758 break;
760 case WGL_SUPPORT_GDI_ARB:
761 /* This flag is set in a pixel format */
762 pop = iWGLAttr[++cur];
763 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
764 break;
766 case WGL_DRAW_TO_BITMAP_ARB:
767 /* This flag is set in a pixel format */
768 pop = iWGLAttr[++cur];
769 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
770 break;
772 case WGL_DRAW_TO_WINDOW_ARB:
773 pop = iWGLAttr[++cur];
774 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
775 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
776 if (pop) {
777 drawattrib |= GLX_WINDOW_BIT;
779 break;
781 case WGL_DRAW_TO_PBUFFER_ARB:
782 pop = iWGLAttr[++cur];
783 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
784 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
785 if (pop) {
786 drawattrib |= GLX_PBUFFER_BIT;
788 break;
790 case WGL_ACCELERATION_ARB:
791 /* This flag is set in a pixel format */
792 pop = iWGLAttr[++cur];
793 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
794 break;
796 case WGL_SUPPORT_OPENGL_ARB:
797 pop = iWGLAttr[++cur];
798 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
799 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
800 break;
802 case WGL_SWAP_METHOD_ARB:
803 pop = iWGLAttr[++cur];
804 /* For now we ignore this and just return SWAP_EXCHANGE */
805 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
806 break;
808 case WGL_PBUFFER_LARGEST_ARB:
809 pop = iWGLAttr[++cur];
810 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
811 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
812 break;
814 case WGL_SAMPLE_BUFFERS_ARB:
815 pop = iWGLAttr[++cur];
816 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
817 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
818 break;
820 case WGL_SAMPLES_ARB:
821 pop = iWGLAttr[++cur];
822 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
823 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
824 break;
826 case WGL_TEXTURE_FORMAT_ARB:
827 case WGL_TEXTURE_TARGET_ARB:
828 case WGL_MIPMAP_TEXTURE_ARB:
829 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
830 pop = iWGLAttr[++cur];
831 if (NULL == pbuf) {
832 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
834 if (!use_render_texture_emulation) {
835 if (WGL_NO_TEXTURE_ARB != pop) {
836 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
837 return -1; /** error: don't support it */
838 } else {
839 drawattrib |= GLX_PBUFFER_BIT;
842 break ;
843 case WGL_FLOAT_COMPONENTS_NV:
844 nvfloatattrib = iWGLAttr[++cur];
845 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
846 break ;
847 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
848 case WGL_BIND_TO_TEXTURE_RGB_ARB:
849 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
850 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
851 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
852 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
853 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
854 pop = iWGLAttr[++cur];
855 /** cannot be converted, see direct handling on
856 * - wglGetPixelFormatAttribivARB
857 * TODO: wglChoosePixelFormat
859 break ;
860 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
861 pop = iWGLAttr[++cur];
862 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
863 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
864 break ;
866 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
867 pop = iWGLAttr[++cur];
868 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
869 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
870 break ;
871 default:
872 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
873 break;
875 ++cur;
878 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
879 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
880 * pixmap and pbuffer formats appear as well. */
881 if (!drawattrib) drawattrib = GLX_DONT_CARE;
882 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
883 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
885 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
886 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
887 * GLX_DONT_CARE unless it is overridden. */
888 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
889 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
891 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
892 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
893 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
894 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
897 return nAttribs;
900 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
902 int render_type=0, render_type_bit;
903 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
904 switch(render_type_bit)
906 case GLX_RGBA_BIT:
907 render_type = GLX_RGBA_TYPE;
908 break;
909 case GLX_COLOR_INDEX_BIT:
910 render_type = GLX_COLOR_INDEX_TYPE;
911 break;
912 case GLX_RGBA_FLOAT_BIT:
913 render_type = GLX_RGBA_FLOAT_TYPE;
914 break;
915 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
916 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
917 break;
918 default:
919 ERR("Unknown render_type: %x\n", render_type_bit);
921 return render_type;
924 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
925 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
927 int dbuf, value;
928 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
929 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
931 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
932 * the GLX_PIXMAP_BIT set. */
933 return !dbuf && (value & GLX_PIXMAP_BIT);
936 static void init_pixel_formats( Display *display )
938 struct wgl_pixel_format *list;
939 int size = 0, onscreen_size = 0;
940 int fmt_id, nCfgs, i, run, bmp_formats;
941 GLXFBConfig* cfgs;
942 XVisualInfo *visinfo;
944 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
945 if (NULL == cfgs || 0 == nCfgs) {
946 if(cfgs != NULL) XFree(cfgs);
947 ERR("glXChooseFBConfig returns NULL\n");
948 return;
951 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
952 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
953 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
954 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
956 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
958 for(i=0, bmp_formats=0; i<nCfgs; i++)
960 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
961 bmp_formats++;
963 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
965 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
967 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
968 * Do this as GLX doesn't guarantee that the list is sorted */
969 for(run=0; run < 2; run++)
971 for(i=0; i<nCfgs; i++) {
972 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
973 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
975 /* The first run we only add onscreen formats (ones which have an associated X Visual).
976 * The second run we only set offscreen formats. */
977 if(!run && visinfo)
979 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
980 * The contents is copied to the destination using XCopyArea. For the copying to work
981 * the depth of the source and destination window should be the same. In general this should
982 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
983 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
984 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
985 * list formats with the same depth. */
986 if(visinfo->depth != default_visual.depth)
988 XFree(visinfo);
989 continue;
992 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
993 list[size].fbconfig = cfgs[i];
994 list[size].fmt_id = fmt_id;
995 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
996 list[size].dwFlags = 0;
997 size++;
998 onscreen_size++;
1000 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1001 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1003 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1004 list[size].fbconfig = cfgs[i];
1005 list[size].fmt_id = fmt_id;
1006 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1007 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1008 size++;
1009 onscreen_size++;
1011 } else if(run && !visinfo) {
1012 int window_drawable=0;
1013 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1015 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1016 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1017 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1018 * likely make our child window opengl rendering more complicated since likely you can't use
1019 * XCopyArea on a GLX Window.
1020 * For now ignore fbconfigs which are window drawable but lack a visual. */
1021 if(window_drawable & GLX_WINDOW_BIT)
1023 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1024 continue;
1027 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1028 list[size].fbconfig = cfgs[i];
1029 list[size].fmt_id = fmt_id;
1030 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1031 list[size].dwFlags = 0;
1032 size++;
1035 if (visinfo) XFree(visinfo);
1039 XFree(cfgs);
1041 pixel_formats = list;
1042 nb_pixel_formats = size;
1043 nb_onscreen_formats = onscreen_size;
1046 static inline BOOL is_valid_pixel_format( int format )
1048 return format > 0 && format <= nb_pixel_formats;
1051 static inline BOOL is_onscreen_pixel_format( int format )
1053 return format > 0 && format <= nb_onscreen_formats;
1056 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1057 * In our WGL implementation we only support a subset of these formats namely the format of
1058 * Wine's main visual and offscreen formats (if they are available).
1059 * This function converts a WGL format to its corresponding GLX one.
1061 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1063 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1064 * iPixelFormat in case of probing the number of pixelformats.
1066 if (is_valid_pixel_format( iPixelFormat ) &&
1067 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1068 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1069 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1070 return &pixel_formats[iPixelFormat-1];
1072 return NULL;
1075 static int pixelformat_from_fbconfig_id(XID fbconfig_id)
1077 int i;
1079 if (!fbconfig_id) return 0;
1081 for (i = 0; i < nb_pixel_formats; i++)
1082 if (pixel_formats[i].fmt_id == fbconfig_id) return i + 1;
1084 /* This will happen on hwnds without a pixel format set; it's ok */
1085 return 0;
1089 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1090 static void mark_drawable_dirty(Drawable old, Drawable new)
1092 struct wgl_context *ctx;
1094 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1096 if (old == ctx->drawables[0]) {
1097 ctx->drawables[0] = new;
1098 ctx->refresh_drawables = TRUE;
1100 if (old == ctx->drawables[1]) {
1101 ctx->drawables[1] = new;
1102 ctx->refresh_drawables = TRUE;
1107 /* Given the current context, make sure its drawable is sync'd */
1108 static inline void sync_context(struct wgl_context *context)
1110 if (context->refresh_drawables) {
1111 if (glxRequireVersion(3))
1112 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1113 context->drawables[1], context->ctx);
1114 else
1115 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1116 context->refresh_drawables = FALSE;
1120 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1122 struct gl_drawable *gl;
1124 EnterCriticalSection( &context_section );
1125 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1126 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1127 LeaveCriticalSection( &context_section );
1128 return NULL;
1131 static void release_gl_drawable( struct gl_drawable *gl )
1133 if (gl) LeaveCriticalSection( &context_section );
1136 BOOL has_gl_drawable( HWND hwnd )
1138 struct gl_drawable *gl;
1140 gl = get_gl_drawable( hwnd, 0 );
1141 release_gl_drawable( gl );
1142 return gl != NULL;
1145 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1147 GLXContext ctx;
1149 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1150 BOOL indirect = !(context->fmt->dwFlags & PFD_DRAW_TO_BITMAP);
1152 if(context->gl3_context)
1154 if(context->numAttribs)
1155 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1156 else
1157 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1159 else if(context->vis)
1160 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1161 else /* Create a GLX Context for a pbuffer */
1162 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1164 return ctx;
1168 /***********************************************************************
1169 * free_gl_drawable
1171 static void free_gl_drawable( struct gl_drawable *gl )
1173 switch (gl->type)
1175 case DC_GL_WINDOW:
1176 case DC_GL_CHILD_WIN:
1177 XDestroyWindow( gdi_display, gl->drawable );
1178 XFreeColormap( gdi_display, gl->colormap );
1179 break;
1180 case DC_GL_PIXMAP_WIN:
1181 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1182 XFreePixmap( gdi_display, gl->pixmap );
1183 break;
1184 default:
1185 break;
1187 if (gl->visual) XFree( gl->visual );
1188 HeapFree( GetProcessHeap(), 0, gl );
1192 /***********************************************************************
1193 * set_win_format
1195 BOOL set_win_format( HWND hwnd, XID fbconfig_id )
1197 HWND parent = GetAncestor( hwnd, GA_PARENT );
1198 XSetWindowAttributes attrib;
1199 struct gl_drawable *gl, *prev;
1200 int format;
1202 if (!(format = pixelformat_from_fbconfig_id( fbconfig_id ))) return FALSE;
1204 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1205 gl->format = &pixel_formats[format - 1];
1206 gl->visual = pglXGetVisualFromFBConfig( gdi_display, gl->format->fbconfig );
1207 if (!gl->visual)
1209 HeapFree( GetProcessHeap(), 0, gl );
1210 return FALSE;
1213 GetClientRect( hwnd, &gl->rect );
1214 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1215 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1217 if (parent == GetDesktopWindow()) /* top-level window */
1219 Window xparent = X11DRV_get_whole_window( hwnd );
1221 gl->type = DC_GL_WINDOW;
1222 gl->colormap = XCreateColormap( gdi_display, root_window, gl->visual->visual,
1223 (gl->visual->class == PseudoColor ||
1224 gl->visual->class == GrayScale ||
1225 gl->visual->class == DirectColor) ? AllocAll : AllocNone );
1226 attrib.colormap = gl->colormap;
1227 attrib.bit_gravity = NorthWestGravity;
1228 attrib.win_gravity = NorthWestGravity;
1229 attrib.backing_store = NotUseful;
1230 /* put the initial rect outside of the window, it will be moved into place by SetWindowPos */
1231 OffsetRect( &gl->rect, gl->rect.right, gl->rect.bottom );
1232 if (xparent)
1233 gl->drawable = XCreateWindow( gdi_display, xparent, gl->rect.left, gl->rect.top,
1234 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1235 0, default_visual.depth, InputOutput, gl->visual->visual,
1236 CWBitGravity | CWWinGravity | CWBackingStore | CWColormap,
1237 &attrib );
1238 if (gl->drawable)
1239 XMapWindow( gdi_display, gl->drawable );
1240 else
1241 XFreeColormap( gdi_display, gl->colormap );
1243 else if (!GetAncestor( parent, GA_PARENT ))
1245 FIXME( "can't set format of HWND_MESSAGE window %p\n", hwnd );
1247 #ifdef SONAME_LIBXCOMPOSITE
1248 else if(usexcomposite)
1250 static Window dummy_parent;
1252 attrib.override_redirect = True;
1253 if (!dummy_parent)
1255 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1256 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1257 XMapWindow( gdi_display, dummy_parent );
1259 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1260 (gl->visual->class == PseudoColor ||
1261 gl->visual->class == GrayScale ||
1262 gl->visual->class == DirectColor) ?
1263 AllocAll : AllocNone);
1264 attrib.colormap = gl->colormap;
1265 XInstallColormap(gdi_display, attrib.colormap);
1267 gl->type = DC_GL_CHILD_WIN;
1268 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1269 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1270 0, gl->visual->depth, InputOutput, gl->visual->visual,
1271 CWColormap | CWOverrideRedirect, &attrib );
1272 if (gl->drawable)
1274 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1275 XMapWindow(gdi_display, gl->drawable);
1277 else XFreeColormap( gdi_display, gl->colormap );
1279 #endif
1280 else
1282 WARN("XComposite is not available, using GLXPixmap hack\n");
1284 gl->type = DC_GL_PIXMAP_WIN;
1285 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1286 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1287 gl->visual->depth );
1288 if (gl->pixmap)
1290 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1291 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1295 if (!gl->drawable)
1297 XFree( gl->visual );
1298 HeapFree( GetProcessHeap(), 0, gl );
1299 return FALSE;
1302 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n", gl->drawable, fbconfig_id);
1304 XFlush( gdi_display );
1306 EnterCriticalSection( &context_section );
1307 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1308 free_gl_drawable( prev );
1309 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1310 LeaveCriticalSection( &context_section );
1312 /* force DCE invalidation */
1313 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
1314 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
1315 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
1316 return TRUE;
1319 /***********************************************************************
1320 * sync_gl_drawable
1322 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1324 struct gl_drawable *gl;
1325 Drawable glxp;
1326 Pixmap pix;
1327 int mask = 0;
1328 XWindowChanges changes;
1330 changes.x = client_rect->left - visible_rect->left;
1331 changes.y = client_rect->top - visible_rect->top;
1332 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1333 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1335 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1337 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1338 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1340 TRACE( "setting drawable %lx pos %d,%d,%dx%d\n",
1341 gl->drawable, changes.x, changes.y, changes.width, changes.height );
1343 switch (gl->type)
1345 case DC_GL_WINDOW:
1346 if (changes.x != gl->rect.left) mask |= CWX;
1347 if (changes.y != gl->rect.top) mask |= CWY;
1348 /* fallthrough */
1349 case DC_GL_CHILD_WIN:
1350 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1351 break;
1352 case DC_GL_PIXMAP_WIN:
1353 if (!mask) break;
1354 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1355 if (!pix) goto done;
1356 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1357 if (!glxp)
1359 XFreePixmap(gdi_display, pix);
1360 goto done;
1362 mark_drawable_dirty(gl->drawable, glxp);
1363 XFlush( gdi_display );
1365 XFreePixmap(gdi_display, gl->pixmap);
1366 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1367 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1369 gl->pixmap = pix;
1370 gl->drawable = glxp;
1371 break;
1372 default:
1373 break;
1375 SetRect( &gl->rect, changes.x, changes.y, changes.x + changes.width, changes.y + changes.height );
1376 done:
1377 release_gl_drawable( gl );
1380 /***********************************************************************
1381 * destroy_gl_drawable
1383 void destroy_gl_drawable( HWND hwnd )
1385 struct gl_drawable *gl;
1387 EnterCriticalSection( &context_section );
1388 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1390 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1391 free_gl_drawable( gl );
1393 LeaveCriticalSection( &context_section );
1398 * glxdrv_DescribePixelFormat
1400 * Get the pixel-format descriptor associated to the given id
1402 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1403 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1405 /*XVisualInfo *vis;*/
1406 int value;
1407 int rb,gb,bb,ab;
1408 const struct wgl_pixel_format *fmt;
1410 if (!has_opengl()) return 0;
1412 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1414 if (!ppfd) return nb_onscreen_formats;
1416 /* 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 */
1417 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1418 if (!fmt) {
1419 WARN("unexpected format %d\n", iPixelFormat);
1420 return 0;
1423 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1424 ERR("Wrong structure size !\n");
1425 /* Should set error */
1426 return 0;
1429 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1430 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1431 ppfd->nVersion = 1;
1433 /* These flags are always the same... */
1434 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1435 /* Now the flags extracted from the Visual */
1437 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1438 if(value & GLX_WINDOW_BIT)
1439 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1441 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1442 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1443 * 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
1444 * offered the GDI bit either. */
1445 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1447 /* PFD_GENERIC_FORMAT - gdi software rendering
1448 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1449 * none set - full hardware accelerated by a ICD
1451 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1452 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1454 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1455 if (value) {
1456 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1457 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1459 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1461 /* Pixel type */
1462 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1463 if (value & GLX_RGBA_BIT)
1464 ppfd->iPixelType = PFD_TYPE_RGBA;
1465 else
1466 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1468 /* Color bits */
1469 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1470 ppfd->cColorBits = value;
1472 /* Red, green, blue and alpha bits / shifts */
1473 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1474 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1475 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1476 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1477 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1479 ppfd->cRedBits = rb;
1480 ppfd->cRedShift = gb + bb + ab;
1481 ppfd->cBlueBits = bb;
1482 ppfd->cBlueShift = ab;
1483 ppfd->cGreenBits = gb;
1484 ppfd->cGreenShift = bb + ab;
1485 ppfd->cAlphaBits = ab;
1486 ppfd->cAlphaShift = 0;
1487 } else {
1488 ppfd->cRedBits = 0;
1489 ppfd->cRedShift = 0;
1490 ppfd->cBlueBits = 0;
1491 ppfd->cBlueShift = 0;
1492 ppfd->cGreenBits = 0;
1493 ppfd->cGreenShift = 0;
1494 ppfd->cAlphaBits = 0;
1495 ppfd->cAlphaShift = 0;
1498 /* Accum RGBA bits */
1499 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1500 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1501 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1502 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1504 ppfd->cAccumBits = rb+gb+bb+ab;
1505 ppfd->cAccumRedBits = rb;
1506 ppfd->cAccumGreenBits = gb;
1507 ppfd->cAccumBlueBits = bb;
1508 ppfd->cAccumAlphaBits = ab;
1510 /* Aux bits */
1511 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1512 ppfd->cAuxBuffers = value;
1514 /* Depth bits */
1515 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1516 ppfd->cDepthBits = value;
1518 /* stencil bits */
1519 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1520 ppfd->cStencilBits = value;
1522 ppfd->iLayerType = PFD_MAIN_PLANE;
1524 if (TRACE_ON(wgl)) {
1525 dump_PIXELFORMATDESCRIPTOR(ppfd);
1528 return nb_onscreen_formats;
1531 /***********************************************************************
1532 * glxdrv_wglGetPixelFormat
1534 static int glxdrv_wglGetPixelFormat( HDC hdc )
1536 struct gl_drawable *gl;
1537 int ret = 0;
1539 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1541 ret = gl->format - pixel_formats + 1;
1542 /* Offscreen formats can't be used with traditional WGL calls.
1543 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1544 if (!is_onscreen_pixel_format( ret )) ret = 1;
1545 release_gl_drawable( gl );
1547 TRACE( "%p -> %d\n", hdc, ret );
1548 return ret;
1551 /***********************************************************************
1552 * glxdrv_wglSetPixelFormat
1554 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1556 const struct wgl_pixel_format *fmt;
1557 int value;
1558 struct gl_drawable *gl;
1559 HWND hwnd = WindowFromDC( hdc );
1561 TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
1563 if (!hwnd || hwnd == GetDesktopWindow())
1565 WARN( "not a proper window DC %p/%p\n", hdc, hwnd );
1566 return FALSE;
1569 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1570 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1571 if(!fmt) {
1572 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1573 return FALSE;
1576 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1577 if (!(value & GLX_WINDOW_BIT))
1579 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1580 return FALSE;
1583 if ((gl = get_gl_drawable( hwnd, hdc )))
1585 int prev = gl->format - pixel_formats + 1;
1586 release_gl_drawable( gl );
1587 return prev == iPixelFormat; /* cannot change it if already set */
1590 if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, fmt->fmt_id, 0)) {
1591 ERR("Couldn't set format of the window, returning failure\n");
1592 return FALSE;
1594 /* physDev->current_pf will be set by the DCE update */
1596 if (TRACE_ON(wgl)) {
1597 int gl_test = 0;
1599 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1600 if (gl_test) {
1601 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1602 } else {
1603 TRACE(" FBConfig have :\n");
1604 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1605 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1606 TRACE(" - VISUAL_ID 0x%x\n", value);
1607 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1608 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1611 return TRUE;
1614 /***********************************************************************
1615 * glxdrv_wglCopyContext
1617 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1619 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1621 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1623 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1624 return TRUE;
1627 /***********************************************************************
1628 * glxdrv_wglCreateContext
1630 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1632 struct wgl_context *ret = NULL;
1633 struct gl_drawable *gl;
1635 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1637 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1638 return NULL;
1641 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1643 ret->hdc = hdc;
1644 ret->fmt = gl->format;
1645 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1646 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1647 list_add_head( &context_list, &ret->entry );
1649 release_gl_drawable( gl );
1650 TRACE( "%p -> %p\n", hdc, ret );
1651 return ret;
1654 /***********************************************************************
1655 * glxdrv_wglDeleteContext
1657 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1659 TRACE("(%p)\n", ctx);
1661 EnterCriticalSection( &context_section );
1662 list_remove( &ctx->entry );
1663 LeaveCriticalSection( &context_section );
1665 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1666 if (ctx->vis) XFree( ctx->vis );
1667 HeapFree( GetProcessHeap(), 0, ctx );
1670 /***********************************************************************
1671 * glxdrv_wglGetProcAddress
1673 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1675 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1676 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1679 /***********************************************************************
1680 * glxdrv_wglMakeCurrent
1682 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1684 BOOL ret = FALSE;
1685 struct gl_drawable *gl;
1687 TRACE("(%p,%p)\n", hdc, ctx);
1689 if (!ctx)
1691 pglXMakeCurrent(gdi_display, None, NULL);
1692 NtCurrentTeb()->glContext = NULL;
1693 return TRUE;
1696 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1698 if (ctx->fmt != gl->format)
1700 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1701 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1702 goto done;
1705 if (TRACE_ON(wgl)) {
1706 describeContext(ctx);
1707 TRACE("hdc %p drawable %lx fmt %p ctx %p\n", hdc, gl->drawable, gl->format, ctx->ctx );
1710 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1711 if (ret)
1713 NtCurrentTeb()->glContext = ctx;
1714 ctx->has_been_current = TRUE;
1715 ctx->hdc = hdc;
1716 ctx->drawables[0] = gl->drawable;
1717 ctx->drawables[1] = gl->drawable;
1718 ctx->refresh_drawables = FALSE;
1719 goto done;
1722 SetLastError( ERROR_INVALID_HANDLE );
1724 done:
1725 release_gl_drawable( gl );
1726 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1727 return ret;
1730 /***********************************************************************
1731 * X11DRV_wglMakeContextCurrentARB
1733 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1735 BOOL ret = FALSE;
1736 struct gl_drawable *draw_gl, *read_gl = NULL;
1738 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1740 if (!ctx)
1742 pglXMakeCurrent(gdi_display, None, NULL);
1743 NtCurrentTeb()->glContext = NULL;
1744 return TRUE;
1747 if (!pglXMakeContextCurrent) return FALSE;
1749 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1751 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1752 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1753 read_gl ? read_gl->drawable : 0, ctx->ctx);
1754 if (ret)
1756 ctx->has_been_current = TRUE;
1757 ctx->hdc = draw_hdc;
1758 ctx->drawables[0] = draw_gl->drawable;
1759 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1760 ctx->refresh_drawables = FALSE;
1761 NtCurrentTeb()->glContext = ctx;
1762 goto done;
1765 SetLastError( ERROR_INVALID_HANDLE );
1766 done:
1767 release_gl_drawable( read_gl );
1768 release_gl_drawable( draw_gl );
1769 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1770 return ret;
1773 /***********************************************************************
1774 * glxdrv_wglShareLists
1776 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1778 TRACE("(%p, %p)\n", org, dest);
1780 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1781 * at context creation time but in case of WGL it is done using wglShareLists.
1782 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1783 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1784 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1785 * so there delaying context creation doesn't work.
1787 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1788 * and when a program requests sharing we recreate the destination context if it hasn't been made
1789 * current or when it hasn't shared display lists before.
1792 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1794 ERR("Could not share display lists, one of the contexts has been current already !\n");
1795 return FALSE;
1797 else if(dest->sharing)
1799 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1800 return FALSE;
1802 else
1804 describeContext(org);
1805 describeContext(dest);
1807 /* Re-create the GLX context and share display lists */
1808 pglXDestroyContext(gdi_display, dest->ctx);
1809 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1810 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1812 org->sharing = TRUE;
1813 dest->sharing = TRUE;
1814 return TRUE;
1816 return FALSE;
1819 static void wglFinish(void)
1821 struct x11drv_escape_flush_gl_drawable escape;
1822 struct gl_drawable *gl;
1823 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1825 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1826 escape.gl_drawable = 0;
1828 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1830 switch (gl->type)
1832 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1833 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1834 default: break;
1836 sync_context(ctx);
1837 release_gl_drawable( gl );
1840 pglFinish();
1841 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1844 static void wglFlush(void)
1846 struct x11drv_escape_flush_gl_drawable escape;
1847 struct gl_drawable *gl;
1848 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1850 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1851 escape.gl_drawable = 0;
1853 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1855 switch (gl->type)
1857 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1858 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1859 default: break;
1861 sync_context(ctx);
1862 release_gl_drawable( gl );
1865 pglFlush();
1866 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1869 /***********************************************************************
1870 * X11DRV_wglCreateContextAttribsARB
1872 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
1873 const int* attribList )
1875 struct wgl_context *ret = NULL;
1876 struct gl_drawable *gl;
1878 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
1880 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1882 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1883 return NULL;
1886 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1888 ret->hdc = hdc;
1889 ret->fmt = gl->format;
1890 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
1891 ret->gl3_context = TRUE;
1892 ret->numAttribs = 0;
1893 if (attribList)
1895 int *pContextAttribList = &ret->attribList[0];
1896 /* attribList consists of pairs {token, value] terminated with 0 */
1897 while(attribList[0] != 0)
1899 TRACE("%#x %#x\n", attribList[0], attribList[1]);
1900 switch(attribList[0])
1902 case WGL_CONTEXT_MAJOR_VERSION_ARB:
1903 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
1904 pContextAttribList[1] = attribList[1];
1905 break;
1906 case WGL_CONTEXT_MINOR_VERSION_ARB:
1907 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
1908 pContextAttribList[1] = attribList[1];
1909 break;
1910 case WGL_CONTEXT_LAYER_PLANE_ARB:
1911 break;
1912 case WGL_CONTEXT_FLAGS_ARB:
1913 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
1914 pContextAttribList[1] = attribList[1];
1915 break;
1916 case WGL_CONTEXT_PROFILE_MASK_ARB:
1917 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
1918 pContextAttribList[1] = attribList[1];
1919 break;
1920 default:
1921 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
1923 ret->numAttribs++;
1924 attribList += 2;
1925 pContextAttribList += 2;
1929 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1930 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1931 XSync(gdi_display, False);
1932 if (X11DRV_check_error() || !ret->ctx)
1934 /* In the future we should convert the GLX error to a win32 one here if needed */
1935 ERR("Context creation failed\n");
1936 HeapFree( GetProcessHeap(), 0, ret );
1937 ret = NULL;
1939 else list_add_head( &context_list, &ret->entry );
1942 release_gl_drawable( gl );
1943 TRACE( "%p -> %p\n", hdc, ret );
1944 return ret;
1948 * X11DRV_wglGetExtensionsStringARB
1950 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
1952 static const GLubyte *X11DRV_wglGetExtensionsStringARB(HDC hdc)
1954 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
1955 return (const GLubyte *)WineGLInfo.wglExtensions;
1959 * X11DRV_wglCreatePbufferARB
1961 * WGL_ARB_pbuffer: wglCreatePbufferARB
1963 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
1964 const int *piAttribList )
1966 struct wgl_pbuffer* object = NULL;
1967 const struct wgl_pixel_format *fmt = NULL;
1968 int attribs[256];
1969 int nAttribs = 0;
1971 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
1973 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
1974 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
1975 if(!fmt) {
1976 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
1977 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1978 return NULL;
1981 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1982 if (NULL == object) {
1983 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
1984 return NULL;
1986 object->width = iWidth;
1987 object->height = iHeight;
1988 object->fmt = fmt;
1990 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
1991 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
1992 while (piAttribList && 0 != *piAttribList) {
1993 int attr_v;
1994 switch (*piAttribList) {
1995 case WGL_PBUFFER_LARGEST_ARB: {
1996 ++piAttribList;
1997 attr_v = *piAttribList;
1998 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
1999 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2000 break;
2003 case WGL_TEXTURE_FORMAT_ARB: {
2004 ++piAttribList;
2005 attr_v = *piAttribList;
2006 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2007 if (WGL_NO_TEXTURE_ARB == attr_v) {
2008 object->use_render_texture = 0;
2009 } else {
2010 if (!use_render_texture_emulation) {
2011 SetLastError(ERROR_INVALID_DATA);
2012 goto create_failed;
2014 switch (attr_v) {
2015 case WGL_TEXTURE_RGB_ARB:
2016 object->use_render_texture = GL_RGB;
2017 object->texture_bpp = 3;
2018 object->texture_format = GL_RGB;
2019 object->texture_type = GL_UNSIGNED_BYTE;
2020 break;
2021 case WGL_TEXTURE_RGBA_ARB:
2022 object->use_render_texture = GL_RGBA;
2023 object->texture_bpp = 4;
2024 object->texture_format = GL_RGBA;
2025 object->texture_type = GL_UNSIGNED_BYTE;
2026 break;
2028 /* WGL_FLOAT_COMPONENTS_NV */
2029 case WGL_TEXTURE_FLOAT_R_NV:
2030 object->use_render_texture = GL_FLOAT_R_NV;
2031 object->texture_bpp = 4;
2032 object->texture_format = GL_RED;
2033 object->texture_type = GL_FLOAT;
2034 break;
2035 case WGL_TEXTURE_FLOAT_RG_NV:
2036 object->use_render_texture = GL_FLOAT_RG_NV;
2037 object->texture_bpp = 8;
2038 object->texture_format = GL_LUMINANCE_ALPHA;
2039 object->texture_type = GL_FLOAT;
2040 break;
2041 case WGL_TEXTURE_FLOAT_RGB_NV:
2042 object->use_render_texture = GL_FLOAT_RGB_NV;
2043 object->texture_bpp = 12;
2044 object->texture_format = GL_RGB;
2045 object->texture_type = GL_FLOAT;
2046 break;
2047 case WGL_TEXTURE_FLOAT_RGBA_NV:
2048 object->use_render_texture = GL_FLOAT_RGBA_NV;
2049 object->texture_bpp = 16;
2050 object->texture_format = GL_RGBA;
2051 object->texture_type = GL_FLOAT;
2052 break;
2053 default:
2054 ERR("Unknown texture format: %x\n", attr_v);
2055 SetLastError(ERROR_INVALID_DATA);
2056 goto create_failed;
2059 break;
2062 case WGL_TEXTURE_TARGET_ARB: {
2063 ++piAttribList;
2064 attr_v = *piAttribList;
2065 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2066 if (WGL_NO_TEXTURE_ARB == attr_v) {
2067 object->texture_target = 0;
2068 } else {
2069 if (!use_render_texture_emulation) {
2070 SetLastError(ERROR_INVALID_DATA);
2071 goto create_failed;
2073 switch (attr_v) {
2074 case WGL_TEXTURE_CUBE_MAP_ARB: {
2075 if (iWidth != iHeight) {
2076 SetLastError(ERROR_INVALID_DATA);
2077 goto create_failed;
2079 object->texture_target = GL_TEXTURE_CUBE_MAP;
2080 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2081 break;
2083 case WGL_TEXTURE_1D_ARB: {
2084 if (1 != iHeight) {
2085 SetLastError(ERROR_INVALID_DATA);
2086 goto create_failed;
2088 object->texture_target = GL_TEXTURE_1D;
2089 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2090 break;
2092 case WGL_TEXTURE_2D_ARB: {
2093 object->texture_target = GL_TEXTURE_2D;
2094 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2095 break;
2097 case WGL_TEXTURE_RECTANGLE_NV: {
2098 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2099 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2100 break;
2102 default:
2103 ERR("Unknown texture target: %x\n", attr_v);
2104 SetLastError(ERROR_INVALID_DATA);
2105 goto create_failed;
2108 break;
2111 case WGL_MIPMAP_TEXTURE_ARB: {
2112 ++piAttribList;
2113 attr_v = *piAttribList;
2114 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2115 if (!use_render_texture_emulation) {
2116 SetLastError(ERROR_INVALID_DATA);
2117 goto create_failed;
2119 break;
2122 ++piAttribList;
2125 PUSH1(attribs, None);
2126 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2127 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2128 if (!object->drawable) {
2129 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2130 goto create_failed; /* unexpected error */
2132 TRACE("->(%p)\n", object);
2133 return object;
2135 create_failed:
2136 HeapFree(GetProcessHeap(), 0, object);
2137 TRACE("->(FAILED)\n");
2138 return NULL;
2142 * X11DRV_wglDestroyPbufferARB
2144 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2146 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2148 TRACE("(%p)\n", object);
2150 pglXDestroyPbuffer(gdi_display, object->drawable);
2151 HeapFree(GetProcessHeap(), 0, object);
2152 return GL_TRUE;
2156 * X11DRV_wglGetPbufferDCARB
2158 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2160 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2162 struct x11drv_escape_set_drawable escape;
2163 struct gl_drawable *gl, *prev;
2164 HDC hdc;
2166 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2167 if (!hdc) return 0;
2169 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2171 DeleteDC( hdc );
2172 return 0;
2174 gl->type = DC_GL_PBUFFER;
2175 gl->drawable = object->drawable;
2176 gl->format = object->fmt;
2178 EnterCriticalSection( &context_section );
2179 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2180 free_gl_drawable( prev );
2181 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2182 LeaveCriticalSection( &context_section );
2184 escape.code = X11DRV_SET_DRAWABLE;
2185 escape.hwnd = 0;
2186 escape.drawable = object->drawable;
2187 escape.mode = IncludeInferiors;
2188 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2189 escape.fbconfig_id = object->fmt->fmt_id;
2190 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2192 TRACE( "(%p)->(%p)\n", object, hdc );
2193 return hdc;
2197 * X11DRV_wglQueryPbufferARB
2199 * WGL_ARB_pbuffer: wglQueryPbufferARB
2201 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2203 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2205 switch (iAttribute) {
2206 case WGL_PBUFFER_WIDTH_ARB:
2207 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2208 break;
2209 case WGL_PBUFFER_HEIGHT_ARB:
2210 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2211 break;
2213 case WGL_PBUFFER_LOST_ARB:
2214 /* GLX Pbuffers cannot be lost by default. We can support this by
2215 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2216 * to receive pixel buffer clobber events, however that may or may
2217 * not give any benefit */
2218 *piValue = GL_FALSE;
2219 break;
2221 case WGL_TEXTURE_FORMAT_ARB:
2222 if (!object->use_render_texture) {
2223 *piValue = WGL_NO_TEXTURE_ARB;
2224 } else {
2225 if (!use_render_texture_emulation) {
2226 SetLastError(ERROR_INVALID_HANDLE);
2227 return GL_FALSE;
2229 switch(object->use_render_texture) {
2230 case GL_RGB:
2231 *piValue = WGL_TEXTURE_RGB_ARB;
2232 break;
2233 case GL_RGBA:
2234 *piValue = WGL_TEXTURE_RGBA_ARB;
2235 break;
2236 /* WGL_FLOAT_COMPONENTS_NV */
2237 case GL_FLOAT_R_NV:
2238 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2239 break;
2240 case GL_FLOAT_RG_NV:
2241 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2242 break;
2243 case GL_FLOAT_RGB_NV:
2244 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2245 break;
2246 case GL_FLOAT_RGBA_NV:
2247 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2248 break;
2249 default:
2250 ERR("Unknown texture format: %x\n", object->use_render_texture);
2253 break;
2255 case WGL_TEXTURE_TARGET_ARB:
2256 if (!object->texture_target){
2257 *piValue = WGL_NO_TEXTURE_ARB;
2258 } else {
2259 if (!use_render_texture_emulation) {
2260 SetLastError(ERROR_INVALID_DATA);
2261 return GL_FALSE;
2263 switch (object->texture_target) {
2264 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2265 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2266 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2267 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2270 break;
2272 case WGL_MIPMAP_TEXTURE_ARB:
2273 *piValue = GL_FALSE; /** don't support that */
2274 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2275 break;
2277 default:
2278 FIXME("unexpected attribute %x\n", iAttribute);
2279 break;
2282 return GL_TRUE;
2286 * X11DRV_wglReleasePbufferDCARB
2288 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2290 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2292 struct gl_drawable *gl;
2294 TRACE("(%p, %p)\n", object, hdc);
2296 EnterCriticalSection( &context_section );
2297 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2299 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2300 free_gl_drawable( gl );
2302 LeaveCriticalSection( &context_section );
2304 return DeleteDC(hdc);
2308 * X11DRV_wglSetPbufferAttribARB
2310 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2312 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2314 GLboolean ret = GL_FALSE;
2316 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2318 if (!object->use_render_texture) {
2319 SetLastError(ERROR_INVALID_HANDLE);
2320 return GL_FALSE;
2322 if (1 == use_render_texture_emulation) {
2323 return GL_TRUE;
2325 return ret;
2329 * X11DRV_wglChoosePixelFormatARB
2331 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2333 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2334 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2336 int attribs[256];
2337 int nAttribs = 0;
2338 GLXFBConfig* cfgs = NULL;
2339 int nCfgs = 0;
2340 int it;
2341 int fmt_id;
2342 int start, end;
2343 UINT pfmt_it = 0;
2344 int run;
2345 int i;
2346 DWORD dwFlags = 0;
2348 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2349 if (NULL != pfAttribFList) {
2350 FIXME("unused pfAttribFList\n");
2353 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2354 if (-1 == nAttribs) {
2355 WARN("Cannot convert WGL to GLX attributes\n");
2356 return GL_FALSE;
2358 PUSH1(attribs, None);
2360 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2361 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2362 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2364 for(i=0; piAttribIList[i] != 0; i+=2)
2366 switch(piAttribIList[i])
2368 case WGL_DRAW_TO_BITMAP_ARB:
2369 if(piAttribIList[i+1])
2370 dwFlags |= PFD_DRAW_TO_BITMAP;
2371 break;
2372 case WGL_ACCELERATION_ARB:
2373 switch(piAttribIList[i+1])
2375 case WGL_NO_ACCELERATION_ARB:
2376 dwFlags |= PFD_GENERIC_FORMAT;
2377 break;
2378 case WGL_GENERIC_ACCELERATION_ARB:
2379 dwFlags |= PFD_GENERIC_ACCELERATED;
2380 break;
2381 case WGL_FULL_ACCELERATION_ARB:
2382 /* Nothing to do */
2383 break;
2385 break;
2386 case WGL_SUPPORT_GDI_ARB:
2387 if(piAttribIList[i+1])
2388 dwFlags |= PFD_SUPPORT_GDI;
2389 break;
2393 /* Search for FB configurations matching the requirements in attribs */
2394 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2395 if (NULL == cfgs) {
2396 WARN("Compatible Pixel Format not found\n");
2397 return GL_FALSE;
2400 /* Loop through all matching formats and check if they are suitable.
2401 * Note that this function should at max return nMaxFormats different formats */
2402 for(run=0; run < 2; run++)
2404 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2406 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2408 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2409 continue;
2412 /* During the first run we only want onscreen formats and during the second only offscreen */
2413 start = run == 1 ? nb_onscreen_formats : 0;
2414 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2416 for (i = start; i < end; i++)
2418 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2420 piFormats[pfmt_it++] = i + 1;
2421 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2422 it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2423 break;
2429 *nNumFormats = pfmt_it;
2430 /** free list */
2431 XFree(cfgs);
2432 return GL_TRUE;
2436 * X11DRV_wglGetPixelFormatAttribivARB
2438 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2440 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2441 UINT nAttributes, const int *piAttributes, int *piValues )
2443 UINT i;
2444 const struct wgl_pixel_format *fmt = NULL;
2445 int hTest;
2446 int tmp;
2447 int curGLXAttr = 0;
2449 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2451 if (0 < iLayerPlane) {
2452 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2453 return GL_FALSE;
2456 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2457 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2458 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2459 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2460 if(!fmt) {
2461 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2464 for (i = 0; i < nAttributes; ++i) {
2465 const int curWGLAttr = piAttributes[i];
2466 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2468 switch (curWGLAttr) {
2469 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2470 piValues[i] = nb_pixel_formats;
2471 continue;
2473 case WGL_SUPPORT_OPENGL_ARB:
2474 piValues[i] = GL_TRUE;
2475 continue;
2477 case WGL_ACCELERATION_ARB:
2478 curGLXAttr = GLX_CONFIG_CAVEAT;
2479 if (!fmt) goto pix_error;
2480 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2481 piValues[i] = WGL_NO_ACCELERATION_ARB;
2482 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2483 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2484 else
2485 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2486 continue;
2488 case WGL_TRANSPARENT_ARB:
2489 curGLXAttr = GLX_TRANSPARENT_TYPE;
2490 if (!fmt) goto pix_error;
2491 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2492 if (hTest) goto get_error;
2493 piValues[i] = GL_FALSE;
2494 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2495 continue;
2497 case WGL_PIXEL_TYPE_ARB:
2498 curGLXAttr = GLX_RENDER_TYPE;
2499 if (!fmt) goto pix_error;
2500 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2501 if (hTest) goto get_error;
2502 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2503 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2504 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2505 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2506 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2507 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2508 else {
2509 ERR("unexpected RenderType(%x)\n", tmp);
2510 piValues[i] = WGL_TYPE_RGBA_ARB;
2512 continue;
2514 case WGL_COLOR_BITS_ARB:
2515 curGLXAttr = GLX_BUFFER_SIZE;
2516 break;
2518 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2519 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2520 if (!use_render_texture_emulation) {
2521 piValues[i] = GL_FALSE;
2522 continue;
2524 curGLXAttr = GLX_RENDER_TYPE;
2525 if (!fmt) goto pix_error;
2526 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2527 if (hTest) goto get_error;
2528 if (GLX_COLOR_INDEX_BIT == tmp) {
2529 piValues[i] = GL_FALSE;
2530 continue;
2532 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2533 if (hTest) goto get_error;
2534 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2535 continue;
2537 case WGL_BLUE_BITS_ARB:
2538 curGLXAttr = GLX_BLUE_SIZE;
2539 break;
2540 case WGL_RED_BITS_ARB:
2541 curGLXAttr = GLX_RED_SIZE;
2542 break;
2543 case WGL_GREEN_BITS_ARB:
2544 curGLXAttr = GLX_GREEN_SIZE;
2545 break;
2546 case WGL_ALPHA_BITS_ARB:
2547 curGLXAttr = GLX_ALPHA_SIZE;
2548 break;
2549 case WGL_DEPTH_BITS_ARB:
2550 curGLXAttr = GLX_DEPTH_SIZE;
2551 break;
2552 case WGL_STENCIL_BITS_ARB:
2553 curGLXAttr = GLX_STENCIL_SIZE;
2554 break;
2555 case WGL_DOUBLE_BUFFER_ARB:
2556 curGLXAttr = GLX_DOUBLEBUFFER;
2557 break;
2558 case WGL_STEREO_ARB:
2559 curGLXAttr = GLX_STEREO;
2560 break;
2561 case WGL_AUX_BUFFERS_ARB:
2562 curGLXAttr = GLX_AUX_BUFFERS;
2563 break;
2565 case WGL_SUPPORT_GDI_ARB:
2566 if (!fmt) goto pix_error;
2567 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2568 continue;
2570 case WGL_DRAW_TO_BITMAP_ARB:
2571 if (!fmt) goto pix_error;
2572 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2573 continue;
2575 case WGL_DRAW_TO_WINDOW_ARB:
2576 case WGL_DRAW_TO_PBUFFER_ARB:
2577 if (!fmt) goto pix_error;
2578 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2579 if (hTest) goto get_error;
2580 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2581 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2582 piValues[i] = GL_TRUE;
2583 else
2584 piValues[i] = GL_FALSE;
2585 continue;
2587 case WGL_SWAP_METHOD_ARB:
2588 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2589 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2590 * point only ATI offers this.
2592 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2593 break;
2595 case WGL_PBUFFER_LARGEST_ARB:
2596 curGLXAttr = GLX_LARGEST_PBUFFER;
2597 break;
2599 case WGL_SAMPLE_BUFFERS_ARB:
2600 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2601 break;
2603 case WGL_SAMPLES_ARB:
2604 curGLXAttr = GLX_SAMPLES_ARB;
2605 break;
2607 case WGL_FLOAT_COMPONENTS_NV:
2608 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2609 break;
2611 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2612 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2613 break;
2615 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2616 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2617 break;
2619 case WGL_ACCUM_RED_BITS_ARB:
2620 curGLXAttr = GLX_ACCUM_RED_SIZE;
2621 break;
2622 case WGL_ACCUM_GREEN_BITS_ARB:
2623 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2624 break;
2625 case WGL_ACCUM_BLUE_BITS_ARB:
2626 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2627 break;
2628 case WGL_ACCUM_ALPHA_BITS_ARB:
2629 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2630 break;
2631 case WGL_ACCUM_BITS_ARB:
2632 if (!fmt) goto pix_error;
2633 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2634 if (hTest) goto get_error;
2635 piValues[i] = tmp;
2636 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2637 if (hTest) goto get_error;
2638 piValues[i] += tmp;
2639 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2640 if (hTest) goto get_error;
2641 piValues[i] += tmp;
2642 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2643 if (hTest) goto get_error;
2644 piValues[i] += tmp;
2645 continue;
2647 default:
2648 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2651 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2652 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2653 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2655 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2656 * and check which options can work using iPixelFormat=0 and which not.
2657 * A problem would be that this function is an extension. This would
2658 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2660 if (0 != curGLXAttr && iPixelFormat != 0) {
2661 if (!fmt) goto pix_error;
2662 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2663 if (hTest) goto get_error;
2664 curGLXAttr = 0;
2665 } else {
2666 piValues[i] = GL_FALSE;
2669 return GL_TRUE;
2671 get_error:
2672 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2673 return GL_FALSE;
2675 pix_error:
2676 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2677 return GL_FALSE;
2681 * X11DRV_wglGetPixelFormatAttribfvARB
2683 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2685 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2686 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2688 int *attr;
2689 int ret;
2690 UINT i;
2692 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2694 /* Allocate a temporary array to store integer values */
2695 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2696 if (!attr) {
2697 ERR("couldn't allocate %d array\n", nAttributes);
2698 return GL_FALSE;
2701 /* Piggy-back on wglGetPixelFormatAttribivARB */
2702 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2703 if (ret) {
2704 /* Convert integer values to float. Should also check for attributes
2705 that can give decimal values here */
2706 for (i=0; i<nAttributes;i++) {
2707 pfValues[i] = attr[i];
2711 HeapFree(GetProcessHeap(), 0, attr);
2712 return ret;
2716 * X11DRV_wglBindTexImageARB
2718 * WGL_ARB_render_texture: wglBindTexImageARB
2720 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2722 GLboolean ret = GL_FALSE;
2724 TRACE("(%p, %d)\n", object, iBuffer);
2726 if (!object->use_render_texture) {
2727 SetLastError(ERROR_INVALID_HANDLE);
2728 return GL_FALSE;
2731 if (1 == use_render_texture_emulation) {
2732 static int init = 0;
2733 int prev_binded_texture = 0;
2734 GLXContext prev_context;
2735 Drawable prev_drawable;
2736 GLXContext tmp_context;
2738 prev_context = pglXGetCurrentContext();
2739 prev_drawable = pglXGetCurrentDrawable();
2741 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2742 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2743 isn't ideal performance wise but I wasn't able to get other ways working.
2745 if(!init) {
2746 init = 1; /* Only show the FIXME once for performance reasons */
2747 FIXME("partial stub!\n");
2750 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2751 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2753 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2755 /* Switch to our pbuffer */
2756 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2758 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2759 * After that upload the pbuffer texture data. */
2760 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2761 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2763 /* Switch back to the original drawable and upload the pbuffer-texture */
2764 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2765 pglXDestroyContext(gdi_display, tmp_context);
2766 return GL_TRUE;
2769 return ret;
2773 * X11DRV_wglReleaseTexImageARB
2775 * WGL_ARB_render_texture: wglReleaseTexImageARB
2777 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2779 GLboolean ret = GL_FALSE;
2781 TRACE("(%p, %d)\n", object, iBuffer);
2783 if (!object->use_render_texture) {
2784 SetLastError(ERROR_INVALID_HANDLE);
2785 return GL_FALSE;
2787 if (1 == use_render_texture_emulation) {
2788 return GL_TRUE;
2790 return ret;
2794 * X11DRV_wglGetExtensionsStringEXT
2796 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2798 static const GLubyte *X11DRV_wglGetExtensionsStringEXT(void)
2800 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2801 return (const GLubyte *)WineGLInfo.wglExtensions;
2805 * X11DRV_wglGetSwapIntervalEXT
2807 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2809 static int X11DRV_wglGetSwapIntervalEXT(void)
2811 /* GLX_SGI_swap_control doesn't have any provisions for getting the swap
2812 * interval, so the swap interval has to be tracked. */
2813 TRACE("()\n");
2814 return swap_interval;
2818 * X11DRV_wglSwapIntervalEXT
2820 * WGL_EXT_swap_control: wglSwapIntervalEXT
2822 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
2824 BOOL ret = TRUE;
2826 TRACE("(%d)\n", interval);
2828 if (interval < 0)
2830 SetLastError(ERROR_INVALID_DATA);
2831 return FALSE;
2833 else if (!has_swap_control && interval == 0)
2835 /* wglSwapIntervalEXT considers an interval value of zero to mean that
2836 * vsync should be disabled, but glXSwapIntervalSGI considers such a
2837 * value to be an error. Just silently ignore the request for now. */
2838 WARN("Request to disable vertical sync is not handled\n");
2839 swap_interval = 0;
2841 else
2843 if (pglXSwapIntervalSGI)
2844 ret = !pglXSwapIntervalSGI(interval);
2845 else
2846 WARN("GLX_SGI_swap_control extension is not available\n");
2848 if (ret)
2849 swap_interval = interval;
2850 else
2851 SetLastError(ERROR_DC_NOT_FOUND);
2854 return ret;
2858 * X11DRV_wglSetPixelFormatWINE
2860 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
2861 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
2863 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
2865 const struct wgl_pixel_format *fmt;
2866 int value;
2867 HWND hwnd;
2869 TRACE("(%p,%d)\n", hdc, format);
2871 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
2872 if (!fmt)
2874 ERR( "Invalid format %d\n", format );
2875 return FALSE;
2878 hwnd = WindowFromDC( hdc );
2879 if (!hwnd || hwnd == GetDesktopWindow())
2881 ERR( "not a valid window DC %p\n", hdc );
2882 return FALSE;
2885 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
2886 if (!(value & GLX_WINDOW_BIT))
2888 WARN( "Pixel format %d is not compatible for window rendering\n", format );
2889 return FALSE;
2892 return SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, fmt->fmt_id, 0);
2893 /* DC pixel format will be set by the DCE update */
2897 * glxRequireVersion (internal)
2899 * Check if the supported GLX version matches requiredVersion.
2901 static BOOL glxRequireVersion(int requiredVersion)
2903 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2904 if(requiredVersion <= WineGLInfo.glxVersion[1])
2905 return TRUE;
2907 return FALSE;
2910 static void register_extension(const char *ext)
2912 if (WineGLInfo.wglExtensions[0])
2913 strcat(WineGLInfo.wglExtensions, " ");
2914 strcat(WineGLInfo.wglExtensions, ext);
2916 TRACE("'%s'\n", ext);
2920 * X11DRV_WineGL_LoadExtensions
2922 static void X11DRV_WineGL_LoadExtensions(void)
2924 WineGLInfo.wglExtensions[0] = 0;
2926 /* ARB Extensions */
2928 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
2930 register_extension( "WGL_ARB_create_context" );
2931 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
2933 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
2934 register_extension("WGL_ARB_create_context_profile");
2937 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
2939 register_extension("WGL_ARB_pixel_format_float");
2940 register_extension("WGL_ATI_pixel_format_float");
2943 register_extension( "WGL_ARB_extensions_string" );
2944 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
2946 if (glxRequireVersion(3))
2948 register_extension( "WGL_ARB_make_current_read" );
2949 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
2950 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
2953 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
2955 /* In general pbuffer functionality requires support in the X-server. The functionality is
2956 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
2958 if ( glxRequireVersion(3) && has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_pbuffer") )
2960 register_extension( "WGL_ARB_pbuffer" );
2961 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
2962 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
2963 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
2964 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
2965 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
2966 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
2969 register_extension( "WGL_ARB_pixel_format" );
2970 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
2971 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
2972 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
2974 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
2975 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
2976 (glxRequireVersion(3) && has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_pbuffer") && use_render_texture_emulation))
2978 register_extension( "WGL_ARB_render_texture" );
2979 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
2980 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
2982 /* The WGL version of GLX_NV_float_buffer requires render_texture */
2983 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
2984 register_extension("WGL_NV_float_buffer");
2986 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
2987 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
2988 register_extension("WGL_NV_texture_rectangle");
2991 /* EXT Extensions */
2993 register_extension( "WGL_EXT_extensions_string" );
2994 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
2996 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
2997 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
2998 register_extension( "WGL_EXT_swap_control" );
2999 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3000 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3002 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3003 register_extension("WGL_EXT_framebuffer_sRGB");
3005 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3006 register_extension("WGL_EXT_pixel_format_packed_float");
3008 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3009 has_swap_control = TRUE;
3011 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3012 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3014 register_extension( "WGL_NV_vertex_array_range" );
3015 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3016 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3019 /* WINE-specific WGL Extensions */
3021 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3022 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3024 register_extension( "WGL_WINE_pixel_format_passthrough" );
3025 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3030 * glxdrv_SwapBuffers
3032 * Swap the buffers of this DC
3034 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3036 struct x11drv_escape_flush_gl_drawable escape;
3037 struct gl_drawable *gl;
3038 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3040 TRACE("(%p)\n", hdc);
3042 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3043 escape.gl_drawable = 0;
3045 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3047 SetLastError( ERROR_INVALID_HANDLE );
3048 return FALSE;
3051 switch (gl->type)
3053 case DC_GL_PIXMAP_WIN:
3054 if (ctx) sync_context( ctx );
3055 escape.gl_drawable = gl->pixmap;
3056 if (pglXCopySubBufferMESA) {
3057 /* (glX)SwapBuffers has an implicit glFlush effect, however
3058 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3059 * copying */
3060 pglFlush();
3061 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3062 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3063 break;
3065 pglXSwapBuffers(gdi_display, gl->drawable);
3066 break;
3067 case DC_GL_CHILD_WIN:
3068 escape.gl_drawable = gl->drawable;
3069 /* fall through */
3070 default:
3071 pglXSwapBuffers(gdi_display, gl->drawable);
3072 break;
3075 release_gl_drawable( gl );
3077 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3078 return TRUE;
3081 static BOOL create_glx_dc( PHYSDEV *pdev )
3083 /* assume that only the main x11 device implements GetDeviceCaps */
3084 X11DRV_PDEVICE *x11dev = get_x11drv_dev( GET_NEXT_PHYSDEV( *pdev, pGetDeviceCaps ));
3085 struct glx_physdev *physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) );
3087 if (!physdev) return FALSE;
3088 physdev->x11dev = x11dev;
3089 push_dc_driver( pdev, &physdev->dev, &glxdrv_funcs );
3090 return TRUE;
3093 /**********************************************************************
3094 * glxdrv_CreateDC
3096 static BOOL glxdrv_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
3097 LPCWSTR output, const DEVMODEW* initData )
3099 return create_glx_dc( pdev );
3102 /**********************************************************************
3103 * glxdrv_CreateCompatibleDC
3105 static BOOL glxdrv_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
3107 if (orig) /* chain to next driver first */
3109 orig = GET_NEXT_PHYSDEV( orig, pCreateCompatibleDC );
3110 if (!orig->funcs->pCreateCompatibleDC( orig, pdev )) return FALSE;
3112 /* otherwise we have been called by x11drv */
3113 return create_glx_dc( pdev );
3116 /**********************************************************************
3117 * glxdrv_DeleteDC
3119 static BOOL glxdrv_DeleteDC( PHYSDEV dev )
3121 struct glx_physdev *physdev = get_glxdrv_dev( dev );
3122 HeapFree( GetProcessHeap(), 0, physdev );
3123 return TRUE;
3126 /**********************************************************************
3127 * glxdrv_wine_get_wgl_driver
3129 static struct opengl_funcs * glxdrv_wine_get_wgl_driver( PHYSDEV dev, UINT version )
3131 if (version != WINE_WGL_DRIVER_VERSION)
3133 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3134 return NULL;
3137 if (has_opengl()) return &opengl_funcs;
3139 dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
3140 return dev->funcs->wine_get_wgl_driver( dev, version );
3143 static const struct gdi_dc_funcs glxdrv_funcs =
3145 NULL, /* pAbortDoc */
3146 NULL, /* pAbortPath */
3147 NULL, /* pAlphaBlend */
3148 NULL, /* pAngleArc */
3149 NULL, /* pArc */
3150 NULL, /* pArcTo */
3151 NULL, /* pBeginPath */
3152 NULL, /* pBlendImage */
3153 NULL, /* pChord */
3154 NULL, /* pCloseFigure */
3155 glxdrv_CreateCompatibleDC, /* pCreateCompatibleDC */
3156 glxdrv_CreateDC, /* pCreateDC */
3157 glxdrv_DeleteDC, /* pDeleteDC */
3158 NULL, /* pDeleteObject */
3159 NULL, /* pDeviceCapabilities */
3160 NULL, /* pEllipse */
3161 NULL, /* pEndDoc */
3162 NULL, /* pEndPage */
3163 NULL, /* pEndPath */
3164 NULL, /* pEnumFonts */
3165 NULL, /* pEnumICMProfiles */
3166 NULL, /* pExcludeClipRect */
3167 NULL, /* pExtDeviceMode */
3168 NULL, /* pExtEscape */
3169 NULL, /* pExtFloodFill */
3170 NULL, /* pExtSelectClipRgn */
3171 NULL, /* pExtTextOut */
3172 NULL, /* pFillPath */
3173 NULL, /* pFillRgn */
3174 NULL, /* pFlattenPath */
3175 NULL, /* pFontIsLinked */
3176 NULL, /* pFrameRgn */
3177 NULL, /* pGdiComment */
3178 NULL, /* pGdiRealizationInfo */
3179 NULL, /* pGetBoundsRect */
3180 NULL, /* pGetCharABCWidths */
3181 NULL, /* pGetCharABCWidthsI */
3182 NULL, /* pGetCharWidth */
3183 NULL, /* pGetDeviceCaps */
3184 NULL, /* pGetDeviceGammaRamp */
3185 NULL, /* pGetFontData */
3186 NULL, /* pGetFontUnicodeRanges */
3187 NULL, /* pGetGlyphIndices */
3188 NULL, /* pGetGlyphOutline */
3189 NULL, /* pGetICMProfile */
3190 NULL, /* pGetImage */
3191 NULL, /* pGetKerningPairs */
3192 NULL, /* pGetNearestColor */
3193 NULL, /* pGetOutlineTextMetrics */
3194 NULL, /* pGetPixel */
3195 NULL, /* pGetSystemPaletteEntries */
3196 NULL, /* pGetTextCharsetInfo */
3197 NULL, /* pGetTextExtentExPoint */
3198 NULL, /* pGetTextExtentExPointI */
3199 NULL, /* pGetTextFace */
3200 NULL, /* pGetTextMetrics */
3201 NULL, /* pGradientFill */
3202 NULL, /* pIntersectClipRect */
3203 NULL, /* pInvertRgn */
3204 NULL, /* pLineTo */
3205 NULL, /* pModifyWorldTransform */
3206 NULL, /* pMoveTo */
3207 NULL, /* pOffsetClipRgn */
3208 NULL, /* pOffsetViewportOrg */
3209 NULL, /* pOffsetWindowOrg */
3210 NULL, /* pPaintRgn */
3211 NULL, /* pPatBlt */
3212 NULL, /* pPie */
3213 NULL, /* pPolyBezier */
3214 NULL, /* pPolyBezierTo */
3215 NULL, /* pPolyDraw */
3216 NULL, /* pPolyPolygon */
3217 NULL, /* pPolyPolyline */
3218 NULL, /* pPolygon */
3219 NULL, /* pPolyline */
3220 NULL, /* pPolylineTo */
3221 NULL, /* pPutImage */
3222 NULL, /* pRealizeDefaultPalette */
3223 NULL, /* pRealizePalette */
3224 NULL, /* pRectangle */
3225 NULL, /* pResetDC */
3226 NULL, /* pRestoreDC */
3227 NULL, /* pRoundRect */
3228 NULL, /* pSaveDC */
3229 NULL, /* pScaleViewportExt */
3230 NULL, /* pScaleWindowExt */
3231 NULL, /* pSelectBitmap */
3232 NULL, /* pSelectBrush */
3233 NULL, /* pSelectClipPath */
3234 NULL, /* pSelectFont */
3235 NULL, /* pSelectPalette */
3236 NULL, /* pSelectPen */
3237 NULL, /* pSetArcDirection */
3238 NULL, /* pSetBkColor */
3239 NULL, /* pSetBkMode */
3240 NULL, /* pSetBoundsRect */
3241 NULL, /* pSetDCBrushColor */
3242 NULL, /* pSetDCPenColor */
3243 NULL, /* pSetDIBitsToDevice */
3244 NULL, /* pSetDeviceClipping */
3245 NULL, /* pSetDeviceGammaRamp */
3246 NULL, /* pSetLayout */
3247 NULL, /* pSetMapMode */
3248 NULL, /* pSetMapperFlags */
3249 NULL, /* pSetPixel */
3250 NULL, /* pSetPolyFillMode */
3251 NULL, /* pSetROP2 */
3252 NULL, /* pSetRelAbs */
3253 NULL, /* pSetStretchBltMode */
3254 NULL, /* pSetTextAlign */
3255 NULL, /* pSetTextCharacterExtra */
3256 NULL, /* pSetTextColor */
3257 NULL, /* pSetTextJustification */
3258 NULL, /* pSetViewportExt */
3259 NULL, /* pSetViewportOrg */
3260 NULL, /* pSetWindowExt */
3261 NULL, /* pSetWindowOrg */
3262 NULL, /* pSetWorldTransform */
3263 NULL, /* pStartDoc */
3264 NULL, /* pStartPage */
3265 NULL, /* pStretchBlt */
3266 NULL, /* pStretchDIBits */
3267 NULL, /* pStrokeAndFillPath */
3268 NULL, /* pStrokePath */
3269 NULL, /* pUnrealizePalette */
3270 NULL, /* pWidenPath */
3271 glxdrv_wine_get_wgl_driver, /* wine_get_wgl_driver */
3272 GDI_PRIORITY_GRAPHICS_DRV + 20 /* priority */
3275 static struct opengl_funcs opengl_funcs =
3278 glxdrv_wglCopyContext, /* p_wglCopyContext */
3279 glxdrv_wglCreateContext, /* p_wglCreateContext */
3280 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3281 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3282 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3283 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3284 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3285 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3286 glxdrv_wglShareLists, /* p_wglShareLists */
3287 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3291 const struct gdi_dc_funcs *get_glx_driver(void)
3293 return &glxdrv_funcs;
3296 #else /* no OpenGL includes */
3298 const struct gdi_dc_funcs *get_glx_driver(void)
3300 return NULL;
3303 BOOL set_win_format( HWND hwnd, XID fbconfig_id )
3305 return FALSE;
3308 BOOL has_gl_drawable( HWND hwnd )
3310 return FALSE;
3313 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3317 void destroy_gl_drawable( HWND hwnd )
3321 #endif /* defined(SONAME_LIBGL) */