push e9c4c6cdd0babd7b2cb4288f191bb331b756eaf2
[wine/hacks.git] / dlls / winex11.drv / opengl.c
blobb15b4e255d851a2bcc5ece61e952c8ecf8396483
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
51 #ifdef HAVE_GL_GLEXT_H
52 # include <GL/glext.h>
53 #endif
55 #include "wine/wgl.h"
57 #undef APIENTRY
58 #undef CALLBACK
59 #undef WINAPI
61 /* Redefines the constants */
62 #define CALLBACK __stdcall
63 #define WINAPI __stdcall
64 #define APIENTRY WINAPI
67 WINE_DECLARE_DEBUG_CHANNEL(fps);
69 typedef struct wine_glextension {
70 const char *extName;
71 struct {
72 const char *funcName;
73 void *funcAddress;
74 } extEntryPoints[9];
75 } WineGLExtension;
77 struct WineGLInfo {
78 const char *glVersion;
79 const char *glExtensions;
81 int glxVersion[2];
83 const char *glxServerVersion;
84 const char *glxServerVendor;
85 const char *glxServerExtensions;
87 const char *glxClientVersion;
88 const char *glxClientVendor;
89 const char *glxClientExtensions;
91 const char *glxExtensions;
93 BOOL glxDirect;
94 char wglExtensions[4096];
97 typedef struct wine_glpixelformat {
98 int iPixelFormat;
99 GLXFBConfig fbconfig;
100 int fmt_id;
101 int render_type;
102 BOOL offscreenOnly;
103 DWORD dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
104 } WineGLPixelFormat;
106 typedef struct wine_glcontext {
107 HDC hdc;
108 BOOL do_escape;
109 XVisualInfo *vis;
110 WineGLPixelFormat *fmt;
111 GLXContext ctx;
112 HDC read_hdc;
113 Drawable drawables[2];
114 BOOL refresh_drawables;
115 struct wine_glcontext *next;
116 struct wine_glcontext *prev;
117 } Wine_GLContext;
119 typedef struct wine_glpbuffer {
120 Drawable drawable;
121 Display* display;
122 WineGLPixelFormat* fmt;
123 int width;
124 int height;
125 int* attribList;
126 HDC hdc;
128 int use_render_texture; /* This is also the internal texture format */
129 int texture_bind_target;
130 int texture_bpp;
131 GLint texture_format;
132 GLuint texture_target;
133 GLenum texture_type;
134 GLuint texture;
135 int texture_level;
136 } Wine_GLPBuffer;
138 static Wine_GLContext *context_list;
139 static struct WineGLInfo WineGLInfo = { 0 };
140 static int use_render_texture_emulation = 1;
141 static int use_render_texture_ati = 0;
142 static int swap_interval = 1;
144 #define MAX_EXTENSIONS 16
145 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
146 static int WineGLExtensionListSize;
148 static void X11DRV_WineGL_LoadExtensions(void);
149 static BOOL glxRequireVersion(int requiredVersion);
150 static BOOL glxRequireExtension(const char *requiredExtension);
152 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
153 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
154 TRACE(" - dwFlags : ");
155 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
156 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
157 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
158 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
159 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
160 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
161 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
162 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
163 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
164 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
165 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
166 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
167 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
168 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
169 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
170 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
171 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
172 /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
173 * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
174 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
175 #undef TEST_AND_DUMP
176 TRACE("\n");
178 TRACE(" - iPixelType : ");
179 switch (ppfd->iPixelType) {
180 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
181 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
183 TRACE("\n");
185 TRACE(" - Color : %d\n", ppfd->cColorBits);
186 TRACE(" - Red : %d\n", ppfd->cRedBits);
187 TRACE(" - Green : %d\n", ppfd->cGreenBits);
188 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
189 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
190 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
191 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
192 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
193 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
195 TRACE(" - iLayerType : ");
196 switch (ppfd->iLayerType) {
197 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
198 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
199 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
201 TRACE("\n");
204 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
205 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
207 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
208 /* GLX 1.0 */
209 MAKE_FUNCPTR(glXChooseVisual)
210 MAKE_FUNCPTR(glXCopyContext)
211 MAKE_FUNCPTR(glXCreateContext)
212 MAKE_FUNCPTR(glXCreateGLXPixmap)
213 MAKE_FUNCPTR(glXGetCurrentContext)
214 MAKE_FUNCPTR(glXGetCurrentDrawable)
215 MAKE_FUNCPTR(glXDestroyContext)
216 MAKE_FUNCPTR(glXDestroyGLXPixmap)
217 MAKE_FUNCPTR(glXGetConfig)
218 MAKE_FUNCPTR(glXIsDirect)
219 MAKE_FUNCPTR(glXMakeCurrent)
220 MAKE_FUNCPTR(glXSwapBuffers)
221 MAKE_FUNCPTR(glXQueryExtension)
222 MAKE_FUNCPTR(glXQueryVersion)
223 MAKE_FUNCPTR(glXUseXFont)
225 /* GLX 1.1 */
226 MAKE_FUNCPTR(glXGetClientString)
227 MAKE_FUNCPTR(glXQueryExtensionsString)
228 MAKE_FUNCPTR(glXQueryServerString)
230 /* GLX 1.3 */
231 MAKE_FUNCPTR(glXGetFBConfigs)
232 MAKE_FUNCPTR(glXChooseFBConfig)
233 MAKE_FUNCPTR(glXCreatePbuffer)
234 MAKE_FUNCPTR(glXCreateNewContext)
235 MAKE_FUNCPTR(glXDestroyPbuffer)
236 MAKE_FUNCPTR(glXGetFBConfigAttrib)
237 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
238 MAKE_FUNCPTR(glXMakeContextCurrent)
239 MAKE_FUNCPTR(glXQueryDrawable)
240 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
242 /* GLX Extensions */
243 static void* (*pglXGetProcAddressARB)(const GLubyte *);
244 static int (*pglXSwapIntervalSGI)(int);
246 /* ATI GLX Extensions */
247 static BOOL (*pglXBindTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
248 static BOOL (*pglXReleaseTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
249 static BOOL (*pglXDrawableAttribATI)(Display *dpy, GLXDrawable draw, const int *attribList);
251 /* NV GLX Extension */
252 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
253 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
255 /* MESA GLX Extensions */
256 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
258 /* Standard OpenGL */
259 MAKE_FUNCPTR(glBindTexture)
260 MAKE_FUNCPTR(glBitmap)
261 MAKE_FUNCPTR(glCopyTexSubImage1D)
262 MAKE_FUNCPTR(glCopyTexImage2D)
263 MAKE_FUNCPTR(glCopyTexSubImage2D)
264 MAKE_FUNCPTR(glDrawBuffer)
265 MAKE_FUNCPTR(glEndList)
266 MAKE_FUNCPTR(glGetError)
267 MAKE_FUNCPTR(glGetIntegerv)
268 MAKE_FUNCPTR(glGetString)
269 MAKE_FUNCPTR(glNewList)
270 MAKE_FUNCPTR(glPixelStorei)
271 MAKE_FUNCPTR(glReadPixels)
272 MAKE_FUNCPTR(glTexImage2D)
273 MAKE_FUNCPTR(glFinish)
274 MAKE_FUNCPTR(glFlush)
275 #undef MAKE_FUNCPTR
277 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
279 static BOOL infoInitialized = FALSE;
281 int screen = DefaultScreen(gdi_display);
282 Window win = RootWindow(gdi_display, screen);
283 Visual *visual;
284 XVisualInfo template;
285 XVisualInfo *vis;
286 int num;
287 GLXContext ctx = NULL;
289 if (infoInitialized)
290 return TRUE;
291 infoInitialized = TRUE;
293 wine_tsx11_lock();
295 visual = DefaultVisual(gdi_display, screen);
296 template.visualid = XVisualIDFromVisual(visual);
297 vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
298 if (vis) {
299 WORD old_fs = wine_get_fs();
300 /* Create a GLX Context. Without one we can't query GL information */
301 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
302 if (wine_get_fs() != old_fs)
304 wine_set_fs( old_fs );
305 wine_tsx11_unlock();
306 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
307 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
308 return FALSE;
312 if (ctx) {
313 pglXMakeCurrent(gdi_display, win, ctx);
314 } else {
315 ERR(" couldn't initialize OpenGL, expect problems\n");
316 wine_tsx11_unlock();
317 return FALSE;
320 WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
321 WineGLInfo.glExtensions = strdup((const char *) pglGetString(GL_EXTENSIONS));
323 /* Get the common GLX version supported by GLX client and server ( major/minor) */
324 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
326 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
327 WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
328 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
330 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
331 WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
332 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
334 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
335 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
337 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
338 TRACE("GL renderer : %s.\n", pglGetString(GL_RENDERER));
339 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
340 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
341 TRACE("Server GLX vendor: : %s.\n", WineGLInfo.glxServerVendor);
342 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
343 TRACE("Client GLX vendor: : %s.\n", WineGLInfo.glxClientVendor);
344 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
346 if(vis) XFree(vis);
347 if(ctx) {
348 pglXMakeCurrent(gdi_display, None, NULL);
349 pglXDestroyContext(gdi_display, ctx);
351 wine_tsx11_unlock();
352 return TRUE;
355 static BOOL has_opengl(void)
357 static int init_done;
358 static void *opengl_handle;
360 char buffer[200];
361 int error_base, event_base;
363 if (init_done) return (opengl_handle != NULL);
364 init_done = 1;
366 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
367 and include all dependencies */
368 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
369 if (opengl_handle == NULL)
371 ERR( "Failed to load libGL: %s\n", buffer );
372 ERR( "OpenGL support is disabled.\n");
373 return FALSE;
376 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
377 if (pglXGetProcAddressARB == NULL) {
378 ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
379 goto failed;
382 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
384 ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
385 goto failed; \
386 } while(0)
388 /* GLX 1.0 */
389 LOAD_FUNCPTR(glXChooseVisual);
390 LOAD_FUNCPTR(glXCopyContext);
391 LOAD_FUNCPTR(glXCreateContext);
392 LOAD_FUNCPTR(glXCreateGLXPixmap);
393 LOAD_FUNCPTR(glXGetCurrentContext);
394 LOAD_FUNCPTR(glXGetCurrentDrawable);
395 LOAD_FUNCPTR(glXDestroyContext);
396 LOAD_FUNCPTR(glXDestroyGLXPixmap);
397 LOAD_FUNCPTR(glXGetConfig);
398 LOAD_FUNCPTR(glXIsDirect);
399 LOAD_FUNCPTR(glXMakeCurrent);
400 LOAD_FUNCPTR(glXSwapBuffers);
401 LOAD_FUNCPTR(glXQueryExtension);
402 LOAD_FUNCPTR(glXQueryVersion);
403 LOAD_FUNCPTR(glXUseXFont);
405 /* GLX 1.1 */
406 LOAD_FUNCPTR(glXGetClientString);
407 LOAD_FUNCPTR(glXQueryExtensionsString);
408 LOAD_FUNCPTR(glXQueryServerString);
410 /* GLX 1.3 */
411 LOAD_FUNCPTR(glXCreatePbuffer);
412 LOAD_FUNCPTR(glXCreateNewContext);
413 LOAD_FUNCPTR(glXDestroyPbuffer);
414 LOAD_FUNCPTR(glXMakeContextCurrent);
415 LOAD_FUNCPTR(glXGetCurrentReadDrawable);
416 LOAD_FUNCPTR(glXGetFBConfigs);
418 /* Standard OpenGL calls */
419 LOAD_FUNCPTR(glBindTexture);
420 LOAD_FUNCPTR(glBitmap);
421 LOAD_FUNCPTR(glCopyTexSubImage1D);
422 LOAD_FUNCPTR(glCopyTexImage2D);
423 LOAD_FUNCPTR(glCopyTexSubImage2D);
424 LOAD_FUNCPTR(glDrawBuffer);
425 LOAD_FUNCPTR(glEndList);
426 LOAD_FUNCPTR(glGetError);
427 LOAD_FUNCPTR(glGetIntegerv);
428 LOAD_FUNCPTR(glGetString);
429 LOAD_FUNCPTR(glNewList);
430 LOAD_FUNCPTR(glPixelStorei);
431 LOAD_FUNCPTR(glReadPixels);
432 LOAD_FUNCPTR(glTexImage2D);
433 LOAD_FUNCPTR(glFinish);
434 LOAD_FUNCPTR(glFlush);
435 #undef LOAD_FUNCPTR
437 /* It doesn't matter if these fail. They'll only be used if the driver reports
438 the associated extension is available (and if a driver reports the extension
439 is available but fails to provide the functions, it's quite broken) */
440 #define LOAD_FUNCPTR(f) p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)
441 /* NV GLX Extension */
442 LOAD_FUNCPTR(glXAllocateMemoryNV);
443 LOAD_FUNCPTR(glXFreeMemoryNV);
444 #undef LOAD_FUNCPTR
446 if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
448 wine_tsx11_lock();
449 if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
450 TRACE("GLX is up and running error_base = %d\n", error_base);
451 } else {
452 wine_tsx11_unlock();
453 ERR( "GLX extension is missing, disabling OpenGL.\n" );
454 goto failed;
457 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
458 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
459 * rendering is used.
461 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
462 * depends on the reported GLX client / server version and on the client / server extension list.
463 * Those don't have to be the same.
465 * In general the server GLX information lists the capabilities in case of indirect rendering.
466 * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
467 * available and in that case the client GLX informat can be used.
468 * OpenGL programs should use the 'intersection' of both sets of information which is advertised
469 * in the GLX version/extension list. When a program does this it works for certain for both
470 * direct and indirect rendering.
472 * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
473 * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
474 * extension list which causes this extension not to be listed. (Wine requires this extension).
475 * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
476 * pbuffers is around.
478 * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
479 * the ATI drivers and from then on use GLX client information for them.
482 if(glxRequireVersion(3)) {
483 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
484 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
485 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
486 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
487 } else if(glxRequireExtension("GLX_SGIX_fbconfig")) {
488 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
489 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
490 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
492 /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
493 * enable this function when the Xserver understand GLX 1.3 or newer
495 pglXQueryDrawable = NULL;
496 } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
497 TRACE("Overriding ATI GLX capabilities!\n");
498 pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
499 pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
500 pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
501 pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
503 /* Use client GLX information in case of the ATI drivers. We override the
504 * capabilities over here and not somewhere else as ATI might better their
505 * life in the future. In case they release proper drivers this block of
506 * code won't be called. */
507 WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
508 } else {
509 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
512 if(glxRequireExtension("GLX_ATI_render_texture")) {
513 use_render_texture_ati = 1;
514 pglXBindTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageATI");
515 pglXReleaseTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageATI");
516 pglXDrawableAttribATI = pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribATI");
519 if(glxRequireExtension("GLX_MESA_copy_sub_buffer")) {
520 pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
523 X11DRV_WineGL_LoadExtensions();
525 wine_tsx11_unlock();
526 return TRUE;
528 failed:
529 wine_dlclose(opengl_handle, NULL, 0);
530 opengl_handle = NULL;
531 return FALSE;
534 static inline Wine_GLContext *alloc_context(void)
536 Wine_GLContext *ret;
538 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
540 ret->next = context_list;
541 if (context_list) context_list->prev = ret;
542 context_list = ret;
544 return ret;
547 static inline void free_context(Wine_GLContext *context)
549 if (context->next != NULL) context->next->prev = context->prev;
550 if (context->prev != NULL) context->prev->next = context->next;
551 else context_list = context->next;
553 if (context->vis) XFree(context->vis);
554 HeapFree(GetProcessHeap(), 0, context);
557 static inline BOOL is_valid_context( Wine_GLContext *ctx )
559 Wine_GLContext *ptr;
560 for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
561 return (ptr != NULL);
564 static int describeContext(Wine_GLContext* ctx) {
565 int tmp;
566 int ctx_vis_id;
567 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
568 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
569 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
570 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
571 TRACE(" - VISUAL_ID 0x%x\n", tmp);
572 ctx_vis_id = tmp;
573 return ctx_vis_id;
576 static int describeDrawable(Wine_GLContext* ctx, Drawable drawable) {
577 int tmp;
578 int nElements;
579 int attribList[3] = { GLX_FBCONFIG_ID, 0, None };
580 GLXFBConfig *fbCfgs;
582 if (pglXQueryDrawable == NULL) {
583 /** glXQueryDrawable not available so returns not supported */
584 return -1;
587 TRACE(" Drawable %p have :\n", (void*) drawable);
588 pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &tmp);
589 TRACE(" - WIDTH as %d\n", tmp);
590 pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &tmp);
591 TRACE(" - HEIGHT as %d\n", tmp);
592 pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &tmp);
593 TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
595 attribList[1] = tmp;
596 fbCfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribList, &nElements);
597 if (fbCfgs == NULL) {
598 return -1;
601 pglXGetFBConfigAttrib(gdi_display, fbCfgs[0], GLX_VISUAL_ID, &tmp);
602 TRACE(" - VISUAL_ID as 0x%x\n", tmp);
604 XFree(fbCfgs);
606 return tmp;
609 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
610 int nAttribs = 0;
611 unsigned cur = 0;
612 int pop;
613 int drawattrib = 0;
614 int nvfloatattrib = GLX_DONT_CARE;
615 int pixelattrib = 0;
617 /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
618 * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
619 while (iWGLAttr && 0 != iWGLAttr[cur]) {
620 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
622 switch (iWGLAttr[cur]) {
623 case WGL_AUX_BUFFERS_ARB:
624 pop = iWGLAttr[++cur];
625 PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
626 TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
627 break;
628 case WGL_COLOR_BITS_ARB:
629 pop = iWGLAttr[++cur];
630 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
631 TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
632 break;
633 case WGL_BLUE_BITS_ARB:
634 pop = iWGLAttr[++cur];
635 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
636 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
637 break;
638 case WGL_RED_BITS_ARB:
639 pop = iWGLAttr[++cur];
640 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
641 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
642 break;
643 case WGL_GREEN_BITS_ARB:
644 pop = iWGLAttr[++cur];
645 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
646 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
647 break;
648 case WGL_ALPHA_BITS_ARB:
649 pop = iWGLAttr[++cur];
650 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
651 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
652 break;
653 case WGL_DEPTH_BITS_ARB:
654 pop = iWGLAttr[++cur];
655 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
656 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
657 break;
658 case WGL_STENCIL_BITS_ARB:
659 pop = iWGLAttr[++cur];
660 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
661 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
662 break;
663 case WGL_DOUBLE_BUFFER_ARB:
664 pop = iWGLAttr[++cur];
665 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
666 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
667 break;
668 case WGL_STEREO_ARB:
669 pop = iWGLAttr[++cur];
670 PUSH2(oGLXAttr, GLX_STEREO, pop);
671 TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
672 break;
674 case WGL_PIXEL_TYPE_ARB:
675 pop = iWGLAttr[++cur];
676 TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
677 switch (pop) {
678 case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
679 case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
680 /* 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 */
681 case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
682 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
683 default:
684 ERR("unexpected PixelType(%x)\n", pop);
685 pop = 0;
687 break;
689 case WGL_SUPPORT_GDI_ARB:
690 /* This flag is set in a WineGLPixelFormat */
691 pop = iWGLAttr[++cur];
692 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
693 break;
695 case WGL_DRAW_TO_BITMAP_ARB:
696 /* This flag is set in a WineGLPixelFormat */
697 pop = iWGLAttr[++cur];
698 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
699 break;
701 case WGL_DRAW_TO_WINDOW_ARB:
702 pop = iWGLAttr[++cur];
703 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
704 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
705 if (pop) {
706 drawattrib |= GLX_WINDOW_BIT;
708 break;
710 case WGL_DRAW_TO_PBUFFER_ARB:
711 pop = iWGLAttr[++cur];
712 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
713 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
714 if (pop) {
715 drawattrib |= GLX_PBUFFER_BIT;
717 break;
719 case WGL_ACCELERATION_ARB:
720 /* This flag is set in a WineGLPixelFormat */
721 pop = iWGLAttr[++cur];
722 TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
723 break;
725 case WGL_SUPPORT_OPENGL_ARB:
726 pop = iWGLAttr[++cur];
727 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
728 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
729 break;
731 case WGL_SWAP_METHOD_ARB:
732 pop = iWGLAttr[++cur];
733 /* For now we ignore this and just return SWAP_EXCHANGE */
734 TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
735 break;
737 case WGL_PBUFFER_LARGEST_ARB:
738 pop = iWGLAttr[++cur];
739 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
740 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
741 break;
743 case WGL_SAMPLE_BUFFERS_ARB:
744 pop = iWGLAttr[++cur];
745 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
746 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
747 break;
749 case WGL_SAMPLES_ARB:
750 pop = iWGLAttr[++cur];
751 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
752 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
753 break;
755 case WGL_TEXTURE_FORMAT_ARB:
756 case WGL_TEXTURE_TARGET_ARB:
757 case WGL_MIPMAP_TEXTURE_ARB:
758 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
759 pop = iWGLAttr[++cur];
760 if (NULL == pbuf) {
761 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
763 if (use_render_texture_ati) {
764 /** nothing to do here */
766 else if (!use_render_texture_emulation) {
767 if (WGL_NO_TEXTURE_ARB != pop) {
768 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
769 return -1; /** error: don't support it */
770 } else {
771 PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
772 drawattrib |= GLX_PBUFFER_BIT;
775 break ;
776 case WGL_FLOAT_COMPONENTS_NV:
777 nvfloatattrib = iWGLAttr[++cur];
778 TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
779 break ;
780 case WGL_BIND_TO_TEXTURE_DEPTH_NV:
781 case WGL_BIND_TO_TEXTURE_RGB_ARB:
782 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
783 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
784 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
785 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
786 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
787 pop = iWGLAttr[++cur];
788 /** cannot be converted, see direct handling on
789 * - wglGetPixelFormatAttribivARB
790 * TODO: wglChoosePixelFormat
792 break ;
793 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
794 pop = iWGLAttr[++cur];
795 PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
796 TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
797 break ;
799 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
800 pop = iWGLAttr[++cur];
801 PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
802 TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
803 break ;
804 default:
805 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
806 break;
808 ++cur;
811 /* Apply the OR'd drawable type bitmask now EVEN when WGL_DRAW_TO* is unset.
812 * It is needed in all cases because GLX_DRAWABLE_TYPE default to GLX_WINDOW_BIT. */
813 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
814 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
816 /* Set GLX_RENDER_TYPE all the time */
817 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
818 TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
820 /* Set GLX_FLOAT_COMPONENTS_NV all the time */
821 if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
822 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
823 TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
826 return nAttribs;
829 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
831 int render_type=0, render_type_bit;
832 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
833 switch(render_type_bit)
835 case GLX_RGBA_BIT:
836 render_type = GLX_RGBA_TYPE;
837 break;
838 case GLX_COLOR_INDEX_BIT:
839 render_type = GLX_COLOR_INDEX_TYPE;
840 break;
841 case GLX_RGBA_FLOAT_BIT:
842 render_type = GLX_RGBA_FLOAT_TYPE;
843 break;
844 case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
845 render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
846 break;
847 default:
848 ERR("Unknown render_type: %x\n", render_type);
850 return render_type;
853 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
854 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
856 int dbuf, value;
857 pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
858 pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
860 /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
861 * the GLX_PIXMAP_BIT set. */
862 return !dbuf && (value & GLX_PIXMAP_BIT);
865 static WineGLPixelFormat *get_formats(Display *display, int *size_ret, int *onscreen_size_ret)
867 static WineGLPixelFormat *list;
868 static int size, onscreen_size;
870 int fmt_id, nCfgs, i, run, bmp_formats;
871 GLXFBConfig* cfgs;
872 XVisualInfo *visinfo;
874 wine_tsx11_lock();
875 if (list) goto done;
877 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
878 if (NULL == cfgs || 0 == nCfgs) {
879 if(cfgs != NULL) XFree(cfgs);
880 wine_tsx11_unlock();
881 ERR("glXChooseFBConfig returns NULL\n");
882 return NULL;
885 /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
886 * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
887 * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
888 * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
890 * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
892 for(i=0, bmp_formats=0; i<nCfgs; i++)
894 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
895 bmp_formats++;
897 TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
899 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats)*sizeof(WineGLPixelFormat));
901 /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
902 * Do this as GLX doesn't guarantee that the list is sorted */
903 for(run=0; run < 2; run++)
905 for(i=0; i<nCfgs; i++) {
906 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
907 visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
909 /* The first run we only add onscreen formats (ones which have an associated X Visual).
910 * The second run we only set offscreen formats. */
911 if(!run && visinfo)
913 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
914 * The contents is copied to the destination using XCopyArea. For the copying to work
915 * the depth of the source and destination window should be the same. In general this should
916 * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
917 * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
918 * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
919 * list formats with the same depth. */
920 if(visinfo->depth != screen_depth)
921 continue;
923 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
924 list[size].iPixelFormat = size+1; /* The index starts at 1 */
925 list[size].fbconfig = cfgs[i];
926 list[size].fmt_id = fmt_id;
927 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
928 list[size].offscreenOnly = FALSE;
929 list[size].dwFlags = 0;
930 size++;
931 onscreen_size++;
933 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
934 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
936 TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
937 list[size].iPixelFormat = size+1; /* The index starts at 1 */
938 list[size].fbconfig = cfgs[i];
939 list[size].fmt_id = fmt_id;
940 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
941 list[size].offscreenOnly = FALSE;
942 list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
943 size++;
944 onscreen_size++;
947 XFree(visinfo);
948 } else if(run && !visinfo) {
949 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
950 list[size].iPixelFormat = size+1; /* The index starts at 1 */
951 list[size].fbconfig = cfgs[i];
952 list[size].fmt_id = fmt_id;
953 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
954 list[size].offscreenOnly = TRUE;
955 list[size].dwFlags = 0;
956 size++;
961 if(cfgs != NULL) XFree(cfgs);
963 done:
964 if (size_ret) *size_ret = size;
965 if (onscreen_size_ret) *onscreen_size_ret = onscreen_size;
966 wine_tsx11_unlock();
967 return list;
970 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
971 * In our WGL implementation we only support a subset of these formats namely the format of
972 * Wine's main visual and offscreen formats (if they are available).
973 * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
974 * and it returns the number of supported WGL formats in fmt_count.
976 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
978 WineGLPixelFormat *list, *res = NULL;
979 int size, onscreen_size;
981 if (!(list = get_formats(display, &size, &onscreen_size ))) return NULL;
983 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
984 * iPixelFormat in case of probing the number of pixelformats.
986 if((iPixelFormat > 0) && (iPixelFormat <= size) &&
987 (!list[iPixelFormat-1].offscreenOnly || AllowOffscreen)) {
988 res = &list[iPixelFormat-1];
989 TRACE("Returning FBConfig=%p for iPixelFormat=%d\n", res->fbconfig, iPixelFormat);
992 if(AllowOffscreen)
993 *fmt_count = size;
994 else
995 *fmt_count = onscreen_size;
997 TRACE("Number of returned pixelformats=%d\n", *fmt_count);
999 return res;
1002 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
1003 static WineGLPixelFormat* ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id, DWORD dwFlags)
1005 WineGLPixelFormat *list;
1006 int i, size;
1008 if (!(list = get_formats(display, &size, NULL ))) return NULL;
1010 for(i=0; i<size; i++) {
1011 /* A GLX format can appear multiple times in the pixel format list due to fake formats for bitmap rendering.
1012 * Fake formats might get selected when the user passes the proper flags using the dwFlags parameter. */
1013 if( (list[i].fmt_id == fmt_id) && ((list[i].dwFlags & dwFlags) == dwFlags) ) {
1014 TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", list[i].iPixelFormat, fmt_id);
1015 return &list[i];
1018 TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1019 return NULL;
1022 int pixelformat_from_fbconfig_id(XID fbconfig_id)
1024 WineGLPixelFormat *fmt;
1026 if (!fbconfig_id) return 0;
1028 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
1029 if(fmt)
1030 return fmt->iPixelFormat;
1031 /* This will happen on hwnds without a pixel format set; it's ok */
1032 return 0;
1036 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1037 void mark_drawable_dirty(Drawable old, Drawable new)
1039 Wine_GLContext *ctx;
1040 for (ctx = context_list; ctx; ctx = ctx->next) {
1041 if (old == ctx->drawables[0]) {
1042 ctx->drawables[0] = new;
1043 ctx->refresh_drawables = TRUE;
1045 if (old == ctx->drawables[1]) {
1046 ctx->drawables[1] = new;
1047 ctx->refresh_drawables = TRUE;
1052 /* Given the current context, make sure its drawable is sync'd */
1053 static inline void sync_context(Wine_GLContext *context)
1055 if(context && context->refresh_drawables) {
1056 if (glxRequireVersion(3))
1057 pglXMakeContextCurrent(gdi_display, context->drawables[0],
1058 context->drawables[1], context->ctx);
1059 else
1060 pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1061 context->refresh_drawables = FALSE;
1066 static GLXContext create_glxcontext(Display *display, Wine_GLContext *context, GLXContext shareList)
1068 GLXContext ctx;
1070 /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1071 BOOL indirect = (context->fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? FALSE : TRUE;
1073 if(context->vis)
1074 ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1075 else /* Create a GLX Context for a pbuffer */
1076 ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1078 return ctx;
1082 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
1084 return pglXCreateGLXPixmap(display, vis, parent);
1088 static XID create_bitmap_glxpixmap(X11DRV_PDEVICE *physDev, WineGLPixelFormat *fmt)
1090 GLXPixmap ret = 0;
1091 XVisualInfo *vis;
1093 wine_tsx11_lock();
1095 vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1096 if(vis) {
1097 if(vis->depth == physDev->bitmap->pixmap_depth)
1098 ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
1099 XFree(vis);
1101 wine_tsx11_unlock();
1102 TRACE("return %lx\n", ret);
1103 return ret;
1107 * X11DRV_ChoosePixelFormat
1109 * Equivalent to glXChooseVisual.
1111 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
1112 const PIXELFORMATDESCRIPTOR *ppfd) {
1113 WineGLPixelFormat *list;
1114 int onscreen_size;
1115 int ret = 0;
1116 int value = 0;
1117 int i = 0;
1118 int bestFormat = -1;
1119 int bestDBuffer = -1;
1120 int bestStereo = -1;
1121 int bestColor = -1;
1122 int bestAlpha = -1;
1123 int bestDepth = -1;
1124 int bestStencil = -1;
1125 int bestAux = -1;
1127 if (!has_opengl()) return 0;
1129 if (TRACE_ON(wgl)) {
1130 TRACE("(%p,%p)\n", physDev, ppfd);
1132 dump_PIXELFORMATDESCRIPTOR(ppfd);
1135 if (!(list = get_formats(gdi_display, NULL, &onscreen_size ))) return 0;
1137 wine_tsx11_lock();
1138 for(i=0; i<onscreen_size; i++)
1140 int dwFlags = 0;
1141 int iPixelType = 0;
1142 int alpha=0, color=0, depth=0, stencil=0, aux=0;
1143 WineGLPixelFormat *fmt = &list[i];
1145 /* Pixel type */
1146 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1147 if (value & GLX_RGBA_BIT)
1148 iPixelType = PFD_TYPE_RGBA;
1149 else
1150 iPixelType = PFD_TYPE_COLORINDEX;
1152 if (ppfd->iPixelType != iPixelType)
1154 TRACE("pixel type mismatch for iPixelFormat=%d\n", i+1);
1155 continue;
1158 /* Only use bitmap capable for formats for bitmap rendering.
1159 * See get_formats for more info. */
1160 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (fmt->dwFlags & PFD_DRAW_TO_BITMAP))
1162 TRACE("PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i+1);
1163 continue;
1166 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1167 if (value) dwFlags |= PFD_DOUBLEBUFFER;
1168 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value);
1169 if (value) dwFlags |= PFD_STEREO;
1170 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &color); /* cColorBits */
1171 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &alpha); /* cAlphaBits */
1172 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &depth); /* cDepthBits */
1173 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &stencil); /* cStencilBits */
1174 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &aux); /* cAuxBuffers */
1176 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
1177 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
1178 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
1179 * formats without the given flag set.
1180 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
1181 * has indicated that a format without stereo is returned when stereo is unavailable.
1182 * So in case PFD_STEREO is set, formats that support it should have priority above formats
1183 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
1185 * To summarize the following is most likely the correct behavior:
1186 * stereo not set -> prefer no-stereo formats, else also accept stereo formats
1187 * stereo set -> prefer stereo formats, else also accept no-stereo formats
1188 * stereo don't care -> it doesn't matter whether we get stereo or not
1190 * In Wine we will treat no-stereo the same way as don't care because it makes
1191 * format selection even more complicated and second drivers with Stereo advertise
1192 * each format twice anyway.
1195 /* Doublebuffer, see the comments above */
1196 if( !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) ) {
1197 if( ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
1198 ((dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)) )
1200 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1201 bestStereo = dwFlags & PFD_STEREO;
1202 bestAlpha = alpha;
1203 bestColor = color;
1204 bestDepth = depth;
1205 bestStencil = stencil;
1206 bestAux = aux;
1207 bestFormat = i;
1208 continue;
1210 if(bestDBuffer != -1 && (dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer)
1211 continue;
1214 /* Stereo, see the comments above. */
1215 if( !(ppfd->dwFlags & PFD_STEREO_DONTCARE) ) {
1216 if( ((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
1217 ((dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)) )
1219 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1220 bestStereo = dwFlags & PFD_STEREO;
1221 bestAlpha = alpha;
1222 bestColor = color;
1223 bestDepth = depth;
1224 bestStencil = stencil;
1225 bestAux = aux;
1226 bestFormat = i;
1227 continue;
1229 if(bestStereo != -1 && (dwFlags & PFD_STEREO) != bestStereo)
1230 continue;
1233 /* Below we will do a number of checks to select the 'best' pixelformat.
1234 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
1235 * The code works by trying to match the most important options as close as possible.
1236 * When a reasonable format is found, we will try to match more options.
1237 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
1238 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
1239 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
1241 /* Color bits */
1242 if(ppfd->cColorBits) {
1243 if( ((ppfd->cColorBits > bestColor) && (color > bestColor)) ||
1244 ((color >= ppfd->cColorBits) && (color < bestColor)) )
1246 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1247 bestStereo = dwFlags & PFD_STEREO;
1248 bestAlpha = alpha;
1249 bestColor = color;
1250 bestDepth = depth;
1251 bestStencil = stencil;
1252 bestAux = aux;
1253 bestFormat = i;
1254 continue;
1255 } else if(bestColor != color) { /* Do further checks if the format is compatible */
1256 TRACE("color mismatch for iPixelFormat=%d\n", i+1);
1257 continue;
1261 /* Alpha bits */
1262 if(ppfd->cAlphaBits) {
1263 if( ((ppfd->cAlphaBits > bestAlpha) && (alpha > bestAlpha)) ||
1264 ((alpha >= ppfd->cAlphaBits) && (alpha < bestAlpha)) )
1266 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1267 bestStereo = dwFlags & PFD_STEREO;
1268 bestAlpha = alpha;
1269 bestColor = color;
1270 bestDepth = depth;
1271 bestStencil = stencil;
1272 bestAux = aux;
1273 bestFormat = i;
1274 continue;
1275 } else if(bestAlpha != alpha) {
1276 TRACE("alpha mismatch for iPixelFormat=%d\n", i+1);
1277 continue;
1281 /* Depth bits */
1282 if(ppfd->cDepthBits) {
1283 if( ((ppfd->cDepthBits > bestDepth) && (depth > bestDepth)) ||
1284 ((depth >= ppfd->cDepthBits) && (depth < bestDepth)) )
1286 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1287 bestStereo = dwFlags & PFD_STEREO;
1288 bestAlpha = alpha;
1289 bestColor = color;
1290 bestDepth = depth;
1291 bestStencil = stencil;
1292 bestAux = aux;
1293 bestFormat = i;
1294 continue;
1295 } else if(bestDepth != depth) {
1296 TRACE("depth mismatch for iPixelFormat=%d\n", i+1);
1297 continue;
1301 /* Stencil bits */
1302 if(ppfd->cStencilBits) {
1303 if( ((ppfd->cStencilBits > bestStencil) && (stencil > bestStencil)) ||
1304 ((stencil >= ppfd->cStencilBits) && (stencil < bestStencil)) )
1306 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1307 bestStereo = dwFlags & PFD_STEREO;
1308 bestAlpha = alpha;
1309 bestColor = color;
1310 bestDepth = depth;
1311 bestStencil = stencil;
1312 bestAux = aux;
1313 bestFormat = i;
1314 continue;
1315 } else if(bestStencil != stencil) {
1316 TRACE("stencil mismatch for iPixelFormat=%d\n", i+1);
1317 continue;
1321 /* Aux buffers */
1322 if(ppfd->cAuxBuffers) {
1323 if( ((ppfd->cAuxBuffers > bestAux) && (aux > bestAux)) ||
1324 ((aux >= ppfd->cAuxBuffers) && (aux < bestAux)) )
1326 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1327 bestStereo = dwFlags & PFD_STEREO;
1328 bestAlpha = alpha;
1329 bestColor = color;
1330 bestDepth = depth;
1331 bestStencil = stencil;
1332 bestAux = aux;
1333 bestFormat = i;
1334 continue;
1335 } else if(bestAux != aux) {
1336 TRACE("aux mismatch for iPixelFormat=%d\n", i+1);
1337 continue;
1342 if(bestFormat == -1) {
1343 TRACE("No matching mode was found returning 0\n");
1344 ret = 0;
1346 else {
1347 ret = bestFormat+1; /* the return value should be a 1-based index */
1348 TRACE("Successfully found a matching mode, returning index: %d %x\n", ret, list[bestFormat].fmt_id);
1351 wine_tsx11_unlock();
1353 return ret;
1356 * X11DRV_DescribePixelFormat
1358 * Get the pixel-format descriptor associated to the given id
1360 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
1361 int iPixelFormat,
1362 UINT nBytes,
1363 PIXELFORMATDESCRIPTOR *ppfd) {
1364 /*XVisualInfo *vis;*/
1365 int value;
1366 int rb,gb,bb,ab;
1367 WineGLPixelFormat *fmt;
1368 int ret = 0;
1369 int fmt_count = 0;
1371 if (!has_opengl()) return 0;
1373 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
1375 /* 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 */
1376 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1377 if (ppfd == NULL) {
1378 /* The application is only querying the number of pixelformats */
1379 return fmt_count;
1380 } else if(fmt == NULL) {
1381 WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1382 return 0;
1385 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1386 ERR("Wrong structure size !\n");
1387 /* Should set error */
1388 return 0;
1391 ret = fmt_count;
1393 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1394 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1395 ppfd->nVersion = 1;
1397 /* These flags are always the same... */
1398 ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1399 /* Now the flags extracted from the Visual */
1401 wine_tsx11_lock();
1403 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1404 if(value & GLX_WINDOW_BIT)
1405 ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1407 /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1408 * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1409 * 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
1410 * offered the GDI bit either. */
1411 ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1413 /* PFD_GENERIC_FORMAT - gdi software rendering
1414 * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1415 * none set - full hardware accelerated by a ICD
1417 * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do */
1418 ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1420 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1421 if (value) {
1422 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1423 ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1425 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1427 /* Pixel type */
1428 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1429 if (value & GLX_RGBA_BIT)
1430 ppfd->iPixelType = PFD_TYPE_RGBA;
1431 else
1432 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1434 /* Color bits */
1435 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1436 ppfd->cColorBits = value;
1438 /* Red, green, blue and alpha bits / shifts */
1439 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1440 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1441 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1442 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1443 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1445 ppfd->cRedBits = rb;
1446 ppfd->cRedShift = gb + bb + ab;
1447 ppfd->cBlueBits = bb;
1448 ppfd->cBlueShift = ab;
1449 ppfd->cGreenBits = gb;
1450 ppfd->cGreenShift = bb + ab;
1451 ppfd->cAlphaBits = ab;
1452 ppfd->cAlphaShift = 0;
1453 } else {
1454 ppfd->cRedBits = 0;
1455 ppfd->cRedShift = 0;
1456 ppfd->cBlueBits = 0;
1457 ppfd->cBlueShift = 0;
1458 ppfd->cGreenBits = 0;
1459 ppfd->cGreenShift = 0;
1460 ppfd->cAlphaBits = 0;
1461 ppfd->cAlphaShift = 0;
1464 /* Accum RGBA bits */
1465 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1466 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1467 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1468 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1470 ppfd->cAccumBits = rb+gb+bb+ab;
1471 ppfd->cAccumRedBits = rb;
1472 ppfd->cAccumGreenBits = gb;
1473 ppfd->cAccumBlueBits = bb;
1474 ppfd->cAccumAlphaBits = ab;
1476 /* Aux bits */
1477 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1478 ppfd->cAuxBuffers = value;
1480 /* Depth bits */
1481 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1482 ppfd->cDepthBits = value;
1484 /* stencil bits */
1485 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1486 ppfd->cStencilBits = value;
1488 wine_tsx11_unlock();
1490 ppfd->iLayerType = PFD_MAIN_PLANE;
1492 if (TRACE_ON(wgl)) {
1493 dump_PIXELFORMATDESCRIPTOR(ppfd);
1496 return ret;
1500 * X11DRV_GetPixelFormat
1502 * Get the pixel-format id used by this DC
1504 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1505 WineGLPixelFormat *fmt;
1506 int tmp;
1507 TRACE("(%p)\n", physDev);
1509 if (!physDev->current_pf) return 0; /* not set yet */
1511 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE, &tmp);
1512 if(!fmt)
1514 ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physDev->current_pf);
1515 return 0;
1517 else if(fmt->offscreenOnly)
1519 /* Offscreen formats can't be used with traditional WGL calls.
1520 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1521 TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1522 return 1;
1525 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1526 return physDev->current_pf;
1529 /* This function is the core of X11DRV_SetPixelFormat and X11DRV_SetPixelFormatWINE.
1530 * Both functions are the same except that X11DRV_SetPixelFormatWINE allows you to
1531 * set the pixel format multiple times. */
1532 static BOOL internal_SetPixelFormat(X11DRV_PDEVICE *physDev,
1533 int iPixelFormat,
1534 const PIXELFORMATDESCRIPTOR *ppfd) {
1535 WineGLPixelFormat *fmt;
1536 int value;
1537 HWND hwnd;
1539 /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1540 if(get_glxdrawable(physDev) == root_window)
1542 ERR("Invalid operation on root_window\n");
1543 return FALSE;
1546 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1547 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1548 if(!fmt) {
1549 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1550 return FALSE;
1553 wine_tsx11_lock();
1554 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1555 wine_tsx11_unlock();
1557 hwnd = WindowFromDC(physDev->hdc);
1558 if(hwnd) {
1559 if(!(value&GLX_WINDOW_BIT)) {
1560 WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1561 return FALSE;
1564 if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, (WPARAM)fmt->fmt_id, 0)) {
1565 ERR("Couldn't set format of the window, returning failure\n");
1566 return FALSE;
1569 else if(physDev->bitmap) {
1570 if(!(value&GLX_PIXMAP_BIT)) {
1571 WARN("Pixel format %d is not compatible for bitmap rendering\n", iPixelFormat);
1572 return FALSE;
1575 physDev->bitmap->glxpixmap = create_bitmap_glxpixmap(physDev, fmt);
1576 if(!physDev->bitmap->glxpixmap) {
1577 WARN("Couldn't create glxpixmap for pixel format %d\n", iPixelFormat);
1578 return FALSE;
1581 else {
1582 FIXME("called on a non-window, non-bitmap object?\n");
1585 physDev->current_pf = iPixelFormat;
1587 if (TRACE_ON(wgl)) {
1588 int gl_test = 0;
1590 wine_tsx11_lock();
1591 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1592 if (gl_test) {
1593 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1594 } else {
1595 TRACE(" FBConfig have :\n");
1596 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1597 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1598 TRACE(" - VISUAL_ID 0x%x\n", value);
1599 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1600 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1602 wine_tsx11_unlock();
1604 return TRUE;
1609 * X11DRV_SetPixelFormat
1611 * Set the pixel-format id used by this DC
1613 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1614 int iPixelFormat,
1615 const PIXELFORMATDESCRIPTOR *ppfd) {
1616 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1618 if (!has_opengl()) return FALSE;
1620 if(physDev->current_pf) /* cannot change it if already set */
1621 return (physDev->current_pf == iPixelFormat);
1623 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
1627 * X11DRV_wglCopyContext
1629 * For OpenGL32 wglCopyContext.
1631 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
1632 Wine_GLContext *src = (Wine_GLContext*)hglrcSrc;
1633 Wine_GLContext *dst = (Wine_GLContext*)hglrcDst;
1635 TRACE("hglrcSrc: (%p), hglrcDst: (%p), mask: %#x\n", hglrcSrc, hglrcDst, mask);
1637 /* There is a slight difference in the way GL contexts share display lists in WGL and GLX.
1638 * In case of GLX you need to specify this at context creation time but in case of WGL you
1639 * do this using wglShareLists which you can call after creating the context.
1640 * To emulate WGL we try to delay the creation of the context until wglShareLists or wglMakeCurrent.
1641 * Up to now that works fine.
1643 * The delayed GLX context creation could cause issues for wglCopyContext as it might get called
1644 * when there is no GLX context yet. The chance this will cause problems is small as at the time of
1645 * writing Wine has had OpenGL support for more than 7 years and this function has remained a stub
1646 * ever since then.
1648 if(!src->ctx || !dst->ctx) {
1649 /* NOTE: As a special case, if both GLX contexts are NULL, that means
1650 * neither WGL context was made current. In that case, both contexts
1651 * are in a default state, so any copy would no-op.
1653 if(!src->ctx && !dst->ctx) {
1654 TRACE("No source or destination contexts set. No-op.\n");
1655 return TRUE;
1658 if (!src->ctx) {
1659 wine_tsx11_lock();
1660 src->ctx = create_glxcontext(gdi_display, src, NULL);
1661 TRACE(" created a delayed OpenGL context (%p)\n", src->ctx);
1663 else if (!dst->ctx) {
1664 wine_tsx11_lock();
1665 dst->ctx = create_glxcontext(gdi_display, dst, NULL);
1666 TRACE(" created a delayed OpenGL context (%p)\n", dst->ctx);
1669 else
1670 wine_tsx11_lock();
1672 pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1673 wine_tsx11_unlock();
1675 /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1676 return TRUE;
1680 * X11DRV_wglCreateContext
1682 * For OpenGL32 wglCreateContext.
1684 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1686 Wine_GLContext *ret;
1687 WineGLPixelFormat *fmt;
1688 int hdcPF = physDev->current_pf;
1689 int fmt_count = 0;
1690 HDC hdc = physDev->hdc;
1692 TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1694 if (!has_opengl()) return 0;
1696 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
1697 /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1698 * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1699 * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1700 * If this fails something is very wrong on the system. */
1701 if(!fmt) {
1702 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
1703 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1704 return NULL;
1707 /* The context will be allocated in the wglMakeCurrent call */
1708 wine_tsx11_lock();
1709 ret = alloc_context();
1710 wine_tsx11_unlock();
1711 ret->hdc = hdc;
1712 ret->fmt = fmt;
1714 /*ret->vis = vis;*/
1715 ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1717 TRACE(" creating context %p (GL context creation delayed)\n", ret);
1718 return (HGLRC) ret;
1722 * X11DRV_wglDeleteContext
1724 * For OpenGL32 wglDeleteContext.
1726 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc)
1728 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1729 BOOL ret = TRUE;
1731 TRACE("(%p)\n", hglrc);
1733 if (!has_opengl()) return 0;
1735 wine_tsx11_lock();
1736 /* A game (Half Life not to name it) deletes twice the same context,
1737 * so make sure it is valid first */
1738 if (is_valid_context( ctx ))
1740 if (ctx->ctx) pglXDestroyContext(gdi_display, ctx->ctx);
1741 free_context(ctx);
1743 else
1745 WARN("Error deleting context !\n");
1746 SetLastError(ERROR_INVALID_HANDLE);
1747 ret = FALSE;
1749 wine_tsx11_unlock();
1751 return ret;
1755 * X11DRV_wglGetCurrentReadDCARB
1757 * For OpenGL32 wglGetCurrentReadDCARB.
1759 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void)
1761 HDC ret = 0;
1762 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
1764 if (ctx) ret = ctx->read_hdc;
1766 TRACE(" returning %p (GL drawable %lu)\n", ret, ctx ? ctx->drawables[1] : 0);
1767 return ret;
1771 * X11DRV_wglGetProcAddress
1773 * For OpenGL32 wglGetProcAddress.
1775 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1777 int i, j;
1778 const WineGLExtension *ext;
1780 int padding = 32 - strlen(lpszProc);
1781 if (padding < 0)
1782 padding = 0;
1784 if (!has_opengl()) return NULL;
1786 /* Check the table of WGL extensions to see if we need to return a WGL extension
1787 * or a function pointer to a native OpenGL function. */
1788 if(strncmp(lpszProc, "wgl", 3) != 0) {
1789 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1790 } else {
1791 TRACE("('%s'):%*s", lpszProc, padding, " ");
1792 for (i = 0; i < WineGLExtensionListSize; ++i) {
1793 ext = WineGLExtensionList[i];
1794 for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1795 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1796 TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1797 return ext->extEntryPoints[j].funcAddress;
1803 WARN("(%s) - not found\n", lpszProc);
1804 return NULL;
1808 * X11DRV_wglMakeCurrent
1810 * For OpenGL32 wglMakeCurrent.
1812 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1813 BOOL ret;
1814 HDC hdc = physDev->hdc;
1815 DWORD type = GetObjectType(hdc);
1816 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1818 TRACE("(%p,%p)\n", hdc, hglrc);
1820 if (!has_opengl()) return FALSE;
1822 wine_tsx11_lock();
1823 if (hglrc == NULL) {
1824 ret = pglXMakeCurrent(gdi_display, None, NULL);
1825 NtCurrentTeb()->glContext = NULL;
1826 } else if (ctx->fmt->iPixelFormat != physDev->current_pf) {
1827 WARN( "mismatched pixel format hdc %p %u ctx %p %u\n",
1828 hdc, physDev->current_pf, ctx, ctx->fmt->iPixelFormat );
1829 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1830 ret = FALSE;
1831 } else {
1832 Drawable drawable = get_glxdrawable(physDev);
1833 if (ctx->ctx == NULL) {
1834 /* The describe lines below are for debugging purposes only */
1835 if (TRACE_ON(wgl)) {
1836 describeDrawable(ctx, drawable);
1837 describeContext(ctx);
1840 /* Create a GLX context using the same visual as chosen earlier in wglCreateContext.
1841 * We are certain that the drawable and context are compatible as we only allow compatible formats.
1843 TRACE(" Creating GLX Context\n");
1844 ctx->ctx = create_glxcontext(gdi_display, ctx, NULL);
1845 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1847 TRACE(" make current for dis %p, drawable %p, ctx %p\n", gdi_display, (void*) drawable, ctx->ctx);
1848 ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1849 NtCurrentTeb()->glContext = ctx;
1850 if(ret)
1852 ctx->hdc = hdc;
1853 ctx->read_hdc = hdc;
1854 ctx->drawables[0] = drawable;
1855 ctx->drawables[1] = drawable;
1856 ctx->refresh_drawables = FALSE;
1858 if (type == OBJ_MEMDC)
1860 ctx->do_escape = TRUE;
1861 pglDrawBuffer(GL_FRONT_LEFT);
1865 wine_tsx11_unlock();
1866 TRACE(" returning %s\n", (ret ? "True" : "False"));
1867 return ret;
1871 * X11DRV_wglMakeContextCurrentARB
1873 * For OpenGL32 wglMakeContextCurrentARB
1875 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* pDrawDev, X11DRV_PDEVICE* pReadDev, HGLRC hglrc)
1877 BOOL ret;
1879 TRACE("(%p,%p,%p)\n", pDrawDev, pReadDev, hglrc);
1881 if (!has_opengl()) return 0;
1883 wine_tsx11_lock();
1884 if (hglrc == NULL) {
1885 ret = pglXMakeCurrent(gdi_display, None, NULL);
1886 NtCurrentTeb()->glContext = NULL;
1887 } else {
1888 if (NULL == pglXMakeContextCurrent) {
1889 ret = FALSE;
1890 } else {
1891 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1892 Drawable d_draw = get_glxdrawable(pDrawDev);
1893 Drawable d_read = get_glxdrawable(pReadDev);
1895 if (ctx->ctx == NULL) {
1896 ctx->ctx = create_glxcontext(gdi_display, ctx, NULL);
1897 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1899 ctx->hdc = pDrawDev->hdc;
1900 ctx->read_hdc = pReadDev->hdc;
1901 ctx->drawables[0] = d_draw;
1902 ctx->drawables[1] = d_read;
1903 ctx->refresh_drawables = FALSE;
1904 ret = pglXMakeContextCurrent(gdi_display, d_draw, d_read, ctx->ctx);
1905 NtCurrentTeb()->glContext = ctx;
1908 wine_tsx11_unlock();
1910 TRACE(" returning %s\n", (ret ? "True" : "False"));
1911 return ret;
1915 * X11DRV_wglShareLists
1917 * For OpenGL32 wglShareLists.
1919 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1920 Wine_GLContext *org = (Wine_GLContext *) hglrc1;
1921 Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1923 TRACE("(%p, %p)\n", org, dest);
1925 if (!has_opengl()) return FALSE;
1927 if (NULL != dest && dest->ctx != NULL) {
1928 ERR("Could not share display lists, context already created !\n");
1929 return FALSE;
1930 } else {
1931 if(org && dest && (GetObjectType(org->hdc) == OBJ_MEMDC) ^ (GetObjectType(dest->hdc) == OBJ_MEMDC)) {
1932 WARN("Attempting to share a context between a direct and indirect rendering context, expect issues!\n");
1935 if (org->ctx == NULL) {
1936 wine_tsx11_lock();
1937 describeContext(org);
1939 org->ctx = create_glxcontext(gdi_display, org, NULL);
1940 wine_tsx11_unlock();
1941 TRACE(" created a delayed OpenGL context (%p) for Wine context %p\n", org->ctx, org);
1943 if (NULL != dest) {
1944 wine_tsx11_lock();
1945 describeContext(dest);
1946 dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1947 wine_tsx11_unlock();
1948 TRACE(" created a delayed OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1949 return TRUE;
1952 return FALSE;
1955 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1957 /* We are running using client-side rendering fonts... */
1958 GLYPHMETRICS gm;
1959 unsigned int glyph, size = 0;
1960 void *bitmap = NULL, *gl_bitmap = NULL;
1961 int org_alignment;
1963 wine_tsx11_lock();
1964 pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1965 pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1966 wine_tsx11_unlock();
1968 for (glyph = first; glyph < first + count; glyph++) {
1969 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1970 unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1971 unsigned int height, width_int;
1973 TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1974 if (needed_size == GDI_ERROR) {
1975 TRACE(" - needed size : %d (GDI_ERROR)\n", needed_size);
1976 goto error;
1977 } else {
1978 TRACE(" - needed size : %d\n", needed_size);
1981 if (needed_size > size) {
1982 size = needed_size;
1983 HeapFree(GetProcessHeap(), 0, bitmap);
1984 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1985 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1986 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1988 if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) == GDI_ERROR)
1989 goto error;
1990 if (TRACE_ON(wgl)) {
1991 unsigned int height, width, bitmask;
1992 unsigned char *bitmap_ = bitmap;
1994 TRACE(" - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1995 TRACE(" - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1996 TRACE(" - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1997 if (needed_size != 0) {
1998 TRACE(" - bitmap :\n");
1999 for (height = 0; height < gm.gmBlackBoxY; height++) {
2000 TRACE(" ");
2001 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
2002 if (bitmask == 0) {
2003 bitmap_ += 1;
2004 bitmask = 0x80;
2006 if (*bitmap_ & bitmask)
2007 TRACE("*");
2008 else
2009 TRACE(" ");
2011 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
2012 TRACE("\n");
2017 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
2018 * glyph for it to be drawn properly.
2020 if (needed_size != 0) {
2021 width_int = (gm.gmBlackBoxX + 31) / 32;
2022 for (height = 0; height < gm.gmBlackBoxY; height++) {
2023 unsigned int width;
2024 for (width = 0; width < width_int; width++) {
2025 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
2026 ((int *) bitmap)[height * width_int + width];
2031 wine_tsx11_lock();
2032 pglNewList(listBase++, GL_COMPILE);
2033 if (needed_size != 0) {
2034 pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
2035 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
2036 gm.gmCellIncX, gm.gmCellIncY,
2037 gl_bitmap);
2038 } else {
2039 /* This is the case of 'empty' glyphs like the space character */
2040 pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
2042 pglEndList();
2043 wine_tsx11_unlock();
2046 wine_tsx11_lock();
2047 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2048 wine_tsx11_unlock();
2050 HeapFree(GetProcessHeap(), 0, bitmap);
2051 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2052 return TRUE;
2054 error:
2055 wine_tsx11_lock();
2056 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2057 wine_tsx11_unlock();
2059 HeapFree(GetProcessHeap(), 0, bitmap);
2060 HeapFree(GetProcessHeap(), 0, gl_bitmap);
2061 return FALSE;
2065 * X11DRV_wglUseFontBitmapsA
2067 * For OpenGL32 wglUseFontBitmapsA.
2069 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2071 Font fid = physDev->font;
2073 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2075 if (!has_opengl()) return FALSE;
2077 if (fid == 0) {
2078 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
2081 wine_tsx11_lock();
2082 /* I assume that the glyphs are at the same position for X and for Windows */
2083 pglXUseXFont(fid, first, count, listBase);
2084 wine_tsx11_unlock();
2085 return TRUE;
2089 * X11DRV_wglUseFontBitmapsW
2091 * For OpenGL32 wglUseFontBitmapsW.
2093 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2095 Font fid = physDev->font;
2097 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2099 if (!has_opengl()) return FALSE;
2101 if (fid == 0) {
2102 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
2105 WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
2107 wine_tsx11_lock();
2108 /* I assume that the glyphs are at the same position for X and for Windows */
2109 pglXUseXFont(fid, first, count, listBase);
2110 wine_tsx11_unlock();
2111 return TRUE;
2114 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
2115 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
2117 wine_tsx11_lock();
2118 switch(pname)
2120 case GL_DEPTH_BITS:
2122 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2124 pglGetIntegerv(pname, params);
2126 * if we cannot find a Wine Context
2127 * we only have the default wine desktop context,
2128 * so if we have only a 24 depth say we have 32
2130 if (!ctx && *params == 24) {
2131 *params = 32;
2133 TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
2134 break;
2136 case GL_ALPHA_BITS:
2138 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2140 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_ALPHA_SIZE, params);
2141 TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
2142 break;
2144 default:
2145 pglGetIntegerv(pname, params);
2146 break;
2148 wine_tsx11_unlock();
2151 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
2153 int w, h;
2155 if (!physDev->gl_copy)
2156 return;
2158 w = physDev->dc_rect.right - physDev->dc_rect.left;
2159 h = physDev->dc_rect.bottom - physDev->dc_rect.top;
2161 if(w > 0 && h > 0) {
2162 Drawable src = physDev->pixmap;
2163 if(!src) src = physDev->gl_drawable;
2165 /* The GL drawable may be lagged behind if we don't flush first, so
2166 * flush the display make sure we copy up-to-date data */
2167 wine_tsx11_lock();
2168 XFlush(gdi_display);
2169 XSetFunction(gdi_display, physDev->gc, GXcopy);
2170 XCopyArea(gdi_display, src, physDev->drawable, physDev->gc, 0, 0, w, h,
2171 physDev->dc_rect.left, physDev->dc_rect.top);
2172 wine_tsx11_unlock();
2177 static void WINAPI X11DRV_wglFinish(void)
2179 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2180 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2182 wine_tsx11_lock();
2183 sync_context(ctx);
2184 pglFinish();
2185 wine_tsx11_unlock();
2186 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2189 static void WINAPI X11DRV_wglFlush(void)
2191 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2192 enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2194 wine_tsx11_lock();
2195 sync_context(ctx);
2196 pglFlush();
2197 wine_tsx11_unlock();
2198 if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2202 * X11DRV_wglGetExtensionsStringARB
2204 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2206 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
2207 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2208 return WineGLInfo.wglExtensions;
2212 * X11DRV_wglCreatePbufferARB
2214 * WGL_ARB_pbuffer: wglCreatePbufferARB
2216 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
2218 Wine_GLPBuffer* object = NULL;
2219 WineGLPixelFormat *fmt = NULL;
2220 int nCfgs = 0;
2221 int attribs[256];
2222 int nAttribs = 0;
2224 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2226 if (0 >= iPixelFormat) {
2227 ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2228 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2229 return NULL; /* unexpected error */
2232 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2233 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2234 if(!fmt) {
2235 ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2236 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2237 goto create_failed; /* unexpected error */
2240 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2241 if (NULL == object) {
2242 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2243 goto create_failed; /* unexpected error */
2245 object->hdc = hdc;
2246 object->display = gdi_display;
2247 object->width = iWidth;
2248 object->height = iHeight;
2249 object->fmt = fmt;
2251 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2252 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2253 while (piAttribList && 0 != *piAttribList) {
2254 int attr_v;
2255 switch (*piAttribList) {
2256 case WGL_PBUFFER_LARGEST_ARB: {
2257 ++piAttribList;
2258 attr_v = *piAttribList;
2259 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2260 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2261 break;
2264 case WGL_TEXTURE_FORMAT_ARB: {
2265 ++piAttribList;
2266 attr_v = *piAttribList;
2267 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2268 if (use_render_texture_ati) {
2269 int type = 0;
2270 switch (attr_v) {
2271 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2272 case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2273 case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2274 default:
2275 SetLastError(ERROR_INVALID_DATA);
2276 goto create_failed;
2278 object->use_render_texture = 1;
2279 PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2280 } else {
2281 if (WGL_NO_TEXTURE_ARB == attr_v) {
2282 object->use_render_texture = 0;
2283 } else {
2284 if (!use_render_texture_emulation) {
2285 SetLastError(ERROR_INVALID_DATA);
2286 goto create_failed;
2288 switch (attr_v) {
2289 case WGL_TEXTURE_RGB_ARB:
2290 object->use_render_texture = GL_RGB;
2291 object->texture_bpp = 3;
2292 object->texture_format = GL_RGB;
2293 object->texture_type = GL_UNSIGNED_BYTE;
2294 break;
2295 case WGL_TEXTURE_RGBA_ARB:
2296 object->use_render_texture = GL_RGBA;
2297 object->texture_bpp = 4;
2298 object->texture_format = GL_RGBA;
2299 object->texture_type = GL_UNSIGNED_BYTE;
2300 break;
2302 /* WGL_FLOAT_COMPONENTS_NV */
2303 case WGL_TEXTURE_FLOAT_R_NV:
2304 object->use_render_texture = GL_FLOAT_R_NV;
2305 object->texture_bpp = 4;
2306 object->texture_format = GL_RED;
2307 object->texture_type = GL_FLOAT;
2308 break;
2309 case WGL_TEXTURE_FLOAT_RG_NV:
2310 object->use_render_texture = GL_FLOAT_RG_NV;
2311 object->texture_bpp = 8;
2312 object->texture_format = GL_LUMINANCE_ALPHA;
2313 object->texture_type = GL_FLOAT;
2314 break;
2315 case WGL_TEXTURE_FLOAT_RGB_NV:
2316 object->use_render_texture = GL_FLOAT_RGB_NV;
2317 object->texture_bpp = 12;
2318 object->texture_format = GL_RGB;
2319 object->texture_type = GL_FLOAT;
2320 break;
2321 case WGL_TEXTURE_FLOAT_RGBA_NV:
2322 object->use_render_texture = GL_FLOAT_RGBA_NV;
2323 object->texture_bpp = 16;
2324 object->texture_format = GL_RGBA;
2325 object->texture_type = GL_FLOAT;
2326 break;
2327 default:
2328 ERR("Unknown texture format: %x\n", attr_v);
2329 SetLastError(ERROR_INVALID_DATA);
2330 goto create_failed;
2334 break;
2337 case WGL_TEXTURE_TARGET_ARB: {
2338 ++piAttribList;
2339 attr_v = *piAttribList;
2340 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2341 if (use_render_texture_ati) {
2342 int type = 0;
2343 switch (attr_v) {
2344 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2345 case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2346 case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2347 case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2348 default:
2349 SetLastError(ERROR_INVALID_DATA);
2350 goto create_failed;
2352 PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2353 } else {
2354 if (WGL_NO_TEXTURE_ARB == attr_v) {
2355 object->texture_target = 0;
2356 } else {
2357 if (!use_render_texture_emulation) {
2358 SetLastError(ERROR_INVALID_DATA);
2359 goto create_failed;
2361 switch (attr_v) {
2362 case WGL_TEXTURE_CUBE_MAP_ARB: {
2363 if (iWidth != iHeight) {
2364 SetLastError(ERROR_INVALID_DATA);
2365 goto create_failed;
2367 object->texture_target = GL_TEXTURE_CUBE_MAP;
2368 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2369 break;
2371 case WGL_TEXTURE_1D_ARB: {
2372 if (1 != iHeight) {
2373 SetLastError(ERROR_INVALID_DATA);
2374 goto create_failed;
2376 object->texture_target = GL_TEXTURE_1D;
2377 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2378 break;
2380 case WGL_TEXTURE_2D_ARB: {
2381 object->texture_target = GL_TEXTURE_2D;
2382 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2383 break;
2385 case WGL_TEXTURE_RECTANGLE_NV: {
2386 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2387 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2388 break;
2390 default:
2391 ERR("Unknown texture target: %x\n", attr_v);
2392 SetLastError(ERROR_INVALID_DATA);
2393 goto create_failed;
2397 break;
2400 case WGL_MIPMAP_TEXTURE_ARB: {
2401 ++piAttribList;
2402 attr_v = *piAttribList;
2403 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2404 if (use_render_texture_ati) {
2405 PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2406 } else {
2407 if (!use_render_texture_emulation) {
2408 SetLastError(ERROR_INVALID_DATA);
2409 goto create_failed;
2412 break;
2415 ++piAttribList;
2418 PUSH1(attribs, None);
2419 wine_tsx11_lock();
2420 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2421 wine_tsx11_unlock();
2422 TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
2423 if (!object->drawable) {
2424 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2425 goto create_failed; /* unexpected error */
2427 TRACE("->(%p)\n", object);
2428 return object;
2430 create_failed:
2431 HeapFree(GetProcessHeap(), 0, object);
2432 TRACE("->(FAILED)\n");
2433 return NULL;
2437 * X11DRV_wglDestroyPbufferARB
2439 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2441 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2443 Wine_GLPBuffer* object = hPbuffer;
2444 TRACE("(%p)\n", hPbuffer);
2445 if (NULL == object) {
2446 SetLastError(ERROR_INVALID_HANDLE);
2447 return GL_FALSE;
2449 wine_tsx11_lock();
2450 pglXDestroyPbuffer(object->display, object->drawable);
2451 wine_tsx11_unlock();
2452 HeapFree(GetProcessHeap(), 0, object);
2453 return GL_TRUE;
2457 * X11DRV_wglGetPbufferDCARB
2459 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2460 * The function wglGetPbufferDCARB returns a device context for a pbuffer.
2461 * Gdi32 implements the part of this function which creates a device context.
2462 * This part associates the physDev with the X drawable of the pbuffer.
2464 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
2466 Wine_GLPBuffer* object = hPbuffer;
2467 if (NULL == object) {
2468 SetLastError(ERROR_INVALID_HANDLE);
2469 return NULL;
2472 /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
2473 * All formats in our pixelformat list are compatible with each other and the main drawable. */
2474 physDev->current_pf = object->fmt->iPixelFormat;
2475 physDev->drawable = object->drawable;
2476 SetRect( &physDev->drawable_rect, 0, 0, object->width, object->height );
2477 physDev->dc_rect = physDev->drawable_rect;
2479 TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
2480 return physDev->hdc;
2484 * X11DRV_wglQueryPbufferARB
2486 * WGL_ARB_pbuffer: wglQueryPbufferARB
2488 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2490 Wine_GLPBuffer* object = hPbuffer;
2491 TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2492 if (NULL == object) {
2493 SetLastError(ERROR_INVALID_HANDLE);
2494 return GL_FALSE;
2496 switch (iAttribute) {
2497 case WGL_PBUFFER_WIDTH_ARB:
2498 wine_tsx11_lock();
2499 pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2500 wine_tsx11_unlock();
2501 break;
2502 case WGL_PBUFFER_HEIGHT_ARB:
2503 wine_tsx11_lock();
2504 pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2505 wine_tsx11_unlock();
2506 break;
2508 case WGL_PBUFFER_LOST_ARB:
2509 /* GLX Pbuffers cannot be lost by default. We can support this by
2510 * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2511 * to receive pixel buffer clobber events, however that may or may
2512 * not give any benefit */
2513 *piValue = GL_FALSE;
2514 break;
2516 case WGL_TEXTURE_FORMAT_ARB:
2517 if (use_render_texture_ati) {
2518 unsigned int tmp;
2519 int type = WGL_NO_TEXTURE_ARB;
2520 wine_tsx11_lock();
2521 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2522 wine_tsx11_unlock();
2523 switch (tmp) {
2524 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2525 case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2526 case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2528 *piValue = type;
2529 } else {
2530 if (!object->use_render_texture) {
2531 *piValue = WGL_NO_TEXTURE_ARB;
2532 } else {
2533 if (!use_render_texture_emulation) {
2534 SetLastError(ERROR_INVALID_HANDLE);
2535 return GL_FALSE;
2537 switch(object->use_render_texture) {
2538 case GL_RGB:
2539 *piValue = WGL_TEXTURE_RGB_ARB;
2540 break;
2541 case GL_RGBA:
2542 *piValue = WGL_TEXTURE_RGBA_ARB;
2543 break;
2544 /* WGL_FLOAT_COMPONENTS_NV */
2545 case GL_FLOAT_R_NV:
2546 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2547 break;
2548 case GL_FLOAT_RG_NV:
2549 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2550 break;
2551 case GL_FLOAT_RGB_NV:
2552 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2553 break;
2554 case GL_FLOAT_RGBA_NV:
2555 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2556 break;
2557 default:
2558 ERR("Unknown texture format: %x\n", object->use_render_texture);
2562 break;
2564 case WGL_TEXTURE_TARGET_ARB:
2565 if (use_render_texture_ati) {
2566 unsigned int tmp;
2567 int type = WGL_NO_TEXTURE_ARB;
2568 wine_tsx11_lock();
2569 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2570 wine_tsx11_unlock();
2571 switch (tmp) {
2572 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2573 case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2574 case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2575 case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2577 *piValue = type;
2578 } else {
2579 if (!object->texture_target) {
2580 *piValue = WGL_NO_TEXTURE_ARB;
2581 } else {
2582 if (!use_render_texture_emulation) {
2583 SetLastError(ERROR_INVALID_DATA);
2584 return GL_FALSE;
2586 switch (object->texture_target) {
2587 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2588 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2589 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2590 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2594 break;
2596 case WGL_MIPMAP_TEXTURE_ARB:
2597 if (use_render_texture_ati) {
2598 wine_tsx11_lock();
2599 pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2600 wine_tsx11_unlock();
2601 } else {
2602 *piValue = GL_FALSE; /** don't support that */
2603 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2605 break;
2607 default:
2608 FIXME("unexpected attribute %x\n", iAttribute);
2609 break;
2612 return GL_TRUE;
2616 * X11DRV_wglReleasePbufferDCARB
2618 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2620 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2622 TRACE("(%p, %p)\n", hPbuffer, hdc);
2623 return DeleteDC(hdc);
2627 * X11DRV_wglSetPbufferAttribARB
2629 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2631 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2633 Wine_GLPBuffer* object = hPbuffer;
2634 GLboolean ret = GL_FALSE;
2636 WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2637 if (NULL == object) {
2638 SetLastError(ERROR_INVALID_HANDLE);
2639 return GL_FALSE;
2641 if (!object->use_render_texture) {
2642 SetLastError(ERROR_INVALID_HANDLE);
2643 return GL_FALSE;
2645 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2646 return GL_TRUE;
2648 if (NULL != pglXDrawableAttribATI) {
2649 if (use_render_texture_ati) {
2650 FIXME("Need conversion for GLX_ATI_render_texture\n");
2652 wine_tsx11_lock();
2653 ret = pglXDrawableAttribATI(object->display, object->drawable, piAttribList);
2654 wine_tsx11_unlock();
2656 return ret;
2660 * X11DRV_wglChoosePixelFormatARB
2662 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2664 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2666 int gl_test = 0;
2667 int attribs[256];
2668 int nAttribs = 0;
2669 GLXFBConfig* cfgs = NULL;
2670 int nCfgs = 0;
2671 int it;
2672 int fmt_id;
2673 WineGLPixelFormat *fmt;
2674 UINT pfmt_it = 0;
2675 int run;
2676 int i;
2677 DWORD dwFlags = 0;
2679 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2680 if (NULL != pfAttribFList) {
2681 FIXME("unused pfAttribFList\n");
2684 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2685 if (-1 == nAttribs) {
2686 WARN("Cannot convert WGL to GLX attributes\n");
2687 return GL_FALSE;
2689 PUSH1(attribs, None);
2691 /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2692 * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the WineGLPixelFormat. We don't query these attributes
2693 * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on by passing a dwFlags to 'ConvertPixelFormatGLXtoWGL'. */
2694 for(i=0; piAttribIList[i] != 0; i+=2)
2696 switch(piAttribIList[i])
2698 case WGL_DRAW_TO_BITMAP_ARB:
2699 if(piAttribIList[i+1])
2700 dwFlags |= PFD_DRAW_TO_BITMAP;
2701 break;
2702 case WGL_ACCELERATION_ARB:
2703 switch(piAttribIList[i+1])
2705 case WGL_NO_ACCELERATION_ARB:
2706 dwFlags |= PFD_GENERIC_FORMAT;
2707 break;
2708 case WGL_GENERIC_ACCELERATION_ARB:
2709 dwFlags |= PFD_GENERIC_ACCELERATED;
2710 break;
2711 case WGL_FULL_ACCELERATION_ARB:
2712 /* Nothing to do */
2713 break;
2715 break;
2716 case WGL_SUPPORT_GDI_ARB:
2717 if(piAttribIList[i+1])
2718 dwFlags |= PFD_SUPPORT_GDI;
2719 break;
2723 /* Search for FB configurations matching the requirements in attribs */
2724 wine_tsx11_lock();
2725 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2726 if (NULL == cfgs) {
2727 wine_tsx11_unlock();
2728 WARN("Compatible Pixel Format not found\n");
2729 return GL_FALSE;
2732 /* Loop through all matching formats and check if they are suitable.
2733 * Note that this function should at max return nMaxFormats different formats */
2734 for(run=0; run < 2; run++)
2736 for (it = 0; it < nCfgs; ++it) {
2737 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2738 if (gl_test) {
2739 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2740 continue;
2743 /* Search for the format in our list of compatible formats */
2744 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id, dwFlags);
2745 if(!fmt)
2746 continue;
2748 /* During the first run we only want onscreen formats and during the second only offscreen 'XOR' */
2749 if( ((run == 0) && fmt->offscreenOnly) || ((run == 1) && !fmt->offscreenOnly) )
2750 continue;
2752 if(pfmt_it < nMaxFormats) {
2753 piFormats[pfmt_it] = fmt->iPixelFormat;
2754 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2756 pfmt_it++;
2760 *nNumFormats = pfmt_it;
2761 /** free list */
2762 XFree(cfgs);
2763 wine_tsx11_unlock();
2764 return GL_TRUE;
2768 * X11DRV_wglGetPixelFormatAttribivARB
2770 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2772 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2774 UINT i;
2775 WineGLPixelFormat *fmt = NULL;
2776 int hTest;
2777 int tmp;
2778 int curGLXAttr = 0;
2779 int nWGLFormats = 0;
2781 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2783 if (0 < iLayerPlane) {
2784 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2785 return GL_FALSE;
2788 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2789 * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2790 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2791 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2792 if(!fmt) {
2793 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2796 wine_tsx11_lock();
2797 for (i = 0; i < nAttributes; ++i) {
2798 const int curWGLAttr = piAttributes[i];
2799 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2801 switch (curWGLAttr) {
2802 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2803 piValues[i] = nWGLFormats;
2804 continue;
2806 case WGL_SUPPORT_OPENGL_ARB:
2807 piValues[i] = GL_TRUE;
2808 continue;
2810 case WGL_ACCELERATION_ARB:
2811 curGLXAttr = GLX_CONFIG_CAVEAT;
2812 if (!fmt) goto pix_error;
2813 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2814 piValues[i] = WGL_NO_ACCELERATION_ARB;
2815 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2816 piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2817 else
2818 piValues[i] = WGL_FULL_ACCELERATION_ARB;
2819 continue;
2821 case WGL_TRANSPARENT_ARB:
2822 curGLXAttr = GLX_TRANSPARENT_TYPE;
2823 if (!fmt) goto pix_error;
2824 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2825 if (hTest) goto get_error;
2826 piValues[i] = GL_FALSE;
2827 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2828 continue;
2830 case WGL_PIXEL_TYPE_ARB:
2831 curGLXAttr = GLX_RENDER_TYPE;
2832 if (!fmt) goto pix_error;
2833 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2834 if (hTest) goto get_error;
2835 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2836 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2837 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2838 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2839 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2840 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2841 else {
2842 ERR("unexpected RenderType(%x)\n", tmp);
2843 piValues[i] = WGL_TYPE_RGBA_ARB;
2845 continue;
2847 case WGL_COLOR_BITS_ARB:
2848 curGLXAttr = GLX_BUFFER_SIZE;
2849 break;
2851 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2852 if (use_render_texture_ati) {
2853 curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2854 break;
2856 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2857 if (use_render_texture_ati) {
2858 curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2859 break;
2861 if (!use_render_texture_emulation) {
2862 piValues[i] = GL_FALSE;
2863 continue;
2865 curGLXAttr = GLX_RENDER_TYPE;
2866 if (!fmt) goto pix_error;
2867 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2868 if (hTest) goto get_error;
2869 if (GLX_COLOR_INDEX_BIT == tmp) {
2870 piValues[i] = GL_FALSE;
2871 continue;
2873 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2874 if (hTest) goto get_error;
2875 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2876 continue;
2878 case WGL_BLUE_BITS_ARB:
2879 curGLXAttr = GLX_BLUE_SIZE;
2880 break;
2881 case WGL_RED_BITS_ARB:
2882 curGLXAttr = GLX_RED_SIZE;
2883 break;
2884 case WGL_GREEN_BITS_ARB:
2885 curGLXAttr = GLX_GREEN_SIZE;
2886 break;
2887 case WGL_ALPHA_BITS_ARB:
2888 curGLXAttr = GLX_ALPHA_SIZE;
2889 break;
2890 case WGL_DEPTH_BITS_ARB:
2891 curGLXAttr = GLX_DEPTH_SIZE;
2892 break;
2893 case WGL_STENCIL_BITS_ARB:
2894 curGLXAttr = GLX_STENCIL_SIZE;
2895 break;
2896 case WGL_DOUBLE_BUFFER_ARB:
2897 curGLXAttr = GLX_DOUBLEBUFFER;
2898 break;
2899 case WGL_STEREO_ARB:
2900 curGLXAttr = GLX_STEREO;
2901 break;
2902 case WGL_AUX_BUFFERS_ARB:
2903 curGLXAttr = GLX_AUX_BUFFERS;
2904 break;
2906 case WGL_SUPPORT_GDI_ARB:
2907 if (!fmt) goto pix_error;
2908 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) ? TRUE : FALSE;
2909 continue;
2911 case WGL_DRAW_TO_BITMAP_ARB:
2912 if (!fmt) goto pix_error;
2913 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? TRUE : FALSE;
2914 continue;
2916 case WGL_DRAW_TO_WINDOW_ARB:
2917 case WGL_DRAW_TO_PBUFFER_ARB:
2918 if (!fmt) goto pix_error;
2919 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2920 if (hTest) goto get_error;
2921 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2922 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2923 piValues[i] = GL_TRUE;
2924 else
2925 piValues[i] = GL_FALSE;
2926 continue;
2928 case WGL_SWAP_METHOD_ARB:
2929 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
2930 * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
2931 * point only ATI offers this.
2933 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
2934 break;
2936 case WGL_PBUFFER_LARGEST_ARB:
2937 curGLXAttr = GLX_LARGEST_PBUFFER;
2938 break;
2940 case WGL_SAMPLE_BUFFERS_ARB:
2941 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2942 break;
2944 case WGL_SAMPLES_ARB:
2945 curGLXAttr = GLX_SAMPLES_ARB;
2946 break;
2948 case WGL_FLOAT_COMPONENTS_NV:
2949 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2950 break;
2952 case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
2953 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
2954 break;
2956 case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
2957 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
2958 break;
2960 case WGL_ACCUM_RED_BITS_ARB:
2961 curGLXAttr = GLX_ACCUM_RED_SIZE;
2962 break;
2963 case WGL_ACCUM_GREEN_BITS_ARB:
2964 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
2965 break;
2966 case WGL_ACCUM_BLUE_BITS_ARB:
2967 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
2968 break;
2969 case WGL_ACCUM_ALPHA_BITS_ARB:
2970 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
2971 break;
2972 case WGL_ACCUM_BITS_ARB:
2973 if (!fmt) goto pix_error;
2974 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
2975 if (hTest) goto get_error;
2976 piValues[i] = tmp;
2977 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
2978 if (hTest) goto get_error;
2979 piValues[i] += tmp;
2980 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
2981 if (hTest) goto get_error;
2982 piValues[i] += tmp;
2983 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
2984 if (hTest) goto get_error;
2985 piValues[i] += tmp;
2986 continue;
2988 default:
2989 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2992 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2993 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2994 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2996 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2997 * and check which options can work using iPixelFormat=0 and which not.
2998 * A problem would be that this function is an extension. This would
2999 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
3001 if (0 != curGLXAttr && iPixelFormat != 0) {
3002 if (!fmt) goto pix_error;
3003 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
3004 if (hTest) goto get_error;
3005 curGLXAttr = 0;
3006 } else {
3007 piValues[i] = GL_FALSE;
3010 wine_tsx11_unlock();
3011 return GL_TRUE;
3013 get_error:
3014 wine_tsx11_unlock();
3015 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
3016 return GL_FALSE;
3018 pix_error:
3019 wine_tsx11_unlock();
3020 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
3021 return GL_FALSE;
3025 * X11DRV_wglGetPixelFormatAttribfvARB
3027 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
3029 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
3031 int *attr;
3032 int ret;
3033 UINT i;
3035 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
3037 /* Allocate a temporary array to store integer values */
3038 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
3039 if (!attr) {
3040 ERR("couldn't allocate %d array\n", nAttributes);
3041 return GL_FALSE;
3044 /* Piggy-back on wglGetPixelFormatAttribivARB */
3045 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
3046 if (ret) {
3047 /* Convert integer values to float. Should also check for attributes
3048 that can give decimal values here */
3049 for (i=0; i<nAttributes;i++) {
3050 pfValues[i] = attr[i];
3054 HeapFree(GetProcessHeap(), 0, attr);
3055 return ret;
3059 * X11DRV_wglBindTexImageARB
3061 * WGL_ARB_render_texture: wglBindTexImageARB
3063 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3065 Wine_GLPBuffer* object = hPbuffer;
3066 GLboolean ret = GL_FALSE;
3068 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3069 if (NULL == object) {
3070 SetLastError(ERROR_INVALID_HANDLE);
3071 return GL_FALSE;
3073 if (!object->use_render_texture) {
3074 SetLastError(ERROR_INVALID_HANDLE);
3075 return GL_FALSE;
3078 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3079 static int init = 0;
3080 int prev_binded_texture = 0;
3081 GLXContext prev_context;
3082 Drawable prev_drawable;
3083 GLXContext tmp_context;
3085 wine_tsx11_lock();
3086 prev_context = pglXGetCurrentContext();
3087 prev_drawable = pglXGetCurrentDrawable();
3089 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
3090 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
3091 isn't ideal performance wise but I wasn't able to get other ways working.
3093 if(!init) {
3094 init = 1; /* Only show the FIXME once for performance reasons */
3095 FIXME("partial stub!\n");
3098 TRACE("drawable=%p, context=%p\n", (void*)object->drawable, prev_context);
3099 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3101 pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3103 /* Switch to our pbuffer */
3104 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
3106 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3107 * After that upload the pbuffer texture data. */
3108 pglBindTexture(object->texture_target, prev_binded_texture);
3109 pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3111 /* Switch back to the original drawable and upload the pbuffer-texture */
3112 pglXMakeCurrent(object->display, prev_drawable, prev_context);
3113 pglXDestroyContext(gdi_display, tmp_context);
3114 wine_tsx11_unlock();
3115 return GL_TRUE;
3118 if (NULL != pglXBindTexImageATI) {
3119 int buffer;
3121 switch(iBuffer)
3123 case WGL_FRONT_LEFT_ARB:
3124 buffer = GLX_FRONT_LEFT_ATI;
3125 break;
3126 case WGL_FRONT_RIGHT_ARB:
3127 buffer = GLX_FRONT_RIGHT_ATI;
3128 break;
3129 case WGL_BACK_LEFT_ARB:
3130 buffer = GLX_BACK_LEFT_ATI;
3131 break;
3132 case WGL_BACK_RIGHT_ARB:
3133 buffer = GLX_BACK_RIGHT_ATI;
3134 break;
3135 default:
3136 ERR("Unknown iBuffer=%#x\n", iBuffer);
3137 return FALSE;
3140 /* In the sample 'ogl_offscreen_rendering_3' from codesampler.net I get garbage on the screen.
3141 * I'm not sure if that's a bug in the ATI extension or in the program. I think that the program
3142 * expected a single buffering format since it didn't ask for double buffering. A buffer swap
3143 * fixed the program. I don't know what the correct behavior is. On the other hand that demo
3144 * works fine using our pbuffer emulation path.
3146 wine_tsx11_lock();
3147 ret = pglXBindTexImageATI(object->display, object->drawable, buffer);
3148 wine_tsx11_unlock();
3150 return ret;
3154 * X11DRV_wglReleaseTexImageARB
3156 * WGL_ARB_render_texture: wglReleaseTexImageARB
3158 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3160 Wine_GLPBuffer* object = hPbuffer;
3161 GLboolean ret = GL_FALSE;
3163 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3164 if (NULL == object) {
3165 SetLastError(ERROR_INVALID_HANDLE);
3166 return GL_FALSE;
3168 if (!object->use_render_texture) {
3169 SetLastError(ERROR_INVALID_HANDLE);
3170 return GL_FALSE;
3172 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3173 return GL_TRUE;
3175 if (NULL != pglXReleaseTexImageATI) {
3176 int buffer;
3178 switch(iBuffer)
3180 case WGL_FRONT_LEFT_ARB:
3181 buffer = GLX_FRONT_LEFT_ATI;
3182 break;
3183 case WGL_FRONT_RIGHT_ARB:
3184 buffer = GLX_FRONT_RIGHT_ATI;
3185 break;
3186 case WGL_BACK_LEFT_ARB:
3187 buffer = GLX_BACK_LEFT_ATI;
3188 break;
3189 case WGL_BACK_RIGHT_ARB:
3190 buffer = GLX_BACK_RIGHT_ATI;
3191 break;
3192 default:
3193 ERR("Unknown iBuffer=%#x\n", iBuffer);
3194 return FALSE;
3196 wine_tsx11_lock();
3197 ret = pglXReleaseTexImageATI(object->display, object->drawable, buffer);
3198 wine_tsx11_unlock();
3200 return ret;
3204 * X11DRV_wglGetExtensionsStringEXT
3206 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3208 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
3209 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3210 return WineGLInfo.wglExtensions;
3214 * X11DRV_wglGetSwapIntervalEXT
3216 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3218 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
3219 FIXME("(),stub!\n");
3220 return swap_interval;
3224 * X11DRV_wglSwapIntervalEXT
3226 * WGL_EXT_swap_control: wglSwapIntervalEXT
3228 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
3229 BOOL ret = TRUE;
3231 TRACE("(%d)\n", interval);
3232 swap_interval = interval;
3233 if (NULL != pglXSwapIntervalSGI) {
3234 wine_tsx11_lock();
3235 ret = !pglXSwapIntervalSGI(interval);
3236 wine_tsx11_unlock();
3238 else WARN("(): GLX_SGI_swap_control extension seems not supported\n");
3239 return ret;
3243 * X11DRV_wglAllocateMemoryNV
3245 * WGL_NV_vertex_array_range: wglAllocateMemoryNV
3247 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
3248 void *ret = NULL;
3249 TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
3251 if (pglXAllocateMemoryNV)
3253 wine_tsx11_lock();
3254 ret = pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
3255 wine_tsx11_unlock();
3257 return ret;
3261 * X11DRV_wglFreeMemoryNV
3263 * WGL_NV_vertex_array_range: wglFreeMemoryNV
3265 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
3266 TRACE("(%p)\n", pointer);
3267 if (pglXFreeMemoryNV == NULL)
3268 return;
3270 wine_tsx11_lock();
3271 pglXFreeMemoryNV(pointer);
3272 wine_tsx11_unlock();
3276 * X11DRV_wglSetPixelFormatWINE
3278 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3279 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3281 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3283 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
3285 if (!has_opengl()) return FALSE;
3287 if (physDev->current_pf == iPixelFormat) return TRUE;
3289 /* Relay to the core SetPixelFormat */
3290 TRACE("Changing iPixelFormat from %d to %d\n", physDev->current_pf, iPixelFormat);
3291 return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
3295 * glxRequireVersion (internal)
3297 * Check if the supported GLX version matches requiredVersion.
3299 static BOOL glxRequireVersion(int requiredVersion)
3301 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3302 if(requiredVersion <= WineGLInfo.glxVersion[1])
3303 return TRUE;
3305 return FALSE;
3308 static BOOL glxRequireExtension(const char *requiredExtension)
3310 if (strstr(WineGLInfo.glxExtensions, requiredExtension) == NULL) {
3311 return FALSE;
3314 return TRUE;
3317 static void register_extension_string(const char *ext)
3319 if (WineGLInfo.wglExtensions[0])
3320 strcat(WineGLInfo.wglExtensions, " ");
3321 strcat(WineGLInfo.wglExtensions, ext);
3323 TRACE("'%s'\n", ext);
3326 static BOOL register_extension(const WineGLExtension * ext)
3328 int i;
3330 assert( WineGLExtensionListSize < MAX_EXTENSIONS );
3331 WineGLExtensionList[WineGLExtensionListSize++] = ext;
3333 register_extension_string(ext->extName);
3335 for (i = 0; ext->extEntryPoints[i].funcName; ++i)
3336 TRACE(" - '%s'\n", ext->extEntryPoints[i].funcName);
3338 return TRUE;
3341 static const WineGLExtension WGL_internal_functions =
3345 { "wglGetIntegerv", X11DRV_wglGetIntegerv },
3346 { "wglFinish", X11DRV_wglFinish },
3347 { "wglFlush", X11DRV_wglFlush },
3352 static const WineGLExtension WGL_ARB_extensions_string =
3354 "WGL_ARB_extensions_string",
3356 { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
3360 static const WineGLExtension WGL_ARB_make_current_read =
3362 "WGL_ARB_make_current_read",
3364 { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
3365 { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
3369 static const WineGLExtension WGL_ARB_multisample =
3371 "WGL_ARB_multisample",
3374 static const WineGLExtension WGL_ARB_pbuffer =
3376 "WGL_ARB_pbuffer",
3378 { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
3379 { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
3380 { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
3381 { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
3382 { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
3383 { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
3387 static const WineGLExtension WGL_ARB_pixel_format =
3389 "WGL_ARB_pixel_format",
3391 { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3392 { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3393 { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3397 static const WineGLExtension WGL_ARB_render_texture =
3399 "WGL_ARB_render_texture",
3401 { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3402 { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3406 static const WineGLExtension WGL_EXT_extensions_string =
3408 "WGL_EXT_extensions_string",
3410 { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3414 static const WineGLExtension WGL_EXT_swap_control =
3416 "WGL_EXT_swap_control",
3418 { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3419 { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3423 static const WineGLExtension WGL_NV_vertex_array_range =
3425 "WGL_NV_vertex_array_range",
3427 { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3428 { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3432 static const WineGLExtension WGL_WINE_pixel_format_passthrough =
3434 "WGL_WINE_pixel_format_passthrough",
3436 { "wglSetPixelFormatWINE", X11DRV_wglSetPixelFormatWINE },
3441 * X11DRV_WineGL_LoadExtensions
3443 static void X11DRV_WineGL_LoadExtensions(void)
3445 WineGLInfo.wglExtensions[0] = 0;
3447 /* Load Wine internal functions */
3448 register_extension(&WGL_internal_functions);
3450 /* ARB Extensions */
3452 if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3454 register_extension_string("WGL_ARB_pixel_format_float");
3455 register_extension_string("WGL_ATI_pixel_format_float");
3458 register_extension(&WGL_ARB_extensions_string);
3460 if (glxRequireVersion(3))
3461 register_extension(&WGL_ARB_make_current_read);
3463 if (glxRequireExtension("GLX_ARB_multisample"))
3464 register_extension(&WGL_ARB_multisample);
3466 /* In general pbuffer functionality requires support in the X-server. The functionality is
3467 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3468 * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3469 * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3471 * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3472 * without support in the X-server (which other Mesa based drivers require).
3474 * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3475 * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3476 * is available pbuffers must be available too. */
3477 if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3478 register_extension(&WGL_ARB_pbuffer);
3480 register_extension(&WGL_ARB_pixel_format);
3482 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3483 if (glxRequireExtension("GLX_ATI_render_texture") ||
3484 glxRequireExtension("GLX_ARB_render_texture") ||
3485 (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3487 register_extension(&WGL_ARB_render_texture);
3489 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3490 if(glxRequireExtension("GLX_NV_float_buffer"))
3491 register_extension_string("WGL_NV_float_buffer");
3493 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3494 if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3495 register_extension_string("WGL_NV_texture_rectangle");
3498 /* EXT Extensions */
3500 register_extension(&WGL_EXT_extensions_string);
3502 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3503 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3504 register_extension(&WGL_EXT_swap_control);
3506 if(glxRequireExtension("GLX_EXT_framebuffer_sRGB"))
3507 register_extension_string("WGL_EXT_framebuffer_sRGB");
3509 if(glxRequireExtension("GLX_EXT_fbconfig_packed_float"))
3510 register_extension_string("WGL_EXT_pixel_format_packed_float");
3512 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3513 if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3514 register_extension(&WGL_NV_vertex_array_range);
3516 /* WINE-specific WGL Extensions */
3518 /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3519 * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3521 register_extension(&WGL_WINE_pixel_format_passthrough);
3525 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3527 Drawable ret;
3529 if(physDev->bitmap)
3531 if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
3532 ret = physDev->drawable; /* PBuffer */
3533 else
3534 ret = physDev->bitmap->glxpixmap;
3536 else if(physDev->gl_drawable)
3537 ret = physDev->gl_drawable;
3538 else
3539 ret = physDev->drawable;
3540 return ret;
3543 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3545 wine_tsx11_lock();
3546 pglXDestroyGLXPixmap(display, glxpixmap);
3547 wine_tsx11_unlock();
3548 return TRUE;
3552 * X11DRV_SwapBuffers
3554 * Swap the buffers of this DC
3556 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
3558 GLXDrawable drawable;
3559 Wine_GLContext *ctx = NtCurrentTeb()->glContext;
3561 if (!has_opengl()) return FALSE;
3563 TRACE("(%p)\n", physDev);
3565 drawable = get_glxdrawable(physDev);
3567 wine_tsx11_lock();
3568 sync_context(ctx);
3569 if(physDev->pixmap) {
3570 if(pglXCopySubBufferMESA) {
3571 int w = physDev->dc_rect.right - physDev->dc_rect.left;
3572 int h = physDev->dc_rect.bottom - physDev->dc_rect.top;
3574 /* (glX)SwapBuffers has an implicit glFlush effect, however
3575 * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3576 * copying */
3577 pglFlush();
3578 if(w > 0 && h > 0)
3579 pglXCopySubBufferMESA(gdi_display, drawable, 0, 0, w, h);
3581 else
3582 pglXSwapBuffers(gdi_display, drawable);
3584 else
3585 pglXSwapBuffers(gdi_display, drawable);
3587 flush_gl_drawable(physDev);
3588 wine_tsx11_unlock();
3590 /* FPS support */
3591 if (TRACE_ON(fps))
3593 static long prev_time, start_time;
3594 static unsigned long frames, frames_total;
3596 DWORD time = GetTickCount();
3597 frames++;
3598 frames_total++;
3599 /* every 1.5 seconds */
3600 if (time - prev_time > 1500) {
3601 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
3602 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
3603 prev_time = time;
3604 frames = 0;
3605 if(start_time == 0) start_time = time;
3609 return TRUE;
3612 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3614 WineGLPixelFormat *fmt;
3615 XVisualInfo *ret;
3617 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
3618 if(fmt == NULL)
3619 return NULL;
3621 wine_tsx11_lock();
3622 ret = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
3623 wine_tsx11_unlock();
3624 return ret;
3627 #else /* no OpenGL includes */
3629 static inline void opengl_error(void)
3631 static int warned;
3632 if (!warned++) ERR("No OpenGL support compiled in.\n");
3635 int pixelformat_from_fbconfig_id(XID fbconfig_id)
3637 return 0;
3640 void mark_drawable_dirty(Drawable old, Drawable new)
3644 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
3648 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
3650 return 0;
3653 /***********************************************************************
3654 * ChoosePixelFormat (X11DRV.@)
3656 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
3657 const PIXELFORMATDESCRIPTOR *ppfd) {
3658 opengl_error();
3659 return 0;
3662 /***********************************************************************
3663 * DescribePixelFormat (X11DRV.@)
3665 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
3666 int iPixelFormat,
3667 UINT nBytes,
3668 PIXELFORMATDESCRIPTOR *ppfd) {
3669 opengl_error();
3670 return 0;
3673 /***********************************************************************
3674 * GetPixelFormat (X11DRV.@)
3676 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
3677 opengl_error();
3678 return 0;
3681 /***********************************************************************
3682 * SetPixelFormat (X11DRV.@)
3684 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
3685 int iPixelFormat,
3686 const PIXELFORMATDESCRIPTOR *ppfd) {
3687 opengl_error();
3688 return FALSE;
3691 /***********************************************************************
3692 * SwapBuffers (X11DRV.@)
3694 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
3695 opengl_error();
3696 return FALSE;
3700 * X11DRV_wglCopyContext
3702 * For OpenGL32 wglCopyContext.
3704 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
3705 opengl_error();
3706 return FALSE;
3710 * X11DRV_wglCreateContext
3712 * For OpenGL32 wglCreateContext.
3714 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
3715 opengl_error();
3716 return NULL;
3720 * X11DRV_wglDeleteContext
3722 * For OpenGL32 wglDeleteContext.
3724 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc) {
3725 opengl_error();
3726 return FALSE;
3730 * X11DRV_wglGetProcAddress
3732 * For OpenGL32 wglGetProcAddress.
3734 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
3735 opengl_error();
3736 return NULL;
3739 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, void *hPbuffer)
3741 opengl_error();
3742 return NULL;
3745 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
3746 opengl_error();
3747 return FALSE;
3751 * X11DRV_wglMakeCurrent
3753 * For OpenGL32 wglMakeCurrent.
3755 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
3756 opengl_error();
3757 return FALSE;
3761 * X11DRV_wglShareLists
3763 * For OpenGL32 wglShareLists.
3765 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
3766 opengl_error();
3767 return FALSE;
3771 * X11DRV_wglUseFontBitmapsA
3773 * For OpenGL32 wglUseFontBitmapsA.
3775 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3777 opengl_error();
3778 return FALSE;
3782 * X11DRV_wglUseFontBitmapsW
3784 * For OpenGL32 wglUseFontBitmapsW.
3786 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3788 opengl_error();
3789 return FALSE;
3793 * X11DRV_wglSetPixelFormatWINE
3795 * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3796 * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3798 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3800 opengl_error();
3801 return FALSE;
3804 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3806 return 0;
3809 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3811 return FALSE;
3814 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3816 return NULL;
3819 #endif /* defined(HAVE_OPENGL) */