windowscodecs/tests: Use BOOL type where appropriate.
[wine.git] / dlls / opengl32 / wgl.c
blob361a1ecb4cb1ba1d95ce08daa8140c2b45ea1614
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 0;
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 )
670 size_t len = strlen( ext );
672 while (list)
674 while (*list == ' ') list++;
675 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
676 list = strchr( list, ' ' );
678 return FALSE;
681 static int compar(const void *elt_a, const void *elt_b) {
682 return strcmp(((const OpenGL_extension *) elt_a)->name,
683 ((const OpenGL_extension *) elt_b)->name);
686 /* Check if a GL extension is supported */
687 static BOOL is_extension_supported(const char* extension)
689 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
690 const char *gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
692 TRACE("Checking for extension '%s'\n", extension);
694 if(!gl_ext_string) {
695 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
696 return FALSE;
699 /* We use the GetProcAddress function from the display driver to retrieve function pointers
700 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
701 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
702 * require the function to return NULL when an extension isn't found. For this reason we check
703 * if the OpenGL extension required for the function we are looking up is supported. */
705 /* Check if the extension is part of the GL extension string to see if it is supported. */
706 if (has_extension(gl_ext_string, extension))
707 return TRUE;
709 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
710 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
711 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
712 * Check if we are searching for a core GL function */
713 if(strncmp(extension, "GL_VERSION_", 11) == 0)
715 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
716 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
718 if(!gl_version) {
719 ERR("No OpenGL version found!\n");
720 return FALSE;
723 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
724 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
725 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
726 return TRUE;
728 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]);
731 return FALSE;
734 /***********************************************************************
735 * wglGetProcAddress (OPENGL32.@)
737 PROC WINAPI wglGetProcAddress( LPCSTR name )
739 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
740 void **func_ptr;
741 OpenGL_extension ext;
742 const OpenGL_extension *ext_ret;
744 if (!name) return NULL;
746 /* Without an active context opengl32 doesn't know to what
747 * driver it has to dispatch wglGetProcAddress.
749 if (!get_current_context_ptr())
751 WARN("No active WGL context found\n");
752 return NULL;
755 ext.name = name;
756 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
757 if (!ext_ret)
759 WARN("Function %s unknown\n", name);
760 return NULL;
763 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
764 if (!*func_ptr)
766 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
768 if (!is_extension_supported(ext_ret->extension))
769 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
771 if (driver_func == NULL)
773 WARN("Function %s not supported by driver\n", name);
774 return NULL;
776 *func_ptr = driver_func;
779 TRACE("returning %s -> %p\n", name, ext_ret->func);
780 return ext_ret->func;
783 /***********************************************************************
784 * wglRealizeLayerPalette (OPENGL32.@)
786 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
787 int iLayerPlane,
788 BOOL bRealize) {
789 FIXME("()\n");
791 return FALSE;
794 /***********************************************************************
795 * wglSetLayerPaletteEntries (OPENGL32.@)
797 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
798 int iLayerPlane,
799 int iStart,
800 int cEntries,
801 const COLORREF *pcr) {
802 FIXME("(): stub!\n");
804 return 0;
807 /***********************************************************************
808 * wglSwapLayerBuffers (OPENGL32.@)
810 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
811 UINT fuPlanes) {
812 TRACE("(%p, %08x)\n", hdc, fuPlanes);
814 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
815 if (!wglSwapBuffers( hdc )) return FALSE;
816 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
819 if (fuPlanes) {
820 WARN("Following layers unhandled: %08x\n", fuPlanes);
823 return TRUE;
826 /***********************************************************************
827 * wglAllocateMemoryNV
829 * Provided by the WGL_NV_vertex_array_range extension.
831 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
833 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
835 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
836 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
839 /***********************************************************************
840 * wglFreeMemoryNV
842 * Provided by the WGL_NV_vertex_array_range extension.
844 void WINAPI wglFreeMemoryNV( void *pointer )
846 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
848 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
851 /***********************************************************************
852 * wglBindTexImageARB
854 * Provided by the WGL_ARB_render_texture extension.
856 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
858 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
859 BOOL ret;
861 if (!ptr) return FALSE;
862 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
863 release_handle_ptr( ptr );
864 return ret;
867 /***********************************************************************
868 * wglReleaseTexImageARB
870 * Provided by the WGL_ARB_render_texture extension.
872 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
874 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
875 BOOL ret;
877 if (!ptr) return FALSE;
878 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
879 release_handle_ptr( ptr );
880 return ret;
883 /***********************************************************************
884 * wglSetPbufferAttribARB
886 * Provided by the WGL_ARB_render_texture extension.
888 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
890 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
891 BOOL ret;
893 if (!ptr) return FALSE;
894 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
895 release_handle_ptr( ptr );
896 return ret;
899 /***********************************************************************
900 * wglChoosePixelFormatARB
902 * Provided by the WGL_ARB_pixel_format extension.
904 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
905 UINT max, int *formats, UINT *count )
907 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
909 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
910 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
913 /***********************************************************************
914 * wglGetPixelFormatAttribivARB
916 * Provided by the WGL_ARB_pixel_format extension.
918 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
919 int *values )
921 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
923 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
924 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
927 /***********************************************************************
928 * wglGetPixelFormatAttribfvARB
930 * Provided by the WGL_ARB_pixel_format extension.
932 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
933 FLOAT *values )
935 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
937 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
938 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
941 /***********************************************************************
942 * wglCreatePbufferARB
944 * Provided by the WGL_ARB_pbuffer extension.
946 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
948 HPBUFFERARB ret = 0;
949 struct wgl_pbuffer *pbuffer;
950 struct opengl_funcs *funcs = get_dc_funcs( hdc );
952 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
953 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
954 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
955 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
956 return ret;
959 /***********************************************************************
960 * wglGetPbufferDCARB
962 * Provided by the WGL_ARB_pbuffer extension.
964 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
966 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
967 HDC ret;
969 if (!ptr) return 0;
970 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
971 release_handle_ptr( ptr );
972 return ret;
975 /***********************************************************************
976 * wglReleasePbufferDCARB
978 * Provided by the WGL_ARB_pbuffer extension.
980 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
982 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
983 BOOL ret;
985 if (!ptr) return FALSE;
986 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
987 release_handle_ptr( ptr );
988 return ret;
991 /***********************************************************************
992 * wglDestroyPbufferARB
994 * Provided by the WGL_ARB_pbuffer extension.
996 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
998 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1000 if (!ptr) return FALSE;
1001 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1002 free_handle_ptr( ptr );
1003 return TRUE;
1006 /***********************************************************************
1007 * wglQueryPbufferARB
1009 * Provided by the WGL_ARB_pbuffer extension.
1011 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1013 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1014 BOOL ret;
1016 if (!ptr) return FALSE;
1017 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1018 release_handle_ptr( ptr );
1019 return ret;
1022 /***********************************************************************
1023 * wglGetExtensionsStringARB
1025 * Provided by the WGL_ARB_extensions_string extension.
1027 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1029 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1031 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1032 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1035 /***********************************************************************
1036 * wglGetExtensionsStringEXT
1038 * Provided by the WGL_EXT_extensions_string extension.
1040 const char * WINAPI wglGetExtensionsStringEXT(void)
1042 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1044 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1045 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1048 /***********************************************************************
1049 * wglSwapIntervalEXT
1051 * Provided by the WGL_EXT_swap_control extension.
1053 BOOL WINAPI wglSwapIntervalEXT( int interval )
1055 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1057 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1058 return funcs->ext.p_wglSwapIntervalEXT( interval );
1061 /***********************************************************************
1062 * wglGetSwapIntervalEXT
1064 * Provided by the WGL_EXT_swap_control extension.
1066 int WINAPI wglGetSwapIntervalEXT(void)
1068 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1070 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1071 return funcs->ext.p_wglGetSwapIntervalEXT();
1074 /***********************************************************************
1075 * wglSetPixelFormatWINE
1077 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1079 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1081 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1083 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1084 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1087 /***********************************************************************
1088 * wglUseFontBitmaps_common
1090 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1092 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1093 GLYPHMETRICS gm;
1094 unsigned int glyph, size = 0;
1095 void *bitmap = NULL, *gl_bitmap = NULL;
1096 int org_alignment;
1097 BOOL ret = TRUE;
1099 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1100 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1102 for (glyph = first; glyph < first + count; glyph++) {
1103 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1104 unsigned int needed_size, height, width, width_int;
1106 if (unicode)
1107 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1108 else
1109 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1111 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1112 if (needed_size == GDI_ERROR) {
1113 ret = FALSE;
1114 break;
1117 if (needed_size > size) {
1118 size = needed_size;
1119 HeapFree(GetProcessHeap(), 0, bitmap);
1120 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1121 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1122 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1124 if (needed_size != 0) {
1125 if (unicode)
1126 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1127 size, bitmap, &identity) != GDI_ERROR);
1128 else
1129 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1130 size, bitmap, &identity) != GDI_ERROR);
1131 if (!ret) break;
1134 if (TRACE_ON(wgl)) {
1135 unsigned int bitmask;
1136 unsigned char *bitmap_ = bitmap;
1138 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1139 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1140 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1141 if (needed_size != 0) {
1142 TRACE(" - bitmap:\n");
1143 for (height = 0; height < gm.gmBlackBoxY; height++) {
1144 TRACE(" ");
1145 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1146 if (bitmask == 0) {
1147 bitmap_ += 1;
1148 bitmask = 0x80;
1150 if (*bitmap_ & bitmask)
1151 TRACE("*");
1152 else
1153 TRACE(" ");
1155 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1156 TRACE("\n");
1161 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1162 * glyph for it to be drawn properly.
1164 if (needed_size != 0) {
1165 width_int = (gm.gmBlackBoxX + 31) / 32;
1166 for (height = 0; height < gm.gmBlackBoxY; height++) {
1167 for (width = 0; width < width_int; width++) {
1168 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1169 ((int *) bitmap)[height * width_int + width];
1174 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1175 if (needed_size != 0) {
1176 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1177 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1178 gm.gmCellIncX, gm.gmCellIncY,
1179 gl_bitmap);
1180 } else {
1181 /* This is the case of 'empty' glyphs like the space character */
1182 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1184 funcs->gl.p_glEndList();
1187 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1188 HeapFree(GetProcessHeap(), 0, bitmap);
1189 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1190 return ret;
1193 /***********************************************************************
1194 * wglUseFontBitmapsA (OPENGL32.@)
1196 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1198 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1201 /***********************************************************************
1202 * wglUseFontBitmapsW (OPENGL32.@)
1204 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1206 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1209 /* FIXME: should probably have a glu.h header */
1211 typedef struct GLUtesselator GLUtesselator;
1212 typedef void (WINAPI *_GLUfuncptr)(void);
1214 #define GLU_TESS_BEGIN 100100
1215 #define GLU_TESS_VERTEX 100101
1216 #define GLU_TESS_END 100102
1218 static GLUtesselator * (WINAPI *pgluNewTess)(void);
1219 static void (WINAPI *pgluDeleteTess)(GLUtesselator *tess);
1220 static void (WINAPI *pgluTessNormal)(GLUtesselator *tess, GLdouble x, GLdouble y, GLdouble z);
1221 static void (WINAPI *pgluTessBeginPolygon)(GLUtesselator *tess, void *polygon_data);
1222 static void (WINAPI *pgluTessEndPolygon)(GLUtesselator *tess);
1223 static void (WINAPI *pgluTessCallback)(GLUtesselator *tess, GLenum which, _GLUfuncptr fn);
1224 static void (WINAPI *pgluTessBeginContour)(GLUtesselator *tess);
1225 static void (WINAPI *pgluTessEndContour)(GLUtesselator *tess);
1226 static void (WINAPI *pgluTessVertex)(GLUtesselator *tess, GLdouble *location, GLvoid* data);
1228 static HMODULE load_libglu(void)
1230 static const WCHAR glu32W[] = {'g','l','u','3','2','.','d','l','l',0};
1231 static int already_loaded;
1232 static HMODULE module;
1234 if (already_loaded) return module;
1235 already_loaded = 1;
1237 TRACE("Trying to load GLU library\n");
1238 module = LoadLibraryW( glu32W );
1239 if (!module)
1241 WARN("Failed to load glu32\n");
1242 return NULL;
1244 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1245 LOAD_FUNCPTR(gluNewTess);
1246 LOAD_FUNCPTR(gluDeleteTess);
1247 LOAD_FUNCPTR(gluTessBeginContour);
1248 LOAD_FUNCPTR(gluTessNormal);
1249 LOAD_FUNCPTR(gluTessBeginPolygon);
1250 LOAD_FUNCPTR(gluTessCallback);
1251 LOAD_FUNCPTR(gluTessEndContour);
1252 LOAD_FUNCPTR(gluTessEndPolygon);
1253 LOAD_FUNCPTR(gluTessVertex);
1254 #undef LOAD_FUNCPTR
1255 return module;
1258 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1260 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1261 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1262 vertex[2] = 0.0;
1265 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1267 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1268 GLdouble *dbl = vertex;
1269 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1270 funcs->gl.p_glVertex3dv(vertex);
1273 static void WINAPI tess_callback_begin(GLenum which)
1275 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1276 TRACE("%d\n", which);
1277 funcs->gl.p_glBegin(which);
1280 static void WINAPI tess_callback_end(void)
1282 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1283 TRACE("\n");
1284 funcs->gl.p_glEnd();
1287 typedef struct _bezier_vector {
1288 GLdouble x;
1289 GLdouble y;
1290 } bezier_vector;
1292 static double bezier_deviation_squared(const bezier_vector *p)
1294 bezier_vector deviation;
1295 bezier_vector vertex;
1296 bezier_vector base;
1297 double base_length;
1298 double dot;
1300 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1301 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1303 base.x = p[2].x - p[0].x;
1304 base.y = p[2].y - p[0].y;
1306 base_length = sqrt(base.x*base.x + base.y*base.y);
1307 base.x /= base_length;
1308 base.y /= base_length;
1310 dot = base.x*vertex.x + base.y*vertex.y;
1311 dot = min(max(dot, 0.0), base_length);
1312 base.x *= dot;
1313 base.y *= dot;
1315 deviation.x = vertex.x-base.x;
1316 deviation.y = vertex.y-base.y;
1318 return deviation.x*deviation.x + deviation.y*deviation.y;
1321 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1323 bezier_vector first_curve[3];
1324 bezier_vector second_curve[3];
1325 bezier_vector vertex;
1326 int total_vertices;
1328 if(bezier_deviation_squared(p) <= deviation*deviation)
1330 if(points)
1331 *points = p[2];
1332 return 1;
1335 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1336 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1338 first_curve[0] = p[0];
1339 first_curve[1].x = (p[0].x + p[1].x)/2;
1340 first_curve[1].y = (p[0].y + p[1].y)/2;
1341 first_curve[2] = vertex;
1343 second_curve[0] = vertex;
1344 second_curve[1].x = (p[2].x + p[1].x)/2;
1345 second_curve[1].y = (p[2].y + p[1].y)/2;
1346 second_curve[2] = p[2];
1348 total_vertices = bezier_approximate(first_curve, points, deviation);
1349 if(points)
1350 points += total_vertices;
1351 total_vertices += bezier_approximate(second_curve, points, deviation);
1352 return total_vertices;
1355 /***********************************************************************
1356 * wglUseFontOutlines_common
1358 static BOOL wglUseFontOutlines_common(HDC hdc,
1359 DWORD first,
1360 DWORD count,
1361 DWORD listBase,
1362 FLOAT deviation,
1363 FLOAT extrusion,
1364 int format,
1365 LPGLYPHMETRICSFLOAT lpgmf,
1366 BOOL unicode)
1368 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1369 UINT glyph;
1370 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
1371 GLUtesselator *tess = NULL;
1372 LOGFONTW lf;
1373 HFONT old_font, unscaled_font;
1374 UINT em_size = 1024;
1375 RECT rc;
1377 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1378 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1380 if(deviation <= 0.0)
1381 deviation = 1.0/em_size;
1383 if(format == WGL_FONT_POLYGONS)
1385 if (!load_libglu())
1387 ERR("glu32 is required for this function but isn't available\n");
1388 return FALSE;
1391 tess = pgluNewTess();
1392 if(!tess) return FALSE;
1393 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
1394 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
1395 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1398 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1399 rc.left = rc.right = rc.bottom = 0;
1400 rc.top = em_size;
1401 DPtoLP(hdc, (POINT*)&rc, 2);
1402 lf.lfHeight = -abs(rc.top - rc.bottom);
1403 lf.lfOrientation = lf.lfEscapement = 0;
1404 unscaled_font = CreateFontIndirectW(&lf);
1405 old_font = SelectObject(hdc, unscaled_font);
1407 for (glyph = first; glyph < first + count; glyph++)
1409 DWORD needed;
1410 GLYPHMETRICS gm;
1411 BYTE *buf;
1412 TTPOLYGONHEADER *pph;
1413 TTPOLYCURVE *ppc;
1414 GLdouble *vertices = NULL;
1415 int vertex_total = -1;
1417 if(unicode)
1418 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1419 else
1420 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1422 if(needed == GDI_ERROR)
1423 goto error;
1425 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1427 if(unicode)
1428 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1429 else
1430 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1432 TRACE("glyph %d\n", glyph);
1434 if(lpgmf)
1436 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1437 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1438 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1439 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1440 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1441 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1443 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1444 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1445 lpgmf++;
1448 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1449 funcs->gl.p_glFrontFace(GL_CCW);
1450 if(format == WGL_FONT_POLYGONS)
1452 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1453 pgluTessNormal(tess, 0, 0, 1);
1454 pgluTessBeginPolygon(tess, NULL);
1457 while(!vertices)
1459 if(vertex_total != -1)
1460 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1461 vertex_total = 0;
1463 pph = (TTPOLYGONHEADER*)buf;
1464 while((BYTE*)pph < buf + needed)
1466 GLdouble previous[3];
1467 fixed_to_double(pph->pfxStart, em_size, previous);
1469 if(vertices)
1470 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1472 if(format == WGL_FONT_POLYGONS)
1473 pgluTessBeginContour(tess);
1474 else
1475 funcs->gl.p_glBegin(GL_LINE_LOOP);
1477 if(vertices)
1479 fixed_to_double(pph->pfxStart, em_size, vertices);
1480 if(format == WGL_FONT_POLYGONS)
1481 pgluTessVertex(tess, vertices, vertices);
1482 else
1483 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1484 vertices += 3;
1486 vertex_total++;
1488 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1489 while((char*)ppc < (char*)pph + pph->cb)
1491 int i, j;
1492 int num;
1494 switch(ppc->wType) {
1495 case TT_PRIM_LINE:
1496 for(i = 0; i < ppc->cpfx; i++)
1498 if(vertices)
1500 TRACE("\t\tline to %d, %d\n",
1501 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1502 fixed_to_double(ppc->apfx[i], em_size, vertices);
1503 if(format == WGL_FONT_POLYGONS)
1504 pgluTessVertex(tess, vertices, vertices);
1505 else
1506 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1507 vertices += 3;
1509 fixed_to_double(ppc->apfx[i], em_size, previous);
1510 vertex_total++;
1512 break;
1514 case TT_PRIM_QSPLINE:
1515 for(i = 0; i < ppc->cpfx-1; i++)
1517 bezier_vector curve[3];
1518 bezier_vector *points;
1519 GLdouble curve_vertex[3];
1521 if(vertices)
1522 TRACE("\t\tcurve %d,%d %d,%d\n",
1523 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1524 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1526 curve[0].x = previous[0];
1527 curve[0].y = previous[1];
1528 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1529 curve[1].x = curve_vertex[0];
1530 curve[1].y = curve_vertex[1];
1531 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1532 curve[2].x = curve_vertex[0];
1533 curve[2].y = curve_vertex[1];
1534 if(i < ppc->cpfx-2)
1536 curve[2].x = (curve[1].x + curve[2].x)/2;
1537 curve[2].y = (curve[1].y + curve[2].y)/2;
1539 num = bezier_approximate(curve, NULL, deviation);
1540 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1541 num = bezier_approximate(curve, points, deviation);
1542 vertex_total += num;
1543 if(vertices)
1545 for(j=0; j<num; j++)
1547 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1548 vertices[0] = points[j].x;
1549 vertices[1] = points[j].y;
1550 vertices[2] = 0.0;
1551 if(format == WGL_FONT_POLYGONS)
1552 pgluTessVertex(tess, vertices, vertices);
1553 else
1554 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1555 vertices += 3;
1558 HeapFree(GetProcessHeap(), 0, points);
1559 previous[0] = curve[2].x;
1560 previous[1] = curve[2].y;
1562 break;
1563 default:
1564 ERR("\t\tcurve type = %d\n", ppc->wType);
1565 if(format == WGL_FONT_POLYGONS)
1566 pgluTessEndContour(tess);
1567 else
1568 funcs->gl.p_glEnd();
1569 goto error_in_list;
1572 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1573 (ppc->cpfx - 1) * sizeof(POINTFX));
1575 if(format == WGL_FONT_POLYGONS)
1576 pgluTessEndContour(tess);
1577 else
1578 funcs->gl.p_glEnd();
1579 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1583 error_in_list:
1584 if(format == WGL_FONT_POLYGONS)
1585 pgluTessEndPolygon(tess);
1586 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1587 funcs->gl.p_glEndList();
1588 HeapFree(GetProcessHeap(), 0, buf);
1589 HeapFree(GetProcessHeap(), 0, vertices);
1592 error:
1593 DeleteObject(SelectObject(hdc, old_font));
1594 if(format == WGL_FONT_POLYGONS)
1595 pgluDeleteTess(tess);
1596 return TRUE;
1600 /***********************************************************************
1601 * wglUseFontOutlinesA (OPENGL32.@)
1603 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1604 DWORD first,
1605 DWORD count,
1606 DWORD listBase,
1607 FLOAT deviation,
1608 FLOAT extrusion,
1609 int format,
1610 LPGLYPHMETRICSFLOAT lpgmf)
1612 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1615 /***********************************************************************
1616 * wglUseFontOutlinesW (OPENGL32.@)
1618 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1619 DWORD first,
1620 DWORD count,
1621 DWORD listBase,
1622 FLOAT deviation,
1623 FLOAT extrusion,
1624 int format,
1625 LPGLYPHMETRICSFLOAT lpgmf)
1627 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1630 /***********************************************************************
1631 * glDebugEntry (OPENGL32.@)
1633 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1635 return 0;
1638 /* build the extension string by filtering out the disabled extensions */
1639 static GLubyte *filter_extensions( const char *extensions )
1641 static const char *disabled;
1642 char *p, *str;
1643 const char *end;
1645 TRACE( "GL_EXTENSIONS:\n" );
1647 if (!extensions) extensions = "";
1649 if (!disabled)
1651 HKEY hkey;
1652 DWORD size;
1654 str = NULL;
1655 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1656 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1658 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1660 str = HeapAlloc( GetProcessHeap(), 0, size );
1661 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1663 RegCloseKey( hkey );
1665 if (str)
1667 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1668 HeapFree( GetProcessHeap(), 0, str );
1670 else disabled = "";
1673 if (!disabled[0]) return NULL;
1674 if ((str = HeapAlloc( GetProcessHeap(), 0, strlen(extensions) + 2 )))
1676 p = str;
1677 for (;;)
1679 while (*extensions == ' ') extensions++;
1680 if (!*extensions) break;
1681 if (!(end = strchr( extensions, ' ' ))) end = extensions + strlen( extensions );
1682 memcpy( p, extensions, end - extensions );
1683 p[end - extensions] = 0;
1684 if (!has_extension( disabled, p ))
1686 TRACE("++ %s\n", p );
1687 p += end - extensions;
1688 *p++ = ' ';
1690 else TRACE("-- %s (disabled by config)\n", p );
1691 extensions = end;
1693 *p = 0;
1695 return (GLubyte *)str;
1698 /***********************************************************************
1699 * glGetString (OPENGL32.@)
1701 const GLubyte * WINAPI glGetString( GLenum name )
1703 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1704 const GLubyte *ret = funcs->gl.p_glGetString( name );
1706 if (name == GL_EXTENSIONS && ret)
1708 struct wgl_handle *ptr = get_current_context_ptr();
1709 if (ptr->u.context->extensions ||
1710 ((ptr->u.context->extensions = filter_extensions( (const char *)ret ))))
1711 ret = ptr->u.context->extensions;
1713 return ret;
1716 /***********************************************************************
1717 * OpenGL initialisation routine
1719 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1721 switch(reason)
1723 case DLL_PROCESS_ATTACH:
1724 DisableThreadLibraryCalls(hinst);
1725 NtCurrentTeb()->glTable = &null_opengl_funcs;
1726 break;
1727 case DLL_THREAD_ATTACH:
1728 NtCurrentTeb()->glTable = &null_opengl_funcs;
1729 break;
1731 return TRUE;