wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / winex11.drv / opengl.c
blobc44c587bf14f37af3cb1ec767419e463932f197b
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;
254 enum glx_swap_control_method
256 GLX_SWAP_CONTROL_NONE,
257 GLX_SWAP_CONTROL_EXT,
258 GLX_SWAP_CONTROL_SGI,
259 GLX_SWAP_CONTROL_MESA
262 /* X context to associate a struct gl_drawable to an hwnd */
263 static XContext gl_hwnd_context;
264 /* X context to associate a struct gl_drawable to a pbuffer hdc */
265 static XContext gl_pbuffer_context;
267 static struct list context_list = LIST_INIT( context_list );
268 static struct list pbuffer_list = LIST_INIT( pbuffer_list );
269 static struct wgl_pixel_format *pixel_formats;
270 static int nb_pixel_formats, nb_onscreen_formats;
271 static BOOL use_render_texture_emulation = TRUE;
273 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
274 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
275 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
276 static BOOL has_swap_control_tear = FALSE;
277 static BOOL has_swap_method = FALSE;
279 static CRITICAL_SECTION context_section;
280 static CRITICAL_SECTION_DEBUG critsect_debug =
282 0, 0, &context_section,
283 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
284 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
286 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
288 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
290 static struct opengl_funcs opengl_funcs;
292 #define USE_GL_FUNC(name) #name,
293 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
294 #undef USE_GL_FUNC
296 static void X11DRV_WineGL_LoadExtensions(void);
297 static void init_pixel_formats( Display *display );
298 static BOOL glxRequireVersion(int requiredVersion);
300 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
301 TRACE( "size %u version %u flags %u type %u color %u %u,%u,%u,%u "
302 "accum %u depth %u stencil %u aux %u ",
303 ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
304 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
305 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
306 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
307 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
308 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
309 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
310 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
311 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
312 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
313 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
315 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
316 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
318 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
322 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
323 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
324 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
325 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
326 #undef TEST_AND_DUMP
327 TRACE("\n");
330 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
331 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
333 /* GLX 1.0 */
334 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
335 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
336 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
337 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
338 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
339 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
340 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
341 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
342 static GLXContext (*pglXGetCurrentContext)( void );
343 static GLXDrawable (*pglXGetCurrentDrawable)( void );
345 /* GLX 1.1 */
346 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
347 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
348 static const char *(*pglXGetClientString)( Display *dpy, int name );
350 /* GLX 1.3 */
351 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
352 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
353 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
354 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
355 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
356 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
357 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
358 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
359 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
360 static GLXPixmap (*pglXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list );
361 static void (*pglXDestroyPixmap)( Display *dpy, GLXPixmap pixmap );
362 static GLXWindow (*pglXCreateWindow)( Display *dpy, GLXFBConfig config, Window win, const int *attrib_list );
363 static void (*pglXDestroyWindow)( Display *dpy, GLXWindow win );
365 /* GLX Extensions */
366 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
367 static void* (*pglXGetProcAddressARB)(const GLubyte *);
368 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
369 static int (*pglXSwapIntervalSGI)(int);
371 /* NV GLX Extension */
372 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
373 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
375 /* MESA GLX Extensions */
376 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
377 static int (*pglXSwapIntervalMESA)(unsigned int interval);
378 static Bool (*pglXQueryCurrentRendererIntegerMESA)(int attribute, unsigned int *value);
379 static const char *(*pglXQueryCurrentRendererStringMESA)(int attribute);
380 static Bool (*pglXQueryRendererIntegerMESA)(Display *dpy, int screen, int renderer, int attribute, unsigned int *value);
381 static const char *(*pglXQueryRendererStringMESA)(Display *dpy, int screen, int renderer, int attribute);
383 /* OpenML GLX Extensions */
384 static Bool (*pglXWaitForSbcOML)( Display *dpy, GLXDrawable drawable,
385 INT64 target_sbc, INT64 *ust, INT64 *msc, INT64 *sbc );
386 static INT64 (*pglXSwapBuffersMscOML)( Display *dpy, GLXDrawable drawable,
387 INT64 target_msc, INT64 divisor, INT64 remainder );
389 /* Standard OpenGL */
390 static void (*pglFinish)(void);
391 static void (*pglFlush)(void);
392 static const GLubyte *(*pglGetString)(GLenum name);
394 static void wglFinish(void);
395 static void wglFlush(void);
396 static const GLubyte *wglGetString(GLenum name);
398 /* check if the extension is present in the list */
399 static BOOL has_extension( const char *list, const char *ext )
401 size_t len = strlen( ext );
403 while (list)
405 while (*list == ' ') list++;
406 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
407 list = strchr( list, ' ' );
409 return FALSE;
412 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
414 /* In the future we might want to find the exact X or GLX error to report back to the app */
415 if (event->request_code != glx_opcode)
416 return 0;
417 return 1;
420 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
422 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
424 int screen = DefaultScreen(gdi_display);
425 Window win = 0, root = 0;
426 const char *gl_version;
427 const char *gl_renderer;
428 const char* str;
429 BOOL glx_direct;
430 XVisualInfo *vis;
431 GLXContext ctx = NULL;
432 XSetWindowAttributes attr;
433 BOOL ret = FALSE;
434 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
436 attr.override_redirect = True;
437 attr.colormap = None;
438 attr.border_pixel = 0;
440 vis = pglXChooseVisual(gdi_display, screen, attribList);
441 if (vis) {
442 #ifdef __i386__
443 WORD old_fs, new_fs;
444 __asm__( "mov %%fs,%0" : "=r" (old_fs) );
445 /* Create a GLX Context. Without one we can't query GL information */
446 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
447 __asm__( "mov %%fs,%0" : "=r" (new_fs) );
448 __asm__( "mov %0,%%fs" :: "r" (old_fs) );
449 if (old_fs != new_fs)
451 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
452 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
453 goto done;
455 #else
456 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
457 #endif
459 if (!ctx) goto done;
461 root = RootWindow( gdi_display, vis->screen );
462 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
463 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
464 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
465 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
466 XMapWindow( gdi_display, win );
467 else
468 win = root;
470 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
472 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
473 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
474 goto done;
476 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
477 gl_version = (const char *)opengl_funcs.gl.p_glGetString(GL_VERSION);
478 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
479 glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
480 strcpy(glExtensions, str);
481 strcat(glExtensions, legacy_extensions);
483 /* Get the common GLX version supported by GLX client and server ( major/minor) */
484 pglXQueryVersion(gdi_display, &glxVersion[0], &glxVersion[1]);
486 glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
487 glx_direct = pglXIsDirect(gdi_display, ctx);
489 TRACE("GL version : %s.\n", gl_version);
490 TRACE("GL renderer : %s.\n", gl_renderer);
491 TRACE("GLX version : %d.%d.\n", glxVersion[0], glxVersion[1]);
492 TRACE("Server GLX version : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VERSION));
493 TRACE("Server GLX vendor: : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VENDOR));
494 TRACE("Client GLX version : %s.\n", pglXGetClientString(gdi_display, GLX_VERSION));
495 TRACE("Client GLX vendor: : %s.\n", pglXGetClientString(gdi_display, GLX_VENDOR));
496 TRACE("Direct rendering enabled: %s\n", glx_direct ? "True" : "False");
498 if(!glx_direct)
500 int fd = ConnectionNumber(gdi_display);
501 struct sockaddr_un uaddr;
502 unsigned int uaddrlen = sizeof(struct sockaddr_un);
504 /* In general indirect rendering on a local X11 server indicates a driver problem.
505 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
507 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
508 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
509 "haven't been installed correctly (using GL renderer %s, version %s).\n",
510 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
511 debugstr_a(gl_version));
513 else
515 /* In general you would expect that if direct rendering is returned, that you receive hardware
516 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
517 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
518 * software rendering, it shows direct rendering.
520 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
521 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
522 * it shows 'Mesa X11'.
524 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
525 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
526 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
527 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
528 debugstr_a(gl_version));
530 ret = TRUE;
532 done:
533 if(vis) XFree(vis);
534 if(ctx) {
535 pglXMakeCurrent(gdi_display, None, NULL);
536 pglXDestroyContext(gdi_display, ctx);
538 if (win != root) XDestroyWindow( gdi_display, win );
539 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
540 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
541 return ret;
544 static void *opengl_handle;
546 static BOOL WINAPI init_opengl( INIT_ONCE *once, void *param, void **context )
548 int error_base, event_base;
549 unsigned int i;
551 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
552 and include all dependencies */
553 opengl_handle = dlopen( SONAME_LIBGL, RTLD_NOW | RTLD_GLOBAL );
554 if (opengl_handle == NULL)
556 ERR( "Failed to load libGL: %s\n", dlerror() );
557 ERR( "OpenGL support is disabled.\n");
558 return TRUE;
561 for (i = 0; i < ARRAY_SIZE( opengl_func_names ); i++)
563 if (!(((void **)&opengl_funcs.gl)[i] = dlsym( opengl_handle, opengl_func_names[i] )))
565 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
566 goto failed;
570 /* redirect some standard OpenGL functions */
571 #define REDIRECT(func) \
572 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
573 REDIRECT( glFinish );
574 REDIRECT( glFlush );
575 REDIRECT( glGetString );
576 #undef REDIRECT
578 pglXGetProcAddressARB = dlsym(opengl_handle, "glXGetProcAddressARB");
579 if (pglXGetProcAddressARB == NULL) {
580 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
581 goto failed;
584 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
586 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
587 goto failed; \
588 } while(0)
590 /* GLX 1.0 */
591 LOAD_FUNCPTR(glXChooseVisual);
592 LOAD_FUNCPTR(glXCopyContext);
593 LOAD_FUNCPTR(glXCreateContext);
594 LOAD_FUNCPTR(glXGetCurrentContext);
595 LOAD_FUNCPTR(glXGetCurrentDrawable);
596 LOAD_FUNCPTR(glXDestroyContext);
597 LOAD_FUNCPTR(glXIsDirect);
598 LOAD_FUNCPTR(glXMakeCurrent);
599 LOAD_FUNCPTR(glXSwapBuffers);
600 LOAD_FUNCPTR(glXQueryVersion);
602 /* GLX 1.1 */
603 LOAD_FUNCPTR(glXGetClientString);
604 LOAD_FUNCPTR(glXQueryExtensionsString);
605 LOAD_FUNCPTR(glXQueryServerString);
607 /* GLX 1.3 */
608 LOAD_FUNCPTR(glXCreatePbuffer);
609 LOAD_FUNCPTR(glXCreateNewContext);
610 LOAD_FUNCPTR(glXDestroyPbuffer);
611 LOAD_FUNCPTR(glXMakeContextCurrent);
612 LOAD_FUNCPTR(glXGetFBConfigs);
613 LOAD_FUNCPTR(glXCreatePixmap);
614 LOAD_FUNCPTR(glXDestroyPixmap);
615 LOAD_FUNCPTR(glXCreateWindow);
616 LOAD_FUNCPTR(glXDestroyWindow);
617 #undef LOAD_FUNCPTR
619 /* It doesn't matter if these fail. They'll only be used if the driver reports
620 the associated extension is available (and if a driver reports the extension
621 is available but fails to provide the functions, it's quite broken) */
622 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
623 /* ARB GLX Extension */
624 LOAD_FUNCPTR(glXCreateContextAttribsARB);
625 /* EXT GLX Extension */
626 LOAD_FUNCPTR(glXSwapIntervalEXT);
627 /* MESA GLX Extension */
628 LOAD_FUNCPTR(glXSwapIntervalMESA);
629 /* SGI GLX Extension */
630 LOAD_FUNCPTR(glXSwapIntervalSGI);
631 /* NV GLX Extension */
632 LOAD_FUNCPTR(glXAllocateMemoryNV);
633 LOAD_FUNCPTR(glXFreeMemoryNV);
634 #undef LOAD_FUNCPTR
636 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
638 if (XQueryExtension( gdi_display, "GLX", &glx_opcode, &event_base, &error_base ))
640 TRACE("GLX is up and running error_base = %d\n", error_base);
641 } else {
642 ERR( "GLX extension is missing, disabling OpenGL.\n" );
643 goto failed;
645 gl_hwnd_context = XUniqueContext();
646 gl_pbuffer_context = XUniqueContext();
648 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
649 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
650 * rendering is used.
652 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
653 * depends on the reported GLX client / server version and on the client / server extension list.
654 * Those don't have to be the same.
656 * In general the server GLX information lists the capabilities in case of indirect rendering.
657 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
658 * available and in that case the client GLX informat can be used.
659 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
660 * in the GLX version/extension list. When a program does this it works for certain for both
661 * direct and indirect rendering.
663 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
664 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
665 * extension list which causes this extension not to be listed. (Wine requires this extension).
666 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
667 * pbuffers is around.
669 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
670 * the ATI drivers and from then on use GLX client information for them.
673 if(glxRequireVersion(3)) {
674 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
675 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
676 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
677 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
678 } else if (has_extension( glxExtensions, "GLX_SGIX_fbconfig")) {
679 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
680 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
681 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
683 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
684 * enable this function when the Xserver understand GLX 1.3 or newer
686 pglXQueryDrawable = NULL;
687 } else if(strcmp("ATI", pglXGetClientString(gdi_display, GLX_VENDOR)) == 0) {
688 TRACE("Overriding ATI GLX capabilities!\n");
689 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
690 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
691 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
692 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
694 /* Use client GLX information in case of the ATI drivers. We override the
695 * capabilities over here and not somewhere else as ATI might better their
696 * life in the future. In case they release proper drivers this block of
697 * code won't be called. */
698 glxExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
699 } else {
700 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n",
701 pglXQueryServerString(gdi_display, DefaultScreen(gdi_display), GLX_VERSION));
704 if (has_extension( glxExtensions, "GLX_MESA_copy_sub_buffer")) {
705 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
708 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
710 pglXQueryCurrentRendererIntegerMESA = pglXGetProcAddressARB(
711 (const GLubyte *)"glXQueryCurrentRendererIntegerMESA" );
712 pglXQueryCurrentRendererStringMESA = pglXGetProcAddressARB(
713 (const GLubyte *)"glXQueryCurrentRendererStringMESA" );
714 pglXQueryRendererIntegerMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererIntegerMESA" );
715 pglXQueryRendererStringMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererStringMESA" );
718 if (has_extension( glxExtensions, "GLX_OML_sync_control" ))
720 pglXWaitForSbcOML = pglXGetProcAddressARB( (const GLubyte *)"glXWaitForSbcOML" );
721 pglXSwapBuffersMscOML = pglXGetProcAddressARB( (const GLubyte *)"glXSwapBuffersMscOML" );
724 X11DRV_WineGL_LoadExtensions();
725 init_pixel_formats( gdi_display );
726 return TRUE;
728 failed:
729 dlclose(opengl_handle);
730 opengl_handle = NULL;
731 return TRUE;
734 static BOOL has_opengl(void)
736 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
737 InitOnceExecuteOnce(&init_once, init_opengl, NULL, NULL);
738 return opengl_handle != NULL;
741 static const char *debugstr_fbconfig( GLXFBConfig fbconfig )
743 int id, visual, drawable;
745 if (pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_FBCONFIG_ID, &id ))
746 return "*** invalid fbconfig";
747 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_VISUAL_ID, &visual );
748 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &drawable );
749 return wine_dbg_sprintf( "fbconfig %#x visual id %#x drawable type %#x", id, visual, drawable );
752 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
753 int nAttribs = 0;
754 unsigned cur = 0;
755 int attr, pop;
756 int drawattrib = 0;
757 int nvfloatattrib = GLX_DONT_CARE;
758 int pixelattrib = GLX_DONT_CARE;
760 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
761 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
762 while (iWGLAttr && 0 != iWGLAttr[cur]) {
763 attr = iWGLAttr[cur];
764 TRACE("pAttr[%d] = %x\n", cur, attr);
765 pop = iWGLAttr[++cur];
767 switch (attr) {
768 case WGL_AUX_BUFFERS_ARB:
769 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
770 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
771 break;
772 case WGL_COLOR_BITS_ARB:
773 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
774 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
775 break;
776 case WGL_BLUE_BITS_ARB:
777 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
778 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
779 break;
780 case WGL_RED_BITS_ARB:
781 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
782 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
783 break;
784 case WGL_GREEN_BITS_ARB:
785 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
786 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
787 break;
788 case WGL_ALPHA_BITS_ARB:
789 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
790 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
791 break;
792 case WGL_DEPTH_BITS_ARB:
793 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
794 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
795 break;
796 case WGL_STENCIL_BITS_ARB:
797 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
798 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
799 break;
800 case WGL_DOUBLE_BUFFER_ARB:
801 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
802 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
803 break;
804 case WGL_STEREO_ARB:
805 PUSH2(oGLXAttr, GLX_STEREO, pop);
806 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
807 break;
809 case WGL_PIXEL_TYPE_ARB:
810 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
811 switch (pop) {
812 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
813 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
814 /* 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 */
815 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
816 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
817 default:
818 ERR("unexpected PixelType(%x)\n", pop);
820 break;
822 case WGL_SUPPORT_GDI_ARB:
823 /* This flag is set in a pixel format */
824 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
825 break;
827 case WGL_DRAW_TO_BITMAP_ARB:
828 /* This flag is set in a pixel format */
829 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
830 break;
832 case WGL_DRAW_TO_WINDOW_ARB:
833 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
834 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
835 if (pop) {
836 drawattrib |= GLX_WINDOW_BIT;
838 break;
840 case WGL_DRAW_TO_PBUFFER_ARB:
841 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
842 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
843 if (pop) {
844 drawattrib |= GLX_PBUFFER_BIT;
846 break;
848 case WGL_ACCELERATION_ARB:
849 /* This flag is set in a pixel format */
850 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
851 break;
853 case WGL_SUPPORT_OPENGL_ARB:
854 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
855 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
856 break;
858 case WGL_SWAP_METHOD_ARB:
859 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
860 if (has_swap_method)
862 switch (pop)
864 case WGL_SWAP_EXCHANGE_ARB:
865 pop = GLX_SWAP_EXCHANGE_OML;
866 break;
867 case WGL_SWAP_COPY_ARB:
868 pop = GLX_SWAP_COPY_OML;
869 break;
870 case WGL_SWAP_UNDEFINED_ARB:
871 pop = GLX_SWAP_UNDEFINED_OML;
872 break;
873 default:
874 ERR("Unexpected swap method %#x.\n", pop);
875 pop = GLX_DONT_CARE;
877 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
879 else
881 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
883 break;
885 case WGL_PBUFFER_LARGEST_ARB:
886 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
887 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
888 break;
890 case WGL_SAMPLE_BUFFERS_ARB:
891 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
892 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
893 break;
895 case WGL_SAMPLES_ARB:
896 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
897 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
898 break;
900 case WGL_TEXTURE_FORMAT_ARB:
901 case WGL_TEXTURE_TARGET_ARB:
902 case WGL_MIPMAP_TEXTURE_ARB:
903 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur - 1], iWGLAttr[cur]);
904 if (NULL == pbuf) {
905 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
907 if (!use_render_texture_emulation) {
908 if (WGL_NO_TEXTURE_ARB != pop) {
909 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
910 return -1; /** error: don't support it */
911 } else {
912 drawattrib |= GLX_PBUFFER_BIT;
915 break ;
916 case WGL_FLOAT_COMPONENTS_NV:
917 nvfloatattrib = pop;
918 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
919 break ;
920 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
921 case WGL_BIND_TO_TEXTURE_RGB_ARB:
922 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
923 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
924 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
925 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
926 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
927 /** cannot be converted, see direct handling on
928 * - wglGetPixelFormatAttribivARB
929 * TODO: wglChoosePixelFormat
931 break ;
932 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
933 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
934 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
935 break ;
937 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
938 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
939 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
940 break ;
941 default:
942 FIXME("unsupported %x WGL Attribute\n", attr);
943 break;
945 ++cur;
948 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
949 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
950 * pixmap and pbuffer formats appear as well. */
951 if (!drawattrib) drawattrib = GLX_DONT_CARE;
952 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
953 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
955 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
956 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
957 * GLX_DONT_CARE unless it is overridden. */
958 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
959 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
961 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
962 if (has_extension(glxExtensions, "GLX_NV_float_buffer")) {
963 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
964 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
967 return nAttribs;
970 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
972 int render_type, render_type_bit;
973 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
974 switch(render_type_bit)
976 case GLX_RGBA_BIT:
977 render_type = GLX_RGBA_TYPE;
978 break;
979 case GLX_COLOR_INDEX_BIT:
980 render_type = GLX_COLOR_INDEX_TYPE;
981 break;
982 case GLX_RGBA_FLOAT_BIT:
983 render_type = GLX_RGBA_FLOAT_TYPE;
984 break;
985 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
986 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
987 break;
988 default:
989 ERR("Unknown render_type: %x\n", render_type_bit);
990 render_type = 0;
992 return render_type;
995 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
996 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
998 int dbuf, value;
999 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
1000 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1002 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1003 * the GLX_PIXMAP_BIT set. */
1004 return !dbuf && (value & GLX_PIXMAP_BIT);
1007 static void init_pixel_formats( Display *display )
1009 struct wgl_pixel_format *list;
1010 int size = 0, onscreen_size = 0;
1011 int fmt_id, nCfgs, i, run, bmp_formats;
1012 GLXFBConfig* cfgs;
1013 XVisualInfo *visinfo;
1015 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1016 if (NULL == cfgs || 0 == nCfgs) {
1017 if(cfgs != NULL) XFree(cfgs);
1018 ERR("glXChooseFBConfig returns NULL\n");
1019 return;
1022 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1023 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1024 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1025 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1027 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1029 for(i=0, bmp_formats=0; i<nCfgs; i++)
1031 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1032 bmp_formats++;
1034 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1036 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1038 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1039 * Do this as GLX doesn't guarantee that the list is sorted */
1040 for(run=0; run < 2; run++)
1042 for(i=0; i<nCfgs; i++) {
1043 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1044 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1046 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1047 * The second run we only set offscreen formats. */
1048 if(!run && visinfo)
1050 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1051 * The contents is copied to the destination using XCopyArea. For the copying to work
1052 * the depth of the source and destination window should be the same. In general this should
1053 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1054 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1055 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1056 * list formats with the same depth. */
1057 if(visinfo->depth != default_visual.depth)
1059 XFree(visinfo);
1060 continue;
1063 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1064 list[size].fbconfig = cfgs[i];
1065 list[size].visual = visinfo;
1066 list[size].fmt_id = fmt_id;
1067 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1068 list[size].dwFlags = 0;
1069 size++;
1070 onscreen_size++;
1072 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1073 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1075 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1076 list[size].fbconfig = cfgs[i];
1077 list[size].visual = visinfo;
1078 list[size].fmt_id = fmt_id;
1079 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1080 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1081 size++;
1082 onscreen_size++;
1084 } else if(run && !visinfo) {
1085 int window_drawable=0;
1086 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1088 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1089 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1090 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1091 * likely make our child window opengl rendering more complicated since likely you can't use
1092 * XCopyArea on a GLX Window.
1093 * For now ignore fbconfigs which are window drawable but lack a visual. */
1094 if(window_drawable & GLX_WINDOW_BIT)
1096 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1097 continue;
1100 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1101 list[size].fbconfig = cfgs[i];
1102 list[size].fmt_id = fmt_id;
1103 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1104 list[size].dwFlags = 0;
1105 size++;
1107 else if (visinfo) XFree(visinfo);
1111 XFree(cfgs);
1113 pixel_formats = list;
1114 nb_pixel_formats = size;
1115 nb_onscreen_formats = onscreen_size;
1118 static inline BOOL is_valid_pixel_format( int format )
1120 return format > 0 && format <= nb_pixel_formats;
1123 static inline BOOL is_onscreen_pixel_format( int format )
1125 return format > 0 && format <= nb_onscreen_formats;
1128 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1130 return format - pixel_formats + 1;
1133 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1134 * In our WGL implementation we only support a subset of these formats namely the format of
1135 * Wine's main visual and offscreen formats (if they are available).
1136 * This function converts a WGL format to its corresponding GLX one.
1138 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1140 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1141 * iPixelFormat in case of probing the number of pixelformats.
1143 if (is_valid_pixel_format( iPixelFormat ) &&
1144 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1145 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1146 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1147 return &pixel_formats[iPixelFormat-1];
1149 return NULL;
1152 static struct gl_drawable *grab_gl_drawable( struct gl_drawable *gl )
1154 InterlockedIncrement( &gl->ref );
1155 return gl;
1158 static void release_gl_drawable( struct gl_drawable *gl )
1160 if (!gl) return;
1161 if (InterlockedDecrement( &gl->ref )) return;
1162 switch (gl->type)
1164 case DC_GL_WINDOW:
1165 case DC_GL_CHILD_WIN:
1166 TRACE( "destroying %lx drawable %lx\n", gl->window, gl->drawable );
1167 pglXDestroyWindow( gdi_display, gl->drawable );
1168 XDestroyWindow( gdi_display, gl->window );
1169 break;
1170 case DC_GL_PIXMAP_WIN:
1171 TRACE( "destroying pixmap %lx drawable %lx\n", gl->pixmap, gl->drawable );
1172 pglXDestroyPixmap( gdi_display, gl->drawable );
1173 XFreePixmap( gdi_display, gl->pixmap );
1174 break;
1175 default:
1176 break;
1178 HeapFree( GetProcessHeap(), 0, gl );
1181 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1182 static void mark_drawable_dirty( struct gl_drawable *old, struct gl_drawable *new )
1184 struct wgl_context *ctx;
1186 EnterCriticalSection( &context_section );
1187 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1189 if (old == ctx->drawables[0] || old == ctx->new_drawables[0])
1191 release_gl_drawable( ctx->new_drawables[0] );
1192 ctx->new_drawables[0] = grab_gl_drawable( new );
1194 if (old == ctx->drawables[1] || old == ctx->new_drawables[1])
1196 release_gl_drawable( ctx->new_drawables[1] );
1197 ctx->new_drawables[1] = grab_gl_drawable( new );
1200 LeaveCriticalSection( &context_section );
1203 /* Given the current context, make sure its drawable is sync'd */
1204 static inline void sync_context(struct wgl_context *context)
1206 BOOL refresh = FALSE;
1208 EnterCriticalSection( &context_section );
1209 if (context->new_drawables[0])
1211 release_gl_drawable( context->drawables[0] );
1212 context->drawables[0] = context->new_drawables[0];
1213 context->new_drawables[0] = NULL;
1214 refresh = TRUE;
1216 if (context->new_drawables[1])
1218 release_gl_drawable( context->drawables[1] );
1219 context->drawables[1] = context->new_drawables[1];
1220 context->new_drawables[1] = NULL;
1221 refresh = TRUE;
1223 if (refresh)
1225 if (glxRequireVersion(3))
1226 pglXMakeContextCurrent(gdi_display, context->drawables[0]->drawable,
1227 context->drawables[1]->drawable, context->ctx);
1228 else
1229 pglXMakeCurrent(gdi_display, context->drawables[0]->drawable, context->ctx);
1231 LeaveCriticalSection( &context_section );
1234 static BOOL set_swap_interval(GLXDrawable drawable, int interval)
1236 BOOL ret = TRUE;
1238 switch (swap_control_method)
1240 case GLX_SWAP_CONTROL_EXT:
1241 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1242 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1243 XSync(gdi_display, False);
1244 ret = !X11DRV_check_error();
1245 break;
1247 case GLX_SWAP_CONTROL_MESA:
1248 ret = !pglXSwapIntervalMESA(interval);
1249 break;
1251 case GLX_SWAP_CONTROL_SGI:
1252 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1253 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1254 * value to be an error. Just silently ignore the request for now.
1256 if (!interval)
1257 WARN("Request to disable vertical sync is not handled\n");
1258 else
1259 ret = !pglXSwapIntervalSGI(interval);
1260 break;
1262 case GLX_SWAP_CONTROL_NONE:
1263 /* Unlikely to happen on modern GLX implementations */
1264 WARN("Request to adjust swap interval is not handled\n");
1265 break;
1268 return ret;
1271 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1273 struct gl_drawable *gl;
1275 EnterCriticalSection( &context_section );
1276 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1277 gl = grab_gl_drawable( gl );
1278 else if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
1279 gl = grab_gl_drawable( gl );
1280 else
1281 gl = NULL;
1282 LeaveCriticalSection( &context_section );
1283 return gl;
1286 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1288 GLXContext ctx;
1290 if(context->gl3_context)
1292 if(context->numAttribs)
1293 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, context->attribList);
1294 else
1295 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, NULL);
1297 else if(context->fmt->visual)
1298 ctx = pglXCreateContext(gdi_display, context->fmt->visual, shareList, GL_TRUE);
1299 else /* Create a GLX Context for a pbuffer */
1300 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1302 return ctx;
1306 /***********************************************************************
1307 * create_gl_drawable
1309 static struct gl_drawable *create_gl_drawable( HWND hwnd, const struct wgl_pixel_format *format, BOOL known_child )
1311 struct gl_drawable *gl, *prev;
1312 XVisualInfo *visual = format->visual;
1313 RECT rect;
1314 int width, height;
1316 GetClientRect( hwnd, &rect );
1317 width = min( max( 1, rect.right ), 65535 );
1318 height = min( max( 1, rect.bottom ), 65535 );
1320 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) ))) return NULL;
1322 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1323 * there is no way to query it, so we have to store it here.
1325 gl->swap_interval = 1;
1326 gl->refresh_swap_interval = TRUE;
1327 gl->format = format;
1328 gl->ref = 1;
1330 if (!known_child && !GetWindow( hwnd, GW_CHILD ) && GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* childless top-level window */
1332 gl->type = DC_GL_WINDOW;
1333 gl->window = create_client_window( hwnd, visual );
1334 if (gl->window)
1335 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1336 TRACE( "%p created client %lx drawable %lx\n", hwnd, gl->window, gl->drawable );
1338 #ifdef SONAME_LIBXCOMPOSITE
1339 else if(usexcomposite)
1341 gl->type = DC_GL_CHILD_WIN;
1342 gl->window = create_client_window( hwnd, visual );
1343 if (gl->window)
1345 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1346 pXCompositeRedirectWindow( gdi_display, gl->window, CompositeRedirectManual );
1348 TRACE( "%p created child %lx drawable %lx\n", hwnd, gl->window, gl->drawable );
1350 #endif
1351 else
1353 WARN("XComposite is not available, using GLXPixmap hack\n");
1355 gl->type = DC_GL_PIXMAP_WIN;
1356 gl->pixmap = XCreatePixmap( gdi_display, root_window, width, height, visual->depth );
1357 if (gl->pixmap)
1359 gl->drawable = pglXCreatePixmap( gdi_display, gl->format->fbconfig, gl->pixmap, NULL );
1360 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1361 gl->pixmap_size.cx = width;
1362 gl->pixmap_size.cy = height;
1366 if (!gl->drawable)
1368 HeapFree( GetProcessHeap(), 0, gl );
1369 return NULL;
1372 EnterCriticalSection( &context_section );
1373 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1375 gl->swap_interval = prev->swap_interval;
1376 release_gl_drawable( prev );
1378 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)grab_gl_drawable(gl) );
1379 LeaveCriticalSection( &context_section );
1380 return gl;
1384 /***********************************************************************
1385 * set_win_format
1387 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1389 struct gl_drawable *gl;
1391 if (!format->visual) return FALSE;
1393 if (!(gl = create_gl_drawable( hwnd, format, FALSE ))) return FALSE;
1395 TRACE( "created GL drawable %lx for win %p %s\n",
1396 gl->drawable, hwnd, debugstr_fbconfig( format->fbconfig ));
1398 XFlush( gdi_display );
1399 release_gl_drawable( gl );
1401 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1402 return TRUE;
1406 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1408 const struct wgl_pixel_format *fmt;
1409 int value;
1410 HWND hwnd = WindowFromDC( hdc );
1412 TRACE("(%p,%d)\n", hdc, format);
1414 if (!hwnd || hwnd == GetDesktopWindow())
1416 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1417 return FALSE;
1420 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1421 if (!fmt)
1423 ERR( "Invalid format %d\n", format );
1424 return FALSE;
1427 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1428 if (!(value & GLX_WINDOW_BIT))
1430 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1431 return FALSE;
1434 if (!allow_change)
1436 struct gl_drawable *gl;
1437 if ((gl = get_gl_drawable( hwnd, hdc )))
1439 int prev = pixel_format_index( gl->format );
1440 release_gl_drawable( gl );
1441 return prev == format; /* cannot change it if already set */
1445 return set_win_format( hwnd, fmt );
1449 /***********************************************************************
1450 * sync_gl_drawable
1452 void sync_gl_drawable( HWND hwnd, BOOL known_child )
1454 struct gl_drawable *old, *new;
1456 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1458 switch (old->type)
1460 case DC_GL_WINDOW:
1461 if (!known_child) break; /* Still a childless top-level window */
1462 /* fall through */
1463 case DC_GL_PIXMAP_WIN:
1464 if (!(new = create_gl_drawable( hwnd, old->format, known_child ))) break;
1465 mark_drawable_dirty( old, new );
1466 XFlush( gdi_display );
1467 TRACE( "Recreated GL drawable %lx to replace %lx\n", new->drawable, old->drawable );
1468 release_gl_drawable( new );
1469 break;
1470 default:
1471 break;
1473 release_gl_drawable( old );
1477 /***********************************************************************
1478 * set_gl_drawable_parent
1480 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1482 struct gl_drawable *old, *new;
1484 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1486 TRACE( "setting drawable %lx parent %p\n", old->drawable, parent );
1488 switch (old->type)
1490 case DC_GL_WINDOW:
1491 break;
1492 case DC_GL_CHILD_WIN:
1493 case DC_GL_PIXMAP_WIN:
1494 if (parent == GetDesktopWindow()) break;
1495 /* fall through */
1496 default:
1497 release_gl_drawable( old );
1498 return;
1501 if ((new = create_gl_drawable( hwnd, old->format, FALSE )))
1503 mark_drawable_dirty( old, new );
1504 release_gl_drawable( new );
1506 else
1508 destroy_gl_drawable( hwnd );
1509 __wine_set_pixel_format( hwnd, 0 );
1511 release_gl_drawable( old );
1515 /***********************************************************************
1516 * destroy_gl_drawable
1518 void destroy_gl_drawable( HWND hwnd )
1520 struct gl_drawable *gl;
1522 EnterCriticalSection( &context_section );
1523 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1525 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1526 release_gl_drawable( gl );
1528 LeaveCriticalSection( &context_section );
1533 * glxdrv_DescribePixelFormat
1535 * Get the pixel-format descriptor associated to the given id
1537 static int WINAPI describe_pixel_format( int iPixelFormat, PIXELFORMATDESCRIPTOR *ppfd, BOOL allow_offscreen )
1539 /*XVisualInfo *vis;*/
1540 int value;
1541 int rb,gb,bb,ab;
1542 const struct wgl_pixel_format *fmt;
1544 if (!has_opengl()) return 0;
1546 /* 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 */
1547 fmt = get_pixel_format(gdi_display, iPixelFormat, allow_offscreen);
1548 if (!fmt) {
1549 WARN("unexpected format %d\n", iPixelFormat);
1550 return 0;
1553 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1554 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1555 ppfd->nVersion = 1;
1557 /* These flags are always the same... */
1558 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1559 /* Now the flags extracted from the Visual */
1561 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1562 if(value & GLX_WINDOW_BIT)
1563 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1565 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1566 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1567 * 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
1568 * offered the GDI bit either. */
1569 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1571 /* PFD_GENERIC_FORMAT - gdi software rendering
1572 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1573 * none set - full hardware accelerated by a ICD
1575 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1576 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1578 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1579 if (value) {
1580 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1581 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1583 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1585 /* Pixel type */
1586 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1587 if (value & GLX_RGBA_BIT)
1588 ppfd->iPixelType = PFD_TYPE_RGBA;
1589 else
1590 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1592 /* Color bits */
1593 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1594 ppfd->cColorBits = value;
1596 /* Red, green, blue and alpha bits / shifts */
1597 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1598 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1599 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1600 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1601 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1603 ppfd->cRedBits = rb;
1604 ppfd->cRedShift = gb + bb + ab;
1605 ppfd->cBlueBits = bb;
1606 ppfd->cBlueShift = ab;
1607 ppfd->cGreenBits = gb;
1608 ppfd->cGreenShift = bb + ab;
1609 ppfd->cAlphaBits = ab;
1610 ppfd->cAlphaShift = 0;
1611 } else {
1612 ppfd->cRedBits = 0;
1613 ppfd->cRedShift = 0;
1614 ppfd->cBlueBits = 0;
1615 ppfd->cBlueShift = 0;
1616 ppfd->cGreenBits = 0;
1617 ppfd->cGreenShift = 0;
1618 ppfd->cAlphaBits = 0;
1619 ppfd->cAlphaShift = 0;
1622 /* Accum RGBA bits */
1623 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1624 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1625 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1626 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1628 ppfd->cAccumBits = rb+gb+bb+ab;
1629 ppfd->cAccumRedBits = rb;
1630 ppfd->cAccumGreenBits = gb;
1631 ppfd->cAccumBlueBits = bb;
1632 ppfd->cAccumAlphaBits = ab;
1634 /* Aux bits */
1635 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1636 ppfd->cAuxBuffers = value;
1638 /* Depth bits */
1639 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1640 ppfd->cDepthBits = value;
1642 /* stencil bits */
1643 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1644 ppfd->cStencilBits = value;
1646 ppfd->iLayerType = PFD_MAIN_PLANE;
1648 if (TRACE_ON(wgl)) {
1649 dump_PIXELFORMATDESCRIPTOR(ppfd);
1652 return nb_onscreen_formats;
1656 * glxdrv_DescribePixelFormat
1658 * Get the pixel-format descriptor associated to the given id
1660 static int WINAPI glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1661 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1663 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1665 if (!ppfd) return nb_onscreen_formats;
1667 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR))
1669 ERR("Wrong structure size !\n");
1670 /* Should set error */
1671 return 0;
1674 return describe_pixel_format(iPixelFormat, ppfd, FALSE);
1677 /***********************************************************************
1678 * glxdrv_wglGetPixelFormat
1680 static int WINAPI glxdrv_wglGetPixelFormat( HDC hdc )
1682 struct gl_drawable *gl;
1683 int ret = 0;
1685 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1687 ret = pixel_format_index( gl->format );
1688 /* Offscreen formats can't be used with traditional WGL calls.
1689 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1690 if (!is_onscreen_pixel_format( ret )) ret = 1;
1691 release_gl_drawable( gl );
1693 TRACE( "%p -> %d\n", hdc, ret );
1694 return ret;
1697 /***********************************************************************
1698 * glxdrv_wglSetPixelFormat
1700 static BOOL WINAPI glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1702 return set_pixel_format(hdc, iPixelFormat, FALSE);
1705 /***********************************************************************
1706 * glxdrv_wglCopyContext
1708 static BOOL WINAPI glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1710 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1712 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1714 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1715 return TRUE;
1718 /***********************************************************************
1719 * glxdrv_wglCreateContext
1721 static struct wgl_context * WINAPI glxdrv_wglCreateContext( HDC hdc )
1723 struct wgl_context *ret;
1724 struct gl_drawable *gl;
1726 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1728 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1729 return NULL;
1732 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1734 ret->hdc = hdc;
1735 ret->fmt = gl->format;
1736 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1737 EnterCriticalSection( &context_section );
1738 list_add_head( &context_list, &ret->entry );
1739 LeaveCriticalSection( &context_section );
1741 release_gl_drawable( gl );
1742 TRACE( "%p -> %p\n", hdc, ret );
1743 return ret;
1746 /***********************************************************************
1747 * glxdrv_wglDeleteContext
1749 static BOOL WINAPI glxdrv_wglDeleteContext(struct wgl_context *ctx)
1751 struct wgl_pbuffer *pb;
1753 TRACE("(%p)\n", ctx);
1755 EnterCriticalSection( &context_section );
1756 list_remove( &ctx->entry );
1757 LIST_FOR_EACH_ENTRY( pb, &pbuffer_list, struct wgl_pbuffer, entry )
1759 if (pb->prev_context == ctx->ctx) {
1760 pglXDestroyContext(gdi_display, pb->tmp_context);
1761 pb->prev_context = pb->tmp_context = NULL;
1764 LeaveCriticalSection( &context_section );
1766 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1767 release_gl_drawable( ctx->drawables[0] );
1768 release_gl_drawable( ctx->drawables[1] );
1769 release_gl_drawable( ctx->new_drawables[0] );
1770 release_gl_drawable( ctx->new_drawables[1] );
1771 return HeapFree( GetProcessHeap(), 0, ctx );
1774 /***********************************************************************
1775 * glxdrv_wglGetProcAddress
1777 static PROC WINAPI glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1779 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1780 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1783 static void set_context_drawables( struct wgl_context *ctx, struct gl_drawable *draw,
1784 struct gl_drawable *read )
1786 struct gl_drawable *prev[4];
1787 int i;
1789 prev[0] = ctx->drawables[0];
1790 prev[1] = ctx->drawables[1];
1791 prev[2] = ctx->new_drawables[0];
1792 prev[3] = ctx->new_drawables[1];
1793 ctx->drawables[0] = grab_gl_drawable( draw );
1794 ctx->drawables[1] = read ? grab_gl_drawable( read ) : NULL;
1795 ctx->new_drawables[0] = ctx->new_drawables[1] = NULL;
1796 for (i = 0; i < 4; i++) release_gl_drawable( prev[i] );
1799 /***********************************************************************
1800 * glxdrv_wglMakeCurrent
1802 static BOOL WINAPI glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1804 BOOL ret = FALSE;
1805 struct gl_drawable *gl;
1807 TRACE("(%p,%p)\n", hdc, ctx);
1809 if (!ctx)
1811 pglXMakeCurrent(gdi_display, None, NULL);
1812 NtCurrentTeb()->glContext = NULL;
1813 return TRUE;
1816 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1818 if (ctx->fmt != gl->format)
1820 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1821 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1822 goto done;
1825 TRACE("hdc %p drawable %lx fmt %p ctx %p %s\n", hdc, gl->drawable, gl->format, ctx->ctx,
1826 debugstr_fbconfig( gl->format->fbconfig ));
1828 EnterCriticalSection( &context_section );
1829 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1830 if (ret)
1832 NtCurrentTeb()->glContext = ctx;
1833 ctx->has_been_current = TRUE;
1834 ctx->hdc = hdc;
1835 set_context_drawables( ctx, gl, gl );
1836 ctx->refresh_drawables = FALSE;
1837 LeaveCriticalSection( &context_section );
1838 goto done;
1840 LeaveCriticalSection( &context_section );
1842 SetLastError( ERROR_INVALID_HANDLE );
1844 done:
1845 release_gl_drawable( gl );
1846 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1847 return ret;
1850 /***********************************************************************
1851 * X11DRV_wglMakeContextCurrentARB
1853 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1855 BOOL ret = FALSE;
1856 struct gl_drawable *draw_gl, *read_gl = NULL;
1858 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1860 if (!ctx)
1862 pglXMakeCurrent(gdi_display, None, NULL);
1863 NtCurrentTeb()->glContext = NULL;
1864 return TRUE;
1867 if (!pglXMakeContextCurrent) return FALSE;
1869 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1871 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1873 EnterCriticalSection( &context_section );
1874 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1875 read_gl ? read_gl->drawable : 0, ctx->ctx);
1876 if (ret)
1878 ctx->has_been_current = TRUE;
1879 ctx->hdc = draw_hdc;
1880 set_context_drawables( ctx, draw_gl, read_gl );
1881 ctx->refresh_drawables = FALSE;
1882 NtCurrentTeb()->glContext = ctx;
1883 LeaveCriticalSection( &context_section );
1884 goto done;
1886 LeaveCriticalSection( &context_section );
1888 SetLastError( ERROR_INVALID_HANDLE );
1889 done:
1890 release_gl_drawable( read_gl );
1891 release_gl_drawable( draw_gl );
1892 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1893 return ret;
1896 /***********************************************************************
1897 * glxdrv_wglShareLists
1899 static BOOL WINAPI glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1901 TRACE("(%p, %p)\n", org, dest);
1903 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1904 * at context creation time but in case of WGL it is done using wglShareLists.
1905 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1906 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1907 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1908 * so there delaying context creation doesn't work.
1910 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1911 * and when a program requests sharing we recreate the destination context if it hasn't been made
1912 * current or when it hasn't shared display lists before.
1915 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1917 ERR("Could not share display lists, one of the contexts has been current already !\n");
1918 return FALSE;
1920 else if(dest->sharing)
1922 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1923 return FALSE;
1925 else
1927 /* Re-create the GLX context and share display lists */
1928 pglXDestroyContext(gdi_display, dest->ctx);
1929 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1930 TRACE(" re-created context (%p) for Wine context %p (%s) sharing lists with ctx %p (%s)\n",
1931 dest->ctx, dest, debugstr_fbconfig(dest->fmt->fbconfig),
1932 org->ctx, debugstr_fbconfig( org->fmt->fbconfig));
1934 org->sharing = TRUE;
1935 dest->sharing = TRUE;
1936 return TRUE;
1938 return FALSE;
1941 static void wglFinish(void)
1943 struct x11drv_escape_flush_gl_drawable escape;
1944 struct gl_drawable *gl;
1945 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1947 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1948 escape.gl_drawable = 0;
1949 escape.flush = FALSE;
1951 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1953 switch (gl->type)
1955 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1956 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
1957 default: break;
1959 sync_context(ctx);
1960 release_gl_drawable( gl );
1963 pglFinish();
1964 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1967 static void wglFlush(void)
1969 struct x11drv_escape_flush_gl_drawable escape;
1970 struct gl_drawable *gl;
1971 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1973 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1974 escape.gl_drawable = 0;
1975 escape.flush = FALSE;
1977 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1979 switch (gl->type)
1981 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1982 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
1983 default: break;
1985 sync_context(ctx);
1986 release_gl_drawable( gl );
1989 pglFlush();
1990 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1993 static const GLubyte *wglGetString(GLenum name)
1995 if (name == GL_EXTENSIONS && glExtensions) return (const GLubyte *)glExtensions;
1996 return pglGetString(name);
1999 /***********************************************************************
2000 * X11DRV_wglCreateContextAttribsARB
2002 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2003 const int* attribList )
2005 struct wgl_context *ret;
2006 struct gl_drawable *gl;
2007 int err = 0;
2009 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2011 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2013 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2014 return NULL;
2017 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2019 ret->hdc = hdc;
2020 ret->fmt = gl->format;
2021 ret->gl3_context = TRUE;
2022 if (attribList)
2024 int *pContextAttribList = &ret->attribList[0];
2025 /* attribList consists of pairs {token, value] terminated with 0 */
2026 while(attribList[0] != 0)
2028 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2029 switch(attribList[0])
2031 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2032 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2033 pContextAttribList[1] = attribList[1];
2034 pContextAttribList += 2;
2035 ret->numAttribs++;
2036 break;
2037 case WGL_CONTEXT_MINOR_VERSION_ARB:
2038 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2039 pContextAttribList[1] = attribList[1];
2040 pContextAttribList += 2;
2041 ret->numAttribs++;
2042 break;
2043 case WGL_CONTEXT_LAYER_PLANE_ARB:
2044 break;
2045 case WGL_CONTEXT_FLAGS_ARB:
2046 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2047 pContextAttribList[1] = attribList[1];
2048 pContextAttribList += 2;
2049 ret->numAttribs++;
2050 break;
2051 case WGL_CONTEXT_OPENGL_NO_ERROR_ARB:
2052 pContextAttribList[0] = GLX_CONTEXT_OPENGL_NO_ERROR_ARB;
2053 pContextAttribList[1] = attribList[1];
2054 pContextAttribList += 2;
2055 ret->numAttribs++;
2056 break;
2057 case WGL_CONTEXT_PROFILE_MASK_ARB:
2058 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2059 pContextAttribList[1] = attribList[1];
2060 pContextAttribList += 2;
2061 ret->numAttribs++;
2062 break;
2063 case WGL_RENDERER_ID_WINE:
2064 pContextAttribList[0] = GLX_RENDERER_ID_MESA;
2065 pContextAttribList[1] = attribList[1];
2066 pContextAttribList += 2;
2067 ret->numAttribs++;
2068 break;
2069 default:
2070 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2072 attribList += 2;
2076 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2077 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2078 XSync(gdi_display, False);
2079 if ((err = X11DRV_check_error()) || !ret->ctx)
2081 /* In the future we should convert the GLX error to a win32 one here if needed */
2082 WARN("Context creation failed (error %#x).\n", err);
2083 HeapFree( GetProcessHeap(), 0, ret );
2084 ret = NULL;
2086 else
2088 EnterCriticalSection( &context_section );
2089 list_add_head( &context_list, &ret->entry );
2090 LeaveCriticalSection( &context_section );
2094 release_gl_drawable( gl );
2095 TRACE( "%p -> %p\n", hdc, ret );
2096 return ret;
2100 * X11DRV_wglGetExtensionsStringARB
2102 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2104 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2106 TRACE("() returning \"%s\"\n", wglExtensions);
2107 return wglExtensions;
2111 * X11DRV_wglCreatePbufferARB
2113 * WGL_ARB_pbuffer: wglCreatePbufferARB
2115 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2116 const int *piAttribList )
2118 struct wgl_pbuffer* object;
2119 const struct wgl_pixel_format *fmt;
2120 int attribs[256];
2121 int nAttribs = 0;
2123 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2125 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2126 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2127 if(!fmt) {
2128 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2129 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2130 return NULL;
2133 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2134 if (NULL == object) {
2135 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2136 return NULL;
2138 object->width = iWidth;
2139 object->height = iHeight;
2140 object->fmt = fmt;
2142 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2143 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2144 while (piAttribList && 0 != *piAttribList) {
2145 int attr_v;
2146 switch (*piAttribList) {
2147 case WGL_PBUFFER_LARGEST_ARB: {
2148 ++piAttribList;
2149 attr_v = *piAttribList;
2150 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2151 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2152 break;
2155 case WGL_TEXTURE_FORMAT_ARB: {
2156 ++piAttribList;
2157 attr_v = *piAttribList;
2158 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2159 if (WGL_NO_TEXTURE_ARB == attr_v) {
2160 object->use_render_texture = 0;
2161 } else {
2162 if (!use_render_texture_emulation) {
2163 SetLastError(ERROR_INVALID_DATA);
2164 goto create_failed;
2166 switch (attr_v) {
2167 case WGL_TEXTURE_RGB_ARB:
2168 object->use_render_texture = GL_RGB;
2169 object->texture_bpp = 3;
2170 object->texture_format = GL_RGB;
2171 object->texture_type = GL_UNSIGNED_BYTE;
2172 break;
2173 case WGL_TEXTURE_RGBA_ARB:
2174 object->use_render_texture = GL_RGBA;
2175 object->texture_bpp = 4;
2176 object->texture_format = GL_RGBA;
2177 object->texture_type = GL_UNSIGNED_BYTE;
2178 break;
2180 /* WGL_FLOAT_COMPONENTS_NV */
2181 case WGL_TEXTURE_FLOAT_R_NV:
2182 object->use_render_texture = GL_FLOAT_R_NV;
2183 object->texture_bpp = 4;
2184 object->texture_format = GL_RED;
2185 object->texture_type = GL_FLOAT;
2186 break;
2187 case WGL_TEXTURE_FLOAT_RG_NV:
2188 object->use_render_texture = GL_FLOAT_RG_NV;
2189 object->texture_bpp = 8;
2190 object->texture_format = GL_LUMINANCE_ALPHA;
2191 object->texture_type = GL_FLOAT;
2192 break;
2193 case WGL_TEXTURE_FLOAT_RGB_NV:
2194 object->use_render_texture = GL_FLOAT_RGB_NV;
2195 object->texture_bpp = 12;
2196 object->texture_format = GL_RGB;
2197 object->texture_type = GL_FLOAT;
2198 break;
2199 case WGL_TEXTURE_FLOAT_RGBA_NV:
2200 object->use_render_texture = GL_FLOAT_RGBA_NV;
2201 object->texture_bpp = 16;
2202 object->texture_format = GL_RGBA;
2203 object->texture_type = GL_FLOAT;
2204 break;
2205 default:
2206 ERR("Unknown texture format: %x\n", attr_v);
2207 SetLastError(ERROR_INVALID_DATA);
2208 goto create_failed;
2211 break;
2214 case WGL_TEXTURE_TARGET_ARB: {
2215 ++piAttribList;
2216 attr_v = *piAttribList;
2217 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2218 if (WGL_NO_TEXTURE_ARB == attr_v) {
2219 object->texture_target = 0;
2220 } else {
2221 if (!use_render_texture_emulation) {
2222 SetLastError(ERROR_INVALID_DATA);
2223 goto create_failed;
2225 switch (attr_v) {
2226 case WGL_TEXTURE_CUBE_MAP_ARB: {
2227 if (iWidth != iHeight) {
2228 SetLastError(ERROR_INVALID_DATA);
2229 goto create_failed;
2231 object->texture_target = GL_TEXTURE_CUBE_MAP;
2232 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2233 break;
2235 case WGL_TEXTURE_1D_ARB: {
2236 if (1 != iHeight) {
2237 SetLastError(ERROR_INVALID_DATA);
2238 goto create_failed;
2240 object->texture_target = GL_TEXTURE_1D;
2241 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2242 break;
2244 case WGL_TEXTURE_2D_ARB: {
2245 object->texture_target = GL_TEXTURE_2D;
2246 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2247 break;
2249 case WGL_TEXTURE_RECTANGLE_NV: {
2250 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2251 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2252 break;
2254 default:
2255 ERR("Unknown texture target: %x\n", attr_v);
2256 SetLastError(ERROR_INVALID_DATA);
2257 goto create_failed;
2260 break;
2263 case WGL_MIPMAP_TEXTURE_ARB: {
2264 ++piAttribList;
2265 attr_v = *piAttribList;
2266 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2267 if (!use_render_texture_emulation) {
2268 SetLastError(ERROR_INVALID_DATA);
2269 goto create_failed;
2271 break;
2274 ++piAttribList;
2277 PUSH1(attribs, None);
2278 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2279 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2280 if (!object->drawable) {
2281 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2282 goto create_failed; /* unexpected error */
2284 EnterCriticalSection( &context_section );
2285 list_add_head( &pbuffer_list, &object->entry );
2286 LeaveCriticalSection( &context_section );
2287 TRACE("->(%p)\n", object);
2288 return object;
2290 create_failed:
2291 HeapFree(GetProcessHeap(), 0, object);
2292 TRACE("->(FAILED)\n");
2293 return NULL;
2297 * X11DRV_wglDestroyPbufferARB
2299 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2301 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2303 TRACE("(%p)\n", object);
2305 EnterCriticalSection( &context_section );
2306 list_remove( &object->entry );
2307 LeaveCriticalSection( &context_section );
2308 pglXDestroyPbuffer(gdi_display, object->drawable);
2309 if (object->tmp_context)
2310 pglXDestroyContext(gdi_display, object->tmp_context);
2311 HeapFree(GetProcessHeap(), 0, object);
2312 return GL_TRUE;
2316 * X11DRV_wglGetPbufferDCARB
2318 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2320 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2322 struct x11drv_escape_set_drawable escape;
2323 struct gl_drawable *gl, *prev;
2324 HDC hdc;
2326 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2327 if (!hdc) return 0;
2329 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2331 DeleteDC( hdc );
2332 return 0;
2334 gl->type = DC_GL_PBUFFER;
2335 gl->drawable = object->drawable;
2336 gl->format = object->fmt;
2337 gl->ref = 1;
2339 EnterCriticalSection( &context_section );
2340 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2341 release_gl_drawable( prev );
2342 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2343 LeaveCriticalSection( &context_section );
2345 escape.code = X11DRV_SET_DRAWABLE;
2346 escape.drawable = object->drawable;
2347 escape.mode = IncludeInferiors;
2348 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2349 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2351 TRACE( "(%p)->(%p)\n", object, hdc );
2352 return hdc;
2356 * X11DRV_wglQueryPbufferARB
2358 * WGL_ARB_pbuffer: wglQueryPbufferARB
2360 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2362 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2364 switch (iAttribute) {
2365 case WGL_PBUFFER_WIDTH_ARB:
2366 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2367 break;
2368 case WGL_PBUFFER_HEIGHT_ARB:
2369 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2370 break;
2372 case WGL_PBUFFER_LOST_ARB:
2373 /* GLX Pbuffers cannot be lost by default. We can support this by
2374 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2375 * to receive pixel buffer clobber events, however that may or may
2376 * not give any benefit */
2377 *piValue = GL_FALSE;
2378 break;
2380 case WGL_TEXTURE_FORMAT_ARB:
2381 if (!object->use_render_texture) {
2382 *piValue = WGL_NO_TEXTURE_ARB;
2383 } else {
2384 if (!use_render_texture_emulation) {
2385 SetLastError(ERROR_INVALID_HANDLE);
2386 return GL_FALSE;
2388 switch(object->use_render_texture) {
2389 case GL_RGB:
2390 *piValue = WGL_TEXTURE_RGB_ARB;
2391 break;
2392 case GL_RGBA:
2393 *piValue = WGL_TEXTURE_RGBA_ARB;
2394 break;
2395 /* WGL_FLOAT_COMPONENTS_NV */
2396 case GL_FLOAT_R_NV:
2397 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2398 break;
2399 case GL_FLOAT_RG_NV:
2400 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2401 break;
2402 case GL_FLOAT_RGB_NV:
2403 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2404 break;
2405 case GL_FLOAT_RGBA_NV:
2406 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2407 break;
2408 default:
2409 ERR("Unknown texture format: %x\n", object->use_render_texture);
2412 break;
2414 case WGL_TEXTURE_TARGET_ARB:
2415 if (!object->texture_target){
2416 *piValue = WGL_NO_TEXTURE_ARB;
2417 } else {
2418 if (!use_render_texture_emulation) {
2419 SetLastError(ERROR_INVALID_DATA);
2420 return GL_FALSE;
2422 switch (object->texture_target) {
2423 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2424 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2425 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2426 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2429 break;
2431 case WGL_MIPMAP_TEXTURE_ARB:
2432 *piValue = GL_FALSE; /** don't support that */
2433 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2434 break;
2436 default:
2437 FIXME("unexpected attribute %x\n", iAttribute);
2438 break;
2441 return GL_TRUE;
2445 * X11DRV_wglReleasePbufferDCARB
2447 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2449 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2451 struct gl_drawable *gl;
2453 TRACE("(%p, %p)\n", object, hdc);
2455 EnterCriticalSection( &context_section );
2457 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2459 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2460 release_gl_drawable( gl );
2462 else hdc = 0;
2464 LeaveCriticalSection( &context_section );
2466 return hdc && DeleteDC(hdc);
2470 * X11DRV_wglSetPbufferAttribARB
2472 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2474 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2476 GLboolean ret = GL_FALSE;
2478 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2480 if (!object->use_render_texture) {
2481 SetLastError(ERROR_INVALID_HANDLE);
2482 return GL_FALSE;
2484 if (use_render_texture_emulation) {
2485 return GL_TRUE;
2487 return ret;
2490 struct choose_pixel_format_arb_format
2492 int format;
2493 int original_index;
2494 PIXELFORMATDESCRIPTOR pfd;
2495 int depth, stencil;
2498 static int compare_formats(const void *a, const void *b)
2500 /* Order formats so that onscreen formats go first. Then, if no depth bits requested,
2501 * prioritize formats with smaller depth within the original sort order with respect to
2502 * other attributes. */
2503 const struct choose_pixel_format_arb_format *fmt_a = a, *fmt_b = b;
2504 BOOL offscreen_a, offscreen_b;
2506 offscreen_a = fmt_a->format > nb_onscreen_formats;
2507 offscreen_b = fmt_b->format > nb_onscreen_formats;
2509 if (offscreen_a != offscreen_b)
2510 return offscreen_a - offscreen_b;
2511 if (memcmp(&fmt_a->pfd, &fmt_b->pfd, sizeof(fmt_a->pfd)))
2512 return fmt_a->original_index - fmt_b->original_index;
2513 if (fmt_a->depth != fmt_b->depth)
2514 return fmt_a->depth - fmt_b->depth;
2515 if (fmt_a->stencil != fmt_b->stencil)
2516 return fmt_a->stencil - fmt_b->stencil;
2518 return fmt_a->original_index - fmt_b->original_index;
2522 * X11DRV_wglChoosePixelFormatARB
2524 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2526 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2527 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2529 struct choose_pixel_format_arb_format *formats;
2530 int it, i, format_count;
2531 BYTE depth_bits = 0;
2532 GLXFBConfig* cfgs;
2533 DWORD dwFlags = 0;
2534 int attribs[256];
2535 int nAttribs = 0;
2536 int nCfgs = 0;
2537 int fmt_id;
2539 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2540 if (NULL != pfAttribFList) {
2541 FIXME("unused pfAttribFList\n");
2544 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2545 if (-1 == nAttribs) {
2546 WARN("Cannot convert WGL to GLX attributes\n");
2547 return GL_FALSE;
2549 PUSH1(attribs, None);
2551 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2552 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2553 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2555 for(i=0; piAttribIList[i] != 0; i+=2)
2557 switch(piAttribIList[i])
2559 case WGL_DRAW_TO_BITMAP_ARB:
2560 if(piAttribIList[i+1])
2561 dwFlags |= PFD_DRAW_TO_BITMAP;
2562 break;
2563 case WGL_ACCELERATION_ARB:
2564 switch(piAttribIList[i+1])
2566 case WGL_NO_ACCELERATION_ARB:
2567 dwFlags |= PFD_GENERIC_FORMAT;
2568 break;
2569 case WGL_GENERIC_ACCELERATION_ARB:
2570 dwFlags |= PFD_GENERIC_ACCELERATED;
2571 break;
2572 case WGL_FULL_ACCELERATION_ARB:
2573 /* Nothing to do */
2574 break;
2576 break;
2577 case WGL_SUPPORT_GDI_ARB:
2578 if(piAttribIList[i+1])
2579 dwFlags |= PFD_SUPPORT_GDI;
2580 break;
2581 case WGL_DEPTH_BITS_ARB:
2582 depth_bits = piAttribIList[i+1];
2583 break;
2588 /* Search for FB configurations matching the requirements in attribs */
2589 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2590 if (NULL == cfgs) {
2591 WARN("Compatible Pixel Format not found\n");
2592 return GL_FALSE;
2595 if (!(formats = heap_alloc(nCfgs * sizeof(*formats))))
2597 ERR("No memory.\n");
2598 XFree(cfgs);
2599 return GL_FALSE;
2602 format_count = 0;
2603 for (it = 0; it < nCfgs; ++it)
2605 struct choose_pixel_format_arb_format *format;
2607 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2609 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2610 continue;
2613 for (i = 0; i < nb_pixel_formats; ++i)
2614 if (pixel_formats[i].fmt_id == fmt_id)
2615 break;
2617 if (i == nb_pixel_formats)
2618 continue;
2620 format = &formats[format_count];
2621 format->format = i + 1;
2622 format->original_index = it;
2624 memset(&format->pfd, 0, sizeof(format->pfd));
2625 if (!describe_pixel_format(format->format, &format->pfd, TRUE))
2626 ERR("describe_pixel_format failed, format %d.\n", format->format);
2628 format->depth = format->pfd.cDepthBits;
2629 format->stencil = format->pfd.cStencilBits;
2630 if (!depth_bits && !(format->pfd.dwFlags & PFD_GENERIC_FORMAT))
2632 format->pfd.cDepthBits = 0;
2633 format->pfd.cStencilBits = 0;
2636 ++format_count;
2639 qsort(formats, format_count, sizeof(*formats), compare_formats);
2641 *nNumFormats = min(nMaxFormats, format_count);
2642 for (i = 0; i < *nNumFormats; ++i)
2643 piFormats[i] = formats[i].format;
2645 heap_free(formats);
2646 XFree(cfgs);
2647 return GL_TRUE;
2651 * X11DRV_wglGetPixelFormatAttribivARB
2653 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2655 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2656 UINT nAttributes, const int *piAttributes, int *piValues )
2658 UINT i;
2659 const struct wgl_pixel_format *fmt;
2660 int hTest;
2661 int tmp;
2662 int curGLXAttr = 0;
2664 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2666 if (0 < iLayerPlane) {
2667 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2668 return GL_FALSE;
2671 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2672 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2673 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2674 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2675 if(!fmt) {
2676 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2679 for (i = 0; i < nAttributes; ++i) {
2680 const int curWGLAttr = piAttributes[i];
2681 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2683 switch (curWGLAttr) {
2684 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2685 piValues[i] = nb_pixel_formats;
2686 continue;
2688 case WGL_SUPPORT_OPENGL_ARB:
2689 piValues[i] = GL_TRUE;
2690 continue;
2692 case WGL_ACCELERATION_ARB:
2693 curGLXAttr = GLX_CONFIG_CAVEAT;
2694 if (!fmt) goto pix_error;
2695 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2696 piValues[i] = WGL_NO_ACCELERATION_ARB;
2697 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2698 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2699 else
2700 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2701 continue;
2703 case WGL_TRANSPARENT_ARB:
2704 curGLXAttr = GLX_TRANSPARENT_TYPE;
2705 if (!fmt) goto pix_error;
2706 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2707 if (hTest) goto get_error;
2708 piValues[i] = GL_FALSE;
2709 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2710 continue;
2712 case WGL_PIXEL_TYPE_ARB:
2713 curGLXAttr = GLX_RENDER_TYPE;
2714 if (!fmt) goto pix_error;
2715 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2716 if (hTest) goto get_error;
2717 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2718 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2719 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2720 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2721 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2722 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2723 else {
2724 ERR("unexpected RenderType(%x)\n", tmp);
2725 piValues[i] = WGL_TYPE_RGBA_ARB;
2727 continue;
2729 case WGL_COLOR_BITS_ARB:
2730 curGLXAttr = GLX_BUFFER_SIZE;
2731 break;
2733 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2734 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2735 if (!use_render_texture_emulation) {
2736 piValues[i] = GL_FALSE;
2737 continue;
2739 curGLXAttr = GLX_RENDER_TYPE;
2740 if (!fmt) goto pix_error;
2741 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2742 if (hTest) goto get_error;
2743 if (GLX_COLOR_INDEX_BIT == tmp) {
2744 piValues[i] = GL_FALSE;
2745 continue;
2747 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2748 if (hTest) goto get_error;
2749 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2750 continue;
2752 case WGL_BLUE_BITS_ARB:
2753 curGLXAttr = GLX_BLUE_SIZE;
2754 break;
2755 case WGL_RED_BITS_ARB:
2756 curGLXAttr = GLX_RED_SIZE;
2757 break;
2758 case WGL_GREEN_BITS_ARB:
2759 curGLXAttr = GLX_GREEN_SIZE;
2760 break;
2761 case WGL_ALPHA_BITS_ARB:
2762 curGLXAttr = GLX_ALPHA_SIZE;
2763 break;
2764 case WGL_DEPTH_BITS_ARB:
2765 curGLXAttr = GLX_DEPTH_SIZE;
2766 break;
2767 case WGL_STENCIL_BITS_ARB:
2768 curGLXAttr = GLX_STENCIL_SIZE;
2769 break;
2770 case WGL_DOUBLE_BUFFER_ARB:
2771 curGLXAttr = GLX_DOUBLEBUFFER;
2772 break;
2773 case WGL_STEREO_ARB:
2774 curGLXAttr = GLX_STEREO;
2775 break;
2776 case WGL_AUX_BUFFERS_ARB:
2777 curGLXAttr = GLX_AUX_BUFFERS;
2778 break;
2780 case WGL_SUPPORT_GDI_ARB:
2781 if (!fmt) goto pix_error;
2782 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2783 continue;
2785 case WGL_DRAW_TO_BITMAP_ARB:
2786 if (!fmt) goto pix_error;
2787 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2788 continue;
2790 case WGL_DRAW_TO_WINDOW_ARB:
2791 case WGL_DRAW_TO_PBUFFER_ARB:
2792 if (!fmt) goto pix_error;
2793 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2794 if (hTest) goto get_error;
2795 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2796 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2797 piValues[i] = GL_TRUE;
2798 else
2799 piValues[i] = GL_FALSE;
2800 continue;
2802 case WGL_SWAP_METHOD_ARB:
2803 if (has_swap_method)
2805 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_SWAP_METHOD_OML, &tmp);
2806 if (hTest) goto get_error;
2807 switch (tmp)
2809 case GLX_SWAP_EXCHANGE_OML:
2810 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2811 break;
2812 case GLX_SWAP_COPY_OML:
2813 piValues[i] = WGL_SWAP_COPY_ARB;
2814 break;
2815 case GLX_SWAP_UNDEFINED_OML:
2816 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2817 break;
2818 default:
2819 ERR("Unexpected swap method %x.\n", tmp);
2822 else
2824 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2825 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2827 continue;
2829 case WGL_PBUFFER_LARGEST_ARB:
2830 curGLXAttr = GLX_LARGEST_PBUFFER;
2831 break;
2833 case WGL_SAMPLE_BUFFERS_ARB:
2834 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2835 break;
2837 case WGL_SAMPLES_ARB:
2838 curGLXAttr = GLX_SAMPLES_ARB;
2839 break;
2841 case WGL_FLOAT_COMPONENTS_NV:
2842 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2843 break;
2845 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2846 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2847 break;
2849 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2850 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2851 break;
2853 case WGL_ACCUM_RED_BITS_ARB:
2854 curGLXAttr = GLX_ACCUM_RED_SIZE;
2855 break;
2856 case WGL_ACCUM_GREEN_BITS_ARB:
2857 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2858 break;
2859 case WGL_ACCUM_BLUE_BITS_ARB:
2860 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2861 break;
2862 case WGL_ACCUM_ALPHA_BITS_ARB:
2863 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2864 break;
2865 case WGL_ACCUM_BITS_ARB:
2866 if (!fmt) goto pix_error;
2867 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2868 if (hTest) goto get_error;
2869 piValues[i] = tmp;
2870 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2871 if (hTest) goto get_error;
2872 piValues[i] += tmp;
2873 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2874 if (hTest) goto get_error;
2875 piValues[i] += tmp;
2876 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2877 if (hTest) goto get_error;
2878 piValues[i] += tmp;
2879 continue;
2881 default:
2882 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2885 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2886 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2887 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2889 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2890 * and check which options can work using iPixelFormat=0 and which not.
2891 * A problem would be that this function is an extension. This would
2892 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2894 if (0 != curGLXAttr && iPixelFormat != 0) {
2895 if (!fmt) goto pix_error;
2896 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2897 if (hTest) goto get_error;
2898 curGLXAttr = 0;
2899 } else {
2900 piValues[i] = GL_FALSE;
2903 return GL_TRUE;
2905 get_error:
2906 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2907 return GL_FALSE;
2909 pix_error:
2910 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2911 return GL_FALSE;
2915 * X11DRV_wglGetPixelFormatAttribfvARB
2917 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2919 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2920 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2922 int *attr;
2923 int ret;
2924 UINT i;
2926 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2928 /* Allocate a temporary array to store integer values */
2929 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2930 if (!attr) {
2931 ERR("couldn't allocate %d array\n", nAttributes);
2932 return GL_FALSE;
2935 /* Piggy-back on wglGetPixelFormatAttribivARB */
2936 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2937 if (ret) {
2938 /* Convert integer values to float. Should also check for attributes
2939 that can give decimal values here */
2940 for (i=0; i<nAttributes;i++) {
2941 pfValues[i] = attr[i];
2945 HeapFree(GetProcessHeap(), 0, attr);
2946 return ret;
2950 * X11DRV_wglBindTexImageARB
2952 * WGL_ARB_render_texture: wglBindTexImageARB
2954 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2956 GLboolean ret = GL_FALSE;
2958 TRACE("(%p, %d)\n", object, iBuffer);
2960 if (!object->use_render_texture) {
2961 SetLastError(ERROR_INVALID_HANDLE);
2962 return GL_FALSE;
2965 if (use_render_texture_emulation) {
2966 static BOOL initialized = FALSE;
2967 int prev_binded_texture = 0;
2968 GLXContext prev_context;
2969 GLXDrawable prev_drawable;
2971 prev_context = pglXGetCurrentContext();
2972 prev_drawable = pglXGetCurrentDrawable();
2974 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2975 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2976 isn't ideal performance wise but I wasn't able to get other ways working.
2978 if(!initialized) {
2979 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2980 FIXME("partial stub!\n");
2983 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2984 if (!object->tmp_context || object->prev_context != prev_context) {
2985 if (object->tmp_context)
2986 pglXDestroyContext(gdi_display, object->tmp_context);
2987 object->tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2988 object->prev_context = prev_context;
2991 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2993 /* Switch to our pbuffer */
2994 pglXMakeCurrent(gdi_display, object->drawable, object->tmp_context);
2996 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2997 * After that copy the pbuffer texture data. */
2998 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2999 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3001 /* Switch back to the original drawable and context */
3002 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
3003 return GL_TRUE;
3006 return ret;
3010 * X11DRV_wglReleaseTexImageARB
3012 * WGL_ARB_render_texture: wglReleaseTexImageARB
3014 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
3016 GLboolean ret = GL_FALSE;
3018 TRACE("(%p, %d)\n", object, iBuffer);
3020 if (!object->use_render_texture) {
3021 SetLastError(ERROR_INVALID_HANDLE);
3022 return GL_FALSE;
3024 if (use_render_texture_emulation) {
3025 return GL_TRUE;
3027 return ret;
3031 * X11DRV_wglGetExtensionsStringEXT
3033 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3035 static const char *X11DRV_wglGetExtensionsStringEXT(void)
3037 TRACE("() returning \"%s\"\n", wglExtensions);
3038 return wglExtensions;
3042 * X11DRV_wglGetSwapIntervalEXT
3044 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3046 static int X11DRV_wglGetSwapIntervalEXT(void)
3048 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3049 struct gl_drawable *gl;
3050 int swap_interval;
3052 TRACE("()\n");
3054 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3056 /* This can't happen because a current WGL context is required to get
3057 * here. Likely the application is buggy.
3059 WARN("No GL drawable found, returning swap interval 0\n");
3060 return 0;
3063 swap_interval = gl->swap_interval;
3064 release_gl_drawable(gl);
3066 return swap_interval;
3070 * X11DRV_wglSwapIntervalEXT
3072 * WGL_EXT_swap_control: wglSwapIntervalEXT
3074 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3076 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3077 struct gl_drawable *gl;
3078 BOOL ret;
3080 TRACE("(%d)\n", interval);
3082 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3083 * is invalid.
3085 if (interval < 0 && !has_swap_control_tear)
3087 SetLastError(ERROR_INVALID_DATA);
3088 return FALSE;
3091 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3093 SetLastError(ERROR_DC_NOT_FOUND);
3094 return FALSE;
3097 EnterCriticalSection( &context_section );
3098 ret = set_swap_interval(gl->drawable, interval);
3099 gl->refresh_swap_interval = FALSE;
3100 if (ret)
3101 gl->swap_interval = interval;
3102 else
3103 SetLastError(ERROR_DC_NOT_FOUND);
3105 LeaveCriticalSection( &context_section );
3106 release_gl_drawable(gl);
3108 return ret;
3112 * X11DRV_wglSetPixelFormatWINE
3114 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3115 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3117 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3119 return set_pixel_format(hdc, format, TRUE);
3122 static BOOL X11DRV_wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
3124 return pglXQueryCurrentRendererIntegerMESA( attribute, value );
3127 static const char *X11DRV_wglQueryCurrentRendererStringWINE( GLenum attribute )
3129 return pglXQueryCurrentRendererStringMESA( attribute );
3132 static BOOL X11DRV_wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
3134 return pglXQueryRendererIntegerMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute, value );
3137 static const char *X11DRV_wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
3139 return pglXQueryRendererStringMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute );
3143 * glxRequireVersion (internal)
3145 * Check if the supported GLX version matches requiredVersion.
3147 static BOOL glxRequireVersion(int requiredVersion)
3149 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3150 return (requiredVersion <= glxVersion[1]);
3153 static void register_extension(const char *ext)
3155 if (wglExtensions[0])
3156 strcat(wglExtensions, " ");
3157 strcat(wglExtensions, ext);
3159 TRACE("'%s'\n", ext);
3163 * X11DRV_WineGL_LoadExtensions
3165 static void X11DRV_WineGL_LoadExtensions(void)
3167 wglExtensions[0] = 0;
3169 /* ARB Extensions */
3171 if (has_extension( glxExtensions, "GLX_ARB_create_context"))
3173 register_extension( "WGL_ARB_create_context" );
3174 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3176 if (has_extension( glxExtensions, "GLX_ARB_create_context_no_error" ))
3177 register_extension( "WGL_ARB_create_context_no_error" );
3178 if (has_extension( glxExtensions, "GLX_ARB_create_context_profile"))
3179 register_extension("WGL_ARB_create_context_profile");
3182 if (has_extension( glxExtensions, "GLX_ARB_fbconfig_float"))
3184 register_extension("WGL_ARB_pixel_format_float");
3185 register_extension("WGL_ATI_pixel_format_float");
3188 register_extension( "WGL_ARB_extensions_string" );
3189 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3191 if (glxRequireVersion(3))
3193 register_extension( "WGL_ARB_make_current_read" );
3194 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3195 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3198 if (has_extension( glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3200 if (glxRequireVersion(3))
3202 register_extension( "WGL_ARB_pbuffer" );
3203 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3204 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3205 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3206 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3207 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3208 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3211 register_extension( "WGL_ARB_pixel_format" );
3212 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3213 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3214 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3216 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3217 if (has_extension( glxExtensions, "GLX_ARB_render_texture") ||
3218 (glxRequireVersion(3) && use_render_texture_emulation))
3220 register_extension( "WGL_ARB_render_texture" );
3221 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3222 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3224 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3225 if (has_extension( glxExtensions, "GLX_NV_float_buffer"))
3226 register_extension("WGL_NV_float_buffer");
3228 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3229 if (has_extension(glExtensions, "GL_NV_texture_rectangle"))
3230 register_extension("WGL_NV_render_texture_rectangle");
3233 /* EXT Extensions */
3235 register_extension( "WGL_EXT_extensions_string" );
3236 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3238 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3239 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3240 register_extension( "WGL_EXT_swap_control" );
3241 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3242 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3244 if (has_extension( glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3245 register_extension("WGL_EXT_framebuffer_sRGB");
3247 if (has_extension( glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3248 register_extension("WGL_EXT_pixel_format_packed_float");
3250 if (has_extension( glxExtensions, "GLX_EXT_swap_control"))
3252 swap_control_method = GLX_SWAP_CONTROL_EXT;
3253 if (has_extension( glxExtensions, "GLX_EXT_swap_control_tear"))
3255 register_extension("WGL_EXT_swap_control_tear");
3256 has_swap_control_tear = TRUE;
3259 else if (has_extension( glxExtensions, "GLX_MESA_swap_control"))
3261 swap_control_method = GLX_SWAP_CONTROL_MESA;
3263 else if (has_extension( glxExtensions, "GLX_SGI_swap_control"))
3265 swap_control_method = GLX_SWAP_CONTROL_SGI;
3268 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3269 if (has_extension(glExtensions, "GL_NV_vertex_array_range"))
3271 register_extension( "WGL_NV_vertex_array_range" );
3272 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3273 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3276 if (has_extension(glxExtensions, "GLX_OML_swap_method"))
3277 has_swap_method = TRUE;
3279 /* WINE-specific WGL Extensions */
3281 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3282 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3284 register_extension( "WGL_WINE_pixel_format_passthrough" );
3285 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3287 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
3289 register_extension( "WGL_WINE_query_renderer" );
3290 opengl_funcs.ext.p_wglQueryCurrentRendererIntegerWINE = X11DRV_wglQueryCurrentRendererIntegerWINE;
3291 opengl_funcs.ext.p_wglQueryCurrentRendererStringWINE = X11DRV_wglQueryCurrentRendererStringWINE;
3292 opengl_funcs.ext.p_wglQueryRendererIntegerWINE = X11DRV_wglQueryRendererIntegerWINE;
3293 opengl_funcs.ext.p_wglQueryRendererStringWINE = X11DRV_wglQueryRendererStringWINE;
3299 * glxdrv_SwapBuffers
3301 * Swap the buffers of this DC
3303 static BOOL WINAPI glxdrv_wglSwapBuffers( HDC hdc )
3305 struct x11drv_escape_flush_gl_drawable escape;
3306 struct gl_drawable *gl;
3307 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3308 INT64 ust, msc, sbc, target_sbc = 0;
3310 TRACE("(%p)\n", hdc);
3312 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3313 escape.gl_drawable = 0;
3314 escape.flush = !pglXWaitForSbcOML;
3316 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3318 SetLastError( ERROR_INVALID_HANDLE );
3319 return FALSE;
3322 EnterCriticalSection( &context_section );
3323 if (gl->refresh_swap_interval)
3325 set_swap_interval(gl->drawable, gl->swap_interval);
3326 gl->refresh_swap_interval = FALSE;
3328 LeaveCriticalSection( &context_section );
3330 switch (gl->type)
3332 case DC_GL_PIXMAP_WIN:
3333 if (ctx) sync_context( ctx );
3334 escape.gl_drawable = gl->pixmap;
3335 if (pglXCopySubBufferMESA) {
3336 /* (glX)SwapBuffers has an implicit glFlush effect, however
3337 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3338 * copying */
3339 pglFlush();
3340 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3341 gl->pixmap_size.cx, gl->pixmap_size.cy );
3342 break;
3344 if (pglXSwapBuffersMscOML)
3346 pglFlush();
3347 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3348 break;
3350 pglXSwapBuffers(gdi_display, gl->drawable);
3351 break;
3352 case DC_GL_WINDOW:
3353 case DC_GL_CHILD_WIN:
3354 if (ctx) sync_context( ctx );
3355 if (gl->type == DC_GL_CHILD_WIN) escape.gl_drawable = gl->window;
3356 /* fall through */
3357 default:
3358 if (escape.gl_drawable && pglXSwapBuffersMscOML)
3360 pglFlush();
3361 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3362 break;
3364 pglXSwapBuffers(gdi_display, gl->drawable);
3365 break;
3368 if (escape.gl_drawable && pglXWaitForSbcOML)
3369 pglXWaitForSbcOML( gdi_display, gl->drawable, target_sbc, &ust, &msc, &sbc );
3371 release_gl_drawable( gl );
3373 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3374 return TRUE;
3377 static struct opengl_funcs opengl_funcs =
3380 glxdrv_wglCopyContext, /* p_wglCopyContext */
3381 glxdrv_wglCreateContext, /* p_wglCreateContext */
3382 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3383 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3384 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3385 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3386 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3387 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3388 glxdrv_wglShareLists, /* p_wglShareLists */
3389 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3393 struct opengl_funcs *get_glx_driver( UINT version )
3395 if (version != WINE_WGL_DRIVER_VERSION)
3397 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3398 return NULL;
3400 if (has_opengl()) return &opengl_funcs;
3401 return NULL;
3404 #else /* no OpenGL includes */
3406 struct opengl_funcs *get_glx_driver( UINT version )
3408 return NULL;
3411 void sync_gl_drawable( HWND hwnd, BOOL known_child )
3415 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3419 void destroy_gl_drawable( HWND hwnd )
3423 #endif /* defined(SONAME_LIBGL) */