tests: Make sure only one thread prints traces at a time.
[wine.git] / dlls / opengl32 / wgl.c
blob49f1e4700b4b363bfe4f27e8d53682c1a9f434fe
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"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <math.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 #include "opengl_ext.h"
38 #include "wine/gdi_driver.h"
39 #include "wine/glu.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
43 WINE_DECLARE_DEBUG_CHANNEL(fps);
45 /* handle management */
47 #define MAX_WGL_HANDLES 1024
49 enum wgl_handle_type
51 HANDLE_PBUFFER = 0 << 12,
52 HANDLE_CONTEXT = 1 << 12,
53 HANDLE_CONTEXT_V3 = 3 << 12,
54 HANDLE_TYPE_MASK = 15 << 12
57 struct opengl_context
59 DWORD tid; /* thread that the context is current in */
60 HDC draw_dc; /* current drawing DC */
61 HDC read_dc; /* current reading DC */
62 void (CALLBACK *debug_callback)(GLenum, GLenum, GLuint, GLenum,
63 GLsizei, const GLchar *, const void *); /* debug callback */
64 const void *debug_user; /* debug user parameter */
65 GLubyte *extensions; /* extension string */
66 GLuint *disabled_exts; /* indices of disabled extensions */
67 struct wgl_context *drv_ctx; /* driver context */
70 struct wgl_handle
72 UINT handle;
73 struct opengl_funcs *funcs;
74 union
76 struct opengl_context *context; /* for HANDLE_CONTEXT */
77 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
78 struct wgl_handle *next; /* for free handles */
79 } u;
82 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
83 static struct wgl_handle *next_free;
84 static unsigned int handle_count;
86 static CRITICAL_SECTION wgl_section;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
89 0, 0, &wgl_section,
90 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
93 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
95 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
97 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
99 WORD generation = HIWORD( ptr->handle ) + 1;
100 if (!generation) generation++;
101 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
102 return ULongToHandle( ptr->handle );
105 /* the current context is assumed valid and doesn't need locking */
106 static inline struct wgl_handle *get_current_context_ptr(void)
108 if (!NtCurrentTeb()->glCurrentRC) return NULL;
109 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
112 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
114 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
116 EnterCriticalSection( &wgl_section );
117 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
118 return &wgl_handles[index];
120 LeaveCriticalSection( &wgl_section );
121 SetLastError( ERROR_INVALID_HANDLE );
122 return NULL;
125 static void release_handle_ptr( struct wgl_handle *ptr )
127 if (ptr) LeaveCriticalSection( &wgl_section );
130 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
132 HANDLE handle = 0;
133 struct wgl_handle *ptr = NULL;
135 EnterCriticalSection( &wgl_section );
136 if ((ptr = next_free))
137 next_free = next_free->u.next;
138 else if (handle_count < MAX_WGL_HANDLES)
139 ptr = &wgl_handles[handle_count++];
141 if (ptr)
143 ptr->funcs = funcs;
144 ptr->u.context = user_ptr;
145 handle = next_handle( ptr, type );
147 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
148 LeaveCriticalSection( &wgl_section );
149 return handle;
152 static void free_handle_ptr( struct wgl_handle *ptr )
154 ptr->handle |= 0xffff;
155 ptr->u.next = next_free;
156 ptr->funcs = NULL;
157 next_free = ptr;
158 LeaveCriticalSection( &wgl_section );
161 static inline enum wgl_handle_type get_current_context_type(void)
163 if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
164 return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
167 /***********************************************************************
168 * wglCopyContext (OPENGL32.@)
170 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
172 struct wgl_handle *src, *dst;
173 BOOL ret = FALSE;
175 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
176 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
178 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
179 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
180 dst->u.context->drv_ctx, mask );
182 release_handle_ptr( dst );
183 release_handle_ptr( src );
184 return ret;
187 /***********************************************************************
188 * wglDeleteContext (OPENGL32.@)
190 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
192 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
194 if (!ptr) return FALSE;
196 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
198 SetLastError( ERROR_BUSY );
199 release_handle_ptr( ptr );
200 return FALSE;
202 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
203 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
204 HeapFree( GetProcessHeap(), 0, ptr->u.context->disabled_exts );
205 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
206 HeapFree( GetProcessHeap(), 0, ptr->u.context );
207 free_handle_ptr( ptr );
208 return TRUE;
211 /***********************************************************************
212 * wglMakeCurrent (OPENGL32.@)
214 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
216 BOOL ret = TRUE;
217 struct wgl_handle *ptr, *prev = get_current_context_ptr();
219 if (hglrc)
221 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
222 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
224 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
225 if (ret)
227 if (prev) prev->u.context->tid = 0;
228 ptr->u.context->tid = GetCurrentThreadId();
229 ptr->u.context->draw_dc = hdc;
230 ptr->u.context->read_dc = hdc;
231 NtCurrentTeb()->glCurrentRC = hglrc;
232 NtCurrentTeb()->glTable = ptr->funcs;
235 else
237 SetLastError( ERROR_BUSY );
238 ret = FALSE;
240 release_handle_ptr( ptr );
242 else if (prev)
244 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
245 prev->u.context->tid = 0;
246 NtCurrentTeb()->glCurrentRC = 0;
247 NtCurrentTeb()->glTable = &null_opengl_funcs;
249 else if (!hdc)
251 SetLastError( ERROR_INVALID_HANDLE );
252 ret = FALSE;
254 return ret;
257 /***********************************************************************
258 * wglCreateContextAttribsARB
260 * Provided by the WGL_ARB_create_context extension.
262 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
264 HGLRC ret = 0;
265 struct wgl_context *drv_ctx;
266 struct wgl_handle *share_ptr = NULL;
267 struct opengl_context *context;
268 struct opengl_funcs *funcs = get_dc_funcs( hdc );
270 if (!funcs)
272 SetLastError( ERROR_DC_NOT_FOUND );
273 return 0;
275 if (!funcs->ext.p_wglCreateContextAttribsARB) return 0;
276 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT )))
278 SetLastError( ERROR_INVALID_OPERATION );
279 return 0;
281 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
282 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
284 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
286 enum wgl_handle_type type = HANDLE_CONTEXT;
288 if (attribs)
290 while (*attribs)
292 if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
294 if (attribs[1] >= 3)
295 type = HANDLE_CONTEXT_V3;
296 break;
298 attribs += 2;
302 context->drv_ctx = drv_ctx;
303 if (!(ret = alloc_handle( type, funcs, context )))
304 HeapFree( GetProcessHeap(), 0, context );
306 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
308 release_handle_ptr( share_ptr );
309 return ret;
313 /***********************************************************************
314 * wglMakeContextCurrentARB
316 * Provided by the WGL_ARB_make_current_read extension.
318 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
320 BOOL ret = TRUE;
321 struct wgl_handle *ptr, *prev = get_current_context_ptr();
323 if (hglrc)
325 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
326 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
328 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
329 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
330 ptr->u.context->drv_ctx ));
331 if (ret)
333 if (prev) prev->u.context->tid = 0;
334 ptr->u.context->tid = GetCurrentThreadId();
335 ptr->u.context->draw_dc = draw_hdc;
336 ptr->u.context->read_dc = read_hdc;
337 NtCurrentTeb()->glCurrentRC = hglrc;
338 NtCurrentTeb()->glTable = ptr->funcs;
341 else
343 SetLastError( ERROR_BUSY );
344 ret = FALSE;
346 release_handle_ptr( ptr );
348 else if (prev)
350 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
351 prev->u.context->tid = 0;
352 NtCurrentTeb()->glCurrentRC = 0;
353 NtCurrentTeb()->glTable = &null_opengl_funcs;
355 return ret;
358 /***********************************************************************
359 * wglGetCurrentReadDCARB
361 * Provided by the WGL_ARB_make_current_read extension.
363 HDC WINAPI wglGetCurrentReadDCARB(void)
365 struct wgl_handle *ptr = get_current_context_ptr();
367 if (!ptr) return 0;
368 return ptr->u.context->read_dc;
371 /***********************************************************************
372 * wglShareLists (OPENGL32.@)
374 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
376 BOOL ret = FALSE;
377 struct wgl_handle *src, *dst;
379 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
380 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
382 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
383 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
385 release_handle_ptr( dst );
386 release_handle_ptr( src );
387 return ret;
390 /***********************************************************************
391 * wglGetCurrentDC (OPENGL32.@)
393 HDC WINAPI wglGetCurrentDC(void)
395 struct wgl_handle *ptr = get_current_context_ptr();
397 if (!ptr) return 0;
398 return ptr->u.context->draw_dc;
401 /***********************************************************************
402 * wgl_create_context wrapper for hooking
404 static HGLRC wgl_create_context(HDC hdc)
406 HGLRC ret = 0;
407 struct wgl_context *drv_ctx;
408 struct opengl_context *context;
409 struct opengl_funcs *funcs = get_dc_funcs( hdc );
411 if (!funcs) return 0;
412 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
413 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
415 context->drv_ctx = drv_ctx;
416 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
417 HeapFree( GetProcessHeap(), 0, context );
419 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
420 return ret;
423 /***********************************************************************
424 * wglCreateContext (OPENGL32.@)
426 HGLRC WINAPI wglCreateContext(HDC hdc)
428 return wgl_create_context(hdc);
431 /***********************************************************************
432 * wglGetCurrentContext (OPENGL32.@)
434 HGLRC WINAPI wglGetCurrentContext(void)
436 return NtCurrentTeb()->glCurrentRC;
439 /***********************************************************************
440 * wglDescribePixelFormat (OPENGL32.@)
442 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
444 struct opengl_funcs *funcs = get_dc_funcs( hdc );
445 if (!funcs) return 0;
446 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
449 /***********************************************************************
450 * wglChoosePixelFormat (OPENGL32.@)
452 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
454 PIXELFORMATDESCRIPTOR format, best;
455 int i, count, best_format;
456 int bestDBuffer = -1, bestStereo = -1;
458 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
459 "accum %u depth %u stencil %u aux %u\n",
460 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
461 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
462 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
464 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
465 if (!count) return 0;
467 best_format = 0;
468 best.dwFlags = 0;
469 best.cAlphaBits = -1;
470 best.cColorBits = -1;
471 best.cDepthBits = -1;
472 best.cStencilBits = -1;
473 best.cAuxBuffers = -1;
475 for (i = 1; i <= count; i++)
477 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
479 if ((ppfd->iPixelType == PFD_TYPE_COLORINDEX) != (format.iPixelType == PFD_TYPE_COLORINDEX))
481 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
482 continue;
485 /* only use bitmap capable for formats for bitmap rendering */
486 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
488 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
489 continue;
492 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
493 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
494 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
495 * formats without the given flag set.
496 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
497 * has indicated that a format without stereo is returned when stereo is unavailable.
498 * So in case PFD_STEREO is set, formats that support it should have priority above formats
499 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
501 * To summarize the following is most likely the correct behavior:
502 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
503 * stereo set -> prefer stereo formats, but also accept non-stereo formats
504 * stereo don't care -> it doesn't matter whether we get stereo or not
506 * In Wine we will treat non-stereo the same way as don't care because it makes
507 * format selection even more complicated and second drivers with Stereo advertise
508 * each format twice anyway.
511 /* Doublebuffer, see the comments above */
512 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
514 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
515 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
516 goto found;
518 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
520 else if (!best_format)
521 goto found;
523 /* Stereo, see the comments above. */
524 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
526 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
527 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
528 goto found;
530 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
532 else if (!best_format)
533 goto found;
535 /* Below we will do a number of checks to select the 'best' pixelformat.
536 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
537 * The code works by trying to match the most important options as close as possible.
538 * When a reasonable format is found, we will try to match more options.
539 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
540 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
541 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
543 if (ppfd->cColorBits)
545 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
546 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
547 goto found;
549 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
551 TRACE( "color mismatch for iPixelFormat=%d\n", i );
552 continue;
555 if (ppfd->cAlphaBits)
557 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
558 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
559 goto found;
561 if (best.cAlphaBits != format.cAlphaBits)
563 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
564 continue;
567 if (ppfd->cStencilBits)
569 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
570 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
571 goto found;
573 if (best.cStencilBits != format.cStencilBits)
575 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
576 continue;
579 if (ppfd->cDepthBits && !(ppfd->dwFlags & PFD_DEPTH_DONTCARE))
581 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
582 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
583 goto found;
585 if (best.cDepthBits != format.cDepthBits)
587 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
588 continue;
591 if (ppfd->cAuxBuffers)
593 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
594 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
595 goto found;
597 if (best.cAuxBuffers != format.cAuxBuffers)
599 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
600 continue;
603 if (ppfd->dwFlags & PFD_DEPTH_DONTCARE && format.cDepthBits < best.cDepthBits)
604 goto found;
606 continue;
608 found:
609 best_format = i;
610 best = format;
611 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
612 bestStereo = format.dwFlags & PFD_STEREO;
615 TRACE( "returning %u\n", best_format );
616 return best_format;
619 /***********************************************************************
620 * wglGetPixelFormat (OPENGL32.@)
622 INT WINAPI wglGetPixelFormat(HDC hdc)
624 struct opengl_funcs *funcs = get_dc_funcs( hdc );
625 if (!funcs)
627 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
628 return 0;
630 return funcs->wgl.p_wglGetPixelFormat( hdc );
633 /***********************************************************************
634 * wglSetPixelFormat(OPENGL32.@)
636 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
638 struct opengl_funcs *funcs = get_dc_funcs( hdc );
639 if (!funcs) return FALSE;
640 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
643 /***********************************************************************
644 * wglSwapBuffers (OPENGL32.@)
646 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
648 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
650 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
651 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
653 if (TRACE_ON(fps))
655 static long prev_time, start_time;
656 static unsigned long frames, frames_total;
658 DWORD time = GetTickCount();
659 frames++;
660 frames_total++;
661 /* every 1.5 seconds */
662 if (time - prev_time > 1500)
664 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
665 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
666 prev_time = time;
667 frames = 0;
668 if (start_time == 0) start_time = time;
671 return TRUE;
674 /***********************************************************************
675 * wglCreateLayerContext (OPENGL32.@)
677 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
678 int iLayerPlane) {
679 TRACE("(%p,%d)\n", hdc, iLayerPlane);
681 if (iLayerPlane == 0) {
682 return wgl_create_context(hdc);
684 FIXME("no handler for layer %d\n", iLayerPlane);
686 return NULL;
689 /***********************************************************************
690 * wglDescribeLayerPlane (OPENGL32.@)
692 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
693 int iPixelFormat,
694 int iLayerPlane,
695 UINT nBytes,
696 LPLAYERPLANEDESCRIPTOR plpd) {
697 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
699 return FALSE;
702 /***********************************************************************
703 * wglGetLayerPaletteEntries (OPENGL32.@)
705 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
706 int iLayerPlane,
707 int iStart,
708 int cEntries,
709 const COLORREF *pcr) {
710 FIXME("(): stub!\n");
712 return 0;
715 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
717 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
719 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
721 TRACE("(%d, %p)\n", pname, data);
722 if (pname == GL_NUM_EXTENSIONS)
724 struct wgl_handle *ptr = get_current_context_ptr();
726 if (ptr->u.context->disabled_exts ||
727 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
729 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
730 GLint count, disabled_count = 0;
732 funcs->gl.p_glGetIntegerv(pname, &count);
733 while (*disabled_exts++ != ~0u)
734 disabled_count++;
735 *data = count - disabled_count;
736 return;
739 funcs->gl.p_glGetIntegerv(pname, data);
742 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
744 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
746 TRACE("(%d, %d)\n", name, index);
747 if (!funcs->ext.p_glGetStringi)
749 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
751 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
754 if (name == GL_EXTENSIONS)
756 struct wgl_handle *ptr = get_current_context_ptr();
758 if (ptr->u.context->disabled_exts ||
759 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
761 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
762 unsigned int disabled_count = 0;
764 while (index + disabled_count >= *disabled_exts++)
765 disabled_count++;
766 return funcs->ext.p_glGetStringi(name, index + disabled_count);
769 return funcs->ext.p_glGetStringi(name, index);
772 /* check if the extension is present in the list */
773 static BOOL has_extension( const char *list, const char *ext, size_t len )
775 if (!list)
777 const char *gl_ext;
778 unsigned int i;
779 GLint extensions_count;
781 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
782 for (i = 0; i < extensions_count; ++i)
784 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
785 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
786 return TRUE;
788 return FALSE;
791 while (list)
793 while (*list == ' ') list++;
794 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
795 list = strchr( list, ' ' );
797 return FALSE;
800 static int compar(const void *elt_a, const void *elt_b) {
801 return strcmp(((const OpenGL_extension *) elt_a)->name,
802 ((const OpenGL_extension *) elt_b)->name);
805 /* Check if a GL extension is supported */
806 static BOOL is_extension_supported(const char* extension)
808 enum wgl_handle_type type = get_current_context_type();
809 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
810 const char *gl_ext_string = NULL;
811 size_t len;
813 TRACE("Checking for extension '%s'\n", extension);
815 if (type == HANDLE_CONTEXT)
817 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
818 if (!gl_ext_string)
820 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
821 return FALSE;
825 /* We use the GetProcAddress function from the display driver to retrieve function pointers
826 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
827 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
828 * require the function to return NULL when an extension isn't found. For this reason we check
829 * if the OpenGL extension required for the function we are looking up is supported. */
831 while ((len = strcspn(extension, " ")) != 0)
833 /* Check if the extension is part of the GL extension string to see if it is supported. */
834 if (has_extension(gl_ext_string, extension, len))
835 return TRUE;
837 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
838 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
839 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
840 * Check if we are searching for a core GL function */
841 if(strncmp(extension, "GL_VERSION_", 11) == 0)
843 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
844 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
846 if(!gl_version) {
847 ERR("No OpenGL version found!\n");
848 return FALSE;
851 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
852 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
853 if( (gl_version[0] > version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
854 return TRUE;
856 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]);
859 if (extension[len] == ' ') len++;
860 extension += len;
863 return FALSE;
866 /***********************************************************************
867 * wglGetProcAddress (OPENGL32.@)
869 PROC WINAPI wglGetProcAddress( LPCSTR name )
871 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
872 void **func_ptr;
873 OpenGL_extension ext;
874 const OpenGL_extension *ext_ret;
876 if (!name) return NULL;
878 /* Without an active context opengl32 doesn't know to what
879 * driver it has to dispatch wglGetProcAddress.
881 if (!get_current_context_ptr())
883 WARN("No active WGL context found\n");
884 return NULL;
887 ext.name = name;
888 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
889 if (!ext_ret)
891 WARN("Function %s unknown\n", name);
892 return NULL;
895 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
896 if (!*func_ptr)
898 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
900 if (!is_extension_supported(ext_ret->extension))
902 unsigned int i;
903 static const struct { const char *name, *alt; } alternatives[] =
905 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
906 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
909 for (i = 0; i < ARRAY_SIZE(alternatives); i++)
911 if (strcmp( name, alternatives[i].name )) continue;
912 WARN("Extension %s required for %s not supported, trying %s\n",
913 ext_ret->extension, name, alternatives[i].alt );
914 return wglGetProcAddress( alternatives[i].alt );
916 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
917 return NULL;
920 if (driver_func == NULL)
922 WARN("Function %s not supported by driver\n", name);
923 return NULL;
925 *func_ptr = driver_func;
928 TRACE("returning %s -> %p\n", name, ext_ret->func);
929 return ext_ret->func;
932 /***********************************************************************
933 * wglRealizeLayerPalette (OPENGL32.@)
935 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
936 int iLayerPlane,
937 BOOL bRealize) {
938 FIXME("()\n");
940 return FALSE;
943 /***********************************************************************
944 * wglSetLayerPaletteEntries (OPENGL32.@)
946 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
947 int iLayerPlane,
948 int iStart,
949 int cEntries,
950 const COLORREF *pcr) {
951 FIXME("(): stub!\n");
953 return 0;
956 /***********************************************************************
957 * wglGetDefaultProcAddress (OPENGL32.@)
959 PROC WINAPI wglGetDefaultProcAddress( LPCSTR name )
961 FIXME( "%s: stub\n", debugstr_a(name));
962 return NULL;
965 /***********************************************************************
966 * wglSwapLayerBuffers (OPENGL32.@)
968 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
969 UINT fuPlanes) {
970 TRACE("(%p, %08x)\n", hdc, fuPlanes);
972 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
973 if (!wglSwapBuffers( hdc )) return FALSE;
974 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
977 if (fuPlanes) {
978 WARN("Following layers unhandled: %08x\n", fuPlanes);
981 return TRUE;
984 /***********************************************************************
985 * wglBindTexImageARB
987 * Provided by the WGL_ARB_render_texture extension.
989 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
991 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
992 BOOL ret;
994 if (!ptr) return FALSE;
995 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
996 release_handle_ptr( ptr );
997 return ret;
1000 /***********************************************************************
1001 * wglReleaseTexImageARB
1003 * Provided by the WGL_ARB_render_texture extension.
1005 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
1007 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1008 BOOL ret;
1010 if (!ptr) return FALSE;
1011 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
1012 release_handle_ptr( ptr );
1013 return ret;
1016 /***********************************************************************
1017 * wglSetPbufferAttribARB
1019 * Provided by the WGL_ARB_render_texture extension.
1021 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
1023 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1024 BOOL ret;
1026 if (!ptr) return FALSE;
1027 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
1028 release_handle_ptr( ptr );
1029 return ret;
1032 /***********************************************************************
1033 * wglCreatePbufferARB
1035 * Provided by the WGL_ARB_pbuffer extension.
1037 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1039 HPBUFFERARB ret;
1040 struct wgl_pbuffer *pbuffer;
1041 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1043 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1044 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1045 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1046 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1047 return ret;
1050 /***********************************************************************
1051 * wglGetPbufferDCARB
1053 * Provided by the WGL_ARB_pbuffer extension.
1055 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1057 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1058 HDC ret;
1060 if (!ptr) return 0;
1061 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1062 release_handle_ptr( ptr );
1063 return ret;
1066 /***********************************************************************
1067 * wglReleasePbufferDCARB
1069 * Provided by the WGL_ARB_pbuffer extension.
1071 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1073 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1074 BOOL ret;
1076 if (!ptr) return FALSE;
1077 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1078 release_handle_ptr( ptr );
1079 return ret;
1082 /***********************************************************************
1083 * wglDestroyPbufferARB
1085 * Provided by the WGL_ARB_pbuffer extension.
1087 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1089 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1091 if (!ptr) return FALSE;
1092 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1093 free_handle_ptr( ptr );
1094 return TRUE;
1097 /***********************************************************************
1098 * wglQueryPbufferARB
1100 * Provided by the WGL_ARB_pbuffer extension.
1102 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1104 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1105 BOOL ret;
1107 if (!ptr) return FALSE;
1108 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1109 release_handle_ptr( ptr );
1110 return ret;
1113 /***********************************************************************
1114 * wglUseFontBitmaps_common
1116 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1118 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1119 GLYPHMETRICS gm;
1120 unsigned int glyph, size = 0;
1121 void *bitmap = NULL, *gl_bitmap = NULL;
1122 int org_alignment;
1123 BOOL ret = TRUE;
1125 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1126 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1128 for (glyph = first; glyph < first + count; glyph++) {
1129 unsigned int needed_size, height, width, width_int;
1131 if (unicode)
1132 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1133 else
1134 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1136 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1137 if (needed_size == GDI_ERROR) {
1138 ret = FALSE;
1139 break;
1142 if (needed_size > size) {
1143 size = needed_size;
1144 HeapFree(GetProcessHeap(), 0, bitmap);
1145 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1146 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1147 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1149 if (needed_size != 0) {
1150 if (unicode)
1151 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1152 size, bitmap, &identity) != GDI_ERROR);
1153 else
1154 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1155 size, bitmap, &identity) != GDI_ERROR);
1156 if (!ret) break;
1159 if (TRACE_ON(wgl)) {
1160 unsigned int bitmask;
1161 unsigned char *bitmap_ = bitmap;
1163 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1164 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1165 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1166 if (needed_size != 0) {
1167 TRACE(" - bitmap:\n");
1168 for (height = 0; height < gm.gmBlackBoxY; height++) {
1169 TRACE(" ");
1170 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1171 if (bitmask == 0) {
1172 bitmap_ += 1;
1173 bitmask = 0x80;
1175 if (*bitmap_ & bitmask)
1176 TRACE("*");
1177 else
1178 TRACE(" ");
1180 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1181 TRACE("\n");
1186 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1187 * glyph for it to be drawn properly.
1189 if (needed_size != 0) {
1190 width_int = (gm.gmBlackBoxX + 31) / 32;
1191 for (height = 0; height < gm.gmBlackBoxY; height++) {
1192 for (width = 0; width < width_int; width++) {
1193 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1194 ((int *) bitmap)[height * width_int + width];
1199 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1200 if (needed_size != 0) {
1201 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1202 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1203 gm.gmCellIncX, gm.gmCellIncY,
1204 gl_bitmap);
1205 } else {
1206 /* This is the case of 'empty' glyphs like the space character */
1207 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1209 funcs->gl.p_glEndList();
1212 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1213 HeapFree(GetProcessHeap(), 0, bitmap);
1214 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1215 return ret;
1218 /***********************************************************************
1219 * wglUseFontBitmapsA (OPENGL32.@)
1221 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1223 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1226 /***********************************************************************
1227 * wglUseFontBitmapsW (OPENGL32.@)
1229 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1231 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1234 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1236 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1237 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1238 vertex[2] = 0.0;
1241 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1243 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1244 GLdouble *dbl = vertex;
1245 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1246 funcs->gl.p_glVertex3dv(vertex);
1249 static void WINAPI tess_callback_begin(GLenum which)
1251 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1252 TRACE("%d\n", which);
1253 funcs->gl.p_glBegin(which);
1256 static void WINAPI tess_callback_end(void)
1258 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1259 TRACE("\n");
1260 funcs->gl.p_glEnd();
1263 typedef struct _bezier_vector {
1264 GLdouble x;
1265 GLdouble y;
1266 } bezier_vector;
1268 static double bezier_deviation_squared(const bezier_vector *p)
1270 bezier_vector deviation;
1271 bezier_vector vertex;
1272 bezier_vector base;
1273 double base_length;
1274 double dot;
1276 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1277 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1279 base.x = p[2].x - p[0].x;
1280 base.y = p[2].y - p[0].y;
1282 base_length = sqrt(base.x*base.x + base.y*base.y);
1283 base.x /= base_length;
1284 base.y /= base_length;
1286 dot = base.x*vertex.x + base.y*vertex.y;
1287 dot = min(max(dot, 0.0), base_length);
1288 base.x *= dot;
1289 base.y *= dot;
1291 deviation.x = vertex.x-base.x;
1292 deviation.y = vertex.y-base.y;
1294 return deviation.x*deviation.x + deviation.y*deviation.y;
1297 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1299 bezier_vector first_curve[3];
1300 bezier_vector second_curve[3];
1301 bezier_vector vertex;
1302 int total_vertices;
1304 if(bezier_deviation_squared(p) <= deviation*deviation)
1306 if(points)
1307 *points = p[2];
1308 return 1;
1311 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1312 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1314 first_curve[0] = p[0];
1315 first_curve[1].x = (p[0].x + p[1].x)/2;
1316 first_curve[1].y = (p[0].y + p[1].y)/2;
1317 first_curve[2] = vertex;
1319 second_curve[0] = vertex;
1320 second_curve[1].x = (p[2].x + p[1].x)/2;
1321 second_curve[1].y = (p[2].y + p[1].y)/2;
1322 second_curve[2] = p[2];
1324 total_vertices = bezier_approximate(first_curve, points, deviation);
1325 if(points)
1326 points += total_vertices;
1327 total_vertices += bezier_approximate(second_curve, points, deviation);
1328 return total_vertices;
1331 /***********************************************************************
1332 * wglUseFontOutlines_common
1334 static BOOL wglUseFontOutlines_common(HDC hdc,
1335 DWORD first,
1336 DWORD count,
1337 DWORD listBase,
1338 FLOAT deviation,
1339 FLOAT extrusion,
1340 int format,
1341 LPGLYPHMETRICSFLOAT lpgmf,
1342 BOOL unicode)
1344 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1345 UINT glyph;
1346 GLUtesselator *tess = NULL;
1347 LOGFONTW lf;
1348 HFONT old_font, unscaled_font;
1349 UINT em_size = 1024;
1350 RECT rc;
1352 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1353 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1355 if(deviation <= 0.0)
1356 deviation = 1.0/em_size;
1358 if(format == WGL_FONT_POLYGONS)
1360 tess = gluNewTess();
1361 if(!tess)
1363 ERR("glu32 is required for this function but isn't available\n");
1364 return FALSE;
1366 gluTessCallback(tess, GLU_TESS_VERTEX, (void *)tess_callback_vertex);
1367 gluTessCallback(tess, GLU_TESS_BEGIN, (void *)tess_callback_begin);
1368 gluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1371 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1372 rc.left = rc.right = rc.bottom = 0;
1373 rc.top = em_size;
1374 DPtoLP(hdc, (POINT*)&rc, 2);
1375 lf.lfHeight = -abs(rc.top - rc.bottom);
1376 lf.lfOrientation = lf.lfEscapement = 0;
1377 unscaled_font = CreateFontIndirectW(&lf);
1378 old_font = SelectObject(hdc, unscaled_font);
1380 for (glyph = first; glyph < first + count; glyph++)
1382 DWORD needed;
1383 GLYPHMETRICS gm;
1384 BYTE *buf;
1385 TTPOLYGONHEADER *pph;
1386 TTPOLYCURVE *ppc;
1387 GLdouble *vertices = NULL;
1388 int vertex_total = -1;
1390 if(unicode)
1391 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1392 else
1393 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1395 if(needed == GDI_ERROR)
1396 goto error;
1398 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1400 if(unicode)
1401 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1402 else
1403 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1405 TRACE("glyph %d\n", glyph);
1407 if(lpgmf)
1409 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1410 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1411 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1412 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1413 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1414 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1416 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1417 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1418 lpgmf++;
1421 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1422 funcs->gl.p_glFrontFace(GL_CCW);
1423 if(format == WGL_FONT_POLYGONS)
1425 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1426 gluTessNormal(tess, 0, 0, 1);
1427 gluTessBeginPolygon(tess, NULL);
1430 while(!vertices)
1432 if(vertex_total != -1)
1433 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1434 vertex_total = 0;
1436 pph = (TTPOLYGONHEADER*)buf;
1437 while((BYTE*)pph < buf + needed)
1439 GLdouble previous[3];
1440 fixed_to_double(pph->pfxStart, em_size, previous);
1442 if(vertices)
1443 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1445 if(format == WGL_FONT_POLYGONS)
1446 gluTessBeginContour(tess);
1447 else
1448 funcs->gl.p_glBegin(GL_LINE_LOOP);
1450 if(vertices)
1452 fixed_to_double(pph->pfxStart, em_size, vertices);
1453 if(format == WGL_FONT_POLYGONS)
1454 gluTessVertex(tess, vertices, vertices);
1455 else
1456 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1457 vertices += 3;
1459 vertex_total++;
1461 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1462 while((char*)ppc < (char*)pph + pph->cb)
1464 int i, j;
1465 int num;
1467 switch(ppc->wType) {
1468 case TT_PRIM_LINE:
1469 for(i = 0; i < ppc->cpfx; i++)
1471 if(vertices)
1473 TRACE("\t\tline to %d, %d\n",
1474 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1475 fixed_to_double(ppc->apfx[i], em_size, vertices);
1476 if(format == WGL_FONT_POLYGONS)
1477 gluTessVertex(tess, vertices, vertices);
1478 else
1479 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1480 vertices += 3;
1482 fixed_to_double(ppc->apfx[i], em_size, previous);
1483 vertex_total++;
1485 break;
1487 case TT_PRIM_QSPLINE:
1488 for(i = 0; i < ppc->cpfx-1; i++)
1490 bezier_vector curve[3];
1491 bezier_vector *points;
1492 GLdouble curve_vertex[3];
1494 if(vertices)
1495 TRACE("\t\tcurve %d,%d %d,%d\n",
1496 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1497 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1499 curve[0].x = previous[0];
1500 curve[0].y = previous[1];
1501 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1502 curve[1].x = curve_vertex[0];
1503 curve[1].y = curve_vertex[1];
1504 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1505 curve[2].x = curve_vertex[0];
1506 curve[2].y = curve_vertex[1];
1507 if(i < ppc->cpfx-2)
1509 curve[2].x = (curve[1].x + curve[2].x)/2;
1510 curve[2].y = (curve[1].y + curve[2].y)/2;
1512 num = bezier_approximate(curve, NULL, deviation);
1513 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1514 num = bezier_approximate(curve, points, deviation);
1515 vertex_total += num;
1516 if(vertices)
1518 for(j=0; j<num; j++)
1520 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1521 vertices[0] = points[j].x;
1522 vertices[1] = points[j].y;
1523 vertices[2] = 0.0;
1524 if(format == WGL_FONT_POLYGONS)
1525 gluTessVertex(tess, vertices, vertices);
1526 else
1527 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1528 vertices += 3;
1531 HeapFree(GetProcessHeap(), 0, points);
1532 previous[0] = curve[2].x;
1533 previous[1] = curve[2].y;
1535 break;
1536 default:
1537 ERR("\t\tcurve type = %d\n", ppc->wType);
1538 if(format == WGL_FONT_POLYGONS)
1539 gluTessEndContour(tess);
1540 else
1541 funcs->gl.p_glEnd();
1542 goto error_in_list;
1545 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1546 (ppc->cpfx - 1) * sizeof(POINTFX));
1548 if(format == WGL_FONT_POLYGONS)
1549 gluTessEndContour(tess);
1550 else
1551 funcs->gl.p_glEnd();
1552 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1556 error_in_list:
1557 if(format == WGL_FONT_POLYGONS)
1558 gluTessEndPolygon(tess);
1559 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1560 funcs->gl.p_glEndList();
1561 HeapFree(GetProcessHeap(), 0, buf);
1562 HeapFree(GetProcessHeap(), 0, vertices);
1565 error:
1566 DeleteObject(SelectObject(hdc, old_font));
1567 if(format == WGL_FONT_POLYGONS)
1568 gluDeleteTess(tess);
1569 return TRUE;
1573 /***********************************************************************
1574 * wglUseFontOutlinesA (OPENGL32.@)
1576 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1577 DWORD first,
1578 DWORD count,
1579 DWORD listBase,
1580 FLOAT deviation,
1581 FLOAT extrusion,
1582 int format,
1583 LPGLYPHMETRICSFLOAT lpgmf)
1585 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1588 /***********************************************************************
1589 * wglUseFontOutlinesW (OPENGL32.@)
1591 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1592 DWORD first,
1593 DWORD count,
1594 DWORD listBase,
1595 FLOAT deviation,
1596 FLOAT extrusion,
1597 int format,
1598 LPGLYPHMETRICSFLOAT lpgmf)
1600 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1603 /***********************************************************************
1604 * glDebugEntry (OPENGL32.@)
1606 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1608 return 0;
1611 static GLubyte *filter_extensions_list(const char *extensions, const char *disabled)
1613 char *p, *str;
1614 const char *end;
1616 p = str = HeapAlloc(GetProcessHeap(), 0, strlen(extensions) + 2);
1617 if (!str)
1618 return NULL;
1620 TRACE( "GL_EXTENSIONS:\n" );
1622 for (;;)
1624 while (*extensions == ' ')
1625 extensions++;
1626 if (!*extensions)
1627 break;
1628 if (!(end = strchr(extensions, ' ')))
1629 end = extensions + strlen(extensions);
1630 memcpy(p, extensions, end - extensions);
1631 p[end - extensions] = 0;
1632 if (!has_extension(disabled, p, strlen(p)))
1634 TRACE("++ %s\n", p);
1635 p += end - extensions;
1636 *p++ = ' ';
1638 else
1640 TRACE("-- %s (disabled by config)\n", p);
1642 extensions = end;
1644 *p = 0;
1645 return (GLubyte *)str;
1648 static GLuint *filter_extensions_index(const char *disabled)
1650 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1651 const char *ext, *end, *gl_ext;
1652 GLuint *disabled_exts, *new_disabled_exts;
1653 unsigned int i = 0, j, disabled_size;
1654 GLint extensions_count;
1656 if (!funcs->ext.p_glGetStringi)
1658 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
1660 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
1661 if (!funcs->ext.p_glGetStringi)
1662 return NULL;
1665 funcs->gl.p_glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
1666 disabled_size = 2;
1667 disabled_exts = HeapAlloc(GetProcessHeap(), 0, disabled_size * sizeof(*disabled_exts));
1668 if (!disabled_exts)
1669 return NULL;
1671 TRACE( "GL_EXTENSIONS:\n" );
1673 for (j = 0; j < extensions_count; ++j)
1675 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, j);
1676 ext = disabled;
1677 for (;;)
1679 while (*ext == ' ')
1680 ext++;
1681 if (!*ext)
1683 TRACE("++ %s\n", gl_ext);
1684 break;
1686 if (!(end = strchr(ext, ' ')))
1687 end = ext + strlen(ext);
1689 if (!strncmp(gl_ext, ext, end - ext) && !gl_ext[end - ext])
1691 if (i + 1 == disabled_size)
1693 disabled_size *= 2;
1694 new_disabled_exts = HeapReAlloc(GetProcessHeap(), 0, disabled_exts,
1695 disabled_size * sizeof(*disabled_exts));
1696 if (!new_disabled_exts)
1698 disabled_exts[i] = ~0u;
1699 return disabled_exts;
1701 disabled_exts = new_disabled_exts;
1703 TRACE("-- %s (disabled by config)\n", gl_ext);
1704 disabled_exts[i++] = j;
1705 break;
1707 ext = end;
1710 disabled_exts[i] = ~0u;
1711 return disabled_exts;
1714 /* build the extension string by filtering out the disabled extensions */
1715 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts)
1717 static const char *disabled;
1719 if (!disabled)
1721 HKEY hkey;
1722 DWORD size;
1723 char *str = NULL;
1725 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1726 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1728 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1730 str = HeapAlloc( GetProcessHeap(), 0, size );
1731 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1733 RegCloseKey( hkey );
1735 if (str)
1737 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1738 HeapFree( GetProcessHeap(), 0, str );
1740 else disabled = "";
1743 if (!disabled[0])
1744 return FALSE;
1746 if (extensions && !*exts_list)
1747 *exts_list = filter_extensions_list(extensions, disabled);
1749 if (!*disabled_exts)
1750 *disabled_exts = filter_extensions_index(disabled);
1752 return (exts_list && *exts_list) || *disabled_exts;
1755 /***********************************************************************
1756 * glGetString (OPENGL32.@)
1758 const GLubyte * WINAPI glGetString( GLenum name )
1760 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1761 const GLubyte *ret = funcs->gl.p_glGetString( name );
1763 if (name == GL_EXTENSIONS && ret)
1765 struct wgl_handle *ptr = get_current_context_ptr();
1766 if (ptr->u.context->extensions ||
1767 filter_extensions((const char *)ret, &ptr->u.context->extensions, &ptr->u.context->disabled_exts))
1768 ret = ptr->u.context->extensions;
1770 return ret;
1773 /* wrapper for glDebugMessageCallback* functions */
1774 static void gl_debug_message_callback( GLenum source, GLenum type, GLuint id, GLenum severity,
1775 GLsizei length, const GLchar *message,const void *userParam )
1777 struct wgl_handle *ptr = (struct wgl_handle *)userParam;
1778 if (!ptr->u.context->debug_callback) return;
1779 ptr->u.context->debug_callback( source, type, id, severity, length, message, ptr->u.context->debug_user );
1782 /***********************************************************************
1783 * glDebugMessageCallback
1785 void WINAPI glDebugMessageCallback( GLDEBUGPROC callback, const void *userParam )
1787 struct wgl_handle *ptr = get_current_context_ptr();
1788 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1790 TRACE( "(%p, %p)\n", callback, userParam );
1792 ptr->u.context->debug_callback = callback;
1793 ptr->u.context->debug_user = userParam;
1794 funcs->ext.p_glDebugMessageCallback( gl_debug_message_callback, ptr );
1797 /***********************************************************************
1798 * glDebugMessageCallbackAMD
1800 void WINAPI glDebugMessageCallbackAMD( GLDEBUGPROCAMD callback, void *userParam )
1802 struct wgl_handle *ptr = get_current_context_ptr();
1803 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1805 TRACE( "(%p, %p)\n", callback, userParam );
1807 ptr->u.context->debug_callback = callback;
1808 ptr->u.context->debug_user = userParam;
1809 funcs->ext.p_glDebugMessageCallbackAMD( gl_debug_message_callback, ptr );
1812 /***********************************************************************
1813 * glDebugMessageCallbackARB
1815 void WINAPI glDebugMessageCallbackARB( GLDEBUGPROCARB callback, const void *userParam )
1817 struct wgl_handle *ptr = get_current_context_ptr();
1818 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1820 TRACE( "(%p, %p)\n", callback, userParam );
1822 ptr->u.context->debug_callback = callback;
1823 ptr->u.context->debug_user = userParam;
1824 funcs->ext.p_glDebugMessageCallbackARB( gl_debug_message_callback, ptr );
1827 /***********************************************************************
1828 * OpenGL initialisation routine
1830 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1832 switch(reason)
1834 case DLL_PROCESS_ATTACH:
1835 NtCurrentTeb()->glTable = &null_opengl_funcs;
1836 break;
1837 case DLL_THREAD_ATTACH:
1838 NtCurrentTeb()->glTable = &null_opengl_funcs;
1839 break;
1841 return TRUE;