msi: Don't defer custom actions in the UI sequence if they match the currently runnin...
[wine/multimedia.git] / dlls / winex11.drv / opengl.c
blobcd245752913a42652f7f4f51f0e1d3ebe769d0d1
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;
257 /* X context to associate a struct gl_drawable to an hwnd */
258 static XContext gl_hwnd_context;
259 /* X context to associate a struct gl_drawable to a pbuffer hdc */
260 static XContext gl_pbuffer_context;
262 static const struct gdi_dc_funcs glxdrv_funcs;
264 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
266 return (struct glx_physdev *)dev;
269 static struct list context_list = LIST_INIT( context_list );
270 static struct WineGLInfo WineGLInfo = { 0 };
271 static struct wgl_pixel_format *pixel_formats;
272 static int nb_pixel_formats, nb_onscreen_formats;
273 static int use_render_texture_emulation = 1;
274 static BOOL has_swap_control;
276 static CRITICAL_SECTION context_section;
277 static CRITICAL_SECTION_DEBUG critsect_debug =
279 0, 0, &context_section,
280 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
281 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
283 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
285 static struct opengl_funcs opengl_funcs;
287 #define USE_GL_FUNC(name) #name,
288 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
289 #undef USE_GL_FUNC
291 static void X11DRV_WineGL_LoadExtensions(void);
292 static void init_pixel_formats( Display *display );
293 static BOOL glxRequireVersion(int requiredVersion);
295 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
296 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
297 TRACE(" - dwFlags : ");
298 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
299 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
300 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
301 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
302 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
303 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
304 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
305 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
306 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
307 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
308 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
309 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
310 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
311 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
312 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
313 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
315 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
316 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
318 #undef TEST_AND_DUMP
319 TRACE("\n");
321 TRACE(" - iPixelType : ");
322 switch (ppfd->iPixelType) {
323 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
324 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
326 TRACE("\n");
328 TRACE(" - Color : %d\n", ppfd->cColorBits);
329 TRACE(" - Red : %d\n", ppfd->cRedBits);
330 TRACE(" - Green : %d\n", ppfd->cGreenBits);
331 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
332 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
333 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
334 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
335 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
336 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
338 TRACE(" - iLayerType : ");
339 switch (ppfd->iLayerType) {
340 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
341 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
342 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
344 TRACE("\n");
347 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
348 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
350 /* GLX 1.0 */
351 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
352 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
353 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
354 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
355 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
356 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
357 static GLXPixmap (*pglXCreateGLXPixmap)( Display *dpy, XVisualInfo *visual, Pixmap pixmap );
358 static void (*pglXDestroyGLXPixmap)( Display *dpy, GLXPixmap pixmap );
359 static Bool (*pglXQueryExtension)( Display *dpy, int *errorb, int *event );
360 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
361 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
362 static int (*pglXGetConfig)( Display *dpy, XVisualInfo *visual, int attrib, int *value );
363 static GLXContext (*pglXGetCurrentContext)( void );
364 static GLXDrawable (*pglXGetCurrentDrawable)( void );
366 /* GLX 1.1 */
367 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
368 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
369 static const char *(*pglXGetClientString)( Display *dpy, int name );
371 /* GLX 1.3 */
372 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
373 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
374 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
375 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
376 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
377 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
378 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
379 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
380 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
382 /* GLX Extensions */
383 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
384 static void* (*pglXGetProcAddressARB)(const GLubyte *);
385 static int (*pglXSwapIntervalSGI)(int);
387 /* NV GLX Extension */
388 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
389 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
391 /* MESA GLX Extensions */
392 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
394 /* Standard OpenGL */
395 static void (*pglFinish)(void);
396 static void (*pglFlush)(void);
398 static void wglFinish(void);
399 static void wglFlush(void);
401 /* check if the extension is present in the list */
402 static BOOL has_extension( const char *list, const char *ext )
404 size_t len = strlen( ext );
406 while (list)
408 while (*list == ' ') list++;
409 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
410 list = strchr( list, ' ' );
412 return FALSE;
415 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
417 /* In the future we might want to find the exact X or GLX error to report back to the app */
418 return 1;
421 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
423 int screen = DefaultScreen(gdi_display);
424 Window win = 0, root = 0;
425 const char *gl_renderer;
426 const char* str;
427 XVisualInfo *vis;
428 GLXContext ctx = NULL;
429 XSetWindowAttributes attr;
430 BOOL ret = FALSE;
431 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
433 attr.override_redirect = True;
434 attr.colormap = None;
435 attr.border_pixel = 0;
437 vis = pglXChooseVisual(gdi_display, screen, attribList);
438 if (vis) {
439 #ifdef __i386__
440 WORD old_fs = wine_get_fs();
441 /* Create a GLX Context. Without one we can't query GL information */
442 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
443 if (wine_get_fs() != old_fs)
445 wine_set_fs( old_fs );
446 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
447 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
448 goto done;
450 #else
451 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
452 #endif
454 if (!ctx) goto done;
456 root = RootWindow( gdi_display, vis->screen );
457 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
458 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
459 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
460 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
461 XMapWindow( gdi_display, win );
462 else
463 win = root;
465 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
467 ERR_(winediag)( "Unable to activate OpenGL context, most likely your OpenGL drivers haven't been installed correctly\n" );
468 goto done;
470 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
471 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
472 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
473 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
474 strcpy(WineGLInfo.glExtensions, str);
476 /* Get the common GLX version supported by GLX client and server ( major/minor) */
477 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
479 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
480 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
481 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
483 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
484 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
485 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
487 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
488 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
490 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
491 TRACE("GL renderer : %s.\n", gl_renderer);
492 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
493 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
494 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
495 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
496 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
497 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
499 if(!WineGLInfo.glxDirect)
501 int fd = ConnectionNumber(gdi_display);
502 struct sockaddr_un uaddr;
503 unsigned int uaddrlen = sizeof(struct sockaddr_un);
505 /* In general indirect rendering on a local X11 server indicates a driver problem.
506 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
508 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
509 ERR_(winediag)("Direct rendering is disabled, most likely your OpenGL drivers "
510 "haven't been installed correctly (using GL renderer %s, version %s).\n",
511 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
513 else
515 /* In general you would expect that if direct rendering is returned, that you receive hardware
516 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
517 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
518 * software rendering, it shows direct rendering.
520 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
521 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
522 * it shows 'Mesa X11'.
524 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
525 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your OpenGL "
526 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
527 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
529 ret = TRUE;
531 done:
532 if(vis) XFree(vis);
533 if(ctx) {
534 pglXMakeCurrent(gdi_display, None, NULL);
535 pglXDestroyContext(gdi_display, ctx);
537 if (win != root) XDestroyWindow( gdi_display, win );
538 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
539 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
540 return ret;
543 static BOOL has_opengl(void)
545 static int init_done;
546 static void *opengl_handle;
548 char buffer[200];
549 int error_base, event_base;
550 unsigned int i;
552 if (init_done) return (opengl_handle != NULL);
553 init_done = 1;
555 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
556 and include all dependencies */
557 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
558 if (opengl_handle == NULL)
560 ERR( "Failed to load libGL: %s\n", buffer );
561 ERR( "OpenGL support is disabled.\n");
562 return FALSE;
565 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
567 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
569 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
570 goto failed;
574 /* redirect some standard OpenGL functions */
575 #define REDIRECT(func) \
576 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
577 REDIRECT( glFinish );
578 REDIRECT( glFlush );
579 #undef REDIRECT
581 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
582 if (pglXGetProcAddressARB == NULL) {
583 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
584 goto failed;
587 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
589 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
590 goto failed; \
591 } while(0)
593 /* GLX 1.0 */
594 LOAD_FUNCPTR(glXChooseVisual);
595 LOAD_FUNCPTR(glXCopyContext);
596 LOAD_FUNCPTR(glXCreateContext);
597 LOAD_FUNCPTR(glXCreateGLXPixmap);
598 LOAD_FUNCPTR(glXGetCurrentContext);
599 LOAD_FUNCPTR(glXGetCurrentDrawable);
600 LOAD_FUNCPTR(glXDestroyContext);
601 LOAD_FUNCPTR(glXDestroyGLXPixmap);
602 LOAD_FUNCPTR(glXGetConfig);
603 LOAD_FUNCPTR(glXIsDirect);
604 LOAD_FUNCPTR(glXMakeCurrent);
605 LOAD_FUNCPTR(glXSwapBuffers);
606 LOAD_FUNCPTR(glXQueryExtension);
607 LOAD_FUNCPTR(glXQueryVersion);
609 /* GLX 1.1 */
610 LOAD_FUNCPTR(glXGetClientString);
611 LOAD_FUNCPTR(glXQueryExtensionsString);
612 LOAD_FUNCPTR(glXQueryServerString);
614 /* GLX 1.3 */
615 LOAD_FUNCPTR(glXCreatePbuffer);
616 LOAD_FUNCPTR(glXCreateNewContext);
617 LOAD_FUNCPTR(glXDestroyPbuffer);
618 LOAD_FUNCPTR(glXMakeContextCurrent);
619 LOAD_FUNCPTR(glXGetFBConfigs);
620 #undef LOAD_FUNCPTR
622 /* It doesn't matter if these fail. They'll only be used if the driver reports
623 the associated extension is available (and if a driver reports the extension
624 is available but fails to provide the functions, it's quite broken) */
625 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
626 /* ARB GLX Extension */
627 LOAD_FUNCPTR(glXCreateContextAttribsARB);
628 /* SGI GLX Extension */
629 LOAD_FUNCPTR(glXSwapIntervalSGI);
630 /* NV GLX Extension */
631 LOAD_FUNCPTR(glXAllocateMemoryNV);
632 LOAD_FUNCPTR(glXFreeMemoryNV);
633 #undef LOAD_FUNCPTR
635 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
637 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
638 TRACE("GLX is up and running error_base = %d\n", error_base);
639 } else {
640 ERR( "GLX extension is missing, disabling OpenGL.\n" );
641 goto failed;
643 gl_hwnd_context = XUniqueContext();
644 gl_pbuffer_context = XUniqueContext();
646 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
647 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
648 * rendering is used.
650 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
651 * depends on the reported GLX client / server version and on the client / server extension list.
652 * Those don't have to be the same.
654 * In general the server GLX information lists the capabilities in case of indirect rendering.
655 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
656 * available and in that case the client GLX informat can be used.
657 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
658 * in the GLX version/extension list. When a program does this it works for certain for both
659 * direct and indirect rendering.
661 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
662 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
663 * extension list which causes this extension not to be listed. (Wine requires this extension).
664 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
665 * pbuffers is around.
667 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
668 * the ATI drivers and from then on use GLX client information for them.
671 if(glxRequireVersion(3)) {
672 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
673 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
674 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
675 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
676 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
677 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
678 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
679 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
681 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
682 * enable this function when the Xserver understand GLX 1.3 or newer
684 pglXQueryDrawable = NULL;
685 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
686 TRACE("Overriding ATI GLX capabilities!\n");
687 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
688 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
689 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
690 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
692 /* Use client GLX information in case of the ATI drivers. We override the
693 * capabilities over here and not somewhere else as ATI might better their
694 * life in the future. In case they release proper drivers this block of
695 * code won't be called. */
696 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
697 } else {
698 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
701 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
702 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
705 X11DRV_WineGL_LoadExtensions();
706 init_pixel_formats( gdi_display );
707 return TRUE;
709 failed:
710 wine_dlclose(opengl_handle, NULL, 0);
711 opengl_handle = NULL;
712 return FALSE;
715 static int describeContext( struct wgl_context *ctx ) {
716 int tmp;
717 int ctx_vis_id;
718 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
719 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
720 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
721 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
722 TRACE(" - VISUAL_ID 0x%x\n", tmp);
723 ctx_vis_id = tmp;
724 return ctx_vis_id;
727 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
728 int nAttribs = 0;
729 unsigned cur = 0;
730 int pop;
731 int drawattrib = 0;
732 int nvfloatattrib = GLX_DONT_CARE;
733 int pixelattrib = GLX_DONT_CARE;
735 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
736 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
737 while (iWGLAttr && 0 != iWGLAttr[cur]) {
738 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
740 switch (iWGLAttr[cur]) {
741 case WGL_AUX_BUFFERS_ARB:
742 pop = iWGLAttr[++cur];
743 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
744 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
745 break;
746 case WGL_COLOR_BITS_ARB:
747 pop = iWGLAttr[++cur];
748 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
749 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
750 break;
751 case WGL_BLUE_BITS_ARB:
752 pop = iWGLAttr[++cur];
753 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
754 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
755 break;
756 case WGL_RED_BITS_ARB:
757 pop = iWGLAttr[++cur];
758 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
759 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
760 break;
761 case WGL_GREEN_BITS_ARB:
762 pop = iWGLAttr[++cur];
763 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
764 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
765 break;
766 case WGL_ALPHA_BITS_ARB:
767 pop = iWGLAttr[++cur];
768 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
769 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
770 break;
771 case WGL_DEPTH_BITS_ARB:
772 pop = iWGLAttr[++cur];
773 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
774 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
775 break;
776 case WGL_STENCIL_BITS_ARB:
777 pop = iWGLAttr[++cur];
778 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
779 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
780 break;
781 case WGL_DOUBLE_BUFFER_ARB:
782 pop = iWGLAttr[++cur];
783 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
784 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
785 break;
786 case WGL_STEREO_ARB:
787 pop = iWGLAttr[++cur];
788 PUSH2(oGLXAttr, GLX_STEREO, pop);
789 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
790 break;
792 case WGL_PIXEL_TYPE_ARB:
793 pop = iWGLAttr[++cur];
794 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
795 switch (pop) {
796 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
797 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
798 /* 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 */
799 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
800 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
801 default:
802 ERR("unexpected PixelType(%x)\n", pop);
803 pop = 0;
805 break;
807 case WGL_SUPPORT_GDI_ARB:
808 /* This flag is set in a pixel format */
809 pop = iWGLAttr[++cur];
810 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
811 break;
813 case WGL_DRAW_TO_BITMAP_ARB:
814 /* This flag is set in a pixel format */
815 pop = iWGLAttr[++cur];
816 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
817 break;
819 case WGL_DRAW_TO_WINDOW_ARB:
820 pop = iWGLAttr[++cur];
821 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
822 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
823 if (pop) {
824 drawattrib |= GLX_WINDOW_BIT;
826 break;
828 case WGL_DRAW_TO_PBUFFER_ARB:
829 pop = iWGLAttr[++cur];
830 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
831 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
832 if (pop) {
833 drawattrib |= GLX_PBUFFER_BIT;
835 break;
837 case WGL_ACCELERATION_ARB:
838 /* This flag is set in a pixel format */
839 pop = iWGLAttr[++cur];
840 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
841 break;
843 case WGL_SUPPORT_OPENGL_ARB:
844 pop = iWGLAttr[++cur];
845 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
846 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
847 break;
849 case WGL_SWAP_METHOD_ARB:
850 pop = iWGLAttr[++cur];
851 /* For now we ignore this and just return SWAP_EXCHANGE */
852 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
853 break;
855 case WGL_PBUFFER_LARGEST_ARB:
856 pop = iWGLAttr[++cur];
857 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
858 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
859 break;
861 case WGL_SAMPLE_BUFFERS_ARB:
862 pop = iWGLAttr[++cur];
863 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
864 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
865 break;
867 case WGL_SAMPLES_ARB:
868 pop = iWGLAttr[++cur];
869 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
870 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
871 break;
873 case WGL_TEXTURE_FORMAT_ARB:
874 case WGL_TEXTURE_TARGET_ARB:
875 case WGL_MIPMAP_TEXTURE_ARB:
876 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
877 pop = iWGLAttr[++cur];
878 if (NULL == pbuf) {
879 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
881 if (!use_render_texture_emulation) {
882 if (WGL_NO_TEXTURE_ARB != pop) {
883 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
884 return -1; /** error: don't support it */
885 } else {
886 drawattrib |= GLX_PBUFFER_BIT;
889 break ;
890 case WGL_FLOAT_COMPONENTS_NV:
891 nvfloatattrib = iWGLAttr[++cur];
892 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
893 break ;
894 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
895 case WGL_BIND_TO_TEXTURE_RGB_ARB:
896 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
897 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
898 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
899 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
900 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
901 pop = iWGLAttr[++cur];
902 /** cannot be converted, see direct handling on
903 * - wglGetPixelFormatAttribivARB
904 * TODO: wglChoosePixelFormat
906 break ;
907 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
908 pop = iWGLAttr[++cur];
909 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
910 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
911 break ;
913 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
914 pop = iWGLAttr[++cur];
915 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
916 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
917 break ;
918 default:
919 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
920 break;
922 ++cur;
925 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
926 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
927 * pixmap and pbuffer formats appear as well. */
928 if (!drawattrib) drawattrib = GLX_DONT_CARE;
929 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
930 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
932 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
933 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
934 * GLX_DONT_CARE unless it is overridden. */
935 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
936 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
938 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
939 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
940 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
941 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
944 return nAttribs;
947 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
949 int render_type=0, render_type_bit;
950 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
951 switch(render_type_bit)
953 case GLX_RGBA_BIT:
954 render_type = GLX_RGBA_TYPE;
955 break;
956 case GLX_COLOR_INDEX_BIT:
957 render_type = GLX_COLOR_INDEX_TYPE;
958 break;
959 case GLX_RGBA_FLOAT_BIT:
960 render_type = GLX_RGBA_FLOAT_TYPE;
961 break;
962 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
963 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
964 break;
965 default:
966 ERR("Unknown render_type: %x\n", render_type_bit);
968 return render_type;
971 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
972 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
974 int dbuf, value;
975 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
976 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
978 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
979 * the GLX_PIXMAP_BIT set. */
980 return !dbuf && (value & GLX_PIXMAP_BIT);
983 static void init_pixel_formats( Display *display )
985 struct wgl_pixel_format *list;
986 int size = 0, onscreen_size = 0;
987 int fmt_id, nCfgs, i, run, bmp_formats;
988 GLXFBConfig* cfgs;
989 XVisualInfo *visinfo;
991 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
992 if (NULL == cfgs || 0 == nCfgs) {
993 if(cfgs != NULL) XFree(cfgs);
994 ERR("glXChooseFBConfig returns NULL\n");
995 return;
998 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
999 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1000 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1001 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1003 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1005 for(i=0, bmp_formats=0; i<nCfgs; i++)
1007 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1008 bmp_formats++;
1010 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1012 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1014 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1015 * Do this as GLX doesn't guarantee that the list is sorted */
1016 for(run=0; run < 2; run++)
1018 for(i=0; i<nCfgs; i++) {
1019 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1020 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1022 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1023 * The second run we only set offscreen formats. */
1024 if(!run && visinfo)
1026 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1027 * The contents is copied to the destination using XCopyArea. For the copying to work
1028 * the depth of the source and destination window should be the same. In general this should
1029 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1030 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1031 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1032 * list formats with the same depth. */
1033 if(visinfo->depth != default_visual.depth)
1035 XFree(visinfo);
1036 continue;
1039 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1040 list[size].fbconfig = cfgs[i];
1041 list[size].fmt_id = fmt_id;
1042 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1043 list[size].dwFlags = 0;
1044 size++;
1045 onscreen_size++;
1047 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1048 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1050 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1051 list[size].fbconfig = cfgs[i];
1052 list[size].fmt_id = fmt_id;
1053 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1054 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1055 size++;
1056 onscreen_size++;
1058 } else if(run && !visinfo) {
1059 int window_drawable=0;
1060 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1062 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1063 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1064 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1065 * likely make our child window opengl rendering more complicated since likely you can't use
1066 * XCopyArea on a GLX Window.
1067 * For now ignore fbconfigs which are window drawable but lack a visual. */
1068 if(window_drawable & GLX_WINDOW_BIT)
1070 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1071 continue;
1074 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1075 list[size].fbconfig = cfgs[i];
1076 list[size].fmt_id = fmt_id;
1077 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1078 list[size].dwFlags = 0;
1079 size++;
1082 if (visinfo) XFree(visinfo);
1086 XFree(cfgs);
1088 pixel_formats = list;
1089 nb_pixel_formats = size;
1090 nb_onscreen_formats = onscreen_size;
1093 static inline BOOL is_valid_pixel_format( int format )
1095 return format > 0 && format <= nb_pixel_formats;
1098 static inline BOOL is_onscreen_pixel_format( int format )
1100 return format > 0 && format <= nb_onscreen_formats;
1103 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1105 return format - pixel_formats + 1;
1108 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1109 * In our WGL implementation we only support a subset of these formats namely the format of
1110 * Wine's main visual and offscreen formats (if they are available).
1111 * This function converts a WGL format to its corresponding GLX one.
1113 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1115 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1116 * iPixelFormat in case of probing the number of pixelformats.
1118 if (is_valid_pixel_format( iPixelFormat ) &&
1119 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1120 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1121 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1122 return &pixel_formats[iPixelFormat-1];
1124 return NULL;
1127 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1128 static void mark_drawable_dirty(Drawable old, Drawable new)
1130 struct wgl_context *ctx;
1132 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1134 if (old == ctx->drawables[0]) {
1135 ctx->drawables[0] = new;
1136 ctx->refresh_drawables = TRUE;
1138 if (old == ctx->drawables[1]) {
1139 ctx->drawables[1] = new;
1140 ctx->refresh_drawables = TRUE;
1145 /* Given the current context, make sure its drawable is sync'd */
1146 static inline void sync_context(struct wgl_context *context)
1148 if (context->refresh_drawables) {
1149 if (glxRequireVersion(3))
1150 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1151 context->drawables[1], context->ctx);
1152 else
1153 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1154 context->refresh_drawables = FALSE;
1158 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1160 struct gl_drawable *gl;
1162 EnterCriticalSection( &context_section );
1163 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1164 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1165 LeaveCriticalSection( &context_section );
1166 return NULL;
1169 static void release_gl_drawable( struct gl_drawable *gl )
1171 if (gl) LeaveCriticalSection( &context_section );
1174 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1176 GLXContext ctx;
1178 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1179 BOOL indirect = !(context->fmt->dwFlags & PFD_DRAW_TO_BITMAP);
1181 if(context->gl3_context)
1183 if(context->numAttribs)
1184 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1185 else
1186 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1188 else if(context->vis)
1189 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1190 else /* Create a GLX Context for a pbuffer */
1191 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1193 return ctx;
1197 /***********************************************************************
1198 * free_gl_drawable
1200 static void free_gl_drawable( struct gl_drawable *gl )
1202 switch (gl->type)
1204 case DC_GL_CHILD_WIN:
1205 XDestroyWindow( gdi_display, gl->drawable );
1206 XFreeColormap( gdi_display, gl->colormap );
1207 break;
1208 case DC_GL_PIXMAP_WIN:
1209 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1210 XFreePixmap( gdi_display, gl->pixmap );
1211 break;
1212 default:
1213 break;
1215 if (gl->visual) XFree( gl->visual );
1216 HeapFree( GetProcessHeap(), 0, gl );
1220 /***********************************************************************
1221 * create_gl_drawable
1223 static BOOL create_gl_drawable( HWND hwnd, HWND parent, struct gl_drawable *gl )
1225 gl->drawable = 0;
1226 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1227 * there is no way to query it, so we have to store it here.
1229 gl->swap_interval = 1;
1231 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1233 struct x11drv_win_data *data = get_win_data( hwnd );
1235 if (data)
1237 gl->type = DC_GL_WINDOW;
1238 gl->drawable = create_client_window( data, gl->visual );
1239 release_win_data( data );
1242 #ifdef SONAME_LIBXCOMPOSITE
1243 else if(usexcomposite)
1245 static Window dummy_parent;
1246 XSetWindowAttributes attrib;
1248 attrib.override_redirect = True;
1249 if (!dummy_parent)
1251 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1252 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1253 XMapWindow( gdi_display, dummy_parent );
1255 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1256 (gl->visual->class == PseudoColor ||
1257 gl->visual->class == GrayScale ||
1258 gl->visual->class == DirectColor) ?
1259 AllocAll : AllocNone);
1260 attrib.colormap = gl->colormap;
1261 XInstallColormap(gdi_display, attrib.colormap);
1263 gl->type = DC_GL_CHILD_WIN;
1264 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1265 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1266 0, gl->visual->depth, InputOutput, gl->visual->visual,
1267 CWColormap | CWOverrideRedirect, &attrib );
1268 if (gl->drawable)
1270 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1271 XMapWindow(gdi_display, gl->drawable);
1273 else XFreeColormap( gdi_display, gl->colormap );
1275 #endif
1276 else
1278 WARN("XComposite is not available, using GLXPixmap hack\n");
1280 gl->type = DC_GL_PIXMAP_WIN;
1281 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1282 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1283 gl->visual->depth );
1284 if (gl->pixmap)
1286 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1287 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1291 return gl->drawable != 0;
1295 /***********************************************************************
1296 * set_win_format
1298 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1300 HWND parent = GetAncestor( hwnd, GA_PARENT );
1301 struct gl_drawable *gl, *prev;
1303 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1304 gl->format = format;
1305 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1306 if (!gl->visual)
1308 HeapFree( GetProcessHeap(), 0, gl );
1309 return FALSE;
1312 GetClientRect( hwnd, &gl->rect );
1313 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1314 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1316 if (!create_gl_drawable( hwnd, parent, gl ))
1318 XFree( gl->visual );
1319 HeapFree( GetProcessHeap(), 0, gl );
1320 return FALSE;
1323 TRACE("created GL drawable %lx for win %p format %x\n", gl->drawable, hwnd, format->fmt_id );
1325 XFlush( gdi_display );
1327 EnterCriticalSection( &context_section );
1328 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1329 free_gl_drawable( prev );
1330 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1331 LeaveCriticalSection( &context_section );
1333 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1334 return TRUE;
1337 /***********************************************************************
1338 * sync_gl_drawable
1340 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1342 struct gl_drawable *gl;
1343 Drawable glxp;
1344 Pixmap pix;
1345 int mask = 0;
1346 XWindowChanges changes;
1348 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1349 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1351 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1353 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1354 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1356 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1358 switch (gl->type)
1360 case DC_GL_CHILD_WIN:
1361 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1362 break;
1363 case DC_GL_PIXMAP_WIN:
1364 if (!mask) break;
1365 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1366 if (!pix) goto done;
1367 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1368 if (!glxp)
1370 XFreePixmap(gdi_display, pix);
1371 goto done;
1373 mark_drawable_dirty(gl->drawable, glxp);
1374 XFlush( gdi_display );
1376 XFreePixmap(gdi_display, gl->pixmap);
1377 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1378 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1380 gl->pixmap = pix;
1381 gl->drawable = glxp;
1382 break;
1383 default:
1384 break;
1386 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1387 done:
1388 release_gl_drawable( gl );
1392 /***********************************************************************
1393 * set_gl_drawable_parent
1395 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1397 struct gl_drawable *gl;
1398 Drawable old_drawable;
1400 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1402 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1404 old_drawable = gl->drawable;
1405 switch (gl->type)
1407 case DC_GL_WINDOW:
1408 break;
1409 case DC_GL_CHILD_WIN:
1410 if (parent != GetDesktopWindow()) goto done;
1411 XDestroyWindow( gdi_display, gl->drawable );
1412 XFreeColormap( gdi_display, gl->colormap );
1413 break;
1414 case DC_GL_PIXMAP_WIN:
1415 if (parent != GetDesktopWindow()) goto done;
1416 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1417 XFreePixmap( gdi_display, gl->pixmap );
1418 break;
1419 default:
1420 goto done;
1423 if (!create_gl_drawable( hwnd, parent, gl ))
1425 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1426 release_gl_drawable( gl );
1427 XFree( gl->visual );
1428 HeapFree( GetProcessHeap(), 0, gl );
1429 __wine_set_pixel_format( hwnd, 0 );
1430 return;
1432 mark_drawable_dirty( old_drawable, gl->drawable );
1434 done:
1435 release_gl_drawable( gl );
1440 /***********************************************************************
1441 * destroy_gl_drawable
1443 void destroy_gl_drawable( HWND hwnd )
1445 struct gl_drawable *gl;
1447 EnterCriticalSection( &context_section );
1448 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1450 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1451 free_gl_drawable( gl );
1453 LeaveCriticalSection( &context_section );
1458 * glxdrv_DescribePixelFormat
1460 * Get the pixel-format descriptor associated to the given id
1462 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1463 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1465 /*XVisualInfo *vis;*/
1466 int value;
1467 int rb,gb,bb,ab;
1468 const struct wgl_pixel_format *fmt;
1470 if (!has_opengl()) return 0;
1472 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1474 if (!ppfd) return nb_onscreen_formats;
1476 /* 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 */
1477 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1478 if (!fmt) {
1479 WARN("unexpected format %d\n", iPixelFormat);
1480 return 0;
1483 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1484 ERR("Wrong structure size !\n");
1485 /* Should set error */
1486 return 0;
1489 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1490 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1491 ppfd->nVersion = 1;
1493 /* These flags are always the same... */
1494 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1495 /* Now the flags extracted from the Visual */
1497 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1498 if(value & GLX_WINDOW_BIT)
1499 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1501 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1502 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1503 * 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
1504 * offered the GDI bit either. */
1505 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1507 /* PFD_GENERIC_FORMAT - gdi software rendering
1508 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1509 * none set - full hardware accelerated by a ICD
1511 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1512 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1514 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1515 if (value) {
1516 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1517 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1519 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1521 /* Pixel type */
1522 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1523 if (value & GLX_RGBA_BIT)
1524 ppfd->iPixelType = PFD_TYPE_RGBA;
1525 else
1526 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1528 /* Color bits */
1529 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1530 ppfd->cColorBits = value;
1532 /* Red, green, blue and alpha bits / shifts */
1533 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1534 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1535 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1536 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1537 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1539 ppfd->cRedBits = rb;
1540 ppfd->cRedShift = gb + bb + ab;
1541 ppfd->cBlueBits = bb;
1542 ppfd->cBlueShift = ab;
1543 ppfd->cGreenBits = gb;
1544 ppfd->cGreenShift = bb + ab;
1545 ppfd->cAlphaBits = ab;
1546 ppfd->cAlphaShift = 0;
1547 } else {
1548 ppfd->cRedBits = 0;
1549 ppfd->cRedShift = 0;
1550 ppfd->cBlueBits = 0;
1551 ppfd->cBlueShift = 0;
1552 ppfd->cGreenBits = 0;
1553 ppfd->cGreenShift = 0;
1554 ppfd->cAlphaBits = 0;
1555 ppfd->cAlphaShift = 0;
1558 /* Accum RGBA bits */
1559 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1560 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1561 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1562 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1564 ppfd->cAccumBits = rb+gb+bb+ab;
1565 ppfd->cAccumRedBits = rb;
1566 ppfd->cAccumGreenBits = gb;
1567 ppfd->cAccumBlueBits = bb;
1568 ppfd->cAccumAlphaBits = ab;
1570 /* Aux bits */
1571 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1572 ppfd->cAuxBuffers = value;
1574 /* Depth bits */
1575 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1576 ppfd->cDepthBits = value;
1578 /* stencil bits */
1579 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1580 ppfd->cStencilBits = value;
1582 ppfd->iLayerType = PFD_MAIN_PLANE;
1584 if (TRACE_ON(wgl)) {
1585 dump_PIXELFORMATDESCRIPTOR(ppfd);
1588 return nb_onscreen_formats;
1591 /***********************************************************************
1592 * glxdrv_wglGetPixelFormat
1594 static int glxdrv_wglGetPixelFormat( HDC hdc )
1596 struct gl_drawable *gl;
1597 int ret = 0;
1599 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1601 ret = pixel_format_index( gl->format );
1602 /* Offscreen formats can't be used with traditional WGL calls.
1603 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1604 if (!is_onscreen_pixel_format( ret )) ret = 1;
1605 release_gl_drawable( gl );
1607 TRACE( "%p -> %d\n", hdc, ret );
1608 return ret;
1611 /***********************************************************************
1612 * glxdrv_wglSetPixelFormat
1614 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1616 const struct wgl_pixel_format *fmt;
1617 int value;
1618 struct gl_drawable *gl;
1619 HWND hwnd = WindowFromDC( hdc );
1621 TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
1623 if (!hwnd || hwnd == GetDesktopWindow())
1625 WARN( "not a proper window DC %p/%p\n", hdc, hwnd );
1626 return FALSE;
1629 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1630 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1631 if(!fmt) {
1632 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1633 return FALSE;
1636 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1637 if (!(value & GLX_WINDOW_BIT))
1639 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1640 return FALSE;
1643 if ((gl = get_gl_drawable( hwnd, hdc )))
1645 int prev = pixel_format_index( gl->format );
1646 release_gl_drawable( gl );
1647 return prev == iPixelFormat; /* cannot change it if already set */
1650 if (TRACE_ON(wgl)) {
1651 int gl_test = 0;
1653 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1654 if (gl_test) {
1655 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1656 } else {
1657 TRACE(" FBConfig have :\n");
1658 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1659 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1660 TRACE(" - VISUAL_ID 0x%x\n", value);
1661 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1662 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1666 return set_win_format( hwnd, fmt );
1669 /***********************************************************************
1670 * glxdrv_wglCopyContext
1672 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1674 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1676 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1678 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1679 return TRUE;
1682 /***********************************************************************
1683 * glxdrv_wglCreateContext
1685 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1687 struct wgl_context *ret = NULL;
1688 struct gl_drawable *gl;
1690 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1692 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1693 return NULL;
1696 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1698 ret->hdc = hdc;
1699 ret->fmt = gl->format;
1700 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1701 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1702 list_add_head( &context_list, &ret->entry );
1704 release_gl_drawable( gl );
1705 TRACE( "%p -> %p\n", hdc, ret );
1706 return ret;
1709 /***********************************************************************
1710 * glxdrv_wglDeleteContext
1712 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1714 TRACE("(%p)\n", ctx);
1716 EnterCriticalSection( &context_section );
1717 list_remove( &ctx->entry );
1718 LeaveCriticalSection( &context_section );
1720 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1721 if (ctx->vis) XFree( ctx->vis );
1722 HeapFree( GetProcessHeap(), 0, ctx );
1725 /***********************************************************************
1726 * glxdrv_wglGetProcAddress
1728 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1730 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1731 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1734 /***********************************************************************
1735 * glxdrv_wglMakeCurrent
1737 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1739 BOOL ret = FALSE;
1740 struct gl_drawable *gl;
1742 TRACE("(%p,%p)\n", hdc, ctx);
1744 if (!ctx)
1746 pglXMakeCurrent(gdi_display, None, NULL);
1747 NtCurrentTeb()->glContext = NULL;
1748 return TRUE;
1751 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1753 if (ctx->fmt != gl->format)
1755 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1756 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1757 goto done;
1760 if (TRACE_ON(wgl)) {
1761 describeContext(ctx);
1762 TRACE("hdc %p drawable %lx fmt %p ctx %p\n", hdc, gl->drawable, gl->format, ctx->ctx );
1765 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1766 if (ret)
1768 NtCurrentTeb()->glContext = ctx;
1769 ctx->has_been_current = TRUE;
1770 ctx->hdc = hdc;
1771 ctx->drawables[0] = gl->drawable;
1772 ctx->drawables[1] = gl->drawable;
1773 ctx->refresh_drawables = FALSE;
1774 goto done;
1777 SetLastError( ERROR_INVALID_HANDLE );
1779 done:
1780 release_gl_drawable( gl );
1781 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1782 return ret;
1785 /***********************************************************************
1786 * X11DRV_wglMakeContextCurrentARB
1788 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1790 BOOL ret = FALSE;
1791 struct gl_drawable *draw_gl, *read_gl = NULL;
1793 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1795 if (!ctx)
1797 pglXMakeCurrent(gdi_display, None, NULL);
1798 NtCurrentTeb()->glContext = NULL;
1799 return TRUE;
1802 if (!pglXMakeContextCurrent) return FALSE;
1804 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1806 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1807 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1808 read_gl ? read_gl->drawable : 0, ctx->ctx);
1809 if (ret)
1811 ctx->has_been_current = TRUE;
1812 ctx->hdc = draw_hdc;
1813 ctx->drawables[0] = draw_gl->drawable;
1814 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1815 ctx->refresh_drawables = FALSE;
1816 NtCurrentTeb()->glContext = ctx;
1817 goto done;
1820 SetLastError( ERROR_INVALID_HANDLE );
1821 done:
1822 release_gl_drawable( read_gl );
1823 release_gl_drawable( draw_gl );
1824 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1825 return ret;
1828 /***********************************************************************
1829 * glxdrv_wglShareLists
1831 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1833 TRACE("(%p, %p)\n", org, dest);
1835 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1836 * at context creation time but in case of WGL it is done using wglShareLists.
1837 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1838 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1839 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1840 * so there delaying context creation doesn't work.
1842 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1843 * and when a program requests sharing we recreate the destination context if it hasn't been made
1844 * current or when it hasn't shared display lists before.
1847 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1849 ERR("Could not share display lists, one of the contexts has been current already !\n");
1850 return FALSE;
1852 else if(dest->sharing)
1854 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1855 return FALSE;
1857 else
1859 describeContext(org);
1860 describeContext(dest);
1862 /* Re-create the GLX context and share display lists */
1863 pglXDestroyContext(gdi_display, dest->ctx);
1864 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1865 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1867 org->sharing = TRUE;
1868 dest->sharing = TRUE;
1869 return TRUE;
1871 return FALSE;
1874 static void wglFinish(void)
1876 struct x11drv_escape_flush_gl_drawable escape;
1877 struct gl_drawable *gl;
1878 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1880 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1881 escape.gl_drawable = 0;
1883 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1885 switch (gl->type)
1887 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1888 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1889 default: break;
1891 sync_context(ctx);
1892 release_gl_drawable( gl );
1895 pglFinish();
1896 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1899 static void wglFlush(void)
1901 struct x11drv_escape_flush_gl_drawable escape;
1902 struct gl_drawable *gl;
1903 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1905 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1906 escape.gl_drawable = 0;
1908 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1910 switch (gl->type)
1912 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1913 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1914 default: break;
1916 sync_context(ctx);
1917 release_gl_drawable( gl );
1920 pglFlush();
1921 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1924 /***********************************************************************
1925 * X11DRV_wglCreateContextAttribsARB
1927 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
1928 const int* attribList )
1930 struct wgl_context *ret = NULL;
1931 struct gl_drawable *gl;
1932 int err = 0;
1934 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
1936 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1938 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1939 return NULL;
1942 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1944 ret->hdc = hdc;
1945 ret->fmt = gl->format;
1946 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
1947 ret->gl3_context = TRUE;
1948 if (attribList)
1950 int *pContextAttribList = &ret->attribList[0];
1951 /* attribList consists of pairs {token, value] terminated with 0 */
1952 while(attribList[0] != 0)
1954 TRACE("%#x %#x\n", attribList[0], attribList[1]);
1955 switch(attribList[0])
1957 case WGL_CONTEXT_MAJOR_VERSION_ARB:
1958 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
1959 pContextAttribList[1] = attribList[1];
1960 pContextAttribList += 2;
1961 ret->numAttribs++;
1962 break;
1963 case WGL_CONTEXT_MINOR_VERSION_ARB:
1964 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
1965 pContextAttribList[1] = attribList[1];
1966 pContextAttribList += 2;
1967 ret->numAttribs++;
1968 break;
1969 case WGL_CONTEXT_LAYER_PLANE_ARB:
1970 break;
1971 case WGL_CONTEXT_FLAGS_ARB:
1972 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
1973 pContextAttribList[1] = attribList[1];
1974 pContextAttribList += 2;
1975 ret->numAttribs++;
1976 break;
1977 case WGL_CONTEXT_PROFILE_MASK_ARB:
1978 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
1979 pContextAttribList[1] = attribList[1];
1980 pContextAttribList += 2;
1981 ret->numAttribs++;
1982 break;
1983 default:
1984 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
1986 attribList += 2;
1990 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1991 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
1992 XSync(gdi_display, False);
1993 if ((err = X11DRV_check_error()) || !ret->ctx)
1995 /* In the future we should convert the GLX error to a win32 one here if needed */
1996 ERR("Context creation failed (error %x)\n", err);
1997 HeapFree( GetProcessHeap(), 0, ret );
1998 ret = NULL;
2000 else list_add_head( &context_list, &ret->entry );
2003 release_gl_drawable( gl );
2004 TRACE( "%p -> %p\n", hdc, ret );
2005 return ret;
2009 * X11DRV_wglGetExtensionsStringARB
2011 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2013 static const GLubyte *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2015 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2016 return (const GLubyte *)WineGLInfo.wglExtensions;
2020 * X11DRV_wglCreatePbufferARB
2022 * WGL_ARB_pbuffer: wglCreatePbufferARB
2024 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2025 const int *piAttribList )
2027 struct wgl_pbuffer* object = NULL;
2028 const struct wgl_pixel_format *fmt = NULL;
2029 int attribs[256];
2030 int nAttribs = 0;
2032 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2034 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2035 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2036 if(!fmt) {
2037 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2038 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2039 return NULL;
2042 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2043 if (NULL == object) {
2044 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2045 return NULL;
2047 object->width = iWidth;
2048 object->height = iHeight;
2049 object->fmt = fmt;
2051 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2052 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2053 while (piAttribList && 0 != *piAttribList) {
2054 int attr_v;
2055 switch (*piAttribList) {
2056 case WGL_PBUFFER_LARGEST_ARB: {
2057 ++piAttribList;
2058 attr_v = *piAttribList;
2059 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2060 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2061 break;
2064 case WGL_TEXTURE_FORMAT_ARB: {
2065 ++piAttribList;
2066 attr_v = *piAttribList;
2067 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2068 if (WGL_NO_TEXTURE_ARB == attr_v) {
2069 object->use_render_texture = 0;
2070 } else {
2071 if (!use_render_texture_emulation) {
2072 SetLastError(ERROR_INVALID_DATA);
2073 goto create_failed;
2075 switch (attr_v) {
2076 case WGL_TEXTURE_RGB_ARB:
2077 object->use_render_texture = GL_RGB;
2078 object->texture_bpp = 3;
2079 object->texture_format = GL_RGB;
2080 object->texture_type = GL_UNSIGNED_BYTE;
2081 break;
2082 case WGL_TEXTURE_RGBA_ARB:
2083 object->use_render_texture = GL_RGBA;
2084 object->texture_bpp = 4;
2085 object->texture_format = GL_RGBA;
2086 object->texture_type = GL_UNSIGNED_BYTE;
2087 break;
2089 /* WGL_FLOAT_COMPONENTS_NV */
2090 case WGL_TEXTURE_FLOAT_R_NV:
2091 object->use_render_texture = GL_FLOAT_R_NV;
2092 object->texture_bpp = 4;
2093 object->texture_format = GL_RED;
2094 object->texture_type = GL_FLOAT;
2095 break;
2096 case WGL_TEXTURE_FLOAT_RG_NV:
2097 object->use_render_texture = GL_FLOAT_RG_NV;
2098 object->texture_bpp = 8;
2099 object->texture_format = GL_LUMINANCE_ALPHA;
2100 object->texture_type = GL_FLOAT;
2101 break;
2102 case WGL_TEXTURE_FLOAT_RGB_NV:
2103 object->use_render_texture = GL_FLOAT_RGB_NV;
2104 object->texture_bpp = 12;
2105 object->texture_format = GL_RGB;
2106 object->texture_type = GL_FLOAT;
2107 break;
2108 case WGL_TEXTURE_FLOAT_RGBA_NV:
2109 object->use_render_texture = GL_FLOAT_RGBA_NV;
2110 object->texture_bpp = 16;
2111 object->texture_format = GL_RGBA;
2112 object->texture_type = GL_FLOAT;
2113 break;
2114 default:
2115 ERR("Unknown texture format: %x\n", attr_v);
2116 SetLastError(ERROR_INVALID_DATA);
2117 goto create_failed;
2120 break;
2123 case WGL_TEXTURE_TARGET_ARB: {
2124 ++piAttribList;
2125 attr_v = *piAttribList;
2126 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2127 if (WGL_NO_TEXTURE_ARB == attr_v) {
2128 object->texture_target = 0;
2129 } else {
2130 if (!use_render_texture_emulation) {
2131 SetLastError(ERROR_INVALID_DATA);
2132 goto create_failed;
2134 switch (attr_v) {
2135 case WGL_TEXTURE_CUBE_MAP_ARB: {
2136 if (iWidth != iHeight) {
2137 SetLastError(ERROR_INVALID_DATA);
2138 goto create_failed;
2140 object->texture_target = GL_TEXTURE_CUBE_MAP;
2141 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2142 break;
2144 case WGL_TEXTURE_1D_ARB: {
2145 if (1 != iHeight) {
2146 SetLastError(ERROR_INVALID_DATA);
2147 goto create_failed;
2149 object->texture_target = GL_TEXTURE_1D;
2150 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2151 break;
2153 case WGL_TEXTURE_2D_ARB: {
2154 object->texture_target = GL_TEXTURE_2D;
2155 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2156 break;
2158 case WGL_TEXTURE_RECTANGLE_NV: {
2159 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2160 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2161 break;
2163 default:
2164 ERR("Unknown texture target: %x\n", attr_v);
2165 SetLastError(ERROR_INVALID_DATA);
2166 goto create_failed;
2169 break;
2172 case WGL_MIPMAP_TEXTURE_ARB: {
2173 ++piAttribList;
2174 attr_v = *piAttribList;
2175 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2176 if (!use_render_texture_emulation) {
2177 SetLastError(ERROR_INVALID_DATA);
2178 goto create_failed;
2180 break;
2183 ++piAttribList;
2186 PUSH1(attribs, None);
2187 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2188 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2189 if (!object->drawable) {
2190 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2191 goto create_failed; /* unexpected error */
2193 TRACE("->(%p)\n", object);
2194 return object;
2196 create_failed:
2197 HeapFree(GetProcessHeap(), 0, object);
2198 TRACE("->(FAILED)\n");
2199 return NULL;
2203 * X11DRV_wglDestroyPbufferARB
2205 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2207 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2209 TRACE("(%p)\n", object);
2211 pglXDestroyPbuffer(gdi_display, object->drawable);
2212 HeapFree(GetProcessHeap(), 0, object);
2213 return GL_TRUE;
2217 * X11DRV_wglGetPbufferDCARB
2219 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2221 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2223 struct x11drv_escape_set_drawable escape;
2224 struct gl_drawable *gl, *prev;
2225 HDC hdc;
2227 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2228 if (!hdc) return 0;
2230 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2232 DeleteDC( hdc );
2233 return 0;
2235 gl->type = DC_GL_PBUFFER;
2236 gl->drawable = object->drawable;
2237 gl->format = object->fmt;
2239 EnterCriticalSection( &context_section );
2240 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2241 free_gl_drawable( prev );
2242 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2243 LeaveCriticalSection( &context_section );
2245 escape.code = X11DRV_SET_DRAWABLE;
2246 escape.hwnd = 0;
2247 escape.drawable = object->drawable;
2248 escape.mode = IncludeInferiors;
2249 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2250 escape.fbconfig_id = object->fmt->fmt_id;
2251 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2253 TRACE( "(%p)->(%p)\n", object, hdc );
2254 return hdc;
2258 * X11DRV_wglQueryPbufferARB
2260 * WGL_ARB_pbuffer: wglQueryPbufferARB
2262 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2264 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2266 switch (iAttribute) {
2267 case WGL_PBUFFER_WIDTH_ARB:
2268 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2269 break;
2270 case WGL_PBUFFER_HEIGHT_ARB:
2271 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2272 break;
2274 case WGL_PBUFFER_LOST_ARB:
2275 /* GLX Pbuffers cannot be lost by default. We can support this by
2276 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2277 * to receive pixel buffer clobber events, however that may or may
2278 * not give any benefit */
2279 *piValue = GL_FALSE;
2280 break;
2282 case WGL_TEXTURE_FORMAT_ARB:
2283 if (!object->use_render_texture) {
2284 *piValue = WGL_NO_TEXTURE_ARB;
2285 } else {
2286 if (!use_render_texture_emulation) {
2287 SetLastError(ERROR_INVALID_HANDLE);
2288 return GL_FALSE;
2290 switch(object->use_render_texture) {
2291 case GL_RGB:
2292 *piValue = WGL_TEXTURE_RGB_ARB;
2293 break;
2294 case GL_RGBA:
2295 *piValue = WGL_TEXTURE_RGBA_ARB;
2296 break;
2297 /* WGL_FLOAT_COMPONENTS_NV */
2298 case GL_FLOAT_R_NV:
2299 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2300 break;
2301 case GL_FLOAT_RG_NV:
2302 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2303 break;
2304 case GL_FLOAT_RGB_NV:
2305 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2306 break;
2307 case GL_FLOAT_RGBA_NV:
2308 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2309 break;
2310 default:
2311 ERR("Unknown texture format: %x\n", object->use_render_texture);
2314 break;
2316 case WGL_TEXTURE_TARGET_ARB:
2317 if (!object->texture_target){
2318 *piValue = WGL_NO_TEXTURE_ARB;
2319 } else {
2320 if (!use_render_texture_emulation) {
2321 SetLastError(ERROR_INVALID_DATA);
2322 return GL_FALSE;
2324 switch (object->texture_target) {
2325 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2326 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2327 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2328 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2331 break;
2333 case WGL_MIPMAP_TEXTURE_ARB:
2334 *piValue = GL_FALSE; /** don't support that */
2335 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2336 break;
2338 default:
2339 FIXME("unexpected attribute %x\n", iAttribute);
2340 break;
2343 return GL_TRUE;
2347 * X11DRV_wglReleasePbufferDCARB
2349 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2351 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2353 struct gl_drawable *gl;
2355 TRACE("(%p, %p)\n", object, hdc);
2357 EnterCriticalSection( &context_section );
2359 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2361 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2362 free_gl_drawable( gl );
2364 else hdc = 0;
2366 LeaveCriticalSection( &context_section );
2368 return hdc && DeleteDC(hdc);
2372 * X11DRV_wglSetPbufferAttribARB
2374 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2376 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2378 GLboolean ret = GL_FALSE;
2380 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2382 if (!object->use_render_texture) {
2383 SetLastError(ERROR_INVALID_HANDLE);
2384 return GL_FALSE;
2386 if (1 == use_render_texture_emulation) {
2387 return GL_TRUE;
2389 return ret;
2393 * X11DRV_wglChoosePixelFormatARB
2395 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2397 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2398 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2400 int attribs[256];
2401 int nAttribs = 0;
2402 GLXFBConfig* cfgs = NULL;
2403 int nCfgs = 0;
2404 int it;
2405 int fmt_id;
2406 int start, end;
2407 UINT pfmt_it = 0;
2408 int run;
2409 int i;
2410 DWORD dwFlags = 0;
2412 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2413 if (NULL != pfAttribFList) {
2414 FIXME("unused pfAttribFList\n");
2417 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2418 if (-1 == nAttribs) {
2419 WARN("Cannot convert WGL to GLX attributes\n");
2420 return GL_FALSE;
2422 PUSH1(attribs, None);
2424 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2425 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2426 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2428 for(i=0; piAttribIList[i] != 0; i+=2)
2430 switch(piAttribIList[i])
2432 case WGL_DRAW_TO_BITMAP_ARB:
2433 if(piAttribIList[i+1])
2434 dwFlags |= PFD_DRAW_TO_BITMAP;
2435 break;
2436 case WGL_ACCELERATION_ARB:
2437 switch(piAttribIList[i+1])
2439 case WGL_NO_ACCELERATION_ARB:
2440 dwFlags |= PFD_GENERIC_FORMAT;
2441 break;
2442 case WGL_GENERIC_ACCELERATION_ARB:
2443 dwFlags |= PFD_GENERIC_ACCELERATED;
2444 break;
2445 case WGL_FULL_ACCELERATION_ARB:
2446 /* Nothing to do */
2447 break;
2449 break;
2450 case WGL_SUPPORT_GDI_ARB:
2451 if(piAttribIList[i+1])
2452 dwFlags |= PFD_SUPPORT_GDI;
2453 break;
2457 /* Search for FB configurations matching the requirements in attribs */
2458 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2459 if (NULL == cfgs) {
2460 WARN("Compatible Pixel Format not found\n");
2461 return GL_FALSE;
2464 /* Loop through all matching formats and check if they are suitable.
2465 * Note that this function should at max return nMaxFormats different formats */
2466 for(run=0; run < 2; run++)
2468 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2470 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2472 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2473 continue;
2476 /* During the first run we only want onscreen formats and during the second only offscreen */
2477 start = run == 1 ? nb_onscreen_formats : 0;
2478 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2480 for (i = start; i < end; i++)
2482 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2484 piFormats[pfmt_it++] = i + 1;
2485 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2486 it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2487 break;
2493 *nNumFormats = pfmt_it;
2494 /** free list */
2495 XFree(cfgs);
2496 return GL_TRUE;
2500 * X11DRV_wglGetPixelFormatAttribivARB
2502 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2504 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2505 UINT nAttributes, const int *piAttributes, int *piValues )
2507 UINT i;
2508 const struct wgl_pixel_format *fmt = NULL;
2509 int hTest;
2510 int tmp;
2511 int curGLXAttr = 0;
2513 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2515 if (0 < iLayerPlane) {
2516 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2517 return GL_FALSE;
2520 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2521 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2522 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2523 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2524 if(!fmt) {
2525 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2528 for (i = 0; i < nAttributes; ++i) {
2529 const int curWGLAttr = piAttributes[i];
2530 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2532 switch (curWGLAttr) {
2533 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2534 piValues[i] = nb_pixel_formats;
2535 continue;
2537 case WGL_SUPPORT_OPENGL_ARB:
2538 piValues[i] = GL_TRUE;
2539 continue;
2541 case WGL_ACCELERATION_ARB:
2542 curGLXAttr = GLX_CONFIG_CAVEAT;
2543 if (!fmt) goto pix_error;
2544 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2545 piValues[i] = WGL_NO_ACCELERATION_ARB;
2546 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2547 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2548 else
2549 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2550 continue;
2552 case WGL_TRANSPARENT_ARB:
2553 curGLXAttr = GLX_TRANSPARENT_TYPE;
2554 if (!fmt) goto pix_error;
2555 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2556 if (hTest) goto get_error;
2557 piValues[i] = GL_FALSE;
2558 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2559 continue;
2561 case WGL_PIXEL_TYPE_ARB:
2562 curGLXAttr = GLX_RENDER_TYPE;
2563 if (!fmt) goto pix_error;
2564 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2565 if (hTest) goto get_error;
2566 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2567 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2568 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2569 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2570 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2571 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2572 else {
2573 ERR("unexpected RenderType(%x)\n", tmp);
2574 piValues[i] = WGL_TYPE_RGBA_ARB;
2576 continue;
2578 case WGL_COLOR_BITS_ARB:
2579 curGLXAttr = GLX_BUFFER_SIZE;
2580 break;
2582 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2583 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2584 if (!use_render_texture_emulation) {
2585 piValues[i] = GL_FALSE;
2586 continue;
2588 curGLXAttr = GLX_RENDER_TYPE;
2589 if (!fmt) goto pix_error;
2590 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2591 if (hTest) goto get_error;
2592 if (GLX_COLOR_INDEX_BIT == tmp) {
2593 piValues[i] = GL_FALSE;
2594 continue;
2596 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2597 if (hTest) goto get_error;
2598 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2599 continue;
2601 case WGL_BLUE_BITS_ARB:
2602 curGLXAttr = GLX_BLUE_SIZE;
2603 break;
2604 case WGL_RED_BITS_ARB:
2605 curGLXAttr = GLX_RED_SIZE;
2606 break;
2607 case WGL_GREEN_BITS_ARB:
2608 curGLXAttr = GLX_GREEN_SIZE;
2609 break;
2610 case WGL_ALPHA_BITS_ARB:
2611 curGLXAttr = GLX_ALPHA_SIZE;
2612 break;
2613 case WGL_DEPTH_BITS_ARB:
2614 curGLXAttr = GLX_DEPTH_SIZE;
2615 break;
2616 case WGL_STENCIL_BITS_ARB:
2617 curGLXAttr = GLX_STENCIL_SIZE;
2618 break;
2619 case WGL_DOUBLE_BUFFER_ARB:
2620 curGLXAttr = GLX_DOUBLEBUFFER;
2621 break;
2622 case WGL_STEREO_ARB:
2623 curGLXAttr = GLX_STEREO;
2624 break;
2625 case WGL_AUX_BUFFERS_ARB:
2626 curGLXAttr = GLX_AUX_BUFFERS;
2627 break;
2629 case WGL_SUPPORT_GDI_ARB:
2630 if (!fmt) goto pix_error;
2631 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2632 continue;
2634 case WGL_DRAW_TO_BITMAP_ARB:
2635 if (!fmt) goto pix_error;
2636 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2637 continue;
2639 case WGL_DRAW_TO_WINDOW_ARB:
2640 case WGL_DRAW_TO_PBUFFER_ARB:
2641 if (!fmt) goto pix_error;
2642 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2643 if (hTest) goto get_error;
2644 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2645 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2646 piValues[i] = GL_TRUE;
2647 else
2648 piValues[i] = GL_FALSE;
2649 continue;
2651 case WGL_SWAP_METHOD_ARB:
2652 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2653 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2654 * point only ATI offers this.
2656 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2657 break;
2659 case WGL_PBUFFER_LARGEST_ARB:
2660 curGLXAttr = GLX_LARGEST_PBUFFER;
2661 break;
2663 case WGL_SAMPLE_BUFFERS_ARB:
2664 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2665 break;
2667 case WGL_SAMPLES_ARB:
2668 curGLXAttr = GLX_SAMPLES_ARB;
2669 break;
2671 case WGL_FLOAT_COMPONENTS_NV:
2672 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2673 break;
2675 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2676 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2677 break;
2679 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2680 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2681 break;
2683 case WGL_ACCUM_RED_BITS_ARB:
2684 curGLXAttr = GLX_ACCUM_RED_SIZE;
2685 break;
2686 case WGL_ACCUM_GREEN_BITS_ARB:
2687 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2688 break;
2689 case WGL_ACCUM_BLUE_BITS_ARB:
2690 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2691 break;
2692 case WGL_ACCUM_ALPHA_BITS_ARB:
2693 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2694 break;
2695 case WGL_ACCUM_BITS_ARB:
2696 if (!fmt) goto pix_error;
2697 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2698 if (hTest) goto get_error;
2699 piValues[i] = tmp;
2700 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2701 if (hTest) goto get_error;
2702 piValues[i] += tmp;
2703 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2704 if (hTest) goto get_error;
2705 piValues[i] += tmp;
2706 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2707 if (hTest) goto get_error;
2708 piValues[i] += tmp;
2709 continue;
2711 default:
2712 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2715 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2716 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2717 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2719 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2720 * and check which options can work using iPixelFormat=0 and which not.
2721 * A problem would be that this function is an extension. This would
2722 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2724 if (0 != curGLXAttr && iPixelFormat != 0) {
2725 if (!fmt) goto pix_error;
2726 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2727 if (hTest) goto get_error;
2728 curGLXAttr = 0;
2729 } else {
2730 piValues[i] = GL_FALSE;
2733 return GL_TRUE;
2735 get_error:
2736 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2737 return GL_FALSE;
2739 pix_error:
2740 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2741 return GL_FALSE;
2745 * X11DRV_wglGetPixelFormatAttribfvARB
2747 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2749 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2750 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2752 int *attr;
2753 int ret;
2754 UINT i;
2756 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2758 /* Allocate a temporary array to store integer values */
2759 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2760 if (!attr) {
2761 ERR("couldn't allocate %d array\n", nAttributes);
2762 return GL_FALSE;
2765 /* Piggy-back on wglGetPixelFormatAttribivARB */
2766 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2767 if (ret) {
2768 /* Convert integer values to float. Should also check for attributes
2769 that can give decimal values here */
2770 for (i=0; i<nAttributes;i++) {
2771 pfValues[i] = attr[i];
2775 HeapFree(GetProcessHeap(), 0, attr);
2776 return ret;
2780 * X11DRV_wglBindTexImageARB
2782 * WGL_ARB_render_texture: wglBindTexImageARB
2784 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2786 GLboolean ret = GL_FALSE;
2788 TRACE("(%p, %d)\n", object, iBuffer);
2790 if (!object->use_render_texture) {
2791 SetLastError(ERROR_INVALID_HANDLE);
2792 return GL_FALSE;
2795 if (1 == use_render_texture_emulation) {
2796 static int init = 0;
2797 int prev_binded_texture = 0;
2798 GLXContext prev_context;
2799 Drawable prev_drawable;
2800 GLXContext tmp_context;
2802 prev_context = pglXGetCurrentContext();
2803 prev_drawable = pglXGetCurrentDrawable();
2805 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2806 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2807 isn't ideal performance wise but I wasn't able to get other ways working.
2809 if(!init) {
2810 init = 1; /* Only show the FIXME once for performance reasons */
2811 FIXME("partial stub!\n");
2814 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2815 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2817 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2819 /* Switch to our pbuffer */
2820 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2822 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2823 * After that upload the pbuffer texture data. */
2824 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2825 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2827 /* Switch back to the original drawable and upload the pbuffer-texture */
2828 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2829 pglXDestroyContext(gdi_display, tmp_context);
2830 return GL_TRUE;
2833 return ret;
2837 * X11DRV_wglReleaseTexImageARB
2839 * WGL_ARB_render_texture: wglReleaseTexImageARB
2841 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2843 GLboolean ret = GL_FALSE;
2845 TRACE("(%p, %d)\n", object, iBuffer);
2847 if (!object->use_render_texture) {
2848 SetLastError(ERROR_INVALID_HANDLE);
2849 return GL_FALSE;
2851 if (1 == use_render_texture_emulation) {
2852 return GL_TRUE;
2854 return ret;
2858 * X11DRV_wglGetExtensionsStringEXT
2860 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2862 static const GLubyte *X11DRV_wglGetExtensionsStringEXT(void)
2864 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2865 return (const GLubyte *)WineGLInfo.wglExtensions;
2869 * X11DRV_wglGetSwapIntervalEXT
2871 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2873 static int X11DRV_wglGetSwapIntervalEXT(void)
2875 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2876 struct gl_drawable *gl;
2877 int swap_interval;
2879 TRACE("()\n");
2881 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
2883 /* This can't happen because a current WGL context is required to get
2884 * here. Likely the application is buggy.
2886 WARN("No GL drawable found, returning swap interval 0\n");
2887 return 0;
2890 swap_interval = gl->swap_interval;
2891 release_gl_drawable(gl);
2893 return swap_interval;
2897 * X11DRV_wglSwapIntervalEXT
2899 * WGL_EXT_swap_control: wglSwapIntervalEXT
2901 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
2903 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2904 struct gl_drawable *gl;
2905 BOOL ret = TRUE;
2907 TRACE("(%d)\n", interval);
2909 if (interval < 0)
2911 SetLastError(ERROR_INVALID_DATA);
2912 return FALSE;
2915 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
2917 SetLastError(ERROR_DC_NOT_FOUND);
2918 return FALSE;
2921 if (!has_swap_control && interval == 0)
2923 /* wglSwapIntervalEXT considers an interval value of zero to mean that
2924 * vsync should be disabled, but glXSwapIntervalSGI considers such a
2925 * value to be an error. Just silently ignore the request for now. */
2926 WARN("Request to disable vertical sync is not handled\n");
2927 gl->swap_interval = 0;
2929 else
2931 if (pglXSwapIntervalSGI)
2932 ret = !pglXSwapIntervalSGI(interval);
2933 else
2934 WARN("GLX_SGI_swap_control extension is not available\n");
2936 if (ret)
2937 gl->swap_interval = interval;
2938 else
2939 SetLastError(ERROR_DC_NOT_FOUND);
2942 release_gl_drawable(gl);
2944 return ret;
2948 * X11DRV_wglSetPixelFormatWINE
2950 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
2951 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
2953 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
2955 const struct wgl_pixel_format *fmt;
2956 int value;
2957 HWND hwnd;
2959 TRACE("(%p,%d)\n", hdc, format);
2961 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
2962 if (!fmt)
2964 ERR( "Invalid format %d\n", format );
2965 return FALSE;
2968 hwnd = WindowFromDC( hdc );
2969 if (!hwnd || hwnd == GetDesktopWindow())
2971 ERR( "not a valid window DC %p\n", hdc );
2972 return FALSE;
2975 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
2976 if (!(value & GLX_WINDOW_BIT))
2978 WARN( "Pixel format %d is not compatible for window rendering\n", format );
2979 return FALSE;
2982 return set_win_format( hwnd, fmt );
2986 * glxRequireVersion (internal)
2988 * Check if the supported GLX version matches requiredVersion.
2990 static BOOL glxRequireVersion(int requiredVersion)
2992 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2993 if(requiredVersion <= WineGLInfo.glxVersion[1])
2994 return TRUE;
2996 return FALSE;
2999 static void register_extension(const char *ext)
3001 if (WineGLInfo.wglExtensions[0])
3002 strcat(WineGLInfo.wglExtensions, " ");
3003 strcat(WineGLInfo.wglExtensions, ext);
3005 TRACE("'%s'\n", ext);
3009 * X11DRV_WineGL_LoadExtensions
3011 static void X11DRV_WineGL_LoadExtensions(void)
3013 WineGLInfo.wglExtensions[0] = 0;
3015 /* ARB Extensions */
3017 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
3019 register_extension( "WGL_ARB_create_context" );
3020 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3022 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
3023 register_extension("WGL_ARB_create_context_profile");
3026 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
3028 register_extension("WGL_ARB_pixel_format_float");
3029 register_extension("WGL_ATI_pixel_format_float");
3032 register_extension( "WGL_ARB_extensions_string" );
3033 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3035 if (glxRequireVersion(3))
3037 register_extension( "WGL_ARB_make_current_read" );
3038 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3039 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3042 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3044 if (glxRequireVersion(3))
3046 register_extension( "WGL_ARB_pbuffer" );
3047 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3048 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3049 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3050 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3051 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3052 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3055 register_extension( "WGL_ARB_pixel_format" );
3056 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3057 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3058 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3060 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3061 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3062 (glxRequireVersion(3) && use_render_texture_emulation))
3064 register_extension( "WGL_ARB_render_texture" );
3065 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3066 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3068 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3069 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3070 register_extension("WGL_NV_float_buffer");
3072 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3073 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3074 register_extension("WGL_NV_render_texture_rectangle");
3077 /* EXT Extensions */
3079 register_extension( "WGL_EXT_extensions_string" );
3080 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3082 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3083 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3084 register_extension( "WGL_EXT_swap_control" );
3085 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3086 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3088 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3089 register_extension("WGL_EXT_framebuffer_sRGB");
3091 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3092 register_extension("WGL_EXT_pixel_format_packed_float");
3094 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3095 has_swap_control = TRUE;
3097 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3098 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3100 register_extension( "WGL_NV_vertex_array_range" );
3101 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3102 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3105 /* WINE-specific WGL Extensions */
3107 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3108 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3110 register_extension( "WGL_WINE_pixel_format_passthrough" );
3111 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3116 * glxdrv_SwapBuffers
3118 * Swap the buffers of this DC
3120 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3122 struct x11drv_escape_flush_gl_drawable escape;
3123 struct gl_drawable *gl;
3124 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3126 TRACE("(%p)\n", hdc);
3128 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3129 escape.gl_drawable = 0;
3131 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3133 SetLastError( ERROR_INVALID_HANDLE );
3134 return FALSE;
3137 switch (gl->type)
3139 case DC_GL_PIXMAP_WIN:
3140 if (ctx) sync_context( ctx );
3141 escape.gl_drawable = gl->pixmap;
3142 if (pglXCopySubBufferMESA) {
3143 /* (glX)SwapBuffers has an implicit glFlush effect, however
3144 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3145 * copying */
3146 pglFlush();
3147 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3148 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3149 break;
3151 pglXSwapBuffers(gdi_display, gl->drawable);
3152 break;
3153 case DC_GL_CHILD_WIN:
3154 escape.gl_drawable = gl->drawable;
3155 /* fall through */
3156 default:
3157 pglXSwapBuffers(gdi_display, gl->drawable);
3158 break;
3161 release_gl_drawable( gl );
3163 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3164 return TRUE;
3167 static struct opengl_funcs opengl_funcs =
3170 glxdrv_wglCopyContext, /* p_wglCopyContext */
3171 glxdrv_wglCreateContext, /* p_wglCreateContext */
3172 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3173 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3174 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3175 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3176 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3177 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3178 glxdrv_wglShareLists, /* p_wglShareLists */
3179 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3183 struct opengl_funcs *get_glx_driver( UINT version )
3185 if (version != WINE_WGL_DRIVER_VERSION)
3187 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3188 return NULL;
3190 if (has_opengl()) return &opengl_funcs;
3191 return NULL;
3194 #else /* no OpenGL includes */
3196 struct opengl_funcs *get_glx_driver( UINT version )
3198 return NULL;
3201 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3205 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3209 void destroy_gl_drawable( HWND hwnd )
3213 #endif /* defined(SONAME_LIBGL) */