opengl32: Introduce wrappers for glGetIntegerv and glGetStringi. (v3).
[wine.git] / dlls / opengl32 / wgl.c
blobb028fe006637a9f163e4b476c57711d1c0f2731e
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 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
692 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
694 TRACE("(%d, %p)\n", pname, data);
695 funcs->gl.p_glGetIntegerv(pname, data);
698 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
700 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
702 TRACE("(%d, %d)\n", name, index);
703 if (!funcs->ext.p_glGetStringi)
705 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
707 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
710 return funcs->ext.p_glGetStringi(name, index);
713 /* check if the extension is present in the list */
714 static BOOL has_extension( const char *list, const char *ext, size_t len )
716 if (!list)
718 const char *gl_ext;
719 unsigned int i;
720 GLint extensions_count;
722 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
723 for (i = 0; i < extensions_count; ++i)
725 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
726 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
727 return TRUE;
729 return FALSE;
732 while (list)
734 while (*list == ' ') list++;
735 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
736 list = strchr( list, ' ' );
738 return FALSE;
741 static int compar(const void *elt_a, const void *elt_b) {
742 return strcmp(((const OpenGL_extension *) elt_a)->name,
743 ((const OpenGL_extension *) elt_b)->name);
746 /* Check if a GL extension is supported */
747 static BOOL is_extension_supported(const char* extension)
749 enum wgl_handle_type type = get_current_context_type();
750 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
751 const char *gl_ext_string = NULL;
752 size_t len;
754 TRACE("Checking for extension '%s'\n", extension);
756 if (type == HANDLE_CONTEXT)
758 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
759 if (!gl_ext_string)
761 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
762 return FALSE;
766 /* We use the GetProcAddress function from the display driver to retrieve function pointers
767 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
768 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
769 * require the function to return NULL when an extension isn't found. For this reason we check
770 * if the OpenGL extension required for the function we are looking up is supported. */
772 while ((len = strcspn(extension, " ")) != 0)
774 /* Check if the extension is part of the GL extension string to see if it is supported. */
775 if (has_extension(gl_ext_string, extension, len))
776 return TRUE;
778 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
779 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
780 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
781 * Check if we are searching for a core GL function */
782 if(strncmp(extension, "GL_VERSION_", 11) == 0)
784 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
785 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
787 if(!gl_version) {
788 ERR("No OpenGL version found!\n");
789 return FALSE;
792 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
793 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
794 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
795 return TRUE;
797 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]);
800 if (extension[len] == ' ') len++;
801 extension += len;
804 return FALSE;
807 /***********************************************************************
808 * wglGetProcAddress (OPENGL32.@)
810 PROC WINAPI wglGetProcAddress( LPCSTR name )
812 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
813 void **func_ptr;
814 OpenGL_extension ext;
815 const OpenGL_extension *ext_ret;
817 if (!name) return NULL;
819 /* Without an active context opengl32 doesn't know to what
820 * driver it has to dispatch wglGetProcAddress.
822 if (!get_current_context_ptr())
824 WARN("No active WGL context found\n");
825 return NULL;
828 ext.name = name;
829 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
830 if (!ext_ret)
832 WARN("Function %s unknown\n", name);
833 return NULL;
836 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
837 if (!*func_ptr)
839 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
841 if (!is_extension_supported(ext_ret->extension))
842 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
844 if (driver_func == NULL)
846 WARN("Function %s not supported by driver\n", name);
847 return NULL;
849 *func_ptr = driver_func;
852 TRACE("returning %s -> %p\n", name, ext_ret->func);
853 return ext_ret->func;
856 /***********************************************************************
857 * wglRealizeLayerPalette (OPENGL32.@)
859 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
860 int iLayerPlane,
861 BOOL bRealize) {
862 FIXME("()\n");
864 return FALSE;
867 /***********************************************************************
868 * wglSetLayerPaletteEntries (OPENGL32.@)
870 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
871 int iLayerPlane,
872 int iStart,
873 int cEntries,
874 const COLORREF *pcr) {
875 FIXME("(): stub!\n");
877 return 0;
880 /***********************************************************************
881 * wglSwapLayerBuffers (OPENGL32.@)
883 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
884 UINT fuPlanes) {
885 TRACE("(%p, %08x)\n", hdc, fuPlanes);
887 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
888 if (!wglSwapBuffers( hdc )) return FALSE;
889 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
892 if (fuPlanes) {
893 WARN("Following layers unhandled: %08x\n", fuPlanes);
896 return TRUE;
899 /***********************************************************************
900 * wglAllocateMemoryNV
902 * Provided by the WGL_NV_vertex_array_range extension.
904 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
906 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
908 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
909 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
912 /***********************************************************************
913 * wglFreeMemoryNV
915 * Provided by the WGL_NV_vertex_array_range extension.
917 void WINAPI wglFreeMemoryNV( void *pointer )
919 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
921 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
924 /***********************************************************************
925 * wglBindTexImageARB
927 * Provided by the WGL_ARB_render_texture extension.
929 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
931 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
932 BOOL ret;
934 if (!ptr) return FALSE;
935 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
936 release_handle_ptr( ptr );
937 return ret;
940 /***********************************************************************
941 * wglReleaseTexImageARB
943 * Provided by the WGL_ARB_render_texture extension.
945 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
947 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
948 BOOL ret;
950 if (!ptr) return FALSE;
951 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
952 release_handle_ptr( ptr );
953 return ret;
956 /***********************************************************************
957 * wglSetPbufferAttribARB
959 * Provided by the WGL_ARB_render_texture extension.
961 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
963 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
964 BOOL ret;
966 if (!ptr) return FALSE;
967 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
968 release_handle_ptr( ptr );
969 return ret;
972 /***********************************************************************
973 * wglChoosePixelFormatARB
975 * Provided by the WGL_ARB_pixel_format extension.
977 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
978 UINT max, int *formats, UINT *count )
980 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
982 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
983 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
986 /***********************************************************************
987 * wglGetPixelFormatAttribivARB
989 * Provided by the WGL_ARB_pixel_format extension.
991 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
992 int *values )
994 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
996 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
997 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
1000 /***********************************************************************
1001 * wglGetPixelFormatAttribfvARB
1003 * Provided by the WGL_ARB_pixel_format extension.
1005 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
1006 FLOAT *values )
1008 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1010 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
1011 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
1014 /***********************************************************************
1015 * wglCreatePbufferARB
1017 * Provided by the WGL_ARB_pbuffer extension.
1019 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1021 HPBUFFERARB ret = 0;
1022 struct wgl_pbuffer *pbuffer;
1023 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1025 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1026 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1027 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1028 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1029 return ret;
1032 /***********************************************************************
1033 * wglGetPbufferDCARB
1035 * Provided by the WGL_ARB_pbuffer extension.
1037 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1039 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1040 HDC ret;
1042 if (!ptr) return 0;
1043 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1044 release_handle_ptr( ptr );
1045 return ret;
1048 /***********************************************************************
1049 * wglReleasePbufferDCARB
1051 * Provided by the WGL_ARB_pbuffer extension.
1053 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1055 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1056 BOOL ret;
1058 if (!ptr) return FALSE;
1059 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1060 release_handle_ptr( ptr );
1061 return ret;
1064 /***********************************************************************
1065 * wglDestroyPbufferARB
1067 * Provided by the WGL_ARB_pbuffer extension.
1069 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1071 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1073 if (!ptr) return FALSE;
1074 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1075 free_handle_ptr( ptr );
1076 return TRUE;
1079 /***********************************************************************
1080 * wglQueryPbufferARB
1082 * Provided by the WGL_ARB_pbuffer extension.
1084 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1086 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1087 BOOL ret;
1089 if (!ptr) return FALSE;
1090 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1091 release_handle_ptr( ptr );
1092 return ret;
1095 /***********************************************************************
1096 * wglGetExtensionsStringARB
1098 * Provided by the WGL_ARB_extensions_string extension.
1100 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1102 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1104 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1105 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1108 /***********************************************************************
1109 * wglGetExtensionsStringEXT
1111 * Provided by the WGL_EXT_extensions_string extension.
1113 const char * WINAPI wglGetExtensionsStringEXT(void)
1115 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1117 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1118 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1121 /***********************************************************************
1122 * wglSwapIntervalEXT
1124 * Provided by the WGL_EXT_swap_control extension.
1126 BOOL WINAPI wglSwapIntervalEXT( int interval )
1128 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1130 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1131 return funcs->ext.p_wglSwapIntervalEXT( interval );
1134 /***********************************************************************
1135 * wglGetSwapIntervalEXT
1137 * Provided by the WGL_EXT_swap_control extension.
1139 int WINAPI wglGetSwapIntervalEXT(void)
1141 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1143 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1144 return funcs->ext.p_wglGetSwapIntervalEXT();
1147 /***********************************************************************
1148 * wglSetPixelFormatWINE
1150 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1152 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1154 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1156 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1157 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1160 /***********************************************************************
1161 * wglUseFontBitmaps_common
1163 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1165 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1166 GLYPHMETRICS gm;
1167 unsigned int glyph, size = 0;
1168 void *bitmap = NULL, *gl_bitmap = NULL;
1169 int org_alignment;
1170 BOOL ret = TRUE;
1172 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1173 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1175 for (glyph = first; glyph < first + count; glyph++) {
1176 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1177 unsigned int needed_size, height, width, width_int;
1179 if (unicode)
1180 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1181 else
1182 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1184 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1185 if (needed_size == GDI_ERROR) {
1186 ret = FALSE;
1187 break;
1190 if (needed_size > size) {
1191 size = needed_size;
1192 HeapFree(GetProcessHeap(), 0, bitmap);
1193 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1194 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1195 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1197 if (needed_size != 0) {
1198 if (unicode)
1199 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1200 size, bitmap, &identity) != GDI_ERROR);
1201 else
1202 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1203 size, bitmap, &identity) != GDI_ERROR);
1204 if (!ret) break;
1207 if (TRACE_ON(wgl)) {
1208 unsigned int bitmask;
1209 unsigned char *bitmap_ = bitmap;
1211 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1212 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1213 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1214 if (needed_size != 0) {
1215 TRACE(" - bitmap:\n");
1216 for (height = 0; height < gm.gmBlackBoxY; height++) {
1217 TRACE(" ");
1218 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1219 if (bitmask == 0) {
1220 bitmap_ += 1;
1221 bitmask = 0x80;
1223 if (*bitmap_ & bitmask)
1224 TRACE("*");
1225 else
1226 TRACE(" ");
1228 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1229 TRACE("\n");
1234 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1235 * glyph for it to be drawn properly.
1237 if (needed_size != 0) {
1238 width_int = (gm.gmBlackBoxX + 31) / 32;
1239 for (height = 0; height < gm.gmBlackBoxY; height++) {
1240 for (width = 0; width < width_int; width++) {
1241 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1242 ((int *) bitmap)[height * width_int + width];
1247 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1248 if (needed_size != 0) {
1249 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1250 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1251 gm.gmCellIncX, gm.gmCellIncY,
1252 gl_bitmap);
1253 } else {
1254 /* This is the case of 'empty' glyphs like the space character */
1255 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1257 funcs->gl.p_glEndList();
1260 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1261 HeapFree(GetProcessHeap(), 0, bitmap);
1262 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1263 return ret;
1266 /***********************************************************************
1267 * wglUseFontBitmapsA (OPENGL32.@)
1269 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1271 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1274 /***********************************************************************
1275 * wglUseFontBitmapsW (OPENGL32.@)
1277 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1279 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1282 /* FIXME: should probably have a glu.h header */
1284 typedef struct GLUtesselator GLUtesselator;
1285 typedef void (WINAPI *_GLUfuncptr)(void);
1287 #define GLU_TESS_BEGIN 100100
1288 #define GLU_TESS_VERTEX 100101
1289 #define GLU_TESS_END 100102
1291 static GLUtesselator * (WINAPI *pgluNewTess)(void);
1292 static void (WINAPI *pgluDeleteTess)(GLUtesselator *tess);
1293 static void (WINAPI *pgluTessNormal)(GLUtesselator *tess, GLdouble x, GLdouble y, GLdouble z);
1294 static void (WINAPI *pgluTessBeginPolygon)(GLUtesselator *tess, void *polygon_data);
1295 static void (WINAPI *pgluTessEndPolygon)(GLUtesselator *tess);
1296 static void (WINAPI *pgluTessCallback)(GLUtesselator *tess, GLenum which, _GLUfuncptr fn);
1297 static void (WINAPI *pgluTessBeginContour)(GLUtesselator *tess);
1298 static void (WINAPI *pgluTessEndContour)(GLUtesselator *tess);
1299 static void (WINAPI *pgluTessVertex)(GLUtesselator *tess, GLdouble *location, GLvoid* data);
1301 static HMODULE load_libglu(void)
1303 static const WCHAR glu32W[] = {'g','l','u','3','2','.','d','l','l',0};
1304 static BOOL already_loaded;
1305 static HMODULE module;
1307 if (already_loaded) return module;
1308 already_loaded = TRUE;
1310 TRACE("Trying to load GLU library\n");
1311 module = LoadLibraryW( glu32W );
1312 if (!module)
1314 WARN("Failed to load glu32\n");
1315 return NULL;
1317 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1318 LOAD_FUNCPTR(gluNewTess);
1319 LOAD_FUNCPTR(gluDeleteTess);
1320 LOAD_FUNCPTR(gluTessBeginContour);
1321 LOAD_FUNCPTR(gluTessNormal);
1322 LOAD_FUNCPTR(gluTessBeginPolygon);
1323 LOAD_FUNCPTR(gluTessCallback);
1324 LOAD_FUNCPTR(gluTessEndContour);
1325 LOAD_FUNCPTR(gluTessEndPolygon);
1326 LOAD_FUNCPTR(gluTessVertex);
1327 #undef LOAD_FUNCPTR
1328 return module;
1331 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1333 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1334 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1335 vertex[2] = 0.0;
1338 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1340 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1341 GLdouble *dbl = vertex;
1342 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1343 funcs->gl.p_glVertex3dv(vertex);
1346 static void WINAPI tess_callback_begin(GLenum which)
1348 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1349 TRACE("%d\n", which);
1350 funcs->gl.p_glBegin(which);
1353 static void WINAPI tess_callback_end(void)
1355 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1356 TRACE("\n");
1357 funcs->gl.p_glEnd();
1360 typedef struct _bezier_vector {
1361 GLdouble x;
1362 GLdouble y;
1363 } bezier_vector;
1365 static double bezier_deviation_squared(const bezier_vector *p)
1367 bezier_vector deviation;
1368 bezier_vector vertex;
1369 bezier_vector base;
1370 double base_length;
1371 double dot;
1373 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1374 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1376 base.x = p[2].x - p[0].x;
1377 base.y = p[2].y - p[0].y;
1379 base_length = sqrt(base.x*base.x + base.y*base.y);
1380 base.x /= base_length;
1381 base.y /= base_length;
1383 dot = base.x*vertex.x + base.y*vertex.y;
1384 dot = min(max(dot, 0.0), base_length);
1385 base.x *= dot;
1386 base.y *= dot;
1388 deviation.x = vertex.x-base.x;
1389 deviation.y = vertex.y-base.y;
1391 return deviation.x*deviation.x + deviation.y*deviation.y;
1394 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1396 bezier_vector first_curve[3];
1397 bezier_vector second_curve[3];
1398 bezier_vector vertex;
1399 int total_vertices;
1401 if(bezier_deviation_squared(p) <= deviation*deviation)
1403 if(points)
1404 *points = p[2];
1405 return 1;
1408 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1409 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1411 first_curve[0] = p[0];
1412 first_curve[1].x = (p[0].x + p[1].x)/2;
1413 first_curve[1].y = (p[0].y + p[1].y)/2;
1414 first_curve[2] = vertex;
1416 second_curve[0] = vertex;
1417 second_curve[1].x = (p[2].x + p[1].x)/2;
1418 second_curve[1].y = (p[2].y + p[1].y)/2;
1419 second_curve[2] = p[2];
1421 total_vertices = bezier_approximate(first_curve, points, deviation);
1422 if(points)
1423 points += total_vertices;
1424 total_vertices += bezier_approximate(second_curve, points, deviation);
1425 return total_vertices;
1428 /***********************************************************************
1429 * wglUseFontOutlines_common
1431 static BOOL wglUseFontOutlines_common(HDC hdc,
1432 DWORD first,
1433 DWORD count,
1434 DWORD listBase,
1435 FLOAT deviation,
1436 FLOAT extrusion,
1437 int format,
1438 LPGLYPHMETRICSFLOAT lpgmf,
1439 BOOL unicode)
1441 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1442 UINT glyph;
1443 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
1444 GLUtesselator *tess = NULL;
1445 LOGFONTW lf;
1446 HFONT old_font, unscaled_font;
1447 UINT em_size = 1024;
1448 RECT rc;
1450 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1451 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1453 if(deviation <= 0.0)
1454 deviation = 1.0/em_size;
1456 if(format == WGL_FONT_POLYGONS)
1458 if (!load_libglu())
1460 ERR("glu32 is required for this function but isn't available\n");
1461 return FALSE;
1464 tess = pgluNewTess();
1465 if(!tess) return FALSE;
1466 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
1467 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
1468 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1471 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1472 rc.left = rc.right = rc.bottom = 0;
1473 rc.top = em_size;
1474 DPtoLP(hdc, (POINT*)&rc, 2);
1475 lf.lfHeight = -abs(rc.top - rc.bottom);
1476 lf.lfOrientation = lf.lfEscapement = 0;
1477 unscaled_font = CreateFontIndirectW(&lf);
1478 old_font = SelectObject(hdc, unscaled_font);
1480 for (glyph = first; glyph < first + count; glyph++)
1482 DWORD needed;
1483 GLYPHMETRICS gm;
1484 BYTE *buf;
1485 TTPOLYGONHEADER *pph;
1486 TTPOLYCURVE *ppc;
1487 GLdouble *vertices = NULL;
1488 int vertex_total = -1;
1490 if(unicode)
1491 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1492 else
1493 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1495 if(needed == GDI_ERROR)
1496 goto error;
1498 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1500 if(unicode)
1501 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1502 else
1503 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1505 TRACE("glyph %d\n", glyph);
1507 if(lpgmf)
1509 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1510 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1511 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1512 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1513 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1514 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1516 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1517 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1518 lpgmf++;
1521 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1522 funcs->gl.p_glFrontFace(GL_CCW);
1523 if(format == WGL_FONT_POLYGONS)
1525 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1526 pgluTessNormal(tess, 0, 0, 1);
1527 pgluTessBeginPolygon(tess, NULL);
1530 while(!vertices)
1532 if(vertex_total != -1)
1533 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1534 vertex_total = 0;
1536 pph = (TTPOLYGONHEADER*)buf;
1537 while((BYTE*)pph < buf + needed)
1539 GLdouble previous[3];
1540 fixed_to_double(pph->pfxStart, em_size, previous);
1542 if(vertices)
1543 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1545 if(format == WGL_FONT_POLYGONS)
1546 pgluTessBeginContour(tess);
1547 else
1548 funcs->gl.p_glBegin(GL_LINE_LOOP);
1550 if(vertices)
1552 fixed_to_double(pph->pfxStart, em_size, vertices);
1553 if(format == WGL_FONT_POLYGONS)
1554 pgluTessVertex(tess, vertices, vertices);
1555 else
1556 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1557 vertices += 3;
1559 vertex_total++;
1561 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1562 while((char*)ppc < (char*)pph + pph->cb)
1564 int i, j;
1565 int num;
1567 switch(ppc->wType) {
1568 case TT_PRIM_LINE:
1569 for(i = 0; i < ppc->cpfx; i++)
1571 if(vertices)
1573 TRACE("\t\tline to %d, %d\n",
1574 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1575 fixed_to_double(ppc->apfx[i], em_size, vertices);
1576 if(format == WGL_FONT_POLYGONS)
1577 pgluTessVertex(tess, vertices, vertices);
1578 else
1579 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1580 vertices += 3;
1582 fixed_to_double(ppc->apfx[i], em_size, previous);
1583 vertex_total++;
1585 break;
1587 case TT_PRIM_QSPLINE:
1588 for(i = 0; i < ppc->cpfx-1; i++)
1590 bezier_vector curve[3];
1591 bezier_vector *points;
1592 GLdouble curve_vertex[3];
1594 if(vertices)
1595 TRACE("\t\tcurve %d,%d %d,%d\n",
1596 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1597 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1599 curve[0].x = previous[0];
1600 curve[0].y = previous[1];
1601 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1602 curve[1].x = curve_vertex[0];
1603 curve[1].y = curve_vertex[1];
1604 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1605 curve[2].x = curve_vertex[0];
1606 curve[2].y = curve_vertex[1];
1607 if(i < ppc->cpfx-2)
1609 curve[2].x = (curve[1].x + curve[2].x)/2;
1610 curve[2].y = (curve[1].y + curve[2].y)/2;
1612 num = bezier_approximate(curve, NULL, deviation);
1613 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1614 num = bezier_approximate(curve, points, deviation);
1615 vertex_total += num;
1616 if(vertices)
1618 for(j=0; j<num; j++)
1620 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1621 vertices[0] = points[j].x;
1622 vertices[1] = points[j].y;
1623 vertices[2] = 0.0;
1624 if(format == WGL_FONT_POLYGONS)
1625 pgluTessVertex(tess, vertices, vertices);
1626 else
1627 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1628 vertices += 3;
1631 HeapFree(GetProcessHeap(), 0, points);
1632 previous[0] = curve[2].x;
1633 previous[1] = curve[2].y;
1635 break;
1636 default:
1637 ERR("\t\tcurve type = %d\n", ppc->wType);
1638 if(format == WGL_FONT_POLYGONS)
1639 pgluTessEndContour(tess);
1640 else
1641 funcs->gl.p_glEnd();
1642 goto error_in_list;
1645 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1646 (ppc->cpfx - 1) * sizeof(POINTFX));
1648 if(format == WGL_FONT_POLYGONS)
1649 pgluTessEndContour(tess);
1650 else
1651 funcs->gl.p_glEnd();
1652 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1656 error_in_list:
1657 if(format == WGL_FONT_POLYGONS)
1658 pgluTessEndPolygon(tess);
1659 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1660 funcs->gl.p_glEndList();
1661 HeapFree(GetProcessHeap(), 0, buf);
1662 HeapFree(GetProcessHeap(), 0, vertices);
1665 error:
1666 DeleteObject(SelectObject(hdc, old_font));
1667 if(format == WGL_FONT_POLYGONS)
1668 pgluDeleteTess(tess);
1669 return TRUE;
1673 /***********************************************************************
1674 * wglUseFontOutlinesA (OPENGL32.@)
1676 BOOL WINAPI wglUseFontOutlinesA(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, FALSE);
1688 /***********************************************************************
1689 * wglUseFontOutlinesW (OPENGL32.@)
1691 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1692 DWORD first,
1693 DWORD count,
1694 DWORD listBase,
1695 FLOAT deviation,
1696 FLOAT extrusion,
1697 int format,
1698 LPGLYPHMETRICSFLOAT lpgmf)
1700 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1703 /***********************************************************************
1704 * glDebugEntry (OPENGL32.@)
1706 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1708 return 0;
1711 /* build the extension string by filtering out the disabled extensions */
1712 static GLubyte *filter_extensions( const char *extensions )
1714 static const char *disabled;
1715 char *p, *str;
1716 const char *end;
1718 TRACE( "GL_EXTENSIONS:\n" );
1720 if (!extensions) extensions = "";
1722 if (!disabled)
1724 HKEY hkey;
1725 DWORD size;
1727 str = NULL;
1728 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1729 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1731 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1733 str = HeapAlloc( GetProcessHeap(), 0, size );
1734 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1736 RegCloseKey( hkey );
1738 if (str)
1740 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1741 HeapFree( GetProcessHeap(), 0, str );
1743 else disabled = "";
1746 if (!disabled[0]) return NULL;
1747 if ((str = HeapAlloc( GetProcessHeap(), 0, strlen(extensions) + 2 )))
1749 p = str;
1750 for (;;)
1752 while (*extensions == ' ') extensions++;
1753 if (!*extensions) break;
1754 if (!(end = strchr( extensions, ' ' ))) end = extensions + strlen( extensions );
1755 memcpy( p, extensions, end - extensions );
1756 p[end - extensions] = 0;
1757 if (!has_extension( disabled, p , strlen( p )))
1759 TRACE("++ %s\n", p );
1760 p += end - extensions;
1761 *p++ = ' ';
1763 else TRACE("-- %s (disabled by config)\n", p );
1764 extensions = end;
1766 *p = 0;
1768 return (GLubyte *)str;
1771 /***********************************************************************
1772 * glGetString (OPENGL32.@)
1774 const GLubyte * WINAPI glGetString( GLenum name )
1776 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1777 const GLubyte *ret = funcs->gl.p_glGetString( name );
1779 if (name == GL_EXTENSIONS && ret)
1781 struct wgl_handle *ptr = get_current_context_ptr();
1782 if (ptr->u.context->extensions ||
1783 ((ptr->u.context->extensions = filter_extensions( (const char *)ret ))))
1784 ret = ptr->u.context->extensions;
1786 return ret;
1789 /***********************************************************************
1790 * OpenGL initialisation routine
1792 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1794 switch(reason)
1796 case DLL_PROCESS_ATTACH:
1797 NtCurrentTeb()->glTable = &null_opengl_funcs;
1798 break;
1799 case DLL_THREAD_ATTACH:
1800 NtCurrentTeb()->glTable = &null_opengl_funcs;
1801 break;
1803 return TRUE;