winex11: Add support for OpenGL rendering in DIBs using an intermediate pixmap.
[wine/multimedia.git] / dlls / winex11.drv / opengl.c
blob1b5c7a9adb452246aaf202a02cd192fc41c09b9c
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 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #ifdef HAVE_SYS_UN_H
36 #include <sys/un.h>
37 #endif
39 #include "x11drv.h"
40 #include "winternl.h"
41 #include "wine/library.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
46 #ifdef SONAME_LIBGL
48 WINE_DECLARE_DEBUG_CHANNEL(winediag);
50 #undef APIENTRY
51 #undef CALLBACK
52 #undef WINAPI
54 #ifdef HAVE_GL_GL_H
55 # include <GL/gl.h>
56 #endif
57 #ifdef HAVE_GL_GLX_H
58 # include <GL/glx.h>
59 #endif
61 #include "wine/wgl.h"
63 #undef APIENTRY
64 #undef CALLBACK
65 #undef WINAPI
67 /* Redefines the constants */
68 #define CALLBACK __stdcall
69 #define WINAPI __stdcall
70 #define APIENTRY WINAPI
73 WINE_DECLARE_DEBUG_CHANNEL(fps);
75 typedef struct wine_glextension {
76 const char *extName;
77 struct {
78 const char *funcName;
79 void *funcAddress;
80 } extEntryPoints[9];
81 } WineGLExtension;
83 struct WineGLInfo {
84 const char *glVersion;
85 char *glExtensions;
87 int glxVersion[2];
89 const char *glxServerVersion;
90 const char *glxServerVendor;
91 const char *glxServerExtensions;
93 const char *glxClientVersion;
94 const char *glxClientVendor;
95 const char *glxClientExtensions;
97 const char *glxExtensions;
99 BOOL glxDirect;
100 char wglExtensions[4096];
103 typedef struct wine_glpixelformat {
104 int iPixelFormat;
105 GLXFBConfig fbconfig;
106 int fmt_id;
107 int render_type;
108 BOOL offscreenOnly;
109 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
110 } WineGLPixelFormat;
112 typedef struct wine_glcontext {
113 HDC hdc;
114 BOOL has_been_current;
115 BOOL sharing;
116 DWORD tid;
117 BOOL gl3_context;
118 XVisualInfo *vis;
119 WineGLPixelFormat *fmt;
120 int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
121 int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
122 GLXContext ctx;
123 HDC read_hdc;
124 Drawable drawables[2];
125 BOOL refresh_drawables;
126 Pixmap pixmap; /* pixmap for memory DCs */
127 GLXPixmap glxpixmap; /* GLX pixmap for memory DCs */
128 SIZE pixmap_size; /* pixmap size for memory DCs */
129 struct list entry;
130 } Wine_GLContext;
132 typedef struct wine_glpbuffer {
133 Drawable drawable;
134 Display* display;
135 WineGLPixelFormat* fmt;
136 int width;
137 int height;
138 int* attribList;
139 HDC hdc;
141 int use_render_texture; /* This is also the internal texture format */
142 int texture_bind_target;
143 int texture_bpp;
144 GLint texture_format;
145 GLuint texture_target;
146 GLenum texture_type;
147 GLuint texture;
148 int texture_level;
149 } Wine_GLPBuffer;
151 struct glx_physdev
153 struct gdi_physdev dev;
154 X11DRV_PDEVICE *x11dev;
155 enum dc_gl_type type; /* type of GL device context */
156 int pixel_format;
157 Drawable drawable;
158 Pixmap pixmap; /* pixmap for a DL_GL_PIXMAP_WIN drawable */
161 static const struct gdi_dc_funcs glxdrv_funcs;
163 static inline struct glx_physdev *get_glxdrv_dev( PHYSDEV dev )
165 return (struct glx_physdev *)dev;
168 static struct list context_list = LIST_INIT( context_list );
169 static struct WineGLInfo WineGLInfo = { 0 };
170 static int use_render_texture_emulation = 1;
171 static int use_render_texture_ati = 0;
172 static BOOL has_swap_control;
173 static int swap_interval = 1;
175 #define MAX_EXTENSIONS 16
176 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
177 static int WineGLExtensionListSize;
179 static void X11DRV_WineGL_LoadExtensions(void);
180 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count);
181 static BOOL glxRequireVersion(int requiredVersion);
182 static BOOL glxRequireExtension(const char *requiredExtension);
184 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
185 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
186 TRACE(" - dwFlags : ");
187 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
188 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
189 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
190 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
191 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
192 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
193 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
194 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
195 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
196 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
197 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
198 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
199 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
200 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
201 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
202 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
203 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
204 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
205 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
206 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
207 #undef TEST_AND_DUMP
208 TRACE("\n");
210 TRACE(" - iPixelType : ");
211 switch (ppfd->iPixelType) {
212 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
213 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
215 TRACE("\n");
217 TRACE(" - Color : %d\n", ppfd->cColorBits);
218 TRACE(" - Red : %d\n", ppfd->cRedBits);
219 TRACE(" - Green : %d\n", ppfd->cGreenBits);
220 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
221 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
222 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
223 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
224 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
225 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
227 TRACE(" - iLayerType : ");
228 switch (ppfd->iLayerType) {
229 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
230 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
231 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
233 TRACE("\n");
236 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
237 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
239 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
240 /* GLX 1.0 */
241 MAKE_FUNCPTR(glXChooseVisual)
242 MAKE_FUNCPTR(glXCopyContext)
243 MAKE_FUNCPTR(glXCreateContext)
244 MAKE_FUNCPTR(glXCreateGLXPixmap)
245 MAKE_FUNCPTR(glXGetCurrentContext)
246 MAKE_FUNCPTR(glXGetCurrentDrawable)
247 MAKE_FUNCPTR(glXDestroyContext)
248 MAKE_FUNCPTR(glXDestroyGLXPixmap)
249 MAKE_FUNCPTR(glXGetConfig)
250 MAKE_FUNCPTR(glXIsDirect)
251 MAKE_FUNCPTR(glXMakeCurrent)
252 MAKE_FUNCPTR(glXSwapBuffers)
253 MAKE_FUNCPTR(glXQueryExtension)
254 MAKE_FUNCPTR(glXQueryVersion)
256 /* GLX 1.1 */
257 MAKE_FUNCPTR(glXGetClientString)
258 MAKE_FUNCPTR(glXQueryExtensionsString)
259 MAKE_FUNCPTR(glXQueryServerString)
261 /* GLX 1.3 */
262 MAKE_FUNCPTR(glXGetFBConfigs)
263 MAKE_FUNCPTR(glXChooseFBConfig)
264 MAKE_FUNCPTR(glXCreatePbuffer)
265 MAKE_FUNCPTR(glXCreateNewContext)
266 MAKE_FUNCPTR(glXDestroyPbuffer)
267 MAKE_FUNCPTR(glXGetFBConfigAttrib)
268 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
269 MAKE_FUNCPTR(glXMakeContextCurrent)
270 MAKE_FUNCPTR(glXQueryDrawable)
271 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
273 /* GLX Extensions */
274 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
275 static void* (*pglXGetProcAddressARB)(const GLubyte *);
276 static int (*pglXSwapIntervalSGI)(int);
278 /* ATI GLX Extensions */
279 static BOOL (*pglXBindTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
280 static BOOL (*pglXReleaseTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
281 static BOOL (*pglXDrawableAttribATI)(Display *dpy, GLXDrawable draw, const int *attribList);
283 /* NV GLX Extension */
284 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
285 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
287 /* MESA GLX Extensions */
288 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
290 /* Standard OpenGL */
291 MAKE_FUNCPTR(glBindTexture)
292 MAKE_FUNCPTR(glBitmap)
293 MAKE_FUNCPTR(glCopyTexSubImage1D)
294 MAKE_FUNCPTR(glCopyTexImage2D)
295 MAKE_FUNCPTR(glCopyTexSubImage2D)
296 MAKE_FUNCPTR(glDrawBuffer)
297 MAKE_FUNCPTR(glEndList)
298 MAKE_FUNCPTR(glGetError)
299 MAKE_FUNCPTR(glGetIntegerv)
300 MAKE_FUNCPTR(glGetString)
301 MAKE_FUNCPTR(glNewList)
302 MAKE_FUNCPTR(glPixelStorei)
303 MAKE_FUNCPTR(glReadPixels)
304 MAKE_FUNCPTR(glTexImage2D)
305 MAKE_FUNCPTR(glFinish)
306 MAKE_FUNCPTR(glFlush)
307 #undef MAKE_FUNCPTR
309 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
311 /* In the future we might want to find the exact X or GLX error to report back to the app */
312 return 1;
315 static BOOL infoInitialized = FALSE;
316 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
318 int screen = DefaultScreen(gdi_display);
319 Window win = 0, root = 0;
320 const char *gl_renderer;
321 const char* str;
322 XVisualInfo *vis;
323 GLXContext ctx = NULL;
324 XSetWindowAttributes attr;
325 BOOL ret = FALSE;
326 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
328 if (infoInitialized)
329 return TRUE;
330 infoInitialized = TRUE;
332 attr.override_redirect = True;
333 attr.colormap = None;
334 attr.border_pixel = 0;
336 wine_tsx11_lock();
338 vis = pglXChooseVisual(gdi_display, screen, attribList);
339 if (vis) {
340 #ifdef __i386__
341 WORD old_fs = wine_get_fs();
342 /* Create a GLX Context. Without one we can't query GL information */
343 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
344 if (wine_get_fs() != old_fs)
346 wine_set_fs( old_fs );
347 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
348 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
349 goto done;
351 #else
352 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
353 #endif
355 if (!ctx) goto done;
357 root = RootWindow( gdi_display, vis->screen );
358 if (vis->visual != DefaultVisual( gdi_display, vis->screen ))
359 attr.colormap = XCreateColormap( gdi_display, root, vis->visual, AllocNone );
360 if ((win = XCreateWindow( gdi_display, root, -1, -1, 1, 1, 0, vis->depth, InputOutput,
361 vis->visual, CWBorderPixel | CWOverrideRedirect | CWColormap, &attr )))
362 XMapWindow( gdi_display, win );
363 else
364 win = root;
366 if(pglXMakeCurrent(gdi_display, win, ctx) == 0)
368 ERR_(winediag)( "Unable to activate OpenGL context, most likely your OpenGL drivers haven't been installed correctly\n" );
369 goto done;
371 gl_renderer = (const char *)pglGetString(GL_RENDERER);
372 WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
373 str = (const char *) pglGetString(GL_EXTENSIONS);
374 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
375 strcpy(WineGLInfo.glExtensions, str);
377 /* Get the common GLX version supported by GLX client and server ( major/minor) */
378 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
380 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
381 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
382 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
384 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
385 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
386 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
388 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
389 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
391 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
392 TRACE("GL renderer : %s.\n", gl_renderer);
393 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
394 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
395 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
396 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
397 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
398 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
400 if(!WineGLInfo.glxDirect)
402 int fd = ConnectionNumber(gdi_display);
403 struct sockaddr_un uaddr;
404 unsigned int uaddrlen = sizeof(struct sockaddr_un);
406 /* In general indirect rendering on a local X11 server indicates a driver problem.
407 * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
409 if(!getsockname(fd, (struct sockaddr *)&uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
410 ERR_(winediag)("Direct rendering is disabled, most likely your OpenGL drivers "
411 "haven't been installed correctly (using GL renderer %s, version %s).\n",
412 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
414 else
416 /* In general you would expect that if direct rendering is returned, that you receive hardware
417 * accelerated OpenGL rendering. The definition of direct rendering is that rendering is performed
418 * client side without sending all GL commands to X using the GLX protocol. When Mesa falls back to
419 * software rendering, it shows direct rendering.
421 * Depending on the cause of software rendering a different rendering string is shown. In case Mesa fails
422 * to load a DRI module 'Software Rasterizer' is returned. When Mesa is compiled as a OpenGL reference driver
423 * it shows 'Mesa X11'.
425 if(!strcmp(gl_renderer, "Software Rasterizer") || !strcmp(gl_renderer, "Mesa X11"))
426 ERR_(winediag)("The Mesa OpenGL driver is using software rendering, most likely your OpenGL "
427 "drivers haven't been installed correctly (using GL renderer %s, version %s).\n",
428 debugstr_a(gl_renderer), debugstr_a(WineGLInfo.glVersion));
430 ret = TRUE;
432 done:
433 if(vis) XFree(vis);
434 if(ctx) {
435 pglXMakeCurrent(gdi_display, None, NULL);
436 pglXDestroyContext(gdi_display, ctx);
438 if (win != root) XDestroyWindow( gdi_display, win );
439 if (attr.colormap) XFreeColormap( gdi_display, attr.colormap );
440 wine_tsx11_unlock();
441 if (!ret) ERR(" couldn't initialize OpenGL, expect problems\n");
442 return ret;
445 static BOOL has_opengl(void)
447 static int init_done;
448 static void *opengl_handle;
450 char buffer[200];
451 int error_base, event_base;
453 if (init_done) return (opengl_handle != NULL);
454 init_done = 1;
456 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
457 and include all dependencies */
458 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
459 if (opengl_handle == NULL)
461 ERR( "Failed to load libGL: %s\n", buffer );
462 ERR( "OpenGL support is disabled.\n");
463 return FALSE;
466 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
467 if (pglXGetProcAddressARB == NULL) {
468 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
469 goto failed;
472 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
474 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
475 goto failed; \
476 } while(0)
478 /* GLX 1.0 */
479 LOAD_FUNCPTR(glXChooseVisual);
480 LOAD_FUNCPTR(glXCopyContext);
481 LOAD_FUNCPTR(glXCreateContext);
482 LOAD_FUNCPTR(glXCreateGLXPixmap);
483 LOAD_FUNCPTR(glXGetCurrentContext);
484 LOAD_FUNCPTR(glXGetCurrentDrawable);
485 LOAD_FUNCPTR(glXDestroyContext);
486 LOAD_FUNCPTR(glXDestroyGLXPixmap);
487 LOAD_FUNCPTR(glXGetConfig);
488 LOAD_FUNCPTR(glXIsDirect);
489 LOAD_FUNCPTR(glXMakeCurrent);
490 LOAD_FUNCPTR(glXSwapBuffers);
491 LOAD_FUNCPTR(glXQueryExtension);
492 LOAD_FUNCPTR(glXQueryVersion);
494 /* GLX 1.1 */
495 LOAD_FUNCPTR(glXGetClientString);
496 LOAD_FUNCPTR(glXQueryExtensionsString);
497 LOAD_FUNCPTR(glXQueryServerString);
499 /* GLX 1.3 */
500 LOAD_FUNCPTR(glXCreatePbuffer);
501 LOAD_FUNCPTR(glXCreateNewContext);
502 LOAD_FUNCPTR(glXDestroyPbuffer);
503 LOAD_FUNCPTR(glXMakeContextCurrent);
504 LOAD_FUNCPTR(glXGetCurrentReadDrawable);
505 LOAD_FUNCPTR(glXGetFBConfigs);
507 /* Standard OpenGL calls */
508 LOAD_FUNCPTR(glBindTexture);
509 LOAD_FUNCPTR(glBitmap);
510 LOAD_FUNCPTR(glCopyTexSubImage1D);
511 LOAD_FUNCPTR(glCopyTexImage2D);
512 LOAD_FUNCPTR(glCopyTexSubImage2D);
513 LOAD_FUNCPTR(glDrawBuffer);
514 LOAD_FUNCPTR(glEndList);
515 LOAD_FUNCPTR(glGetError);
516 LOAD_FUNCPTR(glGetIntegerv);
517 LOAD_FUNCPTR(glGetString);
518 LOAD_FUNCPTR(glNewList);
519 LOAD_FUNCPTR(glPixelStorei);
520 LOAD_FUNCPTR(glReadPixels);
521 LOAD_FUNCPTR(glTexImage2D);
522 LOAD_FUNCPTR(glFinish);
523 LOAD_FUNCPTR(glFlush);
524 #undef LOAD_FUNCPTR
526 /* It doesn't matter if these fail. They'll only be used if the driver reports
527 the associated extension is available (and if a driver reports the extension
528 is available but fails to provide the functions, it's quite broken) */
529 #define LOAD_FUNCPTR(f) p##f = pglXGetProcAddressARB((const GLubyte *)#f)
530 /* ARB GLX Extension */
531 LOAD_FUNCPTR(glXCreateContextAttribsARB);
532 /* SGI GLX Extension */
533 LOAD_FUNCPTR(glXSwapIntervalSGI);
534 /* NV GLX Extension */
535 LOAD_FUNCPTR(glXAllocateMemoryNV);
536 LOAD_FUNCPTR(glXFreeMemoryNV);
537 #undef LOAD_FUNCPTR
539 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
541 wine_tsx11_lock();
542 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
543 TRACE("GLX is up and running error_base = %d\n", error_base);
544 } else {
545 wine_tsx11_unlock();
546 ERR( "GLX extension is missing, disabling OpenGL.\n" );
547 goto failed;
550 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
551 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
552 * rendering is used.
554 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
555 * depends on the reported GLX client / server version and on the client / server extension list.
556 * Those don't have to be the same.
558 * In general the server GLX information lists the capabilities in case of indirect rendering.
559 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
560 * available and in that case the client GLX informat can be used.
561 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
562 * in the GLX version/extension list. When a program does this it works for certain for both
563 * direct and indirect rendering.
565 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
566 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
567 * extension list which causes this extension not to be listed. (Wine requires this extension).
568 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
569 * pbuffers is around.
571 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
572 * the ATI drivers and from then on use GLX client information for them.
575 if(glxRequireVersion(3)) {
576 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
577 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
578 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
579 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
580 } else if(glxRequireExtension("GLX_SGIX_fbconfig")) {
581 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
582 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
583 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
585 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
586 * enable this function when the Xserver understand GLX 1.3 or newer
588 pglXQueryDrawable = NULL;
589 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
590 TRACE("Overriding ATI GLX capabilities!\n");
591 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
592 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
593 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
594 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
596 /* Use client GLX information in case of the ATI drivers. We override the
597 * capabilities over here and not somewhere else as ATI might better their
598 * life in the future. In case they release proper drivers this block of
599 * code won't be called. */
600 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
601 } else {
602 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
605 if(glxRequireExtension("GLX_ATI_render_texture")) {
606 use_render_texture_ati = 1;
607 pglXBindTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageATI");
608 pglXReleaseTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageATI");
609 pglXDrawableAttribATI = pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribATI");
612 if(glxRequireExtension("GLX_MESA_copy_sub_buffer")) {
613 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
616 X11DRV_WineGL_LoadExtensions();
618 wine_tsx11_unlock();
619 return TRUE;
621 failed:
622 wine_dlclose(opengl_handle, NULL, 0);
623 opengl_handle = NULL;
624 return FALSE;
627 static inline BOOL is_valid_context( Wine_GLContext *ctx )
629 Wine_GLContext *ptr;
630 LIST_FOR_EACH_ENTRY( ptr, &context_list, struct wine_glcontext, entry )
631 if (ptr == ctx) return TRUE;
632 return FALSE;
635 static int describeContext(Wine_GLContext* ctx) {
636 int tmp;
637 int ctx_vis_id;
638 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
639 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
640 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
641 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
642 TRACE(" - VISUAL_ID 0x%x\n", tmp);
643 ctx_vis_id = tmp;
644 return ctx_vis_id;
647 static BOOL describeDrawable( struct glx_physdev *physdev )
649 int tmp;
650 WineGLPixelFormat *fmt;
651 int fmt_count = 0;
653 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physdev->pixel_format, TRUE /* Offscreen */, &fmt_count);
654 if(!fmt) return FALSE;
656 TRACE(" HDC %p has:\n", physdev->dev.hdc);
657 TRACE(" - iPixelFormat %d\n", fmt->iPixelFormat);
658 TRACE(" - Drawable %lx\n", physdev->drawable);
659 TRACE(" - FBCONFIG_ID 0x%x\n", fmt->fmt_id);
661 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &tmp);
662 TRACE(" - VISUAL_ID 0x%x\n", tmp);
664 return TRUE;
667 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
668 int nAttribs = 0;
669 unsigned cur = 0;
670 int pop;
671 int drawattrib = 0;
672 int nvfloatattrib = GLX_DONT_CARE;
673 int pixelattrib = GLX_DONT_CARE;
675 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
676 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
677 while (iWGLAttr && 0 != iWGLAttr[cur]) {
678 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
680 switch (iWGLAttr[cur]) {
681 case WGL_AUX_BUFFERS_ARB:
682 pop = iWGLAttr[++cur];
683 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
684 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
685 break;
686 case WGL_COLOR_BITS_ARB:
687 pop = iWGLAttr[++cur];
688 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
689 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
690 break;
691 case WGL_BLUE_BITS_ARB:
692 pop = iWGLAttr[++cur];
693 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
694 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
695 break;
696 case WGL_RED_BITS_ARB:
697 pop = iWGLAttr[++cur];
698 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
699 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
700 break;
701 case WGL_GREEN_BITS_ARB:
702 pop = iWGLAttr[++cur];
703 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
704 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
705 break;
706 case WGL_ALPHA_BITS_ARB:
707 pop = iWGLAttr[++cur];
708 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
709 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
710 break;
711 case WGL_DEPTH_BITS_ARB:
712 pop = iWGLAttr[++cur];
713 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
714 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
715 break;
716 case WGL_STENCIL_BITS_ARB:
717 pop = iWGLAttr[++cur];
718 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
719 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
720 break;
721 case WGL_DOUBLE_BUFFER_ARB:
722 pop = iWGLAttr[++cur];
723 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
724 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
725 break;
726 case WGL_STEREO_ARB:
727 pop = iWGLAttr[++cur];
728 PUSH2(oGLXAttr, GLX_STEREO, pop);
729 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
730 break;
732 case WGL_PIXEL_TYPE_ARB:
733 pop = iWGLAttr[++cur];
734 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
735 switch (pop) {
736 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
737 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
738 /* 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 */
739 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
740 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
741 default:
742 ERR("unexpected PixelType(%x)\n", pop);
743 pop = 0;
745 break;
747 case WGL_SUPPORT_GDI_ARB:
748 /* This flag is set in a WineGLPixelFormat */
749 pop = iWGLAttr[++cur];
750 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
751 break;
753 case WGL_DRAW_TO_BITMAP_ARB:
754 /* This flag is set in a WineGLPixelFormat */
755 pop = iWGLAttr[++cur];
756 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
757 break;
759 case WGL_DRAW_TO_WINDOW_ARB:
760 pop = iWGLAttr[++cur];
761 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
762 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
763 if (pop) {
764 drawattrib |= GLX_WINDOW_BIT;
766 break;
768 case WGL_DRAW_TO_PBUFFER_ARB:
769 pop = iWGLAttr[++cur];
770 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
771 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
772 if (pop) {
773 drawattrib |= GLX_PBUFFER_BIT;
775 break;
777 case WGL_ACCELERATION_ARB:
778 /* This flag is set in a WineGLPixelFormat */
779 pop = iWGLAttr[++cur];
780 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
781 break;
783 case WGL_SUPPORT_OPENGL_ARB:
784 pop = iWGLAttr[++cur];
785 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
786 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
787 break;
789 case WGL_SWAP_METHOD_ARB:
790 pop = iWGLAttr[++cur];
791 /* For now we ignore this and just return SWAP_EXCHANGE */
792 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
793 break;
795 case WGL_PBUFFER_LARGEST_ARB:
796 pop = iWGLAttr[++cur];
797 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
798 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
799 break;
801 case WGL_SAMPLE_BUFFERS_ARB:
802 pop = iWGLAttr[++cur];
803 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
804 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
805 break;
807 case WGL_SAMPLES_ARB:
808 pop = iWGLAttr[++cur];
809 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
810 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
811 break;
813 case WGL_TEXTURE_FORMAT_ARB:
814 case WGL_TEXTURE_TARGET_ARB:
815 case WGL_MIPMAP_TEXTURE_ARB:
816 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
817 pop = iWGLAttr[++cur];
818 if (NULL == pbuf) {
819 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
821 if (use_render_texture_ati) {
822 /** nothing to do here */
824 else if (!use_render_texture_emulation) {
825 if (WGL_NO_TEXTURE_ARB != pop) {
826 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
827 return -1; /** error: don't support it */
828 } else {
829 drawattrib |= GLX_PBUFFER_BIT;
832 break ;
833 case WGL_FLOAT_COMPONENTS_NV:
834 nvfloatattrib = iWGLAttr[++cur];
835 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
836 break ;
837 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
838 case WGL_BIND_TO_TEXTURE_RGB_ARB:
839 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
840 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
841 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
842 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
843 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
844 pop = iWGLAttr[++cur];
845 /** cannot be converted, see direct handling on
846 * - wglGetPixelFormatAttribivARB
847 * TODO: wglChoosePixelFormat
849 break ;
850 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
851 pop = iWGLAttr[++cur];
852 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
853 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
854 break ;
856 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
857 pop = iWGLAttr[++cur];
858 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
859 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
860 break ;
861 default:
862 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
863 break;
865 ++cur;
868 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
869 * all formats. Unless drawattrib is set to a non-zero value override it with GLX_DONT_CARE, so that
870 * pixmap and pbuffer formats appear as well. */
871 if (!drawattrib) drawattrib = GLX_DONT_CARE;
872 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
873 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
875 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
876 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to
877 * GLX_DONT_CARE unless it is overridden. */
878 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
879 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
881 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
882 if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
883 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
884 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
887 return nAttribs;
890 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
892 int render_type=0, render_type_bit;
893 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
894 switch(render_type_bit)
896 case GLX_RGBA_BIT:
897 render_type = GLX_RGBA_TYPE;
898 break;
899 case GLX_COLOR_INDEX_BIT:
900 render_type = GLX_COLOR_INDEX_TYPE;
901 break;
902 case GLX_RGBA_FLOAT_BIT:
903 render_type = GLX_RGBA_FLOAT_TYPE;
904 break;
905 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
906 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
907 break;
908 default:
909 ERR("Unknown render_type: %x\n", render_type_bit);
911 return render_type;
914 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
915 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
917 int dbuf, value;
918 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
919 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
921 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
922 * the GLX_PIXMAP_BIT set. */
923 return !dbuf && (value & GLX_PIXMAP_BIT);
926 static WineGLPixelFormat *get_formats(Display *display, int *size_ret, int *onscreen_size_ret)
928 static WineGLPixelFormat *list;
929 static int size, onscreen_size;
931 int fmt_id, nCfgs, i, run, bmp_formats;
932 GLXFBConfig* cfgs;
933 XVisualInfo *visinfo;
935 wine_tsx11_lock();
936 if (list) goto done;
938 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
939 if (NULL == cfgs || 0 == nCfgs) {
940 if(cfgs != NULL) XFree(cfgs);
941 wine_tsx11_unlock();
942 ERR("glXChooseFBConfig returns NULL\n");
943 return NULL;
946 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
947 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
948 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
949 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
951 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
953 for(i=0, bmp_formats=0; i<nCfgs; i++)
955 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
956 bmp_formats++;
958 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
960 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats)*sizeof(WineGLPixelFormat));
962 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
963 * Do this as GLX doesn't guarantee that the list is sorted */
964 for(run=0; run < 2; run++)
966 for(i=0; i<nCfgs; i++) {
967 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
968 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
970 /* The first run we only add onscreen formats (ones which have an associated X Visual).
971 * The second run we only set offscreen formats. */
972 if(!run && visinfo)
974 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
975 * The contents is copied to the destination using XCopyArea. For the copying to work
976 * the depth of the source and destination window should be the same. In general this should
977 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
978 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
979 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
980 * list formats with the same depth. */
981 if(visinfo->depth != screen_depth)
983 XFree(visinfo);
984 continue;
987 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
988 list[size].iPixelFormat = size+1; /* The index starts at 1 */
989 list[size].fbconfig = cfgs[i];
990 list[size].fmt_id = fmt_id;
991 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
992 list[size].offscreenOnly = FALSE;
993 list[size].dwFlags = 0;
994 size++;
995 onscreen_size++;
997 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
998 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
1000 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1001 list[size].iPixelFormat = size+1; /* The index starts at 1 */
1002 list[size].fbconfig = cfgs[i];
1003 list[size].fmt_id = fmt_id;
1004 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1005 list[size].offscreenOnly = FALSE;
1006 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
1007 size++;
1008 onscreen_size++;
1010 } else if(run && !visinfo) {
1011 int window_drawable=0;
1012 pglXGetFBConfigAttrib(gdi_display, cfgs[i], GLX_DRAWABLE_TYPE, &window_drawable);
1014 /* Recent Nvidia drivers and DRI drivers offer window drawable formats without a visual.
1015 * This are formats like 16-bit rgb on a 24-bit desktop. In order to support these formats
1016 * onscreen we would have to use glXCreateWindow instead of XCreateWindow. Further it will
1017 * likely make our child window opengl rendering more complicated since likely you can't use
1018 * XCopyArea on a GLX Window.
1019 * For now ignore fbconfigs which are window drawable but lack a visual. */
1020 if(window_drawable & GLX_WINDOW_BIT)
1022 TRACE("Skipping FBCONFIG_ID 0x%x as an offscreen format because it is window_drawable\n", fmt_id);
1023 continue;
1026 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
1027 list[size].iPixelFormat = size+1; /* The index starts at 1 */
1028 list[size].fbconfig = cfgs[i];
1029 list[size].fmt_id = fmt_id;
1030 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
1031 list[size].offscreenOnly = TRUE;
1032 list[size].dwFlags = 0;
1033 size++;
1036 if (visinfo) XFree(visinfo);
1040 XFree(cfgs);
1042 done:
1043 if (size_ret) *size_ret = size;
1044 if (onscreen_size_ret) *onscreen_size_ret = onscreen_size;
1045 wine_tsx11_unlock();
1046 return list;
1049 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1050 * In our WGL implementation we only support a subset of these formats namely the format of
1051 * Wine's main visual and offscreen formats (if they are available).
1052 * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
1053 * and it returns the number of supported WGL formats in fmt_count.
1055 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
1057 WineGLPixelFormat *list, *res = NULL;
1058 int size, onscreen_size;
1060 if (!(list = get_formats(display, &size, &onscreen_size ))) return NULL;
1062 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1063 * iPixelFormat in case of probing the number of pixelformats.
1065 if((iPixelFormat > 0) && (iPixelFormat <= size) &&
1066 (!list[iPixelFormat-1].offscreenOnly || AllowOffscreen)) {
1067 res = &list[iPixelFormat-1];
1068 TRACE("Returning fmt_id=%#x for iPixelFormat=%d\n", res->fmt_id, iPixelFormat);
1071 if(AllowOffscreen)
1072 *fmt_count = size;
1073 else
1074 *fmt_count = onscreen_size;
1076 TRACE("Number of returned pixelformats=%d\n", *fmt_count);
1078 return res;
1081 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
1082 static WineGLPixelFormat* ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id, DWORD dwFlags)
1084 WineGLPixelFormat *list;
1085 int i, size;
1087 if (!(list = get_formats(display, &size, NULL ))) return NULL;
1089 for(i=0; i<size; i++) {
1090 /* A GLX format can appear multiple times in the pixel format list due to fake formats for bitmap rendering.
1091 * Fake formats might get selected when the user passes the proper flags using the dwFlags parameter. */
1092 if( (list[i].fmt_id == fmt_id) && ((list[i].dwFlags & dwFlags) == dwFlags) ) {
1093 TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", list[i].iPixelFormat, fmt_id);
1094 return &list[i];
1097 TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1098 return NULL;
1101 static int pixelformat_from_fbconfig_id(XID fbconfig_id)
1103 WineGLPixelFormat *fmt;
1105 if (!fbconfig_id) return 0;
1107 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
1108 if(fmt)
1109 return fmt->iPixelFormat;
1110 /* This will happen on hwnds without a pixel format set; it's ok */
1111 return 0;
1115 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1116 void mark_drawable_dirty(Drawable old, Drawable new)
1118 Wine_GLContext *ctx;
1119 LIST_FOR_EACH_ENTRY( ctx, &context_list, struct wine_glcontext, entry )
1121 if (old == ctx->drawables[0]) {
1122 ctx->drawables[0] = new;
1123 ctx->refresh_drawables = TRUE;
1125 if (old == ctx->drawables[1]) {
1126 ctx->drawables[1] = new;
1127 ctx->refresh_drawables = TRUE;
1132 /* Given the current context, make sure its drawable is sync'd */
1133 static inline void sync_context(Wine_GLContext *context)
1135 if(context && context->refresh_drawables) {
1136 if (glxRequireVersion(3))
1137 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1138 context->drawables[1], context->ctx);
1139 else
1140 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1141 context->refresh_drawables = FALSE;
1146 static GLXContext create_glxcontext(Display *display, Wine_GLContext *context, GLXContext shareList)
1148 GLXContext ctx;
1150 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1151 BOOL indirect = (context->fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? FALSE : TRUE;
1153 if(context->gl3_context)
1155 if(context->numAttribs)
1156 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1157 else
1158 ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1160 else if(context->vis)
1161 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1162 else /* Create a GLX Context for a pbuffer */
1163 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1165 return ctx;
1169 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
1171 return pglXCreateGLXPixmap(display, vis, parent);
1175 static XID create_bitmap_glxpixmap(X11DRV_PDEVICE *physDev, WineGLPixelFormat *fmt)
1177 GLXPixmap ret = 0;
1178 XVisualInfo *vis;
1180 wine_tsx11_lock();
1182 vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1183 if(vis) {
1184 if(vis->depth == physDev->bitmap->depth)
1185 ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
1186 XFree(vis);
1188 wine_tsx11_unlock();
1189 TRACE("return %lx\n", ret);
1190 return ret;
1194 * glxdrv_ChoosePixelFormat
1196 * Equivalent to glXChooseVisual.
1198 static int glxdrv_ChoosePixelFormat(PHYSDEV dev, const PIXELFORMATDESCRIPTOR *ppfd)
1200 WineGLPixelFormat *list;
1201 int onscreen_size;
1202 int ret = 0;
1203 int value = 0;
1204 int i = 0;
1205 int bestFormat = -1;
1206 int bestDBuffer = -1;
1207 int bestStereo = -1;
1208 int bestColor = -1;
1209 int bestAlpha = -1;
1210 int bestDepth = -1;
1211 int bestStencil = -1;
1212 int bestAux = -1;
1214 if (!has_opengl()) return 0;
1216 if (TRACE_ON(wgl)) {
1217 TRACE("(%p,%p)\n", dev->hdc, ppfd);
1219 dump_PIXELFORMATDESCRIPTOR(ppfd);
1222 if (!(list = get_formats(gdi_display, NULL, &onscreen_size ))) return 0;
1224 wine_tsx11_lock();
1225 for(i=0; i<onscreen_size; i++)
1227 int dwFlags = 0;
1228 int iPixelType = 0;
1229 int alpha=0, color=0, depth=0, stencil=0, aux=0;
1230 WineGLPixelFormat *fmt = &list[i];
1232 /* Pixel type */
1233 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1234 if (value & GLX_RGBA_BIT)
1235 iPixelType = PFD_TYPE_RGBA;
1236 else
1237 iPixelType = PFD_TYPE_COLORINDEX;
1239 if (ppfd->iPixelType != iPixelType)
1241 TRACE("pixel type mismatch for iPixelFormat=%d\n", i+1);
1242 continue;
1245 /* Only use bitmap capable for formats for bitmap rendering.
1246 * See get_formats for more info. */
1247 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (fmt->dwFlags & PFD_DRAW_TO_BITMAP))
1249 TRACE("PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i+1);
1250 continue;
1253 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1254 if (value) dwFlags |= PFD_DOUBLEBUFFER;
1255 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value);
1256 if (value) dwFlags |= PFD_STEREO;
1257 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &color); /* cColorBits */
1258 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &alpha); /* cAlphaBits */
1259 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &depth); /* cDepthBits */
1260 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &stencil); /* cStencilBits */
1261 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &aux); /* cAuxBuffers */
1263 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
1264 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
1265 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
1266 * formats without the given flag set.
1267 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
1268 * has indicated that a format without stereo is returned when stereo is unavailable.
1269 * So in case PFD_STEREO is set, formats that support it should have priority above formats
1270 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
1272 * To summarize the following is most likely the correct behavior:
1273 * stereo not set -> prefer no-stereo formats, else also accept stereo formats
1274 * stereo set -> prefer stereo formats, else also accept no-stereo formats
1275 * stereo don't care -> it doesn't matter whether we get stereo or not
1277 * In Wine we will treat no-stereo the same way as don't care because it makes
1278 * format selection even more complicated and second drivers with Stereo advertise
1279 * each format twice anyway.
1282 /* Doublebuffer, see the comments above */
1283 if( !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) ) {
1284 if( ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
1285 ((dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)) )
1286 goto found;
1288 if(bestDBuffer != -1 && (dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer)
1289 continue;
1292 /* Stereo, see the comments above. */
1293 if( !(ppfd->dwFlags & PFD_STEREO_DONTCARE) ) {
1294 if( ((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
1295 ((dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)) )
1296 goto found;
1298 if(bestStereo != -1 && (dwFlags & PFD_STEREO) != bestStereo)
1299 continue;
1302 /* Below we will do a number of checks to select the 'best' pixelformat.
1303 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
1304 * The code works by trying to match the most important options as close as possible.
1305 * When a reasonable format is found, we will try to match more options.
1306 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
1307 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
1308 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
1310 if(ppfd->cColorBits) {
1311 if( ((ppfd->cColorBits > bestColor) && (color > bestColor)) ||
1312 ((color >= ppfd->cColorBits) && (color < bestColor)) )
1313 goto found;
1315 if(bestColor != color) { /* Do further checks if the format is compatible */
1316 TRACE("color mismatch for iPixelFormat=%d\n", i+1);
1317 continue;
1321 if(ppfd->cAlphaBits) {
1322 if( ((ppfd->cAlphaBits > bestAlpha) && (alpha > bestAlpha)) ||
1323 ((alpha >= ppfd->cAlphaBits) && (alpha < bestAlpha)) )
1324 goto found;
1326 if(bestAlpha != alpha) {
1327 TRACE("alpha mismatch for iPixelFormat=%d\n", i+1);
1328 continue;
1332 if(ppfd->cDepthBits) {
1333 if( ((ppfd->cDepthBits > bestDepth) && (depth > bestDepth)) ||
1334 ((depth >= ppfd->cDepthBits) && (depth < bestDepth)) )
1335 goto found;
1337 if(bestDepth != depth) {
1338 TRACE("depth mismatch for iPixelFormat=%d\n", i+1);
1339 continue;
1343 if(ppfd->cStencilBits) {
1344 if( ((ppfd->cStencilBits > bestStencil) && (stencil > bestStencil)) ||
1345 ((stencil >= ppfd->cStencilBits) && (stencil < bestStencil)) )
1346 goto found;
1348 if(bestStencil != stencil) {
1349 TRACE("stencil mismatch for iPixelFormat=%d\n", i+1);
1350 continue;
1354 if(ppfd->cAuxBuffers) {
1355 if( ((ppfd->cAuxBuffers > bestAux) && (aux > bestAux)) ||
1356 ((aux >= ppfd->cAuxBuffers) && (aux < bestAux)) )
1357 goto found;
1359 if(bestAux != aux) {
1360 TRACE("aux mismatch for iPixelFormat=%d\n", i+1);
1361 continue;
1364 continue;
1366 found:
1367 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1368 bestStereo = dwFlags & PFD_STEREO;
1369 bestAlpha = alpha;
1370 bestColor = color;
1371 bestDepth = depth;
1372 bestStencil = stencil;
1373 bestAux = aux;
1374 bestFormat = i;
1377 if(bestFormat == -1) {
1378 TRACE("No matching mode was found returning 0\n");
1379 ret = 0;
1381 else {
1382 ret = bestFormat+1; /* the return value should be a 1-based index */
1383 TRACE("Successfully found a matching mode, returning index: %d %x\n", ret, list[bestFormat].fmt_id);
1386 wine_tsx11_unlock();
1388 return ret;
1392 * glxdrv_DescribePixelFormat
1394 * Get the pixel-format descriptor associated to the given id
1396 static int glxdrv_DescribePixelFormat(PHYSDEV dev, int iPixelFormat,
1397 UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
1399 /*XVisualInfo *vis;*/
1400 int value;
1401 int rb,gb,bb,ab;
1402 WineGLPixelFormat *fmt;
1403 int ret = 0;
1404 int fmt_count = 0;
1406 if (!has_opengl()) return 0;
1408 TRACE("(%p,%d,%d,%p)\n", dev->hdc, iPixelFormat, nBytes, ppfd);
1410 /* 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 */
1411 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1412 if (ppfd == NULL) {
1413 /* The application is only querying the number of pixelformats */
1414 return fmt_count;
1415 } else if(fmt == NULL) {
1416 WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1417 return 0;
1420 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1421 ERR("Wrong structure size !\n");
1422 /* Should set error */
1423 return 0;
1426 ret = fmt_count;
1428 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1429 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1430 ppfd->nVersion = 1;
1432 /* These flags are always the same... */
1433 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1434 /* Now the flags extracted from the Visual */
1436 wine_tsx11_lock();
1438 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1439 if(value & GLX_WINDOW_BIT)
1440 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1442 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1443 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1444 * 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
1445 * offered the GDI bit either. */
1446 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1448 /* PFD_GENERIC_FORMAT - gdi software rendering
1449 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1450 * none set - full hardware accelerated by a ICD
1452 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1453 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1455 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1456 if (value) {
1457 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1458 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1460 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1462 /* Pixel type */
1463 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1464 if (value & GLX_RGBA_BIT)
1465 ppfd->iPixelType = PFD_TYPE_RGBA;
1466 else
1467 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1469 /* Color bits */
1470 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1471 ppfd->cColorBits = value;
1473 /* Red, green, blue and alpha bits / shifts */
1474 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1475 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1476 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1477 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1478 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1480 ppfd->cRedBits = rb;
1481 ppfd->cRedShift = gb + bb + ab;
1482 ppfd->cBlueBits = bb;
1483 ppfd->cBlueShift = ab;
1484 ppfd->cGreenBits = gb;
1485 ppfd->cGreenShift = bb + ab;
1486 ppfd->cAlphaBits = ab;
1487 ppfd->cAlphaShift = 0;
1488 } else {
1489 ppfd->cRedBits = 0;
1490 ppfd->cRedShift = 0;
1491 ppfd->cBlueBits = 0;
1492 ppfd->cBlueShift = 0;
1493 ppfd->cGreenBits = 0;
1494 ppfd->cGreenShift = 0;
1495 ppfd->cAlphaBits = 0;
1496 ppfd->cAlphaShift = 0;
1499 /* Accum RGBA bits */
1500 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1501 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1502 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1503 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1505 ppfd->cAccumBits = rb+gb+bb+ab;
1506 ppfd->cAccumRedBits = rb;
1507 ppfd->cAccumGreenBits = gb;
1508 ppfd->cAccumBlueBits = bb;
1509 ppfd->cAccumAlphaBits = ab;
1511 /* Aux bits */
1512 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1513 ppfd->cAuxBuffers = value;
1515 /* Depth bits */
1516 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1517 ppfd->cDepthBits = value;
1519 /* stencil bits */
1520 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1521 ppfd->cStencilBits = value;
1523 wine_tsx11_unlock();
1525 ppfd->iLayerType = PFD_MAIN_PLANE;
1527 if (TRACE_ON(wgl)) {
1528 dump_PIXELFORMATDESCRIPTOR(ppfd);
1531 return ret;
1535 * glxdrv_GetPixelFormat
1537 * Get the pixel-format id used by this DC
1539 static int glxdrv_GetPixelFormat(PHYSDEV dev)
1541 struct glx_physdev *physdev = get_glxdrv_dev( dev );
1542 WineGLPixelFormat *fmt;
1543 int tmp;
1545 if (!physdev->pixel_format) return 0; /* not set yet */
1547 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physdev->pixel_format, TRUE, &tmp);
1548 if(!fmt)
1550 ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physdev->pixel_format);
1551 return 0;
1553 else if(fmt->offscreenOnly)
1555 /* Offscreen formats can't be used with traditional WGL calls.
1556 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1557 TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1558 return 1;
1561 TRACE("(%p): returns %d\n", dev->hdc, physdev->pixel_format);
1562 return physdev->pixel_format;
1565 /* This function is the core of X11DRV_SetPixelFormat and X11DRV_SetPixelFormatWINE.
1566 * Both functions are the same except that X11DRV_SetPixelFormatWINE allows you to
1567 * set the pixel format multiple times. */
1568 static BOOL internal_SetPixelFormat( struct glx_physdev *physdev,
1569 int iPixelFormat,
1570 const PIXELFORMATDESCRIPTOR *ppfd)
1572 WineGLPixelFormat *fmt;
1573 int value;
1574 HWND hwnd;
1576 /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1577 if(physdev->x11dev->drawable == root_window)
1579 ERR("Invalid operation on root_window\n");
1580 return FALSE;
1583 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1584 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1585 if(!fmt) {
1586 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1587 return FALSE;
1590 wine_tsx11_lock();
1591 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1592 wine_tsx11_unlock();
1594 hwnd = WindowFromDC(physdev->dev.hdc);
1595 if(hwnd) {
1596 if(!(value&GLX_WINDOW_BIT)) {
1597 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1598 return FALSE;
1601 if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, fmt->fmt_id, 0)) {
1602 ERR("Couldn't set format of the window, returning failure\n");
1603 return FALSE;
1605 /* physDev->current_pf will be set by the DCE update */
1607 else if(physdev->x11dev->bitmap) {
1608 if(!(value&GLX_PIXMAP_BIT)) {
1609 WARN("Pixel format %d is not compatible for bitmap rendering\n", iPixelFormat);
1610 return FALSE;
1613 if (physdev->x11dev->bitmap->hbitmap == GetCurrentObject( physdev->dev.hdc, OBJ_BITMAP ))
1615 physdev->x11dev->bitmap->glxpixmap = create_bitmap_glxpixmap(physdev->x11dev, fmt);
1616 if(!physdev->x11dev->bitmap->glxpixmap) {
1617 WARN("Couldn't create glxpixmap for pixel format %d\n", iPixelFormat);
1618 return FALSE;
1621 /* otherwise we have a DIB section */
1623 physdev->pixel_format = iPixelFormat;
1624 physdev->type = DC_GL_BITMAP;
1626 else {
1627 FIXME("called on a non-window, non-bitmap object?\n");
1630 if (TRACE_ON(wgl)) {
1631 int gl_test = 0;
1633 wine_tsx11_lock();
1634 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1635 if (gl_test) {
1636 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1637 } else {
1638 TRACE(" FBConfig have :\n");
1639 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1640 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1641 TRACE(" - VISUAL_ID 0x%x\n", value);
1642 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1643 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1645 wine_tsx11_unlock();
1647 return TRUE;
1652 * glxdrv_SetPixelFormat
1654 * Set the pixel-format id used by this DC
1656 static BOOL glxdrv_SetPixelFormat(PHYSDEV dev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
1658 struct glx_physdev *physdev = get_glxdrv_dev( dev );
1660 TRACE("(%p,%d,%p)\n", dev->hdc, iPixelFormat, ppfd);
1662 if (!has_opengl()) return FALSE;
1664 if(physdev->pixel_format) /* cannot change it if already set */
1665 return (physdev->pixel_format == iPixelFormat);
1667 return internal_SetPixelFormat(physdev, iPixelFormat, ppfd);
1671 * glxdrv_wglCopyContext
1673 * For OpenGL32 wglCopyContext.
1675 static BOOL glxdrv_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
1677 Wine_GLContext *src = (Wine_GLContext*)hglrcSrc;
1678 Wine_GLContext *dst = (Wine_GLContext*)hglrcDst;
1680 TRACE("hglrcSrc: (%p), hglrcDst: (%p), mask: %#x\n", hglrcSrc, hglrcDst, mask);
1682 wine_tsx11_lock();
1683 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1684 wine_tsx11_unlock();
1686 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1687 return TRUE;
1691 * X11DRV_wglCreateContext
1693 * For OpenGL32 wglCreateContext.
1695 static HGLRC glxdrv_wglCreateContext(PHYSDEV dev)
1697 struct glx_physdev *physdev = get_glxdrv_dev( dev );
1698 Wine_GLContext *ret;
1699 WineGLPixelFormat *fmt;
1700 int fmt_count = 0;
1702 TRACE("(%p)->(PF:%d)\n", dev->hdc, physdev->pixel_format);
1704 if (!has_opengl()) return 0;
1706 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physdev->pixel_format, TRUE /* Offscreen */, &fmt_count);
1707 /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1708 * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1709 * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1710 * If this fails something is very wrong on the system. */
1711 if(!fmt) {
1712 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", physdev->pixel_format);
1713 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1714 return NULL;
1717 if (!(ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret)))) return 0;
1719 ret->hdc = dev->hdc;
1720 ret->fmt = fmt;
1721 ret->has_been_current = FALSE;
1722 ret->sharing = FALSE;
1724 wine_tsx11_lock();
1725 ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1726 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1727 list_add_head( &context_list, &ret->entry );
1728 wine_tsx11_unlock();
1730 TRACE(" creating context %p (GL context creation delayed)\n", ret);
1731 return (HGLRC) ret;
1735 * glxdrv_wglDeleteContext
1737 * For OpenGL32 wglDeleteContext.
1739 static BOOL glxdrv_wglDeleteContext(HGLRC hglrc)
1741 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1743 TRACE("(%p)\n", hglrc);
1745 if (!has_opengl()) return 0;
1747 if (!is_valid_context(ctx))
1749 WARN("Error deleting context !\n");
1750 SetLastError(ERROR_INVALID_HANDLE);
1751 return FALSE;
1754 /* WGL doesn't allow deletion of a context which is current in another thread */
1755 if (ctx->tid != 0 && ctx->tid != GetCurrentThreadId())
1757 TRACE("Cannot delete context=%p because it is current in another thread.\n", ctx);
1758 SetLastError(ERROR_BUSY);
1759 return FALSE;
1762 /* WGL makes a context not current if it is active before deletion. GLX waits until the context is not current. */
1763 if (ctx == NtCurrentTeb()->glContext)
1764 wglMakeCurrent(ctx->hdc, NULL);
1766 wine_tsx11_lock();
1767 list_remove( &ctx->entry );
1768 if (ctx->ctx) pglXDestroyContext( gdi_display, ctx->ctx );
1769 if (ctx->glxpixmap) pglXDestroyGLXPixmap( gdi_display, ctx->glxpixmap );
1770 if (ctx->pixmap) XFreePixmap( gdi_display, ctx->pixmap );
1771 if (ctx->vis) XFree( ctx->vis );
1772 wine_tsx11_unlock();
1774 HeapFree( GetProcessHeap(), 0, ctx );
1775 return TRUE;
1779 * X11DRV_wglGetCurrentReadDCARB
1781 * For OpenGL32 wglGetCurrentReadDCARB.
1783 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void)
1785 HDC ret = 0;
1786 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
1788 if (ctx) ret = ctx->read_hdc;
1790 TRACE(" returning %p (GL drawable %lu)\n", ret, ctx ? ctx->drawables[1] : 0);
1791 return ret;
1795 * glxdrv_wglGetProcAddress
1797 * For OpenGL32 wglGetProcAddress.
1799 static PROC glxdrv_wglGetProcAddress(LPCSTR lpszProc)
1801 int i, j;
1802 const WineGLExtension *ext;
1804 int padding = 32 - strlen(lpszProc);
1805 if (padding < 0)
1806 padding = 0;
1808 if (!has_opengl()) return NULL;
1810 /* Check the table of WGL extensions to see if we need to return a WGL extension
1811 * or a function pointer to a native OpenGL function. */
1812 if(strncmp(lpszProc, "wgl", 3) != 0) {
1813 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1814 } else {
1815 TRACE("('%s'):%*s", lpszProc, padding, " ");
1816 for (i = 0; i < WineGLExtensionListSize; ++i) {
1817 ext = WineGLExtensionList[i];
1818 for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1819 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1820 TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1821 return ext->extEntryPoints[j].funcAddress;
1827 WARN("(%s) - not found\n", lpszProc);
1828 return NULL;
1831 static GLXPixmap get_context_pixmap( struct glx_physdev *physdev, struct wine_glcontext *ctx )
1833 HBITMAP bitmap = GetCurrentObject( physdev->dev.hdc, OBJ_BITMAP );
1835 if (physdev->x11dev->bitmap->hbitmap == bitmap) return physdev->x11dev->bitmap->glxpixmap;
1837 /* otherwise we have a DIB section */
1839 if (!ctx->pixmap)
1841 BITMAP bmp;
1843 GetObjectW( bitmap, sizeof(bmp), &bmp );
1845 wine_tsx11_lock();
1846 ctx->pixmap = XCreatePixmap( gdi_display, root_window,
1847 bmp.bmWidth, bmp.bmHeight, ctx->vis->depth );
1848 ctx->glxpixmap = pglXCreateGLXPixmap( gdi_display, ctx->vis, ctx->pixmap );
1849 wine_tsx11_unlock();
1850 ctx->pixmap_size.cx = bmp.bmWidth;
1851 ctx->pixmap_size.cy = bmp.bmHeight;
1853 return ctx->glxpixmap;
1857 * glxdrv_wglMakeCurrent
1859 * For OpenGL32 wglMakeCurrent.
1861 static BOOL glxdrv_wglMakeCurrent(PHYSDEV dev, HGLRC hglrc)
1863 struct glx_physdev *physdev = get_glxdrv_dev( dev );
1864 BOOL ret;
1865 Wine_GLContext *prev_ctx = NtCurrentTeb()->glContext;
1866 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1868 TRACE("(%p,%p)\n", dev->hdc, hglrc);
1870 if (!has_opengl()) return FALSE;
1872 if (hglrc == NULL)
1874 if (prev_ctx) prev_ctx->tid = 0;
1876 wine_tsx11_lock();
1877 ret = pglXMakeCurrent(gdi_display, None, NULL);
1878 wine_tsx11_unlock();
1879 NtCurrentTeb()->glContext = NULL;
1881 else if (!physdev->pixel_format)
1883 WARN("Trying to use an invalid drawable\n");
1884 SetLastError(ERROR_INVALID_HANDLE);
1885 return FALSE;
1887 else if (ctx->fmt->iPixelFormat != physdev->pixel_format)
1889 WARN( "mismatched pixel format hdc %p %u ctx %p %u\n",
1890 dev->hdc, physdev->pixel_format, ctx, ctx->fmt->iPixelFormat );
1891 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1892 return FALSE;
1894 else
1896 Drawable drawable = physdev->drawable;
1898 if (physdev->type == DC_GL_BITMAP) drawable = get_context_pixmap( physdev, ctx );
1900 wine_tsx11_lock();
1902 if (TRACE_ON(wgl)) {
1903 describeDrawable( physdev );
1904 describeContext(ctx);
1907 TRACE(" make current for drawable %lx, ctx %p\n", drawable, ctx->ctx);
1909 ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1911 if (ret)
1913 if (prev_ctx) prev_ctx->tid = 0;
1914 NtCurrentTeb()->glContext = ctx;
1916 ctx->has_been_current = TRUE;
1917 ctx->tid = GetCurrentThreadId();
1918 ctx->hdc = dev->hdc;
1919 ctx->read_hdc = dev->hdc;
1920 ctx->drawables[0] = drawable;
1921 ctx->drawables[1] = drawable;
1922 ctx->refresh_drawables = FALSE;
1924 if (physdev->type == DC_GL_BITMAP) pglDrawBuffer(GL_FRONT_LEFT);
1926 else
1927 SetLastError(ERROR_INVALID_HANDLE);
1928 wine_tsx11_unlock();
1930 TRACE(" returning %s\n", (ret ? "True" : "False"));
1931 return ret;
1935 * glxdrv_wglMakeContextCurrentARB
1937 * For OpenGL32 wglMakeContextCurrentARB
1939 static BOOL glxdrv_wglMakeContextCurrentARB( PHYSDEV draw_dev, PHYSDEV read_dev, HGLRC hglrc )
1941 Wine_GLContext *prev_ctx = NtCurrentTeb()->glContext;
1942 struct glx_physdev *draw_physdev = get_glxdrv_dev( draw_dev );
1943 struct glx_physdev *read_physdev = get_glxdrv_dev( read_dev );
1944 BOOL ret;
1946 TRACE("(%p,%p,%p)\n", draw_dev->hdc, read_dev->hdc, hglrc);
1948 if (!has_opengl()) return 0;
1950 if (hglrc == NULL)
1952 if (prev_ctx) prev_ctx->tid = 0;
1954 wine_tsx11_lock();
1955 ret = pglXMakeCurrent(gdi_display, None, NULL);
1956 wine_tsx11_unlock();
1957 NtCurrentTeb()->glContext = NULL;
1959 else if (!draw_physdev->pixel_format)
1961 WARN("Trying to use an invalid drawable\n");
1962 SetLastError(ERROR_INVALID_HANDLE);
1963 return FALSE;
1965 else
1967 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1968 Drawable draw_drawable = draw_physdev->drawable;
1969 Drawable read_drawable = read_physdev->drawable;
1971 if (!pglXMakeContextCurrent) return FALSE;
1973 if (draw_physdev->type == DC_GL_BITMAP) draw_drawable = get_context_pixmap( draw_physdev, ctx );
1974 if (read_physdev->type == DC_GL_BITMAP) read_drawable = get_context_pixmap( read_physdev, ctx );
1976 wine_tsx11_lock();
1977 ret = pglXMakeContextCurrent(gdi_display, draw_drawable, read_drawable, ctx->ctx);
1978 if (ret)
1980 if (prev_ctx) prev_ctx->tid = 0;
1982 ctx->has_been_current = TRUE;
1983 ctx->tid = GetCurrentThreadId();
1984 ctx->hdc = draw_dev->hdc;
1985 ctx->read_hdc = read_dev->hdc;
1986 ctx->drawables[0] = draw_drawable;
1987 ctx->drawables[1] = read_drawable;
1988 ctx->refresh_drawables = FALSE;
1989 NtCurrentTeb()->glContext = ctx;
1991 else
1992 SetLastError(ERROR_INVALID_HANDLE);
1993 wine_tsx11_unlock();
1996 TRACE(" returning %s\n", (ret ? "True" : "False"));
1997 return ret;
2001 * glxdrv_wglShareLists
2003 * For OpenGL32 wglShareLists.
2005 static BOOL glxdrv_wglShareLists(HGLRC hglrc1, HGLRC hglrc2)
2007 Wine_GLContext *org = (Wine_GLContext *) hglrc1;
2008 Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
2010 TRACE("(%p, %p)\n", org, dest);
2012 if (!has_opengl()) return FALSE;
2014 /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
2015 * at context creation time but in case of WGL it is done using wglShareLists.
2016 * In the past we tried to emulate wglShareLists by delaying GLX context creation until
2017 * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
2018 * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
2019 * so there delaying context creation doesn't work.
2021 * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
2022 * and when a program requests sharing we recreate the destination context if it hasn't been made
2023 * current or when it hasn't shared display lists before.
2026 if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
2028 ERR("Could not share display lists, one of the contexts has been current already !\n");
2029 return FALSE;
2031 else if(dest->sharing)
2033 ERR("Could not share display lists because hglrc2 has already shared lists before\n");
2034 return FALSE;
2036 else
2038 if((GetObjectType(org->hdc) == OBJ_MEMDC) ^ (GetObjectType(dest->hdc) == OBJ_MEMDC))
2040 WARN("Attempting to share a context between a direct and indirect rendering context, expect issues!\n");
2043 wine_tsx11_lock();
2044 describeContext(org);
2045 describeContext(dest);
2047 /* Re-create the GLX context and share display lists */
2048 pglXDestroyContext(gdi_display, dest->ctx);
2049 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
2050 wine_tsx11_unlock();
2051 TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
2053 org->sharing = TRUE;
2054 dest->sharing = TRUE;
2055 return TRUE;
2057 return FALSE;
2060 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
2062 /* We are running using client-side rendering fonts... */
2063 GLYPHMETRICS gm;
2064 unsigned int glyph, size = 0;
2065 void *bitmap = NULL, *gl_bitmap = NULL;
2066 int org_alignment;
2068 wine_tsx11_lock();
2069 pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
2070 pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2071 wine_tsx11_unlock();
2073 for (glyph = first; glyph < first + count; glyph++) {
2074 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
2075 unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
2076 unsigned int height, width_int;
2078 TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
2079 if (needed_size == GDI_ERROR) {
2080 TRACE(" - needed size : %d (GDI_ERROR)\n", needed_size);
2081 goto error;
2082 } else {
2083 TRACE(" - needed size : %d\n", needed_size);
2086 if (needed_size > size) {
2087 size = needed_size;
2088 HeapFree(GetProcessHeap(), 0, bitmap);
2089 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2090 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
2091 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
2093 if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) == GDI_ERROR)
2094 goto error;
2095 if (TRACE_ON(wgl)) {
2096 unsigned int height, width, bitmask;
2097 unsigned char *bitmap_ = bitmap;
2099 TRACE(" - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
2100 TRACE(" - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
2101 TRACE(" - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
2102 if (needed_size != 0) {
2103 TRACE(" - bitmap :\n");
2104 for (height = 0; height < gm.gmBlackBoxY; height++) {
2105 TRACE(" ");
2106 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
2107 if (bitmask == 0) {
2108 bitmap_ += 1;
2109 bitmask = 0x80;
2111 if (*bitmap_ & bitmask)
2112 TRACE("*");
2113 else
2114 TRACE(" ");
2116 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
2117 TRACE("\n");
2122 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
2123 * glyph for it to be drawn properly.
2125 if (needed_size != 0) {
2126 width_int = (gm.gmBlackBoxX + 31) / 32;
2127 for (height = 0; height < gm.gmBlackBoxY; height++) {
2128 unsigned int width;
2129 for (width = 0; width < width_int; width++) {
2130 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
2131 ((int *) bitmap)[height * width_int + width];
2136 wine_tsx11_lock();
2137 pglNewList(listBase++, GL_COMPILE);
2138 if (needed_size != 0) {
2139 pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
2140 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
2141 gm.gmCellIncX, gm.gmCellIncY,
2142 gl_bitmap);
2143 } else {
2144 /* This is the case of 'empty' glyphs like the space character */
2145 pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
2147 pglEndList();
2148 wine_tsx11_unlock();
2151 wine_tsx11_lock();
2152 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2153 wine_tsx11_unlock();
2155 HeapFree(GetProcessHeap(), 0, bitmap);
2156 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2157 return TRUE;
2159 error:
2160 wine_tsx11_lock();
2161 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2162 wine_tsx11_unlock();
2164 HeapFree(GetProcessHeap(), 0, bitmap);
2165 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2166 return FALSE;
2170 * glxdrv_wglUseFontBitmapsA
2172 * For OpenGL32 wglUseFontBitmapsA.
2174 static BOOL glxdrv_wglUseFontBitmapsA(PHYSDEV dev, DWORD first, DWORD count, DWORD listBase)
2176 TRACE("(%p, %d, %d, %d)\n", dev->hdc, first, count, listBase);
2178 if (!has_opengl()) return FALSE;
2179 return internal_wglUseFontBitmaps(dev->hdc, first, count, listBase, GetGlyphOutlineA);
2183 * glxdrv_wglUseFontBitmapsW
2185 * For OpenGL32 wglUseFontBitmapsW.
2187 static BOOL glxdrv_wglUseFontBitmapsW(PHYSDEV dev, DWORD first, DWORD count, DWORD listBase)
2189 TRACE("(%p, %d, %d, %d)\n", dev->hdc, first, count, listBase);
2191 if (!has_opengl()) return FALSE;
2192 return internal_wglUseFontBitmaps(dev->hdc, first, count, listBase, GetGlyphOutlineW);
2195 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
2196 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
2198 wine_tsx11_lock();
2199 switch(pname)
2201 case GL_DEPTH_BITS:
2203 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2205 pglGetIntegerv(pname, params);
2207 * if we cannot find a Wine Context
2208 * we only have the default wine desktop context,
2209 * so if we have only a 24 depth say we have 32
2211 if (!ctx && *params == 24) {
2212 *params = 32;
2214 TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
2215 break;
2217 case GL_ALPHA_BITS:
2219 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2221 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_ALPHA_SIZE, params);
2222 TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
2223 break;
2225 default:
2226 pglGetIntegerv(pname, params);
2227 break;
2229 wine_tsx11_unlock();
2232 static void flush_pixmap( struct wine_glcontext *ctx )
2234 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2235 BITMAPINFO *info = (BITMAPINFO *)buffer;
2236 XImage *image;
2237 struct gdi_image_bits src_bits, bits;
2238 struct bitblt_coords coords;
2239 DWORD *masks;
2240 BOOL is_r8g8b8 = FALSE;
2241 const XPixmapFormatValues *format = pixmap_formats[ctx->vis->depth];
2243 coords.x = 0;
2244 coords.y = 0;
2245 coords.width = ctx->pixmap_size.cx;
2246 coords.height = ctx->pixmap_size.cy;
2247 SetRect( &coords.visrect, 0, 0, coords.width, coords.height );
2249 info->bmiHeader.biSize = sizeof(info->bmiHeader);
2250 info->bmiHeader.biWidth = coords.width;
2251 info->bmiHeader.biHeight = -coords.height;
2252 info->bmiHeader.biPlanes = 1;
2253 info->bmiHeader.biBitCount = format->bits_per_pixel;
2254 info->bmiHeader.biCompression = BI_RGB;
2255 info->bmiHeader.biClrUsed = 0;
2256 switch (info->bmiHeader.biBitCount)
2258 case 16:
2259 masks = (DWORD *)info->bmiColors;
2260 masks[0] = ctx->vis->red_mask;
2261 masks[1] = ctx->vis->green_mask;
2262 masks[2] = ctx->vis->blue_mask;
2263 info->bmiHeader.biCompression = BI_BITFIELDS;
2264 break;
2265 case 24:
2266 is_r8g8b8 = (ctx->vis->red_mask == 0xff0000 && ctx->vis->blue_mask == 0x0000ff);
2267 break;
2268 case 32:
2269 masks = (DWORD *)info->bmiColors;
2270 masks[0] = ctx->vis->red_mask;
2271 masks[1] = ctx->vis->green_mask;
2272 masks[2] = ctx->vis->blue_mask;
2273 if (masks[0] != 0xff0000 || masks[1] != 0x00ff00 || masks[2] != 0x0000ff)
2274 info->bmiHeader.biCompression = BI_BITFIELDS;
2275 break;
2276 default:
2277 FIXME( "depth %u not supported\n", info->bmiHeader.biBitCount );
2278 return;
2281 wine_tsx11_lock();
2282 image = XGetImage( gdi_display, ctx->pixmap, 0, 0, coords.width, coords.height, AllPlanes, ZPixmap );
2283 wine_tsx11_unlock();
2284 if (!image) return;
2286 src_bits.ptr = image->data;
2287 src_bits.is_copy = TRUE;
2288 if (!copy_image_bits( info, is_r8g8b8, image, &src_bits, &bits, &coords, NULL, ~0u ))
2290 HBITMAP bitmap = GetCurrentObject( ctx->hdc, OBJ_BITMAP );
2291 TRACE( "drawable %lx -> hdc %p bitmap %p\n", ctx->pixmap, ctx->hdc, bitmap );
2292 SetDIBits( 0, bitmap, 0, coords.height, bits.ptr, info, DIB_RGB_COLORS );
2293 if (bits.free) bits.free( &bits );
2295 wine_tsx11_lock();
2296 XDestroyImage( image );
2297 wine_tsx11_unlock();
2300 static void flush_gl_drawable( struct glx_physdev *physdev )
2302 RECT rect;
2303 int w = physdev->x11dev->dc_rect.right - physdev->x11dev->dc_rect.left;
2304 int h = physdev->x11dev->dc_rect.bottom - physdev->x11dev->dc_rect.top;
2305 Drawable src = physdev->drawable;
2307 if (w <= 0 || h <= 0) return;
2309 switch (physdev->type)
2311 case DC_GL_PIXMAP_WIN:
2312 src = physdev->pixmap;
2313 /* fall through */
2314 case DC_GL_CHILD_WIN:
2315 /* The GL drawable may be lagged behind if we don't flush first, so
2316 * flush the display make sure we copy up-to-date data */
2317 wine_tsx11_lock();
2318 XFlush(gdi_display);
2319 XSetFunction(gdi_display, physdev->x11dev->gc, GXcopy);
2320 XCopyArea(gdi_display, src, physdev->x11dev->drawable, physdev->x11dev->gc, 0, 0, w, h,
2321 physdev->x11dev->dc_rect.left, physdev->x11dev->dc_rect.top);
2322 wine_tsx11_unlock();
2323 SetRect( &rect, 0, 0, w, h );
2324 add_device_bounds( physdev->x11dev, &rect );
2325 default:
2326 break;
2331 static void WINAPI X11DRV_wglFinish(void)
2333 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2334 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2336 wine_tsx11_lock();
2337 sync_context(ctx);
2338 pglFinish();
2339 wine_tsx11_unlock();
2340 if (ctx)
2342 if (ctx->pixmap) flush_pixmap( ctx );
2343 else ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2347 static void WINAPI X11DRV_wglFlush(void)
2349 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2350 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2352 wine_tsx11_lock();
2353 sync_context(ctx);
2354 pglFlush();
2355 wine_tsx11_unlock();
2356 if (ctx)
2358 if (ctx->pixmap) flush_pixmap( ctx );
2359 else ExtEscape( ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2364 * glxdrv_wglCreateContextAttribsARB
2366 * WGL_ARB_create_context: wglCreateContextAttribsARB
2368 static HGLRC glxdrv_wglCreateContextAttribsARB(PHYSDEV dev, HGLRC hShareContext, const int* attribList)
2370 struct glx_physdev *physdev = get_glxdrv_dev( dev );
2371 Wine_GLContext *ret;
2372 WineGLPixelFormat *fmt;
2373 int fmt_count = 0;
2375 TRACE("(%p %p %p)\n", dev->hdc, hShareContext, attribList);
2377 if (!has_opengl()) return 0;
2379 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physdev->pixel_format, TRUE /* Offscreen */, &fmt_count);
2380 /* wglCreateContextAttribsARB supports ALL pixel formats, so also offscreen ones.
2381 * If this fails something is very wrong on the system. */
2382 if(!fmt)
2384 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", physdev->pixel_format);
2385 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2386 return NULL;
2389 if (!(ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret)))) return 0;
2391 ret->hdc = dev->hdc;
2392 ret->fmt = fmt;
2393 ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2394 ret->gl3_context = TRUE;
2396 ret->numAttribs = 0;
2397 if(attribList)
2399 int *pAttribList = (int*)attribList;
2400 int *pContextAttribList = &ret->attribList[0];
2401 /* attribList consists of pairs {token, value] terminated with 0 */
2402 while(pAttribList[0] != 0)
2404 TRACE("%#x %#x\n", pAttribList[0], pAttribList[1]);
2405 switch(pAttribList[0])
2407 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2408 pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2409 pContextAttribList[1] = pAttribList[1];
2410 break;
2411 case WGL_CONTEXT_MINOR_VERSION_ARB:
2412 pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2413 pContextAttribList[1] = pAttribList[1];
2414 break;
2415 case WGL_CONTEXT_LAYER_PLANE_ARB:
2416 break;
2417 case WGL_CONTEXT_FLAGS_ARB:
2418 pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2419 pContextAttribList[1] = pAttribList[1];
2420 break;
2421 case WGL_CONTEXT_PROFILE_MASK_ARB:
2422 pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2423 pContextAttribList[1] = pAttribList[1];
2424 break;
2425 default:
2426 ERR("Unhandled attribList pair: %#x %#x\n", pAttribList[0], pAttribList[1]);
2429 ret->numAttribs++;
2430 pAttribList += 2;
2431 pContextAttribList += 2;
2435 wine_tsx11_lock();
2436 X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2437 ret->ctx = create_glxcontext(gdi_display, ret, NULL);
2439 XSync(gdi_display, False);
2440 if(X11DRV_check_error() || !ret->ctx)
2442 /* In the future we should convert the GLX error to a win32 one here if needed */
2443 ERR("Context creation failed\n");
2444 HeapFree( GetProcessHeap(), 0, ret );
2445 wine_tsx11_unlock();
2446 return NULL;
2449 list_add_head( &context_list, &ret->entry );
2450 wine_tsx11_unlock();
2451 TRACE(" creating context %p\n", ret);
2452 return (HGLRC) ret;
2456 * X11DRV_wglGetExtensionsStringARB
2458 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2460 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
2461 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2462 return WineGLInfo.wglExtensions;
2466 * X11DRV_wglCreatePbufferARB
2468 * WGL_ARB_pbuffer: wglCreatePbufferARB
2470 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
2472 Wine_GLPBuffer* object = NULL;
2473 WineGLPixelFormat *fmt = NULL;
2474 int nCfgs = 0;
2475 int attribs[256];
2476 int nAttribs = 0;
2478 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2480 if (0 >= iPixelFormat) {
2481 ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2482 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2483 return NULL; /* unexpected error */
2486 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2487 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2488 if(!fmt) {
2489 ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2490 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2491 goto create_failed; /* unexpected error */
2494 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2495 if (NULL == object) {
2496 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2497 goto create_failed; /* unexpected error */
2499 object->hdc = hdc;
2500 object->display = gdi_display;
2501 object->width = iWidth;
2502 object->height = iHeight;
2503 object->fmt = fmt;
2505 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2506 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2507 while (piAttribList && 0 != *piAttribList) {
2508 int attr_v;
2509 switch (*piAttribList) {
2510 case WGL_PBUFFER_LARGEST_ARB: {
2511 ++piAttribList;
2512 attr_v = *piAttribList;
2513 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2514 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2515 break;
2518 case WGL_TEXTURE_FORMAT_ARB: {
2519 ++piAttribList;
2520 attr_v = *piAttribList;
2521 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2522 if (use_render_texture_ati) {
2523 int type = 0;
2524 switch (attr_v) {
2525 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2526 case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2527 case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2528 default:
2529 SetLastError(ERROR_INVALID_DATA);
2530 goto create_failed;
2532 object->use_render_texture = 1;
2533 PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2534 } else {
2535 if (WGL_NO_TEXTURE_ARB == attr_v) {
2536 object->use_render_texture = 0;
2537 } else {
2538 if (!use_render_texture_emulation) {
2539 SetLastError(ERROR_INVALID_DATA);
2540 goto create_failed;
2542 switch (attr_v) {
2543 case WGL_TEXTURE_RGB_ARB:
2544 object->use_render_texture = GL_RGB;
2545 object->texture_bpp = 3;
2546 object->texture_format = GL_RGB;
2547 object->texture_type = GL_UNSIGNED_BYTE;
2548 break;
2549 case WGL_TEXTURE_RGBA_ARB:
2550 object->use_render_texture = GL_RGBA;
2551 object->texture_bpp = 4;
2552 object->texture_format = GL_RGBA;
2553 object->texture_type = GL_UNSIGNED_BYTE;
2554 break;
2556 /* WGL_FLOAT_COMPONENTS_NV */
2557 case WGL_TEXTURE_FLOAT_R_NV:
2558 object->use_render_texture = GL_FLOAT_R_NV;
2559 object->texture_bpp = 4;
2560 object->texture_format = GL_RED;
2561 object->texture_type = GL_FLOAT;
2562 break;
2563 case WGL_TEXTURE_FLOAT_RG_NV:
2564 object->use_render_texture = GL_FLOAT_RG_NV;
2565 object->texture_bpp = 8;
2566 object->texture_format = GL_LUMINANCE_ALPHA;
2567 object->texture_type = GL_FLOAT;
2568 break;
2569 case WGL_TEXTURE_FLOAT_RGB_NV:
2570 object->use_render_texture = GL_FLOAT_RGB_NV;
2571 object->texture_bpp = 12;
2572 object->texture_format = GL_RGB;
2573 object->texture_type = GL_FLOAT;
2574 break;
2575 case WGL_TEXTURE_FLOAT_RGBA_NV:
2576 object->use_render_texture = GL_FLOAT_RGBA_NV;
2577 object->texture_bpp = 16;
2578 object->texture_format = GL_RGBA;
2579 object->texture_type = GL_FLOAT;
2580 break;
2581 default:
2582 ERR("Unknown texture format: %x\n", attr_v);
2583 SetLastError(ERROR_INVALID_DATA);
2584 goto create_failed;
2588 break;
2591 case WGL_TEXTURE_TARGET_ARB: {
2592 ++piAttribList;
2593 attr_v = *piAttribList;
2594 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2595 if (use_render_texture_ati) {
2596 int type = 0;
2597 switch (attr_v) {
2598 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2599 case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2600 case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2601 case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2602 default:
2603 SetLastError(ERROR_INVALID_DATA);
2604 goto create_failed;
2606 PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2607 } else {
2608 if (WGL_NO_TEXTURE_ARB == attr_v) {
2609 object->texture_target = 0;
2610 } else {
2611 if (!use_render_texture_emulation) {
2612 SetLastError(ERROR_INVALID_DATA);
2613 goto create_failed;
2615 switch (attr_v) {
2616 case WGL_TEXTURE_CUBE_MAP_ARB: {
2617 if (iWidth != iHeight) {
2618 SetLastError(ERROR_INVALID_DATA);
2619 goto create_failed;
2621 object->texture_target = GL_TEXTURE_CUBE_MAP;
2622 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2623 break;
2625 case WGL_TEXTURE_1D_ARB: {
2626 if (1 != iHeight) {
2627 SetLastError(ERROR_INVALID_DATA);
2628 goto create_failed;
2630 object->texture_target = GL_TEXTURE_1D;
2631 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2632 break;
2634 case WGL_TEXTURE_2D_ARB: {
2635 object->texture_target = GL_TEXTURE_2D;
2636 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2637 break;
2639 case WGL_TEXTURE_RECTANGLE_NV: {
2640 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2641 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2642 break;
2644 default:
2645 ERR("Unknown texture target: %x\n", attr_v);
2646 SetLastError(ERROR_INVALID_DATA);
2647 goto create_failed;
2651 break;
2654 case WGL_MIPMAP_TEXTURE_ARB: {
2655 ++piAttribList;
2656 attr_v = *piAttribList;
2657 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2658 if (use_render_texture_ati) {
2659 PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2660 } else {
2661 if (!use_render_texture_emulation) {
2662 SetLastError(ERROR_INVALID_DATA);
2663 goto create_failed;
2666 break;
2669 ++piAttribList;
2672 PUSH1(attribs, None);
2673 wine_tsx11_lock();
2674 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2675 wine_tsx11_unlock();
2676 TRACE("new Pbuffer drawable as %lx\n", object->drawable);
2677 if (!object->drawable) {
2678 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2679 goto create_failed; /* unexpected error */
2681 TRACE("->(%p)\n", object);
2682 return object;
2684 create_failed:
2685 HeapFree(GetProcessHeap(), 0, object);
2686 TRACE("->(FAILED)\n");
2687 return NULL;
2691 * X11DRV_wglDestroyPbufferARB
2693 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2695 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2697 Wine_GLPBuffer* object = hPbuffer;
2698 TRACE("(%p)\n", hPbuffer);
2699 if (NULL == object) {
2700 SetLastError(ERROR_INVALID_HANDLE);
2701 return GL_FALSE;
2703 wine_tsx11_lock();
2704 pglXDestroyPbuffer(object->display, object->drawable);
2705 wine_tsx11_unlock();
2706 HeapFree(GetProcessHeap(), 0, object);
2707 return GL_TRUE;
2711 * X11DRV_wglGetPbufferDCARB
2713 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2715 static HDC WINAPI X11DRV_wglGetPbufferDCARB(HPBUFFERARB hPbuffer)
2717 struct x11drv_escape_set_drawable escape;
2718 Wine_GLPBuffer* object = hPbuffer;
2719 HDC hdc;
2721 if (NULL == object) {
2722 SetLastError(ERROR_INVALID_HANDLE);
2723 return NULL;
2726 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
2727 if (!hdc) return 0;
2729 escape.code = X11DRV_SET_DRAWABLE;
2730 escape.drawable = object->drawable;
2731 escape.mode = IncludeInferiors;
2732 SetRect( &escape.drawable_rect, 0, 0, object->width, object->height );
2733 escape.dc_rect = escape.drawable_rect;
2734 escape.fbconfig_id = object->fmt->fmt_id;
2735 escape.gl_drawable = object->drawable;
2736 escape.pixmap = 0;
2737 escape.gl_type = DC_GL_PBUFFER;
2738 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2740 TRACE( "(%p)->(%p)\n", hPbuffer, hdc );
2741 return hdc;
2745 * X11DRV_wglQueryPbufferARB
2747 * WGL_ARB_pbuffer: wglQueryPbufferARB
2749 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2751 Wine_GLPBuffer* object = hPbuffer;
2752 TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2753 if (NULL == object) {
2754 SetLastError(ERROR_INVALID_HANDLE);
2755 return GL_FALSE;
2757 switch (iAttribute) {
2758 case WGL_PBUFFER_WIDTH_ARB:
2759 wine_tsx11_lock();
2760 pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2761 wine_tsx11_unlock();
2762 break;
2763 case WGL_PBUFFER_HEIGHT_ARB:
2764 wine_tsx11_lock();
2765 pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2766 wine_tsx11_unlock();
2767 break;
2769 case WGL_PBUFFER_LOST_ARB:
2770 /* GLX Pbuffers cannot be lost by default. We can support this by
2771 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2772 * to receive pixel buffer clobber events, however that may or may
2773 * not give any benefit */
2774 *piValue = GL_FALSE;
2775 break;
2777 case WGL_TEXTURE_FORMAT_ARB:
2778 if (use_render_texture_ati) {
2779 unsigned int tmp;
2780 int type = WGL_NO_TEXTURE_ARB;
2781 wine_tsx11_lock();
2782 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2783 wine_tsx11_unlock();
2784 switch (tmp) {
2785 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2786 case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2787 case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2789 *piValue = type;
2790 } else {
2791 if (!object->use_render_texture) {
2792 *piValue = WGL_NO_TEXTURE_ARB;
2793 } else {
2794 if (!use_render_texture_emulation) {
2795 SetLastError(ERROR_INVALID_HANDLE);
2796 return GL_FALSE;
2798 switch(object->use_render_texture) {
2799 case GL_RGB:
2800 *piValue = WGL_TEXTURE_RGB_ARB;
2801 break;
2802 case GL_RGBA:
2803 *piValue = WGL_TEXTURE_RGBA_ARB;
2804 break;
2805 /* WGL_FLOAT_COMPONENTS_NV */
2806 case GL_FLOAT_R_NV:
2807 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2808 break;
2809 case GL_FLOAT_RG_NV:
2810 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2811 break;
2812 case GL_FLOAT_RGB_NV:
2813 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2814 break;
2815 case GL_FLOAT_RGBA_NV:
2816 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2817 break;
2818 default:
2819 ERR("Unknown texture format: %x\n", object->use_render_texture);
2823 break;
2825 case WGL_TEXTURE_TARGET_ARB:
2826 if (use_render_texture_ati) {
2827 unsigned int tmp;
2828 int type = WGL_NO_TEXTURE_ARB;
2829 wine_tsx11_lock();
2830 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2831 wine_tsx11_unlock();
2832 switch (tmp) {
2833 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2834 case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2835 case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2836 case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2838 *piValue = type;
2839 } else {
2840 if (!object->texture_target) {
2841 *piValue = WGL_NO_TEXTURE_ARB;
2842 } else {
2843 if (!use_render_texture_emulation) {
2844 SetLastError(ERROR_INVALID_DATA);
2845 return GL_FALSE;
2847 switch (object->texture_target) {
2848 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2849 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2850 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2851 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2855 break;
2857 case WGL_MIPMAP_TEXTURE_ARB:
2858 if (use_render_texture_ati) {
2859 wine_tsx11_lock();
2860 pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2861 wine_tsx11_unlock();
2862 } else {
2863 *piValue = GL_FALSE; /** don't support that */
2864 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2866 break;
2868 default:
2869 FIXME("unexpected attribute %x\n", iAttribute);
2870 break;
2873 return GL_TRUE;
2877 * X11DRV_wglReleasePbufferDCARB
2879 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2881 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2883 TRACE("(%p, %p)\n", hPbuffer, hdc);
2884 return DeleteDC(hdc);
2888 * X11DRV_wglSetPbufferAttribARB
2890 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2892 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2894 Wine_GLPBuffer* object = hPbuffer;
2895 GLboolean ret = GL_FALSE;
2897 WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2898 if (NULL == object) {
2899 SetLastError(ERROR_INVALID_HANDLE);
2900 return GL_FALSE;
2902 if (!object->use_render_texture) {
2903 SetLastError(ERROR_INVALID_HANDLE);
2904 return GL_FALSE;
2906 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2907 return GL_TRUE;
2909 if (NULL != pglXDrawableAttribATI) {
2910 if (use_render_texture_ati) {
2911 FIXME("Need conversion for GLX_ATI_render_texture\n");
2913 wine_tsx11_lock();
2914 ret = pglXDrawableAttribATI(object->display, object->drawable, piAttribList);
2915 wine_tsx11_unlock();
2917 return ret;
2921 * X11DRV_wglChoosePixelFormatARB
2923 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2925 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2927 int gl_test = 0;
2928 int attribs[256];
2929 int nAttribs = 0;
2930 GLXFBConfig* cfgs = NULL;
2931 int nCfgs = 0;
2932 int it;
2933 int fmt_id;
2934 WineGLPixelFormat *fmt;
2935 UINT pfmt_it = 0;
2936 int run;
2937 int i;
2938 DWORD dwFlags = 0;
2940 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2941 if (NULL != pfAttribFList) {
2942 FIXME("unused pfAttribFList\n");
2945 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2946 if (-1 == nAttribs) {
2947 WARN("Cannot convert WGL to GLX attributes\n");
2948 return GL_FALSE;
2950 PUSH1(attribs, None);
2952 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2953 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the WineGLPixelFormat. We don't query these attributes
2954 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on by passing a dwFlags to 'ConvertPixelFormatGLXtoWGL'. */
2955 for(i=0; piAttribIList[i] != 0; i+=2)
2957 switch(piAttribIList[i])
2959 case WGL_DRAW_TO_BITMAP_ARB:
2960 if(piAttribIList[i+1])
2961 dwFlags |= PFD_DRAW_TO_BITMAP;
2962 break;
2963 case WGL_ACCELERATION_ARB:
2964 switch(piAttribIList[i+1])
2966 case WGL_NO_ACCELERATION_ARB:
2967 dwFlags |= PFD_GENERIC_FORMAT;
2968 break;
2969 case WGL_GENERIC_ACCELERATION_ARB:
2970 dwFlags |= PFD_GENERIC_ACCELERATED;
2971 break;
2972 case WGL_FULL_ACCELERATION_ARB:
2973 /* Nothing to do */
2974 break;
2976 break;
2977 case WGL_SUPPORT_GDI_ARB:
2978 if(piAttribIList[i+1])
2979 dwFlags |= PFD_SUPPORT_GDI;
2980 break;
2984 /* Search for FB configurations matching the requirements in attribs */
2985 wine_tsx11_lock();
2986 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2987 if (NULL == cfgs) {
2988 wine_tsx11_unlock();
2989 WARN("Compatible Pixel Format not found\n");
2990 return GL_FALSE;
2993 /* Loop through all matching formats and check if they are suitable.
2994 * Note that this function should at max return nMaxFormats different formats */
2995 for(run=0; run < 2; run++)
2997 for (it = 0; it < nCfgs; ++it) {
2998 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2999 if (gl_test) {
3000 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
3001 continue;
3004 /* Search for the format in our list of compatible formats */
3005 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id, dwFlags);
3006 if(!fmt)
3007 continue;
3009 /* During the first run we only want onscreen formats and during the second only offscreen 'XOR' */
3010 if( ((run == 0) && fmt->offscreenOnly) || ((run == 1) && !fmt->offscreenOnly) )
3011 continue;
3013 if(pfmt_it < nMaxFormats) {
3014 piFormats[pfmt_it] = fmt->iPixelFormat;
3015 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
3017 pfmt_it++;
3021 *nNumFormats = pfmt_it;
3022 /** free list */
3023 XFree(cfgs);
3024 wine_tsx11_unlock();
3025 return GL_TRUE;
3029 * X11DRV_wglGetPixelFormatAttribivARB
3031 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
3033 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
3035 UINT i;
3036 WineGLPixelFormat *fmt = NULL;
3037 int hTest;
3038 int tmp;
3039 int curGLXAttr = 0;
3040 int nWGLFormats = 0;
3042 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
3044 if (0 < iLayerPlane) {
3045 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
3046 return GL_FALSE;
3049 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
3050 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
3051 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
3052 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
3053 if(!fmt) {
3054 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
3057 wine_tsx11_lock();
3058 for (i = 0; i < nAttributes; ++i) {
3059 const int curWGLAttr = piAttributes[i];
3060 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
3062 switch (curWGLAttr) {
3063 case WGL_NUMBER_PIXEL_FORMATS_ARB:
3064 piValues[i] = nWGLFormats;
3065 continue;
3067 case WGL_SUPPORT_OPENGL_ARB:
3068 piValues[i] = GL_TRUE;
3069 continue;
3071 case WGL_ACCELERATION_ARB:
3072 curGLXAttr = GLX_CONFIG_CAVEAT;
3073 if (!fmt) goto pix_error;
3074 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
3075 piValues[i] = WGL_NO_ACCELERATION_ARB;
3076 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
3077 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
3078 else
3079 piValues[i] = WGL_FULL_ACCELERATION_ARB;
3080 continue;
3082 case WGL_TRANSPARENT_ARB:
3083 curGLXAttr = GLX_TRANSPARENT_TYPE;
3084 if (!fmt) goto pix_error;
3085 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
3086 if (hTest) goto get_error;
3087 piValues[i] = GL_FALSE;
3088 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
3089 continue;
3091 case WGL_PIXEL_TYPE_ARB:
3092 curGLXAttr = GLX_RENDER_TYPE;
3093 if (!fmt) goto pix_error;
3094 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
3095 if (hTest) goto get_error;
3096 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
3097 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
3098 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
3099 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
3100 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
3101 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
3102 else {
3103 ERR("unexpected RenderType(%x)\n", tmp);
3104 piValues[i] = WGL_TYPE_RGBA_ARB;
3106 continue;
3108 case WGL_COLOR_BITS_ARB:
3109 curGLXAttr = GLX_BUFFER_SIZE;
3110 break;
3112 case WGL_BIND_TO_TEXTURE_RGB_ARB:
3113 if (use_render_texture_ati) {
3114 curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
3115 break;
3117 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
3118 if (use_render_texture_ati) {
3119 curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
3120 break;
3122 if (!use_render_texture_emulation) {
3123 piValues[i] = GL_FALSE;
3124 continue;
3126 curGLXAttr = GLX_RENDER_TYPE;
3127 if (!fmt) goto pix_error;
3128 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
3129 if (hTest) goto get_error;
3130 if (GLX_COLOR_INDEX_BIT == tmp) {
3131 piValues[i] = GL_FALSE;
3132 continue;
3134 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
3135 if (hTest) goto get_error;
3136 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
3137 continue;
3139 case WGL_BLUE_BITS_ARB:
3140 curGLXAttr = GLX_BLUE_SIZE;
3141 break;
3142 case WGL_RED_BITS_ARB:
3143 curGLXAttr = GLX_RED_SIZE;
3144 break;
3145 case WGL_GREEN_BITS_ARB:
3146 curGLXAttr = GLX_GREEN_SIZE;
3147 break;
3148 case WGL_ALPHA_BITS_ARB:
3149 curGLXAttr = GLX_ALPHA_SIZE;
3150 break;
3151 case WGL_DEPTH_BITS_ARB:
3152 curGLXAttr = GLX_DEPTH_SIZE;
3153 break;
3154 case WGL_STENCIL_BITS_ARB:
3155 curGLXAttr = GLX_STENCIL_SIZE;
3156 break;
3157 case WGL_DOUBLE_BUFFER_ARB:
3158 curGLXAttr = GLX_DOUBLEBUFFER;
3159 break;
3160 case WGL_STEREO_ARB:
3161 curGLXAttr = GLX_STEREO;
3162 break;
3163 case WGL_AUX_BUFFERS_ARB:
3164 curGLXAttr = GLX_AUX_BUFFERS;
3165 break;
3167 case WGL_SUPPORT_GDI_ARB:
3168 if (!fmt) goto pix_error;
3169 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) ? TRUE : FALSE;
3170 continue;
3172 case WGL_DRAW_TO_BITMAP_ARB:
3173 if (!fmt) goto pix_error;
3174 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? TRUE : FALSE;
3175 continue;
3177 case WGL_DRAW_TO_WINDOW_ARB:
3178 case WGL_DRAW_TO_PBUFFER_ARB:
3179 if (!fmt) goto pix_error;
3180 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
3181 if (hTest) goto get_error;
3182 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
3183 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
3184 piValues[i] = GL_TRUE;
3185 else
3186 piValues[i] = GL_FALSE;
3187 continue;
3189 case WGL_SWAP_METHOD_ARB:
3190 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
3191 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
3192 * point only ATI offers this.
3194 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
3195 break;
3197 case WGL_PBUFFER_LARGEST_ARB:
3198 curGLXAttr = GLX_LARGEST_PBUFFER;
3199 break;
3201 case WGL_SAMPLE_BUFFERS_ARB:
3202 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
3203 break;
3205 case WGL_SAMPLES_ARB:
3206 curGLXAttr = GLX_SAMPLES_ARB;
3207 break;
3209 case WGL_FLOAT_COMPONENTS_NV:
3210 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
3211 break;
3213 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
3214 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
3215 break;
3217 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
3218 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
3219 break;
3221 case WGL_ACCUM_RED_BITS_ARB:
3222 curGLXAttr = GLX_ACCUM_RED_SIZE;
3223 break;
3224 case WGL_ACCUM_GREEN_BITS_ARB:
3225 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
3226 break;
3227 case WGL_ACCUM_BLUE_BITS_ARB:
3228 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
3229 break;
3230 case WGL_ACCUM_ALPHA_BITS_ARB:
3231 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
3232 break;
3233 case WGL_ACCUM_BITS_ARB:
3234 if (!fmt) goto pix_error;
3235 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
3236 if (hTest) goto get_error;
3237 piValues[i] = tmp;
3238 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
3239 if (hTest) goto get_error;
3240 piValues[i] += tmp;
3241 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
3242 if (hTest) goto get_error;
3243 piValues[i] += tmp;
3244 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
3245 if (hTest) goto get_error;
3246 piValues[i] += tmp;
3247 continue;
3249 default:
3250 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
3253 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
3254 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
3255 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
3257 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
3258 * and check which options can work using iPixelFormat=0 and which not.
3259 * A problem would be that this function is an extension. This would
3260 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
3262 if (0 != curGLXAttr && iPixelFormat != 0) {
3263 if (!fmt) goto pix_error;
3264 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
3265 if (hTest) goto get_error;
3266 curGLXAttr = 0;
3267 } else {
3268 piValues[i] = GL_FALSE;
3271 wine_tsx11_unlock();
3272 return GL_TRUE;
3274 get_error:
3275 wine_tsx11_unlock();
3276 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
3277 return GL_FALSE;
3279 pix_error:
3280 wine_tsx11_unlock();
3281 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
3282 return GL_FALSE;
3286 * X11DRV_wglGetPixelFormatAttribfvARB
3288 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
3290 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
3292 int *attr;
3293 int ret;
3294 UINT i;
3296 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
3298 /* Allocate a temporary array to store integer values */
3299 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
3300 if (!attr) {
3301 ERR("couldn't allocate %d array\n", nAttributes);
3302 return GL_FALSE;
3305 /* Piggy-back on wglGetPixelFormatAttribivARB */
3306 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
3307 if (ret) {
3308 /* Convert integer values to float. Should also check for attributes
3309 that can give decimal values here */
3310 for (i=0; i<nAttributes;i++) {
3311 pfValues[i] = attr[i];
3315 HeapFree(GetProcessHeap(), 0, attr);
3316 return ret;
3320 * X11DRV_wglBindTexImageARB
3322 * WGL_ARB_render_texture: wglBindTexImageARB
3324 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3326 Wine_GLPBuffer* object = hPbuffer;
3327 GLboolean ret = GL_FALSE;
3329 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3330 if (NULL == object) {
3331 SetLastError(ERROR_INVALID_HANDLE);
3332 return GL_FALSE;
3334 if (!object->use_render_texture) {
3335 SetLastError(ERROR_INVALID_HANDLE);
3336 return GL_FALSE;
3339 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3340 static int init = 0;
3341 int prev_binded_texture = 0;
3342 GLXContext prev_context;
3343 Drawable prev_drawable;
3344 GLXContext tmp_context;
3346 wine_tsx11_lock();
3347 prev_context = pglXGetCurrentContext();
3348 prev_drawable = pglXGetCurrentDrawable();
3350 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
3351 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
3352 isn't ideal performance wise but I wasn't able to get other ways working.
3354 if(!init) {
3355 init = 1; /* Only show the FIXME once for performance reasons */
3356 FIXME("partial stub!\n");
3359 TRACE("drawable=%lx, context=%p\n", object->drawable, prev_context);
3360 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3362 pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3364 /* Switch to our pbuffer */
3365 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
3367 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3368 * After that upload the pbuffer texture data. */
3369 pglBindTexture(object->texture_target, prev_binded_texture);
3370 pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3372 /* Switch back to the original drawable and upload the pbuffer-texture */
3373 pglXMakeCurrent(object->display, prev_drawable, prev_context);
3374 pglXDestroyContext(gdi_display, tmp_context);
3375 wine_tsx11_unlock();
3376 return GL_TRUE;
3379 if (NULL != pglXBindTexImageATI) {
3380 int buffer;
3382 switch(iBuffer)
3384 case WGL_FRONT_LEFT_ARB:
3385 buffer = GLX_FRONT_LEFT_ATI;
3386 break;
3387 case WGL_FRONT_RIGHT_ARB:
3388 buffer = GLX_FRONT_RIGHT_ATI;
3389 break;
3390 case WGL_BACK_LEFT_ARB:
3391 buffer = GLX_BACK_LEFT_ATI;
3392 break;
3393 case WGL_BACK_RIGHT_ARB:
3394 buffer = GLX_BACK_RIGHT_ATI;
3395 break;
3396 default:
3397 ERR("Unknown iBuffer=%#x\n", iBuffer);
3398 return FALSE;
3401 /* In the sample 'ogl_offscreen_rendering_3' from codesampler.net I get garbage on the screen.
3402 * I'm not sure if that's a bug in the ATI extension or in the program. I think that the program
3403 * expected a single buffering format since it didn't ask for double buffering. A buffer swap
3404 * fixed the program. I don't know what the correct behavior is. On the other hand that demo
3405 * works fine using our pbuffer emulation path.
3407 wine_tsx11_lock();
3408 ret = pglXBindTexImageATI(object->display, object->drawable, buffer);
3409 wine_tsx11_unlock();
3411 return ret;
3415 * X11DRV_wglReleaseTexImageARB
3417 * WGL_ARB_render_texture: wglReleaseTexImageARB
3419 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3421 Wine_GLPBuffer* object = hPbuffer;
3422 GLboolean ret = GL_FALSE;
3424 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3425 if (NULL == object) {
3426 SetLastError(ERROR_INVALID_HANDLE);
3427 return GL_FALSE;
3429 if (!object->use_render_texture) {
3430 SetLastError(ERROR_INVALID_HANDLE);
3431 return GL_FALSE;
3433 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3434 return GL_TRUE;
3436 if (NULL != pglXReleaseTexImageATI) {
3437 int buffer;
3439 switch(iBuffer)
3441 case WGL_FRONT_LEFT_ARB:
3442 buffer = GLX_FRONT_LEFT_ATI;
3443 break;
3444 case WGL_FRONT_RIGHT_ARB:
3445 buffer = GLX_FRONT_RIGHT_ATI;
3446 break;
3447 case WGL_BACK_LEFT_ARB:
3448 buffer = GLX_BACK_LEFT_ATI;
3449 break;
3450 case WGL_BACK_RIGHT_ARB:
3451 buffer = GLX_BACK_RIGHT_ATI;
3452 break;
3453 default:
3454 ERR("Unknown iBuffer=%#x\n", iBuffer);
3455 return FALSE;
3457 wine_tsx11_lock();
3458 ret = pglXReleaseTexImageATI(object->display, object->drawable, buffer);
3459 wine_tsx11_unlock();
3461 return ret;
3465 * X11DRV_wglGetExtensionsStringEXT
3467 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3469 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
3470 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3471 return WineGLInfo.wglExtensions;
3475 * X11DRV_wglGetSwapIntervalEXT
3477 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3479 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
3480 /* GLX_SGI_swap_control doesn't have any provisions for getting the swap
3481 * interval, so the swap interval has to be tracked. */
3482 TRACE("()\n");
3483 return swap_interval;
3487 * X11DRV_wglSwapIntervalEXT
3489 * WGL_EXT_swap_control: wglSwapIntervalEXT
3491 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
3492 BOOL ret = TRUE;
3494 TRACE("(%d)\n", interval);
3496 if (interval < 0)
3498 SetLastError(ERROR_INVALID_DATA);
3499 return FALSE;
3501 else if (!has_swap_control && interval == 0)
3503 /* wglSwapIntervalEXT considers an interval value of zero to mean that
3504 * vsync should be disabled, but glXSwapIntervalSGI considers such a
3505 * value to be an error. Just silently ignore the request for now. */
3506 WARN("Request to disable vertical sync is not handled\n");
3507 swap_interval = 0;
3509 else
3511 if (pglXSwapIntervalSGI)
3513 wine_tsx11_lock();
3514 ret = !pglXSwapIntervalSGI(interval);
3515 wine_tsx11_unlock();
3517 else
3518 WARN("GLX_SGI_swap_control extension is not available\n");
3520 if (ret)
3521 swap_interval = interval;
3522 else
3523 SetLastError(ERROR_DC_NOT_FOUND);
3526 return ret;
3530 * X11DRV_wglAllocateMemoryNV
3532 * WGL_NV_vertex_array_range: wglAllocateMemoryNV
3534 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
3535 void *ret = NULL;
3536 TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
3538 if (pglXAllocateMemoryNV)
3540 wine_tsx11_lock();
3541 ret = pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
3542 wine_tsx11_unlock();
3544 return ret;
3548 * X11DRV_wglFreeMemoryNV
3550 * WGL_NV_vertex_array_range: wglFreeMemoryNV
3552 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
3553 TRACE("(%p)\n", pointer);
3554 if (pglXFreeMemoryNV == NULL)
3555 return;
3557 wine_tsx11_lock();
3558 pglXFreeMemoryNV(pointer);
3559 wine_tsx11_unlock();
3563 * glxdrv_wglSetPixelFormatWINE
3565 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3566 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3568 static BOOL glxdrv_wglSetPixelFormatWINE(PHYSDEV dev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3570 struct glx_physdev *physdev = get_glxdrv_dev( dev );
3572 TRACE("(%p,%d,%p)\n", dev->hdc, iPixelFormat, ppfd);
3574 if (!has_opengl()) return FALSE;
3576 if (physdev->pixel_format == iPixelFormat) return TRUE;
3578 /* Relay to the core SetPixelFormat */
3579 TRACE("Changing iPixelFormat from %d to %d\n", physdev->pixel_format, iPixelFormat);
3580 return internal_SetPixelFormat(physdev, iPixelFormat, ppfd);
3584 * glxRequireVersion (internal)
3586 * Check if the supported GLX version matches requiredVersion.
3588 static BOOL glxRequireVersion(int requiredVersion)
3590 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3591 if(requiredVersion <= WineGLInfo.glxVersion[1])
3592 return TRUE;
3594 return FALSE;
3597 static BOOL glxRequireExtension(const char *requiredExtension)
3599 if (strstr(WineGLInfo.glxExtensions, requiredExtension) == NULL) {
3600 return FALSE;
3603 return TRUE;
3606 static void register_extension_string(const char *ext)
3608 if (WineGLInfo.wglExtensions[0])
3609 strcat(WineGLInfo.wglExtensions, " ");
3610 strcat(WineGLInfo.wglExtensions, ext);
3612 TRACE("'%s'\n", ext);
3615 static BOOL register_extension(const WineGLExtension * ext)
3617 int i;
3619 assert( WineGLExtensionListSize < MAX_EXTENSIONS );
3620 WineGLExtensionList[WineGLExtensionListSize++] = ext;
3622 register_extension_string(ext->extName);
3624 for (i = 0; ext->extEntryPoints[i].funcName; ++i)
3625 TRACE(" - '%s'\n", ext->extEntryPoints[i].funcName);
3627 return TRUE;
3630 static const WineGLExtension WGL_internal_functions =
3634 { "wglGetIntegerv", X11DRV_wglGetIntegerv },
3635 { "wglFinish", X11DRV_wglFinish },
3636 { "wglFlush", X11DRV_wglFlush },
3641 static const WineGLExtension WGL_ARB_create_context =
3643 "WGL_ARB_create_context",
3645 { "wglCreateContextAttribsARB", (void *)1 /* not called directly */ },
3649 static const WineGLExtension WGL_ARB_extensions_string =
3651 "WGL_ARB_extensions_string",
3653 { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
3657 static const WineGLExtension WGL_ARB_make_current_read =
3659 "WGL_ARB_make_current_read",
3661 { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
3662 { "wglMakeContextCurrentARB", (void *)1 /* not called directly */ },
3666 static const WineGLExtension WGL_ARB_multisample =
3668 "WGL_ARB_multisample",
3671 static const WineGLExtension WGL_ARB_pbuffer =
3673 "WGL_ARB_pbuffer",
3675 { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
3676 { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
3677 { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
3678 { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
3679 { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
3680 { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
3684 static const WineGLExtension WGL_ARB_pixel_format =
3686 "WGL_ARB_pixel_format",
3688 { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3689 { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3690 { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3694 static const WineGLExtension WGL_ARB_render_texture =
3696 "WGL_ARB_render_texture",
3698 { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3699 { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3703 static const WineGLExtension WGL_EXT_extensions_string =
3705 "WGL_EXT_extensions_string",
3707 { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3711 static const WineGLExtension WGL_EXT_swap_control =
3713 "WGL_EXT_swap_control",
3715 { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3716 { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3720 static const WineGLExtension WGL_NV_vertex_array_range =
3722 "WGL_NV_vertex_array_range",
3724 { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3725 { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3729 static const WineGLExtension WGL_WINE_pixel_format_passthrough =
3731 "WGL_WINE_pixel_format_passthrough",
3733 { "wglSetPixelFormatWINE", (void *)1 /* not called directly */ },
3738 * X11DRV_WineGL_LoadExtensions
3740 static void X11DRV_WineGL_LoadExtensions(void)
3742 WineGLInfo.wglExtensions[0] = 0;
3744 /* Load Wine internal functions */
3745 register_extension(&WGL_internal_functions);
3747 /* ARB Extensions */
3749 if(glxRequireExtension("GLX_ARB_create_context"))
3751 register_extension(&WGL_ARB_create_context);
3753 if(glxRequireExtension("GLX_ARB_create_context_profile"))
3754 register_extension_string("WGL_ARB_create_context_profile");
3757 if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3759 register_extension_string("WGL_ARB_pixel_format_float");
3760 register_extension_string("WGL_ATI_pixel_format_float");
3763 register_extension(&WGL_ARB_extensions_string);
3765 if (glxRequireVersion(3))
3766 register_extension(&WGL_ARB_make_current_read);
3768 if (glxRequireExtension("GLX_ARB_multisample"))
3769 register_extension(&WGL_ARB_multisample);
3771 /* In general pbuffer functionality requires support in the X-server. The functionality is
3772 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3773 * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3774 * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3776 * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3777 * without support in the X-server (which other Mesa based drivers require).
3779 * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3780 * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3781 * is available pbuffers must be available too. */
3782 if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3783 register_extension(&WGL_ARB_pbuffer);
3785 register_extension(&WGL_ARB_pixel_format);
3787 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3788 if (glxRequireExtension("GLX_ATI_render_texture") ||
3789 glxRequireExtension("GLX_ARB_render_texture") ||
3790 (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3792 register_extension(&WGL_ARB_render_texture);
3794 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3795 if(glxRequireExtension("GLX_NV_float_buffer"))
3796 register_extension_string("WGL_NV_float_buffer");
3798 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3799 if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3800 register_extension_string("WGL_NV_texture_rectangle");
3803 /* EXT Extensions */
3805 register_extension(&WGL_EXT_extensions_string);
3807 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3808 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3809 register_extension(&WGL_EXT_swap_control);
3811 if(glxRequireExtension("GLX_EXT_framebuffer_sRGB"))
3812 register_extension_string("WGL_EXT_framebuffer_sRGB");
3814 if(glxRequireExtension("GLX_EXT_fbconfig_packed_float"))
3815 register_extension_string("WGL_EXT_pixel_format_packed_float");
3817 if (glxRequireExtension("GLX_EXT_swap_control"))
3818 has_swap_control = TRUE;
3820 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3821 if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3822 register_extension(&WGL_NV_vertex_array_range);
3824 /* WINE-specific WGL Extensions */
3826 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3827 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3829 register_extension(&WGL_WINE_pixel_format_passthrough);
3833 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3835 wine_tsx11_lock();
3836 pglXDestroyGLXPixmap(display, glxpixmap);
3837 wine_tsx11_unlock();
3838 return TRUE;
3842 * glxdrv_SwapBuffers
3844 * Swap the buffers of this DC
3846 static BOOL glxdrv_SwapBuffers(PHYSDEV dev)
3848 struct glx_physdev *physdev = get_glxdrv_dev( dev );
3849 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
3851 if (!has_opengl()) return FALSE;
3853 TRACE("(%p)\n", dev->hdc);
3855 if (!ctx)
3857 WARN("Using a NULL context, skipping\n");
3858 SetLastError(ERROR_INVALID_HANDLE);
3859 return FALSE;
3862 if (!physdev->drawable)
3864 WARN("Using an invalid drawable, skipping\n");
3865 SetLastError(ERROR_INVALID_HANDLE);
3866 return FALSE;
3869 wine_tsx11_lock();
3870 sync_context(ctx);
3871 switch (physdev->type)
3873 case DC_GL_PIXMAP_WIN:
3874 if(pglXCopySubBufferMESA) {
3875 int w = physdev->x11dev->dc_rect.right - physdev->x11dev->dc_rect.left;
3876 int h = physdev->x11dev->dc_rect.bottom - physdev->x11dev->dc_rect.top;
3878 /* (glX)SwapBuffers has an implicit glFlush effect, however
3879 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3880 * copying */
3881 pglFlush();
3882 if(w > 0 && h > 0)
3883 pglXCopySubBufferMESA(gdi_display, physdev->drawable, 0, 0, w, h);
3884 break;
3886 /* fall through */
3887 default:
3888 pglXSwapBuffers(gdi_display, physdev->drawable);
3889 break;
3892 flush_gl_drawable( physdev );
3893 wine_tsx11_unlock();
3895 /* FPS support */
3896 if (TRACE_ON(fps))
3898 static long prev_time, start_time;
3899 static unsigned long frames, frames_total;
3901 DWORD time = GetTickCount();
3902 frames++;
3903 frames_total++;
3904 /* every 1.5 seconds */
3905 if (time - prev_time > 1500) {
3906 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
3907 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
3908 prev_time = time;
3909 frames = 0;
3910 if(start_time == 0) start_time = time;
3914 return TRUE;
3917 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3919 WineGLPixelFormat *fmt;
3920 XVisualInfo *ret;
3922 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
3923 if(fmt == NULL)
3924 return NULL;
3926 wine_tsx11_lock();
3927 ret = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
3928 wine_tsx11_unlock();
3929 return ret;
3932 static BOOL create_glx_dc( PHYSDEV *pdev )
3934 /* assume that only the main x11 device implements GetDeviceCaps */
3935 X11DRV_PDEVICE *x11dev = get_x11drv_dev( GET_NEXT_PHYSDEV( *pdev, pGetDeviceCaps ));
3936 struct glx_physdev *physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) );
3938 if (!physdev) return FALSE;
3939 physdev->x11dev = x11dev;
3940 push_dc_driver( pdev, &physdev->dev, &glxdrv_funcs );
3941 return TRUE;
3944 /**********************************************************************
3945 * glxdrv_CreateDC
3947 static BOOL glxdrv_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
3948 LPCWSTR output, const DEVMODEW* initData )
3950 return create_glx_dc( pdev );
3953 /**********************************************************************
3954 * glxdrv_CreateCompatibleDC
3956 static BOOL glxdrv_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
3958 if (orig) /* chain to next driver first */
3960 orig = GET_NEXT_PHYSDEV( orig, pCreateCompatibleDC );
3961 if (!orig->funcs->pCreateCompatibleDC( orig, pdev )) return FALSE;
3963 /* otherwise we have been called by x11drv */
3964 return create_glx_dc( pdev );
3967 /**********************************************************************
3968 * glxdrv_DeleteDC
3970 static BOOL glxdrv_DeleteDC( PHYSDEV dev )
3972 struct glx_physdev *physdev = get_glxdrv_dev( dev );
3973 HeapFree( GetProcessHeap(), 0, physdev );
3974 return TRUE;
3977 /***********************************************************************
3978 * glxdrv_SelectBitmap
3980 static HBITMAP glxdrv_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
3982 HBITMAP ret;
3983 struct glx_physdev *physdev = get_glxdrv_dev( dev );
3985 dev = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
3986 ret = dev->funcs->pSelectBitmap( dev, hbitmap );
3987 if (ret)
3989 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physdev->drawable = 0;
3990 else physdev->drawable = X11DRV_get_phys_bitmap(hbitmap)->glxpixmap;
3992 return ret;
3995 /**********************************************************************
3996 * glxdrv_ExtEscape
3998 static INT glxdrv_ExtEscape( PHYSDEV dev, INT escape, INT in_count, LPCVOID in_data,
3999 INT out_count, LPVOID out_data )
4001 struct glx_physdev *physdev = get_glxdrv_dev( dev );
4003 dev = GET_NEXT_PHYSDEV( dev, pExtEscape );
4005 if (escape == X11DRV_ESCAPE && in_data && in_count >= sizeof(enum x11drv_escape_codes))
4007 switch (*(const enum x11drv_escape_codes *)in_data)
4009 case X11DRV_SET_DRAWABLE:
4010 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
4012 const struct x11drv_escape_set_drawable *data = in_data;
4013 physdev->pixel_format = pixelformat_from_fbconfig_id( data->fbconfig_id );
4014 physdev->type = data->gl_type;
4015 physdev->drawable = data->gl_drawable;
4016 physdev->pixmap = data->pixmap;
4017 TRACE( "SET_DRAWABLE hdc %p drawable %lx pf %u type %u\n",
4018 dev->hdc, physdev->drawable, physdev->pixel_format, physdev->type );
4020 break;
4021 case X11DRV_FLUSH_GL_DRAWABLE:
4022 flush_gl_drawable( physdev );
4023 return TRUE;
4024 default:
4025 break;
4028 return dev->funcs->pExtEscape( dev, escape, in_count, in_data, out_count, out_data );
4031 static const struct gdi_dc_funcs glxdrv_funcs =
4033 NULL, /* pAbortDoc */
4034 NULL, /* pAbortPath */
4035 NULL, /* pAlphaBlend */
4036 NULL, /* pAngleArc */
4037 NULL, /* pArc */
4038 NULL, /* pArcTo */
4039 NULL, /* pBeginPath */
4040 NULL, /* pBlendImage */
4041 glxdrv_ChoosePixelFormat, /* pChoosePixelFormat */
4042 NULL, /* pChord */
4043 NULL, /* pCloseFigure */
4044 NULL, /* pCopyBitmap */
4045 NULL, /* pCreateBitmap */
4046 glxdrv_CreateCompatibleDC, /* pCreateCompatibleDC */
4047 glxdrv_CreateDC, /* pCreateDC */
4048 NULL, /* pDeleteBitmap */
4049 glxdrv_DeleteDC, /* pDeleteDC */
4050 NULL, /* pDeleteObject */
4051 glxdrv_DescribePixelFormat, /* pDescribePixelFormat */
4052 NULL, /* pDeviceCapabilities */
4053 NULL, /* pEllipse */
4054 NULL, /* pEndDoc */
4055 NULL, /* pEndPage */
4056 NULL, /* pEndPath */
4057 NULL, /* pEnumFonts */
4058 NULL, /* pEnumICMProfiles */
4059 NULL, /* pExcludeClipRect */
4060 NULL, /* pExtDeviceMode */
4061 glxdrv_ExtEscape, /* pExtEscape */
4062 NULL, /* pExtFloodFill */
4063 NULL, /* pExtSelectClipRgn */
4064 NULL, /* pExtTextOut */
4065 NULL, /* pFillPath */
4066 NULL, /* pFillRgn */
4067 NULL, /* pFlattenPath */
4068 NULL, /* pFontIsLinked */
4069 NULL, /* pFrameRgn */
4070 NULL, /* pGdiComment */
4071 NULL, /* pGdiRealizationInfo */
4072 NULL, /* pGetBoundsRect */
4073 NULL, /* pGetCharABCWidths */
4074 NULL, /* pGetCharABCWidthsI */
4075 NULL, /* pGetCharWidth */
4076 NULL, /* pGetDeviceCaps */
4077 NULL, /* pGetDeviceGammaRamp */
4078 NULL, /* pGetFontData */
4079 NULL, /* pGetFontUnicodeRanges */
4080 NULL, /* pGetGlyphIndices */
4081 NULL, /* pGetGlyphOutline */
4082 NULL, /* pGetICMProfile */
4083 NULL, /* pGetImage */
4084 NULL, /* pGetKerningPairs */
4085 NULL, /* pGetNearestColor */
4086 NULL, /* pGetOutlineTextMetrics */
4087 NULL, /* pGetPixel */
4088 glxdrv_GetPixelFormat, /* pGetPixelFormat */
4089 NULL, /* pGetSystemPaletteEntries */
4090 NULL, /* pGetTextCharsetInfo */
4091 NULL, /* pGetTextExtentExPoint */
4092 NULL, /* pGetTextExtentExPointI */
4093 NULL, /* pGetTextFace */
4094 NULL, /* pGetTextMetrics */
4095 NULL, /* pGradientFill */
4096 NULL, /* pIntersectClipRect */
4097 NULL, /* pInvertRgn */
4098 NULL, /* pLineTo */
4099 NULL, /* pModifyWorldTransform */
4100 NULL, /* pMoveTo */
4101 NULL, /* pOffsetClipRgn */
4102 NULL, /* pOffsetViewportOrg */
4103 NULL, /* pOffsetWindowOrg */
4104 NULL, /* pPaintRgn */
4105 NULL, /* pPatBlt */
4106 NULL, /* pPie */
4107 NULL, /* pPolyBezier */
4108 NULL, /* pPolyBezierTo */
4109 NULL, /* pPolyDraw */
4110 NULL, /* pPolyPolygon */
4111 NULL, /* pPolyPolyline */
4112 NULL, /* pPolygon */
4113 NULL, /* pPolyline */
4114 NULL, /* pPolylineTo */
4115 NULL, /* pPutImage */
4116 NULL, /* pRealizeDefaultPalette */
4117 NULL, /* pRealizePalette */
4118 NULL, /* pRectangle */
4119 NULL, /* pResetDC */
4120 NULL, /* pRestoreDC */
4121 NULL, /* pRoundRect */
4122 NULL, /* pSaveDC */
4123 NULL, /* pScaleViewportExt */
4124 NULL, /* pScaleWindowExt */
4125 glxdrv_SelectBitmap, /* pSelectBitmap */
4126 NULL, /* pSelectBrush */
4127 NULL, /* pSelectClipPath */
4128 NULL, /* pSelectFont */
4129 NULL, /* pSelectPalette */
4130 NULL, /* pSelectPen */
4131 NULL, /* pSetArcDirection */
4132 NULL, /* pSetBkColor */
4133 NULL, /* pSetBkMode */
4134 NULL, /* pSetBoundsRect */
4135 NULL, /* pSetDCBrushColor */
4136 NULL, /* pSetDCPenColor */
4137 NULL, /* pSetDIBitsToDevice */
4138 NULL, /* pSetDeviceClipping */
4139 NULL, /* pSetDeviceGammaRamp */
4140 NULL, /* pSetLayout */
4141 NULL, /* pSetMapMode */
4142 NULL, /* pSetMapperFlags */
4143 NULL, /* pSetPixel */
4144 glxdrv_SetPixelFormat, /* pSetPixelFormat */
4145 NULL, /* pSetPolyFillMode */
4146 NULL, /* pSetROP2 */
4147 NULL, /* pSetRelAbs */
4148 NULL, /* pSetStretchBltMode */
4149 NULL, /* pSetTextAlign */
4150 NULL, /* pSetTextCharacterExtra */
4151 NULL, /* pSetTextColor */
4152 NULL, /* pSetTextJustification */
4153 NULL, /* pSetViewportExt */
4154 NULL, /* pSetViewportOrg */
4155 NULL, /* pSetWindowExt */
4156 NULL, /* pSetWindowOrg */
4157 NULL, /* pSetWorldTransform */
4158 NULL, /* pStartDoc */
4159 NULL, /* pStartPage */
4160 NULL, /* pStretchBlt */
4161 NULL, /* pStretchDIBits */
4162 NULL, /* pStrokeAndFillPath */
4163 NULL, /* pStrokePath */
4164 glxdrv_SwapBuffers, /* pSwapBuffers */
4165 NULL, /* pUnrealizePalette */
4166 NULL, /* pWidenPath */
4167 glxdrv_wglCopyContext, /* pwglCopyContext */
4168 glxdrv_wglCreateContext, /* pwglCreateContext */
4169 glxdrv_wglCreateContextAttribsARB, /* pwglCreateContextAttribsARB */
4170 glxdrv_wglDeleteContext, /* pwglDeleteContext */
4171 glxdrv_wglGetProcAddress, /* pwglGetProcAddress */
4172 glxdrv_wglMakeContextCurrentARB, /* pwglMakeContextCurrentARB */
4173 glxdrv_wglMakeCurrent, /* pwglMakeCurrent */
4174 glxdrv_wglSetPixelFormatWINE, /* pwglSetPixelFormatWINE */
4175 glxdrv_wglShareLists, /* pwglShareLists */
4176 glxdrv_wglUseFontBitmapsA, /* pwglUseFontBitmapsA */
4177 glxdrv_wglUseFontBitmapsW, /* pwglUseFontBitmapsW */
4178 GDI_PRIORITY_GRAPHICS_DRV + 20 /* priority */
4181 const struct gdi_dc_funcs *get_glx_driver(void)
4183 return &glxdrv_funcs;
4186 #else /* no OpenGL includes */
4188 const struct gdi_dc_funcs *get_glx_driver(void)
4190 return NULL;
4193 void mark_drawable_dirty(Drawable old, Drawable new)
4197 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
4199 return 0;
4203 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
4205 return FALSE;
4208 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
4210 return NULL;
4213 #endif /* defined(SONAME_LIBGL) */