opengl32: Fix get_current_context_type() to not shift the type bits down.
[wine.git] / dlls / opengl32 / wgl.c
blob6ff54f0dfb0d3a5f8e5a7a69d2b2b009304b4db2
1 /* Window-specific OpenGL functions implementation.
3 * Copyright (c) 1999 Lionel Ulmer
4 * Copyright (c) 2005 Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "opengl_ext.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "wingdi.h"
34 #include "winternl.h"
35 #include "winnt.h"
37 #define WGL_WGLEXT_PROTOTYPES
38 #include "wine/wglext.h"
39 #include "wine/gdi_driver.h"
40 #include "wine/wgl_driver.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
44 WINE_DECLARE_DEBUG_CHANNEL(fps);
46 extern struct opengl_funcs null_opengl_funcs;
48 /* handle management */
50 #define MAX_WGL_HANDLES 1024
52 enum wgl_handle_type
54 HANDLE_PBUFFER = 0 << 12,
55 HANDLE_CONTEXT = 1 << 12,
56 HANDLE_CONTEXT_V3 = 3 << 12,
57 HANDLE_TYPE_MASK = 15 << 12
60 struct opengl_context
62 DWORD tid; /* thread that the context is current in */
63 HDC draw_dc; /* current drawing DC */
64 HDC read_dc; /* current reading DC */
65 GLubyte *extensions; /* extension string */
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 inline struct opengl_funcs *get_dc_funcs( HDC hdc )
96 struct opengl_funcs *funcs = __wine_get_wgl_driver( hdc, WINE_WGL_DRIVER_VERSION );
97 if (funcs == (void *)-1) funcs = &null_opengl_funcs;
98 return funcs;
101 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
103 WORD generation = HIWORD( ptr->handle ) + 1;
104 if (!generation) generation++;
105 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
106 return ULongToHandle( ptr->handle );
109 /* the current context is assumed valid and doesn't need locking */
110 static inline struct wgl_handle *get_current_context_ptr(void)
112 if (!NtCurrentTeb()->glCurrentRC) return NULL;
113 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
116 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
118 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
120 EnterCriticalSection( &wgl_section );
121 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
122 return &wgl_handles[index];
124 LeaveCriticalSection( &wgl_section );
125 SetLastError( ERROR_INVALID_HANDLE );
126 return NULL;
129 static void release_handle_ptr( struct wgl_handle *ptr )
131 if (ptr) LeaveCriticalSection( &wgl_section );
134 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
136 HANDLE handle = 0;
137 struct wgl_handle *ptr = NULL;
139 EnterCriticalSection( &wgl_section );
140 if ((ptr = next_free))
141 next_free = next_free->u.next;
142 else if (handle_count < MAX_WGL_HANDLES)
143 ptr = &wgl_handles[handle_count++];
145 if (ptr)
147 ptr->funcs = funcs;
148 ptr->u.context = user_ptr;
149 handle = next_handle( ptr, type );
151 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
152 LeaveCriticalSection( &wgl_section );
153 return handle;
156 static void free_handle_ptr( struct wgl_handle *ptr )
158 ptr->handle |= 0xffff;
159 ptr->u.next = next_free;
160 ptr->funcs = NULL;
161 next_free = ptr;
162 LeaveCriticalSection( &wgl_section );
165 static inline enum wgl_handle_type get_current_context_type(void)
167 if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
168 return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
171 /***********************************************************************
172 * wglCopyContext (OPENGL32.@)
174 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
176 struct wgl_handle *src, *dst;
177 BOOL ret = FALSE;
179 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
180 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
182 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
183 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
184 dst->u.context->drv_ctx, mask );
186 release_handle_ptr( dst );
187 release_handle_ptr( src );
188 return ret;
191 /***********************************************************************
192 * wglDeleteContext (OPENGL32.@)
194 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
196 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
198 if (!ptr) return FALSE;
200 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
202 SetLastError( ERROR_BUSY );
203 release_handle_ptr( ptr );
204 return FALSE;
206 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
207 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
208 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
209 HeapFree( GetProcessHeap(), 0, ptr->u.context );
210 free_handle_ptr( ptr );
211 return TRUE;
214 /***********************************************************************
215 * wglMakeCurrent (OPENGL32.@)
217 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
219 BOOL ret = TRUE;
220 struct wgl_handle *ptr, *prev = get_current_context_ptr();
222 if (hglrc)
224 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
225 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
227 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
228 if (ret)
230 if (prev) prev->u.context->tid = 0;
231 ptr->u.context->tid = GetCurrentThreadId();
232 ptr->u.context->draw_dc = hdc;
233 ptr->u.context->read_dc = hdc;
234 NtCurrentTeb()->glCurrentRC = hglrc;
235 NtCurrentTeb()->glTable = ptr->funcs;
238 else
240 SetLastError( ERROR_BUSY );
241 ret = FALSE;
243 release_handle_ptr( ptr );
245 else if (prev)
247 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
248 prev->u.context->tid = 0;
249 NtCurrentTeb()->glCurrentRC = 0;
250 NtCurrentTeb()->glTable = &null_opengl_funcs;
252 else if (!hdc)
254 SetLastError( ERROR_INVALID_HANDLE );
255 ret = FALSE;
257 return ret;
260 /***********************************************************************
261 * wglCreateContextAttribsARB
263 * Provided by the WGL_ARB_create_context extension.
265 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
267 HGLRC ret = 0;
268 struct wgl_context *drv_ctx;
269 struct wgl_handle *share_ptr = NULL;
270 struct opengl_context *context;
271 struct opengl_funcs *funcs = get_dc_funcs( hdc );
273 if (!funcs || !funcs->ext.p_wglCreateContextAttribsARB) return 0;
274 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT ))) return 0;
275 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
276 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
278 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
280 enum wgl_handle_type type = HANDLE_CONTEXT;
282 if (attribs)
284 while (*attribs)
286 if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
288 if (attribs[1] >= 3)
289 type = HANDLE_CONTEXT_V3;
290 break;
292 attribs += 2;
296 context->drv_ctx = drv_ctx;
297 if (!(ret = alloc_handle( type, funcs, context )))
298 HeapFree( GetProcessHeap(), 0, context );
300 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
302 release_handle_ptr( share_ptr );
303 return ret;
307 /***********************************************************************
308 * wglMakeContextCurrentARB
310 * Provided by the WGL_ARB_make_current_read extension.
312 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
314 BOOL ret = TRUE;
315 struct wgl_handle *ptr, *prev = get_current_context_ptr();
317 if (hglrc)
319 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
320 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
322 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
323 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
324 ptr->u.context->drv_ctx ));
325 if (ret)
327 if (prev) prev->u.context->tid = 0;
328 ptr->u.context->tid = GetCurrentThreadId();
329 ptr->u.context->draw_dc = draw_hdc;
330 ptr->u.context->read_dc = read_hdc;
331 NtCurrentTeb()->glCurrentRC = hglrc;
332 NtCurrentTeb()->glTable = ptr->funcs;
335 else
337 SetLastError( ERROR_BUSY );
338 ret = FALSE;
340 release_handle_ptr( ptr );
342 else if (prev)
344 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
345 prev->u.context->tid = 0;
346 NtCurrentTeb()->glCurrentRC = 0;
347 NtCurrentTeb()->glTable = &null_opengl_funcs;
349 return ret;
352 /***********************************************************************
353 * wglGetCurrentReadDCARB
355 * Provided by the WGL_ARB_make_current_read extension.
357 HDC WINAPI wglGetCurrentReadDCARB(void)
359 struct wgl_handle *ptr = get_current_context_ptr();
361 if (!ptr) return 0;
362 return ptr->u.context->read_dc;
365 /***********************************************************************
366 * wglShareLists (OPENGL32.@)
368 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
370 BOOL ret = FALSE;
371 struct wgl_handle *src, *dst;
373 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
374 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
376 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
377 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
379 release_handle_ptr( dst );
380 release_handle_ptr( src );
381 return ret;
384 /***********************************************************************
385 * wglGetCurrentDC (OPENGL32.@)
387 HDC WINAPI wglGetCurrentDC(void)
389 struct wgl_handle *ptr = get_current_context_ptr();
391 if (!ptr) return 0;
392 return ptr->u.context->draw_dc;
395 /***********************************************************************
396 * wglCreateContext (OPENGL32.@)
398 HGLRC WINAPI wglCreateContext(HDC hdc)
400 HGLRC ret = 0;
401 struct wgl_context *drv_ctx;
402 struct opengl_context *context;
403 struct opengl_funcs *funcs = get_dc_funcs( hdc );
405 if (!funcs) return 0;
406 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
407 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
409 context->drv_ctx = drv_ctx;
410 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
411 HeapFree( GetProcessHeap(), 0, context );
413 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
414 return ret;
417 /***********************************************************************
418 * wglGetCurrentContext (OPENGL32.@)
420 HGLRC WINAPI wglGetCurrentContext(void)
422 return NtCurrentTeb()->glCurrentRC;
425 /***********************************************************************
426 * wglDescribePixelFormat (OPENGL32.@)
428 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
430 struct opengl_funcs *funcs = get_dc_funcs( hdc );
431 if (!funcs) return 0;
432 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
435 /***********************************************************************
436 * wglChoosePixelFormat (OPENGL32.@)
438 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
440 PIXELFORMATDESCRIPTOR format, best;
441 int i, count, best_format;
442 int bestDBuffer = -1, bestStereo = -1;
444 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
445 "accum %u depth %u stencil %u aux %u\n",
446 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
447 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
448 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
450 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
451 if (!count) return 0;
453 best_format = 0;
454 best.dwFlags = 0;
455 best.cAlphaBits = -1;
456 best.cColorBits = -1;
457 best.cDepthBits = -1;
458 best.cStencilBits = -1;
459 best.cAuxBuffers = -1;
461 for (i = 1; i <= count; i++)
463 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
465 if (ppfd->iPixelType != format.iPixelType)
467 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
468 continue;
471 /* only use bitmap capable for formats for bitmap rendering */
472 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
474 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
475 continue;
478 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
479 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
480 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
481 * formats without the given flag set.
482 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
483 * has indicated that a format without stereo is returned when stereo is unavailable.
484 * So in case PFD_STEREO is set, formats that support it should have priority above formats
485 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
487 * To summarize the following is most likely the correct behavior:
488 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
489 * stereo set -> prefer stereo formats, but also accept non-stereo formats
490 * stereo don't care -> it doesn't matter whether we get stereo or not
492 * In Wine we will treat non-stereo the same way as don't care because it makes
493 * format selection even more complicated and second drivers with Stereo advertise
494 * each format twice anyway.
497 /* Doublebuffer, see the comments above */
498 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
500 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
501 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
502 goto found;
504 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
507 /* Stereo, see the comments above. */
508 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
510 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
511 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
512 goto found;
514 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
517 /* Below we will do a number of checks to select the 'best' pixelformat.
518 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
519 * The code works by trying to match the most important options as close as possible.
520 * When a reasonable format is found, we will try to match more options.
521 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
522 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
523 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
525 if (ppfd->cColorBits)
527 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
528 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
529 goto found;
531 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
533 TRACE( "color mismatch for iPixelFormat=%d\n", i );
534 continue;
537 if (ppfd->cAlphaBits)
539 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
540 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
541 goto found;
543 if (best.cAlphaBits != format.cAlphaBits)
545 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
546 continue;
549 if (ppfd->cDepthBits)
551 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
552 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
553 goto found;
555 if (best.cDepthBits != format.cDepthBits)
557 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
558 continue;
561 if (ppfd->cStencilBits)
563 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
564 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
565 goto found;
567 if (best.cStencilBits != format.cStencilBits)
569 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
570 continue;
573 if (ppfd->cAuxBuffers)
575 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
576 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
577 goto found;
579 if (best.cAuxBuffers != format.cAuxBuffers)
581 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
582 continue;
585 continue;
587 found:
588 best_format = i;
589 best = format;
590 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
591 bestStereo = format.dwFlags & PFD_STEREO;
594 TRACE( "returning %u\n", best_format );
595 return best_format;
598 /***********************************************************************
599 * wglGetPixelFormat (OPENGL32.@)
601 INT WINAPI wglGetPixelFormat(HDC hdc)
603 struct opengl_funcs *funcs = get_dc_funcs( hdc );
604 if (!funcs) return 0;
605 return funcs->wgl.p_wglGetPixelFormat( hdc );
608 /***********************************************************************
609 * wglSetPixelFormat(OPENGL32.@)
611 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
613 struct opengl_funcs *funcs = get_dc_funcs( hdc );
614 if (!funcs) return FALSE;
615 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
618 /***********************************************************************
619 * wglSwapBuffers (OPENGL32.@)
621 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
623 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
625 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
626 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
628 if (TRACE_ON(fps))
630 static long prev_time, start_time;
631 static unsigned long frames, frames_total;
633 DWORD time = GetTickCount();
634 frames++;
635 frames_total++;
636 /* every 1.5 seconds */
637 if (time - prev_time > 1500)
639 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
640 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
641 prev_time = time;
642 frames = 0;
643 if (start_time == 0) start_time = time;
646 return TRUE;
649 /***********************************************************************
650 * wglCreateLayerContext (OPENGL32.@)
652 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
653 int iLayerPlane) {
654 TRACE("(%p,%d)\n", hdc, iLayerPlane);
656 if (iLayerPlane == 0) {
657 return wglCreateContext(hdc);
659 FIXME("no handler for layer %d\n", iLayerPlane);
661 return NULL;
664 /***********************************************************************
665 * wglDescribeLayerPlane (OPENGL32.@)
667 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
668 int iPixelFormat,
669 int iLayerPlane,
670 UINT nBytes,
671 LPLAYERPLANEDESCRIPTOR plpd) {
672 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
674 return FALSE;
677 /***********************************************************************
678 * wglGetLayerPaletteEntries (OPENGL32.@)
680 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
681 int iLayerPlane,
682 int iStart,
683 int cEntries,
684 const COLORREF *pcr) {
685 FIXME("(): stub!\n");
687 return 0;
690 /* check if the extension is present in the list */
691 static BOOL has_extension( const char *list, const char *ext, size_t len )
693 if (!list)
695 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
696 const char *gl_ext;
697 unsigned int i;
698 GLint extensions_count;
700 if (!funcs->ext.p_glGetStringi)
702 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
704 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
707 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
708 for (i = 0; i < extensions_count; ++i)
710 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, i);
711 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
712 return TRUE;
714 return FALSE;
717 while (list)
719 while (*list == ' ') list++;
720 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
721 list = strchr( list, ' ' );
723 return FALSE;
726 static int compar(const void *elt_a, const void *elt_b) {
727 return strcmp(((const OpenGL_extension *) elt_a)->name,
728 ((const OpenGL_extension *) elt_b)->name);
731 /* Check if a GL extension is supported */
732 static BOOL is_extension_supported(const char* extension)
734 enum wgl_handle_type type = get_current_context_type();
735 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
736 const char *gl_ext_string = NULL;
737 size_t len;
739 TRACE("Checking for extension '%s'\n", extension);
741 if (type == HANDLE_CONTEXT)
743 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
744 if (!gl_ext_string)
746 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
747 return FALSE;
751 /* We use the GetProcAddress function from the display driver to retrieve function pointers
752 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
753 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
754 * require the function to return NULL when an extension isn't found. For this reason we check
755 * if the OpenGL extension required for the function we are looking up is supported. */
757 while ((len = strcspn(extension, " ")) != 0)
759 /* Check if the extension is part of the GL extension string to see if it is supported. */
760 if (has_extension(gl_ext_string, extension, len))
761 return TRUE;
763 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
764 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
765 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
766 * Check if we are searching for a core GL function */
767 if(strncmp(extension, "GL_VERSION_", 11) == 0)
769 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
770 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
772 if(!gl_version) {
773 ERR("No OpenGL version found!\n");
774 return FALSE;
777 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
778 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
779 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
780 return TRUE;
782 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]);
785 if (extension[len] == ' ') len++;
786 extension += len;
789 return FALSE;
792 /***********************************************************************
793 * wglGetProcAddress (OPENGL32.@)
795 PROC WINAPI wglGetProcAddress( LPCSTR name )
797 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
798 void **func_ptr;
799 OpenGL_extension ext;
800 const OpenGL_extension *ext_ret;
802 if (!name) return NULL;
804 /* Without an active context opengl32 doesn't know to what
805 * driver it has to dispatch wglGetProcAddress.
807 if (!get_current_context_ptr())
809 WARN("No active WGL context found\n");
810 return NULL;
813 ext.name = name;
814 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
815 if (!ext_ret)
817 WARN("Function %s unknown\n", name);
818 return NULL;
821 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
822 if (!*func_ptr)
824 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
826 if (!is_extension_supported(ext_ret->extension))
827 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
829 if (driver_func == NULL)
831 WARN("Function %s not supported by driver\n", name);
832 return NULL;
834 *func_ptr = driver_func;
837 TRACE("returning %s -> %p\n", name, ext_ret->func);
838 return ext_ret->func;
841 /***********************************************************************
842 * wglRealizeLayerPalette (OPENGL32.@)
844 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
845 int iLayerPlane,
846 BOOL bRealize) {
847 FIXME("()\n");
849 return FALSE;
852 /***********************************************************************
853 * wglSetLayerPaletteEntries (OPENGL32.@)
855 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
856 int iLayerPlane,
857 int iStart,
858 int cEntries,
859 const COLORREF *pcr) {
860 FIXME("(): stub!\n");
862 return 0;
865 /***********************************************************************
866 * wglSwapLayerBuffers (OPENGL32.@)
868 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
869 UINT fuPlanes) {
870 TRACE("(%p, %08x)\n", hdc, fuPlanes);
872 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
873 if (!wglSwapBuffers( hdc )) return FALSE;
874 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
877 if (fuPlanes) {
878 WARN("Following layers unhandled: %08x\n", fuPlanes);
881 return TRUE;
884 /***********************************************************************
885 * wglAllocateMemoryNV
887 * Provided by the WGL_NV_vertex_array_range extension.
889 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
891 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
893 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
894 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
897 /***********************************************************************
898 * wglFreeMemoryNV
900 * Provided by the WGL_NV_vertex_array_range extension.
902 void WINAPI wglFreeMemoryNV( void *pointer )
904 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
906 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
909 /***********************************************************************
910 * wglBindTexImageARB
912 * Provided by the WGL_ARB_render_texture extension.
914 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
916 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
917 BOOL ret;
919 if (!ptr) return FALSE;
920 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
921 release_handle_ptr( ptr );
922 return ret;
925 /***********************************************************************
926 * wglReleaseTexImageARB
928 * Provided by the WGL_ARB_render_texture extension.
930 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
932 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
933 BOOL ret;
935 if (!ptr) return FALSE;
936 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
937 release_handle_ptr( ptr );
938 return ret;
941 /***********************************************************************
942 * wglSetPbufferAttribARB
944 * Provided by the WGL_ARB_render_texture extension.
946 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
948 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
949 BOOL ret;
951 if (!ptr) return FALSE;
952 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
953 release_handle_ptr( ptr );
954 return ret;
957 /***********************************************************************
958 * wglChoosePixelFormatARB
960 * Provided by the WGL_ARB_pixel_format extension.
962 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
963 UINT max, int *formats, UINT *count )
965 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
967 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
968 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
971 /***********************************************************************
972 * wglGetPixelFormatAttribivARB
974 * Provided by the WGL_ARB_pixel_format extension.
976 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
977 int *values )
979 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
981 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
982 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
985 /***********************************************************************
986 * wglGetPixelFormatAttribfvARB
988 * Provided by the WGL_ARB_pixel_format extension.
990 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
991 FLOAT *values )
993 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
995 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
996 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
999 /***********************************************************************
1000 * wglCreatePbufferARB
1002 * Provided by the WGL_ARB_pbuffer extension.
1004 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1006 HPBUFFERARB ret = 0;
1007 struct wgl_pbuffer *pbuffer;
1008 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1010 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1011 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1012 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1013 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1014 return ret;
1017 /***********************************************************************
1018 * wglGetPbufferDCARB
1020 * Provided by the WGL_ARB_pbuffer extension.
1022 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1024 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1025 HDC ret;
1027 if (!ptr) return 0;
1028 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1029 release_handle_ptr( ptr );
1030 return ret;
1033 /***********************************************************************
1034 * wglReleasePbufferDCARB
1036 * Provided by the WGL_ARB_pbuffer extension.
1038 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1040 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1041 BOOL ret;
1043 if (!ptr) return FALSE;
1044 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1045 release_handle_ptr( ptr );
1046 return ret;
1049 /***********************************************************************
1050 * wglDestroyPbufferARB
1052 * Provided by the WGL_ARB_pbuffer extension.
1054 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1056 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1058 if (!ptr) return FALSE;
1059 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1060 free_handle_ptr( ptr );
1061 return TRUE;
1064 /***********************************************************************
1065 * wglQueryPbufferARB
1067 * Provided by the WGL_ARB_pbuffer extension.
1069 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1071 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1072 BOOL ret;
1074 if (!ptr) return FALSE;
1075 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1076 release_handle_ptr( ptr );
1077 return ret;
1080 /***********************************************************************
1081 * wglGetExtensionsStringARB
1083 * Provided by the WGL_ARB_extensions_string extension.
1085 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1087 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1089 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1090 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1093 /***********************************************************************
1094 * wglGetExtensionsStringEXT
1096 * Provided by the WGL_EXT_extensions_string extension.
1098 const char * WINAPI wglGetExtensionsStringEXT(void)
1100 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1102 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1103 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1106 /***********************************************************************
1107 * wglSwapIntervalEXT
1109 * Provided by the WGL_EXT_swap_control extension.
1111 BOOL WINAPI wglSwapIntervalEXT( int interval )
1113 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1115 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1116 return funcs->ext.p_wglSwapIntervalEXT( interval );
1119 /***********************************************************************
1120 * wglGetSwapIntervalEXT
1122 * Provided by the WGL_EXT_swap_control extension.
1124 int WINAPI wglGetSwapIntervalEXT(void)
1126 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1128 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1129 return funcs->ext.p_wglGetSwapIntervalEXT();
1132 /***********************************************************************
1133 * wglSetPixelFormatWINE
1135 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1137 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1139 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1141 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1142 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1145 /***********************************************************************
1146 * wglUseFontBitmaps_common
1148 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1150 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1151 GLYPHMETRICS gm;
1152 unsigned int glyph, size = 0;
1153 void *bitmap = NULL, *gl_bitmap = NULL;
1154 int org_alignment;
1155 BOOL ret = TRUE;
1157 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1158 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1160 for (glyph = first; glyph < first + count; glyph++) {
1161 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1162 unsigned int needed_size, height, width, width_int;
1164 if (unicode)
1165 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1166 else
1167 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1169 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1170 if (needed_size == GDI_ERROR) {
1171 ret = FALSE;
1172 break;
1175 if (needed_size > size) {
1176 size = needed_size;
1177 HeapFree(GetProcessHeap(), 0, bitmap);
1178 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1179 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1180 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1182 if (needed_size != 0) {
1183 if (unicode)
1184 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1185 size, bitmap, &identity) != GDI_ERROR);
1186 else
1187 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1188 size, bitmap, &identity) != GDI_ERROR);
1189 if (!ret) break;
1192 if (TRACE_ON(wgl)) {
1193 unsigned int bitmask;
1194 unsigned char *bitmap_ = bitmap;
1196 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1197 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1198 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1199 if (needed_size != 0) {
1200 TRACE(" - bitmap:\n");
1201 for (height = 0; height < gm.gmBlackBoxY; height++) {
1202 TRACE(" ");
1203 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1204 if (bitmask == 0) {
1205 bitmap_ += 1;
1206 bitmask = 0x80;
1208 if (*bitmap_ & bitmask)
1209 TRACE("*");
1210 else
1211 TRACE(" ");
1213 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1214 TRACE("\n");
1219 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1220 * glyph for it to be drawn properly.
1222 if (needed_size != 0) {
1223 width_int = (gm.gmBlackBoxX + 31) / 32;
1224 for (height = 0; height < gm.gmBlackBoxY; height++) {
1225 for (width = 0; width < width_int; width++) {
1226 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1227 ((int *) bitmap)[height * width_int + width];
1232 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1233 if (needed_size != 0) {
1234 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1235 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1236 gm.gmCellIncX, gm.gmCellIncY,
1237 gl_bitmap);
1238 } else {
1239 /* This is the case of 'empty' glyphs like the space character */
1240 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1242 funcs->gl.p_glEndList();
1245 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1246 HeapFree(GetProcessHeap(), 0, bitmap);
1247 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1248 return ret;
1251 /***********************************************************************
1252 * wglUseFontBitmapsA (OPENGL32.@)
1254 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1256 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1259 /***********************************************************************
1260 * wglUseFontBitmapsW (OPENGL32.@)
1262 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1264 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1267 /* FIXME: should probably have a glu.h header */
1269 typedef struct GLUtesselator GLUtesselator;
1270 typedef void (WINAPI *_GLUfuncptr)(void);
1272 #define GLU_TESS_BEGIN 100100
1273 #define GLU_TESS_VERTEX 100101
1274 #define GLU_TESS_END 100102
1276 static GLUtesselator * (WINAPI *pgluNewTess)(void);
1277 static void (WINAPI *pgluDeleteTess)(GLUtesselator *tess);
1278 static void (WINAPI *pgluTessNormal)(GLUtesselator *tess, GLdouble x, GLdouble y, GLdouble z);
1279 static void (WINAPI *pgluTessBeginPolygon)(GLUtesselator *tess, void *polygon_data);
1280 static void (WINAPI *pgluTessEndPolygon)(GLUtesselator *tess);
1281 static void (WINAPI *pgluTessCallback)(GLUtesselator *tess, GLenum which, _GLUfuncptr fn);
1282 static void (WINAPI *pgluTessBeginContour)(GLUtesselator *tess);
1283 static void (WINAPI *pgluTessEndContour)(GLUtesselator *tess);
1284 static void (WINAPI *pgluTessVertex)(GLUtesselator *tess, GLdouble *location, GLvoid* data);
1286 static HMODULE load_libglu(void)
1288 static const WCHAR glu32W[] = {'g','l','u','3','2','.','d','l','l',0};
1289 static BOOL already_loaded;
1290 static HMODULE module;
1292 if (already_loaded) return module;
1293 already_loaded = TRUE;
1295 TRACE("Trying to load GLU library\n");
1296 module = LoadLibraryW( glu32W );
1297 if (!module)
1299 WARN("Failed to load glu32\n");
1300 return NULL;
1302 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1303 LOAD_FUNCPTR(gluNewTess);
1304 LOAD_FUNCPTR(gluDeleteTess);
1305 LOAD_FUNCPTR(gluTessBeginContour);
1306 LOAD_FUNCPTR(gluTessNormal);
1307 LOAD_FUNCPTR(gluTessBeginPolygon);
1308 LOAD_FUNCPTR(gluTessCallback);
1309 LOAD_FUNCPTR(gluTessEndContour);
1310 LOAD_FUNCPTR(gluTessEndPolygon);
1311 LOAD_FUNCPTR(gluTessVertex);
1312 #undef LOAD_FUNCPTR
1313 return module;
1316 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1318 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1319 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1320 vertex[2] = 0.0;
1323 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1325 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1326 GLdouble *dbl = vertex;
1327 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1328 funcs->gl.p_glVertex3dv(vertex);
1331 static void WINAPI tess_callback_begin(GLenum which)
1333 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1334 TRACE("%d\n", which);
1335 funcs->gl.p_glBegin(which);
1338 static void WINAPI tess_callback_end(void)
1340 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1341 TRACE("\n");
1342 funcs->gl.p_glEnd();
1345 typedef struct _bezier_vector {
1346 GLdouble x;
1347 GLdouble y;
1348 } bezier_vector;
1350 static double bezier_deviation_squared(const bezier_vector *p)
1352 bezier_vector deviation;
1353 bezier_vector vertex;
1354 bezier_vector base;
1355 double base_length;
1356 double dot;
1358 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1359 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1361 base.x = p[2].x - p[0].x;
1362 base.y = p[2].y - p[0].y;
1364 base_length = sqrt(base.x*base.x + base.y*base.y);
1365 base.x /= base_length;
1366 base.y /= base_length;
1368 dot = base.x*vertex.x + base.y*vertex.y;
1369 dot = min(max(dot, 0.0), base_length);
1370 base.x *= dot;
1371 base.y *= dot;
1373 deviation.x = vertex.x-base.x;
1374 deviation.y = vertex.y-base.y;
1376 return deviation.x*deviation.x + deviation.y*deviation.y;
1379 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1381 bezier_vector first_curve[3];
1382 bezier_vector second_curve[3];
1383 bezier_vector vertex;
1384 int total_vertices;
1386 if(bezier_deviation_squared(p) <= deviation*deviation)
1388 if(points)
1389 *points = p[2];
1390 return 1;
1393 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1394 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1396 first_curve[0] = p[0];
1397 first_curve[1].x = (p[0].x + p[1].x)/2;
1398 first_curve[1].y = (p[0].y + p[1].y)/2;
1399 first_curve[2] = vertex;
1401 second_curve[0] = vertex;
1402 second_curve[1].x = (p[2].x + p[1].x)/2;
1403 second_curve[1].y = (p[2].y + p[1].y)/2;
1404 second_curve[2] = p[2];
1406 total_vertices = bezier_approximate(first_curve, points, deviation);
1407 if(points)
1408 points += total_vertices;
1409 total_vertices += bezier_approximate(second_curve, points, deviation);
1410 return total_vertices;
1413 /***********************************************************************
1414 * wglUseFontOutlines_common
1416 static BOOL wglUseFontOutlines_common(HDC hdc,
1417 DWORD first,
1418 DWORD count,
1419 DWORD listBase,
1420 FLOAT deviation,
1421 FLOAT extrusion,
1422 int format,
1423 LPGLYPHMETRICSFLOAT lpgmf,
1424 BOOL unicode)
1426 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1427 UINT glyph;
1428 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
1429 GLUtesselator *tess = NULL;
1430 LOGFONTW lf;
1431 HFONT old_font, unscaled_font;
1432 UINT em_size = 1024;
1433 RECT rc;
1435 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1436 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1438 if(deviation <= 0.0)
1439 deviation = 1.0/em_size;
1441 if(format == WGL_FONT_POLYGONS)
1443 if (!load_libglu())
1445 ERR("glu32 is required for this function but isn't available\n");
1446 return FALSE;
1449 tess = pgluNewTess();
1450 if(!tess) return FALSE;
1451 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
1452 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
1453 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1456 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1457 rc.left = rc.right = rc.bottom = 0;
1458 rc.top = em_size;
1459 DPtoLP(hdc, (POINT*)&rc, 2);
1460 lf.lfHeight = -abs(rc.top - rc.bottom);
1461 lf.lfOrientation = lf.lfEscapement = 0;
1462 unscaled_font = CreateFontIndirectW(&lf);
1463 old_font = SelectObject(hdc, unscaled_font);
1465 for (glyph = first; glyph < first + count; glyph++)
1467 DWORD needed;
1468 GLYPHMETRICS gm;
1469 BYTE *buf;
1470 TTPOLYGONHEADER *pph;
1471 TTPOLYCURVE *ppc;
1472 GLdouble *vertices = NULL;
1473 int vertex_total = -1;
1475 if(unicode)
1476 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1477 else
1478 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1480 if(needed == GDI_ERROR)
1481 goto error;
1483 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1485 if(unicode)
1486 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1487 else
1488 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1490 TRACE("glyph %d\n", glyph);
1492 if(lpgmf)
1494 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1495 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1496 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1497 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1498 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1499 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1501 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1502 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1503 lpgmf++;
1506 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1507 funcs->gl.p_glFrontFace(GL_CCW);
1508 if(format == WGL_FONT_POLYGONS)
1510 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1511 pgluTessNormal(tess, 0, 0, 1);
1512 pgluTessBeginPolygon(tess, NULL);
1515 while(!vertices)
1517 if(vertex_total != -1)
1518 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1519 vertex_total = 0;
1521 pph = (TTPOLYGONHEADER*)buf;
1522 while((BYTE*)pph < buf + needed)
1524 GLdouble previous[3];
1525 fixed_to_double(pph->pfxStart, em_size, previous);
1527 if(vertices)
1528 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1530 if(format == WGL_FONT_POLYGONS)
1531 pgluTessBeginContour(tess);
1532 else
1533 funcs->gl.p_glBegin(GL_LINE_LOOP);
1535 if(vertices)
1537 fixed_to_double(pph->pfxStart, em_size, vertices);
1538 if(format == WGL_FONT_POLYGONS)
1539 pgluTessVertex(tess, vertices, vertices);
1540 else
1541 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1542 vertices += 3;
1544 vertex_total++;
1546 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1547 while((char*)ppc < (char*)pph + pph->cb)
1549 int i, j;
1550 int num;
1552 switch(ppc->wType) {
1553 case TT_PRIM_LINE:
1554 for(i = 0; i < ppc->cpfx; i++)
1556 if(vertices)
1558 TRACE("\t\tline to %d, %d\n",
1559 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1560 fixed_to_double(ppc->apfx[i], em_size, vertices);
1561 if(format == WGL_FONT_POLYGONS)
1562 pgluTessVertex(tess, vertices, vertices);
1563 else
1564 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1565 vertices += 3;
1567 fixed_to_double(ppc->apfx[i], em_size, previous);
1568 vertex_total++;
1570 break;
1572 case TT_PRIM_QSPLINE:
1573 for(i = 0; i < ppc->cpfx-1; i++)
1575 bezier_vector curve[3];
1576 bezier_vector *points;
1577 GLdouble curve_vertex[3];
1579 if(vertices)
1580 TRACE("\t\tcurve %d,%d %d,%d\n",
1581 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1582 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1584 curve[0].x = previous[0];
1585 curve[0].y = previous[1];
1586 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1587 curve[1].x = curve_vertex[0];
1588 curve[1].y = curve_vertex[1];
1589 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1590 curve[2].x = curve_vertex[0];
1591 curve[2].y = curve_vertex[1];
1592 if(i < ppc->cpfx-2)
1594 curve[2].x = (curve[1].x + curve[2].x)/2;
1595 curve[2].y = (curve[1].y + curve[2].y)/2;
1597 num = bezier_approximate(curve, NULL, deviation);
1598 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1599 num = bezier_approximate(curve, points, deviation);
1600 vertex_total += num;
1601 if(vertices)
1603 for(j=0; j<num; j++)
1605 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1606 vertices[0] = points[j].x;
1607 vertices[1] = points[j].y;
1608 vertices[2] = 0.0;
1609 if(format == WGL_FONT_POLYGONS)
1610 pgluTessVertex(tess, vertices, vertices);
1611 else
1612 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1613 vertices += 3;
1616 HeapFree(GetProcessHeap(), 0, points);
1617 previous[0] = curve[2].x;
1618 previous[1] = curve[2].y;
1620 break;
1621 default:
1622 ERR("\t\tcurve type = %d\n", ppc->wType);
1623 if(format == WGL_FONT_POLYGONS)
1624 pgluTessEndContour(tess);
1625 else
1626 funcs->gl.p_glEnd();
1627 goto error_in_list;
1630 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1631 (ppc->cpfx - 1) * sizeof(POINTFX));
1633 if(format == WGL_FONT_POLYGONS)
1634 pgluTessEndContour(tess);
1635 else
1636 funcs->gl.p_glEnd();
1637 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1641 error_in_list:
1642 if(format == WGL_FONT_POLYGONS)
1643 pgluTessEndPolygon(tess);
1644 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1645 funcs->gl.p_glEndList();
1646 HeapFree(GetProcessHeap(), 0, buf);
1647 HeapFree(GetProcessHeap(), 0, vertices);
1650 error:
1651 DeleteObject(SelectObject(hdc, old_font));
1652 if(format == WGL_FONT_POLYGONS)
1653 pgluDeleteTess(tess);
1654 return TRUE;
1658 /***********************************************************************
1659 * wglUseFontOutlinesA (OPENGL32.@)
1661 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1662 DWORD first,
1663 DWORD count,
1664 DWORD listBase,
1665 FLOAT deviation,
1666 FLOAT extrusion,
1667 int format,
1668 LPGLYPHMETRICSFLOAT lpgmf)
1670 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1673 /***********************************************************************
1674 * wglUseFontOutlinesW (OPENGL32.@)
1676 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1677 DWORD first,
1678 DWORD count,
1679 DWORD listBase,
1680 FLOAT deviation,
1681 FLOAT extrusion,
1682 int format,
1683 LPGLYPHMETRICSFLOAT lpgmf)
1685 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1688 /***********************************************************************
1689 * glDebugEntry (OPENGL32.@)
1691 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1693 return 0;
1696 /* build the extension string by filtering out the disabled extensions */
1697 static GLubyte *filter_extensions( const char *extensions )
1699 static const char *disabled;
1700 char *p, *str;
1701 const char *end;
1703 TRACE( "GL_EXTENSIONS:\n" );
1705 if (!extensions) extensions = "";
1707 if (!disabled)
1709 HKEY hkey;
1710 DWORD size;
1712 str = NULL;
1713 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1714 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1716 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1718 str = HeapAlloc( GetProcessHeap(), 0, size );
1719 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1721 RegCloseKey( hkey );
1723 if (str)
1725 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1726 HeapFree( GetProcessHeap(), 0, str );
1728 else disabled = "";
1731 if (!disabled[0]) return NULL;
1732 if ((str = HeapAlloc( GetProcessHeap(), 0, strlen(extensions) + 2 )))
1734 p = str;
1735 for (;;)
1737 while (*extensions == ' ') extensions++;
1738 if (!*extensions) break;
1739 if (!(end = strchr( extensions, ' ' ))) end = extensions + strlen( extensions );
1740 memcpy( p, extensions, end - extensions );
1741 p[end - extensions] = 0;
1742 if (!has_extension( disabled, p , strlen( p )))
1744 TRACE("++ %s\n", p );
1745 p += end - extensions;
1746 *p++ = ' ';
1748 else TRACE("-- %s (disabled by config)\n", p );
1749 extensions = end;
1751 *p = 0;
1753 return (GLubyte *)str;
1756 /***********************************************************************
1757 * glGetString (OPENGL32.@)
1759 const GLubyte * WINAPI glGetString( GLenum name )
1761 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1762 const GLubyte *ret = funcs->gl.p_glGetString( name );
1764 if (name == GL_EXTENSIONS && ret)
1766 struct wgl_handle *ptr = get_current_context_ptr();
1767 if (ptr->u.context->extensions ||
1768 ((ptr->u.context->extensions = filter_extensions( (const char *)ret ))))
1769 ret = ptr->u.context->extensions;
1771 return ret;
1774 /***********************************************************************
1775 * OpenGL initialisation routine
1777 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1779 switch(reason)
1781 case DLL_PROCESS_ATTACH:
1782 NtCurrentTeb()->glTable = &null_opengl_funcs;
1783 break;
1784 case DLL_THREAD_ATTACH:
1785 NtCurrentTeb()->glTable = &null_opengl_funcs;
1786 break;
1788 return TRUE;