2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
28 #include "wine/library.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
33 #if defined(HAVE_GL_GL_H) && defined(HAVE_GL_GLX_H)
45 #ifdef HAVE_GL_GLEXT_H
46 # include <GL/glext.h>
53 /* Redefines the constants */
54 #define CALLBACK __stdcall
55 #define WINAPI __stdcall
56 #define APIENTRY WINAPI
59 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
60 TRACE(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
61 TRACE(" - dwFlags : ");
62 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
63 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
64 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
65 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
66 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
67 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
68 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
69 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
70 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
71 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
72 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
73 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
74 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
75 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
76 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
77 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
78 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
82 TRACE(" - iPixelType : ");
83 switch (ppfd->iPixelType) {
84 case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
85 case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
89 TRACE(" - Color : %d\n", ppfd->cColorBits);
90 TRACE(" - Red : %d\n", ppfd->cRedBits);
91 TRACE(" - Green : %d\n", ppfd->cGreenBits);
92 TRACE(" - Blue : %d\n", ppfd->cBlueBits);
93 TRACE(" - Alpha : %d\n", ppfd->cAlphaBits);
94 TRACE(" - Accum : %d\n", ppfd->cAccumBits);
95 TRACE(" - Depth : %d\n", ppfd->cDepthBits);
96 TRACE(" - Stencil : %d\n", ppfd->cStencilBits);
97 TRACE(" - Aux : %d\n", ppfd->cAuxBuffers);
99 TRACE(" - iLayerType : ");
100 switch (ppfd->iLayerType) {
101 case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
102 case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
103 case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
108 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient and
109 include all dependencies
112 #define SONAME_LIBGL "libGL.so"
115 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
116 MAKE_FUNCPTR(glGetError)
117 MAKE_FUNCPTR(glXChooseVisual)
118 MAKE_FUNCPTR(glXGetConfig)
119 MAKE_FUNCPTR(glXSwapBuffers)
120 MAKE_FUNCPTR(glXQueryExtension)
122 MAKE_FUNCPTR(glXGetFBConfigs)
123 MAKE_FUNCPTR(glXChooseFBConfig)
124 MAKE_FUNCPTR(glXGetFBConfigAttrib)
125 MAKE_FUNCPTR(glXCreateGLXPixmap)
126 MAKE_FUNCPTR(glXDestroyGLXPixmap)
127 /* MAKE_FUNCPTR(glXQueryDrawable) */
130 static BOOL has_opengl(void)
132 static int init_done;
133 static void *opengl_handle;
135 int error_base, event_base;
137 if (init_done) return (opengl_handle != NULL);
140 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
141 if (opengl_handle == NULL) return FALSE;
143 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(opengl_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
144 LOAD_FUNCPTR(glGetError)
145 LOAD_FUNCPTR(glXChooseVisual)
146 LOAD_FUNCPTR(glXGetConfig)
147 LOAD_FUNCPTR(glXSwapBuffers)
148 LOAD_FUNCPTR(glXQueryExtension)
150 LOAD_FUNCPTR(glXGetFBConfigs)
151 LOAD_FUNCPTR(glXChooseFBConfig)
152 LOAD_FUNCPTR(glXGetFBConfigAttrib)
153 LOAD_FUNCPTR(glXCreateGLXPixmap)
154 LOAD_FUNCPTR(glXDestroyGLXPixmap)
158 if (pglXQueryExtension(gdi_display, &event_base, &error_base) == True) {
159 TRACE("GLX is up and running error_base = %d\n", error_base);
161 wine_dlclose(opengl_handle, NULL, 0);
162 opengl_handle = NULL;
165 return (opengl_handle != NULL);
168 wine_dlclose(opengl_handle, NULL, 0);
169 opengl_handle = NULL;
173 #define TEST_AND_ADD1(t,a) if (t) att_list[att_pos++] = (a)
174 #define TEST_AND_ADD2(t,a,b) if (t) { att_list[att_pos++] = (a); att_list[att_pos++] = (b); }
175 #define NULL_TEST_AND_ADD2(tv,a,b) att_list[att_pos++] = (a); att_list[att_pos++] = ((tv) == 0 ? 0 : (b))
176 #define ADD2(a,b) att_list[att_pos++] = (a); att_list[att_pos++] = (b)
179 * X11DRV_ChoosePixelFormat
181 * Equivalent of glXChooseVisual
183 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
184 const PIXELFORMATDESCRIPTOR *ppfd) {
188 GLXFBConfig* cfgs = NULL;
192 ERR("No libGL on this box - disabling OpenGL support !\n");
196 if (TRACE_ON(opengl)) {
197 TRACE("(%p,%p)\n", physDev, ppfd);
199 dump_PIXELFORMATDESCRIPTOR((const PIXELFORMATDESCRIPTOR *) ppfd);
202 if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) {
203 ERR("Flag not supported !\n");
204 /* Should SetError here... */
208 /* Now, build the request to GLX */
210 if (ppfd->iPixelType == PFD_TYPE_COLORINDEX) {
211 ADD2(GLX_BUFFER_SIZE, ppfd->cColorBits);
213 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
214 ADD2(GLX_RENDER_TYPE, GLX_RGBA_BIT);
215 if (ppfd->dwFlags & PFD_DEPTH_DONTCARE) {
216 ADD2(GLX_DEPTH_SIZE, GLX_DONT_CARE);
218 if (32 == ppfd->cDepthBits) {
220 * for 32 bpp depth buffers force to use 24.
221 * needed as some drivers don't support 32bpp
223 TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, 24);
225 TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, ppfd->cDepthBits);
228 if (32 == ppfd->cColorBits) {
229 ADD2(GLX_RED_SIZE, 8);
230 ADD2(GLX_GREEN_SIZE, 8);
231 ADD2(GLX_BLUE_SIZE, 8);
232 ADD2(GLX_ALPHA_SIZE, 8);
234 ADD2(GLX_BUFFER_SIZE, ppfd->cColorBits);
235 TEST_AND_ADD2(ppfd->cAlphaBits, GLX_ALPHA_SIZE, ppfd->cAlphaBits);
238 TEST_AND_ADD2(ppfd->cStencilBits, GLX_STENCIL_SIZE, ppfd->cStencilBits);
239 TEST_AND_ADD2(ppfd->cAuxBuffers, GLX_AUX_BUFFERS, ppfd->cAuxBuffers);
241 /* These flags are not supported yet...
242 ADD2(GLX_ACCUM_SIZE, ppfd->cAccumBits);
245 /** facultative flags now */
246 att_pos_fac = att_pos;
247 if (ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) {
248 ADD2(GLX_DOUBLEBUFFER, GLX_DONT_CARE);
250 ADD2(GLX_DOUBLEBUFFER, (ppfd->dwFlags & PFD_DOUBLEBUFFER) ? TRUE : FALSE);
252 if (ppfd->dwFlags & PFD_STEREO_DONTCARE) {
253 ADD2(GLX_STEREO, GLX_DONT_CARE);
255 ADD2(GLX_STEREO, (ppfd->dwFlags & PFD_STEREO) ? TRUE : FALSE);
258 /** Attributes List End */
259 att_list[att_pos] = None;
267 GLXFBConfig* cfgs_fmt = NULL;
272 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), att_list, &nCfgs);
274 * if we have facultative flags and we failed, try without
277 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/ntopnglr_2qb8.asp
279 if ((NULL == cfgs || 0 == nCfgs) && att_pos > att_pos_fac) {
280 att_list[att_pos_fac] = None;
281 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), att_list, &nCfgs);
284 if (NULL == cfgs || 0 == nCfgs) {
285 ERR("glXChooseFBConfig returns NULL (glError: %d)\n", pglGetError());
290 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[0], GLX_FBCONFIG_ID, &fmt_id);
292 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
297 cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
298 if (NULL == cfgs_fmt) {
299 ERR("Failed to get All FB Configs\n");
304 for (it_fmt = 0; it_fmt < nCfgs_fmt; ++it_fmt) {
305 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs_fmt[it_fmt], GLX_FBCONFIG_ID, &tmp_fmt_id);
307 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
312 if (fmt_id == tmp_fmt_id) {
317 if (it_fmt == nCfgs_fmt) {
318 ERR("Failed to get valid fmt for FBCONFIG_ID(%d)\n", fmt_id);
325 if (NULL != cfgs) XFree(cfgs);
331 * X11DRV_DescribePixelFormat
333 * Get the pixel-format descriptor associated to the given id
335 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
338 PIXELFORMATDESCRIPTOR *ppfd) {
339 /*XVisualInfo *vis;*/
343 GLXFBConfig* cfgs = NULL;
349 ERR("No libGL on this box - disabling OpenGL support !\n");
353 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
356 cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
359 if (NULL == cfgs || 0 == nCfgs) {
360 ERR("unexpected iPixelFormat(%d), returns NULL\n", iPixelFormat);
361 return 0; /* unespected error */
365 /* The application is only querying the number of visuals */
367 if (NULL != cfgs) XFree(cfgs);
372 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
373 ERR("Wrong structure size !\n");
374 /* Should set error */
378 if (nCfgs < iPixelFormat) {
379 ERR("unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", iPixelFormat, nCfgs);
380 return 0; /* unespected error */
384 cur = cfgs[iPixelFormat - 1];
386 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
387 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
390 /* These flags are always the same... */
391 ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
392 /* Now the flags extracted from the Visual */
396 pglXGetFBConfigAttrib(gdi_display, cur, GLX_CONFIG_CAVEAT, &value);
397 if(value == GLX_SLOW_CONFIG)
398 ppfd->dwFlags |= PFD_GENERIC_ACCELERATED;
400 pglXGetFBConfigAttrib(gdi_display, cur, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
401 pglXGetFBConfigAttrib(gdi_display, cur, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
404 pglXGetFBConfigAttrib(gdi_display, cur, GLX_RENDER_TYPE, &value);
405 if (value & GLX_RGBA_BIT)
406 ppfd->iPixelType = PFD_TYPE_RGBA;
408 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
411 pglXGetFBConfigAttrib(gdi_display, cur, GLX_BUFFER_SIZE, &value);
412 ppfd->cColorBits = value;
414 /* Red, green, blue and alpha bits / shifts */
415 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
416 pglXGetFBConfigAttrib(gdi_display, cur, GLX_RED_SIZE, &rb);
417 pglXGetFBConfigAttrib(gdi_display, cur, GLX_GREEN_SIZE, &gb);
418 pglXGetFBConfigAttrib(gdi_display, cur, GLX_BLUE_SIZE, &bb);
419 pglXGetFBConfigAttrib(gdi_display, cur, GLX_ALPHA_SIZE, &ab);
422 ppfd->cRedShift = gb + bb + ab;
423 ppfd->cBlueBits = bb;
424 ppfd->cBlueShift = ab;
425 ppfd->cGreenBits = gb;
426 ppfd->cGreenShift = bb + ab;
427 ppfd->cAlphaBits = ab;
428 ppfd->cAlphaShift = 0;
433 ppfd->cBlueShift = 0;
434 ppfd->cGreenBits = 0;
435 ppfd->cGreenShift = 0;
436 ppfd->cAlphaBits = 0;
437 ppfd->cAlphaShift = 0;
439 /* Accums : to do ... */
442 pglXGetFBConfigAttrib(gdi_display, cur, GLX_DEPTH_SIZE, &value);
443 ppfd->cDepthBits = value;
446 pglXGetFBConfigAttrib(gdi_display, cur, GLX_STENCIL_SIZE, &value);
447 ppfd->cStencilBits = value;
451 /* Aux : to do ... */
453 ppfd->iLayerType = PFD_MAIN_PLANE;
455 if (TRACE_ON(opengl)) {
456 dump_PIXELFORMATDESCRIPTOR(ppfd);
460 if (NULL != cfgs) XFree(cfgs);
467 * X11DRV_GetPixelFormat
469 * Get the pixel-format id used by this DC
471 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
472 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
474 return physDev->current_pf;
478 * X11DRV_SetPixelFormat
480 * Set the pixel-format id used by this DC
482 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
484 const PIXELFORMATDESCRIPTOR *ppfd) {
485 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
487 physDev->current_pf = iPixelFormat;
489 if (TRACE_ON(opengl)) {
491 GLXFBConfig* cfgs_fmt = NULL;
497 * How to test if hdc current drawable is compatible (visual/FBConfig) ?
499 * in case of root window created HDCs we crash here :(
501 Drawable drawable = get_drawable( physDev->hdc );
502 TRACE(" drawable (%p,%p) have :\n", drawable, root_window);
503 pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &value);
504 TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
505 pglXQueryDrawable(gdi_display, drawable, GLX_VISUAL_ID, (unsigned int*) &value);
506 TRACE(" - VISUAL_ID as 0x%x\n", tmp);
507 pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &value);
508 TRACE(" - WIDTH as %d\n", tmp);
509 pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &value);
510 TRACE(" - HEIGHT as %d\n", tmp);
512 cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
513 cur_cfg = cfgs_fmt[iPixelFormat - 1];
514 gl_test = pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_FBCONFIG_ID, &value);
516 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
518 TRACE(" FBConfig have :\n");
519 TRACE(" - FBCONFIG_ID 0x%x\n", value);
520 pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_VISUAL_ID, &value);
521 TRACE(" - VISUAL_ID 0x%x\n", value);
522 pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_DRAWABLE_TYPE, &value);
523 TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
533 * Swap the buffers of this DC
535 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
537 ERR("No libGL on this box - disabling OpenGL support !\n");
541 TRACE("(%p)\n", physDev);
544 pglXSwapBuffers(gdi_display, physDev->drawable);
550 /***********************************************************************
551 * X11DRV_setup_opengl_visual
553 * Setup the default visual used for OpenGL and Direct3D, and the desktop
554 * window (if it exists). If OpenGL isn't available, the visual is simply
555 * set to the default visual for the display
557 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
559 XVisualInfo *visual = NULL;
560 /* In order to support OpenGL or D3D, we require a double-buffered visual and stencil buffer support, */
561 int dblBuf[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, GLX_DOUBLEBUFFER, None};
562 if (!has_opengl()) return NULL;
565 visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf);
567 if (visual == NULL) {
568 /* fallback to no stencil */
569 int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
570 WARN("Failed to get a visual with at least 8 bits of stencil\n");
573 visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
575 if (visual == NULL) {
576 /* This should only happen if we cannot find a match with a depth size 16 */
577 FIXME("Failed to find a suitable visual\n");
581 TRACE("Visual ID %lx Chosen\n",visual->visualid);
585 XID create_glxpixmap(X11DRV_PDEVICE *physDev)
589 XVisualInfo template;
593 template.visualid = XVisualIDFromVisual(visual);
594 vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
596 ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
599 TRACE("return %lx\n", ret);
603 BOOL destroy_glxpixmap(XID glxpixmap)
606 pglXDestroyGLXPixmap(gdi_display, glxpixmap);
611 #else /* no OpenGL includes */
613 void X11DRV_OpenGL_Init(Display *display)
617 /***********************************************************************
618 * ChoosePixelFormat (X11DRV.@)
620 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
621 const PIXELFORMATDESCRIPTOR *ppfd) {
622 ERR("No OpenGL support compiled in.\n");
627 /***********************************************************************
628 * DescribePixelFormat (X11DRV.@)
630 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
633 PIXELFORMATDESCRIPTOR *ppfd) {
634 ERR("No OpenGL support compiled in.\n");
639 /***********************************************************************
640 * GetPixelFormat (X11DRV.@)
642 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
643 ERR("No OpenGL support compiled in.\n");
648 /***********************************************************************
649 * SetPixelFormat (X11DRV.@)
651 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
653 const PIXELFORMATDESCRIPTOR *ppfd) {
654 ERR("No OpenGL support compiled in.\n");
659 /***********************************************************************
660 * SwapBuffers (X11DRV.@)
662 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
663 ERR("No OpenGL support compiled in.\n");
668 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
673 XID create_glxpixmap(X11DRV_PDEVICE *physDev)
678 BOOL destroy_glxpixmap(XID glxpixmap)
683 #endif /* defined(HAVE_OPENGL) */