ddraw/tests: Rewrite LimitTest().
[wine.git] / dlls / opengl32 / wgl.c
blobb73920cb256c5e80930760153b276d230c1ffb3d
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 GLubyte *extensions; /* extension string */
62 GLuint *disabled_exts; /* indices of disabled extensions */
63 struct wgl_context *drv_ctx; /* driver context */
66 struct wgl_handle
68 UINT handle;
69 struct opengl_funcs *funcs;
70 union
72 struct opengl_context *context; /* for HANDLE_CONTEXT */
73 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
74 struct wgl_handle *next; /* for free handles */
75 } u;
78 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
79 static struct wgl_handle *next_free;
80 static unsigned int handle_count;
82 static CRITICAL_SECTION wgl_section;
83 static CRITICAL_SECTION_DEBUG critsect_debug =
85 0, 0, &wgl_section,
86 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
89 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
91 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
93 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
95 WORD generation = HIWORD( ptr->handle ) + 1;
96 if (!generation) generation++;
97 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
98 return ULongToHandle( ptr->handle );
101 /* the current context is assumed valid and doesn't need locking */
102 static inline struct wgl_handle *get_current_context_ptr(void)
104 if (!NtCurrentTeb()->glCurrentRC) return NULL;
105 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
108 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
110 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
112 EnterCriticalSection( &wgl_section );
113 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
114 return &wgl_handles[index];
116 LeaveCriticalSection( &wgl_section );
117 SetLastError( ERROR_INVALID_HANDLE );
118 return NULL;
121 static void release_handle_ptr( struct wgl_handle *ptr )
123 if (ptr) LeaveCriticalSection( &wgl_section );
126 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
128 HANDLE handle = 0;
129 struct wgl_handle *ptr = NULL;
131 EnterCriticalSection( &wgl_section );
132 if ((ptr = next_free))
133 next_free = next_free->u.next;
134 else if (handle_count < MAX_WGL_HANDLES)
135 ptr = &wgl_handles[handle_count++];
137 if (ptr)
139 ptr->funcs = funcs;
140 ptr->u.context = user_ptr;
141 handle = next_handle( ptr, type );
143 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
144 LeaveCriticalSection( &wgl_section );
145 return handle;
148 static void free_handle_ptr( struct wgl_handle *ptr )
150 ptr->handle |= 0xffff;
151 ptr->u.next = next_free;
152 ptr->funcs = NULL;
153 next_free = ptr;
154 LeaveCriticalSection( &wgl_section );
157 static inline enum wgl_handle_type get_current_context_type(void)
159 if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
160 return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
163 /***********************************************************************
164 * wglCopyContext (OPENGL32.@)
166 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
168 struct wgl_handle *src, *dst;
169 BOOL ret = FALSE;
171 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
172 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
174 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
175 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
176 dst->u.context->drv_ctx, mask );
178 release_handle_ptr( dst );
179 release_handle_ptr( src );
180 return ret;
183 /***********************************************************************
184 * wglDeleteContext (OPENGL32.@)
186 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
188 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
190 if (!ptr) return FALSE;
192 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
194 SetLastError( ERROR_BUSY );
195 release_handle_ptr( ptr );
196 return FALSE;
198 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
199 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
200 HeapFree( GetProcessHeap(), 0, ptr->u.context->disabled_exts );
201 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
202 HeapFree( GetProcessHeap(), 0, ptr->u.context );
203 free_handle_ptr( ptr );
204 return TRUE;
207 /***********************************************************************
208 * wglMakeCurrent (OPENGL32.@)
210 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
212 BOOL ret = TRUE;
213 struct wgl_handle *ptr, *prev = get_current_context_ptr();
215 if (hglrc)
217 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
218 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
220 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
221 if (ret)
223 if (prev) prev->u.context->tid = 0;
224 ptr->u.context->tid = GetCurrentThreadId();
225 ptr->u.context->draw_dc = hdc;
226 ptr->u.context->read_dc = hdc;
227 NtCurrentTeb()->glCurrentRC = hglrc;
228 NtCurrentTeb()->glTable = ptr->funcs;
231 else
233 SetLastError( ERROR_BUSY );
234 ret = FALSE;
236 release_handle_ptr( ptr );
238 else if (prev)
240 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
241 prev->u.context->tid = 0;
242 NtCurrentTeb()->glCurrentRC = 0;
243 NtCurrentTeb()->glTable = &null_opengl_funcs;
245 else if (!hdc)
247 SetLastError( ERROR_INVALID_HANDLE );
248 ret = FALSE;
250 return ret;
253 /***********************************************************************
254 * wglCreateContextAttribsARB
256 * Provided by the WGL_ARB_create_context extension.
258 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
260 HGLRC ret = 0;
261 struct wgl_context *drv_ctx;
262 struct wgl_handle *share_ptr = NULL;
263 struct opengl_context *context;
264 struct opengl_funcs *funcs = get_dc_funcs( hdc );
266 if (!funcs)
268 SetLastError( ERROR_DC_NOT_FOUND );
269 return 0;
271 if (!funcs->ext.p_wglCreateContextAttribsARB) return 0;
272 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT )))
274 SetLastError( ERROR_INVALID_OPERATION );
275 return 0;
277 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
278 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
280 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
282 enum wgl_handle_type type = HANDLE_CONTEXT;
284 if (attribs)
286 while (*attribs)
288 if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
290 if (attribs[1] >= 3)
291 type = HANDLE_CONTEXT_V3;
292 break;
294 attribs += 2;
298 context->drv_ctx = drv_ctx;
299 if (!(ret = alloc_handle( type, funcs, context )))
300 HeapFree( GetProcessHeap(), 0, context );
302 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
304 release_handle_ptr( share_ptr );
305 return ret;
309 /***********************************************************************
310 * wglMakeContextCurrentARB
312 * Provided by the WGL_ARB_make_current_read extension.
314 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
316 BOOL ret = TRUE;
317 struct wgl_handle *ptr, *prev = get_current_context_ptr();
319 if (hglrc)
321 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
322 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
324 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
325 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
326 ptr->u.context->drv_ctx ));
327 if (ret)
329 if (prev) prev->u.context->tid = 0;
330 ptr->u.context->tid = GetCurrentThreadId();
331 ptr->u.context->draw_dc = draw_hdc;
332 ptr->u.context->read_dc = read_hdc;
333 NtCurrentTeb()->glCurrentRC = hglrc;
334 NtCurrentTeb()->glTable = ptr->funcs;
337 else
339 SetLastError( ERROR_BUSY );
340 ret = FALSE;
342 release_handle_ptr( ptr );
344 else if (prev)
346 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
347 prev->u.context->tid = 0;
348 NtCurrentTeb()->glCurrentRC = 0;
349 NtCurrentTeb()->glTable = &null_opengl_funcs;
351 return ret;
354 /***********************************************************************
355 * wglGetCurrentReadDCARB
357 * Provided by the WGL_ARB_make_current_read extension.
359 HDC WINAPI wglGetCurrentReadDCARB(void)
361 struct wgl_handle *ptr = get_current_context_ptr();
363 if (!ptr) return 0;
364 return ptr->u.context->read_dc;
367 /***********************************************************************
368 * wglShareLists (OPENGL32.@)
370 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
372 BOOL ret = FALSE;
373 struct wgl_handle *src, *dst;
375 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
376 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
378 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
379 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
381 release_handle_ptr( dst );
382 release_handle_ptr( src );
383 return ret;
386 /***********************************************************************
387 * wglGetCurrentDC (OPENGL32.@)
389 HDC WINAPI wglGetCurrentDC(void)
391 struct wgl_handle *ptr = get_current_context_ptr();
393 if (!ptr) return 0;
394 return ptr->u.context->draw_dc;
397 /***********************************************************************
398 * wglCreateContext (OPENGL32.@)
400 HGLRC WINAPI wglCreateContext(HDC hdc)
402 HGLRC ret = 0;
403 struct wgl_context *drv_ctx;
404 struct opengl_context *context;
405 struct opengl_funcs *funcs = get_dc_funcs( hdc );
407 if (!funcs) return 0;
408 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
409 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
411 context->drv_ctx = drv_ctx;
412 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
413 HeapFree( GetProcessHeap(), 0, context );
415 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
416 return ret;
419 /***********************************************************************
420 * wglGetCurrentContext (OPENGL32.@)
422 HGLRC WINAPI wglGetCurrentContext(void)
424 return NtCurrentTeb()->glCurrentRC;
427 /***********************************************************************
428 * wglDescribePixelFormat (OPENGL32.@)
430 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
432 struct opengl_funcs *funcs = get_dc_funcs( hdc );
433 if (!funcs) return 0;
434 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
437 /***********************************************************************
438 * wglChoosePixelFormat (OPENGL32.@)
440 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
442 PIXELFORMATDESCRIPTOR format, best;
443 int i, count, best_format;
444 int bestDBuffer = -1, bestStereo = -1;
446 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
447 "accum %u depth %u stencil %u aux %u\n",
448 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
449 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
450 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
452 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
453 if (!count) return 0;
455 best_format = 0;
456 best.dwFlags = 0;
457 best.cAlphaBits = -1;
458 best.cColorBits = -1;
459 best.cDepthBits = -1;
460 best.cStencilBits = -1;
461 best.cAuxBuffers = -1;
463 for (i = 1; i <= count; i++)
465 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
467 if (ppfd->iPixelType != format.iPixelType)
469 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
470 continue;
473 /* only use bitmap capable for formats for bitmap rendering */
474 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
476 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
477 continue;
480 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
481 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
482 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
483 * formats without the given flag set.
484 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
485 * has indicated that a format without stereo is returned when stereo is unavailable.
486 * So in case PFD_STEREO is set, formats that support it should have priority above formats
487 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
489 * To summarize the following is most likely the correct behavior:
490 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
491 * stereo set -> prefer stereo formats, but also accept non-stereo formats
492 * stereo don't care -> it doesn't matter whether we get stereo or not
494 * In Wine we will treat non-stereo the same way as don't care because it makes
495 * format selection even more complicated and second drivers with Stereo advertise
496 * each format twice anyway.
499 /* Doublebuffer, see the comments above */
500 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
502 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
503 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
504 goto found;
506 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
508 else if (!best_format)
509 goto found;
511 /* Stereo, see the comments above. */
512 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
514 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
515 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
516 goto found;
518 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
520 else if (!best_format)
521 goto found;
523 /* Below we will do a number of checks to select the 'best' pixelformat.
524 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
525 * The code works by trying to match the most important options as close as possible.
526 * When a reasonable format is found, we will try to match more options.
527 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
528 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
529 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
531 if (ppfd->cColorBits)
533 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
534 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
535 goto found;
537 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
539 TRACE( "color mismatch for iPixelFormat=%d\n", i );
540 continue;
543 if (ppfd->cAlphaBits)
545 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
546 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
547 goto found;
549 if (best.cAlphaBits != format.cAlphaBits)
551 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
552 continue;
555 if (ppfd->cDepthBits)
557 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
558 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
559 goto found;
561 if (best.cDepthBits != format.cDepthBits)
563 TRACE( "depth 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->cAuxBuffers)
581 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
582 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
583 goto found;
585 if (best.cAuxBuffers != format.cAuxBuffers)
587 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
588 continue;
591 continue;
593 found:
594 best_format = i;
595 best = format;
596 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
597 bestStereo = format.dwFlags & PFD_STEREO;
600 TRACE( "returning %u\n", best_format );
601 return best_format;
604 /***********************************************************************
605 * wglGetPixelFormat (OPENGL32.@)
607 INT WINAPI wglGetPixelFormat(HDC hdc)
609 struct opengl_funcs *funcs = get_dc_funcs( hdc );
610 if (!funcs)
612 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
613 return 0;
615 return funcs->wgl.p_wglGetPixelFormat( hdc );
618 /***********************************************************************
619 * wglSetPixelFormat(OPENGL32.@)
621 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
623 struct opengl_funcs *funcs = get_dc_funcs( hdc );
624 if (!funcs) return FALSE;
625 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
628 /***********************************************************************
629 * wglSwapBuffers (OPENGL32.@)
631 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
633 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
635 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
636 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
638 if (TRACE_ON(fps))
640 static long prev_time, start_time;
641 static unsigned long frames, frames_total;
643 DWORD time = GetTickCount();
644 frames++;
645 frames_total++;
646 /* every 1.5 seconds */
647 if (time - prev_time > 1500)
649 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
650 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
651 prev_time = time;
652 frames = 0;
653 if (start_time == 0) start_time = time;
656 return TRUE;
659 /***********************************************************************
660 * wglCreateLayerContext (OPENGL32.@)
662 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
663 int iLayerPlane) {
664 TRACE("(%p,%d)\n", hdc, iLayerPlane);
666 if (iLayerPlane == 0) {
667 return wglCreateContext(hdc);
669 FIXME("no handler for layer %d\n", iLayerPlane);
671 return NULL;
674 /***********************************************************************
675 * wglDescribeLayerPlane (OPENGL32.@)
677 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
678 int iPixelFormat,
679 int iLayerPlane,
680 UINT nBytes,
681 LPLAYERPLANEDESCRIPTOR plpd) {
682 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
684 return FALSE;
687 /***********************************************************************
688 * wglGetLayerPaletteEntries (OPENGL32.@)
690 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
691 int iLayerPlane,
692 int iStart,
693 int cEntries,
694 const COLORREF *pcr) {
695 FIXME("(): stub!\n");
697 return 0;
700 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
702 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
704 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
706 TRACE("(%d, %p)\n", pname, data);
707 if (pname == GL_NUM_EXTENSIONS)
709 struct wgl_handle *ptr = get_current_context_ptr();
711 if (ptr->u.context->disabled_exts ||
712 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
714 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
715 GLint count, disabled_count = 0;
717 funcs->gl.p_glGetIntegerv(pname, &count);
718 while (*disabled_exts++ != ~0u)
719 disabled_count++;
720 *data = count - disabled_count;
721 return;
724 funcs->gl.p_glGetIntegerv(pname, data);
727 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
729 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
731 TRACE("(%d, %d)\n", name, index);
732 if (!funcs->ext.p_glGetStringi)
734 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
736 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
739 if (name == GL_EXTENSIONS)
741 struct wgl_handle *ptr = get_current_context_ptr();
743 if (ptr->u.context->disabled_exts ||
744 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
746 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
747 unsigned int disabled_count = 0;
749 while (index + disabled_count >= *disabled_exts++)
750 disabled_count++;
751 return funcs->ext.p_glGetStringi(name, index + disabled_count);
754 return funcs->ext.p_glGetStringi(name, index);
757 /* check if the extension is present in the list */
758 static BOOL has_extension( const char *list, const char *ext, size_t len )
760 if (!list)
762 const char *gl_ext;
763 unsigned int i;
764 GLint extensions_count;
766 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
767 for (i = 0; i < extensions_count; ++i)
769 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
770 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
771 return TRUE;
773 return FALSE;
776 while (list)
778 while (*list == ' ') list++;
779 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
780 list = strchr( list, ' ' );
782 return FALSE;
785 static int compar(const void *elt_a, const void *elt_b) {
786 return strcmp(((const OpenGL_extension *) elt_a)->name,
787 ((const OpenGL_extension *) elt_b)->name);
790 /* Check if a GL extension is supported */
791 static BOOL is_extension_supported(const char* extension)
793 enum wgl_handle_type type = get_current_context_type();
794 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
795 const char *gl_ext_string = NULL;
796 size_t len;
798 TRACE("Checking for extension '%s'\n", extension);
800 if (type == HANDLE_CONTEXT)
802 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
803 if (!gl_ext_string)
805 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
806 return FALSE;
810 /* We use the GetProcAddress function from the display driver to retrieve function pointers
811 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
812 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
813 * require the function to return NULL when an extension isn't found. For this reason we check
814 * if the OpenGL extension required for the function we are looking up is supported. */
816 while ((len = strcspn(extension, " ")) != 0)
818 /* Check if the extension is part of the GL extension string to see if it is supported. */
819 if (has_extension(gl_ext_string, extension, len))
820 return TRUE;
822 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
823 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
824 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
825 * Check if we are searching for a core GL function */
826 if(strncmp(extension, "GL_VERSION_", 11) == 0)
828 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
829 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
831 if(!gl_version) {
832 ERR("No OpenGL version found!\n");
833 return FALSE;
836 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
837 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
838 if( (gl_version[0] > version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
839 return TRUE;
841 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]);
844 if (extension[len] == ' ') len++;
845 extension += len;
848 return FALSE;
851 /***********************************************************************
852 * wglGetProcAddress (OPENGL32.@)
854 PROC WINAPI wglGetProcAddress( LPCSTR name )
856 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
857 void **func_ptr;
858 OpenGL_extension ext;
859 const OpenGL_extension *ext_ret;
861 if (!name) return NULL;
863 /* Without an active context opengl32 doesn't know to what
864 * driver it has to dispatch wglGetProcAddress.
866 if (!get_current_context_ptr())
868 WARN("No active WGL context found\n");
869 return NULL;
872 ext.name = name;
873 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
874 if (!ext_ret)
876 WARN("Function %s unknown\n", name);
877 return NULL;
880 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
881 if (!*func_ptr)
883 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
885 if (!is_extension_supported(ext_ret->extension))
887 unsigned int i;
888 static const struct { const char *name, *alt; } alternatives[] =
890 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
891 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
894 for (i = 0; i < sizeof(alternatives)/sizeof(alternatives[0]); i++)
896 if (strcmp( name, alternatives[i].name )) continue;
897 WARN("Extension %s required for %s not supported, trying %s\n",
898 ext_ret->extension, name, alternatives[i].alt );
899 return wglGetProcAddress( alternatives[i].alt );
901 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
902 return NULL;
905 if (driver_func == NULL)
907 WARN("Function %s not supported by driver\n", name);
908 return NULL;
910 *func_ptr = driver_func;
913 TRACE("returning %s -> %p\n", name, ext_ret->func);
914 return ext_ret->func;
917 /***********************************************************************
918 * wglRealizeLayerPalette (OPENGL32.@)
920 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
921 int iLayerPlane,
922 BOOL bRealize) {
923 FIXME("()\n");
925 return FALSE;
928 /***********************************************************************
929 * wglSetLayerPaletteEntries (OPENGL32.@)
931 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
932 int iLayerPlane,
933 int iStart,
934 int cEntries,
935 const COLORREF *pcr) {
936 FIXME("(): stub!\n");
938 return 0;
941 /***********************************************************************
942 * wglSwapLayerBuffers (OPENGL32.@)
944 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
945 UINT fuPlanes) {
946 TRACE("(%p, %08x)\n", hdc, fuPlanes);
948 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
949 if (!wglSwapBuffers( hdc )) return FALSE;
950 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
953 if (fuPlanes) {
954 WARN("Following layers unhandled: %08x\n", fuPlanes);
957 return TRUE;
960 /***********************************************************************
961 * wglBindTexImageARB
963 * Provided by the WGL_ARB_render_texture extension.
965 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
967 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
968 BOOL ret;
970 if (!ptr) return FALSE;
971 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
972 release_handle_ptr( ptr );
973 return ret;
976 /***********************************************************************
977 * wglReleaseTexImageARB
979 * Provided by the WGL_ARB_render_texture extension.
981 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
983 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
984 BOOL ret;
986 if (!ptr) return FALSE;
987 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
988 release_handle_ptr( ptr );
989 return ret;
992 /***********************************************************************
993 * wglSetPbufferAttribARB
995 * Provided by the WGL_ARB_render_texture extension.
997 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
999 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1000 BOOL ret;
1002 if (!ptr) return FALSE;
1003 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
1004 release_handle_ptr( ptr );
1005 return ret;
1008 /***********************************************************************
1009 * wglCreatePbufferARB
1011 * Provided by the WGL_ARB_pbuffer extension.
1013 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1015 HPBUFFERARB ret;
1016 struct wgl_pbuffer *pbuffer;
1017 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1019 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1020 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1021 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1022 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1023 return ret;
1026 /***********************************************************************
1027 * wglGetPbufferDCARB
1029 * Provided by the WGL_ARB_pbuffer extension.
1031 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1033 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1034 HDC ret;
1036 if (!ptr) return 0;
1037 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1038 release_handle_ptr( ptr );
1039 return ret;
1042 /***********************************************************************
1043 * wglReleasePbufferDCARB
1045 * Provided by the WGL_ARB_pbuffer extension.
1047 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1049 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1050 BOOL ret;
1052 if (!ptr) return FALSE;
1053 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1054 release_handle_ptr( ptr );
1055 return ret;
1058 /***********************************************************************
1059 * wglDestroyPbufferARB
1061 * Provided by the WGL_ARB_pbuffer extension.
1063 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1065 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1067 if (!ptr) return FALSE;
1068 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1069 free_handle_ptr( ptr );
1070 return TRUE;
1073 /***********************************************************************
1074 * wglQueryPbufferARB
1076 * Provided by the WGL_ARB_pbuffer extension.
1078 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1080 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1081 BOOL ret;
1083 if (!ptr) return FALSE;
1084 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1085 release_handle_ptr( ptr );
1086 return ret;
1089 /***********************************************************************
1090 * wglUseFontBitmaps_common
1092 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1094 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1095 GLYPHMETRICS gm;
1096 unsigned int glyph, size = 0;
1097 void *bitmap = NULL, *gl_bitmap = NULL;
1098 int org_alignment;
1099 BOOL ret = TRUE;
1101 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1102 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1104 for (glyph = first; glyph < first + count; glyph++) {
1105 unsigned int needed_size, height, width, width_int;
1107 if (unicode)
1108 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1109 else
1110 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1112 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1113 if (needed_size == GDI_ERROR) {
1114 ret = FALSE;
1115 break;
1118 if (needed_size > size) {
1119 size = needed_size;
1120 HeapFree(GetProcessHeap(), 0, bitmap);
1121 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1122 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1123 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1125 if (needed_size != 0) {
1126 if (unicode)
1127 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1128 size, bitmap, &identity) != GDI_ERROR);
1129 else
1130 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1131 size, bitmap, &identity) != GDI_ERROR);
1132 if (!ret) break;
1135 if (TRACE_ON(wgl)) {
1136 unsigned int bitmask;
1137 unsigned char *bitmap_ = bitmap;
1139 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1140 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1141 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1142 if (needed_size != 0) {
1143 TRACE(" - bitmap:\n");
1144 for (height = 0; height < gm.gmBlackBoxY; height++) {
1145 TRACE(" ");
1146 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1147 if (bitmask == 0) {
1148 bitmap_ += 1;
1149 bitmask = 0x80;
1151 if (*bitmap_ & bitmask)
1152 TRACE("*");
1153 else
1154 TRACE(" ");
1156 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1157 TRACE("\n");
1162 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1163 * glyph for it to be drawn properly.
1165 if (needed_size != 0) {
1166 width_int = (gm.gmBlackBoxX + 31) / 32;
1167 for (height = 0; height < gm.gmBlackBoxY; height++) {
1168 for (width = 0; width < width_int; width++) {
1169 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1170 ((int *) bitmap)[height * width_int + width];
1175 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1176 if (needed_size != 0) {
1177 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1178 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1179 gm.gmCellIncX, gm.gmCellIncY,
1180 gl_bitmap);
1181 } else {
1182 /* This is the case of 'empty' glyphs like the space character */
1183 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1185 funcs->gl.p_glEndList();
1188 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1189 HeapFree(GetProcessHeap(), 0, bitmap);
1190 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1191 return ret;
1194 /***********************************************************************
1195 * wglUseFontBitmapsA (OPENGL32.@)
1197 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1199 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1202 /***********************************************************************
1203 * wglUseFontBitmapsW (OPENGL32.@)
1205 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1207 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1210 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1212 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1213 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1214 vertex[2] = 0.0;
1217 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1219 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1220 GLdouble *dbl = vertex;
1221 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1222 funcs->gl.p_glVertex3dv(vertex);
1225 static void WINAPI tess_callback_begin(GLenum which)
1227 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1228 TRACE("%d\n", which);
1229 funcs->gl.p_glBegin(which);
1232 static void WINAPI tess_callback_end(void)
1234 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1235 TRACE("\n");
1236 funcs->gl.p_glEnd();
1239 typedef struct _bezier_vector {
1240 GLdouble x;
1241 GLdouble y;
1242 } bezier_vector;
1244 static double bezier_deviation_squared(const bezier_vector *p)
1246 bezier_vector deviation;
1247 bezier_vector vertex;
1248 bezier_vector base;
1249 double base_length;
1250 double dot;
1252 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1253 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1255 base.x = p[2].x - p[0].x;
1256 base.y = p[2].y - p[0].y;
1258 base_length = sqrt(base.x*base.x + base.y*base.y);
1259 base.x /= base_length;
1260 base.y /= base_length;
1262 dot = base.x*vertex.x + base.y*vertex.y;
1263 dot = min(max(dot, 0.0), base_length);
1264 base.x *= dot;
1265 base.y *= dot;
1267 deviation.x = vertex.x-base.x;
1268 deviation.y = vertex.y-base.y;
1270 return deviation.x*deviation.x + deviation.y*deviation.y;
1273 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1275 bezier_vector first_curve[3];
1276 bezier_vector second_curve[3];
1277 bezier_vector vertex;
1278 int total_vertices;
1280 if(bezier_deviation_squared(p) <= deviation*deviation)
1282 if(points)
1283 *points = p[2];
1284 return 1;
1287 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1288 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1290 first_curve[0] = p[0];
1291 first_curve[1].x = (p[0].x + p[1].x)/2;
1292 first_curve[1].y = (p[0].y + p[1].y)/2;
1293 first_curve[2] = vertex;
1295 second_curve[0] = vertex;
1296 second_curve[1].x = (p[2].x + p[1].x)/2;
1297 second_curve[1].y = (p[2].y + p[1].y)/2;
1298 second_curve[2] = p[2];
1300 total_vertices = bezier_approximate(first_curve, points, deviation);
1301 if(points)
1302 points += total_vertices;
1303 total_vertices += bezier_approximate(second_curve, points, deviation);
1304 return total_vertices;
1307 /***********************************************************************
1308 * wglUseFontOutlines_common
1310 static BOOL wglUseFontOutlines_common(HDC hdc,
1311 DWORD first,
1312 DWORD count,
1313 DWORD listBase,
1314 FLOAT deviation,
1315 FLOAT extrusion,
1316 int format,
1317 LPGLYPHMETRICSFLOAT lpgmf,
1318 BOOL unicode)
1320 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1321 UINT glyph;
1322 GLUtesselator *tess = NULL;
1323 LOGFONTW lf;
1324 HFONT old_font, unscaled_font;
1325 UINT em_size = 1024;
1326 RECT rc;
1328 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1329 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1331 if(deviation <= 0.0)
1332 deviation = 1.0/em_size;
1334 if(format == WGL_FONT_POLYGONS)
1336 tess = gluNewTess();
1337 if(!tess)
1339 ERR("glu32 is required for this function but isn't available\n");
1340 return FALSE;
1342 gluTessCallback(tess, GLU_TESS_VERTEX, (void *)tess_callback_vertex);
1343 gluTessCallback(tess, GLU_TESS_BEGIN, (void *)tess_callback_begin);
1344 gluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1347 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1348 rc.left = rc.right = rc.bottom = 0;
1349 rc.top = em_size;
1350 DPtoLP(hdc, (POINT*)&rc, 2);
1351 lf.lfHeight = -abs(rc.top - rc.bottom);
1352 lf.lfOrientation = lf.lfEscapement = 0;
1353 unscaled_font = CreateFontIndirectW(&lf);
1354 old_font = SelectObject(hdc, unscaled_font);
1356 for (glyph = first; glyph < first + count; glyph++)
1358 DWORD needed;
1359 GLYPHMETRICS gm;
1360 BYTE *buf;
1361 TTPOLYGONHEADER *pph;
1362 TTPOLYCURVE *ppc;
1363 GLdouble *vertices = NULL;
1364 int vertex_total = -1;
1366 if(unicode)
1367 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1368 else
1369 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1371 if(needed == GDI_ERROR)
1372 goto error;
1374 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1376 if(unicode)
1377 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1378 else
1379 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1381 TRACE("glyph %d\n", glyph);
1383 if(lpgmf)
1385 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1386 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1387 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1388 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1389 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1390 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1392 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1393 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1394 lpgmf++;
1397 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1398 funcs->gl.p_glFrontFace(GL_CCW);
1399 if(format == WGL_FONT_POLYGONS)
1401 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1402 gluTessNormal(tess, 0, 0, 1);
1403 gluTessBeginPolygon(tess, NULL);
1406 while(!vertices)
1408 if(vertex_total != -1)
1409 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1410 vertex_total = 0;
1412 pph = (TTPOLYGONHEADER*)buf;
1413 while((BYTE*)pph < buf + needed)
1415 GLdouble previous[3];
1416 fixed_to_double(pph->pfxStart, em_size, previous);
1418 if(vertices)
1419 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1421 if(format == WGL_FONT_POLYGONS)
1422 gluTessBeginContour(tess);
1423 else
1424 funcs->gl.p_glBegin(GL_LINE_LOOP);
1426 if(vertices)
1428 fixed_to_double(pph->pfxStart, em_size, vertices);
1429 if(format == WGL_FONT_POLYGONS)
1430 gluTessVertex(tess, vertices, vertices);
1431 else
1432 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1433 vertices += 3;
1435 vertex_total++;
1437 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1438 while((char*)ppc < (char*)pph + pph->cb)
1440 int i, j;
1441 int num;
1443 switch(ppc->wType) {
1444 case TT_PRIM_LINE:
1445 for(i = 0; i < ppc->cpfx; i++)
1447 if(vertices)
1449 TRACE("\t\tline to %d, %d\n",
1450 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1451 fixed_to_double(ppc->apfx[i], em_size, vertices);
1452 if(format == WGL_FONT_POLYGONS)
1453 gluTessVertex(tess, vertices, vertices);
1454 else
1455 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1456 vertices += 3;
1458 fixed_to_double(ppc->apfx[i], em_size, previous);
1459 vertex_total++;
1461 break;
1463 case TT_PRIM_QSPLINE:
1464 for(i = 0; i < ppc->cpfx-1; i++)
1466 bezier_vector curve[3];
1467 bezier_vector *points;
1468 GLdouble curve_vertex[3];
1470 if(vertices)
1471 TRACE("\t\tcurve %d,%d %d,%d\n",
1472 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1473 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1475 curve[0].x = previous[0];
1476 curve[0].y = previous[1];
1477 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1478 curve[1].x = curve_vertex[0];
1479 curve[1].y = curve_vertex[1];
1480 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1481 curve[2].x = curve_vertex[0];
1482 curve[2].y = curve_vertex[1];
1483 if(i < ppc->cpfx-2)
1485 curve[2].x = (curve[1].x + curve[2].x)/2;
1486 curve[2].y = (curve[1].y + curve[2].y)/2;
1488 num = bezier_approximate(curve, NULL, deviation);
1489 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1490 num = bezier_approximate(curve, points, deviation);
1491 vertex_total += num;
1492 if(vertices)
1494 for(j=0; j<num; j++)
1496 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1497 vertices[0] = points[j].x;
1498 vertices[1] = points[j].y;
1499 vertices[2] = 0.0;
1500 if(format == WGL_FONT_POLYGONS)
1501 gluTessVertex(tess, vertices, vertices);
1502 else
1503 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1504 vertices += 3;
1507 HeapFree(GetProcessHeap(), 0, points);
1508 previous[0] = curve[2].x;
1509 previous[1] = curve[2].y;
1511 break;
1512 default:
1513 ERR("\t\tcurve type = %d\n", ppc->wType);
1514 if(format == WGL_FONT_POLYGONS)
1515 gluTessEndContour(tess);
1516 else
1517 funcs->gl.p_glEnd();
1518 goto error_in_list;
1521 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1522 (ppc->cpfx - 1) * sizeof(POINTFX));
1524 if(format == WGL_FONT_POLYGONS)
1525 gluTessEndContour(tess);
1526 else
1527 funcs->gl.p_glEnd();
1528 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1532 error_in_list:
1533 if(format == WGL_FONT_POLYGONS)
1534 gluTessEndPolygon(tess);
1535 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1536 funcs->gl.p_glEndList();
1537 HeapFree(GetProcessHeap(), 0, buf);
1538 HeapFree(GetProcessHeap(), 0, vertices);
1541 error:
1542 DeleteObject(SelectObject(hdc, old_font));
1543 if(format == WGL_FONT_POLYGONS)
1544 gluDeleteTess(tess);
1545 return TRUE;
1549 /***********************************************************************
1550 * wglUseFontOutlinesA (OPENGL32.@)
1552 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1553 DWORD first,
1554 DWORD count,
1555 DWORD listBase,
1556 FLOAT deviation,
1557 FLOAT extrusion,
1558 int format,
1559 LPGLYPHMETRICSFLOAT lpgmf)
1561 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1564 /***********************************************************************
1565 * wglUseFontOutlinesW (OPENGL32.@)
1567 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1568 DWORD first,
1569 DWORD count,
1570 DWORD listBase,
1571 FLOAT deviation,
1572 FLOAT extrusion,
1573 int format,
1574 LPGLYPHMETRICSFLOAT lpgmf)
1576 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1579 /***********************************************************************
1580 * glDebugEntry (OPENGL32.@)
1582 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1584 return 0;
1587 static GLubyte *filter_extensions_list(const char *extensions, const char *disabled)
1589 char *p, *str;
1590 const char *end;
1592 p = str = HeapAlloc(GetProcessHeap(), 0, strlen(extensions) + 2);
1593 if (!str)
1594 return NULL;
1596 TRACE( "GL_EXTENSIONS:\n" );
1598 for (;;)
1600 while (*extensions == ' ')
1601 extensions++;
1602 if (!*extensions)
1603 break;
1604 if (!(end = strchr(extensions, ' ')))
1605 end = extensions + strlen(extensions);
1606 memcpy(p, extensions, end - extensions);
1607 p[end - extensions] = 0;
1608 if (!has_extension(disabled, p, strlen(p)))
1610 TRACE("++ %s\n", p);
1611 p += end - extensions;
1612 *p++ = ' ';
1614 else
1616 TRACE("-- %s (disabled by config)\n", p);
1618 extensions = end;
1620 *p = 0;
1621 return (GLubyte *)str;
1624 static GLuint *filter_extensions_index(const char *disabled)
1626 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1627 const char *ext, *end, *gl_ext;
1628 GLuint *disabled_exts, *new_disabled_exts;
1629 unsigned int i = 0, j, disabled_size;
1630 GLint extensions_count;
1632 if (!funcs->ext.p_glGetStringi)
1634 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
1636 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
1637 if (!funcs->ext.p_glGetStringi)
1638 return NULL;
1641 funcs->gl.p_glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
1642 disabled_size = 2;
1643 disabled_exts = HeapAlloc(GetProcessHeap(), 0, disabled_size * sizeof(*disabled_exts));
1644 if (!disabled_exts)
1645 return NULL;
1647 TRACE( "GL_EXTENSIONS:\n" );
1649 for (j = 0; j < extensions_count; ++j)
1651 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, j);
1652 ext = disabled;
1653 for (;;)
1655 while (*ext == ' ')
1656 ext++;
1657 if (!*ext)
1659 TRACE("++ %s\n", gl_ext);
1660 break;
1662 if (!(end = strchr(ext, ' ')))
1663 end = ext + strlen(ext);
1665 if (!strncmp(gl_ext, ext, end - ext) && !gl_ext[end - ext])
1667 if (i + 1 == disabled_size)
1669 disabled_size *= 2;
1670 new_disabled_exts = HeapReAlloc(GetProcessHeap(), 0, disabled_exts,
1671 disabled_size * sizeof(*disabled_exts));
1672 if (!new_disabled_exts)
1674 disabled_exts[i] = ~0u;
1675 return disabled_exts;
1677 disabled_exts = new_disabled_exts;
1679 TRACE("-- %s (disabled by config)\n", gl_ext);
1680 disabled_exts[i++] = j;
1681 break;
1683 ext = end;
1686 disabled_exts[i] = ~0u;
1687 return disabled_exts;
1690 /* build the extension string by filtering out the disabled extensions */
1691 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts)
1693 static const char *disabled;
1695 if (!disabled)
1697 HKEY hkey;
1698 DWORD size;
1699 char *str = NULL;
1701 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1702 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1704 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1706 str = HeapAlloc( GetProcessHeap(), 0, size );
1707 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1709 RegCloseKey( hkey );
1711 if (str)
1713 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1714 HeapFree( GetProcessHeap(), 0, str );
1716 else disabled = "";
1719 if (!disabled[0])
1720 return FALSE;
1722 if (extensions && !*exts_list)
1723 *exts_list = filter_extensions_list(extensions, disabled);
1725 if (!*disabled_exts)
1726 *disabled_exts = filter_extensions_index(disabled);
1728 return (exts_list && *exts_list) || *disabled_exts;
1731 /***********************************************************************
1732 * glGetString (OPENGL32.@)
1734 const GLubyte * WINAPI glGetString( GLenum name )
1736 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1737 const GLubyte *ret = funcs->gl.p_glGetString( name );
1739 if (name == GL_EXTENSIONS && ret)
1741 struct wgl_handle *ptr = get_current_context_ptr();
1742 if (ptr->u.context->extensions ||
1743 filter_extensions((const char *)ret, &ptr->u.context->extensions, &ptr->u.context->disabled_exts))
1744 ret = ptr->u.context->extensions;
1746 return ret;
1749 /***********************************************************************
1750 * OpenGL initialisation routine
1752 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1754 switch(reason)
1756 case DLL_PROCESS_ATTACH:
1757 NtCurrentTeb()->glTable = &null_opengl_funcs;
1758 break;
1759 case DLL_THREAD_ATTACH:
1760 NtCurrentTeb()->glTable = &null_opengl_funcs;
1761 break;
1763 return TRUE;