include: Add CertRegisterSystemStore and CertUnregisterSystemStore.
[wine.git] / dlls / winex11.drv / opengl.c
blob7401288d9c135844248cade8ea9f9ed9622e6cf9
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_no_error */
167 #define GLX_CONTEXT_OPENGL_NO_ERROR_ARB 0x31B3
168 /** GLX_ARB_create_context_profile */
169 #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
170 /** GLX_ATI_pixel_format_float */
171 #define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
172 /** GLX_ARB_pixel_format_float */
173 #define GLX_RGBA_FLOAT_BIT 0x00000004
174 #define GLX_RGBA_FLOAT_TYPE 0x20B9
175 /** GLX_MESA_query_renderer */
176 #define GLX_RENDERER_ID_MESA 0x818E
177 /** GLX_NV_float_buffer */
178 #define GLX_FLOAT_COMPONENTS_NV 0x20B0
181 struct WineGLInfo {
182 const char *glVersion;
183 char *glExtensions;
185 int glxVersion[2];
187 const char *glxServerVersion;
188 const char *glxServerVendor;
189 const char *glxServerExtensions;
191 const char *glxClientVersion;
192 const char *glxClientVendor;
193 const char *glxClientExtensions;
195 const char *glxExtensions;
197 BOOL glxDirect;
198 char wglExtensions[4096];
201 struct wgl_pixel_format
203 GLXFBConfig fbconfig;
204 int fmt_id;
205 int render_type;
206 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
209 struct wgl_context
211 HDC hdc;
212 BOOL has_been_current;
213 BOOL sharing;
214 BOOL gl3_context;
215 XVisualInfo *vis;
216 const struct wgl_pixel_format *fmt;
217 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
218 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
219 GLXContext ctx;
220 GLXDrawable drawables[2];
221 BOOL refresh_drawables;
222 struct list entry;
225 struct wgl_pbuffer
227 Drawable drawable;
228 const struct wgl_pixel_format* fmt;
229 int width;
230 int height;
231 int* attribList;
232 int use_render_texture; /* This is also the internal texture format */
233 int texture_bind_target;
234 int texture_bpp;
235 GLint texture_format;
236 GLuint texture_target;
237 GLenum texture_type;
238 GLuint texture;
239 int texture_level;
240 GLXContext tmp_context;
241 GLXContext prev_context;
242 struct list entry;
245 enum dc_gl_type
247 DC_GL_NONE, /* no GL support (pixel format not set yet) */
248 DC_GL_WINDOW, /* normal top-level window */
249 DC_GL_CHILD_WIN, /* child window using XComposite */
250 DC_GL_PIXMAP_WIN, /* child window using intermediate pixmap */
251 DC_GL_PBUFFER /* pseudo memory DC using a PBuffer */
254 struct gl_drawable
256 enum dc_gl_type type; /* type of GL surface */
257 GLXDrawable drawable; /* drawable for rendering with GL */
258 Window window; /* window if drawable is a GLXWindow */
259 Pixmap pixmap; /* base pixmap if drawable is a GLXPixmap */
260 Colormap colormap; /* colormap used for the drawable */
261 const struct wgl_pixel_format *format; /* pixel format for the drawable */
262 XVisualInfo *visual; /* information about the GL visual */
263 RECT rect; /* drawable rect, relative to whole window drawable */
264 int swap_interval;
265 BOOL refresh_swap_interval;
268 enum glx_swap_control_method
270 GLX_SWAP_CONTROL_NONE,
271 GLX_SWAP_CONTROL_EXT,
272 GLX_SWAP_CONTROL_SGI,
273 GLX_SWAP_CONTROL_MESA
276 /* X context to associate a struct gl_drawable to an hwnd */
277 static XContext gl_hwnd_context;
278 /* X context to associate a struct gl_drawable to a pbuffer hdc */
279 static XContext gl_pbuffer_context;
281 static struct list context_list = LIST_INIT( context_list );
282 static struct list pbuffer_list = LIST_INIT( pbuffer_list );
283 static struct WineGLInfo WineGLInfo = { 0 };
284 static struct wgl_pixel_format *pixel_formats;
285 static int nb_pixel_formats, nb_onscreen_formats;
286 static BOOL use_render_texture_emulation = TRUE;
288 /* Selects the preferred GLX swap control method for use by wglSwapIntervalEXT */
289 static enum glx_swap_control_method swap_control_method = GLX_SWAP_CONTROL_NONE;
290 /* Set when GLX_EXT_swap_control_tear is supported, requires GLX_SWAP_CONTROL_EXT */
291 static BOOL has_swap_control_tear = FALSE;
292 static BOOL has_swap_method = FALSE;
294 static CRITICAL_SECTION context_section;
295 static CRITICAL_SECTION_DEBUG critsect_debug =
297 0, 0, &context_section,
298 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
299 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
301 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
303 static const BOOL is_win64 = sizeof(void *) > sizeof(int);
305 static struct opengl_funcs opengl_funcs;
307 #define USE_GL_FUNC(name) #name,
308 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
309 #undef USE_GL_FUNC
311 static void X11DRV_WineGL_LoadExtensions(void);
312 static void init_pixel_formats( Display *display );
313 static BOOL glxRequireVersion(int requiredVersion);
315 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
316 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
317 TRACE(" - dwFlags : ");
318 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
319 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
320 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
321 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
322 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
323 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
324 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
325 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
326 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
327 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
328 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
329 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
330 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
331 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
332 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
333 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
334 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
335 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
336 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
337 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
338 #undef TEST_AND_DUMP
339 TRACE("\n");
341 TRACE(" - iPixelType : ");
342 switch (ppfd->iPixelType) {
343 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
344 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
346 TRACE("\n");
348 TRACE(" - Color : %d\n", ppfd->cColorBits);
349 TRACE(" - Red : %d\n", ppfd->cRedBits);
350 TRACE(" - Green : %d\n", ppfd->cGreenBits);
351 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
352 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
353 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
354 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
355 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
356 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
358 TRACE(" - iLayerType : ");
359 switch (ppfd->iLayerType) {
360 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
361 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
362 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
364 TRACE("\n");
367 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
368 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
370 /* GLX 1.0 */
371 static XVisualInfo* (*pglXChooseVisual)( Display *dpy, int screen, int *attribList );
372 static GLXContext (*pglXCreateContext)( Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct );
373 static void (*pglXDestroyContext)( Display *dpy, GLXContext ctx );
374 static Bool (*pglXMakeCurrent)( Display *dpy, GLXDrawable drawable, GLXContext ctx);
375 static void (*pglXCopyContext)( Display *dpy, GLXContext src, GLXContext dst, unsigned long mask );
376 static void (*pglXSwapBuffers)( Display *dpy, GLXDrawable drawable );
377 static Bool (*pglXQueryExtension)( Display *dpy, int *errorb, int *event );
378 static Bool (*pglXQueryVersion)( Display *dpy, int *maj, int *min );
379 static Bool (*pglXIsDirect)( Display *dpy, GLXContext ctx );
380 static int (*pglXGetConfig)( Display *dpy, XVisualInfo *visual, int attrib, int *value );
381 static GLXContext (*pglXGetCurrentContext)( void );
382 static GLXDrawable (*pglXGetCurrentDrawable)( void );
384 /* GLX 1.1 */
385 static const char *(*pglXQueryExtensionsString)( Display *dpy, int screen );
386 static const char *(*pglXQueryServerString)( Display *dpy, int screen, int name );
387 static const char *(*pglXGetClientString)( Display *dpy, int name );
389 /* GLX 1.3 */
390 static GLXFBConfig *(*pglXChooseFBConfig)( Display *dpy, int screen, const int *attribList, int *nitems );
391 static int (*pglXGetFBConfigAttrib)( Display *dpy, GLXFBConfig config, int attribute, int *value );
392 static GLXFBConfig *(*pglXGetFBConfigs)( Display *dpy, int screen, int *nelements );
393 static XVisualInfo *(*pglXGetVisualFromFBConfig)( Display *dpy, GLXFBConfig config );
394 static GLXPbuffer (*pglXCreatePbuffer)( Display *dpy, GLXFBConfig config, const int *attribList );
395 static void (*pglXDestroyPbuffer)( Display *dpy, GLXPbuffer pbuf );
396 static void (*pglXQueryDrawable)( Display *dpy, GLXDrawable draw, int attribute, unsigned int *value );
397 static GLXContext (*pglXCreateNewContext)( Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct );
398 static Bool (*pglXMakeContextCurrent)( Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx );
399 static GLXPixmap (*pglXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attrib_list );
400 static void (*pglXDestroyPixmap)( Display *dpy, GLXPixmap pixmap );
401 static GLXWindow (*pglXCreateWindow)( Display *dpy, GLXFBConfig config, Window win, const int *attrib_list );
402 static void (*pglXDestroyWindow)( Display *dpy, GLXWindow win );
404 /* GLX Extensions */
405 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
406 static void* (*pglXGetProcAddressARB)(const GLubyte *);
407 static void (*pglXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval);
408 static int (*pglXSwapIntervalSGI)(int);
410 /* NV GLX Extension */
411 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
412 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
414 /* MESA GLX Extensions */
415 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
416 static int (*pglXSwapIntervalMESA)(unsigned int interval);
417 static Bool (*pglXQueryCurrentRendererIntegerMESA)(int attribute, unsigned int *value);
418 static const char *(*pglXQueryCurrentRendererStringMESA)(int attribute);
419 static Bool (*pglXQueryRendererIntegerMESA)(Display *dpy, int screen, int renderer, int attribute, unsigned int *value);
420 static const char *(*pglXQueryRendererStringMESA)(Display *dpy, int screen, int renderer, int attribute);
422 /* OpenML GLX Extensions */
423 static Bool (*pglXWaitForSbcOML)( Display *dpy, GLXDrawable drawable,
424 INT64 target_sbc, INT64 *ust, INT64 *msc, INT64 *sbc );
425 static INT64 (*pglXSwapBuffersMscOML)( Display *dpy, GLXDrawable drawable,
426 INT64 target_msc, INT64 divisor, INT64 remainder );
428 /* Standard OpenGL */
429 static void (*pglFinish)(void);
430 static void (*pglFlush)(void);
431 static const GLubyte *(*pglGetString)(GLenum name);
433 static void wglFinish(void);
434 static void wglFlush(void);
435 static const GLubyte *wglGetString(GLenum name);
437 /* check if the extension is present in the list */
438 static BOOL has_extension( const char *list, const char *ext )
440 size_t len = strlen( ext );
442 while (list)
444 while (*list == ' ') list++;
445 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
446 list = strchr( list, ' ' );
448 return FALSE;
451 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
453 /* In the future we might want to find the exact X or GLX error to report back to the app */
454 return 1;
457 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
459 static const char legacy_extensions[] = " WGL_EXT_extensions_string WGL_EXT_swap_control";
461 int screen = DefaultScreen(gdi_display);
462 Window win = 0, root = 0;
463 const char *gl_renderer;
464 const char* str;
465 XVisualInfo *vis;
466 GLXContext ctx = NULL;
467 XSetWindowAttributes attr;
468 BOOL ret = FALSE;
469 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
471 attr.override_redirect = True;
472 attr.colormap = None;
473 attr.border_pixel = 0;
475 vis = pglXChooseVisual(gdi_display, screen, attribList);
476 if (vis) {
477 #ifdef __i386__
478 WORD old_fs = wine_get_fs();
479 /* Create a GLX Context. Without one we can't query GL information */
480 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
481 if (wine_get_fs() != old_fs)
483 wine_set_fs( old_fs );
484 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
485 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
486 goto done;
488 #else
489 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
490 #endif
492 if (!ctx) goto done;
494 root = RootWindow( gdi_display, vis->screen );
495 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
496 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
497 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
498 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
499 XMapWindow( gdi_display, win );
500 else
501 win = root;
503 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
505 ERR_(winediag)( "Unable to activate OpenGL context, most likely your %s OpenGL drivers haven't been "
506 "installed correctly\n", is_win64 ? "64-bit" : "32-bit" );
507 goto done;
509 gl_renderer = (const char *)opengl_funcs.gl.p_glGetString(GL_RENDERER);
510 WineGLInfo.glVersion = (const char *) opengl_funcs.gl.p_glGetString(GL_VERSION);
511 str = (const char *) opengl_funcs.gl.p_glGetString(GL_EXTENSIONS);
512 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+sizeof(legacy_extensions));
513 strcpy(WineGLInfo.glExtensions, str);
514 strcat(WineGLInfo.glExtensions, legacy_extensions);
516 /* Get the common GLX version supported by GLX client and server ( major/minor) */
517 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
519 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
520 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
521 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
523 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
524 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
525 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
527 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
528 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
530 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
531 TRACE("GL renderer : %s.\n", gl_renderer);
532 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
533 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
534 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
535 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
536 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
537 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
539 if(!WineGLInfo.glxDirect)
541 int fd = ConnectionNumber(gdi_display);
542 struct sockaddr_un uaddr;
543 unsigned int uaddrlen = sizeof(struct sockaddr_un);
545 /* In general indirect rendering on a local X11 server indicates a driver problem.
546 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
548 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
549 ERR_(winediag)("Direct rendering is disabled, most likely your %s OpenGL drivers "
550 "haven't been installed correctly (using GL renderer %s, version %s).\n",
551 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
552 debugstr_a(WineGLInfo.glVersion));
554 else
556 /* In general you would expect that if direct rendering is returned, that you receive hardware
557 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
558 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
559 * software rendering, it shows direct rendering.
561 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
562 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
563 * it shows 'Mesa X11'.
565 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
566 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your %s OpenGL "
567 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
568 is_win64 ? "64-bit" : "32-bit", debugstr_a(gl_renderer),
569 debugstr_a(WineGLInfo.glVersion));
571 ret = TRUE;
573 done:
574 if(vis) XFree(vis);
575 if(ctx) {
576 pglXMakeCurrent(gdi_display, None, NULL);
577 pglXDestroyContext(gdi_display, ctx);
579 if (win != root) XDestroyWindow( gdi_display, win );
580 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
581 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
582 return ret;
585 static void *opengl_handle;
587 static BOOL WINAPI init_opengl( INIT_ONCE *once, void *param, void **context )
589 char buffer[200];
590 int error_base, event_base;
591 unsigned int i;
593 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
594 and include all dependencies */
595 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
596 if (opengl_handle == NULL)
598 ERR( "Failed to load libGL: %s\n", buffer );
599 ERR( "OpenGL support is disabled.\n");
600 return TRUE;
603 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
605 if (!(((void **)&opengl_funcs.gl)[i] = wine_dlsym( opengl_handle, opengl_func_names[i], NULL, 0 )))
607 ERR( "%s not found in libGL, disabling OpenGL.\n", opengl_func_names[i] );
608 goto failed;
612 /* redirect some standard OpenGL functions */
613 #define REDIRECT(func) \
614 do { p##func = opengl_funcs.gl.p_##func; opengl_funcs.gl.p_##func = w##func; } while(0)
615 REDIRECT( glFinish );
616 REDIRECT( glFlush );
617 REDIRECT( glGetString );
618 #undef REDIRECT
620 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
621 if (pglXGetProcAddressARB == NULL) {
622 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
623 goto failed;
626 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
628 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
629 goto failed; \
630 } while(0)
632 /* GLX 1.0 */
633 LOAD_FUNCPTR(glXChooseVisual);
634 LOAD_FUNCPTR(glXCopyContext);
635 LOAD_FUNCPTR(glXCreateContext);
636 LOAD_FUNCPTR(glXGetCurrentContext);
637 LOAD_FUNCPTR(glXGetCurrentDrawable);
638 LOAD_FUNCPTR(glXDestroyContext);
639 LOAD_FUNCPTR(glXGetConfig);
640 LOAD_FUNCPTR(glXIsDirect);
641 LOAD_FUNCPTR(glXMakeCurrent);
642 LOAD_FUNCPTR(glXSwapBuffers);
643 LOAD_FUNCPTR(glXQueryExtension);
644 LOAD_FUNCPTR(glXQueryVersion);
646 /* GLX 1.1 */
647 LOAD_FUNCPTR(glXGetClientString);
648 LOAD_FUNCPTR(glXQueryExtensionsString);
649 LOAD_FUNCPTR(glXQueryServerString);
651 /* GLX 1.3 */
652 LOAD_FUNCPTR(glXCreatePbuffer);
653 LOAD_FUNCPTR(glXCreateNewContext);
654 LOAD_FUNCPTR(glXDestroyPbuffer);
655 LOAD_FUNCPTR(glXMakeContextCurrent);
656 LOAD_FUNCPTR(glXGetFBConfigs);
657 LOAD_FUNCPTR(glXCreatePixmap);
658 LOAD_FUNCPTR(glXDestroyPixmap);
659 LOAD_FUNCPTR(glXCreateWindow);
660 LOAD_FUNCPTR(glXDestroyWindow);
661 #undef LOAD_FUNCPTR
663 /* It doesn't matter if these fail. They'll only be used if the driver reports
664 the associated extension is available (and if a driver reports the extension
665 is available but fails to provide the functions, it's quite broken) */
666 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
667 /* ARB GLX Extension */
668 LOAD_FUNCPTR(glXCreateContextAttribsARB);
669 /* EXT GLX Extension */
670 LOAD_FUNCPTR(glXSwapIntervalEXT);
671 /* MESA GLX Extension */
672 LOAD_FUNCPTR(glXSwapIntervalMESA);
673 /* SGI GLX Extension */
674 LOAD_FUNCPTR(glXSwapIntervalSGI);
675 /* NV GLX Extension */
676 LOAD_FUNCPTR(glXAllocateMemoryNV);
677 LOAD_FUNCPTR(glXFreeMemoryNV);
678 #undef LOAD_FUNCPTR
680 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
682 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
683 TRACE("GLX is up and running error_base = %d\n", error_base);
684 } else {
685 ERR( "GLX extension is missing, disabling OpenGL.\n" );
686 goto failed;
688 gl_hwnd_context = XUniqueContext();
689 gl_pbuffer_context = XUniqueContext();
691 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
692 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
693 * rendering is used.
695 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
696 * depends on the reported GLX client / server version and on the client / server extension list.
697 * Those don't have to be the same.
699 * In general the server GLX information lists the capabilities in case of indirect rendering.
700 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
701 * available and in that case the client GLX informat can be used.
702 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
703 * in the GLX version/extension list. When a program does this it works for certain for both
704 * direct and indirect rendering.
706 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
707 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
708 * extension list which causes this extension not to be listed. (Wine requires this extension).
709 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
710 * pbuffers is around.
712 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
713 * the ATI drivers and from then on use GLX client information for them.
716 if(glxRequireVersion(3)) {
717 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
718 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
719 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
720 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
721 } else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGIX_fbconfig")) {
722 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
723 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
724 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
726 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
727 * enable this function when the Xserver understand GLX 1.3 or newer
729 pglXQueryDrawable = NULL;
730 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
731 TRACE("Overriding ATI GLX capabilities!\n");
732 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
733 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
734 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
735 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
737 /* Use client GLX information in case of the ATI drivers. We override the
738 * capabilities over here and not somewhere else as ATI might better their
739 * life in the future. In case they release proper drivers this block of
740 * code won't be called. */
741 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
742 } else {
743 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
746 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_copy_sub_buffer")) {
747 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
750 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_query_renderer" ))
752 pglXQueryCurrentRendererIntegerMESA = pglXGetProcAddressARB(
753 (const GLubyte *)"glXQueryCurrentRendererIntegerMESA" );
754 pglXQueryCurrentRendererStringMESA = pglXGetProcAddressARB(
755 (const GLubyte *)"glXQueryCurrentRendererStringMESA" );
756 pglXQueryRendererIntegerMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererIntegerMESA" );
757 pglXQueryRendererStringMESA = pglXGetProcAddressARB( (const GLubyte *)"glXQueryRendererStringMESA" );
760 if (has_extension( WineGLInfo.glxExtensions, "GLX_OML_sync_control" ))
762 pglXWaitForSbcOML = pglXGetProcAddressARB( (const GLubyte *)"glXWaitForSbcOML" );
763 pglXSwapBuffersMscOML = pglXGetProcAddressARB( (const GLubyte *)"glXSwapBuffersMscOML" );
766 X11DRV_WineGL_LoadExtensions();
767 init_pixel_formats( gdi_display );
768 return TRUE;
770 failed:
771 wine_dlclose(opengl_handle, NULL, 0);
772 opengl_handle = NULL;
773 return TRUE;
776 static BOOL has_opengl(void)
778 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
779 InitOnceExecuteOnce(&init_once, init_opengl, NULL, NULL);
780 return opengl_handle != NULL;
783 static const char *debugstr_fbconfig( GLXFBConfig fbconfig )
785 int id, visual, drawable;
787 if (pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_FBCONFIG_ID, &id ))
788 return "*** invalid fbconfig";
789 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_VISUAL_ID, &visual );
790 pglXGetFBConfigAttrib( gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &drawable );
791 return wine_dbg_sprintf( "fbconfig %#x visual id %#x drawable type %#x", id, visual, drawable );
794 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, struct wgl_pbuffer* pbuf) {
795 int nAttribs = 0;
796 unsigned cur = 0;
797 int pop;
798 int drawattrib = 0;
799 int nvfloatattrib = GLX_DONT_CARE;
800 int pixelattrib = GLX_DONT_CARE;
802 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
803 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
804 while (iWGLAttr && 0 != iWGLAttr[cur]) {
805 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
807 switch (iWGLAttr[cur]) {
808 case WGL_AUX_BUFFERS_ARB:
809 pop = iWGLAttr[++cur];
810 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
811 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
812 break;
813 case WGL_COLOR_BITS_ARB:
814 pop = iWGLAttr[++cur];
815 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
816 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
817 break;
818 case WGL_BLUE_BITS_ARB:
819 pop = iWGLAttr[++cur];
820 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
821 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
822 break;
823 case WGL_RED_BITS_ARB:
824 pop = iWGLAttr[++cur];
825 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
826 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
827 break;
828 case WGL_GREEN_BITS_ARB:
829 pop = iWGLAttr[++cur];
830 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
831 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
832 break;
833 case WGL_ALPHA_BITS_ARB:
834 pop = iWGLAttr[++cur];
835 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
836 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
837 break;
838 case WGL_DEPTH_BITS_ARB:
839 pop = iWGLAttr[++cur];
840 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
841 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
842 break;
843 case WGL_STENCIL_BITS_ARB:
844 pop = iWGLAttr[++cur];
845 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
846 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
847 break;
848 case WGL_DOUBLE_BUFFER_ARB:
849 pop = iWGLAttr[++cur];
850 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
851 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
852 break;
853 case WGL_STEREO_ARB:
854 pop = iWGLAttr[++cur];
855 PUSH2(oGLXAttr, GLX_STEREO, pop);
856 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
857 break;
859 case WGL_PIXEL_TYPE_ARB:
860 pop = iWGLAttr[++cur];
861 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
862 switch (pop) {
863 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
864 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
865 /* 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 */
866 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
867 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
868 default:
869 ERR("unexpected PixelType(%x)\n", pop);
870 pop = 0;
872 break;
874 case WGL_SUPPORT_GDI_ARB:
875 /* This flag is set in a pixel format */
876 pop = iWGLAttr[++cur];
877 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
878 break;
880 case WGL_DRAW_TO_BITMAP_ARB:
881 /* This flag is set in a pixel format */
882 pop = iWGLAttr[++cur];
883 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
884 break;
886 case WGL_DRAW_TO_WINDOW_ARB:
887 pop = iWGLAttr[++cur];
888 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
889 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
890 if (pop) {
891 drawattrib |= GLX_WINDOW_BIT;
893 break;
895 case WGL_DRAW_TO_PBUFFER_ARB:
896 pop = iWGLAttr[++cur];
897 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
898 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
899 if (pop) {
900 drawattrib |= GLX_PBUFFER_BIT;
902 break;
904 case WGL_ACCELERATION_ARB:
905 /* This flag is set in a pixel format */
906 pop = iWGLAttr[++cur];
907 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
908 break;
910 case WGL_SUPPORT_OPENGL_ARB:
911 pop = iWGLAttr[++cur];
912 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
913 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
914 break;
916 case WGL_SWAP_METHOD_ARB:
917 pop = iWGLAttr[++cur];
918 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
919 if (has_swap_method)
921 switch (pop)
923 case WGL_SWAP_EXCHANGE_ARB:
924 pop = GLX_SWAP_EXCHANGE_OML;
925 break;
926 case WGL_SWAP_COPY_ARB:
927 pop = GLX_SWAP_COPY_OML;
928 break;
929 case WGL_SWAP_UNDEFINED_ARB:
930 pop = GLX_SWAP_UNDEFINED_OML;
931 break;
932 default:
933 ERR("Unexpected swap method %#x.\n", pop);
934 pop = GLX_DONT_CARE;
936 PUSH2(oGLXAttr, GLX_SWAP_METHOD_OML, pop);
938 else
940 WARN("GLX_OML_swap_method not supported, ignoring attribute.\n");
942 break;
944 case WGL_PBUFFER_LARGEST_ARB:
945 pop = iWGLAttr[++cur];
946 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
947 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
948 break;
950 case WGL_SAMPLE_BUFFERS_ARB:
951 pop = iWGLAttr[++cur];
952 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
953 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
954 break;
956 case WGL_SAMPLES_ARB:
957 pop = iWGLAttr[++cur];
958 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
959 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
960 break;
962 case WGL_TEXTURE_FORMAT_ARB:
963 case WGL_TEXTURE_TARGET_ARB:
964 case WGL_MIPMAP_TEXTURE_ARB:
965 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
966 pop = iWGLAttr[++cur];
967 if (NULL == pbuf) {
968 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
970 if (!use_render_texture_emulation) {
971 if (WGL_NO_TEXTURE_ARB != pop) {
972 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
973 return -1; /** error: don't support it */
974 } else {
975 drawattrib |= GLX_PBUFFER_BIT;
978 break ;
979 case WGL_FLOAT_COMPONENTS_NV:
980 nvfloatattrib = iWGLAttr[++cur];
981 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
982 break ;
983 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
984 case WGL_BIND_TO_TEXTURE_RGB_ARB:
985 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
986 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
987 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
988 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
989 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
990 pop = iWGLAttr[++cur];
991 /** cannot be converted, see direct handling on
992 * - wglGetPixelFormatAttribivARB
993 * TODO: wglChoosePixelFormat
995 break ;
996 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
997 pop = iWGLAttr[++cur];
998 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
999 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
1000 break ;
1002 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
1003 pop = iWGLAttr[++cur];
1004 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
1005 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
1006 break ;
1007 default:
1008 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
1009 cur++;
1010 break;
1012 ++cur;
1015 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
1016 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
1017 * pixmap and pbuffer formats appear as well. */
1018 if (!drawattrib) drawattrib = GLX_DONT_CARE;
1019 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
1020 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
1022 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
1023 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
1024 * GLX_DONT_CARE unless it is overridden. */
1025 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
1026 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
1028 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
1029 if (has_extension(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
1030 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
1031 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
1034 return nAttribs;
1037 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
1039 int render_type, render_type_bit;
1040 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
1041 switch(render_type_bit)
1043 case GLX_RGBA_BIT:
1044 render_type = GLX_RGBA_TYPE;
1045 break;
1046 case GLX_COLOR_INDEX_BIT:
1047 render_type = GLX_COLOR_INDEX_TYPE;
1048 break;
1049 case GLX_RGBA_FLOAT_BIT:
1050 render_type = GLX_RGBA_FLOAT_TYPE;
1051 break;
1052 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
1053 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
1054 break;
1055 default:
1056 ERR("Unknown render_type: %x\n", render_type_bit);
1057 render_type = 0;
1059 return render_type;
1062 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
1063 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
1065 int dbuf, value;
1066 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
1067 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
1069 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
1070 * the GLX_PIXMAP_BIT set. */
1071 return !dbuf && (value & GLX_PIXMAP_BIT);
1074 static void init_pixel_formats( Display *display )
1076 struct wgl_pixel_format *list;
1077 int size = 0, onscreen_size = 0;
1078 int fmt_id, nCfgs, i, run, bmp_formats;
1079 GLXFBConfig* cfgs;
1080 XVisualInfo *visinfo;
1082 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
1083 if (NULL == cfgs || 0 == nCfgs) {
1084 if(cfgs != NULL) XFree(cfgs);
1085 ERR("glXChooseFBConfig returns NULL\n");
1086 return;
1089 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
1090 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
1091 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
1092 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
1094 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
1096 for(i=0, bmp_formats=0; i<nCfgs; i++)
1098 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1099 bmp_formats++;
1101 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
1103 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats) * sizeof(*list));
1105 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
1106 * Do this as GLX doesn't guarantee that the list is sorted */
1107 for(run=0; run < 2; run++)
1109 for(i=0; i<nCfgs; i++) {
1110 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
1111 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
1113 /* The first run we only add onscreen formats (ones which have an associated X Visual).
1114 * The second run we only set offscreen formats. */
1115 if(!run && visinfo)
1117 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
1118 * The contents is copied to the destination using XCopyArea. For the copying to work
1119 * the depth of the source and destination window should be the same. In general this should
1120 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
1121 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
1122 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
1123 * list formats with the same depth. */
1124 if(visinfo->depth != default_visual.depth)
1126 XFree(visinfo);
1127 continue;
1130 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1131 list[size].fbconfig = cfgs[i];
1132 list[size].fmt_id = fmt_id;
1133 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1134 list[size].dwFlags = 0;
1135 size++;
1136 onscreen_size++;
1138 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
1139 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1141 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1142 list[size].fbconfig = cfgs[i];
1143 list[size].fmt_id = fmt_id;
1144 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1145 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1146 size++;
1147 onscreen_size++;
1149 } else if(run && !visinfo) {
1150 int window_drawable=0;
1151 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1153 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1154 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1155 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1156 * likely make our child window opengl rendering more complicated since likely you can't use
1157 * XCopyArea on a GLX Window.
1158 * For now ignore fbconfigs which are window drawable but lack a visual. */
1159 if(window_drawable & GLX_WINDOW_BIT)
1161 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1162 continue;
1165 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1166 list[size].fbconfig = cfgs[i];
1167 list[size].fmt_id = fmt_id;
1168 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1169 list[size].dwFlags = 0;
1170 size++;
1173 if (visinfo) XFree(visinfo);
1177 XFree(cfgs);
1179 pixel_formats = list;
1180 nb_pixel_formats = size;
1181 nb_onscreen_formats = onscreen_size;
1184 static inline BOOL is_valid_pixel_format( int format )
1186 return format > 0 && format <= nb_pixel_formats;
1189 static inline BOOL is_onscreen_pixel_format( int format )
1191 return format > 0 && format <= nb_onscreen_formats;
1194 static inline int pixel_format_index( const struct wgl_pixel_format *format )
1196 return format - pixel_formats + 1;
1199 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1200 * In our WGL implementation we only support a subset of these formats namely the format of
1201 * Wine's main visual and offscreen formats (if they are available).
1202 * This function converts a WGL format to its corresponding GLX one.
1204 static const struct wgl_pixel_format *get_pixel_format(Display *display, int iPixelFormat, BOOL AllowOffscreen)
1206 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1207 * iPixelFormat in case of probing the number of pixelformats.
1209 if (is_valid_pixel_format( iPixelFormat ) &&
1210 (is_onscreen_pixel_format( iPixelFormat ) || AllowOffscreen)) {
1211 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n",
1212 pixel_formats[iPixelFormat-1].fmt_id, iPixelFormat);
1213 return &pixel_formats[iPixelFormat-1];
1215 return NULL;
1218 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1219 static void mark_drawable_dirty(GLXDrawable old, GLXDrawable new)
1221 struct wgl_context *ctx;
1223 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wgl_context, entry )
1225 if (old == ctx->drawables[0]) {
1226 ctx->drawables[0] = new;
1227 ctx->refresh_drawables = TRUE;
1229 if (old == ctx->drawables[1]) {
1230 ctx->drawables[1] = new;
1231 ctx->refresh_drawables = TRUE;
1236 /* Given the current context, make sure its drawable is sync'd */
1237 static inline void sync_context(struct wgl_context *context)
1239 if (context->refresh_drawables) {
1240 if (glxRequireVersion(3))
1241 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1242 context->drawables[1], context->ctx);
1243 else
1244 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1245 context->refresh_drawables = FALSE;
1249 static BOOL set_swap_interval(GLXDrawable drawable, int interval)
1251 BOOL ret = TRUE;
1253 switch (swap_control_method)
1255 case GLX_SWAP_CONTROL_EXT:
1256 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
1257 pglXSwapIntervalEXT(gdi_display, drawable, interval);
1258 XSync(gdi_display, False);
1259 ret = !X11DRV_check_error();
1260 break;
1262 case GLX_SWAP_CONTROL_MESA:
1263 ret = !pglXSwapIntervalMESA(interval);
1264 break;
1266 case GLX_SWAP_CONTROL_SGI:
1267 /* wglSwapIntervalEXT considers an interval value of zero to mean that
1268 * vsync should be disabled, but glXSwapIntervalSGI considers such a
1269 * value to be an error. Just silently ignore the request for now.
1271 if (!interval)
1272 WARN("Request to disable vertical sync is not handled\n");
1273 else
1274 ret = !pglXSwapIntervalSGI(interval);
1275 break;
1277 case GLX_SWAP_CONTROL_NONE:
1278 /* Unlikely to happen on modern GLX implementations */
1279 WARN("Request to adjust swap interval is not handled\n");
1280 break;
1283 return ret;
1286 static struct gl_drawable *get_gl_drawable( HWND hwnd, HDC hdc )
1288 struct gl_drawable *gl;
1290 EnterCriticalSection( &context_section );
1291 if (hwnd && !XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl )) return gl;
1292 if (hdc && !XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl )) return gl;
1293 LeaveCriticalSection( &context_section );
1294 return NULL;
1297 static void release_gl_drawable( struct gl_drawable *gl )
1299 if (gl) LeaveCriticalSection( &context_section );
1302 static GLXContext create_glxcontext(Display *display, struct wgl_context *context, GLXContext shareList)
1304 GLXContext ctx;
1306 if(context->gl3_context)
1308 if(context->numAttribs)
1309 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, context->attribList);
1310 else
1311 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, GL_TRUE, NULL);
1313 else if(context->vis)
1314 ctx = pglXCreateContext(gdi_display, context->vis, shareList, GL_TRUE);
1315 else /* Create a GLX Context for a pbuffer */
1316 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1318 return ctx;
1322 /***********************************************************************
1323 * free_gl_drawable
1325 static void free_gl_drawable( struct gl_drawable *gl )
1327 switch (gl->type)
1329 case DC_GL_CHILD_WIN:
1330 pglXDestroyWindow( gdi_display, gl->drawable );
1331 XDestroyWindow( gdi_display, gl->window );
1332 XFreeColormap( gdi_display, gl->colormap );
1333 break;
1334 case DC_GL_PIXMAP_WIN:
1335 pglXDestroyPixmap( gdi_display, gl->drawable );
1336 XFreePixmap( gdi_display, gl->pixmap );
1337 break;
1338 default:
1339 break;
1341 if (gl->visual) XFree( gl->visual );
1342 HeapFree( GetProcessHeap(), 0, gl );
1346 /***********************************************************************
1347 * create_gl_drawable
1349 static BOOL create_gl_drawable( HWND hwnd, struct gl_drawable *gl )
1351 gl->drawable = 0;
1353 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow()) /* top-level window */
1355 struct x11drv_win_data *data = get_win_data( hwnd );
1357 if (data)
1359 gl->type = DC_GL_WINDOW;
1360 gl->window = create_client_window( data, gl->visual );
1361 if (gl->window)
1362 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1363 release_win_data( data );
1366 #ifdef SONAME_LIBXCOMPOSITE
1367 else if(usexcomposite)
1369 static Window dummy_parent;
1370 XSetWindowAttributes attrib;
1372 attrib.override_redirect = True;
1373 attrib.border_pixel = 0;
1374 if (!dummy_parent)
1376 attrib.colormap = default_colormap;
1377 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1378 InputOutput, default_visual.visual,
1379 CWColormap | CWBorderPixel | CWOverrideRedirect, &attrib );
1380 XMapWindow( gdi_display, dummy_parent );
1382 gl->colormap = XCreateColormap(gdi_display, dummy_parent, gl->visual->visual,
1383 (gl->visual->class == PseudoColor ||
1384 gl->visual->class == GrayScale ||
1385 gl->visual->class == DirectColor) ?
1386 AllocAll : AllocNone);
1387 attrib.colormap = gl->colormap;
1388 XInstallColormap(gdi_display, attrib.colormap);
1390 gl->type = DC_GL_CHILD_WIN;
1391 gl->window = XCreateWindow( gdi_display, dummy_parent, 0, 0,
1392 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1393 0, gl->visual->depth, InputOutput, gl->visual->visual,
1394 CWColormap | CWBorderPixel | CWOverrideRedirect, &attrib );
1395 if (gl->window)
1397 gl->drawable = pglXCreateWindow( gdi_display, gl->format->fbconfig, gl->window, NULL );
1398 if (gl->drawable)
1400 pXCompositeRedirectWindow(gdi_display, gl->window, CompositeRedirectManual);
1401 XMapWindow(gdi_display, gl->window);
1404 else XFreeColormap( gdi_display, gl->colormap );
1406 #endif
1407 else
1409 WARN("XComposite is not available, using GLXPixmap hack\n");
1411 gl->type = DC_GL_PIXMAP_WIN;
1412 gl->pixmap = XCreatePixmap( gdi_display, root_window,
1413 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top,
1414 gl->visual->depth );
1415 if (gl->pixmap)
1417 gl->drawable = pglXCreatePixmap( gdi_display, gl->format->fbconfig, gl->pixmap, NULL );
1418 if (!gl->drawable) XFreePixmap( gdi_display, gl->pixmap );
1422 if (gl->drawable)
1423 gl->refresh_swap_interval = TRUE;
1424 return gl->drawable != 0;
1428 /***********************************************************************
1429 * set_win_format
1431 static BOOL set_win_format( HWND hwnd, const struct wgl_pixel_format *format )
1433 struct gl_drawable *gl, *prev;
1435 gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) );
1436 /* Default GLX and WGL swap interval is 1, but in case of glXSwapIntervalSGI
1437 * there is no way to query it, so we have to store it here.
1439 gl->swap_interval = 1;
1440 gl->refresh_swap_interval = TRUE;
1441 gl->format = format;
1442 gl->visual = pglXGetVisualFromFBConfig( gdi_display, format->fbconfig );
1443 if (!gl->visual)
1445 HeapFree( GetProcessHeap(), 0, gl );
1446 return FALSE;
1449 GetClientRect( hwnd, &gl->rect );
1450 gl->rect.right = min( max( 1, gl->rect.right ), 65535 );
1451 gl->rect.bottom = min( max( 1, gl->rect.bottom ), 65535 );
1453 if (!create_gl_drawable( hwnd, gl ))
1455 XFree( gl->visual );
1456 HeapFree( GetProcessHeap(), 0, gl );
1457 return FALSE;
1460 TRACE( "created GL drawable %lx for win %p %s\n",
1461 gl->drawable, hwnd, debugstr_fbconfig( format->fbconfig ));
1463 XFlush( gdi_display );
1465 EnterCriticalSection( &context_section );
1466 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&prev ))
1468 gl->swap_interval = prev->swap_interval;
1469 free_gl_drawable( prev );
1471 XSaveContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char *)gl );
1472 LeaveCriticalSection( &context_section );
1474 __wine_set_pixel_format( hwnd, pixel_format_index( format ));
1475 return TRUE;
1479 static BOOL set_pixel_format(HDC hdc, int format, BOOL allow_change)
1481 const struct wgl_pixel_format *fmt;
1482 int value;
1483 HWND hwnd = WindowFromDC( hdc );
1485 TRACE("(%p,%d)\n", hdc, format);
1487 if (!hwnd || hwnd == GetDesktopWindow())
1489 WARN( "not a valid window DC %p/%p\n", hdc, hwnd );
1490 return FALSE;
1493 fmt = get_pixel_format(gdi_display, format, FALSE /* Offscreen */);
1494 if (!fmt)
1496 ERR( "Invalid format %d\n", format );
1497 return FALSE;
1500 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1501 if (!(value & GLX_WINDOW_BIT))
1503 WARN( "Pixel format %d is not compatible for window rendering\n", format );
1504 return FALSE;
1507 if (!allow_change)
1509 struct gl_drawable *gl;
1510 if ((gl = get_gl_drawable( hwnd, hdc )))
1512 int prev = pixel_format_index( gl->format );
1513 release_gl_drawable( gl );
1514 return prev == format; /* cannot change it if already set */
1518 return set_win_format( hwnd, fmt );
1522 /***********************************************************************
1523 * sync_gl_drawable
1525 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
1527 struct gl_drawable *gl;
1528 GLXDrawable glxp;
1529 Pixmap pix;
1530 int mask = 0;
1531 XWindowChanges changes;
1533 changes.width = min( max( 1, client_rect->right - client_rect->left ), 65535 );
1534 changes.height = min( max( 1, client_rect->bottom - client_rect->top ), 65535 );
1536 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1538 if (changes.width != gl->rect.right - gl->rect.left) mask |= CWWidth;
1539 if (changes.height != gl->rect.bottom - gl->rect.top) mask |= CWHeight;
1541 TRACE( "setting drawable %lx size %dx%d\n", gl->drawable, changes.width, changes.height );
1543 switch (gl->type)
1545 case DC_GL_CHILD_WIN:
1546 if (mask) XConfigureWindow( gdi_display, gl->window, mask, &changes );
1547 break;
1548 case DC_GL_PIXMAP_WIN:
1549 if (!mask) break;
1550 pix = XCreatePixmap(gdi_display, root_window, changes.width, changes.height, gl->visual->depth);
1551 if (!pix) goto done;
1552 glxp = pglXCreatePixmap(gdi_display, gl->format->fbconfig, pix, NULL );
1553 if (!glxp)
1555 XFreePixmap(gdi_display, pix);
1556 goto done;
1558 mark_drawable_dirty(gl->drawable, glxp);
1559 XFlush( gdi_display );
1561 XFreePixmap(gdi_display, gl->pixmap);
1562 pglXDestroyPixmap(gdi_display, gl->drawable);
1563 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, gl->drawable );
1565 gl->pixmap = pix;
1566 gl->drawable = glxp;
1567 break;
1568 default:
1569 break;
1571 SetRect( &gl->rect, 0, 0, changes.width, changes.height );
1572 done:
1573 release_gl_drawable( gl );
1577 /***********************************************************************
1578 * set_gl_drawable_parent
1580 void set_gl_drawable_parent( HWND hwnd, HWND parent )
1582 struct gl_drawable *gl;
1583 GLXDrawable old_drawable;
1585 if (!(gl = get_gl_drawable( hwnd, 0 ))) return;
1587 TRACE( "setting drawable %lx parent %p\n", gl->drawable, parent );
1589 old_drawable = gl->drawable;
1590 switch (gl->type)
1592 case DC_GL_WINDOW:
1593 break;
1594 case DC_GL_CHILD_WIN:
1595 if (parent != GetDesktopWindow()) goto done;
1596 pglXDestroyWindow( gdi_display, gl->drawable );
1597 XDestroyWindow( gdi_display, gl->window );
1598 XFreeColormap( gdi_display, gl->colormap );
1599 break;
1600 case DC_GL_PIXMAP_WIN:
1601 if (parent != GetDesktopWindow()) goto done;
1602 pglXDestroyPixmap( gdi_display, gl->drawable );
1603 XFreePixmap( gdi_display, gl->pixmap );
1604 break;
1605 default:
1606 goto done;
1609 if (!create_gl_drawable( hwnd, gl ))
1611 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1612 release_gl_drawable( gl );
1613 XFree( gl->visual );
1614 HeapFree( GetProcessHeap(), 0, gl );
1615 __wine_set_pixel_format( hwnd, 0 );
1616 return;
1618 mark_drawable_dirty( old_drawable, gl->drawable );
1620 done:
1621 release_gl_drawable( gl );
1626 /***********************************************************************
1627 * destroy_gl_drawable
1629 void destroy_gl_drawable( HWND hwnd )
1631 struct gl_drawable *gl;
1633 EnterCriticalSection( &context_section );
1634 if (!XFindContext( gdi_display, (XID)hwnd, gl_hwnd_context, (char **)&gl ))
1636 XDeleteContext( gdi_display, (XID)hwnd, gl_hwnd_context );
1637 free_gl_drawable( gl );
1639 LeaveCriticalSection( &context_section );
1644 * glxdrv_DescribePixelFormat
1646 * Get the pixel-format descriptor associated to the given id
1648 static int glxdrv_wglDescribePixelFormat( HDC hdc, int iPixelFormat,
1649 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1651 /*XVisualInfo *vis;*/
1652 int value;
1653 int rb,gb,bb,ab;
1654 const struct wgl_pixel_format *fmt;
1656 if (!has_opengl()) return 0;
1658 TRACE("(%p,%d,%d,%p)\n", hdc, iPixelFormat, nBytes, ppfd);
1660 if (!ppfd) return nb_onscreen_formats;
1662 /* 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 */
1663 fmt = get_pixel_format(gdi_display, iPixelFormat, FALSE /* Offscreen */);
1664 if (!fmt) {
1665 WARN("unexpected format %d\n", iPixelFormat);
1666 return 0;
1669 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1670 ERR("Wrong structure size !\n");
1671 /* Should set error */
1672 return 0;
1675 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1676 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1677 ppfd->nVersion = 1;
1679 /* These flags are always the same... */
1680 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1681 /* Now the flags extracted from the Visual */
1683 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1684 if(value & GLX_WINDOW_BIT)
1685 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1687 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1688 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1689 * 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
1690 * offered the GDI bit either. */
1691 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1693 /* PFD_GENERIC_FORMAT - gdi software rendering
1694 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1695 * none set - full hardware accelerated by a ICD
1697 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1698 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1700 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1701 if (value) {
1702 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1703 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1705 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1707 /* Pixel type */
1708 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1709 if (value & GLX_RGBA_BIT)
1710 ppfd->iPixelType = PFD_TYPE_RGBA;
1711 else
1712 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1714 /* Color bits */
1715 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1716 ppfd->cColorBits = value;
1718 /* Red, green, blue and alpha bits / shifts */
1719 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1720 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1721 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1722 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1723 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1725 ppfd->cRedBits = rb;
1726 ppfd->cRedShift = gb + bb + ab;
1727 ppfd->cBlueBits = bb;
1728 ppfd->cBlueShift = ab;
1729 ppfd->cGreenBits = gb;
1730 ppfd->cGreenShift = bb + ab;
1731 ppfd->cAlphaBits = ab;
1732 ppfd->cAlphaShift = 0;
1733 } else {
1734 ppfd->cRedBits = 0;
1735 ppfd->cRedShift = 0;
1736 ppfd->cBlueBits = 0;
1737 ppfd->cBlueShift = 0;
1738 ppfd->cGreenBits = 0;
1739 ppfd->cGreenShift = 0;
1740 ppfd->cAlphaBits = 0;
1741 ppfd->cAlphaShift = 0;
1744 /* Accum RGBA bits */
1745 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1746 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1747 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1748 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1750 ppfd->cAccumBits = rb+gb+bb+ab;
1751 ppfd->cAccumRedBits = rb;
1752 ppfd->cAccumGreenBits = gb;
1753 ppfd->cAccumBlueBits = bb;
1754 ppfd->cAccumAlphaBits = ab;
1756 /* Aux bits */
1757 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1758 ppfd->cAuxBuffers = value;
1760 /* Depth bits */
1761 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1762 ppfd->cDepthBits = value;
1764 /* stencil bits */
1765 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1766 ppfd->cStencilBits = value;
1768 ppfd->iLayerType = PFD_MAIN_PLANE;
1770 if (TRACE_ON(wgl)) {
1771 dump_PIXELFORMATDESCRIPTOR(ppfd);
1774 return nb_onscreen_formats;
1777 /***********************************************************************
1778 * glxdrv_wglGetPixelFormat
1780 static int glxdrv_wglGetPixelFormat( HDC hdc )
1782 struct gl_drawable *gl;
1783 int ret = 0;
1785 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1787 ret = pixel_format_index( gl->format );
1788 /* Offscreen formats can't be used with traditional WGL calls.
1789 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1790 if (!is_onscreen_pixel_format( ret )) ret = 1;
1791 release_gl_drawable( gl );
1793 TRACE( "%p -> %d\n", hdc, ret );
1794 return ret;
1797 /***********************************************************************
1798 * glxdrv_wglSetPixelFormat
1800 static BOOL glxdrv_wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd )
1802 return set_pixel_format(hdc, iPixelFormat, FALSE);
1805 /***********************************************************************
1806 * glxdrv_wglCopyContext
1808 static BOOL glxdrv_wglCopyContext(struct wgl_context *src, struct wgl_context *dst, UINT mask)
1810 TRACE("%p -> %p mask %#x\n", src, dst, mask);
1812 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1814 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1815 return TRUE;
1818 /***********************************************************************
1819 * glxdrv_wglCreateContext
1821 static struct wgl_context *glxdrv_wglCreateContext( HDC hdc )
1823 struct wgl_context *ret;
1824 struct gl_drawable *gl;
1826 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1828 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1829 return NULL;
1832 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
1834 ret->hdc = hdc;
1835 ret->fmt = gl->format;
1836 ret->vis = pglXGetVisualFromFBConfig(gdi_display, gl->format->fbconfig);
1837 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1838 list_add_head( &context_list, &ret->entry );
1840 release_gl_drawable( gl );
1841 TRACE( "%p -> %p\n", hdc, ret );
1842 return ret;
1845 /***********************************************************************
1846 * glxdrv_wglDeleteContext
1848 static BOOL glxdrv_wglDeleteContext(struct wgl_context *ctx)
1850 struct wgl_pbuffer *pb;
1852 TRACE("(%p)\n", ctx);
1854 EnterCriticalSection( &context_section );
1855 list_remove( &ctx->entry );
1856 LIST_FOR_EACH_ENTRY( pb, &pbuffer_list, struct wgl_pbuffer, entry )
1858 if (pb->prev_context == ctx->ctx) {
1859 pglXDestroyContext(gdi_display, pb->tmp_context);
1860 pb->prev_context = pb->tmp_context = NULL;
1863 LeaveCriticalSection( &context_section );
1865 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1866 if (ctx->vis) XFree( ctx->vis );
1867 return HeapFree( GetProcessHeap(), 0, ctx );
1870 /***********************************************************************
1871 * glxdrv_wglGetProcAddress
1873 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1875 if (!strncmp(lpszProc, "wgl", 3)) return NULL;
1876 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1879 /***********************************************************************
1880 * glxdrv_wglMakeCurrent
1882 static BOOL glxdrv_wglMakeCurrent(HDC hdc, struct wgl_context *ctx)
1884 BOOL ret = FALSE;
1885 struct gl_drawable *gl;
1887 TRACE("(%p,%p)\n", hdc, ctx);
1889 if (!ctx)
1891 pglXMakeCurrent(gdi_display, None, NULL);
1892 NtCurrentTeb()->glContext = NULL;
1893 return TRUE;
1896 if ((gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
1898 if (ctx->fmt != gl->format)
1900 WARN( "mismatched pixel format hdc %p %p ctx %p %p\n", hdc, gl->format, ctx, ctx->fmt );
1901 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1902 goto done;
1905 TRACE("hdc %p drawable %lx fmt %p ctx %p %s\n", hdc, gl->drawable, gl->format, ctx->ctx,
1906 debugstr_fbconfig( gl->format->fbconfig ));
1908 ret = pglXMakeCurrent(gdi_display, gl->drawable, ctx->ctx);
1909 if (ret)
1911 NtCurrentTeb()->glContext = ctx;
1912 ctx->has_been_current = TRUE;
1913 ctx->hdc = hdc;
1914 ctx->drawables[0] = gl->drawable;
1915 ctx->drawables[1] = gl->drawable;
1916 ctx->refresh_drawables = FALSE;
1917 goto done;
1920 SetLastError( ERROR_INVALID_HANDLE );
1922 done:
1923 release_gl_drawable( gl );
1924 TRACE( "%p,%p returning %d\n", hdc, ctx, ret );
1925 return ret;
1928 /***********************************************************************
1929 * X11DRV_wglMakeContextCurrentARB
1931 static BOOL X11DRV_wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, struct wgl_context *ctx )
1933 BOOL ret = FALSE;
1934 struct gl_drawable *draw_gl, *read_gl = NULL;
1936 TRACE("(%p,%p,%p)\n", draw_hdc, read_hdc, ctx);
1938 if (!ctx)
1940 pglXMakeCurrent(gdi_display, None, NULL);
1941 NtCurrentTeb()->glContext = NULL;
1942 return TRUE;
1945 if (!pglXMakeContextCurrent) return FALSE;
1947 if ((draw_gl = get_gl_drawable( WindowFromDC( draw_hdc ), draw_hdc )))
1949 read_gl = get_gl_drawable( WindowFromDC( read_hdc ), read_hdc );
1950 ret = pglXMakeContextCurrent(gdi_display, draw_gl->drawable,
1951 read_gl ? read_gl->drawable : 0, ctx->ctx);
1952 if (ret)
1954 ctx->has_been_current = TRUE;
1955 ctx->hdc = draw_hdc;
1956 ctx->drawables[0] = draw_gl->drawable;
1957 ctx->drawables[1] = read_gl ? read_gl->drawable : 0;
1958 ctx->refresh_drawables = FALSE;
1959 NtCurrentTeb()->glContext = ctx;
1960 goto done;
1963 SetLastError( ERROR_INVALID_HANDLE );
1964 done:
1965 release_gl_drawable( read_gl );
1966 release_gl_drawable( draw_gl );
1967 TRACE( "%p,%p,%p returning %d\n", draw_hdc, read_hdc, ctx, ret );
1968 return ret;
1971 /***********************************************************************
1972 * glxdrv_wglShareLists
1974 static BOOL glxdrv_wglShareLists(struct wgl_context *org, struct wgl_context *dest)
1976 TRACE("(%p, %p)\n", org, dest);
1978 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1979 * at context creation time but in case of WGL it is done using wglShareLists.
1980 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1981 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1982 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1983 * so there delaying context creation doesn't work.
1985 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1986 * and when a program requests sharing we recreate the destination context if it hasn't been made
1987 * current or when it hasn't shared display lists before.
1990 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1992 ERR("Could not share display lists, one of the contexts has been current already !\n");
1993 return FALSE;
1995 else if(dest->sharing)
1997 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1998 return FALSE;
2000 else
2002 /* Re-create the GLX context and share display lists */
2003 pglXDestroyContext(gdi_display, dest->ctx);
2004 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
2005 TRACE(" re-created context (%p) for Wine context %p (%s) sharing lists with ctx %p (%s)\n",
2006 dest->ctx, dest, debugstr_fbconfig(dest->fmt->fbconfig),
2007 org->ctx, debugstr_fbconfig( org->fmt->fbconfig));
2009 org->sharing = TRUE;
2010 dest->sharing = TRUE;
2011 return TRUE;
2013 return FALSE;
2016 static void wglFinish(void)
2018 struct x11drv_escape_flush_gl_drawable escape;
2019 struct gl_drawable *gl;
2020 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2022 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
2023 escape.gl_drawable = 0;
2025 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
2027 switch (gl->type)
2029 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
2030 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
2031 default: break;
2033 sync_context(ctx);
2034 release_gl_drawable( gl );
2037 pglFinish();
2038 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2041 static void wglFlush(void)
2043 struct x11drv_escape_flush_gl_drawable escape;
2044 struct gl_drawable *gl;
2045 struct wgl_context *ctx = NtCurrentTeb()->glContext;
2047 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
2048 escape.gl_drawable = 0;
2050 if ((gl = get_gl_drawable( WindowFromDC( ctx->hdc ), 0 )))
2052 switch (gl->type)
2054 case DC_GL_PIXMAP_WIN: escape.gl_drawable = gl->pixmap; break;
2055 case DC_GL_CHILD_WIN: escape.gl_drawable = gl->window; break;
2056 default: break;
2058 sync_context(ctx);
2059 release_gl_drawable( gl );
2062 pglFlush();
2063 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2066 static const GLubyte *wglGetString(GLenum name)
2068 if (name == GL_EXTENSIONS && WineGLInfo.glExtensions)
2069 return (const GLubyte *)WineGLInfo.glExtensions;
2070 return pglGetString(name);
2073 /***********************************************************************
2074 * X11DRV_wglCreateContextAttribsARB
2076 static struct wgl_context *X11DRV_wglCreateContextAttribsARB( HDC hdc, struct wgl_context *hShareContext,
2077 const int* attribList )
2079 struct wgl_context *ret;
2080 struct gl_drawable *gl;
2081 int err = 0;
2083 TRACE("(%p %p %p)\n", hdc, hShareContext, attribList);
2085 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
2087 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
2088 return NULL;
2091 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
2093 ret->hdc = hdc;
2094 ret->fmt = gl->format;
2095 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2096 ret->gl3_context = TRUE;
2097 if (attribList)
2099 int *pContextAttribList = &ret->attribList[0];
2100 /* attribList consists of pairs {token, value] terminated with 0 */
2101 while(attribList[0] != 0)
2103 TRACE("%#x %#x\n", attribList[0], attribList[1]);
2104 switch(attribList[0])
2106 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2107 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2108 pContextAttribList[1] = attribList[1];
2109 pContextAttribList += 2;
2110 ret->numAttribs++;
2111 break;
2112 case WGL_CONTEXT_MINOR_VERSION_ARB:
2113 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2114 pContextAttribList[1] = attribList[1];
2115 pContextAttribList += 2;
2116 ret->numAttribs++;
2117 break;
2118 case WGL_CONTEXT_LAYER_PLANE_ARB:
2119 break;
2120 case WGL_CONTEXT_FLAGS_ARB:
2121 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2122 pContextAttribList[1] = attribList[1];
2123 pContextAttribList += 2;
2124 ret->numAttribs++;
2125 break;
2126 case WGL_CONTEXT_OPENGL_NO_ERROR_ARB:
2127 pContextAttribList[0] = GLX_CONTEXT_OPENGL_NO_ERROR_ARB;
2128 pContextAttribList[1] = attribList[1];
2129 pContextAttribList += 2;
2130 ret->numAttribs++;
2131 break;
2132 case WGL_CONTEXT_PROFILE_MASK_ARB:
2133 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2134 pContextAttribList[1] = attribList[1];
2135 pContextAttribList += 2;
2136 ret->numAttribs++;
2137 break;
2138 case WGL_RENDERER_ID_WINE:
2139 pContextAttribList[0] = GLX_RENDERER_ID_MESA;
2140 pContextAttribList[1] = attribList[1];
2141 pContextAttribList += 2;
2142 ret->numAttribs++;
2143 break;
2144 default:
2145 ERR("Unhandled attribList pair: %#x %#x\n", attribList[0], attribList[1]);
2147 attribList += 2;
2151 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2152 ret->ctx = create_glxcontext(gdi_display, ret, hShareContext ? hShareContext->ctx : NULL);
2153 XSync(gdi_display, False);
2154 if ((err = X11DRV_check_error()) || !ret->ctx)
2156 /* In the future we should convert the GLX error to a win32 one here if needed */
2157 WARN("Context creation failed (error %#x).\n", err);
2158 HeapFree( GetProcessHeap(), 0, ret );
2159 ret = NULL;
2161 else list_add_head( &context_list, &ret->entry );
2164 release_gl_drawable( gl );
2165 TRACE( "%p -> %p\n", hdc, ret );
2166 return ret;
2170 * X11DRV_wglGetExtensionsStringARB
2172 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2174 static const char *X11DRV_wglGetExtensionsStringARB(HDC hdc)
2176 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2177 return WineGLInfo.wglExtensions;
2181 * X11DRV_wglCreatePbufferARB
2183 * WGL_ARB_pbuffer: wglCreatePbufferARB
2185 static struct wgl_pbuffer *X11DRV_wglCreatePbufferARB( HDC hdc, int iPixelFormat, int iWidth, int iHeight,
2186 const int *piAttribList )
2188 struct wgl_pbuffer* object;
2189 const struct wgl_pixel_format *fmt;
2190 int attribs[256];
2191 int nAttribs = 0;
2193 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2195 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2196 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2197 if(!fmt) {
2198 ERR("(%p): invalid pixel format %d\n", hdc, iPixelFormat);
2199 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2200 return NULL;
2203 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2204 if (NULL == object) {
2205 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2206 return NULL;
2208 object->width = iWidth;
2209 object->height = iHeight;
2210 object->fmt = fmt;
2212 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2213 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2214 while (piAttribList && 0 != *piAttribList) {
2215 int attr_v;
2216 switch (*piAttribList) {
2217 case WGL_PBUFFER_LARGEST_ARB: {
2218 ++piAttribList;
2219 attr_v = *piAttribList;
2220 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2221 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2222 break;
2225 case WGL_TEXTURE_FORMAT_ARB: {
2226 ++piAttribList;
2227 attr_v = *piAttribList;
2228 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2229 if (WGL_NO_TEXTURE_ARB == attr_v) {
2230 object->use_render_texture = 0;
2231 } else {
2232 if (!use_render_texture_emulation) {
2233 SetLastError(ERROR_INVALID_DATA);
2234 goto create_failed;
2236 switch (attr_v) {
2237 case WGL_TEXTURE_RGB_ARB:
2238 object->use_render_texture = GL_RGB;
2239 object->texture_bpp = 3;
2240 object->texture_format = GL_RGB;
2241 object->texture_type = GL_UNSIGNED_BYTE;
2242 break;
2243 case WGL_TEXTURE_RGBA_ARB:
2244 object->use_render_texture = GL_RGBA;
2245 object->texture_bpp = 4;
2246 object->texture_format = GL_RGBA;
2247 object->texture_type = GL_UNSIGNED_BYTE;
2248 break;
2250 /* WGL_FLOAT_COMPONENTS_NV */
2251 case WGL_TEXTURE_FLOAT_R_NV:
2252 object->use_render_texture = GL_FLOAT_R_NV;
2253 object->texture_bpp = 4;
2254 object->texture_format = GL_RED;
2255 object->texture_type = GL_FLOAT;
2256 break;
2257 case WGL_TEXTURE_FLOAT_RG_NV:
2258 object->use_render_texture = GL_FLOAT_RG_NV;
2259 object->texture_bpp = 8;
2260 object->texture_format = GL_LUMINANCE_ALPHA;
2261 object->texture_type = GL_FLOAT;
2262 break;
2263 case WGL_TEXTURE_FLOAT_RGB_NV:
2264 object->use_render_texture = GL_FLOAT_RGB_NV;
2265 object->texture_bpp = 12;
2266 object->texture_format = GL_RGB;
2267 object->texture_type = GL_FLOAT;
2268 break;
2269 case WGL_TEXTURE_FLOAT_RGBA_NV:
2270 object->use_render_texture = GL_FLOAT_RGBA_NV;
2271 object->texture_bpp = 16;
2272 object->texture_format = GL_RGBA;
2273 object->texture_type = GL_FLOAT;
2274 break;
2275 default:
2276 ERR("Unknown texture format: %x\n", attr_v);
2277 SetLastError(ERROR_INVALID_DATA);
2278 goto create_failed;
2281 break;
2284 case WGL_TEXTURE_TARGET_ARB: {
2285 ++piAttribList;
2286 attr_v = *piAttribList;
2287 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2288 if (WGL_NO_TEXTURE_ARB == attr_v) {
2289 object->texture_target = 0;
2290 } else {
2291 if (!use_render_texture_emulation) {
2292 SetLastError(ERROR_INVALID_DATA);
2293 goto create_failed;
2295 switch (attr_v) {
2296 case WGL_TEXTURE_CUBE_MAP_ARB: {
2297 if (iWidth != iHeight) {
2298 SetLastError(ERROR_INVALID_DATA);
2299 goto create_failed;
2301 object->texture_target = GL_TEXTURE_CUBE_MAP;
2302 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2303 break;
2305 case WGL_TEXTURE_1D_ARB: {
2306 if (1 != iHeight) {
2307 SetLastError(ERROR_INVALID_DATA);
2308 goto create_failed;
2310 object->texture_target = GL_TEXTURE_1D;
2311 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2312 break;
2314 case WGL_TEXTURE_2D_ARB: {
2315 object->texture_target = GL_TEXTURE_2D;
2316 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2317 break;
2319 case WGL_TEXTURE_RECTANGLE_NV: {
2320 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2321 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2322 break;
2324 default:
2325 ERR("Unknown texture target: %x\n", attr_v);
2326 SetLastError(ERROR_INVALID_DATA);
2327 goto create_failed;
2330 break;
2333 case WGL_MIPMAP_TEXTURE_ARB: {
2334 ++piAttribList;
2335 attr_v = *piAttribList;
2336 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2337 if (!use_render_texture_emulation) {
2338 SetLastError(ERROR_INVALID_DATA);
2339 goto create_failed;
2341 break;
2344 ++piAttribList;
2347 PUSH1(attribs, None);
2348 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2349 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2350 if (!object->drawable) {
2351 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2352 goto create_failed; /* unexpected error */
2354 EnterCriticalSection( &context_section );
2355 list_add_head( &pbuffer_list, &object->entry );
2356 LeaveCriticalSection( &context_section );
2357 TRACE("->(%p)\n", object);
2358 return object;
2360 create_failed:
2361 HeapFree(GetProcessHeap(), 0, object);
2362 TRACE("->(FAILED)\n");
2363 return NULL;
2367 * X11DRV_wglDestroyPbufferARB
2369 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2371 static BOOL X11DRV_wglDestroyPbufferARB( struct wgl_pbuffer *object )
2373 TRACE("(%p)\n", object);
2375 EnterCriticalSection( &context_section );
2376 list_remove( &object->entry );
2377 LeaveCriticalSection( &context_section );
2378 pglXDestroyPbuffer(gdi_display, object->drawable);
2379 if (object->tmp_context)
2380 pglXDestroyContext(gdi_display, object->tmp_context);
2381 HeapFree(GetProcessHeap(), 0, object);
2382 return GL_TRUE;
2386 * X11DRV_wglGetPbufferDCARB
2388 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2390 static HDC X11DRV_wglGetPbufferDCARB( struct wgl_pbuffer *object )
2392 struct x11drv_escape_set_drawable escape;
2393 struct gl_drawable *gl, *prev;
2394 HDC hdc;
2396 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2397 if (!hdc) return 0;
2399 if (!(gl = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*gl) )))
2401 DeleteDC( hdc );
2402 return 0;
2404 gl->type = DC_GL_PBUFFER;
2405 gl->drawable = object->drawable;
2406 gl->format = object->fmt;
2408 EnterCriticalSection( &context_section );
2409 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&prev ))
2410 free_gl_drawable( prev );
2411 XSaveContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char *)gl );
2412 LeaveCriticalSection( &context_section );
2414 escape.code = X11DRV_SET_DRAWABLE;
2415 escape.drawable = object->drawable;
2416 escape.mode = IncludeInferiors;
2417 SetRect( &escape.dc_rect, 0, 0, object->width, object->height );
2418 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2420 TRACE( "(%p)->(%p)\n", object, hdc );
2421 return hdc;
2425 * X11DRV_wglQueryPbufferARB
2427 * WGL_ARB_pbuffer: wglQueryPbufferARB
2429 static BOOL X11DRV_wglQueryPbufferARB( struct wgl_pbuffer *object, int iAttribute, int *piValue )
2431 TRACE("(%p, 0x%x, %p)\n", object, iAttribute, piValue);
2433 switch (iAttribute) {
2434 case WGL_PBUFFER_WIDTH_ARB:
2435 pglXQueryDrawable(gdi_display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2436 break;
2437 case WGL_PBUFFER_HEIGHT_ARB:
2438 pglXQueryDrawable(gdi_display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2439 break;
2441 case WGL_PBUFFER_LOST_ARB:
2442 /* GLX Pbuffers cannot be lost by default. We can support this by
2443 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2444 * to receive pixel buffer clobber events, however that may or may
2445 * not give any benefit */
2446 *piValue = GL_FALSE;
2447 break;
2449 case WGL_TEXTURE_FORMAT_ARB:
2450 if (!object->use_render_texture) {
2451 *piValue = WGL_NO_TEXTURE_ARB;
2452 } else {
2453 if (!use_render_texture_emulation) {
2454 SetLastError(ERROR_INVALID_HANDLE);
2455 return GL_FALSE;
2457 switch(object->use_render_texture) {
2458 case GL_RGB:
2459 *piValue = WGL_TEXTURE_RGB_ARB;
2460 break;
2461 case GL_RGBA:
2462 *piValue = WGL_TEXTURE_RGBA_ARB;
2463 break;
2464 /* WGL_FLOAT_COMPONENTS_NV */
2465 case GL_FLOAT_R_NV:
2466 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2467 break;
2468 case GL_FLOAT_RG_NV:
2469 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2470 break;
2471 case GL_FLOAT_RGB_NV:
2472 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2473 break;
2474 case GL_FLOAT_RGBA_NV:
2475 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2476 break;
2477 default:
2478 ERR("Unknown texture format: %x\n", object->use_render_texture);
2481 break;
2483 case WGL_TEXTURE_TARGET_ARB:
2484 if (!object->texture_target){
2485 *piValue = WGL_NO_TEXTURE_ARB;
2486 } else {
2487 if (!use_render_texture_emulation) {
2488 SetLastError(ERROR_INVALID_DATA);
2489 return GL_FALSE;
2491 switch (object->texture_target) {
2492 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2493 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2494 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2495 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2498 break;
2500 case WGL_MIPMAP_TEXTURE_ARB:
2501 *piValue = GL_FALSE; /** don't support that */
2502 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2503 break;
2505 default:
2506 FIXME("unexpected attribute %x\n", iAttribute);
2507 break;
2510 return GL_TRUE;
2514 * X11DRV_wglReleasePbufferDCARB
2516 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2518 static int X11DRV_wglReleasePbufferDCARB( struct wgl_pbuffer *object, HDC hdc )
2520 struct gl_drawable *gl;
2522 TRACE("(%p, %p)\n", object, hdc);
2524 EnterCriticalSection( &context_section );
2526 if (!XFindContext( gdi_display, (XID)hdc, gl_pbuffer_context, (char **)&gl ))
2528 XDeleteContext( gdi_display, (XID)hdc, gl_pbuffer_context );
2529 free_gl_drawable( gl );
2531 else hdc = 0;
2533 LeaveCriticalSection( &context_section );
2535 return hdc && DeleteDC(hdc);
2539 * X11DRV_wglSetPbufferAttribARB
2541 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2543 static BOOL X11DRV_wglSetPbufferAttribARB( struct wgl_pbuffer *object, const int *piAttribList )
2545 GLboolean ret = GL_FALSE;
2547 WARN("(%p, %p): alpha-testing, report any problem\n", object, piAttribList);
2549 if (!object->use_render_texture) {
2550 SetLastError(ERROR_INVALID_HANDLE);
2551 return GL_FALSE;
2553 if (use_render_texture_emulation) {
2554 return GL_TRUE;
2556 return ret;
2560 * X11DRV_wglChoosePixelFormatARB
2562 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2564 static BOOL X11DRV_wglChoosePixelFormatARB( HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
2565 UINT nMaxFormats, int *piFormats, UINT *nNumFormats )
2567 int attribs[256];
2568 int nAttribs = 0;
2569 GLXFBConfig* cfgs;
2570 int nCfgs = 0;
2571 int it;
2572 int fmt_id;
2573 int start, end;
2574 UINT pfmt_it = 0;
2575 int run;
2576 int i;
2577 DWORD dwFlags = 0;
2579 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2580 if (NULL != pfAttribFList) {
2581 FIXME("unused pfAttribFList\n");
2584 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2585 if (-1 == nAttribs) {
2586 WARN("Cannot convert WGL to GLX attributes\n");
2587 return GL_FALSE;
2589 PUSH1(attribs, None);
2591 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2592 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the pixel format. We don't query these attributes
2593 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on.
2595 for(i=0; piAttribIList[i] != 0; i+=2)
2597 switch(piAttribIList[i])
2599 case WGL_DRAW_TO_BITMAP_ARB:
2600 if(piAttribIList[i+1])
2601 dwFlags |= PFD_DRAW_TO_BITMAP;
2602 break;
2603 case WGL_ACCELERATION_ARB:
2604 switch(piAttribIList[i+1])
2606 case WGL_NO_ACCELERATION_ARB:
2607 dwFlags |= PFD_GENERIC_FORMAT;
2608 break;
2609 case WGL_GENERIC_ACCELERATION_ARB:
2610 dwFlags |= PFD_GENERIC_ACCELERATED;
2611 break;
2612 case WGL_FULL_ACCELERATION_ARB:
2613 /* Nothing to do */
2614 break;
2616 break;
2617 case WGL_SUPPORT_GDI_ARB:
2618 if(piAttribIList[i+1])
2619 dwFlags |= PFD_SUPPORT_GDI;
2620 break;
2624 /* Search for FB configurations matching the requirements in attribs */
2625 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2626 if (NULL == cfgs) {
2627 WARN("Compatible Pixel Format not found\n");
2628 return GL_FALSE;
2631 /* Loop through all matching formats and check if they are suitable.
2632 * Note that this function should at max return nMaxFormats different formats */
2633 for(run=0; run < 2; run++)
2635 for (it = 0; it < nCfgs && pfmt_it < nMaxFormats; ++it)
2637 if (pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id))
2639 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2640 continue;
2643 /* During the first run we only want onscreen formats and during the second only offscreen */
2644 start = run == 1 ? nb_onscreen_formats : 0;
2645 end = run == 1 ? nb_pixel_formats : nb_onscreen_formats;
2647 for (i = start; i < end; i++)
2649 if (pixel_formats[i].fmt_id == fmt_id && (pixel_formats[i].dwFlags & dwFlags) == dwFlags)
2651 piFormats[pfmt_it++] = i + 1;
2652 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n",
2653 it + 1, nCfgs, fmt_id, i + 1);
2654 break;
2660 *nNumFormats = pfmt_it;
2661 /** free list */
2662 XFree(cfgs);
2663 return GL_TRUE;
2667 * X11DRV_wglGetPixelFormatAttribivARB
2669 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2671 static BOOL X11DRV_wglGetPixelFormatAttribivARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2672 UINT nAttributes, const int *piAttributes, int *piValues )
2674 UINT i;
2675 const struct wgl_pixel_format *fmt;
2676 int hTest;
2677 int tmp;
2678 int curGLXAttr = 0;
2680 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2682 if (0 < iLayerPlane) {
2683 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2684 return GL_FALSE;
2687 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2688 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2689 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2690 fmt = get_pixel_format(gdi_display, iPixelFormat, TRUE /* Offscreen */);
2691 if(!fmt) {
2692 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2695 for (i = 0; i < nAttributes; ++i) {
2696 const int curWGLAttr = piAttributes[i];
2697 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2699 switch (curWGLAttr) {
2700 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2701 piValues[i] = nb_pixel_formats;
2702 continue;
2704 case WGL_SUPPORT_OPENGL_ARB:
2705 piValues[i] = GL_TRUE;
2706 continue;
2708 case WGL_ACCELERATION_ARB:
2709 curGLXAttr = GLX_CONFIG_CAVEAT;
2710 if (!fmt) goto pix_error;
2711 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2712 piValues[i] = WGL_NO_ACCELERATION_ARB;
2713 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2714 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2715 else
2716 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2717 continue;
2719 case WGL_TRANSPARENT_ARB:
2720 curGLXAttr = GLX_TRANSPARENT_TYPE;
2721 if (!fmt) goto pix_error;
2722 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2723 if (hTest) goto get_error;
2724 piValues[i] = GL_FALSE;
2725 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2726 continue;
2728 case WGL_PIXEL_TYPE_ARB:
2729 curGLXAttr = GLX_RENDER_TYPE;
2730 if (!fmt) goto pix_error;
2731 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2732 if (hTest) goto get_error;
2733 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2734 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2735 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2736 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2737 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2738 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2739 else {
2740 ERR("unexpected RenderType(%x)\n", tmp);
2741 piValues[i] = WGL_TYPE_RGBA_ARB;
2743 continue;
2745 case WGL_COLOR_BITS_ARB:
2746 curGLXAttr = GLX_BUFFER_SIZE;
2747 break;
2749 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2750 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2751 if (!use_render_texture_emulation) {
2752 piValues[i] = GL_FALSE;
2753 continue;
2755 curGLXAttr = GLX_RENDER_TYPE;
2756 if (!fmt) goto pix_error;
2757 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2758 if (hTest) goto get_error;
2759 if (GLX_COLOR_INDEX_BIT == tmp) {
2760 piValues[i] = GL_FALSE;
2761 continue;
2763 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2764 if (hTest) goto get_error;
2765 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2766 continue;
2768 case WGL_BLUE_BITS_ARB:
2769 curGLXAttr = GLX_BLUE_SIZE;
2770 break;
2771 case WGL_RED_BITS_ARB:
2772 curGLXAttr = GLX_RED_SIZE;
2773 break;
2774 case WGL_GREEN_BITS_ARB:
2775 curGLXAttr = GLX_GREEN_SIZE;
2776 break;
2777 case WGL_ALPHA_BITS_ARB:
2778 curGLXAttr = GLX_ALPHA_SIZE;
2779 break;
2780 case WGL_DEPTH_BITS_ARB:
2781 curGLXAttr = GLX_DEPTH_SIZE;
2782 break;
2783 case WGL_STENCIL_BITS_ARB:
2784 curGLXAttr = GLX_STENCIL_SIZE;
2785 break;
2786 case WGL_DOUBLE_BUFFER_ARB:
2787 curGLXAttr = GLX_DOUBLEBUFFER;
2788 break;
2789 case WGL_STEREO_ARB:
2790 curGLXAttr = GLX_STEREO;
2791 break;
2792 case WGL_AUX_BUFFERS_ARB:
2793 curGLXAttr = GLX_AUX_BUFFERS;
2794 break;
2796 case WGL_SUPPORT_GDI_ARB:
2797 if (!fmt) goto pix_error;
2798 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) != 0;
2799 continue;
2801 case WGL_DRAW_TO_BITMAP_ARB:
2802 if (!fmt) goto pix_error;
2803 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) != 0;
2804 continue;
2806 case WGL_DRAW_TO_WINDOW_ARB:
2807 case WGL_DRAW_TO_PBUFFER_ARB:
2808 if (!fmt) goto pix_error;
2809 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2810 if (hTest) goto get_error;
2811 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2812 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2813 piValues[i] = GL_TRUE;
2814 else
2815 piValues[i] = GL_FALSE;
2816 continue;
2818 case WGL_SWAP_METHOD_ARB:
2819 if (has_swap_method)
2821 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_SWAP_METHOD_OML, &tmp);
2822 if (hTest) goto get_error;
2823 switch (tmp)
2825 case GLX_SWAP_EXCHANGE_OML:
2826 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2827 break;
2828 case GLX_SWAP_COPY_OML:
2829 piValues[i] = WGL_SWAP_COPY_ARB;
2830 break;
2831 case GLX_SWAP_UNDEFINED_OML:
2832 piValues[i] = WGL_SWAP_UNDEFINED_ARB;
2833 break;
2834 default:
2835 ERR("Unexpected swap method %x.\n", tmp);
2838 else
2840 WARN("GLX_OML_swap_method not supported, returning WGL_SWAP_EXCHANGE_ARB.\n");
2841 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2843 continue;
2845 case WGL_PBUFFER_LARGEST_ARB:
2846 curGLXAttr = GLX_LARGEST_PBUFFER;
2847 break;
2849 case WGL_SAMPLE_BUFFERS_ARB:
2850 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2851 break;
2853 case WGL_SAMPLES_ARB:
2854 curGLXAttr = GLX_SAMPLES_ARB;
2855 break;
2857 case WGL_FLOAT_COMPONENTS_NV:
2858 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2859 break;
2861 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2862 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2863 break;
2865 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2866 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2867 break;
2869 case WGL_ACCUM_RED_BITS_ARB:
2870 curGLXAttr = GLX_ACCUM_RED_SIZE;
2871 break;
2872 case WGL_ACCUM_GREEN_BITS_ARB:
2873 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2874 break;
2875 case WGL_ACCUM_BLUE_BITS_ARB:
2876 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2877 break;
2878 case WGL_ACCUM_ALPHA_BITS_ARB:
2879 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2880 break;
2881 case WGL_ACCUM_BITS_ARB:
2882 if (!fmt) goto pix_error;
2883 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2884 if (hTest) goto get_error;
2885 piValues[i] = tmp;
2886 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2887 if (hTest) goto get_error;
2888 piValues[i] += tmp;
2889 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2890 if (hTest) goto get_error;
2891 piValues[i] += tmp;
2892 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2893 if (hTest) goto get_error;
2894 piValues[i] += tmp;
2895 continue;
2897 default:
2898 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2901 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2902 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2903 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2905 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2906 * and check which options can work using iPixelFormat=0 and which not.
2907 * A problem would be that this function is an extension. This would
2908 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2910 if (0 != curGLXAttr && iPixelFormat != 0) {
2911 if (!fmt) goto pix_error;
2912 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2913 if (hTest) goto get_error;
2914 curGLXAttr = 0;
2915 } else {
2916 piValues[i] = GL_FALSE;
2919 return GL_TRUE;
2921 get_error:
2922 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2923 return GL_FALSE;
2925 pix_error:
2926 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nb_pixel_formats);
2927 return GL_FALSE;
2931 * X11DRV_wglGetPixelFormatAttribfvARB
2933 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2935 static BOOL X11DRV_wglGetPixelFormatAttribfvARB( HDC hdc, int iPixelFormat, int iLayerPlane,
2936 UINT nAttributes, const int *piAttributes, FLOAT *pfValues )
2938 int *attr;
2939 int ret;
2940 UINT i;
2942 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2944 /* Allocate a temporary array to store integer values */
2945 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2946 if (!attr) {
2947 ERR("couldn't allocate %d array\n", nAttributes);
2948 return GL_FALSE;
2951 /* Piggy-back on wglGetPixelFormatAttribivARB */
2952 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2953 if (ret) {
2954 /* Convert integer values to float. Should also check for attributes
2955 that can give decimal values here */
2956 for (i=0; i<nAttributes;i++) {
2957 pfValues[i] = attr[i];
2961 HeapFree(GetProcessHeap(), 0, attr);
2962 return ret;
2966 * X11DRV_wglBindTexImageARB
2968 * WGL_ARB_render_texture: wglBindTexImageARB
2970 static BOOL X11DRV_wglBindTexImageARB( struct wgl_pbuffer *object, int iBuffer )
2972 GLboolean ret = GL_FALSE;
2974 TRACE("(%p, %d)\n", object, iBuffer);
2976 if (!object->use_render_texture) {
2977 SetLastError(ERROR_INVALID_HANDLE);
2978 return GL_FALSE;
2981 if (use_render_texture_emulation) {
2982 static BOOL initialized = FALSE;
2983 int prev_binded_texture = 0;
2984 GLXContext prev_context;
2985 GLXDrawable prev_drawable;
2987 prev_context = pglXGetCurrentContext();
2988 prev_drawable = pglXGetCurrentDrawable();
2990 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2991 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2992 isn't ideal performance wise but I wasn't able to get other ways working.
2994 if(!initialized) {
2995 initialized = TRUE; /* Only show the FIXME once for performance reasons */
2996 FIXME("partial stub!\n");
2999 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
3000 if (!object->tmp_context || object->prev_context != prev_context) {
3001 if (object->tmp_context)
3002 pglXDestroyContext(gdi_display, object->tmp_context);
3003 object->tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3004 object->prev_context = prev_context;
3007 opengl_funcs.gl.p_glGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3009 /* Switch to our pbuffer */
3010 pglXMakeCurrent(gdi_display, object->drawable, object->tmp_context);
3012 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3013 * After that copy the pbuffer texture data. */
3014 opengl_funcs.gl.p_glBindTexture(object->texture_target, prev_binded_texture);
3015 opengl_funcs.gl.p_glCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3017 /* Switch back to the original drawable and context */
3018 pglXMakeCurrent(gdi_display, prev_drawable, prev_context);
3019 return GL_TRUE;
3022 return ret;
3026 * X11DRV_wglReleaseTexImageARB
3028 * WGL_ARB_render_texture: wglReleaseTexImageARB
3030 static BOOL X11DRV_wglReleaseTexImageARB( struct wgl_pbuffer *object, int iBuffer )
3032 GLboolean ret = GL_FALSE;
3034 TRACE("(%p, %d)\n", object, iBuffer);
3036 if (!object->use_render_texture) {
3037 SetLastError(ERROR_INVALID_HANDLE);
3038 return GL_FALSE;
3040 if (use_render_texture_emulation) {
3041 return GL_TRUE;
3043 return ret;
3047 * X11DRV_wglGetExtensionsStringEXT
3049 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3051 static const char *X11DRV_wglGetExtensionsStringEXT(void)
3053 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3054 return WineGLInfo.wglExtensions;
3058 * X11DRV_wglGetSwapIntervalEXT
3060 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3062 static int X11DRV_wglGetSwapIntervalEXT(void)
3064 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3065 struct gl_drawable *gl;
3066 int swap_interval;
3068 TRACE("()\n");
3070 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3072 /* This can't happen because a current WGL context is required to get
3073 * here. Likely the application is buggy.
3075 WARN("No GL drawable found, returning swap interval 0\n");
3076 return 0;
3079 swap_interval = gl->swap_interval;
3080 release_gl_drawable(gl);
3082 return swap_interval;
3086 * X11DRV_wglSwapIntervalEXT
3088 * WGL_EXT_swap_control: wglSwapIntervalEXT
3090 static BOOL X11DRV_wglSwapIntervalEXT(int interval)
3092 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3093 struct gl_drawable *gl;
3094 BOOL ret;
3096 TRACE("(%d)\n", interval);
3098 /* Without WGL/GLX_EXT_swap_control_tear a negative interval
3099 * is invalid.
3101 if (interval < 0 && !has_swap_control_tear)
3103 SetLastError(ERROR_INVALID_DATA);
3104 return FALSE;
3107 if (!(gl = get_gl_drawable( WindowFromDC( ctx->hdc ), ctx->hdc )))
3109 SetLastError(ERROR_DC_NOT_FOUND);
3110 return FALSE;
3113 ret = set_swap_interval(gl->drawable, interval);
3114 gl->refresh_swap_interval = FALSE;
3115 if (ret)
3116 gl->swap_interval = interval;
3117 else
3118 SetLastError(ERROR_DC_NOT_FOUND);
3120 release_gl_drawable(gl);
3122 return ret;
3126 * X11DRV_wglSetPixelFormatWINE
3128 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3129 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3131 static BOOL X11DRV_wglSetPixelFormatWINE(HDC hdc, int format)
3133 return set_pixel_format(hdc, format, TRUE);
3136 static BOOL X11DRV_wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
3138 return pglXQueryCurrentRendererIntegerMESA( attribute, value );
3141 static const char *X11DRV_wglQueryCurrentRendererStringWINE( GLenum attribute )
3143 return pglXQueryCurrentRendererStringMESA( attribute );
3146 static BOOL X11DRV_wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
3148 return pglXQueryRendererIntegerMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute, value );
3151 static const char *X11DRV_wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
3153 return pglXQueryRendererStringMESA( gdi_display, DefaultScreen(gdi_display), renderer, attribute );
3157 * glxRequireVersion (internal)
3159 * Check if the supported GLX version matches requiredVersion.
3161 static BOOL glxRequireVersion(int requiredVersion)
3163 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3164 if(requiredVersion <= WineGLInfo.glxVersion[1])
3165 return TRUE;
3167 return FALSE;
3170 static void register_extension(const char *ext)
3172 if (WineGLInfo.wglExtensions[0])
3173 strcat(WineGLInfo.wglExtensions, " ");
3174 strcat(WineGLInfo.wglExtensions, ext);
3176 TRACE("'%s'\n", ext);
3180 * X11DRV_WineGL_LoadExtensions
3182 static void X11DRV_WineGL_LoadExtensions(void)
3184 WineGLInfo.wglExtensions[0] = 0;
3186 /* ARB Extensions */
3188 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context"))
3190 register_extension( "WGL_ARB_create_context" );
3191 opengl_funcs.ext.p_wglCreateContextAttribsARB = X11DRV_wglCreateContextAttribsARB;
3193 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_no_error" ))
3194 register_extension( "WGL_ARB_create_context_no_error" );
3195 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_create_context_profile"))
3196 register_extension("WGL_ARB_create_context_profile");
3199 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_fbconfig_float"))
3201 register_extension("WGL_ARB_pixel_format_float");
3202 register_extension("WGL_ATI_pixel_format_float");
3205 register_extension( "WGL_ARB_extensions_string" );
3206 opengl_funcs.ext.p_wglGetExtensionsStringARB = X11DRV_wglGetExtensionsStringARB;
3208 if (glxRequireVersion(3))
3210 register_extension( "WGL_ARB_make_current_read" );
3211 opengl_funcs.ext.p_wglGetCurrentReadDCARB = (void *)1; /* never called */
3212 opengl_funcs.ext.p_wglMakeContextCurrentARB = X11DRV_wglMakeContextCurrentARB;
3215 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_multisample")) register_extension( "WGL_ARB_multisample" );
3217 if (glxRequireVersion(3))
3219 register_extension( "WGL_ARB_pbuffer" );
3220 opengl_funcs.ext.p_wglCreatePbufferARB = X11DRV_wglCreatePbufferARB;
3221 opengl_funcs.ext.p_wglDestroyPbufferARB = X11DRV_wglDestroyPbufferARB;
3222 opengl_funcs.ext.p_wglGetPbufferDCARB = X11DRV_wglGetPbufferDCARB;
3223 opengl_funcs.ext.p_wglQueryPbufferARB = X11DRV_wglQueryPbufferARB;
3224 opengl_funcs.ext.p_wglReleasePbufferDCARB = X11DRV_wglReleasePbufferDCARB;
3225 opengl_funcs.ext.p_wglSetPbufferAttribARB = X11DRV_wglSetPbufferAttribARB;
3228 register_extension( "WGL_ARB_pixel_format" );
3229 opengl_funcs.ext.p_wglChoosePixelFormatARB = X11DRV_wglChoosePixelFormatARB;
3230 opengl_funcs.ext.p_wglGetPixelFormatAttribfvARB = X11DRV_wglGetPixelFormatAttribfvARB;
3231 opengl_funcs.ext.p_wglGetPixelFormatAttribivARB = X11DRV_wglGetPixelFormatAttribivARB;
3233 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3234 if (has_extension( WineGLInfo.glxExtensions, "GLX_ARB_render_texture") ||
3235 (glxRequireVersion(3) && use_render_texture_emulation))
3237 register_extension( "WGL_ARB_render_texture" );
3238 opengl_funcs.ext.p_wglBindTexImageARB = X11DRV_wglBindTexImageARB;
3239 opengl_funcs.ext.p_wglReleaseTexImageARB = X11DRV_wglReleaseTexImageARB;
3241 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3242 if (has_extension( WineGLInfo.glxExtensions, "GLX_NV_float_buffer"))
3243 register_extension("WGL_NV_float_buffer");
3245 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3246 if (has_extension(WineGLInfo.glExtensions, "GL_NV_texture_rectangle"))
3247 register_extension("WGL_NV_render_texture_rectangle");
3250 /* EXT Extensions */
3252 register_extension( "WGL_EXT_extensions_string" );
3253 opengl_funcs.ext.p_wglGetExtensionsStringEXT = X11DRV_wglGetExtensionsStringEXT;
3255 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3256 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3257 register_extension( "WGL_EXT_swap_control" );
3258 opengl_funcs.ext.p_wglSwapIntervalEXT = X11DRV_wglSwapIntervalEXT;
3259 opengl_funcs.ext.p_wglGetSwapIntervalEXT = X11DRV_wglGetSwapIntervalEXT;
3261 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_framebuffer_sRGB"))
3262 register_extension("WGL_EXT_framebuffer_sRGB");
3264 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_fbconfig_packed_float"))
3265 register_extension("WGL_EXT_pixel_format_packed_float");
3267 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control"))
3269 swap_control_method = GLX_SWAP_CONTROL_EXT;
3270 if (has_extension( WineGLInfo.glxExtensions, "GLX_EXT_swap_control_tear"))
3272 register_extension("WGL_EXT_swap_control_tear");
3273 has_swap_control_tear = TRUE;
3276 else if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_swap_control"))
3278 swap_control_method = GLX_SWAP_CONTROL_MESA;
3280 else if (has_extension( WineGLInfo.glxExtensions, "GLX_SGI_swap_control"))
3282 swap_control_method = GLX_SWAP_CONTROL_SGI;
3285 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3286 if (has_extension(WineGLInfo.glExtensions, "GL_NV_vertex_array_range"))
3288 register_extension( "WGL_NV_vertex_array_range" );
3289 opengl_funcs.ext.p_wglAllocateMemoryNV = pglXAllocateMemoryNV;
3290 opengl_funcs.ext.p_wglFreeMemoryNV = pglXFreeMemoryNV;
3293 if (has_extension(WineGLInfo.glxExtensions, "GLX_OML_swap_method"))
3294 has_swap_method = TRUE;
3296 /* WINE-specific WGL Extensions */
3298 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3299 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3301 register_extension( "WGL_WINE_pixel_format_passthrough" );
3302 opengl_funcs.ext.p_wglSetPixelFormatWINE = X11DRV_wglSetPixelFormatWINE;
3304 if (has_extension( WineGLInfo.glxExtensions, "GLX_MESA_query_renderer" ))
3306 register_extension( "WGL_WINE_query_renderer" );
3307 opengl_funcs.ext.p_wglQueryCurrentRendererIntegerWINE = X11DRV_wglQueryCurrentRendererIntegerWINE;
3308 opengl_funcs.ext.p_wglQueryCurrentRendererStringWINE = X11DRV_wglQueryCurrentRendererStringWINE;
3309 opengl_funcs.ext.p_wglQueryRendererIntegerWINE = X11DRV_wglQueryRendererIntegerWINE;
3310 opengl_funcs.ext.p_wglQueryRendererStringWINE = X11DRV_wglQueryRendererStringWINE;
3316 * glxdrv_SwapBuffers
3318 * Swap the buffers of this DC
3320 static BOOL glxdrv_wglSwapBuffers( HDC hdc )
3322 struct x11drv_escape_flush_gl_drawable escape;
3323 struct gl_drawable *gl;
3324 struct wgl_context *ctx = NtCurrentTeb()->glContext;
3325 INT64 ust, msc, sbc, target_sbc = 0;
3327 TRACE("(%p)\n", hdc);
3329 escape.code = X11DRV_FLUSH_GL_DRAWABLE;
3330 escape.gl_drawable = 0;
3332 if (!(gl = get_gl_drawable( WindowFromDC( hdc ), hdc )))
3334 SetLastError( ERROR_INVALID_HANDLE );
3335 return FALSE;
3338 if (gl->refresh_swap_interval)
3340 set_swap_interval(gl->drawable, gl->swap_interval);
3341 gl->refresh_swap_interval = FALSE;
3344 switch (gl->type)
3346 case DC_GL_PIXMAP_WIN:
3347 if (ctx) sync_context( ctx );
3348 escape.gl_drawable = gl->pixmap;
3349 if (pglXCopySubBufferMESA) {
3350 /* (glX)SwapBuffers has an implicit glFlush effect, however
3351 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3352 * copying */
3353 pglFlush();
3354 pglXCopySubBufferMESA( gdi_display, gl->drawable, 0, 0,
3355 gl->rect.right - gl->rect.left, gl->rect.bottom - gl->rect.top );
3356 break;
3358 if (pglXSwapBuffersMscOML)
3360 pglFlush();
3361 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3362 break;
3364 pglXSwapBuffers(gdi_display, gl->drawable);
3365 break;
3366 case DC_GL_CHILD_WIN:
3367 if (ctx) sync_context( ctx );
3368 escape.gl_drawable = gl->window;
3369 /* fall through */
3370 default:
3371 if (escape.gl_drawable && pglXSwapBuffersMscOML)
3373 pglFlush();
3374 target_sbc = pglXSwapBuffersMscOML( gdi_display, gl->drawable, 0, 0, 0 );
3375 break;
3377 pglXSwapBuffers(gdi_display, gl->drawable);
3378 break;
3381 if (escape.gl_drawable && pglXWaitForSbcOML)
3382 pglXWaitForSbcOML( gdi_display, gl->drawable, target_sbc, &ust, &msc, &sbc );
3384 release_gl_drawable( gl );
3386 if (escape.gl_drawable) ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
3387 return TRUE;
3390 static struct opengl_funcs opengl_funcs =
3393 glxdrv_wglCopyContext, /* p_wglCopyContext */
3394 glxdrv_wglCreateContext, /* p_wglCreateContext */
3395 glxdrv_wglDeleteContext, /* p_wglDeleteContext */
3396 glxdrv_wglDescribePixelFormat, /* p_wglDescribePixelFormat */
3397 glxdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
3398 glxdrv_wglGetProcAddress, /* p_wglGetProcAddress */
3399 glxdrv_wglMakeCurrent, /* p_wglMakeCurrent */
3400 glxdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
3401 glxdrv_wglShareLists, /* p_wglShareLists */
3402 glxdrv_wglSwapBuffers, /* p_wglSwapBuffers */
3406 struct opengl_funcs *get_glx_driver( UINT version )
3408 if (version != WINE_WGL_DRIVER_VERSION)
3410 ERR( "version mismatch, opengl32 wants %u but driver has %u\n", version, WINE_WGL_DRIVER_VERSION );
3411 return NULL;
3413 if (has_opengl()) return &opengl_funcs;
3414 return NULL;
3417 #else /* no OpenGL includes */
3419 struct opengl_funcs *get_glx_driver( UINT version )
3421 return NULL;
3424 void sync_gl_drawable( HWND hwnd, const RECT *visible_rect, const RECT *client_rect )
3428 void set_gl_drawable_parent( HWND hwnd, HWND parent )
3432 void destroy_gl_drawable( HWND hwnd )
3436 #endif /* defined(SONAME_LIBGL) */