mshtml: Added generic bind_to_tree node notification and use it to set frame's conten...
[wine/multimedia.git] / dlls / winex11.drv / opengl.c
blob19e3fe3bfce1f3206c6e145ee533d5d9f181110d
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
5 * Copyright 2005 Alex Woods
6 * Copyright 2005 Raphael Junqueira
7 * Copyright 2006-2009 Roderick Colenbrander
8 * Copyright 2006 Tomas Carnecky
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "x11drv.h"
33 #include "winternl.h"
34 #include "wine/library.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
39 #ifdef SONAME_LIBGL
41 #undef APIENTRY
42 #undef CALLBACK
43 #undef WINAPI
45 #ifdef HAVE_GL_GL_H
46 # include <GL/gl.h>
47 #endif
48 #ifdef HAVE_GL_GLX_H
49 # include <GL/glx.h>
50 #endif
52 #include "wine/wgl.h"
54 #undef APIENTRY
55 #undef CALLBACK
56 #undef WINAPI
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 {
67 const char *extName;
68 struct {
69 const char *funcName;
70 void *funcAddress;
71 } extEntryPoints[9];
72 } WineGLExtension;
74 struct WineGLInfo {
75 const char *glVersion;
76 char *glExtensions;
78 int glxVersion[2];
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;
90 BOOL glxDirect;
91 char wglExtensions[4096];
94 typedef struct wine_glpixelformat {
95 int iPixelFormat;
96 GLXFBConfig fbconfig;
97 int fmt_id;
98 int render_type;
99 BOOL offscreenOnly;
100 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
101 } WineGLPixelFormat;
103 typedef struct wine_glcontext {
104 HDC hdc;
105 BOOL do_escape;
106 BOOL has_been_current;
107 BOOL sharing;
108 BOOL gl3_context;
109 XVisualInfo *vis;
110 WineGLPixelFormat *fmt;
111 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
112 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
113 GLXContext ctx;
114 HDC read_hdc;
115 Drawable drawables[2];
116 BOOL refresh_drawables;
117 struct wine_glcontext *next;
118 struct wine_glcontext *prev;
119 } Wine_GLContext;
121 typedef struct wine_glpbuffer {
122 Drawable drawable;
123 Display* display;
124 WineGLPixelFormat* fmt;
125 int width;
126 int height;
127 int* attribList;
128 HDC hdc;
130 int use_render_texture; /* This is also the internal texture format */
131 int texture_bind_target;
132 int texture_bpp;
133 GLint texture_format;
134 GLuint texture_target;
135 GLenum texture_type;
136 GLuint texture;
137 int texture_level;
138 } Wine_GLPBuffer;
140 static Wine_GLContext *context_list;
141 static struct WineGLInfo WineGLInfo = { 0 };
142 static int use_render_texture_emulation = 1;
143 static int use_render_texture_ati = 0;
144 static int swap_interval = 1;
146 #define MAX_EXTENSIONS 16
147 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
148 static int WineGLExtensionListSize;
150 static void X11DRV_WineGL_LoadExtensions(void);
151 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count);
152 static BOOL glxRequireVersion(int requiredVersion);
153 static BOOL glxRequireExtension(const char *requiredExtension);
155 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
156 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
157 TRACE(" - dwFlags : ");
158 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
159 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
160 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
161 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
162 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
163 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
164 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
165 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
166 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
167 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
168 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
169 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
170 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
171 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
172 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
173 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
174 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
175 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
176 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
177 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
178 #undef TEST_AND_DUMP
179 TRACE("\n");
181 TRACE(" - iPixelType : ");
182 switch (ppfd->iPixelType) {
183 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
184 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
186 TRACE("\n");
188 TRACE(" - Color : %d\n", ppfd->cColorBits);
189 TRACE(" - Red : %d\n", ppfd->cRedBits);
190 TRACE(" - Green : %d\n", ppfd->cGreenBits);
191 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
192 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
193 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
194 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
195 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
196 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
198 TRACE(" - iLayerType : ");
199 switch (ppfd->iLayerType) {
200 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
201 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
202 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
204 TRACE("\n");
207 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
208 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
210 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
211 /* GLX 1.0 */
212 MAKE_FUNCPTR(glXChooseVisual)
213 MAKE_FUNCPTR(glXCopyContext)
214 MAKE_FUNCPTR(glXCreateContext)
215 MAKE_FUNCPTR(glXCreateGLXPixmap)
216 MAKE_FUNCPTR(glXGetCurrentContext)
217 MAKE_FUNCPTR(glXGetCurrentDrawable)
218 MAKE_FUNCPTR(glXDestroyContext)
219 MAKE_FUNCPTR(glXDestroyGLXPixmap)
220 MAKE_FUNCPTR(glXGetConfig)
221 MAKE_FUNCPTR(glXIsDirect)
222 MAKE_FUNCPTR(glXMakeCurrent)
223 MAKE_FUNCPTR(glXSwapBuffers)
224 MAKE_FUNCPTR(glXQueryExtension)
225 MAKE_FUNCPTR(glXQueryVersion)
226 MAKE_FUNCPTR(glXUseXFont)
228 /* GLX 1.1 */
229 MAKE_FUNCPTR(glXGetClientString)
230 MAKE_FUNCPTR(glXQueryExtensionsString)
231 MAKE_FUNCPTR(glXQueryServerString)
233 /* GLX 1.3 */
234 MAKE_FUNCPTR(glXGetFBConfigs)
235 MAKE_FUNCPTR(glXChooseFBConfig)
236 MAKE_FUNCPTR(glXCreatePbuffer)
237 MAKE_FUNCPTR(glXCreateNewContext)
238 MAKE_FUNCPTR(glXDestroyPbuffer)
239 MAKE_FUNCPTR(glXGetFBConfigAttrib)
240 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
241 MAKE_FUNCPTR(glXMakeContextCurrent)
242 MAKE_FUNCPTR(glXQueryDrawable)
243 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
245 /* GLX Extensions */
246 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
247 static void* (*pglXGetProcAddressARB)(const GLubyte *);
248 static int (*pglXSwapIntervalSGI)(int);
250 /* ATI GLX Extensions */
251 static BOOL (*pglXBindTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
252 static BOOL (*pglXReleaseTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
253 static BOOL (*pglXDrawableAttribATI)(Display *dpy, GLXDrawable draw, const int *attribList);
255 /* NV GLX Extension */
256 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
257 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
259 /* MESA GLX Extensions */
260 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
262 /* Standard OpenGL */
263 MAKE_FUNCPTR(glBindTexture)
264 MAKE_FUNCPTR(glBitmap)
265 MAKE_FUNCPTR(glCopyTexSubImage1D)
266 MAKE_FUNCPTR(glCopyTexImage2D)
267 MAKE_FUNCPTR(glCopyTexSubImage2D)
268 MAKE_FUNCPTR(glDrawBuffer)
269 MAKE_FUNCPTR(glEndList)
270 MAKE_FUNCPTR(glGetError)
271 MAKE_FUNCPTR(glGetIntegerv)
272 MAKE_FUNCPTR(glGetString)
273 MAKE_FUNCPTR(glNewList)
274 MAKE_FUNCPTR(glPixelStorei)
275 MAKE_FUNCPTR(glReadPixels)
276 MAKE_FUNCPTR(glTexImage2D)
277 MAKE_FUNCPTR(glFinish)
278 MAKE_FUNCPTR(glFlush)
279 #undef MAKE_FUNCPTR
281 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
283 /* In the future we might want to find the exact X or GLX error to report back to the app */
284 return 1;
287 static BOOL infoInitialized = FALSE;
288 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
290 int screen = DefaultScreen(gdi_display);
291 Window win = RootWindow(gdi_display, screen);
292 const char* str;
293 XVisualInfo *vis;
294 GLXContext ctx = NULL;
295 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
297 if (infoInitialized)
298 return TRUE;
299 infoInitialized = TRUE;
301 wine_tsx11_lock();
303 vis = pglXChooseVisual(gdi_display, screen, attribList);
304 if (vis) {
305 WORD old_fs = wine_get_fs();
306 /* Create a GLX Context. Without one we can't query GL information */
307 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
308 if (wine_get_fs() != old_fs)
310 wine_set_fs( old_fs );
311 wine_tsx11_unlock();
312 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
313 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
314 return FALSE;
318 if (ctx) {
319 pglXMakeCurrent(gdi_display, win, ctx);
320 } else {
321 ERR(" couldn't initialize OpenGL, expect problems\n");
322 wine_tsx11_unlock();
323 return FALSE;
326 WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
327 str = (const char *) pglGetString(GL_EXTENSIONS);
328 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
329 strcpy(WineGLInfo.glExtensions, str);
331 /* Get the common GLX version supported by GLX client and server ( major/minor) */
332 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
334 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
335 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
336 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
338 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
339 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
340 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
342 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
343 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
345 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
346 TRACE("GL renderer : %s.\n", pglGetString(GL_RENDERER));
347 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
348 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
349 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
350 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
351 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
352 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
354 if(vis) XFree(vis);
355 if(ctx) {
356 pglXMakeCurrent(gdi_display, None, NULL);
357 pglXDestroyContext(gdi_display, ctx);
359 wine_tsx11_unlock();
360 return TRUE;
363 void X11DRV_OpenGL_Cleanup(void)
365 HeapFree(GetProcessHeap(), 0, WineGLInfo.glExtensions);
366 infoInitialized = FALSE;
369 static BOOL has_opengl(void)
371 static int init_done;
372 static void *opengl_handle;
374 char buffer[200];
375 int error_base, event_base;
377 if (init_done) return (opengl_handle != NULL);
378 init_done = 1;
380 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
381 and include all dependencies */
382 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
383 if (opengl_handle == NULL)
385 ERR( "Failed to load libGL: %s\n", buffer );
386 ERR( "OpenGL support is disabled.\n");
387 return FALSE;
390 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
391 if (pglXGetProcAddressARB == NULL) {
392 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
393 goto failed;
396 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
398 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
399 goto failed; \
400 } while(0)
402 /* GLX 1.0 */
403 LOAD_FUNCPTR(glXChooseVisual);
404 LOAD_FUNCPTR(glXCopyContext);
405 LOAD_FUNCPTR(glXCreateContext);
406 LOAD_FUNCPTR(glXCreateGLXPixmap);
407 LOAD_FUNCPTR(glXGetCurrentContext);
408 LOAD_FUNCPTR(glXGetCurrentDrawable);
409 LOAD_FUNCPTR(glXDestroyContext);
410 LOAD_FUNCPTR(glXDestroyGLXPixmap);
411 LOAD_FUNCPTR(glXGetConfig);
412 LOAD_FUNCPTR(glXIsDirect);
413 LOAD_FUNCPTR(glXMakeCurrent);
414 LOAD_FUNCPTR(glXSwapBuffers);
415 LOAD_FUNCPTR(glXQueryExtension);
416 LOAD_FUNCPTR(glXQueryVersion);
417 LOAD_FUNCPTR(glXUseXFont);
419 /* GLX 1.1 */
420 LOAD_FUNCPTR(glXGetClientString);
421 LOAD_FUNCPTR(glXQueryExtensionsString);
422 LOAD_FUNCPTR(glXQueryServerString);
424 /* GLX 1.3 */
425 LOAD_FUNCPTR(glXCreatePbuffer);
426 LOAD_FUNCPTR(glXCreateNewContext);
427 LOAD_FUNCPTR(glXDestroyPbuffer);
428 LOAD_FUNCPTR(glXMakeContextCurrent);
429 LOAD_FUNCPTR(glXGetCurrentReadDrawable);
430 LOAD_FUNCPTR(glXGetFBConfigs);
432 /* Standard OpenGL calls */
433 LOAD_FUNCPTR(glBindTexture);
434 LOAD_FUNCPTR(glBitmap);
435 LOAD_FUNCPTR(glCopyTexSubImage1D);
436 LOAD_FUNCPTR(glCopyTexImage2D);
437 LOAD_FUNCPTR(glCopyTexSubImage2D);
438 LOAD_FUNCPTR(glDrawBuffer);
439 LOAD_FUNCPTR(glEndList);
440 LOAD_FUNCPTR(glGetError);
441 LOAD_FUNCPTR(glGetIntegerv);
442 LOAD_FUNCPTR(glGetString);
443 LOAD_FUNCPTR(glNewList);
444 LOAD_FUNCPTR(glPixelStorei);
445 LOAD_FUNCPTR(glReadPixels);
446 LOAD_FUNCPTR(glTexImage2D);
447 LOAD_FUNCPTR(glFinish);
448 LOAD_FUNCPTR(glFlush);
449 #undef LOAD_FUNCPTR
451 /* It doesn't matter if these fail. They'll only be used if the driver reports
452 the associated extension is available (and if a driver reports the extension
453 is available but fails to provide the functions, it's quite broken) */
454 #define LOAD_FUNCPTR(f) p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)
455 /* ARB GLX Extension */
456 LOAD_FUNCPTR(glXCreateContextAttribsARB);
457 /* NV GLX Extension */
458 LOAD_FUNCPTR(glXAllocateMemoryNV);
459 LOAD_FUNCPTR(glXFreeMemoryNV);
460 #undef LOAD_FUNCPTR
462 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
464 wine_tsx11_lock();
465 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
466 TRACE("GLX is up and running error_base = %d\n", error_base);
467 } else {
468 wine_tsx11_unlock();
469 ERR( "GLX extension is missing, disabling OpenGL.\n" );
470 goto failed;
473 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
474 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
475 * rendering is used.
477 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
478 * depends on the reported GLX client / server version and on the client / server extension list.
479 * Those don't have to be the same.
481 * In general the server GLX information lists the capabilities in case of indirect rendering.
482 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
483 * available and in that case the client GLX informat can be used.
484 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
485 * in the GLX version/extension list. When a program does this it works for certain for both
486 * direct and indirect rendering.
488 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
489 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
490 * extension list which causes this extension not to be listed. (Wine requires this extension).
491 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
492 * pbuffers is around.
494 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
495 * the ATI drivers and from then on use GLX client information for them.
498 if(glxRequireVersion(3)) {
499 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
500 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
501 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
502 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
503 } else if(glxRequireExtension("GLX_SGIX_fbconfig")) {
504 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
505 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
506 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
508 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
509 * enable this function when the Xserver understand GLX 1.3 or newer
511 pglXQueryDrawable = NULL;
512 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
513 TRACE("Overriding ATI GLX capabilities!\n");
514 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
515 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
516 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
517 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
519 /* Use client GLX information in case of the ATI drivers. We override the
520 * capabilities over here and not somewhere else as ATI might better their
521 * life in the future. In case they release proper drivers this block of
522 * code won't be called. */
523 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
524 } else {
525 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
528 if(glxRequireExtension("GLX_ATI_render_texture")) {
529 use_render_texture_ati = 1;
530 pglXBindTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageATI");
531 pglXReleaseTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageATI");
532 pglXDrawableAttribATI = pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribATI");
535 if(glxRequireExtension("GLX_MESA_copy_sub_buffer")) {
536 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
539 X11DRV_WineGL_LoadExtensions();
541 wine_tsx11_unlock();
542 return TRUE;
544 failed:
545 wine_dlclose(opengl_handle, NULL, 0);
546 opengl_handle = NULL;
547 return FALSE;
550 static inline Wine_GLContext *alloc_context(void)
552 Wine_GLContext *ret;
554 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
556 ret->next = context_list;
557 if (context_list) context_list->prev = ret;
558 context_list = ret;
560 return ret;
563 static inline void free_context(Wine_GLContext *context)
565 if (context->next != NULL) context->next->prev = context->prev;
566 if (context->prev != NULL) context->prev->next = context->next;
567 else context_list = context->next;
569 if (context->vis) XFree(context->vis);
570 HeapFree(GetProcessHeap(), 0, context);
573 static inline BOOL is_valid_context( Wine_GLContext *ctx )
575 Wine_GLContext *ptr;
576 for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
577 return (ptr != NULL);
580 static int describeContext(Wine_GLContext* ctx) {
581 int tmp;
582 int ctx_vis_id;
583 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
584 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
585 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
586 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
587 TRACE(" - VISUAL_ID 0x%x\n", tmp);
588 ctx_vis_id = tmp;
589 return ctx_vis_id;
592 static BOOL describeDrawable(X11DRV_PDEVICE *physDev) {
593 int tmp;
594 WineGLPixelFormat *fmt;
595 int fmt_count = 0;
597 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE /* Offscreen */, &fmt_count);
598 if(!fmt) return FALSE;
600 TRACE(" HDC %p has:\n", physDev->hdc);
601 TRACE(" - iPixelFormat %d\n", fmt->iPixelFormat);
602 TRACE(" - Drawable %p\n", (void*) get_glxdrawable(physDev));
603 TRACE(" - FBCONFIG_ID 0x%x\n", fmt->fmt_id);
605 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &tmp);
606 TRACE(" - VISUAL_ID 0x%x\n", tmp);
608 return TRUE;
611 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
612 int nAttribs = 0;
613 unsigned cur = 0;
614 int pop;
615 int drawattrib = 0;
616 int nvfloatattrib = GLX_DONT_CARE;
617 int pixelattrib = ~0;
619 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
620 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
621 while (iWGLAttr && 0 != iWGLAttr[cur]) {
622 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
624 switch (iWGLAttr[cur]) {
625 case WGL_AUX_BUFFERS_ARB:
626 pop = iWGLAttr[++cur];
627 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
628 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
629 break;
630 case WGL_COLOR_BITS_ARB:
631 pop = iWGLAttr[++cur];
632 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
633 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
634 break;
635 case WGL_BLUE_BITS_ARB:
636 pop = iWGLAttr[++cur];
637 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
638 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
639 break;
640 case WGL_RED_BITS_ARB:
641 pop = iWGLAttr[++cur];
642 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
643 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
644 break;
645 case WGL_GREEN_BITS_ARB:
646 pop = iWGLAttr[++cur];
647 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
648 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
649 break;
650 case WGL_ALPHA_BITS_ARB:
651 pop = iWGLAttr[++cur];
652 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
653 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
654 break;
655 case WGL_DEPTH_BITS_ARB:
656 pop = iWGLAttr[++cur];
657 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
658 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
659 break;
660 case WGL_STENCIL_BITS_ARB:
661 pop = iWGLAttr[++cur];
662 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
663 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
664 break;
665 case WGL_DOUBLE_BUFFER_ARB:
666 pop = iWGLAttr[++cur];
667 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
668 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
669 break;
670 case WGL_STEREO_ARB:
671 pop = iWGLAttr[++cur];
672 PUSH2(oGLXAttr, GLX_STEREO, pop);
673 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
674 break;
676 case WGL_PIXEL_TYPE_ARB:
677 pop = iWGLAttr[++cur];
678 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
679 switch (pop) {
680 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
681 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
682 /* 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 */
683 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
684 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
685 default:
686 ERR("unexpected PixelType(%x)\n", pop);
687 pop = 0;
689 break;
691 case WGL_SUPPORT_GDI_ARB:
692 /* This flag is set in a WineGLPixelFormat */
693 pop = iWGLAttr[++cur];
694 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
695 break;
697 case WGL_DRAW_TO_BITMAP_ARB:
698 /* This flag is set in a WineGLPixelFormat */
699 pop = iWGLAttr[++cur];
700 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
701 break;
703 case WGL_DRAW_TO_WINDOW_ARB:
704 pop = iWGLAttr[++cur];
705 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
706 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
707 if (pop) {
708 drawattrib |= GLX_WINDOW_BIT;
710 break;
712 case WGL_DRAW_TO_PBUFFER_ARB:
713 pop = iWGLAttr[++cur];
714 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
715 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
716 if (pop) {
717 drawattrib |= GLX_PBUFFER_BIT;
719 break;
721 case WGL_ACCELERATION_ARB:
722 /* This flag is set in a WineGLPixelFormat */
723 pop = iWGLAttr[++cur];
724 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
725 break;
727 case WGL_SUPPORT_OPENGL_ARB:
728 pop = iWGLAttr[++cur];
729 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
730 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
731 break;
733 case WGL_SWAP_METHOD_ARB:
734 pop = iWGLAttr[++cur];
735 /* For now we ignore this and just return SWAP_EXCHANGE */
736 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
737 break;
739 case WGL_PBUFFER_LARGEST_ARB:
740 pop = iWGLAttr[++cur];
741 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
742 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
743 break;
745 case WGL_SAMPLE_BUFFERS_ARB:
746 pop = iWGLAttr[++cur];
747 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
748 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
749 break;
751 case WGL_SAMPLES_ARB:
752 pop = iWGLAttr[++cur];
753 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
754 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
755 break;
757 case WGL_TEXTURE_FORMAT_ARB:
758 case WGL_TEXTURE_TARGET_ARB:
759 case WGL_MIPMAP_TEXTURE_ARB:
760 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
761 pop = iWGLAttr[++cur];
762 if (NULL == pbuf) {
763 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
765 if (use_render_texture_ati) {
766 /** nothing to do here */
768 else if (!use_render_texture_emulation) {
769 if (WGL_NO_TEXTURE_ARB != pop) {
770 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
771 return -1; /** error: don't support it */
772 } else {
773 drawattrib |= GLX_PBUFFER_BIT;
776 break ;
777 case WGL_FLOAT_COMPONENTS_NV:
778 nvfloatattrib = iWGLAttr[++cur];
779 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
780 break ;
781 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
782 case WGL_BIND_TO_TEXTURE_RGB_ARB:
783 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
784 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
785 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
786 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
787 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
788 pop = iWGLAttr[++cur];
789 /** cannot be converted, see direct handling on
790 * - wglGetPixelFormatAttribivARB
791 * TODO: wglChoosePixelFormat
793 break ;
794 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
795 pop = iWGLAttr[++cur];
796 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
797 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
798 break ;
800 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
801 pop = iWGLAttr[++cur];
802 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
803 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
804 break ;
805 default:
806 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
807 break;
809 ++cur;
812 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
813 * all formats. Unless drawattrib is set to a non-zero value override it with ~0, so that pixmap and pbuffer
814 * formats appear as well. */
815 if(!drawattrib) drawattrib = ~0;
816 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
817 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
819 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
820 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to ~0 unless
821 * it is overridden. */
822 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
823 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
825 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
826 if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
827 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
828 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
831 return nAttribs;
834 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
836 int render_type=0, render_type_bit;
837 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
838 switch(render_type_bit)
840 case GLX_RGBA_BIT:
841 render_type = GLX_RGBA_TYPE;
842 break;
843 case GLX_COLOR_INDEX_BIT:
844 render_type = GLX_COLOR_INDEX_TYPE;
845 break;
846 case GLX_RGBA_FLOAT_BIT:
847 render_type = GLX_RGBA_FLOAT_TYPE;
848 break;
849 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
850 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
851 break;
852 default:
853 ERR("Unknown render_type: %x\n", render_type);
855 return render_type;
858 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
859 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
861 int dbuf, value;
862 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
863 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
865 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
866 * the GLX_PIXMAP_BIT set. */
867 return !dbuf && (value & GLX_PIXMAP_BIT);
870 static WineGLPixelFormat *get_formats(Display *display, int *size_ret, int *onscreen_size_ret)
872 static WineGLPixelFormat *list;
873 static int size, onscreen_size;
875 int fmt_id, nCfgs, i, run, bmp_formats;
876 GLXFBConfig* cfgs;
877 XVisualInfo *visinfo;
879 wine_tsx11_lock();
880 if (list) goto done;
882 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
883 if (NULL == cfgs || 0 == nCfgs) {
884 if(cfgs != NULL) XFree(cfgs);
885 wine_tsx11_unlock();
886 ERR("glXChooseFBConfig returns NULL\n");
887 return NULL;
890 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
891 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
892 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
893 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
895 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
897 for(i=0, bmp_formats=0; i<nCfgs; i++)
899 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
900 bmp_formats++;
902 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
904 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats)*sizeof(WineGLPixelFormat));
906 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
907 * Do this as GLX doesn't guarantee that the list is sorted */
908 for(run=0; run < 2; run++)
910 for(i=0; i<nCfgs; i++) {
911 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
912 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
914 /* The first run we only add onscreen formats (ones which have an associated X Visual).
915 * The second run we only set offscreen formats. */
916 if(!run && visinfo)
918 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
919 * The contents is copied to the destination using XCopyArea. For the copying to work
920 * the depth of the source and destination window should be the same. In general this should
921 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
922 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
923 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
924 * list formats with the same depth. */
925 if(visinfo->depth != screen_depth)
927 XFree(visinfo);
928 continue;
931 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
932 list[size].iPixelFormat = size+1; /* The index starts at 1 */
933 list[size].fbconfig = cfgs[i];
934 list[size].fmt_id = fmt_id;
935 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
936 list[size].offscreenOnly = FALSE;
937 list[size].dwFlags = 0;
938 size++;
939 onscreen_size++;
941 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
942 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
944 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
945 list[size].iPixelFormat = size+1; /* The index starts at 1 */
946 list[size].fbconfig = cfgs[i];
947 list[size].fmt_id = fmt_id;
948 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
949 list[size].offscreenOnly = FALSE;
950 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
951 size++;
952 onscreen_size++;
954 } else if(run && !visinfo) {
955 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
956 list[size].iPixelFormat = size+1; /* The index starts at 1 */
957 list[size].fbconfig = cfgs[i];
958 list[size].fmt_id = fmt_id;
959 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
960 list[size].offscreenOnly = TRUE;
961 list[size].dwFlags = 0;
962 size++;
965 if (visinfo) XFree(visinfo);
969 if(cfgs != NULL) XFree(cfgs);
971 done:
972 if (size_ret) *size_ret = size;
973 if (onscreen_size_ret) *onscreen_size_ret = onscreen_size;
974 wine_tsx11_unlock();
975 return list;
978 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
979 * In our WGL implementation we only support a subset of these formats namely the format of
980 * Wine's main visual and offscreen formats (if they are available).
981 * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
982 * and it returns the number of supported WGL formats in fmt_count.
984 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
986 WineGLPixelFormat *list, *res = NULL;
987 int size, onscreen_size;
989 if (!(list = get_formats(display, &size, &onscreen_size ))) return NULL;
991 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
992 * iPixelFormat in case of probing the number of pixelformats.
994 if((iPixelFormat > 0) && (iPixelFormat <= size) &&
995 (!list[iPixelFormat-1].offscreenOnly || AllowOffscreen)) {
996 res = &list[iPixelFormat-1];
997 TRACE("Returning FBConfig=%p for iPixelFormat=%d\n", res->fbconfig, iPixelFormat);
1000 if(AllowOffscreen)
1001 *fmt_count = size;
1002 else
1003 *fmt_count = onscreen_size;
1005 TRACE("Number of returned pixelformats=%d\n", *fmt_count);
1007 return res;
1010 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
1011 static WineGLPixelFormat* ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id, DWORD dwFlags)
1013 WineGLPixelFormat *list;
1014 int i, size;
1016 if (!(list = get_formats(display, &size, NULL ))) return NULL;
1018 for(i=0; i<size; i++) {
1019 /* A GLX format can appear multiple times in the pixel format list due to fake formats for bitmap rendering.
1020 * Fake formats might get selected when the user passes the proper flags using the dwFlags parameter. */
1021 if( (list[i].fmt_id == fmt_id) && ((list[i].dwFlags & dwFlags) == dwFlags) ) {
1022 TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", list[i].iPixelFormat, fmt_id);
1023 return &list[i];
1026 TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1027 return NULL;
1030 int pixelformat_from_fbconfig_id(XID fbconfig_id)
1032 WineGLPixelFormat *fmt;
1034 if (!fbconfig_id) return 0;
1036 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
1037 if(fmt)
1038 return fmt->iPixelFormat;
1039 /* This will happen on hwnds without a pixel format set; it's ok */
1040 return 0;
1044 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1045 void mark_drawable_dirty(Drawable old, Drawable new)
1047 Wine_GLContext *ctx;
1048 for (ctx = context_list; ctx; ctx = ctx->next) {
1049 if (old == ctx->drawables[0]) {
1050 ctx->drawables[0] = new;
1051 ctx->refresh_drawables = TRUE;
1053 if (old == ctx->drawables[1]) {
1054 ctx->drawables[1] = new;
1055 ctx->refresh_drawables = TRUE;
1060 /* Given the current context, make sure its drawable is sync'd */
1061 static inline void sync_context(Wine_GLContext *context)
1063 if(context && context->refresh_drawables) {
1064 if (glxRequireVersion(3))
1065 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1066 context->drawables[1], context->ctx);
1067 else
1068 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1069 context->refresh_drawables = FALSE;
1074 static GLXContext create_glxcontext(Display *display, Wine_GLContext *context, GLXContext shareList)
1076 GLXContext ctx;
1078 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1079 BOOL indirect = (context->fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? FALSE : TRUE;
1081 if(context->gl3_context)
1083 if(context->numAttribs)
1084 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1085 else
1086 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1088 else if(context->vis)
1089 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1090 else /* Create a GLX Context for a pbuffer */
1091 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1093 return ctx;
1097 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
1099 return pglXCreateGLXPixmap(display, vis, parent);
1103 static XID create_bitmap_glxpixmap(X11DRV_PDEVICE *physDev, WineGLPixelFormat *fmt)
1105 GLXPixmap ret = 0;
1106 XVisualInfo *vis;
1108 wine_tsx11_lock();
1110 vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1111 if(vis) {
1112 if(vis->depth == physDev->bitmap->pixmap_depth)
1113 ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
1114 XFree(vis);
1116 wine_tsx11_unlock();
1117 TRACE("return %lx\n", ret);
1118 return ret;
1122 * X11DRV_ChoosePixelFormat
1124 * Equivalent to glXChooseVisual.
1126 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
1127 const PIXELFORMATDESCRIPTOR *ppfd) {
1128 WineGLPixelFormat *list;
1129 int onscreen_size;
1130 int ret = 0;
1131 int value = 0;
1132 int i = 0;
1133 int bestFormat = -1;
1134 int bestDBuffer = -1;
1135 int bestStereo = -1;
1136 int bestColor = -1;
1137 int bestAlpha = -1;
1138 int bestDepth = -1;
1139 int bestStencil = -1;
1140 int bestAux = -1;
1142 if (!has_opengl()) return 0;
1144 if (TRACE_ON(wgl)) {
1145 TRACE("(%p,%p)\n", physDev, ppfd);
1147 dump_PIXELFORMATDESCRIPTOR(ppfd);
1150 if (!(list = get_formats(gdi_display, NULL, &onscreen_size ))) return 0;
1152 wine_tsx11_lock();
1153 for(i=0; i<onscreen_size; i++)
1155 int dwFlags = 0;
1156 int iPixelType = 0;
1157 int alpha=0, color=0, depth=0, stencil=0, aux=0;
1158 WineGLPixelFormat *fmt = &list[i];
1160 /* Pixel type */
1161 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1162 if (value & GLX_RGBA_BIT)
1163 iPixelType = PFD_TYPE_RGBA;
1164 else
1165 iPixelType = PFD_TYPE_COLORINDEX;
1167 if (ppfd->iPixelType != iPixelType)
1169 TRACE("pixel type mismatch for iPixelFormat=%d\n", i+1);
1170 continue;
1173 /* Only use bitmap capable for formats for bitmap rendering.
1174 * See get_formats for more info. */
1175 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (fmt->dwFlags & PFD_DRAW_TO_BITMAP))
1177 TRACE("PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i+1);
1178 continue;
1181 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1182 if (value) dwFlags |= PFD_DOUBLEBUFFER;
1183 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value);
1184 if (value) dwFlags |= PFD_STEREO;
1185 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &color); /* cColorBits */
1186 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &alpha); /* cAlphaBits */
1187 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &depth); /* cDepthBits */
1188 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &stencil); /* cStencilBits */
1189 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &aux); /* cAuxBuffers */
1191 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
1192 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
1193 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
1194 * formats without the given flag set.
1195 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
1196 * has indicated that a format without stereo is returned when stereo is unavailable.
1197 * So in case PFD_STEREO is set, formats that support it should have priority above formats
1198 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
1200 * To summarize the following is most likely the correct behavior:
1201 * stereo not set -> prefer no-stereo formats, else also accept stereo formats
1202 * stereo set -> prefer stereo formats, else also accept no-stereo formats
1203 * stereo don't care -> it doesn't matter whether we get stereo or not
1205 * In Wine we will treat no-stereo the same way as don't care because it makes
1206 * format selection even more complicated and second drivers with Stereo advertise
1207 * each format twice anyway.
1210 /* Doublebuffer, see the comments above */
1211 if( !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) ) {
1212 if( ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
1213 ((dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)) )
1215 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1216 bestStereo = dwFlags & PFD_STEREO;
1217 bestAlpha = alpha;
1218 bestColor = color;
1219 bestDepth = depth;
1220 bestStencil = stencil;
1221 bestAux = aux;
1222 bestFormat = i;
1223 continue;
1225 if(bestDBuffer != -1 && (dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer)
1226 continue;
1229 /* Stereo, see the comments above. */
1230 if( !(ppfd->dwFlags & PFD_STEREO_DONTCARE) ) {
1231 if( ((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
1232 ((dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)) )
1234 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1235 bestStereo = dwFlags & PFD_STEREO;
1236 bestAlpha = alpha;
1237 bestColor = color;
1238 bestDepth = depth;
1239 bestStencil = stencil;
1240 bestAux = aux;
1241 bestFormat = i;
1242 continue;
1244 if(bestStereo != -1 && (dwFlags & PFD_STEREO) != bestStereo)
1245 continue;
1248 /* Below we will do a number of checks to select the 'best' pixelformat.
1249 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
1250 * The code works by trying to match the most important options as close as possible.
1251 * When a reasonable format is found, we will try to match more options.
1252 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
1253 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
1254 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
1256 /* Color bits */
1257 if(ppfd->cColorBits) {
1258 if( ((ppfd->cColorBits > bestColor) && (color > bestColor)) ||
1259 ((color >= ppfd->cColorBits) && (color < bestColor)) )
1261 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1262 bestStereo = dwFlags & PFD_STEREO;
1263 bestAlpha = alpha;
1264 bestColor = color;
1265 bestDepth = depth;
1266 bestStencil = stencil;
1267 bestAux = aux;
1268 bestFormat = i;
1269 continue;
1270 } else if(bestColor != color) { /* Do further checks if the format is compatible */
1271 TRACE("color mismatch for iPixelFormat=%d\n", i+1);
1272 continue;
1276 /* Alpha bits */
1277 if(ppfd->cAlphaBits) {
1278 if( ((ppfd->cAlphaBits > bestAlpha) && (alpha > bestAlpha)) ||
1279 ((alpha >= ppfd->cAlphaBits) && (alpha < bestAlpha)) )
1281 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1282 bestStereo = dwFlags & PFD_STEREO;
1283 bestAlpha = alpha;
1284 bestColor = color;
1285 bestDepth = depth;
1286 bestStencil = stencil;
1287 bestAux = aux;
1288 bestFormat = i;
1289 continue;
1290 } else if(bestAlpha != alpha) {
1291 TRACE("alpha mismatch for iPixelFormat=%d\n", i+1);
1292 continue;
1296 /* Depth bits */
1297 if(ppfd->cDepthBits) {
1298 if( ((ppfd->cDepthBits > bestDepth) && (depth > bestDepth)) ||
1299 ((depth >= ppfd->cDepthBits) && (depth < bestDepth)) )
1301 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1302 bestStereo = dwFlags & PFD_STEREO;
1303 bestAlpha = alpha;
1304 bestColor = color;
1305 bestDepth = depth;
1306 bestStencil = stencil;
1307 bestAux = aux;
1308 bestFormat = i;
1309 continue;
1310 } else if(bestDepth != depth) {
1311 TRACE("depth mismatch for iPixelFormat=%d\n", i+1);
1312 continue;
1316 /* Stencil bits */
1317 if(ppfd->cStencilBits) {
1318 if( ((ppfd->cStencilBits > bestStencil) && (stencil > bestStencil)) ||
1319 ((stencil >= ppfd->cStencilBits) && (stencil < bestStencil)) )
1321 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1322 bestStereo = dwFlags & PFD_STEREO;
1323 bestAlpha = alpha;
1324 bestColor = color;
1325 bestDepth = depth;
1326 bestStencil = stencil;
1327 bestAux = aux;
1328 bestFormat = i;
1329 continue;
1330 } else if(bestStencil != stencil) {
1331 TRACE("stencil mismatch for iPixelFormat=%d\n", i+1);
1332 continue;
1336 /* Aux buffers */
1337 if(ppfd->cAuxBuffers) {
1338 if( ((ppfd->cAuxBuffers > bestAux) && (aux > bestAux)) ||
1339 ((aux >= ppfd->cAuxBuffers) && (aux < bestAux)) )
1341 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1342 bestStereo = dwFlags & PFD_STEREO;
1343 bestAlpha = alpha;
1344 bestColor = color;
1345 bestDepth = depth;
1346 bestStencil = stencil;
1347 bestAux = aux;
1348 bestFormat = i;
1349 continue;
1350 } else if(bestAux != aux) {
1351 TRACE("aux mismatch for iPixelFormat=%d\n", i+1);
1352 continue;
1357 if(bestFormat == -1) {
1358 TRACE("No matching mode was found returning 0\n");
1359 ret = 0;
1361 else {
1362 ret = bestFormat+1; /* the return value should be a 1-based index */
1363 TRACE("Successfully found a matching mode, returning index: %d %x\n", ret, list[bestFormat].fmt_id);
1366 wine_tsx11_unlock();
1368 return ret;
1371 * X11DRV_DescribePixelFormat
1373 * Get the pixel-format descriptor associated to the given id
1375 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
1376 int iPixelFormat,
1377 UINT nBytes,
1378 PIXELFORMATDESCRIPTOR *ppfd) {
1379 /*XVisualInfo *vis;*/
1380 int value;
1381 int rb,gb,bb,ab;
1382 WineGLPixelFormat *fmt;
1383 int ret = 0;
1384 int fmt_count = 0;
1386 if (!has_opengl()) return 0;
1388 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
1390 /* 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 */
1391 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1392 if (ppfd == NULL) {
1393 /* The application is only querying the number of pixelformats */
1394 return fmt_count;
1395 } else if(fmt == NULL) {
1396 WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1397 return 0;
1400 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1401 ERR("Wrong structure size !\n");
1402 /* Should set error */
1403 return 0;
1406 ret = fmt_count;
1408 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1409 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1410 ppfd->nVersion = 1;
1412 /* These flags are always the same... */
1413 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1414 /* Now the flags extracted from the Visual */
1416 wine_tsx11_lock();
1418 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1419 if(value & GLX_WINDOW_BIT)
1420 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1422 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1423 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1424 * 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
1425 * offered the GDI bit either. */
1426 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1428 /* PFD_GENERIC_FORMAT - gdi software rendering
1429 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1430 * none set - full hardware accelerated by a ICD
1432 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1433 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1435 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1436 if (value) {
1437 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1438 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1440 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1442 /* Pixel type */
1443 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1444 if (value & GLX_RGBA_BIT)
1445 ppfd->iPixelType = PFD_TYPE_RGBA;
1446 else
1447 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1449 /* Color bits */
1450 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1451 ppfd->cColorBits = value;
1453 /* Red, green, blue and alpha bits / shifts */
1454 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1455 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1456 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1457 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1458 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1460 ppfd->cRedBits = rb;
1461 ppfd->cRedShift = gb + bb + ab;
1462 ppfd->cBlueBits = bb;
1463 ppfd->cBlueShift = ab;
1464 ppfd->cGreenBits = gb;
1465 ppfd->cGreenShift = bb + ab;
1466 ppfd->cAlphaBits = ab;
1467 ppfd->cAlphaShift = 0;
1468 } else {
1469 ppfd->cRedBits = 0;
1470 ppfd->cRedShift = 0;
1471 ppfd->cBlueBits = 0;
1472 ppfd->cBlueShift = 0;
1473 ppfd->cGreenBits = 0;
1474 ppfd->cGreenShift = 0;
1475 ppfd->cAlphaBits = 0;
1476 ppfd->cAlphaShift = 0;
1479 /* Accum RGBA bits */
1480 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1481 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1482 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1483 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1485 ppfd->cAccumBits = rb+gb+bb+ab;
1486 ppfd->cAccumRedBits = rb;
1487 ppfd->cAccumGreenBits = gb;
1488 ppfd->cAccumBlueBits = bb;
1489 ppfd->cAccumAlphaBits = ab;
1491 /* Aux bits */
1492 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1493 ppfd->cAuxBuffers = value;
1495 /* Depth bits */
1496 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1497 ppfd->cDepthBits = value;
1499 /* stencil bits */
1500 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1501 ppfd->cStencilBits = value;
1503 wine_tsx11_unlock();
1505 ppfd->iLayerType = PFD_MAIN_PLANE;
1507 if (TRACE_ON(wgl)) {
1508 dump_PIXELFORMATDESCRIPTOR(ppfd);
1511 return ret;
1515 * X11DRV_GetPixelFormat
1517 * Get the pixel-format id used by this DC
1519 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1520 WineGLPixelFormat *fmt;
1521 int tmp;
1522 TRACE("(%p)\n", physDev);
1524 if (!physDev->current_pf) return 0; /* not set yet */
1526 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE, &tmp);
1527 if(!fmt)
1529 ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physDev->current_pf);
1530 return 0;
1532 else if(fmt->offscreenOnly)
1534 /* Offscreen formats can't be used with traditional WGL calls.
1535 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1536 TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1537 return 1;
1540 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1541 return physDev->current_pf;
1544 /* This function is the core of X11DRV_SetPixelFormat and X11DRV_SetPixelFormatWINE.
1545 * Both functions are the same except that X11DRV_SetPixelFormatWINE allows you to
1546 * set the pixel format multiple times. */
1547 static BOOL internal_SetPixelFormat(X11DRV_PDEVICE *physDev,
1548 int iPixelFormat,
1549 const PIXELFORMATDESCRIPTOR *ppfd) {
1550 WineGLPixelFormat *fmt;
1551 int value;
1552 HWND hwnd;
1554 /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1555 if(get_glxdrawable(physDev) == root_window)
1557 ERR("Invalid operation on root_window\n");
1558 return FALSE;
1561 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1562 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1563 if(!fmt) {
1564 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1565 return FALSE;
1568 wine_tsx11_lock();
1569 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1570 wine_tsx11_unlock();
1572 hwnd = WindowFromDC(physDev->hdc);
1573 if(hwnd) {
1574 if(!(value&GLX_WINDOW_BIT)) {
1575 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1576 return FALSE;
1579 if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, (WPARAM)fmt->fmt_id, 0)) {
1580 ERR("Couldn't set format of the window, returning failure\n");
1581 return FALSE;
1584 else if(physDev->bitmap) {
1585 if(!(value&GLX_PIXMAP_BIT)) {
1586 WARN("Pixel format %d is not compatible for bitmap rendering\n", iPixelFormat);
1587 return FALSE;
1590 physDev->bitmap->glxpixmap = create_bitmap_glxpixmap(physDev, fmt);
1591 if(!physDev->bitmap->glxpixmap) {
1592 WARN("Couldn't create glxpixmap for pixel format %d\n", iPixelFormat);
1593 return FALSE;
1596 else {
1597 FIXME("called on a non-window, non-bitmap object?\n");
1600 physDev->current_pf = iPixelFormat;
1602 if (TRACE_ON(wgl)) {
1603 int gl_test = 0;
1605 wine_tsx11_lock();
1606 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1607 if (gl_test) {
1608 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1609 } else {
1610 TRACE(" FBConfig have :\n");
1611 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1612 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1613 TRACE(" - VISUAL_ID 0x%x\n", value);
1614 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1615 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1617 wine_tsx11_unlock();
1619 return TRUE;
1624 * X11DRV_SetPixelFormat
1626 * Set the pixel-format id used by this DC
1628 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1629 int iPixelFormat,
1630 const PIXELFORMATDESCRIPTOR *ppfd) {
1631 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1633 if (!has_opengl()) return FALSE;
1635 if(physDev->current_pf) /* cannot change it if already set */
1636 return (physDev->current_pf == iPixelFormat);
1638 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
1642 * X11DRV_wglCopyContext
1644 * For OpenGL32 wglCopyContext.
1646 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
1647 Wine_GLContext *src = (Wine_GLContext*)hglrcSrc;
1648 Wine_GLContext *dst = (Wine_GLContext*)hglrcDst;
1650 TRACE("hglrcSrc: (%p), hglrcDst: (%p), mask: %#x\n", hglrcSrc, hglrcDst, mask);
1652 wine_tsx11_lock();
1653 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1654 wine_tsx11_unlock();
1656 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1657 return TRUE;
1661 * X11DRV_wglCreateContext
1663 * For OpenGL32 wglCreateContext.
1665 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1667 Wine_GLContext *ret;
1668 WineGLPixelFormat *fmt;
1669 int hdcPF = physDev->current_pf;
1670 int fmt_count = 0;
1671 HDC hdc = physDev->hdc;
1673 TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1675 if (!has_opengl()) return 0;
1677 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
1678 /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1679 * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1680 * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1681 * If this fails something is very wrong on the system. */
1682 if(!fmt) {
1683 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
1684 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1685 return NULL;
1688 wine_tsx11_lock();
1689 ret = alloc_context();
1690 ret->hdc = hdc;
1691 ret->fmt = fmt;
1692 ret->has_been_current = FALSE;
1693 ret->sharing = FALSE;
1695 ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1696 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1697 wine_tsx11_unlock();
1699 TRACE(" creating context %p (GL context creation delayed)\n", ret);
1700 return (HGLRC) ret;
1704 * X11DRV_wglDeleteContext
1706 * For OpenGL32 wglDeleteContext.
1708 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc)
1710 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1711 BOOL ret = TRUE;
1713 TRACE("(%p)\n", hglrc);
1715 if (!has_opengl()) return 0;
1717 wine_tsx11_lock();
1718 /* A game (Half Life not to name it) deletes twice the same context,
1719 * so make sure it is valid first */
1720 if (is_valid_context( ctx ))
1722 if (ctx->ctx) pglXDestroyContext(gdi_display, ctx->ctx);
1723 free_context(ctx);
1725 else
1727 WARN("Error deleting context !\n");
1728 SetLastError(ERROR_INVALID_HANDLE);
1729 ret = FALSE;
1731 wine_tsx11_unlock();
1733 return ret;
1737 * X11DRV_wglGetCurrentReadDCARB
1739 * For OpenGL32 wglGetCurrentReadDCARB.
1741 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void)
1743 HDC ret = 0;
1744 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
1746 if (ctx) ret = ctx->read_hdc;
1748 TRACE(" returning %p (GL drawable %lu)\n", ret, ctx ? ctx->drawables[1] : 0);
1749 return ret;
1753 * X11DRV_wglGetProcAddress
1755 * For OpenGL32 wglGetProcAddress.
1757 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1759 int i, j;
1760 const WineGLExtension *ext;
1762 int padding = 32 - strlen(lpszProc);
1763 if (padding < 0)
1764 padding = 0;
1766 if (!has_opengl()) return NULL;
1768 /* Check the table of WGL extensions to see if we need to return a WGL extension
1769 * or a function pointer to a native OpenGL function. */
1770 if(strncmp(lpszProc, "wgl", 3) != 0) {
1771 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1772 } else {
1773 TRACE("('%s'):%*s", lpszProc, padding, " ");
1774 for (i = 0; i < WineGLExtensionListSize; ++i) {
1775 ext = WineGLExtensionList[i];
1776 for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1777 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1778 TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1779 return ext->extEntryPoints[j].funcAddress;
1785 WARN("(%s) - not found\n", lpszProc);
1786 return NULL;
1790 * X11DRV_wglMakeCurrent
1792 * For OpenGL32 wglMakeCurrent.
1794 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1795 BOOL ret;
1796 HDC hdc = physDev->hdc;
1797 DWORD type = GetObjectType(hdc);
1798 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1800 TRACE("(%p,%p)\n", hdc, hglrc);
1802 if (!has_opengl()) return FALSE;
1804 wine_tsx11_lock();
1805 if (hglrc == NULL) {
1806 ret = pglXMakeCurrent(gdi_display, None, NULL);
1807 NtCurrentTeb()->glContext = NULL;
1808 } else if (ctx->fmt->iPixelFormat != physDev->current_pf) {
1809 WARN( "mismatched pixel format hdc %p %u ctx %p %u\n",
1810 hdc, physDev->current_pf, ctx, ctx->fmt->iPixelFormat );
1811 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1812 ret = FALSE;
1813 } else {
1814 Drawable drawable = get_glxdrawable(physDev);
1816 /* The describe lines below are for debugging purposes only */
1817 if (TRACE_ON(wgl)) {
1818 describeDrawable(physDev);
1819 describeContext(ctx);
1822 TRACE(" make current for dis %p, drawable %p, ctx %p\n", gdi_display, (void*) drawable, ctx->ctx);
1823 ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1824 NtCurrentTeb()->glContext = ctx;
1826 if(ret)
1828 ctx->has_been_current = TRUE;
1829 ctx->hdc = hdc;
1830 ctx->read_hdc = hdc;
1831 ctx->drawables[0] = drawable;
1832 ctx->drawables[1] = drawable;
1833 ctx->refresh_drawables = FALSE;
1835 if (type == OBJ_MEMDC)
1837 ctx->do_escape = TRUE;
1838 pglDrawBuffer(GL_FRONT_LEFT);
1842 wine_tsx11_unlock();
1843 TRACE(" returning %s\n", (ret ? "True" : "False"));
1844 return ret;
1848 * X11DRV_wglMakeContextCurrentARB
1850 * For OpenGL32 wglMakeContextCurrentARB
1852 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* pDrawDev, X11DRV_PDEVICE* pReadDev, HGLRC hglrc)
1854 BOOL ret;
1856 TRACE("(%p,%p,%p)\n", pDrawDev, pReadDev, hglrc);
1858 if (!has_opengl()) return 0;
1860 wine_tsx11_lock();
1861 if (hglrc == NULL) {
1862 ret = pglXMakeCurrent(gdi_display, None, NULL);
1863 NtCurrentTeb()->glContext = NULL;
1864 } else {
1865 if (NULL == pglXMakeContextCurrent) {
1866 ret = FALSE;
1867 } else {
1868 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1869 Drawable d_draw = get_glxdrawable(pDrawDev);
1870 Drawable d_read = get_glxdrawable(pReadDev);
1872 ctx->has_been_current = TRUE;
1873 ctx->hdc = pDrawDev->hdc;
1874 ctx->read_hdc = pReadDev->hdc;
1875 ctx->drawables[0] = d_draw;
1876 ctx->drawables[1] = d_read;
1877 ctx->refresh_drawables = FALSE;
1878 ret = pglXMakeContextCurrent(gdi_display, d_draw, d_read, ctx->ctx);
1879 NtCurrentTeb()->glContext = ctx;
1882 wine_tsx11_unlock();
1884 TRACE(" returning %s\n", (ret ? "True" : "False"));
1885 return ret;
1889 * X11DRV_wglShareLists
1891 * For OpenGL32 wglShareLists.
1893 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1894 Wine_GLContext *org = (Wine_GLContext *) hglrc1;
1895 Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1897 TRACE("(%p, %p)\n", org, dest);
1899 if (!has_opengl()) return FALSE;
1901 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1902 * at context creation time but in case of WGL it is done using wglShareLists.
1903 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1904 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1905 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1906 * so there delaying context creation doesn't work.
1908 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1909 * and when a program requests sharing we recreate the destination context if it hasn't been made
1910 * current or when it hasn't shared display lists before.
1913 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1915 ERR("Could not share display lists, one of the contexts has been current already !\n");
1916 return FALSE;
1918 else if(dest->sharing)
1920 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1921 return FALSE;
1923 else
1925 if((GetObjectType(org->hdc) == OBJ_MEMDC) ^ (GetObjectType(dest->hdc) == OBJ_MEMDC))
1927 WARN("Attempting to share a context between a direct and indirect rendering context, expect issues!\n");
1930 wine_tsx11_lock();
1931 describeContext(org);
1932 describeContext(dest);
1934 /* Re-create the GLX context and share display lists */
1935 pglXDestroyContext(gdi_display, dest->ctx);
1936 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1937 wine_tsx11_unlock();
1938 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1940 org->sharing = TRUE;
1941 dest->sharing = TRUE;
1942 return TRUE;
1944 return FALSE;
1947 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1949 /* We are running using client-side rendering fonts... */
1950 GLYPHMETRICS gm;
1951 unsigned int glyph, size = 0;
1952 void *bitmap = NULL, *gl_bitmap = NULL;
1953 int org_alignment;
1955 wine_tsx11_lock();
1956 pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1957 pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1958 wine_tsx11_unlock();
1960 for (glyph = first; glyph < first + count; glyph++) {
1961 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1962 unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1963 unsigned int height, width_int;
1965 TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1966 if (needed_size == GDI_ERROR) {
1967 TRACE(" - needed size : %d (GDI_ERROR)\n", needed_size);
1968 goto error;
1969 } else {
1970 TRACE(" - needed size : %d\n", needed_size);
1973 if (needed_size > size) {
1974 size = needed_size;
1975 HeapFree(GetProcessHeap(), 0, bitmap);
1976 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1977 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1978 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1980 if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) == GDI_ERROR)
1981 goto error;
1982 if (TRACE_ON(wgl)) {
1983 unsigned int height, width, bitmask;
1984 unsigned char *bitmap_ = bitmap;
1986 TRACE(" - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1987 TRACE(" - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1988 TRACE(" - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1989 if (needed_size != 0) {
1990 TRACE(" - bitmap :\n");
1991 for (height = 0; height < gm.gmBlackBoxY; height++) {
1992 TRACE(" ");
1993 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1994 if (bitmask == 0) {
1995 bitmap_ += 1;
1996 bitmask = 0x80;
1998 if (*bitmap_ & bitmask)
1999 TRACE("*");
2000 else
2001 TRACE(" ");
2003 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
2004 TRACE("\n");
2009 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
2010 * glyph for it to be drawn properly.
2012 if (needed_size != 0) {
2013 width_int = (gm.gmBlackBoxX + 31) / 32;
2014 for (height = 0; height < gm.gmBlackBoxY; height++) {
2015 unsigned int width;
2016 for (width = 0; width < width_int; width++) {
2017 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
2018 ((int *) bitmap)[height * width_int + width];
2023 wine_tsx11_lock();
2024 pglNewList(listBase++, GL_COMPILE);
2025 if (needed_size != 0) {
2026 pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
2027 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
2028 gm.gmCellIncX, gm.gmCellIncY,
2029 gl_bitmap);
2030 } else {
2031 /* This is the case of 'empty' glyphs like the space character */
2032 pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
2034 pglEndList();
2035 wine_tsx11_unlock();
2038 wine_tsx11_lock();
2039 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2040 wine_tsx11_unlock();
2042 HeapFree(GetProcessHeap(), 0, bitmap);
2043 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2044 return TRUE;
2046 error:
2047 wine_tsx11_lock();
2048 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2049 wine_tsx11_unlock();
2051 HeapFree(GetProcessHeap(), 0, bitmap);
2052 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2053 return FALSE;
2057 * X11DRV_wglUseFontBitmapsA
2059 * For OpenGL32 wglUseFontBitmapsA.
2061 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2063 Font fid = physDev->font;
2065 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2067 if (!has_opengl()) return FALSE;
2069 if (fid == 0) {
2070 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
2073 wine_tsx11_lock();
2074 /* I assume that the glyphs are at the same position for X and for Windows */
2075 pglXUseXFont(fid, first, count, listBase);
2076 wine_tsx11_unlock();
2077 return TRUE;
2081 * X11DRV_wglUseFontBitmapsW
2083 * For OpenGL32 wglUseFontBitmapsW.
2085 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2087 Font fid = physDev->font;
2089 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2091 if (!has_opengl()) return FALSE;
2093 if (fid == 0) {
2094 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
2097 WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
2099 wine_tsx11_lock();
2100 /* I assume that the glyphs are at the same position for X and for Windows */
2101 pglXUseXFont(fid, first, count, listBase);
2102 wine_tsx11_unlock();
2103 return TRUE;
2106 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
2107 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
2109 wine_tsx11_lock();
2110 switch(pname)
2112 case GL_DEPTH_BITS:
2114 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2116 pglGetIntegerv(pname, params);
2118 * if we cannot find a Wine Context
2119 * we only have the default wine desktop context,
2120 * so if we have only a 24 depth say we have 32
2122 if (!ctx && *params == 24) {
2123 *params = 32;
2125 TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
2126 break;
2128 case GL_ALPHA_BITS:
2130 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2132 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_ALPHA_SIZE, params);
2133 TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
2134 break;
2136 default:
2137 pglGetIntegerv(pname, params);
2138 break;
2140 wine_tsx11_unlock();
2143 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
2145 int w, h;
2147 if (!physDev->gl_copy)
2148 return;
2150 w = physDev->dc_rect.right - physDev->dc_rect.left;
2151 h = physDev->dc_rect.bottom - physDev->dc_rect.top;
2153 if(w > 0 && h > 0) {
2154 Drawable src = physDev->pixmap;
2155 if(!src) src = physDev->gl_drawable;
2157 /* The GL drawable may be lagged behind if we don't flush first, so
2158 * flush the display make sure we copy up-to-date data */
2159 wine_tsx11_lock();
2160 XFlush(gdi_display);
2161 XSetFunction(gdi_display, physDev->gc, GXcopy);
2162 XCopyArea(gdi_display, src, physDev->drawable, physDev->gc, 0, 0, w, h,
2163 physDev->dc_rect.left, physDev->dc_rect.top);
2164 wine_tsx11_unlock();
2169 static void WINAPI X11DRV_wglFinish(void)
2171 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2172 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2174 wine_tsx11_lock();
2175 sync_context(ctx);
2176 pglFinish();
2177 wine_tsx11_unlock();
2178 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2181 static void WINAPI X11DRV_wglFlush(void)
2183 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2184 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2186 wine_tsx11_lock();
2187 sync_context(ctx);
2188 pglFlush();
2189 wine_tsx11_unlock();
2190 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2194 * X11DRV_wglCreateContextAttribsARB
2196 * WGL_ARB_create_context: wglCreateContextAttribsARB
2198 HGLRC X11DRV_wglCreateContextAttribsARB(X11DRV_PDEVICE *physDev, HGLRC hShareContext, const int* attribList)
2200 Wine_GLContext *ret;
2201 WineGLPixelFormat *fmt;
2202 int hdcPF = physDev->current_pf;
2203 int fmt_count = 0;
2205 TRACE("(%p %p %p)\n", physDev, hShareContext, attribList);
2207 if (!has_opengl()) return 0;
2209 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
2210 /* wglCreateContextAttribsARB supports ALL pixel formats, so also offscreen ones.
2211 * If this fails something is very wrong on the system. */
2212 if(!fmt)
2214 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
2215 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2216 return NULL;
2219 wine_tsx11_lock();
2220 ret = alloc_context();
2221 wine_tsx11_unlock();
2222 ret->hdc = physDev->hdc;
2223 ret->fmt = fmt;
2224 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2225 ret->gl3_context = TRUE;
2227 ret->numAttribs = 0;
2228 if(attribList)
2230 int *pAttribList = (int*)attribList;
2231 int *pContextAttribList = &ret->attribList[0];
2232 /* attribList consists of pairs {token, value] terminated with 0 */
2233 while(pAttribList[0] != 0)
2235 TRACE("%#x %#x\n", pAttribList[0], pAttribList[1]);
2236 switch(pAttribList[0])
2238 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2239 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2240 pContextAttribList[1] = pAttribList[1];
2241 break;
2242 case WGL_CONTEXT_MINOR_VERSION_ARB:
2243 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2244 pContextAttribList[1] = pAttribList[1];
2245 break;
2246 case WGL_CONTEXT_LAYER_PLANE_ARB:
2247 break;
2248 case WGL_CONTEXT_FLAGS_ARB:
2249 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2250 pContextAttribList[1] = pAttribList[1];
2251 break;
2252 case WGL_CONTEXT_PROFILE_MASK_ARB:
2253 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2254 pContextAttribList[1] = pAttribList[1];
2255 break;
2256 default:
2257 ERR("Unhandled attribList pair: %#x %#x\n", pAttribList[0], pAttribList[1]);
2260 ret->numAttribs++;
2261 pAttribList += 2;
2262 pContextAttribList += 2;
2266 wine_tsx11_lock();
2267 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2268 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
2270 XSync(gdi_display, False);
2271 if(X11DRV_check_error() || !ret->ctx)
2273 /* In the future we should convert the GLX error to a win32 one here if needed */
2274 ERR("Context creation failed\n");
2275 free_context(ret);
2276 wine_tsx11_unlock();
2277 return NULL;
2280 wine_tsx11_unlock();
2281 TRACE(" creating context %p\n", ret);
2282 return (HGLRC) ret;
2286 * X11DRV_wglGetExtensionsStringARB
2288 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2290 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
2291 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2292 return WineGLInfo.wglExtensions;
2296 * X11DRV_wglCreatePbufferARB
2298 * WGL_ARB_pbuffer: wglCreatePbufferARB
2300 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
2302 Wine_GLPBuffer* object = NULL;
2303 WineGLPixelFormat *fmt = NULL;
2304 int nCfgs = 0;
2305 int attribs[256];
2306 int nAttribs = 0;
2308 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2310 if (0 >= iPixelFormat) {
2311 ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2312 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2313 return NULL; /* unexpected error */
2316 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2317 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2318 if(!fmt) {
2319 ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2320 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2321 goto create_failed; /* unexpected error */
2324 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2325 if (NULL == object) {
2326 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2327 goto create_failed; /* unexpected error */
2329 object->hdc = hdc;
2330 object->display = gdi_display;
2331 object->width = iWidth;
2332 object->height = iHeight;
2333 object->fmt = fmt;
2335 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2336 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2337 while (piAttribList && 0 != *piAttribList) {
2338 int attr_v;
2339 switch (*piAttribList) {
2340 case WGL_PBUFFER_LARGEST_ARB: {
2341 ++piAttribList;
2342 attr_v = *piAttribList;
2343 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2344 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2345 break;
2348 case WGL_TEXTURE_FORMAT_ARB: {
2349 ++piAttribList;
2350 attr_v = *piAttribList;
2351 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2352 if (use_render_texture_ati) {
2353 int type = 0;
2354 switch (attr_v) {
2355 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2356 case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2357 case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2358 default:
2359 SetLastError(ERROR_INVALID_DATA);
2360 goto create_failed;
2362 object->use_render_texture = 1;
2363 PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2364 } else {
2365 if (WGL_NO_TEXTURE_ARB == attr_v) {
2366 object->use_render_texture = 0;
2367 } else {
2368 if (!use_render_texture_emulation) {
2369 SetLastError(ERROR_INVALID_DATA);
2370 goto create_failed;
2372 switch (attr_v) {
2373 case WGL_TEXTURE_RGB_ARB:
2374 object->use_render_texture = GL_RGB;
2375 object->texture_bpp = 3;
2376 object->texture_format = GL_RGB;
2377 object->texture_type = GL_UNSIGNED_BYTE;
2378 break;
2379 case WGL_TEXTURE_RGBA_ARB:
2380 object->use_render_texture = GL_RGBA;
2381 object->texture_bpp = 4;
2382 object->texture_format = GL_RGBA;
2383 object->texture_type = GL_UNSIGNED_BYTE;
2384 break;
2386 /* WGL_FLOAT_COMPONENTS_NV */
2387 case WGL_TEXTURE_FLOAT_R_NV:
2388 object->use_render_texture = GL_FLOAT_R_NV;
2389 object->texture_bpp = 4;
2390 object->texture_format = GL_RED;
2391 object->texture_type = GL_FLOAT;
2392 break;
2393 case WGL_TEXTURE_FLOAT_RG_NV:
2394 object->use_render_texture = GL_FLOAT_RG_NV;
2395 object->texture_bpp = 8;
2396 object->texture_format = GL_LUMINANCE_ALPHA;
2397 object->texture_type = GL_FLOAT;
2398 break;
2399 case WGL_TEXTURE_FLOAT_RGB_NV:
2400 object->use_render_texture = GL_FLOAT_RGB_NV;
2401 object->texture_bpp = 12;
2402 object->texture_format = GL_RGB;
2403 object->texture_type = GL_FLOAT;
2404 break;
2405 case WGL_TEXTURE_FLOAT_RGBA_NV:
2406 object->use_render_texture = GL_FLOAT_RGBA_NV;
2407 object->texture_bpp = 16;
2408 object->texture_format = GL_RGBA;
2409 object->texture_type = GL_FLOAT;
2410 break;
2411 default:
2412 ERR("Unknown texture format: %x\n", attr_v);
2413 SetLastError(ERROR_INVALID_DATA);
2414 goto create_failed;
2418 break;
2421 case WGL_TEXTURE_TARGET_ARB: {
2422 ++piAttribList;
2423 attr_v = *piAttribList;
2424 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2425 if (use_render_texture_ati) {
2426 int type = 0;
2427 switch (attr_v) {
2428 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2429 case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2430 case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2431 case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2432 default:
2433 SetLastError(ERROR_INVALID_DATA);
2434 goto create_failed;
2436 PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2437 } else {
2438 if (WGL_NO_TEXTURE_ARB == attr_v) {
2439 object->texture_target = 0;
2440 } else {
2441 if (!use_render_texture_emulation) {
2442 SetLastError(ERROR_INVALID_DATA);
2443 goto create_failed;
2445 switch (attr_v) {
2446 case WGL_TEXTURE_CUBE_MAP_ARB: {
2447 if (iWidth != iHeight) {
2448 SetLastError(ERROR_INVALID_DATA);
2449 goto create_failed;
2451 object->texture_target = GL_TEXTURE_CUBE_MAP;
2452 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2453 break;
2455 case WGL_TEXTURE_1D_ARB: {
2456 if (1 != iHeight) {
2457 SetLastError(ERROR_INVALID_DATA);
2458 goto create_failed;
2460 object->texture_target = GL_TEXTURE_1D;
2461 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2462 break;
2464 case WGL_TEXTURE_2D_ARB: {
2465 object->texture_target = GL_TEXTURE_2D;
2466 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2467 break;
2469 case WGL_TEXTURE_RECTANGLE_NV: {
2470 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2471 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2472 break;
2474 default:
2475 ERR("Unknown texture target: %x\n", attr_v);
2476 SetLastError(ERROR_INVALID_DATA);
2477 goto create_failed;
2481 break;
2484 case WGL_MIPMAP_TEXTURE_ARB: {
2485 ++piAttribList;
2486 attr_v = *piAttribList;
2487 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2488 if (use_render_texture_ati) {
2489 PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2490 } else {
2491 if (!use_render_texture_emulation) {
2492 SetLastError(ERROR_INVALID_DATA);
2493 goto create_failed;
2496 break;
2499 ++piAttribList;
2502 PUSH1(attribs, None);
2503 wine_tsx11_lock();
2504 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2505 wine_tsx11_unlock();
2506 TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
2507 if (!object->drawable) {
2508 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2509 goto create_failed; /* unexpected error */
2511 TRACE("->(%p)\n", object);
2512 return object;
2514 create_failed:
2515 HeapFree(GetProcessHeap(), 0, object);
2516 TRACE("->(FAILED)\n");
2517 return NULL;
2521 * X11DRV_wglDestroyPbufferARB
2523 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2525 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2527 Wine_GLPBuffer* object = hPbuffer;
2528 TRACE("(%p)\n", hPbuffer);
2529 if (NULL == object) {
2530 SetLastError(ERROR_INVALID_HANDLE);
2531 return GL_FALSE;
2533 wine_tsx11_lock();
2534 pglXDestroyPbuffer(object->display, object->drawable);
2535 wine_tsx11_unlock();
2536 HeapFree(GetProcessHeap(), 0, object);
2537 return GL_TRUE;
2541 * X11DRV_wglGetPbufferDCARB
2543 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2544 * The function wglGetPbufferDCARB returns a device context for a pbuffer.
2545 * Gdi32 implements the part of this function which creates a device context.
2546 * This part associates the physDev with the X drawable of the pbuffer.
2548 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
2550 Wine_GLPBuffer* object = hPbuffer;
2551 if (NULL == object) {
2552 SetLastError(ERROR_INVALID_HANDLE);
2553 return NULL;
2556 /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
2557 * All formats in our pixelformat list are compatible with each other and the main drawable. */
2558 physDev->current_pf = object->fmt->iPixelFormat;
2559 physDev->drawable = object->drawable;
2560 SetRect( &physDev->drawable_rect, 0, 0, object->width, object->height );
2561 physDev->dc_rect = physDev->drawable_rect;
2563 TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
2564 return physDev->hdc;
2568 * X11DRV_wglQueryPbufferARB
2570 * WGL_ARB_pbuffer: wglQueryPbufferARB
2572 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2574 Wine_GLPBuffer* object = hPbuffer;
2575 TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2576 if (NULL == object) {
2577 SetLastError(ERROR_INVALID_HANDLE);
2578 return GL_FALSE;
2580 switch (iAttribute) {
2581 case WGL_PBUFFER_WIDTH_ARB:
2582 wine_tsx11_lock();
2583 pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2584 wine_tsx11_unlock();
2585 break;
2586 case WGL_PBUFFER_HEIGHT_ARB:
2587 wine_tsx11_lock();
2588 pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2589 wine_tsx11_unlock();
2590 break;
2592 case WGL_PBUFFER_LOST_ARB:
2593 /* GLX Pbuffers cannot be lost by default. We can support this by
2594 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2595 * to receive pixel buffer clobber events, however that may or may
2596 * not give any benefit */
2597 *piValue = GL_FALSE;
2598 break;
2600 case WGL_TEXTURE_FORMAT_ARB:
2601 if (use_render_texture_ati) {
2602 unsigned int tmp;
2603 int type = WGL_NO_TEXTURE_ARB;
2604 wine_tsx11_lock();
2605 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2606 wine_tsx11_unlock();
2607 switch (tmp) {
2608 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2609 case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2610 case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2612 *piValue = type;
2613 } else {
2614 if (!object->use_render_texture) {
2615 *piValue = WGL_NO_TEXTURE_ARB;
2616 } else {
2617 if (!use_render_texture_emulation) {
2618 SetLastError(ERROR_INVALID_HANDLE);
2619 return GL_FALSE;
2621 switch(object->use_render_texture) {
2622 case GL_RGB:
2623 *piValue = WGL_TEXTURE_RGB_ARB;
2624 break;
2625 case GL_RGBA:
2626 *piValue = WGL_TEXTURE_RGBA_ARB;
2627 break;
2628 /* WGL_FLOAT_COMPONENTS_NV */
2629 case GL_FLOAT_R_NV:
2630 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2631 break;
2632 case GL_FLOAT_RG_NV:
2633 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2634 break;
2635 case GL_FLOAT_RGB_NV:
2636 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2637 break;
2638 case GL_FLOAT_RGBA_NV:
2639 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2640 break;
2641 default:
2642 ERR("Unknown texture format: %x\n", object->use_render_texture);
2646 break;
2648 case WGL_TEXTURE_TARGET_ARB:
2649 if (use_render_texture_ati) {
2650 unsigned int tmp;
2651 int type = WGL_NO_TEXTURE_ARB;
2652 wine_tsx11_lock();
2653 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2654 wine_tsx11_unlock();
2655 switch (tmp) {
2656 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2657 case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2658 case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2659 case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2661 *piValue = type;
2662 } else {
2663 if (!object->texture_target) {
2664 *piValue = WGL_NO_TEXTURE_ARB;
2665 } else {
2666 if (!use_render_texture_emulation) {
2667 SetLastError(ERROR_INVALID_DATA);
2668 return GL_FALSE;
2670 switch (object->texture_target) {
2671 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2672 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2673 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2674 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2678 break;
2680 case WGL_MIPMAP_TEXTURE_ARB:
2681 if (use_render_texture_ati) {
2682 wine_tsx11_lock();
2683 pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2684 wine_tsx11_unlock();
2685 } else {
2686 *piValue = GL_FALSE; /** don't support that */
2687 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2689 break;
2691 default:
2692 FIXME("unexpected attribute %x\n", iAttribute);
2693 break;
2696 return GL_TRUE;
2700 * X11DRV_wglReleasePbufferDCARB
2702 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2704 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2706 TRACE("(%p, %p)\n", hPbuffer, hdc);
2707 return DeleteDC(hdc);
2711 * X11DRV_wglSetPbufferAttribARB
2713 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2715 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2717 Wine_GLPBuffer* object = hPbuffer;
2718 GLboolean ret = GL_FALSE;
2720 WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2721 if (NULL == object) {
2722 SetLastError(ERROR_INVALID_HANDLE);
2723 return GL_FALSE;
2725 if (!object->use_render_texture) {
2726 SetLastError(ERROR_INVALID_HANDLE);
2727 return GL_FALSE;
2729 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2730 return GL_TRUE;
2732 if (NULL != pglXDrawableAttribATI) {
2733 if (use_render_texture_ati) {
2734 FIXME("Need conversion for GLX_ATI_render_texture\n");
2736 wine_tsx11_lock();
2737 ret = pglXDrawableAttribATI(object->display, object->drawable, piAttribList);
2738 wine_tsx11_unlock();
2740 return ret;
2744 * X11DRV_wglChoosePixelFormatARB
2746 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2748 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2750 int gl_test = 0;
2751 int attribs[256];
2752 int nAttribs = 0;
2753 GLXFBConfig* cfgs = NULL;
2754 int nCfgs = 0;
2755 int it;
2756 int fmt_id;
2757 WineGLPixelFormat *fmt;
2758 UINT pfmt_it = 0;
2759 int run;
2760 int i;
2761 DWORD dwFlags = 0;
2763 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2764 if (NULL != pfAttribFList) {
2765 FIXME("unused pfAttribFList\n");
2768 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2769 if (-1 == nAttribs) {
2770 WARN("Cannot convert WGL to GLX attributes\n");
2771 return GL_FALSE;
2773 PUSH1(attribs, None);
2775 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2776 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the WineGLPixelFormat. We don't query these attributes
2777 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on by passing a dwFlags to 'ConvertPixelFormatGLXtoWGL'. */
2778 for(i=0; piAttribIList[i] != 0; i+=2)
2780 switch(piAttribIList[i])
2782 case WGL_DRAW_TO_BITMAP_ARB:
2783 if(piAttribIList[i+1])
2784 dwFlags |= PFD_DRAW_TO_BITMAP;
2785 break;
2786 case WGL_ACCELERATION_ARB:
2787 switch(piAttribIList[i+1])
2789 case WGL_NO_ACCELERATION_ARB:
2790 dwFlags |= PFD_GENERIC_FORMAT;
2791 break;
2792 case WGL_GENERIC_ACCELERATION_ARB:
2793 dwFlags |= PFD_GENERIC_ACCELERATED;
2794 break;
2795 case WGL_FULL_ACCELERATION_ARB:
2796 /* Nothing to do */
2797 break;
2799 break;
2800 case WGL_SUPPORT_GDI_ARB:
2801 if(piAttribIList[i+1])
2802 dwFlags |= PFD_SUPPORT_GDI;
2803 break;
2807 /* Search for FB configurations matching the requirements in attribs */
2808 wine_tsx11_lock();
2809 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2810 if (NULL == cfgs) {
2811 wine_tsx11_unlock();
2812 WARN("Compatible Pixel Format not found\n");
2813 return GL_FALSE;
2816 /* Loop through all matching formats and check if they are suitable.
2817 * Note that this function should at max return nMaxFormats different formats */
2818 for(run=0; run < 2; run++)
2820 for (it = 0; it < nCfgs; ++it) {
2821 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2822 if (gl_test) {
2823 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2824 continue;
2827 /* Search for the format in our list of compatible formats */
2828 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id, dwFlags);
2829 if(!fmt)
2830 continue;
2832 /* During the first run we only want onscreen formats and during the second only offscreen 'XOR' */
2833 if( ((run == 0) && fmt->offscreenOnly) || ((run == 1) && !fmt->offscreenOnly) )
2834 continue;
2836 if(pfmt_it < nMaxFormats) {
2837 piFormats[pfmt_it] = fmt->iPixelFormat;
2838 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2840 pfmt_it++;
2844 *nNumFormats = pfmt_it;
2845 /** free list */
2846 XFree(cfgs);
2847 wine_tsx11_unlock();
2848 return GL_TRUE;
2852 * X11DRV_wglGetPixelFormatAttribivARB
2854 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2856 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2858 UINT i;
2859 WineGLPixelFormat *fmt = NULL;
2860 int hTest;
2861 int tmp;
2862 int curGLXAttr = 0;
2863 int nWGLFormats = 0;
2865 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2867 if (0 < iLayerPlane) {
2868 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2869 return GL_FALSE;
2872 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2873 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2874 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2875 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2876 if(!fmt) {
2877 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2880 wine_tsx11_lock();
2881 for (i = 0; i < nAttributes; ++i) {
2882 const int curWGLAttr = piAttributes[i];
2883 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2885 switch (curWGLAttr) {
2886 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2887 piValues[i] = nWGLFormats;
2888 continue;
2890 case WGL_SUPPORT_OPENGL_ARB:
2891 piValues[i] = GL_TRUE;
2892 continue;
2894 case WGL_ACCELERATION_ARB:
2895 curGLXAttr = GLX_CONFIG_CAVEAT;
2896 if (!fmt) goto pix_error;
2897 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2898 piValues[i] = WGL_NO_ACCELERATION_ARB;
2899 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2900 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2901 else
2902 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2903 continue;
2905 case WGL_TRANSPARENT_ARB:
2906 curGLXAttr = GLX_TRANSPARENT_TYPE;
2907 if (!fmt) goto pix_error;
2908 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2909 if (hTest) goto get_error;
2910 piValues[i] = GL_FALSE;
2911 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2912 continue;
2914 case WGL_PIXEL_TYPE_ARB:
2915 curGLXAttr = GLX_RENDER_TYPE;
2916 if (!fmt) goto pix_error;
2917 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2918 if (hTest) goto get_error;
2919 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2920 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2921 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2922 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2923 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2924 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2925 else {
2926 ERR("unexpected RenderType(%x)\n", tmp);
2927 piValues[i] = WGL_TYPE_RGBA_ARB;
2929 continue;
2931 case WGL_COLOR_BITS_ARB:
2932 curGLXAttr = GLX_BUFFER_SIZE;
2933 break;
2935 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2936 if (use_render_texture_ati) {
2937 curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2938 break;
2940 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2941 if (use_render_texture_ati) {
2942 curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2943 break;
2945 if (!use_render_texture_emulation) {
2946 piValues[i] = GL_FALSE;
2947 continue;
2949 curGLXAttr = GLX_RENDER_TYPE;
2950 if (!fmt) goto pix_error;
2951 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2952 if (hTest) goto get_error;
2953 if (GLX_COLOR_INDEX_BIT == tmp) {
2954 piValues[i] = GL_FALSE;
2955 continue;
2957 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2958 if (hTest) goto get_error;
2959 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2960 continue;
2962 case WGL_BLUE_BITS_ARB:
2963 curGLXAttr = GLX_BLUE_SIZE;
2964 break;
2965 case WGL_RED_BITS_ARB:
2966 curGLXAttr = GLX_RED_SIZE;
2967 break;
2968 case WGL_GREEN_BITS_ARB:
2969 curGLXAttr = GLX_GREEN_SIZE;
2970 break;
2971 case WGL_ALPHA_BITS_ARB:
2972 curGLXAttr = GLX_ALPHA_SIZE;
2973 break;
2974 case WGL_DEPTH_BITS_ARB:
2975 curGLXAttr = GLX_DEPTH_SIZE;
2976 break;
2977 case WGL_STENCIL_BITS_ARB:
2978 curGLXAttr = GLX_STENCIL_SIZE;
2979 break;
2980 case WGL_DOUBLE_BUFFER_ARB:
2981 curGLXAttr = GLX_DOUBLEBUFFER;
2982 break;
2983 case WGL_STEREO_ARB:
2984 curGLXAttr = GLX_STEREO;
2985 break;
2986 case WGL_AUX_BUFFERS_ARB:
2987 curGLXAttr = GLX_AUX_BUFFERS;
2988 break;
2990 case WGL_SUPPORT_GDI_ARB:
2991 if (!fmt) goto pix_error;
2992 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) ? TRUE : FALSE;
2993 continue;
2995 case WGL_DRAW_TO_BITMAP_ARB:
2996 if (!fmt) goto pix_error;
2997 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? TRUE : FALSE;
2998 continue;
3000 case WGL_DRAW_TO_WINDOW_ARB:
3001 case WGL_DRAW_TO_PBUFFER_ARB:
3002 if (!fmt) goto pix_error;
3003 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
3004 if (hTest) goto get_error;
3005 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
3006 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
3007 piValues[i] = GL_TRUE;
3008 else
3009 piValues[i] = GL_FALSE;
3010 continue;
3012 case WGL_SWAP_METHOD_ARB:
3013 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
3014 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
3015 * point only ATI offers this.
3017 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
3018 break;
3020 case WGL_PBUFFER_LARGEST_ARB:
3021 curGLXAttr = GLX_LARGEST_PBUFFER;
3022 break;
3024 case WGL_SAMPLE_BUFFERS_ARB:
3025 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
3026 break;
3028 case WGL_SAMPLES_ARB:
3029 curGLXAttr = GLX_SAMPLES_ARB;
3030 break;
3032 case WGL_FLOAT_COMPONENTS_NV:
3033 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
3034 break;
3036 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
3037 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
3038 break;
3040 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
3041 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
3042 break;
3044 case WGL_ACCUM_RED_BITS_ARB:
3045 curGLXAttr = GLX_ACCUM_RED_SIZE;
3046 break;
3047 case WGL_ACCUM_GREEN_BITS_ARB:
3048 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
3049 break;
3050 case WGL_ACCUM_BLUE_BITS_ARB:
3051 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
3052 break;
3053 case WGL_ACCUM_ALPHA_BITS_ARB:
3054 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
3055 break;
3056 case WGL_ACCUM_BITS_ARB:
3057 if (!fmt) goto pix_error;
3058 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
3059 if (hTest) goto get_error;
3060 piValues[i] = tmp;
3061 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
3062 if (hTest) goto get_error;
3063 piValues[i] += tmp;
3064 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
3065 if (hTest) goto get_error;
3066 piValues[i] += tmp;
3067 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
3068 if (hTest) goto get_error;
3069 piValues[i] += tmp;
3070 continue;
3072 default:
3073 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
3076 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
3077 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
3078 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
3080 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
3081 * and check which options can work using iPixelFormat=0 and which not.
3082 * A problem would be that this function is an extension. This would
3083 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
3085 if (0 != curGLXAttr && iPixelFormat != 0) {
3086 if (!fmt) goto pix_error;
3087 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
3088 if (hTest) goto get_error;
3089 curGLXAttr = 0;
3090 } else {
3091 piValues[i] = GL_FALSE;
3094 wine_tsx11_unlock();
3095 return GL_TRUE;
3097 get_error:
3098 wine_tsx11_unlock();
3099 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
3100 return GL_FALSE;
3102 pix_error:
3103 wine_tsx11_unlock();
3104 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
3105 return GL_FALSE;
3109 * X11DRV_wglGetPixelFormatAttribfvARB
3111 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
3113 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
3115 int *attr;
3116 int ret;
3117 UINT i;
3119 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
3121 /* Allocate a temporary array to store integer values */
3122 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
3123 if (!attr) {
3124 ERR("couldn't allocate %d array\n", nAttributes);
3125 return GL_FALSE;
3128 /* Piggy-back on wglGetPixelFormatAttribivARB */
3129 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
3130 if (ret) {
3131 /* Convert integer values to float. Should also check for attributes
3132 that can give decimal values here */
3133 for (i=0; i<nAttributes;i++) {
3134 pfValues[i] = attr[i];
3138 HeapFree(GetProcessHeap(), 0, attr);
3139 return ret;
3143 * X11DRV_wglBindTexImageARB
3145 * WGL_ARB_render_texture: wglBindTexImageARB
3147 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3149 Wine_GLPBuffer* object = hPbuffer;
3150 GLboolean ret = GL_FALSE;
3152 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3153 if (NULL == object) {
3154 SetLastError(ERROR_INVALID_HANDLE);
3155 return GL_FALSE;
3157 if (!object->use_render_texture) {
3158 SetLastError(ERROR_INVALID_HANDLE);
3159 return GL_FALSE;
3162 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3163 static int init = 0;
3164 int prev_binded_texture = 0;
3165 GLXContext prev_context;
3166 Drawable prev_drawable;
3167 GLXContext tmp_context;
3169 wine_tsx11_lock();
3170 prev_context = pglXGetCurrentContext();
3171 prev_drawable = pglXGetCurrentDrawable();
3173 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
3174 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
3175 isn't ideal performance wise but I wasn't able to get other ways working.
3177 if(!init) {
3178 init = 1; /* Only show the FIXME once for performance reasons */
3179 FIXME("partial stub!\n");
3182 TRACE("drawable=%p, context=%p\n", (void*)object->drawable, prev_context);
3183 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3185 pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3187 /* Switch to our pbuffer */
3188 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
3190 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3191 * After that upload the pbuffer texture data. */
3192 pglBindTexture(object->texture_target, prev_binded_texture);
3193 pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3195 /* Switch back to the original drawable and upload the pbuffer-texture */
3196 pglXMakeCurrent(object->display, prev_drawable, prev_context);
3197 pglXDestroyContext(gdi_display, tmp_context);
3198 wine_tsx11_unlock();
3199 return GL_TRUE;
3202 if (NULL != pglXBindTexImageATI) {
3203 int buffer;
3205 switch(iBuffer)
3207 case WGL_FRONT_LEFT_ARB:
3208 buffer = GLX_FRONT_LEFT_ATI;
3209 break;
3210 case WGL_FRONT_RIGHT_ARB:
3211 buffer = GLX_FRONT_RIGHT_ATI;
3212 break;
3213 case WGL_BACK_LEFT_ARB:
3214 buffer = GLX_BACK_LEFT_ATI;
3215 break;
3216 case WGL_BACK_RIGHT_ARB:
3217 buffer = GLX_BACK_RIGHT_ATI;
3218 break;
3219 default:
3220 ERR("Unknown iBuffer=%#x\n", iBuffer);
3221 return FALSE;
3224 /* In the sample 'ogl_offscreen_rendering_3' from codesampler.net I get garbage on the screen.
3225 * I'm not sure if that's a bug in the ATI extension or in the program. I think that the program
3226 * expected a single buffering format since it didn't ask for double buffering. A buffer swap
3227 * fixed the program. I don't know what the correct behavior is. On the other hand that demo
3228 * works fine using our pbuffer emulation path.
3230 wine_tsx11_lock();
3231 ret = pglXBindTexImageATI(object->display, object->drawable, buffer);
3232 wine_tsx11_unlock();
3234 return ret;
3238 * X11DRV_wglReleaseTexImageARB
3240 * WGL_ARB_render_texture: wglReleaseTexImageARB
3242 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3244 Wine_GLPBuffer* object = hPbuffer;
3245 GLboolean ret = GL_FALSE;
3247 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3248 if (NULL == object) {
3249 SetLastError(ERROR_INVALID_HANDLE);
3250 return GL_FALSE;
3252 if (!object->use_render_texture) {
3253 SetLastError(ERROR_INVALID_HANDLE);
3254 return GL_FALSE;
3256 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3257 return GL_TRUE;
3259 if (NULL != pglXReleaseTexImageATI) {
3260 int buffer;
3262 switch(iBuffer)
3264 case WGL_FRONT_LEFT_ARB:
3265 buffer = GLX_FRONT_LEFT_ATI;
3266 break;
3267 case WGL_FRONT_RIGHT_ARB:
3268 buffer = GLX_FRONT_RIGHT_ATI;
3269 break;
3270 case WGL_BACK_LEFT_ARB:
3271 buffer = GLX_BACK_LEFT_ATI;
3272 break;
3273 case WGL_BACK_RIGHT_ARB:
3274 buffer = GLX_BACK_RIGHT_ATI;
3275 break;
3276 default:
3277 ERR("Unknown iBuffer=%#x\n", iBuffer);
3278 return FALSE;
3280 wine_tsx11_lock();
3281 ret = pglXReleaseTexImageATI(object->display, object->drawable, buffer);
3282 wine_tsx11_unlock();
3284 return ret;
3288 * X11DRV_wglGetExtensionsStringEXT
3290 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3292 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
3293 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3294 return WineGLInfo.wglExtensions;
3298 * X11DRV_wglGetSwapIntervalEXT
3300 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3302 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
3303 FIXME("(),stub!\n");
3304 return swap_interval;
3308 * X11DRV_wglSwapIntervalEXT
3310 * WGL_EXT_swap_control: wglSwapIntervalEXT
3312 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
3313 BOOL ret = TRUE;
3315 TRACE("(%d)\n", interval);
3316 swap_interval = interval;
3317 if (NULL != pglXSwapIntervalSGI) {
3318 wine_tsx11_lock();
3319 ret = !pglXSwapIntervalSGI(interval);
3320 wine_tsx11_unlock();
3322 else WARN("(): GLX_SGI_swap_control extension seems not supported\n");
3323 return ret;
3327 * X11DRV_wglAllocateMemoryNV
3329 * WGL_NV_vertex_array_range: wglAllocateMemoryNV
3331 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
3332 void *ret = NULL;
3333 TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
3335 if (pglXAllocateMemoryNV)
3337 wine_tsx11_lock();
3338 ret = pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
3339 wine_tsx11_unlock();
3341 return ret;
3345 * X11DRV_wglFreeMemoryNV
3347 * WGL_NV_vertex_array_range: wglFreeMemoryNV
3349 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
3350 TRACE("(%p)\n", pointer);
3351 if (pglXFreeMemoryNV == NULL)
3352 return;
3354 wine_tsx11_lock();
3355 pglXFreeMemoryNV(pointer);
3356 wine_tsx11_unlock();
3360 * X11DRV_wglSetPixelFormatWINE
3362 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3363 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3365 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3367 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
3369 if (!has_opengl()) return FALSE;
3371 if (physDev->current_pf == iPixelFormat) return TRUE;
3373 /* Relay to the core SetPixelFormat */
3374 TRACE("Changing iPixelFormat from %d to %d\n", physDev->current_pf, iPixelFormat);
3375 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
3379 * glxRequireVersion (internal)
3381 * Check if the supported GLX version matches requiredVersion.
3383 static BOOL glxRequireVersion(int requiredVersion)
3385 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3386 if(requiredVersion <= WineGLInfo.glxVersion[1])
3387 return TRUE;
3389 return FALSE;
3392 static BOOL glxRequireExtension(const char *requiredExtension)
3394 if (strstr(WineGLInfo.glxExtensions, requiredExtension) == NULL) {
3395 return FALSE;
3398 return TRUE;
3401 static void register_extension_string(const char *ext)
3403 if (WineGLInfo.wglExtensions[0])
3404 strcat(WineGLInfo.wglExtensions, " ");
3405 strcat(WineGLInfo.wglExtensions, ext);
3407 TRACE("'%s'\n", ext);
3410 static BOOL register_extension(const WineGLExtension * ext)
3412 int i;
3414 assert( WineGLExtensionListSize < MAX_EXTENSIONS );
3415 WineGLExtensionList[WineGLExtensionListSize++] = ext;
3417 register_extension_string(ext->extName);
3419 for (i = 0; ext->extEntryPoints[i].funcName; ++i)
3420 TRACE(" - '%s'\n", ext->extEntryPoints[i].funcName);
3422 return TRUE;
3425 static const WineGLExtension WGL_internal_functions =
3429 { "wglGetIntegerv", X11DRV_wglGetIntegerv },
3430 { "wglFinish", X11DRV_wglFinish },
3431 { "wglFlush", X11DRV_wglFlush },
3436 static const WineGLExtension WGL_ARB_create_context =
3438 "WGL_ARB_create_context",
3440 { "wglCreateContextAttribsARB", X11DRV_wglCreateContextAttribsARB },
3444 static const WineGLExtension WGL_ARB_extensions_string =
3446 "WGL_ARB_extensions_string",
3448 { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
3452 static const WineGLExtension WGL_ARB_make_current_read =
3454 "WGL_ARB_make_current_read",
3456 { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
3457 { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
3461 static const WineGLExtension WGL_ARB_multisample =
3463 "WGL_ARB_multisample",
3466 static const WineGLExtension WGL_ARB_pbuffer =
3468 "WGL_ARB_pbuffer",
3470 { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
3471 { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
3472 { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
3473 { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
3474 { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
3475 { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
3479 static const WineGLExtension WGL_ARB_pixel_format =
3481 "WGL_ARB_pixel_format",
3483 { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3484 { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3485 { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3489 static const WineGLExtension WGL_ARB_render_texture =
3491 "WGL_ARB_render_texture",
3493 { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3494 { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3498 static const WineGLExtension WGL_EXT_extensions_string =
3500 "WGL_EXT_extensions_string",
3502 { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3506 static const WineGLExtension WGL_EXT_swap_control =
3508 "WGL_EXT_swap_control",
3510 { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3511 { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3515 static const WineGLExtension WGL_NV_vertex_array_range =
3517 "WGL_NV_vertex_array_range",
3519 { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3520 { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3524 static const WineGLExtension WGL_WINE_pixel_format_passthrough =
3526 "WGL_WINE_pixel_format_passthrough",
3528 { "wglSetPixelFormatWINE", X11DRV_wglSetPixelFormatWINE },
3533 * X11DRV_WineGL_LoadExtensions
3535 static void X11DRV_WineGL_LoadExtensions(void)
3537 WineGLInfo.wglExtensions[0] = 0;
3539 /* Load Wine internal functions */
3540 register_extension(&WGL_internal_functions);
3542 /* ARB Extensions */
3544 if(glxRequireExtension("GLX_ARB_create_context"))
3546 register_extension(&WGL_ARB_create_context);
3548 if(glxRequireExtension("GLX_ARB_create_context_profile"))
3549 register_extension_string("WGL_ARB_create_context_profile");
3552 if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3554 register_extension_string("WGL_ARB_pixel_format_float");
3555 register_extension_string("WGL_ATI_pixel_format_float");
3558 register_extension(&WGL_ARB_extensions_string);
3560 if (glxRequireVersion(3))
3561 register_extension(&WGL_ARB_make_current_read);
3563 if (glxRequireExtension("GLX_ARB_multisample"))
3564 register_extension(&WGL_ARB_multisample);
3566 /* In general pbuffer functionality requires support in the X-server. The functionality is
3567 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3568 * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3569 * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3571 * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3572 * without support in the X-server (which other Mesa based drivers require).
3574 * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3575 * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3576 * is available pbuffers must be available too. */
3577 if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3578 register_extension(&WGL_ARB_pbuffer);
3580 register_extension(&WGL_ARB_pixel_format);
3582 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3583 if (glxRequireExtension("GLX_ATI_render_texture") ||
3584 glxRequireExtension("GLX_ARB_render_texture") ||
3585 (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3587 register_extension(&WGL_ARB_render_texture);
3589 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3590 if(glxRequireExtension("GLX_NV_float_buffer"))
3591 register_extension_string("WGL_NV_float_buffer");
3593 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3594 if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3595 register_extension_string("WGL_NV_texture_rectangle");
3598 /* EXT Extensions */
3600 register_extension(&WGL_EXT_extensions_string);
3602 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3603 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3604 register_extension(&WGL_EXT_swap_control);
3606 if(glxRequireExtension("GLX_EXT_framebuffer_sRGB"))
3607 register_extension_string("WGL_EXT_framebuffer_sRGB");
3609 if(glxRequireExtension("GLX_EXT_fbconfig_packed_float"))
3610 register_extension_string("WGL_EXT_pixel_format_packed_float");
3612 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3613 if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3614 register_extension(&WGL_NV_vertex_array_range);
3616 /* WINE-specific WGL Extensions */
3618 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3619 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3621 register_extension(&WGL_WINE_pixel_format_passthrough);
3625 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3627 Drawable ret;
3629 if(physDev->bitmap)
3631 if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
3632 ret = physDev->drawable; /* PBuffer */
3633 else
3634 ret = physDev->bitmap->glxpixmap;
3636 else if(physDev->gl_drawable)
3637 ret = physDev->gl_drawable;
3638 else
3639 ret = physDev->drawable;
3640 return ret;
3643 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3645 wine_tsx11_lock();
3646 pglXDestroyGLXPixmap(display, glxpixmap);
3647 wine_tsx11_unlock();
3648 return TRUE;
3652 * X11DRV_SwapBuffers
3654 * Swap the buffers of this DC
3656 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
3658 GLXDrawable drawable;
3659 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
3661 if (!has_opengl()) return FALSE;
3663 TRACE("(%p)\n", physDev);
3665 drawable = get_glxdrawable(physDev);
3667 wine_tsx11_lock();
3668 sync_context(ctx);
3669 if(physDev->pixmap) {
3670 if(pglXCopySubBufferMESA) {
3671 int w = physDev->dc_rect.right - physDev->dc_rect.left;
3672 int h = physDev->dc_rect.bottom - physDev->dc_rect.top;
3674 /* (glX)SwapBuffers has an implicit glFlush effect, however
3675 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3676 * copying */
3677 pglFlush();
3678 if(w > 0 && h > 0)
3679 pglXCopySubBufferMESA(gdi_display, drawable, 0, 0, w, h);
3681 else
3682 pglXSwapBuffers(gdi_display, drawable);
3684 else
3685 pglXSwapBuffers(gdi_display, drawable);
3687 flush_gl_drawable(physDev);
3688 wine_tsx11_unlock();
3690 /* FPS support */
3691 if (TRACE_ON(fps))
3693 static long prev_time, start_time;
3694 static unsigned long frames, frames_total;
3696 DWORD time = GetTickCount();
3697 frames++;
3698 frames_total++;
3699 /* every 1.5 seconds */
3700 if (time - prev_time > 1500) {
3701 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
3702 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
3703 prev_time = time;
3704 frames = 0;
3705 if(start_time == 0) start_time = time;
3709 return TRUE;
3712 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3714 WineGLPixelFormat *fmt;
3715 XVisualInfo *ret;
3717 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
3718 if(fmt == NULL)
3719 return NULL;
3721 wine_tsx11_lock();
3722 ret = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
3723 wine_tsx11_unlock();
3724 return ret;
3727 #else /* no OpenGL includes */
3729 void X11DRV_OpenGL_Cleanup(void)
3733 static inline void opengl_error(void)
3735 static int warned;
3736 if (!warned++) ERR("No OpenGL support compiled in.\n");
3739 int pixelformat_from_fbconfig_id(XID fbconfig_id)
3741 return 0;
3744 void mark_drawable_dirty(Drawable old, Drawable new)
3748 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
3752 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
3754 return 0;
3757 /***********************************************************************
3758 * ChoosePixelFormat (X11DRV.@)
3760 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
3761 const PIXELFORMATDESCRIPTOR *ppfd) {
3762 opengl_error();
3763 return 0;
3766 /***********************************************************************
3767 * DescribePixelFormat (X11DRV.@)
3769 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
3770 int iPixelFormat,
3771 UINT nBytes,
3772 PIXELFORMATDESCRIPTOR *ppfd) {
3773 opengl_error();
3774 return 0;
3777 /***********************************************************************
3778 * GetPixelFormat (X11DRV.@)
3780 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
3781 opengl_error();
3782 return 0;
3785 /***********************************************************************
3786 * SetPixelFormat (X11DRV.@)
3788 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
3789 int iPixelFormat,
3790 const PIXELFORMATDESCRIPTOR *ppfd) {
3791 opengl_error();
3792 return FALSE;
3795 /***********************************************************************
3796 * SwapBuffers (X11DRV.@)
3798 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
3799 opengl_error();
3800 return FALSE;
3804 * X11DRV_wglCopyContext
3806 * For OpenGL32 wglCopyContext.
3808 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
3809 opengl_error();
3810 return FALSE;
3814 * X11DRV_wglCreateContext
3816 * For OpenGL32 wglCreateContext.
3818 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
3819 opengl_error();
3820 return NULL;
3824 * X11DRV_wglCreateContextAttribsARB
3826 * WGL_ARB_create_context: wglCreateContextAttribsARB
3828 HGLRC X11DRV_wglCreateContextAttribsARB(X11DRV_PDEVICE *physDev, HGLRC hShareContext, const int* attribList)
3830 opengl_error();
3831 return NULL;
3835 * X11DRV_wglDeleteContext
3837 * For OpenGL32 wglDeleteContext.
3839 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc) {
3840 opengl_error();
3841 return FALSE;
3845 * X11DRV_wglGetProcAddress
3847 * For OpenGL32 wglGetProcAddress.
3849 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
3850 opengl_error();
3851 return NULL;
3854 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, void *hPbuffer)
3856 opengl_error();
3857 return NULL;
3860 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
3861 opengl_error();
3862 return FALSE;
3866 * X11DRV_wglMakeCurrent
3868 * For OpenGL32 wglMakeCurrent.
3870 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
3871 opengl_error();
3872 return FALSE;
3876 * X11DRV_wglShareLists
3878 * For OpenGL32 wglShareLists.
3880 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
3881 opengl_error();
3882 return FALSE;
3886 * X11DRV_wglUseFontBitmapsA
3888 * For OpenGL32 wglUseFontBitmapsA.
3890 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3892 opengl_error();
3893 return FALSE;
3897 * X11DRV_wglUseFontBitmapsW
3899 * For OpenGL32 wglUseFontBitmapsW.
3901 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3903 opengl_error();
3904 return FALSE;
3908 * X11DRV_wglSetPixelFormatWINE
3910 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3911 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3913 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3915 opengl_error();
3916 return FALSE;
3919 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3921 return 0;
3924 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3926 return FALSE;
3929 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3931 return NULL;
3934 #endif /* defined(HAVE_OPENGL) */