winex11: Remove set-but-never-read hwnd and fbconfig_id fields of struct x11drv_escap...
[wine/multimedia.git] / dlls / winex11.drv / opengl.c
blob0077ac3fbc4099642fc6c1e559da0eecb1b55e74
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_RGBA_BIT 0x00000001
149 #define GLX_COLOR_INDEX_BIT 0x00000002
150 #define GLX_PBUFFER_CLOBBER_MASK 0x08000000
152 /** GLX_ARB_multisample */
153 #define GLX_SAMPLE_BUFFERS_ARB 100000
154 #define GLX_SAMPLES_ARB 100001
155 /** GLX_ARB_framebuffer_sRGB */
156 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
157 /** GLX_EXT_fbconfig_packed_float */
158 #define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
159 #define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT 0x00000008
160 /** GLX_ARB_create_context */
161 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
162 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
163 #define GLX_CONTEXT_FLAGS_ARB 0x2094
164 /** GLX_ARB_create_context_profile */
165 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
166 /** GLX_ATI_pixel_format_float */
167 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
168 /** GLX_ARB_pixel_format_float */
169 #define GLX_RGBA_FLOAT_BIT 0x00000004
170 #define GLX_RGBA_FLOAT_TYPE 0x20B9
171 /** GLX_NV_float_buffer */
172 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
175 struct WineGLInfo {
176 const char *glVersion;
177 char *glExtensions;
179 int glxVersion[2];
181 const char *glxServerVersion;
182 const char *glxServerVendor;
183 const char *glxServerExtensions;
185 const char *glxClientVersion;
186 const char *glxClientVendor;
187 const char *glxClientExtensions;
189 const char *glxExtensions;
191 BOOL glxDirect;
192 char wglExtensions[4096];
195 struct wgl_pixel_format
197 GLXFBConfig fbconfig;
198 int fmt_id;
199 int render_type;
200 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
203 struct wgl_context
205 HDC hdc;
206 BOOL has_been_current;
207 BOOL sharing;
208 BOOL gl3_context;
209 XVisualInfo *vis;
210 const struct wgl_pixel_format *fmt;
211 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
212 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
213 GLXContext ctx;
214 Drawable drawables[2];
215 BOOL refresh_drawables;
216 struct list entry;
219 struct wgl_pbuffer
221 Drawable drawable;
222 const struct wgl_pixel_format* fmt;
223 int width;
224 int height;
225 int* attribList;
226 int use_render_texture; /* This is also the internal texture format */
227 int texture_bind_target;
228 int texture_bpp;
229 GLint texture_format;
230 GLuint texture_target;
231 GLenum texture_type;
232 GLuint texture;
233 int texture_level;
236 enum dc_gl_type
238 DC_GL_NONE, /* no GL support (pixel format not set yet) */
239 DC_GL_WINDOW, /* normal top-level window */
240 DC_GL_CHILD_WIN, /* child window using XComposite */
241 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
242 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
245 struct gl_drawable
247 enum dc_gl_type type; /* type of GL surface */
248 Drawable drawable; /* drawable for rendering to the client area */
249 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
250 Colormap colormap; /* colormap used for the drawable */
251 const struct wgl_pixel_format *format; /* pixel format for the drawable */
252 XVisualInfo *visual; /* information about the GL visual */
253 RECT rect; /* drawable rect, relative to whole window drawable */
254 int swap_interval;
255 BOOL refresh_swap_interval;
258 enum glx_swap_control_method
260 GLX_SWAP_CONTROL_NONE,
261 GLX_SWAP_CONTROL_EXT,
262 GLX_SWAP_CONTROL_SGI,
263 GLX_SWAP_CONTROL_MESA
266 /* X context to associate a struct gl_drawable to an hwnd */
267 static XContext gl_hwnd_context;
268 /* X context to associate a struct gl_drawable to a pbuffer hdc */
269 static XContext gl_pbuffer_context;
271 static const struct gdi_dc_funcs glxdrv_funcs;
273 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
275 return (struct glx_physdev *)dev;
278 static struct list context_list = LIST_INIT( context_list );
279 static struct WineGLInfo WineGLInfo = { 0 };
280 static struct wgl_pixel_format *pixel_formats;
281 static int nb_pixel_formats, nb_onscreen_formats;
282 static BOOL use_render_texture_emulation = TRUE;
284 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
285 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
286 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
287 static BOOL has_swap_control_tear = FALSE;
289 static CRITICAL_SECTION context_section;
290 static CRITICAL_SECTION_DEBUG critsect_debug =
292 0, 0, &context_section,
293 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
294 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
296 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
298 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
300 static struct opengl_funcs opengl_funcs;
302 #define USE_GL_FUNC(name) #name,
303 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
304 #undef USE_GL_FUNC
306 static void X11DRV_WineGL_LoadExtensions(void);
307 static void init_pixel_formats( Display *display );
308 static BOOL glxRequireVersion(int requiredVersion);
310 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
311 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
312 TRACE(" - dwFlags : ");
313 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
315 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
316 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
318 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
322 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
323 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
324 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
325 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
326 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
327 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
328 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
329 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
330 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
331 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
332 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
333 #undef TEST_AND_DUMP
334 TRACE("\n");
336 TRACE(" - iPixelType : ");
337 switch (ppfd->iPixelType) {
338 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
339 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
341 TRACE("\n");
343 TRACE(" - Color : %d\n", ppfd->cColorBits);
344 TRACE(" - Red : %d\n", ppfd->cRedBits);
345 TRACE(" - Green : %d\n", ppfd->cGreenBits);
346 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
347 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
348 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
349 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
350 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
351 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
353 TRACE(" - iLayerType : ");
354 switch (ppfd->iLayerType) {
355 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
356 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
357 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
359 TRACE("\n");
362 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
363 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
365 /* GLX 1.0 */
366 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
367 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
368 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
369 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
370 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
371 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
372 static GLXPixmap (*pglXCreateGLXPixmap)( Display *dpy, XVisualInfo *visual, Pixmap pixmap );
373 static void (*pglXDestroyGLXPixmap)( Display *dpy, GLXPixmap pixmap );
374 static Bool (*pglXQueryExtension)( Display *dpy, int *errorb, int *event );
375 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
376 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
377 static int (*pglXGetConfig)( Display *dpy, XVisualInfo *visual, int attrib, int *value );
378 static GLXContext (*pglXGetCurrentContext)( void );
379 static GLXDrawable (*pglXGetCurrentDrawable)( void );
381 /* GLX 1.1 */
382 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
383 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
384 static const char *(*pglXGetClientString)( Display *dpy, int name );
386 /* GLX 1.3 */
387 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
388 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
389 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
390 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
391 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
392 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
393 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
394 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
395 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
397 /* GLX Extensions */
398 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
399 static void* (*pglXGetProcAddressARB)(const GLubyte *);
400 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
401 static int (*pglXSwapIntervalSGI)(int);
403 /* NV GLX Extension */
404 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
405 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
407 /* MESA GLX Extensions */
408 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
409 static int (*pglXSwapIntervalMESA)(unsigned int interval);
411 /* Standard OpenGL */
412 static void (*pglFinish)(void);
413 static void (*pglFlush)(void);
414 static const GLubyte *(*pglGetString)(GLenum name);
416 static void wglFinish(void);
417 static void wglFlush(void);
418 static const GLubyte *wglGetString(GLenum name);
420 /* check if the extension is present in the list */
421 static BOOL has_extension( const char *list, const char *ext )
423 size_t len = strlen( ext );
425 while (list)
427 while (*list == ' ') list++;
428 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
429 list = strchr( list, ' ' );
431 return FALSE;
434 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
436 /* In the future we might want to find the exact X or GLX error to report back to the app */
437 return 1;
440 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
442 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
444 int screen = DefaultScreen(gdi_display);
445 Window win = 0, root = 0;
446 const char *gl_renderer;
447 const char* str;
448 XVisualInfo *vis;
449 GLXContext ctx = NULL;
450 XSetWindowAttributes attr;
451 BOOL ret = FALSE;
452 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
454 attr.override_redirect = True;
455 attr.colormap = None;
456 attr.border_pixel = 0;
458 vis = pglXChooseVisual(gdi_display, screen, attribList);
459 if (vis) {
460 #ifdef __i386__
461 WORD old_fs = wine_get_fs();
462 /* Create a GLX Context. Without one we can't query GL information */
463 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
464 if (wine_get_fs() != old_fs)
466 wine_set_fs( old_fs );
467 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
468 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
469 goto done;
471 #else
472 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
473 #endif
475 if (!ctx) goto done;
477 root = RootWindow( gdi_display, vis->screen );
478 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
479 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
480 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
481 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
482 XMapWindow( gdi_display, win );
483 else
484 win = root;
486 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
488 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
489 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
490 goto done;
492 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
493 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
494 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
495 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
496 strcpy(WineGLInfo.glExtensions, str);
497 strcat(WineGLInfo.glExtensions, legacy_extensions);
499 /* Get the common GLX version supported by GLX client and server ( major/minor) */
500 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
502 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
503 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
504 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
506 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
507 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
508 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
510 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
511 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
513 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
514 TRACE("GL renderer : %s.\n", gl_renderer);
515 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
516 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
517 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
518 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
519 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
520 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
522 if(!WineGLInfo.glxDirect)
524 int fd = ConnectionNumber(gdi_display);
525 struct sockaddr_un uaddr;
526 unsigned int uaddrlen = sizeof(struct sockaddr_un);
528 /* In general indirect rendering on a local X11 server indicates a driver problem.
529 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
531 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
532 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
533 "haven't been installed correctly (using GL renderer %s, version %s).\n",
534 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
535 debugstr_a(WineGLInfo.glVersion));
537 else
539 /* In general you would expect that if direct rendering is returned, that you receive hardware
540 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
541 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
542 * software rendering, it shows direct rendering.
544 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
545 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
546 * it shows 'Mesa X11'.
548 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
549 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
550 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
551 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
552 debugstr_a(WineGLInfo.glVersion));
554 ret = TRUE;
556 done:
557 if(vis) XFree(vis);
558 if(ctx) {
559 pglXMakeCurrent(gdi_display, None, NULL);
560 pglXDestroyContext(gdi_display, ctx);
562 if (win != root) XDestroyWindow( gdi_display, win );
563 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
564 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
565 return ret;
568 static BOOL has_opengl(void)
570 static BOOL init_done = FALSE;
571 static void *opengl_handle;
573 char buffer[200];
574 int error_base, event_base;
575 unsigned int i;
577 if (init_done) return (opengl_handle != NULL);
578 init_done = TRUE;
580 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
581 and include all dependencies */
582 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
583 if (opengl_handle == NULL)
585 ERR( "Failed to load libGL: %s\n", buffer );
586 ERR( "OpenGL support is disabled.\n");
587 return FALSE;
590 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
592 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
594 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
595 goto failed;
599 /* redirect some standard OpenGL functions */
600 #define REDIRECT(func) \
601 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
602 REDIRECT( glFinish );
603 REDIRECT( glFlush );
604 REDIRECT( glGetString );
605 #undef REDIRECT
607 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
608 if (pglXGetProcAddressARB == NULL) {
609 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
610 goto failed;
613 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
615 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
616 goto failed; \
617 } while(0)
619 /* GLX 1.0 */
620 LOAD_FUNCPTR(glXChooseVisual);
621 LOAD_FUNCPTR(glXCopyContext);
622 LOAD_FUNCPTR(glXCreateContext);
623 LOAD_FUNCPTR(glXCreateGLXPixmap);
624 LOAD_FUNCPTR(glXGetCurrentContext);
625 LOAD_FUNCPTR(glXGetCurrentDrawable);
626 LOAD_FUNCPTR(glXDestroyContext);
627 LOAD_FUNCPTR(glXDestroyGLXPixmap);
628 LOAD_FUNCPTR(glXGetConfig);
629 LOAD_FUNCPTR(glXIsDirect);
630 LOAD_FUNCPTR(glXMakeCurrent);
631 LOAD_FUNCPTR(glXSwapBuffers);
632 LOAD_FUNCPTR(glXQueryExtension);
633 LOAD_FUNCPTR(glXQueryVersion);
635 /* GLX 1.1 */
636 LOAD_FUNCPTR(glXGetClientString);
637 LOAD_FUNCPTR(glXQueryExtensionsString);
638 LOAD_FUNCPTR(glXQueryServerString);
640 /* GLX 1.3 */
641 LOAD_FUNCPTR(glXCreatePbuffer);
642 LOAD_FUNCPTR(glXCreateNewContext);
643 LOAD_FUNCPTR(glXDestroyPbuffer);
644 LOAD_FUNCPTR(glXMakeContextCurrent);
645 LOAD_FUNCPTR(glXGetFBConfigs);
646 #undef LOAD_FUNCPTR
648 /* It doesn't matter if these fail. They'll only be used if the driver reports
649 the associated extension is available (and if a driver reports the extension
650 is available but fails to provide the functions, it's quite broken) */
651 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
652 /* ARB GLX Extension */
653 LOAD_FUNCPTR(glXCreateContextAttribsARB);
654 /* EXT GLX Extension */
655 LOAD_FUNCPTR(glXSwapIntervalEXT);
656 /* MESA GLX Extension */
657 LOAD_FUNCPTR(glXSwapIntervalMESA);
658 /* SGI GLX Extension */
659 LOAD_FUNCPTR(glXSwapIntervalSGI);
660 /* NV GLX Extension */
661 LOAD_FUNCPTR(glXAllocateMemoryNV);
662 LOAD_FUNCPTR(glXFreeMemoryNV);
663 #undef LOAD_FUNCPTR
665 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
667 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
668 TRACE("GLX is up and running error_base = %d\n", error_base);
669 } else {
670 ERR( "GLX extension is missing, disabling OpenGL.\n" );
671 goto failed;
673 gl_hwnd_context = XUniqueContext();
674 gl_pbuffer_context = XUniqueContext();
676 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
677 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
678 * rendering is used.
680 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
681 * depends on the reported GLX client / server version and on the client / server extension list.
682 * Those don't have to be the same.
684 * In general the server GLX information lists the capabilities in case of indirect rendering.
685 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
686 * available and in that case the client GLX informat can be used.
687 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
688 * in the GLX version/extension list. When a program does this it works for certain for both
689 * direct and indirect rendering.
691 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
692 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
693 * extension list which causes this extension not to be listed. (Wine requires this extension).
694 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
695 * pbuffers is around.
697 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
698 * the ATI drivers and from then on use GLX client information for them.
701 if(glxRequireVersion(3)) {
702 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
703 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
704 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
705 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
706 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
707 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
708 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
709 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
711 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
712 * enable this function when the Xserver understand GLX 1.3 or newer
714 pglXQueryDrawable = NULL;
715 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
716 TRACE("Overriding ATI GLX capabilities!\n");
717 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
718 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
719 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
720 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
722 /* Use client GLX information in case of the ATI drivers. We override the
723 * capabilities over here and not somewhere else as ATI might better their
724 * life in the future. In case they release proper drivers this block of
725 * code won't be called. */
726 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
727 } else {
728 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
731 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
732 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
735 X11DRV_WineGL_LoadExtensions();
736 init_pixel_formats( gdi_display );
737 return TRUE;
739 failed:
740 wine_dlclose(opengl_handle, NULL, 0);
741 opengl_handle = NULL;
742 return FALSE;
745 static int describeContext( struct wgl_context *ctx ) {
746 int tmp;
747 int ctx_vis_id;
748 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
749 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
750 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
751 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
752 TRACE(" - VISUAL_ID 0x%x\n", tmp);
753 ctx_vis_id = tmp;
754 return ctx_vis_id;
757 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
758 int nAttribs = 0;
759 unsigned cur = 0;
760 int pop;
761 int drawattrib = 0;
762 int nvfloatattrib = GLX_DONT_CARE;
763 int pixelattrib = GLX_DONT_CARE;
765 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
766 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
767 while (iWGLAttr && 0 != iWGLAttr[cur]) {
768 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
770 switch (iWGLAttr[cur]) {
771 case WGL_AUX_BUFFERS_ARB:
772 pop = iWGLAttr[++cur];
773 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
774 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
775 break;
776 case WGL_COLOR_BITS_ARB:
777 pop = iWGLAttr[++cur];
778 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
779 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
780 break;
781 case WGL_BLUE_BITS_ARB:
782 pop = iWGLAttr[++cur];
783 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
784 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
785 break;
786 case WGL_RED_BITS_ARB:
787 pop = iWGLAttr[++cur];
788 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
789 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
790 break;
791 case WGL_GREEN_BITS_ARB:
792 pop = iWGLAttr[++cur];
793 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
794 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
795 break;
796 case WGL_ALPHA_BITS_ARB:
797 pop = iWGLAttr[++cur];
798 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
799 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
800 break;
801 case WGL_DEPTH_BITS_ARB:
802 pop = iWGLAttr[++cur];
803 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
804 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
805 break;
806 case WGL_STENCIL_BITS_ARB:
807 pop = iWGLAttr[++cur];
808 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
809 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
810 break;
811 case WGL_DOUBLE_BUFFER_ARB:
812 pop = iWGLAttr[++cur];
813 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
814 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
815 break;
816 case WGL_STEREO_ARB:
817 pop = iWGLAttr[++cur];
818 PUSH2(oGLXAttr, GLX_STEREO, pop);
819 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
820 break;
822 case WGL_PIXEL_TYPE_ARB:
823 pop = iWGLAttr[++cur];
824 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
825 switch (pop) {
826 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
827 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
828 /* 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 */
829 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
830 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
831 default:
832 ERR("unexpected PixelType(%x)\n", pop);
833 pop = 0;
835 break;
837 case WGL_SUPPORT_GDI_ARB:
838 /* This flag is set in a pixel format */
839 pop = iWGLAttr[++cur];
840 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
841 break;
843 case WGL_DRAW_TO_BITMAP_ARB:
844 /* This flag is set in a pixel format */
845 pop = iWGLAttr[++cur];
846 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
847 break;
849 case WGL_DRAW_TO_WINDOW_ARB:
850 pop = iWGLAttr[++cur];
851 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
852 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
853 if (pop) {
854 drawattrib |= GLX_WINDOW_BIT;
856 break;
858 case WGL_DRAW_TO_PBUFFER_ARB:
859 pop = iWGLAttr[++cur];
860 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
861 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
862 if (pop) {
863 drawattrib |= GLX_PBUFFER_BIT;
865 break;
867 case WGL_ACCELERATION_ARB:
868 /* This flag is set in a pixel format */
869 pop = iWGLAttr[++cur];
870 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
871 break;
873 case WGL_SUPPORT_OPENGL_ARB:
874 pop = iWGLAttr[++cur];
875 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
876 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
877 break;
879 case WGL_SWAP_METHOD_ARB:
880 pop = iWGLAttr[++cur];
881 /* For now we ignore this and just return SWAP_EXCHANGE */
882 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
883 break;
885 case WGL_PBUFFER_LARGEST_ARB:
886 pop = iWGLAttr[++cur];
887 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
888 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
889 break;
891 case WGL_SAMPLE_BUFFERS_ARB:
892 pop = iWGLAttr[++cur];
893 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
894 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
895 break;
897 case WGL_SAMPLES_ARB:
898 pop = iWGLAttr[++cur];
899 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
900 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
901 break;
903 case WGL_TEXTURE_FORMAT_ARB:
904 case WGL_TEXTURE_TARGET_ARB:
905 case WGL_MIPMAP_TEXTURE_ARB:
906 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
907 pop = iWGLAttr[++cur];
908 if (NULL == pbuf) {
909 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
911 if (!use_render_texture_emulation) {
912 if (WGL_NO_TEXTURE_ARB != pop) {
913 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
914 return -1; /** error: don't support it */
915 } else {
916 drawattrib |= GLX_PBUFFER_BIT;
919 break ;
920 case WGL_FLOAT_COMPONENTS_NV:
921 nvfloatattrib = iWGLAttr[++cur];
922 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
923 break ;
924 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
925 case WGL_BIND_TO_TEXTURE_RGB_ARB:
926 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
927 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
928 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
929 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
930 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
931 pop = iWGLAttr[++cur];
932 /** cannot be converted, see direct handling on
933 * - wglGetPixelFormatAttribivARB
934 * TODO: wglChoosePixelFormat
936 break ;
937 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
938 pop = iWGLAttr[++cur];
939 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
940 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
941 break ;
943 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
944 pop = iWGLAttr[++cur];
945 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
946 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
947 break ;
948 default:
949 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
950 cur++;
951 break;
953 ++cur;
956 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
957 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
958 * pixmap and pbuffer formats appear as well. */
959 if (!drawattrib) drawattrib = GLX_DONT_CARE;
960 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
961 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
963 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
964 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
965 * GLX_DONT_CARE unless it is overridden. */
966 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
967 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
969 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
970 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
971 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
972 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
975 return nAttribs;
978 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
980 int render_type=0, render_type_bit;
981 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
982 switch(render_type_bit)
984 case GLX_RGBA_BIT:
985 render_type = GLX_RGBA_TYPE;
986 break;
987 case GLX_COLOR_INDEX_BIT:
988 render_type = GLX_COLOR_INDEX_TYPE;
989 break;
990 case GLX_RGBA_FLOAT_BIT:
991 render_type = GLX_RGBA_FLOAT_TYPE;
992 break;
993 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
994 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
995 break;
996 default:
997 ERR("Unknown render_type: %x\n", render_type_bit);
999 return render_type;
1002 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
1003 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
1005 int dbuf, value;
1006 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
1007 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1009 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1010 * the GLX_PIXMAP_BIT set. */
1011 return !dbuf && (value & GLX_PIXMAP_BIT);
1014 static void init_pixel_formats( Display *display )
1016 struct wgl_pixel_format *list;
1017 int size = 0, onscreen_size = 0;
1018 int fmt_id, nCfgs, i, run, bmp_formats;
1019 GLXFBConfig* cfgs;
1020 XVisualInfo *visinfo;
1022 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1023 if (NULL == cfgs || 0 == nCfgs) {
1024 if(cfgs != NULL) XFree(cfgs);
1025 ERR("glXChooseFBConfig returns NULL\n");
1026 return;
1029 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1030 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1031 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1032 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1034 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1036 for(i=0, bmp_formats=0; i<nCfgs; i++)
1038 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1039 bmp_formats++;
1041 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1043 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1045 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1046 * Do this as GLX doesn't guarantee that the list is sorted */
1047 for(run=0; run < 2; run++)
1049 for(i=0; i<nCfgs; i++) {
1050 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1051 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1053 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1054 * The second run we only set offscreen formats. */
1055 if(!run && visinfo)
1057 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1058 * The contents is copied to the destination using XCopyArea. For the copying to work
1059 * the depth of the source and destination window should be the same. In general this should
1060 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1061 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1062 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1063 * list formats with the same depth. */
1064 if(visinfo->depth != default_visual.depth)
1066 XFree(visinfo);
1067 continue;
1070 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1071 list[size].fbconfig = cfgs[i];
1072 list[size].fmt_id = fmt_id;
1073 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1074 list[size].dwFlags = 0;
1075 size++;
1076 onscreen_size++;
1078 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1079 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1081 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1082 list[size].fbconfig = cfgs[i];
1083 list[size].fmt_id = fmt_id;
1084 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1085 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1086 size++;
1087 onscreen_size++;
1089 } else if(run && !visinfo) {
1090 int window_drawable=0;
1091 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1093 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1094 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1095 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1096 * likely make our child window opengl rendering more complicated since likely you can't use
1097 * XCopyArea on a GLX Window.
1098 * For now ignore fbconfigs which are window drawable but lack a visual. */
1099 if(window_drawable & GLX_WINDOW_BIT)
1101 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1102 continue;
1105 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1106 list[size].fbconfig = cfgs[i];
1107 list[size].fmt_id = fmt_id;
1108 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1109 list[size].dwFlags = 0;
1110 size++;
1113 if (visinfo) XFree(visinfo);
1117 XFree(cfgs);
1119 pixel_formats = list;
1120 nb_pixel_formats = size;
1121 nb_onscreen_formats = onscreen_size;
1124 static inline BOOL is_valid_pixel_format( int format )
1126 return format > 0 && format <= nb_pixel_formats;
1129 static inline BOOL is_onscreen_pixel_format( int format )
1131 return format > 0 && format <= nb_onscreen_formats;
1134 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1136 return format - pixel_formats + 1;
1139 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1140 * In our WGL implementation we only support a subset of these formats namely the format of
1141 * Wine's main visual and offscreen formats (if they are available).
1142 * This function converts a WGL format to its corresponding GLX one.
1144 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1146 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1147 * iPixelFormat in case of probing the number of pixelformats.
1149 if (is_valid_pixel_format( iPixelFormat ) &&
1150 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1151 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1152 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1153 return &pixel_formats[iPixelFormat-1];
1155 return NULL;
1158 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1159 static void mark_drawable_dirty(Drawable old, Drawable new)
1161 struct wgl_context *ctx;
1163 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1165 if (old == ctx->drawables[0]) {
1166 ctx->drawables[0] = new;
1167 ctx->refresh_drawables = TRUE;
1169 if (old == ctx->drawables[1]) {
1170 ctx->drawables[1] = new;
1171 ctx->refresh_drawables = TRUE;
1176 /* Given the current context, make sure its drawable is sync'd */
1177 static inline void sync_context(struct wgl_context *context)
1179 if (context->refresh_drawables) {
1180 if (glxRequireVersion(3))
1181 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1182 context->drawables[1], context->ctx);
1183 else
1184 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1185 context->refresh_drawables = FALSE;
1189 static BOOL set_swap_interval(Drawable drawable, int interval)
1191 BOOL ret = TRUE;
1193 switch (swap_control_method)
1195 case GLX_SWAP_CONTROL_EXT:
1196 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1197 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1198 XSync(gdi_display, False);
1199 ret = !X11DRV_check_error();
1200 break;
1202 case GLX_SWAP_CONTROL_MESA:
1203 ret = !pglXSwapIntervalMESA(interval);
1204 break;
1206 case GLX_SWAP_CONTROL_SGI:
1207 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1208 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1209 * value to be an error. Just silently ignore the request for now.
1211 if (!interval)
1212 WARN("Request to disable vertical sync is not handled\n");
1213 else
1214 ret = !pglXSwapIntervalSGI(interval);
1215 break;
1217 case GLX_SWAP_CONTROL_NONE:
1218 /* Unlikely to happen on modern GLX implementations */
1219 WARN("Request to adjust swap interval is not handled\n");
1220 break;
1223 return ret;
1226 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1228 struct gl_drawable *gl;
1230 EnterCriticalSection( &context_section );
1231 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1232 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1233 LeaveCriticalSection( &context_section );
1234 return NULL;
1237 static void release_gl_drawable( struct gl_drawable *gl )
1239 if (gl) LeaveCriticalSection( &context_section );
1242 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1244 GLXContext ctx;
1246 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1247 BOOL indirect = !(context->fmt->dwFlags & PFD_DRAW_TO_BITMAP);
1249 if(context->gl3_context)
1251 if(context->numAttribs)
1252 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1253 else
1254 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1256 else if(context->vis)
1257 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1258 else /* Create a GLX Context for a pbuffer */
1259 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1261 return ctx;
1265 /***********************************************************************
1266 * free_gl_drawable
1268 static void free_gl_drawable( struct gl_drawable *gl )
1270 switch (gl->type)
1272 case DC_GL_CHILD_WIN:
1273 XDestroyWindow( gdi_display, gl->drawable );
1274 XFreeColormap( gdi_display, gl->colormap );
1275 break;
1276 case DC_GL_PIXMAP_WIN:
1277 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1278 XFreePixmap( gdi_display, gl->pixmap );
1279 break;
1280 default:
1281 break;
1283 if (gl->visual) XFree( gl->visual );
1284 HeapFree( GetProcessHeap(), 0, gl );
1288 /***********************************************************************
1289 * create_gl_drawable
1291 static BOOL create_gl_drawable( HWND hwnd, struct gl_drawable *gl )
1293 gl->drawable = 0;
1295 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1297 struct x11drv_win_data *data = get_win_data( hwnd );
1299 if (data)
1301 gl->type = DC_GL_WINDOW;
1302 gl->drawable = create_client_window( data, gl->visual );
1303 release_win_data( data );
1306 #ifdef SONAME_LIBXCOMPOSITE
1307 else if(usexcomposite)
1309 static Window dummy_parent;
1310 XSetWindowAttributes attrib;
1312 attrib.override_redirect = True;
1313 if (!dummy_parent)
1315 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1316 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1317 XMapWindow( gdi_display, dummy_parent );
1319 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1320 (gl->visual->class == PseudoColor ||
1321 gl->visual->class == GrayScale ||
1322 gl->visual->class == DirectColor) ?
1323 AllocAll : AllocNone);
1324 attrib.colormap = gl->colormap;
1325 XInstallColormap(gdi_display, attrib.colormap);
1327 gl->type = DC_GL_CHILD_WIN;
1328 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1329 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1330 0, gl->visual->depth, InputOutput, gl->visual->visual,
1331 CWColormap | CWOverrideRedirect, &attrib );
1332 if (gl->drawable)
1334 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1335 XMapWindow(gdi_display, gl->drawable);
1337 else XFreeColormap( gdi_display, gl->colormap );
1339 #endif
1340 else
1342 WARN("XComposite is not available, using GLXPixmap hack\n");
1344 gl->type = DC_GL_PIXMAP_WIN;
1345 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1346 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1347 gl->visual->depth );
1348 if (gl->pixmap)
1350 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1351 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1355 if (gl->drawable)
1356 gl->refresh_swap_interval = TRUE;
1357 return gl->drawable != 0;
1361 /***********************************************************************
1362 * set_win_format
1364 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1366 struct gl_drawable *gl, *prev;
1368 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1369 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1370 * there is no way to query it, so we have to store it here.
1372 gl->swap_interval = 1;
1373 gl->refresh_swap_interval = TRUE;
1374 gl->format = format;
1375 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1376 if (!gl->visual)
1378 HeapFree( GetProcessHeap(), 0, gl );
1379 return FALSE;
1382 GetClientRect( hwnd, &gl->rect );
1383 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1384 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1386 if (!create_gl_drawable( hwnd, gl ))
1388 XFree( gl->visual );
1389 HeapFree( GetProcessHeap(), 0, gl );
1390 return FALSE;
1393 TRACE("created GL drawable %lx for win %p format %x\n", gl->drawable, hwnd, format->fmt_id );
1395 XFlush( gdi_display );
1397 EnterCriticalSection( &context_section );
1398 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1400 gl->swap_interval = prev->swap_interval;
1401 free_gl_drawable( prev );
1403 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1404 LeaveCriticalSection( &context_section );
1406 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1407 return TRUE;
1410 /***********************************************************************
1411 * sync_gl_drawable
1413 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1415 struct gl_drawable *gl;
1416 Drawable glxp;
1417 Pixmap pix;
1418 int mask = 0;
1419 XWindowChanges changes;
1421 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1422 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1424 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1426 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1427 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1429 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1431 switch (gl->type)
1433 case DC_GL_CHILD_WIN:
1434 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1435 break;
1436 case DC_GL_PIXMAP_WIN:
1437 if (!mask) break;
1438 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1439 if (!pix) goto done;
1440 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1441 if (!glxp)
1443 XFreePixmap(gdi_display, pix);
1444 goto done;
1446 mark_drawable_dirty(gl->drawable, glxp);
1447 XFlush( gdi_display );
1449 XFreePixmap(gdi_display, gl->pixmap);
1450 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1451 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1453 gl->pixmap = pix;
1454 gl->drawable = glxp;
1455 break;
1456 default:
1457 break;
1459 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1460 done:
1461 release_gl_drawable( gl );
1465 /***********************************************************************
1466 * set_gl_drawable_parent
1468 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1470 struct gl_drawable *gl;
1471 Drawable old_drawable;
1473 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1475 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1477 old_drawable = gl->drawable;
1478 switch (gl->type)
1480 case DC_GL_WINDOW:
1481 break;
1482 case DC_GL_CHILD_WIN:
1483 if (parent != GetDesktopWindow()) goto done;
1484 XDestroyWindow( gdi_display, gl->drawable );
1485 XFreeColormap( gdi_display, gl->colormap );
1486 break;
1487 case DC_GL_PIXMAP_WIN:
1488 if (parent != GetDesktopWindow()) goto done;
1489 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1490 XFreePixmap( gdi_display, gl->pixmap );
1491 break;
1492 default:
1493 goto done;
1496 if (!create_gl_drawable( hwnd, gl ))
1498 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1499 release_gl_drawable( gl );
1500 XFree( gl->visual );
1501 HeapFree( GetProcessHeap(), 0, gl );
1502 __wine_set_pixel_format( hwnd, 0 );
1503 return;
1505 mark_drawable_dirty( old_drawable, gl->drawable );
1507 done:
1508 release_gl_drawable( gl );
1513 /***********************************************************************
1514 * destroy_gl_drawable
1516 void destroy_gl_drawable( HWND hwnd )
1518 struct gl_drawable *gl;
1520 EnterCriticalSection( &context_section );
1521 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1523 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1524 free_gl_drawable( gl );
1526 LeaveCriticalSection( &context_section );
1531 * glxdrv_DescribePixelFormat
1533 * Get the pixel-format descriptor associated to the given id
1535 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1536 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1538 /*XVisualInfo *vis;*/
1539 int value;
1540 int rb,gb,bb,ab;
1541 const struct wgl_pixel_format *fmt;
1543 if (!has_opengl()) return 0;
1545 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1547 if (!ppfd) return nb_onscreen_formats;
1549 /* 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 */
1550 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1551 if (!fmt) {
1552 WARN("unexpected format %d\n", iPixelFormat);
1553 return 0;
1556 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1557 ERR("Wrong structure size !\n");
1558 /* Should set error */
1559 return 0;
1562 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1563 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1564 ppfd->nVersion = 1;
1566 /* These flags are always the same... */
1567 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1568 /* Now the flags extracted from the Visual */
1570 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1571 if(value & GLX_WINDOW_BIT)
1572 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1574 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1575 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1576 * 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
1577 * offered the GDI bit either. */
1578 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1580 /* PFD_GENERIC_FORMAT - gdi software rendering
1581 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1582 * none set - full hardware accelerated by a ICD
1584 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1585 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1587 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1588 if (value) {
1589 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1590 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1592 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1594 /* Pixel type */
1595 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1596 if (value & GLX_RGBA_BIT)
1597 ppfd->iPixelType = PFD_TYPE_RGBA;
1598 else
1599 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1601 /* Color bits */
1602 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1603 ppfd->cColorBits = value;
1605 /* Red, green, blue and alpha bits / shifts */
1606 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1607 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1608 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1609 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1610 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1612 ppfd->cRedBits = rb;
1613 ppfd->cRedShift = gb + bb + ab;
1614 ppfd->cBlueBits = bb;
1615 ppfd->cBlueShift = ab;
1616 ppfd->cGreenBits = gb;
1617 ppfd->cGreenShift = bb + ab;
1618 ppfd->cAlphaBits = ab;
1619 ppfd->cAlphaShift = 0;
1620 } else {
1621 ppfd->cRedBits = 0;
1622 ppfd->cRedShift = 0;
1623 ppfd->cBlueBits = 0;
1624 ppfd->cBlueShift = 0;
1625 ppfd->cGreenBits = 0;
1626 ppfd->cGreenShift = 0;
1627 ppfd->cAlphaBits = 0;
1628 ppfd->cAlphaShift = 0;
1631 /* Accum RGBA bits */
1632 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1633 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1634 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1635 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1637 ppfd->cAccumBits = rb+gb+bb+ab;
1638 ppfd->cAccumRedBits = rb;
1639 ppfd->cAccumGreenBits = gb;
1640 ppfd->cAccumBlueBits = bb;
1641 ppfd->cAccumAlphaBits = ab;
1643 /* Aux bits */
1644 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1645 ppfd->cAuxBuffers = value;
1647 /* Depth bits */
1648 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1649 ppfd->cDepthBits = value;
1651 /* stencil bits */
1652 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1653 ppfd->cStencilBits = value;
1655 ppfd->iLayerType = PFD_MAIN_PLANE;
1657 if (TRACE_ON(wgl)) {
1658 dump_PIXELFORMATDESCRIPTOR(ppfd);
1661 return nb_onscreen_formats;
1664 /***********************************************************************
1665 * glxdrv_wglGetPixelFormat
1667 static int glxdrv_wglGetPixelFormat( HDC hdc )
1669 struct gl_drawable *gl;
1670 int ret = 0;
1672 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1674 ret = pixel_format_index( gl->format );
1675 /* Offscreen formats can't be used with traditional WGL calls.
1676 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1677 if (!is_onscreen_pixel_format( ret )) ret = 1;
1678 release_gl_drawable( gl );
1680 TRACE( "%p -> %d\n", hdc, ret );
1681 return ret;
1684 /***********************************************************************
1685 * glxdrv_wglSetPixelFormat
1687 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1689 const struct wgl_pixel_format *fmt;
1690 int value;
1691 struct gl_drawable *gl;
1692 HWND hwnd = WindowFromDC( hdc );
1694 TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
1696 if (!hwnd || hwnd == GetDesktopWindow())
1698 WARN( "not a proper window DC %p/%p\n", hdc, hwnd );
1699 return FALSE;
1702 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1703 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1704 if(!fmt) {
1705 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1706 return FALSE;
1709 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1710 if (!(value & GLX_WINDOW_BIT))
1712 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1713 return FALSE;
1716 if ((gl = get_gl_drawable( hwnd, hdc )))
1718 int prev = pixel_format_index( gl->format );
1719 release_gl_drawable( gl );
1720 return prev == iPixelFormat; /* cannot change it if already set */
1723 if (TRACE_ON(wgl)) {
1724 int gl_test = 0;
1726 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1727 if (gl_test) {
1728 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1729 } else {
1730 TRACE(" FBConfig have :\n");
1731 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1732 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1733 TRACE(" - VISUAL_ID 0x%x\n", value);
1734 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1735 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1739 return set_win_format( hwnd, fmt );
1742 /***********************************************************************
1743 * glxdrv_wglCopyContext
1745 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1747 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1749 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1751 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1752 return TRUE;
1755 /***********************************************************************
1756 * glxdrv_wglCreateContext
1758 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1760 struct wgl_context *ret = NULL;
1761 struct gl_drawable *gl;
1763 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1765 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1766 return NULL;
1769 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1771 ret->hdc = hdc;
1772 ret->fmt = gl->format;
1773 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1774 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1775 list_add_head( &context_list, &ret->entry );
1777 release_gl_drawable( gl );
1778 TRACE( "%p -> %p\n", hdc, ret );
1779 return ret;
1782 /***********************************************************************
1783 * glxdrv_wglDeleteContext
1785 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1787 TRACE("(%p)\n", ctx);
1789 EnterCriticalSection( &context_section );
1790 list_remove( &ctx->entry );
1791 LeaveCriticalSection( &context_section );
1793 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1794 if (ctx->vis) XFree( ctx->vis );
1795 HeapFree( GetProcessHeap(), 0, ctx );
1798 /***********************************************************************
1799 * glxdrv_wglGetProcAddress
1801 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1803 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1804 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1807 /***********************************************************************
1808 * glxdrv_wglMakeCurrent
1810 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1812 BOOL ret = FALSE;
1813 struct gl_drawable *gl;
1815 TRACE("(%p,%p)\n", hdc, ctx);
1817 if (!ctx)
1819 pglXMakeCurrent(gdi_display, None, NULL);
1820 NtCurrentTeb()->glContext = NULL;
1821 return TRUE;
1824 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1826 if (ctx->fmt != gl->format)
1828 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1829 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1830 goto done;
1833 if (TRACE_ON(wgl)) {
1834 describeContext(ctx);
1835 TRACE("hdc %p drawable %lx fmt %p ctx %p\n", hdc, gl->drawable, gl->format, ctx->ctx );
1838 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1839 if (ret)
1841 NtCurrentTeb()->glContext = ctx;
1842 ctx->has_been_current = TRUE;
1843 ctx->hdc = hdc;
1844 ctx->drawables[0] = gl->drawable;
1845 ctx->drawables[1] = gl->drawable;
1846 ctx->refresh_drawables = FALSE;
1847 goto done;
1850 SetLastError( ERROR_INVALID_HANDLE );
1852 done:
1853 release_gl_drawable( gl );
1854 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1855 return ret;
1858 /***********************************************************************
1859 * X11DRV_wglMakeContextCurrentARB
1861 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1863 BOOL ret = FALSE;
1864 struct gl_drawable *draw_gl, *read_gl = NULL;
1866 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1868 if (!ctx)
1870 pglXMakeCurrent(gdi_display, None, NULL);
1871 NtCurrentTeb()->glContext = NULL;
1872 return TRUE;
1875 if (!pglXMakeContextCurrent) return FALSE;
1877 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1879 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1880 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1881 read_gl ? read_gl->drawable : 0, ctx->ctx);
1882 if (ret)
1884 ctx->has_been_current = TRUE;
1885 ctx->hdc = draw_hdc;
1886 ctx->drawables[0] = draw_gl->drawable;
1887 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1888 ctx->refresh_drawables = FALSE;
1889 NtCurrentTeb()->glContext = ctx;
1890 goto done;
1893 SetLastError( ERROR_INVALID_HANDLE );
1894 done:
1895 release_gl_drawable( read_gl );
1896 release_gl_drawable( draw_gl );
1897 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1898 return ret;
1901 /***********************************************************************
1902 * glxdrv_wglShareLists
1904 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1906 TRACE("(%p, %p)\n", org, dest);
1908 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1909 * at context creation time but in case of WGL it is done using wglShareLists.
1910 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1911 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1912 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1913 * so there delaying context creation doesn't work.
1915 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1916 * and when a program requests sharing we recreate the destination context if it hasn't been made
1917 * current or when it hasn't shared display lists before.
1920 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1922 ERR("Could not share display lists, one of the contexts has been current already !\n");
1923 return FALSE;
1925 else if(dest->sharing)
1927 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1928 return FALSE;
1930 else
1932 describeContext(org);
1933 describeContext(dest);
1935 /* Re-create the GLX context and share display lists */
1936 pglXDestroyContext(gdi_display, dest->ctx);
1937 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1938 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1940 org->sharing = TRUE;
1941 dest->sharing = TRUE;
1942 return TRUE;
1944 return FALSE;
1947 static void wglFinish(void)
1949 struct x11drv_escape_flush_gl_drawable escape;
1950 struct gl_drawable *gl;
1951 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1953 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1954 escape.gl_drawable = 0;
1956 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1958 switch (gl->type)
1960 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1961 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1962 default: break;
1964 sync_context(ctx);
1965 release_gl_drawable( gl );
1968 pglFinish();
1969 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1972 static void wglFlush(void)
1974 struct x11drv_escape_flush_gl_drawable escape;
1975 struct gl_drawable *gl;
1976 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1978 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1979 escape.gl_drawable = 0;
1981 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1983 switch (gl->type)
1985 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1986 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1987 default: break;
1989 sync_context(ctx);
1990 release_gl_drawable( gl );
1993 pglFlush();
1994 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1997 static const GLubyte *wglGetString(GLenum name)
1999 if (name == GL_EXTENSIONS && WineGLInfo.glExtensions)
2000 return (const GLubyte *)WineGLInfo.glExtensions;
2001 return pglGetString(name);
2004 /***********************************************************************
2005 * X11DRV_wglCreateContextAttribsARB
2007 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2008 const int* attribList )
2010 struct wgl_context *ret = NULL;
2011 struct gl_drawable *gl;
2012 int err = 0;
2014 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2016 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2018 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2019 return NULL;
2022 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2024 ret->hdc = hdc;
2025 ret->fmt = gl->format;
2026 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2027 ret->gl3_context = TRUE;
2028 if (attribList)
2030 int *pContextAttribList = &ret->attribList[0];
2031 /* attribList consists of pairs {token, value] terminated with 0 */
2032 while(attribList[0] != 0)
2034 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2035 switch(attribList[0])
2037 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2038 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2039 pContextAttribList[1] = attribList[1];
2040 pContextAttribList += 2;
2041 ret->numAttribs++;
2042 break;
2043 case WGL_CONTEXT_MINOR_VERSION_ARB:
2044 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2045 pContextAttribList[1] = attribList[1];
2046 pContextAttribList += 2;
2047 ret->numAttribs++;
2048 break;
2049 case WGL_CONTEXT_LAYER_PLANE_ARB:
2050 break;
2051 case WGL_CONTEXT_FLAGS_ARB:
2052 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2053 pContextAttribList[1] = attribList[1];
2054 pContextAttribList += 2;
2055 ret->numAttribs++;
2056 break;
2057 case WGL_CONTEXT_PROFILE_MASK_ARB:
2058 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2059 pContextAttribList[1] = attribList[1];
2060 pContextAttribList += 2;
2061 ret->numAttribs++;
2062 break;
2063 default:
2064 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2066 attribList += 2;
2070 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2071 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2072 XSync(gdi_display, False);
2073 if ((err = X11DRV_check_error()) || !ret->ctx)
2075 /* In the future we should convert the GLX error to a win32 one here if needed */
2076 ERR("Context creation failed (error %x)\n", err);
2077 HeapFree( GetProcessHeap(), 0, ret );
2078 ret = NULL;
2080 else list_add_head( &context_list, &ret->entry );
2083 release_gl_drawable( gl );
2084 TRACE( "%p -> %p\n", hdc, ret );
2085 return ret;
2089 * X11DRV_wglGetExtensionsStringARB
2091 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2093 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2095 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2096 return WineGLInfo.wglExtensions;
2100 * X11DRV_wglCreatePbufferARB
2102 * WGL_ARB_pbuffer: wglCreatePbufferARB
2104 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2105 const int *piAttribList )
2107 struct wgl_pbuffer* object = NULL;
2108 const struct wgl_pixel_format *fmt = NULL;
2109 int attribs[256];
2110 int nAttribs = 0;
2112 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2114 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2115 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2116 if(!fmt) {
2117 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2118 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2119 return NULL;
2122 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2123 if (NULL == object) {
2124 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2125 return NULL;
2127 object->width = iWidth;
2128 object->height = iHeight;
2129 object->fmt = fmt;
2131 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2132 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2133 while (piAttribList && 0 != *piAttribList) {
2134 int attr_v;
2135 switch (*piAttribList) {
2136 case WGL_PBUFFER_LARGEST_ARB: {
2137 ++piAttribList;
2138 attr_v = *piAttribList;
2139 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2140 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2141 break;
2144 case WGL_TEXTURE_FORMAT_ARB: {
2145 ++piAttribList;
2146 attr_v = *piAttribList;
2147 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2148 if (WGL_NO_TEXTURE_ARB == attr_v) {
2149 object->use_render_texture = 0;
2150 } else {
2151 if (!use_render_texture_emulation) {
2152 SetLastError(ERROR_INVALID_DATA);
2153 goto create_failed;
2155 switch (attr_v) {
2156 case WGL_TEXTURE_RGB_ARB:
2157 object->use_render_texture = GL_RGB;
2158 object->texture_bpp = 3;
2159 object->texture_format = GL_RGB;
2160 object->texture_type = GL_UNSIGNED_BYTE;
2161 break;
2162 case WGL_TEXTURE_RGBA_ARB:
2163 object->use_render_texture = GL_RGBA;
2164 object->texture_bpp = 4;
2165 object->texture_format = GL_RGBA;
2166 object->texture_type = GL_UNSIGNED_BYTE;
2167 break;
2169 /* WGL_FLOAT_COMPONENTS_NV */
2170 case WGL_TEXTURE_FLOAT_R_NV:
2171 object->use_render_texture = GL_FLOAT_R_NV;
2172 object->texture_bpp = 4;
2173 object->texture_format = GL_RED;
2174 object->texture_type = GL_FLOAT;
2175 break;
2176 case WGL_TEXTURE_FLOAT_RG_NV:
2177 object->use_render_texture = GL_FLOAT_RG_NV;
2178 object->texture_bpp = 8;
2179 object->texture_format = GL_LUMINANCE_ALPHA;
2180 object->texture_type = GL_FLOAT;
2181 break;
2182 case WGL_TEXTURE_FLOAT_RGB_NV:
2183 object->use_render_texture = GL_FLOAT_RGB_NV;
2184 object->texture_bpp = 12;
2185 object->texture_format = GL_RGB;
2186 object->texture_type = GL_FLOAT;
2187 break;
2188 case WGL_TEXTURE_FLOAT_RGBA_NV:
2189 object->use_render_texture = GL_FLOAT_RGBA_NV;
2190 object->texture_bpp = 16;
2191 object->texture_format = GL_RGBA;
2192 object->texture_type = GL_FLOAT;
2193 break;
2194 default:
2195 ERR("Unknown texture format: %x\n", attr_v);
2196 SetLastError(ERROR_INVALID_DATA);
2197 goto create_failed;
2200 break;
2203 case WGL_TEXTURE_TARGET_ARB: {
2204 ++piAttribList;
2205 attr_v = *piAttribList;
2206 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2207 if (WGL_NO_TEXTURE_ARB == attr_v) {
2208 object->texture_target = 0;
2209 } else {
2210 if (!use_render_texture_emulation) {
2211 SetLastError(ERROR_INVALID_DATA);
2212 goto create_failed;
2214 switch (attr_v) {
2215 case WGL_TEXTURE_CUBE_MAP_ARB: {
2216 if (iWidth != iHeight) {
2217 SetLastError(ERROR_INVALID_DATA);
2218 goto create_failed;
2220 object->texture_target = GL_TEXTURE_CUBE_MAP;
2221 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2222 break;
2224 case WGL_TEXTURE_1D_ARB: {
2225 if (1 != iHeight) {
2226 SetLastError(ERROR_INVALID_DATA);
2227 goto create_failed;
2229 object->texture_target = GL_TEXTURE_1D;
2230 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2231 break;
2233 case WGL_TEXTURE_2D_ARB: {
2234 object->texture_target = GL_TEXTURE_2D;
2235 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2236 break;
2238 case WGL_TEXTURE_RECTANGLE_NV: {
2239 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2240 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2241 break;
2243 default:
2244 ERR("Unknown texture target: %x\n", attr_v);
2245 SetLastError(ERROR_INVALID_DATA);
2246 goto create_failed;
2249 break;
2252 case WGL_MIPMAP_TEXTURE_ARB: {
2253 ++piAttribList;
2254 attr_v = *piAttribList;
2255 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2256 if (!use_render_texture_emulation) {
2257 SetLastError(ERROR_INVALID_DATA);
2258 goto create_failed;
2260 break;
2263 ++piAttribList;
2266 PUSH1(attribs, None);
2267 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2268 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2269 if (!object->drawable) {
2270 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2271 goto create_failed; /* unexpected error */
2273 TRACE("->(%p)\n", object);
2274 return object;
2276 create_failed:
2277 HeapFree(GetProcessHeap(), 0, object);
2278 TRACE("->(FAILED)\n");
2279 return NULL;
2283 * X11DRV_wglDestroyPbufferARB
2285 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2287 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2289 TRACE("(%p)\n", object);
2291 pglXDestroyPbuffer(gdi_display, object->drawable);
2292 HeapFree(GetProcessHeap(), 0, object);
2293 return GL_TRUE;
2297 * X11DRV_wglGetPbufferDCARB
2299 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2301 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2303 struct x11drv_escape_set_drawable escape;
2304 struct gl_drawable *gl, *prev;
2305 HDC hdc;
2307 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2308 if (!hdc) return 0;
2310 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2312 DeleteDC( hdc );
2313 return 0;
2315 gl->type = DC_GL_PBUFFER;
2316 gl->drawable = object->drawable;
2317 gl->format = object->fmt;
2319 EnterCriticalSection( &context_section );
2320 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2321 free_gl_drawable( prev );
2322 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2323 LeaveCriticalSection( &context_section );
2325 escape.code = X11DRV_SET_DRAWABLE;
2326 escape.drawable = object->drawable;
2327 escape.mode = IncludeInferiors;
2328 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2329 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2331 TRACE( "(%p)->(%p)\n", object, hdc );
2332 return hdc;
2336 * X11DRV_wglQueryPbufferARB
2338 * WGL_ARB_pbuffer: wglQueryPbufferARB
2340 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2342 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2344 switch (iAttribute) {
2345 case WGL_PBUFFER_WIDTH_ARB:
2346 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2347 break;
2348 case WGL_PBUFFER_HEIGHT_ARB:
2349 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2350 break;
2352 case WGL_PBUFFER_LOST_ARB:
2353 /* GLX Pbuffers cannot be lost by default. We can support this by
2354 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2355 * to receive pixel buffer clobber events, however that may or may
2356 * not give any benefit */
2357 *piValue = GL_FALSE;
2358 break;
2360 case WGL_TEXTURE_FORMAT_ARB:
2361 if (!object->use_render_texture) {
2362 *piValue = WGL_NO_TEXTURE_ARB;
2363 } else {
2364 if (!use_render_texture_emulation) {
2365 SetLastError(ERROR_INVALID_HANDLE);
2366 return GL_FALSE;
2368 switch(object->use_render_texture) {
2369 case GL_RGB:
2370 *piValue = WGL_TEXTURE_RGB_ARB;
2371 break;
2372 case GL_RGBA:
2373 *piValue = WGL_TEXTURE_RGBA_ARB;
2374 break;
2375 /* WGL_FLOAT_COMPONENTS_NV */
2376 case GL_FLOAT_R_NV:
2377 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2378 break;
2379 case GL_FLOAT_RG_NV:
2380 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2381 break;
2382 case GL_FLOAT_RGB_NV:
2383 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2384 break;
2385 case GL_FLOAT_RGBA_NV:
2386 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2387 break;
2388 default:
2389 ERR("Unknown texture format: %x\n", object->use_render_texture);
2392 break;
2394 case WGL_TEXTURE_TARGET_ARB:
2395 if (!object->texture_target){
2396 *piValue = WGL_NO_TEXTURE_ARB;
2397 } else {
2398 if (!use_render_texture_emulation) {
2399 SetLastError(ERROR_INVALID_DATA);
2400 return GL_FALSE;
2402 switch (object->texture_target) {
2403 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2404 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2405 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2406 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2409 break;
2411 case WGL_MIPMAP_TEXTURE_ARB:
2412 *piValue = GL_FALSE; /** don't support that */
2413 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2414 break;
2416 default:
2417 FIXME("unexpected attribute %x\n", iAttribute);
2418 break;
2421 return GL_TRUE;
2425 * X11DRV_wglReleasePbufferDCARB
2427 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2429 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2431 struct gl_drawable *gl;
2433 TRACE("(%p, %p)\n", object, hdc);
2435 EnterCriticalSection( &context_section );
2437 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2439 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2440 free_gl_drawable( gl );
2442 else hdc = 0;
2444 LeaveCriticalSection( &context_section );
2446 return hdc && DeleteDC(hdc);
2450 * X11DRV_wglSetPbufferAttribARB
2452 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2454 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2456 GLboolean ret = GL_FALSE;
2458 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2460 if (!object->use_render_texture) {
2461 SetLastError(ERROR_INVALID_HANDLE);
2462 return GL_FALSE;
2464 if (use_render_texture_emulation) {
2465 return GL_TRUE;
2467 return ret;
2471 * X11DRV_wglChoosePixelFormatARB
2473 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2475 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2476 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2478 int attribs[256];
2479 int nAttribs = 0;
2480 GLXFBConfig* cfgs = NULL;
2481 int nCfgs = 0;
2482 int it;
2483 int fmt_id;
2484 int start, end;
2485 UINT pfmt_it = 0;
2486 int run;
2487 int i;
2488 DWORD dwFlags = 0;
2490 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2491 if (NULL != pfAttribFList) {
2492 FIXME("unused pfAttribFList\n");
2495 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2496 if (-1 == nAttribs) {
2497 WARN("Cannot convert WGL to GLX attributes\n");
2498 return GL_FALSE;
2500 PUSH1(attribs, None);
2502 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2503 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2504 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2506 for(i=0; piAttribIList[i] != 0; i+=2)
2508 switch(piAttribIList[i])
2510 case WGL_DRAW_TO_BITMAP_ARB:
2511 if(piAttribIList[i+1])
2512 dwFlags |= PFD_DRAW_TO_BITMAP;
2513 break;
2514 case WGL_ACCELERATION_ARB:
2515 switch(piAttribIList[i+1])
2517 case WGL_NO_ACCELERATION_ARB:
2518 dwFlags |= PFD_GENERIC_FORMAT;
2519 break;
2520 case WGL_GENERIC_ACCELERATION_ARB:
2521 dwFlags |= PFD_GENERIC_ACCELERATED;
2522 break;
2523 case WGL_FULL_ACCELERATION_ARB:
2524 /* Nothing to do */
2525 break;
2527 break;
2528 case WGL_SUPPORT_GDI_ARB:
2529 if(piAttribIList[i+1])
2530 dwFlags |= PFD_SUPPORT_GDI;
2531 break;
2535 /* Search for FB configurations matching the requirements in attribs */
2536 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2537 if (NULL == cfgs) {
2538 WARN("Compatible Pixel Format not found\n");
2539 return GL_FALSE;
2542 /* Loop through all matching formats and check if they are suitable.
2543 * Note that this function should at max return nMaxFormats different formats */
2544 for(run=0; run < 2; run++)
2546 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2548 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2550 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2551 continue;
2554 /* During the first run we only want onscreen formats and during the second only offscreen */
2555 start = run == 1 ? nb_onscreen_formats : 0;
2556 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2558 for (i = start; i < end; i++)
2560 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2562 piFormats[pfmt_it++] = i + 1;
2563 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2564 it + 1, nCfgs, fmt_id, i + 1);
2565 break;
2571 *nNumFormats = pfmt_it;
2572 /** free list */
2573 XFree(cfgs);
2574 return GL_TRUE;
2578 * X11DRV_wglGetPixelFormatAttribivARB
2580 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2582 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2583 UINT nAttributes, const int *piAttributes, int *piValues )
2585 UINT i;
2586 const struct wgl_pixel_format *fmt = NULL;
2587 int hTest;
2588 int tmp;
2589 int curGLXAttr = 0;
2591 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2593 if (0 < iLayerPlane) {
2594 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2595 return GL_FALSE;
2598 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2599 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2600 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2601 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2602 if(!fmt) {
2603 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2606 for (i = 0; i < nAttributes; ++i) {
2607 const int curWGLAttr = piAttributes[i];
2608 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2610 switch (curWGLAttr) {
2611 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2612 piValues[i] = nb_pixel_formats;
2613 continue;
2615 case WGL_SUPPORT_OPENGL_ARB:
2616 piValues[i] = GL_TRUE;
2617 continue;
2619 case WGL_ACCELERATION_ARB:
2620 curGLXAttr = GLX_CONFIG_CAVEAT;
2621 if (!fmt) goto pix_error;
2622 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2623 piValues[i] = WGL_NO_ACCELERATION_ARB;
2624 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2625 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2626 else
2627 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2628 continue;
2630 case WGL_TRANSPARENT_ARB:
2631 curGLXAttr = GLX_TRANSPARENT_TYPE;
2632 if (!fmt) goto pix_error;
2633 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2634 if (hTest) goto get_error;
2635 piValues[i] = GL_FALSE;
2636 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2637 continue;
2639 case WGL_PIXEL_TYPE_ARB:
2640 curGLXAttr = GLX_RENDER_TYPE;
2641 if (!fmt) goto pix_error;
2642 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2643 if (hTest) goto get_error;
2644 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2645 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2646 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2647 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2648 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2649 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2650 else {
2651 ERR("unexpected RenderType(%x)\n", tmp);
2652 piValues[i] = WGL_TYPE_RGBA_ARB;
2654 continue;
2656 case WGL_COLOR_BITS_ARB:
2657 curGLXAttr = GLX_BUFFER_SIZE;
2658 break;
2660 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2661 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2662 if (!use_render_texture_emulation) {
2663 piValues[i] = GL_FALSE;
2664 continue;
2666 curGLXAttr = GLX_RENDER_TYPE;
2667 if (!fmt) goto pix_error;
2668 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2669 if (hTest) goto get_error;
2670 if (GLX_COLOR_INDEX_BIT == tmp) {
2671 piValues[i] = GL_FALSE;
2672 continue;
2674 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2675 if (hTest) goto get_error;
2676 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2677 continue;
2679 case WGL_BLUE_BITS_ARB:
2680 curGLXAttr = GLX_BLUE_SIZE;
2681 break;
2682 case WGL_RED_BITS_ARB:
2683 curGLXAttr = GLX_RED_SIZE;
2684 break;
2685 case WGL_GREEN_BITS_ARB:
2686 curGLXAttr = GLX_GREEN_SIZE;
2687 break;
2688 case WGL_ALPHA_BITS_ARB:
2689 curGLXAttr = GLX_ALPHA_SIZE;
2690 break;
2691 case WGL_DEPTH_BITS_ARB:
2692 curGLXAttr = GLX_DEPTH_SIZE;
2693 break;
2694 case WGL_STENCIL_BITS_ARB:
2695 curGLXAttr = GLX_STENCIL_SIZE;
2696 break;
2697 case WGL_DOUBLE_BUFFER_ARB:
2698 curGLXAttr = GLX_DOUBLEBUFFER;
2699 break;
2700 case WGL_STEREO_ARB:
2701 curGLXAttr = GLX_STEREO;
2702 break;
2703 case WGL_AUX_BUFFERS_ARB:
2704 curGLXAttr = GLX_AUX_BUFFERS;
2705 break;
2707 case WGL_SUPPORT_GDI_ARB:
2708 if (!fmt) goto pix_error;
2709 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2710 continue;
2712 case WGL_DRAW_TO_BITMAP_ARB:
2713 if (!fmt) goto pix_error;
2714 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2715 continue;
2717 case WGL_DRAW_TO_WINDOW_ARB:
2718 case WGL_DRAW_TO_PBUFFER_ARB:
2719 if (!fmt) goto pix_error;
2720 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2721 if (hTest) goto get_error;
2722 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2723 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2724 piValues[i] = GL_TRUE;
2725 else
2726 piValues[i] = GL_FALSE;
2727 continue;
2729 case WGL_SWAP_METHOD_ARB:
2730 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2731 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2732 * point only ATI offers this.
2734 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2735 break;
2737 case WGL_PBUFFER_LARGEST_ARB:
2738 curGLXAttr = GLX_LARGEST_PBUFFER;
2739 break;
2741 case WGL_SAMPLE_BUFFERS_ARB:
2742 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2743 break;
2745 case WGL_SAMPLES_ARB:
2746 curGLXAttr = GLX_SAMPLES_ARB;
2747 break;
2749 case WGL_FLOAT_COMPONENTS_NV:
2750 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2751 break;
2753 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2754 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2755 break;
2757 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2758 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2759 break;
2761 case WGL_ACCUM_RED_BITS_ARB:
2762 curGLXAttr = GLX_ACCUM_RED_SIZE;
2763 break;
2764 case WGL_ACCUM_GREEN_BITS_ARB:
2765 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2766 break;
2767 case WGL_ACCUM_BLUE_BITS_ARB:
2768 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2769 break;
2770 case WGL_ACCUM_ALPHA_BITS_ARB:
2771 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2772 break;
2773 case WGL_ACCUM_BITS_ARB:
2774 if (!fmt) goto pix_error;
2775 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2776 if (hTest) goto get_error;
2777 piValues[i] = tmp;
2778 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2779 if (hTest) goto get_error;
2780 piValues[i] += tmp;
2781 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2782 if (hTest) goto get_error;
2783 piValues[i] += tmp;
2784 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2785 if (hTest) goto get_error;
2786 piValues[i] += tmp;
2787 continue;
2789 default:
2790 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2793 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2794 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2795 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2797 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2798 * and check which options can work using iPixelFormat=0 and which not.
2799 * A problem would be that this function is an extension. This would
2800 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2802 if (0 != curGLXAttr && iPixelFormat != 0) {
2803 if (!fmt) goto pix_error;
2804 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2805 if (hTest) goto get_error;
2806 curGLXAttr = 0;
2807 } else {
2808 piValues[i] = GL_FALSE;
2811 return GL_TRUE;
2813 get_error:
2814 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2815 return GL_FALSE;
2817 pix_error:
2818 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2819 return GL_FALSE;
2823 * X11DRV_wglGetPixelFormatAttribfvARB
2825 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2827 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2828 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2830 int *attr;
2831 int ret;
2832 UINT i;
2834 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2836 /* Allocate a temporary array to store integer values */
2837 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2838 if (!attr) {
2839 ERR("couldn't allocate %d array\n", nAttributes);
2840 return GL_FALSE;
2843 /* Piggy-back on wglGetPixelFormatAttribivARB */
2844 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2845 if (ret) {
2846 /* Convert integer values to float. Should also check for attributes
2847 that can give decimal values here */
2848 for (i=0; i<nAttributes;i++) {
2849 pfValues[i] = attr[i];
2853 HeapFree(GetProcessHeap(), 0, attr);
2854 return ret;
2858 * X11DRV_wglBindTexImageARB
2860 * WGL_ARB_render_texture: wglBindTexImageARB
2862 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2864 GLboolean ret = GL_FALSE;
2866 TRACE("(%p, %d)\n", object, iBuffer);
2868 if (!object->use_render_texture) {
2869 SetLastError(ERROR_INVALID_HANDLE);
2870 return GL_FALSE;
2873 if (use_render_texture_emulation) {
2874 static BOOL initialized = FALSE;
2875 int prev_binded_texture = 0;
2876 GLXContext prev_context;
2877 Drawable prev_drawable;
2878 GLXContext tmp_context;
2880 prev_context = pglXGetCurrentContext();
2881 prev_drawable = pglXGetCurrentDrawable();
2883 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2884 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2885 isn't ideal performance wise but I wasn't able to get other ways working.
2887 if(!initialized) {
2888 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2889 FIXME("partial stub!\n");
2892 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2893 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2895 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2897 /* Switch to our pbuffer */
2898 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2900 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2901 * After that upload the pbuffer texture data. */
2902 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2903 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2905 /* Switch back to the original drawable and upload the pbuffer-texture */
2906 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2907 pglXDestroyContext(gdi_display, tmp_context);
2908 return GL_TRUE;
2911 return ret;
2915 * X11DRV_wglReleaseTexImageARB
2917 * WGL_ARB_render_texture: wglReleaseTexImageARB
2919 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2921 GLboolean ret = GL_FALSE;
2923 TRACE("(%p, %d)\n", object, iBuffer);
2925 if (!object->use_render_texture) {
2926 SetLastError(ERROR_INVALID_HANDLE);
2927 return GL_FALSE;
2929 if (use_render_texture_emulation) {
2930 return GL_TRUE;
2932 return ret;
2936 * X11DRV_wglGetExtensionsStringEXT
2938 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2940 static const char *X11DRV_wglGetExtensionsStringEXT(void)
2942 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2943 return WineGLInfo.wglExtensions;
2947 * X11DRV_wglGetSwapIntervalEXT
2949 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2951 static int X11DRV_wglGetSwapIntervalEXT(void)
2953 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2954 struct gl_drawable *gl;
2955 int swap_interval;
2957 TRACE("()\n");
2959 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
2961 /* This can't happen because a current WGL context is required to get
2962 * here. Likely the application is buggy.
2964 WARN("No GL drawable found, returning swap interval 0\n");
2965 return 0;
2968 swap_interval = gl->swap_interval;
2969 release_gl_drawable(gl);
2971 return swap_interval;
2975 * X11DRV_wglSwapIntervalEXT
2977 * WGL_EXT_swap_control: wglSwapIntervalEXT
2979 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
2981 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2982 struct gl_drawable *gl;
2983 BOOL ret = TRUE;
2985 TRACE("(%d)\n", interval);
2987 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
2988 * is invalid.
2990 if (interval < 0 && !has_swap_control_tear)
2992 SetLastError(ERROR_INVALID_DATA);
2993 return FALSE;
2996 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
2998 SetLastError(ERROR_DC_NOT_FOUND);
2999 return FALSE;
3002 ret = set_swap_interval(gl->drawable, interval);
3003 gl->refresh_swap_interval = FALSE;
3004 if (ret)
3005 gl->swap_interval = interval;
3006 else
3007 SetLastError(ERROR_DC_NOT_FOUND);
3009 release_gl_drawable(gl);
3011 return ret;
3015 * X11DRV_wglSetPixelFormatWINE
3017 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3018 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3020 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3022 const struct wgl_pixel_format *fmt;
3023 int value;
3024 HWND hwnd;
3026 TRACE("(%p,%d)\n", hdc, format);
3028 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
3029 if (!fmt)
3031 ERR( "Invalid format %d\n", format );
3032 return FALSE;
3035 hwnd = WindowFromDC( hdc );
3036 if (!hwnd || hwnd == GetDesktopWindow())
3038 ERR( "not a valid window DC %p\n", hdc );
3039 return FALSE;
3042 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
3043 if (!(value & GLX_WINDOW_BIT))
3045 WARN( "Pixel format %d is not compatible for window rendering\n", format );
3046 return FALSE;
3049 return set_win_format( hwnd, fmt );
3053 * glxRequireVersion (internal)
3055 * Check if the supported GLX version matches requiredVersion.
3057 static BOOL glxRequireVersion(int requiredVersion)
3059 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3060 if(requiredVersion <= WineGLInfo.glxVersion[1])
3061 return TRUE;
3063 return FALSE;
3066 static void register_extension(const char *ext)
3068 if (WineGLInfo.wglExtensions[0])
3069 strcat(WineGLInfo.wglExtensions, " ");
3070 strcat(WineGLInfo.wglExtensions, ext);
3072 TRACE("'%s'\n", ext);
3076 * X11DRV_WineGL_LoadExtensions
3078 static void X11DRV_WineGL_LoadExtensions(void)
3080 WineGLInfo.wglExtensions[0] = 0;
3082 /* ARB Extensions */
3084 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
3086 register_extension( "WGL_ARB_create_context" );
3087 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3089 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
3090 register_extension("WGL_ARB_create_context_profile");
3093 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
3095 register_extension("WGL_ARB_pixel_format_float");
3096 register_extension("WGL_ATI_pixel_format_float");
3099 register_extension( "WGL_ARB_extensions_string" );
3100 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3102 if (glxRequireVersion(3))
3104 register_extension( "WGL_ARB_make_current_read" );
3105 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3106 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3109 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3111 if (glxRequireVersion(3))
3113 register_extension( "WGL_ARB_pbuffer" );
3114 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3115 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3116 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3117 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3118 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3119 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3122 register_extension( "WGL_ARB_pixel_format" );
3123 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3124 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3125 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3127 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3128 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3129 (glxRequireVersion(3) && use_render_texture_emulation))
3131 register_extension( "WGL_ARB_render_texture" );
3132 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3133 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3135 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3136 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3137 register_extension("WGL_NV_float_buffer");
3139 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3140 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3141 register_extension("WGL_NV_render_texture_rectangle");
3144 /* EXT Extensions */
3146 register_extension( "WGL_EXT_extensions_string" );
3147 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3149 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3150 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3151 register_extension( "WGL_EXT_swap_control" );
3152 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3153 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3155 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3156 register_extension("WGL_EXT_framebuffer_sRGB");
3158 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3159 register_extension("WGL_EXT_pixel_format_packed_float");
3161 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3163 swap_control_method = GLX_SWAP_CONTROL_EXT;
3164 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control_tear"))
3166 register_extension("WGL_EXT_swap_control_tear");
3167 has_swap_control_tear = TRUE;
3170 else if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_swap_control"))
3172 swap_control_method = GLX_SWAP_CONTROL_MESA;
3174 else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGI_swap_control"))
3176 swap_control_method = GLX_SWAP_CONTROL_SGI;
3179 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3180 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3182 register_extension( "WGL_NV_vertex_array_range" );
3183 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3184 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3187 /* WINE-specific WGL Extensions */
3189 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3190 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3192 register_extension( "WGL_WINE_pixel_format_passthrough" );
3193 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3198 * glxdrv_SwapBuffers
3200 * Swap the buffers of this DC
3202 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3204 struct x11drv_escape_flush_gl_drawable escape;
3205 struct gl_drawable *gl;
3206 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3208 TRACE("(%p)\n", hdc);
3210 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3211 escape.gl_drawable = 0;
3213 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3215 SetLastError( ERROR_INVALID_HANDLE );
3216 return FALSE;
3219 if (gl->refresh_swap_interval)
3221 set_swap_interval(gl->drawable, gl->swap_interval);
3222 gl->refresh_swap_interval = FALSE;
3225 switch (gl->type)
3227 case DC_GL_PIXMAP_WIN:
3228 if (ctx) sync_context( ctx );
3229 escape.gl_drawable = gl->pixmap;
3230 if (pglXCopySubBufferMESA) {
3231 /* (glX)SwapBuffers has an implicit glFlush effect, however
3232 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3233 * copying */
3234 pglFlush();
3235 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3236 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3237 break;
3239 pglXSwapBuffers(gdi_display, gl->drawable);
3240 break;
3241 case DC_GL_CHILD_WIN:
3242 if (ctx) sync_context( ctx );
3243 escape.gl_drawable = gl->drawable;
3244 /* fall through */
3245 default:
3246 pglXSwapBuffers(gdi_display, gl->drawable);
3247 break;
3250 release_gl_drawable( gl );
3252 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3253 return TRUE;
3256 static struct opengl_funcs opengl_funcs =
3259 glxdrv_wglCopyContext, /* p_wglCopyContext */
3260 glxdrv_wglCreateContext, /* p_wglCreateContext */
3261 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3262 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3263 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3264 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3265 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3266 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3267 glxdrv_wglShareLists, /* p_wglShareLists */
3268 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3272 struct opengl_funcs *get_glx_driver( UINT version )
3274 if (version != WINE_WGL_DRIVER_VERSION)
3276 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3277 return NULL;
3279 if (has_opengl()) return &opengl_funcs;
3280 return NULL;
3283 #else /* no OpenGL includes */
3285 struct opengl_funcs *get_glx_driver( UINT version )
3287 return NULL;
3290 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3294 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3298 void destroy_gl_drawable( HWND hwnd )
3302 #endif /* defined(SONAME_LIBGL) */