1 /* Window-specific OpenGL functions implementation.
3 * Copyright (c) 1999 Lionel Ulmer
4 * Copyright (c) 2005 Raphael Junqueira
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
36 #include "opengl_ext.h"
42 #include "wine/gdi_driver.h"
43 #include "wine/library.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
47 WINE_DECLARE_DEBUG_CHANNEL(opengl);
51 PROC (WINAPI *p_wglGetProcAddress)(LPCSTR lpszProc);
52 INT (WINAPI *p_GetPixelFormat)(HDC hdc);
54 /* internal WGL functions */
55 void (WINAPI *p_wglFinish)(void);
56 void (WINAPI *p_wglFlush)(void);
57 HGLRC (WINAPI *p_wglGetCurrentContext)(void);
58 void (WINAPI *p_wglGetIntegerv)(GLenum pname, GLint* params);
61 static const struct wgl_funcs *wgl_driver;
64 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
65 MAKE_FUNCPTR(gluNewTess)
66 MAKE_FUNCPTR(gluDeleteTess)
67 MAKE_FUNCPTR(gluTessBeginContour)
68 MAKE_FUNCPTR(gluTessBeginPolygon)
69 MAKE_FUNCPTR(gluTessCallback)
70 MAKE_FUNCPTR(gluTessEndContour)
71 MAKE_FUNCPTR(gluTessEndPolygon)
72 MAKE_FUNCPTR(gluTessVertex)
74 #endif /* SONAME_LIBGLU */
76 static HMODULE opengl32_handle;
77 static void* libglu_handle = NULL;
79 const GLubyte * WINAPI wine_glGetString( GLenum name );
81 /* internal GDI functions */
82 extern INT WINAPI GdiDescribePixelFormat( HDC hdc, INT fmt, UINT size, PIXELFORMATDESCRIPTOR *pfd );
83 extern BOOL WINAPI GdiSetPixelFormat( HDC hdc, INT fmt, const PIXELFORMATDESCRIPTOR *pfd );
84 extern BOOL WINAPI GdiSwapBuffers( HDC hdc );
86 /***********************************************************************
87 * wglSetPixelFormat(OPENGL32.@)
89 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT iPixelFormat,
90 const PIXELFORMATDESCRIPTOR *ppfd)
92 return GdiSetPixelFormat(hdc, iPixelFormat, ppfd);
95 /***********************************************************************
96 * wglCopyContext (OPENGL32.@)
98 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
100 if (!hglrcSrc || !hglrcDst)
102 SetLastError(ERROR_INVALID_HANDLE);
105 return wgl_driver->p_wglCopyContext(hglrcSrc, hglrcDst, mask);
108 /***********************************************************************
109 * wglDeleteContext (OPENGL32.@)
111 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
115 SetLastError(ERROR_INVALID_HANDLE);
118 return wgl_driver->p_wglDeleteContext(hglrc);
121 /***********************************************************************
122 * wglMakeCurrent (OPENGL32.@)
124 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
126 if (!hglrc && !hdc && !NtCurrentTeb()->glContext)
128 SetLastError( ERROR_INVALID_HANDLE );
131 return wgl_driver->p_wglMakeCurrent(hdc, hglrc);
134 /***********************************************************************
135 * wglCreateContextAttribsARB (wrapper for the extension function returned by the driver)
137 static HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
139 return wgl_driver->p_wglCreateContextAttribsARB( hdc, share, attribs );
142 /***********************************************************************
143 * wglMakeContextCurrentARB (wrapper for the extension function returned by the driver)
145 static BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
147 return wgl_driver->p_wglMakeContextCurrentARB( draw_hdc, read_hdc, hglrc );
150 /***********************************************************************
151 * wglShareLists (OPENGL32.@)
153 BOOL WINAPI wglShareLists(HGLRC hglrc1, HGLRC hglrc2)
155 if (!hglrc1 || !hglrc2)
157 SetLastError(ERROR_INVALID_HANDLE);
160 return wgl_driver->p_wglShareLists(hglrc1, hglrc2);
163 /***********************************************************************
164 * wglGetCurrentDC (OPENGL32.@)
166 HDC WINAPI wglGetCurrentDC(void)
168 return wgl_driver->p_wglGetCurrentDC();
171 /***********************************************************************
172 * wglCreateContext (OPENGL32.@)
174 HGLRC WINAPI wglCreateContext(HDC hdc)
176 return wgl_driver->p_wglCreateContext(hdc);
179 /***********************************************************************
180 * wglGetCurrentContext (OPENGL32.@)
182 HGLRC WINAPI wglGetCurrentContext(void)
184 return wine_wgl.p_wglGetCurrentContext();
187 /***********************************************************************
188 * wglChoosePixelFormat (OPENGL32.@)
190 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
192 PIXELFORMATDESCRIPTOR format, best;
193 int i, count, best_format;
194 int bestDBuffer = -1, bestStereo = -1;
196 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
197 "accum %u depth %u stencil %u aux %u\n",
198 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
199 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
200 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
202 count = GdiDescribePixelFormat( hdc, 0, 0, NULL );
203 if (!count) return 0;
207 best.cAlphaBits = -1;
208 best.cColorBits = -1;
209 best.cDepthBits = -1;
210 best.cStencilBits = -1;
211 best.cAuxBuffers = -1;
213 for (i = 1; i <= count; i++)
215 if (!GdiDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
217 if (ppfd->iPixelType != format.iPixelType)
219 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
223 /* only use bitmap capable for formats for bitmap rendering */
224 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
226 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
230 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
231 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
232 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
233 * formats without the given flag set.
234 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
235 * has indicated that a format without stereo is returned when stereo is unavailable.
236 * So in case PFD_STEREO is set, formats that support it should have priority above formats
237 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
239 * To summarize the following is most likely the correct behavior:
240 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
241 * stereo set -> prefer stereo formats, but also accept non-stereo formats
242 * stereo don't care -> it doesn't matter whether we get stereo or not
244 * In Wine we will treat non-stereo the same way as don't care because it makes
245 * format selection even more complicated and second drivers with Stereo advertise
246 * each format twice anyway.
249 /* Doublebuffer, see the comments above */
250 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
252 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
253 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
256 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
259 /* Stereo, see the comments above. */
260 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
262 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
263 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
266 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
269 /* Below we will do a number of checks to select the 'best' pixelformat.
270 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
271 * The code works by trying to match the most important options as close as possible.
272 * When a reasonable format is found, we will try to match more options.
273 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
274 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
275 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
277 if (ppfd->cColorBits)
279 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
280 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
283 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
285 TRACE( "color mismatch for iPixelFormat=%d\n", i );
289 if (ppfd->cAlphaBits)
291 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
292 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
295 if (best.cAlphaBits != format.cAlphaBits)
297 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
301 if (ppfd->cDepthBits)
303 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
304 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
307 if (best.cDepthBits != format.cDepthBits)
309 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
313 if (ppfd->cStencilBits)
315 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
316 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
319 if (best.cStencilBits != format.cStencilBits)
321 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
325 if (ppfd->cAuxBuffers)
327 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
328 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
331 if (best.cAuxBuffers != format.cAuxBuffers)
333 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
342 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
343 bestStereo = format.dwFlags & PFD_STEREO;
346 TRACE( "returning %u\n", best_format );
350 /***********************************************************************
351 * wglDescribePixelFormat (OPENGL32.@)
353 INT WINAPI wglDescribePixelFormat(HDC hdc, INT iPixelFormat, UINT nBytes,
354 LPPIXELFORMATDESCRIPTOR ppfd)
356 return GdiDescribePixelFormat(hdc, iPixelFormat, nBytes, ppfd);
359 /***********************************************************************
360 * wglGetPixelFormat (OPENGL32.@)
362 INT WINAPI wglGetPixelFormat(HDC hdc)
364 return wine_wgl.p_GetPixelFormat(hdc);
367 /***********************************************************************
368 * wglCreateLayerContext (OPENGL32.@)
370 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
372 TRACE("(%p,%d)\n", hdc, iLayerPlane);
374 if (iLayerPlane == 0) {
375 return wglCreateContext(hdc);
377 FIXME("no handler for layer %d\n", iLayerPlane);
382 /***********************************************************************
383 * wglDescribeLayerPlane (OPENGL32.@)
385 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
389 LPLAYERPLANEDESCRIPTOR plpd) {
390 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
395 /***********************************************************************
396 * wglGetLayerPaletteEntries (OPENGL32.@)
398 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
402 const COLORREF *pcr) {
403 FIXME("(): stub!\n");
408 /* check if the extension is present in the list */
409 static BOOL has_extension( const char *list, const char *ext )
411 size_t len = strlen( ext );
415 while (*list == ' ') list++;
416 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
417 list = strchr( list, ' ' );
422 static int compar(const void *elt_a, const void *elt_b) {
423 return strcmp(((const OpenGL_extension *) elt_a)->name,
424 ((const OpenGL_extension *) elt_b)->name);
427 /* Check if a GL extension is supported */
428 static BOOL is_extension_supported(const char* extension)
430 const char *gl_ext_string = (const char*)wine_glGetString(GL_EXTENSIONS);
432 TRACE("Checking for extension '%s'\n", extension);
435 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
439 /* We use the GetProcAddress function from the display driver to retrieve function pointers
440 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
441 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
442 * require the function to return NULL when an extension isn't found. For this reason we check
443 * if the OpenGL extension required for the function we are looking up is supported. */
445 /* Check if the extension is part of the GL extension string to see if it is supported. */
446 if (has_extension(gl_ext_string, extension))
449 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
450 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
451 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
452 * Check if we are searching for a core GL function */
453 if(strncmp(extension, "GL_VERSION_", 11) == 0)
455 const GLubyte *gl_version = glGetString(GL_VERSION);
456 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
459 ERR("No OpenGL version found!\n");
463 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
464 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
465 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
468 WARN("The function requires OpenGL version '%c.%c' while your drivers only provide '%c.%c'\n", version[0], version[2], gl_version[0], gl_version[2]);
474 static const OpenGL_extension wgl_extensions[] =
476 { "wglCreateContextAttribsARB", "WGL_ARB_create_context", wglCreateContextAttribsARB },
477 { "wglMakeContextCurrentARB", "WGL_ARB_make_current_read", wglMakeContextCurrentARB },
480 /***********************************************************************
481 * wglGetProcAddress (OPENGL32.@)
483 PROC WINAPI wglGetProcAddress(LPCSTR lpszProc) {
485 OpenGL_extension ext;
486 const OpenGL_extension *ext_ret;
488 TRACE("(%s)\n", lpszProc);
490 if (lpszProc == NULL)
493 /* Without an active context opengl32 doesn't know to what
494 * driver it has to dispatch wglGetProcAddress.
496 if (wglGetCurrentContext() == NULL)
498 WARN("No active WGL context found\n");
502 /* Search in the thunks to find the real name of the extension */
504 ext_ret = bsearch(&ext, extension_registry, extension_registry_size,
505 sizeof(OpenGL_extension), compar);
507 /* If nothing was found, we are looking for a WGL extension or an unknown GL extension. */
508 if (ext_ret == NULL) {
509 /* If the function name starts with a 'w', it is a WGL extension */
510 if(lpszProc[0] == 'w')
512 local_func = wine_wgl.p_wglGetProcAddress( lpszProc );
513 if (local_func == (void *)1) /* special function that needs a wrapper */
515 ext_ret = bsearch( &ext, wgl_extensions, sizeof(wgl_extensions)/sizeof(wgl_extensions[0]),
516 sizeof(OpenGL_extension), compar );
517 if (ext_ret) return ext_ret->func;
519 FIXME( "wrapper missing for %s\n", lpszProc );
525 /* We are dealing with an unknown GL extension */
526 WARN("Extension '%s' not defined in opengl32.dll's function table!\n", lpszProc);
528 } else { /* We are looking for an OpenGL extension */
530 /* Check if the GL extension required by the function is available */
531 if(!is_extension_supported(ext_ret->extension)) {
532 WARN("Extension '%s' required by function '%s' not supported!\n", ext_ret->extension, lpszProc);
535 local_func = wine_wgl.p_wglGetProcAddress(ext_ret->name);
537 /* After that, look at the extensions defined in the Linux OpenGL library */
538 if (local_func == NULL) {
542 /* Remove the last 3 letters (EXT, ARB, ...).
544 I know that some extensions have more than 3 letters (MESA, NV,
545 INTEL, ...), but this is only a stop-gap measure to fix buggy
546 OpenGL drivers (moreover, it is only useful for old 1.0 apps
547 that query the glBindTextureEXT extension).
549 memcpy(buf, ext_ret->name, strlen(ext_ret->name) - 3);
550 buf[strlen(ext_ret->name) - 3] = '\0';
551 TRACE("Extension not found in the Linux OpenGL library, checking against libGL bug with %s..\n", buf);
553 ret = GetProcAddress(opengl32_handle, buf);
555 TRACE("Found function in main OpenGL library (%p)!\n", ret);
557 WARN("Did not find function %s (%s) in your OpenGL library!\n", lpszProc, ext_ret->name);
562 TRACE("returning function (%p)\n", ext_ret->func);
563 extension_funcs[ext_ret - extension_registry] = local_func;
565 return ext_ret->func;
570 /***********************************************************************
571 * wglRealizeLayerPalette (OPENGL32.@)
573 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
581 /***********************************************************************
582 * wglSetLayerPaletteEntries (OPENGL32.@)
584 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
588 const COLORREF *pcr) {
589 FIXME("(): stub!\n");
594 /***********************************************************************
595 * wglSwapLayerBuffers (OPENGL32.@)
597 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
599 TRACE_(opengl)("(%p, %08x)\n", hdc, fuPlanes);
601 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
602 if (!GdiSwapBuffers(hdc)) return FALSE;
603 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
607 WARN("Following layers unhandled: %08x\n", fuPlanes);
613 /***********************************************************************
614 * wglUseFontBitmaps_common
616 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
619 unsigned int glyph, size = 0;
620 void *bitmap = NULL, *gl_bitmap = NULL;
624 glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
625 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
627 for (glyph = first; glyph < first + count; glyph++) {
628 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
629 unsigned int needed_size, height, width, width_int;
632 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
634 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
636 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
637 if (needed_size == GDI_ERROR) {
642 if (needed_size > size) {
644 HeapFree(GetProcessHeap(), 0, bitmap);
645 HeapFree(GetProcessHeap(), 0, gl_bitmap);
646 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
647 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
650 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) != GDI_ERROR);
652 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) != GDI_ERROR);
656 unsigned int bitmask;
657 unsigned char *bitmap_ = bitmap;
659 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
660 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
661 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
662 if (needed_size != 0) {
663 TRACE(" - bitmap:\n");
664 for (height = 0; height < gm.gmBlackBoxY; height++) {
666 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
671 if (*bitmap_ & bitmask)
676 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
682 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
683 * glyph for it to be drawn properly.
685 if (needed_size != 0) {
686 width_int = (gm.gmBlackBoxX + 31) / 32;
687 for (height = 0; height < gm.gmBlackBoxY; height++) {
688 for (width = 0; width < width_int; width++) {
689 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
690 ((int *) bitmap)[height * width_int + width];
695 glNewList(listBase++, GL_COMPILE);
696 if (needed_size != 0) {
697 glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
698 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
699 gm.gmCellIncX, gm.gmCellIncY,
702 /* This is the case of 'empty' glyphs like the space character */
703 glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
708 glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
709 HeapFree(GetProcessHeap(), 0, bitmap);
710 HeapFree(GetProcessHeap(), 0, gl_bitmap);
714 /***********************************************************************
715 * wglUseFontBitmapsA (OPENGL32.@)
717 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
719 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
722 /***********************************************************************
723 * wglUseFontBitmapsW (OPENGL32.@)
725 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
727 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
732 static void *load_libglu(void)
734 static int already_loaded;
737 if (already_loaded) return libglu_handle;
740 TRACE("Trying to load GLU library: %s\n", SONAME_LIBGLU);
741 handle = wine_dlopen(SONAME_LIBGLU, RTLD_NOW, NULL, 0);
744 WARN("Failed to load %s\n", SONAME_LIBGLU);
748 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(handle, #f, NULL, 0)) == NULL) goto sym_not_found;
749 LOAD_FUNCPTR(gluNewTess)
750 LOAD_FUNCPTR(gluDeleteTess)
751 LOAD_FUNCPTR(gluTessBeginContour)
752 LOAD_FUNCPTR(gluTessBeginPolygon)
753 LOAD_FUNCPTR(gluTessCallback)
754 LOAD_FUNCPTR(gluTessEndContour)
755 LOAD_FUNCPTR(gluTessEndPolygon)
756 LOAD_FUNCPTR(gluTessVertex)
758 libglu_handle = handle;
762 WARN("Unable to load function ptrs from libGLU\n");
763 /* Close the library as we won't use it */
764 wine_dlclose(handle, NULL, 0);
768 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
770 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
771 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
775 static void tess_callback_vertex(GLvoid *vertex)
777 GLdouble *dbl = vertex;
778 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
782 static void tess_callback_begin(GLenum which)
784 TRACE("%d\n", which);
788 static void tess_callback_end(void)
794 /***********************************************************************
795 * wglUseFontOutlines_common
797 static BOOL wglUseFontOutlines_common(HDC hdc,
804 LPGLYPHMETRICSFLOAT lpgmf,
808 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
811 HFONT old_font, unscaled_font;
815 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
816 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
820 ERR("libGLU is required for this function but isn't loaded\n");
824 tess = pgluNewTess();
825 if(!tess) return FALSE;
826 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
827 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
828 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
830 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
831 rc.left = rc.right = rc.bottom = 0;
833 DPtoLP(hdc, (POINT*)&rc, 2);
834 lf.lfHeight = -abs(rc.top - rc.bottom);
835 lf.lfOrientation = lf.lfEscapement = 0;
836 unscaled_font = CreateFontIndirectW(&lf);
837 old_font = SelectObject(hdc, unscaled_font);
839 for (glyph = first; glyph < first + count; glyph++)
844 TTPOLYGONHEADER *pph;
849 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
851 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
853 if(needed == GDI_ERROR)
856 buf = HeapAlloc(GetProcessHeap(), 0, needed);
857 vertices = HeapAlloc(GetProcessHeap(), 0, needed / sizeof(POINTFX) * 3 * sizeof(GLdouble));
860 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
862 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
864 TRACE("glyph %d\n", glyph);
868 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
869 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
870 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
871 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
872 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
873 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
875 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
876 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
880 glNewList(listBase++, GL_COMPILE);
881 pgluTessBeginPolygon(tess, NULL);
883 pph = (TTPOLYGONHEADER*)buf;
884 while((BYTE*)pph < buf + needed)
886 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
888 pgluTessBeginContour(tess);
890 fixed_to_double(pph->pfxStart, em_size, vertices);
891 pgluTessVertex(tess, vertices, vertices);
894 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
895 while((char*)ppc < (char*)pph + pph->cb)
901 for(i = 0; i < ppc->cpfx; i++)
903 TRACE("\t\tline to %d, %d\n", ppc->apfx[i].x.value, ppc->apfx[i].y.value);
904 fixed_to_double(ppc->apfx[i], em_size, vertices);
905 pgluTessVertex(tess, vertices, vertices);
910 case TT_PRIM_QSPLINE:
911 for(i = 0; i < ppc->cpfx/2; i++)
913 /* FIXME: just connecting the control points for now */
914 TRACE("\t\tcurve %d,%d %d,%d\n",
915 ppc->apfx[i * 2].x.value, ppc->apfx[i * 3].y.value,
916 ppc->apfx[i * 2 + 1].x.value, ppc->apfx[i * 3 + 1].y.value);
917 fixed_to_double(ppc->apfx[i * 2], em_size, vertices);
918 pgluTessVertex(tess, vertices, vertices);
920 fixed_to_double(ppc->apfx[i * 2 + 1], em_size, vertices);
921 pgluTessVertex(tess, vertices, vertices);
926 ERR("\t\tcurve type = %d\n", ppc->wType);
927 pgluTessEndContour(tess);
931 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
932 (ppc->cpfx - 1) * sizeof(POINTFX));
934 pgluTessEndContour(tess);
935 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
939 pgluTessEndPolygon(tess);
940 glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
942 HeapFree(GetProcessHeap(), 0, buf);
943 HeapFree(GetProcessHeap(), 0, vertices);
947 DeleteObject(SelectObject(hdc, old_font));
948 pgluDeleteTess(tess);
953 #else /* SONAME_LIBGLU */
955 static BOOL wglUseFontOutlines_common(HDC hdc,
962 LPGLYPHMETRICSFLOAT lpgmf,
965 FIXME("Unable to compile in wglUseFontOutlines support without GL/glu.h\n");
969 #endif /* SONAME_LIBGLU */
971 /***********************************************************************
972 * wglUseFontOutlinesA (OPENGL32.@)
974 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
981 LPGLYPHMETRICSFLOAT lpgmf)
983 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
986 /***********************************************************************
987 * wglUseFontOutlinesW (OPENGL32.@)
989 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
996 LPGLYPHMETRICSFLOAT lpgmf)
998 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1001 /***********************************************************************
1002 * glDebugEntry (OPENGL32.@)
1004 GLint WINAPI wine_glDebugEntry( GLint unknown1, GLint unknown2 )
1009 /***********************************************************************
1010 * glFinish (OPENGL32.@)
1012 void WINAPI wine_glFinish( void )
1015 wine_wgl.p_wglFinish();
1018 /***********************************************************************
1019 * glFlush (OPENGL32.@)
1021 void WINAPI wine_glFlush( void )
1024 wine_wgl.p_wglFlush();
1027 /* build the extension string by filtering out the disabled extensions */
1028 static char *build_gl_extensions( const char *extensions )
1030 char *p, *str, *disabled = NULL;
1034 TRACE( "GL_EXTENSIONS:\n" );
1036 if (!extensions) extensions = "";
1038 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1039 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1041 DWORD size, ret = RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size );
1042 if (!ret && (disabled = HeapAlloc( GetProcessHeap(), 0, size )))
1043 ret = RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)disabled, &size );
1044 RegCloseKey( hkey );
1045 if (ret) *disabled = 0;
1048 if ((str = HeapAlloc( GetProcessHeap(), 0, strlen(extensions) + 2 )))
1053 while (*extensions == ' ') extensions++;
1054 if (!*extensions) break;
1055 if (!(end = strchr( extensions, ' ' ))) end = extensions + strlen( extensions );
1056 memcpy( p, extensions, end - extensions );
1057 p[end - extensions] = 0;
1058 if (!has_extension( disabled, p ))
1060 TRACE("++ %s\n", p );
1061 p += end - extensions;
1064 else TRACE("-- %s (disabled by config)\n", p );
1069 HeapFree( GetProcessHeap(), 0, disabled );
1073 /***********************************************************************
1074 * glGetString (OPENGL32.@)
1076 const GLubyte * WINAPI wine_glGetString( GLenum name )
1078 static const GLubyte *gl_extensions;
1080 /* this is for buggy nvidia driver, crashing if called from a different
1081 thread with no context */
1082 if(wglGetCurrentContext() == NULL)
1085 if (name != GL_EXTENSIONS) return glGetString(name);
1089 const char *orig_ext = (const char *)glGetString(GL_EXTENSIONS);
1090 char *new_ext = build_gl_extensions( orig_ext );
1091 if (InterlockedCompareExchangePointer( (void **)&gl_extensions, new_ext, NULL ))
1092 HeapFree( GetProcessHeap(), 0, new_ext );
1094 return gl_extensions;
1097 /***********************************************************************
1098 * glGetIntegerv (OPENGL32.@)
1100 void WINAPI wine_glGetIntegerv( GLenum pname, GLint* params )
1102 wine_wgl.p_wglGetIntegerv(pname, params);
1105 /***********************************************************************
1106 * wglSwapBuffers (OPENGL32.@)
1108 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
1110 return GdiSwapBuffers(hdc);
1113 /* This is for brain-dead applications that use OpenGL functions before even
1114 creating a rendering context.... */
1115 static BOOL process_attach(void)
1118 HDC hdc = GetDC( 0 );
1120 wgl_driver = __wine_get_wgl_driver( hdc, WINE_GDI_DRIVER_VERSION );
1121 ReleaseDC( 0, hdc );
1123 mod_gdi32 = GetModuleHandleA( "gdi32.dll" );
1127 ERR("GDI32 not loaded. Cannot create default context.\n");
1131 wine_wgl.p_wglGetProcAddress = (void *)GetProcAddress(mod_gdi32, "wglGetProcAddress");
1132 wine_wgl.p_GetPixelFormat = (void *)GetProcAddress(mod_gdi32, "GetPixelFormat");
1134 /* internal WGL functions */
1135 wine_wgl.p_wglFinish = (void *)wine_wgl.p_wglGetProcAddress("wglFinish");
1136 wine_wgl.p_wglFlush = (void *)wine_wgl.p_wglGetProcAddress("wglFlush");
1137 wine_wgl.p_wglGetCurrentContext = (void *)wine_wgl.p_wglGetProcAddress("wglGetCurrentContext");
1138 wine_wgl.p_wglGetIntegerv = (void *)wine_wgl.p_wglGetProcAddress("wglGetIntegerv");
1143 /**********************************************************************/
1145 static void process_detach(void)
1147 if (libglu_handle) wine_dlclose(libglu_handle, NULL, 0);
1150 /***********************************************************************
1151 * OpenGL initialisation routine
1153 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1157 case DLL_PROCESS_ATTACH:
1158 opengl32_handle = hinst;
1159 DisableThreadLibraryCalls(hinst);
1160 return process_attach();
1161 case DLL_PROCESS_DETACH: