opengl32: Store the context current DCs on the opengl32 side.
[wine/multimedia.git] / dlls / opengl32 / wgl.c
blob84b37c82b8a18fbd80649ab50d9a88a0e86cb217
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "wingdi.h"
33 #include "winternl.h"
34 #include "winnt.h"
36 #include "opengl_ext.h"
37 #define WGL_WGLEXT_PROTOTYPES
38 #include "wine/wglext.h"
39 #include "wine/gdi_driver.h"
40 #include "wine/wgl_driver.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
44 WINE_DECLARE_DEBUG_CHANNEL(opengl);
46 static HMODULE opengl32_handle;
48 extern struct opengl_funcs null_opengl_funcs;
50 const GLubyte * WINAPI wine_glGetString( GLenum name );
52 /* internal GDI functions */
53 extern INT WINAPI GdiDescribePixelFormat( HDC hdc, INT fmt, UINT size, PIXELFORMATDESCRIPTOR *pfd );
54 extern BOOL WINAPI GdiSetPixelFormat( HDC hdc, INT fmt, const PIXELFORMATDESCRIPTOR *pfd );
55 extern BOOL WINAPI GdiSwapBuffers( HDC hdc );
57 /* handle management */
59 #define MAX_WGL_HANDLES 1024
61 enum wgl_handle_type
63 HANDLE_CONTEXT = 0 << 12,
64 HANDLE_PBUFFER = 1 << 12,
65 HANDLE_TYPE_MASK = 15 << 12
68 struct opengl_context
70 DWORD tid; /* thread that the context is current in */
71 HDC draw_dc; /* current drawing DC */
72 HDC read_dc; /* current reading DC */
73 struct wgl_context *drv_ctx; /* driver context */
76 struct wgl_handle
78 UINT handle;
79 struct opengl_funcs *funcs;
80 union
82 struct opengl_context *context; /* for HANDLE_CONTEXT */
83 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
84 struct wgl_handle *next; /* for free handles */
85 } u;
88 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
89 static struct wgl_handle *next_free;
90 static unsigned int handle_count;
92 static CRITICAL_SECTION wgl_section;
93 static CRITICAL_SECTION_DEBUG critsect_debug =
95 0, 0, &wgl_section,
96 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
97 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
99 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
101 static inline struct opengl_funcs *get_dc_funcs( HDC hdc )
103 struct opengl_funcs *funcs = __wine_get_wgl_driver( hdc, WINE_WGL_DRIVER_VERSION );
104 if (funcs == (void *)-1) funcs = &null_opengl_funcs;
105 return funcs;
108 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
110 WORD generation = HIWORD( ptr->handle ) + 1;
111 if (!generation) generation++;
112 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
113 return ULongToHandle( ptr->handle );
116 /* the current context is assumed valid and doesn't need locking */
117 static inline struct wgl_handle *get_current_context_ptr(void)
119 if (!NtCurrentTeb()->glCurrentRC) return NULL;
120 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
123 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
125 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
127 EnterCriticalSection( &wgl_section );
128 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
129 return &wgl_handles[index];
131 LeaveCriticalSection( &wgl_section );
132 SetLastError( ERROR_INVALID_HANDLE );
133 return NULL;
136 static void release_handle_ptr( struct wgl_handle *ptr )
138 if (ptr) LeaveCriticalSection( &wgl_section );
141 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
143 HANDLE handle = 0;
144 struct wgl_handle *ptr = NULL;
146 EnterCriticalSection( &wgl_section );
147 if ((ptr = next_free))
148 next_free = next_free->u.next;
149 else if (handle_count < MAX_WGL_HANDLES)
150 ptr = &wgl_handles[handle_count++];
152 if (ptr)
154 ptr->funcs = funcs;
155 ptr->u.context = user_ptr;
156 handle = next_handle( ptr, type );
158 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
159 LeaveCriticalSection( &wgl_section );
160 return handle;
163 static void free_handle_ptr( struct wgl_handle *ptr )
165 ptr->handle |= 0xffff;
166 ptr->u.next = next_free;
167 ptr->funcs = NULL;
168 next_free = ptr;
169 LeaveCriticalSection( &wgl_section );
172 /***********************************************************************
173 * wglSetPixelFormat(OPENGL32.@)
175 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT iPixelFormat,
176 const PIXELFORMATDESCRIPTOR *ppfd)
178 return GdiSetPixelFormat(hdc, iPixelFormat, ppfd);
181 /***********************************************************************
182 * wglCopyContext (OPENGL32.@)
184 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
186 struct wgl_handle *src, *dst;
187 BOOL ret = FALSE;
189 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
190 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
192 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
193 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
194 dst->u.context->drv_ctx, mask );
196 release_handle_ptr( dst );
197 release_handle_ptr( src );
198 return ret;
201 /***********************************************************************
202 * wglDeleteContext (OPENGL32.@)
204 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
206 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
208 if (!ptr) return FALSE;
210 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
212 SetLastError( ERROR_BUSY );
213 release_handle_ptr( ptr );
214 return FALSE;
216 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
217 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
218 HeapFree( GetProcessHeap(), 0, ptr->u.context );
219 free_handle_ptr( ptr );
220 return TRUE;
223 /***********************************************************************
224 * wglMakeCurrent (OPENGL32.@)
226 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
228 BOOL ret = TRUE;
229 struct wgl_handle *ptr, *prev = get_current_context_ptr();
231 if (hglrc)
233 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
234 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
236 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
237 if (ret)
239 if (prev) prev->u.context->tid = 0;
240 ptr->u.context->tid = GetCurrentThreadId();
241 ptr->u.context->draw_dc = hdc;
242 ptr->u.context->read_dc = hdc;
243 NtCurrentTeb()->glCurrentRC = hglrc;
244 NtCurrentTeb()->glTable = ptr->funcs;
247 else
249 SetLastError( ERROR_BUSY );
250 ret = FALSE;
252 release_handle_ptr( ptr );
254 else if (prev)
256 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
257 prev->u.context->tid = 0;
258 NtCurrentTeb()->glCurrentRC = 0;
259 NtCurrentTeb()->glTable = &null_opengl_funcs;
261 else if (!hdc)
263 SetLastError( ERROR_INVALID_HANDLE );
264 ret = FALSE;
266 return ret;
269 /***********************************************************************
270 * wglCreateContextAttribsARB
272 * Provided by the WGL_ARB_create_context extension.
274 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
276 HGLRC ret = 0;
277 struct wgl_context *drv_ctx;
278 struct wgl_handle *share_ptr = NULL;
279 struct opengl_context *context;
280 struct opengl_funcs *funcs = get_dc_funcs( hdc );
282 if (!funcs || !funcs->ext.p_wglCreateContextAttribsARB) return 0;
283 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT ))) return 0;
284 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
285 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
287 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
289 context->drv_ctx = drv_ctx;
290 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
291 HeapFree( GetProcessHeap(), 0, context );
293 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
295 release_handle_ptr( share_ptr );
296 return ret;
300 /***********************************************************************
301 * wglMakeContextCurrentARB
303 * Provided by the WGL_ARB_make_current_read extension.
305 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
307 BOOL ret = TRUE;
308 struct wgl_handle *ptr, *prev = get_current_context_ptr();
310 if (hglrc)
312 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
313 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
315 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
316 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
317 ptr->u.context->drv_ctx ));
318 if (ret)
320 if (prev) prev->u.context->tid = 0;
321 ptr->u.context->tid = GetCurrentThreadId();
322 ptr->u.context->draw_dc = draw_hdc;
323 ptr->u.context->read_dc = read_hdc;
324 NtCurrentTeb()->glCurrentRC = hglrc;
325 NtCurrentTeb()->glTable = ptr->funcs;
328 else
330 SetLastError( ERROR_BUSY );
331 ret = FALSE;
333 release_handle_ptr( ptr );
335 else if (prev)
337 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
338 prev->u.context->tid = 0;
339 NtCurrentTeb()->glCurrentRC = 0;
340 NtCurrentTeb()->glTable = &null_opengl_funcs;
342 return ret;
345 /***********************************************************************
346 * wglGetCurrentReadDCARB
348 * Provided by the WGL_ARB_make_current_read extension.
350 HDC WINAPI wglGetCurrentReadDCARB(void)
352 struct wgl_handle *ptr = get_current_context_ptr();
354 if (!ptr) return 0;
355 return ptr->u.context->read_dc;
358 /***********************************************************************
359 * wglShareLists (OPENGL32.@)
361 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
363 BOOL ret = FALSE;
364 struct wgl_handle *src, *dst;
366 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
367 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
369 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
370 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
372 release_handle_ptr( dst );
373 release_handle_ptr( src );
374 return ret;
377 /***********************************************************************
378 * wglGetCurrentDC (OPENGL32.@)
380 HDC WINAPI wglGetCurrentDC(void)
382 struct wgl_handle *ptr = get_current_context_ptr();
384 if (!ptr) return 0;
385 return ptr->u.context->draw_dc;
388 /***********************************************************************
389 * wglCreateContext (OPENGL32.@)
391 HGLRC WINAPI wglCreateContext(HDC hdc)
393 HGLRC ret = 0;
394 struct wgl_context *drv_ctx;
395 struct opengl_context *context;
396 struct opengl_funcs *funcs = get_dc_funcs( hdc );
398 if (!funcs) return 0;
399 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
400 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
402 context->drv_ctx = drv_ctx;
403 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
404 HeapFree( GetProcessHeap(), 0, context );
406 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
407 return ret;
410 /***********************************************************************
411 * wglGetCurrentContext (OPENGL32.@)
413 HGLRC WINAPI wglGetCurrentContext(void)
415 return NtCurrentTeb()->glCurrentRC;
418 /***********************************************************************
419 * wglChoosePixelFormat (OPENGL32.@)
421 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
423 PIXELFORMATDESCRIPTOR format, best;
424 int i, count, best_format;
425 int bestDBuffer = -1, bestStereo = -1;
427 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
428 "accum %u depth %u stencil %u aux %u\n",
429 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
430 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
431 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
433 count = GdiDescribePixelFormat( hdc, 0, 0, NULL );
434 if (!count) return 0;
436 best_format = 0;
437 best.dwFlags = 0;
438 best.cAlphaBits = -1;
439 best.cColorBits = -1;
440 best.cDepthBits = -1;
441 best.cStencilBits = -1;
442 best.cAuxBuffers = -1;
444 for (i = 1; i <= count; i++)
446 if (!GdiDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
448 if (ppfd->iPixelType != format.iPixelType)
450 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
451 continue;
454 /* only use bitmap capable for formats for bitmap rendering */
455 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
457 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
458 continue;
461 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
462 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
463 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
464 * formats without the given flag set.
465 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
466 * has indicated that a format without stereo is returned when stereo is unavailable.
467 * So in case PFD_STEREO is set, formats that support it should have priority above formats
468 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
470 * To summarize the following is most likely the correct behavior:
471 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
472 * stereo set -> prefer stereo formats, but also accept non-stereo formats
473 * stereo don't care -> it doesn't matter whether we get stereo or not
475 * In Wine we will treat non-stereo the same way as don't care because it makes
476 * format selection even more complicated and second drivers with Stereo advertise
477 * each format twice anyway.
480 /* Doublebuffer, see the comments above */
481 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
483 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
484 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
485 goto found;
487 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
490 /* Stereo, see the comments above. */
491 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
493 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
494 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
495 goto found;
497 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
500 /* Below we will do a number of checks to select the 'best' pixelformat.
501 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
502 * The code works by trying to match the most important options as close as possible.
503 * When a reasonable format is found, we will try to match more options.
504 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
505 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
506 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
508 if (ppfd->cColorBits)
510 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
511 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
512 goto found;
514 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
516 TRACE( "color mismatch for iPixelFormat=%d\n", i );
517 continue;
520 if (ppfd->cAlphaBits)
522 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
523 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
524 goto found;
526 if (best.cAlphaBits != format.cAlphaBits)
528 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
529 continue;
532 if (ppfd->cDepthBits)
534 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
535 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
536 goto found;
538 if (best.cDepthBits != format.cDepthBits)
540 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
541 continue;
544 if (ppfd->cStencilBits)
546 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
547 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
548 goto found;
550 if (best.cStencilBits != format.cStencilBits)
552 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
553 continue;
556 if (ppfd->cAuxBuffers)
558 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
559 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
560 goto found;
562 if (best.cAuxBuffers != format.cAuxBuffers)
564 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
565 continue;
568 continue;
570 found:
571 best_format = i;
572 best = format;
573 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
574 bestStereo = format.dwFlags & PFD_STEREO;
577 TRACE( "returning %u\n", best_format );
578 return best_format;
581 /***********************************************************************
582 * wglDescribePixelFormat (OPENGL32.@)
584 INT WINAPI wglDescribePixelFormat(HDC hdc, INT iPixelFormat, UINT nBytes,
585 LPPIXELFORMATDESCRIPTOR ppfd)
587 return GdiDescribePixelFormat(hdc, iPixelFormat, nBytes, ppfd);
590 /***********************************************************************
591 * wglGetPixelFormat (OPENGL32.@)
593 INT WINAPI wglGetPixelFormat(HDC hdc)
595 struct opengl_funcs *funcs = get_dc_funcs( hdc );
596 if (!funcs) return 0;
597 return funcs->wgl.p_wglGetPixelFormat( hdc );
600 /***********************************************************************
601 * wglCreateLayerContext (OPENGL32.@)
603 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
604 int iLayerPlane) {
605 TRACE("(%p,%d)\n", hdc, iLayerPlane);
607 if (iLayerPlane == 0) {
608 return wglCreateContext(hdc);
610 FIXME("no handler for layer %d\n", iLayerPlane);
612 return NULL;
615 /***********************************************************************
616 * wglDescribeLayerPlane (OPENGL32.@)
618 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
619 int iPixelFormat,
620 int iLayerPlane,
621 UINT nBytes,
622 LPLAYERPLANEDESCRIPTOR plpd) {
623 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
625 return FALSE;
628 /***********************************************************************
629 * wglGetLayerPaletteEntries (OPENGL32.@)
631 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
632 int iLayerPlane,
633 int iStart,
634 int cEntries,
635 const COLORREF *pcr) {
636 FIXME("(): stub!\n");
638 return 0;
641 /* check if the extension is present in the list */
642 static BOOL has_extension( const char *list, const char *ext )
644 size_t len = strlen( ext );
646 while (list)
648 while (*list == ' ') list++;
649 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
650 list = strchr( list, ' ' );
652 return FALSE;
655 static int compar(const void *elt_a, const void *elt_b) {
656 return strcmp(((const OpenGL_extension *) elt_a)->name,
657 ((const OpenGL_extension *) elt_b)->name);
660 /* Check if a GL extension is supported */
661 static BOOL is_extension_supported(const char* extension)
663 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
664 const char *gl_ext_string = (const char*)wine_glGetString(GL_EXTENSIONS);
666 TRACE("Checking for extension '%s'\n", extension);
668 if(!gl_ext_string) {
669 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
670 return FALSE;
673 /* We use the GetProcAddress function from the display driver to retrieve function pointers
674 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
675 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
676 * require the function to return NULL when an extension isn't found. For this reason we check
677 * if the OpenGL extension required for the function we are looking up is supported. */
679 /* Check if the extension is part of the GL extension string to see if it is supported. */
680 if (has_extension(gl_ext_string, extension))
681 return TRUE;
683 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
684 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
685 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
686 * Check if we are searching for a core GL function */
687 if(strncmp(extension, "GL_VERSION_", 11) == 0)
689 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
690 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
692 if(!gl_version) {
693 ERR("No OpenGL version found!\n");
694 return FALSE;
697 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
698 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
699 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
700 return TRUE;
702 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]);
705 return FALSE;
708 /***********************************************************************
709 * wglGetProcAddress (OPENGL32.@)
711 PROC WINAPI wglGetProcAddress(LPCSTR lpszProc)
713 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
714 void **func_ptr;
715 void *local_func;
716 OpenGL_extension ext;
717 const OpenGL_extension *ext_ret;
718 struct wgl_handle *context = get_current_context_ptr();
720 TRACE("(%s)\n", lpszProc);
722 if (lpszProc == NULL)
723 return NULL;
725 /* Without an active context opengl32 doesn't know to what
726 * driver it has to dispatch wglGetProcAddress.
728 if (!context)
730 WARN("No active WGL context found\n");
731 return NULL;
734 /* Search in the thunks to find the real name of the extension */
735 ext.name = lpszProc;
736 ext_ret = bsearch(&ext, extension_registry, extension_registry_size,
737 sizeof(OpenGL_extension), compar);
738 if (!ext_ret)
740 WARN("Extension '%s' not defined in opengl32.dll's function table!\n", lpszProc);
741 return NULL;
744 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
745 if (!*func_ptr)
747 /* Check if the GL extension required by the function is available */
748 if(!is_extension_supported(ext_ret->extension)) {
749 WARN("Extension '%s' required by function '%s' not supported!\n", ext_ret->extension, lpszProc);
752 local_func = context->funcs->wgl.p_wglGetProcAddress( ext_ret->name );
754 /* After that, look at the extensions defined in the Linux OpenGL library */
755 if (local_func == NULL) {
756 char buf[256];
757 void *ret = NULL;
759 /* Remove the last 3 letters (EXT, ARB, ...).
761 I know that some extensions have more than 3 letters (MESA, NV,
762 INTEL, ...), but this is only a stop-gap measure to fix buggy
763 OpenGL drivers (moreover, it is only useful for old 1.0 apps
764 that query the glBindTextureEXT extension).
766 memcpy(buf, ext_ret->name, strlen(ext_ret->name) - 3);
767 buf[strlen(ext_ret->name) - 3] = '\0';
768 TRACE("Extension not found in the Linux OpenGL library, checking against libGL bug with %s..\n", buf);
770 ret = GetProcAddress(opengl32_handle, buf);
771 if (ret != NULL) {
772 TRACE("Found function in main OpenGL library (%p)!\n", ret);
773 } else {
774 WARN("Did not find function %s (%s) in your OpenGL library!\n", lpszProc, ext_ret->name);
777 return ret;
779 *func_ptr = local_func;
782 TRACE("returning function (%p)\n", ext_ret->func);
783 return ext_ret->func;
786 /***********************************************************************
787 * wglRealizeLayerPalette (OPENGL32.@)
789 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
790 int iLayerPlane,
791 BOOL bRealize) {
792 FIXME("()\n");
794 return FALSE;
797 /***********************************************************************
798 * wglSetLayerPaletteEntries (OPENGL32.@)
800 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
801 int iLayerPlane,
802 int iStart,
803 int cEntries,
804 const COLORREF *pcr) {
805 FIXME("(): stub!\n");
807 return 0;
810 /***********************************************************************
811 * wglSwapLayerBuffers (OPENGL32.@)
813 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
814 UINT fuPlanes) {
815 TRACE_(opengl)("(%p, %08x)\n", hdc, fuPlanes);
817 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
818 if (!GdiSwapBuffers(hdc)) return FALSE;
819 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
822 if (fuPlanes) {
823 WARN("Following layers unhandled: %08x\n", fuPlanes);
826 return TRUE;
829 /***********************************************************************
830 * wglAllocateMemoryNV
832 * Provided by the WGL_NV_vertex_array_range extension.
834 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
836 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
838 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
839 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
842 /***********************************************************************
843 * wglFreeMemoryNV
845 * Provided by the WGL_NV_vertex_array_range extension.
847 void WINAPI wglFreeMemoryNV( void *pointer )
849 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
851 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
854 /***********************************************************************
855 * wglBindTexImageARB
857 * Provided by the WGL_ARB_render_texture extension.
859 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
861 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
862 BOOL ret;
864 if (!ptr) return FALSE;
865 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
866 release_handle_ptr( ptr );
867 return ret;
870 /***********************************************************************
871 * wglReleaseTexImageARB
873 * Provided by the WGL_ARB_render_texture extension.
875 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
877 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
878 BOOL ret;
880 if (!ptr) return FALSE;
881 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
882 release_handle_ptr( ptr );
883 return ret;
886 /***********************************************************************
887 * wglSetPbufferAttribARB
889 * Provided by the WGL_ARB_render_texture extension.
891 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
893 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
894 BOOL ret;
896 if (!ptr) return FALSE;
897 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
898 release_handle_ptr( ptr );
899 return ret;
902 /***********************************************************************
903 * wglChoosePixelFormatARB
905 * Provided by the WGL_ARB_pixel_format extension.
907 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
908 UINT max, int *formats, UINT *count )
910 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
912 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
913 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
916 /***********************************************************************
917 * wglGetPixelFormatAttribivARB
919 * Provided by the WGL_ARB_pixel_format extension.
921 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
922 int *values )
924 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
926 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
927 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
930 /***********************************************************************
931 * wglGetPixelFormatAttribfvARB
933 * Provided by the WGL_ARB_pixel_format extension.
935 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
936 FLOAT *values )
938 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
940 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
941 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
944 /***********************************************************************
945 * wglCreatePbufferARB
947 * Provided by the WGL_ARB_pbuffer extension.
949 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
951 HPBUFFERARB ret = 0;
952 struct wgl_pbuffer *pbuffer;
953 struct opengl_funcs *funcs = get_dc_funcs( hdc );
955 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
956 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
957 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
958 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
959 return ret;
962 /***********************************************************************
963 * wglGetPbufferDCARB
965 * Provided by the WGL_ARB_pbuffer extension.
967 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
969 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
970 HDC ret;
972 if (!ptr) return 0;
973 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
974 release_handle_ptr( ptr );
975 return ret;
978 /***********************************************************************
979 * wglReleasePbufferDCARB
981 * Provided by the WGL_ARB_pbuffer extension.
983 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
985 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
986 BOOL ret;
988 if (!ptr) return FALSE;
989 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
990 release_handle_ptr( ptr );
991 return ret;
994 /***********************************************************************
995 * wglDestroyPbufferARB
997 * Provided by the WGL_ARB_pbuffer extension.
999 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1001 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1003 if (!ptr) return FALSE;
1004 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1005 free_handle_ptr( ptr );
1006 return TRUE;
1009 /***********************************************************************
1010 * wglQueryPbufferARB
1012 * Provided by the WGL_ARB_pbuffer extension.
1014 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1016 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1017 BOOL ret;
1019 if (!ptr) return FALSE;
1020 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1021 release_handle_ptr( ptr );
1022 return ret;
1025 /***********************************************************************
1026 * wglGetExtensionsStringARB
1028 * Provided by the WGL_ARB_extensions_string extension.
1030 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1032 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1034 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1035 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1038 /***********************************************************************
1039 * wglGetExtensionsStringEXT
1041 * Provided by the WGL_EXT_extensions_string extension.
1043 const char * WINAPI wglGetExtensionsStringEXT(void)
1045 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1047 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1048 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1051 /***********************************************************************
1052 * wglSwapIntervalEXT
1054 * Provided by the WGL_EXT_swap_control extension.
1056 BOOL WINAPI wglSwapIntervalEXT( int interval )
1058 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1060 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1061 return funcs->ext.p_wglSwapIntervalEXT( interval );
1064 /***********************************************************************
1065 * wglGetSwapIntervalEXT
1067 * Provided by the WGL_EXT_swap_control extension.
1069 int WINAPI wglGetSwapIntervalEXT(void)
1071 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1073 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1074 return funcs->ext.p_wglGetSwapIntervalEXT();
1077 /***********************************************************************
1078 * wglSetPixelFormatWINE
1080 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1082 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1084 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1086 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1087 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1090 /***********************************************************************
1091 * wglUseFontBitmaps_common
1093 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1095 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1096 GLYPHMETRICS gm;
1097 unsigned int glyph, size = 0;
1098 void *bitmap = NULL, *gl_bitmap = NULL;
1099 int org_alignment;
1100 BOOL ret = TRUE;
1102 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1103 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1105 for (glyph = first; glyph < first + count; glyph++) {
1106 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1107 unsigned int needed_size, height, width, width_int;
1109 if (unicode)
1110 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1111 else
1112 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1114 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1115 if (needed_size == GDI_ERROR) {
1116 ret = FALSE;
1117 break;
1120 if (needed_size > size) {
1121 size = needed_size;
1122 HeapFree(GetProcessHeap(), 0, bitmap);
1123 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1124 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1125 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1127 if (unicode)
1128 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) != GDI_ERROR);
1129 else
1130 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) != GDI_ERROR);
1131 if (!ret) break;
1133 if (TRACE_ON(wgl)) {
1134 unsigned int bitmask;
1135 unsigned char *bitmap_ = bitmap;
1137 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1138 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1139 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1140 if (needed_size != 0) {
1141 TRACE(" - bitmap:\n");
1142 for (height = 0; height < gm.gmBlackBoxY; height++) {
1143 TRACE(" ");
1144 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1145 if (bitmask == 0) {
1146 bitmap_ += 1;
1147 bitmask = 0x80;
1149 if (*bitmap_ & bitmask)
1150 TRACE("*");
1151 else
1152 TRACE(" ");
1154 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1155 TRACE("\n");
1160 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1161 * glyph for it to be drawn properly.
1163 if (needed_size != 0) {
1164 width_int = (gm.gmBlackBoxX + 31) / 32;
1165 for (height = 0; height < gm.gmBlackBoxY; height++) {
1166 for (width = 0; width < width_int; width++) {
1167 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1168 ((int *) bitmap)[height * width_int + width];
1173 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1174 if (needed_size != 0) {
1175 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1176 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1177 gm.gmCellIncX, gm.gmCellIncY,
1178 gl_bitmap);
1179 } else {
1180 /* This is the case of 'empty' glyphs like the space character */
1181 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1183 funcs->gl.p_glEndList();
1186 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1187 HeapFree(GetProcessHeap(), 0, bitmap);
1188 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1189 return ret;
1192 /***********************************************************************
1193 * wglUseFontBitmapsA (OPENGL32.@)
1195 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1197 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1200 /***********************************************************************
1201 * wglUseFontBitmapsW (OPENGL32.@)
1203 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1205 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1208 /* FIXME: should probably have a glu.h header */
1210 typedef struct GLUtesselator GLUtesselator;
1211 typedef void (WINAPI *_GLUfuncptr)(void);
1213 #define GLU_TESS_BEGIN 100100
1214 #define GLU_TESS_VERTEX 100101
1215 #define GLU_TESS_END 100102
1217 static GLUtesselator * (WINAPI *pgluNewTess)(void);
1218 static void (WINAPI *pgluDeleteTess)(GLUtesselator *tess);
1219 static void (WINAPI *pgluTessBeginPolygon)(GLUtesselator *tess, void *polygon_data);
1220 static void (WINAPI *pgluTessEndPolygon)(GLUtesselator *tess);
1221 static void (WINAPI *pgluTessCallback)(GLUtesselator *tess, GLenum which, _GLUfuncptr fn);
1222 static void (WINAPI *pgluTessBeginContour)(GLUtesselator *tess);
1223 static void (WINAPI *pgluTessEndContour)(GLUtesselator *tess);
1224 static void (WINAPI *pgluTessVertex)(GLUtesselator *tess, GLdouble *location, GLvoid* data);
1226 static HMODULE load_libglu(void)
1228 static const WCHAR glu32W[] = {'g','l','u','3','2','.','d','l','l',0};
1229 static int already_loaded;
1230 static HMODULE module;
1232 if (already_loaded) return module;
1233 already_loaded = 1;
1235 TRACE("Trying to load GLU library\n");
1236 module = LoadLibraryW( glu32W );
1237 if (!module)
1239 WARN("Failed to load glu32\n");
1240 return NULL;
1242 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1243 LOAD_FUNCPTR(gluNewTess);
1244 LOAD_FUNCPTR(gluDeleteTess);
1245 LOAD_FUNCPTR(gluTessBeginContour);
1246 LOAD_FUNCPTR(gluTessBeginPolygon);
1247 LOAD_FUNCPTR(gluTessCallback);
1248 LOAD_FUNCPTR(gluTessEndContour);
1249 LOAD_FUNCPTR(gluTessEndPolygon);
1250 LOAD_FUNCPTR(gluTessVertex);
1251 #undef LOAD_FUNCPTR
1252 return module;
1255 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1257 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1258 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1259 vertex[2] = 0.0;
1262 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1264 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1265 GLdouble *dbl = vertex;
1266 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1267 funcs->gl.p_glVertex3dv(vertex);
1270 static void WINAPI tess_callback_begin(GLenum which)
1272 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1273 TRACE("%d\n", which);
1274 funcs->gl.p_glBegin(which);
1277 static void WINAPI tess_callback_end(void)
1279 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1280 TRACE("\n");
1281 funcs->gl.p_glEnd();
1284 /***********************************************************************
1285 * wglUseFontOutlines_common
1287 static BOOL wglUseFontOutlines_common(HDC hdc,
1288 DWORD first,
1289 DWORD count,
1290 DWORD listBase,
1291 FLOAT deviation,
1292 FLOAT extrusion,
1293 int format,
1294 LPGLYPHMETRICSFLOAT lpgmf,
1295 BOOL unicode)
1297 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1298 UINT glyph;
1299 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
1300 GLUtesselator *tess;
1301 LOGFONTW lf;
1302 HFONT old_font, unscaled_font;
1303 UINT em_size = 1024;
1304 RECT rc;
1306 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1307 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1309 if (!load_libglu())
1311 ERR("glu32 is required for this function but isn't available\n");
1312 return FALSE;
1315 tess = pgluNewTess();
1316 if(!tess) return FALSE;
1317 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
1318 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
1319 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1321 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1322 rc.left = rc.right = rc.bottom = 0;
1323 rc.top = em_size;
1324 DPtoLP(hdc, (POINT*)&rc, 2);
1325 lf.lfHeight = -abs(rc.top - rc.bottom);
1326 lf.lfOrientation = lf.lfEscapement = 0;
1327 unscaled_font = CreateFontIndirectW(&lf);
1328 old_font = SelectObject(hdc, unscaled_font);
1330 for (glyph = first; glyph < first + count; glyph++)
1332 DWORD needed;
1333 GLYPHMETRICS gm;
1334 BYTE *buf;
1335 TTPOLYGONHEADER *pph;
1336 TTPOLYCURVE *ppc;
1337 GLdouble *vertices;
1339 if(unicode)
1340 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1341 else
1342 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1344 if(needed == GDI_ERROR)
1345 goto error;
1347 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1348 vertices = HeapAlloc(GetProcessHeap(), 0, needed / sizeof(POINTFX) * 3 * sizeof(GLdouble));
1350 if(unicode)
1351 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1352 else
1353 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1355 TRACE("glyph %d\n", glyph);
1357 if(lpgmf)
1359 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1360 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1361 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1362 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1363 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1364 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1366 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1367 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1368 lpgmf++;
1371 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1372 pgluTessBeginPolygon(tess, NULL);
1374 pph = (TTPOLYGONHEADER*)buf;
1375 while((BYTE*)pph < buf + needed)
1377 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1379 pgluTessBeginContour(tess);
1381 fixed_to_double(pph->pfxStart, em_size, vertices);
1382 pgluTessVertex(tess, vertices, vertices);
1383 vertices += 3;
1385 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1386 while((char*)ppc < (char*)pph + pph->cb)
1388 int i;
1390 switch(ppc->wType) {
1391 case TT_PRIM_LINE:
1392 for(i = 0; i < ppc->cpfx; i++)
1394 TRACE("\t\tline to %d, %d\n", ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1395 fixed_to_double(ppc->apfx[i], em_size, vertices);
1396 pgluTessVertex(tess, vertices, vertices);
1397 vertices += 3;
1399 break;
1401 case TT_PRIM_QSPLINE:
1402 for(i = 0; i < ppc->cpfx/2; i++)
1404 /* FIXME: just connecting the control points for now */
1405 TRACE("\t\tcurve %d,%d %d,%d\n",
1406 ppc->apfx[i * 2].x.value, ppc->apfx[i * 3].y.value,
1407 ppc->apfx[i * 2 + 1].x.value, ppc->apfx[i * 3 + 1].y.value);
1408 fixed_to_double(ppc->apfx[i * 2], em_size, vertices);
1409 pgluTessVertex(tess, vertices, vertices);
1410 vertices += 3;
1411 fixed_to_double(ppc->apfx[i * 2 + 1], em_size, vertices);
1412 pgluTessVertex(tess, vertices, vertices);
1413 vertices += 3;
1415 break;
1416 default:
1417 ERR("\t\tcurve type = %d\n", ppc->wType);
1418 pgluTessEndContour(tess);
1419 goto error_in_list;
1422 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1423 (ppc->cpfx - 1) * sizeof(POINTFX));
1425 pgluTessEndContour(tess);
1426 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1429 error_in_list:
1430 pgluTessEndPolygon(tess);
1431 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1432 funcs->gl.p_glEndList();
1433 HeapFree(GetProcessHeap(), 0, buf);
1434 HeapFree(GetProcessHeap(), 0, vertices);
1437 error:
1438 DeleteObject(SelectObject(hdc, old_font));
1439 pgluDeleteTess(tess);
1440 return TRUE;
1444 /***********************************************************************
1445 * wglUseFontOutlinesA (OPENGL32.@)
1447 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1448 DWORD first,
1449 DWORD count,
1450 DWORD listBase,
1451 FLOAT deviation,
1452 FLOAT extrusion,
1453 int format,
1454 LPGLYPHMETRICSFLOAT lpgmf)
1456 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1459 /***********************************************************************
1460 * wglUseFontOutlinesW (OPENGL32.@)
1462 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1463 DWORD first,
1464 DWORD count,
1465 DWORD listBase,
1466 FLOAT deviation,
1467 FLOAT extrusion,
1468 int format,
1469 LPGLYPHMETRICSFLOAT lpgmf)
1471 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1474 /***********************************************************************
1475 * glDebugEntry (OPENGL32.@)
1477 GLint WINAPI wine_glDebugEntry( GLint unknown1, GLint unknown2 )
1479 return 0;
1482 /* build the extension string by filtering out the disabled extensions */
1483 static char *build_gl_extensions( const char *extensions )
1485 char *p, *str, *disabled = NULL;
1486 const char *end;
1487 HKEY hkey;
1489 TRACE( "GL_EXTENSIONS:\n" );
1491 if (!extensions) extensions = "";
1493 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1494 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1496 DWORD size, ret = RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size );
1497 if (!ret && (disabled = HeapAlloc( GetProcessHeap(), 0, size )))
1498 ret = RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)disabled, &size );
1499 RegCloseKey( hkey );
1500 if (ret) *disabled = 0;
1503 if ((str = HeapAlloc( GetProcessHeap(), 0, strlen(extensions) + 2 )))
1505 p = str;
1506 for (;;)
1508 while (*extensions == ' ') extensions++;
1509 if (!*extensions) break;
1510 if (!(end = strchr( extensions, ' ' ))) end = extensions + strlen( extensions );
1511 memcpy( p, extensions, end - extensions );
1512 p[end - extensions] = 0;
1513 if (!has_extension( disabled, p ))
1515 TRACE("++ %s\n", p );
1516 p += end - extensions;
1517 *p++ = ' ';
1519 else TRACE("-- %s (disabled by config)\n", p );
1520 extensions = end;
1522 *p = 0;
1524 HeapFree( GetProcessHeap(), 0, disabled );
1525 return str;
1528 /***********************************************************************
1529 * glGetString (OPENGL32.@)
1531 const GLubyte * WINAPI wine_glGetString( GLenum name )
1533 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1534 static const GLubyte *gl_extensions;
1536 /* this is for buggy nvidia driver, crashing if called from a different
1537 thread with no context */
1538 if(wglGetCurrentContext() == NULL)
1539 return NULL;
1541 if (name != GL_EXTENSIONS) return funcs->gl.p_glGetString(name);
1543 if (!gl_extensions)
1545 const char *orig_ext = (const char *)funcs->gl.p_glGetString(GL_EXTENSIONS);
1546 char *new_ext = build_gl_extensions( orig_ext );
1547 if (InterlockedCompareExchangePointer( (void **)&gl_extensions, new_ext, NULL ))
1548 HeapFree( GetProcessHeap(), 0, new_ext );
1550 return gl_extensions;
1553 /***********************************************************************
1554 * wglSwapBuffers (OPENGL32.@)
1556 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
1558 return GdiSwapBuffers(hdc);
1561 /***********************************************************************
1562 * OpenGL initialisation routine
1564 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1566 switch(reason)
1568 case DLL_PROCESS_ATTACH:
1569 opengl32_handle = hinst;
1570 DisableThreadLibraryCalls(hinst);
1571 NtCurrentTeb()->glTable = &null_opengl_funcs;
1572 break;
1573 case DLL_THREAD_ATTACH:
1574 NtCurrentTeb()->glTable = &null_opengl_funcs;
1575 break;
1577 return TRUE;