ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / winex11.drv / opengl.c
blob5a7cfcc7a9c2ee4a7fe33de10846fc0c4f15ddec
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 #ifdef SONAME_LIBGL
48 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
49 WINE_DECLARE_DEBUG_CHANNEL(winediag);
51 #include "wine/wgl.h"
52 #include "wine/wgl_driver.h"
54 typedef struct __GLXcontextRec *GLXContext;
55 typedef struct __GLXFBConfigRec *GLXFBConfig;
56 typedef XID GLXPixmap;
57 typedef XID GLXDrawable;
58 typedef XID GLXFBConfigID;
59 typedef XID GLXContextID;
60 typedef XID GLXWindow;
61 typedef XID GLXPbuffer;
63 #define GLX_USE_GL 1
64 #define GLX_BUFFER_SIZE 2
65 #define GLX_LEVEL 3
66 #define GLX_RGBA 4
67 #define GLX_DOUBLEBUFFER 5
68 #define GLX_STEREO 6
69 #define GLX_AUX_BUFFERS 7
70 #define GLX_RED_SIZE 8
71 #define GLX_GREEN_SIZE 9
72 #define GLX_BLUE_SIZE 10
73 #define GLX_ALPHA_SIZE 11
74 #define GLX_DEPTH_SIZE 12
75 #define GLX_STENCIL_SIZE 13
76 #define GLX_ACCUM_RED_SIZE 14
77 #define GLX_ACCUM_GREEN_SIZE 15
78 #define GLX_ACCUM_BLUE_SIZE 16
79 #define GLX_ACCUM_ALPHA_SIZE 17
81 #define GLX_BAD_SCREEN 1
82 #define GLX_BAD_ATTRIBUTE 2
83 #define GLX_NO_EXTENSION 3
84 #define GLX_BAD_VISUAL 4
85 #define GLX_BAD_CONTEXT 5
86 #define GLX_BAD_VALUE 6
87 #define GLX_BAD_ENUM 7
89 #define GLX_VENDOR 1
90 #define GLX_VERSION 2
91 #define GLX_EXTENSIONS 3
93 #define GLX_CONFIG_CAVEAT 0x20
94 #define GLX_DONT_CARE 0xFFFFFFFF
95 #define GLX_X_VISUAL_TYPE 0x22
96 #define GLX_TRANSPARENT_TYPE 0x23
97 #define GLX_TRANSPARENT_INDEX_VALUE 0x24
98 #define GLX_TRANSPARENT_RED_VALUE 0x25
99 #define GLX_TRANSPARENT_GREEN_VALUE 0x26
100 #define GLX_TRANSPARENT_BLUE_VALUE 0x27
101 #define GLX_TRANSPARENT_ALPHA_VALUE 0x28
102 #define GLX_WINDOW_BIT 0x00000001
103 #define GLX_PIXMAP_BIT 0x00000002
104 #define GLX_PBUFFER_BIT 0x00000004
105 #define GLX_AUX_BUFFERS_BIT 0x00000010
106 #define GLX_FRONT_LEFT_BUFFER_BIT 0x00000001
107 #define GLX_FRONT_RIGHT_BUFFER_BIT 0x00000002
108 #define GLX_BACK_LEFT_BUFFER_BIT 0x00000004
109 #define GLX_BACK_RIGHT_BUFFER_BIT 0x00000008
110 #define GLX_DEPTH_BUFFER_BIT 0x00000020
111 #define GLX_STENCIL_BUFFER_BIT 0x00000040
112 #define GLX_ACCUM_BUFFER_BIT 0x00000080
113 #define GLX_NONE 0x8000
114 #define GLX_SLOW_CONFIG 0x8001
115 #define GLX_TRUE_COLOR 0x8002
116 #define GLX_DIRECT_COLOR 0x8003
117 #define GLX_PSEUDO_COLOR 0x8004
118 #define GLX_STATIC_COLOR 0x8005
119 #define GLX_GRAY_SCALE 0x8006
120 #define GLX_STATIC_GRAY 0x8007
121 #define GLX_TRANSPARENT_RGB 0x8008
122 #define GLX_TRANSPARENT_INDEX 0x8009
123 #define GLX_VISUAL_ID 0x800B
124 #define GLX_SCREEN 0x800C
125 #define GLX_NON_CONFORMANT_CONFIG 0x800D
126 #define GLX_DRAWABLE_TYPE 0x8010
127 #define GLX_RENDER_TYPE 0x8011
128 #define GLX_X_RENDERABLE 0x8012
129 #define GLX_FBCONFIG_ID 0x8013
130 #define GLX_RGBA_TYPE 0x8014
131 #define GLX_COLOR_INDEX_TYPE 0x8015
132 #define GLX_MAX_PBUFFER_WIDTH 0x8016
133 #define GLX_MAX_PBUFFER_HEIGHT 0x8017
134 #define GLX_MAX_PBUFFER_PIXELS 0x8018
135 #define GLX_PRESERVED_CONTENTS 0x801B
136 #define GLX_LARGEST_PBUFFER 0x801C
137 #define GLX_WIDTH 0x801D
138 #define GLX_HEIGHT 0x801E
139 #define GLX_EVENT_MASK 0x801F
140 #define GLX_DAMAGED 0x8020
141 #define GLX_SAVED 0x8021
142 #define GLX_WINDOW 0x8022
143 #define GLX_PBUFFER 0x8023
144 #define GLX_PBUFFER_HEIGHT 0x8040
145 #define GLX_PBUFFER_WIDTH 0x8041
146 #define GLX_SWAP_METHOD_OML 0x8060
147 #define GLX_SWAP_EXCHANGE_OML 0x8061
148 #define GLX_SWAP_COPY_OML 0x8062
149 #define GLX_SWAP_UNDEFINED_OML 0x8063
150 #define GLX_RGBA_BIT 0x00000001
151 #define GLX_COLOR_INDEX_BIT 0x00000002
152 #define GLX_PBUFFER_CLOBBER_MASK 0x08000000
154 /** GLX_ARB_multisample */
155 #define GLX_SAMPLE_BUFFERS_ARB 100000
156 #define GLX_SAMPLES_ARB 100001
157 /** GLX_ARB_framebuffer_sRGB */
158 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT 0x20B2
159 /** GLX_EXT_fbconfig_packed_float */
160 #define GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT 0x20B1
161 #define GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT 0x00000008
162 /** GLX_ARB_create_context */
163 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
164 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
165 #define GLX_CONTEXT_FLAGS_ARB 0x2094
166 /** GLX_ARB_create_context_profile */
167 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
168 /** GLX_ATI_pixel_format_float */
169 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
170 /** GLX_ARB_pixel_format_float */
171 #define GLX_RGBA_FLOAT_BIT 0x00000004
172 #define GLX_RGBA_FLOAT_TYPE 0x20B9
173 /** GLX_MESA_query_renderer */
174 #define GLX_RENDERER_ID_MESA 0x818E
175 /** GLX_NV_float_buffer */
176 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
179 struct WineGLInfo {
180 const char *glVersion;
181 char *glExtensions;
183 int glxVersion[2];
185 const char *glxServerVersion;
186 const char *glxServerVendor;
187 const char *glxServerExtensions;
189 const char *glxClientVersion;
190 const char *glxClientVendor;
191 const char *glxClientExtensions;
193 const char *glxExtensions;
195 BOOL glxDirect;
196 char wglExtensions[4096];
199 struct wgl_pixel_format
201 GLXFBConfig fbconfig;
202 int fmt_id;
203 int render_type;
204 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
207 struct wgl_context
209 HDC hdc;
210 BOOL has_been_current;
211 BOOL sharing;
212 BOOL gl3_context;
213 XVisualInfo *vis;
214 const struct wgl_pixel_format *fmt;
215 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
216 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
217 GLXContext ctx;
218 GLXDrawable drawables[2];
219 BOOL refresh_drawables;
220 struct list entry;
223 struct wgl_pbuffer
225 Drawable drawable;
226 const struct wgl_pixel_format* fmt;
227 int width;
228 int height;
229 int* attribList;
230 int use_render_texture; /* This is also the internal texture format */
231 int texture_bind_target;
232 int texture_bpp;
233 GLint texture_format;
234 GLuint texture_target;
235 GLenum texture_type;
236 GLuint texture;
237 int texture_level;
238 GLXContext tmp_context;
239 GLXContext prev_context;
240 struct list entry;
243 enum dc_gl_type
245 DC_GL_NONE, /* no GL support (pixel format not set yet) */
246 DC_GL_WINDOW, /* normal top-level window */
247 DC_GL_CHILD_WIN, /* child window using XComposite */
248 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
249 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
252 struct gl_drawable
254 enum dc_gl_type type; /* type of GL surface */
255 GLXDrawable drawable; /* drawable for rendering with GL */
256 Window window; /* window if drawable is a GLXWindow */
257 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
258 Colormap colormap; /* colormap used for the drawable */
259 const struct wgl_pixel_format *format; /* pixel format for the drawable */
260 XVisualInfo *visual; /* information about the GL visual */
261 RECT rect; /* drawable rect, relative to whole window drawable */
262 int swap_interval;
263 BOOL refresh_swap_interval;
266 enum glx_swap_control_method
268 GLX_SWAP_CONTROL_NONE,
269 GLX_SWAP_CONTROL_EXT,
270 GLX_SWAP_CONTROL_SGI,
271 GLX_SWAP_CONTROL_MESA
274 /* X context to associate a struct gl_drawable to an hwnd */
275 static XContext gl_hwnd_context;
276 /* X context to associate a struct gl_drawable to a pbuffer hdc */
277 static XContext gl_pbuffer_context;
279 static struct list context_list = LIST_INIT( context_list );
280 static struct list pbuffer_list = LIST_INIT( pbuffer_list );
281 static struct WineGLInfo WineGLInfo = { 0 };
282 static struct wgl_pixel_format *pixel_formats;
283 static int nb_pixel_formats, nb_onscreen_formats;
284 static BOOL use_render_texture_emulation = TRUE;
286 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
287 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
288 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
289 static BOOL has_swap_control_tear = FALSE;
290 static BOOL has_swap_method = FALSE;
292 static CRITICAL_SECTION context_section;
293 static CRITICAL_SECTION_DEBUG critsect_debug =
295 0, 0, &context_section,
296 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
297 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
299 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
301 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
303 static struct opengl_funcs opengl_funcs;
305 #define USE_GL_FUNC(name) #name,
306 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
307 #undef USE_GL_FUNC
309 static void X11DRV_WineGL_LoadExtensions(void);
310 static void init_pixel_formats( Display *display );
311 static BOOL glxRequireVersion(int requiredVersion);
313 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
314 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
315 TRACE(" - dwFlags : ");
316 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
317 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
318 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
322 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
323 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
324 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
325 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
326 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
327 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
328 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
329 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
330 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
331 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
332 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
333 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
334 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
335 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
336 #undef TEST_AND_DUMP
337 TRACE("\n");
339 TRACE(" - iPixelType : ");
340 switch (ppfd->iPixelType) {
341 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
342 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
344 TRACE("\n");
346 TRACE(" - Color : %d\n", ppfd->cColorBits);
347 TRACE(" - Red : %d\n", ppfd->cRedBits);
348 TRACE(" - Green : %d\n", ppfd->cGreenBits);
349 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
350 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
351 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
352 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
353 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
354 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
356 TRACE(" - iLayerType : ");
357 switch (ppfd->iLayerType) {
358 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
359 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
360 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
362 TRACE("\n");
365 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
366 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
368 /* GLX 1.0 */
369 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
370 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
371 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
372 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
373 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
374 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
375 static Bool (*pglXQueryExtension)( Display *dpy, int *errorb, int *event );
376 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
377 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
378 static int (*pglXGetConfig)( Display *dpy, XVisualInfo *visual, int attrib, int *value );
379 static GLXContext (*pglXGetCurrentContext)( void );
380 static GLXDrawable (*pglXGetCurrentDrawable)( void );
382 /* GLX 1.1 */
383 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
384 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
385 static const char *(*pglXGetClientString)( Display *dpy, int name );
387 /* GLX 1.3 */
388 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
389 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
390 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
391 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
392 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
393 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
394 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
395 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
396 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
397 static GLXPixmap (*pglXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list );
398 static void (*pglXDestroyPixmap)( Display *dpy, GLXPixmap pixmap );
399 static GLXWindow (*pglXCreateWindow)( Display *dpy, GLXFBConfig config, Window win, const int *attrib_list );
400 static void (*pglXDestroyWindow)( Display *dpy, GLXWindow win );
402 /* GLX Extensions */
403 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
404 static void* (*pglXGetProcAddressARB)(const GLubyte *);
405 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
406 static int (*pglXSwapIntervalSGI)(int);
408 /* NV GLX Extension */
409 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
410 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
412 /* MESA GLX Extensions */
413 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
414 static int (*pglXSwapIntervalMESA)(unsigned int interval);
415 static Bool (*pglXQueryCurrentRendererIntegerMESA)(int attribute, unsigned int *value);
416 static const char *(*pglXQueryCurrentRendererStringMESA)(int attribute);
417 static Bool (*pglXQueryRendererIntegerMESA)(Display *dpy, int screen, int renderer, int attribute, unsigned int *value);
418 static const char *(*pglXQueryRendererStringMESA)(Display *dpy, int screen, int renderer, int attribute);
420 /* OpenML GLX Extensions */
421 static Bool (*pglXWaitForSbcOML)( Display *dpy, GLXDrawable drawable,
422 INT64 target_sbc, INT64 *ust, INT64 *msc, INT64 *sbc );
423 static INT64 (*pglXSwapBuffersMscOML)( Display *dpy, GLXDrawable drawable,
424 INT64 target_msc, INT64 divisor, INT64 remainder );
426 /* Standard OpenGL */
427 static void (*pglFinish)(void);
428 static void (*pglFlush)(void);
429 static const GLubyte *(*pglGetString)(GLenum name);
431 static void wglFinish(void);
432 static void wglFlush(void);
433 static const GLubyte *wglGetString(GLenum name);
435 /* check if the extension is present in the list */
436 static BOOL has_extension( const char *list, const char *ext )
438 size_t len = strlen( ext );
440 while (list)
442 while (*list == ' ') list++;
443 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
444 list = strchr( list, ' ' );
446 return FALSE;
449 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
451 /* In the future we might want to find the exact X or GLX error to report back to the app */
452 return 1;
455 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
457 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
459 int screen = DefaultScreen(gdi_display);
460 Window win = 0, root = 0;
461 const char *gl_renderer;
462 const char* str;
463 XVisualInfo *vis;
464 GLXContext ctx = NULL;
465 XSetWindowAttributes attr;
466 BOOL ret = FALSE;
467 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
469 attr.override_redirect = True;
470 attr.colormap = None;
471 attr.border_pixel = 0;
473 vis = pglXChooseVisual(gdi_display, screen, attribList);
474 if (vis) {
475 #ifdef __i386__
476 WORD old_fs = wine_get_fs();
477 /* Create a GLX Context. Without one we can't query GL information */
478 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
479 if (wine_get_fs() != old_fs)
481 wine_set_fs( old_fs );
482 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
483 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
484 goto done;
486 #else
487 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
488 #endif
490 if (!ctx) goto done;
492 root = RootWindow( gdi_display, vis->screen );
493 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
494 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
495 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
496 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
497 XMapWindow( gdi_display, win );
498 else
499 win = root;
501 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
503 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
504 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
505 goto done;
507 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
508 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
509 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
510 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
511 strcpy(WineGLInfo.glExtensions, str);
512 strcat(WineGLInfo.glExtensions, legacy_extensions);
514 /* Get the common GLX version supported by GLX client and server ( major/minor) */
515 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
517 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
518 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
519 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
521 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
522 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
523 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
525 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
526 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
528 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
529 TRACE("GL renderer : %s.\n", gl_renderer);
530 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
531 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
532 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
533 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
534 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
535 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
537 if(!WineGLInfo.glxDirect)
539 int fd = ConnectionNumber(gdi_display);
540 struct sockaddr_un uaddr;
541 unsigned int uaddrlen = sizeof(struct sockaddr_un);
543 /* In general indirect rendering on a local X11 server indicates a driver problem.
544 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
546 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
547 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
548 "haven't been installed correctly (using GL renderer %s, version %s).\n",
549 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
550 debugstr_a(WineGLInfo.glVersion));
552 else
554 /* In general you would expect that if direct rendering is returned, that you receive hardware
555 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
556 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
557 * software rendering, it shows direct rendering.
559 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
560 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
561 * it shows 'Mesa X11'.
563 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
564 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
565 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
566 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
567 debugstr_a(WineGLInfo.glVersion));
569 ret = TRUE;
571 done:
572 if(vis) XFree(vis);
573 if(ctx) {
574 pglXMakeCurrent(gdi_display, None, NULL);
575 pglXDestroyContext(gdi_display, ctx);
577 if (win != root) XDestroyWindow( gdi_display, win );
578 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
579 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
580 return ret;
583 static void *opengl_handle;
585 static BOOL WINAPI init_opengl( INIT_ONCE *once, void *param, void **context )
587 char buffer[200];
588 int error_base, event_base;
589 unsigned int i;
591 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
592 and include all dependencies */
593 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
594 if (opengl_handle == NULL)
596 ERR( "Failed to load libGL: %s\n", buffer );
597 ERR( "OpenGL support is disabled.\n");
598 return TRUE;
601 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
603 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
605 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
606 goto failed;
610 /* redirect some standard OpenGL functions */
611 #define REDIRECT(func) \
612 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
613 REDIRECT( glFinish );
614 REDIRECT( glFlush );
615 REDIRECT( glGetString );
616 #undef REDIRECT
618 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
619 if (pglXGetProcAddressARB == NULL) {
620 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
621 goto failed;
624 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
626 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
627 goto failed; \
628 } while(0)
630 /* GLX 1.0 */
631 LOAD_FUNCPTR(glXChooseVisual);
632 LOAD_FUNCPTR(glXCopyContext);
633 LOAD_FUNCPTR(glXCreateContext);
634 LOAD_FUNCPTR(glXGetCurrentContext);
635 LOAD_FUNCPTR(glXGetCurrentDrawable);
636 LOAD_FUNCPTR(glXDestroyContext);
637 LOAD_FUNCPTR(glXGetConfig);
638 LOAD_FUNCPTR(glXIsDirect);
639 LOAD_FUNCPTR(glXMakeCurrent);
640 LOAD_FUNCPTR(glXSwapBuffers);
641 LOAD_FUNCPTR(glXQueryExtension);
642 LOAD_FUNCPTR(glXQueryVersion);
644 /* GLX 1.1 */
645 LOAD_FUNCPTR(glXGetClientString);
646 LOAD_FUNCPTR(glXQueryExtensionsString);
647 LOAD_FUNCPTR(glXQueryServerString);
649 /* GLX 1.3 */
650 LOAD_FUNCPTR(glXCreatePbuffer);
651 LOAD_FUNCPTR(glXCreateNewContext);
652 LOAD_FUNCPTR(glXDestroyPbuffer);
653 LOAD_FUNCPTR(glXMakeContextCurrent);
654 LOAD_FUNCPTR(glXGetFBConfigs);
655 LOAD_FUNCPTR(glXCreatePixmap);
656 LOAD_FUNCPTR(glXDestroyPixmap);
657 LOAD_FUNCPTR(glXCreateWindow);
658 LOAD_FUNCPTR(glXDestroyWindow);
659 #undef LOAD_FUNCPTR
661 /* It doesn't matter if these fail. They'll only be used if the driver reports
662 the associated extension is available (and if a driver reports the extension
663 is available but fails to provide the functions, it's quite broken) */
664 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
665 /* ARB GLX Extension */
666 LOAD_FUNCPTR(glXCreateContextAttribsARB);
667 /* EXT GLX Extension */
668 LOAD_FUNCPTR(glXSwapIntervalEXT);
669 /* MESA GLX Extension */
670 LOAD_FUNCPTR(glXSwapIntervalMESA);
671 /* SGI GLX Extension */
672 LOAD_FUNCPTR(glXSwapIntervalSGI);
673 /* NV GLX Extension */
674 LOAD_FUNCPTR(glXAllocateMemoryNV);
675 LOAD_FUNCPTR(glXFreeMemoryNV);
676 #undef LOAD_FUNCPTR
678 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
680 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
681 TRACE("GLX is up and running error_base = %d\n", error_base);
682 } else {
683 ERR( "GLX extension is missing, disabling OpenGL.\n" );
684 goto failed;
686 gl_hwnd_context = XUniqueContext();
687 gl_pbuffer_context = XUniqueContext();
689 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
690 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
691 * rendering is used.
693 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
694 * depends on the reported GLX client / server version and on the client / server extension list.
695 * Those don't have to be the same.
697 * In general the server GLX information lists the capabilities in case of indirect rendering.
698 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
699 * available and in that case the client GLX informat can be used.
700 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
701 * in the GLX version/extension list. When a program does this it works for certain for both
702 * direct and indirect rendering.
704 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
705 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
706 * extension list which causes this extension not to be listed. (Wine requires this extension).
707 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
708 * pbuffers is around.
710 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
711 * the ATI drivers and from then on use GLX client information for them.
714 if(glxRequireVersion(3)) {
715 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
716 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
717 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
718 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
719 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
720 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
721 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
722 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
724 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
725 * enable this function when the Xserver understand GLX 1.3 or newer
727 pglXQueryDrawable = NULL;
728 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
729 TRACE("Overriding ATI GLX capabilities!\n");
730 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
731 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
732 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
733 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
735 /* Use client GLX information in case of the ATI drivers. We override the
736 * capabilities over here and not somewhere else as ATI might better their
737 * life in the future. In case they release proper drivers this block of
738 * code won't be called. */
739 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
740 } else {
741 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
744 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
745 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
748 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_query_renderer" ))
750 pglXQueryCurrentRendererIntegerMESA = pglXGetProcAddressARB(
751 (const GLubyte *)"glXQueryCurrentRendererIntegerMESA" );
752 pglXQueryCurrentRendererStringMESA = pglXGetProcAddressARB(
753 (const GLubyte *)"glXQueryCurrentRendererStringMESA" );
754 pglXQueryRendererIntegerMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererIntegerMESA" );
755 pglXQueryRendererStringMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererStringMESA" );
758 if (has_extension( WineGLInfo.glxExtensions, "GLX_OML_sync_control" ))
760 pglXWaitForSbcOML = pglXGetProcAddressARB( (const GLubyte *)"glXWaitForSbcOML" );
761 pglXSwapBuffersMscOML = pglXGetProcAddressARB( (const GLubyte *)"glXSwapBuffersMscOML" );
764 X11DRV_WineGL_LoadExtensions();
765 init_pixel_formats( gdi_display );
766 return TRUE;
768 failed:
769 wine_dlclose(opengl_handle, NULL, 0);
770 opengl_handle = NULL;
771 return TRUE;
774 static BOOL has_opengl(void)
776 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
777 InitOnceExecuteOnce(&init_once, init_opengl, NULL, NULL);
778 return opengl_handle != NULL;
781 static const char *debugstr_fbconfig( GLXFBConfig fbconfig )
783 int id, visual, drawable;
785 if (pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_FBCONFIG_ID, &id ))
786 return "*** invalid fbconfig";
787 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_VISUAL_ID, &visual );
788 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &drawable );
789 return wine_dbg_sprintf( "fbconfig %#x visual id %#x drawable type %#x", id, visual, drawable );
792 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
793 int nAttribs = 0;
794 unsigned cur = 0;
795 int pop;
796 int drawattrib = 0;
797 int nvfloatattrib = GLX_DONT_CARE;
798 int pixelattrib = GLX_DONT_CARE;
800 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
801 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
802 while (iWGLAttr && 0 != iWGLAttr[cur]) {
803 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
805 switch (iWGLAttr[cur]) {
806 case WGL_AUX_BUFFERS_ARB:
807 pop = iWGLAttr[++cur];
808 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
809 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
810 break;
811 case WGL_COLOR_BITS_ARB:
812 pop = iWGLAttr[++cur];
813 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
814 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
815 break;
816 case WGL_BLUE_BITS_ARB:
817 pop = iWGLAttr[++cur];
818 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
819 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
820 break;
821 case WGL_RED_BITS_ARB:
822 pop = iWGLAttr[++cur];
823 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
824 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
825 break;
826 case WGL_GREEN_BITS_ARB:
827 pop = iWGLAttr[++cur];
828 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
829 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
830 break;
831 case WGL_ALPHA_BITS_ARB:
832 pop = iWGLAttr[++cur];
833 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
834 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
835 break;
836 case WGL_DEPTH_BITS_ARB:
837 pop = iWGLAttr[++cur];
838 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
839 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
840 break;
841 case WGL_STENCIL_BITS_ARB:
842 pop = iWGLAttr[++cur];
843 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
844 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
845 break;
846 case WGL_DOUBLE_BUFFER_ARB:
847 pop = iWGLAttr[++cur];
848 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
849 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
850 break;
851 case WGL_STEREO_ARB:
852 pop = iWGLAttr[++cur];
853 PUSH2(oGLXAttr, GLX_STEREO, pop);
854 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
855 break;
857 case WGL_PIXEL_TYPE_ARB:
858 pop = iWGLAttr[++cur];
859 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
860 switch (pop) {
861 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
862 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
863 /* 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 */
864 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
865 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
866 default:
867 ERR("unexpected PixelType(%x)\n", pop);
868 pop = 0;
870 break;
872 case WGL_SUPPORT_GDI_ARB:
873 /* This flag is set in a pixel format */
874 pop = iWGLAttr[++cur];
875 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
876 break;
878 case WGL_DRAW_TO_BITMAP_ARB:
879 /* This flag is set in a pixel format */
880 pop = iWGLAttr[++cur];
881 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
882 break;
884 case WGL_DRAW_TO_WINDOW_ARB:
885 pop = iWGLAttr[++cur];
886 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
887 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
888 if (pop) {
889 drawattrib |= GLX_WINDOW_BIT;
891 break;
893 case WGL_DRAW_TO_PBUFFER_ARB:
894 pop = iWGLAttr[++cur];
895 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
896 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
897 if (pop) {
898 drawattrib |= GLX_PBUFFER_BIT;
900 break;
902 case WGL_ACCELERATION_ARB:
903 /* This flag is set in a pixel format */
904 pop = iWGLAttr[++cur];
905 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
906 break;
908 case WGL_SUPPORT_OPENGL_ARB:
909 pop = iWGLAttr[++cur];
910 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
911 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
912 break;
914 case WGL_SWAP_METHOD_ARB:
915 pop = iWGLAttr[++cur];
916 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
917 if (has_swap_method)
919 switch (pop)
921 case WGL_SWAP_EXCHANGE_ARB:
922 pop = GLX_SWAP_EXCHANGE_OML;
923 break;
924 case WGL_SWAP_COPY_ARB:
925 pop = GLX_SWAP_COPY_OML;
926 break;
927 case WGL_SWAP_UNDEFINED_ARB:
928 pop = GLX_SWAP_UNDEFINED_OML;
929 break;
930 default:
931 ERR("Unexpected swap method %#x.\n", pop);
932 pop = GLX_DONT_CARE;
934 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
936 else
938 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
940 break;
942 case WGL_PBUFFER_LARGEST_ARB:
943 pop = iWGLAttr[++cur];
944 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
945 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
946 break;
948 case WGL_SAMPLE_BUFFERS_ARB:
949 pop = iWGLAttr[++cur];
950 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
951 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
952 break;
954 case WGL_SAMPLES_ARB:
955 pop = iWGLAttr[++cur];
956 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
957 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
958 break;
960 case WGL_TEXTURE_FORMAT_ARB:
961 case WGL_TEXTURE_TARGET_ARB:
962 case WGL_MIPMAP_TEXTURE_ARB:
963 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
964 pop = iWGLAttr[++cur];
965 if (NULL == pbuf) {
966 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
968 if (!use_render_texture_emulation) {
969 if (WGL_NO_TEXTURE_ARB != pop) {
970 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
971 return -1; /** error: don't support it */
972 } else {
973 drawattrib |= GLX_PBUFFER_BIT;
976 break ;
977 case WGL_FLOAT_COMPONENTS_NV:
978 nvfloatattrib = iWGLAttr[++cur];
979 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
980 break ;
981 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
982 case WGL_BIND_TO_TEXTURE_RGB_ARB:
983 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
984 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
985 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
986 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
987 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
988 pop = iWGLAttr[++cur];
989 /** cannot be converted, see direct handling on
990 * - wglGetPixelFormatAttribivARB
991 * TODO: wglChoosePixelFormat
993 break ;
994 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
995 pop = iWGLAttr[++cur];
996 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
997 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
998 break ;
1000 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
1001 pop = iWGLAttr[++cur];
1002 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
1003 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
1004 break ;
1005 default:
1006 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
1007 cur++;
1008 break;
1010 ++cur;
1013 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
1014 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
1015 * pixmap and pbuffer formats appear as well. */
1016 if (!drawattrib) drawattrib = GLX_DONT_CARE;
1017 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
1018 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
1020 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
1021 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
1022 * GLX_DONT_CARE unless it is overridden. */
1023 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
1024 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
1026 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
1027 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
1028 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
1029 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
1032 return nAttribs;
1035 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
1037 int render_type, render_type_bit;
1038 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
1039 switch(render_type_bit)
1041 case GLX_RGBA_BIT:
1042 render_type = GLX_RGBA_TYPE;
1043 break;
1044 case GLX_COLOR_INDEX_BIT:
1045 render_type = GLX_COLOR_INDEX_TYPE;
1046 break;
1047 case GLX_RGBA_FLOAT_BIT:
1048 render_type = GLX_RGBA_FLOAT_TYPE;
1049 break;
1050 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
1051 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
1052 break;
1053 default:
1054 ERR("Unknown render_type: %x\n", render_type_bit);
1055 render_type = 0;
1057 return render_type;
1060 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
1061 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
1063 int dbuf, value;
1064 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
1065 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1067 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1068 * the GLX_PIXMAP_BIT set. */
1069 return !dbuf && (value & GLX_PIXMAP_BIT);
1072 static void init_pixel_formats( Display *display )
1074 struct wgl_pixel_format *list;
1075 int size = 0, onscreen_size = 0;
1076 int fmt_id, nCfgs, i, run, bmp_formats;
1077 GLXFBConfig* cfgs;
1078 XVisualInfo *visinfo;
1080 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1081 if (NULL == cfgs || 0 == nCfgs) {
1082 if(cfgs != NULL) XFree(cfgs);
1083 ERR("glXChooseFBConfig returns NULL\n");
1084 return;
1087 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1088 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1089 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1090 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1092 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1094 for(i=0, bmp_formats=0; i<nCfgs; i++)
1096 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1097 bmp_formats++;
1099 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1101 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1103 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1104 * Do this as GLX doesn't guarantee that the list is sorted */
1105 for(run=0; run < 2; run++)
1107 for(i=0; i<nCfgs; i++) {
1108 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1109 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1111 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1112 * The second run we only set offscreen formats. */
1113 if(!run && visinfo)
1115 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1116 * The contents is copied to the destination using XCopyArea. For the copying to work
1117 * the depth of the source and destination window should be the same. In general this should
1118 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1119 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1120 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1121 * list formats with the same depth. */
1122 if(visinfo->depth != default_visual.depth)
1124 XFree(visinfo);
1125 continue;
1128 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1129 list[size].fbconfig = cfgs[i];
1130 list[size].fmt_id = fmt_id;
1131 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1132 list[size].dwFlags = 0;
1133 size++;
1134 onscreen_size++;
1136 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1137 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1139 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1140 list[size].fbconfig = cfgs[i];
1141 list[size].fmt_id = fmt_id;
1142 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1143 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1144 size++;
1145 onscreen_size++;
1147 } else if(run && !visinfo) {
1148 int window_drawable=0;
1149 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1151 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1152 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1153 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1154 * likely make our child window opengl rendering more complicated since likely you can't use
1155 * XCopyArea on a GLX Window.
1156 * For now ignore fbconfigs which are window drawable but lack a visual. */
1157 if(window_drawable & GLX_WINDOW_BIT)
1159 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1160 continue;
1163 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1164 list[size].fbconfig = cfgs[i];
1165 list[size].fmt_id = fmt_id;
1166 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1167 list[size].dwFlags = 0;
1168 size++;
1171 if (visinfo) XFree(visinfo);
1175 XFree(cfgs);
1177 pixel_formats = list;
1178 nb_pixel_formats = size;
1179 nb_onscreen_formats = onscreen_size;
1182 static inline BOOL is_valid_pixel_format( int format )
1184 return format > 0 && format <= nb_pixel_formats;
1187 static inline BOOL is_onscreen_pixel_format( int format )
1189 return format > 0 && format <= nb_onscreen_formats;
1192 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1194 return format - pixel_formats + 1;
1197 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1198 * In our WGL implementation we only support a subset of these formats namely the format of
1199 * Wine's main visual and offscreen formats (if they are available).
1200 * This function converts a WGL format to its corresponding GLX one.
1202 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1204 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1205 * iPixelFormat in case of probing the number of pixelformats.
1207 if (is_valid_pixel_format( iPixelFormat ) &&
1208 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1209 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1210 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1211 return &pixel_formats[iPixelFormat-1];
1213 return NULL;
1216 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1217 static void mark_drawable_dirty(GLXDrawable old, GLXDrawable new)
1219 struct wgl_context *ctx;
1221 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1223 if (old == ctx->drawables[0]) {
1224 ctx->drawables[0] = new;
1225 ctx->refresh_drawables = TRUE;
1227 if (old == ctx->drawables[1]) {
1228 ctx->drawables[1] = new;
1229 ctx->refresh_drawables = TRUE;
1234 /* Given the current context, make sure its drawable is sync'd */
1235 static inline void sync_context(struct wgl_context *context)
1237 if (context->refresh_drawables) {
1238 if (glxRequireVersion(3))
1239 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1240 context->drawables[1], context->ctx);
1241 else
1242 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1243 context->refresh_drawables = FALSE;
1247 static BOOL set_swap_interval(GLXDrawable drawable, int interval)
1249 BOOL ret = TRUE;
1251 switch (swap_control_method)
1253 case GLX_SWAP_CONTROL_EXT:
1254 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1255 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1256 XSync(gdi_display, False);
1257 ret = !X11DRV_check_error();
1258 break;
1260 case GLX_SWAP_CONTROL_MESA:
1261 ret = !pglXSwapIntervalMESA(interval);
1262 break;
1264 case GLX_SWAP_CONTROL_SGI:
1265 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1266 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1267 * value to be an error. Just silently ignore the request for now.
1269 if (!interval)
1270 WARN("Request to disable vertical sync is not handled\n");
1271 else
1272 ret = !pglXSwapIntervalSGI(interval);
1273 break;
1275 case GLX_SWAP_CONTROL_NONE:
1276 /* Unlikely to happen on modern GLX implementations */
1277 WARN("Request to adjust swap interval is not handled\n");
1278 break;
1281 return ret;
1284 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1286 struct gl_drawable *gl;
1288 EnterCriticalSection( &context_section );
1289 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1290 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1291 LeaveCriticalSection( &context_section );
1292 return NULL;
1295 static void release_gl_drawable( struct gl_drawable *gl )
1297 if (gl) LeaveCriticalSection( &context_section );
1300 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1302 GLXContext ctx;
1304 if(context->gl3_context)
1306 if(context->numAttribs)
1307 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, context->attribList);
1308 else
1309 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, NULL);
1311 else if(context->vis)
1312 ctx = pglXCreateContext(gdi_display, context->vis, shareList, GL_TRUE);
1313 else /* Create a GLX Context for a pbuffer */
1314 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1316 return ctx;
1320 /***********************************************************************
1321 * free_gl_drawable
1323 static void free_gl_drawable( struct gl_drawable *gl )
1325 switch (gl->type)
1327 case DC_GL_CHILD_WIN:
1328 pglXDestroyWindow( gdi_display, gl->drawable );
1329 XDestroyWindow( gdi_display, gl->window );
1330 XFreeColormap( gdi_display, gl->colormap );
1331 break;
1332 case DC_GL_PIXMAP_WIN:
1333 pglXDestroyPixmap( gdi_display, gl->drawable );
1334 XFreePixmap( gdi_display, gl->pixmap );
1335 break;
1336 default:
1337 break;
1339 if (gl->visual) XFree( gl->visual );
1340 HeapFree( GetProcessHeap(), 0, gl );
1344 /***********************************************************************
1345 * create_gl_drawable
1347 static BOOL create_gl_drawable( HWND hwnd, struct gl_drawable *gl )
1349 gl->drawable = 0;
1351 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1353 struct x11drv_win_data *data = get_win_data( hwnd );
1355 if (data)
1357 gl->type = DC_GL_WINDOW;
1358 gl->window = create_client_window( data, gl->visual );
1359 if (gl->window)
1360 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1361 release_win_data( data );
1364 #ifdef SONAME_LIBXCOMPOSITE
1365 else if(usexcomposite)
1367 static Window dummy_parent;
1368 XSetWindowAttributes attrib;
1370 attrib.override_redirect = True;
1371 if (!dummy_parent)
1373 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1374 InputOutput, default_visual.visual, CWOverrideRedirect, &attrib );
1375 XMapWindow( gdi_display, dummy_parent );
1377 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1378 (gl->visual->class == PseudoColor ||
1379 gl->visual->class == GrayScale ||
1380 gl->visual->class == DirectColor) ?
1381 AllocAll : AllocNone);
1382 attrib.colormap = gl->colormap;
1383 XInstallColormap(gdi_display, attrib.colormap);
1385 gl->type = DC_GL_CHILD_WIN;
1386 gl->window = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1387 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1388 0, gl->visual->depth, InputOutput, gl->visual->visual,
1389 CWColormap | CWOverrideRedirect, &attrib );
1390 if (gl->window)
1392 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1393 if (gl->drawable)
1395 pXCompositeRedirectWindow(gdi_display, gl->window, CompositeRedirectManual);
1396 XMapWindow(gdi_display, gl->window);
1399 else XFreeColormap( gdi_display, gl->colormap );
1401 #endif
1402 else
1404 WARN("XComposite is not available, using GLXPixmap hack\n");
1406 gl->type = DC_GL_PIXMAP_WIN;
1407 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1408 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1409 gl->visual->depth );
1410 if (gl->pixmap)
1412 gl->drawable = pglXCreatePixmap( gdi_display, gl->format->fbconfig, gl->pixmap, NULL );
1413 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1417 if (gl->drawable)
1418 gl->refresh_swap_interval = TRUE;
1419 return gl->drawable != 0;
1423 /***********************************************************************
1424 * set_win_format
1426 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1428 struct gl_drawable *gl, *prev;
1430 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1431 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1432 * there is no way to query it, so we have to store it here.
1434 gl->swap_interval = 1;
1435 gl->refresh_swap_interval = TRUE;
1436 gl->format = format;
1437 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1438 if (!gl->visual)
1440 HeapFree( GetProcessHeap(), 0, gl );
1441 return FALSE;
1444 GetClientRect( hwnd, &gl->rect );
1445 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1446 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1448 if (!create_gl_drawable( hwnd, gl ))
1450 XFree( gl->visual );
1451 HeapFree( GetProcessHeap(), 0, gl );
1452 return FALSE;
1455 TRACE( "created GL drawable %lx for win %p %s\n",
1456 gl->drawable, hwnd, debugstr_fbconfig( format->fbconfig ));
1458 XFlush( gdi_display );
1460 EnterCriticalSection( &context_section );
1461 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1463 gl->swap_interval = prev->swap_interval;
1464 free_gl_drawable( prev );
1466 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1467 LeaveCriticalSection( &context_section );
1469 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1470 return TRUE;
1474 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1476 const struct wgl_pixel_format *fmt;
1477 int value;
1478 HWND hwnd = WindowFromDC( hdc );
1480 TRACE("(%p,%d)\n", hdc, format);
1482 if (!hwnd || hwnd == GetDesktopWindow())
1484 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1485 return FALSE;
1488 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1489 if (!fmt)
1491 ERR( "Invalid format %d\n", format );
1492 return FALSE;
1495 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1496 if (!(value & GLX_WINDOW_BIT))
1498 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1499 return FALSE;
1502 if (!allow_change)
1504 struct gl_drawable *gl;
1505 if ((gl = get_gl_drawable( hwnd, hdc )))
1507 int prev = pixel_format_index( gl->format );
1508 release_gl_drawable( gl );
1509 return prev == format; /* cannot change it if already set */
1513 return set_win_format( hwnd, fmt );
1517 /***********************************************************************
1518 * sync_gl_drawable
1520 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1522 struct gl_drawable *gl;
1523 GLXDrawable glxp;
1524 Pixmap pix;
1525 int mask = 0;
1526 XWindowChanges changes;
1528 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1529 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1531 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1533 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1534 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1536 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1538 switch (gl->type)
1540 case DC_GL_CHILD_WIN:
1541 if (mask) XConfigureWindow( gdi_display, gl->window, mask, &changes );
1542 break;
1543 case DC_GL_PIXMAP_WIN:
1544 if (!mask) break;
1545 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1546 if (!pix) goto done;
1547 glxp = pglXCreatePixmap(gdi_display, gl->format->fbconfig, pix, NULL );
1548 if (!glxp)
1550 XFreePixmap(gdi_display, pix);
1551 goto done;
1553 mark_drawable_dirty(gl->drawable, glxp);
1554 XFlush( gdi_display );
1556 XFreePixmap(gdi_display, gl->pixmap);
1557 pglXDestroyPixmap(gdi_display, gl->drawable);
1558 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1560 gl->pixmap = pix;
1561 gl->drawable = glxp;
1562 break;
1563 default:
1564 break;
1566 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1567 done:
1568 release_gl_drawable( gl );
1572 /***********************************************************************
1573 * set_gl_drawable_parent
1575 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1577 struct gl_drawable *gl;
1578 GLXDrawable old_drawable;
1580 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1582 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1584 old_drawable = gl->drawable;
1585 switch (gl->type)
1587 case DC_GL_WINDOW:
1588 break;
1589 case DC_GL_CHILD_WIN:
1590 if (parent != GetDesktopWindow()) goto done;
1591 pglXDestroyWindow( gdi_display, gl->drawable );
1592 XDestroyWindow( gdi_display, gl->window );
1593 XFreeColormap( gdi_display, gl->colormap );
1594 break;
1595 case DC_GL_PIXMAP_WIN:
1596 if (parent != GetDesktopWindow()) goto done;
1597 pglXDestroyPixmap( gdi_display, gl->drawable );
1598 XFreePixmap( gdi_display, gl->pixmap );
1599 break;
1600 default:
1601 goto done;
1604 if (!create_gl_drawable( hwnd, gl ))
1606 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1607 release_gl_drawable( gl );
1608 XFree( gl->visual );
1609 HeapFree( GetProcessHeap(), 0, gl );
1610 __wine_set_pixel_format( hwnd, 0 );
1611 return;
1613 mark_drawable_dirty( old_drawable, gl->drawable );
1615 done:
1616 release_gl_drawable( gl );
1621 /***********************************************************************
1622 * destroy_gl_drawable
1624 void destroy_gl_drawable( HWND hwnd )
1626 struct gl_drawable *gl;
1628 EnterCriticalSection( &context_section );
1629 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1631 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1632 free_gl_drawable( gl );
1634 LeaveCriticalSection( &context_section );
1639 * glxdrv_DescribePixelFormat
1641 * Get the pixel-format descriptor associated to the given id
1643 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1644 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1646 /*XVisualInfo *vis;*/
1647 int value;
1648 int rb,gb,bb,ab;
1649 const struct wgl_pixel_format *fmt;
1651 if (!has_opengl()) return 0;
1653 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1655 if (!ppfd) return nb_onscreen_formats;
1657 /* 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 */
1658 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1659 if (!fmt) {
1660 WARN("unexpected format %d\n", iPixelFormat);
1661 return 0;
1664 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1665 ERR("Wrong structure size !\n");
1666 /* Should set error */
1667 return 0;
1670 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1671 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1672 ppfd->nVersion = 1;
1674 /* These flags are always the same... */
1675 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1676 /* Now the flags extracted from the Visual */
1678 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1679 if(value & GLX_WINDOW_BIT)
1680 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1682 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1683 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1684 * 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
1685 * offered the GDI bit either. */
1686 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1688 /* PFD_GENERIC_FORMAT - gdi software rendering
1689 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1690 * none set - full hardware accelerated by a ICD
1692 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1693 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1695 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1696 if (value) {
1697 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1698 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1700 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1702 /* Pixel type */
1703 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1704 if (value & GLX_RGBA_BIT)
1705 ppfd->iPixelType = PFD_TYPE_RGBA;
1706 else
1707 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1709 /* Color bits */
1710 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1711 ppfd->cColorBits = value;
1713 /* Red, green, blue and alpha bits / shifts */
1714 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1715 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1716 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1717 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1718 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1720 ppfd->cRedBits = rb;
1721 ppfd->cRedShift = gb + bb + ab;
1722 ppfd->cBlueBits = bb;
1723 ppfd->cBlueShift = ab;
1724 ppfd->cGreenBits = gb;
1725 ppfd->cGreenShift = bb + ab;
1726 ppfd->cAlphaBits = ab;
1727 ppfd->cAlphaShift = 0;
1728 } else {
1729 ppfd->cRedBits = 0;
1730 ppfd->cRedShift = 0;
1731 ppfd->cBlueBits = 0;
1732 ppfd->cBlueShift = 0;
1733 ppfd->cGreenBits = 0;
1734 ppfd->cGreenShift = 0;
1735 ppfd->cAlphaBits = 0;
1736 ppfd->cAlphaShift = 0;
1739 /* Accum RGBA bits */
1740 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1741 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1742 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1743 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1745 ppfd->cAccumBits = rb+gb+bb+ab;
1746 ppfd->cAccumRedBits = rb;
1747 ppfd->cAccumGreenBits = gb;
1748 ppfd->cAccumBlueBits = bb;
1749 ppfd->cAccumAlphaBits = ab;
1751 /* Aux bits */
1752 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1753 ppfd->cAuxBuffers = value;
1755 /* Depth bits */
1756 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1757 ppfd->cDepthBits = value;
1759 /* stencil bits */
1760 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1761 ppfd->cStencilBits = value;
1763 ppfd->iLayerType = PFD_MAIN_PLANE;
1765 if (TRACE_ON(wgl)) {
1766 dump_PIXELFORMATDESCRIPTOR(ppfd);
1769 return nb_onscreen_formats;
1772 /***********************************************************************
1773 * glxdrv_wglGetPixelFormat
1775 static int glxdrv_wglGetPixelFormat( HDC hdc )
1777 struct gl_drawable *gl;
1778 int ret = 0;
1780 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1782 ret = pixel_format_index( gl->format );
1783 /* Offscreen formats can't be used with traditional WGL calls.
1784 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1785 if (!is_onscreen_pixel_format( ret )) ret = 1;
1786 release_gl_drawable( gl );
1788 TRACE( "%p -> %d\n", hdc, ret );
1789 return ret;
1792 /***********************************************************************
1793 * glxdrv_wglSetPixelFormat
1795 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1797 return set_pixel_format(hdc, iPixelFormat, FALSE);
1800 /***********************************************************************
1801 * glxdrv_wglCopyContext
1803 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1805 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1807 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1809 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1810 return TRUE;
1813 /***********************************************************************
1814 * glxdrv_wglCreateContext
1816 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1818 struct wgl_context *ret;
1819 struct gl_drawable *gl;
1821 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1823 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1824 return NULL;
1827 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1829 ret->hdc = hdc;
1830 ret->fmt = gl->format;
1831 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1832 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1833 list_add_head( &context_list, &ret->entry );
1835 release_gl_drawable( gl );
1836 TRACE( "%p -> %p\n", hdc, ret );
1837 return ret;
1840 /***********************************************************************
1841 * glxdrv_wglDeleteContext
1843 static BOOL glxdrv_wglDeleteContext(struct wgl_context *ctx)
1845 struct wgl_pbuffer *pb;
1847 TRACE("(%p)\n", ctx);
1849 EnterCriticalSection( &context_section );
1850 list_remove( &ctx->entry );
1851 LIST_FOR_EACH_ENTRY( pb, &pbuffer_list, struct wgl_pbuffer, entry )
1853 if (pb->prev_context == ctx->ctx) {
1854 pglXDestroyContext(gdi_display, pb->tmp_context);
1855 pb->prev_context = pb->tmp_context = NULL;
1858 LeaveCriticalSection( &context_section );
1860 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1861 if (ctx->vis) XFree( ctx->vis );
1862 return HeapFree( GetProcessHeap(), 0, ctx );
1865 /***********************************************************************
1866 * glxdrv_wglGetProcAddress
1868 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1870 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1871 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1874 /***********************************************************************
1875 * glxdrv_wglMakeCurrent
1877 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1879 BOOL ret = FALSE;
1880 struct gl_drawable *gl;
1882 TRACE("(%p,%p)\n", hdc, ctx);
1884 if (!ctx)
1886 pglXMakeCurrent(gdi_display, None, NULL);
1887 NtCurrentTeb()->glContext = NULL;
1888 return TRUE;
1891 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1893 if (ctx->fmt != gl->format)
1895 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1896 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1897 goto done;
1900 TRACE("hdc %p drawable %lx fmt %p ctx %p %s\n", hdc, gl->drawable, gl->format, ctx->ctx,
1901 debugstr_fbconfig( gl->format->fbconfig ));
1903 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1904 if (ret)
1906 NtCurrentTeb()->glContext = ctx;
1907 ctx->has_been_current = TRUE;
1908 ctx->hdc = hdc;
1909 ctx->drawables[0] = gl->drawable;
1910 ctx->drawables[1] = gl->drawable;
1911 ctx->refresh_drawables = FALSE;
1912 goto done;
1915 SetLastError( ERROR_INVALID_HANDLE );
1917 done:
1918 release_gl_drawable( gl );
1919 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1920 return ret;
1923 /***********************************************************************
1924 * X11DRV_wglMakeContextCurrentARB
1926 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1928 BOOL ret = FALSE;
1929 struct gl_drawable *draw_gl, *read_gl = NULL;
1931 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1933 if (!ctx)
1935 pglXMakeCurrent(gdi_display, None, NULL);
1936 NtCurrentTeb()->glContext = NULL;
1937 return TRUE;
1940 if (!pglXMakeContextCurrent) return FALSE;
1942 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1944 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1945 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1946 read_gl ? read_gl->drawable : 0, ctx->ctx);
1947 if (ret)
1949 ctx->has_been_current = TRUE;
1950 ctx->hdc = draw_hdc;
1951 ctx->drawables[0] = draw_gl->drawable;
1952 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1953 ctx->refresh_drawables = FALSE;
1954 NtCurrentTeb()->glContext = ctx;
1955 goto done;
1958 SetLastError( ERROR_INVALID_HANDLE );
1959 done:
1960 release_gl_drawable( read_gl );
1961 release_gl_drawable( draw_gl );
1962 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1963 return ret;
1966 /***********************************************************************
1967 * glxdrv_wglShareLists
1969 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1971 TRACE("(%p, %p)\n", org, dest);
1973 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1974 * at context creation time but in case of WGL it is done using wglShareLists.
1975 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1976 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1977 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1978 * so there delaying context creation doesn't work.
1980 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1981 * and when a program requests sharing we recreate the destination context if it hasn't been made
1982 * current or when it hasn't shared display lists before.
1985 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1987 ERR("Could not share display lists, one of the contexts has been current already !\n");
1988 return FALSE;
1990 else if(dest->sharing)
1992 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1993 return FALSE;
1995 else
1997 /* Re-create the GLX context and share display lists */
1998 pglXDestroyContext(gdi_display, dest->ctx);
1999 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
2000 TRACE(" re-created context (%p) for Wine context %p (%s) sharing lists with ctx %p (%s)\n",
2001 dest->ctx, dest, debugstr_fbconfig(dest->fmt->fbconfig),
2002 org->ctx, debugstr_fbconfig( org->fmt->fbconfig));
2004 org->sharing = TRUE;
2005 dest->sharing = TRUE;
2006 return TRUE;
2008 return FALSE;
2011 static void wglFinish(void)
2013 struct x11drv_escape_flush_gl_drawable escape;
2014 struct gl_drawable *gl;
2015 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2017 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
2018 escape.gl_drawable = 0;
2020 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
2022 switch (gl->type)
2024 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
2025 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
2026 default: break;
2028 sync_context(ctx);
2029 release_gl_drawable( gl );
2032 pglFinish();
2033 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2036 static void wglFlush(void)
2038 struct x11drv_escape_flush_gl_drawable escape;
2039 struct gl_drawable *gl;
2040 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2042 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
2043 escape.gl_drawable = 0;
2045 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
2047 switch (gl->type)
2049 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
2050 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
2051 default: break;
2053 sync_context(ctx);
2054 release_gl_drawable( gl );
2057 pglFlush();
2058 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2061 static const GLubyte *wglGetString(GLenum name)
2063 if (name == GL_EXTENSIONS && WineGLInfo.glExtensions)
2064 return (const GLubyte *)WineGLInfo.glExtensions;
2065 return pglGetString(name);
2068 /***********************************************************************
2069 * X11DRV_wglCreateContextAttribsARB
2071 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2072 const int* attribList )
2074 struct wgl_context *ret;
2075 struct gl_drawable *gl;
2076 int err = 0;
2078 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2080 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2082 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2083 return NULL;
2086 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2088 ret->hdc = hdc;
2089 ret->fmt = gl->format;
2090 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2091 ret->gl3_context = TRUE;
2092 if (attribList)
2094 int *pContextAttribList = &ret->attribList[0];
2095 /* attribList consists of pairs {token, value] terminated with 0 */
2096 while(attribList[0] != 0)
2098 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2099 switch(attribList[0])
2101 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2102 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2103 pContextAttribList[1] = attribList[1];
2104 pContextAttribList += 2;
2105 ret->numAttribs++;
2106 break;
2107 case WGL_CONTEXT_MINOR_VERSION_ARB:
2108 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2109 pContextAttribList[1] = attribList[1];
2110 pContextAttribList += 2;
2111 ret->numAttribs++;
2112 break;
2113 case WGL_CONTEXT_LAYER_PLANE_ARB:
2114 break;
2115 case WGL_CONTEXT_FLAGS_ARB:
2116 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2117 pContextAttribList[1] = attribList[1];
2118 pContextAttribList += 2;
2119 ret->numAttribs++;
2120 break;
2121 case WGL_CONTEXT_PROFILE_MASK_ARB:
2122 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2123 pContextAttribList[1] = attribList[1];
2124 pContextAttribList += 2;
2125 ret->numAttribs++;
2126 break;
2127 case WGL_RENDERER_ID_WINE:
2128 pContextAttribList[0] = GLX_RENDERER_ID_MESA;
2129 pContextAttribList[1] = attribList[1];
2130 pContextAttribList += 2;
2131 ret->numAttribs++;
2132 break;
2133 default:
2134 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2136 attribList += 2;
2140 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2141 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2142 XSync(gdi_display, False);
2143 if ((err = X11DRV_check_error()) || !ret->ctx)
2145 /* In the future we should convert the GLX error to a win32 one here if needed */
2146 ERR("Context creation failed (error %x)\n", err);
2147 HeapFree( GetProcessHeap(), 0, ret );
2148 ret = NULL;
2150 else list_add_head( &context_list, &ret->entry );
2153 release_gl_drawable( gl );
2154 TRACE( "%p -> %p\n", hdc, ret );
2155 return ret;
2159 * X11DRV_wglGetExtensionsStringARB
2161 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2163 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2165 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2166 return WineGLInfo.wglExtensions;
2170 * X11DRV_wglCreatePbufferARB
2172 * WGL_ARB_pbuffer: wglCreatePbufferARB
2174 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2175 const int *piAttribList )
2177 struct wgl_pbuffer* object;
2178 const struct wgl_pixel_format *fmt;
2179 int attribs[256];
2180 int nAttribs = 0;
2182 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2184 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2185 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2186 if(!fmt) {
2187 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2188 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2189 return NULL;
2192 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2193 if (NULL == object) {
2194 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2195 return NULL;
2197 object->width = iWidth;
2198 object->height = iHeight;
2199 object->fmt = fmt;
2201 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2202 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2203 while (piAttribList && 0 != *piAttribList) {
2204 int attr_v;
2205 switch (*piAttribList) {
2206 case WGL_PBUFFER_LARGEST_ARB: {
2207 ++piAttribList;
2208 attr_v = *piAttribList;
2209 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2210 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2211 break;
2214 case WGL_TEXTURE_FORMAT_ARB: {
2215 ++piAttribList;
2216 attr_v = *piAttribList;
2217 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2218 if (WGL_NO_TEXTURE_ARB == attr_v) {
2219 object->use_render_texture = 0;
2220 } else {
2221 if (!use_render_texture_emulation) {
2222 SetLastError(ERROR_INVALID_DATA);
2223 goto create_failed;
2225 switch (attr_v) {
2226 case WGL_TEXTURE_RGB_ARB:
2227 object->use_render_texture = GL_RGB;
2228 object->texture_bpp = 3;
2229 object->texture_format = GL_RGB;
2230 object->texture_type = GL_UNSIGNED_BYTE;
2231 break;
2232 case WGL_TEXTURE_RGBA_ARB:
2233 object->use_render_texture = GL_RGBA;
2234 object->texture_bpp = 4;
2235 object->texture_format = GL_RGBA;
2236 object->texture_type = GL_UNSIGNED_BYTE;
2237 break;
2239 /* WGL_FLOAT_COMPONENTS_NV */
2240 case WGL_TEXTURE_FLOAT_R_NV:
2241 object->use_render_texture = GL_FLOAT_R_NV;
2242 object->texture_bpp = 4;
2243 object->texture_format = GL_RED;
2244 object->texture_type = GL_FLOAT;
2245 break;
2246 case WGL_TEXTURE_FLOAT_RG_NV:
2247 object->use_render_texture = GL_FLOAT_RG_NV;
2248 object->texture_bpp = 8;
2249 object->texture_format = GL_LUMINANCE_ALPHA;
2250 object->texture_type = GL_FLOAT;
2251 break;
2252 case WGL_TEXTURE_FLOAT_RGB_NV:
2253 object->use_render_texture = GL_FLOAT_RGB_NV;
2254 object->texture_bpp = 12;
2255 object->texture_format = GL_RGB;
2256 object->texture_type = GL_FLOAT;
2257 break;
2258 case WGL_TEXTURE_FLOAT_RGBA_NV:
2259 object->use_render_texture = GL_FLOAT_RGBA_NV;
2260 object->texture_bpp = 16;
2261 object->texture_format = GL_RGBA;
2262 object->texture_type = GL_FLOAT;
2263 break;
2264 default:
2265 ERR("Unknown texture format: %x\n", attr_v);
2266 SetLastError(ERROR_INVALID_DATA);
2267 goto create_failed;
2270 break;
2273 case WGL_TEXTURE_TARGET_ARB: {
2274 ++piAttribList;
2275 attr_v = *piAttribList;
2276 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2277 if (WGL_NO_TEXTURE_ARB == attr_v) {
2278 object->texture_target = 0;
2279 } else {
2280 if (!use_render_texture_emulation) {
2281 SetLastError(ERROR_INVALID_DATA);
2282 goto create_failed;
2284 switch (attr_v) {
2285 case WGL_TEXTURE_CUBE_MAP_ARB: {
2286 if (iWidth != iHeight) {
2287 SetLastError(ERROR_INVALID_DATA);
2288 goto create_failed;
2290 object->texture_target = GL_TEXTURE_CUBE_MAP;
2291 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2292 break;
2294 case WGL_TEXTURE_1D_ARB: {
2295 if (1 != iHeight) {
2296 SetLastError(ERROR_INVALID_DATA);
2297 goto create_failed;
2299 object->texture_target = GL_TEXTURE_1D;
2300 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2301 break;
2303 case WGL_TEXTURE_2D_ARB: {
2304 object->texture_target = GL_TEXTURE_2D;
2305 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2306 break;
2308 case WGL_TEXTURE_RECTANGLE_NV: {
2309 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2310 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2311 break;
2313 default:
2314 ERR("Unknown texture target: %x\n", attr_v);
2315 SetLastError(ERROR_INVALID_DATA);
2316 goto create_failed;
2319 break;
2322 case WGL_MIPMAP_TEXTURE_ARB: {
2323 ++piAttribList;
2324 attr_v = *piAttribList;
2325 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2326 if (!use_render_texture_emulation) {
2327 SetLastError(ERROR_INVALID_DATA);
2328 goto create_failed;
2330 break;
2333 ++piAttribList;
2336 PUSH1(attribs, None);
2337 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2338 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2339 if (!object->drawable) {
2340 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2341 goto create_failed; /* unexpected error */
2343 EnterCriticalSection( &context_section );
2344 list_add_head( &pbuffer_list, &object->entry );
2345 LeaveCriticalSection( &context_section );
2346 TRACE("->(%p)\n", object);
2347 return object;
2349 create_failed:
2350 HeapFree(GetProcessHeap(), 0, object);
2351 TRACE("->(FAILED)\n");
2352 return NULL;
2356 * X11DRV_wglDestroyPbufferARB
2358 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2360 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2362 TRACE("(%p)\n", object);
2364 EnterCriticalSection( &context_section );
2365 list_remove( &object->entry );
2366 LeaveCriticalSection( &context_section );
2367 pglXDestroyPbuffer(gdi_display, object->drawable);
2368 if (object->tmp_context)
2369 pglXDestroyContext(gdi_display, object->tmp_context);
2370 HeapFree(GetProcessHeap(), 0, object);
2371 return GL_TRUE;
2375 * X11DRV_wglGetPbufferDCARB
2377 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2379 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2381 struct x11drv_escape_set_drawable escape;
2382 struct gl_drawable *gl, *prev;
2383 HDC hdc;
2385 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2386 if (!hdc) return 0;
2388 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2390 DeleteDC( hdc );
2391 return 0;
2393 gl->type = DC_GL_PBUFFER;
2394 gl->drawable = object->drawable;
2395 gl->format = object->fmt;
2397 EnterCriticalSection( &context_section );
2398 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2399 free_gl_drawable( prev );
2400 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2401 LeaveCriticalSection( &context_section );
2403 escape.code = X11DRV_SET_DRAWABLE;
2404 escape.drawable = object->drawable;
2405 escape.mode = IncludeInferiors;
2406 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2407 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2409 TRACE( "(%p)->(%p)\n", object, hdc );
2410 return hdc;
2414 * X11DRV_wglQueryPbufferARB
2416 * WGL_ARB_pbuffer: wglQueryPbufferARB
2418 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2420 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2422 switch (iAttribute) {
2423 case WGL_PBUFFER_WIDTH_ARB:
2424 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2425 break;
2426 case WGL_PBUFFER_HEIGHT_ARB:
2427 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2428 break;
2430 case WGL_PBUFFER_LOST_ARB:
2431 /* GLX Pbuffers cannot be lost by default. We can support this by
2432 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2433 * to receive pixel buffer clobber events, however that may or may
2434 * not give any benefit */
2435 *piValue = GL_FALSE;
2436 break;
2438 case WGL_TEXTURE_FORMAT_ARB:
2439 if (!object->use_render_texture) {
2440 *piValue = WGL_NO_TEXTURE_ARB;
2441 } else {
2442 if (!use_render_texture_emulation) {
2443 SetLastError(ERROR_INVALID_HANDLE);
2444 return GL_FALSE;
2446 switch(object->use_render_texture) {
2447 case GL_RGB:
2448 *piValue = WGL_TEXTURE_RGB_ARB;
2449 break;
2450 case GL_RGBA:
2451 *piValue = WGL_TEXTURE_RGBA_ARB;
2452 break;
2453 /* WGL_FLOAT_COMPONENTS_NV */
2454 case GL_FLOAT_R_NV:
2455 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2456 break;
2457 case GL_FLOAT_RG_NV:
2458 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2459 break;
2460 case GL_FLOAT_RGB_NV:
2461 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2462 break;
2463 case GL_FLOAT_RGBA_NV:
2464 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2465 break;
2466 default:
2467 ERR("Unknown texture format: %x\n", object->use_render_texture);
2470 break;
2472 case WGL_TEXTURE_TARGET_ARB:
2473 if (!object->texture_target){
2474 *piValue = WGL_NO_TEXTURE_ARB;
2475 } else {
2476 if (!use_render_texture_emulation) {
2477 SetLastError(ERROR_INVALID_DATA);
2478 return GL_FALSE;
2480 switch (object->texture_target) {
2481 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2482 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2483 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2484 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2487 break;
2489 case WGL_MIPMAP_TEXTURE_ARB:
2490 *piValue = GL_FALSE; /** don't support that */
2491 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2492 break;
2494 default:
2495 FIXME("unexpected attribute %x\n", iAttribute);
2496 break;
2499 return GL_TRUE;
2503 * X11DRV_wglReleasePbufferDCARB
2505 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2507 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2509 struct gl_drawable *gl;
2511 TRACE("(%p, %p)\n", object, hdc);
2513 EnterCriticalSection( &context_section );
2515 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2517 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2518 free_gl_drawable( gl );
2520 else hdc = 0;
2522 LeaveCriticalSection( &context_section );
2524 return hdc && DeleteDC(hdc);
2528 * X11DRV_wglSetPbufferAttribARB
2530 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2532 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2534 GLboolean ret = GL_FALSE;
2536 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2538 if (!object->use_render_texture) {
2539 SetLastError(ERROR_INVALID_HANDLE);
2540 return GL_FALSE;
2542 if (use_render_texture_emulation) {
2543 return GL_TRUE;
2545 return ret;
2549 * X11DRV_wglChoosePixelFormatARB
2551 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2553 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2554 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2556 int attribs[256];
2557 int nAttribs = 0;
2558 GLXFBConfig* cfgs;
2559 int nCfgs = 0;
2560 int it;
2561 int fmt_id;
2562 int start, end;
2563 UINT pfmt_it = 0;
2564 int run;
2565 int i;
2566 DWORD dwFlags = 0;
2568 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2569 if (NULL != pfAttribFList) {
2570 FIXME("unused pfAttribFList\n");
2573 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2574 if (-1 == nAttribs) {
2575 WARN("Cannot convert WGL to GLX attributes\n");
2576 return GL_FALSE;
2578 PUSH1(attribs, None);
2580 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2581 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2582 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2584 for(i=0; piAttribIList[i] != 0; i+=2)
2586 switch(piAttribIList[i])
2588 case WGL_DRAW_TO_BITMAP_ARB:
2589 if(piAttribIList[i+1])
2590 dwFlags |= PFD_DRAW_TO_BITMAP;
2591 break;
2592 case WGL_ACCELERATION_ARB:
2593 switch(piAttribIList[i+1])
2595 case WGL_NO_ACCELERATION_ARB:
2596 dwFlags |= PFD_GENERIC_FORMAT;
2597 break;
2598 case WGL_GENERIC_ACCELERATION_ARB:
2599 dwFlags |= PFD_GENERIC_ACCELERATED;
2600 break;
2601 case WGL_FULL_ACCELERATION_ARB:
2602 /* Nothing to do */
2603 break;
2605 break;
2606 case WGL_SUPPORT_GDI_ARB:
2607 if(piAttribIList[i+1])
2608 dwFlags |= PFD_SUPPORT_GDI;
2609 break;
2613 /* Search for FB configurations matching the requirements in attribs */
2614 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2615 if (NULL == cfgs) {
2616 WARN("Compatible Pixel Format not found\n");
2617 return GL_FALSE;
2620 /* Loop through all matching formats and check if they are suitable.
2621 * Note that this function should at max return nMaxFormats different formats */
2622 for(run=0; run < 2; run++)
2624 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2626 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2628 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2629 continue;
2632 /* During the first run we only want onscreen formats and during the second only offscreen */
2633 start = run == 1 ? nb_onscreen_formats : 0;
2634 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2636 for (i = start; i < end; i++)
2638 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2640 piFormats[pfmt_it++] = i + 1;
2641 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2642 it + 1, nCfgs, fmt_id, i + 1);
2643 break;
2649 *nNumFormats = pfmt_it;
2650 /** free list */
2651 XFree(cfgs);
2652 return GL_TRUE;
2656 * X11DRV_wglGetPixelFormatAttribivARB
2658 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2660 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2661 UINT nAttributes, const int *piAttributes, int *piValues )
2663 UINT i;
2664 const struct wgl_pixel_format *fmt;
2665 int hTest;
2666 int tmp;
2667 int curGLXAttr = 0;
2669 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2671 if (0 < iLayerPlane) {
2672 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2673 return GL_FALSE;
2676 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2677 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2678 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2679 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2680 if(!fmt) {
2681 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2684 for (i = 0; i < nAttributes; ++i) {
2685 const int curWGLAttr = piAttributes[i];
2686 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2688 switch (curWGLAttr) {
2689 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2690 piValues[i] = nb_pixel_formats;
2691 continue;
2693 case WGL_SUPPORT_OPENGL_ARB:
2694 piValues[i] = GL_TRUE;
2695 continue;
2697 case WGL_ACCELERATION_ARB:
2698 curGLXAttr = GLX_CONFIG_CAVEAT;
2699 if (!fmt) goto pix_error;
2700 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2701 piValues[i] = WGL_NO_ACCELERATION_ARB;
2702 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2703 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2704 else
2705 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2706 continue;
2708 case WGL_TRANSPARENT_ARB:
2709 curGLXAttr = GLX_TRANSPARENT_TYPE;
2710 if (!fmt) goto pix_error;
2711 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2712 if (hTest) goto get_error;
2713 piValues[i] = GL_FALSE;
2714 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2715 continue;
2717 case WGL_PIXEL_TYPE_ARB:
2718 curGLXAttr = GLX_RENDER_TYPE;
2719 if (!fmt) goto pix_error;
2720 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2721 if (hTest) goto get_error;
2722 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2723 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2724 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2725 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2726 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2727 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2728 else {
2729 ERR("unexpected RenderType(%x)\n", tmp);
2730 piValues[i] = WGL_TYPE_RGBA_ARB;
2732 continue;
2734 case WGL_COLOR_BITS_ARB:
2735 curGLXAttr = GLX_BUFFER_SIZE;
2736 break;
2738 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2739 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2740 if (!use_render_texture_emulation) {
2741 piValues[i] = GL_FALSE;
2742 continue;
2744 curGLXAttr = GLX_RENDER_TYPE;
2745 if (!fmt) goto pix_error;
2746 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2747 if (hTest) goto get_error;
2748 if (GLX_COLOR_INDEX_BIT == tmp) {
2749 piValues[i] = GL_FALSE;
2750 continue;
2752 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2753 if (hTest) goto get_error;
2754 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2755 continue;
2757 case WGL_BLUE_BITS_ARB:
2758 curGLXAttr = GLX_BLUE_SIZE;
2759 break;
2760 case WGL_RED_BITS_ARB:
2761 curGLXAttr = GLX_RED_SIZE;
2762 break;
2763 case WGL_GREEN_BITS_ARB:
2764 curGLXAttr = GLX_GREEN_SIZE;
2765 break;
2766 case WGL_ALPHA_BITS_ARB:
2767 curGLXAttr = GLX_ALPHA_SIZE;
2768 break;
2769 case WGL_DEPTH_BITS_ARB:
2770 curGLXAttr = GLX_DEPTH_SIZE;
2771 break;
2772 case WGL_STENCIL_BITS_ARB:
2773 curGLXAttr = GLX_STENCIL_SIZE;
2774 break;
2775 case WGL_DOUBLE_BUFFER_ARB:
2776 curGLXAttr = GLX_DOUBLEBUFFER;
2777 break;
2778 case WGL_STEREO_ARB:
2779 curGLXAttr = GLX_STEREO;
2780 break;
2781 case WGL_AUX_BUFFERS_ARB:
2782 curGLXAttr = GLX_AUX_BUFFERS;
2783 break;
2785 case WGL_SUPPORT_GDI_ARB:
2786 if (!fmt) goto pix_error;
2787 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2788 continue;
2790 case WGL_DRAW_TO_BITMAP_ARB:
2791 if (!fmt) goto pix_error;
2792 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2793 continue;
2795 case WGL_DRAW_TO_WINDOW_ARB:
2796 case WGL_DRAW_TO_PBUFFER_ARB:
2797 if (!fmt) goto pix_error;
2798 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2799 if (hTest) goto get_error;
2800 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2801 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2802 piValues[i] = GL_TRUE;
2803 else
2804 piValues[i] = GL_FALSE;
2805 continue;
2807 case WGL_SWAP_METHOD_ARB:
2808 if (has_swap_method)
2810 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_SWAP_METHOD_OML, &tmp);
2811 if (hTest) goto get_error;
2812 switch (tmp)
2814 case GLX_SWAP_EXCHANGE_OML:
2815 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2816 break;
2817 case GLX_SWAP_COPY_OML:
2818 piValues[i] = WGL_SWAP_COPY_ARB;
2819 break;
2820 case GLX_SWAP_UNDEFINED_OML:
2821 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2822 break;
2823 default:
2824 ERR("Unexpected swap method %x.\n", tmp);
2827 else
2829 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2830 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2832 continue;
2834 case WGL_PBUFFER_LARGEST_ARB:
2835 curGLXAttr = GLX_LARGEST_PBUFFER;
2836 break;
2838 case WGL_SAMPLE_BUFFERS_ARB:
2839 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2840 break;
2842 case WGL_SAMPLES_ARB:
2843 curGLXAttr = GLX_SAMPLES_ARB;
2844 break;
2846 case WGL_FLOAT_COMPONENTS_NV:
2847 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2848 break;
2850 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2851 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2852 break;
2854 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2855 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2856 break;
2858 case WGL_ACCUM_RED_BITS_ARB:
2859 curGLXAttr = GLX_ACCUM_RED_SIZE;
2860 break;
2861 case WGL_ACCUM_GREEN_BITS_ARB:
2862 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2863 break;
2864 case WGL_ACCUM_BLUE_BITS_ARB:
2865 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2866 break;
2867 case WGL_ACCUM_ALPHA_BITS_ARB:
2868 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2869 break;
2870 case WGL_ACCUM_BITS_ARB:
2871 if (!fmt) goto pix_error;
2872 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2873 if (hTest) goto get_error;
2874 piValues[i] = tmp;
2875 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2876 if (hTest) goto get_error;
2877 piValues[i] += tmp;
2878 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2879 if (hTest) goto get_error;
2880 piValues[i] += tmp;
2881 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2882 if (hTest) goto get_error;
2883 piValues[i] += tmp;
2884 continue;
2886 default:
2887 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2890 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2891 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2892 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2894 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2895 * and check which options can work using iPixelFormat=0 and which not.
2896 * A problem would be that this function is an extension. This would
2897 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2899 if (0 != curGLXAttr && iPixelFormat != 0) {
2900 if (!fmt) goto pix_error;
2901 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2902 if (hTest) goto get_error;
2903 curGLXAttr = 0;
2904 } else {
2905 piValues[i] = GL_FALSE;
2908 return GL_TRUE;
2910 get_error:
2911 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2912 return GL_FALSE;
2914 pix_error:
2915 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2916 return GL_FALSE;
2920 * X11DRV_wglGetPixelFormatAttribfvARB
2922 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2924 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2925 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2927 int *attr;
2928 int ret;
2929 UINT i;
2931 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2933 /* Allocate a temporary array to store integer values */
2934 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2935 if (!attr) {
2936 ERR("couldn't allocate %d array\n", nAttributes);
2937 return GL_FALSE;
2940 /* Piggy-back on wglGetPixelFormatAttribivARB */
2941 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2942 if (ret) {
2943 /* Convert integer values to float. Should also check for attributes
2944 that can give decimal values here */
2945 for (i=0; i<nAttributes;i++) {
2946 pfValues[i] = attr[i];
2950 HeapFree(GetProcessHeap(), 0, attr);
2951 return ret;
2955 * X11DRV_wglBindTexImageARB
2957 * WGL_ARB_render_texture: wglBindTexImageARB
2959 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2961 GLboolean ret = GL_FALSE;
2963 TRACE("(%p, %d)\n", object, iBuffer);
2965 if (!object->use_render_texture) {
2966 SetLastError(ERROR_INVALID_HANDLE);
2967 return GL_FALSE;
2970 if (use_render_texture_emulation) {
2971 static BOOL initialized = FALSE;
2972 int prev_binded_texture = 0;
2973 GLXContext prev_context;
2974 GLXDrawable prev_drawable;
2976 prev_context = pglXGetCurrentContext();
2977 prev_drawable = pglXGetCurrentDrawable();
2979 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2980 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2981 isn't ideal performance wise but I wasn't able to get other ways working.
2983 if(!initialized) {
2984 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2985 FIXME("partial stub!\n");
2988 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
2989 if (!object->tmp_context || object->prev_context != prev_context) {
2990 if (object->tmp_context)
2991 pglXDestroyContext(gdi_display, object->tmp_context);
2992 object->tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2993 object->prev_context = prev_context;
2996 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2998 /* Switch to our pbuffer */
2999 pglXMakeCurrent(gdi_display, object->drawable, object->tmp_context);
3001 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3002 * After that copy the pbuffer texture data. */
3003 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
3004 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3006 /* Switch back to the original drawable and context */
3007 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
3008 return GL_TRUE;
3011 return ret;
3015 * X11DRV_wglReleaseTexImageARB
3017 * WGL_ARB_render_texture: wglReleaseTexImageARB
3019 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
3021 GLboolean ret = GL_FALSE;
3023 TRACE("(%p, %d)\n", object, iBuffer);
3025 if (!object->use_render_texture) {
3026 SetLastError(ERROR_INVALID_HANDLE);
3027 return GL_FALSE;
3029 if (use_render_texture_emulation) {
3030 return GL_TRUE;
3032 return ret;
3036 * X11DRV_wglGetExtensionsStringEXT
3038 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3040 static const char *X11DRV_wglGetExtensionsStringEXT(void)
3042 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3043 return WineGLInfo.wglExtensions;
3047 * X11DRV_wglGetSwapIntervalEXT
3049 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3051 static int X11DRV_wglGetSwapIntervalEXT(void)
3053 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3054 struct gl_drawable *gl;
3055 int swap_interval;
3057 TRACE("()\n");
3059 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3061 /* This can't happen because a current WGL context is required to get
3062 * here. Likely the application is buggy.
3064 WARN("No GL drawable found, returning swap interval 0\n");
3065 return 0;
3068 swap_interval = gl->swap_interval;
3069 release_gl_drawable(gl);
3071 return swap_interval;
3075 * X11DRV_wglSwapIntervalEXT
3077 * WGL_EXT_swap_control: wglSwapIntervalEXT
3079 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3081 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3082 struct gl_drawable *gl;
3083 BOOL ret;
3085 TRACE("(%d)\n", interval);
3087 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3088 * is invalid.
3090 if (interval < 0 && !has_swap_control_tear)
3092 SetLastError(ERROR_INVALID_DATA);
3093 return FALSE;
3096 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3098 SetLastError(ERROR_DC_NOT_FOUND);
3099 return FALSE;
3102 ret = set_swap_interval(gl->drawable, interval);
3103 gl->refresh_swap_interval = FALSE;
3104 if (ret)
3105 gl->swap_interval = interval;
3106 else
3107 SetLastError(ERROR_DC_NOT_FOUND);
3109 release_gl_drawable(gl);
3111 return ret;
3115 * X11DRV_wglSetPixelFormatWINE
3117 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3118 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3120 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3122 return set_pixel_format(hdc, format, TRUE);
3125 static BOOL X11DRV_wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
3127 return pglXQueryCurrentRendererIntegerMESA( attribute, value );
3130 static const char *X11DRV_wglQueryCurrentRendererStringWINE( GLenum attribute )
3132 return pglXQueryCurrentRendererStringMESA( attribute );
3135 static BOOL X11DRV_wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
3137 return pglXQueryRendererIntegerMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute, value );
3140 static const char *X11DRV_wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
3142 return pglXQueryRendererStringMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute );
3146 * glxRequireVersion (internal)
3148 * Check if the supported GLX version matches requiredVersion.
3150 static BOOL glxRequireVersion(int requiredVersion)
3152 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3153 if(requiredVersion <= WineGLInfo.glxVersion[1])
3154 return TRUE;
3156 return FALSE;
3159 static void register_extension(const char *ext)
3161 if (WineGLInfo.wglExtensions[0])
3162 strcat(WineGLInfo.wglExtensions, " ");
3163 strcat(WineGLInfo.wglExtensions, ext);
3165 TRACE("'%s'\n", ext);
3169 * X11DRV_WineGL_LoadExtensions
3171 static void X11DRV_WineGL_LoadExtensions(void)
3173 WineGLInfo.wglExtensions[0] = 0;
3175 /* ARB Extensions */
3177 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
3179 register_extension( "WGL_ARB_create_context" );
3180 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3182 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
3183 register_extension("WGL_ARB_create_context_profile");
3186 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
3188 register_extension("WGL_ARB_pixel_format_float");
3189 register_extension("WGL_ATI_pixel_format_float");
3192 register_extension( "WGL_ARB_extensions_string" );
3193 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3195 if (glxRequireVersion(3))
3197 register_extension( "WGL_ARB_make_current_read" );
3198 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3199 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3202 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3204 if (glxRequireVersion(3))
3206 register_extension( "WGL_ARB_pbuffer" );
3207 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3208 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3209 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3210 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3211 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3212 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3215 register_extension( "WGL_ARB_pixel_format" );
3216 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3217 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3218 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3220 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3221 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3222 (glxRequireVersion(3) && use_render_texture_emulation))
3224 register_extension( "WGL_ARB_render_texture" );
3225 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3226 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3228 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3229 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3230 register_extension("WGL_NV_float_buffer");
3232 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3233 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3234 register_extension("WGL_NV_render_texture_rectangle");
3237 /* EXT Extensions */
3239 register_extension( "WGL_EXT_extensions_string" );
3240 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3242 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3243 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3244 register_extension( "WGL_EXT_swap_control" );
3245 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3246 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3248 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3249 register_extension("WGL_EXT_framebuffer_sRGB");
3251 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3252 register_extension("WGL_EXT_pixel_format_packed_float");
3254 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3256 swap_control_method = GLX_SWAP_CONTROL_EXT;
3257 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control_tear"))
3259 register_extension("WGL_EXT_swap_control_tear");
3260 has_swap_control_tear = TRUE;
3263 else if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_swap_control"))
3265 swap_control_method = GLX_SWAP_CONTROL_MESA;
3267 else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGI_swap_control"))
3269 swap_control_method = GLX_SWAP_CONTROL_SGI;
3272 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3273 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3275 register_extension( "WGL_NV_vertex_array_range" );
3276 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3277 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3280 if (has_extension(WineGLInfo.glxExtensions, "GLX_OML_swap_method"))
3281 has_swap_method = TRUE;
3283 /* WINE-specific WGL Extensions */
3285 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3286 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3288 register_extension( "WGL_WINE_pixel_format_passthrough" );
3289 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3291 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_query_renderer" ))
3293 register_extension( "WGL_WINE_query_renderer" );
3294 opengl_funcs.ext.p_wglQueryCurrentRendererIntegerWINE = X11DRV_wglQueryCurrentRendererIntegerWINE;
3295 opengl_funcs.ext.p_wglQueryCurrentRendererStringWINE = X11DRV_wglQueryCurrentRendererStringWINE;
3296 opengl_funcs.ext.p_wglQueryRendererIntegerWINE = X11DRV_wglQueryRendererIntegerWINE;
3297 opengl_funcs.ext.p_wglQueryRendererStringWINE = X11DRV_wglQueryRendererStringWINE;
3303 * glxdrv_SwapBuffers
3305 * Swap the buffers of this DC
3307 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3309 struct x11drv_escape_flush_gl_drawable escape;
3310 struct gl_drawable *gl;
3311 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3312 INT64 ust, msc, sbc, target_sbc = 0;
3314 TRACE("(%p)\n", hdc);
3316 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3317 escape.gl_drawable = 0;
3319 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3321 SetLastError( ERROR_INVALID_HANDLE );
3322 return FALSE;
3325 if (gl->refresh_swap_interval)
3327 set_swap_interval(gl->drawable, gl->swap_interval);
3328 gl->refresh_swap_interval = FALSE;
3331 switch (gl->type)
3333 case DC_GL_PIXMAP_WIN:
3334 if (ctx) sync_context( ctx );
3335 escape.gl_drawable = gl->pixmap;
3336 if (pglXCopySubBufferMESA) {
3337 /* (glX)SwapBuffers has an implicit glFlush effect, however
3338 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3339 * copying */
3340 pglFlush();
3341 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3342 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3343 break;
3345 if (pglXSwapBuffersMscOML)
3347 pglFlush();
3348 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3349 break;
3351 pglXSwapBuffers(gdi_display, gl->drawable);
3352 break;
3353 case DC_GL_CHILD_WIN:
3354 if (ctx) sync_context( ctx );
3355 escape.gl_drawable = gl->window;
3356 /* fall through */
3357 default:
3358 if (escape.gl_drawable && pglXSwapBuffersMscOML)
3360 pglFlush();
3361 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3362 break;
3364 pglXSwapBuffers(gdi_display, gl->drawable);
3365 break;
3368 if (escape.gl_drawable && pglXWaitForSbcOML)
3369 pglXWaitForSbcOML( gdi_display, gl->drawable, target_sbc, &ust, &msc, &sbc );
3371 release_gl_drawable( gl );
3373 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3374 return TRUE;
3377 static struct opengl_funcs opengl_funcs =
3380 glxdrv_wglCopyContext, /* p_wglCopyContext */
3381 glxdrv_wglCreateContext, /* p_wglCreateContext */
3382 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3383 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3384 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3385 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3386 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3387 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3388 glxdrv_wglShareLists, /* p_wglShareLists */
3389 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3393 struct opengl_funcs *get_glx_driver( UINT version )
3395 if (version != WINE_WGL_DRIVER_VERSION)
3397 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3398 return NULL;
3400 if (has_opengl()) return &opengl_funcs;
3401 return NULL;
3404 #else /* no OpenGL includes */
3406 struct opengl_funcs *get_glx_driver( UINT version )
3408 return NULL;
3411 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3415 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3419 void destroy_gl_drawable( HWND hwnd )
3423 #endif /* defined(SONAME_LIBGL) */