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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
34 #include "wine/library.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
58 /* Redefines the constants */
59 #define CALLBACK __stdcall
60 #define WINAPI __stdcall
61 #define APIENTRY WINAPI
64 WINE_DECLARE_DEBUG_CHANNEL(fps);
66 typedef struct wine_glextension {
75 const char *glVersion;
80 const char *glxServerVersion;
81 const char *glxServerVendor;
82 const char *glxServerExtensions;
84 const char *glxClientVersion;
85 const char *glxClientVendor;
86 const char *glxClientExtensions;
88 const char *glxExtensions;
91 char wglExtensions[4096];
94 typedef struct wine_glpixelformat {
100 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
103 typedef struct wine_glcontext {
107 WineGLPixelFormat *fmt;
110 Drawable drawables[2];
111 BOOL refresh_drawables;
112 struct wine_glcontext *next;
113 struct wine_glcontext *prev;
116 typedef struct wine_glpbuffer {
119 WineGLPixelFormat* fmt;
125 int use_render_texture; /* This is also the internal texture format */
126 int texture_bind_target;
128 GLint texture_format;
129 GLuint texture_target;
135 static Wine_GLContext *context_list;
136 static struct WineGLInfo WineGLInfo = { 0 };
137 static int use_render_texture_emulation = 1;
138 static int use_render_texture_ati = 0;
139 static int swap_interval = 1;
141 #define MAX_EXTENSIONS 16
142 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
143 static int WineGLExtensionListSize;
145 static void X11DRV_WineGL_LoadExtensions(void);
146 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count);
147 static BOOL glxRequireVersion(int requiredVersion);
148 static BOOL glxRequireExtension(const char *requiredExtension);
150 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
151 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
152 TRACE(" - dwFlags : ");
153 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
154 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
155 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
156 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
157 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
158 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
159 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
160 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
161 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
162 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
163 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
164 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
165 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
166 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
167 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
168 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
169 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
170 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
171 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
172 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
176 TRACE(" - iPixelType : ");
177 switch (ppfd->iPixelType) {
178 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
179 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
183 TRACE(" - Color : %d\n", ppfd->cColorBits);
184 TRACE(" - Red : %d\n", ppfd->cRedBits);
185 TRACE(" - Green : %d\n", ppfd->cGreenBits);
186 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
187 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
188 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
189 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
190 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
191 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
193 TRACE(" - iLayerType : ");
194 switch (ppfd->iLayerType) {
195 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
196 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
197 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
202 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
203 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
205 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
207 MAKE_FUNCPTR(glXChooseVisual)
208 MAKE_FUNCPTR(glXCopyContext)
209 MAKE_FUNCPTR(glXCreateContext)
210 MAKE_FUNCPTR(glXCreateGLXPixmap)
211 MAKE_FUNCPTR(glXGetCurrentContext)
212 MAKE_FUNCPTR(glXGetCurrentDrawable)
213 MAKE_FUNCPTR(glXDestroyContext)
214 MAKE_FUNCPTR(glXDestroyGLXPixmap)
215 MAKE_FUNCPTR(glXGetConfig)
216 MAKE_FUNCPTR(glXIsDirect)
217 MAKE_FUNCPTR(glXMakeCurrent)
218 MAKE_FUNCPTR(glXSwapBuffers)
219 MAKE_FUNCPTR(glXQueryExtension)
220 MAKE_FUNCPTR(glXQueryVersion)
221 MAKE_FUNCPTR(glXUseXFont)
224 MAKE_FUNCPTR(glXGetClientString)
225 MAKE_FUNCPTR(glXQueryExtensionsString)
226 MAKE_FUNCPTR(glXQueryServerString)
229 MAKE_FUNCPTR(glXGetFBConfigs)
230 MAKE_FUNCPTR(glXChooseFBConfig)
231 MAKE_FUNCPTR(glXCreatePbuffer)
232 MAKE_FUNCPTR(glXCreateNewContext)
233 MAKE_FUNCPTR(glXDestroyPbuffer)
234 MAKE_FUNCPTR(glXGetFBConfigAttrib)
235 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
236 MAKE_FUNCPTR(glXMakeContextCurrent)
237 MAKE_FUNCPTR(glXQueryDrawable)
238 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
241 static void* (*pglXGetProcAddressARB)(const GLubyte *);
242 static int (*pglXSwapIntervalSGI)(int);
244 /* ATI GLX Extensions */
245 static BOOL (*pglXBindTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
246 static BOOL (*pglXReleaseTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
247 static BOOL (*pglXDrawableAttribATI)(Display *dpy, GLXDrawable draw, const int *attribList);
249 /* NV GLX Extension */
250 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
251 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
253 /* MESA GLX Extensions */
254 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
256 /* Standard OpenGL */
257 MAKE_FUNCPTR(glBindTexture)
258 MAKE_FUNCPTR(glBitmap)
259 MAKE_FUNCPTR(glCopyTexSubImage1D)
260 MAKE_FUNCPTR(glCopyTexImage2D)
261 MAKE_FUNCPTR(glCopyTexSubImage2D)
262 MAKE_FUNCPTR(glDrawBuffer)
263 MAKE_FUNCPTR(glEndList)
264 MAKE_FUNCPTR(glGetError)
265 MAKE_FUNCPTR(glGetIntegerv)
266 MAKE_FUNCPTR(glGetString)
267 MAKE_FUNCPTR(glNewList)
268 MAKE_FUNCPTR(glPixelStorei)
269 MAKE_FUNCPTR(glReadPixels)
270 MAKE_FUNCPTR(glTexImage2D)
271 MAKE_FUNCPTR(glFinish)
272 MAKE_FUNCPTR(glFlush)
275 static BOOL infoInitialized = FALSE;
276 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
279 int screen = DefaultScreen(gdi_display);
280 Window win = RootWindow(gdi_display, screen);
283 XVisualInfo template;
286 GLXContext ctx = NULL;
290 infoInitialized = TRUE;
294 visual = DefaultVisual(gdi_display, screen);
295 template.visualid = XVisualIDFromVisual(visual);
296 vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
298 WORD old_fs = wine_get_fs();
299 /* Create a GLX Context. Without one we can't query GL information */
300 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
301 if (wine_get_fs() != old_fs)
303 wine_set_fs( old_fs );
305 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
306 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
312 pglXMakeCurrent(gdi_display, win, ctx);
314 ERR(" couldn't initialize OpenGL, expect problems\n");
319 WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
320 str = (const char *) pglGetString(GL_EXTENSIONS);
321 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
322 strcpy(WineGLInfo.glExtensions, str);
324 /* Get the common GLX version supported by GLX client and server ( major/minor) */
325 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
327 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
328 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
329 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
331 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
332 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
333 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
335 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
336 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
338 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
339 TRACE("GL renderer : %s.\n", pglGetString(GL_RENDERER));
340 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
341 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
342 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
343 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
344 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
345 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
349 pglXMakeCurrent(gdi_display, None, NULL);
350 pglXDestroyContext(gdi_display, ctx);
356 void X11DRV_OpenGL_Cleanup(void)
358 HeapFree(GetProcessHeap(), 0, WineGLInfo.glExtensions);
359 infoInitialized = FALSE;
362 static BOOL has_opengl(void)
364 static int init_done;
365 static void *opengl_handle;
368 int error_base, event_base;
370 if (init_done) return (opengl_handle != NULL);
373 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
374 and include all dependencies */
375 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
376 if (opengl_handle == NULL)
378 ERR( "Failed to load libGL: %s\n", buffer );
379 ERR( "OpenGL support is disabled.\n");
383 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
384 if (pglXGetProcAddressARB == NULL) {
385 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
389 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
391 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
396 LOAD_FUNCPTR(glXChooseVisual);
397 LOAD_FUNCPTR(glXCopyContext);
398 LOAD_FUNCPTR(glXCreateContext);
399 LOAD_FUNCPTR(glXCreateGLXPixmap);
400 LOAD_FUNCPTR(glXGetCurrentContext);
401 LOAD_FUNCPTR(glXGetCurrentDrawable);
402 LOAD_FUNCPTR(glXDestroyContext);
403 LOAD_FUNCPTR(glXDestroyGLXPixmap);
404 LOAD_FUNCPTR(glXGetConfig);
405 LOAD_FUNCPTR(glXIsDirect);
406 LOAD_FUNCPTR(glXMakeCurrent);
407 LOAD_FUNCPTR(glXSwapBuffers);
408 LOAD_FUNCPTR(glXQueryExtension);
409 LOAD_FUNCPTR(glXQueryVersion);
410 LOAD_FUNCPTR(glXUseXFont);
413 LOAD_FUNCPTR(glXGetClientString);
414 LOAD_FUNCPTR(glXQueryExtensionsString);
415 LOAD_FUNCPTR(glXQueryServerString);
418 LOAD_FUNCPTR(glXCreatePbuffer);
419 LOAD_FUNCPTR(glXCreateNewContext);
420 LOAD_FUNCPTR(glXDestroyPbuffer);
421 LOAD_FUNCPTR(glXMakeContextCurrent);
422 LOAD_FUNCPTR(glXGetCurrentReadDrawable);
423 LOAD_FUNCPTR(glXGetFBConfigs);
425 /* Standard OpenGL calls */
426 LOAD_FUNCPTR(glBindTexture);
427 LOAD_FUNCPTR(glBitmap);
428 LOAD_FUNCPTR(glCopyTexSubImage1D);
429 LOAD_FUNCPTR(glCopyTexImage2D);
430 LOAD_FUNCPTR(glCopyTexSubImage2D);
431 LOAD_FUNCPTR(glDrawBuffer);
432 LOAD_FUNCPTR(glEndList);
433 LOAD_FUNCPTR(glGetError);
434 LOAD_FUNCPTR(glGetIntegerv);
435 LOAD_FUNCPTR(glGetString);
436 LOAD_FUNCPTR(glNewList);
437 LOAD_FUNCPTR(glPixelStorei);
438 LOAD_FUNCPTR(glReadPixels);
439 LOAD_FUNCPTR(glTexImage2D);
440 LOAD_FUNCPTR(glFinish);
441 LOAD_FUNCPTR(glFlush);
444 /* It doesn't matter if these fail. They'll only be used if the driver reports
445 the associated extension is available (and if a driver reports the extension
446 is available but fails to provide the functions, it's quite broken) */
447 #define LOAD_FUNCPTR(f) p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)
448 /* NV GLX Extension */
449 LOAD_FUNCPTR(glXAllocateMemoryNV);
450 LOAD_FUNCPTR(glXFreeMemoryNV);
453 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
456 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
457 TRACE("GLX is up and running error_base = %d\n", error_base);
460 ERR( "GLX extension is missing, disabling OpenGL.\n" );
464 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
465 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
468 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
469 * depends on the reported GLX client / server version and on the client / server extension list.
470 * Those don't have to be the same.
472 * In general the server GLX information lists the capabilities in case of indirect rendering.
473 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
474 * available and in that case the client GLX informat can be used.
475 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
476 * in the GLX version/extension list. When a program does this it works for certain for both
477 * direct and indirect rendering.
479 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
480 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
481 * extension list which causes this extension not to be listed. (Wine requires this extension).
482 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
483 * pbuffers is around.
485 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
486 * the ATI drivers and from then on use GLX client information for them.
489 if(glxRequireVersion(3)) {
490 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
491 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
492 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
493 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
494 } else if(glxRequireExtension("GLX_SGIX_fbconfig")) {
495 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
496 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
497 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
499 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
500 * enable this function when the Xserver understand GLX 1.3 or newer
502 pglXQueryDrawable = NULL;
503 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
504 TRACE("Overriding ATI GLX capabilities!\n");
505 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
506 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
507 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
508 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
510 /* Use client GLX information in case of the ATI drivers. We override the
511 * capabilities over here and not somewhere else as ATI might better their
512 * life in the future. In case they release proper drivers this block of
513 * code won't be called. */
514 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
516 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
519 if(glxRequireExtension("GLX_ATI_render_texture")) {
520 use_render_texture_ati = 1;
521 pglXBindTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageATI");
522 pglXReleaseTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageATI");
523 pglXDrawableAttribATI = pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribATI");
526 if(glxRequireExtension("GLX_MESA_copy_sub_buffer")) {
527 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
530 X11DRV_WineGL_LoadExtensions();
536 wine_dlclose(opengl_handle, NULL, 0);
537 opengl_handle = NULL;
541 static inline Wine_GLContext *alloc_context(void)
545 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
547 ret->next = context_list;
548 if (context_list) context_list->prev = ret;
554 static inline void free_context(Wine_GLContext *context)
556 if (context->next != NULL) context->next->prev = context->prev;
557 if (context->prev != NULL) context->prev->next = context->next;
558 else context_list = context->next;
560 if (context->vis) XFree(context->vis);
561 HeapFree(GetProcessHeap(), 0, context);
564 static inline BOOL is_valid_context( Wine_GLContext *ctx )
567 for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
568 return (ptr != NULL);
571 static int describeContext(Wine_GLContext* ctx) {
574 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
575 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
576 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
577 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
578 TRACE(" - VISUAL_ID 0x%x\n", tmp);
583 static BOOL describeDrawable(X11DRV_PDEVICE *physDev) {
585 WineGLPixelFormat *fmt;
588 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE /* Offscreen */, &fmt_count);
589 if(!fmt) return FALSE;
591 TRACE(" HDC %p has:\n", physDev->hdc);
592 TRACE(" - iPixelFormat %d\n", fmt->iPixelFormat);
593 TRACE(" - Drawable %p\n", (void*) get_glxdrawable(physDev));
594 TRACE(" - FBCONFIG_ID 0x%x\n", fmt->fmt_id);
596 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &tmp);
597 TRACE(" - VISUAL_ID 0x%x\n", tmp);
602 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
607 int nvfloatattrib = GLX_DONT_CARE;
610 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
611 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
612 while (iWGLAttr && 0 != iWGLAttr[cur]) {
613 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
615 switch (iWGLAttr[cur]) {
616 case WGL_AUX_BUFFERS_ARB:
617 pop = iWGLAttr[++cur];
618 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
619 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
621 case WGL_COLOR_BITS_ARB:
622 pop = iWGLAttr[++cur];
623 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
624 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
626 case WGL_BLUE_BITS_ARB:
627 pop = iWGLAttr[++cur];
628 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
629 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
631 case WGL_RED_BITS_ARB:
632 pop = iWGLAttr[++cur];
633 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
634 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
636 case WGL_GREEN_BITS_ARB:
637 pop = iWGLAttr[++cur];
638 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
639 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
641 case WGL_ALPHA_BITS_ARB:
642 pop = iWGLAttr[++cur];
643 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
644 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
646 case WGL_DEPTH_BITS_ARB:
647 pop = iWGLAttr[++cur];
648 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
649 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
651 case WGL_STENCIL_BITS_ARB:
652 pop = iWGLAttr[++cur];
653 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
654 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
656 case WGL_DOUBLE_BUFFER_ARB:
657 pop = iWGLAttr[++cur];
658 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
659 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
662 pop = iWGLAttr[++cur];
663 PUSH2(oGLXAttr, GLX_STEREO, pop);
664 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
667 case WGL_PIXEL_TYPE_ARB:
668 pop = iWGLAttr[++cur];
669 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
671 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
672 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
673 /* 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 */
674 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
675 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
677 ERR("unexpected PixelType(%x)\n", pop);
682 case WGL_SUPPORT_GDI_ARB:
683 /* This flag is set in a WineGLPixelFormat */
684 pop = iWGLAttr[++cur];
685 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
688 case WGL_DRAW_TO_BITMAP_ARB:
689 /* This flag is set in a WineGLPixelFormat */
690 pop = iWGLAttr[++cur];
691 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
694 case WGL_DRAW_TO_WINDOW_ARB:
695 pop = iWGLAttr[++cur];
696 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
697 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
699 drawattrib |= GLX_WINDOW_BIT;
703 case WGL_DRAW_TO_PBUFFER_ARB:
704 pop = iWGLAttr[++cur];
705 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
706 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
708 drawattrib |= GLX_PBUFFER_BIT;
712 case WGL_ACCELERATION_ARB:
713 /* This flag is set in a WineGLPixelFormat */
714 pop = iWGLAttr[++cur];
715 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
718 case WGL_SUPPORT_OPENGL_ARB:
719 pop = iWGLAttr[++cur];
720 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
721 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
724 case WGL_SWAP_METHOD_ARB:
725 pop = iWGLAttr[++cur];
726 /* For now we ignore this and just return SWAP_EXCHANGE */
727 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
730 case WGL_PBUFFER_LARGEST_ARB:
731 pop = iWGLAttr[++cur];
732 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
733 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
736 case WGL_SAMPLE_BUFFERS_ARB:
737 pop = iWGLAttr[++cur];
738 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
739 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
742 case WGL_SAMPLES_ARB:
743 pop = iWGLAttr[++cur];
744 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
745 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
748 case WGL_TEXTURE_FORMAT_ARB:
749 case WGL_TEXTURE_TARGET_ARB:
750 case WGL_MIPMAP_TEXTURE_ARB:
751 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
752 pop = iWGLAttr[++cur];
754 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
756 if (use_render_texture_ati) {
757 /** nothing to do here */
759 else if (!use_render_texture_emulation) {
760 if (WGL_NO_TEXTURE_ARB != pop) {
761 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
762 return -1; /** error: don't support it */
764 PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
765 drawattrib |= GLX_PBUFFER_BIT;
769 case WGL_FLOAT_COMPONENTS_NV:
770 nvfloatattrib = iWGLAttr[++cur];
771 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
773 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
774 case WGL_BIND_TO_TEXTURE_RGB_ARB:
775 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
776 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
777 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
778 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
779 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
780 pop = iWGLAttr[++cur];
781 /** cannot be converted, see direct handling on
782 * - wglGetPixelFormatAttribivARB
783 * TODO: wglChoosePixelFormat
786 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
787 pop = iWGLAttr[++cur];
788 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
789 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
792 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
793 pop = iWGLAttr[++cur];
794 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
795 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
798 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
804 /* Apply the OR'd drawable type bitmask now EVEN when WGL_DRAW_TO* is unset.
805 * It is needed in all cases because GLX_DRAWABLE_TYPE default to GLX_WINDOW_BIT. */
806 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
807 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
809 /* Set GLX_RENDER_TYPE all the time */
810 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
811 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
813 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
814 if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
815 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
816 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
822 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
824 int render_type=0, render_type_bit;
825 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
826 switch(render_type_bit)
829 render_type = GLX_RGBA_TYPE;
831 case GLX_COLOR_INDEX_BIT:
832 render_type = GLX_COLOR_INDEX_TYPE;
834 case GLX_RGBA_FLOAT_BIT:
835 render_type = GLX_RGBA_FLOAT_TYPE;
837 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
838 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
841 ERR("Unknown render_type: %x\n", render_type);
846 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
847 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
850 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
851 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
853 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
854 * the GLX_PIXMAP_BIT set. */
855 return !dbuf && (value & GLX_PIXMAP_BIT);
858 static WineGLPixelFormat *get_formats(Display *display, int *size_ret, int *onscreen_size_ret)
860 static WineGLPixelFormat *list;
861 static int size, onscreen_size;
863 int fmt_id, nCfgs, i, run, bmp_formats;
865 XVisualInfo *visinfo;
870 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
871 if (NULL == cfgs || 0 == nCfgs) {
872 if(cfgs != NULL) XFree(cfgs);
874 ERR("glXChooseFBConfig returns NULL\n");
878 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
879 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
880 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
881 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
883 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
885 for(i=0, bmp_formats=0; i<nCfgs; i++)
887 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
890 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
892 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats)*sizeof(WineGLPixelFormat));
894 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
895 * Do this as GLX doesn't guarantee that the list is sorted */
896 for(run=0; run < 2; run++)
898 for(i=0; i<nCfgs; i++) {
899 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
900 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
902 /* The first run we only add onscreen formats (ones which have an associated X Visual).
903 * The second run we only set offscreen formats. */
906 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
907 * The contents is copied to the destination using XCopyArea. For the copying to work
908 * the depth of the source and destination window should be the same. In general this should
909 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
910 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
911 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
912 * list formats with the same depth. */
913 if(visinfo->depth != screen_depth)
916 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
917 list[size].iPixelFormat = size+1; /* The index starts at 1 */
918 list[size].fbconfig = cfgs[i];
919 list[size].fmt_id = fmt_id;
920 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
921 list[size].offscreenOnly = FALSE;
922 list[size].dwFlags = 0;
926 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
927 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
929 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
930 list[size].iPixelFormat = size+1; /* The index starts at 1 */
931 list[size].fbconfig = cfgs[i];
932 list[size].fmt_id = fmt_id;
933 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
934 list[size].offscreenOnly = FALSE;
935 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
941 } else if(run && !visinfo) {
942 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
943 list[size].iPixelFormat = size+1; /* The index starts at 1 */
944 list[size].fbconfig = cfgs[i];
945 list[size].fmt_id = fmt_id;
946 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
947 list[size].offscreenOnly = TRUE;
948 list[size].dwFlags = 0;
954 if(cfgs != NULL) XFree(cfgs);
957 if (size_ret) *size_ret = size;
958 if (onscreen_size_ret) *onscreen_size_ret = onscreen_size;
963 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
964 * In our WGL implementation we only support a subset of these formats namely the format of
965 * Wine's main visual and offscreen formats (if they are available).
966 * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
967 * and it returns the number of supported WGL formats in fmt_count.
969 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
971 WineGLPixelFormat *list, *res = NULL;
972 int size, onscreen_size;
974 if (!(list = get_formats(display, &size, &onscreen_size ))) return NULL;
976 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
977 * iPixelFormat in case of probing the number of pixelformats.
979 if((iPixelFormat > 0) && (iPixelFormat <= size) &&
980 (!list[iPixelFormat-1].offscreenOnly || AllowOffscreen)) {
981 res = &list[iPixelFormat-1];
982 TRACE("Returning FBConfig=%p for iPixelFormat=%d\n", res->fbconfig, iPixelFormat);
988 *fmt_count = onscreen_size;
990 TRACE("Number of returned pixelformats=%d\n", *fmt_count);
995 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
996 static WineGLPixelFormat* ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id, DWORD dwFlags)
998 WineGLPixelFormat *list;
1001 if (!(list = get_formats(display, &size, NULL ))) return NULL;
1003 for(i=0; i<size; i++) {
1004 /* A GLX format can appear multiple times in the pixel format list due to fake formats for bitmap rendering.
1005 * Fake formats might get selected when the user passes the proper flags using the dwFlags parameter. */
1006 if( (list[i].fmt_id == fmt_id) && ((list[i].dwFlags & dwFlags) == dwFlags) ) {
1007 TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", list[i].iPixelFormat, fmt_id);
1011 TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1015 int pixelformat_from_fbconfig_id(XID fbconfig_id)
1017 WineGLPixelFormat *fmt;
1019 if (!fbconfig_id) return 0;
1021 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
1023 return fmt->iPixelFormat;
1024 /* This will happen on hwnds without a pixel format set; it's ok */
1029 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1030 void mark_drawable_dirty(Drawable old, Drawable new)
1032 Wine_GLContext *ctx;
1033 for (ctx = context_list; ctx; ctx = ctx->next) {
1034 if (old == ctx->drawables[0]) {
1035 ctx->drawables[0] = new;
1036 ctx->refresh_drawables = TRUE;
1038 if (old == ctx->drawables[1]) {
1039 ctx->drawables[1] = new;
1040 ctx->refresh_drawables = TRUE;
1045 /* Given the current context, make sure its drawable is sync'd */
1046 static inline void sync_context(Wine_GLContext *context)
1048 if(context && context->refresh_drawables) {
1049 if (glxRequireVersion(3))
1050 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1051 context->drawables[1], context->ctx);
1053 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1054 context->refresh_drawables = FALSE;
1059 static GLXContext create_glxcontext(Display *display, Wine_GLContext *context, GLXContext shareList)
1063 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1064 BOOL indirect = (context->fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? FALSE : TRUE;
1067 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1068 else /* Create a GLX Context for a pbuffer */
1069 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1075 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
1077 return pglXCreateGLXPixmap(display, vis, parent);
1081 static XID create_bitmap_glxpixmap(X11DRV_PDEVICE *physDev, WineGLPixelFormat *fmt)
1088 vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1090 if(vis->depth == physDev->bitmap->pixmap_depth)
1091 ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
1094 wine_tsx11_unlock();
1095 TRACE("return %lx\n", ret);
1100 * X11DRV_ChoosePixelFormat
1102 * Equivalent to glXChooseVisual.
1104 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
1105 const PIXELFORMATDESCRIPTOR *ppfd) {
1106 WineGLPixelFormat *list;
1111 int bestFormat = -1;
1112 int bestDBuffer = -1;
1113 int bestStereo = -1;
1117 int bestStencil = -1;
1120 if (!has_opengl()) return 0;
1122 if (TRACE_ON(wgl)) {
1123 TRACE("(%p,%p)\n", physDev, ppfd);
1125 dump_PIXELFORMATDESCRIPTOR(ppfd);
1128 if (!(list = get_formats(gdi_display, NULL, &onscreen_size ))) return 0;
1131 for(i=0; i<onscreen_size; i++)
1135 int alpha=0, color=0, depth=0, stencil=0, aux=0;
1136 WineGLPixelFormat *fmt = &list[i];
1139 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1140 if (value & GLX_RGBA_BIT)
1141 iPixelType = PFD_TYPE_RGBA;
1143 iPixelType = PFD_TYPE_COLORINDEX;
1145 if (ppfd->iPixelType != iPixelType)
1147 TRACE("pixel type mismatch for iPixelFormat=%d\n", i+1);
1151 /* Only use bitmap capable for formats for bitmap rendering.
1152 * See get_formats for more info. */
1153 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (fmt->dwFlags & PFD_DRAW_TO_BITMAP))
1155 TRACE("PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i+1);
1159 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1160 if (value) dwFlags |= PFD_DOUBLEBUFFER;
1161 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value);
1162 if (value) dwFlags |= PFD_STEREO;
1163 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &color); /* cColorBits */
1164 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &alpha); /* cAlphaBits */
1165 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &depth); /* cDepthBits */
1166 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &stencil); /* cStencilBits */
1167 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &aux); /* cAuxBuffers */
1169 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
1170 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
1171 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
1172 * formats without the given flag set.
1173 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
1174 * has indicated that a format without stereo is returned when stereo is unavailable.
1175 * So in case PFD_STEREO is set, formats that support it should have priority above formats
1176 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
1178 * To summarize the following is most likely the correct behavior:
1179 * stereo not set -> prefer no-stereo formats, else also accept stereo formats
1180 * stereo set -> prefer stereo formats, else also accept no-stereo formats
1181 * stereo don't care -> it doesn't matter whether we get stereo or not
1183 * In Wine we will treat no-stereo the same way as don't care because it makes
1184 * format selection even more complicated and second drivers with Stereo advertise
1185 * each format twice anyway.
1188 /* Doublebuffer, see the comments above */
1189 if( !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) ) {
1190 if( ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
1191 ((dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)) )
1193 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1194 bestStereo = dwFlags & PFD_STEREO;
1198 bestStencil = stencil;
1203 if(bestDBuffer != -1 && (dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer)
1207 /* Stereo, see the comments above. */
1208 if( !(ppfd->dwFlags & PFD_STEREO_DONTCARE) ) {
1209 if( ((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
1210 ((dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)) )
1212 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1213 bestStereo = dwFlags & PFD_STEREO;
1217 bestStencil = stencil;
1222 if(bestStereo != -1 && (dwFlags & PFD_STEREO) != bestStereo)
1226 /* Below we will do a number of checks to select the 'best' pixelformat.
1227 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
1228 * The code works by trying to match the most important options as close as possible.
1229 * When a reasonable format is found, we will try to match more options.
1230 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
1231 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
1232 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
1235 if(ppfd->cColorBits) {
1236 if( ((ppfd->cColorBits > bestColor) && (color > bestColor)) ||
1237 ((color >= ppfd->cColorBits) && (color < bestColor)) )
1239 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1240 bestStereo = dwFlags & PFD_STEREO;
1244 bestStencil = stencil;
1248 } else if(bestColor != color) { /* Do further checks if the format is compatible */
1249 TRACE("color mismatch for iPixelFormat=%d\n", i+1);
1255 if(ppfd->cAlphaBits) {
1256 if( ((ppfd->cAlphaBits > bestAlpha) && (alpha > bestAlpha)) ||
1257 ((alpha >= ppfd->cAlphaBits) && (alpha < bestAlpha)) )
1259 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1260 bestStereo = dwFlags & PFD_STEREO;
1264 bestStencil = stencil;
1268 } else if(bestAlpha != alpha) {
1269 TRACE("alpha mismatch for iPixelFormat=%d\n", i+1);
1275 if(ppfd->cDepthBits) {
1276 if( ((ppfd->cDepthBits > bestDepth) && (depth > bestDepth)) ||
1277 ((depth >= ppfd->cDepthBits) && (depth < bestDepth)) )
1279 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1280 bestStereo = dwFlags & PFD_STEREO;
1284 bestStencil = stencil;
1288 } else if(bestDepth != depth) {
1289 TRACE("depth mismatch for iPixelFormat=%d\n", i+1);
1295 if(ppfd->cStencilBits) {
1296 if( ((ppfd->cStencilBits > bestStencil) && (stencil > bestStencil)) ||
1297 ((stencil >= ppfd->cStencilBits) && (stencil < bestStencil)) )
1299 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1300 bestStereo = dwFlags & PFD_STEREO;
1304 bestStencil = stencil;
1308 } else if(bestStencil != stencil) {
1309 TRACE("stencil mismatch for iPixelFormat=%d\n", i+1);
1315 if(ppfd->cAuxBuffers) {
1316 if( ((ppfd->cAuxBuffers > bestAux) && (aux > bestAux)) ||
1317 ((aux >= ppfd->cAuxBuffers) && (aux < bestAux)) )
1319 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1320 bestStereo = dwFlags & PFD_STEREO;
1324 bestStencil = stencil;
1328 } else if(bestAux != aux) {
1329 TRACE("aux mismatch for iPixelFormat=%d\n", i+1);
1335 if(bestFormat == -1) {
1336 TRACE("No matching mode was found returning 0\n");
1340 ret = bestFormat+1; /* the return value should be a 1-based index */
1341 TRACE("Successfully found a matching mode, returning index: %d %x\n", ret, list[bestFormat].fmt_id);
1344 wine_tsx11_unlock();
1349 * X11DRV_DescribePixelFormat
1351 * Get the pixel-format descriptor associated to the given id
1353 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
1356 PIXELFORMATDESCRIPTOR *ppfd) {
1357 /*XVisualInfo *vis;*/
1360 WineGLPixelFormat *fmt;
1364 if (!has_opengl()) return 0;
1366 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
1368 /* 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 */
1369 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1371 /* The application is only querying the number of pixelformats */
1373 } else if(fmt == NULL) {
1374 WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1378 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1379 ERR("Wrong structure size !\n");
1380 /* Should set error */
1386 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1387 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1390 /* These flags are always the same... */
1391 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1392 /* Now the flags extracted from the Visual */
1396 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1397 if(value & GLX_WINDOW_BIT)
1398 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1400 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1401 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1402 * 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
1403 * offered the GDI bit either. */
1404 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1406 /* PFD_GENERIC_FORMAT - gdi software rendering
1407 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1408 * none set - full hardware accelerated by a ICD
1410 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1411 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1413 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1415 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1416 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1418 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1421 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1422 if (value & GLX_RGBA_BIT)
1423 ppfd->iPixelType = PFD_TYPE_RGBA;
1425 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1428 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1429 ppfd->cColorBits = value;
1431 /* Red, green, blue and alpha bits / shifts */
1432 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1433 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1434 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1435 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1436 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1438 ppfd->cRedBits = rb;
1439 ppfd->cRedShift = gb + bb + ab;
1440 ppfd->cBlueBits = bb;
1441 ppfd->cBlueShift = ab;
1442 ppfd->cGreenBits = gb;
1443 ppfd->cGreenShift = bb + ab;
1444 ppfd->cAlphaBits = ab;
1445 ppfd->cAlphaShift = 0;
1448 ppfd->cRedShift = 0;
1449 ppfd->cBlueBits = 0;
1450 ppfd->cBlueShift = 0;
1451 ppfd->cGreenBits = 0;
1452 ppfd->cGreenShift = 0;
1453 ppfd->cAlphaBits = 0;
1454 ppfd->cAlphaShift = 0;
1457 /* Accum RGBA bits */
1458 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1459 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1460 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1461 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1463 ppfd->cAccumBits = rb+gb+bb+ab;
1464 ppfd->cAccumRedBits = rb;
1465 ppfd->cAccumGreenBits = gb;
1466 ppfd->cAccumBlueBits = bb;
1467 ppfd->cAccumAlphaBits = ab;
1470 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1471 ppfd->cAuxBuffers = value;
1474 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1475 ppfd->cDepthBits = value;
1478 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1479 ppfd->cStencilBits = value;
1481 wine_tsx11_unlock();
1483 ppfd->iLayerType = PFD_MAIN_PLANE;
1485 if (TRACE_ON(wgl)) {
1486 dump_PIXELFORMATDESCRIPTOR(ppfd);
1493 * X11DRV_GetPixelFormat
1495 * Get the pixel-format id used by this DC
1497 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1498 WineGLPixelFormat *fmt;
1500 TRACE("(%p)\n", physDev);
1502 if (!physDev->current_pf) return 0; /* not set yet */
1504 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE, &tmp);
1507 ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physDev->current_pf);
1510 else if(fmt->offscreenOnly)
1512 /* Offscreen formats can't be used with traditional WGL calls.
1513 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1514 TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1518 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1519 return physDev->current_pf;
1522 /* This function is the core of X11DRV_SetPixelFormat and X11DRV_SetPixelFormatWINE.
1523 * Both functions are the same except that X11DRV_SetPixelFormatWINE allows you to
1524 * set the pixel format multiple times. */
1525 static BOOL internal_SetPixelFormat(X11DRV_PDEVICE *physDev,
1527 const PIXELFORMATDESCRIPTOR *ppfd) {
1528 WineGLPixelFormat *fmt;
1532 /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1533 if(get_glxdrawable(physDev) == root_window)
1535 ERR("Invalid operation on root_window\n");
1539 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1540 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1542 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1547 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1548 wine_tsx11_unlock();
1550 hwnd = WindowFromDC(physDev->hdc);
1552 if(!(value&GLX_WINDOW_BIT)) {
1553 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1557 if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, (WPARAM)fmt->fmt_id, 0)) {
1558 ERR("Couldn't set format of the window, returning failure\n");
1562 else if(physDev->bitmap) {
1563 if(!(value&GLX_PIXMAP_BIT)) {
1564 WARN("Pixel format %d is not compatible for bitmap rendering\n", iPixelFormat);
1568 physDev->bitmap->glxpixmap = create_bitmap_glxpixmap(physDev, fmt);
1569 if(!physDev->bitmap->glxpixmap) {
1570 WARN("Couldn't create glxpixmap for pixel format %d\n", iPixelFormat);
1575 FIXME("called on a non-window, non-bitmap object?\n");
1578 physDev->current_pf = iPixelFormat;
1580 if (TRACE_ON(wgl)) {
1584 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1586 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1588 TRACE(" FBConfig have :\n");
1589 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1590 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1591 TRACE(" - VISUAL_ID 0x%x\n", value);
1592 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1593 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1595 wine_tsx11_unlock();
1602 * X11DRV_SetPixelFormat
1604 * Set the pixel-format id used by this DC
1606 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1608 const PIXELFORMATDESCRIPTOR *ppfd) {
1609 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1611 if (!has_opengl()) return FALSE;
1613 if(physDev->current_pf) /* cannot change it if already set */
1614 return (physDev->current_pf == iPixelFormat);
1616 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
1620 * X11DRV_wglCopyContext
1622 * For OpenGL32 wglCopyContext.
1624 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
1625 Wine_GLContext *src = (Wine_GLContext*)hglrcSrc;
1626 Wine_GLContext *dst = (Wine_GLContext*)hglrcDst;
1628 TRACE("hglrcSrc: (%p), hglrcDst: (%p), mask: %#x\n", hglrcSrc, hglrcDst, mask);
1630 /* There is a slight difference in the way GL contexts share display lists in WGL and GLX.
1631 * In case of GLX you need to specify this at context creation time but in case of WGL you
1632 * do this using wglShareLists which you can call after creating the context.
1633 * To emulate WGL we try to delay the creation of the context until wglShareLists or wglMakeCurrent.
1634 * Up to now that works fine.
1636 * The delayed GLX context creation could cause issues for wglCopyContext as it might get called
1637 * when there is no GLX context yet. The chance this will cause problems is small as at the time of
1638 * writing Wine has had OpenGL support for more than 7 years and this function has remained a stub
1641 if(!src->ctx || !dst->ctx) {
1642 /* NOTE: As a special case, if both GLX contexts are NULL, that means
1643 * neither WGL context was made current. In that case, both contexts
1644 * are in a default state, so any copy would no-op.
1646 if(!src->ctx && !dst->ctx) {
1647 TRACE("No source or destination contexts set. No-op.\n");
1653 src->ctx = create_glxcontext(gdi_display, src, NULL);
1654 TRACE(" created a delayed OpenGL context (%p)\n", src->ctx);
1656 else if (!dst->ctx) {
1658 dst->ctx = create_glxcontext(gdi_display, dst, NULL);
1659 TRACE(" created a delayed OpenGL context (%p)\n", dst->ctx);
1665 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1666 wine_tsx11_unlock();
1668 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1673 * X11DRV_wglCreateContext
1675 * For OpenGL32 wglCreateContext.
1677 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1679 Wine_GLContext *ret;
1680 WineGLPixelFormat *fmt;
1681 int hdcPF = physDev->current_pf;
1683 HDC hdc = physDev->hdc;
1685 TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1687 if (!has_opengl()) return 0;
1689 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
1690 /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1691 * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1692 * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1693 * If this fails something is very wrong on the system. */
1695 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
1696 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1700 /* The context will be allocated in the wglMakeCurrent call */
1702 ret = alloc_context();
1703 wine_tsx11_unlock();
1708 ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1710 TRACE(" creating context %p (GL context creation delayed)\n", ret);
1715 * X11DRV_wglDeleteContext
1717 * For OpenGL32 wglDeleteContext.
1719 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc)
1721 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1724 TRACE("(%p)\n", hglrc);
1726 if (!has_opengl()) return 0;
1729 /* A game (Half Life not to name it) deletes twice the same context,
1730 * so make sure it is valid first */
1731 if (is_valid_context( ctx ))
1733 if (ctx->ctx) pglXDestroyContext(gdi_display, ctx->ctx);
1738 WARN("Error deleting context !\n");
1739 SetLastError(ERROR_INVALID_HANDLE);
1742 wine_tsx11_unlock();
1748 * X11DRV_wglGetCurrentReadDCARB
1750 * For OpenGL32 wglGetCurrentReadDCARB.
1752 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void)
1755 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
1757 if (ctx) ret = ctx->read_hdc;
1759 TRACE(" returning %p (GL drawable %lu)\n", ret, ctx ? ctx->drawables[1] : 0);
1764 * X11DRV_wglGetProcAddress
1766 * For OpenGL32 wglGetProcAddress.
1768 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1771 const WineGLExtension *ext;
1773 int padding = 32 - strlen(lpszProc);
1777 if (!has_opengl()) return NULL;
1779 /* Check the table of WGL extensions to see if we need to return a WGL extension
1780 * or a function pointer to a native OpenGL function. */
1781 if(strncmp(lpszProc, "wgl", 3) != 0) {
1782 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1784 TRACE("('%s'):%*s", lpszProc, padding, " ");
1785 for (i = 0; i < WineGLExtensionListSize; ++i) {
1786 ext = WineGLExtensionList[i];
1787 for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1788 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1789 TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1790 return ext->extEntryPoints[j].funcAddress;
1796 WARN("(%s) - not found\n", lpszProc);
1801 * X11DRV_wglMakeCurrent
1803 * For OpenGL32 wglMakeCurrent.
1805 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1807 HDC hdc = physDev->hdc;
1808 DWORD type = GetObjectType(hdc);
1809 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1811 TRACE("(%p,%p)\n", hdc, hglrc);
1813 if (!has_opengl()) return FALSE;
1816 if (hglrc == NULL) {
1817 ret = pglXMakeCurrent(gdi_display, None, NULL);
1818 NtCurrentTeb()->glContext = NULL;
1819 } else if (ctx->fmt->iPixelFormat != physDev->current_pf) {
1820 WARN( "mismatched pixel format hdc %p %u ctx %p %u\n",
1821 hdc, physDev->current_pf, ctx, ctx->fmt->iPixelFormat );
1822 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1825 Drawable drawable = get_glxdrawable(physDev);
1826 if (ctx->ctx == NULL) {
1827 /* The describe lines below are for debugging purposes only */
1828 if (TRACE_ON(wgl)) {
1829 describeDrawable(physDev);
1830 describeContext(ctx);
1833 /* Create a GLX context using the same visual as chosen earlier in wglCreateContext.
1834 * We are certain that the drawable and context are compatible as we only allow compatible formats.
1836 TRACE(" Creating GLX Context\n");
1837 ctx->ctx = create_glxcontext(gdi_display, ctx, NULL);
1838 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1840 TRACE(" make current for dis %p, drawable %p, ctx %p\n", gdi_display, (void*) drawable, ctx->ctx);
1841 ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1842 NtCurrentTeb()->glContext = ctx;
1846 ctx->read_hdc = hdc;
1847 ctx->drawables[0] = drawable;
1848 ctx->drawables[1] = drawable;
1849 ctx->refresh_drawables = FALSE;
1851 if (type == OBJ_MEMDC)
1853 ctx->do_escape = TRUE;
1854 pglDrawBuffer(GL_FRONT_LEFT);
1858 wine_tsx11_unlock();
1859 TRACE(" returning %s\n", (ret ? "True" : "False"));
1864 * X11DRV_wglMakeContextCurrentARB
1866 * For OpenGL32 wglMakeContextCurrentARB
1868 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* pDrawDev, X11DRV_PDEVICE* pReadDev, HGLRC hglrc)
1872 TRACE("(%p,%p,%p)\n", pDrawDev, pReadDev, hglrc);
1874 if (!has_opengl()) return 0;
1877 if (hglrc == NULL) {
1878 ret = pglXMakeCurrent(gdi_display, None, NULL);
1879 NtCurrentTeb()->glContext = NULL;
1881 if (NULL == pglXMakeContextCurrent) {
1884 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1885 Drawable d_draw = get_glxdrawable(pDrawDev);
1886 Drawable d_read = get_glxdrawable(pReadDev);
1888 if (ctx->ctx == NULL) {
1889 ctx->ctx = create_glxcontext(gdi_display, ctx, NULL);
1890 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1892 ctx->hdc = pDrawDev->hdc;
1893 ctx->read_hdc = pReadDev->hdc;
1894 ctx->drawables[0] = d_draw;
1895 ctx->drawables[1] = d_read;
1896 ctx->refresh_drawables = FALSE;
1897 ret = pglXMakeContextCurrent(gdi_display, d_draw, d_read, ctx->ctx);
1898 NtCurrentTeb()->glContext = ctx;
1901 wine_tsx11_unlock();
1903 TRACE(" returning %s\n", (ret ? "True" : "False"));
1908 * X11DRV_wglShareLists
1910 * For OpenGL32 wglShareLists.
1912 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1913 Wine_GLContext *org = (Wine_GLContext *) hglrc1;
1914 Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1916 TRACE("(%p, %p)\n", org, dest);
1918 if (!has_opengl()) return FALSE;
1920 if (NULL != dest && dest->ctx != NULL) {
1921 ERR("Could not share display lists, context already created !\n");
1924 if(org && dest && (GetObjectType(org->hdc) == OBJ_MEMDC) ^ (GetObjectType(dest->hdc) == OBJ_MEMDC)) {
1925 WARN("Attempting to share a context between a direct and indirect rendering context, expect issues!\n");
1928 if (org->ctx == NULL) {
1930 describeContext(org);
1932 org->ctx = create_glxcontext(gdi_display, org, NULL);
1933 wine_tsx11_unlock();
1934 TRACE(" created a delayed OpenGL context (%p) for Wine context %p\n", org->ctx, org);
1938 describeContext(dest);
1939 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1940 wine_tsx11_unlock();
1941 TRACE(" created a delayed OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1948 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1950 /* We are running using client-side rendering fonts... */
1952 unsigned int glyph, size = 0;
1953 void *bitmap = NULL, *gl_bitmap = NULL;
1957 pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1958 pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1959 wine_tsx11_unlock();
1961 for (glyph = first; glyph < first + count; glyph++) {
1962 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1963 unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1964 unsigned int height, width_int;
1966 TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1967 if (needed_size == GDI_ERROR) {
1968 TRACE(" - needed size : %d (GDI_ERROR)\n", needed_size);
1971 TRACE(" - needed size : %d\n", needed_size);
1974 if (needed_size > size) {
1976 HeapFree(GetProcessHeap(), 0, bitmap);
1977 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1978 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1979 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1981 if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) == GDI_ERROR)
1983 if (TRACE_ON(wgl)) {
1984 unsigned int height, width, bitmask;
1985 unsigned char *bitmap_ = bitmap;
1987 TRACE(" - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1988 TRACE(" - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1989 TRACE(" - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1990 if (needed_size != 0) {
1991 TRACE(" - bitmap :\n");
1992 for (height = 0; height < gm.gmBlackBoxY; height++) {
1994 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1999 if (*bitmap_ & bitmask)
2004 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
2010 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
2011 * glyph for it to be drawn properly.
2013 if (needed_size != 0) {
2014 width_int = (gm.gmBlackBoxX + 31) / 32;
2015 for (height = 0; height < gm.gmBlackBoxY; height++) {
2017 for (width = 0; width < width_int; width++) {
2018 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
2019 ((int *) bitmap)[height * width_int + width];
2025 pglNewList(listBase++, GL_COMPILE);
2026 if (needed_size != 0) {
2027 pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
2028 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
2029 gm.gmCellIncX, gm.gmCellIncY,
2032 /* This is the case of 'empty' glyphs like the space character */
2033 pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
2036 wine_tsx11_unlock();
2040 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2041 wine_tsx11_unlock();
2043 HeapFree(GetProcessHeap(), 0, bitmap);
2044 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2049 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2050 wine_tsx11_unlock();
2052 HeapFree(GetProcessHeap(), 0, bitmap);
2053 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2058 * X11DRV_wglUseFontBitmapsA
2060 * For OpenGL32 wglUseFontBitmapsA.
2062 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2064 Font fid = physDev->font;
2066 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2068 if (!has_opengl()) return FALSE;
2071 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
2075 /* I assume that the glyphs are at the same position for X and for Windows */
2076 pglXUseXFont(fid, first, count, listBase);
2077 wine_tsx11_unlock();
2082 * X11DRV_wglUseFontBitmapsW
2084 * For OpenGL32 wglUseFontBitmapsW.
2086 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2088 Font fid = physDev->font;
2090 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2092 if (!has_opengl()) return FALSE;
2095 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
2098 WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
2101 /* I assume that the glyphs are at the same position for X and for Windows */
2102 pglXUseXFont(fid, first, count, listBase);
2103 wine_tsx11_unlock();
2107 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
2108 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
2115 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2117 pglGetIntegerv(pname, params);
2119 * if we cannot find a Wine Context
2120 * we only have the default wine desktop context,
2121 * so if we have only a 24 depth say we have 32
2123 if (!ctx && *params == 24) {
2126 TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
2131 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2133 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_ALPHA_SIZE, params);
2134 TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
2138 pglGetIntegerv(pname, params);
2141 wine_tsx11_unlock();
2144 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
2148 if (!physDev->gl_copy)
2151 w = physDev->dc_rect.right - physDev->dc_rect.left;
2152 h = physDev->dc_rect.bottom - physDev->dc_rect.top;
2154 if(w > 0 && h > 0) {
2155 Drawable src = physDev->pixmap;
2156 if(!src) src = physDev->gl_drawable;
2158 /* The GL drawable may be lagged behind if we don't flush first, so
2159 * flush the display make sure we copy up-to-date data */
2161 XFlush(gdi_display);
2162 XSetFunction(gdi_display, physDev->gc, GXcopy);
2163 XCopyArea(gdi_display, src, physDev->drawable, physDev->gc, 0, 0, w, h,
2164 physDev->dc_rect.left, physDev->dc_rect.top);
2165 wine_tsx11_unlock();
2170 static void WINAPI X11DRV_wglFinish(void)
2172 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2173 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2178 wine_tsx11_unlock();
2179 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2182 static void WINAPI X11DRV_wglFlush(void)
2184 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2185 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2190 wine_tsx11_unlock();
2191 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2195 * X11DRV_wglGetExtensionsStringARB
2197 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2199 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
2200 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2201 return WineGLInfo.wglExtensions;
2205 * X11DRV_wglCreatePbufferARB
2207 * WGL_ARB_pbuffer: wglCreatePbufferARB
2209 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
2211 Wine_GLPBuffer* object = NULL;
2212 WineGLPixelFormat *fmt = NULL;
2217 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2219 if (0 >= iPixelFormat) {
2220 ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2221 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2222 return NULL; /* unexpected error */
2225 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2226 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2228 ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2229 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2230 goto create_failed; /* unexpected error */
2233 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2234 if (NULL == object) {
2235 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2236 goto create_failed; /* unexpected error */
2239 object->display = gdi_display;
2240 object->width = iWidth;
2241 object->height = iHeight;
2244 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2245 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2246 while (piAttribList && 0 != *piAttribList) {
2248 switch (*piAttribList) {
2249 case WGL_PBUFFER_LARGEST_ARB: {
2251 attr_v = *piAttribList;
2252 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2253 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2257 case WGL_TEXTURE_FORMAT_ARB: {
2259 attr_v = *piAttribList;
2260 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2261 if (use_render_texture_ati) {
2264 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2265 case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2266 case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2268 SetLastError(ERROR_INVALID_DATA);
2271 object->use_render_texture = 1;
2272 PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2274 if (WGL_NO_TEXTURE_ARB == attr_v) {
2275 object->use_render_texture = 0;
2277 if (!use_render_texture_emulation) {
2278 SetLastError(ERROR_INVALID_DATA);
2282 case WGL_TEXTURE_RGB_ARB:
2283 object->use_render_texture = GL_RGB;
2284 object->texture_bpp = 3;
2285 object->texture_format = GL_RGB;
2286 object->texture_type = GL_UNSIGNED_BYTE;
2288 case WGL_TEXTURE_RGBA_ARB:
2289 object->use_render_texture = GL_RGBA;
2290 object->texture_bpp = 4;
2291 object->texture_format = GL_RGBA;
2292 object->texture_type = GL_UNSIGNED_BYTE;
2295 /* WGL_FLOAT_COMPONENTS_NV */
2296 case WGL_TEXTURE_FLOAT_R_NV:
2297 object->use_render_texture = GL_FLOAT_R_NV;
2298 object->texture_bpp = 4;
2299 object->texture_format = GL_RED;
2300 object->texture_type = GL_FLOAT;
2302 case WGL_TEXTURE_FLOAT_RG_NV:
2303 object->use_render_texture = GL_FLOAT_RG_NV;
2304 object->texture_bpp = 8;
2305 object->texture_format = GL_LUMINANCE_ALPHA;
2306 object->texture_type = GL_FLOAT;
2308 case WGL_TEXTURE_FLOAT_RGB_NV:
2309 object->use_render_texture = GL_FLOAT_RGB_NV;
2310 object->texture_bpp = 12;
2311 object->texture_format = GL_RGB;
2312 object->texture_type = GL_FLOAT;
2314 case WGL_TEXTURE_FLOAT_RGBA_NV:
2315 object->use_render_texture = GL_FLOAT_RGBA_NV;
2316 object->texture_bpp = 16;
2317 object->texture_format = GL_RGBA;
2318 object->texture_type = GL_FLOAT;
2321 ERR("Unknown texture format: %x\n", attr_v);
2322 SetLastError(ERROR_INVALID_DATA);
2330 case WGL_TEXTURE_TARGET_ARB: {
2332 attr_v = *piAttribList;
2333 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2334 if (use_render_texture_ati) {
2337 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2338 case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2339 case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2340 case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2342 SetLastError(ERROR_INVALID_DATA);
2345 PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2347 if (WGL_NO_TEXTURE_ARB == attr_v) {
2348 object->texture_target = 0;
2350 if (!use_render_texture_emulation) {
2351 SetLastError(ERROR_INVALID_DATA);
2355 case WGL_TEXTURE_CUBE_MAP_ARB: {
2356 if (iWidth != iHeight) {
2357 SetLastError(ERROR_INVALID_DATA);
2360 object->texture_target = GL_TEXTURE_CUBE_MAP;
2361 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2364 case WGL_TEXTURE_1D_ARB: {
2366 SetLastError(ERROR_INVALID_DATA);
2369 object->texture_target = GL_TEXTURE_1D;
2370 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2373 case WGL_TEXTURE_2D_ARB: {
2374 object->texture_target = GL_TEXTURE_2D;
2375 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2378 case WGL_TEXTURE_RECTANGLE_NV: {
2379 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2380 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2384 ERR("Unknown texture target: %x\n", attr_v);
2385 SetLastError(ERROR_INVALID_DATA);
2393 case WGL_MIPMAP_TEXTURE_ARB: {
2395 attr_v = *piAttribList;
2396 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2397 if (use_render_texture_ati) {
2398 PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2400 if (!use_render_texture_emulation) {
2401 SetLastError(ERROR_INVALID_DATA);
2411 PUSH1(attribs, None);
2413 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2414 wine_tsx11_unlock();
2415 TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
2416 if (!object->drawable) {
2417 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2418 goto create_failed; /* unexpected error */
2420 TRACE("->(%p)\n", object);
2424 HeapFree(GetProcessHeap(), 0, object);
2425 TRACE("->(FAILED)\n");
2430 * X11DRV_wglDestroyPbufferARB
2432 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2434 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2436 Wine_GLPBuffer* object = hPbuffer;
2437 TRACE("(%p)\n", hPbuffer);
2438 if (NULL == object) {
2439 SetLastError(ERROR_INVALID_HANDLE);
2443 pglXDestroyPbuffer(object->display, object->drawable);
2444 wine_tsx11_unlock();
2445 HeapFree(GetProcessHeap(), 0, object);
2450 * X11DRV_wglGetPbufferDCARB
2452 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2453 * The function wglGetPbufferDCARB returns a device context for a pbuffer.
2454 * Gdi32 implements the part of this function which creates a device context.
2455 * This part associates the physDev with the X drawable of the pbuffer.
2457 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
2459 Wine_GLPBuffer* object = hPbuffer;
2460 if (NULL == object) {
2461 SetLastError(ERROR_INVALID_HANDLE);
2465 /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
2466 * All formats in our pixelformat list are compatible with each other and the main drawable. */
2467 physDev->current_pf = object->fmt->iPixelFormat;
2468 physDev->drawable = object->drawable;
2469 SetRect( &physDev->drawable_rect, 0, 0, object->width, object->height );
2470 physDev->dc_rect = physDev->drawable_rect;
2472 TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
2473 return physDev->hdc;
2477 * X11DRV_wglQueryPbufferARB
2479 * WGL_ARB_pbuffer: wglQueryPbufferARB
2481 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2483 Wine_GLPBuffer* object = hPbuffer;
2484 TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2485 if (NULL == object) {
2486 SetLastError(ERROR_INVALID_HANDLE);
2489 switch (iAttribute) {
2490 case WGL_PBUFFER_WIDTH_ARB:
2492 pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2493 wine_tsx11_unlock();
2495 case WGL_PBUFFER_HEIGHT_ARB:
2497 pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2498 wine_tsx11_unlock();
2501 case WGL_PBUFFER_LOST_ARB:
2502 /* GLX Pbuffers cannot be lost by default. We can support this by
2503 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2504 * to receive pixel buffer clobber events, however that may or may
2505 * not give any benefit */
2506 *piValue = GL_FALSE;
2509 case WGL_TEXTURE_FORMAT_ARB:
2510 if (use_render_texture_ati) {
2512 int type = WGL_NO_TEXTURE_ARB;
2514 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2515 wine_tsx11_unlock();
2517 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2518 case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2519 case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2523 if (!object->use_render_texture) {
2524 *piValue = WGL_NO_TEXTURE_ARB;
2526 if (!use_render_texture_emulation) {
2527 SetLastError(ERROR_INVALID_HANDLE);
2530 switch(object->use_render_texture) {
2532 *piValue = WGL_TEXTURE_RGB_ARB;
2535 *piValue = WGL_TEXTURE_RGBA_ARB;
2537 /* WGL_FLOAT_COMPONENTS_NV */
2539 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2541 case GL_FLOAT_RG_NV:
2542 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2544 case GL_FLOAT_RGB_NV:
2545 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2547 case GL_FLOAT_RGBA_NV:
2548 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2551 ERR("Unknown texture format: %x\n", object->use_render_texture);
2557 case WGL_TEXTURE_TARGET_ARB:
2558 if (use_render_texture_ati) {
2560 int type = WGL_NO_TEXTURE_ARB;
2562 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2563 wine_tsx11_unlock();
2565 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2566 case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2567 case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2568 case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2572 if (!object->texture_target) {
2573 *piValue = WGL_NO_TEXTURE_ARB;
2575 if (!use_render_texture_emulation) {
2576 SetLastError(ERROR_INVALID_DATA);
2579 switch (object->texture_target) {
2580 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2581 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2582 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2583 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2589 case WGL_MIPMAP_TEXTURE_ARB:
2590 if (use_render_texture_ati) {
2592 pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2593 wine_tsx11_unlock();
2595 *piValue = GL_FALSE; /** don't support that */
2596 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2601 FIXME("unexpected attribute %x\n", iAttribute);
2609 * X11DRV_wglReleasePbufferDCARB
2611 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2613 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2615 TRACE("(%p, %p)\n", hPbuffer, hdc);
2616 return DeleteDC(hdc);
2620 * X11DRV_wglSetPbufferAttribARB
2622 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2624 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2626 Wine_GLPBuffer* object = hPbuffer;
2627 GLboolean ret = GL_FALSE;
2629 WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2630 if (NULL == object) {
2631 SetLastError(ERROR_INVALID_HANDLE);
2634 if (!object->use_render_texture) {
2635 SetLastError(ERROR_INVALID_HANDLE);
2638 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2641 if (NULL != pglXDrawableAttribATI) {
2642 if (use_render_texture_ati) {
2643 FIXME("Need conversion for GLX_ATI_render_texture\n");
2646 ret = pglXDrawableAttribATI(object->display, object->drawable, piAttribList);
2647 wine_tsx11_unlock();
2653 * X11DRV_wglChoosePixelFormatARB
2655 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2657 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2662 GLXFBConfig* cfgs = NULL;
2666 WineGLPixelFormat *fmt;
2672 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2673 if (NULL != pfAttribFList) {
2674 FIXME("unused pfAttribFList\n");
2677 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2678 if (-1 == nAttribs) {
2679 WARN("Cannot convert WGL to GLX attributes\n");
2682 PUSH1(attribs, None);
2684 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2685 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the WineGLPixelFormat. We don't query these attributes
2686 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on by passing a dwFlags to 'ConvertPixelFormatGLXtoWGL'. */
2687 for(i=0; piAttribIList[i] != 0; i+=2)
2689 switch(piAttribIList[i])
2691 case WGL_DRAW_TO_BITMAP_ARB:
2692 if(piAttribIList[i+1])
2693 dwFlags |= PFD_DRAW_TO_BITMAP;
2695 case WGL_ACCELERATION_ARB:
2696 switch(piAttribIList[i+1])
2698 case WGL_NO_ACCELERATION_ARB:
2699 dwFlags |= PFD_GENERIC_FORMAT;
2701 case WGL_GENERIC_ACCELERATION_ARB:
2702 dwFlags |= PFD_GENERIC_ACCELERATED;
2704 case WGL_FULL_ACCELERATION_ARB:
2709 case WGL_SUPPORT_GDI_ARB:
2710 if(piAttribIList[i+1])
2711 dwFlags |= PFD_SUPPORT_GDI;
2716 /* Search for FB configurations matching the requirements in attribs */
2718 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2720 wine_tsx11_unlock();
2721 WARN("Compatible Pixel Format not found\n");
2725 /* Loop through all matching formats and check if they are suitable.
2726 * Note that this function should at max return nMaxFormats different formats */
2727 for(run=0; run < 2; run++)
2729 for (it = 0; it < nCfgs; ++it) {
2730 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2732 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2736 /* Search for the format in our list of compatible formats */
2737 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id, dwFlags);
2741 /* During the first run we only want onscreen formats and during the second only offscreen 'XOR' */
2742 if( ((run == 0) && fmt->offscreenOnly) || ((run == 1) && !fmt->offscreenOnly) )
2745 if(pfmt_it < nMaxFormats) {
2746 piFormats[pfmt_it] = fmt->iPixelFormat;
2747 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2753 *nNumFormats = pfmt_it;
2756 wine_tsx11_unlock();
2761 * X11DRV_wglGetPixelFormatAttribivARB
2763 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2765 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2768 WineGLPixelFormat *fmt = NULL;
2772 int nWGLFormats = 0;
2774 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2776 if (0 < iLayerPlane) {
2777 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2781 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2782 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2783 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2784 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2786 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2790 for (i = 0; i < nAttributes; ++i) {
2791 const int curWGLAttr = piAttributes[i];
2792 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2794 switch (curWGLAttr) {
2795 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2796 piValues[i] = nWGLFormats;
2799 case WGL_SUPPORT_OPENGL_ARB:
2800 piValues[i] = GL_TRUE;
2803 case WGL_ACCELERATION_ARB:
2804 curGLXAttr = GLX_CONFIG_CAVEAT;
2805 if (!fmt) goto pix_error;
2806 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2807 piValues[i] = WGL_NO_ACCELERATION_ARB;
2808 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2809 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2811 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2814 case WGL_TRANSPARENT_ARB:
2815 curGLXAttr = GLX_TRANSPARENT_TYPE;
2816 if (!fmt) goto pix_error;
2817 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2818 if (hTest) goto get_error;
2819 piValues[i] = GL_FALSE;
2820 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2823 case WGL_PIXEL_TYPE_ARB:
2824 curGLXAttr = GLX_RENDER_TYPE;
2825 if (!fmt) goto pix_error;
2826 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2827 if (hTest) goto get_error;
2828 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2829 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2830 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2831 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2832 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2833 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2835 ERR("unexpected RenderType(%x)\n", tmp);
2836 piValues[i] = WGL_TYPE_RGBA_ARB;
2840 case WGL_COLOR_BITS_ARB:
2841 curGLXAttr = GLX_BUFFER_SIZE;
2844 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2845 if (use_render_texture_ati) {
2846 curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2849 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2850 if (use_render_texture_ati) {
2851 curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2854 if (!use_render_texture_emulation) {
2855 piValues[i] = GL_FALSE;
2858 curGLXAttr = GLX_RENDER_TYPE;
2859 if (!fmt) goto pix_error;
2860 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2861 if (hTest) goto get_error;
2862 if (GLX_COLOR_INDEX_BIT == tmp) {
2863 piValues[i] = GL_FALSE;
2866 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2867 if (hTest) goto get_error;
2868 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2871 case WGL_BLUE_BITS_ARB:
2872 curGLXAttr = GLX_BLUE_SIZE;
2874 case WGL_RED_BITS_ARB:
2875 curGLXAttr = GLX_RED_SIZE;
2877 case WGL_GREEN_BITS_ARB:
2878 curGLXAttr = GLX_GREEN_SIZE;
2880 case WGL_ALPHA_BITS_ARB:
2881 curGLXAttr = GLX_ALPHA_SIZE;
2883 case WGL_DEPTH_BITS_ARB:
2884 curGLXAttr = GLX_DEPTH_SIZE;
2886 case WGL_STENCIL_BITS_ARB:
2887 curGLXAttr = GLX_STENCIL_SIZE;
2889 case WGL_DOUBLE_BUFFER_ARB:
2890 curGLXAttr = GLX_DOUBLEBUFFER;
2892 case WGL_STEREO_ARB:
2893 curGLXAttr = GLX_STEREO;
2895 case WGL_AUX_BUFFERS_ARB:
2896 curGLXAttr = GLX_AUX_BUFFERS;
2899 case WGL_SUPPORT_GDI_ARB:
2900 if (!fmt) goto pix_error;
2901 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) ? TRUE : FALSE;
2904 case WGL_DRAW_TO_BITMAP_ARB:
2905 if (!fmt) goto pix_error;
2906 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? TRUE : FALSE;
2909 case WGL_DRAW_TO_WINDOW_ARB:
2910 case WGL_DRAW_TO_PBUFFER_ARB:
2911 if (!fmt) goto pix_error;
2912 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2913 if (hTest) goto get_error;
2914 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2915 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2916 piValues[i] = GL_TRUE;
2918 piValues[i] = GL_FALSE;
2921 case WGL_SWAP_METHOD_ARB:
2922 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2923 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2924 * point only ATI offers this.
2926 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2929 case WGL_PBUFFER_LARGEST_ARB:
2930 curGLXAttr = GLX_LARGEST_PBUFFER;
2933 case WGL_SAMPLE_BUFFERS_ARB:
2934 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2937 case WGL_SAMPLES_ARB:
2938 curGLXAttr = GLX_SAMPLES_ARB;
2941 case WGL_FLOAT_COMPONENTS_NV:
2942 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2945 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2946 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2949 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2950 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2953 case WGL_ACCUM_RED_BITS_ARB:
2954 curGLXAttr = GLX_ACCUM_RED_SIZE;
2956 case WGL_ACCUM_GREEN_BITS_ARB:
2957 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2959 case WGL_ACCUM_BLUE_BITS_ARB:
2960 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2962 case WGL_ACCUM_ALPHA_BITS_ARB:
2963 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2965 case WGL_ACCUM_BITS_ARB:
2966 if (!fmt) goto pix_error;
2967 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2968 if (hTest) goto get_error;
2970 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2971 if (hTest) goto get_error;
2973 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2974 if (hTest) goto get_error;
2976 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2977 if (hTest) goto get_error;
2982 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2985 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2986 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2987 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2989 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2990 * and check which options can work using iPixelFormat=0 and which not.
2991 * A problem would be that this function is an extension. This would
2992 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2994 if (0 != curGLXAttr && iPixelFormat != 0) {
2995 if (!fmt) goto pix_error;
2996 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2997 if (hTest) goto get_error;
3000 piValues[i] = GL_FALSE;
3003 wine_tsx11_unlock();
3007 wine_tsx11_unlock();
3008 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
3012 wine_tsx11_unlock();
3013 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
3018 * X11DRV_wglGetPixelFormatAttribfvARB
3020 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
3022 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
3028 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
3030 /* Allocate a temporary array to store integer values */
3031 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
3033 ERR("couldn't allocate %d array\n", nAttributes);
3037 /* Piggy-back on wglGetPixelFormatAttribivARB */
3038 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
3040 /* Convert integer values to float. Should also check for attributes
3041 that can give decimal values here */
3042 for (i=0; i<nAttributes;i++) {
3043 pfValues[i] = attr[i];
3047 HeapFree(GetProcessHeap(), 0, attr);
3052 * X11DRV_wglBindTexImageARB
3054 * WGL_ARB_render_texture: wglBindTexImageARB
3056 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3058 Wine_GLPBuffer* object = hPbuffer;
3059 GLboolean ret = GL_FALSE;
3061 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3062 if (NULL == object) {
3063 SetLastError(ERROR_INVALID_HANDLE);
3066 if (!object->use_render_texture) {
3067 SetLastError(ERROR_INVALID_HANDLE);
3071 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3072 static int init = 0;
3073 int prev_binded_texture = 0;
3074 GLXContext prev_context;
3075 Drawable prev_drawable;
3076 GLXContext tmp_context;
3079 prev_context = pglXGetCurrentContext();
3080 prev_drawable = pglXGetCurrentDrawable();
3082 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
3083 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
3084 isn't ideal performance wise but I wasn't able to get other ways working.
3087 init = 1; /* Only show the FIXME once for performance reasons */
3088 FIXME("partial stub!\n");
3091 TRACE("drawable=%p, context=%p\n", (void*)object->drawable, prev_context);
3092 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3094 pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3096 /* Switch to our pbuffer */
3097 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
3099 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3100 * After that upload the pbuffer texture data. */
3101 pglBindTexture(object->texture_target, prev_binded_texture);
3102 pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3104 /* Switch back to the original drawable and upload the pbuffer-texture */
3105 pglXMakeCurrent(object->display, prev_drawable, prev_context);
3106 pglXDestroyContext(gdi_display, tmp_context);
3107 wine_tsx11_unlock();
3111 if (NULL != pglXBindTexImageATI) {
3116 case WGL_FRONT_LEFT_ARB:
3117 buffer = GLX_FRONT_LEFT_ATI;
3119 case WGL_FRONT_RIGHT_ARB:
3120 buffer = GLX_FRONT_RIGHT_ATI;
3122 case WGL_BACK_LEFT_ARB:
3123 buffer = GLX_BACK_LEFT_ATI;
3125 case WGL_BACK_RIGHT_ARB:
3126 buffer = GLX_BACK_RIGHT_ATI;
3129 ERR("Unknown iBuffer=%#x\n", iBuffer);
3133 /* In the sample 'ogl_offscreen_rendering_3' from codesampler.net I get garbage on the screen.
3134 * I'm not sure if that's a bug in the ATI extension or in the program. I think that the program
3135 * expected a single buffering format since it didn't ask for double buffering. A buffer swap
3136 * fixed the program. I don't know what the correct behavior is. On the other hand that demo
3137 * works fine using our pbuffer emulation path.
3140 ret = pglXBindTexImageATI(object->display, object->drawable, buffer);
3141 wine_tsx11_unlock();
3147 * X11DRV_wglReleaseTexImageARB
3149 * WGL_ARB_render_texture: wglReleaseTexImageARB
3151 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3153 Wine_GLPBuffer* object = hPbuffer;
3154 GLboolean ret = GL_FALSE;
3156 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3157 if (NULL == object) {
3158 SetLastError(ERROR_INVALID_HANDLE);
3161 if (!object->use_render_texture) {
3162 SetLastError(ERROR_INVALID_HANDLE);
3165 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3168 if (NULL != pglXReleaseTexImageATI) {
3173 case WGL_FRONT_LEFT_ARB:
3174 buffer = GLX_FRONT_LEFT_ATI;
3176 case WGL_FRONT_RIGHT_ARB:
3177 buffer = GLX_FRONT_RIGHT_ATI;
3179 case WGL_BACK_LEFT_ARB:
3180 buffer = GLX_BACK_LEFT_ATI;
3182 case WGL_BACK_RIGHT_ARB:
3183 buffer = GLX_BACK_RIGHT_ATI;
3186 ERR("Unknown iBuffer=%#x\n", iBuffer);
3190 ret = pglXReleaseTexImageATI(object->display, object->drawable, buffer);
3191 wine_tsx11_unlock();
3197 * X11DRV_wglGetExtensionsStringEXT
3199 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3201 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
3202 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3203 return WineGLInfo.wglExtensions;
3207 * X11DRV_wglGetSwapIntervalEXT
3209 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3211 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
3212 FIXME("(),stub!\n");
3213 return swap_interval;
3217 * X11DRV_wglSwapIntervalEXT
3219 * WGL_EXT_swap_control: wglSwapIntervalEXT
3221 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
3224 TRACE("(%d)\n", interval);
3225 swap_interval = interval;
3226 if (NULL != pglXSwapIntervalSGI) {
3228 ret = !pglXSwapIntervalSGI(interval);
3229 wine_tsx11_unlock();
3231 else WARN("(): GLX_SGI_swap_control extension seems not supported\n");
3236 * X11DRV_wglAllocateMemoryNV
3238 * WGL_NV_vertex_array_range: wglAllocateMemoryNV
3240 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
3242 TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
3244 if (pglXAllocateMemoryNV)
3247 ret = pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
3248 wine_tsx11_unlock();
3254 * X11DRV_wglFreeMemoryNV
3256 * WGL_NV_vertex_array_range: wglFreeMemoryNV
3258 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
3259 TRACE("(%p)\n", pointer);
3260 if (pglXFreeMemoryNV == NULL)
3264 pglXFreeMemoryNV(pointer);
3265 wine_tsx11_unlock();
3269 * X11DRV_wglSetPixelFormatWINE
3271 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3272 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3274 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3276 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
3278 if (!has_opengl()) return FALSE;
3280 if (physDev->current_pf == iPixelFormat) return TRUE;
3282 /* Relay to the core SetPixelFormat */
3283 TRACE("Changing iPixelFormat from %d to %d\n", physDev->current_pf, iPixelFormat);
3284 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
3288 * glxRequireVersion (internal)
3290 * Check if the supported GLX version matches requiredVersion.
3292 static BOOL glxRequireVersion(int requiredVersion)
3294 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3295 if(requiredVersion <= WineGLInfo.glxVersion[1])
3301 static BOOL glxRequireExtension(const char *requiredExtension)
3303 if (strstr(WineGLInfo.glxExtensions, requiredExtension) == NULL) {
3310 static void register_extension_string(const char *ext)
3312 if (WineGLInfo.wglExtensions[0])
3313 strcat(WineGLInfo.wglExtensions, " ");
3314 strcat(WineGLInfo.wglExtensions, ext);
3316 TRACE("'%s'\n", ext);
3319 static BOOL register_extension(const WineGLExtension * ext)
3323 assert( WineGLExtensionListSize < MAX_EXTENSIONS );
3324 WineGLExtensionList[WineGLExtensionListSize++] = ext;
3326 register_extension_string(ext->extName);
3328 for (i = 0; ext->extEntryPoints[i].funcName; ++i)
3329 TRACE(" - '%s'\n", ext->extEntryPoints[i].funcName);
3334 static const WineGLExtension WGL_internal_functions =
3338 { "wglGetIntegerv", X11DRV_wglGetIntegerv },
3339 { "wglFinish", X11DRV_wglFinish },
3340 { "wglFlush", X11DRV_wglFlush },
3345 static const WineGLExtension WGL_ARB_extensions_string =
3347 "WGL_ARB_extensions_string",
3349 { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
3353 static const WineGLExtension WGL_ARB_make_current_read =
3355 "WGL_ARB_make_current_read",
3357 { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
3358 { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
3362 static const WineGLExtension WGL_ARB_multisample =
3364 "WGL_ARB_multisample",
3367 static const WineGLExtension WGL_ARB_pbuffer =
3371 { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
3372 { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
3373 { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
3374 { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
3375 { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
3376 { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
3380 static const WineGLExtension WGL_ARB_pixel_format =
3382 "WGL_ARB_pixel_format",
3384 { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3385 { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3386 { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3390 static const WineGLExtension WGL_ARB_render_texture =
3392 "WGL_ARB_render_texture",
3394 { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3395 { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3399 static const WineGLExtension WGL_EXT_extensions_string =
3401 "WGL_EXT_extensions_string",
3403 { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3407 static const WineGLExtension WGL_EXT_swap_control =
3409 "WGL_EXT_swap_control",
3411 { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3412 { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3416 static const WineGLExtension WGL_NV_vertex_array_range =
3418 "WGL_NV_vertex_array_range",
3420 { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3421 { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3425 static const WineGLExtension WGL_WINE_pixel_format_passthrough =
3427 "WGL_WINE_pixel_format_passthrough",
3429 { "wglSetPixelFormatWINE", X11DRV_wglSetPixelFormatWINE },
3434 * X11DRV_WineGL_LoadExtensions
3436 static void X11DRV_WineGL_LoadExtensions(void)
3438 WineGLInfo.wglExtensions[0] = 0;
3440 /* Load Wine internal functions */
3441 register_extension(&WGL_internal_functions);
3443 /* ARB Extensions */
3445 if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3447 register_extension_string("WGL_ARB_pixel_format_float");
3448 register_extension_string("WGL_ATI_pixel_format_float");
3451 register_extension(&WGL_ARB_extensions_string);
3453 if (glxRequireVersion(3))
3454 register_extension(&WGL_ARB_make_current_read);
3456 if (glxRequireExtension("GLX_ARB_multisample"))
3457 register_extension(&WGL_ARB_multisample);
3459 /* In general pbuffer functionality requires support in the X-server. The functionality is
3460 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3461 * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3462 * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3464 * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3465 * without support in the X-server (which other Mesa based drivers require).
3467 * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3468 * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3469 * is available pbuffers must be available too. */
3470 if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3471 register_extension(&WGL_ARB_pbuffer);
3473 register_extension(&WGL_ARB_pixel_format);
3475 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3476 if (glxRequireExtension("GLX_ATI_render_texture") ||
3477 glxRequireExtension("GLX_ARB_render_texture") ||
3478 (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3480 register_extension(&WGL_ARB_render_texture);
3482 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3483 if(glxRequireExtension("GLX_NV_float_buffer"))
3484 register_extension_string("WGL_NV_float_buffer");
3486 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3487 if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3488 register_extension_string("WGL_NV_texture_rectangle");
3491 /* EXT Extensions */
3493 register_extension(&WGL_EXT_extensions_string);
3495 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3496 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3497 register_extension(&WGL_EXT_swap_control);
3499 if(glxRequireExtension("GLX_EXT_framebuffer_sRGB"))
3500 register_extension_string("WGL_EXT_framebuffer_sRGB");
3502 if(glxRequireExtension("GLX_EXT_fbconfig_packed_float"))
3503 register_extension_string("WGL_EXT_pixel_format_packed_float");
3505 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3506 if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3507 register_extension(&WGL_NV_vertex_array_range);
3509 /* WINE-specific WGL Extensions */
3511 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3512 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3514 register_extension(&WGL_WINE_pixel_format_passthrough);
3518 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3524 if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
3525 ret = physDev->drawable; /* PBuffer */
3527 ret = physDev->bitmap->glxpixmap;
3529 else if(physDev->gl_drawable)
3530 ret = physDev->gl_drawable;
3532 ret = physDev->drawable;
3536 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3539 pglXDestroyGLXPixmap(display, glxpixmap);
3540 wine_tsx11_unlock();
3545 * X11DRV_SwapBuffers
3547 * Swap the buffers of this DC
3549 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
3551 GLXDrawable drawable;
3552 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
3554 if (!has_opengl()) return FALSE;
3556 TRACE("(%p)\n", physDev);
3558 drawable = get_glxdrawable(physDev);
3562 if(physDev->pixmap) {
3563 if(pglXCopySubBufferMESA) {
3564 int w = physDev->dc_rect.right - physDev->dc_rect.left;
3565 int h = physDev->dc_rect.bottom - physDev->dc_rect.top;
3567 /* (glX)SwapBuffers has an implicit glFlush effect, however
3568 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3572 pglXCopySubBufferMESA(gdi_display, drawable, 0, 0, w, h);
3575 pglXSwapBuffers(gdi_display, drawable);
3578 pglXSwapBuffers(gdi_display, drawable);
3580 flush_gl_drawable(physDev);
3581 wine_tsx11_unlock();
3586 static long prev_time, start_time;
3587 static unsigned long frames, frames_total;
3589 DWORD time = GetTickCount();
3592 /* every 1.5 seconds */
3593 if (time - prev_time > 1500) {
3594 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
3595 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
3598 if(start_time == 0) start_time = time;
3605 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3607 WineGLPixelFormat *fmt;
3610 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
3615 ret = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
3616 wine_tsx11_unlock();
3620 #else /* no OpenGL includes */
3622 void X11DRV_OpenGL_Cleanup(void)
3626 static inline void opengl_error(void)
3629 if (!warned++) ERR("No OpenGL support compiled in.\n");
3632 int pixelformat_from_fbconfig_id(XID fbconfig_id)
3637 void mark_drawable_dirty(Drawable old, Drawable new)
3641 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
3645 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
3650 /***********************************************************************
3651 * ChoosePixelFormat (X11DRV.@)
3653 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
3654 const PIXELFORMATDESCRIPTOR *ppfd) {
3659 /***********************************************************************
3660 * DescribePixelFormat (X11DRV.@)
3662 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
3665 PIXELFORMATDESCRIPTOR *ppfd) {
3670 /***********************************************************************
3671 * GetPixelFormat (X11DRV.@)
3673 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
3678 /***********************************************************************
3679 * SetPixelFormat (X11DRV.@)
3681 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
3683 const PIXELFORMATDESCRIPTOR *ppfd) {
3688 /***********************************************************************
3689 * SwapBuffers (X11DRV.@)
3691 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
3697 * X11DRV_wglCopyContext
3699 * For OpenGL32 wglCopyContext.
3701 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
3707 * X11DRV_wglCreateContext
3709 * For OpenGL32 wglCreateContext.
3711 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
3717 * X11DRV_wglDeleteContext
3719 * For OpenGL32 wglDeleteContext.
3721 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc) {
3727 * X11DRV_wglGetProcAddress
3729 * For OpenGL32 wglGetProcAddress.
3731 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
3736 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, void *hPbuffer)
3742 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
3748 * X11DRV_wglMakeCurrent
3750 * For OpenGL32 wglMakeCurrent.
3752 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
3758 * X11DRV_wglShareLists
3760 * For OpenGL32 wglShareLists.
3762 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
3768 * X11DRV_wglUseFontBitmapsA
3770 * For OpenGL32 wglUseFontBitmapsA.
3772 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3779 * X11DRV_wglUseFontBitmapsW
3781 * For OpenGL32 wglUseFontBitmapsW.
3783 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3790 * X11DRV_wglSetPixelFormatWINE
3792 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3793 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3795 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3801 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3806 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3811 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3816 #endif /* defined(HAVE_OPENGL) */