d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / winex11.drv / opengl.c
blob71af3db30d6abf7c8d544fb2a255977a130d54e1
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
5 * Copyright 2005 Alex Woods
6 * Copyright 2005 Raphael Junqueira
7 * Copyright 2006-2009 Roderick Colenbrander
8 * Copyright 2006 Tomas Carnecky
9 * Copyright 2012 Alexandre Julliard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include "wine/port.h"
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_SYS_UN_H
37 #include <sys/un.h>
38 #endif
40 #include "x11drv.h"
41 #include "xcomposite.h"
42 #include "winternl.h"
43 #include "wine/library.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
48 #ifdef SONAME_LIBGL
50 WINE_DECLARE_DEBUG_CHANNEL(winediag);
52 #include "wine/wgl.h"
53 #include "wine/wgl_driver.h"
54 #include "wine/wglext.h"
56 typedef struct __GLXcontextRec *GLXContext;
57 typedef struct __GLXFBConfigRec *GLXFBConfig;
58 typedef XID GLXPixmap;
59 typedef XID GLXDrawable;
60 typedef XID GLXFBConfigID;
61 typedef XID GLXContextID;
62 typedef XID GLXWindow;
63 typedef XID GLXPbuffer;
65 #define GLX_USE_GL 1
66 #define GLX_BUFFER_SIZE 2
67 #define GLX_LEVEL 3
68 #define GLX_RGBA 4
69 #define GLX_DOUBLEBUFFER 5
70 #define GLX_STEREO 6
71 #define GLX_AUX_BUFFERS 7
72 #define GLX_RED_SIZE 8
73 #define GLX_GREEN_SIZE 9
74 #define GLX_BLUE_SIZE 10
75 #define GLX_ALPHA_SIZE 11
76 #define GLX_DEPTH_SIZE 12
77 #define GLX_STENCIL_SIZE 13
78 #define GLX_ACCUM_RED_SIZE 14
79 #define GLX_ACCUM_GREEN_SIZE 15
80 #define GLX_ACCUM_BLUE_SIZE 16
81 #define GLX_ACCUM_ALPHA_SIZE 17
83 #define GLX_BAD_SCREEN 1
84 #define GLX_BAD_ATTRIBUTE 2
85 #define GLX_NO_EXTENSION 3
86 #define GLX_BAD_VISUAL 4
87 #define GLX_BAD_CONTEXT 5
88 #define GLX_BAD_VALUE 6
89 #define GLX_BAD_ENUM 7
91 #define GLX_VENDOR 1
92 #define GLX_VERSION 2
93 #define GLX_EXTENSIONS 3
95 #define GLX_CONFIG_CAVEAT 0x20
96 #define GLX_DONT_CARE 0xFFFFFFFF
97 #define GLX_X_VISUAL_TYPE 0x22
98 #define GLX_TRANSPARENT_TYPE 0x23
99 #define GLX_TRANSPARENT_INDEX_VALUE 0x24
100 #define GLX_TRANSPARENT_RED_VALUE 0x25
101 #define GLX_TRANSPARENT_GREEN_VALUE 0x26
102 #define GLX_TRANSPARENT_BLUE_VALUE 0x27
103 #define GLX_TRANSPARENT_ALPHA_VALUE 0x28
104 #define GLX_WINDOW_BIT 0x00000001
105 #define GLX_PIXMAP_BIT 0x00000002
106 #define GLX_PBUFFER_BIT 0x00000004
107 #define GLX_AUX_BUFFERS_BIT 0x00000010
108 #define GLX_FRONT_LEFT_BUFFER_BIT 0x00000001
109 #define GLX_FRONT_RIGHT_BUFFER_BIT 0x00000002
110 #define GLX_BACK_LEFT_BUFFER_BIT 0x00000004
111 #define GLX_BACK_RIGHT_BUFFER_BIT 0x00000008
112 #define GLX_DEPTH_BUFFER_BIT 0x00000020
113 #define GLX_STENCIL_BUFFER_BIT 0x00000040
114 #define GLX_ACCUM_BUFFER_BIT 0x00000080
115 #define GLX_NONE 0x8000
116 #define GLX_SLOW_CONFIG 0x8001
117 #define GLX_TRUE_COLOR 0x8002
118 #define GLX_DIRECT_COLOR 0x8003
119 #define GLX_PSEUDO_COLOR 0x8004
120 #define GLX_STATIC_COLOR 0x8005
121 #define GLX_GRAY_SCALE 0x8006
122 #define GLX_STATIC_GRAY 0x8007
123 #define GLX_TRANSPARENT_RGB 0x8008
124 #define GLX_TRANSPARENT_INDEX 0x8009
125 #define GLX_VISUAL_ID 0x800B
126 #define GLX_SCREEN 0x800C
127 #define GLX_NON_CONFORMANT_CONFIG 0x800D
128 #define GLX_DRAWABLE_TYPE 0x8010
129 #define GLX_RENDER_TYPE 0x8011
130 #define GLX_X_RENDERABLE 0x8012
131 #define GLX_FBCONFIG_ID 0x8013
132 #define GLX_RGBA_TYPE 0x8014
133 #define GLX_COLOR_INDEX_TYPE 0x8015
134 #define GLX_MAX_PBUFFER_WIDTH 0x8016
135 #define GLX_MAX_PBUFFER_HEIGHT 0x8017
136 #define GLX_MAX_PBUFFER_PIXELS 0x8018
137 #define GLX_PRESERVED_CONTENTS 0x801B
138 #define GLX_LARGEST_PBUFFER 0x801C
139 #define GLX_WIDTH 0x801D
140 #define GLX_HEIGHT 0x801E
141 #define GLX_EVENT_MASK 0x801F
142 #define GLX_DAMAGED 0x8020
143 #define GLX_SAVED 0x8021
144 #define GLX_WINDOW 0x8022
145 #define GLX_PBUFFER 0x8023
146 #define GLX_PBUFFER_HEIGHT 0x8040
147 #define GLX_PBUFFER_WIDTH 0x8041
148 #define GLX_RGBA_BIT 0x00000001
149 #define GLX_COLOR_INDEX_BIT 0x00000002
150 #define GLX_PBUFFER_CLOBBER_MASK 0x08000000
152 /** GLX_ARB_multisample */
153 #define GLX_SAMPLE_BUFFERS_ARB 100000
154 #define GLX_SAMPLES_ARB 100001
155 /** GLX_ARB_framebuffer_sRGB */
156 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
157 /** GLX_EXT_fbconfig_packed_float */
158 #define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
159 #define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT 0x00000008
160 /** GLX_ARB_create_context */
161 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
162 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
163 #define GLX_CONTEXT_FLAGS_ARB 0x2094
164 /** GLX_ARB_create_context_profile */
165 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
166 /** GLX_ATI_pixel_format_float */
167 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
168 /** GLX_ARB_pixel_format_float */
169 #define GLX_RGBA_FLOAT_BIT 0x00000004
170 #define GLX_RGBA_FLOAT_TYPE 0x20B9
171 /** GLX_NV_float_buffer */
172 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
175 struct WineGLInfo {
176 const char *glVersion;
177 char *glExtensions;
179 int glxVersion[2];
181 const char *glxServerVersion;
182 const char *glxServerVendor;
183 const char *glxServerExtensions;
185 const char *glxClientVersion;
186 const char *glxClientVendor;
187 const char *glxClientExtensions;
189 const char *glxExtensions;
191 BOOL glxDirect;
192 char wglExtensions[4096];
195 struct wgl_pixel_format
197 GLXFBConfig fbconfig;
198 int fmt_id;
199 int render_type;
200 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
203 struct wgl_context
205 HDC hdc;
206 BOOL has_been_current;
207 BOOL sharing;
208 BOOL gl3_context;
209 XVisualInfo *vis;
210 const struct wgl_pixel_format *fmt;
211 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
212 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
213 GLXContext ctx;
214 Drawable drawables[2];
215 BOOL refresh_drawables;
216 struct list entry;
219 struct wgl_pbuffer
221 Drawable drawable;
222 const struct wgl_pixel_format* fmt;
223 int width;
224 int height;
225 int* attribList;
226 int use_render_texture; /* This is also the internal texture format */
227 int texture_bind_target;
228 int texture_bpp;
229 GLint texture_format;
230 GLuint texture_target;
231 GLenum texture_type;
232 GLuint texture;
233 int texture_level;
236 enum dc_gl_type
238 DC_GL_NONE, /* no GL support (pixel format not set yet) */
239 DC_GL_WINDOW, /* normal top-level window */
240 DC_GL_CHILD_WIN, /* child window using XComposite */
241 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
242 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
245 struct gl_drawable
247 enum dc_gl_type type; /* type of GL surface */
248 Drawable drawable; /* drawable for rendering to the client area */
249 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
250 Colormap colormap; /* colormap used for the drawable */
251 const struct wgl_pixel_format *format; /* pixel format for the drawable */
252 XVisualInfo *visual; /* information about the GL visual */
253 RECT rect; /* drawable rect, relative to whole window drawable */
254 int swap_interval;
255 BOOL refresh_swap_interval;
258 enum glx_swap_control_method
260 GLX_SWAP_CONTROL_NONE,
261 GLX_SWAP_CONTROL_EXT,
262 GLX_SWAP_CONTROL_SGI,
263 GLX_SWAP_CONTROL_MESA
266 /* X context to associate a struct gl_drawable to an hwnd */
267 static XContext gl_hwnd_context;
268 /* X context to associate a struct gl_drawable to a pbuffer hdc */
269 static XContext gl_pbuffer_context;
271 static const struct gdi_dc_funcs glxdrv_funcs;
273 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
275 return (struct glx_physdev *)dev;
278 static struct list context_list = LIST_INIT( context_list );
279 static struct WineGLInfo WineGLInfo = { 0 };
280 static struct wgl_pixel_format *pixel_formats;
281 static int nb_pixel_formats, nb_onscreen_formats;
282 static BOOL use_render_texture_emulation = TRUE;
284 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
285 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
286 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
287 static BOOL has_swap_control_tear = FALSE;
289 static CRITICAL_SECTION context_section;
290 static CRITICAL_SECTION_DEBUG critsect_debug =
292 0, 0, &context_section,
293 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
294 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
296 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
298 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
300 static struct opengl_funcs opengl_funcs;
302 #define USE_GL_FUNC(name) #name,
303 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
304 #undef USE_GL_FUNC
306 static void X11DRV_WineGL_LoadExtensions(void);
307 static void init_pixel_formats( Display *display );
308 static BOOL glxRequireVersion(int requiredVersion);
310 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
311 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
312 TRACE(" - dwFlags : ");
313 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
314 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
315 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
316 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
318 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
322 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
323 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
324 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
325 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
326 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
327 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
328 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
329 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
330 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
331 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
332 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
333 #undef TEST_AND_DUMP
334 TRACE("\n");
336 TRACE(" - iPixelType : ");
337 switch (ppfd->iPixelType) {
338 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
339 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
341 TRACE("\n");
343 TRACE(" - Color : %d\n", ppfd->cColorBits);
344 TRACE(" - Red : %d\n", ppfd->cRedBits);
345 TRACE(" - Green : %d\n", ppfd->cGreenBits);
346 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
347 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
348 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
349 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
350 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
351 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
353 TRACE(" - iLayerType : ");
354 switch (ppfd->iLayerType) {
355 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
356 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
357 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
359 TRACE("\n");
362 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
363 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
365 /* GLX 1.0 */
366 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
367 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
368 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
369 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
370 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
371 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
372 static GLXPixmap (*pglXCreateGLXPixmap)( Display *dpy, XVisualInfo *visual, Pixmap pixmap );
373 static void (*pglXDestroyGLXPixmap)( Display *dpy, GLXPixmap pixmap );
374 static Bool (*pglXQueryExtension)( Display *dpy, int *errorb, int *event );
375 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
376 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
377 static int (*pglXGetConfig)( Display *dpy, XVisualInfo *visual, int attrib, int *value );
378 static GLXContext (*pglXGetCurrentContext)( void );
379 static GLXDrawable (*pglXGetCurrentDrawable)( void );
381 /* GLX 1.1 */
382 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
383 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
384 static const char *(*pglXGetClientString)( Display *dpy, int name );
386 /* GLX 1.3 */
387 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
388 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
389 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
390 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
391 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
392 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
393 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
394 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
395 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
397 /* GLX Extensions */
398 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
399 static void* (*pglXGetProcAddressARB)(const GLubyte *);
400 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
401 static int (*pglXSwapIntervalSGI)(int);
403 /* NV GLX Extension */
404 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
405 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
407 /* MESA GLX Extensions */
408 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
409 static int (*pglXSwapIntervalMESA)(unsigned int interval);
411 /* Standard OpenGL */
412 static void (*pglFinish)(void);
413 static void (*pglFlush)(void);
414 static const GLubyte *(*pglGetString)(GLenum name);
416 static void wglFinish(void);
417 static void wglFlush(void);
418 static const GLubyte *wglGetString(GLenum name);
420 /* check if the extension is present in the list */
421 static BOOL has_extension( const char *list, const char *ext )
423 size_t len = strlen( ext );
425 while (list)
427 while (*list == ' ') list++;
428 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
429 list = strchr( list, ' ' );
431 return FALSE;
434 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
436 /* In the future we might want to find the exact X or GLX error to report back to the app */
437 return 1;
440 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
442 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
444 int screen = DefaultScreen(gdi_display);
445 Window win = 0, root = 0;
446 const char *gl_renderer;
447 const char* str;
448 XVisualInfo *vis;
449 GLXContext ctx = NULL;
450 XSetWindowAttributes attr;
451 BOOL ret = FALSE;
452 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
454 attr.override_redirect = True;
455 attr.colormap = None;
456 attr.border_pixel = 0;
458 vis = pglXChooseVisual(gdi_display, screen, attribList);
459 if (vis) {
460 #ifdef __i386__
461 WORD old_fs = wine_get_fs();
462 /* Create a GLX Context. Without one we can't query GL information */
463 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
464 if (wine_get_fs() != old_fs)
466 wine_set_fs( old_fs );
467 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
468 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
469 goto done;
471 #else
472 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
473 #endif
475 if (!ctx) goto done;
477 root = RootWindow( gdi_display, vis->screen );
478 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
479 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
480 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
481 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
482 XMapWindow( gdi_display, win );
483 else
484 win = root;
486 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
488 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
489 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
490 goto done;
492 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
493 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
494 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
495 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
496 strcpy(WineGLInfo.glExtensions, str);
497 strcat(WineGLInfo.glExtensions, legacy_extensions);
499 /* Get the common GLX version supported by GLX client and server ( major/minor) */
500 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
502 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
503 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
504 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
506 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
507 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
508 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
510 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
511 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
513 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
514 TRACE("GL renderer : %s.\n", gl_renderer);
515 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
516 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
517 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
518 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
519 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
520 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
522 if(!WineGLInfo.glxDirect)
524 int fd = ConnectionNumber(gdi_display);
525 struct sockaddr_un uaddr;
526 unsigned int uaddrlen = sizeof(struct sockaddr_un);
528 /* In general indirect rendering on a local X11 server indicates a driver problem.
529 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
531 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
532 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
533 "haven't been installed correctly (using GL renderer %s, version %s).\n",
534 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
535 debugstr_a(WineGLInfo.glVersion));
537 else
539 /* In general you would expect that if direct rendering is returned, that you receive hardware
540 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
541 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
542 * software rendering, it shows direct rendering.
544 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
545 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
546 * it shows 'Mesa X11'.
548 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
549 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
550 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
551 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
552 debugstr_a(WineGLInfo.glVersion));
554 ret = TRUE;
556 done:
557 if(vis) XFree(vis);
558 if(ctx) {
559 pglXMakeCurrent(gdi_display, None, NULL);
560 pglXDestroyContext(gdi_display, ctx);
562 if (win != root) XDestroyWindow( gdi_display, win );
563 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
564 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
565 return ret;
568 static BOOL has_opengl(void)
570 static BOOL init_done = FALSE;
571 static void *opengl_handle;
573 char buffer[200];
574 int error_base, event_base;
575 unsigned int i;
577 if (init_done) return (opengl_handle != NULL);
578 init_done = TRUE;
580 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
581 and include all dependencies */
582 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
583 if (opengl_handle == NULL)
585 ERR( "Failed to load libGL: %s\n", buffer );
586 ERR( "OpenGL support is disabled.\n");
587 return FALSE;
590 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
592 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
594 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
595 goto failed;
599 /* redirect some standard OpenGL functions */
600 #define REDIRECT(func) \
601 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
602 REDIRECT( glFinish );
603 REDIRECT( glFlush );
604 REDIRECT( glGetString );
605 #undef REDIRECT
607 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
608 if (pglXGetProcAddressARB == NULL) {
609 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
610 goto failed;
613 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
615 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
616 goto failed; \
617 } while(0)
619 /* GLX 1.0 */
620 LOAD_FUNCPTR(glXChooseVisual);
621 LOAD_FUNCPTR(glXCopyContext);
622 LOAD_FUNCPTR(glXCreateContext);
623 LOAD_FUNCPTR(glXCreateGLXPixmap);
624 LOAD_FUNCPTR(glXGetCurrentContext);
625 LOAD_FUNCPTR(glXGetCurrentDrawable);
626 LOAD_FUNCPTR(glXDestroyContext);
627 LOAD_FUNCPTR(glXDestroyGLXPixmap);
628 LOAD_FUNCPTR(glXGetConfig);
629 LOAD_FUNCPTR(glXIsDirect);
630 LOAD_FUNCPTR(glXMakeCurrent);
631 LOAD_FUNCPTR(glXSwapBuffers);
632 LOAD_FUNCPTR(glXQueryExtension);
633 LOAD_FUNCPTR(glXQueryVersion);
635 /* GLX 1.1 */
636 LOAD_FUNCPTR(glXGetClientString);
637 LOAD_FUNCPTR(glXQueryExtensionsString);
638 LOAD_FUNCPTR(glXQueryServerString);
640 /* GLX 1.3 */
641 LOAD_FUNCPTR(glXCreatePbuffer);
642 LOAD_FUNCPTR(glXCreateNewContext);
643 LOAD_FUNCPTR(glXDestroyPbuffer);
644 LOAD_FUNCPTR(glXMakeContextCurrent);
645 LOAD_FUNCPTR(glXGetFBConfigs);
646 #undef LOAD_FUNCPTR
648 /* It doesn't matter if these fail. They'll only be used if the driver reports
649 the associated extension is available (and if a driver reports the extension
650 is available but fails to provide the functions, it's quite broken) */
651 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
652 /* ARB GLX Extension */
653 LOAD_FUNCPTR(glXCreateContextAttribsARB);
654 /* EXT GLX Extension */
655 LOAD_FUNCPTR(glXSwapIntervalEXT);
656 /* MESA GLX Extension */
657 LOAD_FUNCPTR(glXSwapIntervalMESA);
658 /* SGI GLX Extension */
659 LOAD_FUNCPTR(glXSwapIntervalSGI);
660 /* NV GLX Extension */
661 LOAD_FUNCPTR(glXAllocateMemoryNV);
662 LOAD_FUNCPTR(glXFreeMemoryNV);
663 #undef LOAD_FUNCPTR
665 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
667 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
668 TRACE("GLX is up and running error_base = %d\n", error_base);
669 } else {
670 ERR( "GLX extension is missing, disabling OpenGL.\n" );
671 goto failed;
673 gl_hwnd_context = XUniqueContext();
674 gl_pbuffer_context = XUniqueContext();
676 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
677 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
678 * rendering is used.
680 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
681 * depends on the reported GLX client / server version and on the client / server extension list.
682 * Those don't have to be the same.
684 * In general the server GLX information lists the capabilities in case of indirect rendering.
685 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
686 * available and in that case the client GLX informat can be used.
687 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
688 * in the GLX version/extension list. When a program does this it works for certain for both
689 * direct and indirect rendering.
691 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
692 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
693 * extension list which causes this extension not to be listed. (Wine requires this extension).
694 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
695 * pbuffers is around.
697 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
698 * the ATI drivers and from then on use GLX client information for them.
701 if(glxRequireVersion(3)) {
702 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
703 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
704 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
705 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
706 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
707 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
708 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
709 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
711 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
712 * enable this function when the Xserver understand GLX 1.3 or newer
714 pglXQueryDrawable = NULL;
715 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
716 TRACE("Overriding ATI GLX capabilities!\n");
717 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
718 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
719 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
720 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
722 /* Use client GLX information in case of the ATI drivers. We override the
723 * capabilities over here and not somewhere else as ATI might better their
724 * life in the future. In case they release proper drivers this block of
725 * code won't be called. */
726 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
727 } else {
728 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
731 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
732 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
735 X11DRV_WineGL_LoadExtensions();
736 init_pixel_formats( gdi_display );
737 return TRUE;
739 failed:
740 wine_dlclose(opengl_handle, NULL, 0);
741 opengl_handle = NULL;
742 return FALSE;
745 static int describeContext( struct wgl_context *ctx ) {
746 int tmp;
747 int ctx_vis_id;
748 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
749 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
750 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
751 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
752 TRACE(" - VISUAL_ID 0x%x\n", tmp);
753 ctx_vis_id = tmp;
754 return ctx_vis_id;
757 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
758 int nAttribs = 0;
759 unsigned cur = 0;
760 int pop;
761 int drawattrib = 0;
762 int nvfloatattrib = GLX_DONT_CARE;
763 int pixelattrib = GLX_DONT_CARE;
765 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
766 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
767 while (iWGLAttr && 0 != iWGLAttr[cur]) {
768 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
770 switch (iWGLAttr[cur]) {
771 case WGL_AUX_BUFFERS_ARB:
772 pop = iWGLAttr[++cur];
773 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
774 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
775 break;
776 case WGL_COLOR_BITS_ARB:
777 pop = iWGLAttr[++cur];
778 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
779 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
780 break;
781 case WGL_BLUE_BITS_ARB:
782 pop = iWGLAttr[++cur];
783 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
784 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
785 break;
786 case WGL_RED_BITS_ARB:
787 pop = iWGLAttr[++cur];
788 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
789 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
790 break;
791 case WGL_GREEN_BITS_ARB:
792 pop = iWGLAttr[++cur];
793 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
794 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
795 break;
796 case WGL_ALPHA_BITS_ARB:
797 pop = iWGLAttr[++cur];
798 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
799 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
800 break;
801 case WGL_DEPTH_BITS_ARB:
802 pop = iWGLAttr[++cur];
803 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
804 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
805 break;
806 case WGL_STENCIL_BITS_ARB:
807 pop = iWGLAttr[++cur];
808 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
809 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
810 break;
811 case WGL_DOUBLE_BUFFER_ARB:
812 pop = iWGLAttr[++cur];
813 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
814 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
815 break;
816 case WGL_STEREO_ARB:
817 pop = iWGLAttr[++cur];
818 PUSH2(oGLXAttr, GLX_STEREO, pop);
819 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
820 break;
822 case WGL_PIXEL_TYPE_ARB:
823 pop = iWGLAttr[++cur];
824 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
825 switch (pop) {
826 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
827 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
828 /* This is the same as WGL_TYPE_RGBA_FLOAT_ATI but the GLX constants differ, only the ARB GLX one is widely supported so use that */
829 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
830 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
831 default:
832 ERR("unexpected PixelType(%x)\n", pop);
833 pop = 0;
835 break;
837 case WGL_SUPPORT_GDI_ARB:
838 /* This flag is set in a pixel format */
839 pop = iWGLAttr[++cur];
840 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
841 break;
843 case WGL_DRAW_TO_BITMAP_ARB:
844 /* This flag is set in a pixel format */
845 pop = iWGLAttr[++cur];
846 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
847 break;
849 case WGL_DRAW_TO_WINDOW_ARB:
850 pop = iWGLAttr[++cur];
851 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
852 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
853 if (pop) {
854 drawattrib |= GLX_WINDOW_BIT;
856 break;
858 case WGL_DRAW_TO_PBUFFER_ARB:
859 pop = iWGLAttr[++cur];
860 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
861 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
862 if (pop) {
863 drawattrib |= GLX_PBUFFER_BIT;
865 break;
867 case WGL_ACCELERATION_ARB:
868 /* This flag is set in a pixel format */
869 pop = iWGLAttr[++cur];
870 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
871 break;
873 case WGL_SUPPORT_OPENGL_ARB:
874 pop = iWGLAttr[++cur];
875 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
876 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
877 break;
879 case WGL_SWAP_METHOD_ARB:
880 pop = iWGLAttr[++cur];
881 /* For now we ignore this and just return SWAP_EXCHANGE */
882 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
883 break;
885 case WGL_PBUFFER_LARGEST_ARB:
886 pop = iWGLAttr[++cur];
887 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
888 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
889 break;
891 case WGL_SAMPLE_BUFFERS_ARB:
892 pop = iWGLAttr[++cur];
893 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
894 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
895 break;
897 case WGL_SAMPLES_ARB:
898 pop = iWGLAttr[++cur];
899 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
900 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
901 break;
903 case WGL_TEXTURE_FORMAT_ARB:
904 case WGL_TEXTURE_TARGET_ARB:
905 case WGL_MIPMAP_TEXTURE_ARB:
906 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
907 pop = iWGLAttr[++cur];
908 if (NULL == pbuf) {
909 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
911 if (!use_render_texture_emulation) {
912 if (WGL_NO_TEXTURE_ARB != pop) {
913 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
914 return -1; /** error: don't support it */
915 } else {
916 drawattrib |= GLX_PBUFFER_BIT;
919 break ;
920 case WGL_FLOAT_COMPONENTS_NV:
921 nvfloatattrib = iWGLAttr[++cur];
922 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
923 break ;
924 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
925 case WGL_BIND_TO_TEXTURE_RGB_ARB:
926 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
927 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
928 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
929 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
930 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
931 pop = iWGLAttr[++cur];
932 /** cannot be converted, see direct handling on
933 * - wglGetPixelFormatAttribivARB
934 * TODO: wglChoosePixelFormat
936 break ;
937 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
938 pop = iWGLAttr[++cur];
939 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
940 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
941 break ;
943 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
944 pop = iWGLAttr[++cur];
945 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
946 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
947 break ;
948 default:
949 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
950 cur++;
951 break;
953 ++cur;
956 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
957 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
958 * pixmap and pbuffer formats appear as well. */
959 if (!drawattrib) drawattrib = GLX_DONT_CARE;
960 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
961 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
963 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
964 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
965 * GLX_DONT_CARE unless it is overridden. */
966 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
967 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
969 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
970 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
971 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
972 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
975 return nAttribs;
978 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
980 int render_type=0, render_type_bit;
981 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
982 switch(render_type_bit)
984 case GLX_RGBA_BIT:
985 render_type = GLX_RGBA_TYPE;
986 break;
987 case GLX_COLOR_INDEX_BIT:
988 render_type = GLX_COLOR_INDEX_TYPE;
989 break;
990 case GLX_RGBA_FLOAT_BIT:
991 render_type = GLX_RGBA_FLOAT_TYPE;
992 break;
993 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
994 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
995 break;
996 default:
997 ERR("Unknown render_type: %x\n", render_type_bit);
999 return render_type;
1002 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
1003 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
1005 int dbuf, value;
1006 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
1007 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1009 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1010 * the GLX_PIXMAP_BIT set. */
1011 return !dbuf && (value & GLX_PIXMAP_BIT);
1014 static void init_pixel_formats( Display *display )
1016 struct wgl_pixel_format *list;
1017 int size = 0, onscreen_size = 0;
1018 int fmt_id, nCfgs, i, run, bmp_formats;
1019 GLXFBConfig* cfgs;
1020 XVisualInfo *visinfo;
1022 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1023 if (NULL == cfgs || 0 == nCfgs) {
1024 if(cfgs != NULL) XFree(cfgs);
1025 ERR("glXChooseFBConfig returns NULL\n");
1026 return;
1029 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1030 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1031 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1032 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1034 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1036 for(i=0, bmp_formats=0; i<nCfgs; i++)
1038 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1039 bmp_formats++;
1041 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1043 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1045 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1046 * Do this as GLX doesn't guarantee that the list is sorted */
1047 for(run=0; run < 2; run++)
1049 for(i=0; i<nCfgs; i++) {
1050 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1051 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1053 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1054 * The second run we only set offscreen formats. */
1055 if(!run && visinfo)
1057 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1058 * The contents is copied to the destination using XCopyArea. For the copying to work
1059 * the depth of the source and destination window should be the same. In general this should
1060 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1061 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1062 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1063 * list formats with the same depth. */
1064 if(visinfo->depth != default_visual.depth)
1066 XFree(visinfo);
1067 continue;
1070 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1071 list[size].fbconfig = cfgs[i];
1072 list[size].fmt_id = fmt_id;
1073 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1074 list[size].dwFlags = 0;
1075 size++;
1076 onscreen_size++;
1078 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1079 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1081 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1082 list[size].fbconfig = cfgs[i];
1083 list[size].fmt_id = fmt_id;
1084 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1085 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1086 size++;
1087 onscreen_size++;
1089 } else if(run && !visinfo) {
1090 int window_drawable=0;
1091 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1093 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1094 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1095 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1096 * likely make our child window opengl rendering more complicated since likely you can't use
1097 * XCopyArea on a GLX Window.
1098 * For now ignore fbconfigs which are window drawable but lack a visual. */
1099 if(window_drawable & GLX_WINDOW_BIT)
1101 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1102 continue;
1105 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1106 list[size].fbconfig = cfgs[i];
1107 list[size].fmt_id = fmt_id;
1108 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1109 list[size].dwFlags = 0;
1110 size++;
1113 if (visinfo) XFree(visinfo);
1117 XFree(cfgs);
1119 pixel_formats = list;
1120 nb_pixel_formats = size;
1121 nb_onscreen_formats = onscreen_size;
1124 static inline BOOL is_valid_pixel_format( int format )
1126 return format > 0 && format <= nb_pixel_formats;
1129 static inline BOOL is_onscreen_pixel_format( int format )
1131 return format > 0 && format <= nb_onscreen_formats;
1134 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1136 return format - pixel_formats + 1;
1139 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1140 * In our WGL implementation we only support a subset of these formats namely the format of
1141 * Wine's main visual and offscreen formats (if they are available).
1142 * This function converts a WGL format to its corresponding GLX one.
1144 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1146 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1147 * iPixelFormat in case of probing the number of pixelformats.
1149 if (is_valid_pixel_format( iPixelFormat ) &&
1150 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1151 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1152 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1153 return &pixel_formats[iPixelFormat-1];
1155 return NULL;
1158 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1159 static void mark_drawable_dirty(Drawable old, Drawable new)
1161 struct wgl_context *ctx;
1163 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1165 if (old == ctx->drawables[0]) {
1166 ctx->drawables[0] = new;
1167 ctx->refresh_drawables = TRUE;
1169 if (old == ctx->drawables[1]) {
1170 ctx->drawables[1] = new;
1171 ctx->refresh_drawables = TRUE;
1176 /* Given the current context, make sure its drawable is sync'd */
1177 static inline void sync_context(struct wgl_context *context)
1179 if (context->refresh_drawables) {
1180 if (glxRequireVersion(3))
1181 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1182 context->drawables[1], context->ctx);
1183 else
1184 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1185 context->refresh_drawables = FALSE;
1189 static BOOL set_swap_interval(Drawable drawable, int interval)
1191 BOOL ret = TRUE;
1193 switch (swap_control_method)
1195 case GLX_SWAP_CONTROL_EXT:
1196 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1197 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1198 XSync(gdi_display, False);
1199 ret = !X11DRV_check_error();
1200 break;
1202 case GLX_SWAP_CONTROL_MESA:
1203 ret = !pglXSwapIntervalMESA(interval);
1204 break;
1206 case GLX_SWAP_CONTROL_SGI:
1207 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1208 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1209 * value to be an error. Just silently ignore the request for now.
1211 if (!interval)
1212 WARN("Request to disable vertical sync is not handled\n");
1213 else
1214 ret = !pglXSwapIntervalSGI(interval);
1215 break;
1217 case GLX_SWAP_CONTROL_NONE:
1218 /* Unlikely to happen on modern GLX implementations */
1219 WARN("Request to adjust swap interval is not handled\n");
1220 break;
1223 return ret;
1226 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1228 struct gl_drawable *gl;
1230 EnterCriticalSection( &context_section );
1231 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1232 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1233 LeaveCriticalSection( &context_section );
1234 return NULL;
1237 static void release_gl_drawable( struct gl_drawable *gl )
1239 if (gl) LeaveCriticalSection( &context_section );
1242 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1244 GLXContext ctx;
1246 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1247 BOOL indirect = !(context->fmt->dwFlags & PFD_DRAW_TO_BITMAP);
1249 if(context->gl3_context)
1251 if(context->numAttribs)
1252 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1253 else
1254 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1256 else if(context->vis)
1257 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1258 else /* Create a GLX Context for a pbuffer */
1259 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1261 return ctx;
1265 /***********************************************************************
1266 * free_gl_drawable
1268 static void free_gl_drawable( struct gl_drawable *gl )
1270 switch (gl->type)
1272 case DC_GL_CHILD_WIN:
1273 XDestroyWindow( gdi_display, gl->drawable );
1274 XFreeColormap( gdi_display, gl->colormap );
1275 break;
1276 case DC_GL_PIXMAP_WIN:
1277 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1278 XFreePixmap( gdi_display, gl->pixmap );
1279 break;
1280 default:
1281 break;
1283 if (gl->visual) XFree( gl->visual );
1284 HeapFree( GetProcessHeap(), 0, gl );
1288 /***********************************************************************
1289 * create_gl_drawable
1291 static BOOL create_gl_drawable( HWND hwnd, struct gl_drawable *gl )
1293 gl->drawable = 0;
1295 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1297 struct x11drv_win_data *data = get_win_data( hwnd );
1299 if (data)
1301 gl->type = DC_GL_WINDOW;
1302 gl->drawable = create_client_window( data, gl->visual );
1303 release_win_data( data );
1306 #ifdef SONAME_LIBXCOMPOSITE
1307 else if(usexcomposite)
1309 static Window dummy_parent;
1310 XSetWindowAttributes attrib;
1312 attrib.override_redirect = True;
1313 if (!dummy_parent)
1315 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1316 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1317 XMapWindow( gdi_display, dummy_parent );
1319 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1320 (gl->visual->class == PseudoColor ||
1321 gl->visual->class == GrayScale ||
1322 gl->visual->class == DirectColor) ?
1323 AllocAll : AllocNone);
1324 attrib.colormap = gl->colormap;
1325 XInstallColormap(gdi_display, attrib.colormap);
1327 gl->type = DC_GL_CHILD_WIN;
1328 gl->drawable = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1329 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1330 0, gl->visual->depth, InputOutput, gl->visual->visual,
1331 CWColormap | CWOverrideRedirect, &attrib );
1332 if (gl->drawable)
1334 pXCompositeRedirectWindow(gdi_display, gl->drawable, CompositeRedirectManual);
1335 XMapWindow(gdi_display, gl->drawable);
1337 else XFreeColormap( gdi_display, gl->colormap );
1339 #endif
1340 else
1342 WARN("XComposite is not available, using GLXPixmap hack\n");
1344 gl->type = DC_GL_PIXMAP_WIN;
1345 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1346 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1347 gl->visual->depth );
1348 if (gl->pixmap)
1350 gl->drawable = pglXCreateGLXPixmap( gdi_display, gl->visual, gl->pixmap );
1351 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1355 if (gl->drawable)
1356 gl->refresh_swap_interval = TRUE;
1357 return gl->drawable != 0;
1361 /***********************************************************************
1362 * set_win_format
1364 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1366 struct gl_drawable *gl, *prev;
1368 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1369 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1370 * there is no way to query it, so we have to store it here.
1372 gl->swap_interval = 1;
1373 gl->refresh_swap_interval = TRUE;
1374 gl->format = format;
1375 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1376 if (!gl->visual)
1378 HeapFree( GetProcessHeap(), 0, gl );
1379 return FALSE;
1382 GetClientRect( hwnd, &gl->rect );
1383 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1384 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1386 if (!create_gl_drawable( hwnd, gl ))
1388 XFree( gl->visual );
1389 HeapFree( GetProcessHeap(), 0, gl );
1390 return FALSE;
1393 TRACE("created GL drawable %lx for win %p format %x\n", gl->drawable, hwnd, format->fmt_id );
1395 XFlush( gdi_display );
1397 EnterCriticalSection( &context_section );
1398 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1400 gl->swap_interval = prev->swap_interval;
1401 free_gl_drawable( prev );
1403 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1404 LeaveCriticalSection( &context_section );
1406 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1407 return TRUE;
1411 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1413 const struct wgl_pixel_format *fmt;
1414 int value;
1415 HWND hwnd = WindowFromDC( hdc );
1417 TRACE("(%p,%d)\n", hdc, format);
1419 if (!hwnd || hwnd == GetDesktopWindow())
1421 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1422 return FALSE;
1425 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1426 if (!fmt)
1428 ERR( "Invalid format %d\n", format );
1429 return FALSE;
1432 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1433 if (!(value & GLX_WINDOW_BIT))
1435 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1436 return FALSE;
1439 if (!allow_change)
1441 struct gl_drawable *gl;
1442 if ((gl = get_gl_drawable( hwnd, hdc )))
1444 int prev = pixel_format_index( gl->format );
1445 release_gl_drawable( gl );
1446 return prev == format; /* cannot change it if already set */
1450 if (TRACE_ON(wgl)) {
1451 int gl_test = 0;
1453 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1454 if (gl_test) {
1455 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1456 } else {
1457 TRACE(" FBConfig have :\n");
1458 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1459 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1460 TRACE(" - VISUAL_ID 0x%x\n", value);
1461 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1462 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1466 return set_win_format( hwnd, fmt );
1470 /***********************************************************************
1471 * sync_gl_drawable
1473 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1475 struct gl_drawable *gl;
1476 Drawable glxp;
1477 Pixmap pix;
1478 int mask = 0;
1479 XWindowChanges changes;
1481 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1482 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1484 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1486 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1487 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1489 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1491 switch (gl->type)
1493 case DC_GL_CHILD_WIN:
1494 if (mask) XConfigureWindow( gdi_display, gl->drawable, mask, &changes );
1495 break;
1496 case DC_GL_PIXMAP_WIN:
1497 if (!mask) break;
1498 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1499 if (!pix) goto done;
1500 glxp = pglXCreateGLXPixmap(gdi_display, gl->visual, pix);
1501 if (!glxp)
1503 XFreePixmap(gdi_display, pix);
1504 goto done;
1506 mark_drawable_dirty(gl->drawable, glxp);
1507 XFlush( gdi_display );
1509 XFreePixmap(gdi_display, gl->pixmap);
1510 pglXDestroyGLXPixmap(gdi_display, gl->drawable);
1511 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1513 gl->pixmap = pix;
1514 gl->drawable = glxp;
1515 break;
1516 default:
1517 break;
1519 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1520 done:
1521 release_gl_drawable( gl );
1525 /***********************************************************************
1526 * set_gl_drawable_parent
1528 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1530 struct gl_drawable *gl;
1531 Drawable old_drawable;
1533 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1535 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1537 old_drawable = gl->drawable;
1538 switch (gl->type)
1540 case DC_GL_WINDOW:
1541 break;
1542 case DC_GL_CHILD_WIN:
1543 if (parent != GetDesktopWindow()) goto done;
1544 XDestroyWindow( gdi_display, gl->drawable );
1545 XFreeColormap( gdi_display, gl->colormap );
1546 break;
1547 case DC_GL_PIXMAP_WIN:
1548 if (parent != GetDesktopWindow()) goto done;
1549 pglXDestroyGLXPixmap( gdi_display, gl->drawable );
1550 XFreePixmap( gdi_display, gl->pixmap );
1551 break;
1552 default:
1553 goto done;
1556 if (!create_gl_drawable( hwnd, gl ))
1558 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1559 release_gl_drawable( gl );
1560 XFree( gl->visual );
1561 HeapFree( GetProcessHeap(), 0, gl );
1562 __wine_set_pixel_format( hwnd, 0 );
1563 return;
1565 mark_drawable_dirty( old_drawable, gl->drawable );
1567 done:
1568 release_gl_drawable( gl );
1573 /***********************************************************************
1574 * destroy_gl_drawable
1576 void destroy_gl_drawable( HWND hwnd )
1578 struct gl_drawable *gl;
1580 EnterCriticalSection( &context_section );
1581 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1583 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1584 free_gl_drawable( gl );
1586 LeaveCriticalSection( &context_section );
1591 * glxdrv_DescribePixelFormat
1593 * Get the pixel-format descriptor associated to the given id
1595 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1596 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1598 /*XVisualInfo *vis;*/
1599 int value;
1600 int rb,gb,bb,ab;
1601 const struct wgl_pixel_format *fmt;
1603 if (!has_opengl()) return 0;
1605 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1607 if (!ppfd) return nb_onscreen_formats;
1609 /* 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 */
1610 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1611 if (!fmt) {
1612 WARN("unexpected format %d\n", iPixelFormat);
1613 return 0;
1616 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1617 ERR("Wrong structure size !\n");
1618 /* Should set error */
1619 return 0;
1622 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1623 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1624 ppfd->nVersion = 1;
1626 /* These flags are always the same... */
1627 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1628 /* Now the flags extracted from the Visual */
1630 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1631 if(value & GLX_WINDOW_BIT)
1632 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1634 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1635 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1636 * 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
1637 * offered the GDI bit either. */
1638 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1640 /* PFD_GENERIC_FORMAT - gdi software rendering
1641 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1642 * none set - full hardware accelerated by a ICD
1644 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1645 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1647 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1648 if (value) {
1649 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1650 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1652 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1654 /* Pixel type */
1655 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1656 if (value & GLX_RGBA_BIT)
1657 ppfd->iPixelType = PFD_TYPE_RGBA;
1658 else
1659 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1661 /* Color bits */
1662 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1663 ppfd->cColorBits = value;
1665 /* Red, green, blue and alpha bits / shifts */
1666 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1667 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1668 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1669 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1670 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1672 ppfd->cRedBits = rb;
1673 ppfd->cRedShift = gb + bb + ab;
1674 ppfd->cBlueBits = bb;
1675 ppfd->cBlueShift = ab;
1676 ppfd->cGreenBits = gb;
1677 ppfd->cGreenShift = bb + ab;
1678 ppfd->cAlphaBits = ab;
1679 ppfd->cAlphaShift = 0;
1680 } else {
1681 ppfd->cRedBits = 0;
1682 ppfd->cRedShift = 0;
1683 ppfd->cBlueBits = 0;
1684 ppfd->cBlueShift = 0;
1685 ppfd->cGreenBits = 0;
1686 ppfd->cGreenShift = 0;
1687 ppfd->cAlphaBits = 0;
1688 ppfd->cAlphaShift = 0;
1691 /* Accum RGBA bits */
1692 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1693 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1694 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1695 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1697 ppfd->cAccumBits = rb+gb+bb+ab;
1698 ppfd->cAccumRedBits = rb;
1699 ppfd->cAccumGreenBits = gb;
1700 ppfd->cAccumBlueBits = bb;
1701 ppfd->cAccumAlphaBits = ab;
1703 /* Aux bits */
1704 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1705 ppfd->cAuxBuffers = value;
1707 /* Depth bits */
1708 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1709 ppfd->cDepthBits = value;
1711 /* stencil bits */
1712 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1713 ppfd->cStencilBits = value;
1715 ppfd->iLayerType = PFD_MAIN_PLANE;
1717 if (TRACE_ON(wgl)) {
1718 dump_PIXELFORMATDESCRIPTOR(ppfd);
1721 return nb_onscreen_formats;
1724 /***********************************************************************
1725 * glxdrv_wglGetPixelFormat
1727 static int glxdrv_wglGetPixelFormat( HDC hdc )
1729 struct gl_drawable *gl;
1730 int ret = 0;
1732 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1734 ret = pixel_format_index( gl->format );
1735 /* Offscreen formats can't be used with traditional WGL calls.
1736 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1737 if (!is_onscreen_pixel_format( ret )) ret = 1;
1738 release_gl_drawable( gl );
1740 TRACE( "%p -> %d\n", hdc, ret );
1741 return ret;
1744 /***********************************************************************
1745 * glxdrv_wglSetPixelFormat
1747 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1749 return set_pixel_format(hdc, iPixelFormat, FALSE);
1752 /***********************************************************************
1753 * glxdrv_wglCopyContext
1755 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1757 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1759 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1761 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1762 return TRUE;
1765 /***********************************************************************
1766 * glxdrv_wglCreateContext
1768 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1770 struct wgl_context *ret = NULL;
1771 struct gl_drawable *gl;
1773 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1775 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1776 return NULL;
1779 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1781 ret->hdc = hdc;
1782 ret->fmt = gl->format;
1783 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1784 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1785 list_add_head( &context_list, &ret->entry );
1787 release_gl_drawable( gl );
1788 TRACE( "%p -> %p\n", hdc, ret );
1789 return ret;
1792 /***********************************************************************
1793 * glxdrv_wglDeleteContext
1795 static void glxdrv_wglDeleteContext(struct wgl_context *ctx)
1797 TRACE("(%p)\n", ctx);
1799 EnterCriticalSection( &context_section );
1800 list_remove( &ctx->entry );
1801 LeaveCriticalSection( &context_section );
1803 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1804 if (ctx->vis) XFree( ctx->vis );
1805 HeapFree( GetProcessHeap(), 0, ctx );
1808 /***********************************************************************
1809 * glxdrv_wglGetProcAddress
1811 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1813 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1814 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1817 /***********************************************************************
1818 * glxdrv_wglMakeCurrent
1820 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1822 BOOL ret = FALSE;
1823 struct gl_drawable *gl;
1825 TRACE("(%p,%p)\n", hdc, ctx);
1827 if (!ctx)
1829 pglXMakeCurrent(gdi_display, None, NULL);
1830 NtCurrentTeb()->glContext = NULL;
1831 return TRUE;
1834 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1836 if (ctx->fmt != gl->format)
1838 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1839 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1840 goto done;
1843 if (TRACE_ON(wgl)) {
1844 describeContext(ctx);
1845 TRACE("hdc %p drawable %lx fmt %p ctx %p\n", hdc, gl->drawable, gl->format, ctx->ctx );
1848 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1849 if (ret)
1851 NtCurrentTeb()->glContext = ctx;
1852 ctx->has_been_current = TRUE;
1853 ctx->hdc = hdc;
1854 ctx->drawables[0] = gl->drawable;
1855 ctx->drawables[1] = gl->drawable;
1856 ctx->refresh_drawables = FALSE;
1857 goto done;
1860 SetLastError( ERROR_INVALID_HANDLE );
1862 done:
1863 release_gl_drawable( gl );
1864 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1865 return ret;
1868 /***********************************************************************
1869 * X11DRV_wglMakeContextCurrentARB
1871 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1873 BOOL ret = FALSE;
1874 struct gl_drawable *draw_gl, *read_gl = NULL;
1876 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1878 if (!ctx)
1880 pglXMakeCurrent(gdi_display, None, NULL);
1881 NtCurrentTeb()->glContext = NULL;
1882 return TRUE;
1885 if (!pglXMakeContextCurrent) return FALSE;
1887 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1889 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1890 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1891 read_gl ? read_gl->drawable : 0, ctx->ctx);
1892 if (ret)
1894 ctx->has_been_current = TRUE;
1895 ctx->hdc = draw_hdc;
1896 ctx->drawables[0] = draw_gl->drawable;
1897 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1898 ctx->refresh_drawables = FALSE;
1899 NtCurrentTeb()->glContext = ctx;
1900 goto done;
1903 SetLastError( ERROR_INVALID_HANDLE );
1904 done:
1905 release_gl_drawable( read_gl );
1906 release_gl_drawable( draw_gl );
1907 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1908 return ret;
1911 /***********************************************************************
1912 * glxdrv_wglShareLists
1914 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1916 TRACE("(%p, %p)\n", org, dest);
1918 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1919 * at context creation time but in case of WGL it is done using wglShareLists.
1920 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1921 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1922 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1923 * so there delaying context creation doesn't work.
1925 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1926 * and when a program requests sharing we recreate the destination context if it hasn't been made
1927 * current or when it hasn't shared display lists before.
1930 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1932 ERR("Could not share display lists, one of the contexts has been current already !\n");
1933 return FALSE;
1935 else if(dest->sharing)
1937 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1938 return FALSE;
1940 else
1942 describeContext(org);
1943 describeContext(dest);
1945 /* Re-create the GLX context and share display lists */
1946 pglXDestroyContext(gdi_display, dest->ctx);
1947 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1948 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1950 org->sharing = TRUE;
1951 dest->sharing = TRUE;
1952 return TRUE;
1954 return FALSE;
1957 static void wglFinish(void)
1959 struct x11drv_escape_flush_gl_drawable escape;
1960 struct gl_drawable *gl;
1961 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1963 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1964 escape.gl_drawable = 0;
1966 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1968 switch (gl->type)
1970 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1971 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1972 default: break;
1974 sync_context(ctx);
1975 release_gl_drawable( gl );
1978 pglFinish();
1979 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1982 static void wglFlush(void)
1984 struct x11drv_escape_flush_gl_drawable escape;
1985 struct gl_drawable *gl;
1986 struct wgl_context *ctx = NtCurrentTeb()->glContext;
1988 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
1989 escape.gl_drawable = 0;
1991 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
1993 switch (gl->type)
1995 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
1996 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->drawable; break;
1997 default: break;
1999 sync_context(ctx);
2000 release_gl_drawable( gl );
2003 pglFlush();
2004 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2007 static const GLubyte *wglGetString(GLenum name)
2009 if (name == GL_EXTENSIONS && WineGLInfo.glExtensions)
2010 return (const GLubyte *)WineGLInfo.glExtensions;
2011 return pglGetString(name);
2014 /***********************************************************************
2015 * X11DRV_wglCreateContextAttribsARB
2017 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2018 const int* attribList )
2020 struct wgl_context *ret = NULL;
2021 struct gl_drawable *gl;
2022 int err = 0;
2024 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2026 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2028 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2029 return NULL;
2032 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2034 ret->hdc = hdc;
2035 ret->fmt = gl->format;
2036 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2037 ret->gl3_context = TRUE;
2038 if (attribList)
2040 int *pContextAttribList = &ret->attribList[0];
2041 /* attribList consists of pairs {token, value] terminated with 0 */
2042 while(attribList[0] != 0)
2044 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2045 switch(attribList[0])
2047 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2048 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2049 pContextAttribList[1] = attribList[1];
2050 pContextAttribList += 2;
2051 ret->numAttribs++;
2052 break;
2053 case WGL_CONTEXT_MINOR_VERSION_ARB:
2054 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2055 pContextAttribList[1] = attribList[1];
2056 pContextAttribList += 2;
2057 ret->numAttribs++;
2058 break;
2059 case WGL_CONTEXT_LAYER_PLANE_ARB:
2060 break;
2061 case WGL_CONTEXT_FLAGS_ARB:
2062 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2063 pContextAttribList[1] = attribList[1];
2064 pContextAttribList += 2;
2065 ret->numAttribs++;
2066 break;
2067 case WGL_CONTEXT_PROFILE_MASK_ARB:
2068 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2069 pContextAttribList[1] = attribList[1];
2070 pContextAttribList += 2;
2071 ret->numAttribs++;
2072 break;
2073 default:
2074 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2076 attribList += 2;
2080 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2081 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2082 XSync(gdi_display, False);
2083 if ((err = X11DRV_check_error()) || !ret->ctx)
2085 /* In the future we should convert the GLX error to a win32 one here if needed */
2086 ERR("Context creation failed (error %x)\n", err);
2087 HeapFree( GetProcessHeap(), 0, ret );
2088 ret = NULL;
2090 else list_add_head( &context_list, &ret->entry );
2093 release_gl_drawable( gl );
2094 TRACE( "%p -> %p\n", hdc, ret );
2095 return ret;
2099 * X11DRV_wglGetExtensionsStringARB
2101 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2103 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2105 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2106 return WineGLInfo.wglExtensions;
2110 * X11DRV_wglCreatePbufferARB
2112 * WGL_ARB_pbuffer: wglCreatePbufferARB
2114 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2115 const int *piAttribList )
2117 struct wgl_pbuffer* object = NULL;
2118 const struct wgl_pixel_format *fmt = NULL;
2119 int attribs[256];
2120 int nAttribs = 0;
2122 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2124 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2125 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2126 if(!fmt) {
2127 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2128 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2129 return NULL;
2132 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2133 if (NULL == object) {
2134 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2135 return NULL;
2137 object->width = iWidth;
2138 object->height = iHeight;
2139 object->fmt = fmt;
2141 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2142 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2143 while (piAttribList && 0 != *piAttribList) {
2144 int attr_v;
2145 switch (*piAttribList) {
2146 case WGL_PBUFFER_LARGEST_ARB: {
2147 ++piAttribList;
2148 attr_v = *piAttribList;
2149 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2150 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2151 break;
2154 case WGL_TEXTURE_FORMAT_ARB: {
2155 ++piAttribList;
2156 attr_v = *piAttribList;
2157 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2158 if (WGL_NO_TEXTURE_ARB == attr_v) {
2159 object->use_render_texture = 0;
2160 } else {
2161 if (!use_render_texture_emulation) {
2162 SetLastError(ERROR_INVALID_DATA);
2163 goto create_failed;
2165 switch (attr_v) {
2166 case WGL_TEXTURE_RGB_ARB:
2167 object->use_render_texture = GL_RGB;
2168 object->texture_bpp = 3;
2169 object->texture_format = GL_RGB;
2170 object->texture_type = GL_UNSIGNED_BYTE;
2171 break;
2172 case WGL_TEXTURE_RGBA_ARB:
2173 object->use_render_texture = GL_RGBA;
2174 object->texture_bpp = 4;
2175 object->texture_format = GL_RGBA;
2176 object->texture_type = GL_UNSIGNED_BYTE;
2177 break;
2179 /* WGL_FLOAT_COMPONENTS_NV */
2180 case WGL_TEXTURE_FLOAT_R_NV:
2181 object->use_render_texture = GL_FLOAT_R_NV;
2182 object->texture_bpp = 4;
2183 object->texture_format = GL_RED;
2184 object->texture_type = GL_FLOAT;
2185 break;
2186 case WGL_TEXTURE_FLOAT_RG_NV:
2187 object->use_render_texture = GL_FLOAT_RG_NV;
2188 object->texture_bpp = 8;
2189 object->texture_format = GL_LUMINANCE_ALPHA;
2190 object->texture_type = GL_FLOAT;
2191 break;
2192 case WGL_TEXTURE_FLOAT_RGB_NV:
2193 object->use_render_texture = GL_FLOAT_RGB_NV;
2194 object->texture_bpp = 12;
2195 object->texture_format = GL_RGB;
2196 object->texture_type = GL_FLOAT;
2197 break;
2198 case WGL_TEXTURE_FLOAT_RGBA_NV:
2199 object->use_render_texture = GL_FLOAT_RGBA_NV;
2200 object->texture_bpp = 16;
2201 object->texture_format = GL_RGBA;
2202 object->texture_type = GL_FLOAT;
2203 break;
2204 default:
2205 ERR("Unknown texture format: %x\n", attr_v);
2206 SetLastError(ERROR_INVALID_DATA);
2207 goto create_failed;
2210 break;
2213 case WGL_TEXTURE_TARGET_ARB: {
2214 ++piAttribList;
2215 attr_v = *piAttribList;
2216 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2217 if (WGL_NO_TEXTURE_ARB == attr_v) {
2218 object->texture_target = 0;
2219 } else {
2220 if (!use_render_texture_emulation) {
2221 SetLastError(ERROR_INVALID_DATA);
2222 goto create_failed;
2224 switch (attr_v) {
2225 case WGL_TEXTURE_CUBE_MAP_ARB: {
2226 if (iWidth != iHeight) {
2227 SetLastError(ERROR_INVALID_DATA);
2228 goto create_failed;
2230 object->texture_target = GL_TEXTURE_CUBE_MAP;
2231 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2232 break;
2234 case WGL_TEXTURE_1D_ARB: {
2235 if (1 != iHeight) {
2236 SetLastError(ERROR_INVALID_DATA);
2237 goto create_failed;
2239 object->texture_target = GL_TEXTURE_1D;
2240 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2241 break;
2243 case WGL_TEXTURE_2D_ARB: {
2244 object->texture_target = GL_TEXTURE_2D;
2245 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2246 break;
2248 case WGL_TEXTURE_RECTANGLE_NV: {
2249 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2250 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2251 break;
2253 default:
2254 ERR("Unknown texture target: %x\n", attr_v);
2255 SetLastError(ERROR_INVALID_DATA);
2256 goto create_failed;
2259 break;
2262 case WGL_MIPMAP_TEXTURE_ARB: {
2263 ++piAttribList;
2264 attr_v = *piAttribList;
2265 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2266 if (!use_render_texture_emulation) {
2267 SetLastError(ERROR_INVALID_DATA);
2268 goto create_failed;
2270 break;
2273 ++piAttribList;
2276 PUSH1(attribs, None);
2277 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2278 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2279 if (!object->drawable) {
2280 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2281 goto create_failed; /* unexpected error */
2283 TRACE("->(%p)\n", object);
2284 return object;
2286 create_failed:
2287 HeapFree(GetProcessHeap(), 0, object);
2288 TRACE("->(FAILED)\n");
2289 return NULL;
2293 * X11DRV_wglDestroyPbufferARB
2295 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2297 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2299 TRACE("(%p)\n", object);
2301 pglXDestroyPbuffer(gdi_display, object->drawable);
2302 HeapFree(GetProcessHeap(), 0, object);
2303 return GL_TRUE;
2307 * X11DRV_wglGetPbufferDCARB
2309 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2311 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2313 struct x11drv_escape_set_drawable escape;
2314 struct gl_drawable *gl, *prev;
2315 HDC hdc;
2317 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2318 if (!hdc) return 0;
2320 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2322 DeleteDC( hdc );
2323 return 0;
2325 gl->type = DC_GL_PBUFFER;
2326 gl->drawable = object->drawable;
2327 gl->format = object->fmt;
2329 EnterCriticalSection( &context_section );
2330 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2331 free_gl_drawable( prev );
2332 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2333 LeaveCriticalSection( &context_section );
2335 escape.code = X11DRV_SET_DRAWABLE;
2336 escape.drawable = object->drawable;
2337 escape.mode = IncludeInferiors;
2338 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2339 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2341 TRACE( "(%p)->(%p)\n", object, hdc );
2342 return hdc;
2346 * X11DRV_wglQueryPbufferARB
2348 * WGL_ARB_pbuffer: wglQueryPbufferARB
2350 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2352 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2354 switch (iAttribute) {
2355 case WGL_PBUFFER_WIDTH_ARB:
2356 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2357 break;
2358 case WGL_PBUFFER_HEIGHT_ARB:
2359 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2360 break;
2362 case WGL_PBUFFER_LOST_ARB:
2363 /* GLX Pbuffers cannot be lost by default. We can support this by
2364 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2365 * to receive pixel buffer clobber events, however that may or may
2366 * not give any benefit */
2367 *piValue = GL_FALSE;
2368 break;
2370 case WGL_TEXTURE_FORMAT_ARB:
2371 if (!object->use_render_texture) {
2372 *piValue = WGL_NO_TEXTURE_ARB;
2373 } else {
2374 if (!use_render_texture_emulation) {
2375 SetLastError(ERROR_INVALID_HANDLE);
2376 return GL_FALSE;
2378 switch(object->use_render_texture) {
2379 case GL_RGB:
2380 *piValue = WGL_TEXTURE_RGB_ARB;
2381 break;
2382 case GL_RGBA:
2383 *piValue = WGL_TEXTURE_RGBA_ARB;
2384 break;
2385 /* WGL_FLOAT_COMPONENTS_NV */
2386 case GL_FLOAT_R_NV:
2387 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2388 break;
2389 case GL_FLOAT_RG_NV:
2390 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2391 break;
2392 case GL_FLOAT_RGB_NV:
2393 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2394 break;
2395 case GL_FLOAT_RGBA_NV:
2396 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2397 break;
2398 default:
2399 ERR("Unknown texture format: %x\n", object->use_render_texture);
2402 break;
2404 case WGL_TEXTURE_TARGET_ARB:
2405 if (!object->texture_target){
2406 *piValue = WGL_NO_TEXTURE_ARB;
2407 } else {
2408 if (!use_render_texture_emulation) {
2409 SetLastError(ERROR_INVALID_DATA);
2410 return GL_FALSE;
2412 switch (object->texture_target) {
2413 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2414 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2415 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2416 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2419 break;
2421 case WGL_MIPMAP_TEXTURE_ARB:
2422 *piValue = GL_FALSE; /** don't support that */
2423 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2424 break;
2426 default:
2427 FIXME("unexpected attribute %x\n", iAttribute);
2428 break;
2431 return GL_TRUE;
2435 * X11DRV_wglReleasePbufferDCARB
2437 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2439 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2441 struct gl_drawable *gl;
2443 TRACE("(%p, %p)\n", object, hdc);
2445 EnterCriticalSection( &context_section );
2447 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2449 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2450 free_gl_drawable( gl );
2452 else hdc = 0;
2454 LeaveCriticalSection( &context_section );
2456 return hdc && DeleteDC(hdc);
2460 * X11DRV_wglSetPbufferAttribARB
2462 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2464 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2466 GLboolean ret = GL_FALSE;
2468 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2470 if (!object->use_render_texture) {
2471 SetLastError(ERROR_INVALID_HANDLE);
2472 return GL_FALSE;
2474 if (use_render_texture_emulation) {
2475 return GL_TRUE;
2477 return ret;
2481 * X11DRV_wglChoosePixelFormatARB
2483 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2485 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2486 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2488 int attribs[256];
2489 int nAttribs = 0;
2490 GLXFBConfig* cfgs = NULL;
2491 int nCfgs = 0;
2492 int it;
2493 int fmt_id;
2494 int start, end;
2495 UINT pfmt_it = 0;
2496 int run;
2497 int i;
2498 DWORD dwFlags = 0;
2500 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2501 if (NULL != pfAttribFList) {
2502 FIXME("unused pfAttribFList\n");
2505 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2506 if (-1 == nAttribs) {
2507 WARN("Cannot convert WGL to GLX attributes\n");
2508 return GL_FALSE;
2510 PUSH1(attribs, None);
2512 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2513 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2514 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2516 for(i=0; piAttribIList[i] != 0; i+=2)
2518 switch(piAttribIList[i])
2520 case WGL_DRAW_TO_BITMAP_ARB:
2521 if(piAttribIList[i+1])
2522 dwFlags |= PFD_DRAW_TO_BITMAP;
2523 break;
2524 case WGL_ACCELERATION_ARB:
2525 switch(piAttribIList[i+1])
2527 case WGL_NO_ACCELERATION_ARB:
2528 dwFlags |= PFD_GENERIC_FORMAT;
2529 break;
2530 case WGL_GENERIC_ACCELERATION_ARB:
2531 dwFlags |= PFD_GENERIC_ACCELERATED;
2532 break;
2533 case WGL_FULL_ACCELERATION_ARB:
2534 /* Nothing to do */
2535 break;
2537 break;
2538 case WGL_SUPPORT_GDI_ARB:
2539 if(piAttribIList[i+1])
2540 dwFlags |= PFD_SUPPORT_GDI;
2541 break;
2545 /* Search for FB configurations matching the requirements in attribs */
2546 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2547 if (NULL == cfgs) {
2548 WARN("Compatible Pixel Format not found\n");
2549 return GL_FALSE;
2552 /* Loop through all matching formats and check if they are suitable.
2553 * Note that this function should at max return nMaxFormats different formats */
2554 for(run=0; run < 2; run++)
2556 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2558 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2560 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2561 continue;
2564 /* During the first run we only want onscreen formats and during the second only offscreen */
2565 start = run == 1 ? nb_onscreen_formats : 0;
2566 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2568 for (i = start; i < end; i++)
2570 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2572 piFormats[pfmt_it++] = i + 1;
2573 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2574 it + 1, nCfgs, fmt_id, i + 1);
2575 break;
2581 *nNumFormats = pfmt_it;
2582 /** free list */
2583 XFree(cfgs);
2584 return GL_TRUE;
2588 * X11DRV_wglGetPixelFormatAttribivARB
2590 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2592 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2593 UINT nAttributes, const int *piAttributes, int *piValues )
2595 UINT i;
2596 const struct wgl_pixel_format *fmt = NULL;
2597 int hTest;
2598 int tmp;
2599 int curGLXAttr = 0;
2601 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2603 if (0 < iLayerPlane) {
2604 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2605 return GL_FALSE;
2608 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2609 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2610 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2611 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2612 if(!fmt) {
2613 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2616 for (i = 0; i < nAttributes; ++i) {
2617 const int curWGLAttr = piAttributes[i];
2618 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2620 switch (curWGLAttr) {
2621 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2622 piValues[i] = nb_pixel_formats;
2623 continue;
2625 case WGL_SUPPORT_OPENGL_ARB:
2626 piValues[i] = GL_TRUE;
2627 continue;
2629 case WGL_ACCELERATION_ARB:
2630 curGLXAttr = GLX_CONFIG_CAVEAT;
2631 if (!fmt) goto pix_error;
2632 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2633 piValues[i] = WGL_NO_ACCELERATION_ARB;
2634 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2635 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2636 else
2637 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2638 continue;
2640 case WGL_TRANSPARENT_ARB:
2641 curGLXAttr = GLX_TRANSPARENT_TYPE;
2642 if (!fmt) goto pix_error;
2643 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2644 if (hTest) goto get_error;
2645 piValues[i] = GL_FALSE;
2646 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2647 continue;
2649 case WGL_PIXEL_TYPE_ARB:
2650 curGLXAttr = GLX_RENDER_TYPE;
2651 if (!fmt) goto pix_error;
2652 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2653 if (hTest) goto get_error;
2654 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2655 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2656 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2657 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2658 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2659 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2660 else {
2661 ERR("unexpected RenderType(%x)\n", tmp);
2662 piValues[i] = WGL_TYPE_RGBA_ARB;
2664 continue;
2666 case WGL_COLOR_BITS_ARB:
2667 curGLXAttr = GLX_BUFFER_SIZE;
2668 break;
2670 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2671 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2672 if (!use_render_texture_emulation) {
2673 piValues[i] = GL_FALSE;
2674 continue;
2676 curGLXAttr = GLX_RENDER_TYPE;
2677 if (!fmt) goto pix_error;
2678 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2679 if (hTest) goto get_error;
2680 if (GLX_COLOR_INDEX_BIT == tmp) {
2681 piValues[i] = GL_FALSE;
2682 continue;
2684 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2685 if (hTest) goto get_error;
2686 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2687 continue;
2689 case WGL_BLUE_BITS_ARB:
2690 curGLXAttr = GLX_BLUE_SIZE;
2691 break;
2692 case WGL_RED_BITS_ARB:
2693 curGLXAttr = GLX_RED_SIZE;
2694 break;
2695 case WGL_GREEN_BITS_ARB:
2696 curGLXAttr = GLX_GREEN_SIZE;
2697 break;
2698 case WGL_ALPHA_BITS_ARB:
2699 curGLXAttr = GLX_ALPHA_SIZE;
2700 break;
2701 case WGL_DEPTH_BITS_ARB:
2702 curGLXAttr = GLX_DEPTH_SIZE;
2703 break;
2704 case WGL_STENCIL_BITS_ARB:
2705 curGLXAttr = GLX_STENCIL_SIZE;
2706 break;
2707 case WGL_DOUBLE_BUFFER_ARB:
2708 curGLXAttr = GLX_DOUBLEBUFFER;
2709 break;
2710 case WGL_STEREO_ARB:
2711 curGLXAttr = GLX_STEREO;
2712 break;
2713 case WGL_AUX_BUFFERS_ARB:
2714 curGLXAttr = GLX_AUX_BUFFERS;
2715 break;
2717 case WGL_SUPPORT_GDI_ARB:
2718 if (!fmt) goto pix_error;
2719 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2720 continue;
2722 case WGL_DRAW_TO_BITMAP_ARB:
2723 if (!fmt) goto pix_error;
2724 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2725 continue;
2727 case WGL_DRAW_TO_WINDOW_ARB:
2728 case WGL_DRAW_TO_PBUFFER_ARB:
2729 if (!fmt) goto pix_error;
2730 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2731 if (hTest) goto get_error;
2732 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2733 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2734 piValues[i] = GL_TRUE;
2735 else
2736 piValues[i] = GL_FALSE;
2737 continue;
2739 case WGL_SWAP_METHOD_ARB:
2740 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2741 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2742 * point only ATI offers this.
2744 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2745 break;
2747 case WGL_PBUFFER_LARGEST_ARB:
2748 curGLXAttr = GLX_LARGEST_PBUFFER;
2749 break;
2751 case WGL_SAMPLE_BUFFERS_ARB:
2752 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2753 break;
2755 case WGL_SAMPLES_ARB:
2756 curGLXAttr = GLX_SAMPLES_ARB;
2757 break;
2759 case WGL_FLOAT_COMPONENTS_NV:
2760 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2761 break;
2763 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2764 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2765 break;
2767 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2768 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2769 break;
2771 case WGL_ACCUM_RED_BITS_ARB:
2772 curGLXAttr = GLX_ACCUM_RED_SIZE;
2773 break;
2774 case WGL_ACCUM_GREEN_BITS_ARB:
2775 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2776 break;
2777 case WGL_ACCUM_BLUE_BITS_ARB:
2778 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2779 break;
2780 case WGL_ACCUM_ALPHA_BITS_ARB:
2781 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2782 break;
2783 case WGL_ACCUM_BITS_ARB:
2784 if (!fmt) goto pix_error;
2785 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2786 if (hTest) goto get_error;
2787 piValues[i] = tmp;
2788 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2789 if (hTest) goto get_error;
2790 piValues[i] += tmp;
2791 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2792 if (hTest) goto get_error;
2793 piValues[i] += tmp;
2794 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2795 if (hTest) goto get_error;
2796 piValues[i] += tmp;
2797 continue;
2799 default:
2800 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2803 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2804 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2805 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2807 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2808 * and check which options can work using iPixelFormat=0 and which not.
2809 * A problem would be that this function is an extension. This would
2810 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2812 if (0 != curGLXAttr && iPixelFormat != 0) {
2813 if (!fmt) goto pix_error;
2814 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2815 if (hTest) goto get_error;
2816 curGLXAttr = 0;
2817 } else {
2818 piValues[i] = GL_FALSE;
2821 return GL_TRUE;
2823 get_error:
2824 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2825 return GL_FALSE;
2827 pix_error:
2828 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2829 return GL_FALSE;
2833 * X11DRV_wglGetPixelFormatAttribfvARB
2835 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2837 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2838 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2840 int *attr;
2841 int ret;
2842 UINT i;
2844 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2846 /* Allocate a temporary array to store integer values */
2847 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2848 if (!attr) {
2849 ERR("couldn't allocate %d array\n", nAttributes);
2850 return GL_FALSE;
2853 /* Piggy-back on wglGetPixelFormatAttribivARB */
2854 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2855 if (ret) {
2856 /* Convert integer values to float. Should also check for attributes
2857 that can give decimal values here */
2858 for (i=0; i<nAttributes;i++) {
2859 pfValues[i] = attr[i];
2863 HeapFree(GetProcessHeap(), 0, attr);
2864 return ret;
2868 * X11DRV_wglBindTexImageARB
2870 * WGL_ARB_render_texture: wglBindTexImageARB
2872 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2874 GLboolean ret = GL_FALSE;
2876 TRACE("(%p, %d)\n", object, iBuffer);
2878 if (!object->use_render_texture) {
2879 SetLastError(ERROR_INVALID_HANDLE);
2880 return GL_FALSE;
2883 if (use_render_texture_emulation) {
2884 static BOOL initialized = FALSE;
2885 int prev_binded_texture = 0;
2886 GLXContext prev_context;
2887 Drawable prev_drawable;
2888 GLXContext tmp_context;
2890 prev_context = pglXGetCurrentContext();
2891 prev_drawable = pglXGetCurrentDrawable();
2893 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2894 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2895 isn't ideal performance wise but I wasn't able to get other ways working.
2897 if(!initialized) {
2898 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2899 FIXME("partial stub!\n");
2902 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2903 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2905 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2907 /* Switch to our pbuffer */
2908 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2910 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2911 * After that upload the pbuffer texture data. */
2912 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
2913 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2915 /* Switch back to the original drawable and upload the pbuffer-texture */
2916 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
2917 pglXDestroyContext(gdi_display, tmp_context);
2918 return GL_TRUE;
2921 return ret;
2925 * X11DRV_wglReleaseTexImageARB
2927 * WGL_ARB_render_texture: wglReleaseTexImageARB
2929 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2931 GLboolean ret = GL_FALSE;
2933 TRACE("(%p, %d)\n", object, iBuffer);
2935 if (!object->use_render_texture) {
2936 SetLastError(ERROR_INVALID_HANDLE);
2937 return GL_FALSE;
2939 if (use_render_texture_emulation) {
2940 return GL_TRUE;
2942 return ret;
2946 * X11DRV_wglGetExtensionsStringEXT
2948 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2950 static const char *X11DRV_wglGetExtensionsStringEXT(void)
2952 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2953 return WineGLInfo.wglExtensions;
2957 * X11DRV_wglGetSwapIntervalEXT
2959 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2961 static int X11DRV_wglGetSwapIntervalEXT(void)
2963 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2964 struct gl_drawable *gl;
2965 int swap_interval;
2967 TRACE("()\n");
2969 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
2971 /* This can't happen because a current WGL context is required to get
2972 * here. Likely the application is buggy.
2974 WARN("No GL drawable found, returning swap interval 0\n");
2975 return 0;
2978 swap_interval = gl->swap_interval;
2979 release_gl_drawable(gl);
2981 return swap_interval;
2985 * X11DRV_wglSwapIntervalEXT
2987 * WGL_EXT_swap_control: wglSwapIntervalEXT
2989 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
2991 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2992 struct gl_drawable *gl;
2993 BOOL ret = TRUE;
2995 TRACE("(%d)\n", interval);
2997 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
2998 * is invalid.
3000 if (interval < 0 && !has_swap_control_tear)
3002 SetLastError(ERROR_INVALID_DATA);
3003 return FALSE;
3006 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3008 SetLastError(ERROR_DC_NOT_FOUND);
3009 return FALSE;
3012 ret = set_swap_interval(gl->drawable, interval);
3013 gl->refresh_swap_interval = FALSE;
3014 if (ret)
3015 gl->swap_interval = interval;
3016 else
3017 SetLastError(ERROR_DC_NOT_FOUND);
3019 release_gl_drawable(gl);
3021 return ret;
3025 * X11DRV_wglSetPixelFormatWINE
3027 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3028 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3030 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3032 return set_pixel_format(hdc, format, TRUE);
3036 * glxRequireVersion (internal)
3038 * Check if the supported GLX version matches requiredVersion.
3040 static BOOL glxRequireVersion(int requiredVersion)
3042 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3043 if(requiredVersion <= WineGLInfo.glxVersion[1])
3044 return TRUE;
3046 return FALSE;
3049 static void register_extension(const char *ext)
3051 if (WineGLInfo.wglExtensions[0])
3052 strcat(WineGLInfo.wglExtensions, " ");
3053 strcat(WineGLInfo.wglExtensions, ext);
3055 TRACE("'%s'\n", ext);
3059 * X11DRV_WineGL_LoadExtensions
3061 static void X11DRV_WineGL_LoadExtensions(void)
3063 WineGLInfo.wglExtensions[0] = 0;
3065 /* ARB Extensions */
3067 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
3069 register_extension( "WGL_ARB_create_context" );
3070 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3072 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
3073 register_extension("WGL_ARB_create_context_profile");
3076 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
3078 register_extension("WGL_ARB_pixel_format_float");
3079 register_extension("WGL_ATI_pixel_format_float");
3082 register_extension( "WGL_ARB_extensions_string" );
3083 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3085 if (glxRequireVersion(3))
3087 register_extension( "WGL_ARB_make_current_read" );
3088 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3089 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3092 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3094 if (glxRequireVersion(3))
3096 register_extension( "WGL_ARB_pbuffer" );
3097 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3098 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3099 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3100 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3101 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3102 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3105 register_extension( "WGL_ARB_pixel_format" );
3106 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3107 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3108 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3110 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3111 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3112 (glxRequireVersion(3) && use_render_texture_emulation))
3114 register_extension( "WGL_ARB_render_texture" );
3115 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3116 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3118 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3119 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3120 register_extension("WGL_NV_float_buffer");
3122 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3123 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3124 register_extension("WGL_NV_render_texture_rectangle");
3127 /* EXT Extensions */
3129 register_extension( "WGL_EXT_extensions_string" );
3130 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3132 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3133 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3134 register_extension( "WGL_EXT_swap_control" );
3135 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3136 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3138 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3139 register_extension("WGL_EXT_framebuffer_sRGB");
3141 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3142 register_extension("WGL_EXT_pixel_format_packed_float");
3144 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3146 swap_control_method = GLX_SWAP_CONTROL_EXT;
3147 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control_tear"))
3149 register_extension("WGL_EXT_swap_control_tear");
3150 has_swap_control_tear = TRUE;
3153 else if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_swap_control"))
3155 swap_control_method = GLX_SWAP_CONTROL_MESA;
3157 else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGI_swap_control"))
3159 swap_control_method = GLX_SWAP_CONTROL_SGI;
3162 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3163 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3165 register_extension( "WGL_NV_vertex_array_range" );
3166 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3167 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3170 /* WINE-specific WGL Extensions */
3172 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3173 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3175 register_extension( "WGL_WINE_pixel_format_passthrough" );
3176 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3181 * glxdrv_SwapBuffers
3183 * Swap the buffers of this DC
3185 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3187 struct x11drv_escape_flush_gl_drawable escape;
3188 struct gl_drawable *gl;
3189 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3191 TRACE("(%p)\n", hdc);
3193 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3194 escape.gl_drawable = 0;
3196 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3198 SetLastError( ERROR_INVALID_HANDLE );
3199 return FALSE;
3202 if (gl->refresh_swap_interval)
3204 set_swap_interval(gl->drawable, gl->swap_interval);
3205 gl->refresh_swap_interval = FALSE;
3208 switch (gl->type)
3210 case DC_GL_PIXMAP_WIN:
3211 if (ctx) sync_context( ctx );
3212 escape.gl_drawable = gl->pixmap;
3213 if (pglXCopySubBufferMESA) {
3214 /* (glX)SwapBuffers has an implicit glFlush effect, however
3215 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3216 * copying */
3217 pglFlush();
3218 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3219 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3220 break;
3222 pglXSwapBuffers(gdi_display, gl->drawable);
3223 break;
3224 case DC_GL_CHILD_WIN:
3225 if (ctx) sync_context( ctx );
3226 escape.gl_drawable = gl->drawable;
3227 /* fall through */
3228 default:
3229 pglXSwapBuffers(gdi_display, gl->drawable);
3230 break;
3233 release_gl_drawable( gl );
3235 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3236 return TRUE;
3239 static struct opengl_funcs opengl_funcs =
3242 glxdrv_wglCopyContext, /* p_wglCopyContext */
3243 glxdrv_wglCreateContext, /* p_wglCreateContext */
3244 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3245 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3246 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3247 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3248 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3249 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3250 glxdrv_wglShareLists, /* p_wglShareLists */
3251 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3255 struct opengl_funcs *get_glx_driver( UINT version )
3257 if (version != WINE_WGL_DRIVER_VERSION)
3259 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3260 return NULL;
3262 if (has_opengl()) return &opengl_funcs;
3263 return NULL;
3266 #else /* no OpenGL includes */
3268 struct opengl_funcs *get_glx_driver( UINT version )
3270 return NULL;
3273 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3277 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3281 void destroy_gl_drawable( HWND hwnd )
3285 #endif /* defined(SONAME_LIBGL) */