winepulse: Set pulse master volume to 0 when session is muted.
[wine.git] / dlls / winex11.drv / opengl.c
blobfd39b58bb3b12f9e20d2a20f1e3bd81876f6b21b
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
5 * Copyright 2005 Alex Woods
6 * Copyright 2005 Raphael Junqueira
7 * Copyright 2006-2009 Roderick Colenbrander
8 * Copyright 2006 Tomas Carnecky
9 * Copyright 2012 Alexandre Julliard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include "wine/port.h"
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_SYS_UN_H
37 #include <sys/un.h>
38 #endif
40 #include "x11drv.h"
41 #include "xcomposite.h"
42 #include "winternl.h"
43 #include "wine/heap.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 struct wgl_pbuffer
214 Drawable drawable;
215 const struct wgl_pixel_format* fmt;
216 int width;
217 int height;
218 int* attribList;
219 int use_render_texture; /* This is also the internal texture format */
220 int texture_bind_target;
221 int texture_bpp;
222 GLint texture_format;
223 GLuint texture_target;
224 GLenum texture_type;
225 GLuint texture;
226 int texture_level;
227 GLXContext tmp_context;
228 GLXContext prev_context;
229 struct list entry;
232 enum dc_gl_type
234 DC_GL_NONE, /* no GL support (pixel format not set yet) */
235 DC_GL_WINDOW, /* normal top-level window */
236 DC_GL_CHILD_WIN, /* child window using XComposite */
237 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
238 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
241 struct gl_drawable
243 LONG ref; /* reference count */
244 enum dc_gl_type type; /* type of GL surface */
245 GLXDrawable drawable; /* drawable for rendering with GL */
246 Window window; /* window if drawable is a GLXWindow */
247 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
248 const struct wgl_pixel_format *format; /* pixel format for the drawable */
249 SIZE pixmap_size; /* pixmap size for GLXPixmap drawables */
250 int swap_interval;
251 BOOL refresh_swap_interval;
252 BOOL mutable_pf;
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 CRITICAL_SECTION context_section;
281 static CRITICAL_SECTION_DEBUG critsect_debug =
283 0, 0, &context_section,
284 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
285 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
287 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
289 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
291 static struct opengl_funcs opengl_funcs;
293 #define USE_GL_FUNC(name) #name,
294 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
295 #undef USE_GL_FUNC
297 static void X11DRV_WineGL_LoadExtensions(void);
298 static void init_pixel_formats( Display *display );
299 static BOOL glxRequireVersion(int requiredVersion);
301 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
302 TRACE( "size %u version %u flags %u type %u color %u %u,%u,%u,%u "
303 "accum %u depth %u stencil %u aux %u ",
304 ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
305 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
306 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
307 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
308 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
309 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
310 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
311 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
312 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
313 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
315 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
316 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
318 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
322 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
323 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
324 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
325 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
326 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
327 #undef TEST_AND_DUMP
328 TRACE("\n");
331 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
332 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
334 /* GLX 1.0 */
335 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
336 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
337 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
338 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
339 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
340 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
341 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
342 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
343 static GLXContext (*pglXGetCurrentContext)( void );
344 static GLXDrawable (*pglXGetCurrentDrawable)( void );
346 /* GLX 1.1 */
347 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
348 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
349 static const char *(*pglXGetClientString)( Display *dpy, int name );
351 /* GLX 1.3 */
352 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
353 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
354 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
355 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
356 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
357 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
358 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
359 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
360 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
361 static GLXPixmap (*pglXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list );
362 static void (*pglXDestroyPixmap)( Display *dpy, GLXPixmap pixmap );
363 static GLXWindow (*pglXCreateWindow)( Display *dpy, GLXFBConfig config, Window win, const int *attrib_list );
364 static void (*pglXDestroyWindow)( Display *dpy, GLXWindow win );
366 /* GLX Extensions */
367 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
368 static void* (*pglXGetProcAddressARB)(const GLubyte *);
369 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
370 static int (*pglXSwapIntervalSGI)(int);
372 /* NV GLX Extension */
373 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
374 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
376 /* MESA GLX Extensions */
377 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
378 static int (*pglXSwapIntervalMESA)(unsigned int interval);
379 static Bool (*pglXQueryCurrentRendererIntegerMESA)(int attribute, unsigned int *value);
380 static const char *(*pglXQueryCurrentRendererStringMESA)(int attribute);
381 static Bool (*pglXQueryRendererIntegerMESA)(Display *dpy, int screen, int renderer, int attribute, unsigned int *value);
382 static const char *(*pglXQueryRendererStringMESA)(Display *dpy, int screen, int renderer, int attribute);
384 /* OpenML GLX Extensions */
385 static Bool (*pglXWaitForSbcOML)( Display *dpy, GLXDrawable drawable,
386 INT64 target_sbc, INT64 *ust, INT64 *msc, INT64 *sbc );
387 static INT64 (*pglXSwapBuffersMscOML)( Display *dpy, GLXDrawable drawable,
388 INT64 target_msc, INT64 divisor, INT64 remainder );
390 /* Standard OpenGL */
391 static void (*pglFinish)(void);
392 static void (*pglFlush)(void);
393 static const GLubyte *(*pglGetString)(GLenum name);
395 static void wglFinish(void);
396 static void wglFlush(void);
397 static const GLubyte *wglGetString(GLenum name);
399 /* check if the extension is present in the list */
400 static BOOL has_extension( const char *list, const char *ext )
402 size_t len = strlen( ext );
404 while (list)
406 while (*list == ' ') list++;
407 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
408 list = strchr( list, ' ' );
410 return FALSE;
413 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
415 /* In the future we might want to find the exact X or GLX error to report back to the app */
416 if (event->request_code != glx_opcode)
417 return 0;
418 return 1;
421 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
423 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
425 int screen = DefaultScreen(gdi_display);
426 Window win = 0, root = 0;
427 const char *gl_version;
428 const char *gl_renderer;
429 const char* str;
430 BOOL glx_direct;
431 XVisualInfo *vis;
432 GLXContext ctx = NULL;
433 XSetWindowAttributes attr;
434 BOOL ret = FALSE;
435 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
437 attr.override_redirect = True;
438 attr.colormap = None;
439 attr.border_pixel = 0;
441 vis = pglXChooseVisual(gdi_display, screen, attribList);
442 if (vis) {
443 #ifdef __i386__
444 WORD old_fs, new_fs;
445 __asm__( "mov %%fs,%0" : "=r" (old_fs) );
446 /* Create a GLX Context. Without one we can't query GL information */
447 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
448 __asm__( "mov %%fs,%0" : "=r" (new_fs) );
449 __asm__( "mov %0,%%fs" :: "r" (old_fs) );
450 if (old_fs != new_fs)
452 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
453 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
454 goto done;
456 #else
457 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
458 #endif
460 if (!ctx) goto done;
462 root = RootWindow( gdi_display, vis->screen );
463 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
464 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
465 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
466 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
467 XMapWindow( gdi_display, win );
468 else
469 win = root;
471 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
473 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
474 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
475 goto done;
477 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
478 gl_version = (const char *)opengl_funcs.gl.p_glGetString(GL_VERSION);
479 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
480 glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
481 strcpy(glExtensions, str);
482 strcat(glExtensions, legacy_extensions);
484 /* Get the common GLX version supported by GLX client and server ( major/minor) */
485 pglXQueryVersion(gdi_display, &glxVersion[0], &glxVersion[1]);
487 glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
488 glx_direct = pglXIsDirect(gdi_display, ctx);
490 TRACE("GL version : %s.\n", gl_version);
491 TRACE("GL renderer : %s.\n", gl_renderer);
492 TRACE("GLX version : %d.%d.\n", glxVersion[0], glxVersion[1]);
493 TRACE("Server GLX version : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VERSION));
494 TRACE("Server GLX vendor: : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VENDOR));
495 TRACE("Client GLX version : %s.\n", pglXGetClientString(gdi_display, GLX_VERSION));
496 TRACE("Client GLX vendor: : %s.\n", pglXGetClientString(gdi_display, GLX_VENDOR));
497 TRACE("Direct rendering enabled: %s\n", glx_direct ? "True" : "False");
499 if(!glx_direct)
501 int fd = ConnectionNumber(gdi_display);
502 struct sockaddr_un uaddr;
503 unsigned int uaddrlen = sizeof(struct sockaddr_un);
505 /* In general indirect rendering on a local X11 server indicates a driver problem.
506 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
508 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
509 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
510 "haven't been installed correctly (using GL renderer %s, version %s).\n",
511 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
512 debugstr_a(gl_version));
514 else
516 /* In general you would expect that if direct rendering is returned, that you receive hardware
517 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
518 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
519 * software rendering, it shows direct rendering.
521 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
522 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
523 * it shows 'Mesa X11'.
525 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
526 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
527 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
528 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
529 debugstr_a(gl_version));
531 ret = TRUE;
533 done:
534 if(vis) XFree(vis);
535 if(ctx) {
536 pglXMakeCurrent(gdi_display, None, NULL);
537 pglXDestroyContext(gdi_display, ctx);
539 if (win != root) XDestroyWindow( gdi_display, win );
540 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
541 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
542 return ret;
545 static void *opengl_handle;
547 static BOOL WINAPI init_opengl( INIT_ONCE *once, void *param, void **context )
549 int error_base, event_base;
550 unsigned int i;
552 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
553 and include all dependencies */
554 opengl_handle = dlopen( SONAME_LIBGL, RTLD_NOW | RTLD_GLOBAL );
555 if (opengl_handle == NULL)
557 ERR( "Failed to load libGL: %s\n", dlerror() );
558 ERR( "OpenGL support is disabled.\n");
559 return TRUE;
562 for (i = 0; i < ARRAY_SIZE( opengl_func_names ); i++)
564 if (!(((void **)&opengl_funcs.gl)[i] = dlsym( opengl_handle, opengl_func_names[i] )))
566 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
567 goto failed;
571 /* redirect some standard OpenGL functions */
572 #define REDIRECT(func) \
573 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
574 REDIRECT( glFinish );
575 REDIRECT( glFlush );
576 REDIRECT( glGetString );
577 #undef REDIRECT
579 pglXGetProcAddressARB = dlsym(opengl_handle, "glXGetProcAddressARB");
580 if (pglXGetProcAddressARB == NULL) {
581 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
582 goto failed;
585 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
587 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
588 goto failed; \
589 } while(0)
591 /* GLX 1.0 */
592 LOAD_FUNCPTR(glXChooseVisual);
593 LOAD_FUNCPTR(glXCopyContext);
594 LOAD_FUNCPTR(glXCreateContext);
595 LOAD_FUNCPTR(glXGetCurrentContext);
596 LOAD_FUNCPTR(glXGetCurrentDrawable);
597 LOAD_FUNCPTR(glXDestroyContext);
598 LOAD_FUNCPTR(glXIsDirect);
599 LOAD_FUNCPTR(glXMakeCurrent);
600 LOAD_FUNCPTR(glXSwapBuffers);
601 LOAD_FUNCPTR(glXQueryVersion);
603 /* GLX 1.1 */
604 LOAD_FUNCPTR(glXGetClientString);
605 LOAD_FUNCPTR(glXQueryExtensionsString);
606 LOAD_FUNCPTR(glXQueryServerString);
608 /* GLX 1.3 */
609 LOAD_FUNCPTR(glXCreatePbuffer);
610 LOAD_FUNCPTR(glXCreateNewContext);
611 LOAD_FUNCPTR(glXDestroyPbuffer);
612 LOAD_FUNCPTR(glXMakeContextCurrent);
613 LOAD_FUNCPTR(glXGetFBConfigs);
614 LOAD_FUNCPTR(glXCreatePixmap);
615 LOAD_FUNCPTR(glXDestroyPixmap);
616 LOAD_FUNCPTR(glXCreateWindow);
617 LOAD_FUNCPTR(glXDestroyWindow);
618 #undef LOAD_FUNCPTR
620 /* It doesn't matter if these fail. They'll only be used if the driver reports
621 the associated extension is available (and if a driver reports the extension
622 is available but fails to provide the functions, it's quite broken) */
623 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
624 /* ARB GLX Extension */
625 LOAD_FUNCPTR(glXCreateContextAttribsARB);
626 /* EXT GLX Extension */
627 LOAD_FUNCPTR(glXSwapIntervalEXT);
628 /* MESA GLX Extension */
629 LOAD_FUNCPTR(glXSwapIntervalMESA);
630 /* SGI GLX Extension */
631 LOAD_FUNCPTR(glXSwapIntervalSGI);
632 /* NV GLX Extension */
633 LOAD_FUNCPTR(glXAllocateMemoryNV);
634 LOAD_FUNCPTR(glXFreeMemoryNV);
635 #undef LOAD_FUNCPTR
637 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
639 if (XQueryExtension( gdi_display, "GLX", &glx_opcode, &event_base, &error_base ))
641 TRACE("GLX is up and running error_base = %d\n", error_base);
642 } else {
643 ERR( "GLX extension is missing, disabling OpenGL.\n" );
644 goto failed;
646 gl_hwnd_context = XUniqueContext();
647 gl_pbuffer_context = XUniqueContext();
649 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
650 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
651 * rendering is used.
653 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
654 * depends on the reported GLX client / server version and on the client / server extension list.
655 * Those don't have to be the same.
657 * In general the server GLX information lists the capabilities in case of indirect rendering.
658 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
659 * available and in that case the client GLX informat can be used.
660 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
661 * in the GLX version/extension list. When a program does this it works for certain for both
662 * direct and indirect rendering.
664 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
665 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
666 * extension list which causes this extension not to be listed. (Wine requires this extension).
667 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
668 * pbuffers is around.
670 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
671 * the ATI drivers and from then on use GLX client information for them.
674 if(glxRequireVersion(3)) {
675 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
676 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
677 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
678 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
679 } else if (has_extension( glxExtensions, "GLX_SGIX_fbconfig")) {
680 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
681 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
682 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
684 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
685 * enable this function when the Xserver understand GLX 1.3 or newer
687 pglXQueryDrawable = NULL;
688 } else if(strcmp("ATI", pglXGetClientString(gdi_display, GLX_VENDOR)) == 0) {
689 TRACE("Overriding ATI GLX capabilities!\n");
690 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
691 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
692 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
693 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
695 /* Use client GLX information in case of the ATI drivers. We override the
696 * capabilities over here and not somewhere else as ATI might better their
697 * life in the future. In case they release proper drivers this block of
698 * code won't be called. */
699 glxExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
700 } else {
701 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n",
702 pglXQueryServerString(gdi_display, DefaultScreen(gdi_display), GLX_VERSION));
705 if (has_extension( glxExtensions, "GLX_MESA_copy_sub_buffer")) {
706 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
709 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
711 pglXQueryCurrentRendererIntegerMESA = pglXGetProcAddressARB(
712 (const GLubyte *)"glXQueryCurrentRendererIntegerMESA" );
713 pglXQueryCurrentRendererStringMESA = pglXGetProcAddressARB(
714 (const GLubyte *)"glXQueryCurrentRendererStringMESA" );
715 pglXQueryRendererIntegerMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererIntegerMESA" );
716 pglXQueryRendererStringMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererStringMESA" );
719 if (has_extension( glxExtensions, "GLX_OML_sync_control" ))
721 pglXWaitForSbcOML = pglXGetProcAddressARB( (const GLubyte *)"glXWaitForSbcOML" );
722 pglXSwapBuffersMscOML = pglXGetProcAddressARB( (const GLubyte *)"glXSwapBuffersMscOML" );
725 X11DRV_WineGL_LoadExtensions();
726 init_pixel_formats( gdi_display );
727 return TRUE;
729 failed:
730 dlclose(opengl_handle);
731 opengl_handle = NULL;
732 return TRUE;
735 static BOOL has_opengl(void)
737 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
738 InitOnceExecuteOnce(&init_once, init_opengl, NULL, NULL);
739 return opengl_handle != NULL;
742 static const char *debugstr_fbconfig( GLXFBConfig fbconfig )
744 int id, visual, drawable;
746 if (pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_FBCONFIG_ID, &id ))
747 return "*** invalid fbconfig";
748 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_VISUAL_ID, &visual );
749 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &drawable );
750 return wine_dbg_sprintf( "fbconfig %#x visual id %#x drawable type %#x", id, visual, drawable );
753 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
754 int nAttribs = 0;
755 unsigned cur = 0;
756 int attr, pop;
757 int drawattrib = 0;
758 int nvfloatattrib = GLX_DONT_CARE;
759 int pixelattrib = GLX_DONT_CARE;
761 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
762 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
763 while (iWGLAttr && 0 != iWGLAttr[cur]) {
764 attr = iWGLAttr[cur];
765 TRACE("pAttr[%d] = %x\n", cur, attr);
766 pop = iWGLAttr[++cur];
768 switch (attr) {
769 case WGL_AUX_BUFFERS_ARB:
770 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
771 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
772 break;
773 case WGL_COLOR_BITS_ARB:
774 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
775 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
776 break;
777 case WGL_BLUE_BITS_ARB:
778 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
779 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
780 break;
781 case WGL_RED_BITS_ARB:
782 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
783 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
784 break;
785 case WGL_GREEN_BITS_ARB:
786 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
787 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
788 break;
789 case WGL_ALPHA_BITS_ARB:
790 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
791 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
792 break;
793 case WGL_DEPTH_BITS_ARB:
794 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
795 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
796 break;
797 case WGL_STENCIL_BITS_ARB:
798 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
799 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
800 break;
801 case WGL_DOUBLE_BUFFER_ARB:
802 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
803 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
804 break;
805 case WGL_STEREO_ARB:
806 PUSH2(oGLXAttr, GLX_STEREO, pop);
807 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
808 break;
810 case WGL_PIXEL_TYPE_ARB:
811 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
812 switch (pop) {
813 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
814 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
815 /* 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 */
816 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
817 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
818 default:
819 ERR("unexpected PixelType(%x)\n", pop);
821 break;
823 case WGL_SUPPORT_GDI_ARB:
824 /* This flag is set in a pixel format */
825 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
826 break;
828 case WGL_DRAW_TO_BITMAP_ARB:
829 /* This flag is set in a pixel format */
830 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
831 break;
833 case WGL_DRAW_TO_WINDOW_ARB:
834 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
835 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
836 if (pop) {
837 drawattrib |= GLX_WINDOW_BIT;
839 break;
841 case WGL_DRAW_TO_PBUFFER_ARB:
842 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
843 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
844 if (pop) {
845 drawattrib |= GLX_PBUFFER_BIT;
847 break;
849 case WGL_ACCELERATION_ARB:
850 /* This flag is set in a pixel format */
851 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
852 break;
854 case WGL_SUPPORT_OPENGL_ARB:
855 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
856 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
857 break;
859 case WGL_SWAP_METHOD_ARB:
860 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
861 if (has_swap_method)
863 switch (pop)
865 case WGL_SWAP_EXCHANGE_ARB:
866 pop = GLX_SWAP_EXCHANGE_OML;
867 break;
868 case WGL_SWAP_COPY_ARB:
869 pop = GLX_SWAP_COPY_OML;
870 break;
871 case WGL_SWAP_UNDEFINED_ARB:
872 pop = GLX_SWAP_UNDEFINED_OML;
873 break;
874 default:
875 ERR("Unexpected swap method %#x.\n", pop);
876 pop = GLX_DONT_CARE;
878 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
880 else
882 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
884 break;
886 case WGL_PBUFFER_LARGEST_ARB:
887 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
888 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
889 break;
891 case WGL_SAMPLE_BUFFERS_ARB:
892 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
893 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
894 break;
896 case WGL_SAMPLES_ARB:
897 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
898 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
899 break;
901 case WGL_TEXTURE_FORMAT_ARB:
902 case WGL_TEXTURE_TARGET_ARB:
903 case WGL_MIPMAP_TEXTURE_ARB:
904 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur - 1], iWGLAttr[cur]);
905 if (NULL == pbuf) {
906 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
908 if (!use_render_texture_emulation) {
909 if (WGL_NO_TEXTURE_ARB != pop) {
910 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
911 return -1; /** error: don't support it */
912 } else {
913 drawattrib |= GLX_PBUFFER_BIT;
916 break ;
917 case WGL_FLOAT_COMPONENTS_NV:
918 nvfloatattrib = pop;
919 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
920 break ;
921 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
922 case WGL_BIND_TO_TEXTURE_RGB_ARB:
923 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
924 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
925 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
926 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
927 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
928 /** cannot be converted, see direct handling on
929 * - wglGetPixelFormatAttribivARB
930 * TODO: wglChoosePixelFormat
932 break ;
933 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
934 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
935 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
936 break ;
938 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
939 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
940 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
941 break ;
942 default:
943 FIXME("unsupported %x WGL Attribute\n", attr);
944 break;
946 ++cur;
949 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
950 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
951 * pixmap and pbuffer formats appear as well. */
952 if (!drawattrib) drawattrib = GLX_DONT_CARE;
953 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
954 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
956 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
957 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
958 * GLX_DONT_CARE unless it is overridden. */
959 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
960 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
962 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
963 if (has_extension(glxExtensions, "GLX_NV_float_buffer")) {
964 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
965 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
968 return nAttribs;
971 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
973 int render_type, render_type_bit;
974 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
975 switch(render_type_bit)
977 case GLX_RGBA_BIT:
978 render_type = GLX_RGBA_TYPE;
979 break;
980 case GLX_COLOR_INDEX_BIT:
981 render_type = GLX_COLOR_INDEX_TYPE;
982 break;
983 case GLX_RGBA_FLOAT_BIT:
984 render_type = GLX_RGBA_FLOAT_TYPE;
985 break;
986 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
987 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
988 break;
989 default:
990 ERR("Unknown render_type: %x\n", render_type_bit);
991 render_type = 0;
993 return render_type;
996 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
997 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
999 int dbuf, value;
1000 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
1001 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1003 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1004 * the GLX_PIXMAP_BIT set. */
1005 return !dbuf && (value & GLX_PIXMAP_BIT);
1008 static void init_pixel_formats( Display *display )
1010 struct wgl_pixel_format *list;
1011 int size = 0, onscreen_size = 0;
1012 int fmt_id, nCfgs, i, run, bmp_formats;
1013 GLXFBConfig* cfgs;
1014 XVisualInfo *visinfo;
1016 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1017 if (NULL == cfgs || 0 == nCfgs) {
1018 if(cfgs != NULL) XFree(cfgs);
1019 ERR("glXChooseFBConfig returns NULL\n");
1020 return;
1023 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1024 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1025 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1026 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1028 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1030 for(i=0, bmp_formats=0; i<nCfgs; i++)
1032 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1033 bmp_formats++;
1035 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1037 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1039 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1040 * Do this as GLX doesn't guarantee that the list is sorted */
1041 for(run=0; run < 2; run++)
1043 for(i=0; i<nCfgs; i++) {
1044 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1045 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1047 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1048 * The second run we only set offscreen formats. */
1049 if(!run && visinfo)
1051 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1052 * The contents is copied to the destination using XCopyArea. For the copying to work
1053 * the depth of the source and destination window should be the same. In general this should
1054 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1055 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1056 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1057 * list formats with the same depth. */
1058 if(visinfo->depth != default_visual.depth)
1060 XFree(visinfo);
1061 continue;
1064 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1065 list[size].fbconfig = cfgs[i];
1066 list[size].visual = visinfo;
1067 list[size].fmt_id = fmt_id;
1068 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1069 list[size].dwFlags = 0;
1070 size++;
1071 onscreen_size++;
1073 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1074 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1076 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1077 list[size].fbconfig = cfgs[i];
1078 list[size].visual = visinfo;
1079 list[size].fmt_id = fmt_id;
1080 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1081 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1082 size++;
1083 onscreen_size++;
1085 } else if(run && !visinfo) {
1086 int window_drawable=0;
1087 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1089 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1090 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1091 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1092 * likely make our child window opengl rendering more complicated since likely you can't use
1093 * XCopyArea on a GLX Window.
1094 * For now ignore fbconfigs which are window drawable but lack a visual. */
1095 if(window_drawable & GLX_WINDOW_BIT)
1097 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1098 continue;
1101 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1102 list[size].fbconfig = cfgs[i];
1103 list[size].fmt_id = fmt_id;
1104 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1105 list[size].dwFlags = 0;
1106 size++;
1108 else if (visinfo) XFree(visinfo);
1112 XFree(cfgs);
1114 pixel_formats = list;
1115 nb_pixel_formats = size;
1116 nb_onscreen_formats = onscreen_size;
1119 static inline BOOL is_valid_pixel_format( int format )
1121 return format > 0 && format <= nb_pixel_formats;
1124 static inline BOOL is_onscreen_pixel_format( int format )
1126 return format > 0 && format <= nb_onscreen_formats;
1129 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1131 return format - pixel_formats + 1;
1134 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1135 * In our WGL implementation we only support a subset of these formats namely the format of
1136 * Wine's main visual and offscreen formats (if they are available).
1137 * This function converts a WGL format to its corresponding GLX one.
1139 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1141 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1142 * iPixelFormat in case of probing the number of pixelformats.
1144 if (is_valid_pixel_format( iPixelFormat ) &&
1145 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1146 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1147 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1148 return &pixel_formats[iPixelFormat-1];
1150 return NULL;
1153 static struct gl_drawable *grab_gl_drawable( struct gl_drawable *gl )
1155 InterlockedIncrement( &gl->ref );
1156 return gl;
1159 static void release_gl_drawable( struct gl_drawable *gl )
1161 if (!gl) return;
1162 if (InterlockedDecrement( &gl->ref )) return;
1163 switch (gl->type)
1165 case DC_GL_WINDOW:
1166 case DC_GL_CHILD_WIN:
1167 TRACE( "destroying %lx drawable %lx\n", gl->window, gl->drawable );
1168 pglXDestroyWindow( gdi_display, gl->drawable );
1169 XDestroyWindow( gdi_display, gl->window );
1170 break;
1171 case DC_GL_PIXMAP_WIN:
1172 TRACE( "destroying pixmap %lx drawable %lx\n", gl->pixmap, gl->drawable );
1173 pglXDestroyPixmap( gdi_display, gl->drawable );
1174 XFreePixmap( gdi_display, gl->pixmap );
1175 break;
1176 default:
1177 break;
1179 HeapFree( GetProcessHeap(), 0, gl );
1182 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1183 static void mark_drawable_dirty( struct gl_drawable *old, struct gl_drawable *new )
1185 struct wgl_context *ctx;
1187 EnterCriticalSection( &context_section );
1188 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1190 if (old == ctx->drawables[0] || old == ctx->new_drawables[0])
1192 release_gl_drawable( ctx->new_drawables[0] );
1193 ctx->new_drawables[0] = grab_gl_drawable( new );
1195 if (old == ctx->drawables[1] || old == ctx->new_drawables[1])
1197 release_gl_drawable( ctx->new_drawables[1] );
1198 ctx->new_drawables[1] = grab_gl_drawable( new );
1201 LeaveCriticalSection( &context_section );
1204 /* Given the current context, make sure its drawable is sync'd */
1205 static inline void sync_context(struct wgl_context *context)
1207 BOOL refresh = FALSE;
1209 EnterCriticalSection( &context_section );
1210 if (context->new_drawables[0])
1212 release_gl_drawable( context->drawables[0] );
1213 context->drawables[0] = context->new_drawables[0];
1214 context->new_drawables[0] = NULL;
1215 refresh = TRUE;
1217 if (context->new_drawables[1])
1219 release_gl_drawable( context->drawables[1] );
1220 context->drawables[1] = context->new_drawables[1];
1221 context->new_drawables[1] = NULL;
1222 refresh = TRUE;
1224 if (refresh)
1226 if (glxRequireVersion(3))
1227 pglXMakeContextCurrent(gdi_display, context->drawables[0]->drawable,
1228 context->drawables[1]->drawable, context->ctx);
1229 else
1230 pglXMakeCurrent(gdi_display, context->drawables[0]->drawable, context->ctx);
1232 LeaveCriticalSection( &context_section );
1235 static BOOL set_swap_interval(GLXDrawable drawable, int interval)
1237 BOOL ret = TRUE;
1239 switch (swap_control_method)
1241 case GLX_SWAP_CONTROL_EXT:
1242 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1243 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1244 XSync(gdi_display, False);
1245 ret = !X11DRV_check_error();
1246 break;
1248 case GLX_SWAP_CONTROL_MESA:
1249 ret = !pglXSwapIntervalMESA(interval);
1250 break;
1252 case GLX_SWAP_CONTROL_SGI:
1253 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1254 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1255 * value to be an error. Just silently ignore the request for now.
1257 if (!interval)
1258 WARN("Request to disable vertical sync is not handled\n");
1259 else
1260 ret = !pglXSwapIntervalSGI(interval);
1261 break;
1263 case GLX_SWAP_CONTROL_NONE:
1264 /* Unlikely to happen on modern GLX implementations */
1265 WARN("Request to adjust swap interval is not handled\n");
1266 break;
1269 return ret;
1272 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1274 struct gl_drawable *gl;
1276 EnterCriticalSection( &context_section );
1277 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1278 gl = grab_gl_drawable( gl );
1279 else if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
1280 gl = grab_gl_drawable( gl );
1281 else
1282 gl = NULL;
1283 LeaveCriticalSection( &context_section );
1284 return gl;
1287 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1289 GLXContext ctx;
1291 if(context->gl3_context)
1293 if(context->numAttribs)
1294 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, context->attribList);
1295 else
1296 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, NULL);
1298 else if(context->fmt->visual)
1299 ctx = pglXCreateContext(gdi_display, context->fmt->visual, shareList, GL_TRUE);
1300 else /* Create a GLX Context for a pbuffer */
1301 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1303 return ctx;
1307 /***********************************************************************
1308 * create_gl_drawable
1310 static struct gl_drawable *create_gl_drawable( HWND hwnd, const struct wgl_pixel_format *format, BOOL known_child,
1311 BOOL mutable_pf )
1313 struct gl_drawable *gl, *prev;
1314 XVisualInfo *visual = format->visual;
1315 RECT rect;
1316 int width, height;
1318 GetClientRect( hwnd, &rect );
1319 width = min( max( 1, rect.right ), 65535 );
1320 height = min( max( 1, rect.bottom ), 65535 );
1322 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) ))) return NULL;
1324 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1325 * there is no way to query it, so we have to store it here.
1327 gl->swap_interval = 1;
1328 gl->refresh_swap_interval = TRUE;
1329 gl->format = format;
1330 gl->ref = 1;
1331 gl->mutable_pf = mutable_pf;
1333 if (!known_child && !GetWindow( hwnd, GW_CHILD ) && GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* 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 WARN("XComposite is not available, using GLXPixmap hack\n");
1358 gl->type = DC_GL_PIXMAP_WIN;
1359 gl->pixmap = XCreatePixmap( gdi_display, root_window, width, height, visual->depth );
1360 if (gl->pixmap)
1362 gl->drawable = pglXCreatePixmap( gdi_display, gl->format->fbconfig, gl->pixmap, NULL );
1363 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1364 gl->pixmap_size.cx = width;
1365 gl->pixmap_size.cy = height;
1369 if (!gl->drawable)
1371 HeapFree( GetProcessHeap(), 0, gl );
1372 return NULL;
1375 EnterCriticalSection( &context_section );
1376 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1378 gl->swap_interval = prev->swap_interval;
1379 release_gl_drawable( prev );
1381 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)grab_gl_drawable(gl) );
1382 LeaveCriticalSection( &context_section );
1383 return gl;
1387 /***********************************************************************
1388 * set_win_format
1390 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format, BOOL mutable_pf )
1392 struct gl_drawable *gl;
1394 if (!format->visual) return FALSE;
1396 if (!(gl = create_gl_drawable( hwnd, format, FALSE, mutable_pf ))) return FALSE;
1398 TRACE( "created GL drawable %lx for win %p %s\n",
1399 gl->drawable, hwnd, debugstr_fbconfig( format->fbconfig ));
1401 XFlush( gdi_display );
1402 release_gl_drawable( gl );
1404 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1405 return TRUE;
1409 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1411 const struct wgl_pixel_format *fmt;
1412 int value;
1413 HWND hwnd = WindowFromDC( hdc );
1415 TRACE("(%p,%d)\n", hdc, format);
1417 if (!hwnd || hwnd == GetDesktopWindow())
1419 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1420 return FALSE;
1423 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1424 if (!fmt)
1426 ERR( "Invalid format %d\n", format );
1427 return FALSE;
1430 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1431 if (!(value & GLX_WINDOW_BIT))
1433 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1434 return FALSE;
1437 if (!allow_change)
1439 struct gl_drawable *gl;
1440 if ((gl = get_gl_drawable( hwnd, hdc )))
1442 int prev = pixel_format_index( gl->format );
1443 BOOL mutable_pf = gl->mutable_pf;
1444 release_gl_drawable( gl );
1445 if (!mutable_pf)
1446 return prev == format; /* cannot change it if already set */
1450 return set_win_format( hwnd, fmt, allow_change );
1454 /***********************************************************************
1455 * sync_gl_drawable
1457 void sync_gl_drawable( HWND hwnd, BOOL known_child )
1459 struct gl_drawable *old, *new;
1461 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1463 switch (old->type)
1465 case DC_GL_WINDOW:
1466 if (!known_child) break; /* Still a childless top-level window */
1467 /* fall through */
1468 case DC_GL_PIXMAP_WIN:
1469 if (!(new = create_gl_drawable( hwnd, old->format, known_child, old->mutable_pf ))) break;
1470 mark_drawable_dirty( old, new );
1471 XFlush( gdi_display );
1472 TRACE( "Recreated GL drawable %lx to replace %lx\n", new->drawable, old->drawable );
1473 release_gl_drawable( new );
1474 break;
1475 default:
1476 break;
1478 release_gl_drawable( old );
1482 /***********************************************************************
1483 * set_gl_drawable_parent
1485 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1487 struct gl_drawable *old, *new;
1489 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1491 TRACE( "setting drawable %lx parent %p\n", old->drawable, parent );
1493 switch (old->type)
1495 case DC_GL_WINDOW:
1496 break;
1497 case DC_GL_CHILD_WIN:
1498 case DC_GL_PIXMAP_WIN:
1499 if (parent == GetDesktopWindow()) break;
1500 /* fall through */
1501 default:
1502 release_gl_drawable( old );
1503 return;
1506 if ((new = create_gl_drawable( hwnd, old->format, FALSE, old->mutable_pf )))
1508 mark_drawable_dirty( old, new );
1509 release_gl_drawable( new );
1511 else
1513 destroy_gl_drawable( hwnd );
1514 __wine_set_pixel_format( hwnd, 0 );
1516 release_gl_drawable( old );
1520 /***********************************************************************
1521 * destroy_gl_drawable
1523 void destroy_gl_drawable( HWND hwnd )
1525 struct gl_drawable *gl;
1527 EnterCriticalSection( &context_section );
1528 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1530 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1531 release_gl_drawable( gl );
1533 LeaveCriticalSection( &context_section );
1538 * glxdrv_DescribePixelFormat
1540 * Get the pixel-format descriptor associated to the given id
1542 static int WINAPI describe_pixel_format( int iPixelFormat, PIXELFORMATDESCRIPTOR *ppfd, BOOL allow_offscreen )
1544 /*XVisualInfo *vis;*/
1545 int value;
1546 int rb,gb,bb,ab;
1547 const struct wgl_pixel_format *fmt;
1549 if (!has_opengl()) return 0;
1551 /* 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 */
1552 fmt = get_pixel_format(gdi_display, iPixelFormat, allow_offscreen);
1553 if (!fmt) {
1554 WARN("unexpected format %d\n", iPixelFormat);
1555 return 0;
1558 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1559 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1560 ppfd->nVersion = 1;
1562 /* These flags are always the same... */
1563 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1564 /* Now the flags extracted from the Visual */
1566 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1567 if(value & GLX_WINDOW_BIT)
1568 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1570 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1571 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1572 * 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
1573 * offered the GDI bit either. */
1574 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1576 /* PFD_GENERIC_FORMAT - gdi software rendering
1577 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1578 * none set - full hardware accelerated by a ICD
1580 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1581 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1583 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1584 if (value) {
1585 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1586 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1588 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1590 /* Pixel type */
1591 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1592 if (value & GLX_RGBA_BIT)
1593 ppfd->iPixelType = PFD_TYPE_RGBA;
1594 else
1595 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1597 /* Color bits */
1598 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1599 ppfd->cColorBits = value;
1601 /* Red, green, blue and alpha bits / shifts */
1602 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1603 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1604 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1605 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1606 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1608 ppfd->cRedBits = rb;
1609 ppfd->cRedShift = gb + bb + ab;
1610 ppfd->cBlueBits = bb;
1611 ppfd->cBlueShift = ab;
1612 ppfd->cGreenBits = gb;
1613 ppfd->cGreenShift = bb + ab;
1614 ppfd->cAlphaBits = ab;
1615 ppfd->cAlphaShift = 0;
1616 } else {
1617 ppfd->cRedBits = 0;
1618 ppfd->cRedShift = 0;
1619 ppfd->cBlueBits = 0;
1620 ppfd->cBlueShift = 0;
1621 ppfd->cGreenBits = 0;
1622 ppfd->cGreenShift = 0;
1623 ppfd->cAlphaBits = 0;
1624 ppfd->cAlphaShift = 0;
1627 /* Accum RGBA bits */
1628 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1629 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1630 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1631 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1633 ppfd->cAccumBits = rb+gb+bb+ab;
1634 ppfd->cAccumRedBits = rb;
1635 ppfd->cAccumGreenBits = gb;
1636 ppfd->cAccumBlueBits = bb;
1637 ppfd->cAccumAlphaBits = ab;
1639 /* Aux bits */
1640 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1641 ppfd->cAuxBuffers = value;
1643 /* Depth bits */
1644 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1645 ppfd->cDepthBits = value;
1647 /* stencil bits */
1648 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1649 ppfd->cStencilBits = value;
1651 ppfd->iLayerType = PFD_MAIN_PLANE;
1653 if (TRACE_ON(wgl)) {
1654 dump_PIXELFORMATDESCRIPTOR(ppfd);
1657 return nb_onscreen_formats;
1661 * glxdrv_DescribePixelFormat
1663 * Get the pixel-format descriptor associated to the given id
1665 static int WINAPI glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1666 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1668 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1670 if (!ppfd) return nb_onscreen_formats;
1672 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR))
1674 ERR("Wrong structure size !\n");
1675 /* Should set error */
1676 return 0;
1679 return describe_pixel_format(iPixelFormat, ppfd, FALSE);
1682 /***********************************************************************
1683 * glxdrv_wglGetPixelFormat
1685 static int WINAPI glxdrv_wglGetPixelFormat( HDC hdc )
1687 struct gl_drawable *gl;
1688 int ret = 0;
1690 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1692 ret = pixel_format_index( gl->format );
1693 /* Offscreen formats can't be used with traditional WGL calls.
1694 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1695 if (!is_onscreen_pixel_format( ret )) ret = 1;
1696 release_gl_drawable( gl );
1698 TRACE( "%p -> %d\n", hdc, ret );
1699 return ret;
1702 /***********************************************************************
1703 * glxdrv_wglSetPixelFormat
1705 static BOOL WINAPI glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1707 return set_pixel_format(hdc, iPixelFormat, FALSE);
1710 /***********************************************************************
1711 * glxdrv_wglCopyContext
1713 static BOOL WINAPI glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1715 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1717 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1719 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1720 return TRUE;
1723 /***********************************************************************
1724 * glxdrv_wglCreateContext
1726 static struct wgl_context * WINAPI glxdrv_wglCreateContext( HDC hdc )
1728 struct wgl_context *ret;
1729 struct gl_drawable *gl;
1731 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1733 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1734 return NULL;
1737 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1739 ret->hdc = hdc;
1740 ret->fmt = gl->format;
1741 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1742 EnterCriticalSection( &context_section );
1743 list_add_head( &context_list, &ret->entry );
1744 LeaveCriticalSection( &context_section );
1746 release_gl_drawable( gl );
1747 TRACE( "%p -> %p\n", hdc, ret );
1748 return ret;
1751 /***********************************************************************
1752 * glxdrv_wglDeleteContext
1754 static BOOL WINAPI glxdrv_wglDeleteContext(struct wgl_context *ctx)
1756 struct wgl_pbuffer *pb;
1758 TRACE("(%p)\n", ctx);
1760 EnterCriticalSection( &context_section );
1761 list_remove( &ctx->entry );
1762 LIST_FOR_EACH_ENTRY( pb, &pbuffer_list, struct wgl_pbuffer, entry )
1764 if (pb->prev_context == ctx->ctx) {
1765 pglXDestroyContext(gdi_display, pb->tmp_context);
1766 pb->prev_context = pb->tmp_context = NULL;
1769 LeaveCriticalSection( &context_section );
1771 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1772 release_gl_drawable( ctx->drawables[0] );
1773 release_gl_drawable( ctx->drawables[1] );
1774 release_gl_drawable( ctx->new_drawables[0] );
1775 release_gl_drawable( ctx->new_drawables[1] );
1776 return HeapFree( GetProcessHeap(), 0, ctx );
1779 /***********************************************************************
1780 * glxdrv_wglGetProcAddress
1782 static PROC WINAPI glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1784 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1785 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1788 static void set_context_drawables( struct wgl_context *ctx, struct gl_drawable *draw,
1789 struct gl_drawable *read )
1791 struct gl_drawable *prev[4];
1792 int i;
1794 prev[0] = ctx->drawables[0];
1795 prev[1] = ctx->drawables[1];
1796 prev[2] = ctx->new_drawables[0];
1797 prev[3] = ctx->new_drawables[1];
1798 ctx->drawables[0] = grab_gl_drawable( draw );
1799 ctx->drawables[1] = read ? grab_gl_drawable( read ) : NULL;
1800 ctx->new_drawables[0] = ctx->new_drawables[1] = NULL;
1801 for (i = 0; i < 4; i++) release_gl_drawable( prev[i] );
1804 /***********************************************************************
1805 * glxdrv_wglMakeCurrent
1807 static BOOL WINAPI glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1809 BOOL ret = FALSE;
1810 struct gl_drawable *gl;
1812 TRACE("(%p,%p)\n", hdc, ctx);
1814 if (!ctx)
1816 pglXMakeCurrent(gdi_display, None, NULL);
1817 NtCurrentTeb()->glContext = NULL;
1818 return TRUE;
1821 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1823 if (ctx->fmt != gl->format)
1825 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1826 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1827 goto done;
1830 TRACE("hdc %p drawable %lx fmt %p ctx %p %s\n", hdc, gl->drawable, gl->format, ctx->ctx,
1831 debugstr_fbconfig( gl->format->fbconfig ));
1833 EnterCriticalSection( &context_section );
1834 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1835 if (ret)
1837 NtCurrentTeb()->glContext = ctx;
1838 ctx->has_been_current = TRUE;
1839 ctx->hdc = hdc;
1840 set_context_drawables( ctx, gl, gl );
1841 ctx->refresh_drawables = FALSE;
1842 LeaveCriticalSection( &context_section );
1843 goto done;
1845 LeaveCriticalSection( &context_section );
1847 SetLastError( ERROR_INVALID_HANDLE );
1849 done:
1850 release_gl_drawable( gl );
1851 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1852 return ret;
1855 /***********************************************************************
1856 * X11DRV_wglMakeContextCurrentARB
1858 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1860 BOOL ret = FALSE;
1861 struct gl_drawable *draw_gl, *read_gl = NULL;
1863 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1865 if (!ctx)
1867 pglXMakeCurrent(gdi_display, None, NULL);
1868 NtCurrentTeb()->glContext = NULL;
1869 return TRUE;
1872 if (!pglXMakeContextCurrent) return FALSE;
1874 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1876 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1878 EnterCriticalSection( &context_section );
1879 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1880 read_gl ? read_gl->drawable : 0, ctx->ctx);
1881 if (ret)
1883 ctx->has_been_current = TRUE;
1884 ctx->hdc = draw_hdc;
1885 set_context_drawables( ctx, draw_gl, read_gl );
1886 ctx->refresh_drawables = FALSE;
1887 NtCurrentTeb()->glContext = ctx;
1888 LeaveCriticalSection( &context_section );
1889 goto done;
1891 LeaveCriticalSection( &context_section );
1893 SetLastError( ERROR_INVALID_HANDLE );
1894 done:
1895 release_gl_drawable( read_gl );
1896 release_gl_drawable( draw_gl );
1897 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1898 return ret;
1901 /***********************************************************************
1902 * glxdrv_wglShareLists
1904 static BOOL WINAPI glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1906 TRACE("(%p, %p)\n", org, dest);
1908 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1909 * at context creation time but in case of WGL it is done using wglShareLists.
1910 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1911 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1912 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1913 * so there delaying context creation doesn't work.
1915 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1916 * and when a program requests sharing we recreate the destination context if it hasn't been made
1917 * current or when it hasn't shared display lists before.
1920 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1922 ERR("Could not share display lists, one of the contexts has been current already !\n");
1923 return FALSE;
1925 else if(dest->sharing)
1927 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1928 return FALSE;
1930 else
1932 /* Re-create the GLX context and share display lists */
1933 pglXDestroyContext(gdi_display, dest->ctx);
1934 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1935 TRACE(" re-created context (%p) for Wine context %p (%s) sharing lists with ctx %p (%s)\n",
1936 dest->ctx, dest, debugstr_fbconfig(dest->fmt->fbconfig),
1937 org->ctx, debugstr_fbconfig( org->fmt->fbconfig));
1939 org->sharing = TRUE;
1940 dest->sharing = TRUE;
1941 return TRUE;
1943 return FALSE;
1946 static void wglFinish(void)
1948 struct x11drv_escape_flush_gl_drawable escape;
1949 struct gl_drawable *gl;
1950 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1952 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1953 escape.gl_drawable = 0;
1954 escape.flush = FALSE;
1956 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1958 switch (gl->type)
1960 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1961 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
1962 default: break;
1964 sync_context(ctx);
1965 release_gl_drawable( gl );
1968 pglFinish();
1969 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1972 static void wglFlush(void)
1974 struct x11drv_escape_flush_gl_drawable escape;
1975 struct gl_drawable *gl;
1976 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1978 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1979 escape.gl_drawable = 0;
1980 escape.flush = FALSE;
1982 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1984 switch (gl->type)
1986 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1987 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
1988 default: break;
1990 sync_context(ctx);
1991 release_gl_drawable( gl );
1994 pglFlush();
1995 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1998 static const GLubyte *wglGetString(GLenum name)
2000 if (name == GL_EXTENSIONS && glExtensions) return (const GLubyte *)glExtensions;
2001 return pglGetString(name);
2004 /***********************************************************************
2005 * X11DRV_wglCreateContextAttribsARB
2007 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2008 const int* attribList )
2010 struct wgl_context *ret;
2011 struct gl_drawable *gl;
2012 int err = 0;
2014 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2016 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2018 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2019 return NULL;
2022 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2024 ret->hdc = hdc;
2025 ret->fmt = gl->format;
2026 ret->gl3_context = TRUE;
2027 if (attribList)
2029 int *pContextAttribList = &ret->attribList[0];
2030 /* attribList consists of pairs {token, value] terminated with 0 */
2031 while(attribList[0] != 0)
2033 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2034 switch(attribList[0])
2036 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2037 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2038 pContextAttribList[1] = attribList[1];
2039 pContextAttribList += 2;
2040 ret->numAttribs++;
2041 break;
2042 case WGL_CONTEXT_MINOR_VERSION_ARB:
2043 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2044 pContextAttribList[1] = attribList[1];
2045 pContextAttribList += 2;
2046 ret->numAttribs++;
2047 break;
2048 case WGL_CONTEXT_LAYER_PLANE_ARB:
2049 break;
2050 case WGL_CONTEXT_FLAGS_ARB:
2051 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2052 pContextAttribList[1] = attribList[1];
2053 pContextAttribList += 2;
2054 ret->numAttribs++;
2055 break;
2056 case WGL_CONTEXT_OPENGL_NO_ERROR_ARB:
2057 pContextAttribList[0] = GLX_CONTEXT_OPENGL_NO_ERROR_ARB;
2058 pContextAttribList[1] = attribList[1];
2059 pContextAttribList += 2;
2060 ret->numAttribs++;
2061 break;
2062 case WGL_CONTEXT_PROFILE_MASK_ARB:
2063 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2064 pContextAttribList[1] = attribList[1];
2065 pContextAttribList += 2;
2066 ret->numAttribs++;
2067 break;
2068 case WGL_RENDERER_ID_WINE:
2069 pContextAttribList[0] = GLX_RENDERER_ID_MESA;
2070 pContextAttribList[1] = attribList[1];
2071 pContextAttribList += 2;
2072 ret->numAttribs++;
2073 break;
2074 default:
2075 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2077 attribList += 2;
2081 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2082 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2083 XSync(gdi_display, False);
2084 if ((err = X11DRV_check_error()) || !ret->ctx)
2086 /* In the future we should convert the GLX error to a win32 one here if needed */
2087 WARN("Context creation failed (error %#x).\n", err);
2088 HeapFree( GetProcessHeap(), 0, ret );
2089 ret = NULL;
2091 else
2093 EnterCriticalSection( &context_section );
2094 list_add_head( &context_list, &ret->entry );
2095 LeaveCriticalSection( &context_section );
2099 release_gl_drawable( gl );
2100 TRACE( "%p -> %p\n", hdc, ret );
2101 return ret;
2105 * X11DRV_wglGetExtensionsStringARB
2107 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2109 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2111 TRACE("() returning \"%s\"\n", wglExtensions);
2112 return wglExtensions;
2116 * X11DRV_wglCreatePbufferARB
2118 * WGL_ARB_pbuffer: wglCreatePbufferARB
2120 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2121 const int *piAttribList )
2123 struct wgl_pbuffer* object;
2124 const struct wgl_pixel_format *fmt;
2125 int attribs[256];
2126 int nAttribs = 0;
2128 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2130 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2131 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2132 if(!fmt) {
2133 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2134 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2135 return NULL;
2138 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2139 if (NULL == object) {
2140 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2141 return NULL;
2143 object->width = iWidth;
2144 object->height = iHeight;
2145 object->fmt = fmt;
2147 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2148 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2149 while (piAttribList && 0 != *piAttribList) {
2150 int attr_v;
2151 switch (*piAttribList) {
2152 case WGL_PBUFFER_LARGEST_ARB: {
2153 ++piAttribList;
2154 attr_v = *piAttribList;
2155 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2156 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2157 break;
2160 case WGL_TEXTURE_FORMAT_ARB: {
2161 ++piAttribList;
2162 attr_v = *piAttribList;
2163 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2164 if (WGL_NO_TEXTURE_ARB == attr_v) {
2165 object->use_render_texture = 0;
2166 } else {
2167 if (!use_render_texture_emulation) {
2168 SetLastError(ERROR_INVALID_DATA);
2169 goto create_failed;
2171 switch (attr_v) {
2172 case WGL_TEXTURE_RGB_ARB:
2173 object->use_render_texture = GL_RGB;
2174 object->texture_bpp = 3;
2175 object->texture_format = GL_RGB;
2176 object->texture_type = GL_UNSIGNED_BYTE;
2177 break;
2178 case WGL_TEXTURE_RGBA_ARB:
2179 object->use_render_texture = GL_RGBA;
2180 object->texture_bpp = 4;
2181 object->texture_format = GL_RGBA;
2182 object->texture_type = GL_UNSIGNED_BYTE;
2183 break;
2185 /* WGL_FLOAT_COMPONENTS_NV */
2186 case WGL_TEXTURE_FLOAT_R_NV:
2187 object->use_render_texture = GL_FLOAT_R_NV;
2188 object->texture_bpp = 4;
2189 object->texture_format = GL_RED;
2190 object->texture_type = GL_FLOAT;
2191 break;
2192 case WGL_TEXTURE_FLOAT_RG_NV:
2193 object->use_render_texture = GL_FLOAT_RG_NV;
2194 object->texture_bpp = 8;
2195 object->texture_format = GL_LUMINANCE_ALPHA;
2196 object->texture_type = GL_FLOAT;
2197 break;
2198 case WGL_TEXTURE_FLOAT_RGB_NV:
2199 object->use_render_texture = GL_FLOAT_RGB_NV;
2200 object->texture_bpp = 12;
2201 object->texture_format = GL_RGB;
2202 object->texture_type = GL_FLOAT;
2203 break;
2204 case WGL_TEXTURE_FLOAT_RGBA_NV:
2205 object->use_render_texture = GL_FLOAT_RGBA_NV;
2206 object->texture_bpp = 16;
2207 object->texture_format = GL_RGBA;
2208 object->texture_type = GL_FLOAT;
2209 break;
2210 default:
2211 ERR("Unknown texture format: %x\n", attr_v);
2212 SetLastError(ERROR_INVALID_DATA);
2213 goto create_failed;
2216 break;
2219 case WGL_TEXTURE_TARGET_ARB: {
2220 ++piAttribList;
2221 attr_v = *piAttribList;
2222 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2223 if (WGL_NO_TEXTURE_ARB == attr_v) {
2224 object->texture_target = 0;
2225 } else {
2226 if (!use_render_texture_emulation) {
2227 SetLastError(ERROR_INVALID_DATA);
2228 goto create_failed;
2230 switch (attr_v) {
2231 case WGL_TEXTURE_CUBE_MAP_ARB: {
2232 if (iWidth != iHeight) {
2233 SetLastError(ERROR_INVALID_DATA);
2234 goto create_failed;
2236 object->texture_target = GL_TEXTURE_CUBE_MAP;
2237 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2238 break;
2240 case WGL_TEXTURE_1D_ARB: {
2241 if (1 != iHeight) {
2242 SetLastError(ERROR_INVALID_DATA);
2243 goto create_failed;
2245 object->texture_target = GL_TEXTURE_1D;
2246 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2247 break;
2249 case WGL_TEXTURE_2D_ARB: {
2250 object->texture_target = GL_TEXTURE_2D;
2251 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2252 break;
2254 case WGL_TEXTURE_RECTANGLE_NV: {
2255 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2256 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2257 break;
2259 default:
2260 ERR("Unknown texture target: %x\n", attr_v);
2261 SetLastError(ERROR_INVALID_DATA);
2262 goto create_failed;
2265 break;
2268 case WGL_MIPMAP_TEXTURE_ARB: {
2269 ++piAttribList;
2270 attr_v = *piAttribList;
2271 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2272 if (!use_render_texture_emulation) {
2273 SetLastError(ERROR_INVALID_DATA);
2274 goto create_failed;
2276 break;
2279 ++piAttribList;
2282 PUSH1(attribs, None);
2283 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2284 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2285 if (!object->drawable) {
2286 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2287 goto create_failed; /* unexpected error */
2289 EnterCriticalSection( &context_section );
2290 list_add_head( &pbuffer_list, &object->entry );
2291 LeaveCriticalSection( &context_section );
2292 TRACE("->(%p)\n", object);
2293 return object;
2295 create_failed:
2296 HeapFree(GetProcessHeap(), 0, object);
2297 TRACE("->(FAILED)\n");
2298 return NULL;
2302 * X11DRV_wglDestroyPbufferARB
2304 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2306 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2308 TRACE("(%p)\n", object);
2310 EnterCriticalSection( &context_section );
2311 list_remove( &object->entry );
2312 LeaveCriticalSection( &context_section );
2313 pglXDestroyPbuffer(gdi_display, object->drawable);
2314 if (object->tmp_context)
2315 pglXDestroyContext(gdi_display, object->tmp_context);
2316 HeapFree(GetProcessHeap(), 0, object);
2317 return GL_TRUE;
2321 * X11DRV_wglGetPbufferDCARB
2323 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2325 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2327 struct x11drv_escape_set_drawable escape;
2328 struct gl_drawable *gl, *prev;
2329 HDC hdc;
2331 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2332 if (!hdc) return 0;
2334 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2336 DeleteDC( hdc );
2337 return 0;
2339 gl->type = DC_GL_PBUFFER;
2340 gl->drawable = object->drawable;
2341 gl->format = object->fmt;
2342 gl->ref = 1;
2344 EnterCriticalSection( &context_section );
2345 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2346 release_gl_drawable( prev );
2347 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2348 LeaveCriticalSection( &context_section );
2350 escape.code = X11DRV_SET_DRAWABLE;
2351 escape.drawable = object->drawable;
2352 escape.mode = IncludeInferiors;
2353 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2354 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2356 TRACE( "(%p)->(%p)\n", object, hdc );
2357 return hdc;
2361 * X11DRV_wglQueryPbufferARB
2363 * WGL_ARB_pbuffer: wglQueryPbufferARB
2365 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2367 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2369 switch (iAttribute) {
2370 case WGL_PBUFFER_WIDTH_ARB:
2371 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2372 break;
2373 case WGL_PBUFFER_HEIGHT_ARB:
2374 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2375 break;
2377 case WGL_PBUFFER_LOST_ARB:
2378 /* GLX Pbuffers cannot be lost by default. We can support this by
2379 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2380 * to receive pixel buffer clobber events, however that may or may
2381 * not give any benefit */
2382 *piValue = GL_FALSE;
2383 break;
2385 case WGL_TEXTURE_FORMAT_ARB:
2386 if (!object->use_render_texture) {
2387 *piValue = WGL_NO_TEXTURE_ARB;
2388 } else {
2389 if (!use_render_texture_emulation) {
2390 SetLastError(ERROR_INVALID_HANDLE);
2391 return GL_FALSE;
2393 switch(object->use_render_texture) {
2394 case GL_RGB:
2395 *piValue = WGL_TEXTURE_RGB_ARB;
2396 break;
2397 case GL_RGBA:
2398 *piValue = WGL_TEXTURE_RGBA_ARB;
2399 break;
2400 /* WGL_FLOAT_COMPONENTS_NV */
2401 case GL_FLOAT_R_NV:
2402 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2403 break;
2404 case GL_FLOAT_RG_NV:
2405 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2406 break;
2407 case GL_FLOAT_RGB_NV:
2408 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2409 break;
2410 case GL_FLOAT_RGBA_NV:
2411 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2412 break;
2413 default:
2414 ERR("Unknown texture format: %x\n", object->use_render_texture);
2417 break;
2419 case WGL_TEXTURE_TARGET_ARB:
2420 if (!object->texture_target){
2421 *piValue = WGL_NO_TEXTURE_ARB;
2422 } else {
2423 if (!use_render_texture_emulation) {
2424 SetLastError(ERROR_INVALID_DATA);
2425 return GL_FALSE;
2427 switch (object->texture_target) {
2428 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2429 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2430 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2431 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2434 break;
2436 case WGL_MIPMAP_TEXTURE_ARB:
2437 *piValue = GL_FALSE; /** don't support that */
2438 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2439 break;
2441 default:
2442 FIXME("unexpected attribute %x\n", iAttribute);
2443 break;
2446 return GL_TRUE;
2450 * X11DRV_wglReleasePbufferDCARB
2452 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2454 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2456 struct gl_drawable *gl;
2458 TRACE("(%p, %p)\n", object, hdc);
2460 EnterCriticalSection( &context_section );
2462 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2464 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2465 release_gl_drawable( gl );
2467 else hdc = 0;
2469 LeaveCriticalSection( &context_section );
2471 return hdc && DeleteDC(hdc);
2475 * X11DRV_wglSetPbufferAttribARB
2477 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2479 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2481 GLboolean ret = GL_FALSE;
2483 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2485 if (!object->use_render_texture) {
2486 SetLastError(ERROR_INVALID_HANDLE);
2487 return GL_FALSE;
2489 if (use_render_texture_emulation) {
2490 return GL_TRUE;
2492 return ret;
2495 struct choose_pixel_format_arb_format
2497 int format;
2498 int original_index;
2499 PIXELFORMATDESCRIPTOR pfd;
2500 int depth, stencil;
2503 static int compare_formats(const void *a, const void *b)
2505 /* Order formats so that onscreen formats go first. Then, if no depth bits requested,
2506 * prioritize formats with smaller depth within the original sort order with respect to
2507 * other attributes. */
2508 const struct choose_pixel_format_arb_format *fmt_a = a, *fmt_b = b;
2509 BOOL offscreen_a, offscreen_b;
2511 offscreen_a = fmt_a->format > nb_onscreen_formats;
2512 offscreen_b = fmt_b->format > nb_onscreen_formats;
2514 if (offscreen_a != offscreen_b)
2515 return offscreen_a - offscreen_b;
2516 if (memcmp(&fmt_a->pfd, &fmt_b->pfd, sizeof(fmt_a->pfd)))
2517 return fmt_a->original_index - fmt_b->original_index;
2518 if (fmt_a->depth != fmt_b->depth)
2519 return fmt_a->depth - fmt_b->depth;
2520 if (fmt_a->stencil != fmt_b->stencil)
2521 return fmt_a->stencil - fmt_b->stencil;
2523 return fmt_a->original_index - fmt_b->original_index;
2527 * X11DRV_wglChoosePixelFormatARB
2529 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2531 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2532 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2534 struct choose_pixel_format_arb_format *formats;
2535 int it, i, format_count;
2536 BYTE depth_bits = 0;
2537 GLXFBConfig* cfgs;
2538 DWORD dwFlags = 0;
2539 int attribs[256];
2540 int nAttribs = 0;
2541 int nCfgs = 0;
2542 int fmt_id;
2544 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2545 if (NULL != pfAttribFList) {
2546 FIXME("unused pfAttribFList\n");
2549 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2550 if (-1 == nAttribs) {
2551 WARN("Cannot convert WGL to GLX attributes\n");
2552 return GL_FALSE;
2554 PUSH1(attribs, None);
2556 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2557 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2558 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2560 for(i=0; piAttribIList[i] != 0; i+=2)
2562 switch(piAttribIList[i])
2564 case WGL_DRAW_TO_BITMAP_ARB:
2565 if(piAttribIList[i+1])
2566 dwFlags |= PFD_DRAW_TO_BITMAP;
2567 break;
2568 case WGL_ACCELERATION_ARB:
2569 switch(piAttribIList[i+1])
2571 case WGL_NO_ACCELERATION_ARB:
2572 dwFlags |= PFD_GENERIC_FORMAT;
2573 break;
2574 case WGL_GENERIC_ACCELERATION_ARB:
2575 dwFlags |= PFD_GENERIC_ACCELERATED;
2576 break;
2577 case WGL_FULL_ACCELERATION_ARB:
2578 /* Nothing to do */
2579 break;
2581 break;
2582 case WGL_SUPPORT_GDI_ARB:
2583 if(piAttribIList[i+1])
2584 dwFlags |= PFD_SUPPORT_GDI;
2585 break;
2586 case WGL_DEPTH_BITS_ARB:
2587 depth_bits = piAttribIList[i+1];
2588 break;
2593 /* Search for FB configurations matching the requirements in attribs */
2594 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2595 if (NULL == cfgs) {
2596 WARN("Compatible Pixel Format not found\n");
2597 return GL_FALSE;
2600 if (!(formats = heap_alloc(nCfgs * sizeof(*formats))))
2602 ERR("No memory.\n");
2603 XFree(cfgs);
2604 return GL_FALSE;
2607 format_count = 0;
2608 for (it = 0; it < nCfgs; ++it)
2610 struct choose_pixel_format_arb_format *format;
2612 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2614 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2615 continue;
2618 for (i = 0; i < nb_pixel_formats; ++i)
2619 if (pixel_formats[i].fmt_id == fmt_id)
2620 break;
2622 if (i == nb_pixel_formats)
2623 continue;
2625 format = &formats[format_count];
2626 format->format = i + 1;
2627 format->original_index = it;
2629 memset(&format->pfd, 0, sizeof(format->pfd));
2630 if (!describe_pixel_format(format->format, &format->pfd, TRUE))
2631 ERR("describe_pixel_format failed, format %d.\n", format->format);
2633 format->depth = format->pfd.cDepthBits;
2634 format->stencil = format->pfd.cStencilBits;
2635 if (!depth_bits && !(format->pfd.dwFlags & PFD_GENERIC_FORMAT))
2637 format->pfd.cDepthBits = 0;
2638 format->pfd.cStencilBits = 0;
2641 ++format_count;
2644 qsort(formats, format_count, sizeof(*formats), compare_formats);
2646 *nNumFormats = min(nMaxFormats, format_count);
2647 for (i = 0; i < *nNumFormats; ++i)
2648 piFormats[i] = formats[i].format;
2650 heap_free(formats);
2651 XFree(cfgs);
2652 return GL_TRUE;
2656 * X11DRV_wglGetPixelFormatAttribivARB
2658 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2660 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2661 UINT nAttributes, const int *piAttributes, int *piValues )
2663 UINT i;
2664 const struct wgl_pixel_format *fmt;
2665 int hTest;
2666 int tmp;
2667 int curGLXAttr = 0;
2669 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2671 if (0 < iLayerPlane) {
2672 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2673 return GL_FALSE;
2676 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2677 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2678 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2679 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2680 if(!fmt) {
2681 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2684 for (i = 0; i < nAttributes; ++i) {
2685 const int curWGLAttr = piAttributes[i];
2686 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2688 switch (curWGLAttr) {
2689 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2690 piValues[i] = nb_pixel_formats;
2691 continue;
2693 case WGL_SUPPORT_OPENGL_ARB:
2694 piValues[i] = GL_TRUE;
2695 continue;
2697 case WGL_ACCELERATION_ARB:
2698 curGLXAttr = GLX_CONFIG_CAVEAT;
2699 if (!fmt) goto pix_error;
2700 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2701 piValues[i] = WGL_NO_ACCELERATION_ARB;
2702 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2703 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2704 else
2705 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2706 continue;
2708 case WGL_TRANSPARENT_ARB:
2709 curGLXAttr = GLX_TRANSPARENT_TYPE;
2710 if (!fmt) goto pix_error;
2711 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2712 if (hTest) goto get_error;
2713 piValues[i] = GL_FALSE;
2714 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2715 continue;
2717 case WGL_PIXEL_TYPE_ARB:
2718 curGLXAttr = GLX_RENDER_TYPE;
2719 if (!fmt) goto pix_error;
2720 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2721 if (hTest) goto get_error;
2722 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2723 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2724 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2725 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2726 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2727 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2728 else {
2729 ERR("unexpected RenderType(%x)\n", tmp);
2730 piValues[i] = WGL_TYPE_RGBA_ARB;
2732 continue;
2734 case WGL_COLOR_BITS_ARB:
2735 curGLXAttr = GLX_BUFFER_SIZE;
2736 break;
2738 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2739 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2740 if (!use_render_texture_emulation) {
2741 piValues[i] = GL_FALSE;
2742 continue;
2744 curGLXAttr = GLX_RENDER_TYPE;
2745 if (!fmt) goto pix_error;
2746 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2747 if (hTest) goto get_error;
2748 if (GLX_COLOR_INDEX_BIT == tmp) {
2749 piValues[i] = GL_FALSE;
2750 continue;
2752 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2753 if (hTest) goto get_error;
2754 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2755 continue;
2757 case WGL_BLUE_BITS_ARB:
2758 curGLXAttr = GLX_BLUE_SIZE;
2759 break;
2760 case WGL_RED_BITS_ARB:
2761 curGLXAttr = GLX_RED_SIZE;
2762 break;
2763 case WGL_GREEN_BITS_ARB:
2764 curGLXAttr = GLX_GREEN_SIZE;
2765 break;
2766 case WGL_ALPHA_BITS_ARB:
2767 curGLXAttr = GLX_ALPHA_SIZE;
2768 break;
2769 case WGL_DEPTH_BITS_ARB:
2770 curGLXAttr = GLX_DEPTH_SIZE;
2771 break;
2772 case WGL_STENCIL_BITS_ARB:
2773 curGLXAttr = GLX_STENCIL_SIZE;
2774 break;
2775 case WGL_DOUBLE_BUFFER_ARB:
2776 curGLXAttr = GLX_DOUBLEBUFFER;
2777 break;
2778 case WGL_STEREO_ARB:
2779 curGLXAttr = GLX_STEREO;
2780 break;
2781 case WGL_AUX_BUFFERS_ARB:
2782 curGLXAttr = GLX_AUX_BUFFERS;
2783 break;
2785 case WGL_SUPPORT_GDI_ARB:
2786 if (!fmt) goto pix_error;
2787 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2788 continue;
2790 case WGL_DRAW_TO_BITMAP_ARB:
2791 if (!fmt) goto pix_error;
2792 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2793 continue;
2795 case WGL_DRAW_TO_WINDOW_ARB:
2796 case WGL_DRAW_TO_PBUFFER_ARB:
2797 if (!fmt) goto pix_error;
2798 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2799 if (hTest) goto get_error;
2800 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2801 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2802 piValues[i] = GL_TRUE;
2803 else
2804 piValues[i] = GL_FALSE;
2805 continue;
2807 case WGL_SWAP_METHOD_ARB:
2808 if (has_swap_method)
2810 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_SWAP_METHOD_OML, &tmp);
2811 if (hTest) goto get_error;
2812 switch (tmp)
2814 case GLX_SWAP_EXCHANGE_OML:
2815 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2816 break;
2817 case GLX_SWAP_COPY_OML:
2818 piValues[i] = WGL_SWAP_COPY_ARB;
2819 break;
2820 case GLX_SWAP_UNDEFINED_OML:
2821 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2822 break;
2823 default:
2824 ERR("Unexpected swap method %x.\n", tmp);
2827 else
2829 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2830 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2832 continue;
2834 case WGL_PBUFFER_LARGEST_ARB:
2835 curGLXAttr = GLX_LARGEST_PBUFFER;
2836 break;
2838 case WGL_SAMPLE_BUFFERS_ARB:
2839 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2840 break;
2842 case WGL_SAMPLES_ARB:
2843 curGLXAttr = GLX_SAMPLES_ARB;
2844 break;
2846 case WGL_FLOAT_COMPONENTS_NV:
2847 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2848 break;
2850 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2851 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2852 break;
2854 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2855 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2856 break;
2858 case WGL_ACCUM_RED_BITS_ARB:
2859 curGLXAttr = GLX_ACCUM_RED_SIZE;
2860 break;
2861 case WGL_ACCUM_GREEN_BITS_ARB:
2862 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2863 break;
2864 case WGL_ACCUM_BLUE_BITS_ARB:
2865 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2866 break;
2867 case WGL_ACCUM_ALPHA_BITS_ARB:
2868 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2869 break;
2870 case WGL_ACCUM_BITS_ARB:
2871 if (!fmt) goto pix_error;
2872 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2873 if (hTest) goto get_error;
2874 piValues[i] = tmp;
2875 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2876 if (hTest) goto get_error;
2877 piValues[i] += tmp;
2878 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2879 if (hTest) goto get_error;
2880 piValues[i] += tmp;
2881 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2882 if (hTest) goto get_error;
2883 piValues[i] += tmp;
2884 continue;
2886 default:
2887 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2890 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2891 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2892 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2894 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2895 * and check which options can work using iPixelFormat=0 and which not.
2896 * A problem would be that this function is an extension. This would
2897 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2899 if (0 != curGLXAttr && iPixelFormat != 0) {
2900 if (!fmt) goto pix_error;
2901 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2902 if (hTest) goto get_error;
2903 curGLXAttr = 0;
2904 } else {
2905 piValues[i] = GL_FALSE;
2908 return GL_TRUE;
2910 get_error:
2911 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2912 return GL_FALSE;
2914 pix_error:
2915 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2916 return GL_FALSE;
2920 * X11DRV_wglGetPixelFormatAttribfvARB
2922 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2924 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2925 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2927 int *attr;
2928 int ret;
2929 UINT i;
2931 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2933 /* Allocate a temporary array to store integer values */
2934 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2935 if (!attr) {
2936 ERR("couldn't allocate %d array\n", nAttributes);
2937 return GL_FALSE;
2940 /* Piggy-back on wglGetPixelFormatAttribivARB */
2941 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2942 if (ret) {
2943 /* Convert integer values to float. Should also check for attributes
2944 that can give decimal values here */
2945 for (i=0; i<nAttributes;i++) {
2946 pfValues[i] = attr[i];
2950 HeapFree(GetProcessHeap(), 0, attr);
2951 return ret;
2955 * X11DRV_wglBindTexImageARB
2957 * WGL_ARB_render_texture: wglBindTexImageARB
2959 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2961 GLboolean ret = GL_FALSE;
2963 TRACE("(%p, %d)\n", object, iBuffer);
2965 if (!object->use_render_texture) {
2966 SetLastError(ERROR_INVALID_HANDLE);
2967 return GL_FALSE;
2970 if (use_render_texture_emulation) {
2971 static BOOL initialized = FALSE;
2972 int prev_binded_texture = 0;
2973 GLXContext prev_context;
2974 GLXDrawable prev_drawable;
2976 prev_context = pglXGetCurrentContext();
2977 prev_drawable = pglXGetCurrentDrawable();
2979 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2980 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2981 isn't ideal performance wise but I wasn't able to get other ways working.
2983 if(!initialized) {
2984 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2985 FIXME("partial stub!\n");
2988 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2989 if (!object->tmp_context || object->prev_context != prev_context) {
2990 if (object->tmp_context)
2991 pglXDestroyContext(gdi_display, object->tmp_context);
2992 object->tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2993 object->prev_context = prev_context;
2996 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2998 /* Switch to our pbuffer */
2999 pglXMakeCurrent(gdi_display, object->drawable, object->tmp_context);
3001 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3002 * After that copy the pbuffer texture data. */
3003 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
3004 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3006 /* Switch back to the original drawable and context */
3007 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
3008 return GL_TRUE;
3011 return ret;
3015 * X11DRV_wglReleaseTexImageARB
3017 * WGL_ARB_render_texture: wglReleaseTexImageARB
3019 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
3021 GLboolean ret = GL_FALSE;
3023 TRACE("(%p, %d)\n", object, iBuffer);
3025 if (!object->use_render_texture) {
3026 SetLastError(ERROR_INVALID_HANDLE);
3027 return GL_FALSE;
3029 if (use_render_texture_emulation) {
3030 return GL_TRUE;
3032 return ret;
3036 * X11DRV_wglGetExtensionsStringEXT
3038 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3040 static const char *X11DRV_wglGetExtensionsStringEXT(void)
3042 TRACE("() returning \"%s\"\n", wglExtensions);
3043 return wglExtensions;
3047 * X11DRV_wglGetSwapIntervalEXT
3049 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3051 static int X11DRV_wglGetSwapIntervalEXT(void)
3053 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3054 struct gl_drawable *gl;
3055 int swap_interval;
3057 TRACE("()\n");
3059 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3061 /* This can't happen because a current WGL context is required to get
3062 * here. Likely the application is buggy.
3064 WARN("No GL drawable found, returning swap interval 0\n");
3065 return 0;
3068 swap_interval = gl->swap_interval;
3069 release_gl_drawable(gl);
3071 return swap_interval;
3075 * X11DRV_wglSwapIntervalEXT
3077 * WGL_EXT_swap_control: wglSwapIntervalEXT
3079 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3081 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3082 struct gl_drawable *gl;
3083 BOOL ret;
3085 TRACE("(%d)\n", interval);
3087 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3088 * is invalid.
3090 if (interval < 0 && !has_swap_control_tear)
3092 SetLastError(ERROR_INVALID_DATA);
3093 return FALSE;
3096 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3098 SetLastError(ERROR_DC_NOT_FOUND);
3099 return FALSE;
3102 EnterCriticalSection( &context_section );
3103 ret = set_swap_interval(gl->drawable, interval);
3104 gl->refresh_swap_interval = FALSE;
3105 if (ret)
3106 gl->swap_interval = interval;
3107 else
3108 SetLastError(ERROR_DC_NOT_FOUND);
3110 LeaveCriticalSection( &context_section );
3111 release_gl_drawable(gl);
3113 return ret;
3117 * X11DRV_wglSetPixelFormatWINE
3119 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3120 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3122 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3124 return set_pixel_format(hdc, format, TRUE);
3127 static BOOL X11DRV_wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
3129 return pglXQueryCurrentRendererIntegerMESA( attribute, value );
3132 static const char *X11DRV_wglQueryCurrentRendererStringWINE( GLenum attribute )
3134 return pglXQueryCurrentRendererStringMESA( attribute );
3137 static BOOL X11DRV_wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
3139 return pglXQueryRendererIntegerMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute, value );
3142 static const char *X11DRV_wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
3144 return pglXQueryRendererStringMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute );
3148 * glxRequireVersion (internal)
3150 * Check if the supported GLX version matches requiredVersion.
3152 static BOOL glxRequireVersion(int requiredVersion)
3154 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3155 return (requiredVersion <= glxVersion[1]);
3158 static void register_extension(const char *ext)
3160 if (wglExtensions[0])
3161 strcat(wglExtensions, " ");
3162 strcat(wglExtensions, ext);
3164 TRACE("'%s'\n", ext);
3168 * X11DRV_WineGL_LoadExtensions
3170 static void X11DRV_WineGL_LoadExtensions(void)
3172 wglExtensions[0] = 0;
3174 /* ARB Extensions */
3176 if (has_extension( glxExtensions, "GLX_ARB_create_context"))
3178 register_extension( "WGL_ARB_create_context" );
3179 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3181 if (has_extension( glxExtensions, "GLX_ARB_create_context_no_error" ))
3182 register_extension( "WGL_ARB_create_context_no_error" );
3183 if (has_extension( glxExtensions, "GLX_ARB_create_context_profile"))
3184 register_extension("WGL_ARB_create_context_profile");
3187 if (has_extension( glxExtensions, "GLX_ARB_fbconfig_float"))
3189 register_extension("WGL_ARB_pixel_format_float");
3190 register_extension("WGL_ATI_pixel_format_float");
3193 register_extension( "WGL_ARB_extensions_string" );
3194 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3196 if (glxRequireVersion(3))
3198 register_extension( "WGL_ARB_make_current_read" );
3199 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3200 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3203 if (has_extension( glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3205 if (glxRequireVersion(3))
3207 register_extension( "WGL_ARB_pbuffer" );
3208 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3209 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3210 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3211 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3212 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3213 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3216 register_extension( "WGL_ARB_pixel_format" );
3217 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3218 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3219 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3221 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3222 if (has_extension( glxExtensions, "GLX_ARB_render_texture") ||
3223 (glxRequireVersion(3) && use_render_texture_emulation))
3225 register_extension( "WGL_ARB_render_texture" );
3226 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3227 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3229 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3230 if (has_extension( glxExtensions, "GLX_NV_float_buffer"))
3231 register_extension("WGL_NV_float_buffer");
3233 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3234 if (has_extension(glExtensions, "GL_NV_texture_rectangle"))
3235 register_extension("WGL_NV_render_texture_rectangle");
3238 /* EXT Extensions */
3240 register_extension( "WGL_EXT_extensions_string" );
3241 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3243 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3244 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3245 register_extension( "WGL_EXT_swap_control" );
3246 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3247 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3249 if (has_extension( glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3250 register_extension("WGL_EXT_framebuffer_sRGB");
3252 if (has_extension( glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3253 register_extension("WGL_EXT_pixel_format_packed_float");
3255 if (has_extension( glxExtensions, "GLX_EXT_swap_control"))
3257 swap_control_method = GLX_SWAP_CONTROL_EXT;
3258 if (has_extension( glxExtensions, "GLX_EXT_swap_control_tear"))
3260 register_extension("WGL_EXT_swap_control_tear");
3261 has_swap_control_tear = TRUE;
3264 else if (has_extension( glxExtensions, "GLX_MESA_swap_control"))
3266 swap_control_method = GLX_SWAP_CONTROL_MESA;
3268 else if (has_extension( glxExtensions, "GLX_SGI_swap_control"))
3270 swap_control_method = GLX_SWAP_CONTROL_SGI;
3273 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3274 if (has_extension(glExtensions, "GL_NV_vertex_array_range"))
3276 register_extension( "WGL_NV_vertex_array_range" );
3277 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3278 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3281 if (has_extension(glxExtensions, "GLX_OML_swap_method"))
3282 has_swap_method = TRUE;
3284 /* WINE-specific WGL Extensions */
3286 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3287 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3289 register_extension( "WGL_WINE_pixel_format_passthrough" );
3290 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3292 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
3294 register_extension( "WGL_WINE_query_renderer" );
3295 opengl_funcs.ext.p_wglQueryCurrentRendererIntegerWINE = X11DRV_wglQueryCurrentRendererIntegerWINE;
3296 opengl_funcs.ext.p_wglQueryCurrentRendererStringWINE = X11DRV_wglQueryCurrentRendererStringWINE;
3297 opengl_funcs.ext.p_wglQueryRendererIntegerWINE = X11DRV_wglQueryRendererIntegerWINE;
3298 opengl_funcs.ext.p_wglQueryRendererStringWINE = X11DRV_wglQueryRendererStringWINE;
3304 * glxdrv_SwapBuffers
3306 * Swap the buffers of this DC
3308 static BOOL WINAPI glxdrv_wglSwapBuffers( HDC hdc )
3310 struct x11drv_escape_flush_gl_drawable escape;
3311 struct gl_drawable *gl;
3312 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3313 INT64 ust, msc, sbc, target_sbc = 0;
3315 TRACE("(%p)\n", hdc);
3317 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3318 escape.gl_drawable = 0;
3319 escape.flush = !pglXWaitForSbcOML;
3321 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3323 SetLastError( ERROR_INVALID_HANDLE );
3324 return FALSE;
3327 EnterCriticalSection( &context_section );
3328 if (gl->refresh_swap_interval)
3330 set_swap_interval(gl->drawable, gl->swap_interval);
3331 gl->refresh_swap_interval = FALSE;
3333 LeaveCriticalSection( &context_section );
3335 switch (gl->type)
3337 case DC_GL_PIXMAP_WIN:
3338 if (ctx) sync_context( ctx );
3339 escape.gl_drawable = gl->pixmap;
3340 if (pglXCopySubBufferMESA) {
3341 /* (glX)SwapBuffers has an implicit glFlush effect, however
3342 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3343 * copying */
3344 pglFlush();
3345 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3346 gl->pixmap_size.cx, gl->pixmap_size.cy );
3347 break;
3349 if (pglXSwapBuffersMscOML)
3351 pglFlush();
3352 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3353 break;
3355 pglXSwapBuffers(gdi_display, gl->drawable);
3356 break;
3357 case DC_GL_WINDOW:
3358 case DC_GL_CHILD_WIN:
3359 if (ctx) sync_context( ctx );
3360 if (gl->type == DC_GL_CHILD_WIN) escape.gl_drawable = gl->window;
3361 /* fall through */
3362 default:
3363 if (escape.gl_drawable && pglXSwapBuffersMscOML)
3365 pglFlush();
3366 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3367 break;
3369 pglXSwapBuffers(gdi_display, gl->drawable);
3370 break;
3373 if (escape.gl_drawable && pglXWaitForSbcOML)
3374 pglXWaitForSbcOML( gdi_display, gl->drawable, target_sbc, &ust, &msc, &sbc );
3376 release_gl_drawable( gl );
3378 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3379 return TRUE;
3382 static struct opengl_funcs opengl_funcs =
3385 glxdrv_wglCopyContext, /* p_wglCopyContext */
3386 glxdrv_wglCreateContext, /* p_wglCreateContext */
3387 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3388 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3389 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3390 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3391 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3392 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3393 glxdrv_wglShareLists, /* p_wglShareLists */
3394 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3398 struct opengl_funcs *get_glx_driver( UINT version )
3400 if (version != WINE_WGL_DRIVER_VERSION)
3402 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3403 return NULL;
3405 if (has_opengl()) return &opengl_funcs;
3406 return NULL;
3409 #else /* no OpenGL includes */
3411 struct opengl_funcs *get_glx_driver( UINT version )
3413 return NULL;
3416 void sync_gl_drawable( HWND hwnd, BOOL known_child )
3420 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3424 void destroy_gl_drawable( HWND hwnd )
3428 #endif /* defined(SONAME_LIBGL) */