winex11: Check for WGL drawing caps.
[wine/winequartzdrv.git] / dlls / winex11.drv / opengl.c
blobed109e1bc13e8c980a182bdbc7d2d1538d4f2c8d
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
5 * Copyright 2005 Alex Woods
6 * Copyright 2005 Raphael Junqueira
7 * Copyright 2006 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);
38 WINE_DECLARE_DEBUG_CHANNEL(opengl);
40 #ifdef SONAME_LIBGL
42 #undef APIENTRY
43 #undef CALLBACK
44 #undef WINAPI
46 #ifdef HAVE_GL_GL_H
47 # include <GL/gl.h>
48 #endif
49 #ifdef HAVE_GL_GLX_H
50 # include <GL/glx.h>
51 #endif
52 #ifdef HAVE_GL_GLEXT_H
53 # include <GL/glext.h>
54 #endif
56 #include "wine/wgl.h"
58 #undef APIENTRY
59 #undef CALLBACK
60 #undef WINAPI
62 /* Redefines the constants */
63 #define CALLBACK __stdcall
64 #define WINAPI __stdcall
65 #define APIENTRY WINAPI
68 WINE_DECLARE_DEBUG_CHANNEL(fps);
70 typedef struct wine_glextension {
71 const char *extName;
72 struct {
73 const char *funcName;
74 void *funcAddress;
75 } extEntryPoints[8];
76 } WineGLExtension;
78 struct WineGLInfo {
79 const char *glVersion;
80 const char *glExtensions;
82 int glxVersion[2];
84 const char *glxServerVersion;
85 const char *glxServerExtensions;
87 const char *glxClientVersion;
88 const char *glxClientExtensions;
90 const char *glxExtensions;
92 BOOL glxDirect;
93 char wglExtensions[4096];
96 typedef struct wine_glpixelformat {
97 int iPixelFormat;
98 GLXFBConfig fbconfig;
99 int fmt_id;
100 int render_type;
101 BOOL offscreenOnly;
102 } WineGLPixelFormat;
104 typedef struct wine_glcontext {
105 HDC hdc;
106 XVisualInfo *vis;
107 WineGLPixelFormat *fmt;
108 GLXContext ctx;
109 BOOL do_escape;
110 X11DRV_PDEVICE *physDev;
111 RECT viewport;
112 RECT scissor;
113 BOOL scissor_enabled;
114 struct wine_glcontext *next;
115 struct wine_glcontext *prev;
116 } Wine_GLContext;
118 typedef struct wine_glpbuffer {
119 Drawable drawable;
120 Display* display;
121 WineGLPixelFormat* fmt;
122 int width;
123 int height;
124 int* attribList;
125 HDC hdc;
127 int use_render_texture; /* This is also the internal texture format */
128 int texture_bind_target;
129 int texture_bpp;
130 GLint texture_format;
131 GLuint texture_target;
132 GLenum texture_type;
133 GLuint texture;
134 int texture_level;
135 } Wine_GLPBuffer;
137 static Wine_GLContext *context_list;
138 static struct WineGLInfo WineGLInfo = { 0 };
139 static int use_render_texture_emulation = 1;
140 static int use_render_texture_ati = 0;
141 static int swap_interval = 1;
143 #define MAX_EXTENSIONS 16
144 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
145 static int WineGLExtensionListSize;
147 static WineGLPixelFormat *WineGLPixelFormatList;
148 static int WineGLPixelFormatListSize = 0;
150 static void X11DRV_WineGL_LoadExtensions(void);
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 #undef TEST_AND_DUMP
173 TRACE("\n");
175 TRACE(" - iPixelType : ");
176 switch (ppfd->iPixelType) {
177 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
178 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
180 TRACE("\n");
182 TRACE(" - Color : %d\n", ppfd->cColorBits);
183 TRACE(" - Red : %d\n", ppfd->cRedBits);
184 TRACE(" - Green : %d\n", ppfd->cGreenBits);
185 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
186 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
187 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
188 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
189 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
190 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
192 TRACE(" - iLayerType : ");
193 switch (ppfd->iLayerType) {
194 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
195 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
196 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
198 TRACE("\n");
201 #define PUSH1(attribs,att) do { attribs[nAttribs++] = (att); } while (0)
202 #define PUSH2(attribs,att,value) do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
204 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
205 /* GLX 1.0 */
206 MAKE_FUNCPTR(glXChooseVisual)
207 MAKE_FUNCPTR(glXCreateContext)
208 MAKE_FUNCPTR(glXCreateGLXPixmap)
209 MAKE_FUNCPTR(glXGetCurrentContext)
210 MAKE_FUNCPTR(glXGetCurrentDrawable)
211 MAKE_FUNCPTR(glXDestroyContext)
212 MAKE_FUNCPTR(glXDestroyGLXPixmap)
213 MAKE_FUNCPTR(glXGetConfig)
214 MAKE_FUNCPTR(glXIsDirect)
215 MAKE_FUNCPTR(glXMakeCurrent)
216 MAKE_FUNCPTR(glXSwapBuffers)
217 MAKE_FUNCPTR(glXQueryExtension)
218 MAKE_FUNCPTR(glXQueryVersion)
219 MAKE_FUNCPTR(glXUseXFont)
221 /* GLX 1.1 */
222 MAKE_FUNCPTR(glXGetClientString)
223 MAKE_FUNCPTR(glXQueryExtensionsString)
224 MAKE_FUNCPTR(glXQueryServerString)
226 /* GLX 1.3 */
227 MAKE_FUNCPTR(glXGetFBConfigs)
228 MAKE_FUNCPTR(glXChooseFBConfig)
229 MAKE_FUNCPTR(glXCreatePbuffer)
230 MAKE_FUNCPTR(glXCreateNewContext)
231 MAKE_FUNCPTR(glXDestroyPbuffer)
232 MAKE_FUNCPTR(glXGetFBConfigAttrib)
233 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
234 MAKE_FUNCPTR(glXMakeContextCurrent)
235 MAKE_FUNCPTR(glXQueryDrawable)
236 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
238 /* GLX Extensions */
239 static void* (*pglXGetProcAddressARB)(const GLubyte *);
240 static BOOL (*pglXBindTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
241 static BOOL (*pglXReleaseTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
242 static BOOL (*pglXDrawableAttribARB)(Display *dpy, GLXDrawable draw, const int *attribList);
243 static int (*pglXSwapIntervalSGI)(int);
245 /* NV GLX Extension */
246 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
247 static void (*pglXFreeMemoryNV)(GLvoid *pointer);
249 /* Standard OpenGL */
250 MAKE_FUNCPTR(glBindTexture)
251 MAKE_FUNCPTR(glBitmap)
252 MAKE_FUNCPTR(glCopyTexSubImage1D)
253 MAKE_FUNCPTR(glCopyTexImage2D)
254 MAKE_FUNCPTR(glCopyTexSubImage2D)
255 MAKE_FUNCPTR(glDisable)
256 MAKE_FUNCPTR(glDrawBuffer)
257 MAKE_FUNCPTR(glEnable)
258 MAKE_FUNCPTR(glEndList)
259 MAKE_FUNCPTR(glGetError)
260 MAKE_FUNCPTR(glGetIntegerv)
261 MAKE_FUNCPTR(glGetString)
262 MAKE_FUNCPTR(glIsEnabled)
263 MAKE_FUNCPTR(glNewList)
264 MAKE_FUNCPTR(glPixelStorei)
265 MAKE_FUNCPTR(glReadPixels)
266 MAKE_FUNCPTR(glScissor)
267 MAKE_FUNCPTR(glTexImage2D)
268 MAKE_FUNCPTR(glViewport)
269 #undef MAKE_FUNCPTR
271 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
273 static BOOL infoInitialized = FALSE;
275 int screen = DefaultScreen(gdi_display);
276 Window win = RootWindow(gdi_display, screen);
277 Visual *visual;
278 XVisualInfo template;
279 XVisualInfo *vis;
280 int num;
281 GLXContext ctx = NULL;
283 if (infoInitialized)
284 return TRUE;
285 infoInitialized = TRUE;
287 wine_tsx11_lock();
289 visual = DefaultVisual(gdi_display, screen);
290 template.visualid = XVisualIDFromVisual(visual);
291 vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
292 if (vis) {
293 WORD old_fs = wine_get_fs();
294 /* Create a GLX Context. Without one we can't query GL information */
295 ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
296 if (wine_get_fs() != old_fs)
298 wine_set_fs( old_fs );
299 wine_tsx11_unlock();
300 ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
301 ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
302 return FALSE;
306 if (ctx) {
307 pglXMakeCurrent(gdi_display, win, ctx);
308 } else {
309 ERR(" couldn't initialize OpenGL, expect problems\n");
310 wine_tsx11_unlock();
311 return FALSE;
314 WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
315 WineGLInfo.glExtensions = strdup((const char *) pglGetString(GL_EXTENSIONS));
317 /* Get the common GLX version supported by GLX client and server ( major/minor) */
318 pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
320 WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
321 WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
323 WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
324 WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
326 WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
327 WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
329 TRACE("GL version : %s.\n", WineGLInfo.glVersion);
330 TRACE("GL renderer : %s.\n", pglGetString(GL_RENDERER));
331 TRACE("GLX version : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
332 TRACE("Server GLX version : %s.\n", WineGLInfo.glxServerVersion);
333 TRACE("Server GLX vendor: : %s.\n", pglXQueryServerString(gdi_display, screen, GLX_VENDOR));
334 TRACE("Client GLX version : %s.\n", WineGLInfo.glxClientVersion);
335 TRACE("Client GLX vendor: : %s.\n", pglXGetClientString(gdi_display, GLX_VENDOR));
336 TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
338 if(vis) XFree(vis);
339 if(ctx) {
340 pglXMakeCurrent(gdi_display, None, NULL);
341 pglXDestroyContext(gdi_display, ctx);
343 wine_tsx11_unlock();
344 return TRUE;
347 static BOOL has_opengl(void)
349 static int init_done;
350 static void *opengl_handle;
351 const char *glx_extensions;
353 int error_base, event_base;
355 if (init_done) return (opengl_handle != NULL);
356 init_done = 1;
358 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
359 and include all dependencies */
360 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
361 if (opengl_handle == NULL) return FALSE;
363 pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
364 if (pglXGetProcAddressARB == NULL) {
365 ERR("could not find glXGetProcAddressARB in libGL.\n");
366 return FALSE;
369 #define LOAD_FUNCPTR(f) if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) goto sym_not_found;
370 /* GLX 1.0 */
371 LOAD_FUNCPTR(glXChooseVisual)
372 LOAD_FUNCPTR(glXCreateContext)
373 LOAD_FUNCPTR(glXCreateGLXPixmap)
374 LOAD_FUNCPTR(glXGetCurrentContext)
375 LOAD_FUNCPTR(glXGetCurrentDrawable)
376 LOAD_FUNCPTR(glXDestroyContext)
377 LOAD_FUNCPTR(glXDestroyGLXPixmap)
378 LOAD_FUNCPTR(glXGetConfig)
379 LOAD_FUNCPTR(glXIsDirect)
380 LOAD_FUNCPTR(glXMakeCurrent)
381 LOAD_FUNCPTR(glXSwapBuffers)
382 LOAD_FUNCPTR(glXQueryExtension)
383 LOAD_FUNCPTR(glXQueryVersion)
384 LOAD_FUNCPTR(glXUseXFont)
386 /* GLX 1.1 */
387 LOAD_FUNCPTR(glXGetClientString)
388 LOAD_FUNCPTR(glXQueryExtensionsString)
389 LOAD_FUNCPTR(glXQueryServerString)
391 /* GLX 1.3 */
392 LOAD_FUNCPTR(glXCreatePbuffer)
393 LOAD_FUNCPTR(glXCreateNewContext)
394 LOAD_FUNCPTR(glXDestroyPbuffer)
395 LOAD_FUNCPTR(glXMakeContextCurrent)
396 LOAD_FUNCPTR(glXGetCurrentReadDrawable)
397 LOAD_FUNCPTR(glXGetFBConfigs)
399 /* Standard OpenGL calls */
400 LOAD_FUNCPTR(glBindTexture)
401 LOAD_FUNCPTR(glBitmap)
402 LOAD_FUNCPTR(glCopyTexSubImage1D)
403 LOAD_FUNCPTR(glCopyTexImage2D)
404 LOAD_FUNCPTR(glCopyTexSubImage2D)
405 LOAD_FUNCPTR(glDisable)
406 LOAD_FUNCPTR(glDrawBuffer)
407 LOAD_FUNCPTR(glEnable)
408 LOAD_FUNCPTR(glEndList)
409 LOAD_FUNCPTR(glGetError)
410 LOAD_FUNCPTR(glGetIntegerv)
411 LOAD_FUNCPTR(glGetString)
412 LOAD_FUNCPTR(glIsEnabled)
413 LOAD_FUNCPTR(glNewList)
414 LOAD_FUNCPTR(glPixelStorei)
415 LOAD_FUNCPTR(glReadPixels)
416 LOAD_FUNCPTR(glScissor)
417 LOAD_FUNCPTR(glTexImage2D)
418 LOAD_FUNCPTR(glViewport)
419 #undef LOAD_FUNCPTR
421 /* It doesn't matter if these fail. They'll only be used if the driver reports
422 the associated extension is available (and if a driver reports the extension
423 is available but fails to provide the functions, it's quite broken) */
424 #define LOAD_FUNCPTR(f) p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f);
425 /* NV GLX Extension */
426 LOAD_FUNCPTR(glXAllocateMemoryNV)
427 LOAD_FUNCPTR(glXFreeMemoryNV)
428 #undef LOAD_FUNCPTR
430 if(!X11DRV_WineGL_InitOpenglInfo()) {
431 wine_dlclose(opengl_handle, NULL, 0);
432 opengl_handle = NULL;
433 return FALSE;
436 wine_tsx11_lock();
437 if (pglXQueryExtension(gdi_display, &error_base, &event_base) == True) {
438 TRACE("GLX is up and running error_base = %d\n", error_base);
439 } else {
440 wine_dlclose(opengl_handle, NULL, 0);
441 opengl_handle = NULL;
444 /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
445 * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
446 * rendering is used.
448 * The main problem for our OpenGL code is that we need certain GLX calls but their presence
449 * depends on the reported GLX client / server version and on the client / server extension list.
450 * Those don't have to be the same.
452 * In general the server GLX information should be used in case of indirect rendering. When direct
453 * rendering is used, the OpenGL client library is responsible for which GLX calls are available.
454 * Nvidia's OpenGL drivers are the best in terms of GLX features. At the moment of writing their
455 * 8762 drivers support 1.3 for the server and 1.4 for the client and they support lots of extensions.
456 * Unfortunately it is much more complicated for Mesa/DRI-based drivers and ATI's drivers.
457 * Both sets of drivers report a server version of 1.2 and the client version can be 1.3 or 1.4.
458 * Further, in case of at least ATI's drivers, one crucial extension needed for our pixel format code
459 * is only available in the list of server extensions and not in the client list.
461 * The versioning checks below try to take into account the comments from above.
464 /* Depending on the use of direct or indirect rendering we need either the list of extensions
465 * exported by the client or by the server.
467 if(WineGLInfo.glxDirect)
468 glx_extensions = WineGLInfo.glxClientExtensions;
469 else
470 glx_extensions = WineGLInfo.glxServerExtensions;
472 /* Based on the default opengl context we decide whether direct or indirect rendering is used.
473 * In case of indirect rendering we check if the GLX version of the server is 1.2 and else
474 * the client version is checked.
476 if ((!WineGLInfo.glxDirect && !strcmp("1.2", WineGLInfo.glxServerVersion)) ||
477 (WineGLInfo.glxDirect && !strcmp("1.2", WineGLInfo.glxClientVersion)))
479 if (NULL != strstr(glx_extensions, "GLX_SGIX_fbconfig")) {
480 pglXChooseFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
481 pglXGetFBConfigAttrib = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
482 pglXGetVisualFromFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
483 } else {
484 ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxClientVersion);
486 } else {
487 pglXChooseFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
488 pglXGetFBConfigAttrib = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
489 pglXGetVisualFromFBConfig = (void*)pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
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 if (!strcmp("1.2", WineGLInfo.glxServerVersion))
496 pglXQueryDrawable = NULL;
497 else
498 pglXQueryDrawable = wine_dlsym(RTLD_DEFAULT, "glXQueryDrawable", NULL, 0);
500 if (NULL != strstr(glx_extensions, "GLX_ATI_render_texture")) {
501 pglXBindTexImageARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageARB");
502 pglXReleaseTexImageARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageARB");
503 pglXDrawableAttribARB = (void*)pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribARB");
506 X11DRV_WineGL_LoadExtensions();
508 wine_tsx11_unlock();
509 return (opengl_handle != NULL);
511 sym_not_found:
512 wine_dlclose(opengl_handle, NULL, 0);
513 opengl_handle = NULL;
514 return FALSE;
517 static inline Wine_GLContext *alloc_context(void)
519 Wine_GLContext *ret;
521 if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
523 ret->next = context_list;
524 if (context_list) context_list->prev = ret;
525 context_list = ret;
527 return ret;
530 static inline void free_context(Wine_GLContext *context)
532 if (context->next != NULL) context->next->prev = context->prev;
533 if (context->prev != NULL) context->prev->next = context->next;
534 else context_list = context->next;
536 HeapFree(GetProcessHeap(), 0, context);
539 static inline Wine_GLContext *get_context_from_GLXContext(GLXContext ctx)
541 Wine_GLContext *ret;
542 if (!ctx) return NULL;
543 for (ret = context_list; ret; ret = ret->next) if (ctx == ret->ctx) break;
544 return ret;
548 * get_hdc_from_Drawable (internal)
550 * For use by wglGetCurrentReadDCARB.
552 static inline HDC get_hdc_from_Drawable(GLXDrawable d)
554 Wine_GLContext *ret;
555 for (ret = context_list; ret; ret = ret->next) {
556 if (d == ret->physDev->drawable) {
557 return ret->hdc;
560 return NULL;
563 static inline BOOL is_valid_context( Wine_GLContext *ctx )
565 Wine_GLContext *ptr;
566 for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
567 return (ptr != NULL);
570 static int describeContext(Wine_GLContext* ctx) {
571 int tmp;
572 int ctx_vis_id;
573 TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
574 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
575 TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
576 pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
577 TRACE(" - VISUAL_ID 0x%x\n", tmp);
578 ctx_vis_id = tmp;
579 return ctx_vis_id;
582 static int describeDrawable(Wine_GLContext* ctx, Drawable drawable) {
583 int tmp;
584 int nElements;
585 int attribList[3] = { GLX_FBCONFIG_ID, 0, None };
586 GLXFBConfig *fbCfgs;
588 if (pglXQueryDrawable == NULL) {
589 /** glXQueryDrawable not available so returns not supported */
590 return -1;
593 TRACE(" Drawable %p have :\n", (void*) drawable);
594 pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &tmp);
595 TRACE(" - WIDTH as %d\n", tmp);
596 pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &tmp);
597 TRACE(" - HEIGHT as %d\n", tmp);
598 pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &tmp);
599 TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
601 attribList[1] = tmp;
602 fbCfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribList, &nElements);
603 if (fbCfgs == NULL) {
604 return -1;
607 pglXGetFBConfigAttrib(gdi_display, fbCfgs[0], GLX_VISUAL_ID, &tmp);
608 TRACE(" - VISUAL_ID as 0x%x\n", tmp);
610 XFree(fbCfgs);
612 return tmp;
615 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
616 int nAttribs = 0;
617 unsigned cur = 0;
618 int pop;
619 int drawattrib = 0;
620 int isColor = 0;
621 int wantColorBits = 0;
622 int sz_alpha = 0;
624 /* The list of WGL attributes is allowed to be NULL, so don't return -1 (error) but just 0 */
625 if(iWGLAttr == NULL)
626 return 0;
628 while (0 != iWGLAttr[cur]) {
629 TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
631 switch (iWGLAttr[cur]) {
632 case WGL_COLOR_BITS_ARB:
633 pop = iWGLAttr[++cur];
634 wantColorBits = pop; /** see end */
635 break;
636 case WGL_BLUE_BITS_ARB:
637 pop = iWGLAttr[++cur];
638 PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
639 TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
640 break;
641 case WGL_RED_BITS_ARB:
642 pop = iWGLAttr[++cur];
643 PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
644 TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
645 break;
646 case WGL_GREEN_BITS_ARB:
647 pop = iWGLAttr[++cur];
648 PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
649 TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
650 break;
651 case WGL_ALPHA_BITS_ARB:
652 pop = iWGLAttr[++cur];
653 sz_alpha = pop;
654 PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
655 TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
656 break;
657 case WGL_DEPTH_BITS_ARB:
658 pop = iWGLAttr[++cur];
659 PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
660 TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
661 break;
662 case WGL_STENCIL_BITS_ARB:
663 pop = iWGLAttr[++cur];
664 PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
665 TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
666 break;
667 case WGL_DOUBLE_BUFFER_ARB:
668 pop = iWGLAttr[++cur];
669 PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
670 TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
671 break;
673 case WGL_PIXEL_TYPE_ARB:
674 pop = iWGLAttr[++cur];
675 switch (pop) {
676 case WGL_TYPE_COLORINDEX_ARB: pop = GLX_COLOR_INDEX_BIT; isColor = 1; break ;
677 case WGL_TYPE_RGBA_ARB: pop = GLX_RGBA_BIT; break ;
678 /* 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 */
679 case WGL_TYPE_RGBA_FLOAT_ATI: pop = GLX_RGBA_FLOAT_BIT; break ;
680 default:
681 ERR("unexpected PixelType(%x)\n", pop);
682 pop = 0;
684 PUSH2(oGLXAttr, GLX_RENDER_TYPE, pop);
685 TRACE("pAttr[%d] = GLX_RENDER_TYPE: %d\n", cur, pop);
686 break;
688 case WGL_SUPPORT_GDI_ARB:
689 pop = iWGLAttr[++cur];
690 PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
691 TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
692 break;
694 case WGL_DRAW_TO_BITMAP_ARB:
695 pop = iWGLAttr[++cur];
696 TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
697 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
698 if (pop) {
699 drawattrib |= GLX_PIXMAP_BIT;
701 break;
703 case WGL_DRAW_TO_WINDOW_ARB:
704 pop = iWGLAttr[++cur];
705 TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
706 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
707 if (pop) {
708 drawattrib |= GLX_WINDOW_BIT;
710 break;
712 case WGL_DRAW_TO_PBUFFER_ARB:
713 pop = iWGLAttr[++cur];
714 TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
715 /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
716 if (pop) {
717 drawattrib |= GLX_PBUFFER_BIT;
719 break;
721 case WGL_ACCELERATION_ARB:
722 case WGL_SUPPORT_OPENGL_ARB:
723 pop = iWGLAttr[++cur];
724 /** nothing to do, if we are here, supposing support Accelerated OpenGL */
725 TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
726 break;
728 case WGL_PBUFFER_LARGEST_ARB:
729 pop = iWGLAttr[++cur];
730 PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
731 TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
732 break;
734 case WGL_SAMPLE_BUFFERS_ARB:
735 pop = iWGLAttr[++cur];
736 PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
737 TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
738 break;
740 case WGL_SAMPLES_ARB:
741 pop = iWGLAttr[++cur];
742 PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
743 TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
744 break;
746 case WGL_TEXTURE_FORMAT_ARB:
747 case WGL_TEXTURE_TARGET_ARB:
748 case WGL_MIPMAP_TEXTURE_ARB:
749 TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
750 pop = iWGLAttr[++cur];
751 if (NULL == pbuf) {
752 ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
754 if (use_render_texture_ati) {
755 /** nothing to do here */
757 else if (!use_render_texture_emulation) {
758 if (WGL_NO_TEXTURE_ARB != pop) {
759 ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
760 return -1; /** error: don't support it */
761 } else {
762 PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
763 drawattrib |= GLX_PBUFFER_BIT;
766 break ;
767 case WGL_FLOAT_COMPONENTS_NV:
768 pop = iWGLAttr[++cur];
769 PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, pop);
770 TRACE("pAttr[%d] = GLX_FLOAT_COMPONENTS_NV: %x\n", cur, pop);
771 break ;
772 case WGL_BIND_TO_TEXTURE_RGB_ARB:
773 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
774 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
775 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
776 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
777 case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
778 pop = iWGLAttr[++cur];
779 /** cannot be converted, see direct handling on
780 * - wglGetPixelFormatAttribivARB
781 * TODO: wglChoosePixelFormat
783 break ;
785 default:
786 FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
787 break;
789 ++cur;
793 * Trick as WGL_COLOR_BITS_ARB != GLX_BUFFER_SIZE
794 * WGL_COLOR_BITS_ARB + WGL_ALPHA_BITS_ARB == GLX_BUFFER_SIZE
796 * WGL_COLOR_BITS_ARB
797 * The number of color bitplanes in each color buffer. For RGBA
798 * pixel types, it is the size of the color buffer, excluding the
799 * alpha bitplanes. For color-index pixels, it is the size of the
800 * color index buffer.
802 * GLX_BUFFER_SIZE
803 * This attribute defines the number of bits per color buffer.
804 * For GLX FBConfigs that correspond to a PseudoColor or StaticColor visual,
805 * this is equal to the depth value reported in the X11 visual.
806 * For GLX FBConfigs that correspond to TrueColor or DirectColor visual,
807 * this is the sum of GLX_RED_SIZE, GLX_GREEN_SIZE, GLX_BLUE_SIZE, and GLX_ALPHA_SIZE.
810 if (0 < wantColorBits) {
811 if (!isColor) {
812 wantColorBits += sz_alpha;
814 if (32 < wantColorBits) {
815 ERR("buggy %d GLX_BUFFER_SIZE default to 32\n", wantColorBits);
816 wantColorBits = 32;
818 PUSH2(oGLXAttr, GLX_BUFFER_SIZE, wantColorBits);
819 TRACE("pAttr[%d] = WGL_COLOR_BITS_ARB: %d\n", cur, wantColorBits);
822 /* Apply the OR'd drawable type bitmask now. */
823 if (drawattrib) {
824 PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
825 TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
828 return nAttribs;
831 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
833 int render_type=0, render_type_bit;
834 pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
835 switch(render_type_bit)
837 case GLX_RGBA_BIT:
838 render_type = GLX_RGBA_TYPE;
839 break;
840 case GLX_COLOR_INDEX_BIT:
841 render_type = GLX_COLOR_INDEX_TYPE;
842 break;
843 case GLX_RGBA_FLOAT_BIT:
844 render_type = GLX_RGBA_FLOAT_TYPE;
845 break;
846 default:
847 ERR("Unknown render_type: %x\n", render_type);
849 return render_type;
852 static BOOL get_fbconfig_from_visualid(Display *display, Visual *visual, GLXFBConfig *fbconfig, int *fmt_id)
854 GLXFBConfig* cfgs = NULL;
855 int i;
856 int nCfgs;
857 int tmp_fmt_id;
858 int tmp_vis_id;
859 VisualID visualid;
861 if(!display || !visual) {
862 ERR("Invalid display or visual\n");
863 return FALSE;
865 visualid = XVisualIDFromVisual(visual);
867 /* Get a list of all available framebuffer configurations */
868 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
869 if (NULL == cfgs || 0 == nCfgs) {
870 ERR("glXChooseFBConfig returns NULL\n");
871 if(cfgs != NULL) XFree(cfgs);
872 return FALSE;
875 /* Find the requested offscreen format and count the number of offscreen formats */
876 for(i=0; i<nCfgs; i++) {
877 pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
878 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &tmp_fmt_id);
880 /* We are looking up the GLX index of our main visual and have found it :) */
881 if(visualid == tmp_vis_id) {
882 TRACE("Found FBCONFIG_ID 0x%x at index %d for VISUAL_ID 0x%x\n", tmp_fmt_id, i, tmp_vis_id);
883 *fbconfig = cfgs[i];
884 *fmt_id = tmp_fmt_id;
885 XFree(cfgs);
886 return TRUE;
890 ERR("No fbconfig found for Wine's main visual (0x%lx), expect problems!\n", visualid);
891 XFree(cfgs);
892 return FALSE;
895 static BOOL init_formats(Display *display, int screen, Visual *visual)
897 int fmt_id, tmp_vis_id, tmp_fmt_id, nCfgs, i;
898 GLXFBConfig* cfgs;
899 GLXFBConfig fbconfig;
900 int nOffscreenFormats = 0;
902 /* Locate the fbconfig correspondig to our main visual */
903 if(!get_fbconfig_from_visualid(display, visual, &fbconfig, &fmt_id)) {
904 ERR("Can't get the FBCONFIG_ID for the main visual, expect problems!\n");
905 return FALSE;
908 /* As mentioned in various parts of the code only the format of the main visual can be used for onscreen rendering.
909 * Next to this format there are also so called offscreen rendering formats (used for pbuffers) which can be supported
910 * because they don't need a visual. Below we use glXGetFBConfigs instead of glXChooseFBConfig to enumerate the fb configurations
911 * because this call lists both types of formats instead of only onscreen ones. */
912 cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
913 if (NULL == cfgs || 0 == nCfgs) {
914 WARN("glXChooseFBConfig returns NULL\n");
915 if(cfgs != NULL) XFree(cfgs);
917 /* We only have the single format of Wine's main visual */
918 WineGLPixelFormatList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WineGLPixelFormat));
919 WineGLPixelFormatList[0].iPixelFormat = 1;
920 WineGLPixelFormatList[0].fbconfig = fbconfig;
921 WineGLPixelFormatList[0].fmt_id = fmt_id;
922 WineGLPixelFormatList[0].render_type = get_render_type_from_fbconfig(display, fbconfig);
923 WineGLPixelFormatList[0].offscreenOnly = FALSE;
924 WineGLPixelFormatListSize = 1;
926 else {
927 /* Count the number of offscreen formats to determine the size for our pixelformat list */
928 for(i=0; i<nCfgs; i++) {
929 pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
931 /* We have found an offscreen rendering format :) */
932 if(tmp_vis_id == 0) {
933 nOffscreenFormats++;
936 TRACE("Number of offscreen formats: %d\n", nOffscreenFormats);
938 /* Allocate memory for all the offscreen pixelformats and the format of Wine's main visual */
939 WineGLPixelFormatList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (1+nOffscreenFormats)*sizeof(WineGLPixelFormat));
940 WineGLPixelFormatList[0].iPixelFormat = 1;
941 WineGLPixelFormatList[0].fbconfig = fbconfig;
942 WineGLPixelFormatList[0].fmt_id = fmt_id;
943 WineGLPixelFormatList[0].render_type = get_render_type_from_fbconfig(display, fbconfig);
944 WineGLPixelFormatList[0].offscreenOnly = FALSE;
945 WineGLPixelFormatListSize = 1;
947 /* Fill the list with offscreen formats */
948 for(i=0; i<nCfgs; i++) {
949 pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
950 pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &tmp_fmt_id);
952 /* We have found an offscreen rendering format :) */
953 if(tmp_vis_id == 0) {
954 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", tmp_fmt_id, WineGLPixelFormatListSize+1, i);
955 WineGLPixelFormatList[WineGLPixelFormatListSize].iPixelFormat = WineGLPixelFormatListSize+1; /* The index starts at 1 */
956 WineGLPixelFormatList[WineGLPixelFormatListSize].fbconfig = cfgs[i];
957 WineGLPixelFormatList[WineGLPixelFormatListSize].fmt_id = tmp_fmt_id;
958 WineGLPixelFormatList[WineGLPixelFormatListSize].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
959 WineGLPixelFormatList[WineGLPixelFormatListSize].offscreenOnly = TRUE;
960 WineGLPixelFormatListSize++;
965 if(cfgs != NULL) XFree(cfgs);
967 return TRUE;
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 *res = NULL;
980 /* Init the list of pixel formats when we need it */
981 if(!WineGLPixelFormatListSize)
982 init_formats(display, DefaultScreen(display), visual);
984 /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
985 * iPixelFormat in case of probing the number of pixelformats.
987 if((iPixelFormat <= 0) || (iPixelFormat > WineGLPixelFormatListSize)) {
988 if(AllowOffscreen)
989 *fmt_count = WineGLPixelFormatListSize;
990 else
991 *fmt_count = 1; /* Only show the format of our main visual */
993 else if(iPixelFormat == 1) {
994 res = &WineGLPixelFormatList[0];
995 *fmt_count = 1; /* Only show the format of our main visual */
997 else if((WineGLPixelFormatList[iPixelFormat-1].offscreenOnly == TRUE) && AllowOffscreen) {
998 res = &WineGLPixelFormatList[iPixelFormat-1];
999 *fmt_count = WineGLPixelFormatListSize;
1000 TRACE("Returning FBConfig=%p for iPixelFormat=%d\n", res->fbconfig, iPixelFormat);
1003 TRACE("Number of returned pixelformats=%d\n", *fmt_count);
1005 return res;
1008 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
1009 static int ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id)
1011 int i;
1013 /* Init the list of pixel formats when we need it */
1014 if(!WineGLPixelFormatListSize)
1015 init_formats(display, DefaultScreen(display), visual);
1017 for(i=0; i<WineGLPixelFormatListSize; i++) {
1018 if(WineGLPixelFormatList[i].fmt_id == fmt_id) {
1019 TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", WineGLPixelFormatList[i].iPixelFormat, fmt_id);
1020 return WineGLPixelFormatList[i].iPixelFormat;
1023 TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1024 return 0;
1028 * X11DRV_ChoosePixelFormat
1030 * Equivalent to glXChooseVisual.
1032 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
1033 const PIXELFORMATDESCRIPTOR *ppfd) {
1034 WineGLPixelFormat *fmt;
1035 int ret = 0;
1036 int value = 0;
1038 if (!has_opengl()) {
1039 ERR("No libGL on this box - disabling OpenGL support !\n");
1040 return 0;
1043 if (TRACE_ON(opengl)) {
1044 TRACE("(%p,%p)\n", physDev, ppfd);
1046 dump_PIXELFORMATDESCRIPTOR((const PIXELFORMATDESCRIPTOR *) ppfd);
1049 wine_tsx11_lock();
1050 if(!visual) {
1051 ERR("Can't get an opengl visual!\n");
1052 goto choose_exit;
1055 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, 1 /* main visual */, FALSE /* offscreen */, &value);
1056 /* In case an fbconfig was found, check if it matches to the requirements of the ppfd */
1057 if(!fmt) {
1058 ERR("Can't find a matching FBCONFIG_ID for VISUAL_ID 0x%lx!\n", visual->visualid);
1059 } else {
1060 int dwFlags = 0;
1061 int iPixelType = 0;
1062 int value = 0;
1064 /* Pixel type */
1065 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1066 if (value & GLX_RGBA_BIT)
1067 iPixelType = PFD_TYPE_RGBA;
1068 else
1069 iPixelType = PFD_TYPE_COLORINDEX;
1071 if (ppfd->iPixelType != iPixelType) {
1072 TRACE("pixel type mismatch\n");
1073 goto choose_exit;
1076 /* Doublebuffer */
1077 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value); if (value) dwFlags |= PFD_DOUBLEBUFFER;
1078 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) && (ppfd->dwFlags & PFD_DOUBLEBUFFER)) {
1079 if (!(dwFlags & PFD_DOUBLEBUFFER)) {
1080 TRACE("dbl buffer mismatch\n");
1081 goto choose_exit;
1085 /* Stereo */
1086 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) dwFlags |= PFD_STEREO;
1087 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE) && (ppfd->dwFlags & PFD_STEREO)) {
1088 if (!(dwFlags & PFD_STEREO)) {
1089 TRACE("stereo mismatch\n");
1090 goto choose_exit;
1094 /* Alpha bits */
1095 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &value);
1096 if (ppfd->iPixelType==PFD_TYPE_RGBA && ppfd->cAlphaBits && !value) {
1097 TRACE("alpha mismatch\n");
1098 goto choose_exit;
1101 /* Depth bits */
1102 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1103 if (ppfd->cDepthBits && !value) {
1104 TRACE("depth mismatch\n");
1105 goto choose_exit;
1108 /* Stencil bits */
1109 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1110 if (ppfd->cStencilBits && !value) {
1111 TRACE("stencil mismatch\n");
1112 goto choose_exit;
1115 /* Aux buffers */
1116 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1117 if (ppfd->cAuxBuffers && !value) {
1118 TRACE("aux mismatch\n");
1119 goto choose_exit;
1122 /* When we pass all the checks we have found a matching format :) */
1123 ret = 1;
1124 TRACE("Successfully found a matching mode, returning index: %d\n", ret);
1127 choose_exit:
1128 if(!ret)
1129 TRACE("No matching mode was found returning 0\n");
1131 wine_tsx11_unlock();
1132 return ret;
1136 * X11DRV_DescribePixelFormat
1138 * Get the pixel-format descriptor associated to the given id
1140 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
1141 int iPixelFormat,
1142 UINT nBytes,
1143 PIXELFORMATDESCRIPTOR *ppfd) {
1144 /*XVisualInfo *vis;*/
1145 int value;
1146 int rb,gb,bb,ab;
1147 WineGLPixelFormat *fmt;
1148 int ret = 0;
1149 int fmt_count = 0;
1151 if (!has_opengl()) {
1152 ERR("No libGL on this box - disabling OpenGL support !\n");
1153 return 0;
1156 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
1158 /* 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 */
1159 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1160 if (ppfd == NULL) {
1161 /* The application is only querying the number of pixelformats */
1162 return fmt_count;
1163 } else if(fmt == NULL) {
1164 WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1165 return 0;
1168 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1169 ERR("Wrong structure size !\n");
1170 /* Should set error */
1171 return 0;
1174 ret = fmt_count;
1176 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1177 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1178 ppfd->nVersion = 1;
1180 /* These flags are always the same... */
1181 ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
1182 /* Now the flags extracted from the Visual */
1184 wine_tsx11_lock();
1186 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_CONFIG_CAVEAT, &value);
1187 if(value == GLX_SLOW_CONFIG)
1188 ppfd->dwFlags |= PFD_GENERIC_ACCELERATED;
1190 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1191 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1193 /* Pixel type */
1194 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1195 if (value & GLX_RGBA_BIT)
1196 ppfd->iPixelType = PFD_TYPE_RGBA;
1197 else
1198 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1200 /* Color bits */
1201 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1202 ppfd->cColorBits = value;
1204 /* Red, green, blue and alpha bits / shifts */
1205 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1206 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1207 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1208 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1209 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1211 ppfd->cRedBits = rb;
1212 ppfd->cRedShift = gb + bb + ab;
1213 ppfd->cBlueBits = bb;
1214 ppfd->cBlueShift = ab;
1215 ppfd->cGreenBits = gb;
1216 ppfd->cGreenShift = bb + ab;
1217 ppfd->cAlphaBits = ab;
1218 ppfd->cAlphaShift = 0;
1219 } else {
1220 ppfd->cRedBits = 0;
1221 ppfd->cRedShift = 0;
1222 ppfd->cBlueBits = 0;
1223 ppfd->cBlueShift = 0;
1224 ppfd->cGreenBits = 0;
1225 ppfd->cGreenShift = 0;
1226 ppfd->cAlphaBits = 0;
1227 ppfd->cAlphaShift = 0;
1229 /* Accums : to do ... */
1231 /* Depth bits */
1232 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1233 ppfd->cDepthBits = value;
1235 /* stencil bits */
1236 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1237 ppfd->cStencilBits = value;
1239 wine_tsx11_unlock();
1241 /* Aux : to do ... */
1243 ppfd->iLayerType = PFD_MAIN_PLANE;
1245 if (TRACE_ON(opengl)) {
1246 dump_PIXELFORMATDESCRIPTOR(ppfd);
1249 return ret;
1253 * X11DRV_GetPixelFormat
1255 * Get the pixel-format id used by this DC
1257 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1258 WineGLPixelFormat *fmt;
1259 int tmp;
1260 TRACE("(%p)\n", physDev);
1262 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE, &tmp);
1263 if(!fmt)
1265 /* This happens on HDCs on which SetPixelFormat wasn't called yet */
1266 ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physDev->current_pf);
1267 return 0;
1269 else if(fmt->offscreenOnly)
1271 /* Offscreen formats can't be used with traditional WGL calls.
1272 * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1273 TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1274 return 1;
1277 return physDev->current_pf;
1278 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1282 * X11DRV_SetPixelFormat
1284 * Set the pixel-format id used by this DC
1286 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1287 int iPixelFormat,
1288 const PIXELFORMATDESCRIPTOR *ppfd) {
1289 WineGLPixelFormat *fmt;
1290 int value;
1292 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1294 if (!has_opengl()) {
1295 ERR("No libGL on this box - disabling OpenGL support !\n");
1296 return 0;
1299 /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1300 if(get_glxdrawable(physDev) == root_window)
1302 ERR("Invalid operation on root_window\n");
1303 return 0;
1306 /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1307 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1308 if(!fmt) {
1309 ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1310 return 0;
1313 physDev->current_pf = iPixelFormat;
1315 if (TRACE_ON(opengl)) {
1316 int gl_test = 0;
1319 * How to test if hdc current drawable is compatible (visual/FBConfig) ?
1321 * in case of root window created HDCs we crash here :(
1323 Drawable drawable = get_drawable( physDev->hdc );
1324 TRACE(" drawable (%p,%p) have :\n", drawable, root_window);
1325 pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &value);
1326 TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
1327 pglXQueryDrawable(gdi_display, drawable, GLX_VISUAL_ID, (unsigned int*) &value);
1328 TRACE(" - VISUAL_ID as 0x%x\n", tmp);
1329 pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &value);
1330 TRACE(" - WIDTH as %d\n", tmp);
1331 pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &value);
1332 TRACE(" - HEIGHT as %d\n", tmp);
1334 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1335 if (gl_test) {
1336 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1337 } else {
1338 TRACE(" FBConfig have :\n");
1339 TRACE(" - FBCONFIG_ID 0x%x\n", value);
1340 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1341 TRACE(" - VISUAL_ID 0x%x\n", value);
1342 pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1343 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1346 return TRUE;
1350 * X11DRV_wglCreateContext
1352 * For OpenGL32 wglCreateContext.
1354 HGLRC X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1356 Wine_GLContext *ret;
1357 WineGLPixelFormat *fmt;
1358 int hdcPF = physDev->current_pf;
1359 int fmt_count = 0;
1360 int value = 0;
1361 int gl_test = 0;
1362 HDC hdc = physDev->hdc;
1364 TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1366 if (!has_opengl()) {
1367 ERR("No libGL on this box - disabling OpenGL support !\n");
1368 return 0;
1371 /* First, get the visual in use by the X11DRV */
1372 if (!gdi_display) return 0;
1374 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
1375 /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1376 * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1377 * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1378 * If this fails something is very wrong on the system. */
1379 if(!fmt) {
1380 ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
1381 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1382 return NULL;
1385 if (fmt_count < hdcPF) {
1386 ERR("(%p): unexpected pixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, hdcPF, fmt_count);
1387 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1388 return NULL;
1391 gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1392 if (gl_test) {
1393 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1394 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1395 return NULL;
1398 /* The context will be allocated in the wglMakeCurrent call */
1399 wine_tsx11_lock();
1400 ret = alloc_context();
1401 wine_tsx11_unlock();
1402 ret->hdc = hdc;
1403 ret->physDev = physDev;
1404 ret->fmt = fmt;
1406 /*ret->vis = vis;*/
1407 ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1409 TRACE(" creating context %p (GL context creation delayed)\n", ret);
1410 return (HGLRC) ret;
1414 * X11DRV_wglDeleteContext
1416 * For OpenGL32 wglDeleteContext.
1418 BOOL X11DRV_wglDeleteContext(HGLRC hglrc)
1420 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1421 BOOL ret = TRUE;
1423 TRACE("(%p)\n", hglrc);
1425 if (!has_opengl()) {
1426 ERR("No libGL on this box - disabling OpenGL support !\n");
1427 return 0;
1430 wine_tsx11_lock();
1431 /* A game (Half Life not to name it) deletes twice the same context,
1432 * so make sure it is valid first */
1433 if (is_valid_context( ctx ))
1435 if (ctx->ctx) pglXDestroyContext(gdi_display, ctx->ctx);
1436 free_context(ctx);
1438 else
1440 WARN("Error deleting context !\n");
1441 SetLastError(ERROR_INVALID_HANDLE);
1442 ret = FALSE;
1444 wine_tsx11_unlock();
1446 return ret;
1450 * X11DRV_wglGetCurrentReadDCARB
1452 * For OpenGL32 wglGetCurrentReadDCARB.
1454 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void)
1456 GLXDrawable gl_d;
1457 HDC ret;
1459 TRACE("()\n");
1461 wine_tsx11_lock();
1462 gl_d = pglXGetCurrentReadDrawable();
1463 ret = get_hdc_from_Drawable(gl_d);
1464 wine_tsx11_unlock();
1466 TRACE(" returning %p (GL drawable %lu)\n", ret, gl_d);
1467 return ret;
1471 * X11DRV_wglGetProcAddress
1473 * For OpenGL32 wglGetProcAddress.
1475 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1477 int i, j;
1478 const WineGLExtension *ext;
1480 int padding = 32 - strlen(lpszProc);
1481 if (padding < 0)
1482 padding = 0;
1484 if (!has_opengl()) {
1485 ERR("No libGL on this box - disabling OpenGL support !\n");
1486 return 0;
1489 /* Check the table of WGL extensions to see if we need to return a WGL extension
1490 * or a function pointer to a native OpenGL function. */
1491 if(strncmp(lpszProc, "wgl", 3) != 0) {
1492 return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1493 } else {
1494 TRACE("('%s'):%*s", lpszProc, padding, " ");
1495 for (i = 0; i < WineGLExtensionListSize; ++i) {
1496 ext = WineGLExtensionList[i];
1497 for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1498 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1499 TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1500 return ext->extEntryPoints[j].funcAddress;
1506 ERR("(%s) - not found\n", lpszProc);
1507 return NULL;
1510 /***********************************************************************
1511 * sync_current_drawable
1513 * Adjust the current viewport and scissor in order to position
1514 * and size the current drawable correctly on the parent window.
1516 static void sync_current_drawable(BOOL updatedc)
1518 int dy;
1519 int width;
1520 int height;
1521 RECT rc;
1522 Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1524 TRACE("\n");
1526 if (ctx && ctx->physDev)
1528 if (updatedc)
1529 GetClipBox(ctx->physDev->hdc, &rc); /* Make sure physDev is up to date */
1531 dy = ctx->physDev->drawable_rect.bottom - ctx->physDev->drawable_rect.top -
1532 ctx->physDev->dc_rect.bottom;
1533 width = ctx->physDev->dc_rect.right - ctx->physDev->dc_rect.left;
1534 height = ctx->physDev->dc_rect.bottom - ctx->physDev->dc_rect.top;
1536 wine_tsx11_lock();
1538 pglViewport(ctx->physDev->dc_rect.left + ctx->viewport.left,
1539 dy + ctx->viewport.top,
1540 ctx->viewport.right ? (ctx->viewport.right - ctx->viewport.left) : width,
1541 ctx->viewport.bottom ? (ctx->viewport.bottom - ctx->viewport.top) : height);
1543 pglEnable(GL_SCISSOR_TEST);
1545 if (ctx->scissor_enabled)
1546 pglScissor(ctx->physDev->dc_rect.left + min(width, max(0, ctx->scissor.left)),
1547 dy + min(height, max(0, ctx->scissor.top)),
1548 min(width, max(0, ctx->scissor.right - ctx->scissor.left)),
1549 min(height, max(0, ctx->scissor.bottom - ctx->scissor.top)));
1550 else
1551 pglScissor(ctx->physDev->dc_rect.left, dy, width, height);
1553 wine_tsx11_unlock();
1558 * X11DRV_wglMakeCurrent
1560 * For OpenGL32 wglMakeCurrent.
1562 BOOL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1563 BOOL ret;
1564 HDC hdc = physDev->hdc;
1565 DWORD type = GetObjectType(hdc);
1567 TRACE("(%p,%p)\n", hdc, hglrc);
1569 if (!has_opengl()) {
1570 ERR("No libGL on this box - disabling OpenGL support !\n");
1571 return 0;
1574 wine_tsx11_lock();
1575 if (hglrc == NULL) {
1576 ret = pglXMakeCurrent(gdi_display, None, NULL);
1577 NtCurrentTeb()->glContext = NULL;
1578 } else {
1579 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1580 Drawable drawable = get_glxdrawable(physDev);
1581 if (ctx->ctx == NULL) {
1582 /* The describe lines below are for debugging purposes only */
1583 if (TRACE_ON(wgl)) {
1584 describeDrawable(ctx, drawable);
1585 describeContext(ctx);
1588 /* Create a GLX context using the same visual as chosen earlier in wglCreateContext.
1589 * We are certain that the drawable and context are compatible as we only allow compatible formats.
1591 TRACE(" Creating GLX Context\n");
1592 if(ctx->vis)
1593 ctx->ctx = pglXCreateContext(gdi_display, ctx->vis, NULL, type == OBJ_MEMDC ? False : True);
1594 else /* Create a GLX Context for a pbuffer */
1595 ctx->ctx = pglXCreateNewContext(gdi_display, ctx->fmt->fbconfig, ctx->fmt->render_type, NULL, True);
1597 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1599 TRACE(" make current for dis %p, drawable %p, ctx %p\n", gdi_display, (void*) drawable, ctx->ctx);
1600 ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1601 NtCurrentTeb()->glContext = ctx;
1602 if(ret)
1604 ctx->physDev = physDev;
1606 if (type == OBJ_MEMDC)
1608 ctx->do_escape = TRUE;
1609 pglDrawBuffer(GL_FRONT_LEFT);
1611 else
1613 sync_current_drawable(FALSE);
1617 wine_tsx11_unlock();
1618 TRACE(" returning %s\n", (ret ? "True" : "False"));
1619 return ret;
1623 * X11DRV_wglMakeContextCurrentARB
1625 * For OpenGL32 wglMakeContextCurrentARB
1627 BOOL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc)
1629 BOOL ret;
1630 TRACE("(%p,%p,%p)\n", hDrawDev, hReadDev, hglrc);
1632 if (!has_opengl()) {
1633 ERR("No libGL on this box - disabling OpenGL support !\n");
1634 return 0;
1637 wine_tsx11_lock();
1638 if (hglrc == NULL) {
1639 ret = pglXMakeCurrent(gdi_display, None, NULL);
1640 NtCurrentTeb()->glContext = NULL;
1641 } else {
1642 if (NULL == pglXMakeContextCurrent) {
1643 ret = FALSE;
1644 } else {
1645 Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1646 Drawable d_draw = get_glxdrawable(hDrawDev);
1647 Drawable d_read = get_glxdrawable(hReadDev);
1649 if (ctx->ctx == NULL) {
1650 ctx->ctx = pglXCreateContext(gdi_display, ctx->vis, NULL, GetObjectType(hDrawDev->hdc) == OBJ_MEMDC ? False : True);
1651 TRACE(" created a delayed OpenGL context (%p)\n", ctx->ctx);
1653 ret = pglXMakeContextCurrent(gdi_display, d_draw, d_read, ctx->ctx);
1654 NtCurrentTeb()->glContext = ctx;
1657 wine_tsx11_unlock();
1659 TRACE(" returning %s\n", (ret ? "True" : "False"));
1660 return ret;
1664 * X11DRV_wglShareLists
1666 * For OpenGL32 wglShaderLists.
1668 BOOL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1669 Wine_GLContext *org = (Wine_GLContext *) hglrc1;
1670 Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1672 TRACE("(%p, %p)\n", org, dest);
1674 if (!has_opengl()) {
1675 ERR("No libGL on this box - disabling OpenGL support !\n");
1676 return 0;
1679 if (NULL != dest && dest->ctx != NULL) {
1680 ERR("Could not share display lists, context already created !\n");
1681 return FALSE;
1682 } else {
1683 if (org->ctx == NULL) {
1684 wine_tsx11_lock();
1685 describeContext(org);
1687 if(org->vis)
1688 org->ctx = pglXCreateContext(gdi_display, org->vis, NULL, GetObjectType(org->physDev->hdc) == OBJ_MEMDC ? False : True);
1689 else /* Create a GLX Context for a pbuffer */
1690 org->ctx = pglXCreateNewContext(gdi_display, org->fmt->fbconfig, org->fmt->render_type, NULL, True);
1691 wine_tsx11_unlock();
1692 TRACE(" created a delayed OpenGL context (%p) for Wine context %p\n", org->ctx, org);
1694 if (NULL != dest) {
1695 wine_tsx11_lock();
1696 describeContext(dest);
1697 /* Create the destination context with display lists shared */
1698 if(dest->vis)
1699 dest->ctx = pglXCreateContext(gdi_display, dest->vis, org->ctx, GetObjectType(org->physDev->hdc) == OBJ_MEMDC ? False : True);
1700 else /* Create a GLX Context for a pbuffer */
1701 dest->ctx = pglXCreateNewContext(gdi_display, dest->fmt->fbconfig, dest->fmt->render_type, org->ctx, True);
1702 wine_tsx11_unlock();
1703 TRACE(" created a delayed OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1704 return TRUE;
1707 return FALSE;
1710 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1712 /* We are running using client-side rendering fonts... */
1713 GLYPHMETRICS gm;
1714 unsigned int glyph;
1715 int size = 0;
1716 void *bitmap = NULL, *gl_bitmap = NULL;
1717 int org_alignment;
1719 wine_tsx11_lock();
1720 pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1721 pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1722 wine_tsx11_unlock();
1724 for (glyph = first; glyph < first + count; glyph++) {
1725 unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, NULL);
1726 int height, width_int;
1728 TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1729 if (needed_size == GDI_ERROR) {
1730 TRACE(" - needed size : %d (GDI_ERROR)\n", needed_size);
1731 goto error;
1732 } else {
1733 TRACE(" - needed size : %d\n", needed_size);
1736 if (needed_size > size) {
1737 size = needed_size;
1738 HeapFree(GetProcessHeap(), 0, bitmap);
1739 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1740 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1741 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1743 if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, NULL) == GDI_ERROR) goto error;
1744 if (TRACE_ON(opengl)) {
1745 unsigned int height, width, bitmask;
1746 unsigned char *bitmap_ = (unsigned char *) bitmap;
1748 TRACE(" - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1749 TRACE(" - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1750 TRACE(" - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1751 if (needed_size != 0) {
1752 TRACE(" - bitmap :\n");
1753 for (height = 0; height < gm.gmBlackBoxY; height++) {
1754 TRACE(" ");
1755 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1756 if (bitmask == 0) {
1757 bitmap_ += 1;
1758 bitmask = 0x80;
1760 if (*bitmap_ & bitmask)
1761 TRACE("*");
1762 else
1763 TRACE(" ");
1765 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1766 TRACE("\n");
1771 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1772 * glyph for it to be drawn properly.
1774 if (needed_size != 0) {
1775 width_int = (gm.gmBlackBoxX + 31) / 32;
1776 for (height = 0; height < gm.gmBlackBoxY; height++) {
1777 int width;
1778 for (width = 0; width < width_int; width++) {
1779 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1780 ((int *) bitmap)[height * width_int + width];
1785 wine_tsx11_lock();
1786 pglNewList(listBase++, GL_COMPILE);
1787 if (needed_size != 0) {
1788 pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1789 0 - (int) gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - (int) gm.gmptGlyphOrigin.y,
1790 gm.gmCellIncX, gm.gmCellIncY,
1791 gl_bitmap);
1792 } else {
1793 /* This is the case of 'empty' glyphs like the space character */
1794 pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1796 pglEndList();
1797 wine_tsx11_unlock();
1800 wine_tsx11_lock();
1801 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1802 wine_tsx11_unlock();
1804 HeapFree(GetProcessHeap(), 0, bitmap);
1805 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1806 return TRUE;
1808 error:
1809 wine_tsx11_lock();
1810 pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1811 wine_tsx11_unlock();
1813 HeapFree(GetProcessHeap(), 0, bitmap);
1814 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1815 return FALSE;
1819 * X11DRV_wglUseFontBitmapsA
1821 * For OpenGL32 wglUseFontBitmapsA.
1823 BOOL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1825 Font fid = physDev->font;
1827 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1829 if (!has_opengl()) {
1830 ERR("No libGL on this box - disabling OpenGL support !\n");
1831 return 0;
1834 if (fid == 0) {
1835 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
1838 wine_tsx11_lock();
1839 /* I assume that the glyphs are at the same position for X and for Windows */
1840 pglXUseXFont(fid, first, count, listBase);
1841 wine_tsx11_unlock();
1842 return TRUE;
1846 * X11DRV_wglUseFontBitmapsW
1848 * For OpenGL32 wglUseFontBitmapsW.
1850 BOOL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
1852 Font fid = physDev->font;
1854 TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
1856 if (!has_opengl()) {
1857 ERR("No libGL on this box - disabling OpenGL support !\n");
1858 return 0;
1861 if (fid == 0) {
1862 return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
1865 WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
1867 wine_tsx11_lock();
1868 /* I assume that the glyphs are at the same position for X and for Windows */
1869 pglXUseXFont(fid, first, count, listBase);
1870 wine_tsx11_unlock();
1871 return TRUE;
1874 static void WINAPI X11DRV_wglDisable(GLenum cap)
1876 if (cap == GL_SCISSOR_TEST)
1878 Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1880 if (ctx)
1881 ctx->scissor_enabled = FALSE;
1883 else
1885 wine_tsx11_lock();
1886 pglDisable(cap);
1887 wine_tsx11_unlock();
1891 static void WINAPI X11DRV_wglEnable(GLenum cap)
1893 if (cap == GL_SCISSOR_TEST)
1895 Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1897 if (ctx)
1898 ctx->scissor_enabled = TRUE;
1900 else
1902 wine_tsx11_lock();
1903 pglEnable(cap);
1904 wine_tsx11_unlock();
1908 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
1909 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
1911 wine_tsx11_lock();
1912 switch(pname)
1914 case GL_DEPTH_BITS:
1916 GLXContext gl_ctx = pglXGetCurrentContext();
1917 Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1919 pglGetIntegerv(pname, params);
1921 * if we cannot find a Wine Context
1922 * we only have the default wine desktop context,
1923 * so if we have only a 24 depth say we have 32
1925 if (NULL == ret && 24 == *params) {
1926 *params = 32;
1928 TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
1929 break;
1931 case GL_ALPHA_BITS:
1933 GLXContext gl_ctx = pglXGetCurrentContext();
1934 Wine_GLContext* ret = get_context_from_GLXContext(gl_ctx);
1936 pglXGetFBConfigAttrib(gdi_display, ret->fmt->fbconfig, GLX_ALPHA_SIZE, params);
1937 TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
1938 break;
1940 default:
1941 pglGetIntegerv(pname, params);
1942 break;
1944 wine_tsx11_unlock();
1947 static GLboolean WINAPI X11DRV_wglIsEnabled(GLenum cap)
1949 GLboolean enabled = False;
1951 if (cap == GL_SCISSOR_TEST)
1953 Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1955 if (ctx)
1956 enabled = ctx->scissor_enabled;
1958 else
1960 wine_tsx11_lock();
1961 enabled = pglIsEnabled(cap);
1962 wine_tsx11_unlock();
1964 return enabled;
1967 static void WINAPI X11DRV_wglScissor(GLint x, GLint y, GLsizei width, GLsizei height)
1969 Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1971 if (ctx)
1973 ctx->scissor.left = x;
1974 ctx->scissor.top = y;
1975 ctx->scissor.right = x + width;
1976 ctx->scissor.bottom = y + height;
1978 sync_current_drawable(TRUE);
1982 static void WINAPI X11DRV_wglViewport(GLint x, GLint y, GLsizei width, GLsizei height)
1984 Wine_GLContext *ctx = (Wine_GLContext *) NtCurrentTeb()->glContext;
1986 if (ctx)
1988 ctx->viewport.left = x;
1989 ctx->viewport.top = y;
1990 ctx->viewport.right = x + width;
1991 ctx->viewport.bottom = y + height;
1993 sync_current_drawable(TRUE);
1998 * X11DRV_wglGetExtensionsStringARB
2000 * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2002 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
2003 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2004 return WineGLInfo.wglExtensions;
2008 * X11DRV_wglCreatePbufferARB
2010 * WGL_ARB_pbuffer: wglCreatePbufferARB
2012 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
2014 Wine_GLPBuffer* object = NULL;
2015 WineGLPixelFormat *fmt = NULL;
2016 int nCfgs = 0;
2017 int attribs[256];
2018 int nAttribs = 0;
2020 TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2022 if (0 >= iPixelFormat) {
2023 ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2024 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2025 return NULL; /* unexpected error */
2028 /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2029 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2030 if(!fmt) {
2031 ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2032 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2033 goto create_failed; /* unexpected error */
2036 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2037 if (NULL == object) {
2038 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2039 goto create_failed; /* unexpected error */
2041 object->hdc = hdc;
2042 object->display = gdi_display;
2043 object->width = iWidth;
2044 object->height = iHeight;
2045 object->fmt = fmt;
2047 nAttribs = ConvertAttribWGLtoGLX(piAttribList, attribs, object);
2048 if (-1 == nAttribs) {
2049 WARN("Cannot convert WGL to GLX attributes\n");
2050 goto create_failed;
2052 PUSH2(attribs, GLX_PBUFFER_WIDTH, iWidth);
2053 PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight);
2054 while (piAttribList && 0 != *piAttribList) {
2055 int attr_v;
2056 switch (*piAttribList) {
2057 case WGL_TEXTURE_FORMAT_ARB: {
2058 ++piAttribList;
2059 attr_v = *piAttribList;
2060 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2061 if (use_render_texture_ati) {
2062 int type = 0;
2063 switch (attr_v) {
2064 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2065 case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2066 case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2067 default:
2068 SetLastError(ERROR_INVALID_DATA);
2069 goto create_failed;
2071 object->use_render_texture = 1;
2072 PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2073 } else {
2074 if (WGL_NO_TEXTURE_ARB == attr_v) {
2075 object->use_render_texture = 0;
2076 } else {
2077 if (!use_render_texture_emulation) {
2078 SetLastError(ERROR_INVALID_DATA);
2079 goto create_failed;
2081 switch (attr_v) {
2082 case WGL_TEXTURE_RGB_ARB:
2083 object->use_render_texture = GL_RGB;
2084 object->texture_bpp = 3;
2085 object->texture_format = GL_RGB;
2086 object->texture_type = GL_UNSIGNED_BYTE;
2087 break;
2088 case WGL_TEXTURE_RGBA_ARB:
2089 object->use_render_texture = GL_RGBA;
2090 object->texture_bpp = 4;
2091 object->texture_format = GL_RGBA;
2092 object->texture_type = GL_UNSIGNED_BYTE;
2093 break;
2095 /* WGL_FLOAT_COMPONENTS_NV */
2096 case WGL_TEXTURE_FLOAT_R_NV:
2097 object->use_render_texture = GL_FLOAT_R_NV;
2098 object->texture_bpp = 4;
2099 object->texture_format = GL_RED;
2100 object->texture_type = GL_FLOAT;
2101 break;
2102 case WGL_TEXTURE_FLOAT_RG_NV:
2103 object->use_render_texture = GL_FLOAT_RG_NV;
2104 object->texture_bpp = 8;
2105 object->texture_format = GL_LUMINANCE_ALPHA;
2106 object->texture_type = GL_FLOAT;
2107 break;
2108 case WGL_TEXTURE_FLOAT_RGB_NV:
2109 object->use_render_texture = GL_FLOAT_RGB_NV;
2110 object->texture_bpp = 12;
2111 object->texture_format = GL_RGB;
2112 object->texture_type = GL_FLOAT;
2113 break;
2114 case WGL_TEXTURE_FLOAT_RGBA_NV:
2115 object->use_render_texture = GL_FLOAT_RGBA_NV;
2116 object->texture_bpp = 16;
2117 object->texture_format = GL_RGBA;
2118 object->texture_type = GL_FLOAT;
2119 break;
2120 default:
2121 ERR("Unknown texture format: %x\n", attr_v);
2122 SetLastError(ERROR_INVALID_DATA);
2123 goto create_failed;
2127 break;
2130 case WGL_TEXTURE_TARGET_ARB: {
2131 ++piAttribList;
2132 attr_v = *piAttribList;
2133 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2134 if (use_render_texture_ati) {
2135 int type = 0;
2136 switch (attr_v) {
2137 case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2138 case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2139 case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2140 case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2141 default:
2142 SetLastError(ERROR_INVALID_DATA);
2143 goto create_failed;
2145 PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2146 } else {
2147 if (WGL_NO_TEXTURE_ARB == attr_v) {
2148 object->texture_target = 0;
2149 } else {
2150 if (!use_render_texture_emulation) {
2151 SetLastError(ERROR_INVALID_DATA);
2152 goto create_failed;
2154 switch (attr_v) {
2155 case WGL_TEXTURE_CUBE_MAP_ARB: {
2156 if (iWidth != iHeight) {
2157 SetLastError(ERROR_INVALID_DATA);
2158 goto create_failed;
2160 object->texture_target = GL_TEXTURE_CUBE_MAP;
2161 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2162 break;
2164 case WGL_TEXTURE_1D_ARB: {
2165 if (1 != iHeight) {
2166 SetLastError(ERROR_INVALID_DATA);
2167 goto create_failed;
2169 object->texture_target = GL_TEXTURE_1D;
2170 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2171 break;
2173 case WGL_TEXTURE_2D_ARB: {
2174 object->texture_target = GL_TEXTURE_2D;
2175 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2176 break;
2178 case WGL_TEXTURE_RECTANGLE_NV: {
2179 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2180 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2181 break;
2183 default:
2184 ERR("Unknown texture target: %x\n", attr_v);
2185 SetLastError(ERROR_INVALID_DATA);
2186 goto create_failed;
2190 break;
2193 case WGL_MIPMAP_TEXTURE_ARB: {
2194 ++piAttribList;
2195 attr_v = *piAttribList;
2196 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2197 if (use_render_texture_ati) {
2198 PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2199 } else {
2200 if (!use_render_texture_emulation) {
2201 SetLastError(ERROR_INVALID_DATA);
2202 goto create_failed;
2205 break;
2208 ++piAttribList;
2211 PUSH1(attribs, None);
2212 object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2213 TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
2214 if (!object->drawable) {
2215 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2216 goto create_failed; /* unexpected error */
2218 TRACE("->(%p)\n", object);
2219 return (HPBUFFERARB) object;
2221 create_failed:
2222 HeapFree(GetProcessHeap(), 0, object);
2223 TRACE("->(FAILED)\n");
2224 return (HPBUFFERARB) NULL;
2228 * X11DRV_wglDestroyPbufferARB
2230 * WGL_ARB_pbuffer: wglDestroyPbufferARB
2232 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2234 Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2235 TRACE("(%p)\n", hPbuffer);
2236 if (NULL == object) {
2237 SetLastError(ERROR_INVALID_HANDLE);
2238 return GL_FALSE;
2240 pglXDestroyPbuffer(object->display, object->drawable);
2241 HeapFree(GetProcessHeap(), 0, object);
2242 return GL_TRUE;
2246 * X11DRV_wglGetPbufferDCARB
2248 * WGL_ARB_pbuffer: wglGetPbufferDCARB
2249 * The function wglGetPbufferDCARB returns a device context for a pbuffer.
2250 * Gdi32 implements the part of this function which creates a device context.
2251 * This part associates the physDev with the X drawable of the pbuffer.
2253 HDC X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
2255 Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2256 if (NULL == object) {
2257 SetLastError(ERROR_INVALID_HANDLE);
2258 return NULL;
2261 /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
2262 * All formats in our pixelformat list are compatible with each other and the main drawable. */
2263 physDev->current_pf = object->fmt->iPixelFormat;
2264 physDev->drawable = object->drawable;
2265 SetRect( &physDev->drawable_rect, 0, 0, object->width, object->height );
2266 physDev->dc_rect = physDev->drawable_rect;
2268 TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
2269 return physDev->hdc;
2273 * X11DRV_wglQueryPbufferARB
2275 * WGL_ARB_pbuffer: wglQueryPbufferARB
2277 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2279 Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2280 TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2281 if (NULL == object) {
2282 SetLastError(ERROR_INVALID_HANDLE);
2283 return GL_FALSE;
2285 switch (iAttribute) {
2286 case WGL_PBUFFER_WIDTH_ARB:
2287 pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2288 break;
2289 case WGL_PBUFFER_HEIGHT_ARB:
2290 pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2291 break;
2293 case WGL_PBUFFER_LOST_ARB:
2294 FIXME("unsupported WGL_PBUFFER_LOST_ARB (need glXSelectEvent/GLX_DAMAGED work)\n");
2295 break;
2297 case WGL_TEXTURE_FORMAT_ARB:
2298 if (use_render_texture_ati) {
2299 unsigned int tmp;
2300 int type = WGL_NO_TEXTURE_ARB;
2301 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2302 switch (tmp) {
2303 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2304 case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2305 case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2307 *piValue = type;
2308 } else {
2309 if (!object->use_render_texture) {
2310 *piValue = WGL_NO_TEXTURE_ARB;
2311 } else {
2312 if (!use_render_texture_emulation) {
2313 SetLastError(ERROR_INVALID_HANDLE);
2314 return GL_FALSE;
2316 switch(object->use_render_texture) {
2317 case GL_RGB:
2318 *piValue = WGL_TEXTURE_RGB_ARB;
2319 break;
2320 case GL_RGBA:
2321 *piValue = WGL_TEXTURE_RGBA_ARB;
2322 break;
2323 /* WGL_FLOAT_COMPONENTS_NV */
2324 case GL_FLOAT_R_NV:
2325 *piValue = WGL_TEXTURE_FLOAT_R_NV;
2326 break;
2327 case GL_FLOAT_RG_NV:
2328 *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2329 break;
2330 case GL_FLOAT_RGB_NV:
2331 *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2332 break;
2333 case GL_FLOAT_RGBA_NV:
2334 *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2335 break;
2336 default:
2337 ERR("Unknown texture format: %x\n", object->use_render_texture);
2341 break;
2343 case WGL_TEXTURE_TARGET_ARB:
2344 if (use_render_texture_ati) {
2345 unsigned int tmp;
2346 int type = WGL_NO_TEXTURE_ARB;
2347 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2348 switch (tmp) {
2349 case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2350 case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2351 case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2352 case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2354 *piValue = type;
2355 } else {
2356 if (!object->texture_target) {
2357 *piValue = WGL_NO_TEXTURE_ARB;
2358 } else {
2359 if (!use_render_texture_emulation) {
2360 SetLastError(ERROR_INVALID_DATA);
2361 return GL_FALSE;
2363 switch (object->texture_target) {
2364 case GL_TEXTURE_1D: *piValue = WGL_TEXTURE_1D_ARB; break;
2365 case GL_TEXTURE_2D: *piValue = WGL_TEXTURE_2D_ARB; break;
2366 case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2367 case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2371 break;
2373 case WGL_MIPMAP_TEXTURE_ARB:
2374 if (use_render_texture_ati) {
2375 pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2376 } else {
2377 *piValue = GL_FALSE; /** don't support that */
2378 FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2380 break;
2382 default:
2383 FIXME("unexpected attribute %x\n", iAttribute);
2384 break;
2387 return GL_TRUE;
2391 * X11DRV_wglReleasePbufferDCARB
2393 * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2395 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2397 TRACE("(%p, %p)\n", hPbuffer, hdc);
2398 DeleteDC(hdc);
2399 return 0;
2403 * X11DRV_wglSetPbufferAttribARB
2405 * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2407 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2409 Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2410 WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2411 if (NULL == object) {
2412 SetLastError(ERROR_INVALID_HANDLE);
2413 return GL_FALSE;
2415 if (!object->use_render_texture) {
2416 SetLastError(ERROR_INVALID_HANDLE);
2417 return GL_FALSE;
2419 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2420 return GL_TRUE;
2422 if (NULL != pglXDrawableAttribARB) {
2423 if (use_render_texture_ati) {
2424 FIXME("Need conversion for GLX_ATI_render_texture\n");
2426 return pglXDrawableAttribARB(object->display, object->drawable, piAttribList);
2428 return GL_FALSE;
2432 * X11DRV_wglChoosePixelFormatARB
2434 * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2436 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2438 int gl_test = 0;
2439 int attribs[256];
2440 int nAttribs = 0;
2441 GLXFBConfig* cfgs = NULL;
2442 int nCfgs = 0;
2443 UINT it;
2444 int fmt_id;
2446 GLXFBConfig* cfgs_fmt = NULL;
2447 int nCfgs_fmt = 0;
2449 int fmt = 0;
2450 int pfmt_it = 0;
2452 TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2453 if (NULL != pfAttribFList) {
2454 FIXME("unused pfAttribFList\n");
2457 nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2458 if (-1 == nAttribs) {
2459 WARN("Cannot convert WGL to GLX attributes\n");
2460 return GL_FALSE;
2462 PUSH1(attribs, None);
2464 /* Search for FB configurations matching the requirements in attribs */
2465 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2466 if (NULL == cfgs) {
2467 WARN("Compatible Pixel Format not found\n");
2468 return GL_FALSE;
2471 /* Get a list of all FB configurations */
2472 cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
2473 if (NULL == cfgs_fmt) {
2474 ERR("Failed to get All FB Configs\n");
2475 XFree(cfgs);
2476 return GL_FALSE;
2479 /* Loop through all matching formats and check if they are suitable.
2480 * Note that this function should at max return nMaxFormats different formats */
2481 for (it = 0; pfmt_it < nMaxFormats && it < nCfgs; ++it) {
2482 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2483 if (gl_test) {
2484 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2485 continue;
2488 /* Search for the format in our list of compatible formats */
2489 fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id);
2490 if(!fmt)
2491 continue;
2493 piFormats[pfmt_it] = fmt;
2494 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d/%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it], nCfgs_fmt);
2495 pfmt_it++;
2498 *nNumFormats = pfmt_it;
2499 /** free list */
2500 XFree(cfgs);
2501 XFree(cfgs_fmt);
2502 return GL_TRUE;
2506 * X11DRV_wglGetPixelFormatAttribivARB
2508 * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2510 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2512 UINT i;
2513 WineGLPixelFormat *fmt = NULL;
2514 int hTest;
2515 int tmp;
2516 int curGLXAttr = 0;
2517 int nWGLFormats = 0;
2519 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2521 if (0 < iLayerPlane) {
2522 FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2523 return GL_FALSE;
2526 /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supoprted.
2527 * We don't have to fail yet as a program can specify an invaled iPixelFormat (lets say 0) if it wants to query
2528 * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2529 fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2530 if(!fmt) {
2531 WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2534 for (i = 0; i < nAttributes; ++i) {
2535 const int curWGLAttr = piAttributes[i];
2536 TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2538 switch (curWGLAttr) {
2539 case WGL_NUMBER_PIXEL_FORMATS_ARB:
2540 piValues[i] = nWGLFormats;
2541 continue;
2543 case WGL_SUPPORT_OPENGL_ARB:
2544 piValues[i] = GL_TRUE;
2545 continue;
2547 case WGL_ACCELERATION_ARB:
2548 curGLXAttr = GLX_CONFIG_CAVEAT;
2549 if (!fmt) goto pix_error;
2550 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2551 if (hTest) goto get_error;
2552 switch (tmp) {
2553 case GLX_NONE: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2554 case GLX_SLOW_CONFIG: piValues[i] = WGL_NO_ACCELERATION_ARB; break;
2555 case GLX_NON_CONFORMANT_CONFIG: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
2556 default:
2557 ERR("unexpected Config Caveat(%x)\n", tmp);
2558 piValues[i] = WGL_NO_ACCELERATION_ARB;
2560 continue;
2562 case WGL_TRANSPARENT_ARB:
2563 curGLXAttr = GLX_TRANSPARENT_TYPE;
2564 if (!fmt) goto pix_error;
2565 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2566 if (hTest) goto get_error;
2567 piValues[i] = GL_FALSE;
2568 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2569 continue;
2571 case WGL_PIXEL_TYPE_ARB:
2572 curGLXAttr = GLX_RENDER_TYPE;
2573 if (!fmt) goto pix_error;
2574 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2575 if (hTest) goto get_error;
2576 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2577 if (tmp & GLX_RGBA_BIT) { piValues[i] = WGL_TYPE_RGBA_ARB; }
2578 else if (tmp & GLX_COLOR_INDEX_BIT) { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2579 else if (tmp & GLX_RGBA_FLOAT_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2580 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2581 else {
2582 ERR("unexpected RenderType(%x)\n", tmp);
2583 piValues[i] = WGL_TYPE_RGBA_ARB;
2585 continue;
2587 case WGL_COLOR_BITS_ARB:
2588 /** see ConvertAttribWGLtoGLX for explain */
2589 if (!fmt) goto pix_error;
2590 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, piValues + i);
2591 if (hTest) goto get_error;
2592 TRACE("WGL_COLOR_BITS_ARB: GLX_BUFFER_SIZE = %d\n", piValues[i]);
2593 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &tmp);
2594 if (hTest) goto get_error;
2595 TRACE("WGL_COLOR_BITS_ARB: GLX_ALPHA_SIZE = %d\n", tmp);
2596 piValues[i] = piValues[i] - tmp;
2597 continue;
2599 case WGL_BIND_TO_TEXTURE_RGB_ARB:
2600 if (use_render_texture_ati) {
2601 curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2602 break;
2604 case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2605 if (use_render_texture_ati) {
2606 curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2607 break;
2609 if (!use_render_texture_emulation) {
2610 piValues[i] = GL_FALSE;
2611 continue;
2613 curGLXAttr = GLX_RENDER_TYPE;
2614 if (!fmt) goto pix_error;
2615 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2616 if (hTest) goto get_error;
2617 if (GLX_COLOR_INDEX_BIT == tmp) {
2618 piValues[i] = GL_FALSE;
2619 continue;
2621 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2622 if (hTest) goto get_error;
2623 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2624 continue;
2626 case WGL_BLUE_BITS_ARB:
2627 curGLXAttr = GLX_BLUE_SIZE;
2628 break;
2629 case WGL_RED_BITS_ARB:
2630 curGLXAttr = GLX_RED_SIZE;
2631 break;
2632 case WGL_GREEN_BITS_ARB:
2633 curGLXAttr = GLX_GREEN_SIZE;
2634 break;
2635 case WGL_ALPHA_BITS_ARB:
2636 curGLXAttr = GLX_ALPHA_SIZE;
2637 break;
2638 case WGL_DEPTH_BITS_ARB:
2639 curGLXAttr = GLX_DEPTH_SIZE;
2640 break;
2641 case WGL_STENCIL_BITS_ARB:
2642 curGLXAttr = GLX_STENCIL_SIZE;
2643 break;
2644 case WGL_DOUBLE_BUFFER_ARB:
2645 curGLXAttr = GLX_DOUBLEBUFFER;
2646 break;
2647 case WGL_STEREO_ARB:
2648 curGLXAttr = GLX_STEREO;
2649 break;
2650 case WGL_AUX_BUFFERS_ARB:
2651 curGLXAttr = GLX_AUX_BUFFERS;
2652 break;
2654 case WGL_SUPPORT_GDI_ARB:
2655 curGLXAttr = GLX_X_RENDERABLE;
2656 break;
2658 case WGL_DRAW_TO_WINDOW_ARB:
2659 case WGL_DRAW_TO_BITMAP_ARB:
2660 case WGL_DRAW_TO_PBUFFER_ARB:
2661 if (!fmt) goto pix_error;
2662 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2663 if (hTest) goto get_error;
2664 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
2665 (curWGLAttr == WGL_DRAW_TO_BITMAP_ARB && (tmp&GLX_PIXMAP_BIT)) ||
2666 (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
2667 piValues[i] = GL_TRUE;
2668 else
2669 piValues[i] = GL_FALSE;
2670 continue;
2672 case WGL_PBUFFER_LARGEST_ARB:
2673 curGLXAttr = GLX_LARGEST_PBUFFER;
2674 break;
2676 case WGL_SAMPLE_BUFFERS_ARB:
2677 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
2678 break;
2680 case WGL_SAMPLES_ARB:
2681 curGLXAttr = GLX_SAMPLES_ARB;
2682 break;
2684 case WGL_FLOAT_COMPONENTS_NV:
2685 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
2686 break;
2688 default:
2689 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
2692 /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
2693 * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
2694 * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
2696 * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
2697 * and check which options can work using iPixelFormat=0 and which not.
2698 * A problem would be that this function is an extension. This would
2699 * mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
2701 if (0 != curGLXAttr && iPixelFormat != 0) {
2702 if (!fmt) goto pix_error;
2703 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
2704 if (hTest) goto get_error;
2705 curGLXAttr = 0;
2706 } else {
2707 piValues[i] = GL_FALSE;
2710 return GL_TRUE;
2712 get_error:
2713 ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
2714 return GL_FALSE;
2716 pix_error:
2717 ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
2718 return GL_FALSE;
2722 * X11DRV_wglGetPixelFormatAttribfvARB
2724 * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
2726 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
2728 int *attr;
2729 int ret;
2730 int i;
2732 TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
2734 /* Allocate a temporary array to store integer values */
2735 attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
2736 if (!attr) {
2737 ERR("couldn't allocate %d array\n", nAttributes);
2738 return GL_FALSE;
2741 /* Piggy-back on wglGetPixelFormatAttribivARB */
2742 ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
2743 if (ret) {
2744 /* Convert integer values to float. Should also check for attributes
2745 that can give decimal values here */
2746 for (i=0; i<nAttributes;i++) {
2747 pfValues[i] = attr[i];
2751 HeapFree(GetProcessHeap(), 0, attr);
2752 return ret;
2756 * X11DRV_wglBindTexImageARB
2758 * WGL_ARB_render_texture: wglBindTexImageARB
2760 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2762 Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2763 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2764 if (NULL == object) {
2765 SetLastError(ERROR_INVALID_HANDLE);
2766 return GL_FALSE;
2768 if (!object->use_render_texture) {
2769 SetLastError(ERROR_INVALID_HANDLE);
2770 return GL_FALSE;
2773 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2774 static int init = 0;
2775 int prev_binded_texture = 0;
2776 GLXContext prev_context = pglXGetCurrentContext();
2777 Drawable prev_drawable = pglXGetCurrentDrawable();
2778 GLXContext tmp_context;
2780 /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
2781 This is mostly due to lack of demos/games using them. Further the use of glReadPixels
2782 isn't ideal performance wise but I wasn't able to get other ways working.
2784 if(!init) {
2785 init = 1; /* Only show the FIXME once for performance reasons */
2786 FIXME("partial stub!\n");
2789 TRACE("drawable=%p, context=%p\n", (void*)object->drawable, prev_context);
2790 tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
2792 pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
2794 /* Switch to our pbuffer */
2795 pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
2797 /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
2798 * After that upload the pbuffer texture data. */
2799 pglBindTexture(object->texture_target, prev_binded_texture);
2800 pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
2802 /* Switch back to the original drawable and upload the pbuffer-texture */
2803 pglXMakeCurrent(object->display, prev_drawable, prev_context);
2804 pglXDestroyContext(gdi_display, tmp_context);
2805 return GL_TRUE;
2808 if (NULL != pglXBindTexImageARB) {
2809 return pglXBindTexImageARB(object->display, object->drawable, iBuffer);
2811 return GL_FALSE;
2815 * X11DRV_wglReleaseTexImageARB
2817 * WGL_ARB_render_texture: wglReleaseTexImageARB
2819 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
2821 Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
2822 TRACE("(%p, %d)\n", hPbuffer, iBuffer);
2823 if (NULL == object) {
2824 SetLastError(ERROR_INVALID_HANDLE);
2825 return GL_FALSE;
2827 if (!object->use_render_texture) {
2828 SetLastError(ERROR_INVALID_HANDLE);
2829 return GL_FALSE;
2831 if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2832 return GL_TRUE;
2834 if (NULL != pglXReleaseTexImageARB) {
2835 return pglXReleaseTexImageARB(object->display, object->drawable, iBuffer);
2837 return GL_FALSE;
2841 * X11DRV_wglGetExtensionsStringEXT
2843 * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
2845 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
2846 TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2847 return WineGLInfo.wglExtensions;
2851 * X11DRV_wglGetSwapIntervalEXT
2853 * WGL_EXT_swap_control: wglGetSwapIntervalEXT
2855 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
2856 FIXME("(),stub!\n");
2857 return swap_interval;
2861 * X11DRV_wglSwapIntervalEXT
2863 * WGL_EXT_swap_control: wglSwapIntervalEXT
2865 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
2866 TRACE("(%d)\n", interval);
2867 swap_interval = interval;
2868 if (NULL != pglXSwapIntervalSGI) {
2869 return 0 == pglXSwapIntervalSGI(interval);
2871 WARN("(): GLX_SGI_swap_control extension seems not supported\n");
2872 return TRUE;
2876 * X11DRV_wglAllocateMemoryNV
2878 * WGL_NV_vertex_array_range: wglAllocateMemoryNV
2880 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
2881 TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
2882 if (pglXAllocateMemoryNV == NULL)
2883 return NULL;
2885 return pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
2889 * X11DRV_wglFreeMemoryNV
2891 * WGL_NV_vertex_array_range: wglFreeMemoryNV
2893 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
2894 TRACE("(%p)\n", pointer);
2895 if (pglXFreeMemoryNV == NULL)
2896 return;
2898 pglXFreeMemoryNV(pointer);
2902 * glxRequireVersion (internal)
2904 * Check if the supported GLX version matches requiredVersion.
2906 static BOOL glxRequireVersion(int requiredVersion)
2908 /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
2909 if(requiredVersion <= WineGLInfo.glxVersion[1])
2910 return TRUE;
2912 return FALSE;
2915 static BOOL glxRequireExtension(const char *requiredExtension)
2917 if (strstr(WineGLInfo.glxClientExtensions, requiredExtension) == NULL) {
2918 return FALSE;
2921 return TRUE;
2924 static void register_extension_string(const char *ext)
2926 strcat(WineGLInfo.wglExtensions, " ");
2927 strcat(WineGLInfo.wglExtensions, ext);
2929 TRACE("'%s'\n", ext);
2932 static BOOL register_extension(const WineGLExtension * ext)
2934 int i;
2936 assert( WineGLExtensionListSize < MAX_EXTENSIONS );
2937 WineGLExtensionList[WineGLExtensionListSize++] = ext;
2939 strcat(WineGLInfo.wglExtensions, " ");
2940 strcat(WineGLInfo.wglExtensions, ext->extName);
2942 TRACE("'%s'\n", ext->extName);
2944 for (i = 0; ext->extEntryPoints[i].funcName; ++i)
2945 TRACE(" - '%s'\n", ext->extEntryPoints[i].funcName);
2947 return TRUE;
2950 static const WineGLExtension WGL_internal_functions =
2954 { "wglDisable", X11DRV_wglDisable },
2955 { "wglEnable", X11DRV_wglEnable },
2956 { "wglGetIntegerv", X11DRV_wglGetIntegerv },
2957 { "wglIsEnabled", X11DRV_wglIsEnabled },
2958 { "wglScissor", X11DRV_wglScissor },
2959 { "wglViewport", X11DRV_wglViewport },
2964 static const WineGLExtension WGL_ARB_extensions_string =
2966 "WGL_ARB_extensions_string",
2968 { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
2972 static const WineGLExtension WGL_ARB_make_current_read =
2974 "WGL_ARB_make_current_read",
2976 { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
2977 { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
2981 static const WineGLExtension WGL_ARB_multisample =
2983 "WGL_ARB_multisample",
2986 static const WineGLExtension WGL_ARB_pbuffer =
2988 "WGL_ARB_pbuffer",
2990 { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
2991 { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
2992 { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
2993 { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
2994 { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
2995 { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
2999 static const WineGLExtension WGL_ARB_pixel_format =
3001 "WGL_ARB_pixel_format",
3003 { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3004 { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3005 { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3009 static const WineGLExtension WGL_ARB_render_texture =
3011 "WGL_ARB_render_texture",
3013 { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3014 { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3018 static const WineGLExtension WGL_EXT_extensions_string =
3020 "WGL_EXT_extensions_string",
3022 { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3026 static const WineGLExtension WGL_EXT_swap_control =
3028 "WGL_EXT_swap_control",
3030 { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3031 { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3035 static const WineGLExtension WGL_NV_vertex_array_range =
3037 "WGL_NV_vertex_array_range",
3039 { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3040 { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3045 * X11DRV_WineGL_LoadExtensions
3047 static void X11DRV_WineGL_LoadExtensions(void)
3049 WineGLInfo.wglExtensions[0] = 0;
3051 /* Load Wine internal functions */
3052 register_extension(&WGL_internal_functions);
3054 /* ARB Extensions */
3056 if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3058 register_extension_string("WGL_ARB_pixel_format_float");
3059 register_extension_string("WGL_ATI_pixel_format_float");
3062 register_extension(&WGL_ARB_extensions_string);
3064 if (glxRequireVersion(3))
3065 register_extension(&WGL_ARB_make_current_read);
3067 if (glxRequireExtension("GLX_ARB_multisample"))
3068 register_extension(&WGL_ARB_multisample);
3070 /* In general pbuffer functionality requires support in the X-server. The functionality is
3071 * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3072 * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3073 * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3075 * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3076 * without support in the X-server (which other Mesa based drivers require).
3078 * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3079 * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3080 * is available pbuffers must be available too. */
3081 if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3082 register_extension(&WGL_ARB_pbuffer);
3084 register_extension(&WGL_ARB_pixel_format);
3086 /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3087 if (glxRequireExtension("GLX_ATI_render_texture") ||
3088 glxRequireExtension("GLX_ARB_render_texture") ||
3089 (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3091 register_extension(&WGL_ARB_render_texture);
3093 /* The WGL version of GLX_NV_float_buffer requires render_texture */
3094 if(glxRequireExtension("GLX_NV_float_buffer"))
3095 register_extension_string("WGL_NV_float_buffer");
3097 /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3098 if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3099 register_extension_string("WGL_NV_texture_rectangle");
3102 /* EXT Extensions */
3104 register_extension(&WGL_EXT_extensions_string);
3106 /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3107 * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3108 register_extension(&WGL_EXT_swap_control);
3110 /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3111 if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3112 register_extension(&WGL_NV_vertex_array_range);
3116 static XID create_glxpixmap(X11DRV_PDEVICE *physDev)
3118 GLXPixmap ret;
3119 XVisualInfo *vis;
3120 XVisualInfo template;
3121 int num;
3123 wine_tsx11_lock();
3125 /* Retrieve the visualid from our main visual which is the only visual we can use */
3126 template.visualid = XVisualIDFromVisual(visual);
3127 vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
3129 ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
3130 XFree(vis);
3131 wine_tsx11_unlock();
3132 TRACE("return %lx\n", ret);
3133 return ret;
3136 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3138 Drawable ret;
3140 if(physDev->bitmap)
3142 if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
3143 ret = physDev->drawable; /* PBuffer */
3144 else
3146 if(!physDev->bitmap->glxpixmap)
3147 physDev->bitmap->glxpixmap = create_glxpixmap(physDev);
3148 ret = physDev->bitmap->glxpixmap;
3151 else
3152 ret = physDev->drawable;
3153 return ret;
3156 BOOL destroy_glxpixmap(XID glxpixmap)
3158 wine_tsx11_lock();
3159 pglXDestroyGLXPixmap(gdi_display, glxpixmap);
3160 wine_tsx11_unlock();
3161 return TRUE;
3165 * X11DRV_SwapBuffers
3167 * Swap the buffers of this DC
3169 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
3171 GLXDrawable drawable;
3172 if (!has_opengl()) {
3173 ERR("No libGL on this box - disabling OpenGL support !\n");
3174 return 0;
3177 TRACE_(opengl)("(%p)\n", physDev);
3179 drawable = get_glxdrawable(physDev);
3180 wine_tsx11_lock();
3181 pglXSwapBuffers(gdi_display, drawable);
3182 wine_tsx11_unlock();
3184 /* FPS support */
3185 if (TRACE_ON(fps))
3187 static long prev_time, frames;
3189 DWORD time = GetTickCount();
3190 frames++;
3191 /* every 1.5 seconds */
3192 if (time - prev_time > 1500) {
3193 TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
3194 prev_time = time;
3195 frames = 0;
3199 return TRUE;
3202 /***********************************************************************
3203 * X11DRV_setup_opengl_visual
3205 * Setup the default visual used for OpenGL and Direct3D, and the desktop
3206 * window (if it exists). If OpenGL isn't available, the visual is simply
3207 * set to the default visual for the display
3209 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
3211 XVisualInfo *visual = NULL;
3212 int i;
3214 /* In order to support OpenGL or D3D, we require a double-buffered visual and stencil buffer support,
3215 * D3D and some applications can make use of aux buffers.
3217 int visualProperties[][11] = {
3218 { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_ALPHA_SIZE, 8, GLX_AUX_BUFFERS, 1, None },
3219 { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_ALPHA_SIZE, 8, None },
3220 { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, None },
3221 { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 16, None },
3224 if (!has_opengl())
3225 return NULL;
3227 wine_tsx11_lock();
3228 for (i = 0; i < sizeof(visualProperties)/sizeof(visualProperties[0]); ++i) {
3229 visual = pglXChooseVisual(display, DefaultScreen(display), visualProperties[i]);
3230 if (visual)
3231 break;
3233 wine_tsx11_unlock();
3235 if (visual)
3236 TRACE("Visual ID %lx Chosen\n", visual->visualid);
3237 else
3238 WARN("No suitable visual found\n");
3240 return visual;
3243 #else /* no OpenGL includes */
3245 /***********************************************************************
3246 * ChoosePixelFormat (X11DRV.@)
3248 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
3249 const PIXELFORMATDESCRIPTOR *ppfd) {
3250 ERR("No OpenGL support compiled in.\n");
3252 return 0;
3255 /***********************************************************************
3256 * DescribePixelFormat (X11DRV.@)
3258 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
3259 int iPixelFormat,
3260 UINT nBytes,
3261 PIXELFORMATDESCRIPTOR *ppfd) {
3262 ERR("No OpenGL support compiled in.\n");
3264 return 0;
3267 /***********************************************************************
3268 * GetPixelFormat (X11DRV.@)
3270 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
3271 ERR("No OpenGL support compiled in.\n");
3273 return 0;
3276 /***********************************************************************
3277 * SetPixelFormat (X11DRV.@)
3279 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
3280 int iPixelFormat,
3281 const PIXELFORMATDESCRIPTOR *ppfd) {
3282 ERR("No OpenGL support compiled in.\n");
3284 return FALSE;
3287 /***********************************************************************
3288 * SwapBuffers (X11DRV.@)
3290 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
3291 ERR_(opengl)("No OpenGL support compiled in.\n");
3293 return FALSE;
3297 * X11DRV_wglCreateContext
3299 * For OpenGL32 wglCreateContext.
3301 HGLRC X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
3302 ERR_(opengl)("No OpenGL support compiled in.\n");
3303 return NULL;
3307 * X11DRV_wglDeleteContext
3309 * For OpenGL32 wglDeleteContext.
3311 BOOL X11DRV_wglDeleteContext(HGLRC hglrc) {
3312 ERR_(opengl)("No OpenGL support compiled in.\n");
3313 return FALSE;
3317 * X11DRV_wglGetProcAddress
3319 * For OpenGL32 wglGetProcAddress.
3321 PROC X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
3322 ERR_(opengl)("No OpenGL support compiled in.\n");
3323 return NULL;
3326 HDC X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, void *hPbuffer)
3328 ERR_(opengl)("No OpenGL support compiled in.\n");
3329 return NULL;
3332 BOOL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
3333 ERR_(opengl)("No OpenGL support compiled in.\n");
3334 return FALSE;
3338 * X11DRV_wglMakeCurrent
3340 * For OpenGL32 wglMakeCurrent.
3342 BOOL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
3343 ERR_(opengl)("No OpenGL support compiled in.\n");
3344 return FALSE;
3348 * X11DRV_wglShareLists
3350 * For OpenGL32 wglShaderLists.
3352 BOOL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
3353 ERR_(opengl)("No OpenGL support compiled in.\n");
3354 return FALSE;
3358 * X11DRV_wglUseFontBitmapsA
3360 * For OpenGL32 wglUseFontBitmapsA.
3362 BOOL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3364 ERR_(opengl)("No OpenGL support compiled in.\n");
3365 return FALSE;
3369 * X11DRV_wglUseFontBitmapsW
3371 * For OpenGL32 wglUseFontBitmapsW.
3373 BOOL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3375 ERR_(opengl)("No OpenGL support compiled in.\n");
3376 return FALSE;
3379 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
3381 return NULL;
3384 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3386 return 0;
3389 BOOL destroy_glxpixmap(XID glxpixmap)
3391 return FALSE;
3394 #endif /* defined(HAVE_OPENGL) */