gdiplus: Don't bother to free memory at process exit.
[wine/wine-gecko.git] / dlls / winex11.drv / opengl.c
blobc617245cab37debfe6d1acd927b417d1ef8d5c6d
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 */
256 /* X context to associate a struct gl_drawable to an hwnd */
257 static XContext gl_hwnd_context;
258 /* X context to associate a struct gl_drawable to a pbuffer hdc */
259 static XContext gl_pbuffer_context;
261 static const struct gdi_dc_funcs glxdrv_funcs;
263 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
265 return (struct glx_physdev *)dev;
268 static struct list context_list = LIST_INIT( context_list );
269 static struct WineGLInfo WineGLInfo = { 0 };
270 static struct wgl_pixel_format *pixel_formats;
271 static int nb_pixel_formats, nb_onscreen_formats;
272 static int use_render_texture_emulation = 1;
273 static BOOL has_swap_control;
274 static int swap_interval = 1;
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;
1227 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1229 struct x11drv_win_data *data = get_win_data( hwnd );
1231 if (data)
1233 gl->type = DC_GL_WINDOW;
1234 gl->drawable = create_client_window( data, gl->visual );
1235 release_win_data( data );
1238 #ifdef SONAME_LIBXCOMPOSITE
1239 else if(usexcomposite)
1241 static Window dummy_parent;
1242 XSetWindowAttributes attrib;
1244 attrib.override_redirect = True;
1245 if (!dummy_parent)
1247 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1248 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1249 XMapWindow( gdi_display, dummy_parent );
1251 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1252 (gl->visual->class == PseudoColor ||
1253 gl->visual->class == GrayScale ||
1254 gl->visual->class == DirectColor) ?
1255 AllocAll : AllocNone);
1256 attrib.colormap = gl->colormap;
1257 XInstallColormap(gdi_display, attrib.colormap);
1259 gl->type = DC_GL_CHILD_WIN;
1260 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1261 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1262 0, gl->visual->depth, InputOutput, gl->visual->visual,
1263 CWColormap | CWOverrideRedirect, &attrib );
1264 if (gl->drawable)
1266 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1267 XMapWindow(gdi_display, gl->drawable);
1269 else XFreeColormap( gdi_display, gl->colormap );
1271 #endif
1272 else
1274 WARN("XComposite is not available, using GLXPixmap hack\n");
1276 gl->type = DC_GL_PIXMAP_WIN;
1277 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1278 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1279 gl->visual->depth );
1280 if (gl->pixmap)
1282 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1283 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1287 return gl->drawable != 0;
1291 /***********************************************************************
1292 * set_win_format
1294 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1296 HWND parent = GetAncestor( hwnd, GA_PARENT );
1297 struct gl_drawable *gl, *prev;
1299 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1300 gl->format = format;
1301 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1302 if (!gl->visual)
1304 HeapFree( GetProcessHeap(), 0, gl );
1305 return FALSE;
1308 GetClientRect( hwnd, &gl->rect );
1309 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1310 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1312 if (!create_gl_drawable( hwnd, parent, gl ))
1314 XFree( gl->visual );
1315 HeapFree( GetProcessHeap(), 0, gl );
1316 return FALSE;
1319 TRACE("created GL drawable %lx for win %p format %x\n", gl->drawable, hwnd, format->fmt_id );
1321 XFlush( gdi_display );
1323 EnterCriticalSection( &context_section );
1324 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1325 free_gl_drawable( prev );
1326 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1327 LeaveCriticalSection( &context_section );
1329 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1330 return TRUE;
1333 /***********************************************************************
1334 * sync_gl_drawable
1336 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1338 struct gl_drawable *gl;
1339 Drawable glxp;
1340 Pixmap pix;
1341 int mask = 0;
1342 XWindowChanges changes;
1344 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1345 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1347 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1349 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1350 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1352 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1354 switch (gl->type)
1356 case DC_GL_CHILD_WIN:
1357 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1358 break;
1359 case DC_GL_PIXMAP_WIN:
1360 if (!mask) break;
1361 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1362 if (!pix) goto done;
1363 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1364 if (!glxp)
1366 XFreePixmap(gdi_display, pix);
1367 goto done;
1369 mark_drawable_dirty(gl->drawable, glxp);
1370 XFlush( gdi_display );
1372 XFreePixmap(gdi_display, gl->pixmap);
1373 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1374 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1376 gl->pixmap = pix;
1377 gl->drawable = glxp;
1378 break;
1379 default:
1380 break;
1382 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1383 done:
1384 release_gl_drawable( gl );
1388 /***********************************************************************
1389 * set_gl_drawable_parent
1391 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1393 struct gl_drawable *gl;
1394 Drawable old_drawable;
1396 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1398 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1400 old_drawable = gl->drawable;
1401 switch (gl->type)
1403 case DC_GL_WINDOW:
1404 break;
1405 case DC_GL_CHILD_WIN:
1406 if (parent != GetDesktopWindow()) goto done;
1407 XDestroyWindow( gdi_display, gl->drawable );
1408 XFreeColormap( gdi_display, gl->colormap );
1409 break;
1410 case DC_GL_PIXMAP_WIN:
1411 if (parent != GetDesktopWindow()) goto done;
1412 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1413 XFreePixmap( gdi_display, gl->pixmap );
1414 break;
1415 default:
1416 goto done;
1419 if (!create_gl_drawable( hwnd, parent, gl ))
1421 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1422 release_gl_drawable( gl );
1423 XFree( gl->visual );
1424 HeapFree( GetProcessHeap(), 0, gl );
1425 __wine_set_pixel_format( hwnd, 0 );
1426 return;
1428 mark_drawable_dirty( old_drawable, gl->drawable );
1430 done:
1431 release_gl_drawable( gl );
1436 /***********************************************************************
1437 * destroy_gl_drawable
1439 void destroy_gl_drawable( HWND hwnd )
1441 struct gl_drawable *gl;
1443 EnterCriticalSection( &context_section );
1444 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1446 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1447 free_gl_drawable( gl );
1449 LeaveCriticalSection( &context_section );
1454 * glxdrv_DescribePixelFormat
1456 * Get the pixel-format descriptor associated to the given id
1458 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1459 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1461 /*XVisualInfo *vis;*/
1462 int value;
1463 int rb,gb,bb,ab;
1464 const struct wgl_pixel_format *fmt;
1466 if (!has_opengl()) return 0;
1468 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1470 if (!ppfd) return nb_onscreen_formats;
1472 /* 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 */
1473 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1474 if (!fmt) {
1475 WARN("unexpected format %d\n", iPixelFormat);
1476 return 0;
1479 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1480 ERR("Wrong structure size !\n");
1481 /* Should set error */
1482 return 0;
1485 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1486 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1487 ppfd->nVersion = 1;
1489 /* These flags are always the same... */
1490 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1491 /* Now the flags extracted from the Visual */
1493 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1494 if(value & GLX_WINDOW_BIT)
1495 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1497 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1498 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1499 * 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
1500 * offered the GDI bit either. */
1501 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1503 /* PFD_GENERIC_FORMAT - gdi software rendering
1504 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1505 * none set - full hardware accelerated by a ICD
1507 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1508 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1510 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1511 if (value) {
1512 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1513 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1515 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1517 /* Pixel type */
1518 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1519 if (value & GLX_RGBA_BIT)
1520 ppfd->iPixelType = PFD_TYPE_RGBA;
1521 else
1522 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1524 /* Color bits */
1525 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1526 ppfd->cColorBits = value;
1528 /* Red, green, blue and alpha bits / shifts */
1529 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1530 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1531 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1532 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1533 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1535 ppfd->cRedBits = rb;
1536 ppfd->cRedShift = gb + bb + ab;
1537 ppfd->cBlueBits = bb;
1538 ppfd->cBlueShift = ab;
1539 ppfd->cGreenBits = gb;
1540 ppfd->cGreenShift = bb + ab;
1541 ppfd->cAlphaBits = ab;
1542 ppfd->cAlphaShift = 0;
1543 } else {
1544 ppfd->cRedBits = 0;
1545 ppfd->cRedShift = 0;
1546 ppfd->cBlueBits = 0;
1547 ppfd->cBlueShift = 0;
1548 ppfd->cGreenBits = 0;
1549 ppfd->cGreenShift = 0;
1550 ppfd->cAlphaBits = 0;
1551 ppfd->cAlphaShift = 0;
1554 /* Accum RGBA bits */
1555 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1556 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1557 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1558 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1560 ppfd->cAccumBits = rb+gb+bb+ab;
1561 ppfd->cAccumRedBits = rb;
1562 ppfd->cAccumGreenBits = gb;
1563 ppfd->cAccumBlueBits = bb;
1564 ppfd->cAccumAlphaBits = ab;
1566 /* Aux bits */
1567 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1568 ppfd->cAuxBuffers = value;
1570 /* Depth bits */
1571 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1572 ppfd->cDepthBits = value;
1574 /* stencil bits */
1575 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1576 ppfd->cStencilBits = value;
1578 ppfd->iLayerType = PFD_MAIN_PLANE;
1580 if (TRACE_ON(wgl)) {
1581 dump_PIXELFORMATDESCRIPTOR(ppfd);
1584 return nb_onscreen_formats;
1587 /***********************************************************************
1588 * glxdrv_wglGetPixelFormat
1590 static int glxdrv_wglGetPixelFormat( HDC hdc )
1592 struct gl_drawable *gl;
1593 int ret = 0;
1595 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1597 ret = pixel_format_index( gl->format );
1598 /* Offscreen formats can't be used with traditional WGL calls.
1599 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1600 if (!is_onscreen_pixel_format( ret )) ret = 1;
1601 release_gl_drawable( gl );
1603 TRACE( "%p -> %d\n", hdc, ret );
1604 return ret;
1607 /***********************************************************************
1608 * glxdrv_wglSetPixelFormat
1610 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1612 const struct wgl_pixel_format *fmt;
1613 int value;
1614 struct gl_drawable *gl;
1615 HWND hwnd = WindowFromDC( hdc );
1617 TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
1619 if (!hwnd || hwnd == GetDesktopWindow())
1621 WARN( "not a proper window DC %p/%p\n", hdc, hwnd );
1622 return FALSE;
1625 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1626 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1627 if(!fmt) {
1628 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1629 return FALSE;
1632 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1633 if (!(value & GLX_WINDOW_BIT))
1635 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1636 return FALSE;
1639 if ((gl = get_gl_drawable( hwnd, hdc )))
1641 int prev = pixel_format_index( gl->format );
1642 release_gl_drawable( gl );
1643 return prev == iPixelFormat; /* cannot change it if already set */
1646 if (TRACE_ON(wgl)) {
1647 int gl_test = 0;
1649 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1650 if (gl_test) {
1651 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1652 } else {
1653 TRACE(" FBConfig have :\n");
1654 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1655 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1656 TRACE(" - VISUAL_ID 0x%x\n", value);
1657 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1658 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1662 return set_win_format( hwnd, fmt );
1665 /***********************************************************************
1666 * glxdrv_wglCopyContext
1668 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1670 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1672 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1674 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1675 return TRUE;
1678 /***********************************************************************
1679 * glxdrv_wglCreateContext
1681 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1683 struct wgl_context *ret = NULL;
1684 struct gl_drawable *gl;
1686 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1688 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1689 return NULL;
1692 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1694 ret->hdc = hdc;
1695 ret->fmt = gl->format;
1696 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1697 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1698 list_add_head( &context_list, &ret->entry );
1700 release_gl_drawable( gl );
1701 TRACE( "%p -> %p\n", hdc, ret );
1702 return ret;
1705 /***********************************************************************
1706 * glxdrv_wglDeleteContext
1708 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1710 TRACE("(%p)\n", ctx);
1712 EnterCriticalSection( &context_section );
1713 list_remove( &ctx->entry );
1714 LeaveCriticalSection( &context_section );
1716 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1717 if (ctx->vis) XFree( ctx->vis );
1718 HeapFree( GetProcessHeap(), 0, ctx );
1721 /***********************************************************************
1722 * glxdrv_wglGetProcAddress
1724 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1726 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1727 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1730 /***********************************************************************
1731 * glxdrv_wglMakeCurrent
1733 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1735 BOOL ret = FALSE;
1736 struct gl_drawable *gl;
1738 TRACE("(%p,%p)\n", hdc, ctx);
1740 if (!ctx)
1742 pglXMakeCurrent(gdi_display, None, NULL);
1743 NtCurrentTeb()->glContext = NULL;
1744 return TRUE;
1747 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1749 if (ctx->fmt != gl->format)
1751 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1752 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1753 goto done;
1756 if (TRACE_ON(wgl)) {
1757 describeContext(ctx);
1758 TRACE("hdc %p drawable %lx fmt %p ctx %p\n", hdc, gl->drawable, gl->format, ctx->ctx );
1761 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1762 if (ret)
1764 NtCurrentTeb()->glContext = ctx;
1765 ctx->has_been_current = TRUE;
1766 ctx->hdc = hdc;
1767 ctx->drawables[0] = gl->drawable;
1768 ctx->drawables[1] = gl->drawable;
1769 ctx->refresh_drawables = FALSE;
1770 goto done;
1773 SetLastError( ERROR_INVALID_HANDLE );
1775 done:
1776 release_gl_drawable( gl );
1777 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1778 return ret;
1781 /***********************************************************************
1782 * X11DRV_wglMakeContextCurrentARB
1784 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1786 BOOL ret = FALSE;
1787 struct gl_drawable *draw_gl, *read_gl = NULL;
1789 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1791 if (!ctx)
1793 pglXMakeCurrent(gdi_display, None, NULL);
1794 NtCurrentTeb()->glContext = NULL;
1795 return TRUE;
1798 if (!pglXMakeContextCurrent) return FALSE;
1800 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1802 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1803 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1804 read_gl ? read_gl->drawable : 0, ctx->ctx);
1805 if (ret)
1807 ctx->has_been_current = TRUE;
1808 ctx->hdc = draw_hdc;
1809 ctx->drawables[0] = draw_gl->drawable;
1810 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1811 ctx->refresh_drawables = FALSE;
1812 NtCurrentTeb()->glContext = ctx;
1813 goto done;
1816 SetLastError( ERROR_INVALID_HANDLE );
1817 done:
1818 release_gl_drawable( read_gl );
1819 release_gl_drawable( draw_gl );
1820 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1821 return ret;
1824 /***********************************************************************
1825 * glxdrv_wglShareLists
1827 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1829 TRACE("(%p, %p)\n", org, dest);
1831 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1832 * at context creation time but in case of WGL it is done using wglShareLists.
1833 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1834 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1835 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1836 * so there delaying context creation doesn't work.
1838 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1839 * and when a program requests sharing we recreate the destination context if it hasn't been made
1840 * current or when it hasn't shared display lists before.
1843 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1845 ERR("Could not share display lists, one of the contexts has been current already !\n");
1846 return FALSE;
1848 else if(dest->sharing)
1850 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1851 return FALSE;
1853 else
1855 describeContext(org);
1856 describeContext(dest);
1858 /* Re-create the GLX context and share display lists */
1859 pglXDestroyContext(gdi_display, dest->ctx);
1860 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1861 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1863 org->sharing = TRUE;
1864 dest->sharing = TRUE;
1865 return TRUE;
1867 return FALSE;
1870 static void wglFinish(void)
1872 struct x11drv_escape_flush_gl_drawable escape;
1873 struct gl_drawable *gl;
1874 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1876 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1877 escape.gl_drawable = 0;
1879 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1881 switch (gl->type)
1883 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1884 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1885 default: break;
1887 sync_context(ctx);
1888 release_gl_drawable( gl );
1891 pglFinish();
1892 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1895 static void wglFlush(void)
1897 struct x11drv_escape_flush_gl_drawable escape;
1898 struct gl_drawable *gl;
1899 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1901 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1902 escape.gl_drawable = 0;
1904 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1906 switch (gl->type)
1908 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1909 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1910 default: break;
1912 sync_context(ctx);
1913 release_gl_drawable( gl );
1916 pglFlush();
1917 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1920 /***********************************************************************
1921 * X11DRV_wglCreateContextAttribsARB
1923 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
1924 const int* attribList )
1926 struct wgl_context *ret = NULL;
1927 struct gl_drawable *gl;
1928 int err = 0;
1930 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
1932 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1934 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1935 return NULL;
1938 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1940 ret->hdc = hdc;
1941 ret->fmt = gl->format;
1942 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
1943 ret->gl3_context = TRUE;
1944 ret->numAttribs = 0;
1945 if (attribList)
1947 int *pContextAttribList = &ret->attribList[0];
1948 /* attribList consists of pairs {token, value] terminated with 0 */
1949 while(attribList[0] != 0)
1951 TRACE("%#x %#x\n", attribList[0], attribList[1]);
1952 switch(attribList[0])
1954 case WGL_CONTEXT_MAJOR_VERSION_ARB:
1955 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
1956 pContextAttribList[1] = attribList[1];
1957 pContextAttribList += 2;
1958 break;
1959 case WGL_CONTEXT_MINOR_VERSION_ARB:
1960 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
1961 pContextAttribList[1] = attribList[1];
1962 pContextAttribList += 2;
1963 break;
1964 case WGL_CONTEXT_LAYER_PLANE_ARB:
1965 break;
1966 case WGL_CONTEXT_FLAGS_ARB:
1967 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
1968 pContextAttribList[1] = attribList[1];
1969 pContextAttribList += 2;
1970 break;
1971 case WGL_CONTEXT_PROFILE_MASK_ARB:
1972 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
1973 pContextAttribList[1] = attribList[1];
1974 pContextAttribList += 2;
1975 break;
1976 default:
1977 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
1979 ret->numAttribs++;
1980 attribList += 2;
1984 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1985 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
1986 XSync(gdi_display, False);
1987 if ((err = X11DRV_check_error()) || !ret->ctx)
1989 /* In the future we should convert the GLX error to a win32 one here if needed */
1990 ERR("Context creation failed (error %x)\n", err);
1991 HeapFree( GetProcessHeap(), 0, ret );
1992 ret = NULL;
1994 else list_add_head( &context_list, &ret->entry );
1997 release_gl_drawable( gl );
1998 TRACE( "%p -> %p\n", hdc, ret );
1999 return ret;
2003 * X11DRV_wglGetExtensionsStringARB
2005 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2007 static const GLubyte *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2009 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2010 return (const GLubyte *)WineGLInfo.wglExtensions;
2014 * X11DRV_wglCreatePbufferARB
2016 * WGL_ARB_pbuffer: wglCreatePbufferARB
2018 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2019 const int *piAttribList )
2021 struct wgl_pbuffer* object = NULL;
2022 const struct wgl_pixel_format *fmt = NULL;
2023 int attribs[256];
2024 int nAttribs = 0;
2026 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2028 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2029 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2030 if(!fmt) {
2031 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2032 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2033 return NULL;
2036 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2037 if (NULL == object) {
2038 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2039 return NULL;
2041 object->width = iWidth;
2042 object->height = iHeight;
2043 object->fmt = fmt;
2045 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2046 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2047 while (piAttribList && 0 != *piAttribList) {
2048 int attr_v;
2049 switch (*piAttribList) {
2050 case WGL_PBUFFER_LARGEST_ARB: {
2051 ++piAttribList;
2052 attr_v = *piAttribList;
2053 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2054 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2055 break;
2058 case WGL_TEXTURE_FORMAT_ARB: {
2059 ++piAttribList;
2060 attr_v = *piAttribList;
2061 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2062 if (WGL_NO_TEXTURE_ARB == attr_v) {
2063 object->use_render_texture = 0;
2064 } else {
2065 if (!use_render_texture_emulation) {
2066 SetLastError(ERROR_INVALID_DATA);
2067 goto create_failed;
2069 switch (attr_v) {
2070 case WGL_TEXTURE_RGB_ARB:
2071 object->use_render_texture = GL_RGB;
2072 object->texture_bpp = 3;
2073 object->texture_format = GL_RGB;
2074 object->texture_type = GL_UNSIGNED_BYTE;
2075 break;
2076 case WGL_TEXTURE_RGBA_ARB:
2077 object->use_render_texture = GL_RGBA;
2078 object->texture_bpp = 4;
2079 object->texture_format = GL_RGBA;
2080 object->texture_type = GL_UNSIGNED_BYTE;
2081 break;
2083 /* WGL_FLOAT_COMPONENTS_NV */
2084 case WGL_TEXTURE_FLOAT_R_NV:
2085 object->use_render_texture = GL_FLOAT_R_NV;
2086 object->texture_bpp = 4;
2087 object->texture_format = GL_RED;
2088 object->texture_type = GL_FLOAT;
2089 break;
2090 case WGL_TEXTURE_FLOAT_RG_NV:
2091 object->use_render_texture = GL_FLOAT_RG_NV;
2092 object->texture_bpp = 8;
2093 object->texture_format = GL_LUMINANCE_ALPHA;
2094 object->texture_type = GL_FLOAT;
2095 break;
2096 case WGL_TEXTURE_FLOAT_RGB_NV:
2097 object->use_render_texture = GL_FLOAT_RGB_NV;
2098 object->texture_bpp = 12;
2099 object->texture_format = GL_RGB;
2100 object->texture_type = GL_FLOAT;
2101 break;
2102 case WGL_TEXTURE_FLOAT_RGBA_NV:
2103 object->use_render_texture = GL_FLOAT_RGBA_NV;
2104 object->texture_bpp = 16;
2105 object->texture_format = GL_RGBA;
2106 object->texture_type = GL_FLOAT;
2107 break;
2108 default:
2109 ERR("Unknown texture format: %x\n", attr_v);
2110 SetLastError(ERROR_INVALID_DATA);
2111 goto create_failed;
2114 break;
2117 case WGL_TEXTURE_TARGET_ARB: {
2118 ++piAttribList;
2119 attr_v = *piAttribList;
2120 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2121 if (WGL_NO_TEXTURE_ARB == attr_v) {
2122 object->texture_target = 0;
2123 } else {
2124 if (!use_render_texture_emulation) {
2125 SetLastError(ERROR_INVALID_DATA);
2126 goto create_failed;
2128 switch (attr_v) {
2129 case WGL_TEXTURE_CUBE_MAP_ARB: {
2130 if (iWidth != iHeight) {
2131 SetLastError(ERROR_INVALID_DATA);
2132 goto create_failed;
2134 object->texture_target = GL_TEXTURE_CUBE_MAP;
2135 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2136 break;
2138 case WGL_TEXTURE_1D_ARB: {
2139 if (1 != iHeight) {
2140 SetLastError(ERROR_INVALID_DATA);
2141 goto create_failed;
2143 object->texture_target = GL_TEXTURE_1D;
2144 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2145 break;
2147 case WGL_TEXTURE_2D_ARB: {
2148 object->texture_target = GL_TEXTURE_2D;
2149 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2150 break;
2152 case WGL_TEXTURE_RECTANGLE_NV: {
2153 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2154 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2155 break;
2157 default:
2158 ERR("Unknown texture target: %x\n", attr_v);
2159 SetLastError(ERROR_INVALID_DATA);
2160 goto create_failed;
2163 break;
2166 case WGL_MIPMAP_TEXTURE_ARB: {
2167 ++piAttribList;
2168 attr_v = *piAttribList;
2169 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2170 if (!use_render_texture_emulation) {
2171 SetLastError(ERROR_INVALID_DATA);
2172 goto create_failed;
2174 break;
2177 ++piAttribList;
2180 PUSH1(attribs, None);
2181 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2182 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2183 if (!object->drawable) {
2184 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2185 goto create_failed; /* unexpected error */
2187 TRACE("->(%p)\n", object);
2188 return object;
2190 create_failed:
2191 HeapFree(GetProcessHeap(), 0, object);
2192 TRACE("->(FAILED)\n");
2193 return NULL;
2197 * X11DRV_wglDestroyPbufferARB
2199 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2201 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2203 TRACE("(%p)\n", object);
2205 pglXDestroyPbuffer(gdi_display, object->drawable);
2206 HeapFree(GetProcessHeap(), 0, object);
2207 return GL_TRUE;
2211 * X11DRV_wglGetPbufferDCARB
2213 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2215 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2217 struct x11drv_escape_set_drawable escape;
2218 struct gl_drawable *gl, *prev;
2219 HDC hdc;
2221 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2222 if (!hdc) return 0;
2224 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2226 DeleteDC( hdc );
2227 return 0;
2229 gl->type = DC_GL_PBUFFER;
2230 gl->drawable = object->drawable;
2231 gl->format = object->fmt;
2233 EnterCriticalSection( &context_section );
2234 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2235 free_gl_drawable( prev );
2236 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2237 LeaveCriticalSection( &context_section );
2239 escape.code = X11DRV_SET_DRAWABLE;
2240 escape.hwnd = 0;
2241 escape.drawable = object->drawable;
2242 escape.mode = IncludeInferiors;
2243 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2244 escape.fbconfig_id = object->fmt->fmt_id;
2245 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2247 TRACE( "(%p)->(%p)\n", object, hdc );
2248 return hdc;
2252 * X11DRV_wglQueryPbufferARB
2254 * WGL_ARB_pbuffer: wglQueryPbufferARB
2256 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2258 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2260 switch (iAttribute) {
2261 case WGL_PBUFFER_WIDTH_ARB:
2262 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2263 break;
2264 case WGL_PBUFFER_HEIGHT_ARB:
2265 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2266 break;
2268 case WGL_PBUFFER_LOST_ARB:
2269 /* GLX Pbuffers cannot be lost by default. We can support this by
2270 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2271 * to receive pixel buffer clobber events, however that may or may
2272 * not give any benefit */
2273 *piValue = GL_FALSE;
2274 break;
2276 case WGL_TEXTURE_FORMAT_ARB:
2277 if (!object->use_render_texture) {
2278 *piValue = WGL_NO_TEXTURE_ARB;
2279 } else {
2280 if (!use_render_texture_emulation) {
2281 SetLastError(ERROR_INVALID_HANDLE);
2282 return GL_FALSE;
2284 switch(object->use_render_texture) {
2285 case GL_RGB:
2286 *piValue = WGL_TEXTURE_RGB_ARB;
2287 break;
2288 case GL_RGBA:
2289 *piValue = WGL_TEXTURE_RGBA_ARB;
2290 break;
2291 /* WGL_FLOAT_COMPONENTS_NV */
2292 case GL_FLOAT_R_NV:
2293 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2294 break;
2295 case GL_FLOAT_RG_NV:
2296 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2297 break;
2298 case GL_FLOAT_RGB_NV:
2299 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2300 break;
2301 case GL_FLOAT_RGBA_NV:
2302 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2303 break;
2304 default:
2305 ERR("Unknown texture format: %x\n", object->use_render_texture);
2308 break;
2310 case WGL_TEXTURE_TARGET_ARB:
2311 if (!object->texture_target){
2312 *piValue = WGL_NO_TEXTURE_ARB;
2313 } else {
2314 if (!use_render_texture_emulation) {
2315 SetLastError(ERROR_INVALID_DATA);
2316 return GL_FALSE;
2318 switch (object->texture_target) {
2319 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2320 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2321 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2322 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2325 break;
2327 case WGL_MIPMAP_TEXTURE_ARB:
2328 *piValue = GL_FALSE; /** don't support that */
2329 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2330 break;
2332 default:
2333 FIXME("unexpected attribute %x\n", iAttribute);
2334 break;
2337 return GL_TRUE;
2341 * X11DRV_wglReleasePbufferDCARB
2343 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2345 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2347 struct gl_drawable *gl;
2349 TRACE("(%p, %p)\n", object, hdc);
2351 EnterCriticalSection( &context_section );
2353 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2355 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2356 free_gl_drawable( gl );
2358 else hdc = 0;
2360 LeaveCriticalSection( &context_section );
2362 return hdc && DeleteDC(hdc);
2366 * X11DRV_wglSetPbufferAttribARB
2368 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2370 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2372 GLboolean ret = GL_FALSE;
2374 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2376 if (!object->use_render_texture) {
2377 SetLastError(ERROR_INVALID_HANDLE);
2378 return GL_FALSE;
2380 if (1 == use_render_texture_emulation) {
2381 return GL_TRUE;
2383 return ret;
2387 * X11DRV_wglChoosePixelFormatARB
2389 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2391 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2392 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2394 int attribs[256];
2395 int nAttribs = 0;
2396 GLXFBConfig* cfgs = NULL;
2397 int nCfgs = 0;
2398 int it;
2399 int fmt_id;
2400 int start, end;
2401 UINT pfmt_it = 0;
2402 int run;
2403 int i;
2404 DWORD dwFlags = 0;
2406 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2407 if (NULL != pfAttribFList) {
2408 FIXME("unused pfAttribFList\n");
2411 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2412 if (-1 == nAttribs) {
2413 WARN("Cannot convert WGL to GLX attributes\n");
2414 return GL_FALSE;
2416 PUSH1(attribs, None);
2418 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2419 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2420 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2422 for(i=0; piAttribIList[i] != 0; i+=2)
2424 switch(piAttribIList[i])
2426 case WGL_DRAW_TO_BITMAP_ARB:
2427 if(piAttribIList[i+1])
2428 dwFlags |= PFD_DRAW_TO_BITMAP;
2429 break;
2430 case WGL_ACCELERATION_ARB:
2431 switch(piAttribIList[i+1])
2433 case WGL_NO_ACCELERATION_ARB:
2434 dwFlags |= PFD_GENERIC_FORMAT;
2435 break;
2436 case WGL_GENERIC_ACCELERATION_ARB:
2437 dwFlags |= PFD_GENERIC_ACCELERATED;
2438 break;
2439 case WGL_FULL_ACCELERATION_ARB:
2440 /* Nothing to do */
2441 break;
2443 break;
2444 case WGL_SUPPORT_GDI_ARB:
2445 if(piAttribIList[i+1])
2446 dwFlags |= PFD_SUPPORT_GDI;
2447 break;
2451 /* Search for FB configurations matching the requirements in attribs */
2452 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2453 if (NULL == cfgs) {
2454 WARN("Compatible Pixel Format not found\n");
2455 return GL_FALSE;
2458 /* Loop through all matching formats and check if they are suitable.
2459 * Note that this function should at max return nMaxFormats different formats */
2460 for(run=0; run < 2; run++)
2462 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2464 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2466 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2467 continue;
2470 /* During the first run we only want onscreen formats and during the second only offscreen */
2471 start = run == 1 ? nb_onscreen_formats : 0;
2472 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2474 for (i = start; i < end; i++)
2476 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2478 piFormats[pfmt_it++] = i + 1;
2479 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2480 it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2481 break;
2487 *nNumFormats = pfmt_it;
2488 /** free list */
2489 XFree(cfgs);
2490 return GL_TRUE;
2494 * X11DRV_wglGetPixelFormatAttribivARB
2496 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2498 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2499 UINT nAttributes, const int *piAttributes, int *piValues )
2501 UINT i;
2502 const struct wgl_pixel_format *fmt = NULL;
2503 int hTest;
2504 int tmp;
2505 int curGLXAttr = 0;
2507 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2509 if (0 < iLayerPlane) {
2510 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2511 return GL_FALSE;
2514 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2515 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2516 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2517 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2518 if(!fmt) {
2519 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2522 for (i = 0; i < nAttributes; ++i) {
2523 const int curWGLAttr = piAttributes[i];
2524 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2526 switch (curWGLAttr) {
2527 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2528 piValues[i] = nb_pixel_formats;
2529 continue;
2531 case WGL_SUPPORT_OPENGL_ARB:
2532 piValues[i] = GL_TRUE;
2533 continue;
2535 case WGL_ACCELERATION_ARB:
2536 curGLXAttr = GLX_CONFIG_CAVEAT;
2537 if (!fmt) goto pix_error;
2538 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2539 piValues[i] = WGL_NO_ACCELERATION_ARB;
2540 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2541 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2542 else
2543 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2544 continue;
2546 case WGL_TRANSPARENT_ARB:
2547 curGLXAttr = GLX_TRANSPARENT_TYPE;
2548 if (!fmt) goto pix_error;
2549 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2550 if (hTest) goto get_error;
2551 piValues[i] = GL_FALSE;
2552 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2553 continue;
2555 case WGL_PIXEL_TYPE_ARB:
2556 curGLXAttr = GLX_RENDER_TYPE;
2557 if (!fmt) goto pix_error;
2558 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2559 if (hTest) goto get_error;
2560 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2561 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2562 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2563 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2564 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2565 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2566 else {
2567 ERR("unexpected RenderType(%x)\n", tmp);
2568 piValues[i] = WGL_TYPE_RGBA_ARB;
2570 continue;
2572 case WGL_COLOR_BITS_ARB:
2573 curGLXAttr = GLX_BUFFER_SIZE;
2574 break;
2576 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2577 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2578 if (!use_render_texture_emulation) {
2579 piValues[i] = GL_FALSE;
2580 continue;
2582 curGLXAttr = GLX_RENDER_TYPE;
2583 if (!fmt) goto pix_error;
2584 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2585 if (hTest) goto get_error;
2586 if (GLX_COLOR_INDEX_BIT == tmp) {
2587 piValues[i] = GL_FALSE;
2588 continue;
2590 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2591 if (hTest) goto get_error;
2592 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2593 continue;
2595 case WGL_BLUE_BITS_ARB:
2596 curGLXAttr = GLX_BLUE_SIZE;
2597 break;
2598 case WGL_RED_BITS_ARB:
2599 curGLXAttr = GLX_RED_SIZE;
2600 break;
2601 case WGL_GREEN_BITS_ARB:
2602 curGLXAttr = GLX_GREEN_SIZE;
2603 break;
2604 case WGL_ALPHA_BITS_ARB:
2605 curGLXAttr = GLX_ALPHA_SIZE;
2606 break;
2607 case WGL_DEPTH_BITS_ARB:
2608 curGLXAttr = GLX_DEPTH_SIZE;
2609 break;
2610 case WGL_STENCIL_BITS_ARB:
2611 curGLXAttr = GLX_STENCIL_SIZE;
2612 break;
2613 case WGL_DOUBLE_BUFFER_ARB:
2614 curGLXAttr = GLX_DOUBLEBUFFER;
2615 break;
2616 case WGL_STEREO_ARB:
2617 curGLXAttr = GLX_STEREO;
2618 break;
2619 case WGL_AUX_BUFFERS_ARB:
2620 curGLXAttr = GLX_AUX_BUFFERS;
2621 break;
2623 case WGL_SUPPORT_GDI_ARB:
2624 if (!fmt) goto pix_error;
2625 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2626 continue;
2628 case WGL_DRAW_TO_BITMAP_ARB:
2629 if (!fmt) goto pix_error;
2630 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2631 continue;
2633 case WGL_DRAW_TO_WINDOW_ARB:
2634 case WGL_DRAW_TO_PBUFFER_ARB:
2635 if (!fmt) goto pix_error;
2636 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2637 if (hTest) goto get_error;
2638 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2639 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2640 piValues[i] = GL_TRUE;
2641 else
2642 piValues[i] = GL_FALSE;
2643 continue;
2645 case WGL_SWAP_METHOD_ARB:
2646 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2647 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2648 * point only ATI offers this.
2650 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2651 break;
2653 case WGL_PBUFFER_LARGEST_ARB:
2654 curGLXAttr = GLX_LARGEST_PBUFFER;
2655 break;
2657 case WGL_SAMPLE_BUFFERS_ARB:
2658 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2659 break;
2661 case WGL_SAMPLES_ARB:
2662 curGLXAttr = GLX_SAMPLES_ARB;
2663 break;
2665 case WGL_FLOAT_COMPONENTS_NV:
2666 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2667 break;
2669 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2670 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2671 break;
2673 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2674 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2675 break;
2677 case WGL_ACCUM_RED_BITS_ARB:
2678 curGLXAttr = GLX_ACCUM_RED_SIZE;
2679 break;
2680 case WGL_ACCUM_GREEN_BITS_ARB:
2681 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2682 break;
2683 case WGL_ACCUM_BLUE_BITS_ARB:
2684 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2685 break;
2686 case WGL_ACCUM_ALPHA_BITS_ARB:
2687 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2688 break;
2689 case WGL_ACCUM_BITS_ARB:
2690 if (!fmt) goto pix_error;
2691 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2692 if (hTest) goto get_error;
2693 piValues[i] = tmp;
2694 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2695 if (hTest) goto get_error;
2696 piValues[i] += tmp;
2697 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2698 if (hTest) goto get_error;
2699 piValues[i] += tmp;
2700 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2701 if (hTest) goto get_error;
2702 piValues[i] += tmp;
2703 continue;
2705 default:
2706 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2709 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2710 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2711 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2713 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2714 * and check which options can work using iPixelFormat=0 and which not.
2715 * A problem would be that this function is an extension. This would
2716 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2718 if (0 != curGLXAttr && iPixelFormat != 0) {
2719 if (!fmt) goto pix_error;
2720 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2721 if (hTest) goto get_error;
2722 curGLXAttr = 0;
2723 } else {
2724 piValues[i] = GL_FALSE;
2727 return GL_TRUE;
2729 get_error:
2730 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2731 return GL_FALSE;
2733 pix_error:
2734 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2735 return GL_FALSE;
2739 * X11DRV_wglGetPixelFormatAttribfvARB
2741 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2743 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2744 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2746 int *attr;
2747 int ret;
2748 UINT i;
2750 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2752 /* Allocate a temporary array to store integer values */
2753 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2754 if (!attr) {
2755 ERR("couldn't allocate %d array\n", nAttributes);
2756 return GL_FALSE;
2759 /* Piggy-back on wglGetPixelFormatAttribivARB */
2760 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2761 if (ret) {
2762 /* Convert integer values to float. Should also check for attributes
2763 that can give decimal values here */
2764 for (i=0; i<nAttributes;i++) {
2765 pfValues[i] = attr[i];
2769 HeapFree(GetProcessHeap(), 0, attr);
2770 return ret;
2774 * X11DRV_wglBindTexImageARB
2776 * WGL_ARB_render_texture: wglBindTexImageARB
2778 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2780 GLboolean ret = GL_FALSE;
2782 TRACE("(%p, %d)\n", object, iBuffer);
2784 if (!object->use_render_texture) {
2785 SetLastError(ERROR_INVALID_HANDLE);
2786 return GL_FALSE;
2789 if (1 == use_render_texture_emulation) {
2790 static int init = 0;
2791 int prev_binded_texture = 0;
2792 GLXContext prev_context;
2793 Drawable prev_drawable;
2794 GLXContext tmp_context;
2796 prev_context = pglXGetCurrentContext();
2797 prev_drawable = pglXGetCurrentDrawable();
2799 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2800 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2801 isn't ideal performance wise but I wasn't able to get other ways working.
2803 if(!init) {
2804 init = 1; /* Only show the FIXME once for performance reasons */
2805 FIXME("partial stub!\n");
2808 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2809 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2811 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2813 /* Switch to our pbuffer */
2814 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2816 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2817 * After that upload the pbuffer texture data. */
2818 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2819 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2821 /* Switch back to the original drawable and upload the pbuffer-texture */
2822 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2823 pglXDestroyContext(gdi_display, tmp_context);
2824 return GL_TRUE;
2827 return ret;
2831 * X11DRV_wglReleaseTexImageARB
2833 * WGL_ARB_render_texture: wglReleaseTexImageARB
2835 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2837 GLboolean ret = GL_FALSE;
2839 TRACE("(%p, %d)\n", object, iBuffer);
2841 if (!object->use_render_texture) {
2842 SetLastError(ERROR_INVALID_HANDLE);
2843 return GL_FALSE;
2845 if (1 == use_render_texture_emulation) {
2846 return GL_TRUE;
2848 return ret;
2852 * X11DRV_wglGetExtensionsStringEXT
2854 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2856 static const GLubyte *X11DRV_wglGetExtensionsStringEXT(void)
2858 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2859 return (const GLubyte *)WineGLInfo.wglExtensions;
2863 * X11DRV_wglGetSwapIntervalEXT
2865 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2867 static int X11DRV_wglGetSwapIntervalEXT(void)
2869 /* GLX_SGI_swap_control doesn't have any provisions for getting the swap
2870 * interval, so the swap interval has to be tracked. */
2871 TRACE("()\n");
2872 return swap_interval;
2876 * X11DRV_wglSwapIntervalEXT
2878 * WGL_EXT_swap_control: wglSwapIntervalEXT
2880 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
2882 BOOL ret = TRUE;
2884 TRACE("(%d)\n", interval);
2886 if (interval < 0)
2888 SetLastError(ERROR_INVALID_DATA);
2889 return FALSE;
2891 else if (!has_swap_control && interval == 0)
2893 /* wglSwapIntervalEXT considers an interval value of zero to mean that
2894 * vsync should be disabled, but glXSwapIntervalSGI considers such a
2895 * value to be an error. Just silently ignore the request for now. */
2896 WARN("Request to disable vertical sync is not handled\n");
2897 swap_interval = 0;
2899 else
2901 if (pglXSwapIntervalSGI)
2902 ret = !pglXSwapIntervalSGI(interval);
2903 else
2904 WARN("GLX_SGI_swap_control extension is not available\n");
2906 if (ret)
2907 swap_interval = interval;
2908 else
2909 SetLastError(ERROR_DC_NOT_FOUND);
2912 return ret;
2916 * X11DRV_wglSetPixelFormatWINE
2918 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
2919 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
2921 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
2923 const struct wgl_pixel_format *fmt;
2924 int value;
2925 HWND hwnd;
2927 TRACE("(%p,%d)\n", hdc, format);
2929 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
2930 if (!fmt)
2932 ERR( "Invalid format %d\n", format );
2933 return FALSE;
2936 hwnd = WindowFromDC( hdc );
2937 if (!hwnd || hwnd == GetDesktopWindow())
2939 ERR( "not a valid window DC %p\n", hdc );
2940 return FALSE;
2943 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
2944 if (!(value & GLX_WINDOW_BIT))
2946 WARN( "Pixel format %d is not compatible for window rendering\n", format );
2947 return FALSE;
2950 return set_win_format( hwnd, fmt );
2954 * glxRequireVersion (internal)
2956 * Check if the supported GLX version matches requiredVersion.
2958 static BOOL glxRequireVersion(int requiredVersion)
2960 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2961 if(requiredVersion <= WineGLInfo.glxVersion[1])
2962 return TRUE;
2964 return FALSE;
2967 static void register_extension(const char *ext)
2969 if (WineGLInfo.wglExtensions[0])
2970 strcat(WineGLInfo.wglExtensions, " ");
2971 strcat(WineGLInfo.wglExtensions, ext);
2973 TRACE("'%s'\n", ext);
2977 * X11DRV_WineGL_LoadExtensions
2979 static void X11DRV_WineGL_LoadExtensions(void)
2981 WineGLInfo.wglExtensions[0] = 0;
2983 /* ARB Extensions */
2985 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
2987 register_extension( "WGL_ARB_create_context" );
2988 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
2990 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
2991 register_extension("WGL_ARB_create_context_profile");
2994 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
2996 register_extension("WGL_ARB_pixel_format_float");
2997 register_extension("WGL_ATI_pixel_format_float");
3000 register_extension( "WGL_ARB_extensions_string" );
3001 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3003 if (glxRequireVersion(3))
3005 register_extension( "WGL_ARB_make_current_read" );
3006 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3007 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3010 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3012 if (glxRequireVersion(3))
3014 register_extension( "WGL_ARB_pbuffer" );
3015 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3016 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3017 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3018 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3019 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3020 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3023 register_extension( "WGL_ARB_pixel_format" );
3024 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3025 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3026 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3028 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3029 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3030 (glxRequireVersion(3) && use_render_texture_emulation))
3032 register_extension( "WGL_ARB_render_texture" );
3033 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3034 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3036 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3037 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3038 register_extension("WGL_NV_float_buffer");
3040 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3041 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3042 register_extension("WGL_NV_render_texture_rectangle");
3045 /* EXT Extensions */
3047 register_extension( "WGL_EXT_extensions_string" );
3048 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3050 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3051 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3052 register_extension( "WGL_EXT_swap_control" );
3053 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3054 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3056 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3057 register_extension("WGL_EXT_framebuffer_sRGB");
3059 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3060 register_extension("WGL_EXT_pixel_format_packed_float");
3062 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3063 has_swap_control = TRUE;
3065 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3066 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3068 register_extension( "WGL_NV_vertex_array_range" );
3069 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3070 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3073 /* WINE-specific WGL Extensions */
3075 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3076 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3078 register_extension( "WGL_WINE_pixel_format_passthrough" );
3079 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3084 * glxdrv_SwapBuffers
3086 * Swap the buffers of this DC
3088 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3090 struct x11drv_escape_flush_gl_drawable escape;
3091 struct gl_drawable *gl;
3092 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3094 TRACE("(%p)\n", hdc);
3096 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3097 escape.gl_drawable = 0;
3099 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3101 SetLastError( ERROR_INVALID_HANDLE );
3102 return FALSE;
3105 switch (gl->type)
3107 case DC_GL_PIXMAP_WIN:
3108 if (ctx) sync_context( ctx );
3109 escape.gl_drawable = gl->pixmap;
3110 if (pglXCopySubBufferMESA) {
3111 /* (glX)SwapBuffers has an implicit glFlush effect, however
3112 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3113 * copying */
3114 pglFlush();
3115 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3116 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3117 break;
3119 pglXSwapBuffers(gdi_display, gl->drawable);
3120 break;
3121 case DC_GL_CHILD_WIN:
3122 escape.gl_drawable = gl->drawable;
3123 /* fall through */
3124 default:
3125 pglXSwapBuffers(gdi_display, gl->drawable);
3126 break;
3129 release_gl_drawable( gl );
3131 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3132 return TRUE;
3135 static struct opengl_funcs opengl_funcs =
3138 glxdrv_wglCopyContext, /* p_wglCopyContext */
3139 glxdrv_wglCreateContext, /* p_wglCreateContext */
3140 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3141 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3142 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3143 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3144 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3145 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3146 glxdrv_wglShareLists, /* p_wglShareLists */
3147 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3151 struct opengl_funcs *get_glx_driver( UINT version )
3153 if (version != WINE_WGL_DRIVER_VERSION)
3155 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3156 return NULL;
3158 if (has_opengl()) return &opengl_funcs;
3159 return NULL;
3162 #else /* no OpenGL includes */
3164 struct opengl_funcs *get_glx_driver( UINT version )
3166 return NULL;
3169 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3173 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3177 void destroy_gl_drawable( HWND hwnd )
3181 #endif /* defined(SONAME_LIBGL) */