makefiles: Output rules for building generated .rc files.
[wine.git] / dlls / opengl32 / wgl.c
blob92410053d56811ccafbd81274b8803da850a3dea
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 * wglCreateContext (OPENGL32.@)
403 HGLRC WINAPI wglCreateContext(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 * wglGetCurrentContext (OPENGL32.@)
425 HGLRC WINAPI wglGetCurrentContext(void)
427 return NtCurrentTeb()->glCurrentRC;
430 /***********************************************************************
431 * wglDescribePixelFormat (OPENGL32.@)
433 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
435 struct opengl_funcs *funcs = get_dc_funcs( hdc );
436 if (!funcs) return 0;
437 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
440 /***********************************************************************
441 * wglChoosePixelFormat (OPENGL32.@)
443 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
445 PIXELFORMATDESCRIPTOR format, best;
446 int i, count, best_format;
447 int bestDBuffer = -1, bestStereo = -1;
449 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
450 "accum %u depth %u stencil %u aux %u\n",
451 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
452 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
453 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
455 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
456 if (!count) return 0;
458 best_format = 0;
459 best.dwFlags = 0;
460 best.cAlphaBits = -1;
461 best.cColorBits = -1;
462 best.cDepthBits = -1;
463 best.cStencilBits = -1;
464 best.cAuxBuffers = -1;
466 for (i = 1; i <= count; i++)
468 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
470 if (ppfd->iPixelType != format.iPixelType)
472 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
473 continue;
476 /* only use bitmap capable for formats for bitmap rendering */
477 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
479 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
480 continue;
483 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
484 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
485 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
486 * formats without the given flag set.
487 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
488 * has indicated that a format without stereo is returned when stereo is unavailable.
489 * So in case PFD_STEREO is set, formats that support it should have priority above formats
490 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
492 * To summarize the following is most likely the correct behavior:
493 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
494 * stereo set -> prefer stereo formats, but also accept non-stereo formats
495 * stereo don't care -> it doesn't matter whether we get stereo or not
497 * In Wine we will treat non-stereo the same way as don't care because it makes
498 * format selection even more complicated and second drivers with Stereo advertise
499 * each format twice anyway.
502 /* Doublebuffer, see the comments above */
503 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
505 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
506 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
507 goto found;
509 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
511 else if (!best_format)
512 goto found;
514 /* Stereo, see the comments above. */
515 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
517 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
518 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
519 goto found;
521 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
523 else if (!best_format)
524 goto found;
526 /* Below we will do a number of checks to select the 'best' pixelformat.
527 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
528 * The code works by trying to match the most important options as close as possible.
529 * When a reasonable format is found, we will try to match more options.
530 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
531 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
532 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
534 if (ppfd->cColorBits)
536 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
537 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
538 goto found;
540 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
542 TRACE( "color mismatch for iPixelFormat=%d\n", i );
543 continue;
546 if (ppfd->cAlphaBits)
548 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
549 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
550 goto found;
552 if (best.cAlphaBits != format.cAlphaBits)
554 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
555 continue;
558 if (ppfd->cDepthBits)
560 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
561 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
562 goto found;
564 if (best.cDepthBits != format.cDepthBits)
566 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
567 continue;
570 if (ppfd->cStencilBits)
572 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
573 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
574 goto found;
576 if (best.cStencilBits != format.cStencilBits)
578 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
579 continue;
582 if (ppfd->cAuxBuffers)
584 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
585 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
586 goto found;
588 if (best.cAuxBuffers != format.cAuxBuffers)
590 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
591 continue;
594 continue;
596 found:
597 best_format = i;
598 best = format;
599 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
600 bestStereo = format.dwFlags & PFD_STEREO;
603 TRACE( "returning %u\n", best_format );
604 return best_format;
607 /***********************************************************************
608 * wglGetPixelFormat (OPENGL32.@)
610 INT WINAPI wglGetPixelFormat(HDC hdc)
612 struct opengl_funcs *funcs = get_dc_funcs( hdc );
613 if (!funcs)
615 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
616 return 0;
618 return funcs->wgl.p_wglGetPixelFormat( hdc );
621 /***********************************************************************
622 * wglSetPixelFormat(OPENGL32.@)
624 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
626 struct opengl_funcs *funcs = get_dc_funcs( hdc );
627 if (!funcs) return FALSE;
628 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
631 /***********************************************************************
632 * wglSwapBuffers (OPENGL32.@)
634 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
636 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
638 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
639 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
641 if (TRACE_ON(fps))
643 static long prev_time, start_time;
644 static unsigned long frames, frames_total;
646 DWORD time = GetTickCount();
647 frames++;
648 frames_total++;
649 /* every 1.5 seconds */
650 if (time - prev_time > 1500)
652 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
653 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
654 prev_time = time;
655 frames = 0;
656 if (start_time == 0) start_time = time;
659 return TRUE;
662 /***********************************************************************
663 * wglCreateLayerContext (OPENGL32.@)
665 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
666 int iLayerPlane) {
667 TRACE("(%p,%d)\n", hdc, iLayerPlane);
669 if (iLayerPlane == 0) {
670 return wglCreateContext(hdc);
672 FIXME("no handler for layer %d\n", iLayerPlane);
674 return NULL;
677 /***********************************************************************
678 * wglDescribeLayerPlane (OPENGL32.@)
680 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
681 int iPixelFormat,
682 int iLayerPlane,
683 UINT nBytes,
684 LPLAYERPLANEDESCRIPTOR plpd) {
685 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
687 return FALSE;
690 /***********************************************************************
691 * wglGetLayerPaletteEntries (OPENGL32.@)
693 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
694 int iLayerPlane,
695 int iStart,
696 int cEntries,
697 const COLORREF *pcr) {
698 FIXME("(): stub!\n");
700 return 0;
703 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
705 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
707 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
709 TRACE("(%d, %p)\n", pname, data);
710 if (pname == GL_NUM_EXTENSIONS)
712 struct wgl_handle *ptr = get_current_context_ptr();
714 if (ptr->u.context->disabled_exts ||
715 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
717 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
718 GLint count, disabled_count = 0;
720 funcs->gl.p_glGetIntegerv(pname, &count);
721 while (*disabled_exts++ != ~0u)
722 disabled_count++;
723 *data = count - disabled_count;
724 return;
727 funcs->gl.p_glGetIntegerv(pname, data);
730 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
732 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
734 TRACE("(%d, %d)\n", name, index);
735 if (!funcs->ext.p_glGetStringi)
737 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
739 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
742 if (name == GL_EXTENSIONS)
744 struct wgl_handle *ptr = get_current_context_ptr();
746 if (ptr->u.context->disabled_exts ||
747 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
749 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
750 unsigned int disabled_count = 0;
752 while (index + disabled_count >= *disabled_exts++)
753 disabled_count++;
754 return funcs->ext.p_glGetStringi(name, index + disabled_count);
757 return funcs->ext.p_glGetStringi(name, index);
760 /* check if the extension is present in the list */
761 static BOOL has_extension( const char *list, const char *ext, size_t len )
763 if (!list)
765 const char *gl_ext;
766 unsigned int i;
767 GLint extensions_count;
769 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
770 for (i = 0; i < extensions_count; ++i)
772 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
773 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
774 return TRUE;
776 return FALSE;
779 while (list)
781 while (*list == ' ') list++;
782 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
783 list = strchr( list, ' ' );
785 return FALSE;
788 static int compar(const void *elt_a, const void *elt_b) {
789 return strcmp(((const OpenGL_extension *) elt_a)->name,
790 ((const OpenGL_extension *) elt_b)->name);
793 /* Check if a GL extension is supported */
794 static BOOL is_extension_supported(const char* extension)
796 enum wgl_handle_type type = get_current_context_type();
797 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
798 const char *gl_ext_string = NULL;
799 size_t len;
801 TRACE("Checking for extension '%s'\n", extension);
803 if (type == HANDLE_CONTEXT)
805 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
806 if (!gl_ext_string)
808 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
809 return FALSE;
813 /* We use the GetProcAddress function from the display driver to retrieve function pointers
814 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
815 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
816 * require the function to return NULL when an extension isn't found. For this reason we check
817 * if the OpenGL extension required for the function we are looking up is supported. */
819 while ((len = strcspn(extension, " ")) != 0)
821 /* Check if the extension is part of the GL extension string to see if it is supported. */
822 if (has_extension(gl_ext_string, extension, len))
823 return TRUE;
825 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
826 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
827 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
828 * Check if we are searching for a core GL function */
829 if(strncmp(extension, "GL_VERSION_", 11) == 0)
831 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
832 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
834 if(!gl_version) {
835 ERR("No OpenGL version found!\n");
836 return FALSE;
839 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
840 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
841 if( (gl_version[0] > version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
842 return TRUE;
844 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]);
847 if (extension[len] == ' ') len++;
848 extension += len;
851 return FALSE;
854 /***********************************************************************
855 * wglGetProcAddress (OPENGL32.@)
857 PROC WINAPI wglGetProcAddress( LPCSTR name )
859 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
860 void **func_ptr;
861 OpenGL_extension ext;
862 const OpenGL_extension *ext_ret;
864 if (!name) return NULL;
866 /* Without an active context opengl32 doesn't know to what
867 * driver it has to dispatch wglGetProcAddress.
869 if (!get_current_context_ptr())
871 WARN("No active WGL context found\n");
872 return NULL;
875 ext.name = name;
876 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
877 if (!ext_ret)
879 WARN("Function %s unknown\n", name);
880 return NULL;
883 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
884 if (!*func_ptr)
886 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
888 if (!is_extension_supported(ext_ret->extension))
890 unsigned int i;
891 static const struct { const char *name, *alt; } alternatives[] =
893 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
894 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
897 for (i = 0; i < ARRAY_SIZE(alternatives); i++)
899 if (strcmp( name, alternatives[i].name )) continue;
900 WARN("Extension %s required for %s not supported, trying %s\n",
901 ext_ret->extension, name, alternatives[i].alt );
902 return wglGetProcAddress( alternatives[i].alt );
904 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
905 return NULL;
908 if (driver_func == NULL)
910 WARN("Function %s not supported by driver\n", name);
911 return NULL;
913 *func_ptr = driver_func;
916 TRACE("returning %s -> %p\n", name, ext_ret->func);
917 return ext_ret->func;
920 /***********************************************************************
921 * wglRealizeLayerPalette (OPENGL32.@)
923 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
924 int iLayerPlane,
925 BOOL bRealize) {
926 FIXME("()\n");
928 return FALSE;
931 /***********************************************************************
932 * wglSetLayerPaletteEntries (OPENGL32.@)
934 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
935 int iLayerPlane,
936 int iStart,
937 int cEntries,
938 const COLORREF *pcr) {
939 FIXME("(): stub!\n");
941 return 0;
944 /***********************************************************************
945 * wglSwapLayerBuffers (OPENGL32.@)
947 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
948 UINT fuPlanes) {
949 TRACE("(%p, %08x)\n", hdc, fuPlanes);
951 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
952 if (!wglSwapBuffers( hdc )) return FALSE;
953 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
956 if (fuPlanes) {
957 WARN("Following layers unhandled: %08x\n", fuPlanes);
960 return TRUE;
963 /***********************************************************************
964 * wglBindTexImageARB
966 * Provided by the WGL_ARB_render_texture extension.
968 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
970 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
971 BOOL ret;
973 if (!ptr) return FALSE;
974 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
975 release_handle_ptr( ptr );
976 return ret;
979 /***********************************************************************
980 * wglReleaseTexImageARB
982 * Provided by the WGL_ARB_render_texture extension.
984 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
986 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
987 BOOL ret;
989 if (!ptr) return FALSE;
990 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
991 release_handle_ptr( ptr );
992 return ret;
995 /***********************************************************************
996 * wglSetPbufferAttribARB
998 * Provided by the WGL_ARB_render_texture extension.
1000 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
1002 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1003 BOOL ret;
1005 if (!ptr) return FALSE;
1006 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
1007 release_handle_ptr( ptr );
1008 return ret;
1011 /***********************************************************************
1012 * wglCreatePbufferARB
1014 * Provided by the WGL_ARB_pbuffer extension.
1016 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1018 HPBUFFERARB ret;
1019 struct wgl_pbuffer *pbuffer;
1020 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1022 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1023 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1024 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1025 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1026 return ret;
1029 /***********************************************************************
1030 * wglGetPbufferDCARB
1032 * Provided by the WGL_ARB_pbuffer extension.
1034 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1036 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1037 HDC ret;
1039 if (!ptr) return 0;
1040 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1041 release_handle_ptr( ptr );
1042 return ret;
1045 /***********************************************************************
1046 * wglReleasePbufferDCARB
1048 * Provided by the WGL_ARB_pbuffer extension.
1050 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1052 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1053 BOOL ret;
1055 if (!ptr) return FALSE;
1056 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1057 release_handle_ptr( ptr );
1058 return ret;
1061 /***********************************************************************
1062 * wglDestroyPbufferARB
1064 * Provided by the WGL_ARB_pbuffer extension.
1066 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1068 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1070 if (!ptr) return FALSE;
1071 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1072 free_handle_ptr( ptr );
1073 return TRUE;
1076 /***********************************************************************
1077 * wglQueryPbufferARB
1079 * Provided by the WGL_ARB_pbuffer extension.
1081 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1083 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1084 BOOL ret;
1086 if (!ptr) return FALSE;
1087 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1088 release_handle_ptr( ptr );
1089 return ret;
1092 /***********************************************************************
1093 * wglUseFontBitmaps_common
1095 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1097 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1098 GLYPHMETRICS gm;
1099 unsigned int glyph, size = 0;
1100 void *bitmap = NULL, *gl_bitmap = NULL;
1101 int org_alignment;
1102 BOOL ret = TRUE;
1104 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1105 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1107 for (glyph = first; glyph < first + count; glyph++) {
1108 unsigned int needed_size, height, width, width_int;
1110 if (unicode)
1111 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1112 else
1113 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1115 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1116 if (needed_size == GDI_ERROR) {
1117 ret = FALSE;
1118 break;
1121 if (needed_size > size) {
1122 size = needed_size;
1123 HeapFree(GetProcessHeap(), 0, bitmap);
1124 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1125 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1126 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1128 if (needed_size != 0) {
1129 if (unicode)
1130 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1131 size, bitmap, &identity) != GDI_ERROR);
1132 else
1133 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1134 size, bitmap, &identity) != GDI_ERROR);
1135 if (!ret) break;
1138 if (TRACE_ON(wgl)) {
1139 unsigned int bitmask;
1140 unsigned char *bitmap_ = bitmap;
1142 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1143 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1144 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1145 if (needed_size != 0) {
1146 TRACE(" - bitmap:\n");
1147 for (height = 0; height < gm.gmBlackBoxY; height++) {
1148 TRACE(" ");
1149 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1150 if (bitmask == 0) {
1151 bitmap_ += 1;
1152 bitmask = 0x80;
1154 if (*bitmap_ & bitmask)
1155 TRACE("*");
1156 else
1157 TRACE(" ");
1159 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1160 TRACE("\n");
1165 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1166 * glyph for it to be drawn properly.
1168 if (needed_size != 0) {
1169 width_int = (gm.gmBlackBoxX + 31) / 32;
1170 for (height = 0; height < gm.gmBlackBoxY; height++) {
1171 for (width = 0; width < width_int; width++) {
1172 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1173 ((int *) bitmap)[height * width_int + width];
1178 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1179 if (needed_size != 0) {
1180 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1181 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1182 gm.gmCellIncX, gm.gmCellIncY,
1183 gl_bitmap);
1184 } else {
1185 /* This is the case of 'empty' glyphs like the space character */
1186 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1188 funcs->gl.p_glEndList();
1191 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1192 HeapFree(GetProcessHeap(), 0, bitmap);
1193 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1194 return ret;
1197 /***********************************************************************
1198 * wglUseFontBitmapsA (OPENGL32.@)
1200 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1202 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1205 /***********************************************************************
1206 * wglUseFontBitmapsW (OPENGL32.@)
1208 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1210 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1213 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1215 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1216 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1217 vertex[2] = 0.0;
1220 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1222 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1223 GLdouble *dbl = vertex;
1224 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1225 funcs->gl.p_glVertex3dv(vertex);
1228 static void WINAPI tess_callback_begin(GLenum which)
1230 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1231 TRACE("%d\n", which);
1232 funcs->gl.p_glBegin(which);
1235 static void WINAPI tess_callback_end(void)
1237 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1238 TRACE("\n");
1239 funcs->gl.p_glEnd();
1242 typedef struct _bezier_vector {
1243 GLdouble x;
1244 GLdouble y;
1245 } bezier_vector;
1247 static double bezier_deviation_squared(const bezier_vector *p)
1249 bezier_vector deviation;
1250 bezier_vector vertex;
1251 bezier_vector base;
1252 double base_length;
1253 double dot;
1255 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1256 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1258 base.x = p[2].x - p[0].x;
1259 base.y = p[2].y - p[0].y;
1261 base_length = sqrt(base.x*base.x + base.y*base.y);
1262 base.x /= base_length;
1263 base.y /= base_length;
1265 dot = base.x*vertex.x + base.y*vertex.y;
1266 dot = min(max(dot, 0.0), base_length);
1267 base.x *= dot;
1268 base.y *= dot;
1270 deviation.x = vertex.x-base.x;
1271 deviation.y = vertex.y-base.y;
1273 return deviation.x*deviation.x + deviation.y*deviation.y;
1276 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1278 bezier_vector first_curve[3];
1279 bezier_vector second_curve[3];
1280 bezier_vector vertex;
1281 int total_vertices;
1283 if(bezier_deviation_squared(p) <= deviation*deviation)
1285 if(points)
1286 *points = p[2];
1287 return 1;
1290 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1291 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1293 first_curve[0] = p[0];
1294 first_curve[1].x = (p[0].x + p[1].x)/2;
1295 first_curve[1].y = (p[0].y + p[1].y)/2;
1296 first_curve[2] = vertex;
1298 second_curve[0] = vertex;
1299 second_curve[1].x = (p[2].x + p[1].x)/2;
1300 second_curve[1].y = (p[2].y + p[1].y)/2;
1301 second_curve[2] = p[2];
1303 total_vertices = bezier_approximate(first_curve, points, deviation);
1304 if(points)
1305 points += total_vertices;
1306 total_vertices += bezier_approximate(second_curve, points, deviation);
1307 return total_vertices;
1310 /***********************************************************************
1311 * wglUseFontOutlines_common
1313 static BOOL wglUseFontOutlines_common(HDC hdc,
1314 DWORD first,
1315 DWORD count,
1316 DWORD listBase,
1317 FLOAT deviation,
1318 FLOAT extrusion,
1319 int format,
1320 LPGLYPHMETRICSFLOAT lpgmf,
1321 BOOL unicode)
1323 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1324 UINT glyph;
1325 GLUtesselator *tess = NULL;
1326 LOGFONTW lf;
1327 HFONT old_font, unscaled_font;
1328 UINT em_size = 1024;
1329 RECT rc;
1331 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1332 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1334 if(deviation <= 0.0)
1335 deviation = 1.0/em_size;
1337 if(format == WGL_FONT_POLYGONS)
1339 tess = gluNewTess();
1340 if(!tess)
1342 ERR("glu32 is required for this function but isn't available\n");
1343 return FALSE;
1345 gluTessCallback(tess, GLU_TESS_VERTEX, (void *)tess_callback_vertex);
1346 gluTessCallback(tess, GLU_TESS_BEGIN, (void *)tess_callback_begin);
1347 gluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1350 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1351 rc.left = rc.right = rc.bottom = 0;
1352 rc.top = em_size;
1353 DPtoLP(hdc, (POINT*)&rc, 2);
1354 lf.lfHeight = -abs(rc.top - rc.bottom);
1355 lf.lfOrientation = lf.lfEscapement = 0;
1356 unscaled_font = CreateFontIndirectW(&lf);
1357 old_font = SelectObject(hdc, unscaled_font);
1359 for (glyph = first; glyph < first + count; glyph++)
1361 DWORD needed;
1362 GLYPHMETRICS gm;
1363 BYTE *buf;
1364 TTPOLYGONHEADER *pph;
1365 TTPOLYCURVE *ppc;
1366 GLdouble *vertices = NULL;
1367 int vertex_total = -1;
1369 if(unicode)
1370 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1371 else
1372 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1374 if(needed == GDI_ERROR)
1375 goto error;
1377 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1379 if(unicode)
1380 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1381 else
1382 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1384 TRACE("glyph %d\n", glyph);
1386 if(lpgmf)
1388 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1389 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1390 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1391 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1392 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1393 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1395 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1396 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1397 lpgmf++;
1400 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1401 funcs->gl.p_glFrontFace(GL_CCW);
1402 if(format == WGL_FONT_POLYGONS)
1404 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1405 gluTessNormal(tess, 0, 0, 1);
1406 gluTessBeginPolygon(tess, NULL);
1409 while(!vertices)
1411 if(vertex_total != -1)
1412 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1413 vertex_total = 0;
1415 pph = (TTPOLYGONHEADER*)buf;
1416 while((BYTE*)pph < buf + needed)
1418 GLdouble previous[3];
1419 fixed_to_double(pph->pfxStart, em_size, previous);
1421 if(vertices)
1422 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1424 if(format == WGL_FONT_POLYGONS)
1425 gluTessBeginContour(tess);
1426 else
1427 funcs->gl.p_glBegin(GL_LINE_LOOP);
1429 if(vertices)
1431 fixed_to_double(pph->pfxStart, em_size, vertices);
1432 if(format == WGL_FONT_POLYGONS)
1433 gluTessVertex(tess, vertices, vertices);
1434 else
1435 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1436 vertices += 3;
1438 vertex_total++;
1440 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1441 while((char*)ppc < (char*)pph + pph->cb)
1443 int i, j;
1444 int num;
1446 switch(ppc->wType) {
1447 case TT_PRIM_LINE:
1448 for(i = 0; i < ppc->cpfx; i++)
1450 if(vertices)
1452 TRACE("\t\tline to %d, %d\n",
1453 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1454 fixed_to_double(ppc->apfx[i], em_size, vertices);
1455 if(format == WGL_FONT_POLYGONS)
1456 gluTessVertex(tess, vertices, vertices);
1457 else
1458 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1459 vertices += 3;
1461 fixed_to_double(ppc->apfx[i], em_size, previous);
1462 vertex_total++;
1464 break;
1466 case TT_PRIM_QSPLINE:
1467 for(i = 0; i < ppc->cpfx-1; i++)
1469 bezier_vector curve[3];
1470 bezier_vector *points;
1471 GLdouble curve_vertex[3];
1473 if(vertices)
1474 TRACE("\t\tcurve %d,%d %d,%d\n",
1475 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1476 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1478 curve[0].x = previous[0];
1479 curve[0].y = previous[1];
1480 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1481 curve[1].x = curve_vertex[0];
1482 curve[1].y = curve_vertex[1];
1483 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1484 curve[2].x = curve_vertex[0];
1485 curve[2].y = curve_vertex[1];
1486 if(i < ppc->cpfx-2)
1488 curve[2].x = (curve[1].x + curve[2].x)/2;
1489 curve[2].y = (curve[1].y + curve[2].y)/2;
1491 num = bezier_approximate(curve, NULL, deviation);
1492 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1493 num = bezier_approximate(curve, points, deviation);
1494 vertex_total += num;
1495 if(vertices)
1497 for(j=0; j<num; j++)
1499 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1500 vertices[0] = points[j].x;
1501 vertices[1] = points[j].y;
1502 vertices[2] = 0.0;
1503 if(format == WGL_FONT_POLYGONS)
1504 gluTessVertex(tess, vertices, vertices);
1505 else
1506 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1507 vertices += 3;
1510 HeapFree(GetProcessHeap(), 0, points);
1511 previous[0] = curve[2].x;
1512 previous[1] = curve[2].y;
1514 break;
1515 default:
1516 ERR("\t\tcurve type = %d\n", ppc->wType);
1517 if(format == WGL_FONT_POLYGONS)
1518 gluTessEndContour(tess);
1519 else
1520 funcs->gl.p_glEnd();
1521 goto error_in_list;
1524 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1525 (ppc->cpfx - 1) * sizeof(POINTFX));
1527 if(format == WGL_FONT_POLYGONS)
1528 gluTessEndContour(tess);
1529 else
1530 funcs->gl.p_glEnd();
1531 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1535 error_in_list:
1536 if(format == WGL_FONT_POLYGONS)
1537 gluTessEndPolygon(tess);
1538 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1539 funcs->gl.p_glEndList();
1540 HeapFree(GetProcessHeap(), 0, buf);
1541 HeapFree(GetProcessHeap(), 0, vertices);
1544 error:
1545 DeleteObject(SelectObject(hdc, old_font));
1546 if(format == WGL_FONT_POLYGONS)
1547 gluDeleteTess(tess);
1548 return TRUE;
1552 /***********************************************************************
1553 * wglUseFontOutlinesA (OPENGL32.@)
1555 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1556 DWORD first,
1557 DWORD count,
1558 DWORD listBase,
1559 FLOAT deviation,
1560 FLOAT extrusion,
1561 int format,
1562 LPGLYPHMETRICSFLOAT lpgmf)
1564 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1567 /***********************************************************************
1568 * wglUseFontOutlinesW (OPENGL32.@)
1570 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1571 DWORD first,
1572 DWORD count,
1573 DWORD listBase,
1574 FLOAT deviation,
1575 FLOAT extrusion,
1576 int format,
1577 LPGLYPHMETRICSFLOAT lpgmf)
1579 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1582 /***********************************************************************
1583 * glDebugEntry (OPENGL32.@)
1585 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1587 return 0;
1590 static GLubyte *filter_extensions_list(const char *extensions, const char *disabled)
1592 char *p, *str;
1593 const char *end;
1595 p = str = HeapAlloc(GetProcessHeap(), 0, strlen(extensions) + 2);
1596 if (!str)
1597 return NULL;
1599 TRACE( "GL_EXTENSIONS:\n" );
1601 for (;;)
1603 while (*extensions == ' ')
1604 extensions++;
1605 if (!*extensions)
1606 break;
1607 if (!(end = strchr(extensions, ' ')))
1608 end = extensions + strlen(extensions);
1609 memcpy(p, extensions, end - extensions);
1610 p[end - extensions] = 0;
1611 if (!has_extension(disabled, p, strlen(p)))
1613 TRACE("++ %s\n", p);
1614 p += end - extensions;
1615 *p++ = ' ';
1617 else
1619 TRACE("-- %s (disabled by config)\n", p);
1621 extensions = end;
1623 *p = 0;
1624 return (GLubyte *)str;
1627 static GLuint *filter_extensions_index(const char *disabled)
1629 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1630 const char *ext, *end, *gl_ext;
1631 GLuint *disabled_exts, *new_disabled_exts;
1632 unsigned int i = 0, j, disabled_size;
1633 GLint extensions_count;
1635 if (!funcs->ext.p_glGetStringi)
1637 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
1639 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
1640 if (!funcs->ext.p_glGetStringi)
1641 return NULL;
1644 funcs->gl.p_glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
1645 disabled_size = 2;
1646 disabled_exts = HeapAlloc(GetProcessHeap(), 0, disabled_size * sizeof(*disabled_exts));
1647 if (!disabled_exts)
1648 return NULL;
1650 TRACE( "GL_EXTENSIONS:\n" );
1652 for (j = 0; j < extensions_count; ++j)
1654 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, j);
1655 ext = disabled;
1656 for (;;)
1658 while (*ext == ' ')
1659 ext++;
1660 if (!*ext)
1662 TRACE("++ %s\n", gl_ext);
1663 break;
1665 if (!(end = strchr(ext, ' ')))
1666 end = ext + strlen(ext);
1668 if (!strncmp(gl_ext, ext, end - ext) && !gl_ext[end - ext])
1670 if (i + 1 == disabled_size)
1672 disabled_size *= 2;
1673 new_disabled_exts = HeapReAlloc(GetProcessHeap(), 0, disabled_exts,
1674 disabled_size * sizeof(*disabled_exts));
1675 if (!new_disabled_exts)
1677 disabled_exts[i] = ~0u;
1678 return disabled_exts;
1680 disabled_exts = new_disabled_exts;
1682 TRACE("-- %s (disabled by config)\n", gl_ext);
1683 disabled_exts[i++] = j;
1684 break;
1686 ext = end;
1689 disabled_exts[i] = ~0u;
1690 return disabled_exts;
1693 /* build the extension string by filtering out the disabled extensions */
1694 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts)
1696 static const char *disabled;
1698 if (!disabled)
1700 HKEY hkey;
1701 DWORD size;
1702 char *str = NULL;
1704 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1705 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1707 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1709 str = HeapAlloc( GetProcessHeap(), 0, size );
1710 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1712 RegCloseKey( hkey );
1714 if (str)
1716 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1717 HeapFree( GetProcessHeap(), 0, str );
1719 else disabled = "";
1722 if (!disabled[0])
1723 return FALSE;
1725 if (extensions && !*exts_list)
1726 *exts_list = filter_extensions_list(extensions, disabled);
1728 if (!*disabled_exts)
1729 *disabled_exts = filter_extensions_index(disabled);
1731 return (exts_list && *exts_list) || *disabled_exts;
1734 /***********************************************************************
1735 * glGetString (OPENGL32.@)
1737 const GLubyte * WINAPI glGetString( GLenum name )
1739 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1740 const GLubyte *ret = funcs->gl.p_glGetString( name );
1742 if (name == GL_EXTENSIONS && ret)
1744 struct wgl_handle *ptr = get_current_context_ptr();
1745 if (ptr->u.context->extensions ||
1746 filter_extensions((const char *)ret, &ptr->u.context->extensions, &ptr->u.context->disabled_exts))
1747 ret = ptr->u.context->extensions;
1749 return ret;
1752 /* wrapper for glDebugMessageCallback* functions */
1753 static void gl_debug_message_callback( GLenum source, GLenum type, GLuint id, GLenum severity,
1754 GLsizei length, const GLchar *message,const void *userParam )
1756 struct wgl_handle *ptr = (struct wgl_handle *)userParam;
1757 if (!ptr->u.context->debug_callback) return;
1758 ptr->u.context->debug_callback( source, type, id, severity, length, message, ptr->u.context->debug_user );
1761 /***********************************************************************
1762 * glDebugMessageCallback
1764 void WINAPI glDebugMessageCallback( GLDEBUGPROC callback, const void *userParam )
1766 struct wgl_handle *ptr = get_current_context_ptr();
1767 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1769 TRACE( "(%p, %p)\n", callback, userParam );
1771 ptr->u.context->debug_callback = callback;
1772 ptr->u.context->debug_user = userParam;
1773 funcs->ext.p_glDebugMessageCallback( gl_debug_message_callback, ptr );
1776 /***********************************************************************
1777 * glDebugMessageCallbackAMD
1779 void WINAPI glDebugMessageCallbackAMD( GLDEBUGPROCAMD callback, void *userParam )
1781 struct wgl_handle *ptr = get_current_context_ptr();
1782 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1784 TRACE( "(%p, %p)\n", callback, userParam );
1786 ptr->u.context->debug_callback = callback;
1787 ptr->u.context->debug_user = userParam;
1788 funcs->ext.p_glDebugMessageCallbackAMD( gl_debug_message_callback, ptr );
1791 /***********************************************************************
1792 * glDebugMessageCallbackARB
1794 void WINAPI glDebugMessageCallbackARB( GLDEBUGPROCARB callback, const void *userParam )
1796 struct wgl_handle *ptr = get_current_context_ptr();
1797 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1799 TRACE( "(%p, %p)\n", callback, userParam );
1801 ptr->u.context->debug_callback = callback;
1802 ptr->u.context->debug_user = userParam;
1803 funcs->ext.p_glDebugMessageCallbackARB( gl_debug_message_callback, ptr );
1806 /***********************************************************************
1807 * OpenGL initialisation routine
1809 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1811 switch(reason)
1813 case DLL_PROCESS_ATTACH:
1814 NtCurrentTeb()->glTable = &null_opengl_funcs;
1815 break;
1816 case DLL_THREAD_ATTACH:
1817 NtCurrentTeb()->glTable = &null_opengl_funcs;
1818 break;
1820 return TRUE;