cmd: DIR command outputs free space for the path.
[wine.git] / dlls / winex11.drv / opengl.c
blobc90006488480652988c392eff3f9e4310e4bff91
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 #if 0
27 #pragma makedep unix
28 #endif
30 #include "config.h"
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <dlfcn.h>
36 #include <sys/socket.h>
37 #ifdef HAVE_SYS_UN_H
38 #include <sys/un.h>
39 #endif
41 #include "x11drv.h"
42 #include "xcomposite.h"
43 #include "winternl.h"
44 #include "wine/debug.h"
46 #ifdef SONAME_LIBGL
48 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
49 WINE_DECLARE_DEBUG_CHANNEL(winediag);
51 #include "wine/wgl.h"
52 #include "wine/wgl_driver.h"
54 typedef struct __GLXcontextRec *GLXContext;
55 typedef struct __GLXFBConfigRec *GLXFBConfig;
56 typedef XID GLXPixmap;
57 typedef XID GLXDrawable;
58 typedef XID GLXFBConfigID;
59 typedef XID GLXContextID;
60 typedef XID GLXWindow;
61 typedef XID GLXPbuffer;
63 #define GLX_USE_GL 1
64 #define GLX_BUFFER_SIZE 2
65 #define GLX_LEVEL 3
66 #define GLX_RGBA 4
67 #define GLX_DOUBLEBUFFER 5
68 #define GLX_STEREO 6
69 #define GLX_AUX_BUFFERS 7
70 #define GLX_RED_SIZE 8
71 #define GLX_GREEN_SIZE 9
72 #define GLX_BLUE_SIZE 10
73 #define GLX_ALPHA_SIZE 11
74 #define GLX_DEPTH_SIZE 12
75 #define GLX_STENCIL_SIZE 13
76 #define GLX_ACCUM_RED_SIZE 14
77 #define GLX_ACCUM_GREEN_SIZE 15
78 #define GLX_ACCUM_BLUE_SIZE 16
79 #define GLX_ACCUM_ALPHA_SIZE 17
81 #define GLX_BAD_SCREEN 1
82 #define GLX_BAD_ATTRIBUTE 2
83 #define GLX_NO_EXTENSION 3
84 #define GLX_BAD_VISUAL 4
85 #define GLX_BAD_CONTEXT 5
86 #define GLX_BAD_VALUE 6
87 #define GLX_BAD_ENUM 7
89 #define GLX_VENDOR 1
90 #define GLX_VERSION 2
91 #define GLX_EXTENSIONS 3
93 #define GLX_CONFIG_CAVEAT 0x20
94 #define GLX_DONT_CARE 0xFFFFFFFF
95 #define GLX_X_VISUAL_TYPE 0x22
96 #define GLX_TRANSPARENT_TYPE 0x23
97 #define GLX_TRANSPARENT_INDEX_VALUE 0x24
98 #define GLX_TRANSPARENT_RED_VALUE 0x25
99 #define GLX_TRANSPARENT_GREEN_VALUE 0x26
100 #define GLX_TRANSPARENT_BLUE_VALUE 0x27
101 #define GLX_TRANSPARENT_ALPHA_VALUE 0x28
102 #define GLX_WINDOW_BIT 0x00000001
103 #define GLX_PIXMAP_BIT 0x00000002
104 #define GLX_PBUFFER_BIT 0x00000004
105 #define GLX_AUX_BUFFERS_BIT 0x00000010
106 #define GLX_FRONT_LEFT_BUFFER_BIT 0x00000001
107 #define GLX_FRONT_RIGHT_BUFFER_BIT 0x00000002
108 #define GLX_BACK_LEFT_BUFFER_BIT 0x00000004
109 #define GLX_BACK_RIGHT_BUFFER_BIT 0x00000008
110 #define GLX_DEPTH_BUFFER_BIT 0x00000020
111 #define GLX_STENCIL_BUFFER_BIT 0x00000040
112 #define GLX_ACCUM_BUFFER_BIT 0x00000080
113 #define GLX_NONE 0x8000
114 #define GLX_SLOW_CONFIG 0x8001
115 #define GLX_TRUE_COLOR 0x8002
116 #define GLX_DIRECT_COLOR 0x8003
117 #define GLX_PSEUDO_COLOR 0x8004
118 #define GLX_STATIC_COLOR 0x8005
119 #define GLX_GRAY_SCALE 0x8006
120 #define GLX_STATIC_GRAY 0x8007
121 #define GLX_TRANSPARENT_RGB 0x8008
122 #define GLX_TRANSPARENT_INDEX 0x8009
123 #define GLX_VISUAL_ID 0x800B
124 #define GLX_SCREEN 0x800C
125 #define GLX_NON_CONFORMANT_CONFIG 0x800D
126 #define GLX_DRAWABLE_TYPE 0x8010
127 #define GLX_RENDER_TYPE 0x8011
128 #define GLX_X_RENDERABLE 0x8012
129 #define GLX_FBCONFIG_ID 0x8013
130 #define GLX_RGBA_TYPE 0x8014
131 #define GLX_COLOR_INDEX_TYPE 0x8015
132 #define GLX_MAX_PBUFFER_WIDTH 0x8016
133 #define GLX_MAX_PBUFFER_HEIGHT 0x8017
134 #define GLX_MAX_PBUFFER_PIXELS 0x8018
135 #define GLX_PRESERVED_CONTENTS 0x801B
136 #define GLX_LARGEST_PBUFFER 0x801C
137 #define GLX_WIDTH 0x801D
138 #define GLX_HEIGHT 0x801E
139 #define GLX_EVENT_MASK 0x801F
140 #define GLX_DAMAGED 0x8020
141 #define GLX_SAVED 0x8021
142 #define GLX_WINDOW 0x8022
143 #define GLX_PBUFFER 0x8023
144 #define GLX_PBUFFER_HEIGHT 0x8040
145 #define GLX_PBUFFER_WIDTH 0x8041
146 #define GLX_SWAP_METHOD_OML 0x8060
147 #define GLX_SWAP_EXCHANGE_OML 0x8061
148 #define GLX_SWAP_COPY_OML 0x8062
149 #define GLX_SWAP_UNDEFINED_OML 0x8063
150 #define GLX_RGBA_BIT 0x00000001
151 #define GLX_COLOR_INDEX_BIT 0x00000002
152 #define GLX_PBUFFER_CLOBBER_MASK 0x08000000
154 /** GLX_ARB_multisample */
155 #define GLX_SAMPLE_BUFFERS_ARB 100000
156 #define GLX_SAMPLES_ARB 100001
157 /** GLX_ARB_framebuffer_sRGB */
158 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
159 /** GLX_EXT_fbconfig_packed_float */
160 #define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
161 #define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT 0x00000008
162 /** GLX_ARB_create_context */
163 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
164 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
165 #define GLX_CONTEXT_FLAGS_ARB 0x2094
166 /** GLX_ARB_create_context_no_error */
167 #define GLX_CONTEXT_OPENGL_NO_ERROR_ARB 0x31B3
168 /** GLX_ARB_create_context_profile */
169 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
170 /** GLX_ATI_pixel_format_float */
171 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
172 /** GLX_ARB_pixel_format_float */
173 #define GLX_RGBA_FLOAT_BIT 0x00000004
174 #define GLX_RGBA_FLOAT_TYPE 0x20B9
175 /** GLX_MESA_query_renderer */
176 #define GLX_RENDERER_ID_MESA 0x818E
177 /** GLX_NV_float_buffer */
178 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
181 static char *glExtensions;
182 static const char *glxExtensions;
183 static char wglExtensions[4096];
184 static int glxVersion[2];
185 static int glx_opcode;
187 struct wgl_pixel_format
189 GLXFBConfig fbconfig;
190 XVisualInfo *visual;
191 int fmt_id;
192 int render_type;
193 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
196 struct wgl_context
198 HDC hdc;
199 BOOL has_been_current;
200 BOOL sharing;
201 BOOL gl3_context;
202 const struct wgl_pixel_format *fmt;
203 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
204 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
205 GLXContext ctx;
206 struct gl_drawable *drawables[2];
207 struct gl_drawable *new_drawables[2];
208 BOOL refresh_drawables;
209 struct list entry;
212 enum dc_gl_type
214 DC_GL_NONE, /* no GL support (pixel format not set yet) */
215 DC_GL_WINDOW, /* normal top-level window */
216 DC_GL_CHILD_WIN, /* child window using XComposite */
217 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
218 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
221 struct gl_drawable
223 LONG ref; /* reference count */
224 enum dc_gl_type type; /* type of GL surface */
225 GLXDrawable drawable; /* drawable for rendering with GL */
226 Window window; /* window if drawable is a GLXWindow */
227 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
228 const struct wgl_pixel_format *format; /* pixel format for the drawable */
229 SIZE pixmap_size; /* pixmap size for GLXPixmap drawables */
230 int swap_interval;
231 BOOL refresh_swap_interval;
232 BOOL mutable_pf;
235 struct wgl_pbuffer
237 struct gl_drawable *gl;
238 const struct wgl_pixel_format* fmt;
239 int width;
240 int height;
241 int* attribList;
242 int use_render_texture; /* This is also the internal texture format */
243 int texture_bind_target;
244 int texture_bpp;
245 GLint texture_format;
246 GLuint texture_target;
247 GLenum texture_type;
248 GLuint texture;
249 int texture_level;
250 GLXContext tmp_context;
251 GLXContext prev_context;
252 struct list entry;
255 enum glx_swap_control_method
257 GLX_SWAP_CONTROL_NONE,
258 GLX_SWAP_CONTROL_EXT,
259 GLX_SWAP_CONTROL_SGI,
260 GLX_SWAP_CONTROL_MESA
263 /* X context to associate a struct gl_drawable to an hwnd */
264 static XContext gl_hwnd_context;
265 /* X context to associate a struct gl_drawable to a pbuffer hdc */
266 static XContext gl_pbuffer_context;
268 static struct list context_list = LIST_INIT( context_list );
269 static struct list pbuffer_list = LIST_INIT( pbuffer_list );
270 static struct wgl_pixel_format *pixel_formats;
271 static int nb_pixel_formats, nb_onscreen_formats;
272 static BOOL use_render_texture_emulation = TRUE;
274 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
275 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
276 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
277 static BOOL has_swap_control_tear = FALSE;
278 static BOOL has_swap_method = FALSE;
280 static pthread_mutex_t context_mutex = PTHREAD_MUTEX_INITIALIZER;
282 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
284 static struct opengl_funcs opengl_funcs;
286 #define USE_GL_FUNC(name) #name,
287 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
288 #undef USE_GL_FUNC
290 static void X11DRV_WineGL_LoadExtensions(void);
291 static void init_pixel_formats( Display *display );
292 static BOOL glxRequireVersion(int requiredVersion);
294 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
295 TRACE( "size %u version %u flags %u type %u color %u %u,%u,%u,%u "
296 "accum %u depth %u stencil %u aux %u ",
297 ppfd->nSize, ppfd->nVersion, (int)ppfd->dwFlags, ppfd->iPixelType,
298 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
299 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
300 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
301 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
302 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
303 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
304 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
305 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
306 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
307 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
308 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
309 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
310 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
311 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
312 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
313 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
315 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
316 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
317 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
318 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
320 #undef TEST_AND_DUMP
321 TRACE("\n");
324 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
325 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
327 /* GLX 1.0 */
328 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
329 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
330 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
331 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
332 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
333 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
334 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
335 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
336 static GLXContext (*pglXGetCurrentContext)( void );
337 static GLXDrawable (*pglXGetCurrentDrawable)( void );
339 /* GLX 1.1 */
340 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
341 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
342 static const char *(*pglXGetClientString)( Display *dpy, int name );
344 /* GLX 1.3 */
345 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
346 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
347 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
348 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
349 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
350 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
351 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
352 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
353 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
354 static GLXPixmap (*pglXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list );
355 static void (*pglXDestroyPixmap)( Display *dpy, GLXPixmap pixmap );
356 static GLXWindow (*pglXCreateWindow)( Display *dpy, GLXFBConfig config, Window win, const int *attrib_list );
357 static void (*pglXDestroyWindow)( Display *dpy, GLXWindow win );
359 /* GLX Extensions */
360 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
361 static void* (*pglXGetProcAddressARB)(const GLubyte *);
362 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
363 static int (*pglXSwapIntervalSGI)(int);
365 /* NV GLX Extension */
366 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
367 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
369 /* MESA GLX Extensions */
370 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
371 static int (*pglXSwapIntervalMESA)(unsigned int interval);
372 static Bool (*pglXQueryCurrentRendererIntegerMESA)(int attribute, unsigned int *value);
373 static const char *(*pglXQueryCurrentRendererStringMESA)(int attribute);
374 static Bool (*pglXQueryRendererIntegerMESA)(Display *dpy, int screen, int renderer, int attribute, unsigned int *value);
375 static const char *(*pglXQueryRendererStringMESA)(Display *dpy, int screen, int renderer, int attribute);
377 /* OpenML GLX Extensions */
378 static Bool (*pglXWaitForSbcOML)( Display *dpy, GLXDrawable drawable,
379 INT64 target_sbc, INT64 *ust, INT64 *msc, INT64 *sbc );
380 static INT64 (*pglXSwapBuffersMscOML)( Display *dpy, GLXDrawable drawable,
381 INT64 target_msc, INT64 divisor, INT64 remainder );
383 /* Standard OpenGL */
384 static void (*pglFinish)(void);
385 static void (*pglFlush)(void);
386 static const GLubyte *(*pglGetString)(GLenum name);
388 static void wglFinish(void);
389 static void wglFlush(void);
390 static const GLubyte *wglGetString(GLenum name);
392 /* check if the extension is present in the list */
393 static BOOL has_extension( const char *list, const char *ext )
395 size_t len = strlen( ext );
397 while (list)
399 while (*list == ' ') list++;
400 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
401 list = strchr( list, ' ' );
403 return FALSE;
406 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
408 /* In the future we might want to find the exact X or GLX error to report back to the app */
409 if (event->request_code != glx_opcode)
410 return 0;
411 return 1;
414 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
416 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
418 int screen = DefaultScreen(gdi_display);
419 Window win = 0, root = 0;
420 const char *gl_version;
421 const char *gl_renderer;
422 const char* str;
423 BOOL glx_direct;
424 XVisualInfo *vis;
425 GLXContext ctx = NULL;
426 XSetWindowAttributes attr;
427 BOOL ret = FALSE;
428 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
430 attr.override_redirect = True;
431 attr.colormap = None;
432 attr.border_pixel = 0;
434 vis = pglXChooseVisual(gdi_display, screen, attribList);
435 if (vis) {
436 #ifdef __i386__
437 WORD old_fs, new_fs;
438 __asm__( "mov %%fs,%0" : "=r" (old_fs) );
439 /* Create a GLX Context. Without one we can't query GL information */
440 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
441 __asm__( "mov %%fs,%0" : "=r" (new_fs) );
442 __asm__( "mov %0,%%fs" :: "r" (old_fs) );
443 if (old_fs != new_fs)
445 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
446 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
447 goto done;
449 #else
450 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
451 #endif
453 if (!ctx) goto done;
455 root = RootWindow( gdi_display, vis->screen );
456 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
457 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
458 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
459 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
460 XMapWindow( gdi_display, win );
461 else
462 win = root;
464 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
466 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
467 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
468 goto done;
470 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
471 gl_version = (const char *)opengl_funcs.gl.p_glGetString(GL_VERSION);
472 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
473 glExtensions = malloc( strlen(str) + sizeof(legacy_extensions) );
474 strcpy(glExtensions, str);
475 strcat(glExtensions, legacy_extensions);
477 /* Get the common GLX version supported by GLX client and server ( major/minor) */
478 pglXQueryVersion(gdi_display, &glxVersion[0], &glxVersion[1]);
480 glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
481 glx_direct = pglXIsDirect(gdi_display, ctx);
483 TRACE("GL version : %s.\n", gl_version);
484 TRACE("GL renderer : %s.\n", gl_renderer);
485 TRACE("GLX version : %d.%d.\n", glxVersion[0], glxVersion[1]);
486 TRACE("Server GLX version : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VERSION));
487 TRACE("Server GLX vendor: : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VENDOR));
488 TRACE("Client GLX version : %s.\n", pglXGetClientString(gdi_display, GLX_VERSION));
489 TRACE("Client GLX vendor: : %s.\n", pglXGetClientString(gdi_display, GLX_VENDOR));
490 TRACE("Direct rendering enabled: %s\n", glx_direct ? "True" : "False");
492 if(!glx_direct)
494 int fd = ConnectionNumber(gdi_display);
495 struct sockaddr_un uaddr;
496 unsigned int uaddrlen = sizeof(struct sockaddr_un);
498 /* In general indirect rendering on a local X11 server indicates a driver problem.
499 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
501 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
502 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
503 "haven't been installed correctly (using GL renderer %s, version %s).\n",
504 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
505 debugstr_a(gl_version));
507 else
509 /* In general you would expect that if direct rendering is returned, that you receive hardware
510 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
511 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
512 * software rendering, it shows direct rendering.
514 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
515 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
516 * it shows 'Mesa X11'.
518 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
519 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
520 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
521 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
522 debugstr_a(gl_version));
524 ret = TRUE;
526 done:
527 if(vis) XFree(vis);
528 if(ctx) {
529 pglXMakeCurrent(gdi_display, None, NULL);
530 pglXDestroyContext(gdi_display, ctx);
532 if (win != root) XDestroyWindow( gdi_display, win );
533 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
534 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
535 return ret;
538 static void *opengl_handle;
540 static void init_opengl(void)
542 int error_base, event_base;
543 unsigned int i;
545 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
546 and include all dependencies */
547 opengl_handle = dlopen( SONAME_LIBGL, RTLD_NOW | RTLD_GLOBAL );
548 if (opengl_handle == NULL)
550 ERR( "Failed to load libGL: %s\n", dlerror() );
551 ERR( "OpenGL support is disabled.\n");
552 return;
555 for (i = 0; i < ARRAY_SIZE( opengl_func_names ); i++)
557 if (!(((void **)&opengl_funcs.gl)[i] = dlsym( opengl_handle, opengl_func_names[i] )))
559 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
560 goto failed;
564 /* redirect some standard OpenGL functions */
565 #define REDIRECT(func) \
566 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
567 REDIRECT( glFinish );
568 REDIRECT( glFlush );
569 REDIRECT( glGetString );
570 #undef REDIRECT
572 pglXGetProcAddressARB = dlsym(opengl_handle, "glXGetProcAddressARB");
573 if (pglXGetProcAddressARB == NULL) {
574 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
575 goto failed;
578 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
580 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
581 goto failed; \
582 } while(0)
584 /* GLX 1.0 */
585 LOAD_FUNCPTR(glXChooseVisual);
586 LOAD_FUNCPTR(glXCopyContext);
587 LOAD_FUNCPTR(glXCreateContext);
588 LOAD_FUNCPTR(glXGetCurrentContext);
589 LOAD_FUNCPTR(glXGetCurrentDrawable);
590 LOAD_FUNCPTR(glXDestroyContext);
591 LOAD_FUNCPTR(glXIsDirect);
592 LOAD_FUNCPTR(glXMakeCurrent);
593 LOAD_FUNCPTR(glXSwapBuffers);
594 LOAD_FUNCPTR(glXQueryVersion);
596 /* GLX 1.1 */
597 LOAD_FUNCPTR(glXGetClientString);
598 LOAD_FUNCPTR(glXQueryExtensionsString);
599 LOAD_FUNCPTR(glXQueryServerString);
601 /* GLX 1.3 */
602 LOAD_FUNCPTR(glXCreatePbuffer);
603 LOAD_FUNCPTR(glXCreateNewContext);
604 LOAD_FUNCPTR(glXDestroyPbuffer);
605 LOAD_FUNCPTR(glXMakeContextCurrent);
606 LOAD_FUNCPTR(glXGetFBConfigs);
607 LOAD_FUNCPTR(glXCreatePixmap);
608 LOAD_FUNCPTR(glXDestroyPixmap);
609 LOAD_FUNCPTR(glXCreateWindow);
610 LOAD_FUNCPTR(glXDestroyWindow);
611 #undef LOAD_FUNCPTR
613 /* It doesn't matter if these fail. They'll only be used if the driver reports
614 the associated extension is available (and if a driver reports the extension
615 is available but fails to provide the functions, it's quite broken) */
616 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
617 /* ARB GLX Extension */
618 LOAD_FUNCPTR(glXCreateContextAttribsARB);
619 /* EXT GLX Extension */
620 LOAD_FUNCPTR(glXSwapIntervalEXT);
621 /* MESA GLX Extension */
622 LOAD_FUNCPTR(glXSwapIntervalMESA);
623 /* SGI GLX Extension */
624 LOAD_FUNCPTR(glXSwapIntervalSGI);
625 /* NV GLX Extension */
626 LOAD_FUNCPTR(glXAllocateMemoryNV);
627 LOAD_FUNCPTR(glXFreeMemoryNV);
628 #undef LOAD_FUNCPTR
630 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
632 if (XQueryExtension( gdi_display, "GLX", &glx_opcode, &event_base, &error_base ))
634 TRACE("GLX is up and running error_base = %d\n", error_base);
635 } else {
636 ERR( "GLX extension is missing, disabling OpenGL.\n" );
637 goto failed;
639 gl_hwnd_context = XUniqueContext();
640 gl_pbuffer_context = XUniqueContext();
642 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
643 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
644 * rendering is used.
646 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
647 * depends on the reported GLX client / server version and on the client / server extension list.
648 * Those don't have to be the same.
650 * In general the server GLX information lists the capabilities in case of indirect rendering.
651 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
652 * available and in that case the client GLX informat can be used.
653 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
654 * in the GLX version/extension list. When a program does this it works for certain for both
655 * direct and indirect rendering.
657 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
658 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
659 * extension list which causes this extension not to be listed. (Wine requires this extension).
660 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
661 * pbuffers is around.
663 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
664 * the ATI drivers and from then on use GLX client information for them.
667 if(glxRequireVersion(3)) {
668 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
669 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
670 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
671 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
672 } else if (has_extension( glxExtensions, "GLX_SGIX_fbconfig")) {
673 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
674 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
675 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
677 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
678 * enable this function when the Xserver understand GLX 1.3 or newer
680 pglXQueryDrawable = NULL;
681 } else if(strcmp("ATI", pglXGetClientString(gdi_display, GLX_VENDOR)) == 0) {
682 TRACE("Overriding ATI GLX capabilities!\n");
683 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
684 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
685 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
686 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
688 /* Use client GLX information in case of the ATI drivers. We override the
689 * capabilities over here and not somewhere else as ATI might better their
690 * life in the future. In case they release proper drivers this block of
691 * code won't be called. */
692 glxExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
693 } else {
694 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n",
695 pglXQueryServerString(gdi_display, DefaultScreen(gdi_display), GLX_VERSION));
698 if (has_extension( glxExtensions, "GLX_MESA_copy_sub_buffer")) {
699 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
702 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
704 pglXQueryCurrentRendererIntegerMESA = pglXGetProcAddressARB(
705 (const GLubyte *)"glXQueryCurrentRendererIntegerMESA" );
706 pglXQueryCurrentRendererStringMESA = pglXGetProcAddressARB(
707 (const GLubyte *)"glXQueryCurrentRendererStringMESA" );
708 pglXQueryRendererIntegerMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererIntegerMESA" );
709 pglXQueryRendererStringMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererStringMESA" );
712 if (has_extension( glxExtensions, "GLX_OML_sync_control" ))
714 pglXWaitForSbcOML = pglXGetProcAddressARB( (const GLubyte *)"glXWaitForSbcOML" );
715 pglXSwapBuffersMscOML = pglXGetProcAddressARB( (const GLubyte *)"glXSwapBuffersMscOML" );
718 X11DRV_WineGL_LoadExtensions();
719 init_pixel_formats( gdi_display );
720 return;
722 failed:
723 dlclose(opengl_handle);
724 opengl_handle = NULL;
727 static BOOL has_opengl(void)
729 static pthread_once_t init_once = PTHREAD_ONCE_INIT;
731 return !pthread_once( &init_once, init_opengl );
734 static const char *debugstr_fbconfig( GLXFBConfig fbconfig )
736 int id, visual, drawable;
738 if (pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_FBCONFIG_ID, &id ))
739 return "*** invalid fbconfig";
740 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_VISUAL_ID, &visual );
741 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &drawable );
742 return wine_dbg_sprintf( "fbconfig %#x visual id %#x drawable type %#x", id, visual, drawable );
745 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
746 int nAttribs = 0;
747 unsigned cur = 0;
748 int attr, pop;
749 int drawattrib = 0;
750 int nvfloatattrib = GLX_DONT_CARE;
751 int pixelattrib = GLX_DONT_CARE;
753 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
754 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
755 while (iWGLAttr && 0 != iWGLAttr[cur]) {
756 attr = iWGLAttr[cur];
757 TRACE("pAttr[%d] = %x\n", cur, attr);
758 pop = iWGLAttr[++cur];
760 switch (attr) {
761 case WGL_AUX_BUFFERS_ARB:
762 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
763 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
764 break;
765 case WGL_COLOR_BITS_ARB:
766 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
767 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
768 break;
769 case WGL_BLUE_BITS_ARB:
770 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
771 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
772 break;
773 case WGL_RED_BITS_ARB:
774 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
775 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
776 break;
777 case WGL_GREEN_BITS_ARB:
778 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
779 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
780 break;
781 case WGL_ALPHA_BITS_ARB:
782 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
783 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
784 break;
785 case WGL_DEPTH_BITS_ARB:
786 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
787 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
788 break;
789 case WGL_STENCIL_BITS_ARB:
790 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
791 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
792 break;
793 case WGL_DOUBLE_BUFFER_ARB:
794 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
795 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
796 break;
797 case WGL_STEREO_ARB:
798 PUSH2(oGLXAttr, GLX_STEREO, pop);
799 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
800 break;
802 case WGL_PIXEL_TYPE_ARB:
803 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
804 switch (pop) {
805 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
806 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
807 /* 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 */
808 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
809 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
810 default:
811 ERR("unexpected PixelType(%x)\n", pop);
813 break;
815 case WGL_SUPPORT_GDI_ARB:
816 /* This flag is set in a pixel format */
817 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
818 break;
820 case WGL_DRAW_TO_BITMAP_ARB:
821 /* This flag is set in a pixel format */
822 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
823 break;
825 case WGL_DRAW_TO_WINDOW_ARB:
826 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
827 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
828 if (pop) {
829 drawattrib |= GLX_WINDOW_BIT;
831 break;
833 case WGL_DRAW_TO_PBUFFER_ARB:
834 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
835 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
836 if (pop) {
837 drawattrib |= GLX_PBUFFER_BIT;
839 break;
841 case WGL_ACCELERATION_ARB:
842 /* This flag is set in a pixel format */
843 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
844 break;
846 case WGL_SUPPORT_OPENGL_ARB:
847 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
848 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
849 break;
851 case WGL_SWAP_METHOD_ARB:
852 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
853 if (has_swap_method)
855 switch (pop)
857 case WGL_SWAP_EXCHANGE_ARB:
858 pop = GLX_SWAP_EXCHANGE_OML;
859 break;
860 case WGL_SWAP_COPY_ARB:
861 pop = GLX_SWAP_COPY_OML;
862 break;
863 case WGL_SWAP_UNDEFINED_ARB:
864 pop = GLX_SWAP_UNDEFINED_OML;
865 break;
866 default:
867 ERR("Unexpected swap method %#x.\n", pop);
868 pop = GLX_DONT_CARE;
870 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
872 else
874 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
876 break;
878 case WGL_PBUFFER_LARGEST_ARB:
879 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
880 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
881 break;
883 case WGL_SAMPLE_BUFFERS_ARB:
884 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
885 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
886 break;
888 case WGL_SAMPLES_ARB:
889 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
890 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
891 break;
893 case WGL_TEXTURE_FORMAT_ARB:
894 case WGL_TEXTURE_TARGET_ARB:
895 case WGL_MIPMAP_TEXTURE_ARB:
896 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur - 1], iWGLAttr[cur]);
897 if (NULL == pbuf) {
898 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
900 if (!use_render_texture_emulation) {
901 if (WGL_NO_TEXTURE_ARB != pop) {
902 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
903 return -1; /** error: don't support it */
904 } else {
905 drawattrib |= GLX_PBUFFER_BIT;
908 break ;
909 case WGL_FLOAT_COMPONENTS_NV:
910 nvfloatattrib = pop;
911 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
912 break ;
913 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
914 case WGL_BIND_TO_TEXTURE_RGB_ARB:
915 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
916 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
917 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
918 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
919 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
920 /** cannot be converted, see direct handling on
921 * - wglGetPixelFormatAttribivARB
922 * TODO: wglChoosePixelFormat
924 break ;
925 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
926 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
927 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
928 break ;
930 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
931 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
932 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
933 break ;
934 default:
935 FIXME("unsupported %x WGL Attribute\n", attr);
936 break;
938 ++cur;
941 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
942 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
943 * pixmap and pbuffer formats appear as well. */
944 if (!drawattrib) drawattrib = GLX_DONT_CARE;
945 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
946 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
948 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
949 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
950 * GLX_DONT_CARE unless it is overridden. */
951 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
952 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
954 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
955 if (has_extension(glxExtensions, "GLX_NV_float_buffer")) {
956 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
957 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
960 return nAttribs;
963 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
965 int render_type, render_type_bit;
966 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
967 switch(render_type_bit)
969 case GLX_RGBA_BIT:
970 render_type = GLX_RGBA_TYPE;
971 break;
972 case GLX_COLOR_INDEX_BIT:
973 render_type = GLX_COLOR_INDEX_TYPE;
974 break;
975 case GLX_RGBA_FLOAT_BIT:
976 render_type = GLX_RGBA_FLOAT_TYPE;
977 break;
978 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
979 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
980 break;
981 default:
982 ERR("Unknown render_type: %x\n", render_type_bit);
983 render_type = 0;
985 return render_type;
988 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
989 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
991 int dbuf, value;
992 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
993 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
995 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
996 * the GLX_PIXMAP_BIT set. */
997 return !dbuf && (value & GLX_PIXMAP_BIT);
1000 static void init_pixel_formats( Display *display )
1002 struct wgl_pixel_format *list;
1003 int size = 0, onscreen_size = 0;
1004 int fmt_id, nCfgs, i, run, bmp_formats;
1005 GLXFBConfig* cfgs;
1006 XVisualInfo *visinfo;
1008 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1009 if (NULL == cfgs || 0 == nCfgs) {
1010 if(cfgs != NULL) XFree(cfgs);
1011 ERR("glXChooseFBConfig returns NULL\n");
1012 return;
1015 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1016 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1017 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1018 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1020 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1022 for(i=0, bmp_formats=0; i<nCfgs; i++)
1024 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1025 bmp_formats++;
1027 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1029 list = calloc( 1, (nCfgs + bmp_formats) * sizeof(*list) );
1031 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1032 * Do this as GLX doesn't guarantee that the list is sorted */
1033 for(run=0; run < 2; run++)
1035 for(i=0; i<nCfgs; i++) {
1036 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1037 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1039 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1040 * The second run we only set offscreen formats. */
1041 if(!run && visinfo)
1043 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1044 * The contents is copied to the destination using XCopyArea. For the copying to work
1045 * the depth of the source and destination window should be the same. In general this should
1046 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1047 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1048 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1049 * list formats with the same depth. */
1050 if(visinfo->depth != default_visual.depth)
1052 XFree(visinfo);
1053 continue;
1056 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1057 list[size].fbconfig = cfgs[i];
1058 list[size].visual = visinfo;
1059 list[size].fmt_id = fmt_id;
1060 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1061 list[size].dwFlags = 0;
1062 size++;
1063 onscreen_size++;
1065 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1066 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1068 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1069 list[size].fbconfig = cfgs[i];
1070 list[size].visual = visinfo;
1071 list[size].fmt_id = fmt_id;
1072 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1073 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1074 size++;
1075 onscreen_size++;
1077 } else if(run && !visinfo) {
1078 int window_drawable=0;
1079 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1081 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1082 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1083 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1084 * likely make our child window opengl rendering more complicated since likely you can't use
1085 * XCopyArea on a GLX Window.
1086 * For now ignore fbconfigs which are window drawable but lack a visual. */
1087 if(window_drawable & GLX_WINDOW_BIT)
1089 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1090 continue;
1093 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1094 list[size].fbconfig = cfgs[i];
1095 list[size].fmt_id = fmt_id;
1096 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1097 list[size].dwFlags = 0;
1098 size++;
1100 else if (visinfo) XFree(visinfo);
1104 XFree(cfgs);
1106 pixel_formats = list;
1107 nb_pixel_formats = size;
1108 nb_onscreen_formats = onscreen_size;
1111 static inline BOOL is_valid_pixel_format( int format )
1113 return format > 0 && format <= nb_pixel_formats;
1116 static inline BOOL is_onscreen_pixel_format( int format )
1118 return format > 0 && format <= nb_onscreen_formats;
1121 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1123 return format - pixel_formats + 1;
1126 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1127 * In our WGL implementation we only support a subset of these formats namely the format of
1128 * Wine's main visual and offscreen formats (if they are available).
1129 * This function converts a WGL format to its corresponding GLX one.
1131 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1133 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1134 * iPixelFormat in case of probing the number of pixelformats.
1136 if (is_valid_pixel_format( iPixelFormat ) &&
1137 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1138 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1139 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1140 return &pixel_formats[iPixelFormat-1];
1142 return NULL;
1145 static struct gl_drawable *grab_gl_drawable( struct gl_drawable *gl )
1147 InterlockedIncrement( &gl->ref );
1148 return gl;
1151 static void release_gl_drawable( struct gl_drawable *gl )
1153 if (!gl) return;
1154 if (InterlockedDecrement( &gl->ref )) return;
1155 switch (gl->type)
1157 case DC_GL_WINDOW:
1158 case DC_GL_CHILD_WIN:
1159 TRACE( "destroying %lx drawable %lx\n", gl->window, gl->drawable );
1160 pglXDestroyWindow( gdi_display, gl->drawable );
1161 XDestroyWindow( gdi_display, gl->window );
1162 break;
1163 case DC_GL_PIXMAP_WIN:
1164 TRACE( "destroying pixmap %lx drawable %lx\n", gl->pixmap, gl->drawable );
1165 pglXDestroyPixmap( gdi_display, gl->drawable );
1166 XFreePixmap( gdi_display, gl->pixmap );
1167 break;
1168 case DC_GL_PBUFFER:
1169 TRACE( "destroying pbuffer drawable %lx\n", gl->drawable );
1170 pglXDestroyPbuffer( gdi_display, gl->drawable );
1171 break;
1172 default:
1173 break;
1175 free( gl );
1178 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1179 static void mark_drawable_dirty( struct gl_drawable *old, struct gl_drawable *new )
1181 struct wgl_context *ctx;
1183 pthread_mutex_lock( &context_mutex );
1184 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1186 if (old == ctx->drawables[0] || old == ctx->new_drawables[0])
1188 release_gl_drawable( ctx->new_drawables[0] );
1189 ctx->new_drawables[0] = grab_gl_drawable( new );
1191 if (old == ctx->drawables[1] || old == ctx->new_drawables[1])
1193 release_gl_drawable( ctx->new_drawables[1] );
1194 ctx->new_drawables[1] = grab_gl_drawable( new );
1197 pthread_mutex_unlock( &context_mutex );
1200 /* Given the current context, make sure its drawable is sync'd */
1201 static inline void sync_context(struct wgl_context *context)
1203 BOOL refresh = FALSE;
1204 struct gl_drawable *old[2] = { NULL };
1206 pthread_mutex_lock( &context_mutex );
1207 if (context->new_drawables[0])
1209 old[0] = context->drawables[0];
1210 context->drawables[0] = context->new_drawables[0];
1211 context->new_drawables[0] = NULL;
1212 refresh = TRUE;
1214 if (context->new_drawables[1])
1216 old[1] = context->drawables[1];
1217 context->drawables[1] = context->new_drawables[1];
1218 context->new_drawables[1] = NULL;
1219 refresh = TRUE;
1221 if (refresh)
1223 if (glxRequireVersion(3))
1224 pglXMakeContextCurrent(gdi_display, context->drawables[0]->drawable,
1225 context->drawables[1]->drawable, context->ctx);
1226 else
1227 pglXMakeCurrent(gdi_display, context->drawables[0]->drawable, context->ctx);
1228 release_gl_drawable( old[0] );
1229 release_gl_drawable( old[1] );
1231 pthread_mutex_unlock( &context_mutex );
1234 static BOOL set_swap_interval(GLXDrawable drawable, int interval)
1236 BOOL ret = TRUE;
1238 switch (swap_control_method)
1240 case GLX_SWAP_CONTROL_EXT:
1241 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1242 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1243 XSync(gdi_display, False);
1244 ret = !X11DRV_check_error();
1245 break;
1247 case GLX_SWAP_CONTROL_MESA:
1248 ret = !pglXSwapIntervalMESA(interval);
1249 break;
1251 case GLX_SWAP_CONTROL_SGI:
1252 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1253 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1254 * value to be an error. Just silently ignore the request for now.
1256 if (!interval)
1257 WARN("Request to disable vertical sync is not handled\n");
1258 else
1259 ret = !pglXSwapIntervalSGI(interval);
1260 break;
1262 case GLX_SWAP_CONTROL_NONE:
1263 /* Unlikely to happen on modern GLX implementations */
1264 WARN("Request to adjust swap interval is not handled\n");
1265 break;
1268 return ret;
1271 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1273 struct gl_drawable *gl;
1275 pthread_mutex_lock( &context_mutex );
1276 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1277 gl = grab_gl_drawable( gl );
1278 else if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
1279 gl = grab_gl_drawable( gl );
1280 else
1281 gl = NULL;
1282 pthread_mutex_unlock( &context_mutex );
1283 return gl;
1286 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1288 GLXContext ctx;
1290 if(context->gl3_context)
1292 if(context->numAttribs)
1293 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, context->attribList);
1294 else
1295 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, NULL);
1297 else if(context->fmt->visual)
1298 ctx = pglXCreateContext(gdi_display, context->fmt->visual, shareList, GL_TRUE);
1299 else /* Create a GLX Context for a pbuffer */
1300 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1302 return ctx;
1306 /***********************************************************************
1307 * create_gl_drawable
1309 static struct gl_drawable *create_gl_drawable( HWND hwnd, const struct wgl_pixel_format *format, BOOL known_child,
1310 BOOL mutable_pf )
1312 struct gl_drawable *gl, *prev;
1313 XVisualInfo *visual = format->visual;
1314 RECT rect;
1315 int width, height;
1317 NtUserGetClientRect( hwnd, &rect );
1318 width = min( max( 1, rect.right ), 65535 );
1319 height = min( max( 1, rect.bottom ), 65535 );
1321 if (!(gl = calloc( 1, sizeof(*gl) ))) return NULL;
1323 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1324 * there is no way to query it, so we have to store it here.
1326 gl->swap_interval = 1;
1327 gl->refresh_swap_interval = TRUE;
1328 gl->format = format;
1329 gl->ref = 1;
1330 gl->mutable_pf = mutable_pf;
1332 if (!known_child && !NtUserGetWindowRelative( hwnd, GW_CHILD ) &&
1333 NtUserGetAncestor( hwnd, GA_PARENT ) == NtUserGetDesktopWindow()) /* childless top-level window */
1335 gl->type = DC_GL_WINDOW;
1336 gl->window = create_client_window( hwnd, visual );
1337 if (gl->window)
1338 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1339 TRACE( "%p created client %lx drawable %lx\n", hwnd, gl->window, gl->drawable );
1341 #ifdef SONAME_LIBXCOMPOSITE
1342 else if(usexcomposite)
1344 gl->type = DC_GL_CHILD_WIN;
1345 gl->window = create_client_window( hwnd, visual );
1346 if (gl->window)
1348 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1349 pXCompositeRedirectWindow( gdi_display, gl->window, CompositeRedirectManual );
1351 TRACE( "%p created child %lx drawable %lx\n", hwnd, gl->window, gl->drawable );
1353 #endif
1354 else
1356 static unsigned int once;
1358 if (!once++)
1359 ERR_(winediag)("XComposite is not available, using GLXPixmap hack.\n");
1360 WARN("XComposite is not available, using GLXPixmap hack.\n");
1362 gl->type = DC_GL_PIXMAP_WIN;
1363 gl->pixmap = XCreatePixmap( gdi_display, root_window, width, height, visual->depth );
1364 if (gl->pixmap)
1366 gl->drawable = pglXCreatePixmap( gdi_display, gl->format->fbconfig, gl->pixmap, NULL );
1367 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1368 gl->pixmap_size.cx = width;
1369 gl->pixmap_size.cy = height;
1373 if (!gl->drawable)
1375 free( gl );
1376 return NULL;
1379 pthread_mutex_lock( &context_mutex );
1380 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1382 gl->swap_interval = prev->swap_interval;
1383 release_gl_drawable( prev );
1385 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)grab_gl_drawable(gl) );
1386 pthread_mutex_unlock( &context_mutex );
1387 return gl;
1391 /***********************************************************************
1392 * set_win_format
1394 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format, BOOL internal )
1396 struct gl_drawable *old, *gl;
1398 if (!format->visual) return FALSE;
1400 old = get_gl_drawable( hwnd, 0 );
1402 if (!(gl = create_gl_drawable( hwnd, format, FALSE, internal )))
1404 release_gl_drawable( old );
1405 return FALSE;
1408 TRACE( "created GL drawable %lx for win %p %s\n",
1409 gl->drawable, hwnd, debugstr_fbconfig( format->fbconfig ));
1411 if (old)
1412 mark_drawable_dirty( old, gl );
1414 XFlush( gdi_display );
1415 release_gl_drawable( gl );
1416 release_gl_drawable( old );
1418 win32u_set_window_pixel_format( hwnd, pixel_format_index( format ), internal );
1419 return TRUE;
1423 static BOOL set_pixel_format( HDC hdc, int format, BOOL internal )
1425 const struct wgl_pixel_format *fmt;
1426 int value;
1427 HWND hwnd = NtUserWindowFromDC( hdc );
1428 int prev;
1430 TRACE("(%p,%d)\n", hdc, format);
1432 if (!hwnd || hwnd == NtUserGetDesktopWindow())
1434 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1435 return FALSE;
1438 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1439 if (!fmt)
1441 ERR( "Invalid format %d\n", format );
1442 return FALSE;
1445 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1446 if (!(value & GLX_WINDOW_BIT))
1448 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1449 return FALSE;
1452 /* Even for internal pixel format fail setting it if the app has already set a
1453 * different pixel format. Let wined3d create a backup GL context instead.
1454 * Switching pixel format involves drawable recreation and is much more expensive
1455 * than blitting from backup context. */
1456 if ((prev = win32u_get_window_pixel_format( hwnd )))
1457 return prev == format;
1459 return set_win_format( hwnd, fmt, internal );
1463 /***********************************************************************
1464 * sync_gl_drawable
1466 void sync_gl_drawable( HWND hwnd, BOOL known_child )
1468 struct gl_drawable *old, *new;
1470 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1472 switch (old->type)
1474 case DC_GL_WINDOW:
1475 if (!known_child) break; /* Still a childless top-level window */
1476 /* fall through */
1477 case DC_GL_PIXMAP_WIN:
1478 if (!(new = create_gl_drawable( hwnd, old->format, known_child, old->mutable_pf ))) break;
1479 mark_drawable_dirty( old, new );
1480 XFlush( gdi_display );
1481 TRACE( "Recreated GL drawable %lx to replace %lx\n", new->drawable, old->drawable );
1482 release_gl_drawable( new );
1483 break;
1484 default:
1485 break;
1487 release_gl_drawable( old );
1491 /***********************************************************************
1492 * set_gl_drawable_parent
1494 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1496 struct gl_drawable *old, *new;
1498 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1500 TRACE( "setting drawable %lx parent %p\n", old->drawable, parent );
1502 switch (old->type)
1504 case DC_GL_WINDOW:
1505 break;
1506 case DC_GL_CHILD_WIN:
1507 case DC_GL_PIXMAP_WIN:
1508 if (parent == NtUserGetDesktopWindow()) break;
1509 /* fall through */
1510 default:
1511 release_gl_drawable( old );
1512 return;
1515 if ((new = create_gl_drawable( hwnd, old->format, FALSE, old->mutable_pf )))
1517 mark_drawable_dirty( old, new );
1518 release_gl_drawable( new );
1520 else
1522 destroy_gl_drawable( hwnd );
1523 win32u_set_window_pixel_format( hwnd, 0, FALSE );
1525 release_gl_drawable( old );
1529 /***********************************************************************
1530 * destroy_gl_drawable
1532 void destroy_gl_drawable( HWND hwnd )
1534 struct gl_drawable *gl;
1536 pthread_mutex_lock( &context_mutex );
1537 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1539 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1540 release_gl_drawable( gl );
1542 pthread_mutex_unlock( &context_mutex );
1547 * glxdrv_DescribePixelFormat
1549 * Get the pixel-format descriptor associated to the given id
1551 static int describe_pixel_format( int iPixelFormat, PIXELFORMATDESCRIPTOR *ppfd, BOOL allow_offscreen )
1553 /*XVisualInfo *vis;*/
1554 int value;
1555 int rb,gb,bb,ab;
1556 const struct wgl_pixel_format *fmt;
1558 if (!has_opengl()) return 0;
1560 /* 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 */
1561 fmt = get_pixel_format(gdi_display, iPixelFormat, allow_offscreen);
1562 if (!fmt) {
1563 WARN("unexpected format %d\n", iPixelFormat);
1564 return 0;
1567 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1568 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1569 ppfd->nVersion = 1;
1571 /* These flags are always the same... */
1572 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1573 /* Now the flags extracted from the Visual */
1575 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1576 if(value & GLX_WINDOW_BIT)
1577 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1579 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1580 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1581 * 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
1582 * offered the GDI bit either. */
1583 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1585 /* PFD_GENERIC_FORMAT - gdi software rendering
1586 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1587 * none set - full hardware accelerated by a ICD
1589 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1590 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1592 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1593 if (value) {
1594 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1595 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1597 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1599 /* Pixel type */
1600 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1601 if (value & GLX_RGBA_BIT)
1602 ppfd->iPixelType = PFD_TYPE_RGBA;
1603 else
1604 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1606 /* Color bits */
1607 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1608 ppfd->cColorBits = value;
1610 /* Red, green, blue and alpha bits / shifts */
1611 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1612 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1613 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1614 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1615 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1617 ppfd->cBlueBits = bb;
1618 ppfd->cBlueShift = 0;
1619 ppfd->cGreenBits = gb;
1620 ppfd->cGreenShift = bb;
1621 ppfd->cRedBits = rb;
1622 ppfd->cRedShift = gb + bb;
1623 ppfd->cAlphaBits = ab;
1624 if (ab)
1625 ppfd->cAlphaShift = rb + gb + bb;
1626 else
1627 ppfd->cAlphaShift = 0;
1628 } else {
1629 ppfd->cRedBits = 0;
1630 ppfd->cRedShift = 0;
1631 ppfd->cBlueBits = 0;
1632 ppfd->cBlueShift = 0;
1633 ppfd->cGreenBits = 0;
1634 ppfd->cGreenShift = 0;
1635 ppfd->cAlphaBits = 0;
1636 ppfd->cAlphaShift = 0;
1639 /* Accum RGBA bits */
1640 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1641 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1642 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1643 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1645 ppfd->cAccumBits = rb+gb+bb+ab;
1646 ppfd->cAccumRedBits = rb;
1647 ppfd->cAccumGreenBits = gb;
1648 ppfd->cAccumBlueBits = bb;
1649 ppfd->cAccumAlphaBits = ab;
1651 /* Aux bits */
1652 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1653 ppfd->cAuxBuffers = value;
1655 /* Depth bits */
1656 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1657 ppfd->cDepthBits = value;
1659 /* stencil bits */
1660 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1661 ppfd->cStencilBits = value;
1663 ppfd->iLayerType = PFD_MAIN_PLANE;
1665 if (TRACE_ON(wgl)) {
1666 dump_PIXELFORMATDESCRIPTOR(ppfd);
1669 return nb_onscreen_formats;
1673 * glxdrv_DescribePixelFormat
1675 * Get the pixel-format descriptor associated to the given id
1677 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1678 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1680 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1682 if (!ppfd) return nb_onscreen_formats;
1684 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR))
1686 ERR("Wrong structure size !\n");
1687 /* Should set error */
1688 return 0;
1691 return describe_pixel_format(iPixelFormat, ppfd, FALSE);
1694 /***********************************************************************
1695 * glxdrv_wglGetPixelFormat
1697 static int glxdrv_wglGetPixelFormat( HDC hdc )
1699 struct gl_drawable *gl;
1700 int ret = 0;
1701 HWND hwnd;
1703 if ((hwnd = NtUserWindowFromDC( hdc )))
1704 return win32u_get_window_pixel_format( hwnd );
1706 if ((gl = get_gl_drawable( NULL, hdc )))
1708 ret = pixel_format_index( gl->format );
1709 /* Offscreen formats can't be used with traditional WGL calls.
1710 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1711 if (!is_onscreen_pixel_format( ret )) ret = 1;
1712 release_gl_drawable( gl );
1714 TRACE( "%p -> %d\n", hdc, ret );
1715 return ret;
1718 /***********************************************************************
1719 * glxdrv_wglSetPixelFormat
1721 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1723 return set_pixel_format(hdc, iPixelFormat, FALSE);
1726 /***********************************************************************
1727 * glxdrv_wglCopyContext
1729 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1731 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1733 X11DRV_expect_error( gdi_display, GLXErrorHandler, NULL );
1734 pglXCopyContext( gdi_display, src->ctx, dst->ctx, mask );
1735 XSync( gdi_display, False );
1736 if (X11DRV_check_error())
1738 static unsigned int once;
1740 if (!once++)
1742 ERR("glXCopyContext failed. glXCopyContext() for direct rendering contexts not "
1743 "implemented in the host graphics driver?\n");
1745 return FALSE;
1748 return TRUE;
1751 /***********************************************************************
1752 * glxdrv_wglCreateContext
1754 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1756 struct wgl_context *ret;
1757 struct gl_drawable *gl;
1759 if (!(gl = get_gl_drawable( NtUserWindowFromDC( hdc ), hdc )))
1761 RtlSetLastWin32Error( ERROR_INVALID_PIXEL_FORMAT );
1762 return NULL;
1765 if ((ret = calloc( 1, sizeof(*ret) )))
1767 ret->hdc = hdc;
1768 ret->fmt = gl->format;
1769 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1770 pthread_mutex_lock( &context_mutex );
1771 list_add_head( &context_list, &ret->entry );
1772 pthread_mutex_unlock( &context_mutex );
1774 release_gl_drawable( gl );
1775 TRACE( "%p -> %p\n", hdc, ret );
1776 return ret;
1779 /***********************************************************************
1780 * glxdrv_wglDeleteContext
1782 static BOOL glxdrv_wglDeleteContext(struct wgl_context *ctx)
1784 struct wgl_pbuffer *pb;
1786 TRACE("(%p)\n", ctx);
1788 pthread_mutex_lock( &context_mutex );
1789 list_remove( &ctx->entry );
1790 LIST_FOR_EACH_ENTRY( pb, &pbuffer_list, struct wgl_pbuffer, entry )
1792 if (pb->prev_context == ctx->ctx) {
1793 pglXDestroyContext(gdi_display, pb->tmp_context);
1794 pb->prev_context = pb->tmp_context = NULL;
1797 pthread_mutex_unlock( &context_mutex );
1799 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1800 release_gl_drawable( ctx->drawables[0] );
1801 release_gl_drawable( ctx->drawables[1] );
1802 release_gl_drawable( ctx->new_drawables[0] );
1803 release_gl_drawable( ctx->new_drawables[1] );
1804 free( ctx );
1805 return TRUE;
1808 /***********************************************************************
1809 * glxdrv_wglGetProcAddress
1811 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1813 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1814 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1817 static void set_context_drawables( struct wgl_context *ctx, struct gl_drawable *draw,
1818 struct gl_drawable *read )
1820 struct gl_drawable *prev[4];
1821 int i;
1823 prev[0] = ctx->drawables[0];
1824 prev[1] = ctx->drawables[1];
1825 prev[2] = ctx->new_drawables[0];
1826 prev[3] = ctx->new_drawables[1];
1827 ctx->drawables[0] = grab_gl_drawable( draw );
1828 ctx->drawables[1] = read ? grab_gl_drawable( read ) : NULL;
1829 ctx->new_drawables[0] = ctx->new_drawables[1] = NULL;
1830 for (i = 0; i < 4; i++) release_gl_drawable( prev[i] );
1833 /***********************************************************************
1834 * glxdrv_wglMakeCurrent
1836 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1838 BOOL ret = FALSE;
1839 struct gl_drawable *gl;
1841 TRACE("(%p,%p)\n", hdc, ctx);
1843 if (!ctx)
1845 pglXMakeCurrent(gdi_display, None, NULL);
1846 NtCurrentTeb()->glContext = NULL;
1847 return TRUE;
1850 if ((gl = get_gl_drawable( NtUserWindowFromDC( hdc ), hdc )))
1852 if (ctx->fmt != gl->format)
1854 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1855 RtlSetLastWin32Error( ERROR_INVALID_PIXEL_FORMAT );
1856 goto done;
1859 TRACE("hdc %p drawable %lx fmt %p ctx %p %s\n", hdc, gl->drawable, gl->format, ctx->ctx,
1860 debugstr_fbconfig( gl->format->fbconfig ));
1862 pthread_mutex_lock( &context_mutex );
1863 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1864 if (ret)
1866 NtCurrentTeb()->glContext = ctx;
1867 ctx->has_been_current = TRUE;
1868 ctx->hdc = hdc;
1869 set_context_drawables( ctx, gl, gl );
1870 ctx->refresh_drawables = FALSE;
1871 pthread_mutex_unlock( &context_mutex );
1872 goto done;
1874 pthread_mutex_unlock( &context_mutex );
1876 RtlSetLastWin32Error( ERROR_INVALID_HANDLE );
1878 done:
1879 release_gl_drawable( gl );
1880 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1881 return ret;
1884 /***********************************************************************
1885 * X11DRV_wglMakeContextCurrentARB
1887 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1889 BOOL ret = FALSE;
1890 struct gl_drawable *draw_gl, *read_gl = NULL;
1892 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1894 if (!ctx)
1896 pglXMakeCurrent(gdi_display, None, NULL);
1897 NtCurrentTeb()->glContext = NULL;
1898 return TRUE;
1901 if (!pglXMakeContextCurrent) return FALSE;
1903 if ((draw_gl = get_gl_drawable( NtUserWindowFromDC( draw_hdc ), draw_hdc )))
1905 read_gl = get_gl_drawable( NtUserWindowFromDC( read_hdc ), read_hdc );
1907 pthread_mutex_lock( &context_mutex );
1908 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1909 read_gl ? read_gl->drawable : 0, ctx->ctx);
1910 if (ret)
1912 ctx->has_been_current = TRUE;
1913 ctx->hdc = draw_hdc;
1914 set_context_drawables( ctx, draw_gl, read_gl );
1915 ctx->refresh_drawables = FALSE;
1916 NtCurrentTeb()->glContext = ctx;
1917 pthread_mutex_unlock( &context_mutex );
1918 goto done;
1920 pthread_mutex_unlock( &context_mutex );
1922 RtlSetLastWin32Error( ERROR_INVALID_HANDLE );
1923 done:
1924 release_gl_drawable( read_gl );
1925 release_gl_drawable( draw_gl );
1926 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1927 return ret;
1930 /***********************************************************************
1931 * glxdrv_wglShareLists
1933 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1935 struct wgl_context *keep, *clobber;
1937 TRACE("(%p, %p)\n", org, dest);
1939 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1940 * at context creation time but in case of WGL it is done using wglShareLists.
1941 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1942 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1943 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1944 * so there delaying context creation doesn't work.
1946 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1947 * and when a program requests sharing we recreate the destination or source context if it
1948 * hasn't been made current and it hasn't shared display lists before.
1951 if (!dest->has_been_current && !dest->sharing)
1953 keep = org;
1954 clobber = dest;
1956 else if (!org->has_been_current && !org->sharing)
1958 keep = dest;
1959 clobber = org;
1961 else
1963 ERR("Could not share display lists because both of the contexts have already been current or shared\n");
1964 return FALSE;
1967 pglXDestroyContext(gdi_display, clobber->ctx);
1968 clobber->ctx = create_glxcontext(gdi_display, clobber, keep->ctx);
1969 TRACE("re-created context (%p) for Wine context %p (%s) sharing lists with ctx %p (%s)\n",
1970 clobber->ctx, clobber, debugstr_fbconfig(clobber->fmt->fbconfig),
1971 keep->ctx, debugstr_fbconfig(keep->fmt->fbconfig));
1973 org->sharing = TRUE;
1974 dest->sharing = TRUE;
1975 return TRUE;
1978 static void wglFinish(void)
1980 struct x11drv_escape_flush_gl_drawable escape;
1981 struct gl_drawable *gl;
1982 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1984 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1985 escape.gl_drawable = 0;
1986 escape.flush = FALSE;
1988 if ((gl = get_gl_drawable( NtUserWindowFromDC( ctx->hdc ), 0 )))
1990 switch (gl->type)
1992 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1993 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
1994 default: break;
1996 sync_context(ctx);
1997 release_gl_drawable( gl );
2000 pglFinish();
2001 if (escape.gl_drawable)
2002 NtGdiExtEscape( ctx->hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2005 static void wglFlush(void)
2007 struct x11drv_escape_flush_gl_drawable escape;
2008 struct gl_drawable *gl;
2009 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2011 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
2012 escape.gl_drawable = 0;
2013 escape.flush = FALSE;
2015 if ((gl = get_gl_drawable( NtUserWindowFromDC( ctx->hdc ), 0 )))
2017 switch (gl->type)
2019 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
2020 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
2021 default: break;
2023 sync_context(ctx);
2024 release_gl_drawable( gl );
2027 pglFlush();
2028 if (escape.gl_drawable)
2029 NtGdiExtEscape( ctx->hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2032 static const GLubyte *wglGetString(GLenum name)
2034 if (name == GL_EXTENSIONS && glExtensions) return (const GLubyte *)glExtensions;
2035 return pglGetString(name);
2038 /***********************************************************************
2039 * X11DRV_wglCreateContextAttribsARB
2041 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2042 const int* attribList )
2044 struct wgl_context *ret;
2045 struct gl_drawable *gl;
2046 int err = 0;
2048 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2050 if (!(gl = get_gl_drawable( NtUserWindowFromDC( hdc ), hdc )))
2052 RtlSetLastWin32Error( ERROR_INVALID_PIXEL_FORMAT );
2053 return NULL;
2056 if ((ret = calloc( 1, sizeof(*ret) )))
2058 ret->hdc = hdc;
2059 ret->fmt = gl->format;
2060 ret->gl3_context = TRUE;
2061 if (attribList)
2063 int *pContextAttribList = &ret->attribList[0];
2064 /* attribList consists of pairs {token, value] terminated with 0 */
2065 while(attribList[0] != 0)
2067 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2068 switch(attribList[0])
2070 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2071 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2072 pContextAttribList[1] = attribList[1];
2073 pContextAttribList += 2;
2074 ret->numAttribs++;
2075 break;
2076 case WGL_CONTEXT_MINOR_VERSION_ARB:
2077 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2078 pContextAttribList[1] = attribList[1];
2079 pContextAttribList += 2;
2080 ret->numAttribs++;
2081 break;
2082 case WGL_CONTEXT_LAYER_PLANE_ARB:
2083 break;
2084 case WGL_CONTEXT_FLAGS_ARB:
2085 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2086 pContextAttribList[1] = attribList[1];
2087 pContextAttribList += 2;
2088 ret->numAttribs++;
2089 break;
2090 case WGL_CONTEXT_OPENGL_NO_ERROR_ARB:
2091 pContextAttribList[0] = GLX_CONTEXT_OPENGL_NO_ERROR_ARB;
2092 pContextAttribList[1] = attribList[1];
2093 pContextAttribList += 2;
2094 ret->numAttribs++;
2095 break;
2096 case WGL_CONTEXT_PROFILE_MASK_ARB:
2097 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2098 pContextAttribList[1] = attribList[1];
2099 pContextAttribList += 2;
2100 ret->numAttribs++;
2101 break;
2102 case WGL_RENDERER_ID_WINE:
2103 pContextAttribList[0] = GLX_RENDERER_ID_MESA;
2104 pContextAttribList[1] = attribList[1];
2105 pContextAttribList += 2;
2106 ret->numAttribs++;
2107 break;
2108 default:
2109 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2111 attribList += 2;
2115 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2116 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2117 XSync(gdi_display, False);
2118 if ((err = X11DRV_check_error()) || !ret->ctx)
2120 /* In the future we should convert the GLX error to a win32 one here if needed */
2121 WARN("Context creation failed (error %#x).\n", err);
2122 free( ret );
2123 ret = NULL;
2125 else
2127 pthread_mutex_lock( &context_mutex );
2128 list_add_head( &context_list, &ret->entry );
2129 pthread_mutex_unlock( &context_mutex );
2133 release_gl_drawable( gl );
2134 TRACE( "%p -> %p\n", hdc, ret );
2135 return ret;
2139 * X11DRV_wglGetExtensionsStringARB
2141 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2143 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2145 TRACE("() returning \"%s\"\n", wglExtensions);
2146 return wglExtensions;
2150 * X11DRV_wglCreatePbufferARB
2152 * WGL_ARB_pbuffer: wglCreatePbufferARB
2154 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2155 const int *piAttribList )
2157 struct wgl_pbuffer* object;
2158 const struct wgl_pixel_format *fmt;
2159 int attribs[256];
2160 int nAttribs = 0;
2162 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2164 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2165 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2166 if(!fmt) {
2167 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2168 RtlSetLastWin32Error(ERROR_INVALID_PIXEL_FORMAT);
2169 return NULL;
2172 object = calloc( 1, sizeof(*object) );
2173 if (NULL == object) {
2174 RtlSetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
2175 return NULL;
2177 object->width = iWidth;
2178 object->height = iHeight;
2179 object->fmt = fmt;
2181 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2182 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2183 while (piAttribList && 0 != *piAttribList) {
2184 int attr_v;
2185 switch (*piAttribList) {
2186 case WGL_PBUFFER_LARGEST_ARB: {
2187 ++piAttribList;
2188 attr_v = *piAttribList;
2189 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2190 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2191 break;
2194 case WGL_TEXTURE_FORMAT_ARB: {
2195 ++piAttribList;
2196 attr_v = *piAttribList;
2197 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2198 if (WGL_NO_TEXTURE_ARB == attr_v) {
2199 object->use_render_texture = 0;
2200 } else {
2201 if (!use_render_texture_emulation) {
2202 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2203 goto create_failed;
2205 switch (attr_v) {
2206 case WGL_TEXTURE_RGB_ARB:
2207 object->use_render_texture = GL_RGB;
2208 object->texture_bpp = 3;
2209 object->texture_format = GL_RGB;
2210 object->texture_type = GL_UNSIGNED_BYTE;
2211 break;
2212 case WGL_TEXTURE_RGBA_ARB:
2213 object->use_render_texture = GL_RGBA;
2214 object->texture_bpp = 4;
2215 object->texture_format = GL_RGBA;
2216 object->texture_type = GL_UNSIGNED_BYTE;
2217 break;
2219 /* WGL_FLOAT_COMPONENTS_NV */
2220 case WGL_TEXTURE_FLOAT_R_NV:
2221 object->use_render_texture = GL_FLOAT_R_NV;
2222 object->texture_bpp = 4;
2223 object->texture_format = GL_RED;
2224 object->texture_type = GL_FLOAT;
2225 break;
2226 case WGL_TEXTURE_FLOAT_RG_NV:
2227 object->use_render_texture = GL_FLOAT_RG_NV;
2228 object->texture_bpp = 8;
2229 object->texture_format = GL_LUMINANCE_ALPHA;
2230 object->texture_type = GL_FLOAT;
2231 break;
2232 case WGL_TEXTURE_FLOAT_RGB_NV:
2233 object->use_render_texture = GL_FLOAT_RGB_NV;
2234 object->texture_bpp = 12;
2235 object->texture_format = GL_RGB;
2236 object->texture_type = GL_FLOAT;
2237 break;
2238 case WGL_TEXTURE_FLOAT_RGBA_NV:
2239 object->use_render_texture = GL_FLOAT_RGBA_NV;
2240 object->texture_bpp = 16;
2241 object->texture_format = GL_RGBA;
2242 object->texture_type = GL_FLOAT;
2243 break;
2244 default:
2245 ERR("Unknown texture format: %x\n", attr_v);
2246 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2247 goto create_failed;
2250 break;
2253 case WGL_TEXTURE_TARGET_ARB: {
2254 ++piAttribList;
2255 attr_v = *piAttribList;
2256 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2257 if (WGL_NO_TEXTURE_ARB == attr_v) {
2258 object->texture_target = 0;
2259 } else {
2260 if (!use_render_texture_emulation) {
2261 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2262 goto create_failed;
2264 switch (attr_v) {
2265 case WGL_TEXTURE_CUBE_MAP_ARB: {
2266 if (iWidth != iHeight) {
2267 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2268 goto create_failed;
2270 object->texture_target = GL_TEXTURE_CUBE_MAP;
2271 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2272 break;
2274 case WGL_TEXTURE_1D_ARB: {
2275 if (1 != iHeight) {
2276 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2277 goto create_failed;
2279 object->texture_target = GL_TEXTURE_1D;
2280 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2281 break;
2283 case WGL_TEXTURE_2D_ARB: {
2284 object->texture_target = GL_TEXTURE_2D;
2285 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2286 break;
2288 case WGL_TEXTURE_RECTANGLE_NV: {
2289 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2290 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2291 break;
2293 default:
2294 ERR("Unknown texture target: %x\n", attr_v);
2295 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2296 goto create_failed;
2299 break;
2302 case WGL_MIPMAP_TEXTURE_ARB: {
2303 ++piAttribList;
2304 attr_v = *piAttribList;
2305 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2306 if (!use_render_texture_emulation) {
2307 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2308 goto create_failed;
2310 break;
2313 ++piAttribList;
2316 PUSH1(attribs, None);
2317 if (!(object->gl = calloc( 1, sizeof(*object->gl) )))
2319 RtlSetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
2320 goto create_failed;
2322 object->gl->type = DC_GL_PBUFFER;
2323 object->gl->format = object->fmt;
2324 object->gl->ref = 1;
2326 object->gl->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2327 TRACE("new Pbuffer drawable as %p (%lx)\n", object->gl, object->gl->drawable);
2328 if (!object->gl->drawable) {
2329 free( object->gl );
2330 RtlSetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES);
2331 goto create_failed; /* unexpected error */
2333 pthread_mutex_lock( &context_mutex );
2334 list_add_head( &pbuffer_list, &object->entry );
2335 pthread_mutex_unlock( &context_mutex );
2336 TRACE("->(%p)\n", object);
2337 return object;
2339 create_failed:
2340 free( object );
2341 TRACE("->(FAILED)\n");
2342 return NULL;
2346 * X11DRV_wglDestroyPbufferARB
2348 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2350 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2352 TRACE("(%p)\n", object);
2354 pthread_mutex_lock( &context_mutex );
2355 list_remove( &object->entry );
2356 pthread_mutex_unlock( &context_mutex );
2357 release_gl_drawable( object->gl );
2358 if (object->tmp_context)
2359 pglXDestroyContext(gdi_display, object->tmp_context);
2360 free( object );
2361 return GL_TRUE;
2365 * X11DRV_wglGetPbufferDCARB
2367 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2369 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2371 struct x11drv_escape_set_drawable escape;
2372 struct gl_drawable *prev;
2373 HDC hdc;
2375 hdc = NtGdiOpenDCW( NULL, NULL, NULL, 0, TRUE, NULL, NULL, NULL );
2376 if (!hdc) return 0;
2378 pthread_mutex_lock( &context_mutex );
2379 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2380 release_gl_drawable( prev );
2381 grab_gl_drawable( object->gl );
2382 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)object->gl );
2383 pthread_mutex_unlock( &context_mutex );
2385 escape.code = X11DRV_SET_DRAWABLE;
2386 escape.drawable = object->gl->drawable;
2387 escape.mode = IncludeInferiors;
2388 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2389 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2391 TRACE( "(%p)->(%p)\n", object, hdc );
2392 return hdc;
2396 * X11DRV_wglQueryPbufferARB
2398 * WGL_ARB_pbuffer: wglQueryPbufferARB
2400 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2402 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2404 switch (iAttribute) {
2405 case WGL_PBUFFER_WIDTH_ARB:
2406 pglXQueryDrawable(gdi_display, object->gl->drawable, GLX_WIDTH, (unsigned int*) piValue);
2407 break;
2408 case WGL_PBUFFER_HEIGHT_ARB:
2409 pglXQueryDrawable(gdi_display, object->gl->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2410 break;
2412 case WGL_PBUFFER_LOST_ARB:
2413 /* GLX Pbuffers cannot be lost by default. We can support this by
2414 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2415 * to receive pixel buffer clobber events, however that may or may
2416 * not give any benefit */
2417 *piValue = GL_FALSE;
2418 break;
2420 case WGL_TEXTURE_FORMAT_ARB:
2421 if (!object->use_render_texture) {
2422 *piValue = WGL_NO_TEXTURE_ARB;
2423 } else {
2424 if (!use_render_texture_emulation) {
2425 RtlSetLastWin32Error(ERROR_INVALID_HANDLE);
2426 return GL_FALSE;
2428 switch(object->use_render_texture) {
2429 case GL_RGB:
2430 *piValue = WGL_TEXTURE_RGB_ARB;
2431 break;
2432 case GL_RGBA:
2433 *piValue = WGL_TEXTURE_RGBA_ARB;
2434 break;
2435 /* WGL_FLOAT_COMPONENTS_NV */
2436 case GL_FLOAT_R_NV:
2437 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2438 break;
2439 case GL_FLOAT_RG_NV:
2440 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2441 break;
2442 case GL_FLOAT_RGB_NV:
2443 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2444 break;
2445 case GL_FLOAT_RGBA_NV:
2446 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2447 break;
2448 default:
2449 ERR("Unknown texture format: %x\n", object->use_render_texture);
2452 break;
2454 case WGL_TEXTURE_TARGET_ARB:
2455 if (!object->texture_target){
2456 *piValue = WGL_NO_TEXTURE_ARB;
2457 } else {
2458 if (!use_render_texture_emulation) {
2459 RtlSetLastWin32Error(ERROR_INVALID_DATA);
2460 return GL_FALSE;
2462 switch (object->texture_target) {
2463 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2464 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2465 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2466 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2469 break;
2471 case WGL_MIPMAP_TEXTURE_ARB:
2472 *piValue = GL_FALSE; /** don't support that */
2473 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2474 break;
2476 default:
2477 FIXME("unexpected attribute %x\n", iAttribute);
2478 break;
2481 return GL_TRUE;
2485 * X11DRV_wglReleasePbufferDCARB
2487 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2489 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2491 struct gl_drawable *gl;
2493 TRACE("(%p, %p)\n", object, hdc);
2495 pthread_mutex_lock( &context_mutex );
2497 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2499 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2500 release_gl_drawable( gl );
2502 else hdc = 0;
2504 pthread_mutex_unlock( &context_mutex );
2506 return hdc && NtGdiDeleteObjectApp(hdc);
2510 * X11DRV_wglSetPbufferAttribARB
2512 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2514 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2516 GLboolean ret = GL_FALSE;
2518 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2520 if (!object->use_render_texture) {
2521 RtlSetLastWin32Error(ERROR_INVALID_HANDLE);
2522 return GL_FALSE;
2524 if (use_render_texture_emulation) {
2525 return GL_TRUE;
2527 return ret;
2530 struct choose_pixel_format_arb_format
2532 int format;
2533 int original_index;
2534 PIXELFORMATDESCRIPTOR pfd;
2535 int depth, stencil;
2538 static int compare_formats(const void *a, const void *b)
2540 /* Order formats so that onscreen formats go first. Then, if no depth bits requested,
2541 * prioritize formats with smaller depth within the original sort order with respect to
2542 * other attributes. */
2543 const struct choose_pixel_format_arb_format *fmt_a = a, *fmt_b = b;
2544 BOOL offscreen_a, offscreen_b;
2546 offscreen_a = fmt_a->format > nb_onscreen_formats;
2547 offscreen_b = fmt_b->format > nb_onscreen_formats;
2549 if (offscreen_a != offscreen_b)
2550 return offscreen_a - offscreen_b;
2551 if (memcmp(&fmt_a->pfd, &fmt_b->pfd, sizeof(fmt_a->pfd)))
2552 return fmt_a->original_index - fmt_b->original_index;
2553 if (fmt_a->depth != fmt_b->depth)
2554 return fmt_a->depth - fmt_b->depth;
2555 if (fmt_a->stencil != fmt_b->stencil)
2556 return fmt_a->stencil - fmt_b->stencil;
2558 return fmt_a->original_index - fmt_b->original_index;
2562 * X11DRV_wglChoosePixelFormatARB
2564 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2566 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2567 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2569 struct choose_pixel_format_arb_format *formats;
2570 int it, i, format_count;
2571 BYTE depth_bits = 0;
2572 GLXFBConfig* cfgs;
2573 DWORD dwFlags = 0;
2574 int attribs[256];
2575 int nAttribs = 0;
2576 int nCfgs = 0;
2577 int fmt_id;
2579 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2580 if (NULL != pfAttribFList) {
2581 FIXME("unused pfAttribFList\n");
2584 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2585 if (-1 == nAttribs) {
2586 WARN("Cannot convert WGL to GLX attributes\n");
2587 return GL_FALSE;
2589 PUSH1(attribs, None);
2591 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2592 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2593 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2595 for(i=0; piAttribIList[i] != 0; i+=2)
2597 switch(piAttribIList[i])
2599 case WGL_DRAW_TO_BITMAP_ARB:
2600 if(piAttribIList[i+1])
2601 dwFlags |= PFD_DRAW_TO_BITMAP;
2602 break;
2603 case WGL_ACCELERATION_ARB:
2604 switch(piAttribIList[i+1])
2606 case WGL_NO_ACCELERATION_ARB:
2607 dwFlags |= PFD_GENERIC_FORMAT;
2608 break;
2609 case WGL_GENERIC_ACCELERATION_ARB:
2610 dwFlags |= PFD_GENERIC_ACCELERATED;
2611 break;
2612 case WGL_FULL_ACCELERATION_ARB:
2613 /* Nothing to do */
2614 break;
2616 break;
2617 case WGL_SUPPORT_GDI_ARB:
2618 if(piAttribIList[i+1])
2619 dwFlags |= PFD_SUPPORT_GDI;
2620 break;
2621 case WGL_DEPTH_BITS_ARB:
2622 depth_bits = piAttribIList[i+1];
2623 break;
2628 /* Search for FB configurations matching the requirements in attribs */
2629 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2630 if (NULL == cfgs) {
2631 WARN("Compatible Pixel Format not found\n");
2632 return GL_FALSE;
2635 if (!(formats = malloc( nCfgs * sizeof(*formats) )))
2637 ERR("No memory.\n");
2638 XFree(cfgs);
2639 return GL_FALSE;
2642 format_count = 0;
2643 for (it = 0; it < nCfgs; ++it)
2645 struct choose_pixel_format_arb_format *format;
2647 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2649 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2650 continue;
2653 for (i = 0; i < nb_pixel_formats; ++i)
2654 if (pixel_formats[i].fmt_id == fmt_id)
2655 break;
2657 if (i == nb_pixel_formats)
2658 continue;
2659 if ((pixel_formats[i].dwFlags & dwFlags) != dwFlags)
2660 continue;
2662 format = &formats[format_count];
2663 format->format = i + 1;
2664 format->original_index = it;
2666 memset(&format->pfd, 0, sizeof(format->pfd));
2667 if (!describe_pixel_format(format->format, &format->pfd, TRUE))
2668 ERR("describe_pixel_format failed, format %d.\n", format->format);
2670 format->depth = format->pfd.cDepthBits;
2671 format->stencil = format->pfd.cStencilBits;
2672 if (!depth_bits && !(format->pfd.dwFlags & PFD_GENERIC_FORMAT))
2674 format->pfd.cDepthBits = 0;
2675 format->pfd.cStencilBits = 0;
2678 ++format_count;
2681 qsort(formats, format_count, sizeof(*formats), compare_formats);
2683 *nNumFormats = min(nMaxFormats, format_count);
2684 for (i = 0; i < *nNumFormats; ++i)
2685 piFormats[i] = formats[i].format;
2687 free( formats );
2688 XFree(cfgs);
2689 return GL_TRUE;
2693 * X11DRV_wglGetPixelFormatAttribivARB
2695 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2697 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2698 UINT nAttributes, const int *piAttributes, int *piValues )
2700 UINT i;
2701 const struct wgl_pixel_format *fmt;
2702 int hTest;
2703 int tmp;
2704 int curGLXAttr = 0;
2706 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2708 if (0 < iLayerPlane) {
2709 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2710 return GL_FALSE;
2713 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2714 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2715 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2716 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2717 if(!fmt) {
2718 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2721 for (i = 0; i < nAttributes; ++i) {
2722 const int curWGLAttr = piAttributes[i];
2723 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2725 switch (curWGLAttr) {
2726 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2727 piValues[i] = nb_pixel_formats;
2728 continue;
2730 case WGL_SUPPORT_OPENGL_ARB:
2731 piValues[i] = GL_TRUE;
2732 continue;
2734 case WGL_ACCELERATION_ARB:
2735 curGLXAttr = GLX_CONFIG_CAVEAT;
2736 if (!fmt) goto pix_error;
2737 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2738 piValues[i] = WGL_NO_ACCELERATION_ARB;
2739 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2740 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2741 else
2742 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2743 continue;
2745 case WGL_TRANSPARENT_ARB:
2746 curGLXAttr = GLX_TRANSPARENT_TYPE;
2747 if (!fmt) goto pix_error;
2748 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2749 if (hTest) goto get_error;
2750 piValues[i] = GL_FALSE;
2751 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2752 continue;
2754 case WGL_PIXEL_TYPE_ARB:
2755 curGLXAttr = GLX_RENDER_TYPE;
2756 if (!fmt) goto pix_error;
2757 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2758 if (hTest) goto get_error;
2759 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2760 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2761 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2762 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2763 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2764 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2765 else {
2766 ERR("unexpected RenderType(%x)\n", tmp);
2767 piValues[i] = WGL_TYPE_RGBA_ARB;
2769 continue;
2771 case WGL_COLOR_BITS_ARB:
2772 curGLXAttr = GLX_BUFFER_SIZE;
2773 break;
2775 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2776 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2777 if (!use_render_texture_emulation) {
2778 piValues[i] = GL_FALSE;
2779 continue;
2781 curGLXAttr = GLX_RENDER_TYPE;
2782 if (!fmt) goto pix_error;
2783 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2784 if (hTest) goto get_error;
2785 if (GLX_COLOR_INDEX_BIT == tmp) {
2786 piValues[i] = GL_FALSE;
2787 continue;
2789 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2790 if (hTest) goto get_error;
2791 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2792 continue;
2794 case WGL_BLUE_BITS_ARB:
2795 curGLXAttr = GLX_BLUE_SIZE;
2796 break;
2797 case WGL_RED_BITS_ARB:
2798 curGLXAttr = GLX_RED_SIZE;
2799 break;
2800 case WGL_GREEN_BITS_ARB:
2801 curGLXAttr = GLX_GREEN_SIZE;
2802 break;
2803 case WGL_ALPHA_BITS_ARB:
2804 curGLXAttr = GLX_ALPHA_SIZE;
2805 break;
2806 case WGL_DEPTH_BITS_ARB:
2807 curGLXAttr = GLX_DEPTH_SIZE;
2808 break;
2809 case WGL_STENCIL_BITS_ARB:
2810 curGLXAttr = GLX_STENCIL_SIZE;
2811 break;
2812 case WGL_DOUBLE_BUFFER_ARB:
2813 curGLXAttr = GLX_DOUBLEBUFFER;
2814 break;
2815 case WGL_STEREO_ARB:
2816 curGLXAttr = GLX_STEREO;
2817 break;
2818 case WGL_AUX_BUFFERS_ARB:
2819 curGLXAttr = GLX_AUX_BUFFERS;
2820 break;
2822 case WGL_SUPPORT_GDI_ARB:
2823 if (!fmt) goto pix_error;
2824 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2825 continue;
2827 case WGL_DRAW_TO_BITMAP_ARB:
2828 if (!fmt) goto pix_error;
2829 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2830 continue;
2832 case WGL_DRAW_TO_WINDOW_ARB:
2833 case WGL_DRAW_TO_PBUFFER_ARB:
2834 if (!fmt) goto pix_error;
2835 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2836 if (hTest) goto get_error;
2837 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2838 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2839 piValues[i] = GL_TRUE;
2840 else
2841 piValues[i] = GL_FALSE;
2842 continue;
2844 case WGL_SWAP_METHOD_ARB:
2845 if (has_swap_method)
2847 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_SWAP_METHOD_OML, &tmp);
2848 if (hTest) goto get_error;
2849 switch (tmp)
2851 case GLX_SWAP_EXCHANGE_OML:
2852 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2853 break;
2854 case GLX_SWAP_COPY_OML:
2855 piValues[i] = WGL_SWAP_COPY_ARB;
2856 break;
2857 case GLX_SWAP_UNDEFINED_OML:
2858 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2859 break;
2860 default:
2861 ERR("Unexpected swap method %x.\n", tmp);
2864 else
2866 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2867 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2869 continue;
2871 case WGL_PBUFFER_LARGEST_ARB:
2872 curGLXAttr = GLX_LARGEST_PBUFFER;
2873 break;
2875 case WGL_SAMPLE_BUFFERS_ARB:
2876 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2877 break;
2879 case WGL_SAMPLES_ARB:
2880 curGLXAttr = GLX_SAMPLES_ARB;
2881 break;
2883 case WGL_FLOAT_COMPONENTS_NV:
2884 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2885 break;
2887 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2888 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2889 break;
2891 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2892 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2893 break;
2895 case WGL_ACCUM_RED_BITS_ARB:
2896 curGLXAttr = GLX_ACCUM_RED_SIZE;
2897 break;
2898 case WGL_ACCUM_GREEN_BITS_ARB:
2899 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2900 break;
2901 case WGL_ACCUM_BLUE_BITS_ARB:
2902 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2903 break;
2904 case WGL_ACCUM_ALPHA_BITS_ARB:
2905 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2906 break;
2907 case WGL_ACCUM_BITS_ARB:
2908 if (!fmt) goto pix_error;
2909 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2910 if (hTest) goto get_error;
2911 piValues[i] = tmp;
2912 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2913 if (hTest) goto get_error;
2914 piValues[i] += tmp;
2915 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2916 if (hTest) goto get_error;
2917 piValues[i] += tmp;
2918 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2919 if (hTest) goto get_error;
2920 piValues[i] += tmp;
2921 continue;
2923 default:
2924 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2927 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2928 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2929 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2931 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2932 * and check which options can work using iPixelFormat=0 and which not.
2933 * A problem would be that this function is an extension. This would
2934 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2936 if (0 != curGLXAttr && iPixelFormat != 0) {
2937 if (!fmt) goto pix_error;
2938 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2939 if (hTest) goto get_error;
2940 curGLXAttr = 0;
2941 } else {
2942 piValues[i] = GL_FALSE;
2945 return GL_TRUE;
2947 get_error:
2948 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2949 return GL_FALSE;
2951 pix_error:
2952 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2953 return GL_FALSE;
2957 * X11DRV_wglGetPixelFormatAttribfvARB
2959 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2961 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2962 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2964 int *attr;
2965 int ret;
2966 UINT i;
2968 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2970 /* Allocate a temporary array to store integer values */
2971 attr = malloc( nAttributes * sizeof(int) );
2972 if (!attr) {
2973 ERR("couldn't allocate %d array\n", nAttributes);
2974 return GL_FALSE;
2977 /* Piggy-back on wglGetPixelFormatAttribivARB */
2978 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2979 if (ret) {
2980 /* Convert integer values to float. Should also check for attributes
2981 that can give decimal values here */
2982 for (i=0; i<nAttributes;i++) {
2983 pfValues[i] = attr[i];
2987 free( attr );
2988 return ret;
2992 * X11DRV_wglBindTexImageARB
2994 * WGL_ARB_render_texture: wglBindTexImageARB
2996 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2998 GLboolean ret = GL_FALSE;
3000 TRACE("(%p, %d)\n", object, iBuffer);
3002 if (!object->use_render_texture) {
3003 RtlSetLastWin32Error(ERROR_INVALID_HANDLE);
3004 return GL_FALSE;
3007 if (use_render_texture_emulation) {
3008 static BOOL initialized = FALSE;
3009 int prev_binded_texture = 0;
3010 GLXContext prev_context;
3011 GLXDrawable prev_drawable;
3013 prev_context = pglXGetCurrentContext();
3014 prev_drawable = pglXGetCurrentDrawable();
3016 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
3017 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
3018 isn't ideal performance wise but I wasn't able to get other ways working.
3020 if(!initialized) {
3021 initialized = TRUE; /* Only show the FIXME once for performance reasons */
3022 FIXME("partial stub!\n");
3025 TRACE("drawable=%p (%lx), context=%p\n", object->gl, object->gl->drawable, prev_context);
3026 if (!object->tmp_context || object->prev_context != prev_context) {
3027 if (object->tmp_context)
3028 pglXDestroyContext(gdi_display, object->tmp_context);
3029 object->tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3030 object->prev_context = prev_context;
3033 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3035 /* Switch to our pbuffer */
3036 pglXMakeCurrent(gdi_display, object->gl->drawable, object->tmp_context);
3038 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3039 * After that copy the pbuffer texture data. */
3040 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
3041 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3043 /* Switch back to the original drawable and context */
3044 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
3045 return GL_TRUE;
3048 return ret;
3052 * X11DRV_wglReleaseTexImageARB
3054 * WGL_ARB_render_texture: wglReleaseTexImageARB
3056 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
3058 GLboolean ret = GL_FALSE;
3060 TRACE("(%p, %d)\n", object, iBuffer);
3062 if (!object->use_render_texture) {
3063 RtlSetLastWin32Error(ERROR_INVALID_HANDLE);
3064 return GL_FALSE;
3066 if (use_render_texture_emulation) {
3067 return GL_TRUE;
3069 return ret;
3073 * X11DRV_wglGetExtensionsStringEXT
3075 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3077 static const char *X11DRV_wglGetExtensionsStringEXT(void)
3079 TRACE("() returning \"%s\"\n", wglExtensions);
3080 return wglExtensions;
3084 * X11DRV_wglGetSwapIntervalEXT
3086 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3088 static int X11DRV_wglGetSwapIntervalEXT(void)
3090 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3091 struct gl_drawable *gl;
3092 int swap_interval;
3094 TRACE("()\n");
3096 if (!(gl = get_gl_drawable( NtUserWindowFromDC( ctx->hdc ), ctx->hdc )))
3098 /* This can't happen because a current WGL context is required to get
3099 * here. Likely the application is buggy.
3101 WARN("No GL drawable found, returning swap interval 0\n");
3102 return 0;
3105 swap_interval = gl->swap_interval;
3106 release_gl_drawable(gl);
3108 return swap_interval;
3112 * X11DRV_wglSwapIntervalEXT
3114 * WGL_EXT_swap_control: wglSwapIntervalEXT
3116 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3118 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3119 struct gl_drawable *gl;
3120 BOOL ret;
3122 TRACE("(%d)\n", interval);
3124 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3125 * is invalid.
3127 if (interval < 0 && !has_swap_control_tear)
3129 RtlSetLastWin32Error(ERROR_INVALID_DATA);
3130 return FALSE;
3133 if (!(gl = get_gl_drawable( NtUserWindowFromDC( ctx->hdc ), ctx->hdc )))
3135 RtlSetLastWin32Error(ERROR_DC_NOT_FOUND);
3136 return FALSE;
3139 pthread_mutex_lock( &context_mutex );
3140 ret = set_swap_interval(gl->drawable, interval);
3141 gl->refresh_swap_interval = FALSE;
3142 if (ret)
3143 gl->swap_interval = interval;
3144 else
3145 RtlSetLastWin32Error(ERROR_DC_NOT_FOUND);
3147 pthread_mutex_unlock( &context_mutex );
3148 release_gl_drawable(gl);
3150 return ret;
3154 * X11DRV_wglSetPixelFormatWINE
3156 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3157 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3159 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3161 return set_pixel_format(hdc, format, TRUE);
3164 static BOOL X11DRV_wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
3166 return pglXQueryCurrentRendererIntegerMESA( attribute, value );
3169 static const char *X11DRV_wglQueryCurrentRendererStringWINE( GLenum attribute )
3171 return pglXQueryCurrentRendererStringMESA( attribute );
3174 static BOOL X11DRV_wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
3176 return pglXQueryRendererIntegerMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute, value );
3179 static const char *X11DRV_wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
3181 return pglXQueryRendererStringMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute );
3185 * glxRequireVersion (internal)
3187 * Check if the supported GLX version matches requiredVersion.
3189 static BOOL glxRequireVersion(int requiredVersion)
3191 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3192 return (requiredVersion <= glxVersion[1]);
3195 static void register_extension(const char *ext)
3197 if (wglExtensions[0])
3198 strcat(wglExtensions, " ");
3199 strcat(wglExtensions, ext);
3201 TRACE("'%s'\n", ext);
3205 * X11DRV_WineGL_LoadExtensions
3207 static void X11DRV_WineGL_LoadExtensions(void)
3209 wglExtensions[0] = 0;
3211 /* ARB Extensions */
3213 if (has_extension( glxExtensions, "GLX_ARB_create_context"))
3215 register_extension( "WGL_ARB_create_context" );
3216 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3218 if (has_extension( glxExtensions, "GLX_ARB_create_context_no_error" ))
3219 register_extension( "WGL_ARB_create_context_no_error" );
3220 if (has_extension( glxExtensions, "GLX_ARB_create_context_profile"))
3221 register_extension("WGL_ARB_create_context_profile");
3225 register_extension( "WGL_ARB_extensions_string" );
3226 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3228 if (glxRequireVersion(3))
3230 register_extension( "WGL_ARB_make_current_read" );
3231 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3232 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3235 if (has_extension( glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3237 if (glxRequireVersion(3))
3239 register_extension( "WGL_ARB_pbuffer" );
3240 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3241 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3242 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3243 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3244 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3245 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3248 register_extension( "WGL_ARB_pixel_format" );
3249 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3250 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3251 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3253 if (has_extension( glxExtensions, "GLX_ARB_fbconfig_float"))
3255 register_extension("WGL_ARB_pixel_format_float");
3256 register_extension("WGL_ATI_pixel_format_float");
3259 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3260 if (has_extension( glxExtensions, "GLX_ARB_render_texture") ||
3261 (glxRequireVersion(3) && use_render_texture_emulation))
3263 register_extension( "WGL_ARB_render_texture" );
3264 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3265 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3267 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3268 if (has_extension( glxExtensions, "GLX_NV_float_buffer"))
3269 register_extension("WGL_NV_float_buffer");
3271 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3272 if (has_extension(glExtensions, "GL_NV_texture_rectangle"))
3273 register_extension("WGL_NV_render_texture_rectangle");
3276 /* EXT Extensions */
3278 register_extension( "WGL_EXT_extensions_string" );
3279 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3281 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3282 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3283 register_extension( "WGL_EXT_swap_control" );
3284 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3285 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3287 if (has_extension( glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3288 register_extension("WGL_EXT_framebuffer_sRGB");
3290 if (has_extension( glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3291 register_extension("WGL_EXT_pixel_format_packed_float");
3293 if (has_extension( glxExtensions, "GLX_EXT_swap_control"))
3295 swap_control_method = GLX_SWAP_CONTROL_EXT;
3296 if (has_extension( glxExtensions, "GLX_EXT_swap_control_tear"))
3298 register_extension("WGL_EXT_swap_control_tear");
3299 has_swap_control_tear = TRUE;
3302 else if (has_extension( glxExtensions, "GLX_MESA_swap_control"))
3304 swap_control_method = GLX_SWAP_CONTROL_MESA;
3306 else if (has_extension( glxExtensions, "GLX_SGI_swap_control"))
3308 swap_control_method = GLX_SWAP_CONTROL_SGI;
3311 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3312 if (has_extension(glExtensions, "GL_NV_vertex_array_range"))
3314 register_extension( "WGL_NV_vertex_array_range" );
3315 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3316 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3319 if (has_extension(glxExtensions, "GLX_OML_swap_method"))
3320 has_swap_method = TRUE;
3322 /* WINE-specific WGL Extensions */
3324 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3325 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3327 register_extension( "WGL_WINE_pixel_format_passthrough" );
3328 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3330 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
3332 register_extension( "WGL_WINE_query_renderer" );
3333 opengl_funcs.ext.p_wglQueryCurrentRendererIntegerWINE = X11DRV_wglQueryCurrentRendererIntegerWINE;
3334 opengl_funcs.ext.p_wglQueryCurrentRendererStringWINE = X11DRV_wglQueryCurrentRendererStringWINE;
3335 opengl_funcs.ext.p_wglQueryRendererIntegerWINE = X11DRV_wglQueryRendererIntegerWINE;
3336 opengl_funcs.ext.p_wglQueryRendererStringWINE = X11DRV_wglQueryRendererStringWINE;
3342 * glxdrv_SwapBuffers
3344 * Swap the buffers of this DC
3346 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3348 struct x11drv_escape_flush_gl_drawable escape;
3349 struct gl_drawable *gl;
3350 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3351 INT64 ust, msc, sbc, target_sbc = 0;
3353 TRACE("(%p)\n", hdc);
3355 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3356 escape.gl_drawable = 0;
3357 escape.flush = !pglXWaitForSbcOML;
3359 if (!(gl = get_gl_drawable( NtUserWindowFromDC( hdc ), hdc )))
3361 RtlSetLastWin32Error( ERROR_INVALID_HANDLE );
3362 return FALSE;
3365 pthread_mutex_lock( &context_mutex );
3366 if (gl->refresh_swap_interval)
3368 set_swap_interval(gl->drawable, gl->swap_interval);
3369 gl->refresh_swap_interval = FALSE;
3371 pthread_mutex_unlock( &context_mutex );
3373 switch (gl->type)
3375 case DC_GL_PIXMAP_WIN:
3376 if (ctx) sync_context( ctx );
3377 escape.gl_drawable = gl->pixmap;
3378 if (pglXCopySubBufferMESA) {
3379 /* (glX)SwapBuffers has an implicit glFlush effect, however
3380 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3381 * copying */
3382 pglFlush();
3383 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3384 gl->pixmap_size.cx, gl->pixmap_size.cy );
3385 break;
3387 if (pglXSwapBuffersMscOML)
3389 pglFlush();
3390 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3391 break;
3393 pglXSwapBuffers(gdi_display, gl->drawable);
3394 break;
3395 case DC_GL_WINDOW:
3396 case DC_GL_CHILD_WIN:
3397 if (ctx) sync_context( ctx );
3398 if (gl->type == DC_GL_CHILD_WIN) escape.gl_drawable = gl->window;
3399 /* fall through */
3400 default:
3401 if (escape.gl_drawable && pglXSwapBuffersMscOML)
3403 pglFlush();
3404 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3405 break;
3407 pglXSwapBuffers(gdi_display, gl->drawable);
3408 break;
3411 if (escape.gl_drawable && pglXWaitForSbcOML)
3412 pglXWaitForSbcOML( gdi_display, gl->drawable, target_sbc, &ust, &msc, &sbc );
3414 release_gl_drawable( gl );
3416 if (escape.gl_drawable)
3417 NtGdiExtEscape( ctx->hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3418 return TRUE;
3421 static struct opengl_funcs opengl_funcs =
3424 glxdrv_wglCopyContext, /* p_wglCopyContext */
3425 glxdrv_wglCreateContext, /* p_wglCreateContext */
3426 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3427 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3428 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3429 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3430 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3431 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3432 glxdrv_wglShareLists, /* p_wglShareLists */
3433 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3437 struct opengl_funcs *get_glx_driver( UINT version )
3439 if (version != WINE_WGL_DRIVER_VERSION)
3441 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3442 return NULL;
3444 if (has_opengl()) return &opengl_funcs;
3445 return NULL;
3448 #else /* no OpenGL includes */
3450 struct opengl_funcs *get_glx_driver( UINT version )
3452 return NULL;
3455 void sync_gl_drawable( HWND hwnd, BOOL known_child )
3459 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3463 void destroy_gl_drawable( HWND hwnd )
3467 #endif /* defined(SONAME_LIBGL) */