mscoree: Factor out common code for calling .NET methods.
[wine.git] / dlls / opengl32 / wgl.c
blob3a5e148bb841a5dc131f029b9b221a17f2351745
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 "opengl_ext.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "wingdi.h"
34 #include "winternl.h"
35 #include "winnt.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(fps);
46 extern struct opengl_funcs null_opengl_funcs;
48 /* handle management */
50 #define MAX_WGL_HANDLES 1024
52 enum wgl_handle_type
54 HANDLE_CONTEXT = 0 << 12,
55 HANDLE_PBUFFER = 1 << 12,
56 HANDLE_TYPE_MASK = 15 << 12
59 struct opengl_context
61 DWORD tid; /* thread that the context is current in */
62 HDC draw_dc; /* current drawing DC */
63 HDC read_dc; /* current reading DC */
64 GLubyte *extensions; /* extension string */
65 struct wgl_context *drv_ctx; /* driver context */
68 struct wgl_handle
70 UINT handle;
71 struct opengl_funcs *funcs;
72 union
74 struct opengl_context *context; /* for HANDLE_CONTEXT */
75 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
76 struct wgl_handle *next; /* for free handles */
77 } u;
80 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
81 static struct wgl_handle *next_free;
82 static unsigned int handle_count;
84 static CRITICAL_SECTION wgl_section;
85 static CRITICAL_SECTION_DEBUG critsect_debug =
87 0, 0, &wgl_section,
88 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
89 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
91 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
93 static inline struct opengl_funcs *get_dc_funcs( HDC hdc )
95 struct opengl_funcs *funcs = __wine_get_wgl_driver( hdc, WINE_WGL_DRIVER_VERSION );
96 if (funcs == (void *)-1) funcs = &null_opengl_funcs;
97 return funcs;
100 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
102 WORD generation = HIWORD( ptr->handle ) + 1;
103 if (!generation) generation++;
104 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
105 return ULongToHandle( ptr->handle );
108 /* the current context is assumed valid and doesn't need locking */
109 static inline struct wgl_handle *get_current_context_ptr(void)
111 if (!NtCurrentTeb()->glCurrentRC) return NULL;
112 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
115 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
117 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
119 EnterCriticalSection( &wgl_section );
120 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
121 return &wgl_handles[index];
123 LeaveCriticalSection( &wgl_section );
124 SetLastError( ERROR_INVALID_HANDLE );
125 return NULL;
128 static void release_handle_ptr( struct wgl_handle *ptr )
130 if (ptr) LeaveCriticalSection( &wgl_section );
133 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
135 HANDLE handle = 0;
136 struct wgl_handle *ptr = NULL;
138 EnterCriticalSection( &wgl_section );
139 if ((ptr = next_free))
140 next_free = next_free->u.next;
141 else if (handle_count < MAX_WGL_HANDLES)
142 ptr = &wgl_handles[handle_count++];
144 if (ptr)
146 ptr->funcs = funcs;
147 ptr->u.context = user_ptr;
148 handle = next_handle( ptr, type );
150 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
151 LeaveCriticalSection( &wgl_section );
152 return handle;
155 static void free_handle_ptr( struct wgl_handle *ptr )
157 ptr->handle |= 0xffff;
158 ptr->u.next = next_free;
159 ptr->funcs = NULL;
160 next_free = ptr;
161 LeaveCriticalSection( &wgl_section );
164 /***********************************************************************
165 * wglCopyContext (OPENGL32.@)
167 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
169 struct wgl_handle *src, *dst;
170 BOOL ret = FALSE;
172 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
173 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
175 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
176 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
177 dst->u.context->drv_ctx, mask );
179 release_handle_ptr( dst );
180 release_handle_ptr( src );
181 return ret;
184 /***********************************************************************
185 * wglDeleteContext (OPENGL32.@)
187 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
189 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
191 if (!ptr) return FALSE;
193 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
195 SetLastError( ERROR_BUSY );
196 release_handle_ptr( ptr );
197 return FALSE;
199 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
200 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
201 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
202 HeapFree( GetProcessHeap(), 0, ptr->u.context );
203 free_handle_ptr( ptr );
204 return TRUE;
207 /***********************************************************************
208 * wglMakeCurrent (OPENGL32.@)
210 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
212 BOOL ret = TRUE;
213 struct wgl_handle *ptr, *prev = get_current_context_ptr();
215 if (hglrc)
217 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
218 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
220 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
221 if (ret)
223 if (prev) prev->u.context->tid = 0;
224 ptr->u.context->tid = GetCurrentThreadId();
225 ptr->u.context->draw_dc = hdc;
226 ptr->u.context->read_dc = hdc;
227 NtCurrentTeb()->glCurrentRC = hglrc;
228 NtCurrentTeb()->glTable = ptr->funcs;
231 else
233 SetLastError( ERROR_BUSY );
234 ret = FALSE;
236 release_handle_ptr( ptr );
238 else if (prev)
240 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
241 prev->u.context->tid = 0;
242 NtCurrentTeb()->glCurrentRC = 0;
243 NtCurrentTeb()->glTable = &null_opengl_funcs;
245 else if (!hdc)
247 SetLastError( ERROR_INVALID_HANDLE );
248 ret = FALSE;
250 return ret;
253 /***********************************************************************
254 * wglCreateContextAttribsARB
256 * Provided by the WGL_ARB_create_context extension.
258 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
260 HGLRC ret = 0;
261 struct wgl_context *drv_ctx;
262 struct wgl_handle *share_ptr = NULL;
263 struct opengl_context *context;
264 struct opengl_funcs *funcs = get_dc_funcs( hdc );
266 if (!funcs || !funcs->ext.p_wglCreateContextAttribsARB) return 0;
267 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT ))) return 0;
268 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
269 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
271 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
273 context->drv_ctx = drv_ctx;
274 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
275 HeapFree( GetProcessHeap(), 0, context );
277 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
279 release_handle_ptr( share_ptr );
280 return ret;
284 /***********************************************************************
285 * wglMakeContextCurrentARB
287 * Provided by the WGL_ARB_make_current_read extension.
289 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
291 BOOL ret = TRUE;
292 struct wgl_handle *ptr, *prev = get_current_context_ptr();
294 if (hglrc)
296 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
297 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
299 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
300 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
301 ptr->u.context->drv_ctx ));
302 if (ret)
304 if (prev) prev->u.context->tid = 0;
305 ptr->u.context->tid = GetCurrentThreadId();
306 ptr->u.context->draw_dc = draw_hdc;
307 ptr->u.context->read_dc = read_hdc;
308 NtCurrentTeb()->glCurrentRC = hglrc;
309 NtCurrentTeb()->glTable = ptr->funcs;
312 else
314 SetLastError( ERROR_BUSY );
315 ret = FALSE;
317 release_handle_ptr( ptr );
319 else if (prev)
321 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
322 prev->u.context->tid = 0;
323 NtCurrentTeb()->glCurrentRC = 0;
324 NtCurrentTeb()->glTable = &null_opengl_funcs;
326 return ret;
329 /***********************************************************************
330 * wglGetCurrentReadDCARB
332 * Provided by the WGL_ARB_make_current_read extension.
334 HDC WINAPI wglGetCurrentReadDCARB(void)
336 struct wgl_handle *ptr = get_current_context_ptr();
338 if (!ptr) return 0;
339 return ptr->u.context->read_dc;
342 /***********************************************************************
343 * wglShareLists (OPENGL32.@)
345 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
347 BOOL ret = FALSE;
348 struct wgl_handle *src, *dst;
350 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
351 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
353 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
354 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
356 release_handle_ptr( dst );
357 release_handle_ptr( src );
358 return ret;
361 /***********************************************************************
362 * wglGetCurrentDC (OPENGL32.@)
364 HDC WINAPI wglGetCurrentDC(void)
366 struct wgl_handle *ptr = get_current_context_ptr();
368 if (!ptr) return 0;
369 return ptr->u.context->draw_dc;
372 /***********************************************************************
373 * wglCreateContext (OPENGL32.@)
375 HGLRC WINAPI wglCreateContext(HDC hdc)
377 HGLRC ret = 0;
378 struct wgl_context *drv_ctx;
379 struct opengl_context *context;
380 struct opengl_funcs *funcs = get_dc_funcs( hdc );
382 if (!funcs) return 0;
383 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
384 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
386 context->drv_ctx = drv_ctx;
387 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
388 HeapFree( GetProcessHeap(), 0, context );
390 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
391 return ret;
394 /***********************************************************************
395 * wglGetCurrentContext (OPENGL32.@)
397 HGLRC WINAPI wglGetCurrentContext(void)
399 return NtCurrentTeb()->glCurrentRC;
402 /***********************************************************************
403 * wglDescribePixelFormat (OPENGL32.@)
405 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
407 struct opengl_funcs *funcs = get_dc_funcs( hdc );
408 if (!funcs) return 0;
409 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
412 /***********************************************************************
413 * wglChoosePixelFormat (OPENGL32.@)
415 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
417 PIXELFORMATDESCRIPTOR format, best;
418 int i, count, best_format;
419 int bestDBuffer = -1, bestStereo = -1;
421 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
422 "accum %u depth %u stencil %u aux %u\n",
423 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
424 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
425 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
427 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
428 if (!count) return 0;
430 best_format = 0;
431 best.dwFlags = 0;
432 best.cAlphaBits = -1;
433 best.cColorBits = -1;
434 best.cDepthBits = -1;
435 best.cStencilBits = -1;
436 best.cAuxBuffers = -1;
438 for (i = 1; i <= count; i++)
440 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
442 if (ppfd->iPixelType != format.iPixelType)
444 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
445 continue;
448 /* only use bitmap capable for formats for bitmap rendering */
449 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
451 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
452 continue;
455 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
456 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
457 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
458 * formats without the given flag set.
459 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
460 * has indicated that a format without stereo is returned when stereo is unavailable.
461 * So in case PFD_STEREO is set, formats that support it should have priority above formats
462 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
464 * To summarize the following is most likely the correct behavior:
465 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
466 * stereo set -> prefer stereo formats, but also accept non-stereo formats
467 * stereo don't care -> it doesn't matter whether we get stereo or not
469 * In Wine we will treat non-stereo the same way as don't care because it makes
470 * format selection even more complicated and second drivers with Stereo advertise
471 * each format twice anyway.
474 /* Doublebuffer, see the comments above */
475 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
477 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
478 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
479 goto found;
481 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
484 /* Stereo, see the comments above. */
485 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
487 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
488 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
489 goto found;
491 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
494 /* Below we will do a number of checks to select the 'best' pixelformat.
495 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
496 * The code works by trying to match the most important options as close as possible.
497 * When a reasonable format is found, we will try to match more options.
498 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
499 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
500 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
502 if (ppfd->cColorBits)
504 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
505 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
506 goto found;
508 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
510 TRACE( "color mismatch for iPixelFormat=%d\n", i );
511 continue;
514 if (ppfd->cAlphaBits)
516 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
517 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
518 goto found;
520 if (best.cAlphaBits != format.cAlphaBits)
522 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
523 continue;
526 if (ppfd->cDepthBits)
528 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
529 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
530 goto found;
532 if (best.cDepthBits != format.cDepthBits)
534 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
535 continue;
538 if (ppfd->cStencilBits)
540 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
541 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
542 goto found;
544 if (best.cStencilBits != format.cStencilBits)
546 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
547 continue;
550 if (ppfd->cAuxBuffers)
552 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
553 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
554 goto found;
556 if (best.cAuxBuffers != format.cAuxBuffers)
558 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
559 continue;
562 continue;
564 found:
565 best_format = i;
566 best = format;
567 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
568 bestStereo = format.dwFlags & PFD_STEREO;
571 TRACE( "returning %u\n", best_format );
572 return best_format;
575 /***********************************************************************
576 * wglGetPixelFormat (OPENGL32.@)
578 INT WINAPI wglGetPixelFormat(HDC hdc)
580 struct opengl_funcs *funcs = get_dc_funcs( hdc );
581 if (!funcs) return 0;
582 return funcs->wgl.p_wglGetPixelFormat( hdc );
585 /***********************************************************************
586 * wglSetPixelFormat(OPENGL32.@)
588 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
590 struct opengl_funcs *funcs = get_dc_funcs( hdc );
591 if (!funcs) return FALSE;
592 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
595 /***********************************************************************
596 * wglSwapBuffers (OPENGL32.@)
598 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
600 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
602 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
603 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
605 if (TRACE_ON(fps))
607 static long prev_time, start_time;
608 static unsigned long frames, frames_total;
610 DWORD time = GetTickCount();
611 frames++;
612 frames_total++;
613 /* every 1.5 seconds */
614 if (time - prev_time > 1500)
616 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
617 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
618 prev_time = time;
619 frames = 0;
620 if (start_time == 0) start_time = time;
623 return TRUE;
626 /***********************************************************************
627 * wglCreateLayerContext (OPENGL32.@)
629 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
630 int iLayerPlane) {
631 TRACE("(%p,%d)\n", hdc, iLayerPlane);
633 if (iLayerPlane == 0) {
634 return wglCreateContext(hdc);
636 FIXME("no handler for layer %d\n", iLayerPlane);
638 return NULL;
641 /***********************************************************************
642 * wglDescribeLayerPlane (OPENGL32.@)
644 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
645 int iPixelFormat,
646 int iLayerPlane,
647 UINT nBytes,
648 LPLAYERPLANEDESCRIPTOR plpd) {
649 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
651 return FALSE;
654 /***********************************************************************
655 * wglGetLayerPaletteEntries (OPENGL32.@)
657 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
658 int iLayerPlane,
659 int iStart,
660 int cEntries,
661 const COLORREF *pcr) {
662 FIXME("(): stub!\n");
664 return 0;
667 /* check if the extension is present in the list */
668 static BOOL has_extension( const char *list, const char *ext, size_t len )
670 while (list)
672 while (*list == ' ') list++;
673 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
674 list = strchr( list, ' ' );
676 return FALSE;
679 static int compar(const void *elt_a, const void *elt_b) {
680 return strcmp(((const OpenGL_extension *) elt_a)->name,
681 ((const OpenGL_extension *) elt_b)->name);
684 /* Check if a GL extension is supported */
685 static BOOL is_extension_supported(const char* extension)
687 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
688 const char *gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
689 size_t len;
691 TRACE("Checking for extension '%s'\n", extension);
693 if(!gl_ext_string) {
694 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
695 return FALSE;
698 /* We use the GetProcAddress function from the display driver to retrieve function pointers
699 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
700 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
701 * require the function to return NULL when an extension isn't found. For this reason we check
702 * if the OpenGL extension required for the function we are looking up is supported. */
704 while ((len = strcspn(extension, " ")) != 0)
706 /* Check if the extension is part of the GL extension string to see if it is supported. */
707 if (has_extension(gl_ext_string, extension, len))
708 return TRUE;
710 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
711 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
712 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
713 * Check if we are searching for a core GL function */
714 if(strncmp(extension, "GL_VERSION_", 11) == 0)
716 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
717 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
719 if(!gl_version) {
720 ERR("No OpenGL version found!\n");
721 return FALSE;
724 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
725 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
726 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
727 return TRUE;
729 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]);
732 if (extension[len] == ' ') len++;
733 extension += len;
736 return FALSE;
739 /***********************************************************************
740 * wglGetProcAddress (OPENGL32.@)
742 PROC WINAPI wglGetProcAddress( LPCSTR name )
744 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
745 void **func_ptr;
746 OpenGL_extension ext;
747 const OpenGL_extension *ext_ret;
749 if (!name) return NULL;
751 /* Without an active context opengl32 doesn't know to what
752 * driver it has to dispatch wglGetProcAddress.
754 if (!get_current_context_ptr())
756 WARN("No active WGL context found\n");
757 return NULL;
760 ext.name = name;
761 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
762 if (!ext_ret)
764 WARN("Function %s unknown\n", name);
765 return NULL;
768 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
769 if (!*func_ptr)
771 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
773 if (!is_extension_supported(ext_ret->extension))
774 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
776 if (driver_func == NULL)
778 WARN("Function %s not supported by driver\n", name);
779 return NULL;
781 *func_ptr = driver_func;
784 TRACE("returning %s -> %p\n", name, ext_ret->func);
785 return ext_ret->func;
788 /***********************************************************************
789 * wglRealizeLayerPalette (OPENGL32.@)
791 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
792 int iLayerPlane,
793 BOOL bRealize) {
794 FIXME("()\n");
796 return FALSE;
799 /***********************************************************************
800 * wglSetLayerPaletteEntries (OPENGL32.@)
802 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
803 int iLayerPlane,
804 int iStart,
805 int cEntries,
806 const COLORREF *pcr) {
807 FIXME("(): stub!\n");
809 return 0;
812 /***********************************************************************
813 * wglSwapLayerBuffers (OPENGL32.@)
815 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
816 UINT fuPlanes) {
817 TRACE("(%p, %08x)\n", hdc, fuPlanes);
819 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
820 if (!wglSwapBuffers( hdc )) return FALSE;
821 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
824 if (fuPlanes) {
825 WARN("Following layers unhandled: %08x\n", fuPlanes);
828 return TRUE;
831 /***********************************************************************
832 * wglAllocateMemoryNV
834 * Provided by the WGL_NV_vertex_array_range extension.
836 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
838 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
840 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
841 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
844 /***********************************************************************
845 * wglFreeMemoryNV
847 * Provided by the WGL_NV_vertex_array_range extension.
849 void WINAPI wglFreeMemoryNV( void *pointer )
851 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
853 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
856 /***********************************************************************
857 * wglBindTexImageARB
859 * Provided by the WGL_ARB_render_texture extension.
861 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
863 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
864 BOOL ret;
866 if (!ptr) return FALSE;
867 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
868 release_handle_ptr( ptr );
869 return ret;
872 /***********************************************************************
873 * wglReleaseTexImageARB
875 * Provided by the WGL_ARB_render_texture extension.
877 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
879 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
880 BOOL ret;
882 if (!ptr) return FALSE;
883 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
884 release_handle_ptr( ptr );
885 return ret;
888 /***********************************************************************
889 * wglSetPbufferAttribARB
891 * Provided by the WGL_ARB_render_texture extension.
893 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
895 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
896 BOOL ret;
898 if (!ptr) return FALSE;
899 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
900 release_handle_ptr( ptr );
901 return ret;
904 /***********************************************************************
905 * wglChoosePixelFormatARB
907 * Provided by the WGL_ARB_pixel_format extension.
909 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
910 UINT max, int *formats, UINT *count )
912 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
914 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
915 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
918 /***********************************************************************
919 * wglGetPixelFormatAttribivARB
921 * Provided by the WGL_ARB_pixel_format extension.
923 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
924 int *values )
926 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
928 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
929 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
932 /***********************************************************************
933 * wglGetPixelFormatAttribfvARB
935 * Provided by the WGL_ARB_pixel_format extension.
937 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
938 FLOAT *values )
940 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
942 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
943 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
946 /***********************************************************************
947 * wglCreatePbufferARB
949 * Provided by the WGL_ARB_pbuffer extension.
951 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
953 HPBUFFERARB ret = 0;
954 struct wgl_pbuffer *pbuffer;
955 struct opengl_funcs *funcs = get_dc_funcs( hdc );
957 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
958 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
959 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
960 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
961 return ret;
964 /***********************************************************************
965 * wglGetPbufferDCARB
967 * Provided by the WGL_ARB_pbuffer extension.
969 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
971 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
972 HDC ret;
974 if (!ptr) return 0;
975 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
976 release_handle_ptr( ptr );
977 return ret;
980 /***********************************************************************
981 * wglReleasePbufferDCARB
983 * Provided by the WGL_ARB_pbuffer extension.
985 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
987 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
988 BOOL ret;
990 if (!ptr) return FALSE;
991 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
992 release_handle_ptr( ptr );
993 return ret;
996 /***********************************************************************
997 * wglDestroyPbufferARB
999 * Provided by the WGL_ARB_pbuffer extension.
1001 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1003 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1005 if (!ptr) return FALSE;
1006 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1007 free_handle_ptr( ptr );
1008 return TRUE;
1011 /***********************************************************************
1012 * wglQueryPbufferARB
1014 * Provided by the WGL_ARB_pbuffer extension.
1016 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1018 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1019 BOOL ret;
1021 if (!ptr) return FALSE;
1022 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1023 release_handle_ptr( ptr );
1024 return ret;
1027 /***********************************************************************
1028 * wglGetExtensionsStringARB
1030 * Provided by the WGL_ARB_extensions_string extension.
1032 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1034 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1036 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1037 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1040 /***********************************************************************
1041 * wglGetExtensionsStringEXT
1043 * Provided by the WGL_EXT_extensions_string extension.
1045 const char * WINAPI wglGetExtensionsStringEXT(void)
1047 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1049 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1050 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1053 /***********************************************************************
1054 * wglSwapIntervalEXT
1056 * Provided by the WGL_EXT_swap_control extension.
1058 BOOL WINAPI wglSwapIntervalEXT( int interval )
1060 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1062 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1063 return funcs->ext.p_wglSwapIntervalEXT( interval );
1066 /***********************************************************************
1067 * wglGetSwapIntervalEXT
1069 * Provided by the WGL_EXT_swap_control extension.
1071 int WINAPI wglGetSwapIntervalEXT(void)
1073 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1075 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1076 return funcs->ext.p_wglGetSwapIntervalEXT();
1079 /***********************************************************************
1080 * wglSetPixelFormatWINE
1082 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1084 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1086 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1088 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1089 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1092 /***********************************************************************
1093 * wglUseFontBitmaps_common
1095 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1097 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1098 GLYPHMETRICS gm;
1099 unsigned int glyph, size = 0;
1100 void *bitmap = NULL, *gl_bitmap = NULL;
1101 int org_alignment;
1102 BOOL ret = TRUE;
1104 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1105 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1107 for (glyph = first; glyph < first + count; glyph++) {
1108 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1109 unsigned int needed_size, height, width, width_int;
1111 if (unicode)
1112 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1113 else
1114 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1116 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1117 if (needed_size == GDI_ERROR) {
1118 ret = FALSE;
1119 break;
1122 if (needed_size > size) {
1123 size = needed_size;
1124 HeapFree(GetProcessHeap(), 0, bitmap);
1125 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1126 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1127 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1129 if (needed_size != 0) {
1130 if (unicode)
1131 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1132 size, bitmap, &identity) != GDI_ERROR);
1133 else
1134 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1135 size, bitmap, &identity) != GDI_ERROR);
1136 if (!ret) break;
1139 if (TRACE_ON(wgl)) {
1140 unsigned int bitmask;
1141 unsigned char *bitmap_ = bitmap;
1143 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1144 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1145 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1146 if (needed_size != 0) {
1147 TRACE(" - bitmap:\n");
1148 for (height = 0; height < gm.gmBlackBoxY; height++) {
1149 TRACE(" ");
1150 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1151 if (bitmask == 0) {
1152 bitmap_ += 1;
1153 bitmask = 0x80;
1155 if (*bitmap_ & bitmask)
1156 TRACE("*");
1157 else
1158 TRACE(" ");
1160 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1161 TRACE("\n");
1166 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1167 * glyph for it to be drawn properly.
1169 if (needed_size != 0) {
1170 width_int = (gm.gmBlackBoxX + 31) / 32;
1171 for (height = 0; height < gm.gmBlackBoxY; height++) {
1172 for (width = 0; width < width_int; width++) {
1173 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1174 ((int *) bitmap)[height * width_int + width];
1179 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1180 if (needed_size != 0) {
1181 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1182 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1183 gm.gmCellIncX, gm.gmCellIncY,
1184 gl_bitmap);
1185 } else {
1186 /* This is the case of 'empty' glyphs like the space character */
1187 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1189 funcs->gl.p_glEndList();
1192 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1193 HeapFree(GetProcessHeap(), 0, bitmap);
1194 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1195 return ret;
1198 /***********************************************************************
1199 * wglUseFontBitmapsA (OPENGL32.@)
1201 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1203 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1206 /***********************************************************************
1207 * wglUseFontBitmapsW (OPENGL32.@)
1209 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1211 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1214 /* FIXME: should probably have a glu.h header */
1216 typedef struct GLUtesselator GLUtesselator;
1217 typedef void (WINAPI *_GLUfuncptr)(void);
1219 #define GLU_TESS_BEGIN 100100
1220 #define GLU_TESS_VERTEX 100101
1221 #define GLU_TESS_END 100102
1223 static GLUtesselator * (WINAPI *pgluNewTess)(void);
1224 static void (WINAPI *pgluDeleteTess)(GLUtesselator *tess);
1225 static void (WINAPI *pgluTessNormal)(GLUtesselator *tess, GLdouble x, GLdouble y, GLdouble z);
1226 static void (WINAPI *pgluTessBeginPolygon)(GLUtesselator *tess, void *polygon_data);
1227 static void (WINAPI *pgluTessEndPolygon)(GLUtesselator *tess);
1228 static void (WINAPI *pgluTessCallback)(GLUtesselator *tess, GLenum which, _GLUfuncptr fn);
1229 static void (WINAPI *pgluTessBeginContour)(GLUtesselator *tess);
1230 static void (WINAPI *pgluTessEndContour)(GLUtesselator *tess);
1231 static void (WINAPI *pgluTessVertex)(GLUtesselator *tess, GLdouble *location, GLvoid* data);
1233 static HMODULE load_libglu(void)
1235 static const WCHAR glu32W[] = {'g','l','u','3','2','.','d','l','l',0};
1236 static BOOL already_loaded;
1237 static HMODULE module;
1239 if (already_loaded) return module;
1240 already_loaded = TRUE;
1242 TRACE("Trying to load GLU library\n");
1243 module = LoadLibraryW( glu32W );
1244 if (!module)
1246 WARN("Failed to load glu32\n");
1247 return NULL;
1249 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1250 LOAD_FUNCPTR(gluNewTess);
1251 LOAD_FUNCPTR(gluDeleteTess);
1252 LOAD_FUNCPTR(gluTessBeginContour);
1253 LOAD_FUNCPTR(gluTessNormal);
1254 LOAD_FUNCPTR(gluTessBeginPolygon);
1255 LOAD_FUNCPTR(gluTessCallback);
1256 LOAD_FUNCPTR(gluTessEndContour);
1257 LOAD_FUNCPTR(gluTessEndPolygon);
1258 LOAD_FUNCPTR(gluTessVertex);
1259 #undef LOAD_FUNCPTR
1260 return module;
1263 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1265 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1266 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1267 vertex[2] = 0.0;
1270 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1272 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1273 GLdouble *dbl = vertex;
1274 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1275 funcs->gl.p_glVertex3dv(vertex);
1278 static void WINAPI tess_callback_begin(GLenum which)
1280 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1281 TRACE("%d\n", which);
1282 funcs->gl.p_glBegin(which);
1285 static void WINAPI tess_callback_end(void)
1287 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1288 TRACE("\n");
1289 funcs->gl.p_glEnd();
1292 typedef struct _bezier_vector {
1293 GLdouble x;
1294 GLdouble y;
1295 } bezier_vector;
1297 static double bezier_deviation_squared(const bezier_vector *p)
1299 bezier_vector deviation;
1300 bezier_vector vertex;
1301 bezier_vector base;
1302 double base_length;
1303 double dot;
1305 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1306 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1308 base.x = p[2].x - p[0].x;
1309 base.y = p[2].y - p[0].y;
1311 base_length = sqrt(base.x*base.x + base.y*base.y);
1312 base.x /= base_length;
1313 base.y /= base_length;
1315 dot = base.x*vertex.x + base.y*vertex.y;
1316 dot = min(max(dot, 0.0), base_length);
1317 base.x *= dot;
1318 base.y *= dot;
1320 deviation.x = vertex.x-base.x;
1321 deviation.y = vertex.y-base.y;
1323 return deviation.x*deviation.x + deviation.y*deviation.y;
1326 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1328 bezier_vector first_curve[3];
1329 bezier_vector second_curve[3];
1330 bezier_vector vertex;
1331 int total_vertices;
1333 if(bezier_deviation_squared(p) <= deviation*deviation)
1335 if(points)
1336 *points = p[2];
1337 return 1;
1340 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1341 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1343 first_curve[0] = p[0];
1344 first_curve[1].x = (p[0].x + p[1].x)/2;
1345 first_curve[1].y = (p[0].y + p[1].y)/2;
1346 first_curve[2] = vertex;
1348 second_curve[0] = vertex;
1349 second_curve[1].x = (p[2].x + p[1].x)/2;
1350 second_curve[1].y = (p[2].y + p[1].y)/2;
1351 second_curve[2] = p[2];
1353 total_vertices = bezier_approximate(first_curve, points, deviation);
1354 if(points)
1355 points += total_vertices;
1356 total_vertices += bezier_approximate(second_curve, points, deviation);
1357 return total_vertices;
1360 /***********************************************************************
1361 * wglUseFontOutlines_common
1363 static BOOL wglUseFontOutlines_common(HDC hdc,
1364 DWORD first,
1365 DWORD count,
1366 DWORD listBase,
1367 FLOAT deviation,
1368 FLOAT extrusion,
1369 int format,
1370 LPGLYPHMETRICSFLOAT lpgmf,
1371 BOOL unicode)
1373 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1374 UINT glyph;
1375 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
1376 GLUtesselator *tess = NULL;
1377 LOGFONTW lf;
1378 HFONT old_font, unscaled_font;
1379 UINT em_size = 1024;
1380 RECT rc;
1382 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1383 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1385 if(deviation <= 0.0)
1386 deviation = 1.0/em_size;
1388 if(format == WGL_FONT_POLYGONS)
1390 if (!load_libglu())
1392 ERR("glu32 is required for this function but isn't available\n");
1393 return FALSE;
1396 tess = pgluNewTess();
1397 if(!tess) return FALSE;
1398 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
1399 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
1400 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1403 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1404 rc.left = rc.right = rc.bottom = 0;
1405 rc.top = em_size;
1406 DPtoLP(hdc, (POINT*)&rc, 2);
1407 lf.lfHeight = -abs(rc.top - rc.bottom);
1408 lf.lfOrientation = lf.lfEscapement = 0;
1409 unscaled_font = CreateFontIndirectW(&lf);
1410 old_font = SelectObject(hdc, unscaled_font);
1412 for (glyph = first; glyph < first + count; glyph++)
1414 DWORD needed;
1415 GLYPHMETRICS gm;
1416 BYTE *buf;
1417 TTPOLYGONHEADER *pph;
1418 TTPOLYCURVE *ppc;
1419 GLdouble *vertices = NULL;
1420 int vertex_total = -1;
1422 if(unicode)
1423 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1424 else
1425 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1427 if(needed == GDI_ERROR)
1428 goto error;
1430 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1432 if(unicode)
1433 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1434 else
1435 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1437 TRACE("glyph %d\n", glyph);
1439 if(lpgmf)
1441 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1442 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1443 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1444 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1445 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1446 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1448 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1449 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1450 lpgmf++;
1453 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1454 funcs->gl.p_glFrontFace(GL_CCW);
1455 if(format == WGL_FONT_POLYGONS)
1457 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1458 pgluTessNormal(tess, 0, 0, 1);
1459 pgluTessBeginPolygon(tess, NULL);
1462 while(!vertices)
1464 if(vertex_total != -1)
1465 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1466 vertex_total = 0;
1468 pph = (TTPOLYGONHEADER*)buf;
1469 while((BYTE*)pph < buf + needed)
1471 GLdouble previous[3];
1472 fixed_to_double(pph->pfxStart, em_size, previous);
1474 if(vertices)
1475 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1477 if(format == WGL_FONT_POLYGONS)
1478 pgluTessBeginContour(tess);
1479 else
1480 funcs->gl.p_glBegin(GL_LINE_LOOP);
1482 if(vertices)
1484 fixed_to_double(pph->pfxStart, em_size, vertices);
1485 if(format == WGL_FONT_POLYGONS)
1486 pgluTessVertex(tess, vertices, vertices);
1487 else
1488 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1489 vertices += 3;
1491 vertex_total++;
1493 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1494 while((char*)ppc < (char*)pph + pph->cb)
1496 int i, j;
1497 int num;
1499 switch(ppc->wType) {
1500 case TT_PRIM_LINE:
1501 for(i = 0; i < ppc->cpfx; i++)
1503 if(vertices)
1505 TRACE("\t\tline to %d, %d\n",
1506 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1507 fixed_to_double(ppc->apfx[i], em_size, vertices);
1508 if(format == WGL_FONT_POLYGONS)
1509 pgluTessVertex(tess, vertices, vertices);
1510 else
1511 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1512 vertices += 3;
1514 fixed_to_double(ppc->apfx[i], em_size, previous);
1515 vertex_total++;
1517 break;
1519 case TT_PRIM_QSPLINE:
1520 for(i = 0; i < ppc->cpfx-1; i++)
1522 bezier_vector curve[3];
1523 bezier_vector *points;
1524 GLdouble curve_vertex[3];
1526 if(vertices)
1527 TRACE("\t\tcurve %d,%d %d,%d\n",
1528 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1529 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1531 curve[0].x = previous[0];
1532 curve[0].y = previous[1];
1533 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1534 curve[1].x = curve_vertex[0];
1535 curve[1].y = curve_vertex[1];
1536 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1537 curve[2].x = curve_vertex[0];
1538 curve[2].y = curve_vertex[1];
1539 if(i < ppc->cpfx-2)
1541 curve[2].x = (curve[1].x + curve[2].x)/2;
1542 curve[2].y = (curve[1].y + curve[2].y)/2;
1544 num = bezier_approximate(curve, NULL, deviation);
1545 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1546 num = bezier_approximate(curve, points, deviation);
1547 vertex_total += num;
1548 if(vertices)
1550 for(j=0; j<num; j++)
1552 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1553 vertices[0] = points[j].x;
1554 vertices[1] = points[j].y;
1555 vertices[2] = 0.0;
1556 if(format == WGL_FONT_POLYGONS)
1557 pgluTessVertex(tess, vertices, vertices);
1558 else
1559 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1560 vertices += 3;
1563 HeapFree(GetProcessHeap(), 0, points);
1564 previous[0] = curve[2].x;
1565 previous[1] = curve[2].y;
1567 break;
1568 default:
1569 ERR("\t\tcurve type = %d\n", ppc->wType);
1570 if(format == WGL_FONT_POLYGONS)
1571 pgluTessEndContour(tess);
1572 else
1573 funcs->gl.p_glEnd();
1574 goto error_in_list;
1577 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1578 (ppc->cpfx - 1) * sizeof(POINTFX));
1580 if(format == WGL_FONT_POLYGONS)
1581 pgluTessEndContour(tess);
1582 else
1583 funcs->gl.p_glEnd();
1584 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1588 error_in_list:
1589 if(format == WGL_FONT_POLYGONS)
1590 pgluTessEndPolygon(tess);
1591 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1592 funcs->gl.p_glEndList();
1593 HeapFree(GetProcessHeap(), 0, buf);
1594 HeapFree(GetProcessHeap(), 0, vertices);
1597 error:
1598 DeleteObject(SelectObject(hdc, old_font));
1599 if(format == WGL_FONT_POLYGONS)
1600 pgluDeleteTess(tess);
1601 return TRUE;
1605 /***********************************************************************
1606 * wglUseFontOutlinesA (OPENGL32.@)
1608 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1609 DWORD first,
1610 DWORD count,
1611 DWORD listBase,
1612 FLOAT deviation,
1613 FLOAT extrusion,
1614 int format,
1615 LPGLYPHMETRICSFLOAT lpgmf)
1617 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1620 /***********************************************************************
1621 * wglUseFontOutlinesW (OPENGL32.@)
1623 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1624 DWORD first,
1625 DWORD count,
1626 DWORD listBase,
1627 FLOAT deviation,
1628 FLOAT extrusion,
1629 int format,
1630 LPGLYPHMETRICSFLOAT lpgmf)
1632 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1635 /***********************************************************************
1636 * glDebugEntry (OPENGL32.@)
1638 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1640 return 0;
1643 /* build the extension string by filtering out the disabled extensions */
1644 static GLubyte *filter_extensions( const char *extensions )
1646 static const char *disabled;
1647 char *p, *str;
1648 const char *end;
1650 TRACE( "GL_EXTENSIONS:\n" );
1652 if (!extensions) extensions = "";
1654 if (!disabled)
1656 HKEY hkey;
1657 DWORD size;
1659 str = NULL;
1660 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1661 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1663 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1665 str = HeapAlloc( GetProcessHeap(), 0, size );
1666 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1668 RegCloseKey( hkey );
1670 if (str)
1672 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1673 HeapFree( GetProcessHeap(), 0, str );
1675 else disabled = "";
1678 if (!disabled[0]) return NULL;
1679 if ((str = HeapAlloc( GetProcessHeap(), 0, strlen(extensions) + 2 )))
1681 p = str;
1682 for (;;)
1684 while (*extensions == ' ') extensions++;
1685 if (!*extensions) break;
1686 if (!(end = strchr( extensions, ' ' ))) end = extensions + strlen( extensions );
1687 memcpy( p, extensions, end - extensions );
1688 p[end - extensions] = 0;
1689 if (!has_extension( disabled, p , strlen( p )))
1691 TRACE("++ %s\n", p );
1692 p += end - extensions;
1693 *p++ = ' ';
1695 else TRACE("-- %s (disabled by config)\n", p );
1696 extensions = end;
1698 *p = 0;
1700 return (GLubyte *)str;
1703 /***********************************************************************
1704 * glGetString (OPENGL32.@)
1706 const GLubyte * WINAPI glGetString( GLenum name )
1708 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1709 const GLubyte *ret = funcs->gl.p_glGetString( name );
1711 if (name == GL_EXTENSIONS && ret)
1713 struct wgl_handle *ptr = get_current_context_ptr();
1714 if (ptr->u.context->extensions ||
1715 ((ptr->u.context->extensions = filter_extensions( (const char *)ret ))))
1716 ret = ptr->u.context->extensions;
1718 return ret;
1721 /***********************************************************************
1722 * OpenGL initialisation routine
1724 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1726 switch(reason)
1728 case DLL_PROCESS_ATTACH:
1729 NtCurrentTeb()->glTable = &null_opengl_funcs;
1730 break;
1731 case DLL_THREAD_ATTACH:
1732 NtCurrentTeb()->glTable = &null_opengl_funcs;
1733 break;
1735 return TRUE;