dbghelp: Use CONTAINING_RECORD instead of reimplementing it.
[wine.git] / dlls / winex11.drv / opengl.c
blob704d62a384efd162d2b28141565ee534eac28bbd
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/library.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
48 #ifdef SONAME_LIBGL
50 WINE_DECLARE_DEBUG_CHANNEL(winediag);
52 #include "wine/wgl.h"
53 #include "wine/wgl_driver.h"
54 #include "wine/wglext.h"
56 typedef struct __GLXcontextRec *GLXContext;
57 typedef struct __GLXFBConfigRec *GLXFBConfig;
58 typedef XID GLXPixmap;
59 typedef XID GLXDrawable;
60 typedef XID GLXFBConfigID;
61 typedef XID GLXContextID;
62 typedef XID GLXWindow;
63 typedef XID GLXPbuffer;
65 #define GLX_USE_GL 1
66 #define GLX_BUFFER_SIZE 2
67 #define GLX_LEVEL 3
68 #define GLX_RGBA 4
69 #define GLX_DOUBLEBUFFER 5
70 #define GLX_STEREO 6
71 #define GLX_AUX_BUFFERS 7
72 #define GLX_RED_SIZE 8
73 #define GLX_GREEN_SIZE 9
74 #define GLX_BLUE_SIZE 10
75 #define GLX_ALPHA_SIZE 11
76 #define GLX_DEPTH_SIZE 12
77 #define GLX_STENCIL_SIZE 13
78 #define GLX_ACCUM_RED_SIZE 14
79 #define GLX_ACCUM_GREEN_SIZE 15
80 #define GLX_ACCUM_BLUE_SIZE 16
81 #define GLX_ACCUM_ALPHA_SIZE 17
83 #define GLX_BAD_SCREEN 1
84 #define GLX_BAD_ATTRIBUTE 2
85 #define GLX_NO_EXTENSION 3
86 #define GLX_BAD_VISUAL 4
87 #define GLX_BAD_CONTEXT 5
88 #define GLX_BAD_VALUE 6
89 #define GLX_BAD_ENUM 7
91 #define GLX_VENDOR 1
92 #define GLX_VERSION 2
93 #define GLX_EXTENSIONS 3
95 #define GLX_CONFIG_CAVEAT 0x20
96 #define GLX_DONT_CARE 0xFFFFFFFF
97 #define GLX_X_VISUAL_TYPE 0x22
98 #define GLX_TRANSPARENT_TYPE 0x23
99 #define GLX_TRANSPARENT_INDEX_VALUE 0x24
100 #define GLX_TRANSPARENT_RED_VALUE 0x25
101 #define GLX_TRANSPARENT_GREEN_VALUE 0x26
102 #define GLX_TRANSPARENT_BLUE_VALUE 0x27
103 #define GLX_TRANSPARENT_ALPHA_VALUE 0x28
104 #define GLX_WINDOW_BIT 0x00000001
105 #define GLX_PIXMAP_BIT 0x00000002
106 #define GLX_PBUFFER_BIT 0x00000004
107 #define GLX_AUX_BUFFERS_BIT 0x00000010
108 #define GLX_FRONT_LEFT_BUFFER_BIT 0x00000001
109 #define GLX_FRONT_RIGHT_BUFFER_BIT 0x00000002
110 #define GLX_BACK_LEFT_BUFFER_BIT 0x00000004
111 #define GLX_BACK_RIGHT_BUFFER_BIT 0x00000008
112 #define GLX_DEPTH_BUFFER_BIT 0x00000020
113 #define GLX_STENCIL_BUFFER_BIT 0x00000040
114 #define GLX_ACCUM_BUFFER_BIT 0x00000080
115 #define GLX_NONE 0x8000
116 #define GLX_SLOW_CONFIG 0x8001
117 #define GLX_TRUE_COLOR 0x8002
118 #define GLX_DIRECT_COLOR 0x8003
119 #define GLX_PSEUDO_COLOR 0x8004
120 #define GLX_STATIC_COLOR 0x8005
121 #define GLX_GRAY_SCALE 0x8006
122 #define GLX_STATIC_GRAY 0x8007
123 #define GLX_TRANSPARENT_RGB 0x8008
124 #define GLX_TRANSPARENT_INDEX 0x8009
125 #define GLX_VISUAL_ID 0x800B
126 #define GLX_SCREEN 0x800C
127 #define GLX_NON_CONFORMANT_CONFIG 0x800D
128 #define GLX_DRAWABLE_TYPE 0x8010
129 #define GLX_RENDER_TYPE 0x8011
130 #define GLX_X_RENDERABLE 0x8012
131 #define GLX_FBCONFIG_ID 0x8013
132 #define GLX_RGBA_TYPE 0x8014
133 #define GLX_COLOR_INDEX_TYPE 0x8015
134 #define GLX_MAX_PBUFFER_WIDTH 0x8016
135 #define GLX_MAX_PBUFFER_HEIGHT 0x8017
136 #define GLX_MAX_PBUFFER_PIXELS 0x8018
137 #define GLX_PRESERVED_CONTENTS 0x801B
138 #define GLX_LARGEST_PBUFFER 0x801C
139 #define GLX_WIDTH 0x801D
140 #define GLX_HEIGHT 0x801E
141 #define GLX_EVENT_MASK 0x801F
142 #define GLX_DAMAGED 0x8020
143 #define GLX_SAVED 0x8021
144 #define GLX_WINDOW 0x8022
145 #define GLX_PBUFFER 0x8023
146 #define GLX_PBUFFER_HEIGHT 0x8040
147 #define GLX_PBUFFER_WIDTH 0x8041
148 #define GLX_SWAP_METHOD_OML 0x8060
149 #define GLX_SWAP_EXCHANGE_OML 0x8061
150 #define GLX_SWAP_COPY_OML 0x8062
151 #define GLX_SWAP_UNDEFINED_OML 0x8063
152 #define GLX_RGBA_BIT 0x00000001
153 #define GLX_COLOR_INDEX_BIT 0x00000002
154 #define GLX_PBUFFER_CLOBBER_MASK 0x08000000
156 /** GLX_ARB_multisample */
157 #define GLX_SAMPLE_BUFFERS_ARB 100000
158 #define GLX_SAMPLES_ARB 100001
159 /** GLX_ARB_framebuffer_sRGB */
160 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
161 /** GLX_EXT_fbconfig_packed_float */
162 #define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
163 #define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT 0x00000008
164 /** GLX_ARB_create_context */
165 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
166 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
167 #define GLX_CONTEXT_FLAGS_ARB 0x2094
168 /** GLX_ARB_create_context_profile */
169 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
170 /** GLX_ATI_pixel_format_float */
171 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
172 /** GLX_ARB_pixel_format_float */
173 #define GLX_RGBA_FLOAT_BIT 0x00000004
174 #define GLX_RGBA_FLOAT_TYPE 0x20B9
175 /** GLX_NV_float_buffer */
176 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
179 struct WineGLInfo {
180 const char *glVersion;
181 char *glExtensions;
183 int glxVersion[2];
185 const char *glxServerVersion;
186 const char *glxServerVendor;
187 const char *glxServerExtensions;
189 const char *glxClientVersion;
190 const char *glxClientVendor;
191 const char *glxClientExtensions;
193 const char *glxExtensions;
195 BOOL glxDirect;
196 char wglExtensions[4096];
199 struct wgl_pixel_format
201 GLXFBConfig fbconfig;
202 int fmt_id;
203 int render_type;
204 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
207 struct wgl_context
209 HDC hdc;
210 BOOL has_been_current;
211 BOOL sharing;
212 BOOL gl3_context;
213 XVisualInfo *vis;
214 const struct wgl_pixel_format *fmt;
215 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
216 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
217 GLXContext ctx;
218 Drawable drawables[2];
219 BOOL refresh_drawables;
220 struct list entry;
223 struct wgl_pbuffer
225 Drawable drawable;
226 const struct wgl_pixel_format* fmt;
227 int width;
228 int height;
229 int* attribList;
230 int use_render_texture; /* This is also the internal texture format */
231 int texture_bind_target;
232 int texture_bpp;
233 GLint texture_format;
234 GLuint texture_target;
235 GLenum texture_type;
236 GLuint texture;
237 int texture_level;
240 enum dc_gl_type
242 DC_GL_NONE, /* no GL support (pixel format not set yet) */
243 DC_GL_WINDOW, /* normal top-level window */
244 DC_GL_CHILD_WIN, /* child window using XComposite */
245 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
246 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
249 struct gl_drawable
251 enum dc_gl_type type; /* type of GL surface */
252 Drawable drawable; /* drawable for rendering to the client area */
253 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
254 Colormap colormap; /* colormap used for the drawable */
255 const struct wgl_pixel_format *format; /* pixel format for the drawable */
256 XVisualInfo *visual; /* information about the GL visual */
257 RECT rect; /* drawable rect, relative to whole window drawable */
258 int swap_interval;
259 BOOL refresh_swap_interval;
262 enum glx_swap_control_method
264 GLX_SWAP_CONTROL_NONE,
265 GLX_SWAP_CONTROL_EXT,
266 GLX_SWAP_CONTROL_SGI,
267 GLX_SWAP_CONTROL_MESA
270 /* X context to associate a struct gl_drawable to an hwnd */
271 static XContext gl_hwnd_context;
272 /* X context to associate a struct gl_drawable to a pbuffer hdc */
273 static XContext gl_pbuffer_context;
275 static struct list context_list = LIST_INIT( context_list );
276 static struct WineGLInfo WineGLInfo = { 0 };
277 static struct wgl_pixel_format *pixel_formats;
278 static int nb_pixel_formats, nb_onscreen_formats;
279 static BOOL use_render_texture_emulation = TRUE;
281 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
282 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
283 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
284 static BOOL has_swap_control_tear = FALSE;
285 static BOOL has_swap_method = FALSE;
287 static CRITICAL_SECTION context_section;
288 static CRITICAL_SECTION_DEBUG critsect_debug =
290 0, 0, &context_section,
291 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
292 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
294 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
296 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
298 static struct opengl_funcs opengl_funcs;
300 #define USE_GL_FUNC(name) #name,
301 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
302 #undef USE_GL_FUNC
304 static void X11DRV_WineGL_LoadExtensions(void);
305 static void init_pixel_formats( Display *display );
306 static BOOL glxRequireVersion(int requiredVersion);
308 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
309 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
310 TRACE(" - dwFlags : ");
311 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
312 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
313 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
315 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
316 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
318 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
322 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
323 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
324 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
325 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
326 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
327 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
328 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
329 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
330 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
331 #undef TEST_AND_DUMP
332 TRACE("\n");
334 TRACE(" - iPixelType : ");
335 switch (ppfd->iPixelType) {
336 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
337 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
339 TRACE("\n");
341 TRACE(" - Color : %d\n", ppfd->cColorBits);
342 TRACE(" - Red : %d\n", ppfd->cRedBits);
343 TRACE(" - Green : %d\n", ppfd->cGreenBits);
344 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
345 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
346 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
347 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
348 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
349 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
351 TRACE(" - iLayerType : ");
352 switch (ppfd->iLayerType) {
353 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
354 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
355 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
357 TRACE("\n");
360 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
361 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
363 /* GLX 1.0 */
364 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
365 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
366 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
367 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
368 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
369 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
370 static GLXPixmap (*pglXCreateGLXPixmap)( Display *dpy, XVisualInfo *visual, Pixmap pixmap );
371 static void (*pglXDestroyGLXPixmap)( Display *dpy, GLXPixmap pixmap );
372 static Bool (*pglXQueryExtension)( Display *dpy, int *errorb, int *event );
373 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
374 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
375 static int (*pglXGetConfig)( Display *dpy, XVisualInfo *visual, int attrib, int *value );
376 static GLXContext (*pglXGetCurrentContext)( void );
377 static GLXDrawable (*pglXGetCurrentDrawable)( void );
379 /* GLX 1.1 */
380 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
381 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
382 static const char *(*pglXGetClientString)( Display *dpy, int name );
384 /* GLX 1.3 */
385 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
386 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
387 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
388 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
389 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
390 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
391 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
392 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
393 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
395 /* GLX Extensions */
396 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
397 static void* (*pglXGetProcAddressARB)(const GLubyte *);
398 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
399 static int (*pglXSwapIntervalSGI)(int);
401 /* NV GLX Extension */
402 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
403 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
405 /* MESA GLX Extensions */
406 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
407 static int (*pglXSwapIntervalMESA)(unsigned int interval);
409 /* Standard OpenGL */
410 static void (*pglFinish)(void);
411 static void (*pglFlush)(void);
412 static const GLubyte *(*pglGetString)(GLenum name);
414 static void wglFinish(void);
415 static void wglFlush(void);
416 static const GLubyte *wglGetString(GLenum name);
418 /* check if the extension is present in the list */
419 static BOOL has_extension( const char *list, const char *ext )
421 size_t len = strlen( ext );
423 while (list)
425 while (*list == ' ') list++;
426 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
427 list = strchr( list, ' ' );
429 return FALSE;
432 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
434 /* In the future we might want to find the exact X or GLX error to report back to the app */
435 return 1;
438 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
440 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
442 int screen = DefaultScreen(gdi_display);
443 Window win = 0, root = 0;
444 const char *gl_renderer;
445 const char* str;
446 XVisualInfo *vis;
447 GLXContext ctx = NULL;
448 XSetWindowAttributes attr;
449 BOOL ret = FALSE;
450 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
452 attr.override_redirect = True;
453 attr.colormap = None;
454 attr.border_pixel = 0;
456 vis = pglXChooseVisual(gdi_display, screen, attribList);
457 if (vis) {
458 #ifdef __i386__
459 WORD old_fs = wine_get_fs();
460 /* Create a GLX Context. Without one we can't query GL information */
461 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
462 if (wine_get_fs() != old_fs)
464 wine_set_fs( old_fs );
465 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
466 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
467 goto done;
469 #else
470 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
471 #endif
473 if (!ctx) goto done;
475 root = RootWindow( gdi_display, vis->screen );
476 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
477 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
478 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
479 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
480 XMapWindow( gdi_display, win );
481 else
482 win = root;
484 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
486 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
487 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
488 goto done;
490 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
491 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
492 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
493 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
494 strcpy(WineGLInfo.glExtensions, str);
495 strcat(WineGLInfo.glExtensions, legacy_extensions);
497 /* Get the common GLX version supported by GLX client and server ( major/minor) */
498 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
500 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
501 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
502 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
504 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
505 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
506 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
508 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
509 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
511 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
512 TRACE("GL renderer : %s.\n", gl_renderer);
513 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
514 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
515 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
516 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
517 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
518 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
520 if(!WineGLInfo.glxDirect)
522 int fd = ConnectionNumber(gdi_display);
523 struct sockaddr_un uaddr;
524 unsigned int uaddrlen = sizeof(struct sockaddr_un);
526 /* In general indirect rendering on a local X11 server indicates a driver problem.
527 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
529 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
530 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
531 "haven't been installed correctly (using GL renderer %s, version %s).\n",
532 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
533 debugstr_a(WineGLInfo.glVersion));
535 else
537 /* In general you would expect that if direct rendering is returned, that you receive hardware
538 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
539 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
540 * software rendering, it shows direct rendering.
542 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
543 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
544 * it shows 'Mesa X11'.
546 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
547 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
548 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
549 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
550 debugstr_a(WineGLInfo.glVersion));
552 ret = TRUE;
554 done:
555 if(vis) XFree(vis);
556 if(ctx) {
557 pglXMakeCurrent(gdi_display, None, NULL);
558 pglXDestroyContext(gdi_display, ctx);
560 if (win != root) XDestroyWindow( gdi_display, win );
561 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
562 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
563 return ret;
566 static BOOL has_opengl(void)
568 static BOOL init_done = FALSE;
569 static void *opengl_handle;
571 char buffer[200];
572 int error_base, event_base;
573 unsigned int i;
575 if (init_done) return (opengl_handle != NULL);
576 init_done = TRUE;
578 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
579 and include all dependencies */
580 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
581 if (opengl_handle == NULL)
583 ERR( "Failed to load libGL: %s\n", buffer );
584 ERR( "OpenGL support is disabled.\n");
585 return FALSE;
588 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
590 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
592 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
593 goto failed;
597 /* redirect some standard OpenGL functions */
598 #define REDIRECT(func) \
599 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
600 REDIRECT( glFinish );
601 REDIRECT( glFlush );
602 REDIRECT( glGetString );
603 #undef REDIRECT
605 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
606 if (pglXGetProcAddressARB == NULL) {
607 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
608 goto failed;
611 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
613 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
614 goto failed; \
615 } while(0)
617 /* GLX 1.0 */
618 LOAD_FUNCPTR(glXChooseVisual);
619 LOAD_FUNCPTR(glXCopyContext);
620 LOAD_FUNCPTR(glXCreateContext);
621 LOAD_FUNCPTR(glXCreateGLXPixmap);
622 LOAD_FUNCPTR(glXGetCurrentContext);
623 LOAD_FUNCPTR(glXGetCurrentDrawable);
624 LOAD_FUNCPTR(glXDestroyContext);
625 LOAD_FUNCPTR(glXDestroyGLXPixmap);
626 LOAD_FUNCPTR(glXGetConfig);
627 LOAD_FUNCPTR(glXIsDirect);
628 LOAD_FUNCPTR(glXMakeCurrent);
629 LOAD_FUNCPTR(glXSwapBuffers);
630 LOAD_FUNCPTR(glXQueryExtension);
631 LOAD_FUNCPTR(glXQueryVersion);
633 /* GLX 1.1 */
634 LOAD_FUNCPTR(glXGetClientString);
635 LOAD_FUNCPTR(glXQueryExtensionsString);
636 LOAD_FUNCPTR(glXQueryServerString);
638 /* GLX 1.3 */
639 LOAD_FUNCPTR(glXCreatePbuffer);
640 LOAD_FUNCPTR(glXCreateNewContext);
641 LOAD_FUNCPTR(glXDestroyPbuffer);
642 LOAD_FUNCPTR(glXMakeContextCurrent);
643 LOAD_FUNCPTR(glXGetFBConfigs);
644 #undef LOAD_FUNCPTR
646 /* It doesn't matter if these fail. They'll only be used if the driver reports
647 the associated extension is available (and if a driver reports the extension
648 is available but fails to provide the functions, it's quite broken) */
649 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
650 /* ARB GLX Extension */
651 LOAD_FUNCPTR(glXCreateContextAttribsARB);
652 /* EXT GLX Extension */
653 LOAD_FUNCPTR(glXSwapIntervalEXT);
654 /* MESA GLX Extension */
655 LOAD_FUNCPTR(glXSwapIntervalMESA);
656 /* SGI GLX Extension */
657 LOAD_FUNCPTR(glXSwapIntervalSGI);
658 /* NV GLX Extension */
659 LOAD_FUNCPTR(glXAllocateMemoryNV);
660 LOAD_FUNCPTR(glXFreeMemoryNV);
661 #undef LOAD_FUNCPTR
663 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
665 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
666 TRACE("GLX is up and running error_base = %d\n", error_base);
667 } else {
668 ERR( "GLX extension is missing, disabling OpenGL.\n" );
669 goto failed;
671 gl_hwnd_context = XUniqueContext();
672 gl_pbuffer_context = XUniqueContext();
674 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
675 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
676 * rendering is used.
678 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
679 * depends on the reported GLX client / server version and on the client / server extension list.
680 * Those don't have to be the same.
682 * In general the server GLX information lists the capabilities in case of indirect rendering.
683 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
684 * available and in that case the client GLX informat can be used.
685 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
686 * in the GLX version/extension list. When a program does this it works for certain for both
687 * direct and indirect rendering.
689 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
690 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
691 * extension list which causes this extension not to be listed. (Wine requires this extension).
692 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
693 * pbuffers is around.
695 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
696 * the ATI drivers and from then on use GLX client information for them.
699 if(glxRequireVersion(3)) {
700 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
701 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
702 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
703 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
704 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
705 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
706 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
707 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
709 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
710 * enable this function when the Xserver understand GLX 1.3 or newer
712 pglXQueryDrawable = NULL;
713 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
714 TRACE("Overriding ATI GLX capabilities!\n");
715 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
716 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
717 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
718 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
720 /* Use client GLX information in case of the ATI drivers. We override the
721 * capabilities over here and not somewhere else as ATI might better their
722 * life in the future. In case they release proper drivers this block of
723 * code won't be called. */
724 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
725 } else {
726 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
729 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
730 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
733 X11DRV_WineGL_LoadExtensions();
734 init_pixel_formats( gdi_display );
735 return TRUE;
737 failed:
738 wine_dlclose(opengl_handle, NULL, 0);
739 opengl_handle = NULL;
740 return FALSE;
743 static const char *debugstr_fbconfig( GLXFBConfig fbconfig )
745 int id, visual, drawable;
747 if (pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_FBCONFIG_ID, &id ))
748 return "*** invalid fbconfig";
749 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_VISUAL_ID, &visual );
750 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &drawable );
751 return wine_dbg_sprintf( "fbconfig %#x visual id %#x drawable type %#x", id, visual, drawable );
754 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
755 int nAttribs = 0;
756 unsigned cur = 0;
757 int pop;
758 int drawattrib = 0;
759 int nvfloatattrib = GLX_DONT_CARE;
760 int pixelattrib = GLX_DONT_CARE;
762 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
763 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
764 while (iWGLAttr && 0 != iWGLAttr[cur]) {
765 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
767 switch (iWGLAttr[cur]) {
768 case WGL_AUX_BUFFERS_ARB:
769 pop = iWGLAttr[++cur];
770 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
771 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
772 break;
773 case WGL_COLOR_BITS_ARB:
774 pop = iWGLAttr[++cur];
775 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
776 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
777 break;
778 case WGL_BLUE_BITS_ARB:
779 pop = iWGLAttr[++cur];
780 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
781 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
782 break;
783 case WGL_RED_BITS_ARB:
784 pop = iWGLAttr[++cur];
785 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
786 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
787 break;
788 case WGL_GREEN_BITS_ARB:
789 pop = iWGLAttr[++cur];
790 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
791 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
792 break;
793 case WGL_ALPHA_BITS_ARB:
794 pop = iWGLAttr[++cur];
795 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
796 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
797 break;
798 case WGL_DEPTH_BITS_ARB:
799 pop = iWGLAttr[++cur];
800 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
801 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
802 break;
803 case WGL_STENCIL_BITS_ARB:
804 pop = iWGLAttr[++cur];
805 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
806 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
807 break;
808 case WGL_DOUBLE_BUFFER_ARB:
809 pop = iWGLAttr[++cur];
810 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
811 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
812 break;
813 case WGL_STEREO_ARB:
814 pop = iWGLAttr[++cur];
815 PUSH2(oGLXAttr, GLX_STEREO, pop);
816 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
817 break;
819 case WGL_PIXEL_TYPE_ARB:
820 pop = iWGLAttr[++cur];
821 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
822 switch (pop) {
823 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
824 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
825 /* 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 */
826 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
827 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
828 default:
829 ERR("unexpected PixelType(%x)\n", pop);
830 pop = 0;
832 break;
834 case WGL_SUPPORT_GDI_ARB:
835 /* This flag is set in a pixel format */
836 pop = iWGLAttr[++cur];
837 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
838 break;
840 case WGL_DRAW_TO_BITMAP_ARB:
841 /* This flag is set in a pixel format */
842 pop = iWGLAttr[++cur];
843 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
844 break;
846 case WGL_DRAW_TO_WINDOW_ARB:
847 pop = iWGLAttr[++cur];
848 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
849 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
850 if (pop) {
851 drawattrib |= GLX_WINDOW_BIT;
853 break;
855 case WGL_DRAW_TO_PBUFFER_ARB:
856 pop = iWGLAttr[++cur];
857 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
858 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
859 if (pop) {
860 drawattrib |= GLX_PBUFFER_BIT;
862 break;
864 case WGL_ACCELERATION_ARB:
865 /* This flag is set in a pixel format */
866 pop = iWGLAttr[++cur];
867 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
868 break;
870 case WGL_SUPPORT_OPENGL_ARB:
871 pop = iWGLAttr[++cur];
872 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
873 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
874 break;
876 case WGL_SWAP_METHOD_ARB:
877 pop = iWGLAttr[++cur];
878 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
879 if (has_swap_method)
881 switch (pop)
883 case WGL_SWAP_EXCHANGE_ARB:
884 pop = GLX_SWAP_EXCHANGE_OML;
885 break;
886 case WGL_SWAP_COPY_ARB:
887 pop = GLX_SWAP_COPY_OML;
888 break;
889 case WGL_SWAP_UNDEFINED_ARB:
890 pop = GLX_SWAP_UNDEFINED_OML;
891 break;
892 default:
893 ERR("Unexpected swap method %#x.\n", pop);
894 pop = GLX_DONT_CARE;
896 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
898 else
900 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
902 break;
904 case WGL_PBUFFER_LARGEST_ARB:
905 pop = iWGLAttr[++cur];
906 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
907 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
908 break;
910 case WGL_SAMPLE_BUFFERS_ARB:
911 pop = iWGLAttr[++cur];
912 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
913 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
914 break;
916 case WGL_SAMPLES_ARB:
917 pop = iWGLAttr[++cur];
918 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
919 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
920 break;
922 case WGL_TEXTURE_FORMAT_ARB:
923 case WGL_TEXTURE_TARGET_ARB:
924 case WGL_MIPMAP_TEXTURE_ARB:
925 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
926 pop = iWGLAttr[++cur];
927 if (NULL == pbuf) {
928 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
930 if (!use_render_texture_emulation) {
931 if (WGL_NO_TEXTURE_ARB != pop) {
932 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
933 return -1; /** error: don't support it */
934 } else {
935 drawattrib |= GLX_PBUFFER_BIT;
938 break ;
939 case WGL_FLOAT_COMPONENTS_NV:
940 nvfloatattrib = iWGLAttr[++cur];
941 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
942 break ;
943 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
944 case WGL_BIND_TO_TEXTURE_RGB_ARB:
945 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
946 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
947 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
948 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
949 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
950 pop = iWGLAttr[++cur];
951 /** cannot be converted, see direct handling on
952 * - wglGetPixelFormatAttribivARB
953 * TODO: wglChoosePixelFormat
955 break ;
956 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
957 pop = iWGLAttr[++cur];
958 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
959 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
960 break ;
962 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
963 pop = iWGLAttr[++cur];
964 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
965 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
966 break ;
967 default:
968 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
969 cur++;
970 break;
972 ++cur;
975 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
976 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
977 * pixmap and pbuffer formats appear as well. */
978 if (!drawattrib) drawattrib = GLX_DONT_CARE;
979 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
980 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
982 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
983 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
984 * GLX_DONT_CARE unless it is overridden. */
985 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
986 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
988 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
989 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
990 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
991 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
994 return nAttribs;
997 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
999 int render_type, render_type_bit;
1000 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
1001 switch(render_type_bit)
1003 case GLX_RGBA_BIT:
1004 render_type = GLX_RGBA_TYPE;
1005 break;
1006 case GLX_COLOR_INDEX_BIT:
1007 render_type = GLX_COLOR_INDEX_TYPE;
1008 break;
1009 case GLX_RGBA_FLOAT_BIT:
1010 render_type = GLX_RGBA_FLOAT_TYPE;
1011 break;
1012 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
1013 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
1014 break;
1015 default:
1016 ERR("Unknown render_type: %x\n", render_type_bit);
1017 render_type = 0;
1019 return render_type;
1022 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
1023 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
1025 int dbuf, value;
1026 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
1027 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1029 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1030 * the GLX_PIXMAP_BIT set. */
1031 return !dbuf && (value & GLX_PIXMAP_BIT);
1034 static void init_pixel_formats( Display *display )
1036 struct wgl_pixel_format *list;
1037 int size = 0, onscreen_size = 0;
1038 int fmt_id, nCfgs, i, run, bmp_formats;
1039 GLXFBConfig* cfgs;
1040 XVisualInfo *visinfo;
1042 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1043 if (NULL == cfgs || 0 == nCfgs) {
1044 if(cfgs != NULL) XFree(cfgs);
1045 ERR("glXChooseFBConfig returns NULL\n");
1046 return;
1049 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1050 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1051 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1052 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1054 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1056 for(i=0, bmp_formats=0; i<nCfgs; i++)
1058 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1059 bmp_formats++;
1061 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1063 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1065 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1066 * Do this as GLX doesn't guarantee that the list is sorted */
1067 for(run=0; run < 2; run++)
1069 for(i=0; i<nCfgs; i++) {
1070 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1071 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1073 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1074 * The second run we only set offscreen formats. */
1075 if(!run && visinfo)
1077 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1078 * The contents is copied to the destination using XCopyArea. For the copying to work
1079 * the depth of the source and destination window should be the same. In general this should
1080 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1081 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1082 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1083 * list formats with the same depth. */
1084 if(visinfo->depth != default_visual.depth)
1086 XFree(visinfo);
1087 continue;
1090 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1091 list[size].fbconfig = cfgs[i];
1092 list[size].fmt_id = fmt_id;
1093 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1094 list[size].dwFlags = 0;
1095 size++;
1096 onscreen_size++;
1098 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1099 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1101 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1102 list[size].fbconfig = cfgs[i];
1103 list[size].fmt_id = fmt_id;
1104 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1105 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1106 size++;
1107 onscreen_size++;
1109 } else if(run && !visinfo) {
1110 int window_drawable=0;
1111 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1113 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1114 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1115 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1116 * likely make our child window opengl rendering more complicated since likely you can't use
1117 * XCopyArea on a GLX Window.
1118 * For now ignore fbconfigs which are window drawable but lack a visual. */
1119 if(window_drawable & GLX_WINDOW_BIT)
1121 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1122 continue;
1125 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1126 list[size].fbconfig = cfgs[i];
1127 list[size].fmt_id = fmt_id;
1128 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1129 list[size].dwFlags = 0;
1130 size++;
1133 if (visinfo) XFree(visinfo);
1137 XFree(cfgs);
1139 pixel_formats = list;
1140 nb_pixel_formats = size;
1141 nb_onscreen_formats = onscreen_size;
1144 static inline BOOL is_valid_pixel_format( int format )
1146 return format > 0 && format <= nb_pixel_formats;
1149 static inline BOOL is_onscreen_pixel_format( int format )
1151 return format > 0 && format <= nb_onscreen_formats;
1154 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1156 return format - pixel_formats + 1;
1159 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1160 * In our WGL implementation we only support a subset of these formats namely the format of
1161 * Wine's main visual and offscreen formats (if they are available).
1162 * This function converts a WGL format to its corresponding GLX one.
1164 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1166 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1167 * iPixelFormat in case of probing the number of pixelformats.
1169 if (is_valid_pixel_format( iPixelFormat ) &&
1170 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1171 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1172 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1173 return &pixel_formats[iPixelFormat-1];
1175 return NULL;
1178 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1179 static void mark_drawable_dirty(Drawable old, Drawable new)
1181 struct wgl_context *ctx;
1183 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1185 if (old == ctx->drawables[0]) {
1186 ctx->drawables[0] = new;
1187 ctx->refresh_drawables = TRUE;
1189 if (old == ctx->drawables[1]) {
1190 ctx->drawables[1] = new;
1191 ctx->refresh_drawables = TRUE;
1196 /* Given the current context, make sure its drawable is sync'd */
1197 static inline void sync_context(struct wgl_context *context)
1199 if (context->refresh_drawables) {
1200 if (glxRequireVersion(3))
1201 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1202 context->drawables[1], context->ctx);
1203 else
1204 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1205 context->refresh_drawables = FALSE;
1209 static BOOL set_swap_interval(Drawable drawable, int interval)
1211 BOOL ret = TRUE;
1213 switch (swap_control_method)
1215 case GLX_SWAP_CONTROL_EXT:
1216 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1217 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1218 XSync(gdi_display, False);
1219 ret = !X11DRV_check_error();
1220 break;
1222 case GLX_SWAP_CONTROL_MESA:
1223 ret = !pglXSwapIntervalMESA(interval);
1224 break;
1226 case GLX_SWAP_CONTROL_SGI:
1227 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1228 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1229 * value to be an error. Just silently ignore the request for now.
1231 if (!interval)
1232 WARN("Request to disable vertical sync is not handled\n");
1233 else
1234 ret = !pglXSwapIntervalSGI(interval);
1235 break;
1237 case GLX_SWAP_CONTROL_NONE:
1238 /* Unlikely to happen on modern GLX implementations */
1239 WARN("Request to adjust swap interval is not handled\n");
1240 break;
1243 return ret;
1246 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1248 struct gl_drawable *gl;
1250 EnterCriticalSection( &context_section );
1251 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1252 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1253 LeaveCriticalSection( &context_section );
1254 return NULL;
1257 static void release_gl_drawable( struct gl_drawable *gl )
1259 if (gl) LeaveCriticalSection( &context_section );
1262 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1264 GLXContext ctx;
1266 if(context->gl3_context)
1268 if(context->numAttribs)
1269 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, context->attribList);
1270 else
1271 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, NULL);
1273 else if(context->vis)
1274 ctx = pglXCreateContext(gdi_display, context->vis, shareList, GL_TRUE);
1275 else /* Create a GLX Context for a pbuffer */
1276 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1278 return ctx;
1282 /***********************************************************************
1283 * free_gl_drawable
1285 static void free_gl_drawable( struct gl_drawable *gl )
1287 switch (gl->type)
1289 case DC_GL_CHILD_WIN:
1290 XDestroyWindow( gdi_display, gl->drawable );
1291 XFreeColormap( gdi_display, gl->colormap );
1292 break;
1293 case DC_GL_PIXMAP_WIN:
1294 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1295 XFreePixmap( gdi_display, gl->pixmap );
1296 break;
1297 default:
1298 break;
1300 if (gl->visual) XFree( gl->visual );
1301 HeapFree( GetProcessHeap(), 0, gl );
1305 /***********************************************************************
1306 * create_gl_drawable
1308 static BOOL create_gl_drawable( HWND hwnd, struct gl_drawable *gl )
1310 gl->drawable = 0;
1312 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1314 struct x11drv_win_data *data = get_win_data( hwnd );
1316 if (data)
1318 gl->type = DC_GL_WINDOW;
1319 gl->drawable = create_client_window( data, gl->visual );
1320 release_win_data( data );
1323 #ifdef SONAME_LIBXCOMPOSITE
1324 else if(usexcomposite)
1326 static Window dummy_parent;
1327 XSetWindowAttributes attrib;
1329 attrib.override_redirect = True;
1330 if (!dummy_parent)
1332 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1333 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1334 XMapWindow( gdi_display, dummy_parent );
1336 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1337 (gl->visual->class == PseudoColor ||
1338 gl->visual->class == GrayScale ||
1339 gl->visual->class == DirectColor) ?
1340 AllocAll : AllocNone);
1341 attrib.colormap = gl->colormap;
1342 XInstallColormap(gdi_display, attrib.colormap);
1344 gl->type = DC_GL_CHILD_WIN;
1345 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1346 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1347 0, gl->visual->depth, InputOutput, gl->visual->visual,
1348 CWColormap | CWOverrideRedirect, &attrib );
1349 if (gl->drawable)
1351 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1352 XMapWindow(gdi_display, gl->drawable);
1354 else XFreeColormap( gdi_display, gl->colormap );
1356 #endif
1357 else
1359 WARN("XComposite is not available, using GLXPixmap hack\n");
1361 gl->type = DC_GL_PIXMAP_WIN;
1362 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1363 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1364 gl->visual->depth );
1365 if (gl->pixmap)
1367 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1368 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1372 if (gl->drawable)
1373 gl->refresh_swap_interval = TRUE;
1374 return gl->drawable != 0;
1378 /***********************************************************************
1379 * set_win_format
1381 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1383 struct gl_drawable *gl, *prev;
1385 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1386 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1387 * there is no way to query it, so we have to store it here.
1389 gl->swap_interval = 1;
1390 gl->refresh_swap_interval = TRUE;
1391 gl->format = format;
1392 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1393 if (!gl->visual)
1395 HeapFree( GetProcessHeap(), 0, gl );
1396 return FALSE;
1399 GetClientRect( hwnd, &gl->rect );
1400 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1401 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1403 if (!create_gl_drawable( hwnd, gl ))
1405 XFree( gl->visual );
1406 HeapFree( GetProcessHeap(), 0, gl );
1407 return FALSE;
1410 TRACE( "created GL drawable %lx for win %p %s\n",
1411 gl->drawable, hwnd, debugstr_fbconfig( format->fbconfig ));
1413 XFlush( gdi_display );
1415 EnterCriticalSection( &context_section );
1416 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1418 gl->swap_interval = prev->swap_interval;
1419 free_gl_drawable( prev );
1421 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1422 LeaveCriticalSection( &context_section );
1424 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1425 return TRUE;
1429 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1431 const struct wgl_pixel_format *fmt;
1432 int value;
1433 HWND hwnd = WindowFromDC( hdc );
1435 TRACE("(%p,%d)\n", hdc, format);
1437 if (!hwnd || hwnd == GetDesktopWindow())
1439 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1440 return FALSE;
1443 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1444 if (!fmt)
1446 ERR( "Invalid format %d\n", format );
1447 return FALSE;
1450 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1451 if (!(value & GLX_WINDOW_BIT))
1453 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1454 return FALSE;
1457 if (!allow_change)
1459 struct gl_drawable *gl;
1460 if ((gl = get_gl_drawable( hwnd, hdc )))
1462 int prev = pixel_format_index( gl->format );
1463 release_gl_drawable( gl );
1464 return prev == format; /* cannot change it if already set */
1468 return set_win_format( hwnd, fmt );
1472 /***********************************************************************
1473 * sync_gl_drawable
1475 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1477 struct gl_drawable *gl;
1478 Drawable glxp;
1479 Pixmap pix;
1480 int mask = 0;
1481 XWindowChanges changes;
1483 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1484 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1486 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1488 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1489 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1491 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1493 switch (gl->type)
1495 case DC_GL_CHILD_WIN:
1496 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1497 break;
1498 case DC_GL_PIXMAP_WIN:
1499 if (!mask) break;
1500 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1501 if (!pix) goto done;
1502 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1503 if (!glxp)
1505 XFreePixmap(gdi_display, pix);
1506 goto done;
1508 mark_drawable_dirty(gl->drawable, glxp);
1509 XFlush( gdi_display );
1511 XFreePixmap(gdi_display, gl->pixmap);
1512 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1513 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1515 gl->pixmap = pix;
1516 gl->drawable = glxp;
1517 break;
1518 default:
1519 break;
1521 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1522 done:
1523 release_gl_drawable( gl );
1527 /***********************************************************************
1528 * set_gl_drawable_parent
1530 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1532 struct gl_drawable *gl;
1533 Drawable old_drawable;
1535 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1537 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1539 old_drawable = gl->drawable;
1540 switch (gl->type)
1542 case DC_GL_WINDOW:
1543 break;
1544 case DC_GL_CHILD_WIN:
1545 if (parent != GetDesktopWindow()) goto done;
1546 XDestroyWindow( gdi_display, gl->drawable );
1547 XFreeColormap( gdi_display, gl->colormap );
1548 break;
1549 case DC_GL_PIXMAP_WIN:
1550 if (parent != GetDesktopWindow()) goto done;
1551 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1552 XFreePixmap( gdi_display, gl->pixmap );
1553 break;
1554 default:
1555 goto done;
1558 if (!create_gl_drawable( hwnd, gl ))
1560 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1561 release_gl_drawable( gl );
1562 XFree( gl->visual );
1563 HeapFree( GetProcessHeap(), 0, gl );
1564 __wine_set_pixel_format( hwnd, 0 );
1565 return;
1567 mark_drawable_dirty( old_drawable, gl->drawable );
1569 done:
1570 release_gl_drawable( gl );
1575 /***********************************************************************
1576 * destroy_gl_drawable
1578 void destroy_gl_drawable( HWND hwnd )
1580 struct gl_drawable *gl;
1582 EnterCriticalSection( &context_section );
1583 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1585 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1586 free_gl_drawable( gl );
1588 LeaveCriticalSection( &context_section );
1593 * glxdrv_DescribePixelFormat
1595 * Get the pixel-format descriptor associated to the given id
1597 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1598 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1600 /*XVisualInfo *vis;*/
1601 int value;
1602 int rb,gb,bb,ab;
1603 const struct wgl_pixel_format *fmt;
1605 if (!has_opengl()) return 0;
1607 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1609 if (!ppfd) return nb_onscreen_formats;
1611 /* 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 */
1612 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1613 if (!fmt) {
1614 WARN("unexpected format %d\n", iPixelFormat);
1615 return 0;
1618 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1619 ERR("Wrong structure size !\n");
1620 /* Should set error */
1621 return 0;
1624 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1625 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1626 ppfd->nVersion = 1;
1628 /* These flags are always the same... */
1629 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1630 /* Now the flags extracted from the Visual */
1632 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1633 if(value & GLX_WINDOW_BIT)
1634 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1636 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1637 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1638 * 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
1639 * offered the GDI bit either. */
1640 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1642 /* PFD_GENERIC_FORMAT - gdi software rendering
1643 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1644 * none set - full hardware accelerated by a ICD
1646 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1647 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1649 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1650 if (value) {
1651 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1652 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1654 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1656 /* Pixel type */
1657 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1658 if (value & GLX_RGBA_BIT)
1659 ppfd->iPixelType = PFD_TYPE_RGBA;
1660 else
1661 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1663 /* Color bits */
1664 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1665 ppfd->cColorBits = value;
1667 /* Red, green, blue and alpha bits / shifts */
1668 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1669 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1670 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1671 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1672 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1674 ppfd->cRedBits = rb;
1675 ppfd->cRedShift = gb + bb + ab;
1676 ppfd->cBlueBits = bb;
1677 ppfd->cBlueShift = ab;
1678 ppfd->cGreenBits = gb;
1679 ppfd->cGreenShift = bb + ab;
1680 ppfd->cAlphaBits = ab;
1681 ppfd->cAlphaShift = 0;
1682 } else {
1683 ppfd->cRedBits = 0;
1684 ppfd->cRedShift = 0;
1685 ppfd->cBlueBits = 0;
1686 ppfd->cBlueShift = 0;
1687 ppfd->cGreenBits = 0;
1688 ppfd->cGreenShift = 0;
1689 ppfd->cAlphaBits = 0;
1690 ppfd->cAlphaShift = 0;
1693 /* Accum RGBA bits */
1694 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1695 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1696 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1697 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1699 ppfd->cAccumBits = rb+gb+bb+ab;
1700 ppfd->cAccumRedBits = rb;
1701 ppfd->cAccumGreenBits = gb;
1702 ppfd->cAccumBlueBits = bb;
1703 ppfd->cAccumAlphaBits = ab;
1705 /* Aux bits */
1706 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1707 ppfd->cAuxBuffers = value;
1709 /* Depth bits */
1710 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1711 ppfd->cDepthBits = value;
1713 /* stencil bits */
1714 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1715 ppfd->cStencilBits = value;
1717 ppfd->iLayerType = PFD_MAIN_PLANE;
1719 if (TRACE_ON(wgl)) {
1720 dump_PIXELFORMATDESCRIPTOR(ppfd);
1723 return nb_onscreen_formats;
1726 /***********************************************************************
1727 * glxdrv_wglGetPixelFormat
1729 static int glxdrv_wglGetPixelFormat( HDC hdc )
1731 struct gl_drawable *gl;
1732 int ret = 0;
1734 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1736 ret = pixel_format_index( gl->format );
1737 /* Offscreen formats can't be used with traditional WGL calls.
1738 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1739 if (!is_onscreen_pixel_format( ret )) ret = 1;
1740 release_gl_drawable( gl );
1742 TRACE( "%p -> %d\n", hdc, ret );
1743 return ret;
1746 /***********************************************************************
1747 * glxdrv_wglSetPixelFormat
1749 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1751 return set_pixel_format(hdc, iPixelFormat, FALSE);
1754 /***********************************************************************
1755 * glxdrv_wglCopyContext
1757 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1759 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1761 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1763 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1764 return TRUE;
1767 /***********************************************************************
1768 * glxdrv_wglCreateContext
1770 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1772 struct wgl_context *ret;
1773 struct gl_drawable *gl;
1775 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1777 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1778 return NULL;
1781 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1783 ret->hdc = hdc;
1784 ret->fmt = gl->format;
1785 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1786 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1787 list_add_head( &context_list, &ret->entry );
1789 release_gl_drawable( gl );
1790 TRACE( "%p -> %p\n", hdc, ret );
1791 return ret;
1794 /***********************************************************************
1795 * glxdrv_wglDeleteContext
1797 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1799 TRACE("(%p)\n", ctx);
1801 EnterCriticalSection( &context_section );
1802 list_remove( &ctx->entry );
1803 LeaveCriticalSection( &context_section );
1805 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1806 if (ctx->vis) XFree( ctx->vis );
1807 HeapFree( GetProcessHeap(), 0, ctx );
1810 /***********************************************************************
1811 * glxdrv_wglGetProcAddress
1813 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1815 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1816 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1819 /***********************************************************************
1820 * glxdrv_wglMakeCurrent
1822 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1824 BOOL ret = FALSE;
1825 struct gl_drawable *gl;
1827 TRACE("(%p,%p)\n", hdc, ctx);
1829 if (!ctx)
1831 pglXMakeCurrent(gdi_display, None, NULL);
1832 NtCurrentTeb()->glContext = NULL;
1833 return TRUE;
1836 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1838 if (ctx->fmt != gl->format)
1840 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1841 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1842 goto done;
1845 TRACE("hdc %p drawable %lx fmt %p ctx %p %s\n", hdc, gl->drawable, gl->format, ctx->ctx,
1846 debugstr_fbconfig( gl->format->fbconfig ));
1848 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1849 if (ret)
1851 NtCurrentTeb()->glContext = ctx;
1852 ctx->has_been_current = TRUE;
1853 ctx->hdc = hdc;
1854 ctx->drawables[0] = gl->drawable;
1855 ctx->drawables[1] = gl->drawable;
1856 ctx->refresh_drawables = FALSE;
1857 goto done;
1860 SetLastError( ERROR_INVALID_HANDLE );
1862 done:
1863 release_gl_drawable( gl );
1864 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1865 return ret;
1868 /***********************************************************************
1869 * X11DRV_wglMakeContextCurrentARB
1871 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1873 BOOL ret = FALSE;
1874 struct gl_drawable *draw_gl, *read_gl = NULL;
1876 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1878 if (!ctx)
1880 pglXMakeCurrent(gdi_display, None, NULL);
1881 NtCurrentTeb()->glContext = NULL;
1882 return TRUE;
1885 if (!pglXMakeContextCurrent) return FALSE;
1887 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1889 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1890 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1891 read_gl ? read_gl->drawable : 0, ctx->ctx);
1892 if (ret)
1894 ctx->has_been_current = TRUE;
1895 ctx->hdc = draw_hdc;
1896 ctx->drawables[0] = draw_gl->drawable;
1897 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1898 ctx->refresh_drawables = FALSE;
1899 NtCurrentTeb()->glContext = ctx;
1900 goto done;
1903 SetLastError( ERROR_INVALID_HANDLE );
1904 done:
1905 release_gl_drawable( read_gl );
1906 release_gl_drawable( draw_gl );
1907 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1908 return ret;
1911 /***********************************************************************
1912 * glxdrv_wglShareLists
1914 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1916 TRACE("(%p, %p)\n", org, dest);
1918 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1919 * at context creation time but in case of WGL it is done using wglShareLists.
1920 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1921 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1922 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1923 * so there delaying context creation doesn't work.
1925 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1926 * and when a program requests sharing we recreate the destination context if it hasn't been made
1927 * current or when it hasn't shared display lists before.
1930 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1932 ERR("Could not share display lists, one of the contexts has been current already !\n");
1933 return FALSE;
1935 else if(dest->sharing)
1937 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1938 return FALSE;
1940 else
1942 /* Re-create the GLX context and share display lists */
1943 pglXDestroyContext(gdi_display, dest->ctx);
1944 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1945 TRACE(" re-created context (%p) for Wine context %p (%s) sharing lists with ctx %p (%s)\n",
1946 dest->ctx, dest, debugstr_fbconfig(dest->fmt->fbconfig),
1947 org->ctx, debugstr_fbconfig( org->fmt->fbconfig));
1949 org->sharing = TRUE;
1950 dest->sharing = TRUE;
1951 return TRUE;
1953 return FALSE;
1956 static void wglFinish(void)
1958 struct x11drv_escape_flush_gl_drawable escape;
1959 struct gl_drawable *gl;
1960 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1962 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1963 escape.gl_drawable = 0;
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->drawable; break;
1971 default: break;
1973 sync_context(ctx);
1974 release_gl_drawable( gl );
1977 pglFinish();
1978 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1981 static void wglFlush(void)
1983 struct x11drv_escape_flush_gl_drawable escape;
1984 struct gl_drawable *gl;
1985 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1987 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1988 escape.gl_drawable = 0;
1990 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1992 switch (gl->type)
1994 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1995 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1996 default: break;
1998 sync_context(ctx);
1999 release_gl_drawable( gl );
2002 pglFlush();
2003 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2006 static const GLubyte *wglGetString(GLenum name)
2008 if (name == GL_EXTENSIONS && WineGLInfo.glExtensions)
2009 return (const GLubyte *)WineGLInfo.glExtensions;
2010 return pglGetString(name);
2013 /***********************************************************************
2014 * X11DRV_wglCreateContextAttribsARB
2016 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2017 const int* attribList )
2019 struct wgl_context *ret;
2020 struct gl_drawable *gl;
2021 int err = 0;
2023 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2025 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2027 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2028 return NULL;
2031 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2033 ret->hdc = hdc;
2034 ret->fmt = gl->format;
2035 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2036 ret->gl3_context = TRUE;
2037 if (attribList)
2039 int *pContextAttribList = &ret->attribList[0];
2040 /* attribList consists of pairs {token, value] terminated with 0 */
2041 while(attribList[0] != 0)
2043 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2044 switch(attribList[0])
2046 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2047 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2048 pContextAttribList[1] = attribList[1];
2049 pContextAttribList += 2;
2050 ret->numAttribs++;
2051 break;
2052 case WGL_CONTEXT_MINOR_VERSION_ARB:
2053 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2054 pContextAttribList[1] = attribList[1];
2055 pContextAttribList += 2;
2056 ret->numAttribs++;
2057 break;
2058 case WGL_CONTEXT_LAYER_PLANE_ARB:
2059 break;
2060 case WGL_CONTEXT_FLAGS_ARB:
2061 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2062 pContextAttribList[1] = attribList[1];
2063 pContextAttribList += 2;
2064 ret->numAttribs++;
2065 break;
2066 case WGL_CONTEXT_PROFILE_MASK_ARB:
2067 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2068 pContextAttribList[1] = attribList[1];
2069 pContextAttribList += 2;
2070 ret->numAttribs++;
2071 break;
2072 default:
2073 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2075 attribList += 2;
2079 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2080 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2081 XSync(gdi_display, False);
2082 if ((err = X11DRV_check_error()) || !ret->ctx)
2084 /* In the future we should convert the GLX error to a win32 one here if needed */
2085 ERR("Context creation failed (error %x)\n", err);
2086 HeapFree( GetProcessHeap(), 0, ret );
2087 ret = NULL;
2089 else list_add_head( &context_list, &ret->entry );
2092 release_gl_drawable( gl );
2093 TRACE( "%p -> %p\n", hdc, ret );
2094 return ret;
2098 * X11DRV_wglGetExtensionsStringARB
2100 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2102 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2104 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2105 return WineGLInfo.wglExtensions;
2109 * X11DRV_wglCreatePbufferARB
2111 * WGL_ARB_pbuffer: wglCreatePbufferARB
2113 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2114 const int *piAttribList )
2116 struct wgl_pbuffer* object;
2117 const struct wgl_pixel_format *fmt;
2118 int attribs[256];
2119 int nAttribs = 0;
2121 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2123 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2124 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2125 if(!fmt) {
2126 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2127 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2128 return NULL;
2131 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2132 if (NULL == object) {
2133 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2134 return NULL;
2136 object->width = iWidth;
2137 object->height = iHeight;
2138 object->fmt = fmt;
2140 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2141 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2142 while (piAttribList && 0 != *piAttribList) {
2143 int attr_v;
2144 switch (*piAttribList) {
2145 case WGL_PBUFFER_LARGEST_ARB: {
2146 ++piAttribList;
2147 attr_v = *piAttribList;
2148 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2149 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2150 break;
2153 case WGL_TEXTURE_FORMAT_ARB: {
2154 ++piAttribList;
2155 attr_v = *piAttribList;
2156 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2157 if (WGL_NO_TEXTURE_ARB == attr_v) {
2158 object->use_render_texture = 0;
2159 } else {
2160 if (!use_render_texture_emulation) {
2161 SetLastError(ERROR_INVALID_DATA);
2162 goto create_failed;
2164 switch (attr_v) {
2165 case WGL_TEXTURE_RGB_ARB:
2166 object->use_render_texture = GL_RGB;
2167 object->texture_bpp = 3;
2168 object->texture_format = GL_RGB;
2169 object->texture_type = GL_UNSIGNED_BYTE;
2170 break;
2171 case WGL_TEXTURE_RGBA_ARB:
2172 object->use_render_texture = GL_RGBA;
2173 object->texture_bpp = 4;
2174 object->texture_format = GL_RGBA;
2175 object->texture_type = GL_UNSIGNED_BYTE;
2176 break;
2178 /* WGL_FLOAT_COMPONENTS_NV */
2179 case WGL_TEXTURE_FLOAT_R_NV:
2180 object->use_render_texture = GL_FLOAT_R_NV;
2181 object->texture_bpp = 4;
2182 object->texture_format = GL_RED;
2183 object->texture_type = GL_FLOAT;
2184 break;
2185 case WGL_TEXTURE_FLOAT_RG_NV:
2186 object->use_render_texture = GL_FLOAT_RG_NV;
2187 object->texture_bpp = 8;
2188 object->texture_format = GL_LUMINANCE_ALPHA;
2189 object->texture_type = GL_FLOAT;
2190 break;
2191 case WGL_TEXTURE_FLOAT_RGB_NV:
2192 object->use_render_texture = GL_FLOAT_RGB_NV;
2193 object->texture_bpp = 12;
2194 object->texture_format = GL_RGB;
2195 object->texture_type = GL_FLOAT;
2196 break;
2197 case WGL_TEXTURE_FLOAT_RGBA_NV:
2198 object->use_render_texture = GL_FLOAT_RGBA_NV;
2199 object->texture_bpp = 16;
2200 object->texture_format = GL_RGBA;
2201 object->texture_type = GL_FLOAT;
2202 break;
2203 default:
2204 ERR("Unknown texture format: %x\n", attr_v);
2205 SetLastError(ERROR_INVALID_DATA);
2206 goto create_failed;
2209 break;
2212 case WGL_TEXTURE_TARGET_ARB: {
2213 ++piAttribList;
2214 attr_v = *piAttribList;
2215 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2216 if (WGL_NO_TEXTURE_ARB == attr_v) {
2217 object->texture_target = 0;
2218 } else {
2219 if (!use_render_texture_emulation) {
2220 SetLastError(ERROR_INVALID_DATA);
2221 goto create_failed;
2223 switch (attr_v) {
2224 case WGL_TEXTURE_CUBE_MAP_ARB: {
2225 if (iWidth != iHeight) {
2226 SetLastError(ERROR_INVALID_DATA);
2227 goto create_failed;
2229 object->texture_target = GL_TEXTURE_CUBE_MAP;
2230 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2231 break;
2233 case WGL_TEXTURE_1D_ARB: {
2234 if (1 != iHeight) {
2235 SetLastError(ERROR_INVALID_DATA);
2236 goto create_failed;
2238 object->texture_target = GL_TEXTURE_1D;
2239 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2240 break;
2242 case WGL_TEXTURE_2D_ARB: {
2243 object->texture_target = GL_TEXTURE_2D;
2244 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2245 break;
2247 case WGL_TEXTURE_RECTANGLE_NV: {
2248 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2249 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2250 break;
2252 default:
2253 ERR("Unknown texture target: %x\n", attr_v);
2254 SetLastError(ERROR_INVALID_DATA);
2255 goto create_failed;
2258 break;
2261 case WGL_MIPMAP_TEXTURE_ARB: {
2262 ++piAttribList;
2263 attr_v = *piAttribList;
2264 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2265 if (!use_render_texture_emulation) {
2266 SetLastError(ERROR_INVALID_DATA);
2267 goto create_failed;
2269 break;
2272 ++piAttribList;
2275 PUSH1(attribs, None);
2276 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2277 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2278 if (!object->drawable) {
2279 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2280 goto create_failed; /* unexpected error */
2282 TRACE("->(%p)\n", object);
2283 return object;
2285 create_failed:
2286 HeapFree(GetProcessHeap(), 0, object);
2287 TRACE("->(FAILED)\n");
2288 return NULL;
2292 * X11DRV_wglDestroyPbufferARB
2294 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2296 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2298 TRACE("(%p)\n", object);
2300 pglXDestroyPbuffer(gdi_display, object->drawable);
2301 HeapFree(GetProcessHeap(), 0, object);
2302 return GL_TRUE;
2306 * X11DRV_wglGetPbufferDCARB
2308 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2310 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2312 struct x11drv_escape_set_drawable escape;
2313 struct gl_drawable *gl, *prev;
2314 HDC hdc;
2316 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2317 if (!hdc) return 0;
2319 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2321 DeleteDC( hdc );
2322 return 0;
2324 gl->type = DC_GL_PBUFFER;
2325 gl->drawable = object->drawable;
2326 gl->format = object->fmt;
2328 EnterCriticalSection( &context_section );
2329 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2330 free_gl_drawable( prev );
2331 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2332 LeaveCriticalSection( &context_section );
2334 escape.code = X11DRV_SET_DRAWABLE;
2335 escape.drawable = object->drawable;
2336 escape.mode = IncludeInferiors;
2337 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2338 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2340 TRACE( "(%p)->(%p)\n", object, hdc );
2341 return hdc;
2345 * X11DRV_wglQueryPbufferARB
2347 * WGL_ARB_pbuffer: wglQueryPbufferARB
2349 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2351 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2353 switch (iAttribute) {
2354 case WGL_PBUFFER_WIDTH_ARB:
2355 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2356 break;
2357 case WGL_PBUFFER_HEIGHT_ARB:
2358 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2359 break;
2361 case WGL_PBUFFER_LOST_ARB:
2362 /* GLX Pbuffers cannot be lost by default. We can support this by
2363 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2364 * to receive pixel buffer clobber events, however that may or may
2365 * not give any benefit */
2366 *piValue = GL_FALSE;
2367 break;
2369 case WGL_TEXTURE_FORMAT_ARB:
2370 if (!object->use_render_texture) {
2371 *piValue = WGL_NO_TEXTURE_ARB;
2372 } else {
2373 if (!use_render_texture_emulation) {
2374 SetLastError(ERROR_INVALID_HANDLE);
2375 return GL_FALSE;
2377 switch(object->use_render_texture) {
2378 case GL_RGB:
2379 *piValue = WGL_TEXTURE_RGB_ARB;
2380 break;
2381 case GL_RGBA:
2382 *piValue = WGL_TEXTURE_RGBA_ARB;
2383 break;
2384 /* WGL_FLOAT_COMPONENTS_NV */
2385 case GL_FLOAT_R_NV:
2386 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2387 break;
2388 case GL_FLOAT_RG_NV:
2389 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2390 break;
2391 case GL_FLOAT_RGB_NV:
2392 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2393 break;
2394 case GL_FLOAT_RGBA_NV:
2395 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2396 break;
2397 default:
2398 ERR("Unknown texture format: %x\n", object->use_render_texture);
2401 break;
2403 case WGL_TEXTURE_TARGET_ARB:
2404 if (!object->texture_target){
2405 *piValue = WGL_NO_TEXTURE_ARB;
2406 } else {
2407 if (!use_render_texture_emulation) {
2408 SetLastError(ERROR_INVALID_DATA);
2409 return GL_FALSE;
2411 switch (object->texture_target) {
2412 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2413 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2414 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2415 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2418 break;
2420 case WGL_MIPMAP_TEXTURE_ARB:
2421 *piValue = GL_FALSE; /** don't support that */
2422 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2423 break;
2425 default:
2426 FIXME("unexpected attribute %x\n", iAttribute);
2427 break;
2430 return GL_TRUE;
2434 * X11DRV_wglReleasePbufferDCARB
2436 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2438 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2440 struct gl_drawable *gl;
2442 TRACE("(%p, %p)\n", object, hdc);
2444 EnterCriticalSection( &context_section );
2446 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2448 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2449 free_gl_drawable( gl );
2451 else hdc = 0;
2453 LeaveCriticalSection( &context_section );
2455 return hdc && DeleteDC(hdc);
2459 * X11DRV_wglSetPbufferAttribARB
2461 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2463 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2465 GLboolean ret = GL_FALSE;
2467 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2469 if (!object->use_render_texture) {
2470 SetLastError(ERROR_INVALID_HANDLE);
2471 return GL_FALSE;
2473 if (use_render_texture_emulation) {
2474 return GL_TRUE;
2476 return ret;
2480 * X11DRV_wglChoosePixelFormatARB
2482 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2484 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2485 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2487 int attribs[256];
2488 int nAttribs = 0;
2489 GLXFBConfig* cfgs;
2490 int nCfgs = 0;
2491 int it;
2492 int fmt_id;
2493 int start, end;
2494 UINT pfmt_it = 0;
2495 int run;
2496 int i;
2497 DWORD dwFlags = 0;
2499 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2500 if (NULL != pfAttribFList) {
2501 FIXME("unused pfAttribFList\n");
2504 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2505 if (-1 == nAttribs) {
2506 WARN("Cannot convert WGL to GLX attributes\n");
2507 return GL_FALSE;
2509 PUSH1(attribs, None);
2511 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2512 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2513 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2515 for(i=0; piAttribIList[i] != 0; i+=2)
2517 switch(piAttribIList[i])
2519 case WGL_DRAW_TO_BITMAP_ARB:
2520 if(piAttribIList[i+1])
2521 dwFlags |= PFD_DRAW_TO_BITMAP;
2522 break;
2523 case WGL_ACCELERATION_ARB:
2524 switch(piAttribIList[i+1])
2526 case WGL_NO_ACCELERATION_ARB:
2527 dwFlags |= PFD_GENERIC_FORMAT;
2528 break;
2529 case WGL_GENERIC_ACCELERATION_ARB:
2530 dwFlags |= PFD_GENERIC_ACCELERATED;
2531 break;
2532 case WGL_FULL_ACCELERATION_ARB:
2533 /* Nothing to do */
2534 break;
2536 break;
2537 case WGL_SUPPORT_GDI_ARB:
2538 if(piAttribIList[i+1])
2539 dwFlags |= PFD_SUPPORT_GDI;
2540 break;
2544 /* Search for FB configurations matching the requirements in attribs */
2545 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2546 if (NULL == cfgs) {
2547 WARN("Compatible Pixel Format not found\n");
2548 return GL_FALSE;
2551 /* Loop through all matching formats and check if they are suitable.
2552 * Note that this function should at max return nMaxFormats different formats */
2553 for(run=0; run < 2; run++)
2555 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2557 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2559 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2560 continue;
2563 /* During the first run we only want onscreen formats and during the second only offscreen */
2564 start = run == 1 ? nb_onscreen_formats : 0;
2565 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2567 for (i = start; i < end; i++)
2569 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2571 piFormats[pfmt_it++] = i + 1;
2572 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2573 it + 1, nCfgs, fmt_id, i + 1);
2574 break;
2580 *nNumFormats = pfmt_it;
2581 /** free list */
2582 XFree(cfgs);
2583 return GL_TRUE;
2587 * X11DRV_wglGetPixelFormatAttribivARB
2589 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2591 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2592 UINT nAttributes, const int *piAttributes, int *piValues )
2594 UINT i;
2595 const struct wgl_pixel_format *fmt;
2596 int hTest;
2597 int tmp;
2598 int curGLXAttr = 0;
2600 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2602 if (0 < iLayerPlane) {
2603 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2604 return GL_FALSE;
2607 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2608 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2609 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2610 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2611 if(!fmt) {
2612 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2615 for (i = 0; i < nAttributes; ++i) {
2616 const int curWGLAttr = piAttributes[i];
2617 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2619 switch (curWGLAttr) {
2620 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2621 piValues[i] = nb_pixel_formats;
2622 continue;
2624 case WGL_SUPPORT_OPENGL_ARB:
2625 piValues[i] = GL_TRUE;
2626 continue;
2628 case WGL_ACCELERATION_ARB:
2629 curGLXAttr = GLX_CONFIG_CAVEAT;
2630 if (!fmt) goto pix_error;
2631 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2632 piValues[i] = WGL_NO_ACCELERATION_ARB;
2633 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2634 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2635 else
2636 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2637 continue;
2639 case WGL_TRANSPARENT_ARB:
2640 curGLXAttr = GLX_TRANSPARENT_TYPE;
2641 if (!fmt) goto pix_error;
2642 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2643 if (hTest) goto get_error;
2644 piValues[i] = GL_FALSE;
2645 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2646 continue;
2648 case WGL_PIXEL_TYPE_ARB:
2649 curGLXAttr = GLX_RENDER_TYPE;
2650 if (!fmt) goto pix_error;
2651 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2652 if (hTest) goto get_error;
2653 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2654 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2655 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2656 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2657 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2658 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2659 else {
2660 ERR("unexpected RenderType(%x)\n", tmp);
2661 piValues[i] = WGL_TYPE_RGBA_ARB;
2663 continue;
2665 case WGL_COLOR_BITS_ARB:
2666 curGLXAttr = GLX_BUFFER_SIZE;
2667 break;
2669 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2670 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2671 if (!use_render_texture_emulation) {
2672 piValues[i] = GL_FALSE;
2673 continue;
2675 curGLXAttr = GLX_RENDER_TYPE;
2676 if (!fmt) goto pix_error;
2677 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2678 if (hTest) goto get_error;
2679 if (GLX_COLOR_INDEX_BIT == tmp) {
2680 piValues[i] = GL_FALSE;
2681 continue;
2683 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2684 if (hTest) goto get_error;
2685 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2686 continue;
2688 case WGL_BLUE_BITS_ARB:
2689 curGLXAttr = GLX_BLUE_SIZE;
2690 break;
2691 case WGL_RED_BITS_ARB:
2692 curGLXAttr = GLX_RED_SIZE;
2693 break;
2694 case WGL_GREEN_BITS_ARB:
2695 curGLXAttr = GLX_GREEN_SIZE;
2696 break;
2697 case WGL_ALPHA_BITS_ARB:
2698 curGLXAttr = GLX_ALPHA_SIZE;
2699 break;
2700 case WGL_DEPTH_BITS_ARB:
2701 curGLXAttr = GLX_DEPTH_SIZE;
2702 break;
2703 case WGL_STENCIL_BITS_ARB:
2704 curGLXAttr = GLX_STENCIL_SIZE;
2705 break;
2706 case WGL_DOUBLE_BUFFER_ARB:
2707 curGLXAttr = GLX_DOUBLEBUFFER;
2708 break;
2709 case WGL_STEREO_ARB:
2710 curGLXAttr = GLX_STEREO;
2711 break;
2712 case WGL_AUX_BUFFERS_ARB:
2713 curGLXAttr = GLX_AUX_BUFFERS;
2714 break;
2716 case WGL_SUPPORT_GDI_ARB:
2717 if (!fmt) goto pix_error;
2718 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2719 continue;
2721 case WGL_DRAW_TO_BITMAP_ARB:
2722 if (!fmt) goto pix_error;
2723 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2724 continue;
2726 case WGL_DRAW_TO_WINDOW_ARB:
2727 case WGL_DRAW_TO_PBUFFER_ARB:
2728 if (!fmt) goto pix_error;
2729 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2730 if (hTest) goto get_error;
2731 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2732 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2733 piValues[i] = GL_TRUE;
2734 else
2735 piValues[i] = GL_FALSE;
2736 continue;
2738 case WGL_SWAP_METHOD_ARB:
2739 if (has_swap_method)
2741 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_SWAP_METHOD_OML, &tmp);
2742 if (hTest) goto get_error;
2743 switch (tmp)
2745 case GLX_SWAP_EXCHANGE_OML:
2746 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2747 break;
2748 case GLX_SWAP_COPY_OML:
2749 piValues[i] = WGL_SWAP_COPY_ARB;
2750 break;
2751 case GLX_SWAP_UNDEFINED_OML:
2752 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2753 break;
2754 default:
2755 ERR("Unexpected swap method %x.\n", tmp);
2758 else
2760 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2761 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2763 continue;
2765 case WGL_PBUFFER_LARGEST_ARB:
2766 curGLXAttr = GLX_LARGEST_PBUFFER;
2767 break;
2769 case WGL_SAMPLE_BUFFERS_ARB:
2770 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2771 break;
2773 case WGL_SAMPLES_ARB:
2774 curGLXAttr = GLX_SAMPLES_ARB;
2775 break;
2777 case WGL_FLOAT_COMPONENTS_NV:
2778 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2779 break;
2781 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2782 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2783 break;
2785 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2786 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2787 break;
2789 case WGL_ACCUM_RED_BITS_ARB:
2790 curGLXAttr = GLX_ACCUM_RED_SIZE;
2791 break;
2792 case WGL_ACCUM_GREEN_BITS_ARB:
2793 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2794 break;
2795 case WGL_ACCUM_BLUE_BITS_ARB:
2796 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2797 break;
2798 case WGL_ACCUM_ALPHA_BITS_ARB:
2799 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2800 break;
2801 case WGL_ACCUM_BITS_ARB:
2802 if (!fmt) goto pix_error;
2803 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2804 if (hTest) goto get_error;
2805 piValues[i] = tmp;
2806 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2807 if (hTest) goto get_error;
2808 piValues[i] += tmp;
2809 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2810 if (hTest) goto get_error;
2811 piValues[i] += tmp;
2812 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2813 if (hTest) goto get_error;
2814 piValues[i] += tmp;
2815 continue;
2817 default:
2818 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2821 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2822 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2823 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2825 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2826 * and check which options can work using iPixelFormat=0 and which not.
2827 * A problem would be that this function is an extension. This would
2828 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2830 if (0 != curGLXAttr && iPixelFormat != 0) {
2831 if (!fmt) goto pix_error;
2832 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2833 if (hTest) goto get_error;
2834 curGLXAttr = 0;
2835 } else {
2836 piValues[i] = GL_FALSE;
2839 return GL_TRUE;
2841 get_error:
2842 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2843 return GL_FALSE;
2845 pix_error:
2846 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2847 return GL_FALSE;
2851 * X11DRV_wglGetPixelFormatAttribfvARB
2853 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2855 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2856 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2858 int *attr;
2859 int ret;
2860 UINT i;
2862 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2864 /* Allocate a temporary array to store integer values */
2865 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2866 if (!attr) {
2867 ERR("couldn't allocate %d array\n", nAttributes);
2868 return GL_FALSE;
2871 /* Piggy-back on wglGetPixelFormatAttribivARB */
2872 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2873 if (ret) {
2874 /* Convert integer values to float. Should also check for attributes
2875 that can give decimal values here */
2876 for (i=0; i<nAttributes;i++) {
2877 pfValues[i] = attr[i];
2881 HeapFree(GetProcessHeap(), 0, attr);
2882 return ret;
2886 * X11DRV_wglBindTexImageARB
2888 * WGL_ARB_render_texture: wglBindTexImageARB
2890 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2892 GLboolean ret = GL_FALSE;
2894 TRACE("(%p, %d)\n", object, iBuffer);
2896 if (!object->use_render_texture) {
2897 SetLastError(ERROR_INVALID_HANDLE);
2898 return GL_FALSE;
2901 if (use_render_texture_emulation) {
2902 static BOOL initialized = FALSE;
2903 int prev_binded_texture = 0;
2904 GLXContext prev_context;
2905 Drawable prev_drawable;
2906 GLXContext tmp_context;
2908 prev_context = pglXGetCurrentContext();
2909 prev_drawable = pglXGetCurrentDrawable();
2911 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2912 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2913 isn't ideal performance wise but I wasn't able to get other ways working.
2915 if(!initialized) {
2916 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2917 FIXME("partial stub!\n");
2920 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2921 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2923 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2925 /* Switch to our pbuffer */
2926 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2928 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2929 * After that upload the pbuffer texture data. */
2930 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2931 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2933 /* Switch back to the original drawable and upload the pbuffer-texture */
2934 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2935 pglXDestroyContext(gdi_display, tmp_context);
2936 return GL_TRUE;
2939 return ret;
2943 * X11DRV_wglReleaseTexImageARB
2945 * WGL_ARB_render_texture: wglReleaseTexImageARB
2947 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2949 GLboolean ret = GL_FALSE;
2951 TRACE("(%p, %d)\n", object, iBuffer);
2953 if (!object->use_render_texture) {
2954 SetLastError(ERROR_INVALID_HANDLE);
2955 return GL_FALSE;
2957 if (use_render_texture_emulation) {
2958 return GL_TRUE;
2960 return ret;
2964 * X11DRV_wglGetExtensionsStringEXT
2966 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2968 static const char *X11DRV_wglGetExtensionsStringEXT(void)
2970 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2971 return WineGLInfo.wglExtensions;
2975 * X11DRV_wglGetSwapIntervalEXT
2977 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2979 static int X11DRV_wglGetSwapIntervalEXT(void)
2981 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2982 struct gl_drawable *gl;
2983 int swap_interval;
2985 TRACE("()\n");
2987 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
2989 /* This can't happen because a current WGL context is required to get
2990 * here. Likely the application is buggy.
2992 WARN("No GL drawable found, returning swap interval 0\n");
2993 return 0;
2996 swap_interval = gl->swap_interval;
2997 release_gl_drawable(gl);
2999 return swap_interval;
3003 * X11DRV_wglSwapIntervalEXT
3005 * WGL_EXT_swap_control: wglSwapIntervalEXT
3007 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3009 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3010 struct gl_drawable *gl;
3011 BOOL ret;
3013 TRACE("(%d)\n", interval);
3015 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3016 * is invalid.
3018 if (interval < 0 && !has_swap_control_tear)
3020 SetLastError(ERROR_INVALID_DATA);
3021 return FALSE;
3024 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3026 SetLastError(ERROR_DC_NOT_FOUND);
3027 return FALSE;
3030 ret = set_swap_interval(gl->drawable, interval);
3031 gl->refresh_swap_interval = FALSE;
3032 if (ret)
3033 gl->swap_interval = interval;
3034 else
3035 SetLastError(ERROR_DC_NOT_FOUND);
3037 release_gl_drawable(gl);
3039 return ret;
3043 * X11DRV_wglSetPixelFormatWINE
3045 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3046 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3048 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3050 return set_pixel_format(hdc, format, TRUE);
3054 * glxRequireVersion (internal)
3056 * Check if the supported GLX version matches requiredVersion.
3058 static BOOL glxRequireVersion(int requiredVersion)
3060 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3061 if(requiredVersion <= WineGLInfo.glxVersion[1])
3062 return TRUE;
3064 return FALSE;
3067 static void register_extension(const char *ext)
3069 if (WineGLInfo.wglExtensions[0])
3070 strcat(WineGLInfo.wglExtensions, " ");
3071 strcat(WineGLInfo.wglExtensions, ext);
3073 TRACE("'%s'\n", ext);
3077 * X11DRV_WineGL_LoadExtensions
3079 static void X11DRV_WineGL_LoadExtensions(void)
3081 WineGLInfo.wglExtensions[0] = 0;
3083 /* ARB Extensions */
3085 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
3087 register_extension( "WGL_ARB_create_context" );
3088 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3090 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
3091 register_extension("WGL_ARB_create_context_profile");
3094 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
3096 register_extension("WGL_ARB_pixel_format_float");
3097 register_extension("WGL_ATI_pixel_format_float");
3100 register_extension( "WGL_ARB_extensions_string" );
3101 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3103 if (glxRequireVersion(3))
3105 register_extension( "WGL_ARB_make_current_read" );
3106 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3107 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3110 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3112 if (glxRequireVersion(3))
3114 register_extension( "WGL_ARB_pbuffer" );
3115 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3116 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3117 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3118 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3119 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3120 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3123 register_extension( "WGL_ARB_pixel_format" );
3124 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3125 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3126 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3128 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3129 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3130 (glxRequireVersion(3) && use_render_texture_emulation))
3132 register_extension( "WGL_ARB_render_texture" );
3133 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3134 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3136 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3137 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3138 register_extension("WGL_NV_float_buffer");
3140 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3141 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3142 register_extension("WGL_NV_render_texture_rectangle");
3145 /* EXT Extensions */
3147 register_extension( "WGL_EXT_extensions_string" );
3148 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3150 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3151 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3152 register_extension( "WGL_EXT_swap_control" );
3153 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3154 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3156 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3157 register_extension("WGL_EXT_framebuffer_sRGB");
3159 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3160 register_extension("WGL_EXT_pixel_format_packed_float");
3162 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3164 swap_control_method = GLX_SWAP_CONTROL_EXT;
3165 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control_tear"))
3167 register_extension("WGL_EXT_swap_control_tear");
3168 has_swap_control_tear = TRUE;
3171 else if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_swap_control"))
3173 swap_control_method = GLX_SWAP_CONTROL_MESA;
3175 else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGI_swap_control"))
3177 swap_control_method = GLX_SWAP_CONTROL_SGI;
3180 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3181 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3183 register_extension( "WGL_NV_vertex_array_range" );
3184 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3185 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3188 if (has_extension(WineGLInfo.glxExtensions, "GLX_OML_swap_method"))
3189 has_swap_method = TRUE;
3191 /* WINE-specific WGL Extensions */
3193 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3194 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3196 register_extension( "WGL_WINE_pixel_format_passthrough" );
3197 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3202 * glxdrv_SwapBuffers
3204 * Swap the buffers of this DC
3206 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3208 struct x11drv_escape_flush_gl_drawable escape;
3209 struct gl_drawable *gl;
3210 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3212 TRACE("(%p)\n", hdc);
3214 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3215 escape.gl_drawable = 0;
3217 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3219 SetLastError( ERROR_INVALID_HANDLE );
3220 return FALSE;
3223 if (gl->refresh_swap_interval)
3225 set_swap_interval(gl->drawable, gl->swap_interval);
3226 gl->refresh_swap_interval = FALSE;
3229 switch (gl->type)
3231 case DC_GL_PIXMAP_WIN:
3232 if (ctx) sync_context( ctx );
3233 escape.gl_drawable = gl->pixmap;
3234 if (pglXCopySubBufferMESA) {
3235 /* (glX)SwapBuffers has an implicit glFlush effect, however
3236 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3237 * copying */
3238 pglFlush();
3239 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3240 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3241 break;
3243 pglXSwapBuffers(gdi_display, gl->drawable);
3244 break;
3245 case DC_GL_CHILD_WIN:
3246 if (ctx) sync_context( ctx );
3247 escape.gl_drawable = gl->drawable;
3248 /* fall through */
3249 default:
3250 pglXSwapBuffers(gdi_display, gl->drawable);
3251 break;
3254 release_gl_drawable( gl );
3256 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3257 return TRUE;
3260 static struct opengl_funcs opengl_funcs =
3263 glxdrv_wglCopyContext, /* p_wglCopyContext */
3264 glxdrv_wglCreateContext, /* p_wglCreateContext */
3265 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3266 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3267 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3268 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3269 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3270 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3271 glxdrv_wglShareLists, /* p_wglShareLists */
3272 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3276 struct opengl_funcs *get_glx_driver( UINT version )
3278 if (version != WINE_WGL_DRIVER_VERSION)
3280 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3281 return NULL;
3283 if (has_opengl()) return &opengl_funcs;
3284 return NULL;
3287 #else /* no OpenGL includes */
3289 struct opengl_funcs *get_glx_driver( UINT version )
3291 return NULL;
3294 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3298 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3302 void destroy_gl_drawable( HWND hwnd )
3306 #endif /* defined(SONAME_LIBGL) */