widl: Generate helper macros for WinRT implementation.
[wine/zf.git] / dlls / winex11.drv / opengl.c
blob4a3ab31273a377dc131587228c4e27fa871eaa84
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/debug.h"
45 #ifdef SONAME_LIBGL
47 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
48 WINE_DECLARE_DEBUG_CHANNEL(winediag);
50 #include "wine/wgl.h"
51 #include "wine/wgl_driver.h"
53 typedef struct __GLXcontextRec *GLXContext;
54 typedef struct __GLXFBConfigRec *GLXFBConfig;
55 typedef XID GLXPixmap;
56 typedef XID GLXDrawable;
57 typedef XID GLXFBConfigID;
58 typedef XID GLXContextID;
59 typedef XID GLXWindow;
60 typedef XID GLXPbuffer;
62 #define GLX_USE_GL 1
63 #define GLX_BUFFER_SIZE 2
64 #define GLX_LEVEL 3
65 #define GLX_RGBA 4
66 #define GLX_DOUBLEBUFFER 5
67 #define GLX_STEREO 6
68 #define GLX_AUX_BUFFERS 7
69 #define GLX_RED_SIZE 8
70 #define GLX_GREEN_SIZE 9
71 #define GLX_BLUE_SIZE 10
72 #define GLX_ALPHA_SIZE 11
73 #define GLX_DEPTH_SIZE 12
74 #define GLX_STENCIL_SIZE 13
75 #define GLX_ACCUM_RED_SIZE 14
76 #define GLX_ACCUM_GREEN_SIZE 15
77 #define GLX_ACCUM_BLUE_SIZE 16
78 #define GLX_ACCUM_ALPHA_SIZE 17
80 #define GLX_BAD_SCREEN 1
81 #define GLX_BAD_ATTRIBUTE 2
82 #define GLX_NO_EXTENSION 3
83 #define GLX_BAD_VISUAL 4
84 #define GLX_BAD_CONTEXT 5
85 #define GLX_BAD_VALUE 6
86 #define GLX_BAD_ENUM 7
88 #define GLX_VENDOR 1
89 #define GLX_VERSION 2
90 #define GLX_EXTENSIONS 3
92 #define GLX_CONFIG_CAVEAT 0x20
93 #define GLX_DONT_CARE 0xFFFFFFFF
94 #define GLX_X_VISUAL_TYPE 0x22
95 #define GLX_TRANSPARENT_TYPE 0x23
96 #define GLX_TRANSPARENT_INDEX_VALUE 0x24
97 #define GLX_TRANSPARENT_RED_VALUE 0x25
98 #define GLX_TRANSPARENT_GREEN_VALUE 0x26
99 #define GLX_TRANSPARENT_BLUE_VALUE 0x27
100 #define GLX_TRANSPARENT_ALPHA_VALUE 0x28
101 #define GLX_WINDOW_BIT 0x00000001
102 #define GLX_PIXMAP_BIT 0x00000002
103 #define GLX_PBUFFER_BIT 0x00000004
104 #define GLX_AUX_BUFFERS_BIT 0x00000010
105 #define GLX_FRONT_LEFT_BUFFER_BIT 0x00000001
106 #define GLX_FRONT_RIGHT_BUFFER_BIT 0x00000002
107 #define GLX_BACK_LEFT_BUFFER_BIT 0x00000004
108 #define GLX_BACK_RIGHT_BUFFER_BIT 0x00000008
109 #define GLX_DEPTH_BUFFER_BIT 0x00000020
110 #define GLX_STENCIL_BUFFER_BIT 0x00000040
111 #define GLX_ACCUM_BUFFER_BIT 0x00000080
112 #define GLX_NONE 0x8000
113 #define GLX_SLOW_CONFIG 0x8001
114 #define GLX_TRUE_COLOR 0x8002
115 #define GLX_DIRECT_COLOR 0x8003
116 #define GLX_PSEUDO_COLOR 0x8004
117 #define GLX_STATIC_COLOR 0x8005
118 #define GLX_GRAY_SCALE 0x8006
119 #define GLX_STATIC_GRAY 0x8007
120 #define GLX_TRANSPARENT_RGB 0x8008
121 #define GLX_TRANSPARENT_INDEX 0x8009
122 #define GLX_VISUAL_ID 0x800B
123 #define GLX_SCREEN 0x800C
124 #define GLX_NON_CONFORMANT_CONFIG 0x800D
125 #define GLX_DRAWABLE_TYPE 0x8010
126 #define GLX_RENDER_TYPE 0x8011
127 #define GLX_X_RENDERABLE 0x8012
128 #define GLX_FBCONFIG_ID 0x8013
129 #define GLX_RGBA_TYPE 0x8014
130 #define GLX_COLOR_INDEX_TYPE 0x8015
131 #define GLX_MAX_PBUFFER_WIDTH 0x8016
132 #define GLX_MAX_PBUFFER_HEIGHT 0x8017
133 #define GLX_MAX_PBUFFER_PIXELS 0x8018
134 #define GLX_PRESERVED_CONTENTS 0x801B
135 #define GLX_LARGEST_PBUFFER 0x801C
136 #define GLX_WIDTH 0x801D
137 #define GLX_HEIGHT 0x801E
138 #define GLX_EVENT_MASK 0x801F
139 #define GLX_DAMAGED 0x8020
140 #define GLX_SAVED 0x8021
141 #define GLX_WINDOW 0x8022
142 #define GLX_PBUFFER 0x8023
143 #define GLX_PBUFFER_HEIGHT 0x8040
144 #define GLX_PBUFFER_WIDTH 0x8041
145 #define GLX_SWAP_METHOD_OML 0x8060
146 #define GLX_SWAP_EXCHANGE_OML 0x8061
147 #define GLX_SWAP_COPY_OML 0x8062
148 #define GLX_SWAP_UNDEFINED_OML 0x8063
149 #define GLX_RGBA_BIT 0x00000001
150 #define GLX_COLOR_INDEX_BIT 0x00000002
151 #define GLX_PBUFFER_CLOBBER_MASK 0x08000000
153 /** GLX_ARB_multisample */
154 #define GLX_SAMPLE_BUFFERS_ARB 100000
155 #define GLX_SAMPLES_ARB 100001
156 /** GLX_ARB_framebuffer_sRGB */
157 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
158 /** GLX_EXT_fbconfig_packed_float */
159 #define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
160 #define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT 0x00000008
161 /** GLX_ARB_create_context */
162 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
163 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
164 #define GLX_CONTEXT_FLAGS_ARB 0x2094
165 /** GLX_ARB_create_context_no_error */
166 #define GLX_CONTEXT_OPENGL_NO_ERROR_ARB 0x31B3
167 /** GLX_ARB_create_context_profile */
168 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
169 /** GLX_ATI_pixel_format_float */
170 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
171 /** GLX_ARB_pixel_format_float */
172 #define GLX_RGBA_FLOAT_BIT 0x00000004
173 #define GLX_RGBA_FLOAT_TYPE 0x20B9
174 /** GLX_MESA_query_renderer */
175 #define GLX_RENDERER_ID_MESA 0x818E
176 /** GLX_NV_float_buffer */
177 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
180 static char *glExtensions;
181 static const char *glxExtensions;
182 static char wglExtensions[4096];
183 static int glxVersion[2];
184 static int glx_opcode;
186 struct wgl_pixel_format
188 GLXFBConfig fbconfig;
189 XVisualInfo *visual;
190 int fmt_id;
191 int render_type;
192 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
195 struct wgl_context
197 HDC hdc;
198 BOOL has_been_current;
199 BOOL sharing;
200 BOOL gl3_context;
201 const struct wgl_pixel_format *fmt;
202 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
203 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
204 GLXContext ctx;
205 struct gl_drawable *drawables[2];
206 struct gl_drawable *new_drawables[2];
207 BOOL refresh_drawables;
208 struct list entry;
211 struct wgl_pbuffer
213 Drawable drawable;
214 const struct wgl_pixel_format* fmt;
215 int width;
216 int height;
217 int* attribList;
218 int use_render_texture; /* This is also the internal texture format */
219 int texture_bind_target;
220 int texture_bpp;
221 GLint texture_format;
222 GLuint texture_target;
223 GLenum texture_type;
224 GLuint texture;
225 int texture_level;
226 GLXContext tmp_context;
227 GLXContext prev_context;
228 struct list entry;
231 enum dc_gl_type
233 DC_GL_NONE, /* no GL support (pixel format not set yet) */
234 DC_GL_WINDOW, /* normal top-level window */
235 DC_GL_CHILD_WIN, /* child window using XComposite */
236 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
237 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
240 struct gl_drawable
242 LONG ref; /* reference count */
243 enum dc_gl_type type; /* type of GL surface */
244 GLXDrawable drawable; /* drawable for rendering with GL */
245 Window window; /* window if drawable is a GLXWindow */
246 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
247 const struct wgl_pixel_format *format; /* pixel format for the drawable */
248 SIZE pixmap_size; /* pixmap size for GLXPixmap drawables */
249 int swap_interval;
250 BOOL refresh_swap_interval;
253 enum glx_swap_control_method
255 GLX_SWAP_CONTROL_NONE,
256 GLX_SWAP_CONTROL_EXT,
257 GLX_SWAP_CONTROL_SGI,
258 GLX_SWAP_CONTROL_MESA
261 /* X context to associate a struct gl_drawable to an hwnd */
262 static XContext gl_hwnd_context;
263 /* X context to associate a struct gl_drawable to a pbuffer hdc */
264 static XContext gl_pbuffer_context;
266 static struct list context_list = LIST_INIT( context_list );
267 static struct list pbuffer_list = LIST_INIT( pbuffer_list );
268 static struct wgl_pixel_format *pixel_formats;
269 static int nb_pixel_formats, nb_onscreen_formats;
270 static BOOL use_render_texture_emulation = TRUE;
272 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
273 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
274 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
275 static BOOL has_swap_control_tear = FALSE;
276 static BOOL has_swap_method = FALSE;
278 static CRITICAL_SECTION context_section;
279 static CRITICAL_SECTION_DEBUG critsect_debug =
281 0, 0, &context_section,
282 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
283 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
285 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
287 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
289 static struct opengl_funcs opengl_funcs;
291 #define USE_GL_FUNC(name) #name,
292 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
293 #undef USE_GL_FUNC
295 static void X11DRV_WineGL_LoadExtensions(void);
296 static void init_pixel_formats( Display *display );
297 static BOOL glxRequireVersion(int requiredVersion);
299 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
300 TRACE( "size %u version %u flags %u type %u color %u %u,%u,%u,%u "
301 "accum %u depth %u stencil %u aux %u ",
302 ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
303 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
304 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
305 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
306 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
307 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
308 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
309 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
310 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
311 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
312 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
313 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
315 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
316 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
318 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
322 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
323 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
324 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
325 #undef TEST_AND_DUMP
326 TRACE("\n");
329 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
330 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
332 /* GLX 1.0 */
333 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
334 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
335 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
336 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
337 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
338 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
339 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
340 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
341 static GLXContext (*pglXGetCurrentContext)( void );
342 static GLXDrawable (*pglXGetCurrentDrawable)( void );
344 /* GLX 1.1 */
345 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
346 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
347 static const char *(*pglXGetClientString)( Display *dpy, int name );
349 /* GLX 1.3 */
350 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
351 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
352 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
353 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
354 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
355 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
356 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
357 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
358 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
359 static GLXPixmap (*pglXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list );
360 static void (*pglXDestroyPixmap)( Display *dpy, GLXPixmap pixmap );
361 static GLXWindow (*pglXCreateWindow)( Display *dpy, GLXFBConfig config, Window win, const int *attrib_list );
362 static void (*pglXDestroyWindow)( Display *dpy, GLXWindow win );
364 /* GLX Extensions */
365 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
366 static void* (*pglXGetProcAddressARB)(const GLubyte *);
367 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
368 static int (*pglXSwapIntervalSGI)(int);
370 /* NV GLX Extension */
371 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
372 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
374 /* MESA GLX Extensions */
375 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
376 static int (*pglXSwapIntervalMESA)(unsigned int interval);
377 static Bool (*pglXQueryCurrentRendererIntegerMESA)(int attribute, unsigned int *value);
378 static const char *(*pglXQueryCurrentRendererStringMESA)(int attribute);
379 static Bool (*pglXQueryRendererIntegerMESA)(Display *dpy, int screen, int renderer, int attribute, unsigned int *value);
380 static const char *(*pglXQueryRendererStringMESA)(Display *dpy, int screen, int renderer, int attribute);
382 /* OpenML GLX Extensions */
383 static Bool (*pglXWaitForSbcOML)( Display *dpy, GLXDrawable drawable,
384 INT64 target_sbc, INT64 *ust, INT64 *msc, INT64 *sbc );
385 static INT64 (*pglXSwapBuffersMscOML)( Display *dpy, GLXDrawable drawable,
386 INT64 target_msc, INT64 divisor, INT64 remainder );
388 /* Standard OpenGL */
389 static void (*pglFinish)(void);
390 static void (*pglFlush)(void);
391 static const GLubyte *(*pglGetString)(GLenum name);
393 static void wglFinish(void);
394 static void wglFlush(void);
395 static const GLubyte *wglGetString(GLenum name);
397 /* check if the extension is present in the list */
398 static BOOL has_extension( const char *list, const char *ext )
400 size_t len = strlen( ext );
402 while (list)
404 while (*list == ' ') list++;
405 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
406 list = strchr( list, ' ' );
408 return FALSE;
411 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
413 /* In the future we might want to find the exact X or GLX error to report back to the app */
414 if (event->request_code != glx_opcode)
415 return 0;
416 return 1;
419 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
421 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
423 int screen = DefaultScreen(gdi_display);
424 Window win = 0, root = 0;
425 const char *gl_version;
426 const char *gl_renderer;
427 const char* str;
428 BOOL glx_direct;
429 XVisualInfo *vis;
430 GLXContext ctx = NULL;
431 XSetWindowAttributes attr;
432 BOOL ret = FALSE;
433 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
435 attr.override_redirect = True;
436 attr.colormap = None;
437 attr.border_pixel = 0;
439 vis = pglXChooseVisual(gdi_display, screen, attribList);
440 if (vis) {
441 #ifdef __i386__
442 WORD old_fs, new_fs;
443 __asm__( "mov %%fs,%0" : "=r" (old_fs) );
444 /* Create a GLX Context. Without one we can't query GL information */
445 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
446 __asm__( "mov %%fs,%0" : "=r" (new_fs) );
447 __asm__( "mov %0,%%fs" :: "r" (old_fs) );
448 if (old_fs != new_fs)
450 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
451 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
452 goto done;
454 #else
455 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
456 #endif
458 if (!ctx) goto done;
460 root = RootWindow( gdi_display, vis->screen );
461 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
462 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
463 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
464 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
465 XMapWindow( gdi_display, win );
466 else
467 win = root;
469 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
471 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
472 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
473 goto done;
475 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
476 gl_version = (const char *)opengl_funcs.gl.p_glGetString(GL_VERSION);
477 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
478 glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
479 strcpy(glExtensions, str);
480 strcat(glExtensions, legacy_extensions);
482 /* Get the common GLX version supported by GLX client and server ( major/minor) */
483 pglXQueryVersion(gdi_display, &glxVersion[0], &glxVersion[1]);
485 glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
486 glx_direct = pglXIsDirect(gdi_display, ctx);
488 TRACE("GL version : %s.\n", gl_version);
489 TRACE("GL renderer : %s.\n", gl_renderer);
490 TRACE("GLX version : %d.%d.\n", glxVersion[0], glxVersion[1]);
491 TRACE("Server GLX version : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VERSION));
492 TRACE("Server GLX vendor: : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VENDOR));
493 TRACE("Client GLX version : %s.\n", pglXGetClientString(gdi_display, GLX_VERSION));
494 TRACE("Client GLX vendor: : %s.\n", pglXGetClientString(gdi_display, GLX_VENDOR));
495 TRACE("Direct rendering enabled: %s\n", glx_direct ? "True" : "False");
497 if(!glx_direct)
499 int fd = ConnectionNumber(gdi_display);
500 struct sockaddr_un uaddr;
501 unsigned int uaddrlen = sizeof(struct sockaddr_un);
503 /* In general indirect rendering on a local X11 server indicates a driver problem.
504 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
506 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
507 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
508 "haven't been installed correctly (using GL renderer %s, version %s).\n",
509 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
510 debugstr_a(gl_version));
512 else
514 /* In general you would expect that if direct rendering is returned, that you receive hardware
515 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
516 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
517 * software rendering, it shows direct rendering.
519 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
520 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
521 * it shows 'Mesa X11'.
523 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
524 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
525 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
526 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
527 debugstr_a(gl_version));
529 ret = TRUE;
531 done:
532 if(vis) XFree(vis);
533 if(ctx) {
534 pglXMakeCurrent(gdi_display, None, NULL);
535 pglXDestroyContext(gdi_display, ctx);
537 if (win != root) XDestroyWindow( gdi_display, win );
538 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
539 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
540 return ret;
543 static void *opengl_handle;
545 static BOOL WINAPI init_opengl( INIT_ONCE *once, void *param, void **context )
547 int error_base, event_base;
548 unsigned int i;
550 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
551 and include all dependencies */
552 opengl_handle = dlopen( SONAME_LIBGL, RTLD_NOW | RTLD_GLOBAL );
553 if (opengl_handle == NULL)
555 ERR( "Failed to load libGL: %s\n", dlerror() );
556 ERR( "OpenGL support is disabled.\n");
557 return TRUE;
560 for (i = 0; i < ARRAY_SIZE( opengl_func_names ); i++)
562 if (!(((void **)&opengl_funcs.gl)[i] = dlsym( opengl_handle, opengl_func_names[i] )))
564 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
565 goto failed;
569 /* redirect some standard OpenGL functions */
570 #define REDIRECT(func) \
571 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
572 REDIRECT( glFinish );
573 REDIRECT( glFlush );
574 REDIRECT( glGetString );
575 #undef REDIRECT
577 pglXGetProcAddressARB = dlsym(opengl_handle, "glXGetProcAddressARB");
578 if (pglXGetProcAddressARB == NULL) {
579 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
580 goto failed;
583 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
585 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
586 goto failed; \
587 } while(0)
589 /* GLX 1.0 */
590 LOAD_FUNCPTR(glXChooseVisual);
591 LOAD_FUNCPTR(glXCopyContext);
592 LOAD_FUNCPTR(glXCreateContext);
593 LOAD_FUNCPTR(glXGetCurrentContext);
594 LOAD_FUNCPTR(glXGetCurrentDrawable);
595 LOAD_FUNCPTR(glXDestroyContext);
596 LOAD_FUNCPTR(glXIsDirect);
597 LOAD_FUNCPTR(glXMakeCurrent);
598 LOAD_FUNCPTR(glXSwapBuffers);
599 LOAD_FUNCPTR(glXQueryVersion);
601 /* GLX 1.1 */
602 LOAD_FUNCPTR(glXGetClientString);
603 LOAD_FUNCPTR(glXQueryExtensionsString);
604 LOAD_FUNCPTR(glXQueryServerString);
606 /* GLX 1.3 */
607 LOAD_FUNCPTR(glXCreatePbuffer);
608 LOAD_FUNCPTR(glXCreateNewContext);
609 LOAD_FUNCPTR(glXDestroyPbuffer);
610 LOAD_FUNCPTR(glXMakeContextCurrent);
611 LOAD_FUNCPTR(glXGetFBConfigs);
612 LOAD_FUNCPTR(glXCreatePixmap);
613 LOAD_FUNCPTR(glXDestroyPixmap);
614 LOAD_FUNCPTR(glXCreateWindow);
615 LOAD_FUNCPTR(glXDestroyWindow);
616 #undef LOAD_FUNCPTR
618 /* It doesn't matter if these fail. They'll only be used if the driver reports
619 the associated extension is available (and if a driver reports the extension
620 is available but fails to provide the functions, it's quite broken) */
621 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
622 /* ARB GLX Extension */
623 LOAD_FUNCPTR(glXCreateContextAttribsARB);
624 /* EXT GLX Extension */
625 LOAD_FUNCPTR(glXSwapIntervalEXT);
626 /* MESA GLX Extension */
627 LOAD_FUNCPTR(glXSwapIntervalMESA);
628 /* SGI GLX Extension */
629 LOAD_FUNCPTR(glXSwapIntervalSGI);
630 /* NV GLX Extension */
631 LOAD_FUNCPTR(glXAllocateMemoryNV);
632 LOAD_FUNCPTR(glXFreeMemoryNV);
633 #undef LOAD_FUNCPTR
635 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
637 if (XQueryExtension( gdi_display, "GLX", &glx_opcode, &event_base, &error_base ))
639 TRACE("GLX is up and running error_base = %d\n", error_base);
640 } else {
641 ERR( "GLX extension is missing, disabling OpenGL.\n" );
642 goto failed;
644 gl_hwnd_context = XUniqueContext();
645 gl_pbuffer_context = XUniqueContext();
647 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
648 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
649 * rendering is used.
651 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
652 * depends on the reported GLX client / server version and on the client / server extension list.
653 * Those don't have to be the same.
655 * In general the server GLX information lists the capabilities in case of indirect rendering.
656 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
657 * available and in that case the client GLX informat can be used.
658 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
659 * in the GLX version/extension list. When a program does this it works for certain for both
660 * direct and indirect rendering.
662 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
663 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
664 * extension list which causes this extension not to be listed. (Wine requires this extension).
665 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
666 * pbuffers is around.
668 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
669 * the ATI drivers and from then on use GLX client information for them.
672 if(glxRequireVersion(3)) {
673 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
674 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
675 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
676 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
677 } else if (has_extension( glxExtensions, "GLX_SGIX_fbconfig")) {
678 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
679 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
680 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
682 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
683 * enable this function when the Xserver understand GLX 1.3 or newer
685 pglXQueryDrawable = NULL;
686 } else if(strcmp("ATI", pglXGetClientString(gdi_display, GLX_VENDOR)) == 0) {
687 TRACE("Overriding ATI GLX capabilities!\n");
688 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
689 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
690 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
691 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
693 /* Use client GLX information in case of the ATI drivers. We override the
694 * capabilities over here and not somewhere else as ATI might better their
695 * life in the future. In case they release proper drivers this block of
696 * code won't be called. */
697 glxExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
698 } else {
699 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n",
700 pglXQueryServerString(gdi_display, DefaultScreen(gdi_display), GLX_VERSION));
703 if (has_extension( glxExtensions, "GLX_MESA_copy_sub_buffer")) {
704 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
707 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
709 pglXQueryCurrentRendererIntegerMESA = pglXGetProcAddressARB(
710 (const GLubyte *)"glXQueryCurrentRendererIntegerMESA" );
711 pglXQueryCurrentRendererStringMESA = pglXGetProcAddressARB(
712 (const GLubyte *)"glXQueryCurrentRendererStringMESA" );
713 pglXQueryRendererIntegerMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererIntegerMESA" );
714 pglXQueryRendererStringMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererStringMESA" );
717 if (has_extension( glxExtensions, "GLX_OML_sync_control" ))
719 pglXWaitForSbcOML = pglXGetProcAddressARB( (const GLubyte *)"glXWaitForSbcOML" );
720 pglXSwapBuffersMscOML = pglXGetProcAddressARB( (const GLubyte *)"glXSwapBuffersMscOML" );
723 X11DRV_WineGL_LoadExtensions();
724 init_pixel_formats( gdi_display );
725 return TRUE;
727 failed:
728 dlclose(opengl_handle);
729 opengl_handle = NULL;
730 return TRUE;
733 static BOOL has_opengl(void)
735 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
736 InitOnceExecuteOnce(&init_once, init_opengl, NULL, NULL);
737 return opengl_handle != NULL;
740 static const char *debugstr_fbconfig( GLXFBConfig fbconfig )
742 int id, visual, drawable;
744 if (pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_FBCONFIG_ID, &id ))
745 return "*** invalid fbconfig";
746 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_VISUAL_ID, &visual );
747 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &drawable );
748 return wine_dbg_sprintf( "fbconfig %#x visual id %#x drawable type %#x", id, visual, drawable );
751 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
752 int nAttribs = 0;
753 unsigned cur = 0;
754 int attr, pop;
755 int drawattrib = 0;
756 int nvfloatattrib = GLX_DONT_CARE;
757 int pixelattrib = GLX_DONT_CARE;
759 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
760 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
761 while (iWGLAttr && 0 != iWGLAttr[cur]) {
762 attr = iWGLAttr[cur];
763 TRACE("pAttr[%d] = %x\n", cur, attr);
764 pop = iWGLAttr[++cur];
766 switch (attr) {
767 case WGL_AUX_BUFFERS_ARB:
768 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
769 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
770 break;
771 case WGL_COLOR_BITS_ARB:
772 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
773 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
774 break;
775 case WGL_BLUE_BITS_ARB:
776 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
777 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
778 break;
779 case WGL_RED_BITS_ARB:
780 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
781 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
782 break;
783 case WGL_GREEN_BITS_ARB:
784 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
785 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
786 break;
787 case WGL_ALPHA_BITS_ARB:
788 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
789 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
790 break;
791 case WGL_DEPTH_BITS_ARB:
792 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
793 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
794 break;
795 case WGL_STENCIL_BITS_ARB:
796 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
797 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
798 break;
799 case WGL_DOUBLE_BUFFER_ARB:
800 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
801 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
802 break;
803 case WGL_STEREO_ARB:
804 PUSH2(oGLXAttr, GLX_STEREO, pop);
805 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
806 break;
808 case WGL_PIXEL_TYPE_ARB:
809 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
810 switch (pop) {
811 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
812 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
813 /* 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 */
814 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
815 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
816 default:
817 ERR("unexpected PixelType(%x)\n", pop);
819 break;
821 case WGL_SUPPORT_GDI_ARB:
822 /* This flag is set in a pixel format */
823 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
824 break;
826 case WGL_DRAW_TO_BITMAP_ARB:
827 /* This flag is set in a pixel format */
828 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
829 break;
831 case WGL_DRAW_TO_WINDOW_ARB:
832 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
833 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
834 if (pop) {
835 drawattrib |= GLX_WINDOW_BIT;
837 break;
839 case WGL_DRAW_TO_PBUFFER_ARB:
840 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
841 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
842 if (pop) {
843 drawattrib |= GLX_PBUFFER_BIT;
845 break;
847 case WGL_ACCELERATION_ARB:
848 /* This flag is set in a pixel format */
849 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
850 break;
852 case WGL_SUPPORT_OPENGL_ARB:
853 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
854 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
855 break;
857 case WGL_SWAP_METHOD_ARB:
858 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
859 if (has_swap_method)
861 switch (pop)
863 case WGL_SWAP_EXCHANGE_ARB:
864 pop = GLX_SWAP_EXCHANGE_OML;
865 break;
866 case WGL_SWAP_COPY_ARB:
867 pop = GLX_SWAP_COPY_OML;
868 break;
869 case WGL_SWAP_UNDEFINED_ARB:
870 pop = GLX_SWAP_UNDEFINED_OML;
871 break;
872 default:
873 ERR("Unexpected swap method %#x.\n", pop);
874 pop = GLX_DONT_CARE;
876 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
878 else
880 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
882 break;
884 case WGL_PBUFFER_LARGEST_ARB:
885 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
886 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
887 break;
889 case WGL_SAMPLE_BUFFERS_ARB:
890 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
891 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
892 break;
894 case WGL_SAMPLES_ARB:
895 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
896 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
897 break;
899 case WGL_TEXTURE_FORMAT_ARB:
900 case WGL_TEXTURE_TARGET_ARB:
901 case WGL_MIPMAP_TEXTURE_ARB:
902 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur - 1], iWGLAttr[cur]);
903 if (NULL == pbuf) {
904 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
906 if (!use_render_texture_emulation) {
907 if (WGL_NO_TEXTURE_ARB != pop) {
908 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
909 return -1; /** error: don't support it */
910 } else {
911 drawattrib |= GLX_PBUFFER_BIT;
914 break ;
915 case WGL_FLOAT_COMPONENTS_NV:
916 nvfloatattrib = pop;
917 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
918 break ;
919 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
920 case WGL_BIND_TO_TEXTURE_RGB_ARB:
921 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
922 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
923 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
924 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
925 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
926 /** cannot be converted, see direct handling on
927 * - wglGetPixelFormatAttribivARB
928 * TODO: wglChoosePixelFormat
930 break ;
931 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
932 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
933 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
934 break ;
936 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
937 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
938 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
939 break ;
940 default:
941 FIXME("unsupported %x WGL Attribute\n", attr);
942 break;
944 ++cur;
947 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
948 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
949 * pixmap and pbuffer formats appear as well. */
950 if (!drawattrib) drawattrib = GLX_DONT_CARE;
951 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
952 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
954 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
955 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
956 * GLX_DONT_CARE unless it is overridden. */
957 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
958 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
960 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
961 if (has_extension(glxExtensions, "GLX_NV_float_buffer")) {
962 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
963 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
966 return nAttribs;
969 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
971 int render_type, render_type_bit;
972 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
973 switch(render_type_bit)
975 case GLX_RGBA_BIT:
976 render_type = GLX_RGBA_TYPE;
977 break;
978 case GLX_COLOR_INDEX_BIT:
979 render_type = GLX_COLOR_INDEX_TYPE;
980 break;
981 case GLX_RGBA_FLOAT_BIT:
982 render_type = GLX_RGBA_FLOAT_TYPE;
983 break;
984 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
985 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
986 break;
987 default:
988 ERR("Unknown render_type: %x\n", render_type_bit);
989 render_type = 0;
991 return render_type;
994 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
995 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
997 int dbuf, value;
998 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
999 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1001 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1002 * the GLX_PIXMAP_BIT set. */
1003 return !dbuf && (value & GLX_PIXMAP_BIT);
1006 static void init_pixel_formats( Display *display )
1008 struct wgl_pixel_format *list;
1009 int size = 0, onscreen_size = 0;
1010 int fmt_id, nCfgs, i, run, bmp_formats;
1011 GLXFBConfig* cfgs;
1012 XVisualInfo *visinfo;
1014 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1015 if (NULL == cfgs || 0 == nCfgs) {
1016 if(cfgs != NULL) XFree(cfgs);
1017 ERR("glXChooseFBConfig returns NULL\n");
1018 return;
1021 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1022 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1023 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1024 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1026 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1028 for(i=0, bmp_formats=0; i<nCfgs; i++)
1030 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1031 bmp_formats++;
1033 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1035 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1037 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1038 * Do this as GLX doesn't guarantee that the list is sorted */
1039 for(run=0; run < 2; run++)
1041 for(i=0; i<nCfgs; i++) {
1042 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1043 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1045 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1046 * The second run we only set offscreen formats. */
1047 if(!run && visinfo)
1049 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1050 * The contents is copied to the destination using XCopyArea. For the copying to work
1051 * the depth of the source and destination window should be the same. In general this should
1052 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1053 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1054 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1055 * list formats with the same depth. */
1056 if(visinfo->depth != default_visual.depth)
1058 XFree(visinfo);
1059 continue;
1062 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1063 list[size].fbconfig = cfgs[i];
1064 list[size].visual = visinfo;
1065 list[size].fmt_id = fmt_id;
1066 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1067 list[size].dwFlags = 0;
1068 size++;
1069 onscreen_size++;
1071 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1072 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1074 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1075 list[size].fbconfig = cfgs[i];
1076 list[size].visual = visinfo;
1077 list[size].fmt_id = fmt_id;
1078 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1079 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1080 size++;
1081 onscreen_size++;
1083 } else if(run && !visinfo) {
1084 int window_drawable=0;
1085 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1087 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1088 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1089 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1090 * likely make our child window opengl rendering more complicated since likely you can't use
1091 * XCopyArea on a GLX Window.
1092 * For now ignore fbconfigs which are window drawable but lack a visual. */
1093 if(window_drawable & GLX_WINDOW_BIT)
1095 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1096 continue;
1099 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1100 list[size].fbconfig = cfgs[i];
1101 list[size].fmt_id = fmt_id;
1102 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1103 list[size].dwFlags = 0;
1104 size++;
1106 else if (visinfo) XFree(visinfo);
1110 XFree(cfgs);
1112 pixel_formats = list;
1113 nb_pixel_formats = size;
1114 nb_onscreen_formats = onscreen_size;
1117 static inline BOOL is_valid_pixel_format( int format )
1119 return format > 0 && format <= nb_pixel_formats;
1122 static inline BOOL is_onscreen_pixel_format( int format )
1124 return format > 0 && format <= nb_onscreen_formats;
1127 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1129 return format - pixel_formats + 1;
1132 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1133 * In our WGL implementation we only support a subset of these formats namely the format of
1134 * Wine's main visual and offscreen formats (if they are available).
1135 * This function converts a WGL format to its corresponding GLX one.
1137 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1139 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1140 * iPixelFormat in case of probing the number of pixelformats.
1142 if (is_valid_pixel_format( iPixelFormat ) &&
1143 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1144 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1145 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1146 return &pixel_formats[iPixelFormat-1];
1148 return NULL;
1151 static struct gl_drawable *grab_gl_drawable( struct gl_drawable *gl )
1153 InterlockedIncrement( &gl->ref );
1154 return gl;
1157 static void release_gl_drawable( struct gl_drawable *gl )
1159 if (!gl) return;
1160 if (InterlockedDecrement( &gl->ref )) return;
1161 switch (gl->type)
1163 case DC_GL_WINDOW:
1164 case DC_GL_CHILD_WIN:
1165 TRACE( "destroying %lx drawable %lx\n", gl->window, gl->drawable );
1166 pglXDestroyWindow( gdi_display, gl->drawable );
1167 XDestroyWindow( gdi_display, gl->window );
1168 break;
1169 case DC_GL_PIXMAP_WIN:
1170 TRACE( "destroying pixmap %lx drawable %lx\n", gl->pixmap, gl->drawable );
1171 pglXDestroyPixmap( gdi_display, gl->drawable );
1172 XFreePixmap( gdi_display, gl->pixmap );
1173 break;
1174 default:
1175 break;
1177 HeapFree( GetProcessHeap(), 0, gl );
1180 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1181 static void mark_drawable_dirty( struct gl_drawable *old, struct gl_drawable *new )
1183 struct wgl_context *ctx;
1185 EnterCriticalSection( &context_section );
1186 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1188 if (old == ctx->drawables[0] || old == ctx->new_drawables[0])
1190 release_gl_drawable( ctx->new_drawables[0] );
1191 ctx->new_drawables[0] = grab_gl_drawable( new );
1193 if (old == ctx->drawables[1] || old == ctx->new_drawables[1])
1195 release_gl_drawable( ctx->new_drawables[1] );
1196 ctx->new_drawables[1] = grab_gl_drawable( new );
1199 LeaveCriticalSection( &context_section );
1202 /* Given the current context, make sure its drawable is sync'd */
1203 static inline void sync_context(struct wgl_context *context)
1205 BOOL refresh = FALSE;
1207 EnterCriticalSection( &context_section );
1208 if (context->new_drawables[0])
1210 release_gl_drawable( context->drawables[0] );
1211 context->drawables[0] = context->new_drawables[0];
1212 context->new_drawables[0] = NULL;
1213 refresh = TRUE;
1215 if (context->new_drawables[1])
1217 release_gl_drawable( context->drawables[1] );
1218 context->drawables[1] = context->new_drawables[1];
1219 context->new_drawables[1] = NULL;
1220 refresh = TRUE;
1222 if (refresh)
1224 if (glxRequireVersion(3))
1225 pglXMakeContextCurrent(gdi_display, context->drawables[0]->drawable,
1226 context->drawables[1]->drawable, context->ctx);
1227 else
1228 pglXMakeCurrent(gdi_display, context->drawables[0]->drawable, context->ctx);
1230 LeaveCriticalSection( &context_section );
1233 static BOOL set_swap_interval(GLXDrawable drawable, int interval)
1235 BOOL ret = TRUE;
1237 switch (swap_control_method)
1239 case GLX_SWAP_CONTROL_EXT:
1240 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1241 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1242 XSync(gdi_display, False);
1243 ret = !X11DRV_check_error();
1244 break;
1246 case GLX_SWAP_CONTROL_MESA:
1247 ret = !pglXSwapIntervalMESA(interval);
1248 break;
1250 case GLX_SWAP_CONTROL_SGI:
1251 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1252 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1253 * value to be an error. Just silently ignore the request for now.
1255 if (!interval)
1256 WARN("Request to disable vertical sync is not handled\n");
1257 else
1258 ret = !pglXSwapIntervalSGI(interval);
1259 break;
1261 case GLX_SWAP_CONTROL_NONE:
1262 /* Unlikely to happen on modern GLX implementations */
1263 WARN("Request to adjust swap interval is not handled\n");
1264 break;
1267 return ret;
1270 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1272 struct gl_drawable *gl;
1274 EnterCriticalSection( &context_section );
1275 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1276 gl = grab_gl_drawable( gl );
1277 else if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
1278 gl = grab_gl_drawable( gl );
1279 else
1280 gl = NULL;
1281 LeaveCriticalSection( &context_section );
1282 return gl;
1285 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1287 GLXContext ctx;
1289 if(context->gl3_context)
1291 if(context->numAttribs)
1292 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, context->attribList);
1293 else
1294 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, NULL);
1296 else if(context->fmt->visual)
1297 ctx = pglXCreateContext(gdi_display, context->fmt->visual, shareList, GL_TRUE);
1298 else /* Create a GLX Context for a pbuffer */
1299 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1301 return ctx;
1305 /***********************************************************************
1306 * create_gl_drawable
1308 static struct gl_drawable *create_gl_drawable( HWND hwnd, const struct wgl_pixel_format *format, BOOL known_child )
1310 struct gl_drawable *gl, *prev;
1311 XVisualInfo *visual = format->visual;
1312 RECT rect;
1313 int width, height;
1315 GetClientRect( hwnd, &rect );
1316 width = min( max( 1, rect.right ), 65535 );
1317 height = min( max( 1, rect.bottom ), 65535 );
1319 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) ))) return NULL;
1321 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1322 * there is no way to query it, so we have to store it here.
1324 gl->swap_interval = 1;
1325 gl->refresh_swap_interval = TRUE;
1326 gl->format = format;
1327 gl->ref = 1;
1329 if (!known_child && !GetWindow( hwnd, GW_CHILD ) && GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* childless top-level window */
1331 gl->type = DC_GL_WINDOW;
1332 gl->window = create_client_window( hwnd, visual );
1333 if (gl->window)
1334 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1335 TRACE( "%p created client %lx drawable %lx\n", hwnd, gl->window, gl->drawable );
1337 #ifdef SONAME_LIBXCOMPOSITE
1338 else if(usexcomposite)
1340 gl->type = DC_GL_CHILD_WIN;
1341 gl->window = create_client_window( hwnd, visual );
1342 if (gl->window)
1344 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1345 pXCompositeRedirectWindow( gdi_display, gl->window, CompositeRedirectManual );
1347 TRACE( "%p created child %lx drawable %lx\n", hwnd, gl->window, gl->drawable );
1349 #endif
1350 else
1352 WARN("XComposite is not available, using GLXPixmap hack\n");
1354 gl->type = DC_GL_PIXMAP_WIN;
1355 gl->pixmap = XCreatePixmap( gdi_display, root_window, width, height, visual->depth );
1356 if (gl->pixmap)
1358 gl->drawable = pglXCreatePixmap( gdi_display, gl->format->fbconfig, gl->pixmap, NULL );
1359 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1360 gl->pixmap_size.cx = width;
1361 gl->pixmap_size.cy = height;
1365 if (!gl->drawable)
1367 HeapFree( GetProcessHeap(), 0, gl );
1368 return NULL;
1371 EnterCriticalSection( &context_section );
1372 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1374 gl->swap_interval = prev->swap_interval;
1375 release_gl_drawable( prev );
1377 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)grab_gl_drawable(gl) );
1378 LeaveCriticalSection( &context_section );
1379 return gl;
1383 /***********************************************************************
1384 * set_win_format
1386 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1388 struct gl_drawable *gl;
1390 if (!format->visual) return FALSE;
1392 if (!(gl = create_gl_drawable( hwnd, format, FALSE ))) return FALSE;
1394 TRACE( "created GL drawable %lx for win %p %s\n",
1395 gl->drawable, hwnd, debugstr_fbconfig( format->fbconfig ));
1397 XFlush( gdi_display );
1398 release_gl_drawable( gl );
1400 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1401 return TRUE;
1405 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1407 const struct wgl_pixel_format *fmt;
1408 int value;
1409 HWND hwnd = WindowFromDC( hdc );
1411 TRACE("(%p,%d)\n", hdc, format);
1413 if (!hwnd || hwnd == GetDesktopWindow())
1415 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1416 return FALSE;
1419 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1420 if (!fmt)
1422 ERR( "Invalid format %d\n", format );
1423 return FALSE;
1426 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1427 if (!(value & GLX_WINDOW_BIT))
1429 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1430 return FALSE;
1433 if (!allow_change)
1435 struct gl_drawable *gl;
1436 if ((gl = get_gl_drawable( hwnd, hdc )))
1438 int prev = pixel_format_index( gl->format );
1439 release_gl_drawable( gl );
1440 return prev == format; /* cannot change it if already set */
1444 return set_win_format( hwnd, fmt );
1448 /***********************************************************************
1449 * sync_gl_drawable
1451 void sync_gl_drawable( HWND hwnd, BOOL known_child )
1453 struct gl_drawable *old, *new;
1455 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1457 switch (old->type)
1459 case DC_GL_WINDOW:
1460 if (!known_child) break; /* Still a childless top-level window */
1461 /* fall through */
1462 case DC_GL_PIXMAP_WIN:
1463 if (!(new = create_gl_drawable( hwnd, old->format, known_child ))) break;
1464 mark_drawable_dirty( old, new );
1465 XFlush( gdi_display );
1466 TRACE( "Recreated GL drawable %lx to replace %lx\n", new->drawable, old->drawable );
1467 release_gl_drawable( new );
1468 break;
1469 default:
1470 break;
1472 release_gl_drawable( old );
1476 /***********************************************************************
1477 * set_gl_drawable_parent
1479 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1481 struct gl_drawable *old, *new;
1483 if (!(old = get_gl_drawable( hwnd, 0 ))) return;
1485 TRACE( "setting drawable %lx parent %p\n", old->drawable, parent );
1487 switch (old->type)
1489 case DC_GL_WINDOW:
1490 break;
1491 case DC_GL_CHILD_WIN:
1492 case DC_GL_PIXMAP_WIN:
1493 if (parent == GetDesktopWindow()) break;
1494 /* fall through */
1495 default:
1496 release_gl_drawable( old );
1497 return;
1500 if ((new = create_gl_drawable( hwnd, old->format, FALSE )))
1502 mark_drawable_dirty( old, new );
1503 release_gl_drawable( new );
1505 else
1507 destroy_gl_drawable( hwnd );
1508 __wine_set_pixel_format( hwnd, 0 );
1510 release_gl_drawable( old );
1514 /***********************************************************************
1515 * destroy_gl_drawable
1517 void destroy_gl_drawable( HWND hwnd )
1519 struct gl_drawable *gl;
1521 EnterCriticalSection( &context_section );
1522 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1524 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1525 release_gl_drawable( gl );
1527 LeaveCriticalSection( &context_section );
1532 * glxdrv_DescribePixelFormat
1534 * Get the pixel-format descriptor associated to the given id
1536 static int WINAPI glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1537 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
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 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1548 if (!ppfd) return nb_onscreen_formats;
1550 /* 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 */
1551 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1552 if (!fmt) {
1553 WARN("unexpected format %d\n", iPixelFormat);
1554 return 0;
1557 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1558 ERR("Wrong structure size !\n");
1559 /* Should set error */
1560 return 0;
1563 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1564 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1565 ppfd->nVersion = 1;
1567 /* These flags are always the same... */
1568 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1569 /* Now the flags extracted from the Visual */
1571 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1572 if(value & GLX_WINDOW_BIT)
1573 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1575 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1576 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1577 * 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
1578 * offered the GDI bit either. */
1579 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1581 /* PFD_GENERIC_FORMAT - gdi software rendering
1582 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1583 * none set - full hardware accelerated by a ICD
1585 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1586 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1588 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1589 if (value) {
1590 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1591 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1593 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1595 /* Pixel type */
1596 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1597 if (value & GLX_RGBA_BIT)
1598 ppfd->iPixelType = PFD_TYPE_RGBA;
1599 else
1600 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1602 /* Color bits */
1603 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1604 ppfd->cColorBits = value;
1606 /* Red, green, blue and alpha bits / shifts */
1607 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1608 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1609 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1610 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1611 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1613 ppfd->cRedBits = rb;
1614 ppfd->cRedShift = gb + bb + ab;
1615 ppfd->cBlueBits = bb;
1616 ppfd->cBlueShift = ab;
1617 ppfd->cGreenBits = gb;
1618 ppfd->cGreenShift = bb + ab;
1619 ppfd->cAlphaBits = ab;
1620 ppfd->cAlphaShift = 0;
1621 } else {
1622 ppfd->cRedBits = 0;
1623 ppfd->cRedShift = 0;
1624 ppfd->cBlueBits = 0;
1625 ppfd->cBlueShift = 0;
1626 ppfd->cGreenBits = 0;
1627 ppfd->cGreenShift = 0;
1628 ppfd->cAlphaBits = 0;
1629 ppfd->cAlphaShift = 0;
1632 /* Accum RGBA bits */
1633 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1634 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1635 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1636 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1638 ppfd->cAccumBits = rb+gb+bb+ab;
1639 ppfd->cAccumRedBits = rb;
1640 ppfd->cAccumGreenBits = gb;
1641 ppfd->cAccumBlueBits = bb;
1642 ppfd->cAccumAlphaBits = ab;
1644 /* Aux bits */
1645 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1646 ppfd->cAuxBuffers = value;
1648 /* Depth bits */
1649 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1650 ppfd->cDepthBits = value;
1652 /* stencil bits */
1653 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1654 ppfd->cStencilBits = value;
1656 ppfd->iLayerType = PFD_MAIN_PLANE;
1658 if (TRACE_ON(wgl)) {
1659 dump_PIXELFORMATDESCRIPTOR(ppfd);
1662 return nb_onscreen_formats;
1665 /***********************************************************************
1666 * glxdrv_wglGetPixelFormat
1668 static int WINAPI glxdrv_wglGetPixelFormat( HDC hdc )
1670 struct gl_drawable *gl;
1671 int ret = 0;
1673 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1675 ret = pixel_format_index( gl->format );
1676 /* Offscreen formats can't be used with traditional WGL calls.
1677 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1678 if (!is_onscreen_pixel_format( ret )) ret = 1;
1679 release_gl_drawable( gl );
1681 TRACE( "%p -> %d\n", hdc, ret );
1682 return ret;
1685 /***********************************************************************
1686 * glxdrv_wglSetPixelFormat
1688 static BOOL WINAPI glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1690 return set_pixel_format(hdc, iPixelFormat, FALSE);
1693 /***********************************************************************
1694 * glxdrv_wglCopyContext
1696 static BOOL WINAPI glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1698 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1700 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1702 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1703 return TRUE;
1706 /***********************************************************************
1707 * glxdrv_wglCreateContext
1709 static struct wgl_context * WINAPI glxdrv_wglCreateContext( HDC hdc )
1711 struct wgl_context *ret;
1712 struct gl_drawable *gl;
1714 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1716 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1717 return NULL;
1720 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1722 ret->hdc = hdc;
1723 ret->fmt = gl->format;
1724 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1725 EnterCriticalSection( &context_section );
1726 list_add_head( &context_list, &ret->entry );
1727 LeaveCriticalSection( &context_section );
1729 release_gl_drawable( gl );
1730 TRACE( "%p -> %p\n", hdc, ret );
1731 return ret;
1734 /***********************************************************************
1735 * glxdrv_wglDeleteContext
1737 static BOOL WINAPI glxdrv_wglDeleteContext(struct wgl_context *ctx)
1739 struct wgl_pbuffer *pb;
1741 TRACE("(%p)\n", ctx);
1743 EnterCriticalSection( &context_section );
1744 list_remove( &ctx->entry );
1745 LIST_FOR_EACH_ENTRY( pb, &pbuffer_list, struct wgl_pbuffer, entry )
1747 if (pb->prev_context == ctx->ctx) {
1748 pglXDestroyContext(gdi_display, pb->tmp_context);
1749 pb->prev_context = pb->tmp_context = NULL;
1752 LeaveCriticalSection( &context_section );
1754 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1755 release_gl_drawable( ctx->drawables[0] );
1756 release_gl_drawable( ctx->drawables[1] );
1757 release_gl_drawable( ctx->new_drawables[0] );
1758 release_gl_drawable( ctx->new_drawables[1] );
1759 return HeapFree( GetProcessHeap(), 0, ctx );
1762 /***********************************************************************
1763 * glxdrv_wglGetProcAddress
1765 static PROC WINAPI glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1767 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1768 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1771 static void set_context_drawables( struct wgl_context *ctx, struct gl_drawable *draw,
1772 struct gl_drawable *read )
1774 struct gl_drawable *prev[4];
1775 int i;
1777 prev[0] = ctx->drawables[0];
1778 prev[1] = ctx->drawables[1];
1779 prev[2] = ctx->new_drawables[0];
1780 prev[3] = ctx->new_drawables[1];
1781 ctx->drawables[0] = grab_gl_drawable( draw );
1782 ctx->drawables[1] = read ? grab_gl_drawable( read ) : NULL;
1783 ctx->new_drawables[0] = ctx->new_drawables[1] = NULL;
1784 for (i = 0; i < 4; i++) release_gl_drawable( prev[i] );
1787 /***********************************************************************
1788 * glxdrv_wglMakeCurrent
1790 static BOOL WINAPI glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1792 BOOL ret = FALSE;
1793 struct gl_drawable *gl;
1795 TRACE("(%p,%p)\n", hdc, ctx);
1797 if (!ctx)
1799 pglXMakeCurrent(gdi_display, None, NULL);
1800 NtCurrentTeb()->glContext = NULL;
1801 return TRUE;
1804 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1806 if (ctx->fmt != gl->format)
1808 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1809 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1810 goto done;
1813 TRACE("hdc %p drawable %lx fmt %p ctx %p %s\n", hdc, gl->drawable, gl->format, ctx->ctx,
1814 debugstr_fbconfig( gl->format->fbconfig ));
1816 EnterCriticalSection( &context_section );
1817 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1818 if (ret)
1820 NtCurrentTeb()->glContext = ctx;
1821 ctx->has_been_current = TRUE;
1822 ctx->hdc = hdc;
1823 set_context_drawables( ctx, gl, gl );
1824 ctx->refresh_drawables = FALSE;
1825 LeaveCriticalSection( &context_section );
1826 goto done;
1828 LeaveCriticalSection( &context_section );
1830 SetLastError( ERROR_INVALID_HANDLE );
1832 done:
1833 release_gl_drawable( gl );
1834 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1835 return ret;
1838 /***********************************************************************
1839 * X11DRV_wglMakeContextCurrentARB
1841 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1843 BOOL ret = FALSE;
1844 struct gl_drawable *draw_gl, *read_gl = NULL;
1846 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1848 if (!ctx)
1850 pglXMakeCurrent(gdi_display, None, NULL);
1851 NtCurrentTeb()->glContext = NULL;
1852 return TRUE;
1855 if (!pglXMakeContextCurrent) return FALSE;
1857 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1859 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1861 EnterCriticalSection( &context_section );
1862 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1863 read_gl ? read_gl->drawable : 0, ctx->ctx);
1864 if (ret)
1866 ctx->has_been_current = TRUE;
1867 ctx->hdc = draw_hdc;
1868 set_context_drawables( ctx, draw_gl, read_gl );
1869 ctx->refresh_drawables = FALSE;
1870 NtCurrentTeb()->glContext = ctx;
1871 LeaveCriticalSection( &context_section );
1872 goto done;
1874 LeaveCriticalSection( &context_section );
1876 SetLastError( ERROR_INVALID_HANDLE );
1877 done:
1878 release_gl_drawable( read_gl );
1879 release_gl_drawable( draw_gl );
1880 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1881 return ret;
1884 /***********************************************************************
1885 * glxdrv_wglShareLists
1887 static BOOL WINAPI glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1889 TRACE("(%p, %p)\n", org, dest);
1891 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1892 * at context creation time but in case of WGL it is done using wglShareLists.
1893 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1894 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1895 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1896 * so there delaying context creation doesn't work.
1898 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1899 * and when a program requests sharing we recreate the destination context if it hasn't been made
1900 * current or when it hasn't shared display lists before.
1903 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1905 ERR("Could not share display lists, one of the contexts has been current already !\n");
1906 return FALSE;
1908 else if(dest->sharing)
1910 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1911 return FALSE;
1913 else
1915 /* Re-create the GLX context and share display lists */
1916 pglXDestroyContext(gdi_display, dest->ctx);
1917 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1918 TRACE(" re-created context (%p) for Wine context %p (%s) sharing lists with ctx %p (%s)\n",
1919 dest->ctx, dest, debugstr_fbconfig(dest->fmt->fbconfig),
1920 org->ctx, debugstr_fbconfig( org->fmt->fbconfig));
1922 org->sharing = TRUE;
1923 dest->sharing = TRUE;
1924 return TRUE;
1926 return FALSE;
1929 static void wglFinish(void)
1931 struct x11drv_escape_flush_gl_drawable escape;
1932 struct gl_drawable *gl;
1933 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1935 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1936 escape.gl_drawable = 0;
1937 escape.flush = FALSE;
1939 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1941 switch (gl->type)
1943 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1944 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
1945 default: break;
1947 sync_context(ctx);
1948 release_gl_drawable( gl );
1951 pglFinish();
1952 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1955 static void wglFlush(void)
1957 struct x11drv_escape_flush_gl_drawable escape;
1958 struct gl_drawable *gl;
1959 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1961 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1962 escape.gl_drawable = 0;
1963 escape.flush = FALSE;
1965 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1967 switch (gl->type)
1969 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1970 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
1971 default: break;
1973 sync_context(ctx);
1974 release_gl_drawable( gl );
1977 pglFlush();
1978 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1981 static const GLubyte *wglGetString(GLenum name)
1983 if (name == GL_EXTENSIONS && glExtensions) return (const GLubyte *)glExtensions;
1984 return pglGetString(name);
1987 /***********************************************************************
1988 * X11DRV_wglCreateContextAttribsARB
1990 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
1991 const int* attribList )
1993 struct wgl_context *ret;
1994 struct gl_drawable *gl;
1995 int err = 0;
1997 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
1999 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2001 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2002 return NULL;
2005 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2007 ret->hdc = hdc;
2008 ret->fmt = gl->format;
2009 ret->gl3_context = TRUE;
2010 if (attribList)
2012 int *pContextAttribList = &ret->attribList[0];
2013 /* attribList consists of pairs {token, value] terminated with 0 */
2014 while(attribList[0] != 0)
2016 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2017 switch(attribList[0])
2019 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2020 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2021 pContextAttribList[1] = attribList[1];
2022 pContextAttribList += 2;
2023 ret->numAttribs++;
2024 break;
2025 case WGL_CONTEXT_MINOR_VERSION_ARB:
2026 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2027 pContextAttribList[1] = attribList[1];
2028 pContextAttribList += 2;
2029 ret->numAttribs++;
2030 break;
2031 case WGL_CONTEXT_LAYER_PLANE_ARB:
2032 break;
2033 case WGL_CONTEXT_FLAGS_ARB:
2034 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2035 pContextAttribList[1] = attribList[1];
2036 pContextAttribList += 2;
2037 ret->numAttribs++;
2038 break;
2039 case WGL_CONTEXT_OPENGL_NO_ERROR_ARB:
2040 pContextAttribList[0] = GLX_CONTEXT_OPENGL_NO_ERROR_ARB;
2041 pContextAttribList[1] = attribList[1];
2042 pContextAttribList += 2;
2043 ret->numAttribs++;
2044 break;
2045 case WGL_CONTEXT_PROFILE_MASK_ARB:
2046 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2047 pContextAttribList[1] = attribList[1];
2048 pContextAttribList += 2;
2049 ret->numAttribs++;
2050 break;
2051 case WGL_RENDERER_ID_WINE:
2052 pContextAttribList[0] = GLX_RENDERER_ID_MESA;
2053 pContextAttribList[1] = attribList[1];
2054 pContextAttribList += 2;
2055 ret->numAttribs++;
2056 break;
2057 default:
2058 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2060 attribList += 2;
2064 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2065 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2066 XSync(gdi_display, False);
2067 if ((err = X11DRV_check_error()) || !ret->ctx)
2069 /* In the future we should convert the GLX error to a win32 one here if needed */
2070 WARN("Context creation failed (error %#x).\n", err);
2071 HeapFree( GetProcessHeap(), 0, ret );
2072 ret = NULL;
2074 else
2076 EnterCriticalSection( &context_section );
2077 list_add_head( &context_list, &ret->entry );
2078 LeaveCriticalSection( &context_section );
2082 release_gl_drawable( gl );
2083 TRACE( "%p -> %p\n", hdc, ret );
2084 return ret;
2088 * X11DRV_wglGetExtensionsStringARB
2090 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2092 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2094 TRACE("() returning \"%s\"\n", wglExtensions);
2095 return wglExtensions;
2099 * X11DRV_wglCreatePbufferARB
2101 * WGL_ARB_pbuffer: wglCreatePbufferARB
2103 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2104 const int *piAttribList )
2106 struct wgl_pbuffer* object;
2107 const struct wgl_pixel_format *fmt;
2108 int attribs[256];
2109 int nAttribs = 0;
2111 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2113 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2114 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2115 if(!fmt) {
2116 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2117 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2118 return NULL;
2121 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2122 if (NULL == object) {
2123 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2124 return NULL;
2126 object->width = iWidth;
2127 object->height = iHeight;
2128 object->fmt = fmt;
2130 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2131 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2132 while (piAttribList && 0 != *piAttribList) {
2133 int attr_v;
2134 switch (*piAttribList) {
2135 case WGL_PBUFFER_LARGEST_ARB: {
2136 ++piAttribList;
2137 attr_v = *piAttribList;
2138 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2139 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2140 break;
2143 case WGL_TEXTURE_FORMAT_ARB: {
2144 ++piAttribList;
2145 attr_v = *piAttribList;
2146 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2147 if (WGL_NO_TEXTURE_ARB == attr_v) {
2148 object->use_render_texture = 0;
2149 } else {
2150 if (!use_render_texture_emulation) {
2151 SetLastError(ERROR_INVALID_DATA);
2152 goto create_failed;
2154 switch (attr_v) {
2155 case WGL_TEXTURE_RGB_ARB:
2156 object->use_render_texture = GL_RGB;
2157 object->texture_bpp = 3;
2158 object->texture_format = GL_RGB;
2159 object->texture_type = GL_UNSIGNED_BYTE;
2160 break;
2161 case WGL_TEXTURE_RGBA_ARB:
2162 object->use_render_texture = GL_RGBA;
2163 object->texture_bpp = 4;
2164 object->texture_format = GL_RGBA;
2165 object->texture_type = GL_UNSIGNED_BYTE;
2166 break;
2168 /* WGL_FLOAT_COMPONENTS_NV */
2169 case WGL_TEXTURE_FLOAT_R_NV:
2170 object->use_render_texture = GL_FLOAT_R_NV;
2171 object->texture_bpp = 4;
2172 object->texture_format = GL_RED;
2173 object->texture_type = GL_FLOAT;
2174 break;
2175 case WGL_TEXTURE_FLOAT_RG_NV:
2176 object->use_render_texture = GL_FLOAT_RG_NV;
2177 object->texture_bpp = 8;
2178 object->texture_format = GL_LUMINANCE_ALPHA;
2179 object->texture_type = GL_FLOAT;
2180 break;
2181 case WGL_TEXTURE_FLOAT_RGB_NV:
2182 object->use_render_texture = GL_FLOAT_RGB_NV;
2183 object->texture_bpp = 12;
2184 object->texture_format = GL_RGB;
2185 object->texture_type = GL_FLOAT;
2186 break;
2187 case WGL_TEXTURE_FLOAT_RGBA_NV:
2188 object->use_render_texture = GL_FLOAT_RGBA_NV;
2189 object->texture_bpp = 16;
2190 object->texture_format = GL_RGBA;
2191 object->texture_type = GL_FLOAT;
2192 break;
2193 default:
2194 ERR("Unknown texture format: %x\n", attr_v);
2195 SetLastError(ERROR_INVALID_DATA);
2196 goto create_failed;
2199 break;
2202 case WGL_TEXTURE_TARGET_ARB: {
2203 ++piAttribList;
2204 attr_v = *piAttribList;
2205 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2206 if (WGL_NO_TEXTURE_ARB == attr_v) {
2207 object->texture_target = 0;
2208 } else {
2209 if (!use_render_texture_emulation) {
2210 SetLastError(ERROR_INVALID_DATA);
2211 goto create_failed;
2213 switch (attr_v) {
2214 case WGL_TEXTURE_CUBE_MAP_ARB: {
2215 if (iWidth != iHeight) {
2216 SetLastError(ERROR_INVALID_DATA);
2217 goto create_failed;
2219 object->texture_target = GL_TEXTURE_CUBE_MAP;
2220 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2221 break;
2223 case WGL_TEXTURE_1D_ARB: {
2224 if (1 != iHeight) {
2225 SetLastError(ERROR_INVALID_DATA);
2226 goto create_failed;
2228 object->texture_target = GL_TEXTURE_1D;
2229 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2230 break;
2232 case WGL_TEXTURE_2D_ARB: {
2233 object->texture_target = GL_TEXTURE_2D;
2234 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2235 break;
2237 case WGL_TEXTURE_RECTANGLE_NV: {
2238 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2239 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2240 break;
2242 default:
2243 ERR("Unknown texture target: %x\n", attr_v);
2244 SetLastError(ERROR_INVALID_DATA);
2245 goto create_failed;
2248 break;
2251 case WGL_MIPMAP_TEXTURE_ARB: {
2252 ++piAttribList;
2253 attr_v = *piAttribList;
2254 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2255 if (!use_render_texture_emulation) {
2256 SetLastError(ERROR_INVALID_DATA);
2257 goto create_failed;
2259 break;
2262 ++piAttribList;
2265 PUSH1(attribs, None);
2266 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2267 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2268 if (!object->drawable) {
2269 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2270 goto create_failed; /* unexpected error */
2272 EnterCriticalSection( &context_section );
2273 list_add_head( &pbuffer_list, &object->entry );
2274 LeaveCriticalSection( &context_section );
2275 TRACE("->(%p)\n", object);
2276 return object;
2278 create_failed:
2279 HeapFree(GetProcessHeap(), 0, object);
2280 TRACE("->(FAILED)\n");
2281 return NULL;
2285 * X11DRV_wglDestroyPbufferARB
2287 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2289 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2291 TRACE("(%p)\n", object);
2293 EnterCriticalSection( &context_section );
2294 list_remove( &object->entry );
2295 LeaveCriticalSection( &context_section );
2296 pglXDestroyPbuffer(gdi_display, object->drawable);
2297 if (object->tmp_context)
2298 pglXDestroyContext(gdi_display, object->tmp_context);
2299 HeapFree(GetProcessHeap(), 0, object);
2300 return GL_TRUE;
2304 * X11DRV_wglGetPbufferDCARB
2306 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2308 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2310 struct x11drv_escape_set_drawable escape;
2311 struct gl_drawable *gl, *prev;
2312 HDC hdc;
2314 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2315 if (!hdc) return 0;
2317 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2319 DeleteDC( hdc );
2320 return 0;
2322 gl->type = DC_GL_PBUFFER;
2323 gl->drawable = object->drawable;
2324 gl->format = object->fmt;
2325 gl->ref = 1;
2327 EnterCriticalSection( &context_section );
2328 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2329 release_gl_drawable( prev );
2330 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2331 LeaveCriticalSection( &context_section );
2333 escape.code = X11DRV_SET_DRAWABLE;
2334 escape.drawable = object->drawable;
2335 escape.mode = IncludeInferiors;
2336 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2337 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2339 TRACE( "(%p)->(%p)\n", object, hdc );
2340 return hdc;
2344 * X11DRV_wglQueryPbufferARB
2346 * WGL_ARB_pbuffer: wglQueryPbufferARB
2348 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2350 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2352 switch (iAttribute) {
2353 case WGL_PBUFFER_WIDTH_ARB:
2354 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2355 break;
2356 case WGL_PBUFFER_HEIGHT_ARB:
2357 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2358 break;
2360 case WGL_PBUFFER_LOST_ARB:
2361 /* GLX Pbuffers cannot be lost by default. We can support this by
2362 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2363 * to receive pixel buffer clobber events, however that may or may
2364 * not give any benefit */
2365 *piValue = GL_FALSE;
2366 break;
2368 case WGL_TEXTURE_FORMAT_ARB:
2369 if (!object->use_render_texture) {
2370 *piValue = WGL_NO_TEXTURE_ARB;
2371 } else {
2372 if (!use_render_texture_emulation) {
2373 SetLastError(ERROR_INVALID_HANDLE);
2374 return GL_FALSE;
2376 switch(object->use_render_texture) {
2377 case GL_RGB:
2378 *piValue = WGL_TEXTURE_RGB_ARB;
2379 break;
2380 case GL_RGBA:
2381 *piValue = WGL_TEXTURE_RGBA_ARB;
2382 break;
2383 /* WGL_FLOAT_COMPONENTS_NV */
2384 case GL_FLOAT_R_NV:
2385 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2386 break;
2387 case GL_FLOAT_RG_NV:
2388 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2389 break;
2390 case GL_FLOAT_RGB_NV:
2391 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2392 break;
2393 case GL_FLOAT_RGBA_NV:
2394 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2395 break;
2396 default:
2397 ERR("Unknown texture format: %x\n", object->use_render_texture);
2400 break;
2402 case WGL_TEXTURE_TARGET_ARB:
2403 if (!object->texture_target){
2404 *piValue = WGL_NO_TEXTURE_ARB;
2405 } else {
2406 if (!use_render_texture_emulation) {
2407 SetLastError(ERROR_INVALID_DATA);
2408 return GL_FALSE;
2410 switch (object->texture_target) {
2411 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2412 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2413 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2414 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2417 break;
2419 case WGL_MIPMAP_TEXTURE_ARB:
2420 *piValue = GL_FALSE; /** don't support that */
2421 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2422 break;
2424 default:
2425 FIXME("unexpected attribute %x\n", iAttribute);
2426 break;
2429 return GL_TRUE;
2433 * X11DRV_wglReleasePbufferDCARB
2435 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2437 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2439 struct gl_drawable *gl;
2441 TRACE("(%p, %p)\n", object, hdc);
2443 EnterCriticalSection( &context_section );
2445 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2447 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2448 release_gl_drawable( gl );
2450 else hdc = 0;
2452 LeaveCriticalSection( &context_section );
2454 return hdc && DeleteDC(hdc);
2458 * X11DRV_wglSetPbufferAttribARB
2460 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2462 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2464 GLboolean ret = GL_FALSE;
2466 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2468 if (!object->use_render_texture) {
2469 SetLastError(ERROR_INVALID_HANDLE);
2470 return GL_FALSE;
2472 if (use_render_texture_emulation) {
2473 return GL_TRUE;
2475 return ret;
2479 * X11DRV_wglChoosePixelFormatARB
2481 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2483 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2484 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2486 int attribs[256];
2487 int nAttribs = 0;
2488 GLXFBConfig* cfgs;
2489 int nCfgs = 0;
2490 int it;
2491 int fmt_id;
2492 int start, end;
2493 UINT pfmt_it = 0;
2494 int run;
2495 int i;
2496 DWORD dwFlags = 0;
2498 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2499 if (NULL != pfAttribFList) {
2500 FIXME("unused pfAttribFList\n");
2503 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2504 if (-1 == nAttribs) {
2505 WARN("Cannot convert WGL to GLX attributes\n");
2506 return GL_FALSE;
2508 PUSH1(attribs, None);
2510 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2511 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2512 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2514 for(i=0; piAttribIList[i] != 0; i+=2)
2516 switch(piAttribIList[i])
2518 case WGL_DRAW_TO_BITMAP_ARB:
2519 if(piAttribIList[i+1])
2520 dwFlags |= PFD_DRAW_TO_BITMAP;
2521 break;
2522 case WGL_ACCELERATION_ARB:
2523 switch(piAttribIList[i+1])
2525 case WGL_NO_ACCELERATION_ARB:
2526 dwFlags |= PFD_GENERIC_FORMAT;
2527 break;
2528 case WGL_GENERIC_ACCELERATION_ARB:
2529 dwFlags |= PFD_GENERIC_ACCELERATED;
2530 break;
2531 case WGL_FULL_ACCELERATION_ARB:
2532 /* Nothing to do */
2533 break;
2535 break;
2536 case WGL_SUPPORT_GDI_ARB:
2537 if(piAttribIList[i+1])
2538 dwFlags |= PFD_SUPPORT_GDI;
2539 break;
2543 /* Search for FB configurations matching the requirements in attribs */
2544 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2545 if (NULL == cfgs) {
2546 WARN("Compatible Pixel Format not found\n");
2547 return GL_FALSE;
2550 /* Loop through all matching formats and check if they are suitable.
2551 * Note that this function should at max return nMaxFormats different formats */
2552 for(run=0; run < 2; run++)
2554 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2556 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2558 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2559 continue;
2562 /* During the first run we only want onscreen formats and during the second only offscreen */
2563 start = run == 1 ? nb_onscreen_formats : 0;
2564 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2566 for (i = start; i < end; i++)
2568 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2570 piFormats[pfmt_it++] = i + 1;
2571 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2572 it + 1, nCfgs, fmt_id, i + 1);
2573 break;
2579 *nNumFormats = pfmt_it;
2580 /** free list */
2581 XFree(cfgs);
2582 return GL_TRUE;
2586 * X11DRV_wglGetPixelFormatAttribivARB
2588 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2590 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2591 UINT nAttributes, const int *piAttributes, int *piValues )
2593 UINT i;
2594 const struct wgl_pixel_format *fmt;
2595 int hTest;
2596 int tmp;
2597 int curGLXAttr = 0;
2599 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2601 if (0 < iLayerPlane) {
2602 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2603 return GL_FALSE;
2606 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2607 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2608 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2609 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2610 if(!fmt) {
2611 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2614 for (i = 0; i < nAttributes; ++i) {
2615 const int curWGLAttr = piAttributes[i];
2616 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2618 switch (curWGLAttr) {
2619 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2620 piValues[i] = nb_pixel_formats;
2621 continue;
2623 case WGL_SUPPORT_OPENGL_ARB:
2624 piValues[i] = GL_TRUE;
2625 continue;
2627 case WGL_ACCELERATION_ARB:
2628 curGLXAttr = GLX_CONFIG_CAVEAT;
2629 if (!fmt) goto pix_error;
2630 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2631 piValues[i] = WGL_NO_ACCELERATION_ARB;
2632 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2633 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2634 else
2635 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2636 continue;
2638 case WGL_TRANSPARENT_ARB:
2639 curGLXAttr = GLX_TRANSPARENT_TYPE;
2640 if (!fmt) goto pix_error;
2641 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2642 if (hTest) goto get_error;
2643 piValues[i] = GL_FALSE;
2644 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2645 continue;
2647 case WGL_PIXEL_TYPE_ARB:
2648 curGLXAttr = GLX_RENDER_TYPE;
2649 if (!fmt) goto pix_error;
2650 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2651 if (hTest) goto get_error;
2652 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2653 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2654 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2655 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2656 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2657 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2658 else {
2659 ERR("unexpected RenderType(%x)\n", tmp);
2660 piValues[i] = WGL_TYPE_RGBA_ARB;
2662 continue;
2664 case WGL_COLOR_BITS_ARB:
2665 curGLXAttr = GLX_BUFFER_SIZE;
2666 break;
2668 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2669 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2670 if (!use_render_texture_emulation) {
2671 piValues[i] = GL_FALSE;
2672 continue;
2674 curGLXAttr = GLX_RENDER_TYPE;
2675 if (!fmt) goto pix_error;
2676 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2677 if (hTest) goto get_error;
2678 if (GLX_COLOR_INDEX_BIT == tmp) {
2679 piValues[i] = GL_FALSE;
2680 continue;
2682 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2683 if (hTest) goto get_error;
2684 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2685 continue;
2687 case WGL_BLUE_BITS_ARB:
2688 curGLXAttr = GLX_BLUE_SIZE;
2689 break;
2690 case WGL_RED_BITS_ARB:
2691 curGLXAttr = GLX_RED_SIZE;
2692 break;
2693 case WGL_GREEN_BITS_ARB:
2694 curGLXAttr = GLX_GREEN_SIZE;
2695 break;
2696 case WGL_ALPHA_BITS_ARB:
2697 curGLXAttr = GLX_ALPHA_SIZE;
2698 break;
2699 case WGL_DEPTH_BITS_ARB:
2700 curGLXAttr = GLX_DEPTH_SIZE;
2701 break;
2702 case WGL_STENCIL_BITS_ARB:
2703 curGLXAttr = GLX_STENCIL_SIZE;
2704 break;
2705 case WGL_DOUBLE_BUFFER_ARB:
2706 curGLXAttr = GLX_DOUBLEBUFFER;
2707 break;
2708 case WGL_STEREO_ARB:
2709 curGLXAttr = GLX_STEREO;
2710 break;
2711 case WGL_AUX_BUFFERS_ARB:
2712 curGLXAttr = GLX_AUX_BUFFERS;
2713 break;
2715 case WGL_SUPPORT_GDI_ARB:
2716 if (!fmt) goto pix_error;
2717 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2718 continue;
2720 case WGL_DRAW_TO_BITMAP_ARB:
2721 if (!fmt) goto pix_error;
2722 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2723 continue;
2725 case WGL_DRAW_TO_WINDOW_ARB:
2726 case WGL_DRAW_TO_PBUFFER_ARB:
2727 if (!fmt) goto pix_error;
2728 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2729 if (hTest) goto get_error;
2730 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2731 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2732 piValues[i] = GL_TRUE;
2733 else
2734 piValues[i] = GL_FALSE;
2735 continue;
2737 case WGL_SWAP_METHOD_ARB:
2738 if (has_swap_method)
2740 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_SWAP_METHOD_OML, &tmp);
2741 if (hTest) goto get_error;
2742 switch (tmp)
2744 case GLX_SWAP_EXCHANGE_OML:
2745 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2746 break;
2747 case GLX_SWAP_COPY_OML:
2748 piValues[i] = WGL_SWAP_COPY_ARB;
2749 break;
2750 case GLX_SWAP_UNDEFINED_OML:
2751 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2752 break;
2753 default:
2754 ERR("Unexpected swap method %x.\n", tmp);
2757 else
2759 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2760 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2762 continue;
2764 case WGL_PBUFFER_LARGEST_ARB:
2765 curGLXAttr = GLX_LARGEST_PBUFFER;
2766 break;
2768 case WGL_SAMPLE_BUFFERS_ARB:
2769 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2770 break;
2772 case WGL_SAMPLES_ARB:
2773 curGLXAttr = GLX_SAMPLES_ARB;
2774 break;
2776 case WGL_FLOAT_COMPONENTS_NV:
2777 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2778 break;
2780 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2781 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2782 break;
2784 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2785 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2786 break;
2788 case WGL_ACCUM_RED_BITS_ARB:
2789 curGLXAttr = GLX_ACCUM_RED_SIZE;
2790 break;
2791 case WGL_ACCUM_GREEN_BITS_ARB:
2792 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2793 break;
2794 case WGL_ACCUM_BLUE_BITS_ARB:
2795 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2796 break;
2797 case WGL_ACCUM_ALPHA_BITS_ARB:
2798 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2799 break;
2800 case WGL_ACCUM_BITS_ARB:
2801 if (!fmt) goto pix_error;
2802 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2803 if (hTest) goto get_error;
2804 piValues[i] = tmp;
2805 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2806 if (hTest) goto get_error;
2807 piValues[i] += tmp;
2808 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2809 if (hTest) goto get_error;
2810 piValues[i] += tmp;
2811 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2812 if (hTest) goto get_error;
2813 piValues[i] += tmp;
2814 continue;
2816 default:
2817 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2820 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2821 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2822 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2824 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2825 * and check which options can work using iPixelFormat=0 and which not.
2826 * A problem would be that this function is an extension. This would
2827 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2829 if (0 != curGLXAttr && iPixelFormat != 0) {
2830 if (!fmt) goto pix_error;
2831 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2832 if (hTest) goto get_error;
2833 curGLXAttr = 0;
2834 } else {
2835 piValues[i] = GL_FALSE;
2838 return GL_TRUE;
2840 get_error:
2841 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2842 return GL_FALSE;
2844 pix_error:
2845 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2846 return GL_FALSE;
2850 * X11DRV_wglGetPixelFormatAttribfvARB
2852 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2854 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2855 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2857 int *attr;
2858 int ret;
2859 UINT i;
2861 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2863 /* Allocate a temporary array to store integer values */
2864 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2865 if (!attr) {
2866 ERR("couldn't allocate %d array\n", nAttributes);
2867 return GL_FALSE;
2870 /* Piggy-back on wglGetPixelFormatAttribivARB */
2871 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2872 if (ret) {
2873 /* Convert integer values to float. Should also check for attributes
2874 that can give decimal values here */
2875 for (i=0; i<nAttributes;i++) {
2876 pfValues[i] = attr[i];
2880 HeapFree(GetProcessHeap(), 0, attr);
2881 return ret;
2885 * X11DRV_wglBindTexImageARB
2887 * WGL_ARB_render_texture: wglBindTexImageARB
2889 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2891 GLboolean ret = GL_FALSE;
2893 TRACE("(%p, %d)\n", object, iBuffer);
2895 if (!object->use_render_texture) {
2896 SetLastError(ERROR_INVALID_HANDLE);
2897 return GL_FALSE;
2900 if (use_render_texture_emulation) {
2901 static BOOL initialized = FALSE;
2902 int prev_binded_texture = 0;
2903 GLXContext prev_context;
2904 GLXDrawable prev_drawable;
2906 prev_context = pglXGetCurrentContext();
2907 prev_drawable = pglXGetCurrentDrawable();
2909 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2910 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2911 isn't ideal performance wise but I wasn't able to get other ways working.
2913 if(!initialized) {
2914 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2915 FIXME("partial stub!\n");
2918 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2919 if (!object->tmp_context || object->prev_context != prev_context) {
2920 if (object->tmp_context)
2921 pglXDestroyContext(gdi_display, object->tmp_context);
2922 object->tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2923 object->prev_context = prev_context;
2926 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2928 /* Switch to our pbuffer */
2929 pglXMakeCurrent(gdi_display, object->drawable, object->tmp_context);
2931 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2932 * After that copy the pbuffer texture data. */
2933 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2934 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2936 /* Switch back to the original drawable and context */
2937 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2938 return GL_TRUE;
2941 return ret;
2945 * X11DRV_wglReleaseTexImageARB
2947 * WGL_ARB_render_texture: wglReleaseTexImageARB
2949 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2951 GLboolean ret = GL_FALSE;
2953 TRACE("(%p, %d)\n", object, iBuffer);
2955 if (!object->use_render_texture) {
2956 SetLastError(ERROR_INVALID_HANDLE);
2957 return GL_FALSE;
2959 if (use_render_texture_emulation) {
2960 return GL_TRUE;
2962 return ret;
2966 * X11DRV_wglGetExtensionsStringEXT
2968 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2970 static const char *X11DRV_wglGetExtensionsStringEXT(void)
2972 TRACE("() returning \"%s\"\n", wglExtensions);
2973 return wglExtensions;
2977 * X11DRV_wglGetSwapIntervalEXT
2979 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2981 static int X11DRV_wglGetSwapIntervalEXT(void)
2983 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2984 struct gl_drawable *gl;
2985 int swap_interval;
2987 TRACE("()\n");
2989 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
2991 /* This can't happen because a current WGL context is required to get
2992 * here. Likely the application is buggy.
2994 WARN("No GL drawable found, returning swap interval 0\n");
2995 return 0;
2998 swap_interval = gl->swap_interval;
2999 release_gl_drawable(gl);
3001 return swap_interval;
3005 * X11DRV_wglSwapIntervalEXT
3007 * WGL_EXT_swap_control: wglSwapIntervalEXT
3009 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3011 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3012 struct gl_drawable *gl;
3013 BOOL ret;
3015 TRACE("(%d)\n", interval);
3017 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3018 * is invalid.
3020 if (interval < 0 && !has_swap_control_tear)
3022 SetLastError(ERROR_INVALID_DATA);
3023 return FALSE;
3026 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3028 SetLastError(ERROR_DC_NOT_FOUND);
3029 return FALSE;
3032 EnterCriticalSection( &context_section );
3033 ret = set_swap_interval(gl->drawable, interval);
3034 gl->refresh_swap_interval = FALSE;
3035 if (ret)
3036 gl->swap_interval = interval;
3037 else
3038 SetLastError(ERROR_DC_NOT_FOUND);
3040 LeaveCriticalSection( &context_section );
3041 release_gl_drawable(gl);
3043 return ret;
3047 * X11DRV_wglSetPixelFormatWINE
3049 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3050 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3052 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3054 return set_pixel_format(hdc, format, TRUE);
3057 static BOOL X11DRV_wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
3059 return pglXQueryCurrentRendererIntegerMESA( attribute, value );
3062 static const char *X11DRV_wglQueryCurrentRendererStringWINE( GLenum attribute )
3064 return pglXQueryCurrentRendererStringMESA( attribute );
3067 static BOOL X11DRV_wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
3069 return pglXQueryRendererIntegerMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute, value );
3072 static const char *X11DRV_wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
3074 return pglXQueryRendererStringMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute );
3078 * glxRequireVersion (internal)
3080 * Check if the supported GLX version matches requiredVersion.
3082 static BOOL glxRequireVersion(int requiredVersion)
3084 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3085 return (requiredVersion <= glxVersion[1]);
3088 static void register_extension(const char *ext)
3090 if (wglExtensions[0])
3091 strcat(wglExtensions, " ");
3092 strcat(wglExtensions, ext);
3094 TRACE("'%s'\n", ext);
3098 * X11DRV_WineGL_LoadExtensions
3100 static void X11DRV_WineGL_LoadExtensions(void)
3102 wglExtensions[0] = 0;
3104 /* ARB Extensions */
3106 if (has_extension( glxExtensions, "GLX_ARB_create_context"))
3108 register_extension( "WGL_ARB_create_context" );
3109 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3111 if (has_extension( glxExtensions, "GLX_ARB_create_context_no_error" ))
3112 register_extension( "WGL_ARB_create_context_no_error" );
3113 if (has_extension( glxExtensions, "GLX_ARB_create_context_profile"))
3114 register_extension("WGL_ARB_create_context_profile");
3117 if (has_extension( glxExtensions, "GLX_ARB_fbconfig_float"))
3119 register_extension("WGL_ARB_pixel_format_float");
3120 register_extension("WGL_ATI_pixel_format_float");
3123 register_extension( "WGL_ARB_extensions_string" );
3124 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3126 if (glxRequireVersion(3))
3128 register_extension( "WGL_ARB_make_current_read" );
3129 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3130 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3133 if (has_extension( glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3135 if (glxRequireVersion(3))
3137 register_extension( "WGL_ARB_pbuffer" );
3138 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3139 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3140 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3141 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3142 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3143 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3146 register_extension( "WGL_ARB_pixel_format" );
3147 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3148 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3149 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3151 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3152 if (has_extension( glxExtensions, "GLX_ARB_render_texture") ||
3153 (glxRequireVersion(3) && use_render_texture_emulation))
3155 register_extension( "WGL_ARB_render_texture" );
3156 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3157 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3159 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3160 if (has_extension( glxExtensions, "GLX_NV_float_buffer"))
3161 register_extension("WGL_NV_float_buffer");
3163 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3164 if (has_extension(glExtensions, "GL_NV_texture_rectangle"))
3165 register_extension("WGL_NV_render_texture_rectangle");
3168 /* EXT Extensions */
3170 register_extension( "WGL_EXT_extensions_string" );
3171 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3173 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3174 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3175 register_extension( "WGL_EXT_swap_control" );
3176 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3177 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3179 if (has_extension( glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3180 register_extension("WGL_EXT_framebuffer_sRGB");
3182 if (has_extension( glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3183 register_extension("WGL_EXT_pixel_format_packed_float");
3185 if (has_extension( glxExtensions, "GLX_EXT_swap_control"))
3187 swap_control_method = GLX_SWAP_CONTROL_EXT;
3188 if (has_extension( glxExtensions, "GLX_EXT_swap_control_tear"))
3190 register_extension("WGL_EXT_swap_control_tear");
3191 has_swap_control_tear = TRUE;
3194 else if (has_extension( glxExtensions, "GLX_MESA_swap_control"))
3196 swap_control_method = GLX_SWAP_CONTROL_MESA;
3198 else if (has_extension( glxExtensions, "GLX_SGI_swap_control"))
3200 swap_control_method = GLX_SWAP_CONTROL_SGI;
3203 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3204 if (has_extension(glExtensions, "GL_NV_vertex_array_range"))
3206 register_extension( "WGL_NV_vertex_array_range" );
3207 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3208 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3211 if (has_extension(glxExtensions, "GLX_OML_swap_method"))
3212 has_swap_method = TRUE;
3214 /* WINE-specific WGL Extensions */
3216 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3217 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3219 register_extension( "WGL_WINE_pixel_format_passthrough" );
3220 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3222 if (has_extension( glxExtensions, "GLX_MESA_query_renderer" ))
3224 register_extension( "WGL_WINE_query_renderer" );
3225 opengl_funcs.ext.p_wglQueryCurrentRendererIntegerWINE = X11DRV_wglQueryCurrentRendererIntegerWINE;
3226 opengl_funcs.ext.p_wglQueryCurrentRendererStringWINE = X11DRV_wglQueryCurrentRendererStringWINE;
3227 opengl_funcs.ext.p_wglQueryRendererIntegerWINE = X11DRV_wglQueryRendererIntegerWINE;
3228 opengl_funcs.ext.p_wglQueryRendererStringWINE = X11DRV_wglQueryRendererStringWINE;
3234 * glxdrv_SwapBuffers
3236 * Swap the buffers of this DC
3238 static BOOL WINAPI glxdrv_wglSwapBuffers( HDC hdc )
3240 struct x11drv_escape_flush_gl_drawable escape;
3241 struct gl_drawable *gl;
3242 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3243 INT64 ust, msc, sbc, target_sbc = 0;
3245 TRACE("(%p)\n", hdc);
3247 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3248 escape.gl_drawable = 0;
3249 escape.flush = !pglXWaitForSbcOML;
3251 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3253 SetLastError( ERROR_INVALID_HANDLE );
3254 return FALSE;
3257 EnterCriticalSection( &context_section );
3258 if (gl->refresh_swap_interval)
3260 set_swap_interval(gl->drawable, gl->swap_interval);
3261 gl->refresh_swap_interval = FALSE;
3263 LeaveCriticalSection( &context_section );
3265 switch (gl->type)
3267 case DC_GL_PIXMAP_WIN:
3268 if (ctx) sync_context( ctx );
3269 escape.gl_drawable = gl->pixmap;
3270 if (pglXCopySubBufferMESA) {
3271 /* (glX)SwapBuffers has an implicit glFlush effect, however
3272 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3273 * copying */
3274 pglFlush();
3275 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3276 gl->pixmap_size.cx, gl->pixmap_size.cy );
3277 break;
3279 if (pglXSwapBuffersMscOML)
3281 pglFlush();
3282 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3283 break;
3285 pglXSwapBuffers(gdi_display, gl->drawable);
3286 break;
3287 case DC_GL_WINDOW:
3288 case DC_GL_CHILD_WIN:
3289 if (ctx) sync_context( ctx );
3290 if (gl->type == DC_GL_CHILD_WIN) escape.gl_drawable = gl->window;
3291 /* fall through */
3292 default:
3293 if (escape.gl_drawable && pglXSwapBuffersMscOML)
3295 pglFlush();
3296 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3297 break;
3299 pglXSwapBuffers(gdi_display, gl->drawable);
3300 break;
3303 if (escape.gl_drawable && pglXWaitForSbcOML)
3304 pglXWaitForSbcOML( gdi_display, gl->drawable, target_sbc, &ust, &msc, &sbc );
3306 release_gl_drawable( gl );
3308 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3309 return TRUE;
3312 static struct opengl_funcs opengl_funcs =
3315 glxdrv_wglCopyContext, /* p_wglCopyContext */
3316 glxdrv_wglCreateContext, /* p_wglCreateContext */
3317 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3318 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3319 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3320 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3321 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3322 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3323 glxdrv_wglShareLists, /* p_wglShareLists */
3324 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3328 struct opengl_funcs *get_glx_driver( UINT version )
3330 if (version != WINE_WGL_DRIVER_VERSION)
3332 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3333 return NULL;
3335 if (has_opengl()) return &opengl_funcs;
3336 return NULL;
3339 #else /* no OpenGL includes */
3341 struct opengl_funcs *get_glx_driver( UINT version )
3343 return NULL;
3346 void sync_gl_drawable( HWND hwnd, BOOL known_child )
3350 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3354 void destroy_gl_drawable( HWND hwnd )
3358 #endif /* defined(SONAME_LIBGL) */