kernel32: Update version to Win 10.
[wine.git] / dlls / opengl32 / wgl.c
blobcf46c6dfa8a54a1baac0a7766d0d532e6afdf9a3
1 /* Window-specific OpenGL functions implementation.
3 * Copyright (c) 1999 Lionel Ulmer
4 * Copyright (c) 2005 Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "wingdi.h"
33 #include "winternl.h"
34 #include "winnt.h"
36 #include "opengl_ext.h"
37 #include "wine/gdi_driver.h"
38 #include "wine/glu.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
42 WINE_DECLARE_DEBUG_CHANNEL(fps);
44 /* handle management */
46 #define MAX_WGL_HANDLES 1024
48 enum wgl_handle_type
50 HANDLE_PBUFFER = 0 << 12,
51 HANDLE_CONTEXT = 1 << 12,
52 HANDLE_CONTEXT_V3 = 3 << 12,
53 HANDLE_TYPE_MASK = 15 << 12
56 struct opengl_context
58 DWORD tid; /* thread that the context is current in */
59 HDC draw_dc; /* current drawing DC */
60 HDC read_dc; /* current reading DC */
61 void (CALLBACK *debug_callback)(GLenum, GLenum, GLuint, GLenum,
62 GLsizei, const GLchar *, const void *); /* debug callback */
63 const void *debug_user; /* debug user parameter */
64 GLubyte *extensions; /* extension string */
65 GLuint *disabled_exts; /* indices of disabled extensions */
66 struct wgl_context *drv_ctx; /* driver context */
69 struct wgl_handle
71 UINT handle;
72 struct opengl_funcs *funcs;
73 union
75 struct opengl_context *context; /* for HANDLE_CONTEXT */
76 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
77 struct wgl_handle *next; /* for free handles */
78 } u;
81 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
82 static struct wgl_handle *next_free;
83 static unsigned int handle_count;
85 static CRITICAL_SECTION wgl_section;
86 static CRITICAL_SECTION_DEBUG critsect_debug =
88 0, 0, &wgl_section,
89 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
90 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
92 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
94 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
96 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
98 WORD generation = HIWORD( ptr->handle ) + 1;
99 if (!generation) generation++;
100 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
101 return ULongToHandle( ptr->handle );
104 /* the current context is assumed valid and doesn't need locking */
105 static inline struct wgl_handle *get_current_context_ptr(void)
107 if (!NtCurrentTeb()->glCurrentRC) return NULL;
108 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
111 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
113 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
115 EnterCriticalSection( &wgl_section );
116 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
117 return &wgl_handles[index];
119 LeaveCriticalSection( &wgl_section );
120 SetLastError( ERROR_INVALID_HANDLE );
121 return NULL;
124 static void release_handle_ptr( struct wgl_handle *ptr )
126 if (ptr) LeaveCriticalSection( &wgl_section );
129 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
131 HANDLE handle = 0;
132 struct wgl_handle *ptr = NULL;
134 EnterCriticalSection( &wgl_section );
135 if ((ptr = next_free))
136 next_free = next_free->u.next;
137 else if (handle_count < MAX_WGL_HANDLES)
138 ptr = &wgl_handles[handle_count++];
140 if (ptr)
142 ptr->funcs = funcs;
143 ptr->u.context = user_ptr;
144 handle = next_handle( ptr, type );
146 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
147 LeaveCriticalSection( &wgl_section );
148 return handle;
151 static void free_handle_ptr( struct wgl_handle *ptr )
153 ptr->handle |= 0xffff;
154 ptr->u.next = next_free;
155 ptr->funcs = NULL;
156 next_free = ptr;
157 LeaveCriticalSection( &wgl_section );
160 static inline enum wgl_handle_type get_current_context_type(void)
162 if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
163 return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
166 /***********************************************************************
167 * wglCopyContext (OPENGL32.@)
169 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
171 struct wgl_handle *src, *dst;
172 BOOL ret = FALSE;
174 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
175 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
177 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
178 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
179 dst->u.context->drv_ctx, mask );
181 release_handle_ptr( dst );
182 release_handle_ptr( src );
183 return ret;
186 /***********************************************************************
187 * wglDeleteContext (OPENGL32.@)
189 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
191 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
193 if (!ptr) return FALSE;
195 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
197 SetLastError( ERROR_BUSY );
198 release_handle_ptr( ptr );
199 return FALSE;
201 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
202 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
203 HeapFree( GetProcessHeap(), 0, ptr->u.context->disabled_exts );
204 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
205 HeapFree( GetProcessHeap(), 0, ptr->u.context );
206 free_handle_ptr( ptr );
207 return TRUE;
210 /***********************************************************************
211 * wglMakeCurrent (OPENGL32.@)
213 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
215 BOOL ret = TRUE;
216 struct wgl_handle *ptr, *prev = get_current_context_ptr();
218 if (hglrc)
220 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
221 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
223 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
224 if (ret)
226 if (prev) prev->u.context->tid = 0;
227 ptr->u.context->tid = GetCurrentThreadId();
228 ptr->u.context->draw_dc = hdc;
229 ptr->u.context->read_dc = hdc;
230 NtCurrentTeb()->glCurrentRC = hglrc;
231 NtCurrentTeb()->glTable = ptr->funcs;
234 else
236 SetLastError( ERROR_BUSY );
237 ret = FALSE;
239 release_handle_ptr( ptr );
241 else if (prev)
243 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
244 prev->u.context->tid = 0;
245 NtCurrentTeb()->glCurrentRC = 0;
246 NtCurrentTeb()->glTable = &null_opengl_funcs;
248 else if (!hdc)
250 SetLastError( ERROR_INVALID_HANDLE );
251 ret = FALSE;
253 return ret;
256 /***********************************************************************
257 * wglCreateContextAttribsARB
259 * Provided by the WGL_ARB_create_context extension.
261 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
263 HGLRC ret = 0;
264 struct wgl_context *drv_ctx;
265 struct wgl_handle *share_ptr = NULL;
266 struct opengl_context *context;
267 struct opengl_funcs *funcs = get_dc_funcs( hdc );
269 if (!funcs)
271 SetLastError( ERROR_DC_NOT_FOUND );
272 return 0;
274 if (!funcs->ext.p_wglCreateContextAttribsARB) return 0;
275 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT )))
277 SetLastError( ERROR_INVALID_OPERATION );
278 return 0;
280 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
281 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
283 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
285 enum wgl_handle_type type = HANDLE_CONTEXT;
287 if (attribs)
289 while (*attribs)
291 if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
293 if (attribs[1] >= 3)
294 type = HANDLE_CONTEXT_V3;
295 break;
297 attribs += 2;
301 context->drv_ctx = drv_ctx;
302 if (!(ret = alloc_handle( type, funcs, context )))
303 HeapFree( GetProcessHeap(), 0, context );
305 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
307 release_handle_ptr( share_ptr );
308 return ret;
312 /***********************************************************************
313 * wglMakeContextCurrentARB
315 * Provided by the WGL_ARB_make_current_read extension.
317 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
319 BOOL ret = TRUE;
320 struct wgl_handle *ptr, *prev = get_current_context_ptr();
322 if (hglrc)
324 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
325 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
327 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
328 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
329 ptr->u.context->drv_ctx ));
330 if (ret)
332 if (prev) prev->u.context->tid = 0;
333 ptr->u.context->tid = GetCurrentThreadId();
334 ptr->u.context->draw_dc = draw_hdc;
335 ptr->u.context->read_dc = read_hdc;
336 NtCurrentTeb()->glCurrentRC = hglrc;
337 NtCurrentTeb()->glTable = ptr->funcs;
340 else
342 SetLastError( ERROR_BUSY );
343 ret = FALSE;
345 release_handle_ptr( ptr );
347 else if (prev)
349 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
350 prev->u.context->tid = 0;
351 NtCurrentTeb()->glCurrentRC = 0;
352 NtCurrentTeb()->glTable = &null_opengl_funcs;
354 return ret;
357 /***********************************************************************
358 * wglGetCurrentReadDCARB
360 * Provided by the WGL_ARB_make_current_read extension.
362 HDC WINAPI wglGetCurrentReadDCARB(void)
364 struct wgl_handle *ptr = get_current_context_ptr();
366 if (!ptr) return 0;
367 return ptr->u.context->read_dc;
370 /***********************************************************************
371 * wglShareLists (OPENGL32.@)
373 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
375 BOOL ret = FALSE;
376 struct wgl_handle *src, *dst;
378 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
379 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
381 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
382 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
384 release_handle_ptr( dst );
385 release_handle_ptr( src );
386 return ret;
389 /***********************************************************************
390 * wglGetCurrentDC (OPENGL32.@)
392 HDC WINAPI wglGetCurrentDC(void)
394 struct wgl_handle *ptr = get_current_context_ptr();
396 if (!ptr) return 0;
397 return ptr->u.context->draw_dc;
400 /***********************************************************************
401 * wgl_create_context wrapper for hooking
403 static HGLRC wgl_create_context(HDC hdc)
405 HGLRC ret = 0;
406 struct wgl_context *drv_ctx;
407 struct opengl_context *context;
408 struct opengl_funcs *funcs = get_dc_funcs( hdc );
410 if (!funcs) return 0;
411 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
412 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
414 context->drv_ctx = drv_ctx;
415 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
416 HeapFree( GetProcessHeap(), 0, context );
418 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
419 return ret;
422 /***********************************************************************
423 * wglCreateContext (OPENGL32.@)
425 HGLRC WINAPI wglCreateContext(HDC hdc)
427 return wgl_create_context(hdc);
430 /***********************************************************************
431 * wglGetCurrentContext (OPENGL32.@)
433 HGLRC WINAPI wglGetCurrentContext(void)
435 return NtCurrentTeb()->glCurrentRC;
438 /***********************************************************************
439 * wglDescribePixelFormat (OPENGL32.@)
441 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
443 struct opengl_funcs *funcs = get_dc_funcs( hdc );
444 if (!funcs) return 0;
445 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
448 /***********************************************************************
449 * wglChoosePixelFormat (OPENGL32.@)
451 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
453 PIXELFORMATDESCRIPTOR format, best;
454 int i, count, best_format;
455 int bestDBuffer = -1, bestStereo = -1;
457 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
458 "accum %u depth %u stencil %u aux %u\n",
459 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
460 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
461 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
463 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
464 if (!count) return 0;
466 best_format = 0;
467 best.dwFlags = 0;
468 best.cAlphaBits = -1;
469 best.cColorBits = -1;
470 best.cDepthBits = -1;
471 best.cStencilBits = -1;
472 best.cAuxBuffers = -1;
474 for (i = 1; i <= count; i++)
476 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
478 if ((ppfd->iPixelType == PFD_TYPE_COLORINDEX) != (format.iPixelType == PFD_TYPE_COLORINDEX))
480 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
481 continue;
484 /* only use bitmap capable for formats for bitmap rendering */
485 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
487 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
488 continue;
491 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
492 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
493 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
494 * formats without the given flag set.
495 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
496 * has indicated that a format without stereo is returned when stereo is unavailable.
497 * So in case PFD_STEREO is set, formats that support it should have priority above formats
498 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
500 * To summarize the following is most likely the correct behavior:
501 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
502 * stereo set -> prefer stereo formats, but also accept non-stereo formats
503 * stereo don't care -> it doesn't matter whether we get stereo or not
505 * In Wine we will treat non-stereo the same way as don't care because it makes
506 * format selection even more complicated and second drivers with Stereo advertise
507 * each format twice anyway.
510 /* Doublebuffer, see the comments above */
511 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
513 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
514 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
515 goto found;
517 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
519 else if (!best_format)
520 goto found;
522 /* Stereo, see the comments above. */
523 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
525 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
526 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
527 goto found;
529 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
531 else if (!best_format)
532 goto found;
534 /* Below we will do a number of checks to select the 'best' pixelformat.
535 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
536 * The code works by trying to match the most important options as close as possible.
537 * When a reasonable format is found, we will try to match more options.
538 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
539 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
540 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
542 if (ppfd->cColorBits)
544 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
545 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
546 goto found;
548 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
550 TRACE( "color mismatch for iPixelFormat=%d\n", i );
551 continue;
554 if (ppfd->cAlphaBits)
556 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
557 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
558 goto found;
560 if (best.cAlphaBits != format.cAlphaBits)
562 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
563 continue;
566 if (ppfd->cDepthBits && !(ppfd->dwFlags & PFD_DEPTH_DONTCARE))
568 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
569 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
570 goto found;
572 if (best.cDepthBits != format.cDepthBits)
574 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
575 continue;
578 if (ppfd->cStencilBits)
580 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
581 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
582 goto found;
584 if (best.cStencilBits != format.cStencilBits)
586 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
587 continue;
590 if (ppfd->cAuxBuffers)
592 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
593 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
594 goto found;
596 if (best.cAuxBuffers != format.cAuxBuffers)
598 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
599 continue;
602 if (ppfd->dwFlags & PFD_DEPTH_DONTCARE && format.cDepthBits < best.cDepthBits)
603 goto found;
605 continue;
607 found:
608 best_format = i;
609 best = format;
610 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
611 bestStereo = format.dwFlags & PFD_STEREO;
614 TRACE( "returning %u\n", best_format );
615 return best_format;
618 /***********************************************************************
619 * wglGetPixelFormat (OPENGL32.@)
621 INT WINAPI wglGetPixelFormat(HDC hdc)
623 struct opengl_funcs *funcs = get_dc_funcs( hdc );
624 if (!funcs)
626 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
627 return 0;
629 return funcs->wgl.p_wglGetPixelFormat( hdc );
632 /***********************************************************************
633 * wglSetPixelFormat(OPENGL32.@)
635 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
637 struct opengl_funcs *funcs = get_dc_funcs( hdc );
638 if (!funcs) return FALSE;
639 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
642 /***********************************************************************
643 * wglSwapBuffers (OPENGL32.@)
645 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
647 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
649 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
650 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
652 if (TRACE_ON(fps))
654 static long prev_time, start_time;
655 static unsigned long frames, frames_total;
657 DWORD time = GetTickCount();
658 frames++;
659 frames_total++;
660 /* every 1.5 seconds */
661 if (time - prev_time > 1500)
663 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
664 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
665 prev_time = time;
666 frames = 0;
667 if (start_time == 0) start_time = time;
670 return TRUE;
673 /***********************************************************************
674 * wglCreateLayerContext (OPENGL32.@)
676 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
677 int iLayerPlane) {
678 TRACE("(%p,%d)\n", hdc, iLayerPlane);
680 if (iLayerPlane == 0) {
681 return wgl_create_context(hdc);
683 FIXME("no handler for layer %d\n", iLayerPlane);
685 return NULL;
688 /***********************************************************************
689 * wglDescribeLayerPlane (OPENGL32.@)
691 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
692 int iPixelFormat,
693 int iLayerPlane,
694 UINT nBytes,
695 LPLAYERPLANEDESCRIPTOR plpd) {
696 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
698 return FALSE;
701 /***********************************************************************
702 * wglGetLayerPaletteEntries (OPENGL32.@)
704 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
705 int iLayerPlane,
706 int iStart,
707 int cEntries,
708 const COLORREF *pcr) {
709 FIXME("(): stub!\n");
711 return 0;
714 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
716 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
718 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
720 TRACE("(%d, %p)\n", pname, data);
721 if (pname == GL_NUM_EXTENSIONS)
723 struct wgl_handle *ptr = get_current_context_ptr();
725 if (ptr->u.context->disabled_exts ||
726 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
728 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
729 GLint count, disabled_count = 0;
731 funcs->gl.p_glGetIntegerv(pname, &count);
732 while (*disabled_exts++ != ~0u)
733 disabled_count++;
734 *data = count - disabled_count;
735 return;
738 funcs->gl.p_glGetIntegerv(pname, data);
741 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
743 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
745 TRACE("(%d, %d)\n", name, index);
746 if (!funcs->ext.p_glGetStringi)
748 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
750 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
753 if (name == GL_EXTENSIONS)
755 struct wgl_handle *ptr = get_current_context_ptr();
757 if (ptr->u.context->disabled_exts ||
758 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
760 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
761 unsigned int disabled_count = 0;
763 while (index + disabled_count >= *disabled_exts++)
764 disabled_count++;
765 return funcs->ext.p_glGetStringi(name, index + disabled_count);
768 return funcs->ext.p_glGetStringi(name, index);
771 /* check if the extension is present in the list */
772 static BOOL has_extension( const char *list, const char *ext, size_t len )
774 if (!list)
776 const char *gl_ext;
777 unsigned int i;
778 GLint extensions_count;
780 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
781 for (i = 0; i < extensions_count; ++i)
783 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
784 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
785 return TRUE;
787 return FALSE;
790 while (list)
792 while (*list == ' ') list++;
793 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
794 list = strchr( list, ' ' );
796 return FALSE;
799 static int compar(const void *elt_a, const void *elt_b) {
800 return strcmp(((const OpenGL_extension *) elt_a)->name,
801 ((const OpenGL_extension *) elt_b)->name);
804 /* Check if a GL extension is supported */
805 static BOOL is_extension_supported(const char* extension)
807 enum wgl_handle_type type = get_current_context_type();
808 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
809 const char *gl_ext_string = NULL;
810 size_t len;
812 TRACE("Checking for extension '%s'\n", extension);
814 if (type == HANDLE_CONTEXT)
816 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
817 if (!gl_ext_string)
819 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
820 return FALSE;
824 /* We use the GetProcAddress function from the display driver to retrieve function pointers
825 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
826 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
827 * require the function to return NULL when an extension isn't found. For this reason we check
828 * if the OpenGL extension required for the function we are looking up is supported. */
830 while ((len = strcspn(extension, " ")) != 0)
832 /* Check if the extension is part of the GL extension string to see if it is supported. */
833 if (has_extension(gl_ext_string, extension, len))
834 return TRUE;
836 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
837 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
838 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
839 * Check if we are searching for a core GL function */
840 if(strncmp(extension, "GL_VERSION_", 11) == 0)
842 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
843 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
845 if(!gl_version) {
846 ERR("No OpenGL version found!\n");
847 return FALSE;
850 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
851 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
852 if( (gl_version[0] > version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
853 return TRUE;
855 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]);
858 if (extension[len] == ' ') len++;
859 extension += len;
862 return FALSE;
865 /***********************************************************************
866 * wglGetProcAddress (OPENGL32.@)
868 PROC WINAPI wglGetProcAddress( LPCSTR name )
870 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
871 void **func_ptr;
872 OpenGL_extension ext;
873 const OpenGL_extension *ext_ret;
875 if (!name) return NULL;
877 /* Without an active context opengl32 doesn't know to what
878 * driver it has to dispatch wglGetProcAddress.
880 if (!get_current_context_ptr())
882 WARN("No active WGL context found\n");
883 return NULL;
886 ext.name = name;
887 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
888 if (!ext_ret)
890 WARN("Function %s unknown\n", name);
891 return NULL;
894 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
895 if (!*func_ptr)
897 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
899 if (!is_extension_supported(ext_ret->extension))
901 unsigned int i;
902 static const struct { const char *name, *alt; } alternatives[] =
904 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
905 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
908 for (i = 0; i < ARRAY_SIZE(alternatives); i++)
910 if (strcmp( name, alternatives[i].name )) continue;
911 WARN("Extension %s required for %s not supported, trying %s\n",
912 ext_ret->extension, name, alternatives[i].alt );
913 return wglGetProcAddress( alternatives[i].alt );
915 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
916 return NULL;
919 if (driver_func == NULL)
921 WARN("Function %s not supported by driver\n", name);
922 return NULL;
924 *func_ptr = driver_func;
927 TRACE("returning %s -> %p\n", name, ext_ret->func);
928 return ext_ret->func;
931 /***********************************************************************
932 * wglRealizeLayerPalette (OPENGL32.@)
934 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
935 int iLayerPlane,
936 BOOL bRealize) {
937 FIXME("()\n");
939 return FALSE;
942 /***********************************************************************
943 * wglSetLayerPaletteEntries (OPENGL32.@)
945 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
946 int iLayerPlane,
947 int iStart,
948 int cEntries,
949 const COLORREF *pcr) {
950 FIXME("(): stub!\n");
952 return 0;
955 /***********************************************************************
956 * wglGetDefaultProcAddress (OPENGL32.@)
958 PROC WINAPI wglGetDefaultProcAddress( LPCSTR name )
960 FIXME( "%s: stub\n", debugstr_a(name));
961 return NULL;
964 /***********************************************************************
965 * wglSwapLayerBuffers (OPENGL32.@)
967 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
968 UINT fuPlanes) {
969 TRACE("(%p, %08x)\n", hdc, fuPlanes);
971 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
972 if (!wglSwapBuffers( hdc )) return FALSE;
973 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
976 if (fuPlanes) {
977 WARN("Following layers unhandled: %08x\n", fuPlanes);
980 return TRUE;
983 /***********************************************************************
984 * wglBindTexImageARB
986 * Provided by the WGL_ARB_render_texture extension.
988 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
990 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
991 BOOL ret;
993 if (!ptr) return FALSE;
994 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
995 release_handle_ptr( ptr );
996 return ret;
999 /***********************************************************************
1000 * wglReleaseTexImageARB
1002 * Provided by the WGL_ARB_render_texture extension.
1004 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
1006 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1007 BOOL ret;
1009 if (!ptr) return FALSE;
1010 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
1011 release_handle_ptr( ptr );
1012 return ret;
1015 /***********************************************************************
1016 * wglSetPbufferAttribARB
1018 * Provided by the WGL_ARB_render_texture extension.
1020 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
1022 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1023 BOOL ret;
1025 if (!ptr) return FALSE;
1026 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
1027 release_handle_ptr( ptr );
1028 return ret;
1031 /***********************************************************************
1032 * wglCreatePbufferARB
1034 * Provided by the WGL_ARB_pbuffer extension.
1036 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1038 HPBUFFERARB ret;
1039 struct wgl_pbuffer *pbuffer;
1040 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1042 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1043 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1044 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1045 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1046 return ret;
1049 /***********************************************************************
1050 * wglGetPbufferDCARB
1052 * Provided by the WGL_ARB_pbuffer extension.
1054 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1056 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1057 HDC ret;
1059 if (!ptr) return 0;
1060 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1061 release_handle_ptr( ptr );
1062 return ret;
1065 /***********************************************************************
1066 * wglReleasePbufferDCARB
1068 * Provided by the WGL_ARB_pbuffer extension.
1070 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1072 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1073 BOOL ret;
1075 if (!ptr) return FALSE;
1076 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1077 release_handle_ptr( ptr );
1078 return ret;
1081 /***********************************************************************
1082 * wglDestroyPbufferARB
1084 * Provided by the WGL_ARB_pbuffer extension.
1086 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1088 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1090 if (!ptr) return FALSE;
1091 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1092 free_handle_ptr( ptr );
1093 return TRUE;
1096 /***********************************************************************
1097 * wglQueryPbufferARB
1099 * Provided by the WGL_ARB_pbuffer extension.
1101 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1103 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1104 BOOL ret;
1106 if (!ptr) return FALSE;
1107 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1108 release_handle_ptr( ptr );
1109 return ret;
1112 /***********************************************************************
1113 * wglUseFontBitmaps_common
1115 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1117 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1118 GLYPHMETRICS gm;
1119 unsigned int glyph, size = 0;
1120 void *bitmap = NULL, *gl_bitmap = NULL;
1121 int org_alignment;
1122 BOOL ret = TRUE;
1124 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1125 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1127 for (glyph = first; glyph < first + count; glyph++) {
1128 unsigned int needed_size, height, width, width_int;
1130 if (unicode)
1131 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1132 else
1133 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1135 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1136 if (needed_size == GDI_ERROR) {
1137 ret = FALSE;
1138 break;
1141 if (needed_size > size) {
1142 size = needed_size;
1143 HeapFree(GetProcessHeap(), 0, bitmap);
1144 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1145 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1146 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1148 if (needed_size != 0) {
1149 if (unicode)
1150 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1151 size, bitmap, &identity) != GDI_ERROR);
1152 else
1153 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1154 size, bitmap, &identity) != GDI_ERROR);
1155 if (!ret) break;
1158 if (TRACE_ON(wgl)) {
1159 unsigned int bitmask;
1160 unsigned char *bitmap_ = bitmap;
1162 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1163 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1164 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1165 if (needed_size != 0) {
1166 TRACE(" - bitmap:\n");
1167 for (height = 0; height < gm.gmBlackBoxY; height++) {
1168 TRACE(" ");
1169 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1170 if (bitmask == 0) {
1171 bitmap_ += 1;
1172 bitmask = 0x80;
1174 if (*bitmap_ & bitmask)
1175 TRACE("*");
1176 else
1177 TRACE(" ");
1179 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1180 TRACE("\n");
1185 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1186 * glyph for it to be drawn properly.
1188 if (needed_size != 0) {
1189 width_int = (gm.gmBlackBoxX + 31) / 32;
1190 for (height = 0; height < gm.gmBlackBoxY; height++) {
1191 for (width = 0; width < width_int; width++) {
1192 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1193 ((int *) bitmap)[height * width_int + width];
1198 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1199 if (needed_size != 0) {
1200 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1201 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1202 gm.gmCellIncX, gm.gmCellIncY,
1203 gl_bitmap);
1204 } else {
1205 /* This is the case of 'empty' glyphs like the space character */
1206 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1208 funcs->gl.p_glEndList();
1211 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1212 HeapFree(GetProcessHeap(), 0, bitmap);
1213 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1214 return ret;
1217 /***********************************************************************
1218 * wglUseFontBitmapsA (OPENGL32.@)
1220 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1222 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1225 /***********************************************************************
1226 * wglUseFontBitmapsW (OPENGL32.@)
1228 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1230 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1233 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1235 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1236 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1237 vertex[2] = 0.0;
1240 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1242 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1243 GLdouble *dbl = vertex;
1244 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1245 funcs->gl.p_glVertex3dv(vertex);
1248 static void WINAPI tess_callback_begin(GLenum which)
1250 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1251 TRACE("%d\n", which);
1252 funcs->gl.p_glBegin(which);
1255 static void WINAPI tess_callback_end(void)
1257 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1258 TRACE("\n");
1259 funcs->gl.p_glEnd();
1262 typedef struct _bezier_vector {
1263 GLdouble x;
1264 GLdouble y;
1265 } bezier_vector;
1267 static double bezier_deviation_squared(const bezier_vector *p)
1269 bezier_vector deviation;
1270 bezier_vector vertex;
1271 bezier_vector base;
1272 double base_length;
1273 double dot;
1275 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1276 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1278 base.x = p[2].x - p[0].x;
1279 base.y = p[2].y - p[0].y;
1281 base_length = sqrt(base.x*base.x + base.y*base.y);
1282 base.x /= base_length;
1283 base.y /= base_length;
1285 dot = base.x*vertex.x + base.y*vertex.y;
1286 dot = min(max(dot, 0.0), base_length);
1287 base.x *= dot;
1288 base.y *= dot;
1290 deviation.x = vertex.x-base.x;
1291 deviation.y = vertex.y-base.y;
1293 return deviation.x*deviation.x + deviation.y*deviation.y;
1296 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1298 bezier_vector first_curve[3];
1299 bezier_vector second_curve[3];
1300 bezier_vector vertex;
1301 int total_vertices;
1303 if(bezier_deviation_squared(p) <= deviation*deviation)
1305 if(points)
1306 *points = p[2];
1307 return 1;
1310 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1311 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1313 first_curve[0] = p[0];
1314 first_curve[1].x = (p[0].x + p[1].x)/2;
1315 first_curve[1].y = (p[0].y + p[1].y)/2;
1316 first_curve[2] = vertex;
1318 second_curve[0] = vertex;
1319 second_curve[1].x = (p[2].x + p[1].x)/2;
1320 second_curve[1].y = (p[2].y + p[1].y)/2;
1321 second_curve[2] = p[2];
1323 total_vertices = bezier_approximate(first_curve, points, deviation);
1324 if(points)
1325 points += total_vertices;
1326 total_vertices += bezier_approximate(second_curve, points, deviation);
1327 return total_vertices;
1330 /***********************************************************************
1331 * wglUseFontOutlines_common
1333 static BOOL wglUseFontOutlines_common(HDC hdc,
1334 DWORD first,
1335 DWORD count,
1336 DWORD listBase,
1337 FLOAT deviation,
1338 FLOAT extrusion,
1339 int format,
1340 LPGLYPHMETRICSFLOAT lpgmf,
1341 BOOL unicode)
1343 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1344 UINT glyph;
1345 GLUtesselator *tess = NULL;
1346 LOGFONTW lf;
1347 HFONT old_font, unscaled_font;
1348 UINT em_size = 1024;
1349 RECT rc;
1351 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1352 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1354 if(deviation <= 0.0)
1355 deviation = 1.0/em_size;
1357 if(format == WGL_FONT_POLYGONS)
1359 tess = gluNewTess();
1360 if(!tess)
1362 ERR("glu32 is required for this function but isn't available\n");
1363 return FALSE;
1365 gluTessCallback(tess, GLU_TESS_VERTEX, (void *)tess_callback_vertex);
1366 gluTessCallback(tess, GLU_TESS_BEGIN, (void *)tess_callback_begin);
1367 gluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1370 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1371 rc.left = rc.right = rc.bottom = 0;
1372 rc.top = em_size;
1373 DPtoLP(hdc, (POINT*)&rc, 2);
1374 lf.lfHeight = -abs(rc.top - rc.bottom);
1375 lf.lfOrientation = lf.lfEscapement = 0;
1376 unscaled_font = CreateFontIndirectW(&lf);
1377 old_font = SelectObject(hdc, unscaled_font);
1379 for (glyph = first; glyph < first + count; glyph++)
1381 DWORD needed;
1382 GLYPHMETRICS gm;
1383 BYTE *buf;
1384 TTPOLYGONHEADER *pph;
1385 TTPOLYCURVE *ppc;
1386 GLdouble *vertices = NULL;
1387 int vertex_total = -1;
1389 if(unicode)
1390 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1391 else
1392 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1394 if(needed == GDI_ERROR)
1395 goto error;
1397 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1399 if(unicode)
1400 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1401 else
1402 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1404 TRACE("glyph %d\n", glyph);
1406 if(lpgmf)
1408 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1409 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1410 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1411 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1412 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1413 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1415 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1416 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1417 lpgmf++;
1420 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1421 funcs->gl.p_glFrontFace(GL_CCW);
1422 if(format == WGL_FONT_POLYGONS)
1424 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1425 gluTessNormal(tess, 0, 0, 1);
1426 gluTessBeginPolygon(tess, NULL);
1429 while(!vertices)
1431 if(vertex_total != -1)
1432 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1433 vertex_total = 0;
1435 pph = (TTPOLYGONHEADER*)buf;
1436 while((BYTE*)pph < buf + needed)
1438 GLdouble previous[3];
1439 fixed_to_double(pph->pfxStart, em_size, previous);
1441 if(vertices)
1442 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1444 if(format == WGL_FONT_POLYGONS)
1445 gluTessBeginContour(tess);
1446 else
1447 funcs->gl.p_glBegin(GL_LINE_LOOP);
1449 if(vertices)
1451 fixed_to_double(pph->pfxStart, em_size, vertices);
1452 if(format == WGL_FONT_POLYGONS)
1453 gluTessVertex(tess, vertices, vertices);
1454 else
1455 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1456 vertices += 3;
1458 vertex_total++;
1460 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1461 while((char*)ppc < (char*)pph + pph->cb)
1463 int i, j;
1464 int num;
1466 switch(ppc->wType) {
1467 case TT_PRIM_LINE:
1468 for(i = 0; i < ppc->cpfx; i++)
1470 if(vertices)
1472 TRACE("\t\tline to %d, %d\n",
1473 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1474 fixed_to_double(ppc->apfx[i], em_size, vertices);
1475 if(format == WGL_FONT_POLYGONS)
1476 gluTessVertex(tess, vertices, vertices);
1477 else
1478 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1479 vertices += 3;
1481 fixed_to_double(ppc->apfx[i], em_size, previous);
1482 vertex_total++;
1484 break;
1486 case TT_PRIM_QSPLINE:
1487 for(i = 0; i < ppc->cpfx-1; i++)
1489 bezier_vector curve[3];
1490 bezier_vector *points;
1491 GLdouble curve_vertex[3];
1493 if(vertices)
1494 TRACE("\t\tcurve %d,%d %d,%d\n",
1495 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1496 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1498 curve[0].x = previous[0];
1499 curve[0].y = previous[1];
1500 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1501 curve[1].x = curve_vertex[0];
1502 curve[1].y = curve_vertex[1];
1503 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1504 curve[2].x = curve_vertex[0];
1505 curve[2].y = curve_vertex[1];
1506 if(i < ppc->cpfx-2)
1508 curve[2].x = (curve[1].x + curve[2].x)/2;
1509 curve[2].y = (curve[1].y + curve[2].y)/2;
1511 num = bezier_approximate(curve, NULL, deviation);
1512 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1513 num = bezier_approximate(curve, points, deviation);
1514 vertex_total += num;
1515 if(vertices)
1517 for(j=0; j<num; j++)
1519 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1520 vertices[0] = points[j].x;
1521 vertices[1] = points[j].y;
1522 vertices[2] = 0.0;
1523 if(format == WGL_FONT_POLYGONS)
1524 gluTessVertex(tess, vertices, vertices);
1525 else
1526 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1527 vertices += 3;
1530 HeapFree(GetProcessHeap(), 0, points);
1531 previous[0] = curve[2].x;
1532 previous[1] = curve[2].y;
1534 break;
1535 default:
1536 ERR("\t\tcurve type = %d\n", ppc->wType);
1537 if(format == WGL_FONT_POLYGONS)
1538 gluTessEndContour(tess);
1539 else
1540 funcs->gl.p_glEnd();
1541 goto error_in_list;
1544 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1545 (ppc->cpfx - 1) * sizeof(POINTFX));
1547 if(format == WGL_FONT_POLYGONS)
1548 gluTessEndContour(tess);
1549 else
1550 funcs->gl.p_glEnd();
1551 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1555 error_in_list:
1556 if(format == WGL_FONT_POLYGONS)
1557 gluTessEndPolygon(tess);
1558 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1559 funcs->gl.p_glEndList();
1560 HeapFree(GetProcessHeap(), 0, buf);
1561 HeapFree(GetProcessHeap(), 0, vertices);
1564 error:
1565 DeleteObject(SelectObject(hdc, old_font));
1566 if(format == WGL_FONT_POLYGONS)
1567 gluDeleteTess(tess);
1568 return TRUE;
1572 /***********************************************************************
1573 * wglUseFontOutlinesA (OPENGL32.@)
1575 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1576 DWORD first,
1577 DWORD count,
1578 DWORD listBase,
1579 FLOAT deviation,
1580 FLOAT extrusion,
1581 int format,
1582 LPGLYPHMETRICSFLOAT lpgmf)
1584 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1587 /***********************************************************************
1588 * wglUseFontOutlinesW (OPENGL32.@)
1590 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1591 DWORD first,
1592 DWORD count,
1593 DWORD listBase,
1594 FLOAT deviation,
1595 FLOAT extrusion,
1596 int format,
1597 LPGLYPHMETRICSFLOAT lpgmf)
1599 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1602 /***********************************************************************
1603 * glDebugEntry (OPENGL32.@)
1605 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1607 return 0;
1610 static GLubyte *filter_extensions_list(const char *extensions, const char *disabled)
1612 char *p, *str;
1613 const char *end;
1615 p = str = HeapAlloc(GetProcessHeap(), 0, strlen(extensions) + 2);
1616 if (!str)
1617 return NULL;
1619 TRACE( "GL_EXTENSIONS:\n" );
1621 for (;;)
1623 while (*extensions == ' ')
1624 extensions++;
1625 if (!*extensions)
1626 break;
1627 if (!(end = strchr(extensions, ' ')))
1628 end = extensions + strlen(extensions);
1629 memcpy(p, extensions, end - extensions);
1630 p[end - extensions] = 0;
1631 if (!has_extension(disabled, p, strlen(p)))
1633 TRACE("++ %s\n", p);
1634 p += end - extensions;
1635 *p++ = ' ';
1637 else
1639 TRACE("-- %s (disabled by config)\n", p);
1641 extensions = end;
1643 *p = 0;
1644 return (GLubyte *)str;
1647 static GLuint *filter_extensions_index(const char *disabled)
1649 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1650 const char *ext, *end, *gl_ext;
1651 GLuint *disabled_exts, *new_disabled_exts;
1652 unsigned int i = 0, j, disabled_size;
1653 GLint extensions_count;
1655 if (!funcs->ext.p_glGetStringi)
1657 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
1659 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
1660 if (!funcs->ext.p_glGetStringi)
1661 return NULL;
1664 funcs->gl.p_glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
1665 disabled_size = 2;
1666 disabled_exts = HeapAlloc(GetProcessHeap(), 0, disabled_size * sizeof(*disabled_exts));
1667 if (!disabled_exts)
1668 return NULL;
1670 TRACE( "GL_EXTENSIONS:\n" );
1672 for (j = 0; j < extensions_count; ++j)
1674 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, j);
1675 ext = disabled;
1676 for (;;)
1678 while (*ext == ' ')
1679 ext++;
1680 if (!*ext)
1682 TRACE("++ %s\n", gl_ext);
1683 break;
1685 if (!(end = strchr(ext, ' ')))
1686 end = ext + strlen(ext);
1688 if (!strncmp(gl_ext, ext, end - ext) && !gl_ext[end - ext])
1690 if (i + 1 == disabled_size)
1692 disabled_size *= 2;
1693 new_disabled_exts = HeapReAlloc(GetProcessHeap(), 0, disabled_exts,
1694 disabled_size * sizeof(*disabled_exts));
1695 if (!new_disabled_exts)
1697 disabled_exts[i] = ~0u;
1698 return disabled_exts;
1700 disabled_exts = new_disabled_exts;
1702 TRACE("-- %s (disabled by config)\n", gl_ext);
1703 disabled_exts[i++] = j;
1704 break;
1706 ext = end;
1709 disabled_exts[i] = ~0u;
1710 return disabled_exts;
1713 /* build the extension string by filtering out the disabled extensions */
1714 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts)
1716 static const char *disabled;
1718 if (!disabled)
1720 HKEY hkey;
1721 DWORD size;
1722 char *str = NULL;
1724 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1725 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1727 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1729 str = HeapAlloc( GetProcessHeap(), 0, size );
1730 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1732 RegCloseKey( hkey );
1734 if (str)
1736 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1737 HeapFree( GetProcessHeap(), 0, str );
1739 else disabled = "";
1742 if (!disabled[0])
1743 return FALSE;
1745 if (extensions && !*exts_list)
1746 *exts_list = filter_extensions_list(extensions, disabled);
1748 if (!*disabled_exts)
1749 *disabled_exts = filter_extensions_index(disabled);
1751 return (exts_list && *exts_list) || *disabled_exts;
1754 /***********************************************************************
1755 * glGetString (OPENGL32.@)
1757 const GLubyte * WINAPI glGetString( GLenum name )
1759 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1760 const GLubyte *ret = funcs->gl.p_glGetString( name );
1762 if (name == GL_EXTENSIONS && ret)
1764 struct wgl_handle *ptr = get_current_context_ptr();
1765 if (ptr->u.context->extensions ||
1766 filter_extensions((const char *)ret, &ptr->u.context->extensions, &ptr->u.context->disabled_exts))
1767 ret = ptr->u.context->extensions;
1769 return ret;
1772 /* wrapper for glDebugMessageCallback* functions */
1773 static void gl_debug_message_callback( GLenum source, GLenum type, GLuint id, GLenum severity,
1774 GLsizei length, const GLchar *message,const void *userParam )
1776 struct wgl_handle *ptr = (struct wgl_handle *)userParam;
1777 if (!ptr->u.context->debug_callback) return;
1778 ptr->u.context->debug_callback( source, type, id, severity, length, message, ptr->u.context->debug_user );
1781 /***********************************************************************
1782 * glDebugMessageCallback
1784 void WINAPI glDebugMessageCallback( GLDEBUGPROC callback, const void *userParam )
1786 struct wgl_handle *ptr = get_current_context_ptr();
1787 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1789 TRACE( "(%p, %p)\n", callback, userParam );
1791 ptr->u.context->debug_callback = callback;
1792 ptr->u.context->debug_user = userParam;
1793 funcs->ext.p_glDebugMessageCallback( gl_debug_message_callback, ptr );
1796 /***********************************************************************
1797 * glDebugMessageCallbackAMD
1799 void WINAPI glDebugMessageCallbackAMD( GLDEBUGPROCAMD callback, void *userParam )
1801 struct wgl_handle *ptr = get_current_context_ptr();
1802 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1804 TRACE( "(%p, %p)\n", callback, userParam );
1806 ptr->u.context->debug_callback = callback;
1807 ptr->u.context->debug_user = userParam;
1808 funcs->ext.p_glDebugMessageCallbackAMD( gl_debug_message_callback, ptr );
1811 /***********************************************************************
1812 * glDebugMessageCallbackARB
1814 void WINAPI glDebugMessageCallbackARB( GLDEBUGPROCARB callback, const void *userParam )
1816 struct wgl_handle *ptr = get_current_context_ptr();
1817 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1819 TRACE( "(%p, %p)\n", callback, userParam );
1821 ptr->u.context->debug_callback = callback;
1822 ptr->u.context->debug_user = userParam;
1823 funcs->ext.p_glDebugMessageCallbackARB( gl_debug_message_callback, ptr );
1826 /***********************************************************************
1827 * OpenGL initialisation routine
1829 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1831 switch(reason)
1833 case DLL_PROCESS_ATTACH:
1834 NtCurrentTeb()->glTable = &null_opengl_funcs;
1835 break;
1836 case DLL_THREAD_ATTACH:
1837 NtCurrentTeb()->glTable = &null_opengl_funcs;
1838 break;
1840 return TRUE;