push 5bc4839baba05cc4333240c25295b8dd6e351557
[wine/hacks.git] / dlls / winex11.drv / opengl.c
blob45da4791ca1b1704c084145ddf9bc098dc40713e
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
5 * Copyright 2005 Alex Woods
6 * Copyright 2005 Raphael Junqueira
7 * Copyright 2006-2009 Roderick Colenbrander
8 * Copyright 2006 Tomas Carnecky
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "x11drv.h"
33 #include "winternl.h"
34 #include "wine/library.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
39 #ifdef SONAME_LIBGL
41 #undef APIENTRY
42 #undef CALLBACK
43 #undef WINAPI
45 #ifdef HAVE_GL_GL_H
46 # include <GL/gl.h>
47 #endif
48 #ifdef HAVE_GL_GLX_H
49 # include <GL/glx.h>
50 #endif
52 #include "wine/wgl.h"
54 #undef APIENTRY
55 #undef CALLBACK
56 #undef WINAPI
58 /* Redefines the constants */
59 #define CALLBACK __stdcall
60 #define WINAPI __stdcall
61 #define APIENTRY WINAPI
64 WINE_DECLARE_DEBUG_CHANNEL(fps);
66 typedef struct wine_glextension {
67 const char *extName;
68 struct {
69 const char *funcName;
70 void *funcAddress;
71 } extEntryPoints[9];
72 } WineGLExtension;
74 struct WineGLInfo {
75 const char *glVersion;
76 char *glExtensions;
78 int glxVersion[2];
80 const char *glxServerVersion;
81 const char *glxServerVendor;
82 const char *glxServerExtensions;
84 const char *glxClientVersion;
85 const char *glxClientVendor;
86 const char *glxClientExtensions;
88 const char *glxExtensions;
90 BOOL glxDirect;
91 char wglExtensions[4096];
94 typedef struct wine_glpixelformat {
95 int iPixelFormat;
96 GLXFBConfig fbconfig;
97 int fmt_id;
98 int render_type;
99 BOOL offscreenOnly;
100 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
101 } WineGLPixelFormat;
103 typedef struct wine_glcontext {
104 HDC hdc;
105 BOOL do_escape;
106 XVisualInfo *vis;
107 WineGLPixelFormat *fmt;
108 GLXContext ctx;
109 HDC read_hdc;
110 Drawable drawables[2];
111 BOOL refresh_drawables;
112 struct wine_glcontext *next;
113 struct wine_glcontext *prev;
114 } Wine_GLContext;
116 typedef struct wine_glpbuffer {
117 Drawable drawable;
118 Display* display;
119 WineGLPixelFormat* fmt;
120 int width;
121 int height;
122 int* attribList;
123 HDC hdc;
125 int use_render_texture; /* This is also the internal texture format */
126 int texture_bind_target;
127 int texture_bpp;
128 GLint texture_format;
129 GLuint texture_target;
130 GLenum texture_type;
131 GLuint texture;
132 int texture_level;
133 } Wine_GLPBuffer;
135 static Wine_GLContext *context_list;
136 static struct WineGLInfo WineGLInfo = { 0 };
137 static int use_render_texture_emulation = 1;
138 static int use_render_texture_ati = 0;
139 static int swap_interval = 1;
141 #define MAX_EXTENSIONS 16
142 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
143 static int WineGLExtensionListSize;
145 static void X11DRV_WineGL_LoadExtensions(void);
146 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count);
147 static BOOL glxRequireVersion(int requiredVersion);
148 static BOOL glxRequireExtension(const char *requiredExtension);
150 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
151 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
152 TRACE(" - dwFlags : ");
153 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
154 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
155 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
156 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
157 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
158 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
159 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
160 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
161 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
162 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
163 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
164 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
165 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
166 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
167 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
168 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
169 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
170 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
171 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
172 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
173 #undef TEST_AND_DUMP
174 TRACE("\n");
176 TRACE(" - iPixelType : ");
177 switch (ppfd->iPixelType) {
178 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
179 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
181 TRACE("\n");
183 TRACE(" - Color : %d\n", ppfd->cColorBits);
184 TRACE(" - Red : %d\n", ppfd->cRedBits);
185 TRACE(" - Green : %d\n", ppfd->cGreenBits);
186 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
187 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
188 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
189 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
190 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
191 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
193 TRACE(" - iLayerType : ");
194 switch (ppfd->iLayerType) {
195 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
196 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
197 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
199 TRACE("\n");
202 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
203 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
205 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
206 /* GLX 1.0 */
207 MAKE_FUNCPTR(glXChooseVisual)
208 MAKE_FUNCPTR(glXCopyContext)
209 MAKE_FUNCPTR(glXCreateContext)
210 MAKE_FUNCPTR(glXCreateGLXPixmap)
211 MAKE_FUNCPTR(glXGetCurrentContext)
212 MAKE_FUNCPTR(glXGetCurrentDrawable)
213 MAKE_FUNCPTR(glXDestroyContext)
214 MAKE_FUNCPTR(glXDestroyGLXPixmap)
215 MAKE_FUNCPTR(glXGetConfig)
216 MAKE_FUNCPTR(glXIsDirect)
217 MAKE_FUNCPTR(glXMakeCurrent)
218 MAKE_FUNCPTR(glXSwapBuffers)
219 MAKE_FUNCPTR(glXQueryExtension)
220 MAKE_FUNCPTR(glXQueryVersion)
221 MAKE_FUNCPTR(glXUseXFont)
223 /* GLX 1.1 */
224 MAKE_FUNCPTR(glXGetClientString)
225 MAKE_FUNCPTR(glXQueryExtensionsString)
226 MAKE_FUNCPTR(glXQueryServerString)
228 /* GLX 1.3 */
229 MAKE_FUNCPTR(glXGetFBConfigs)
230 MAKE_FUNCPTR(glXChooseFBConfig)
231 MAKE_FUNCPTR(glXCreatePbuffer)
232 MAKE_FUNCPTR(glXCreateNewContext)
233 MAKE_FUNCPTR(glXDestroyPbuffer)
234 MAKE_FUNCPTR(glXGetFBConfigAttrib)
235 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
236 MAKE_FUNCPTR(glXMakeContextCurrent)
237 MAKE_FUNCPTR(glXQueryDrawable)
238 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
240 /* GLX Extensions */
241 static void* (*pglXGetProcAddressARB)(const GLubyte *);
242 static int (*pglXSwapIntervalSGI)(int);
244 /* ATI GLX Extensions */
245 static BOOL (*pglXBindTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
246 static BOOL (*pglXReleaseTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
247 static BOOL (*pglXDrawableAttribATI)(Display *dpy, GLXDrawable draw, const int *attribList);
249 /* NV GLX Extension */
250 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
251 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
253 /* MESA GLX Extensions */
254 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
256 /* Standard OpenGL */
257 MAKE_FUNCPTR(glBindTexture)
258 MAKE_FUNCPTR(glBitmap)
259 MAKE_FUNCPTR(glCopyTexSubImage1D)
260 MAKE_FUNCPTR(glCopyTexImage2D)
261 MAKE_FUNCPTR(glCopyTexSubImage2D)
262 MAKE_FUNCPTR(glDrawBuffer)
263 MAKE_FUNCPTR(glEndList)
264 MAKE_FUNCPTR(glGetError)
265 MAKE_FUNCPTR(glGetIntegerv)
266 MAKE_FUNCPTR(glGetString)
267 MAKE_FUNCPTR(glNewList)
268 MAKE_FUNCPTR(glPixelStorei)
269 MAKE_FUNCPTR(glReadPixels)
270 MAKE_FUNCPTR(glTexImage2D)
271 MAKE_FUNCPTR(glFinish)
272 MAKE_FUNCPTR(glFlush)
273 #undef MAKE_FUNCPTR
275 static BOOL infoInitialized = FALSE;
276 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
278 int screen = DefaultScreen(gdi_display);
279 Window win = RootWindow(gdi_display, screen);
280 const char* str;
281 XVisualInfo *vis;
282 GLXContext ctx = NULL;
283 int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
285 if (infoInitialized)
286 return TRUE;
287 infoInitialized = TRUE;
289 wine_tsx11_lock();
291 vis = pglXChooseVisual(gdi_display, screen, attribList);
292 if (vis) {
293 WORD old_fs = wine_get_fs();
294 /* Create a GLX Context. Without one we can't query GL information */
295 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
296 if (wine_get_fs() != old_fs)
298 wine_set_fs( old_fs );
299 wine_tsx11_unlock();
300 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
301 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
302 return FALSE;
306 if (ctx) {
307 pglXMakeCurrent(gdi_display, win, ctx);
308 } else {
309 ERR(" couldn't initialize OpenGL, expect problems\n");
310 wine_tsx11_unlock();
311 return FALSE;
314 WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
315 str = (const char *) pglGetString(GL_EXTENSIONS);
316 WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
317 strcpy(WineGLInfo.glExtensions, str);
319 /* Get the common GLX version supported by GLX client and server ( major/minor) */
320 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
322 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
323 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
324 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
326 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
327 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
328 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
330 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
331 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
333 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
334 TRACE("GL renderer : %s.\n", pglGetString(GL_RENDERER));
335 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
336 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
337 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
338 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
339 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
340 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
342 if(vis) XFree(vis);
343 if(ctx) {
344 pglXMakeCurrent(gdi_display, None, NULL);
345 pglXDestroyContext(gdi_display, ctx);
347 wine_tsx11_unlock();
348 return TRUE;
351 void X11DRV_OpenGL_Cleanup(void)
353 HeapFree(GetProcessHeap(), 0, WineGLInfo.glExtensions);
354 infoInitialized = FALSE;
357 static BOOL has_opengl(void)
359 static int init_done;
360 static void *opengl_handle;
362 char buffer[200];
363 int error_base, event_base;
365 if (init_done) return (opengl_handle != NULL);
366 init_done = 1;
368 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
369 and include all dependencies */
370 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
371 if (opengl_handle == NULL)
373 ERR( "Failed to load libGL: %s\n", buffer );
374 ERR( "OpenGL support is disabled.\n");
375 return FALSE;
378 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
379 if (pglXGetProcAddressARB == NULL) {
380 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
381 goto failed;
384 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
386 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
387 goto failed; \
388 } while(0)
390 /* GLX 1.0 */
391 LOAD_FUNCPTR(glXChooseVisual);
392 LOAD_FUNCPTR(glXCopyContext);
393 LOAD_FUNCPTR(glXCreateContext);
394 LOAD_FUNCPTR(glXCreateGLXPixmap);
395 LOAD_FUNCPTR(glXGetCurrentContext);
396 LOAD_FUNCPTR(glXGetCurrentDrawable);
397 LOAD_FUNCPTR(glXDestroyContext);
398 LOAD_FUNCPTR(glXDestroyGLXPixmap);
399 LOAD_FUNCPTR(glXGetConfig);
400 LOAD_FUNCPTR(glXIsDirect);
401 LOAD_FUNCPTR(glXMakeCurrent);
402 LOAD_FUNCPTR(glXSwapBuffers);
403 LOAD_FUNCPTR(glXQueryExtension);
404 LOAD_FUNCPTR(glXQueryVersion);
405 LOAD_FUNCPTR(glXUseXFont);
407 /* GLX 1.1 */
408 LOAD_FUNCPTR(glXGetClientString);
409 LOAD_FUNCPTR(glXQueryExtensionsString);
410 LOAD_FUNCPTR(glXQueryServerString);
412 /* GLX 1.3 */
413 LOAD_FUNCPTR(glXCreatePbuffer);
414 LOAD_FUNCPTR(glXCreateNewContext);
415 LOAD_FUNCPTR(glXDestroyPbuffer);
416 LOAD_FUNCPTR(glXMakeContextCurrent);
417 LOAD_FUNCPTR(glXGetCurrentReadDrawable);
418 LOAD_FUNCPTR(glXGetFBConfigs);
420 /* Standard OpenGL calls */
421 LOAD_FUNCPTR(glBindTexture);
422 LOAD_FUNCPTR(glBitmap);
423 LOAD_FUNCPTR(glCopyTexSubImage1D);
424 LOAD_FUNCPTR(glCopyTexImage2D);
425 LOAD_FUNCPTR(glCopyTexSubImage2D);
426 LOAD_FUNCPTR(glDrawBuffer);
427 LOAD_FUNCPTR(glEndList);
428 LOAD_FUNCPTR(glGetError);
429 LOAD_FUNCPTR(glGetIntegerv);
430 LOAD_FUNCPTR(glGetString);
431 LOAD_FUNCPTR(glNewList);
432 LOAD_FUNCPTR(glPixelStorei);
433 LOAD_FUNCPTR(glReadPixels);
434 LOAD_FUNCPTR(glTexImage2D);
435 LOAD_FUNCPTR(glFinish);
436 LOAD_FUNCPTR(glFlush);
437 #undef LOAD_FUNCPTR
439 /* It doesn't matter if these fail. They'll only be used if the driver reports
440 the associated extension is available (and if a driver reports the extension
441 is available but fails to provide the functions, it's quite broken) */
442 #define LOAD_FUNCPTR(f) p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)
443 /* NV GLX Extension */
444 LOAD_FUNCPTR(glXAllocateMemoryNV);
445 LOAD_FUNCPTR(glXFreeMemoryNV);
446 #undef LOAD_FUNCPTR
448 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
450 wine_tsx11_lock();
451 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
452 TRACE("GLX is up and running error_base = %d\n", error_base);
453 } else {
454 wine_tsx11_unlock();
455 ERR( "GLX extension is missing, disabling OpenGL.\n" );
456 goto failed;
459 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
460 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
461 * rendering is used.
463 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
464 * depends on the reported GLX client / server version and on the client / server extension list.
465 * Those don't have to be the same.
467 * In general the server GLX information lists the capabilities in case of indirect rendering.
468 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
469 * available and in that case the client GLX informat can be used.
470 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
471 * in the GLX version/extension list. When a program does this it works for certain for both
472 * direct and indirect rendering.
474 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
475 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
476 * extension list which causes this extension not to be listed. (Wine requires this extension).
477 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
478 * pbuffers is around.
480 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
481 * the ATI drivers and from then on use GLX client information for them.
484 if(glxRequireVersion(3)) {
485 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
486 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
487 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
488 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
489 } else if(glxRequireExtension("GLX_SGIX_fbconfig")) {
490 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
491 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
492 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
494 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
495 * enable this function when the Xserver understand GLX 1.3 or newer
497 pglXQueryDrawable = NULL;
498 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
499 TRACE("Overriding ATI GLX capabilities!\n");
500 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
501 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
502 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
503 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
505 /* Use client GLX information in case of the ATI drivers. We override the
506 * capabilities over here and not somewhere else as ATI might better their
507 * life in the future. In case they release proper drivers this block of
508 * code won't be called. */
509 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
510 } else {
511 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
514 if(glxRequireExtension("GLX_ATI_render_texture")) {
515 use_render_texture_ati = 1;
516 pglXBindTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageATI");
517 pglXReleaseTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageATI");
518 pglXDrawableAttribATI = pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribATI");
521 if(glxRequireExtension("GLX_MESA_copy_sub_buffer")) {
522 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
525 X11DRV_WineGL_LoadExtensions();
527 wine_tsx11_unlock();
528 return TRUE;
530 failed:
531 wine_dlclose(opengl_handle, NULL, 0);
532 opengl_handle = NULL;
533 return FALSE;
536 static inline Wine_GLContext *alloc_context(void)
538 Wine_GLContext *ret;
540 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
542 ret->next = context_list;
543 if (context_list) context_list->prev = ret;
544 context_list = ret;
546 return ret;
549 static inline void free_context(Wine_GLContext *context)
551 if (context->next != NULL) context->next->prev = context->prev;
552 if (context->prev != NULL) context->prev->next = context->next;
553 else context_list = context->next;
555 if (context->vis) XFree(context->vis);
556 HeapFree(GetProcessHeap(), 0, context);
559 static inline BOOL is_valid_context( Wine_GLContext *ctx )
561 Wine_GLContext *ptr;
562 for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
563 return (ptr != NULL);
566 static int describeContext(Wine_GLContext* ctx) {
567 int tmp;
568 int ctx_vis_id;
569 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
570 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
571 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
572 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
573 TRACE(" - VISUAL_ID 0x%x\n", tmp);
574 ctx_vis_id = tmp;
575 return ctx_vis_id;
578 static BOOL describeDrawable(X11DRV_PDEVICE *physDev) {
579 int tmp;
580 WineGLPixelFormat *fmt;
581 int fmt_count = 0;
583 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE /* Offscreen */, &fmt_count);
584 if(!fmt) return FALSE;
586 TRACE(" HDC %p has:\n", physDev->hdc);
587 TRACE(" - iPixelFormat %d\n", fmt->iPixelFormat);
588 TRACE(" - Drawable %p\n", (void*) get_glxdrawable(physDev));
589 TRACE(" - FBCONFIG_ID 0x%x\n", fmt->fmt_id);
591 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &tmp);
592 TRACE(" - VISUAL_ID 0x%x\n", tmp);
594 return TRUE;
597 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
598 int nAttribs = 0;
599 unsigned cur = 0;
600 int pop;
601 int drawattrib = 0;
602 int nvfloatattrib = GLX_DONT_CARE;
603 int pixelattrib = ~0;
605 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
606 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
607 while (iWGLAttr && 0 != iWGLAttr[cur]) {
608 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
610 switch (iWGLAttr[cur]) {
611 case WGL_AUX_BUFFERS_ARB:
612 pop = iWGLAttr[++cur];
613 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
614 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
615 break;
616 case WGL_COLOR_BITS_ARB:
617 pop = iWGLAttr[++cur];
618 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
619 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
620 break;
621 case WGL_BLUE_BITS_ARB:
622 pop = iWGLAttr[++cur];
623 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
624 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
625 break;
626 case WGL_RED_BITS_ARB:
627 pop = iWGLAttr[++cur];
628 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
629 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
630 break;
631 case WGL_GREEN_BITS_ARB:
632 pop = iWGLAttr[++cur];
633 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
634 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
635 break;
636 case WGL_ALPHA_BITS_ARB:
637 pop = iWGLAttr[++cur];
638 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
639 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
640 break;
641 case WGL_DEPTH_BITS_ARB:
642 pop = iWGLAttr[++cur];
643 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
644 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
645 break;
646 case WGL_STENCIL_BITS_ARB:
647 pop = iWGLAttr[++cur];
648 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
649 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
650 break;
651 case WGL_DOUBLE_BUFFER_ARB:
652 pop = iWGLAttr[++cur];
653 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
654 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
655 break;
656 case WGL_STEREO_ARB:
657 pop = iWGLAttr[++cur];
658 PUSH2(oGLXAttr, GLX_STEREO, pop);
659 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
660 break;
662 case WGL_PIXEL_TYPE_ARB:
663 pop = iWGLAttr[++cur];
664 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
665 switch (pop) {
666 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
667 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
668 /* 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 */
669 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
670 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
671 default:
672 ERR("unexpected PixelType(%x)\n", pop);
673 pop = 0;
675 break;
677 case WGL_SUPPORT_GDI_ARB:
678 /* This flag is set in a WineGLPixelFormat */
679 pop = iWGLAttr[++cur];
680 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
681 break;
683 case WGL_DRAW_TO_BITMAP_ARB:
684 /* This flag is set in a WineGLPixelFormat */
685 pop = iWGLAttr[++cur];
686 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
687 break;
689 case WGL_DRAW_TO_WINDOW_ARB:
690 pop = iWGLAttr[++cur];
691 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
692 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
693 if (pop) {
694 drawattrib |= GLX_WINDOW_BIT;
696 break;
698 case WGL_DRAW_TO_PBUFFER_ARB:
699 pop = iWGLAttr[++cur];
700 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
701 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
702 if (pop) {
703 drawattrib |= GLX_PBUFFER_BIT;
705 break;
707 case WGL_ACCELERATION_ARB:
708 /* This flag is set in a WineGLPixelFormat */
709 pop = iWGLAttr[++cur];
710 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
711 break;
713 case WGL_SUPPORT_OPENGL_ARB:
714 pop = iWGLAttr[++cur];
715 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
716 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
717 break;
719 case WGL_SWAP_METHOD_ARB:
720 pop = iWGLAttr[++cur];
721 /* For now we ignore this and just return SWAP_EXCHANGE */
722 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
723 break;
725 case WGL_PBUFFER_LARGEST_ARB:
726 pop = iWGLAttr[++cur];
727 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
728 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
729 break;
731 case WGL_SAMPLE_BUFFERS_ARB:
732 pop = iWGLAttr[++cur];
733 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
734 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
735 break;
737 case WGL_SAMPLES_ARB:
738 pop = iWGLAttr[++cur];
739 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
740 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
741 break;
743 case WGL_TEXTURE_FORMAT_ARB:
744 case WGL_TEXTURE_TARGET_ARB:
745 case WGL_MIPMAP_TEXTURE_ARB:
746 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
747 pop = iWGLAttr[++cur];
748 if (NULL == pbuf) {
749 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
751 if (use_render_texture_ati) {
752 /** nothing to do here */
754 else if (!use_render_texture_emulation) {
755 if (WGL_NO_TEXTURE_ARB != pop) {
756 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
757 return -1; /** error: don't support it */
758 } else {
759 drawattrib |= GLX_PBUFFER_BIT;
762 break ;
763 case WGL_FLOAT_COMPONENTS_NV:
764 nvfloatattrib = iWGLAttr[++cur];
765 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
766 break ;
767 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
768 case WGL_BIND_TO_TEXTURE_RGB_ARB:
769 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
770 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
771 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
772 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
773 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
774 pop = iWGLAttr[++cur];
775 /** cannot be converted, see direct handling on
776 * - wglGetPixelFormatAttribivARB
777 * TODO: wglChoosePixelFormat
779 break ;
780 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
781 pop = iWGLAttr[++cur];
782 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
783 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
784 break ;
786 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
787 pop = iWGLAttr[++cur];
788 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
789 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
790 break ;
791 default:
792 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
793 break;
795 ++cur;
798 /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
799 * all formats. Unless drawattrib is set to a non-zero value override it with ~0, so that pixmap and pbuffer
800 * formats appear as well. */
801 if(!drawattrib) drawattrib = ~0;
802 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
803 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
805 /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
806 * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to ~0 unless
807 * it is overridden. */
808 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
809 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
811 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
812 if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
813 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
814 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
817 return nAttribs;
820 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
822 int render_type=0, render_type_bit;
823 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
824 switch(render_type_bit)
826 case GLX_RGBA_BIT:
827 render_type = GLX_RGBA_TYPE;
828 break;
829 case GLX_COLOR_INDEX_BIT:
830 render_type = GLX_COLOR_INDEX_TYPE;
831 break;
832 case GLX_RGBA_FLOAT_BIT:
833 render_type = GLX_RGBA_FLOAT_TYPE;
834 break;
835 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
836 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
837 break;
838 default:
839 ERR("Unknown render_type: %x\n", render_type);
841 return render_type;
844 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
845 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
847 int dbuf, value;
848 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
849 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
851 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
852 * the GLX_PIXMAP_BIT set. */
853 return !dbuf && (value & GLX_PIXMAP_BIT);
856 static WineGLPixelFormat *get_formats(Display *display, int *size_ret, int *onscreen_size_ret)
858 static WineGLPixelFormat *list;
859 static int size, onscreen_size;
861 int fmt_id, nCfgs, i, run, bmp_formats;
862 GLXFBConfig* cfgs;
863 XVisualInfo *visinfo;
865 wine_tsx11_lock();
866 if (list) goto done;
868 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
869 if (NULL == cfgs || 0 == nCfgs) {
870 if(cfgs != NULL) XFree(cfgs);
871 wine_tsx11_unlock();
872 ERR("glXChooseFBConfig returns NULL\n");
873 return NULL;
876 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
877 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
878 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
879 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
881 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
883 for(i=0, bmp_formats=0; i<nCfgs; i++)
885 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
886 bmp_formats++;
888 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
890 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats)*sizeof(WineGLPixelFormat));
892 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
893 * Do this as GLX doesn't guarantee that the list is sorted */
894 for(run=0; run < 2; run++)
896 for(i=0; i<nCfgs; i++) {
897 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
898 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
900 /* The first run we only add onscreen formats (ones which have an associated X Visual).
901 * The second run we only set offscreen formats. */
902 if(!run && visinfo)
904 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
905 * The contents is copied to the destination using XCopyArea. For the copying to work
906 * the depth of the source and destination window should be the same. In general this should
907 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
908 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
909 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
910 * list formats with the same depth. */
911 if(visinfo->depth != screen_depth)
913 XFree(visinfo);
914 continue;
917 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
918 list[size].iPixelFormat = size+1; /* The index starts at 1 */
919 list[size].fbconfig = cfgs[i];
920 list[size].fmt_id = fmt_id;
921 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
922 list[size].offscreenOnly = FALSE;
923 list[size].dwFlags = 0;
924 size++;
925 onscreen_size++;
927 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
928 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
930 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
931 list[size].iPixelFormat = size+1; /* The index starts at 1 */
932 list[size].fbconfig = cfgs[i];
933 list[size].fmt_id = fmt_id;
934 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
935 list[size].offscreenOnly = FALSE;
936 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
937 size++;
938 onscreen_size++;
940 } else if(run && !visinfo) {
941 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
942 list[size].iPixelFormat = size+1; /* The index starts at 1 */
943 list[size].fbconfig = cfgs[i];
944 list[size].fmt_id = fmt_id;
945 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
946 list[size].offscreenOnly = TRUE;
947 list[size].dwFlags = 0;
948 size++;
951 if (visinfo) XFree(visinfo);
955 if(cfgs != NULL) XFree(cfgs);
957 done:
958 if (size_ret) *size_ret = size;
959 if (onscreen_size_ret) *onscreen_size_ret = onscreen_size;
960 wine_tsx11_unlock();
961 return list;
964 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
965 * In our WGL implementation we only support a subset of these formats namely the format of
966 * Wine's main visual and offscreen formats (if they are available).
967 * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
968 * and it returns the number of supported WGL formats in fmt_count.
970 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
972 WineGLPixelFormat *list, *res = NULL;
973 int size, onscreen_size;
975 if (!(list = get_formats(display, &size, &onscreen_size ))) return NULL;
977 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
978 * iPixelFormat in case of probing the number of pixelformats.
980 if((iPixelFormat > 0) && (iPixelFormat <= size) &&
981 (!list[iPixelFormat-1].offscreenOnly || AllowOffscreen)) {
982 res = &list[iPixelFormat-1];
983 TRACE("Returning FBConfig=%p for iPixelFormat=%d\n", res->fbconfig, iPixelFormat);
986 if(AllowOffscreen)
987 *fmt_count = size;
988 else
989 *fmt_count = onscreen_size;
991 TRACE("Number of returned pixelformats=%d\n", *fmt_count);
993 return res;
996 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
997 static WineGLPixelFormat* ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id, DWORD dwFlags)
999 WineGLPixelFormat *list;
1000 int i, size;
1002 if (!(list = get_formats(display, &size, NULL ))) return NULL;
1004 for(i=0; i<size; i++) {
1005 /* A GLX format can appear multiple times in the pixel format list due to fake formats for bitmap rendering.
1006 * Fake formats might get selected when the user passes the proper flags using the dwFlags parameter. */
1007 if( (list[i].fmt_id == fmt_id) && ((list[i].dwFlags & dwFlags) == dwFlags) ) {
1008 TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", list[i].iPixelFormat, fmt_id);
1009 return &list[i];
1012 TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1013 return NULL;
1016 int pixelformat_from_fbconfig_id(XID fbconfig_id)
1018 WineGLPixelFormat *fmt;
1020 if (!fbconfig_id) return 0;
1022 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
1023 if(fmt)
1024 return fmt->iPixelFormat;
1025 /* This will happen on hwnds without a pixel format set; it's ok */
1026 return 0;
1030 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1031 void mark_drawable_dirty(Drawable old, Drawable new)
1033 Wine_GLContext *ctx;
1034 for (ctx = context_list; ctx; ctx = ctx->next) {
1035 if (old == ctx->drawables[0]) {
1036 ctx->drawables[0] = new;
1037 ctx->refresh_drawables = TRUE;
1039 if (old == ctx->drawables[1]) {
1040 ctx->drawables[1] = new;
1041 ctx->refresh_drawables = TRUE;
1046 /* Given the current context, make sure its drawable is sync'd */
1047 static inline void sync_context(Wine_GLContext *context)
1049 if(context && context->refresh_drawables) {
1050 if (glxRequireVersion(3))
1051 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1052 context->drawables[1], context->ctx);
1053 else
1054 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1055 context->refresh_drawables = FALSE;
1060 static GLXContext create_glxcontext(Display *display, Wine_GLContext *context, GLXContext shareList)
1062 GLXContext ctx;
1064 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1065 BOOL indirect = (context->fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? FALSE : TRUE;
1067 if(context->vis)
1068 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1069 else /* Create a GLX Context for a pbuffer */
1070 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1072 return ctx;
1076 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
1078 return pglXCreateGLXPixmap(display, vis, parent);
1082 static XID create_bitmap_glxpixmap(X11DRV_PDEVICE *physDev, WineGLPixelFormat *fmt)
1084 GLXPixmap ret = 0;
1085 XVisualInfo *vis;
1087 wine_tsx11_lock();
1089 vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1090 if(vis) {
1091 if(vis->depth == physDev->bitmap->pixmap_depth)
1092 ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
1093 XFree(vis);
1095 wine_tsx11_unlock();
1096 TRACE("return %lx\n", ret);
1097 return ret;
1101 * X11DRV_ChoosePixelFormat
1103 * Equivalent to glXChooseVisual.
1105 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
1106 const PIXELFORMATDESCRIPTOR *ppfd) {
1107 WineGLPixelFormat *list;
1108 int onscreen_size;
1109 int ret = 0;
1110 int value = 0;
1111 int i = 0;
1112 int bestFormat = -1;
1113 int bestDBuffer = -1;
1114 int bestStereo = -1;
1115 int bestColor = -1;
1116 int bestAlpha = -1;
1117 int bestDepth = -1;
1118 int bestStencil = -1;
1119 int bestAux = -1;
1121 if (!has_opengl()) return 0;
1123 if (TRACE_ON(wgl)) {
1124 TRACE("(%p,%p)\n", physDev, ppfd);
1126 dump_PIXELFORMATDESCRIPTOR(ppfd);
1129 if (!(list = get_formats(gdi_display, NULL, &onscreen_size ))) return 0;
1131 wine_tsx11_lock();
1132 for(i=0; i<onscreen_size; i++)
1134 int dwFlags = 0;
1135 int iPixelType = 0;
1136 int alpha=0, color=0, depth=0, stencil=0, aux=0;
1137 WineGLPixelFormat *fmt = &list[i];
1139 /* Pixel type */
1140 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1141 if (value & GLX_RGBA_BIT)
1142 iPixelType = PFD_TYPE_RGBA;
1143 else
1144 iPixelType = PFD_TYPE_COLORINDEX;
1146 if (ppfd->iPixelType != iPixelType)
1148 TRACE("pixel type mismatch for iPixelFormat=%d\n", i+1);
1149 continue;
1152 /* Only use bitmap capable for formats for bitmap rendering.
1153 * See get_formats for more info. */
1154 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (fmt->dwFlags & PFD_DRAW_TO_BITMAP))
1156 TRACE("PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i+1);
1157 continue;
1160 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1161 if (value) dwFlags |= PFD_DOUBLEBUFFER;
1162 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value);
1163 if (value) dwFlags |= PFD_STEREO;
1164 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &color); /* cColorBits */
1165 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &alpha); /* cAlphaBits */
1166 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &depth); /* cDepthBits */
1167 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &stencil); /* cStencilBits */
1168 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &aux); /* cAuxBuffers */
1170 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
1171 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
1172 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
1173 * formats without the given flag set.
1174 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
1175 * has indicated that a format without stereo is returned when stereo is unavailable.
1176 * So in case PFD_STEREO is set, formats that support it should have priority above formats
1177 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
1179 * To summarize the following is most likely the correct behavior:
1180 * stereo not set -> prefer no-stereo formats, else also accept stereo formats
1181 * stereo set -> prefer stereo formats, else also accept no-stereo formats
1182 * stereo don't care -> it doesn't matter whether we get stereo or not
1184 * In Wine we will treat no-stereo the same way as don't care because it makes
1185 * format selection even more complicated and second drivers with Stereo advertise
1186 * each format twice anyway.
1189 /* Doublebuffer, see the comments above */
1190 if( !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) ) {
1191 if( ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
1192 ((dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)) )
1194 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1195 bestStereo = dwFlags & PFD_STEREO;
1196 bestAlpha = alpha;
1197 bestColor = color;
1198 bestDepth = depth;
1199 bestStencil = stencil;
1200 bestAux = aux;
1201 bestFormat = i;
1202 continue;
1204 if(bestDBuffer != -1 && (dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer)
1205 continue;
1208 /* Stereo, see the comments above. */
1209 if( !(ppfd->dwFlags & PFD_STEREO_DONTCARE) ) {
1210 if( ((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
1211 ((dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)) )
1213 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1214 bestStereo = dwFlags & PFD_STEREO;
1215 bestAlpha = alpha;
1216 bestColor = color;
1217 bestDepth = depth;
1218 bestStencil = stencil;
1219 bestAux = aux;
1220 bestFormat = i;
1221 continue;
1223 if(bestStereo != -1 && (dwFlags & PFD_STEREO) != bestStereo)
1224 continue;
1227 /* Below we will do a number of checks to select the 'best' pixelformat.
1228 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
1229 * The code works by trying to match the most important options as close as possible.
1230 * When a reasonable format is found, we will try to match more options.
1231 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
1232 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
1233 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
1235 /* Color bits */
1236 if(ppfd->cColorBits) {
1237 if( ((ppfd->cColorBits > bestColor) && (color > bestColor)) ||
1238 ((color >= ppfd->cColorBits) && (color < bestColor)) )
1240 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1241 bestStereo = dwFlags & PFD_STEREO;
1242 bestAlpha = alpha;
1243 bestColor = color;
1244 bestDepth = depth;
1245 bestStencil = stencil;
1246 bestAux = aux;
1247 bestFormat = i;
1248 continue;
1249 } else if(bestColor != color) { /* Do further checks if the format is compatible */
1250 TRACE("color mismatch for iPixelFormat=%d\n", i+1);
1251 continue;
1255 /* Alpha bits */
1256 if(ppfd->cAlphaBits) {
1257 if( ((ppfd->cAlphaBits > bestAlpha) && (alpha > bestAlpha)) ||
1258 ((alpha >= ppfd->cAlphaBits) && (alpha < bestAlpha)) )
1260 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1261 bestStereo = dwFlags & PFD_STEREO;
1262 bestAlpha = alpha;
1263 bestColor = color;
1264 bestDepth = depth;
1265 bestStencil = stencil;
1266 bestAux = aux;
1267 bestFormat = i;
1268 continue;
1269 } else if(bestAlpha != alpha) {
1270 TRACE("alpha mismatch for iPixelFormat=%d\n", i+1);
1271 continue;
1275 /* Depth bits */
1276 if(ppfd->cDepthBits) {
1277 if( ((ppfd->cDepthBits > bestDepth) && (depth > bestDepth)) ||
1278 ((depth >= ppfd->cDepthBits) && (depth < bestDepth)) )
1280 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1281 bestStereo = dwFlags & PFD_STEREO;
1282 bestAlpha = alpha;
1283 bestColor = color;
1284 bestDepth = depth;
1285 bestStencil = stencil;
1286 bestAux = aux;
1287 bestFormat = i;
1288 continue;
1289 } else if(bestDepth != depth) {
1290 TRACE("depth mismatch for iPixelFormat=%d\n", i+1);
1291 continue;
1295 /* Stencil bits */
1296 if(ppfd->cStencilBits) {
1297 if( ((ppfd->cStencilBits > bestStencil) && (stencil > bestStencil)) ||
1298 ((stencil >= ppfd->cStencilBits) && (stencil < bestStencil)) )
1300 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1301 bestStereo = dwFlags & PFD_STEREO;
1302 bestAlpha = alpha;
1303 bestColor = color;
1304 bestDepth = depth;
1305 bestStencil = stencil;
1306 bestAux = aux;
1307 bestFormat = i;
1308 continue;
1309 } else if(bestStencil != stencil) {
1310 TRACE("stencil mismatch for iPixelFormat=%d\n", i+1);
1311 continue;
1315 /* Aux buffers */
1316 if(ppfd->cAuxBuffers) {
1317 if( ((ppfd->cAuxBuffers > bestAux) && (aux > bestAux)) ||
1318 ((aux >= ppfd->cAuxBuffers) && (aux < bestAux)) )
1320 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1321 bestStereo = dwFlags & PFD_STEREO;
1322 bestAlpha = alpha;
1323 bestColor = color;
1324 bestDepth = depth;
1325 bestStencil = stencil;
1326 bestAux = aux;
1327 bestFormat = i;
1328 continue;
1329 } else if(bestAux != aux) {
1330 TRACE("aux mismatch for iPixelFormat=%d\n", i+1);
1331 continue;
1336 if(bestFormat == -1) {
1337 TRACE("No matching mode was found returning 0\n");
1338 ret = 0;
1340 else {
1341 ret = bestFormat+1; /* the return value should be a 1-based index */
1342 TRACE("Successfully found a matching mode, returning index: %d %x\n", ret, list[bestFormat].fmt_id);
1345 wine_tsx11_unlock();
1347 return ret;
1350 * X11DRV_DescribePixelFormat
1352 * Get the pixel-format descriptor associated to the given id
1354 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
1355 int iPixelFormat,
1356 UINT nBytes,
1357 PIXELFORMATDESCRIPTOR *ppfd) {
1358 /*XVisualInfo *vis;*/
1359 int value;
1360 int rb,gb,bb,ab;
1361 WineGLPixelFormat *fmt;
1362 int ret = 0;
1363 int fmt_count = 0;
1365 if (!has_opengl()) return 0;
1367 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
1369 /* 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 */
1370 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1371 if (ppfd == NULL) {
1372 /* The application is only querying the number of pixelformats */
1373 return fmt_count;
1374 } else if(fmt == NULL) {
1375 WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1376 return 0;
1379 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1380 ERR("Wrong structure size !\n");
1381 /* Should set error */
1382 return 0;
1385 ret = fmt_count;
1387 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1388 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1389 ppfd->nVersion = 1;
1391 /* These flags are always the same... */
1392 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1393 /* Now the flags extracted from the Visual */
1395 wine_tsx11_lock();
1397 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1398 if(value & GLX_WINDOW_BIT)
1399 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1401 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1402 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1403 * 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
1404 * offered the GDI bit either. */
1405 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1407 /* PFD_GENERIC_FORMAT - gdi software rendering
1408 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1409 * none set - full hardware accelerated by a ICD
1411 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1412 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1414 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1415 if (value) {
1416 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1417 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1419 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1421 /* Pixel type */
1422 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1423 if (value & GLX_RGBA_BIT)
1424 ppfd->iPixelType = PFD_TYPE_RGBA;
1425 else
1426 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1428 /* Color bits */
1429 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1430 ppfd->cColorBits = value;
1432 /* Red, green, blue and alpha bits / shifts */
1433 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1434 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1435 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1436 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1437 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1439 ppfd->cRedBits = rb;
1440 ppfd->cRedShift = gb + bb + ab;
1441 ppfd->cBlueBits = bb;
1442 ppfd->cBlueShift = ab;
1443 ppfd->cGreenBits = gb;
1444 ppfd->cGreenShift = bb + ab;
1445 ppfd->cAlphaBits = ab;
1446 ppfd->cAlphaShift = 0;
1447 } else {
1448 ppfd->cRedBits = 0;
1449 ppfd->cRedShift = 0;
1450 ppfd->cBlueBits = 0;
1451 ppfd->cBlueShift = 0;
1452 ppfd->cGreenBits = 0;
1453 ppfd->cGreenShift = 0;
1454 ppfd->cAlphaBits = 0;
1455 ppfd->cAlphaShift = 0;
1458 /* Accum RGBA bits */
1459 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1460 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1461 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1462 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1464 ppfd->cAccumBits = rb+gb+bb+ab;
1465 ppfd->cAccumRedBits = rb;
1466 ppfd->cAccumGreenBits = gb;
1467 ppfd->cAccumBlueBits = bb;
1468 ppfd->cAccumAlphaBits = ab;
1470 /* Aux bits */
1471 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1472 ppfd->cAuxBuffers = value;
1474 /* Depth bits */
1475 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1476 ppfd->cDepthBits = value;
1478 /* stencil bits */
1479 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1480 ppfd->cStencilBits = value;
1482 wine_tsx11_unlock();
1484 ppfd->iLayerType = PFD_MAIN_PLANE;
1486 if (TRACE_ON(wgl)) {
1487 dump_PIXELFORMATDESCRIPTOR(ppfd);
1490 return ret;
1494 * X11DRV_GetPixelFormat
1496 * Get the pixel-format id used by this DC
1498 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1499 WineGLPixelFormat *fmt;
1500 int tmp;
1501 TRACE("(%p)\n", physDev);
1503 if (!physDev->current_pf) return 0; /* not set yet */
1505 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE, &tmp);
1506 if(!fmt)
1508 ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physDev->current_pf);
1509 return 0;
1511 else if(fmt->offscreenOnly)
1513 /* Offscreen formats can't be used with traditional WGL calls.
1514 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1515 TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1516 return 1;
1519 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1520 return physDev->current_pf;
1523 /* This function is the core of X11DRV_SetPixelFormat and X11DRV_SetPixelFormatWINE.
1524 * Both functions are the same except that X11DRV_SetPixelFormatWINE allows you to
1525 * set the pixel format multiple times. */
1526 static BOOL internal_SetPixelFormat(X11DRV_PDEVICE *physDev,
1527 int iPixelFormat,
1528 const PIXELFORMATDESCRIPTOR *ppfd) {
1529 WineGLPixelFormat *fmt;
1530 int value;
1531 HWND hwnd;
1533 /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1534 if(get_glxdrawable(physDev) == root_window)
1536 ERR("Invalid operation on root_window\n");
1537 return FALSE;
1540 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1541 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1542 if(!fmt) {
1543 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1544 return FALSE;
1547 wine_tsx11_lock();
1548 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1549 wine_tsx11_unlock();
1551 hwnd = WindowFromDC(physDev->hdc);
1552 if(hwnd) {
1553 if(!(value&GLX_WINDOW_BIT)) {
1554 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1555 return FALSE;
1558 if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, (WPARAM)fmt->fmt_id, 0)) {
1559 ERR("Couldn't set format of the window, returning failure\n");
1560 return FALSE;
1563 else if(physDev->bitmap) {
1564 if(!(value&GLX_PIXMAP_BIT)) {
1565 WARN("Pixel format %d is not compatible for bitmap rendering\n", iPixelFormat);
1566 return FALSE;
1569 physDev->bitmap->glxpixmap = create_bitmap_glxpixmap(physDev, fmt);
1570 if(!physDev->bitmap->glxpixmap) {
1571 WARN("Couldn't create glxpixmap for pixel format %d\n", iPixelFormat);
1572 return FALSE;
1575 else {
1576 FIXME("called on a non-window, non-bitmap object?\n");
1579 physDev->current_pf = iPixelFormat;
1581 if (TRACE_ON(wgl)) {
1582 int gl_test = 0;
1584 wine_tsx11_lock();
1585 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1586 if (gl_test) {
1587 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1588 } else {
1589 TRACE(" FBConfig have :\n");
1590 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1591 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1592 TRACE(" - VISUAL_ID 0x%x\n", value);
1593 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1594 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1596 wine_tsx11_unlock();
1598 return TRUE;
1603 * X11DRV_SetPixelFormat
1605 * Set the pixel-format id used by this DC
1607 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1608 int iPixelFormat,
1609 const PIXELFORMATDESCRIPTOR *ppfd) {
1610 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1612 if (!has_opengl()) return FALSE;
1614 if(physDev->current_pf) /* cannot change it if already set */
1615 return (physDev->current_pf == iPixelFormat);
1617 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
1621 * X11DRV_wglCopyContext
1623 * For OpenGL32 wglCopyContext.
1625 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
1626 Wine_GLContext *src = (Wine_GLContext*)hglrcSrc;
1627 Wine_GLContext *dst = (Wine_GLContext*)hglrcDst;
1629 TRACE("hglrcSrc: (%p), hglrcDst: (%p), mask: %#x\n", hglrcSrc, hglrcDst, mask);
1631 /* There is a slight difference in the way GL contexts share display lists in WGL and GLX.
1632 * In case of GLX you need to specify this at context creation time but in case of WGL you
1633 * do this using wglShareLists which you can call after creating the context.
1634 * To emulate WGL we try to delay the creation of the context until wglShareLists or wglMakeCurrent.
1635 * Up to now that works fine.
1637 * The delayed GLX context creation could cause issues for wglCopyContext as it might get called
1638 * when there is no GLX context yet. The chance this will cause problems is small as at the time of
1639 * writing Wine has had OpenGL support for more than 7 years and this function has remained a stub
1640 * ever since then.
1642 if(!src->ctx || !dst->ctx) {
1643 /* NOTE: As a special case, if both GLX contexts are NULL, that means
1644 * neither WGL context was made current. In that case, both contexts
1645 * are in a default state, so any copy would no-op.
1647 if(!src->ctx && !dst->ctx) {
1648 TRACE("No source or destination contexts set. No-op.\n");
1649 return TRUE;
1652 if (!src->ctx) {
1653 wine_tsx11_lock();
1654 src->ctx = create_glxcontext(gdi_display, src, NULL);
1655 TRACE(" created a delayed OpenGL context (%p)\n", src->ctx);
1657 else if (!dst->ctx) {
1658 wine_tsx11_lock();
1659 dst->ctx = create_glxcontext(gdi_display, dst, NULL);
1660 TRACE(" created a delayed OpenGL context (%p)\n", dst->ctx);
1663 else
1664 wine_tsx11_lock();
1666 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1667 wine_tsx11_unlock();
1669 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1670 return TRUE;
1674 * X11DRV_wglCreateContext
1676 * For OpenGL32 wglCreateContext.
1678 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1680 Wine_GLContext *ret;
1681 WineGLPixelFormat *fmt;
1682 int hdcPF = physDev->current_pf;
1683 int fmt_count = 0;
1684 HDC hdc = physDev->hdc;
1686 TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1688 if (!has_opengl()) return 0;
1690 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
1691 /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1692 * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1693 * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1694 * If this fails something is very wrong on the system. */
1695 if(!fmt) {
1696 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
1697 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1698 return NULL;
1701 /* The context will be allocated in the wglMakeCurrent call */
1702 wine_tsx11_lock();
1703 ret = alloc_context();
1704 wine_tsx11_unlock();
1705 ret->hdc = hdc;
1706 ret->fmt = fmt;
1708 /*ret->vis = vis;*/
1709 ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1711 TRACE(" creating context %p (GL context creation delayed)\n", ret);
1712 return (HGLRC) ret;
1716 * X11DRV_wglDeleteContext
1718 * For OpenGL32 wglDeleteContext.
1720 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc)
1722 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1723 BOOL ret = TRUE;
1725 TRACE("(%p)\n", hglrc);
1727 if (!has_opengl()) return 0;
1729 wine_tsx11_lock();
1730 /* A game (Half Life not to name it) deletes twice the same context,
1731 * so make sure it is valid first */
1732 if (is_valid_context( ctx ))
1734 if (ctx->ctx) pglXDestroyContext(gdi_display, ctx->ctx);
1735 free_context(ctx);
1737 else
1739 WARN("Error deleting context !\n");
1740 SetLastError(ERROR_INVALID_HANDLE);
1741 ret = FALSE;
1743 wine_tsx11_unlock();
1745 return ret;
1749 * X11DRV_wglGetCurrentReadDCARB
1751 * For OpenGL32 wglGetCurrentReadDCARB.
1753 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void)
1755 HDC ret = 0;
1756 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
1758 if (ctx) ret = ctx->read_hdc;
1760 TRACE(" returning %p (GL drawable %lu)\n", ret, ctx ? ctx->drawables[1] : 0);
1761 return ret;
1765 * X11DRV_wglGetProcAddress
1767 * For OpenGL32 wglGetProcAddress.
1769 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1771 int i, j;
1772 const WineGLExtension *ext;
1774 int padding = 32 - strlen(lpszProc);
1775 if (padding < 0)
1776 padding = 0;
1778 if (!has_opengl()) return NULL;
1780 /* Check the table of WGL extensions to see if we need to return a WGL extension
1781 * or a function pointer to a native OpenGL function. */
1782 if(strncmp(lpszProc, "wgl", 3) != 0) {
1783 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1784 } else {
1785 TRACE("('%s'):%*s", lpszProc, padding, " ");
1786 for (i = 0; i < WineGLExtensionListSize; ++i) {
1787 ext = WineGLExtensionList[i];
1788 for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1789 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1790 TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1791 return ext->extEntryPoints[j].funcAddress;
1797 WARN("(%s) - not found\n", lpszProc);
1798 return NULL;
1802 * X11DRV_wglMakeCurrent
1804 * For OpenGL32 wglMakeCurrent.
1806 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1807 BOOL ret;
1808 HDC hdc = physDev->hdc;
1809 DWORD type = GetObjectType(hdc);
1810 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1812 TRACE("(%p,%p)\n", hdc, hglrc);
1814 if (!has_opengl()) return FALSE;
1816 wine_tsx11_lock();
1817 if (hglrc == NULL) {
1818 ret = pglXMakeCurrent(gdi_display, None, NULL);
1819 NtCurrentTeb()->glContext = NULL;
1820 } else if (ctx->fmt->iPixelFormat != physDev->current_pf) {
1821 WARN( "mismatched pixel format hdc %p %u ctx %p %u\n",
1822 hdc, physDev->current_pf, ctx, ctx->fmt->iPixelFormat );
1823 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1824 ret = FALSE;
1825 } else {
1826 Drawable drawable = get_glxdrawable(physDev);
1827 if (ctx->ctx == NULL) {
1828 /* The describe lines below are for debugging purposes only */
1829 if (TRACE_ON(wgl)) {
1830 describeDrawable(physDev);
1831 describeContext(ctx);
1834 /* Create a GLX context using the same visual as chosen earlier in wglCreateContext.
1835 * We are certain that the drawable and context are compatible as we only allow compatible formats.
1837 TRACE(" Creating GLX Context\n");
1838 ctx->ctx = create_glxcontext(gdi_display, ctx, NULL);
1839 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1841 TRACE(" make current for dis %p, drawable %p, ctx %p\n", gdi_display, (void*) drawable, ctx->ctx);
1842 ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1843 NtCurrentTeb()->glContext = ctx;
1844 if(ret)
1846 ctx->hdc = hdc;
1847 ctx->read_hdc = hdc;
1848 ctx->drawables[0] = drawable;
1849 ctx->drawables[1] = drawable;
1850 ctx->refresh_drawables = FALSE;
1852 if (type == OBJ_MEMDC)
1854 ctx->do_escape = TRUE;
1855 pglDrawBuffer(GL_FRONT_LEFT);
1859 wine_tsx11_unlock();
1860 TRACE(" returning %s\n", (ret ? "True" : "False"));
1861 return ret;
1865 * X11DRV_wglMakeContextCurrentARB
1867 * For OpenGL32 wglMakeContextCurrentARB
1869 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* pDrawDev, X11DRV_PDEVICE* pReadDev, HGLRC hglrc)
1871 BOOL ret;
1873 TRACE("(%p,%p,%p)\n", pDrawDev, pReadDev, hglrc);
1875 if (!has_opengl()) return 0;
1877 wine_tsx11_lock();
1878 if (hglrc == NULL) {
1879 ret = pglXMakeCurrent(gdi_display, None, NULL);
1880 NtCurrentTeb()->glContext = NULL;
1881 } else {
1882 if (NULL == pglXMakeContextCurrent) {
1883 ret = FALSE;
1884 } else {
1885 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1886 Drawable d_draw = get_glxdrawable(pDrawDev);
1887 Drawable d_read = get_glxdrawable(pReadDev);
1889 if (ctx->ctx == NULL) {
1890 ctx->ctx = create_glxcontext(gdi_display, ctx, NULL);
1891 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1893 ctx->hdc = pDrawDev->hdc;
1894 ctx->read_hdc = pReadDev->hdc;
1895 ctx->drawables[0] = d_draw;
1896 ctx->drawables[1] = d_read;
1897 ctx->refresh_drawables = FALSE;
1898 ret = pglXMakeContextCurrent(gdi_display, d_draw, d_read, ctx->ctx);
1899 NtCurrentTeb()->glContext = ctx;
1902 wine_tsx11_unlock();
1904 TRACE(" returning %s\n", (ret ? "True" : "False"));
1905 return ret;
1909 * X11DRV_wglShareLists
1911 * For OpenGL32 wglShareLists.
1913 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1914 Wine_GLContext *org = (Wine_GLContext *) hglrc1;
1915 Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1917 TRACE("(%p, %p)\n", org, dest);
1919 if (!has_opengl()) return FALSE;
1921 if (NULL != dest && dest->ctx != NULL) {
1922 ERR("Could not share display lists, context already created !\n");
1923 return FALSE;
1924 } else {
1925 if(org && dest && (GetObjectType(org->hdc) == OBJ_MEMDC) ^ (GetObjectType(dest->hdc) == OBJ_MEMDC)) {
1926 WARN("Attempting to share a context between a direct and indirect rendering context, expect issues!\n");
1929 if (org->ctx == NULL) {
1930 wine_tsx11_lock();
1931 describeContext(org);
1933 org->ctx = create_glxcontext(gdi_display, org, NULL);
1934 wine_tsx11_unlock();
1935 TRACE(" created a delayed OpenGL context (%p) for Wine context %p\n", org->ctx, org);
1937 if (NULL != dest) {
1938 wine_tsx11_lock();
1939 describeContext(dest);
1940 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1941 wine_tsx11_unlock();
1942 TRACE(" created a delayed OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1943 return TRUE;
1946 return FALSE;
1949 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1951 /* We are running using client-side rendering fonts... */
1952 GLYPHMETRICS gm;
1953 unsigned int glyph, size = 0;
1954 void *bitmap = NULL, *gl_bitmap = NULL;
1955 int org_alignment;
1957 wine_tsx11_lock();
1958 pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1959 pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1960 wine_tsx11_unlock();
1962 for (glyph = first; glyph < first + count; glyph++) {
1963 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1964 unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1965 unsigned int height, width_int;
1967 TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1968 if (needed_size == GDI_ERROR) {
1969 TRACE(" - needed size : %d (GDI_ERROR)\n", needed_size);
1970 goto error;
1971 } else {
1972 TRACE(" - needed size : %d\n", needed_size);
1975 if (needed_size > size) {
1976 size = needed_size;
1977 HeapFree(GetProcessHeap(), 0, bitmap);
1978 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1979 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1980 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1982 if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) == GDI_ERROR)
1983 goto error;
1984 if (TRACE_ON(wgl)) {
1985 unsigned int height, width, bitmask;
1986 unsigned char *bitmap_ = bitmap;
1988 TRACE(" - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1989 TRACE(" - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1990 TRACE(" - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1991 if (needed_size != 0) {
1992 TRACE(" - bitmap :\n");
1993 for (height = 0; height < gm.gmBlackBoxY; height++) {
1994 TRACE(" ");
1995 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1996 if (bitmask == 0) {
1997 bitmap_ += 1;
1998 bitmask = 0x80;
2000 if (*bitmap_ & bitmask)
2001 TRACE("*");
2002 else
2003 TRACE(" ");
2005 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
2006 TRACE("\n");
2011 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
2012 * glyph for it to be drawn properly.
2014 if (needed_size != 0) {
2015 width_int = (gm.gmBlackBoxX + 31) / 32;
2016 for (height = 0; height < gm.gmBlackBoxY; height++) {
2017 unsigned int width;
2018 for (width = 0; width < width_int; width++) {
2019 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
2020 ((int *) bitmap)[height * width_int + width];
2025 wine_tsx11_lock();
2026 pglNewList(listBase++, GL_COMPILE);
2027 if (needed_size != 0) {
2028 pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
2029 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
2030 gm.gmCellIncX, gm.gmCellIncY,
2031 gl_bitmap);
2032 } else {
2033 /* This is the case of 'empty' glyphs like the space character */
2034 pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
2036 pglEndList();
2037 wine_tsx11_unlock();
2040 wine_tsx11_lock();
2041 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2042 wine_tsx11_unlock();
2044 HeapFree(GetProcessHeap(), 0, bitmap);
2045 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2046 return TRUE;
2048 error:
2049 wine_tsx11_lock();
2050 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2051 wine_tsx11_unlock();
2053 HeapFree(GetProcessHeap(), 0, bitmap);
2054 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2055 return FALSE;
2059 * X11DRV_wglUseFontBitmapsA
2061 * For OpenGL32 wglUseFontBitmapsA.
2063 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2065 Font fid = physDev->font;
2067 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2069 if (!has_opengl()) return FALSE;
2071 if (fid == 0) {
2072 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
2075 wine_tsx11_lock();
2076 /* I assume that the glyphs are at the same position for X and for Windows */
2077 pglXUseXFont(fid, first, count, listBase);
2078 wine_tsx11_unlock();
2079 return TRUE;
2083 * X11DRV_wglUseFontBitmapsW
2085 * For OpenGL32 wglUseFontBitmapsW.
2087 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2089 Font fid = physDev->font;
2091 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2093 if (!has_opengl()) return FALSE;
2095 if (fid == 0) {
2096 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
2099 WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
2101 wine_tsx11_lock();
2102 /* I assume that the glyphs are at the same position for X and for Windows */
2103 pglXUseXFont(fid, first, count, listBase);
2104 wine_tsx11_unlock();
2105 return TRUE;
2108 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
2109 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
2111 wine_tsx11_lock();
2112 switch(pname)
2114 case GL_DEPTH_BITS:
2116 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2118 pglGetIntegerv(pname, params);
2120 * if we cannot find a Wine Context
2121 * we only have the default wine desktop context,
2122 * so if we have only a 24 depth say we have 32
2124 if (!ctx && *params == 24) {
2125 *params = 32;
2127 TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
2128 break;
2130 case GL_ALPHA_BITS:
2132 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2134 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_ALPHA_SIZE, params);
2135 TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
2136 break;
2138 default:
2139 pglGetIntegerv(pname, params);
2140 break;
2142 wine_tsx11_unlock();
2145 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
2147 int w, h;
2149 if (!physDev->gl_copy)
2150 return;
2152 w = physDev->dc_rect.right - physDev->dc_rect.left;
2153 h = physDev->dc_rect.bottom - physDev->dc_rect.top;
2155 if(w > 0 && h > 0) {
2156 Drawable src = physDev->pixmap;
2157 if(!src) src = physDev->gl_drawable;
2159 /* The GL drawable may be lagged behind if we don't flush first, so
2160 * flush the display make sure we copy up-to-date data */
2161 wine_tsx11_lock();
2162 XFlush(gdi_display);
2163 XSetFunction(gdi_display, physDev->gc, GXcopy);
2164 XCopyArea(gdi_display, src, physDev->drawable, physDev->gc, 0, 0, w, h,
2165 physDev->dc_rect.left, physDev->dc_rect.top);
2166 wine_tsx11_unlock();
2171 static void WINAPI X11DRV_wglFinish(void)
2173 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2174 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2176 wine_tsx11_lock();
2177 sync_context(ctx);
2178 pglFinish();
2179 wine_tsx11_unlock();
2180 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2183 static void WINAPI X11DRV_wglFlush(void)
2185 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2186 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2188 wine_tsx11_lock();
2189 sync_context(ctx);
2190 pglFlush();
2191 wine_tsx11_unlock();
2192 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2196 * X11DRV_wglGetExtensionsStringARB
2198 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2200 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
2201 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2202 return WineGLInfo.wglExtensions;
2206 * X11DRV_wglCreatePbufferARB
2208 * WGL_ARB_pbuffer: wglCreatePbufferARB
2210 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
2212 Wine_GLPBuffer* object = NULL;
2213 WineGLPixelFormat *fmt = NULL;
2214 int nCfgs = 0;
2215 int attribs[256];
2216 int nAttribs = 0;
2218 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2220 if (0 >= iPixelFormat) {
2221 ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2222 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2223 return NULL; /* unexpected error */
2226 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2227 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2228 if(!fmt) {
2229 ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2230 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2231 goto create_failed; /* unexpected error */
2234 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2235 if (NULL == object) {
2236 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2237 goto create_failed; /* unexpected error */
2239 object->hdc = hdc;
2240 object->display = gdi_display;
2241 object->width = iWidth;
2242 object->height = iHeight;
2243 object->fmt = fmt;
2245 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2246 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2247 while (piAttribList && 0 != *piAttribList) {
2248 int attr_v;
2249 switch (*piAttribList) {
2250 case WGL_PBUFFER_LARGEST_ARB: {
2251 ++piAttribList;
2252 attr_v = *piAttribList;
2253 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2254 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2255 break;
2258 case WGL_TEXTURE_FORMAT_ARB: {
2259 ++piAttribList;
2260 attr_v = *piAttribList;
2261 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2262 if (use_render_texture_ati) {
2263 int type = 0;
2264 switch (attr_v) {
2265 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2266 case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2267 case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2268 default:
2269 SetLastError(ERROR_INVALID_DATA);
2270 goto create_failed;
2272 object->use_render_texture = 1;
2273 PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2274 } else {
2275 if (WGL_NO_TEXTURE_ARB == attr_v) {
2276 object->use_render_texture = 0;
2277 } else {
2278 if (!use_render_texture_emulation) {
2279 SetLastError(ERROR_INVALID_DATA);
2280 goto create_failed;
2282 switch (attr_v) {
2283 case WGL_TEXTURE_RGB_ARB:
2284 object->use_render_texture = GL_RGB;
2285 object->texture_bpp = 3;
2286 object->texture_format = GL_RGB;
2287 object->texture_type = GL_UNSIGNED_BYTE;
2288 break;
2289 case WGL_TEXTURE_RGBA_ARB:
2290 object->use_render_texture = GL_RGBA;
2291 object->texture_bpp = 4;
2292 object->texture_format = GL_RGBA;
2293 object->texture_type = GL_UNSIGNED_BYTE;
2294 break;
2296 /* WGL_FLOAT_COMPONENTS_NV */
2297 case WGL_TEXTURE_FLOAT_R_NV:
2298 object->use_render_texture = GL_FLOAT_R_NV;
2299 object->texture_bpp = 4;
2300 object->texture_format = GL_RED;
2301 object->texture_type = GL_FLOAT;
2302 break;
2303 case WGL_TEXTURE_FLOAT_RG_NV:
2304 object->use_render_texture = GL_FLOAT_RG_NV;
2305 object->texture_bpp = 8;
2306 object->texture_format = GL_LUMINANCE_ALPHA;
2307 object->texture_type = GL_FLOAT;
2308 break;
2309 case WGL_TEXTURE_FLOAT_RGB_NV:
2310 object->use_render_texture = GL_FLOAT_RGB_NV;
2311 object->texture_bpp = 12;
2312 object->texture_format = GL_RGB;
2313 object->texture_type = GL_FLOAT;
2314 break;
2315 case WGL_TEXTURE_FLOAT_RGBA_NV:
2316 object->use_render_texture = GL_FLOAT_RGBA_NV;
2317 object->texture_bpp = 16;
2318 object->texture_format = GL_RGBA;
2319 object->texture_type = GL_FLOAT;
2320 break;
2321 default:
2322 ERR("Unknown texture format: %x\n", attr_v);
2323 SetLastError(ERROR_INVALID_DATA);
2324 goto create_failed;
2328 break;
2331 case WGL_TEXTURE_TARGET_ARB: {
2332 ++piAttribList;
2333 attr_v = *piAttribList;
2334 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2335 if (use_render_texture_ati) {
2336 int type = 0;
2337 switch (attr_v) {
2338 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2339 case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2340 case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2341 case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2342 default:
2343 SetLastError(ERROR_INVALID_DATA);
2344 goto create_failed;
2346 PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2347 } else {
2348 if (WGL_NO_TEXTURE_ARB == attr_v) {
2349 object->texture_target = 0;
2350 } else {
2351 if (!use_render_texture_emulation) {
2352 SetLastError(ERROR_INVALID_DATA);
2353 goto create_failed;
2355 switch (attr_v) {
2356 case WGL_TEXTURE_CUBE_MAP_ARB: {
2357 if (iWidth != iHeight) {
2358 SetLastError(ERROR_INVALID_DATA);
2359 goto create_failed;
2361 object->texture_target = GL_TEXTURE_CUBE_MAP;
2362 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2363 break;
2365 case WGL_TEXTURE_1D_ARB: {
2366 if (1 != iHeight) {
2367 SetLastError(ERROR_INVALID_DATA);
2368 goto create_failed;
2370 object->texture_target = GL_TEXTURE_1D;
2371 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2372 break;
2374 case WGL_TEXTURE_2D_ARB: {
2375 object->texture_target = GL_TEXTURE_2D;
2376 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2377 break;
2379 case WGL_TEXTURE_RECTANGLE_NV: {
2380 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2381 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2382 break;
2384 default:
2385 ERR("Unknown texture target: %x\n", attr_v);
2386 SetLastError(ERROR_INVALID_DATA);
2387 goto create_failed;
2391 break;
2394 case WGL_MIPMAP_TEXTURE_ARB: {
2395 ++piAttribList;
2396 attr_v = *piAttribList;
2397 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2398 if (use_render_texture_ati) {
2399 PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2400 } else {
2401 if (!use_render_texture_emulation) {
2402 SetLastError(ERROR_INVALID_DATA);
2403 goto create_failed;
2406 break;
2409 ++piAttribList;
2412 PUSH1(attribs, None);
2413 wine_tsx11_lock();
2414 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2415 wine_tsx11_unlock();
2416 TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
2417 if (!object->drawable) {
2418 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2419 goto create_failed; /* unexpected error */
2421 TRACE("->(%p)\n", object);
2422 return object;
2424 create_failed:
2425 HeapFree(GetProcessHeap(), 0, object);
2426 TRACE("->(FAILED)\n");
2427 return NULL;
2431 * X11DRV_wglDestroyPbufferARB
2433 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2435 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2437 Wine_GLPBuffer* object = hPbuffer;
2438 TRACE("(%p)\n", hPbuffer);
2439 if (NULL == object) {
2440 SetLastError(ERROR_INVALID_HANDLE);
2441 return GL_FALSE;
2443 wine_tsx11_lock();
2444 pglXDestroyPbuffer(object->display, object->drawable);
2445 wine_tsx11_unlock();
2446 HeapFree(GetProcessHeap(), 0, object);
2447 return GL_TRUE;
2451 * X11DRV_wglGetPbufferDCARB
2453 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2454 * The function wglGetPbufferDCARB returns a device context for a pbuffer.
2455 * Gdi32 implements the part of this function which creates a device context.
2456 * This part associates the physDev with the X drawable of the pbuffer.
2458 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
2460 Wine_GLPBuffer* object = hPbuffer;
2461 if (NULL == object) {
2462 SetLastError(ERROR_INVALID_HANDLE);
2463 return NULL;
2466 /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
2467 * All formats in our pixelformat list are compatible with each other and the main drawable. */
2468 physDev->current_pf = object->fmt->iPixelFormat;
2469 physDev->drawable = object->drawable;
2470 SetRect( &physDev->drawable_rect, 0, 0, object->width, object->height );
2471 physDev->dc_rect = physDev->drawable_rect;
2473 TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
2474 return physDev->hdc;
2478 * X11DRV_wglQueryPbufferARB
2480 * WGL_ARB_pbuffer: wglQueryPbufferARB
2482 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2484 Wine_GLPBuffer* object = hPbuffer;
2485 TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2486 if (NULL == object) {
2487 SetLastError(ERROR_INVALID_HANDLE);
2488 return GL_FALSE;
2490 switch (iAttribute) {
2491 case WGL_PBUFFER_WIDTH_ARB:
2492 wine_tsx11_lock();
2493 pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2494 wine_tsx11_unlock();
2495 break;
2496 case WGL_PBUFFER_HEIGHT_ARB:
2497 wine_tsx11_lock();
2498 pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2499 wine_tsx11_unlock();
2500 break;
2502 case WGL_PBUFFER_LOST_ARB:
2503 /* GLX Pbuffers cannot be lost by default. We can support this by
2504 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2505 * to receive pixel buffer clobber events, however that may or may
2506 * not give any benefit */
2507 *piValue = GL_FALSE;
2508 break;
2510 case WGL_TEXTURE_FORMAT_ARB:
2511 if (use_render_texture_ati) {
2512 unsigned int tmp;
2513 int type = WGL_NO_TEXTURE_ARB;
2514 wine_tsx11_lock();
2515 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2516 wine_tsx11_unlock();
2517 switch (tmp) {
2518 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2519 case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2520 case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2522 *piValue = type;
2523 } else {
2524 if (!object->use_render_texture) {
2525 *piValue = WGL_NO_TEXTURE_ARB;
2526 } else {
2527 if (!use_render_texture_emulation) {
2528 SetLastError(ERROR_INVALID_HANDLE);
2529 return GL_FALSE;
2531 switch(object->use_render_texture) {
2532 case GL_RGB:
2533 *piValue = WGL_TEXTURE_RGB_ARB;
2534 break;
2535 case GL_RGBA:
2536 *piValue = WGL_TEXTURE_RGBA_ARB;
2537 break;
2538 /* WGL_FLOAT_COMPONENTS_NV */
2539 case GL_FLOAT_R_NV:
2540 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2541 break;
2542 case GL_FLOAT_RG_NV:
2543 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2544 break;
2545 case GL_FLOAT_RGB_NV:
2546 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2547 break;
2548 case GL_FLOAT_RGBA_NV:
2549 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2550 break;
2551 default:
2552 ERR("Unknown texture format: %x\n", object->use_render_texture);
2556 break;
2558 case WGL_TEXTURE_TARGET_ARB:
2559 if (use_render_texture_ati) {
2560 unsigned int tmp;
2561 int type = WGL_NO_TEXTURE_ARB;
2562 wine_tsx11_lock();
2563 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2564 wine_tsx11_unlock();
2565 switch (tmp) {
2566 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2567 case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2568 case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2569 case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2571 *piValue = type;
2572 } else {
2573 if (!object->texture_target) {
2574 *piValue = WGL_NO_TEXTURE_ARB;
2575 } else {
2576 if (!use_render_texture_emulation) {
2577 SetLastError(ERROR_INVALID_DATA);
2578 return GL_FALSE;
2580 switch (object->texture_target) {
2581 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2582 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2583 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2584 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2588 break;
2590 case WGL_MIPMAP_TEXTURE_ARB:
2591 if (use_render_texture_ati) {
2592 wine_tsx11_lock();
2593 pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2594 wine_tsx11_unlock();
2595 } else {
2596 *piValue = GL_FALSE; /** don't support that */
2597 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2599 break;
2601 default:
2602 FIXME("unexpected attribute %x\n", iAttribute);
2603 break;
2606 return GL_TRUE;
2610 * X11DRV_wglReleasePbufferDCARB
2612 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2614 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2616 TRACE("(%p, %p)\n", hPbuffer, hdc);
2617 return DeleteDC(hdc);
2621 * X11DRV_wglSetPbufferAttribARB
2623 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2625 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2627 Wine_GLPBuffer* object = hPbuffer;
2628 GLboolean ret = GL_FALSE;
2630 WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2631 if (NULL == object) {
2632 SetLastError(ERROR_INVALID_HANDLE);
2633 return GL_FALSE;
2635 if (!object->use_render_texture) {
2636 SetLastError(ERROR_INVALID_HANDLE);
2637 return GL_FALSE;
2639 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2640 return GL_TRUE;
2642 if (NULL != pglXDrawableAttribATI) {
2643 if (use_render_texture_ati) {
2644 FIXME("Need conversion for GLX_ATI_render_texture\n");
2646 wine_tsx11_lock();
2647 ret = pglXDrawableAttribATI(object->display, object->drawable, piAttribList);
2648 wine_tsx11_unlock();
2650 return ret;
2654 * X11DRV_wglChoosePixelFormatARB
2656 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2658 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2660 int gl_test = 0;
2661 int attribs[256];
2662 int nAttribs = 0;
2663 GLXFBConfig* cfgs = NULL;
2664 int nCfgs = 0;
2665 int it;
2666 int fmt_id;
2667 WineGLPixelFormat *fmt;
2668 UINT pfmt_it = 0;
2669 int run;
2670 int i;
2671 DWORD dwFlags = 0;
2673 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2674 if (NULL != pfAttribFList) {
2675 FIXME("unused pfAttribFList\n");
2678 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2679 if (-1 == nAttribs) {
2680 WARN("Cannot convert WGL to GLX attributes\n");
2681 return GL_FALSE;
2683 PUSH1(attribs, None);
2685 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2686 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the WineGLPixelFormat. We don't query these attributes
2687 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on by passing a dwFlags to 'ConvertPixelFormatGLXtoWGL'. */
2688 for(i=0; piAttribIList[i] != 0; i+=2)
2690 switch(piAttribIList[i])
2692 case WGL_DRAW_TO_BITMAP_ARB:
2693 if(piAttribIList[i+1])
2694 dwFlags |= PFD_DRAW_TO_BITMAP;
2695 break;
2696 case WGL_ACCELERATION_ARB:
2697 switch(piAttribIList[i+1])
2699 case WGL_NO_ACCELERATION_ARB:
2700 dwFlags |= PFD_GENERIC_FORMAT;
2701 break;
2702 case WGL_GENERIC_ACCELERATION_ARB:
2703 dwFlags |= PFD_GENERIC_ACCELERATED;
2704 break;
2705 case WGL_FULL_ACCELERATION_ARB:
2706 /* Nothing to do */
2707 break;
2709 break;
2710 case WGL_SUPPORT_GDI_ARB:
2711 if(piAttribIList[i+1])
2712 dwFlags |= PFD_SUPPORT_GDI;
2713 break;
2717 /* Search for FB configurations matching the requirements in attribs */
2718 wine_tsx11_lock();
2719 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2720 if (NULL == cfgs) {
2721 wine_tsx11_unlock();
2722 WARN("Compatible Pixel Format not found\n");
2723 return GL_FALSE;
2726 /* Loop through all matching formats and check if they are suitable.
2727 * Note that this function should at max return nMaxFormats different formats */
2728 for(run=0; run < 2; run++)
2730 for (it = 0; it < nCfgs; ++it) {
2731 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2732 if (gl_test) {
2733 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2734 continue;
2737 /* Search for the format in our list of compatible formats */
2738 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id, dwFlags);
2739 if(!fmt)
2740 continue;
2742 /* During the first run we only want onscreen formats and during the second only offscreen 'XOR' */
2743 if( ((run == 0) && fmt->offscreenOnly) || ((run == 1) && !fmt->offscreenOnly) )
2744 continue;
2746 if(pfmt_it < nMaxFormats) {
2747 piFormats[pfmt_it] = fmt->iPixelFormat;
2748 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2750 pfmt_it++;
2754 *nNumFormats = pfmt_it;
2755 /** free list */
2756 XFree(cfgs);
2757 wine_tsx11_unlock();
2758 return GL_TRUE;
2762 * X11DRV_wglGetPixelFormatAttribivARB
2764 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2766 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2768 UINT i;
2769 WineGLPixelFormat *fmt = NULL;
2770 int hTest;
2771 int tmp;
2772 int curGLXAttr = 0;
2773 int nWGLFormats = 0;
2775 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2777 if (0 < iLayerPlane) {
2778 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2779 return GL_FALSE;
2782 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2783 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2784 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2785 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2786 if(!fmt) {
2787 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2790 wine_tsx11_lock();
2791 for (i = 0; i < nAttributes; ++i) {
2792 const int curWGLAttr = piAttributes[i];
2793 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2795 switch (curWGLAttr) {
2796 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2797 piValues[i] = nWGLFormats;
2798 continue;
2800 case WGL_SUPPORT_OPENGL_ARB:
2801 piValues[i] = GL_TRUE;
2802 continue;
2804 case WGL_ACCELERATION_ARB:
2805 curGLXAttr = GLX_CONFIG_CAVEAT;
2806 if (!fmt) goto pix_error;
2807 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2808 piValues[i] = WGL_NO_ACCELERATION_ARB;
2809 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2810 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2811 else
2812 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2813 continue;
2815 case WGL_TRANSPARENT_ARB:
2816 curGLXAttr = GLX_TRANSPARENT_TYPE;
2817 if (!fmt) goto pix_error;
2818 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2819 if (hTest) goto get_error;
2820 piValues[i] = GL_FALSE;
2821 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2822 continue;
2824 case WGL_PIXEL_TYPE_ARB:
2825 curGLXAttr = GLX_RENDER_TYPE;
2826 if (!fmt) goto pix_error;
2827 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2828 if (hTest) goto get_error;
2829 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2830 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2831 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2832 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2833 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2834 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2835 else {
2836 ERR("unexpected RenderType(%x)\n", tmp);
2837 piValues[i] = WGL_TYPE_RGBA_ARB;
2839 continue;
2841 case WGL_COLOR_BITS_ARB:
2842 curGLXAttr = GLX_BUFFER_SIZE;
2843 break;
2845 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2846 if (use_render_texture_ati) {
2847 curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2848 break;
2850 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2851 if (use_render_texture_ati) {
2852 curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2853 break;
2855 if (!use_render_texture_emulation) {
2856 piValues[i] = GL_FALSE;
2857 continue;
2859 curGLXAttr = GLX_RENDER_TYPE;
2860 if (!fmt) goto pix_error;
2861 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2862 if (hTest) goto get_error;
2863 if (GLX_COLOR_INDEX_BIT == tmp) {
2864 piValues[i] = GL_FALSE;
2865 continue;
2867 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2868 if (hTest) goto get_error;
2869 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2870 continue;
2872 case WGL_BLUE_BITS_ARB:
2873 curGLXAttr = GLX_BLUE_SIZE;
2874 break;
2875 case WGL_RED_BITS_ARB:
2876 curGLXAttr = GLX_RED_SIZE;
2877 break;
2878 case WGL_GREEN_BITS_ARB:
2879 curGLXAttr = GLX_GREEN_SIZE;
2880 break;
2881 case WGL_ALPHA_BITS_ARB:
2882 curGLXAttr = GLX_ALPHA_SIZE;
2883 break;
2884 case WGL_DEPTH_BITS_ARB:
2885 curGLXAttr = GLX_DEPTH_SIZE;
2886 break;
2887 case WGL_STENCIL_BITS_ARB:
2888 curGLXAttr = GLX_STENCIL_SIZE;
2889 break;
2890 case WGL_DOUBLE_BUFFER_ARB:
2891 curGLXAttr = GLX_DOUBLEBUFFER;
2892 break;
2893 case WGL_STEREO_ARB:
2894 curGLXAttr = GLX_STEREO;
2895 break;
2896 case WGL_AUX_BUFFERS_ARB:
2897 curGLXAttr = GLX_AUX_BUFFERS;
2898 break;
2900 case WGL_SUPPORT_GDI_ARB:
2901 if (!fmt) goto pix_error;
2902 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) ? TRUE : FALSE;
2903 continue;
2905 case WGL_DRAW_TO_BITMAP_ARB:
2906 if (!fmt) goto pix_error;
2907 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? TRUE : FALSE;
2908 continue;
2910 case WGL_DRAW_TO_WINDOW_ARB:
2911 case WGL_DRAW_TO_PBUFFER_ARB:
2912 if (!fmt) goto pix_error;
2913 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2914 if (hTest) goto get_error;
2915 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2916 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2917 piValues[i] = GL_TRUE;
2918 else
2919 piValues[i] = GL_FALSE;
2920 continue;
2922 case WGL_SWAP_METHOD_ARB:
2923 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2924 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2925 * point only ATI offers this.
2927 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2928 break;
2930 case WGL_PBUFFER_LARGEST_ARB:
2931 curGLXAttr = GLX_LARGEST_PBUFFER;
2932 break;
2934 case WGL_SAMPLE_BUFFERS_ARB:
2935 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2936 break;
2938 case WGL_SAMPLES_ARB:
2939 curGLXAttr = GLX_SAMPLES_ARB;
2940 break;
2942 case WGL_FLOAT_COMPONENTS_NV:
2943 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2944 break;
2946 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2947 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2948 break;
2950 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2951 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2952 break;
2954 case WGL_ACCUM_RED_BITS_ARB:
2955 curGLXAttr = GLX_ACCUM_RED_SIZE;
2956 break;
2957 case WGL_ACCUM_GREEN_BITS_ARB:
2958 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2959 break;
2960 case WGL_ACCUM_BLUE_BITS_ARB:
2961 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2962 break;
2963 case WGL_ACCUM_ALPHA_BITS_ARB:
2964 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2965 break;
2966 case WGL_ACCUM_BITS_ARB:
2967 if (!fmt) goto pix_error;
2968 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2969 if (hTest) goto get_error;
2970 piValues[i] = tmp;
2971 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2972 if (hTest) goto get_error;
2973 piValues[i] += tmp;
2974 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2975 if (hTest) goto get_error;
2976 piValues[i] += tmp;
2977 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2978 if (hTest) goto get_error;
2979 piValues[i] += tmp;
2980 continue;
2982 default:
2983 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2986 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2987 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2988 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2990 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2991 * and check which options can work using iPixelFormat=0 and which not.
2992 * A problem would be that this function is an extension. This would
2993 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2995 if (0 != curGLXAttr && iPixelFormat != 0) {
2996 if (!fmt) goto pix_error;
2997 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2998 if (hTest) goto get_error;
2999 curGLXAttr = 0;
3000 } else {
3001 piValues[i] = GL_FALSE;
3004 wine_tsx11_unlock();
3005 return GL_TRUE;
3007 get_error:
3008 wine_tsx11_unlock();
3009 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
3010 return GL_FALSE;
3012 pix_error:
3013 wine_tsx11_unlock();
3014 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
3015 return GL_FALSE;
3019 * X11DRV_wglGetPixelFormatAttribfvARB
3021 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
3023 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
3025 int *attr;
3026 int ret;
3027 UINT i;
3029 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
3031 /* Allocate a temporary array to store integer values */
3032 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
3033 if (!attr) {
3034 ERR("couldn't allocate %d array\n", nAttributes);
3035 return GL_FALSE;
3038 /* Piggy-back on wglGetPixelFormatAttribivARB */
3039 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
3040 if (ret) {
3041 /* Convert integer values to float. Should also check for attributes
3042 that can give decimal values here */
3043 for (i=0; i<nAttributes;i++) {
3044 pfValues[i] = attr[i];
3048 HeapFree(GetProcessHeap(), 0, attr);
3049 return ret;
3053 * X11DRV_wglBindTexImageARB
3055 * WGL_ARB_render_texture: wglBindTexImageARB
3057 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3059 Wine_GLPBuffer* object = hPbuffer;
3060 GLboolean ret = GL_FALSE;
3062 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3063 if (NULL == object) {
3064 SetLastError(ERROR_INVALID_HANDLE);
3065 return GL_FALSE;
3067 if (!object->use_render_texture) {
3068 SetLastError(ERROR_INVALID_HANDLE);
3069 return GL_FALSE;
3072 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3073 static int init = 0;
3074 int prev_binded_texture = 0;
3075 GLXContext prev_context;
3076 Drawable prev_drawable;
3077 GLXContext tmp_context;
3079 wine_tsx11_lock();
3080 prev_context = pglXGetCurrentContext();
3081 prev_drawable = pglXGetCurrentDrawable();
3083 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
3084 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
3085 isn't ideal performance wise but I wasn't able to get other ways working.
3087 if(!init) {
3088 init = 1; /* Only show the FIXME once for performance reasons */
3089 FIXME("partial stub!\n");
3092 TRACE("drawable=%p, context=%p\n", (void*)object->drawable, prev_context);
3093 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3095 pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3097 /* Switch to our pbuffer */
3098 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
3100 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3101 * After that upload the pbuffer texture data. */
3102 pglBindTexture(object->texture_target, prev_binded_texture);
3103 pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3105 /* Switch back to the original drawable and upload the pbuffer-texture */
3106 pglXMakeCurrent(object->display, prev_drawable, prev_context);
3107 pglXDestroyContext(gdi_display, tmp_context);
3108 wine_tsx11_unlock();
3109 return GL_TRUE;
3112 if (NULL != pglXBindTexImageATI) {
3113 int buffer;
3115 switch(iBuffer)
3117 case WGL_FRONT_LEFT_ARB:
3118 buffer = GLX_FRONT_LEFT_ATI;
3119 break;
3120 case WGL_FRONT_RIGHT_ARB:
3121 buffer = GLX_FRONT_RIGHT_ATI;
3122 break;
3123 case WGL_BACK_LEFT_ARB:
3124 buffer = GLX_BACK_LEFT_ATI;
3125 break;
3126 case WGL_BACK_RIGHT_ARB:
3127 buffer = GLX_BACK_RIGHT_ATI;
3128 break;
3129 default:
3130 ERR("Unknown iBuffer=%#x\n", iBuffer);
3131 return FALSE;
3134 /* In the sample 'ogl_offscreen_rendering_3' from codesampler.net I get garbage on the screen.
3135 * I'm not sure if that's a bug in the ATI extension or in the program. I think that the program
3136 * expected a single buffering format since it didn't ask for double buffering. A buffer swap
3137 * fixed the program. I don't know what the correct behavior is. On the other hand that demo
3138 * works fine using our pbuffer emulation path.
3140 wine_tsx11_lock();
3141 ret = pglXBindTexImageATI(object->display, object->drawable, buffer);
3142 wine_tsx11_unlock();
3144 return ret;
3148 * X11DRV_wglReleaseTexImageARB
3150 * WGL_ARB_render_texture: wglReleaseTexImageARB
3152 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3154 Wine_GLPBuffer* object = hPbuffer;
3155 GLboolean ret = GL_FALSE;
3157 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3158 if (NULL == object) {
3159 SetLastError(ERROR_INVALID_HANDLE);
3160 return GL_FALSE;
3162 if (!object->use_render_texture) {
3163 SetLastError(ERROR_INVALID_HANDLE);
3164 return GL_FALSE;
3166 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3167 return GL_TRUE;
3169 if (NULL != pglXReleaseTexImageATI) {
3170 int buffer;
3172 switch(iBuffer)
3174 case WGL_FRONT_LEFT_ARB:
3175 buffer = GLX_FRONT_LEFT_ATI;
3176 break;
3177 case WGL_FRONT_RIGHT_ARB:
3178 buffer = GLX_FRONT_RIGHT_ATI;
3179 break;
3180 case WGL_BACK_LEFT_ARB:
3181 buffer = GLX_BACK_LEFT_ATI;
3182 break;
3183 case WGL_BACK_RIGHT_ARB:
3184 buffer = GLX_BACK_RIGHT_ATI;
3185 break;
3186 default:
3187 ERR("Unknown iBuffer=%#x\n", iBuffer);
3188 return FALSE;
3190 wine_tsx11_lock();
3191 ret = pglXReleaseTexImageATI(object->display, object->drawable, buffer);
3192 wine_tsx11_unlock();
3194 return ret;
3198 * X11DRV_wglGetExtensionsStringEXT
3200 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3202 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
3203 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3204 return WineGLInfo.wglExtensions;
3208 * X11DRV_wglGetSwapIntervalEXT
3210 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3212 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
3213 FIXME("(),stub!\n");
3214 return swap_interval;
3218 * X11DRV_wglSwapIntervalEXT
3220 * WGL_EXT_swap_control: wglSwapIntervalEXT
3222 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
3223 BOOL ret = TRUE;
3225 TRACE("(%d)\n", interval);
3226 swap_interval = interval;
3227 if (NULL != pglXSwapIntervalSGI) {
3228 wine_tsx11_lock();
3229 ret = !pglXSwapIntervalSGI(interval);
3230 wine_tsx11_unlock();
3232 else WARN("(): GLX_SGI_swap_control extension seems not supported\n");
3233 return ret;
3237 * X11DRV_wglAllocateMemoryNV
3239 * WGL_NV_vertex_array_range: wglAllocateMemoryNV
3241 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
3242 void *ret = NULL;
3243 TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
3245 if (pglXAllocateMemoryNV)
3247 wine_tsx11_lock();
3248 ret = pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
3249 wine_tsx11_unlock();
3251 return ret;
3255 * X11DRV_wglFreeMemoryNV
3257 * WGL_NV_vertex_array_range: wglFreeMemoryNV
3259 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
3260 TRACE("(%p)\n", pointer);
3261 if (pglXFreeMemoryNV == NULL)
3262 return;
3264 wine_tsx11_lock();
3265 pglXFreeMemoryNV(pointer);
3266 wine_tsx11_unlock();
3270 * X11DRV_wglSetPixelFormatWINE
3272 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3273 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3275 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3277 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
3279 if (!has_opengl()) return FALSE;
3281 if (physDev->current_pf == iPixelFormat) return TRUE;
3283 /* Relay to the core SetPixelFormat */
3284 TRACE("Changing iPixelFormat from %d to %d\n", physDev->current_pf, iPixelFormat);
3285 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
3289 * glxRequireVersion (internal)
3291 * Check if the supported GLX version matches requiredVersion.
3293 static BOOL glxRequireVersion(int requiredVersion)
3295 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3296 if(requiredVersion <= WineGLInfo.glxVersion[1])
3297 return TRUE;
3299 return FALSE;
3302 static BOOL glxRequireExtension(const char *requiredExtension)
3304 if (strstr(WineGLInfo.glxExtensions, requiredExtension) == NULL) {
3305 return FALSE;
3308 return TRUE;
3311 static void register_extension_string(const char *ext)
3313 if (WineGLInfo.wglExtensions[0])
3314 strcat(WineGLInfo.wglExtensions, " ");
3315 strcat(WineGLInfo.wglExtensions, ext);
3317 TRACE("'%s'\n", ext);
3320 static BOOL register_extension(const WineGLExtension * ext)
3322 int i;
3324 assert( WineGLExtensionListSize < MAX_EXTENSIONS );
3325 WineGLExtensionList[WineGLExtensionListSize++] = ext;
3327 register_extension_string(ext->extName);
3329 for (i = 0; ext->extEntryPoints[i].funcName; ++i)
3330 TRACE(" - '%s'\n", ext->extEntryPoints[i].funcName);
3332 return TRUE;
3335 static const WineGLExtension WGL_internal_functions =
3339 { "wglGetIntegerv", X11DRV_wglGetIntegerv },
3340 { "wglFinish", X11DRV_wglFinish },
3341 { "wglFlush", X11DRV_wglFlush },
3346 static const WineGLExtension WGL_ARB_extensions_string =
3348 "WGL_ARB_extensions_string",
3350 { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
3354 static const WineGLExtension WGL_ARB_make_current_read =
3356 "WGL_ARB_make_current_read",
3358 { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
3359 { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
3363 static const WineGLExtension WGL_ARB_multisample =
3365 "WGL_ARB_multisample",
3368 static const WineGLExtension WGL_ARB_pbuffer =
3370 "WGL_ARB_pbuffer",
3372 { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
3373 { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
3374 { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
3375 { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
3376 { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
3377 { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
3381 static const WineGLExtension WGL_ARB_pixel_format =
3383 "WGL_ARB_pixel_format",
3385 { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3386 { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3387 { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3391 static const WineGLExtension WGL_ARB_render_texture =
3393 "WGL_ARB_render_texture",
3395 { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3396 { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3400 static const WineGLExtension WGL_EXT_extensions_string =
3402 "WGL_EXT_extensions_string",
3404 { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3408 static const WineGLExtension WGL_EXT_swap_control =
3410 "WGL_EXT_swap_control",
3412 { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3413 { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3417 static const WineGLExtension WGL_NV_vertex_array_range =
3419 "WGL_NV_vertex_array_range",
3421 { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3422 { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3426 static const WineGLExtension WGL_WINE_pixel_format_passthrough =
3428 "WGL_WINE_pixel_format_passthrough",
3430 { "wglSetPixelFormatWINE", X11DRV_wglSetPixelFormatWINE },
3435 * X11DRV_WineGL_LoadExtensions
3437 static void X11DRV_WineGL_LoadExtensions(void)
3439 WineGLInfo.wglExtensions[0] = 0;
3441 /* Load Wine internal functions */
3442 register_extension(&WGL_internal_functions);
3444 /* ARB Extensions */
3446 if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3448 register_extension_string("WGL_ARB_pixel_format_float");
3449 register_extension_string("WGL_ATI_pixel_format_float");
3452 register_extension(&WGL_ARB_extensions_string);
3454 if (glxRequireVersion(3))
3455 register_extension(&WGL_ARB_make_current_read);
3457 if (glxRequireExtension("GLX_ARB_multisample"))
3458 register_extension(&WGL_ARB_multisample);
3460 /* In general pbuffer functionality requires support in the X-server. The functionality is
3461 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3462 * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3463 * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3465 * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3466 * without support in the X-server (which other Mesa based drivers require).
3468 * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3469 * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3470 * is available pbuffers must be available too. */
3471 if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3472 register_extension(&WGL_ARB_pbuffer);
3474 register_extension(&WGL_ARB_pixel_format);
3476 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3477 if (glxRequireExtension("GLX_ATI_render_texture") ||
3478 glxRequireExtension("GLX_ARB_render_texture") ||
3479 (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3481 register_extension(&WGL_ARB_render_texture);
3483 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3484 if(glxRequireExtension("GLX_NV_float_buffer"))
3485 register_extension_string("WGL_NV_float_buffer");
3487 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3488 if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3489 register_extension_string("WGL_NV_texture_rectangle");
3492 /* EXT Extensions */
3494 register_extension(&WGL_EXT_extensions_string);
3496 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3497 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3498 register_extension(&WGL_EXT_swap_control);
3500 if(glxRequireExtension("GLX_EXT_framebuffer_sRGB"))
3501 register_extension_string("WGL_EXT_framebuffer_sRGB");
3503 if(glxRequireExtension("GLX_EXT_fbconfig_packed_float"))
3504 register_extension_string("WGL_EXT_pixel_format_packed_float");
3506 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3507 if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3508 register_extension(&WGL_NV_vertex_array_range);
3510 /* WINE-specific WGL Extensions */
3512 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3513 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3515 register_extension(&WGL_WINE_pixel_format_passthrough);
3519 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3521 Drawable ret;
3523 if(physDev->bitmap)
3525 if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
3526 ret = physDev->drawable; /* PBuffer */
3527 else
3528 ret = physDev->bitmap->glxpixmap;
3530 else if(physDev->gl_drawable)
3531 ret = physDev->gl_drawable;
3532 else
3533 ret = physDev->drawable;
3534 return ret;
3537 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3539 wine_tsx11_lock();
3540 pglXDestroyGLXPixmap(display, glxpixmap);
3541 wine_tsx11_unlock();
3542 return TRUE;
3546 * X11DRV_SwapBuffers
3548 * Swap the buffers of this DC
3550 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
3552 GLXDrawable drawable;
3553 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
3555 if (!has_opengl()) return FALSE;
3557 TRACE("(%p)\n", physDev);
3559 drawable = get_glxdrawable(physDev);
3561 wine_tsx11_lock();
3562 sync_context(ctx);
3563 if(physDev->pixmap) {
3564 if(pglXCopySubBufferMESA) {
3565 int w = physDev->dc_rect.right - physDev->dc_rect.left;
3566 int h = physDev->dc_rect.bottom - physDev->dc_rect.top;
3568 /* (glX)SwapBuffers has an implicit glFlush effect, however
3569 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3570 * copying */
3571 pglFlush();
3572 if(w > 0 && h > 0)
3573 pglXCopySubBufferMESA(gdi_display, drawable, 0, 0, w, h);
3575 else
3576 pglXSwapBuffers(gdi_display, drawable);
3578 else
3579 pglXSwapBuffers(gdi_display, drawable);
3581 flush_gl_drawable(physDev);
3582 wine_tsx11_unlock();
3584 /* FPS support */
3585 if (TRACE_ON(fps))
3587 static long prev_time, start_time;
3588 static unsigned long frames, frames_total;
3590 DWORD time = GetTickCount();
3591 frames++;
3592 frames_total++;
3593 /* every 1.5 seconds */
3594 if (time - prev_time > 1500) {
3595 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
3596 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
3597 prev_time = time;
3598 frames = 0;
3599 if(start_time == 0) start_time = time;
3603 return TRUE;
3606 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3608 WineGLPixelFormat *fmt;
3609 XVisualInfo *ret;
3611 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
3612 if(fmt == NULL)
3613 return NULL;
3615 wine_tsx11_lock();
3616 ret = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
3617 wine_tsx11_unlock();
3618 return ret;
3621 #else /* no OpenGL includes */
3623 void X11DRV_OpenGL_Cleanup(void)
3627 static inline void opengl_error(void)
3629 static int warned;
3630 if (!warned++) ERR("No OpenGL support compiled in.\n");
3633 int pixelformat_from_fbconfig_id(XID fbconfig_id)
3635 return 0;
3638 void mark_drawable_dirty(Drawable old, Drawable new)
3642 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
3646 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
3648 return 0;
3651 /***********************************************************************
3652 * ChoosePixelFormat (X11DRV.@)
3654 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
3655 const PIXELFORMATDESCRIPTOR *ppfd) {
3656 opengl_error();
3657 return 0;
3660 /***********************************************************************
3661 * DescribePixelFormat (X11DRV.@)
3663 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
3664 int iPixelFormat,
3665 UINT nBytes,
3666 PIXELFORMATDESCRIPTOR *ppfd) {
3667 opengl_error();
3668 return 0;
3671 /***********************************************************************
3672 * GetPixelFormat (X11DRV.@)
3674 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
3675 opengl_error();
3676 return 0;
3679 /***********************************************************************
3680 * SetPixelFormat (X11DRV.@)
3682 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
3683 int iPixelFormat,
3684 const PIXELFORMATDESCRIPTOR *ppfd) {
3685 opengl_error();
3686 return FALSE;
3689 /***********************************************************************
3690 * SwapBuffers (X11DRV.@)
3692 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
3693 opengl_error();
3694 return FALSE;
3698 * X11DRV_wglCopyContext
3700 * For OpenGL32 wglCopyContext.
3702 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
3703 opengl_error();
3704 return FALSE;
3708 * X11DRV_wglCreateContext
3710 * For OpenGL32 wglCreateContext.
3712 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
3713 opengl_error();
3714 return NULL;
3718 * X11DRV_wglDeleteContext
3720 * For OpenGL32 wglDeleteContext.
3722 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc) {
3723 opengl_error();
3724 return FALSE;
3728 * X11DRV_wglGetProcAddress
3730 * For OpenGL32 wglGetProcAddress.
3732 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
3733 opengl_error();
3734 return NULL;
3737 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, void *hPbuffer)
3739 opengl_error();
3740 return NULL;
3743 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
3744 opengl_error();
3745 return FALSE;
3749 * X11DRV_wglMakeCurrent
3751 * For OpenGL32 wglMakeCurrent.
3753 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
3754 opengl_error();
3755 return FALSE;
3759 * X11DRV_wglShareLists
3761 * For OpenGL32 wglShareLists.
3763 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
3764 opengl_error();
3765 return FALSE;
3769 * X11DRV_wglUseFontBitmapsA
3771 * For OpenGL32 wglUseFontBitmapsA.
3773 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3775 opengl_error();
3776 return FALSE;
3780 * X11DRV_wglUseFontBitmapsW
3782 * For OpenGL32 wglUseFontBitmapsW.
3784 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3786 opengl_error();
3787 return FALSE;
3791 * X11DRV_wglSetPixelFormatWINE
3793 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3794 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3796 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3798 opengl_error();
3799 return FALSE;
3802 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3804 return 0;
3807 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3809 return FALSE;
3812 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3814 return NULL;
3817 #endif /* defined(HAVE_OPENGL) */