winex11.drv: Remove unused code (Clang).
[wine.git] / dlls / winex11.drv / opengl.c
blob7b8ba821004cb1649359b5e27e15ff5096888ee0
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 int describeContext( struct wgl_context *ctx ) {
744 int tmp;
745 int ctx_vis_id;
746 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
747 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
748 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
749 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
750 TRACE(" - VISUAL_ID 0x%x\n", tmp);
751 ctx_vis_id = tmp;
752 return ctx_vis_id;
755 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
756 int nAttribs = 0;
757 unsigned cur = 0;
758 int pop;
759 int drawattrib = 0;
760 int nvfloatattrib = GLX_DONT_CARE;
761 int pixelattrib = GLX_DONT_CARE;
763 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
764 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
765 while (iWGLAttr && 0 != iWGLAttr[cur]) {
766 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
768 switch (iWGLAttr[cur]) {
769 case WGL_AUX_BUFFERS_ARB:
770 pop = iWGLAttr[++cur];
771 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
772 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
773 break;
774 case WGL_COLOR_BITS_ARB:
775 pop = iWGLAttr[++cur];
776 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
777 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
778 break;
779 case WGL_BLUE_BITS_ARB:
780 pop = iWGLAttr[++cur];
781 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
782 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
783 break;
784 case WGL_RED_BITS_ARB:
785 pop = iWGLAttr[++cur];
786 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
787 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
788 break;
789 case WGL_GREEN_BITS_ARB:
790 pop = iWGLAttr[++cur];
791 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
792 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
793 break;
794 case WGL_ALPHA_BITS_ARB:
795 pop = iWGLAttr[++cur];
796 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
797 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
798 break;
799 case WGL_DEPTH_BITS_ARB:
800 pop = iWGLAttr[++cur];
801 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
802 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
803 break;
804 case WGL_STENCIL_BITS_ARB:
805 pop = iWGLAttr[++cur];
806 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
807 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
808 break;
809 case WGL_DOUBLE_BUFFER_ARB:
810 pop = iWGLAttr[++cur];
811 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
812 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
813 break;
814 case WGL_STEREO_ARB:
815 pop = iWGLAttr[++cur];
816 PUSH2(oGLXAttr, GLX_STEREO, pop);
817 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
818 break;
820 case WGL_PIXEL_TYPE_ARB:
821 pop = iWGLAttr[++cur];
822 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
823 switch (pop) {
824 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
825 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
826 /* 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 */
827 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
828 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
829 default:
830 ERR("unexpected PixelType(%x)\n", pop);
831 pop = 0;
833 break;
835 case WGL_SUPPORT_GDI_ARB:
836 /* This flag is set in a pixel format */
837 pop = iWGLAttr[++cur];
838 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
839 break;
841 case WGL_DRAW_TO_BITMAP_ARB:
842 /* This flag is set in a pixel format */
843 pop = iWGLAttr[++cur];
844 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
845 break;
847 case WGL_DRAW_TO_WINDOW_ARB:
848 pop = iWGLAttr[++cur];
849 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
850 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
851 if (pop) {
852 drawattrib |= GLX_WINDOW_BIT;
854 break;
856 case WGL_DRAW_TO_PBUFFER_ARB:
857 pop = iWGLAttr[++cur];
858 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
859 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
860 if (pop) {
861 drawattrib |= GLX_PBUFFER_BIT;
863 break;
865 case WGL_ACCELERATION_ARB:
866 /* This flag is set in a pixel format */
867 pop = iWGLAttr[++cur];
868 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
869 break;
871 case WGL_SUPPORT_OPENGL_ARB:
872 pop = iWGLAttr[++cur];
873 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
874 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
875 break;
877 case WGL_SWAP_METHOD_ARB:
878 pop = iWGLAttr[++cur];
879 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
880 if (has_swap_method)
882 switch (pop)
884 case WGL_SWAP_EXCHANGE_ARB:
885 pop = GLX_SWAP_EXCHANGE_OML;
886 break;
887 case WGL_SWAP_COPY_ARB:
888 pop = GLX_SWAP_COPY_OML;
889 break;
890 case WGL_SWAP_UNDEFINED_ARB:
891 pop = GLX_SWAP_UNDEFINED_OML;
892 break;
893 default:
894 ERR("Unexpected swap method %#x.\n", pop);
895 pop = GLX_DONT_CARE;
897 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
899 else
901 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
903 break;
905 case WGL_PBUFFER_LARGEST_ARB:
906 pop = iWGLAttr[++cur];
907 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
908 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
909 break;
911 case WGL_SAMPLE_BUFFERS_ARB:
912 pop = iWGLAttr[++cur];
913 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
914 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
915 break;
917 case WGL_SAMPLES_ARB:
918 pop = iWGLAttr[++cur];
919 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
920 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
921 break;
923 case WGL_TEXTURE_FORMAT_ARB:
924 case WGL_TEXTURE_TARGET_ARB:
925 case WGL_MIPMAP_TEXTURE_ARB:
926 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
927 pop = iWGLAttr[++cur];
928 if (NULL == pbuf) {
929 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
931 if (!use_render_texture_emulation) {
932 if (WGL_NO_TEXTURE_ARB != pop) {
933 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
934 return -1; /** error: don't support it */
935 } else {
936 drawattrib |= GLX_PBUFFER_BIT;
939 break ;
940 case WGL_FLOAT_COMPONENTS_NV:
941 nvfloatattrib = iWGLAttr[++cur];
942 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
943 break ;
944 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
945 case WGL_BIND_TO_TEXTURE_RGB_ARB:
946 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
947 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
948 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
949 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
950 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
951 pop = iWGLAttr[++cur];
952 /** cannot be converted, see direct handling on
953 * - wglGetPixelFormatAttribivARB
954 * TODO: wglChoosePixelFormat
956 break ;
957 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
958 pop = iWGLAttr[++cur];
959 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
960 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
961 break ;
963 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
964 pop = iWGLAttr[++cur];
965 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
966 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
967 break ;
968 default:
969 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
970 cur++;
971 break;
973 ++cur;
976 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
977 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
978 * pixmap and pbuffer formats appear as well. */
979 if (!drawattrib) drawattrib = GLX_DONT_CARE;
980 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
981 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
983 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
984 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
985 * GLX_DONT_CARE unless it is overridden. */
986 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
987 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
989 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
990 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
991 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
992 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
995 return nAttribs;
998 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
1000 int render_type=0, render_type_bit;
1001 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
1002 switch(render_type_bit)
1004 case GLX_RGBA_BIT:
1005 render_type = GLX_RGBA_TYPE;
1006 break;
1007 case GLX_COLOR_INDEX_BIT:
1008 render_type = GLX_COLOR_INDEX_TYPE;
1009 break;
1010 case GLX_RGBA_FLOAT_BIT:
1011 render_type = GLX_RGBA_FLOAT_TYPE;
1012 break;
1013 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
1014 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
1015 break;
1016 default:
1017 ERR("Unknown render_type: %x\n", render_type_bit);
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 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1267 BOOL indirect = !(context->fmt->dwFlags & PFD_DRAW_TO_BITMAP);
1269 if(context->gl3_context)
1271 if(context->numAttribs)
1272 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1273 else
1274 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1276 else if(context->vis)
1277 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1278 else /* Create a GLX Context for a pbuffer */
1279 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1281 return ctx;
1285 /***********************************************************************
1286 * free_gl_drawable
1288 static void free_gl_drawable( struct gl_drawable *gl )
1290 switch (gl->type)
1292 case DC_GL_CHILD_WIN:
1293 XDestroyWindow( gdi_display, gl->drawable );
1294 XFreeColormap( gdi_display, gl->colormap );
1295 break;
1296 case DC_GL_PIXMAP_WIN:
1297 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1298 XFreePixmap( gdi_display, gl->pixmap );
1299 break;
1300 default:
1301 break;
1303 if (gl->visual) XFree( gl->visual );
1304 HeapFree( GetProcessHeap(), 0, gl );
1308 /***********************************************************************
1309 * create_gl_drawable
1311 static BOOL create_gl_drawable( HWND hwnd, struct gl_drawable *gl )
1313 gl->drawable = 0;
1315 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1317 struct x11drv_win_data *data = get_win_data( hwnd );
1319 if (data)
1321 gl->type = DC_GL_WINDOW;
1322 gl->drawable = create_client_window( data, gl->visual );
1323 release_win_data( data );
1326 #ifdef SONAME_LIBXCOMPOSITE
1327 else if(usexcomposite)
1329 static Window dummy_parent;
1330 XSetWindowAttributes attrib;
1332 attrib.override_redirect = True;
1333 if (!dummy_parent)
1335 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1336 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1337 XMapWindow( gdi_display, dummy_parent );
1339 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1340 (gl->visual->class == PseudoColor ||
1341 gl->visual->class == GrayScale ||
1342 gl->visual->class == DirectColor) ?
1343 AllocAll : AllocNone);
1344 attrib.colormap = gl->colormap;
1345 XInstallColormap(gdi_display, attrib.colormap);
1347 gl->type = DC_GL_CHILD_WIN;
1348 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1349 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1350 0, gl->visual->depth, InputOutput, gl->visual->visual,
1351 CWColormap | CWOverrideRedirect, &attrib );
1352 if (gl->drawable)
1354 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1355 XMapWindow(gdi_display, gl->drawable);
1357 else XFreeColormap( gdi_display, gl->colormap );
1359 #endif
1360 else
1362 WARN("XComposite is not available, using GLXPixmap hack\n");
1364 gl->type = DC_GL_PIXMAP_WIN;
1365 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1366 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1367 gl->visual->depth );
1368 if (gl->pixmap)
1370 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1371 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1375 if (gl->drawable)
1376 gl->refresh_swap_interval = TRUE;
1377 return gl->drawable != 0;
1381 /***********************************************************************
1382 * set_win_format
1384 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1386 struct gl_drawable *gl, *prev;
1388 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1389 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1390 * there is no way to query it, so we have to store it here.
1392 gl->swap_interval = 1;
1393 gl->refresh_swap_interval = TRUE;
1394 gl->format = format;
1395 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1396 if (!gl->visual)
1398 HeapFree( GetProcessHeap(), 0, gl );
1399 return FALSE;
1402 GetClientRect( hwnd, &gl->rect );
1403 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1404 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1406 if (!create_gl_drawable( hwnd, gl ))
1408 XFree( gl->visual );
1409 HeapFree( GetProcessHeap(), 0, gl );
1410 return FALSE;
1413 TRACE("created GL drawable %lx for win %p format %x\n", gl->drawable, hwnd, format->fmt_id );
1415 XFlush( gdi_display );
1417 EnterCriticalSection( &context_section );
1418 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1420 gl->swap_interval = prev->swap_interval;
1421 free_gl_drawable( prev );
1423 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1424 LeaveCriticalSection( &context_section );
1426 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1427 return TRUE;
1431 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1433 const struct wgl_pixel_format *fmt;
1434 int value;
1435 HWND hwnd = WindowFromDC( hdc );
1437 TRACE("(%p,%d)\n", hdc, format);
1439 if (!hwnd || hwnd == GetDesktopWindow())
1441 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1442 return FALSE;
1445 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1446 if (!fmt)
1448 ERR( "Invalid format %d\n", format );
1449 return FALSE;
1452 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1453 if (!(value & GLX_WINDOW_BIT))
1455 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1456 return FALSE;
1459 if (!allow_change)
1461 struct gl_drawable *gl;
1462 if ((gl = get_gl_drawable( hwnd, hdc )))
1464 int prev = pixel_format_index( gl->format );
1465 release_gl_drawable( gl );
1466 return prev == format; /* cannot change it if already set */
1470 if (TRACE_ON(wgl)) {
1471 int gl_test = 0;
1473 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1474 if (gl_test) {
1475 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1476 } else {
1477 TRACE(" FBConfig have :\n");
1478 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1479 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1480 TRACE(" - VISUAL_ID 0x%x\n", value);
1481 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1482 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1486 return set_win_format( hwnd, fmt );
1490 /***********************************************************************
1491 * sync_gl_drawable
1493 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1495 struct gl_drawable *gl;
1496 Drawable glxp;
1497 Pixmap pix;
1498 int mask = 0;
1499 XWindowChanges changes;
1501 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1502 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1504 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1506 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1507 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1509 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1511 switch (gl->type)
1513 case DC_GL_CHILD_WIN:
1514 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1515 break;
1516 case DC_GL_PIXMAP_WIN:
1517 if (!mask) break;
1518 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1519 if (!pix) goto done;
1520 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1521 if (!glxp)
1523 XFreePixmap(gdi_display, pix);
1524 goto done;
1526 mark_drawable_dirty(gl->drawable, glxp);
1527 XFlush( gdi_display );
1529 XFreePixmap(gdi_display, gl->pixmap);
1530 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1531 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1533 gl->pixmap = pix;
1534 gl->drawable = glxp;
1535 break;
1536 default:
1537 break;
1539 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1540 done:
1541 release_gl_drawable( gl );
1545 /***********************************************************************
1546 * set_gl_drawable_parent
1548 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1550 struct gl_drawable *gl;
1551 Drawable old_drawable;
1553 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1555 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1557 old_drawable = gl->drawable;
1558 switch (gl->type)
1560 case DC_GL_WINDOW:
1561 break;
1562 case DC_GL_CHILD_WIN:
1563 if (parent != GetDesktopWindow()) goto done;
1564 XDestroyWindow( gdi_display, gl->drawable );
1565 XFreeColormap( gdi_display, gl->colormap );
1566 break;
1567 case DC_GL_PIXMAP_WIN:
1568 if (parent != GetDesktopWindow()) goto done;
1569 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1570 XFreePixmap( gdi_display, gl->pixmap );
1571 break;
1572 default:
1573 goto done;
1576 if (!create_gl_drawable( hwnd, gl ))
1578 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1579 release_gl_drawable( gl );
1580 XFree( gl->visual );
1581 HeapFree( GetProcessHeap(), 0, gl );
1582 __wine_set_pixel_format( hwnd, 0 );
1583 return;
1585 mark_drawable_dirty( old_drawable, gl->drawable );
1587 done:
1588 release_gl_drawable( gl );
1593 /***********************************************************************
1594 * destroy_gl_drawable
1596 void destroy_gl_drawable( HWND hwnd )
1598 struct gl_drawable *gl;
1600 EnterCriticalSection( &context_section );
1601 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1603 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1604 free_gl_drawable( gl );
1606 LeaveCriticalSection( &context_section );
1611 * glxdrv_DescribePixelFormat
1613 * Get the pixel-format descriptor associated to the given id
1615 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1616 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1618 /*XVisualInfo *vis;*/
1619 int value;
1620 int rb,gb,bb,ab;
1621 const struct wgl_pixel_format *fmt;
1623 if (!has_opengl()) return 0;
1625 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1627 if (!ppfd) return nb_onscreen_formats;
1629 /* 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 */
1630 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1631 if (!fmt) {
1632 WARN("unexpected format %d\n", iPixelFormat);
1633 return 0;
1636 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1637 ERR("Wrong structure size !\n");
1638 /* Should set error */
1639 return 0;
1642 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1643 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1644 ppfd->nVersion = 1;
1646 /* These flags are always the same... */
1647 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1648 /* Now the flags extracted from the Visual */
1650 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1651 if(value & GLX_WINDOW_BIT)
1652 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1654 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1655 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1656 * 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
1657 * offered the GDI bit either. */
1658 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1660 /* PFD_GENERIC_FORMAT - gdi software rendering
1661 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1662 * none set - full hardware accelerated by a ICD
1664 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1665 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1667 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1668 if (value) {
1669 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1670 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1672 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1674 /* Pixel type */
1675 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1676 if (value & GLX_RGBA_BIT)
1677 ppfd->iPixelType = PFD_TYPE_RGBA;
1678 else
1679 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1681 /* Color bits */
1682 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1683 ppfd->cColorBits = value;
1685 /* Red, green, blue and alpha bits / shifts */
1686 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1687 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1688 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1689 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1690 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1692 ppfd->cRedBits = rb;
1693 ppfd->cRedShift = gb + bb + ab;
1694 ppfd->cBlueBits = bb;
1695 ppfd->cBlueShift = ab;
1696 ppfd->cGreenBits = gb;
1697 ppfd->cGreenShift = bb + ab;
1698 ppfd->cAlphaBits = ab;
1699 ppfd->cAlphaShift = 0;
1700 } else {
1701 ppfd->cRedBits = 0;
1702 ppfd->cRedShift = 0;
1703 ppfd->cBlueBits = 0;
1704 ppfd->cBlueShift = 0;
1705 ppfd->cGreenBits = 0;
1706 ppfd->cGreenShift = 0;
1707 ppfd->cAlphaBits = 0;
1708 ppfd->cAlphaShift = 0;
1711 /* Accum RGBA bits */
1712 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1713 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1714 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1715 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1717 ppfd->cAccumBits = rb+gb+bb+ab;
1718 ppfd->cAccumRedBits = rb;
1719 ppfd->cAccumGreenBits = gb;
1720 ppfd->cAccumBlueBits = bb;
1721 ppfd->cAccumAlphaBits = ab;
1723 /* Aux bits */
1724 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1725 ppfd->cAuxBuffers = value;
1727 /* Depth bits */
1728 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1729 ppfd->cDepthBits = value;
1731 /* stencil bits */
1732 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1733 ppfd->cStencilBits = value;
1735 ppfd->iLayerType = PFD_MAIN_PLANE;
1737 if (TRACE_ON(wgl)) {
1738 dump_PIXELFORMATDESCRIPTOR(ppfd);
1741 return nb_onscreen_formats;
1744 /***********************************************************************
1745 * glxdrv_wglGetPixelFormat
1747 static int glxdrv_wglGetPixelFormat( HDC hdc )
1749 struct gl_drawable *gl;
1750 int ret = 0;
1752 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1754 ret = pixel_format_index( gl->format );
1755 /* Offscreen formats can't be used with traditional WGL calls.
1756 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1757 if (!is_onscreen_pixel_format( ret )) ret = 1;
1758 release_gl_drawable( gl );
1760 TRACE( "%p -> %d\n", hdc, ret );
1761 return ret;
1764 /***********************************************************************
1765 * glxdrv_wglSetPixelFormat
1767 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1769 return set_pixel_format(hdc, iPixelFormat, FALSE);
1772 /***********************************************************************
1773 * glxdrv_wglCopyContext
1775 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1777 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1779 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1781 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1782 return TRUE;
1785 /***********************************************************************
1786 * glxdrv_wglCreateContext
1788 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1790 struct wgl_context *ret = NULL;
1791 struct gl_drawable *gl;
1793 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1795 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1796 return NULL;
1799 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1801 ret->hdc = hdc;
1802 ret->fmt = gl->format;
1803 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1804 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1805 list_add_head( &context_list, &ret->entry );
1807 release_gl_drawable( gl );
1808 TRACE( "%p -> %p\n", hdc, ret );
1809 return ret;
1812 /***********************************************************************
1813 * glxdrv_wglDeleteContext
1815 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1817 TRACE("(%p)\n", ctx);
1819 EnterCriticalSection( &context_section );
1820 list_remove( &ctx->entry );
1821 LeaveCriticalSection( &context_section );
1823 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1824 if (ctx->vis) XFree( ctx->vis );
1825 HeapFree( GetProcessHeap(), 0, ctx );
1828 /***********************************************************************
1829 * glxdrv_wglGetProcAddress
1831 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1833 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1834 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1837 /***********************************************************************
1838 * glxdrv_wglMakeCurrent
1840 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1842 BOOL ret = FALSE;
1843 struct gl_drawable *gl;
1845 TRACE("(%p,%p)\n", hdc, ctx);
1847 if (!ctx)
1849 pglXMakeCurrent(gdi_display, None, NULL);
1850 NtCurrentTeb()->glContext = NULL;
1851 return TRUE;
1854 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1856 if (ctx->fmt != gl->format)
1858 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1859 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1860 goto done;
1863 if (TRACE_ON(wgl)) {
1864 describeContext(ctx);
1865 TRACE("hdc %p drawable %lx fmt %p ctx %p\n", hdc, gl->drawable, gl->format, ctx->ctx );
1868 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1869 if (ret)
1871 NtCurrentTeb()->glContext = ctx;
1872 ctx->has_been_current = TRUE;
1873 ctx->hdc = hdc;
1874 ctx->drawables[0] = gl->drawable;
1875 ctx->drawables[1] = gl->drawable;
1876 ctx->refresh_drawables = FALSE;
1877 goto done;
1880 SetLastError( ERROR_INVALID_HANDLE );
1882 done:
1883 release_gl_drawable( gl );
1884 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1885 return ret;
1888 /***********************************************************************
1889 * X11DRV_wglMakeContextCurrentARB
1891 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1893 BOOL ret = FALSE;
1894 struct gl_drawable *draw_gl, *read_gl = NULL;
1896 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1898 if (!ctx)
1900 pglXMakeCurrent(gdi_display, None, NULL);
1901 NtCurrentTeb()->glContext = NULL;
1902 return TRUE;
1905 if (!pglXMakeContextCurrent) return FALSE;
1907 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1909 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1910 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1911 read_gl ? read_gl->drawable : 0, ctx->ctx);
1912 if (ret)
1914 ctx->has_been_current = TRUE;
1915 ctx->hdc = draw_hdc;
1916 ctx->drawables[0] = draw_gl->drawable;
1917 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1918 ctx->refresh_drawables = FALSE;
1919 NtCurrentTeb()->glContext = ctx;
1920 goto done;
1923 SetLastError( ERROR_INVALID_HANDLE );
1924 done:
1925 release_gl_drawable( read_gl );
1926 release_gl_drawable( draw_gl );
1927 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1928 return ret;
1931 /***********************************************************************
1932 * glxdrv_wglShareLists
1934 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1936 TRACE("(%p, %p)\n", org, dest);
1938 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1939 * at context creation time but in case of WGL it is done using wglShareLists.
1940 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1941 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1942 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1943 * so there delaying context creation doesn't work.
1945 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1946 * and when a program requests sharing we recreate the destination context if it hasn't been made
1947 * current or when it hasn't shared display lists before.
1950 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1952 ERR("Could not share display lists, one of the contexts has been current already !\n");
1953 return FALSE;
1955 else if(dest->sharing)
1957 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1958 return FALSE;
1960 else
1962 describeContext(org);
1963 describeContext(dest);
1965 /* Re-create the GLX context and share display lists */
1966 pglXDestroyContext(gdi_display, dest->ctx);
1967 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1968 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1970 org->sharing = TRUE;
1971 dest->sharing = TRUE;
1972 return TRUE;
1974 return FALSE;
1977 static void wglFinish(void)
1979 struct x11drv_escape_flush_gl_drawable escape;
1980 struct gl_drawable *gl;
1981 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1983 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1984 escape.gl_drawable = 0;
1986 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1988 switch (gl->type)
1990 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1991 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1992 default: break;
1994 sync_context(ctx);
1995 release_gl_drawable( gl );
1998 pglFinish();
1999 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2002 static void wglFlush(void)
2004 struct x11drv_escape_flush_gl_drawable escape;
2005 struct gl_drawable *gl;
2006 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2008 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
2009 escape.gl_drawable = 0;
2011 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
2013 switch (gl->type)
2015 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
2016 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
2017 default: break;
2019 sync_context(ctx);
2020 release_gl_drawable( gl );
2023 pglFlush();
2024 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2027 static const GLubyte *wglGetString(GLenum name)
2029 if (name == GL_EXTENSIONS && WineGLInfo.glExtensions)
2030 return (const GLubyte *)WineGLInfo.glExtensions;
2031 return pglGetString(name);
2034 /***********************************************************************
2035 * X11DRV_wglCreateContextAttribsARB
2037 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2038 const int* attribList )
2040 struct wgl_context *ret = NULL;
2041 struct gl_drawable *gl;
2042 int err = 0;
2044 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2046 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2048 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2049 return NULL;
2052 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2054 ret->hdc = hdc;
2055 ret->fmt = gl->format;
2056 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2057 ret->gl3_context = TRUE;
2058 if (attribList)
2060 int *pContextAttribList = &ret->attribList[0];
2061 /* attribList consists of pairs {token, value] terminated with 0 */
2062 while(attribList[0] != 0)
2064 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2065 switch(attribList[0])
2067 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2068 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2069 pContextAttribList[1] = attribList[1];
2070 pContextAttribList += 2;
2071 ret->numAttribs++;
2072 break;
2073 case WGL_CONTEXT_MINOR_VERSION_ARB:
2074 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2075 pContextAttribList[1] = attribList[1];
2076 pContextAttribList += 2;
2077 ret->numAttribs++;
2078 break;
2079 case WGL_CONTEXT_LAYER_PLANE_ARB:
2080 break;
2081 case WGL_CONTEXT_FLAGS_ARB:
2082 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2083 pContextAttribList[1] = attribList[1];
2084 pContextAttribList += 2;
2085 ret->numAttribs++;
2086 break;
2087 case WGL_CONTEXT_PROFILE_MASK_ARB:
2088 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2089 pContextAttribList[1] = attribList[1];
2090 pContextAttribList += 2;
2091 ret->numAttribs++;
2092 break;
2093 default:
2094 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2096 attribList += 2;
2100 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2101 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2102 XSync(gdi_display, False);
2103 if ((err = X11DRV_check_error()) || !ret->ctx)
2105 /* In the future we should convert the GLX error to a win32 one here if needed */
2106 ERR("Context creation failed (error %x)\n", err);
2107 HeapFree( GetProcessHeap(), 0, ret );
2108 ret = NULL;
2110 else list_add_head( &context_list, &ret->entry );
2113 release_gl_drawable( gl );
2114 TRACE( "%p -> %p\n", hdc, ret );
2115 return ret;
2119 * X11DRV_wglGetExtensionsStringARB
2121 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2123 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2125 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2126 return WineGLInfo.wglExtensions;
2130 * X11DRV_wglCreatePbufferARB
2132 * WGL_ARB_pbuffer: wglCreatePbufferARB
2134 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2135 const int *piAttribList )
2137 struct wgl_pbuffer* object = NULL;
2138 const struct wgl_pixel_format *fmt = NULL;
2139 int attribs[256];
2140 int nAttribs = 0;
2142 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2144 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2145 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2146 if(!fmt) {
2147 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2148 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2149 return NULL;
2152 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2153 if (NULL == object) {
2154 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2155 return NULL;
2157 object->width = iWidth;
2158 object->height = iHeight;
2159 object->fmt = fmt;
2161 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2162 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2163 while (piAttribList && 0 != *piAttribList) {
2164 int attr_v;
2165 switch (*piAttribList) {
2166 case WGL_PBUFFER_LARGEST_ARB: {
2167 ++piAttribList;
2168 attr_v = *piAttribList;
2169 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2170 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2171 break;
2174 case WGL_TEXTURE_FORMAT_ARB: {
2175 ++piAttribList;
2176 attr_v = *piAttribList;
2177 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2178 if (WGL_NO_TEXTURE_ARB == attr_v) {
2179 object->use_render_texture = 0;
2180 } else {
2181 if (!use_render_texture_emulation) {
2182 SetLastError(ERROR_INVALID_DATA);
2183 goto create_failed;
2185 switch (attr_v) {
2186 case WGL_TEXTURE_RGB_ARB:
2187 object->use_render_texture = GL_RGB;
2188 object->texture_bpp = 3;
2189 object->texture_format = GL_RGB;
2190 object->texture_type = GL_UNSIGNED_BYTE;
2191 break;
2192 case WGL_TEXTURE_RGBA_ARB:
2193 object->use_render_texture = GL_RGBA;
2194 object->texture_bpp = 4;
2195 object->texture_format = GL_RGBA;
2196 object->texture_type = GL_UNSIGNED_BYTE;
2197 break;
2199 /* WGL_FLOAT_COMPONENTS_NV */
2200 case WGL_TEXTURE_FLOAT_R_NV:
2201 object->use_render_texture = GL_FLOAT_R_NV;
2202 object->texture_bpp = 4;
2203 object->texture_format = GL_RED;
2204 object->texture_type = GL_FLOAT;
2205 break;
2206 case WGL_TEXTURE_FLOAT_RG_NV:
2207 object->use_render_texture = GL_FLOAT_RG_NV;
2208 object->texture_bpp = 8;
2209 object->texture_format = GL_LUMINANCE_ALPHA;
2210 object->texture_type = GL_FLOAT;
2211 break;
2212 case WGL_TEXTURE_FLOAT_RGB_NV:
2213 object->use_render_texture = GL_FLOAT_RGB_NV;
2214 object->texture_bpp = 12;
2215 object->texture_format = GL_RGB;
2216 object->texture_type = GL_FLOAT;
2217 break;
2218 case WGL_TEXTURE_FLOAT_RGBA_NV:
2219 object->use_render_texture = GL_FLOAT_RGBA_NV;
2220 object->texture_bpp = 16;
2221 object->texture_format = GL_RGBA;
2222 object->texture_type = GL_FLOAT;
2223 break;
2224 default:
2225 ERR("Unknown texture format: %x\n", attr_v);
2226 SetLastError(ERROR_INVALID_DATA);
2227 goto create_failed;
2230 break;
2233 case WGL_TEXTURE_TARGET_ARB: {
2234 ++piAttribList;
2235 attr_v = *piAttribList;
2236 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2237 if (WGL_NO_TEXTURE_ARB == attr_v) {
2238 object->texture_target = 0;
2239 } else {
2240 if (!use_render_texture_emulation) {
2241 SetLastError(ERROR_INVALID_DATA);
2242 goto create_failed;
2244 switch (attr_v) {
2245 case WGL_TEXTURE_CUBE_MAP_ARB: {
2246 if (iWidth != iHeight) {
2247 SetLastError(ERROR_INVALID_DATA);
2248 goto create_failed;
2250 object->texture_target = GL_TEXTURE_CUBE_MAP;
2251 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2252 break;
2254 case WGL_TEXTURE_1D_ARB: {
2255 if (1 != iHeight) {
2256 SetLastError(ERROR_INVALID_DATA);
2257 goto create_failed;
2259 object->texture_target = GL_TEXTURE_1D;
2260 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2261 break;
2263 case WGL_TEXTURE_2D_ARB: {
2264 object->texture_target = GL_TEXTURE_2D;
2265 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2266 break;
2268 case WGL_TEXTURE_RECTANGLE_NV: {
2269 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2270 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2271 break;
2273 default:
2274 ERR("Unknown texture target: %x\n", attr_v);
2275 SetLastError(ERROR_INVALID_DATA);
2276 goto create_failed;
2279 break;
2282 case WGL_MIPMAP_TEXTURE_ARB: {
2283 ++piAttribList;
2284 attr_v = *piAttribList;
2285 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2286 if (!use_render_texture_emulation) {
2287 SetLastError(ERROR_INVALID_DATA);
2288 goto create_failed;
2290 break;
2293 ++piAttribList;
2296 PUSH1(attribs, None);
2297 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2298 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2299 if (!object->drawable) {
2300 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2301 goto create_failed; /* unexpected error */
2303 TRACE("->(%p)\n", object);
2304 return object;
2306 create_failed:
2307 HeapFree(GetProcessHeap(), 0, object);
2308 TRACE("->(FAILED)\n");
2309 return NULL;
2313 * X11DRV_wglDestroyPbufferARB
2315 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2317 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2319 TRACE("(%p)\n", object);
2321 pglXDestroyPbuffer(gdi_display, object->drawable);
2322 HeapFree(GetProcessHeap(), 0, object);
2323 return GL_TRUE;
2327 * X11DRV_wglGetPbufferDCARB
2329 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2331 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2333 struct x11drv_escape_set_drawable escape;
2334 struct gl_drawable *gl, *prev;
2335 HDC hdc;
2337 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2338 if (!hdc) return 0;
2340 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2342 DeleteDC( hdc );
2343 return 0;
2345 gl->type = DC_GL_PBUFFER;
2346 gl->drawable = object->drawable;
2347 gl->format = object->fmt;
2349 EnterCriticalSection( &context_section );
2350 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2351 free_gl_drawable( prev );
2352 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2353 LeaveCriticalSection( &context_section );
2355 escape.code = X11DRV_SET_DRAWABLE;
2356 escape.drawable = object->drawable;
2357 escape.mode = IncludeInferiors;
2358 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2359 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2361 TRACE( "(%p)->(%p)\n", object, hdc );
2362 return hdc;
2366 * X11DRV_wglQueryPbufferARB
2368 * WGL_ARB_pbuffer: wglQueryPbufferARB
2370 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2372 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2374 switch (iAttribute) {
2375 case WGL_PBUFFER_WIDTH_ARB:
2376 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2377 break;
2378 case WGL_PBUFFER_HEIGHT_ARB:
2379 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2380 break;
2382 case WGL_PBUFFER_LOST_ARB:
2383 /* GLX Pbuffers cannot be lost by default. We can support this by
2384 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2385 * to receive pixel buffer clobber events, however that may or may
2386 * not give any benefit */
2387 *piValue = GL_FALSE;
2388 break;
2390 case WGL_TEXTURE_FORMAT_ARB:
2391 if (!object->use_render_texture) {
2392 *piValue = WGL_NO_TEXTURE_ARB;
2393 } else {
2394 if (!use_render_texture_emulation) {
2395 SetLastError(ERROR_INVALID_HANDLE);
2396 return GL_FALSE;
2398 switch(object->use_render_texture) {
2399 case GL_RGB:
2400 *piValue = WGL_TEXTURE_RGB_ARB;
2401 break;
2402 case GL_RGBA:
2403 *piValue = WGL_TEXTURE_RGBA_ARB;
2404 break;
2405 /* WGL_FLOAT_COMPONENTS_NV */
2406 case GL_FLOAT_R_NV:
2407 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2408 break;
2409 case GL_FLOAT_RG_NV:
2410 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2411 break;
2412 case GL_FLOAT_RGB_NV:
2413 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2414 break;
2415 case GL_FLOAT_RGBA_NV:
2416 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2417 break;
2418 default:
2419 ERR("Unknown texture format: %x\n", object->use_render_texture);
2422 break;
2424 case WGL_TEXTURE_TARGET_ARB:
2425 if (!object->texture_target){
2426 *piValue = WGL_NO_TEXTURE_ARB;
2427 } else {
2428 if (!use_render_texture_emulation) {
2429 SetLastError(ERROR_INVALID_DATA);
2430 return GL_FALSE;
2432 switch (object->texture_target) {
2433 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2434 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2435 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2436 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2439 break;
2441 case WGL_MIPMAP_TEXTURE_ARB:
2442 *piValue = GL_FALSE; /** don't support that */
2443 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2444 break;
2446 default:
2447 FIXME("unexpected attribute %x\n", iAttribute);
2448 break;
2451 return GL_TRUE;
2455 * X11DRV_wglReleasePbufferDCARB
2457 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2459 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2461 struct gl_drawable *gl;
2463 TRACE("(%p, %p)\n", object, hdc);
2465 EnterCriticalSection( &context_section );
2467 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2469 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2470 free_gl_drawable( gl );
2472 else hdc = 0;
2474 LeaveCriticalSection( &context_section );
2476 return hdc && DeleteDC(hdc);
2480 * X11DRV_wglSetPbufferAttribARB
2482 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2484 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2486 GLboolean ret = GL_FALSE;
2488 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2490 if (!object->use_render_texture) {
2491 SetLastError(ERROR_INVALID_HANDLE);
2492 return GL_FALSE;
2494 if (use_render_texture_emulation) {
2495 return GL_TRUE;
2497 return ret;
2501 * X11DRV_wglChoosePixelFormatARB
2503 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2505 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2506 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2508 int attribs[256];
2509 int nAttribs = 0;
2510 GLXFBConfig* cfgs = NULL;
2511 int nCfgs = 0;
2512 int it;
2513 int fmt_id;
2514 int start, end;
2515 UINT pfmt_it = 0;
2516 int run;
2517 int i;
2518 DWORD dwFlags = 0;
2520 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2521 if (NULL != pfAttribFList) {
2522 FIXME("unused pfAttribFList\n");
2525 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2526 if (-1 == nAttribs) {
2527 WARN("Cannot convert WGL to GLX attributes\n");
2528 return GL_FALSE;
2530 PUSH1(attribs, None);
2532 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2533 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2534 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2536 for(i=0; piAttribIList[i] != 0; i+=2)
2538 switch(piAttribIList[i])
2540 case WGL_DRAW_TO_BITMAP_ARB:
2541 if(piAttribIList[i+1])
2542 dwFlags |= PFD_DRAW_TO_BITMAP;
2543 break;
2544 case WGL_ACCELERATION_ARB:
2545 switch(piAttribIList[i+1])
2547 case WGL_NO_ACCELERATION_ARB:
2548 dwFlags |= PFD_GENERIC_FORMAT;
2549 break;
2550 case WGL_GENERIC_ACCELERATION_ARB:
2551 dwFlags |= PFD_GENERIC_ACCELERATED;
2552 break;
2553 case WGL_FULL_ACCELERATION_ARB:
2554 /* Nothing to do */
2555 break;
2557 break;
2558 case WGL_SUPPORT_GDI_ARB:
2559 if(piAttribIList[i+1])
2560 dwFlags |= PFD_SUPPORT_GDI;
2561 break;
2565 /* Search for FB configurations matching the requirements in attribs */
2566 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2567 if (NULL == cfgs) {
2568 WARN("Compatible Pixel Format not found\n");
2569 return GL_FALSE;
2572 /* Loop through all matching formats and check if they are suitable.
2573 * Note that this function should at max return nMaxFormats different formats */
2574 for(run=0; run < 2; run++)
2576 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2578 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2580 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2581 continue;
2584 /* During the first run we only want onscreen formats and during the second only offscreen */
2585 start = run == 1 ? nb_onscreen_formats : 0;
2586 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2588 for (i = start; i < end; i++)
2590 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2592 piFormats[pfmt_it++] = i + 1;
2593 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2594 it + 1, nCfgs, fmt_id, i + 1);
2595 break;
2601 *nNumFormats = pfmt_it;
2602 /** free list */
2603 XFree(cfgs);
2604 return GL_TRUE;
2608 * X11DRV_wglGetPixelFormatAttribivARB
2610 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2612 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2613 UINT nAttributes, const int *piAttributes, int *piValues )
2615 UINT i;
2616 const struct wgl_pixel_format *fmt = NULL;
2617 int hTest;
2618 int tmp;
2619 int curGLXAttr = 0;
2621 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2623 if (0 < iLayerPlane) {
2624 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2625 return GL_FALSE;
2628 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2629 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2630 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2631 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2632 if(!fmt) {
2633 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2636 for (i = 0; i < nAttributes; ++i) {
2637 const int curWGLAttr = piAttributes[i];
2638 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2640 switch (curWGLAttr) {
2641 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2642 piValues[i] = nb_pixel_formats;
2643 continue;
2645 case WGL_SUPPORT_OPENGL_ARB:
2646 piValues[i] = GL_TRUE;
2647 continue;
2649 case WGL_ACCELERATION_ARB:
2650 curGLXAttr = GLX_CONFIG_CAVEAT;
2651 if (!fmt) goto pix_error;
2652 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2653 piValues[i] = WGL_NO_ACCELERATION_ARB;
2654 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2655 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2656 else
2657 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2658 continue;
2660 case WGL_TRANSPARENT_ARB:
2661 curGLXAttr = GLX_TRANSPARENT_TYPE;
2662 if (!fmt) goto pix_error;
2663 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2664 if (hTest) goto get_error;
2665 piValues[i] = GL_FALSE;
2666 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2667 continue;
2669 case WGL_PIXEL_TYPE_ARB:
2670 curGLXAttr = GLX_RENDER_TYPE;
2671 if (!fmt) goto pix_error;
2672 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2673 if (hTest) goto get_error;
2674 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2675 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2676 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2677 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2678 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2679 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2680 else {
2681 ERR("unexpected RenderType(%x)\n", tmp);
2682 piValues[i] = WGL_TYPE_RGBA_ARB;
2684 continue;
2686 case WGL_COLOR_BITS_ARB:
2687 curGLXAttr = GLX_BUFFER_SIZE;
2688 break;
2690 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2691 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2692 if (!use_render_texture_emulation) {
2693 piValues[i] = GL_FALSE;
2694 continue;
2696 curGLXAttr = GLX_RENDER_TYPE;
2697 if (!fmt) goto pix_error;
2698 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2699 if (hTest) goto get_error;
2700 if (GLX_COLOR_INDEX_BIT == tmp) {
2701 piValues[i] = GL_FALSE;
2702 continue;
2704 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2705 if (hTest) goto get_error;
2706 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2707 continue;
2709 case WGL_BLUE_BITS_ARB:
2710 curGLXAttr = GLX_BLUE_SIZE;
2711 break;
2712 case WGL_RED_BITS_ARB:
2713 curGLXAttr = GLX_RED_SIZE;
2714 break;
2715 case WGL_GREEN_BITS_ARB:
2716 curGLXAttr = GLX_GREEN_SIZE;
2717 break;
2718 case WGL_ALPHA_BITS_ARB:
2719 curGLXAttr = GLX_ALPHA_SIZE;
2720 break;
2721 case WGL_DEPTH_BITS_ARB:
2722 curGLXAttr = GLX_DEPTH_SIZE;
2723 break;
2724 case WGL_STENCIL_BITS_ARB:
2725 curGLXAttr = GLX_STENCIL_SIZE;
2726 break;
2727 case WGL_DOUBLE_BUFFER_ARB:
2728 curGLXAttr = GLX_DOUBLEBUFFER;
2729 break;
2730 case WGL_STEREO_ARB:
2731 curGLXAttr = GLX_STEREO;
2732 break;
2733 case WGL_AUX_BUFFERS_ARB:
2734 curGLXAttr = GLX_AUX_BUFFERS;
2735 break;
2737 case WGL_SUPPORT_GDI_ARB:
2738 if (!fmt) goto pix_error;
2739 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2740 continue;
2742 case WGL_DRAW_TO_BITMAP_ARB:
2743 if (!fmt) goto pix_error;
2744 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2745 continue;
2747 case WGL_DRAW_TO_WINDOW_ARB:
2748 case WGL_DRAW_TO_PBUFFER_ARB:
2749 if (!fmt) goto pix_error;
2750 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2751 if (hTest) goto get_error;
2752 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2753 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2754 piValues[i] = GL_TRUE;
2755 else
2756 piValues[i] = GL_FALSE;
2757 continue;
2759 case WGL_SWAP_METHOD_ARB:
2760 if (has_swap_method)
2762 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2763 if (hTest) goto get_error;
2764 switch (tmp)
2766 case GLX_SWAP_EXCHANGE_OML:
2767 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2768 break;
2769 case GLX_SWAP_COPY_OML:
2770 piValues[i] = WGL_SWAP_COPY_ARB;
2771 break;
2772 case GLX_SWAP_UNDEFINED_OML:
2773 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2774 break;
2775 default:
2776 ERR("Unexpected swap method %x.\n", tmp);
2779 else
2781 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2782 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2784 continue;
2786 case WGL_PBUFFER_LARGEST_ARB:
2787 curGLXAttr = GLX_LARGEST_PBUFFER;
2788 break;
2790 case WGL_SAMPLE_BUFFERS_ARB:
2791 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2792 break;
2794 case WGL_SAMPLES_ARB:
2795 curGLXAttr = GLX_SAMPLES_ARB;
2796 break;
2798 case WGL_FLOAT_COMPONENTS_NV:
2799 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2800 break;
2802 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2803 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2804 break;
2806 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2807 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2808 break;
2810 case WGL_ACCUM_RED_BITS_ARB:
2811 curGLXAttr = GLX_ACCUM_RED_SIZE;
2812 break;
2813 case WGL_ACCUM_GREEN_BITS_ARB:
2814 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2815 break;
2816 case WGL_ACCUM_BLUE_BITS_ARB:
2817 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2818 break;
2819 case WGL_ACCUM_ALPHA_BITS_ARB:
2820 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2821 break;
2822 case WGL_ACCUM_BITS_ARB:
2823 if (!fmt) goto pix_error;
2824 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2825 if (hTest) goto get_error;
2826 piValues[i] = tmp;
2827 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2828 if (hTest) goto get_error;
2829 piValues[i] += tmp;
2830 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2831 if (hTest) goto get_error;
2832 piValues[i] += tmp;
2833 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2834 if (hTest) goto get_error;
2835 piValues[i] += tmp;
2836 continue;
2838 default:
2839 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2842 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2843 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2844 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2846 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2847 * and check which options can work using iPixelFormat=0 and which not.
2848 * A problem would be that this function is an extension. This would
2849 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2851 if (0 != curGLXAttr && iPixelFormat != 0) {
2852 if (!fmt) goto pix_error;
2853 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2854 if (hTest) goto get_error;
2855 curGLXAttr = 0;
2856 } else {
2857 piValues[i] = GL_FALSE;
2860 return GL_TRUE;
2862 get_error:
2863 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2864 return GL_FALSE;
2866 pix_error:
2867 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2868 return GL_FALSE;
2872 * X11DRV_wglGetPixelFormatAttribfvARB
2874 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2876 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2877 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2879 int *attr;
2880 int ret;
2881 UINT i;
2883 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2885 /* Allocate a temporary array to store integer values */
2886 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2887 if (!attr) {
2888 ERR("couldn't allocate %d array\n", nAttributes);
2889 return GL_FALSE;
2892 /* Piggy-back on wglGetPixelFormatAttribivARB */
2893 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2894 if (ret) {
2895 /* Convert integer values to float. Should also check for attributes
2896 that can give decimal values here */
2897 for (i=0; i<nAttributes;i++) {
2898 pfValues[i] = attr[i];
2902 HeapFree(GetProcessHeap(), 0, attr);
2903 return ret;
2907 * X11DRV_wglBindTexImageARB
2909 * WGL_ARB_render_texture: wglBindTexImageARB
2911 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2913 GLboolean ret = GL_FALSE;
2915 TRACE("(%p, %d)\n", object, iBuffer);
2917 if (!object->use_render_texture) {
2918 SetLastError(ERROR_INVALID_HANDLE);
2919 return GL_FALSE;
2922 if (use_render_texture_emulation) {
2923 static BOOL initialized = FALSE;
2924 int prev_binded_texture = 0;
2925 GLXContext prev_context;
2926 Drawable prev_drawable;
2927 GLXContext tmp_context;
2929 prev_context = pglXGetCurrentContext();
2930 prev_drawable = pglXGetCurrentDrawable();
2932 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2933 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2934 isn't ideal performance wise but I wasn't able to get other ways working.
2936 if(!initialized) {
2937 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2938 FIXME("partial stub!\n");
2941 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2942 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2944 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2946 /* Switch to our pbuffer */
2947 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2949 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2950 * After that upload the pbuffer texture data. */
2951 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2952 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2954 /* Switch back to the original drawable and upload the pbuffer-texture */
2955 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2956 pglXDestroyContext(gdi_display, tmp_context);
2957 return GL_TRUE;
2960 return ret;
2964 * X11DRV_wglReleaseTexImageARB
2966 * WGL_ARB_render_texture: wglReleaseTexImageARB
2968 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2970 GLboolean ret = GL_FALSE;
2972 TRACE("(%p, %d)\n", object, iBuffer);
2974 if (!object->use_render_texture) {
2975 SetLastError(ERROR_INVALID_HANDLE);
2976 return GL_FALSE;
2978 if (use_render_texture_emulation) {
2979 return GL_TRUE;
2981 return ret;
2985 * X11DRV_wglGetExtensionsStringEXT
2987 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2989 static const char *X11DRV_wglGetExtensionsStringEXT(void)
2991 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2992 return WineGLInfo.wglExtensions;
2996 * X11DRV_wglGetSwapIntervalEXT
2998 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3000 static int X11DRV_wglGetSwapIntervalEXT(void)
3002 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3003 struct gl_drawable *gl;
3004 int swap_interval;
3006 TRACE("()\n");
3008 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3010 /* This can't happen because a current WGL context is required to get
3011 * here. Likely the application is buggy.
3013 WARN("No GL drawable found, returning swap interval 0\n");
3014 return 0;
3017 swap_interval = gl->swap_interval;
3018 release_gl_drawable(gl);
3020 return swap_interval;
3024 * X11DRV_wglSwapIntervalEXT
3026 * WGL_EXT_swap_control: wglSwapIntervalEXT
3028 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3030 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3031 struct gl_drawable *gl;
3032 BOOL ret = TRUE;
3034 TRACE("(%d)\n", interval);
3036 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3037 * is invalid.
3039 if (interval < 0 && !has_swap_control_tear)
3041 SetLastError(ERROR_INVALID_DATA);
3042 return FALSE;
3045 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3047 SetLastError(ERROR_DC_NOT_FOUND);
3048 return FALSE;
3051 ret = set_swap_interval(gl->drawable, interval);
3052 gl->refresh_swap_interval = FALSE;
3053 if (ret)
3054 gl->swap_interval = interval;
3055 else
3056 SetLastError(ERROR_DC_NOT_FOUND);
3058 release_gl_drawable(gl);
3060 return ret;
3064 * X11DRV_wglSetPixelFormatWINE
3066 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3067 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3069 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3071 return set_pixel_format(hdc, format, TRUE);
3075 * glxRequireVersion (internal)
3077 * Check if the supported GLX version matches requiredVersion.
3079 static BOOL glxRequireVersion(int requiredVersion)
3081 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3082 if(requiredVersion <= WineGLInfo.glxVersion[1])
3083 return TRUE;
3085 return FALSE;
3088 static void register_extension(const char *ext)
3090 if (WineGLInfo.wglExtensions[0])
3091 strcat(WineGLInfo.wglExtensions, " ");
3092 strcat(WineGLInfo.wglExtensions, ext);
3094 TRACE("'%s'\n", ext);
3098 * X11DRV_WineGL_LoadExtensions
3100 static void X11DRV_WineGL_LoadExtensions(void)
3102 WineGLInfo.wglExtensions[0] = 0;
3104 /* ARB Extensions */
3106 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
3108 register_extension( "WGL_ARB_create_context" );
3109 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3111 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
3112 register_extension("WGL_ARB_create_context_profile");
3115 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
3117 register_extension("WGL_ARB_pixel_format_float");
3118 register_extension("WGL_ATI_pixel_format_float");
3121 register_extension( "WGL_ARB_extensions_string" );
3122 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3124 if (glxRequireVersion(3))
3126 register_extension( "WGL_ARB_make_current_read" );
3127 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3128 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3131 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3133 if (glxRequireVersion(3))
3135 register_extension( "WGL_ARB_pbuffer" );
3136 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3137 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3138 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3139 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3140 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3141 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3144 register_extension( "WGL_ARB_pixel_format" );
3145 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3146 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3147 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3149 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3150 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3151 (glxRequireVersion(3) && use_render_texture_emulation))
3153 register_extension( "WGL_ARB_render_texture" );
3154 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3155 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3157 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3158 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3159 register_extension("WGL_NV_float_buffer");
3161 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3162 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3163 register_extension("WGL_NV_render_texture_rectangle");
3166 /* EXT Extensions */
3168 register_extension( "WGL_EXT_extensions_string" );
3169 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3171 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3172 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3173 register_extension( "WGL_EXT_swap_control" );
3174 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3175 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3177 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3178 register_extension("WGL_EXT_framebuffer_sRGB");
3180 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3181 register_extension("WGL_EXT_pixel_format_packed_float");
3183 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3185 swap_control_method = GLX_SWAP_CONTROL_EXT;
3186 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control_tear"))
3188 register_extension("WGL_EXT_swap_control_tear");
3189 has_swap_control_tear = TRUE;
3192 else if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_swap_control"))
3194 swap_control_method = GLX_SWAP_CONTROL_MESA;
3196 else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGI_swap_control"))
3198 swap_control_method = GLX_SWAP_CONTROL_SGI;
3201 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3202 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3204 register_extension( "WGL_NV_vertex_array_range" );
3205 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3206 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3209 if (has_extension(WineGLInfo.glxExtensions, "GLX_OML_swap_method"))
3210 has_swap_method = TRUE;
3212 /* WINE-specific WGL Extensions */
3214 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3215 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3217 register_extension( "WGL_WINE_pixel_format_passthrough" );
3218 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3223 * glxdrv_SwapBuffers
3225 * Swap the buffers of this DC
3227 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3229 struct x11drv_escape_flush_gl_drawable escape;
3230 struct gl_drawable *gl;
3231 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3233 TRACE("(%p)\n", hdc);
3235 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3236 escape.gl_drawable = 0;
3238 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3240 SetLastError( ERROR_INVALID_HANDLE );
3241 return FALSE;
3244 if (gl->refresh_swap_interval)
3246 set_swap_interval(gl->drawable, gl->swap_interval);
3247 gl->refresh_swap_interval = FALSE;
3250 switch (gl->type)
3252 case DC_GL_PIXMAP_WIN:
3253 if (ctx) sync_context( ctx );
3254 escape.gl_drawable = gl->pixmap;
3255 if (pglXCopySubBufferMESA) {
3256 /* (glX)SwapBuffers has an implicit glFlush effect, however
3257 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3258 * copying */
3259 pglFlush();
3260 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3261 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3262 break;
3264 pglXSwapBuffers(gdi_display, gl->drawable);
3265 break;
3266 case DC_GL_CHILD_WIN:
3267 if (ctx) sync_context( ctx );
3268 escape.gl_drawable = gl->drawable;
3269 /* fall through */
3270 default:
3271 pglXSwapBuffers(gdi_display, gl->drawable);
3272 break;
3275 release_gl_drawable( gl );
3277 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3278 return TRUE;
3281 static struct opengl_funcs opengl_funcs =
3284 glxdrv_wglCopyContext, /* p_wglCopyContext */
3285 glxdrv_wglCreateContext, /* p_wglCreateContext */
3286 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3287 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3288 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3289 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3290 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3291 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3292 glxdrv_wglShareLists, /* p_wglShareLists */
3293 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3297 struct opengl_funcs *get_glx_driver( UINT version )
3299 if (version != WINE_WGL_DRIVER_VERSION)
3301 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3302 return NULL;
3304 if (has_opengl()) return &opengl_funcs;
3305 return NULL;
3308 #else /* no OpenGL includes */
3310 struct opengl_funcs *get_glx_driver( UINT version )
3312 return NULL;
3315 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3319 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3323 void destroy_gl_drawable( HWND hwnd )
3327 #endif /* defined(SONAME_LIBGL) */