msvcp110: Add tr2_sys__File_size_wchar implementation and test.
[wine/multimedia.git] / dlls / opengl32 / wgl.c
blob9482c7584d08b5712b4c830b4e0652d0a3b7c644
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 GLuint *disabled_exts; /* indices of disabled extensions */
67 struct wgl_context *drv_ctx; /* driver context */
70 struct wgl_handle
72 UINT handle;
73 struct opengl_funcs *funcs;
74 union
76 struct opengl_context *context; /* for HANDLE_CONTEXT */
77 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
78 struct wgl_handle *next; /* for free handles */
79 } u;
82 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
83 static struct wgl_handle *next_free;
84 static unsigned int handle_count;
86 static CRITICAL_SECTION wgl_section;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
89 0, 0, &wgl_section,
90 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
93 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
95 static inline struct opengl_funcs *get_dc_funcs( HDC hdc )
97 struct opengl_funcs *funcs = __wine_get_wgl_driver( hdc, WINE_WGL_DRIVER_VERSION );
98 if (funcs == (void *)-1) funcs = &null_opengl_funcs;
99 return funcs;
102 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
104 WORD generation = HIWORD( ptr->handle ) + 1;
105 if (!generation) generation++;
106 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
107 return ULongToHandle( ptr->handle );
110 /* the current context is assumed valid and doesn't need locking */
111 static inline struct wgl_handle *get_current_context_ptr(void)
113 if (!NtCurrentTeb()->glCurrentRC) return NULL;
114 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
117 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
119 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
121 EnterCriticalSection( &wgl_section );
122 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
123 return &wgl_handles[index];
125 LeaveCriticalSection( &wgl_section );
126 SetLastError( ERROR_INVALID_HANDLE );
127 return NULL;
130 static void release_handle_ptr( struct wgl_handle *ptr )
132 if (ptr) LeaveCriticalSection( &wgl_section );
135 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
137 HANDLE handle = 0;
138 struct wgl_handle *ptr = NULL;
140 EnterCriticalSection( &wgl_section );
141 if ((ptr = next_free))
142 next_free = next_free->u.next;
143 else if (handle_count < MAX_WGL_HANDLES)
144 ptr = &wgl_handles[handle_count++];
146 if (ptr)
148 ptr->funcs = funcs;
149 ptr->u.context = user_ptr;
150 handle = next_handle( ptr, type );
152 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
153 LeaveCriticalSection( &wgl_section );
154 return handle;
157 static void free_handle_ptr( struct wgl_handle *ptr )
159 ptr->handle |= 0xffff;
160 ptr->u.next = next_free;
161 ptr->funcs = NULL;
162 next_free = ptr;
163 LeaveCriticalSection( &wgl_section );
166 static inline enum wgl_handle_type get_current_context_type(void)
168 if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
169 return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
172 /***********************************************************************
173 * wglCopyContext (OPENGL32.@)
175 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
177 struct wgl_handle *src, *dst;
178 BOOL ret = FALSE;
180 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
181 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
183 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
184 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
185 dst->u.context->drv_ctx, mask );
187 release_handle_ptr( dst );
188 release_handle_ptr( src );
189 return ret;
192 /***********************************************************************
193 * wglDeleteContext (OPENGL32.@)
195 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
197 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
199 if (!ptr) return FALSE;
201 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
203 SetLastError( ERROR_BUSY );
204 release_handle_ptr( ptr );
205 return FALSE;
207 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
208 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
209 HeapFree( GetProcessHeap(), 0, ptr->u.context->disabled_exts );
210 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
211 HeapFree( GetProcessHeap(), 0, ptr->u.context );
212 free_handle_ptr( ptr );
213 return TRUE;
216 /***********************************************************************
217 * wglMakeCurrent (OPENGL32.@)
219 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
221 BOOL ret = TRUE;
222 struct wgl_handle *ptr, *prev = get_current_context_ptr();
224 if (hglrc)
226 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
227 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
229 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
230 if (ret)
232 if (prev) prev->u.context->tid = 0;
233 ptr->u.context->tid = GetCurrentThreadId();
234 ptr->u.context->draw_dc = hdc;
235 ptr->u.context->read_dc = hdc;
236 NtCurrentTeb()->glCurrentRC = hglrc;
237 NtCurrentTeb()->glTable = ptr->funcs;
240 else
242 SetLastError( ERROR_BUSY );
243 ret = FALSE;
245 release_handle_ptr( ptr );
247 else if (prev)
249 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
250 prev->u.context->tid = 0;
251 NtCurrentTeb()->glCurrentRC = 0;
252 NtCurrentTeb()->glTable = &null_opengl_funcs;
254 else if (!hdc)
256 SetLastError( ERROR_INVALID_HANDLE );
257 ret = FALSE;
259 return ret;
262 /***********************************************************************
263 * wglCreateContextAttribsARB
265 * Provided by the WGL_ARB_create_context extension.
267 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
269 HGLRC ret = 0;
270 struct wgl_context *drv_ctx;
271 struct wgl_handle *share_ptr = NULL;
272 struct opengl_context *context;
273 struct opengl_funcs *funcs = get_dc_funcs( hdc );
275 if (!funcs || !funcs->ext.p_wglCreateContextAttribsARB) return 0;
276 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT ))) return 0;
277 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
278 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
280 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
282 enum wgl_handle_type type = HANDLE_CONTEXT;
284 if (attribs)
286 while (*attribs)
288 if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
290 if (attribs[1] >= 3)
291 type = HANDLE_CONTEXT_V3;
292 break;
294 attribs += 2;
298 context->drv_ctx = drv_ctx;
299 if (!(ret = alloc_handle( type, funcs, context )))
300 HeapFree( GetProcessHeap(), 0, context );
302 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
304 release_handle_ptr( share_ptr );
305 return ret;
309 /***********************************************************************
310 * wglMakeContextCurrentARB
312 * Provided by the WGL_ARB_make_current_read extension.
314 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
316 BOOL ret = TRUE;
317 struct wgl_handle *ptr, *prev = get_current_context_ptr();
319 if (hglrc)
321 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
322 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
324 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
325 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
326 ptr->u.context->drv_ctx ));
327 if (ret)
329 if (prev) prev->u.context->tid = 0;
330 ptr->u.context->tid = GetCurrentThreadId();
331 ptr->u.context->draw_dc = draw_hdc;
332 ptr->u.context->read_dc = read_hdc;
333 NtCurrentTeb()->glCurrentRC = hglrc;
334 NtCurrentTeb()->glTable = ptr->funcs;
337 else
339 SetLastError( ERROR_BUSY );
340 ret = FALSE;
342 release_handle_ptr( ptr );
344 else if (prev)
346 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
347 prev->u.context->tid = 0;
348 NtCurrentTeb()->glCurrentRC = 0;
349 NtCurrentTeb()->glTable = &null_opengl_funcs;
351 return ret;
354 /***********************************************************************
355 * wglGetCurrentReadDCARB
357 * Provided by the WGL_ARB_make_current_read extension.
359 HDC WINAPI wglGetCurrentReadDCARB(void)
361 struct wgl_handle *ptr = get_current_context_ptr();
363 if (!ptr) return 0;
364 return ptr->u.context->read_dc;
367 /***********************************************************************
368 * wglShareLists (OPENGL32.@)
370 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
372 BOOL ret = FALSE;
373 struct wgl_handle *src, *dst;
375 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
376 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
378 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
379 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
381 release_handle_ptr( dst );
382 release_handle_ptr( src );
383 return ret;
386 /***********************************************************************
387 * wglGetCurrentDC (OPENGL32.@)
389 HDC WINAPI wglGetCurrentDC(void)
391 struct wgl_handle *ptr = get_current_context_ptr();
393 if (!ptr) return 0;
394 return ptr->u.context->draw_dc;
397 /***********************************************************************
398 * wglCreateContext (OPENGL32.@)
400 HGLRC WINAPI wglCreateContext(HDC hdc)
402 HGLRC ret = 0;
403 struct wgl_context *drv_ctx;
404 struct opengl_context *context;
405 struct opengl_funcs *funcs = get_dc_funcs( hdc );
407 if (!funcs) return 0;
408 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
409 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
411 context->drv_ctx = drv_ctx;
412 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
413 HeapFree( GetProcessHeap(), 0, context );
415 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
416 return ret;
419 /***********************************************************************
420 * wglGetCurrentContext (OPENGL32.@)
422 HGLRC WINAPI wglGetCurrentContext(void)
424 return NtCurrentTeb()->glCurrentRC;
427 /***********************************************************************
428 * wglDescribePixelFormat (OPENGL32.@)
430 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
432 struct opengl_funcs *funcs = get_dc_funcs( hdc );
433 if (!funcs) return 0;
434 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
437 /***********************************************************************
438 * wglChoosePixelFormat (OPENGL32.@)
440 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
442 PIXELFORMATDESCRIPTOR format, best;
443 int i, count, best_format;
444 int bestDBuffer = -1, bestStereo = -1;
446 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
447 "accum %u depth %u stencil %u aux %u\n",
448 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
449 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
450 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
452 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
453 if (!count) return 0;
455 best_format = 0;
456 best.dwFlags = 0;
457 best.cAlphaBits = -1;
458 best.cColorBits = -1;
459 best.cDepthBits = -1;
460 best.cStencilBits = -1;
461 best.cAuxBuffers = -1;
463 for (i = 1; i <= count; i++)
465 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
467 if (ppfd->iPixelType != format.iPixelType)
469 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
470 continue;
473 /* only use bitmap capable for formats for bitmap rendering */
474 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
476 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
477 continue;
480 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
481 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
482 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
483 * formats without the given flag set.
484 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
485 * has indicated that a format without stereo is returned when stereo is unavailable.
486 * So in case PFD_STEREO is set, formats that support it should have priority above formats
487 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
489 * To summarize the following is most likely the correct behavior:
490 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
491 * stereo set -> prefer stereo formats, but also accept non-stereo formats
492 * stereo don't care -> it doesn't matter whether we get stereo or not
494 * In Wine we will treat non-stereo the same way as don't care because it makes
495 * format selection even more complicated and second drivers with Stereo advertise
496 * each format twice anyway.
499 /* Doublebuffer, see the comments above */
500 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
502 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
503 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
504 goto found;
506 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
509 /* Stereo, see the comments above. */
510 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
512 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
513 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
514 goto found;
516 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
519 /* Below we will do a number of checks to select the 'best' pixelformat.
520 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
521 * The code works by trying to match the most important options as close as possible.
522 * When a reasonable format is found, we will try to match more options.
523 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
524 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
525 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
527 if (ppfd->cColorBits)
529 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
530 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
531 goto found;
533 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
535 TRACE( "color mismatch for iPixelFormat=%d\n", i );
536 continue;
539 if (ppfd->cAlphaBits)
541 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
542 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
543 goto found;
545 if (best.cAlphaBits != format.cAlphaBits)
547 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
548 continue;
551 if (ppfd->cDepthBits)
553 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
554 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
555 goto found;
557 if (best.cDepthBits != format.cDepthBits)
559 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
560 continue;
563 if (ppfd->cStencilBits)
565 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
566 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
567 goto found;
569 if (best.cStencilBits != format.cStencilBits)
571 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
572 continue;
575 if (ppfd->cAuxBuffers)
577 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
578 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
579 goto found;
581 if (best.cAuxBuffers != format.cAuxBuffers)
583 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
584 continue;
587 continue;
589 found:
590 best_format = i;
591 best = format;
592 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
593 bestStereo = format.dwFlags & PFD_STEREO;
596 TRACE( "returning %u\n", best_format );
597 return best_format;
600 /***********************************************************************
601 * wglGetPixelFormat (OPENGL32.@)
603 INT WINAPI wglGetPixelFormat(HDC hdc)
605 struct opengl_funcs *funcs = get_dc_funcs( hdc );
606 if (!funcs) return 0;
607 return funcs->wgl.p_wglGetPixelFormat( hdc );
610 /***********************************************************************
611 * wglSetPixelFormat(OPENGL32.@)
613 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
615 struct opengl_funcs *funcs = get_dc_funcs( hdc );
616 if (!funcs) return FALSE;
617 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
620 /***********************************************************************
621 * wglSwapBuffers (OPENGL32.@)
623 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
625 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
627 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
628 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
630 if (TRACE_ON(fps))
632 static long prev_time, start_time;
633 static unsigned long frames, frames_total;
635 DWORD time = GetTickCount();
636 frames++;
637 frames_total++;
638 /* every 1.5 seconds */
639 if (time - prev_time > 1500)
641 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
642 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
643 prev_time = time;
644 frames = 0;
645 if (start_time == 0) start_time = time;
648 return TRUE;
651 /***********************************************************************
652 * wglCreateLayerContext (OPENGL32.@)
654 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
655 int iLayerPlane) {
656 TRACE("(%p,%d)\n", hdc, iLayerPlane);
658 if (iLayerPlane == 0) {
659 return wglCreateContext(hdc);
661 FIXME("no handler for layer %d\n", iLayerPlane);
663 return NULL;
666 /***********************************************************************
667 * wglDescribeLayerPlane (OPENGL32.@)
669 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
670 int iPixelFormat,
671 int iLayerPlane,
672 UINT nBytes,
673 LPLAYERPLANEDESCRIPTOR plpd) {
674 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
676 return FALSE;
679 /***********************************************************************
680 * wglGetLayerPaletteEntries (OPENGL32.@)
682 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
683 int iLayerPlane,
684 int iStart,
685 int cEntries,
686 const COLORREF *pcr) {
687 FIXME("(): stub!\n");
689 return 0;
692 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
694 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
696 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
698 TRACE("(%d, %p)\n", pname, data);
699 if (pname == GL_NUM_EXTENSIONS)
701 struct wgl_handle *ptr = get_current_context_ptr();
703 if (ptr->u.context->disabled_exts ||
704 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
706 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
707 GLint count, disabled_count = 0;
709 funcs->gl.p_glGetIntegerv(pname, &count);
710 while (*disabled_exts++ != ~0u)
711 disabled_count++;
712 *data = count - disabled_count;
713 return;
716 funcs->gl.p_glGetIntegerv(pname, data);
719 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
721 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
723 TRACE("(%d, %d)\n", name, index);
724 if (!funcs->ext.p_glGetStringi)
726 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
728 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
731 if (name == GL_EXTENSIONS)
733 struct wgl_handle *ptr = get_current_context_ptr();
735 if (ptr->u.context->disabled_exts ||
736 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
738 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
739 unsigned int disabled_count = 0;
741 while (index + disabled_count >= *disabled_exts++)
742 disabled_count++;
743 return funcs->ext.p_glGetStringi(name, index + disabled_count);
746 return funcs->ext.p_glGetStringi(name, index);
749 /* check if the extension is present in the list */
750 static BOOL has_extension( const char *list, const char *ext, size_t len )
752 if (!list)
754 const char *gl_ext;
755 unsigned int i;
756 GLint extensions_count;
758 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
759 for (i = 0; i < extensions_count; ++i)
761 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
762 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
763 return TRUE;
765 return FALSE;
768 while (list)
770 while (*list == ' ') list++;
771 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
772 list = strchr( list, ' ' );
774 return FALSE;
777 static int compar(const void *elt_a, const void *elt_b) {
778 return strcmp(((const OpenGL_extension *) elt_a)->name,
779 ((const OpenGL_extension *) elt_b)->name);
782 /* Check if a GL extension is supported */
783 static BOOL is_extension_supported(const char* extension)
785 enum wgl_handle_type type = get_current_context_type();
786 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
787 const char *gl_ext_string = NULL;
788 size_t len;
790 TRACE("Checking for extension '%s'\n", extension);
792 if (type == HANDLE_CONTEXT)
794 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
795 if (!gl_ext_string)
797 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
798 return FALSE;
802 /* We use the GetProcAddress function from the display driver to retrieve function pointers
803 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
804 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
805 * require the function to return NULL when an extension isn't found. For this reason we check
806 * if the OpenGL extension required for the function we are looking up is supported. */
808 while ((len = strcspn(extension, " ")) != 0)
810 /* Check if the extension is part of the GL extension string to see if it is supported. */
811 if (has_extension(gl_ext_string, extension, len))
812 return TRUE;
814 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
815 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
816 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
817 * Check if we are searching for a core GL function */
818 if(strncmp(extension, "GL_VERSION_", 11) == 0)
820 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
821 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
823 if(!gl_version) {
824 ERR("No OpenGL version found!\n");
825 return FALSE;
828 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
829 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
830 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
831 return TRUE;
833 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]);
836 if (extension[len] == ' ') len++;
837 extension += len;
840 return FALSE;
843 /***********************************************************************
844 * wglGetProcAddress (OPENGL32.@)
846 PROC WINAPI wglGetProcAddress( LPCSTR name )
848 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
849 void **func_ptr;
850 OpenGL_extension ext;
851 const OpenGL_extension *ext_ret;
853 if (!name) return NULL;
855 /* Without an active context opengl32 doesn't know to what
856 * driver it has to dispatch wglGetProcAddress.
858 if (!get_current_context_ptr())
860 WARN("No active WGL context found\n");
861 return NULL;
864 ext.name = name;
865 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
866 if (!ext_ret)
868 WARN("Function %s unknown\n", name);
869 return NULL;
872 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
873 if (!*func_ptr)
875 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
877 if (!is_extension_supported(ext_ret->extension))
879 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
880 return NULL;
883 if (driver_func == NULL)
885 WARN("Function %s not supported by driver\n", name);
886 return NULL;
888 *func_ptr = driver_func;
891 TRACE("returning %s -> %p\n", name, ext_ret->func);
892 return ext_ret->func;
895 /***********************************************************************
896 * wglRealizeLayerPalette (OPENGL32.@)
898 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
899 int iLayerPlane,
900 BOOL bRealize) {
901 FIXME("()\n");
903 return FALSE;
906 /***********************************************************************
907 * wglSetLayerPaletteEntries (OPENGL32.@)
909 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
910 int iLayerPlane,
911 int iStart,
912 int cEntries,
913 const COLORREF *pcr) {
914 FIXME("(): stub!\n");
916 return 0;
919 /***********************************************************************
920 * wglSwapLayerBuffers (OPENGL32.@)
922 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
923 UINT fuPlanes) {
924 TRACE("(%p, %08x)\n", hdc, fuPlanes);
926 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
927 if (!wglSwapBuffers( hdc )) return FALSE;
928 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
931 if (fuPlanes) {
932 WARN("Following layers unhandled: %08x\n", fuPlanes);
935 return TRUE;
938 /***********************************************************************
939 * wglAllocateMemoryNV
941 * Provided by the WGL_NV_vertex_array_range extension.
943 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
945 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
947 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
948 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
951 /***********************************************************************
952 * wglFreeMemoryNV
954 * Provided by the WGL_NV_vertex_array_range extension.
956 void WINAPI wglFreeMemoryNV( void *pointer )
958 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
960 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
963 /***********************************************************************
964 * wglBindTexImageARB
966 * Provided by the WGL_ARB_render_texture extension.
968 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
970 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
971 BOOL ret;
973 if (!ptr) return FALSE;
974 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
975 release_handle_ptr( ptr );
976 return ret;
979 /***********************************************************************
980 * wglReleaseTexImageARB
982 * Provided by the WGL_ARB_render_texture extension.
984 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
986 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
987 BOOL ret;
989 if (!ptr) return FALSE;
990 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
991 release_handle_ptr( ptr );
992 return ret;
995 /***********************************************************************
996 * wglSetPbufferAttribARB
998 * Provided by the WGL_ARB_render_texture extension.
1000 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
1002 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1003 BOOL ret;
1005 if (!ptr) return FALSE;
1006 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
1007 release_handle_ptr( ptr );
1008 return ret;
1011 /***********************************************************************
1012 * wglChoosePixelFormatARB
1014 * Provided by the WGL_ARB_pixel_format extension.
1016 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
1017 UINT max, int *formats, UINT *count )
1019 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1021 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
1022 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
1025 /***********************************************************************
1026 * wglGetPixelFormatAttribivARB
1028 * Provided by the WGL_ARB_pixel_format extension.
1030 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
1031 int *values )
1033 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1035 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
1036 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
1039 /***********************************************************************
1040 * wglGetPixelFormatAttribfvARB
1042 * Provided by the WGL_ARB_pixel_format extension.
1044 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
1045 FLOAT *values )
1047 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1049 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
1050 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
1053 /***********************************************************************
1054 * wglCreatePbufferARB
1056 * Provided by the WGL_ARB_pbuffer extension.
1058 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1060 HPBUFFERARB ret = 0;
1061 struct wgl_pbuffer *pbuffer;
1062 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1064 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1065 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1066 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1067 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1068 return ret;
1071 /***********************************************************************
1072 * wglGetPbufferDCARB
1074 * Provided by the WGL_ARB_pbuffer extension.
1076 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1078 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1079 HDC ret;
1081 if (!ptr) return 0;
1082 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1083 release_handle_ptr( ptr );
1084 return ret;
1087 /***********************************************************************
1088 * wglReleasePbufferDCARB
1090 * Provided by the WGL_ARB_pbuffer extension.
1092 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1094 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1095 BOOL ret;
1097 if (!ptr) return FALSE;
1098 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1099 release_handle_ptr( ptr );
1100 return ret;
1103 /***********************************************************************
1104 * wglDestroyPbufferARB
1106 * Provided by the WGL_ARB_pbuffer extension.
1108 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1110 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1112 if (!ptr) return FALSE;
1113 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1114 free_handle_ptr( ptr );
1115 return TRUE;
1118 /***********************************************************************
1119 * wglQueryPbufferARB
1121 * Provided by the WGL_ARB_pbuffer extension.
1123 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1125 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1126 BOOL ret;
1128 if (!ptr) return FALSE;
1129 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1130 release_handle_ptr( ptr );
1131 return ret;
1134 /***********************************************************************
1135 * wglGetExtensionsStringARB
1137 * Provided by the WGL_ARB_extensions_string extension.
1139 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1141 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1143 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1144 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1147 /***********************************************************************
1148 * wglGetExtensionsStringEXT
1150 * Provided by the WGL_EXT_extensions_string extension.
1152 const char * WINAPI wglGetExtensionsStringEXT(void)
1154 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1156 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1157 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1160 /***********************************************************************
1161 * wglSwapIntervalEXT
1163 * Provided by the WGL_EXT_swap_control extension.
1165 BOOL WINAPI wglSwapIntervalEXT( int interval )
1167 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1169 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1170 return funcs->ext.p_wglSwapIntervalEXT( interval );
1173 /***********************************************************************
1174 * wglGetSwapIntervalEXT
1176 * Provided by the WGL_EXT_swap_control extension.
1178 int WINAPI wglGetSwapIntervalEXT(void)
1180 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1182 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1183 return funcs->ext.p_wglGetSwapIntervalEXT();
1186 /***********************************************************************
1187 * wglSetPixelFormatWINE
1189 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1191 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1193 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1195 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1196 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1199 /***********************************************************************
1200 * wglUseFontBitmaps_common
1202 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1204 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1205 GLYPHMETRICS gm;
1206 unsigned int glyph, size = 0;
1207 void *bitmap = NULL, *gl_bitmap = NULL;
1208 int org_alignment;
1209 BOOL ret = TRUE;
1211 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1212 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1214 for (glyph = first; glyph < first + count; glyph++) {
1215 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1216 unsigned int needed_size, height, width, width_int;
1218 if (unicode)
1219 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1220 else
1221 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1223 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1224 if (needed_size == GDI_ERROR) {
1225 ret = FALSE;
1226 break;
1229 if (needed_size > size) {
1230 size = needed_size;
1231 HeapFree(GetProcessHeap(), 0, bitmap);
1232 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1233 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1234 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1236 if (needed_size != 0) {
1237 if (unicode)
1238 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1239 size, bitmap, &identity) != GDI_ERROR);
1240 else
1241 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1242 size, bitmap, &identity) != GDI_ERROR);
1243 if (!ret) break;
1246 if (TRACE_ON(wgl)) {
1247 unsigned int bitmask;
1248 unsigned char *bitmap_ = bitmap;
1250 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1251 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1252 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1253 if (needed_size != 0) {
1254 TRACE(" - bitmap:\n");
1255 for (height = 0; height < gm.gmBlackBoxY; height++) {
1256 TRACE(" ");
1257 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1258 if (bitmask == 0) {
1259 bitmap_ += 1;
1260 bitmask = 0x80;
1262 if (*bitmap_ & bitmask)
1263 TRACE("*");
1264 else
1265 TRACE(" ");
1267 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1268 TRACE("\n");
1273 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1274 * glyph for it to be drawn properly.
1276 if (needed_size != 0) {
1277 width_int = (gm.gmBlackBoxX + 31) / 32;
1278 for (height = 0; height < gm.gmBlackBoxY; height++) {
1279 for (width = 0; width < width_int; width++) {
1280 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1281 ((int *) bitmap)[height * width_int + width];
1286 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1287 if (needed_size != 0) {
1288 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1289 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1290 gm.gmCellIncX, gm.gmCellIncY,
1291 gl_bitmap);
1292 } else {
1293 /* This is the case of 'empty' glyphs like the space character */
1294 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1296 funcs->gl.p_glEndList();
1299 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1300 HeapFree(GetProcessHeap(), 0, bitmap);
1301 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1302 return ret;
1305 /***********************************************************************
1306 * wglUseFontBitmapsA (OPENGL32.@)
1308 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1310 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1313 /***********************************************************************
1314 * wglUseFontBitmapsW (OPENGL32.@)
1316 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1318 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1321 /* FIXME: should probably have a glu.h header */
1323 typedef struct GLUtesselator GLUtesselator;
1324 typedef void (WINAPI *_GLUfuncptr)(void);
1326 #define GLU_TESS_BEGIN 100100
1327 #define GLU_TESS_VERTEX 100101
1328 #define GLU_TESS_END 100102
1330 static GLUtesselator * (WINAPI *pgluNewTess)(void);
1331 static void (WINAPI *pgluDeleteTess)(GLUtesselator *tess);
1332 static void (WINAPI *pgluTessNormal)(GLUtesselator *tess, GLdouble x, GLdouble y, GLdouble z);
1333 static void (WINAPI *pgluTessBeginPolygon)(GLUtesselator *tess, void *polygon_data);
1334 static void (WINAPI *pgluTessEndPolygon)(GLUtesselator *tess);
1335 static void (WINAPI *pgluTessCallback)(GLUtesselator *tess, GLenum which, _GLUfuncptr fn);
1336 static void (WINAPI *pgluTessBeginContour)(GLUtesselator *tess);
1337 static void (WINAPI *pgluTessEndContour)(GLUtesselator *tess);
1338 static void (WINAPI *pgluTessVertex)(GLUtesselator *tess, GLdouble *location, GLvoid* data);
1340 static HMODULE load_libglu(void)
1342 static const WCHAR glu32W[] = {'g','l','u','3','2','.','d','l','l',0};
1343 static BOOL already_loaded;
1344 static HMODULE module;
1346 if (already_loaded) return module;
1347 already_loaded = TRUE;
1349 TRACE("Trying to load GLU library\n");
1350 module = LoadLibraryW( glu32W );
1351 if (!module)
1353 WARN("Failed to load glu32\n");
1354 return NULL;
1356 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1357 LOAD_FUNCPTR(gluNewTess);
1358 LOAD_FUNCPTR(gluDeleteTess);
1359 LOAD_FUNCPTR(gluTessBeginContour);
1360 LOAD_FUNCPTR(gluTessNormal);
1361 LOAD_FUNCPTR(gluTessBeginPolygon);
1362 LOAD_FUNCPTR(gluTessCallback);
1363 LOAD_FUNCPTR(gluTessEndContour);
1364 LOAD_FUNCPTR(gluTessEndPolygon);
1365 LOAD_FUNCPTR(gluTessVertex);
1366 #undef LOAD_FUNCPTR
1367 return module;
1370 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1372 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1373 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1374 vertex[2] = 0.0;
1377 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1379 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1380 GLdouble *dbl = vertex;
1381 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1382 funcs->gl.p_glVertex3dv(vertex);
1385 static void WINAPI tess_callback_begin(GLenum which)
1387 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1388 TRACE("%d\n", which);
1389 funcs->gl.p_glBegin(which);
1392 static void WINAPI tess_callback_end(void)
1394 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1395 TRACE("\n");
1396 funcs->gl.p_glEnd();
1399 typedef struct _bezier_vector {
1400 GLdouble x;
1401 GLdouble y;
1402 } bezier_vector;
1404 static double bezier_deviation_squared(const bezier_vector *p)
1406 bezier_vector deviation;
1407 bezier_vector vertex;
1408 bezier_vector base;
1409 double base_length;
1410 double dot;
1412 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1413 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1415 base.x = p[2].x - p[0].x;
1416 base.y = p[2].y - p[0].y;
1418 base_length = sqrt(base.x*base.x + base.y*base.y);
1419 base.x /= base_length;
1420 base.y /= base_length;
1422 dot = base.x*vertex.x + base.y*vertex.y;
1423 dot = min(max(dot, 0.0), base_length);
1424 base.x *= dot;
1425 base.y *= dot;
1427 deviation.x = vertex.x-base.x;
1428 deviation.y = vertex.y-base.y;
1430 return deviation.x*deviation.x + deviation.y*deviation.y;
1433 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1435 bezier_vector first_curve[3];
1436 bezier_vector second_curve[3];
1437 bezier_vector vertex;
1438 int total_vertices;
1440 if(bezier_deviation_squared(p) <= deviation*deviation)
1442 if(points)
1443 *points = p[2];
1444 return 1;
1447 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1448 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1450 first_curve[0] = p[0];
1451 first_curve[1].x = (p[0].x + p[1].x)/2;
1452 first_curve[1].y = (p[0].y + p[1].y)/2;
1453 first_curve[2] = vertex;
1455 second_curve[0] = vertex;
1456 second_curve[1].x = (p[2].x + p[1].x)/2;
1457 second_curve[1].y = (p[2].y + p[1].y)/2;
1458 second_curve[2] = p[2];
1460 total_vertices = bezier_approximate(first_curve, points, deviation);
1461 if(points)
1462 points += total_vertices;
1463 total_vertices += bezier_approximate(second_curve, points, deviation);
1464 return total_vertices;
1467 /***********************************************************************
1468 * wglUseFontOutlines_common
1470 static BOOL wglUseFontOutlines_common(HDC hdc,
1471 DWORD first,
1472 DWORD count,
1473 DWORD listBase,
1474 FLOAT deviation,
1475 FLOAT extrusion,
1476 int format,
1477 LPGLYPHMETRICSFLOAT lpgmf,
1478 BOOL unicode)
1480 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1481 UINT glyph;
1482 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
1483 GLUtesselator *tess = NULL;
1484 LOGFONTW lf;
1485 HFONT old_font, unscaled_font;
1486 UINT em_size = 1024;
1487 RECT rc;
1489 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1490 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1492 if(deviation <= 0.0)
1493 deviation = 1.0/em_size;
1495 if(format == WGL_FONT_POLYGONS)
1497 if (!load_libglu())
1499 ERR("glu32 is required for this function but isn't available\n");
1500 return FALSE;
1503 tess = pgluNewTess();
1504 if(!tess) return FALSE;
1505 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
1506 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
1507 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1510 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1511 rc.left = rc.right = rc.bottom = 0;
1512 rc.top = em_size;
1513 DPtoLP(hdc, (POINT*)&rc, 2);
1514 lf.lfHeight = -abs(rc.top - rc.bottom);
1515 lf.lfOrientation = lf.lfEscapement = 0;
1516 unscaled_font = CreateFontIndirectW(&lf);
1517 old_font = SelectObject(hdc, unscaled_font);
1519 for (glyph = first; glyph < first + count; glyph++)
1521 DWORD needed;
1522 GLYPHMETRICS gm;
1523 BYTE *buf;
1524 TTPOLYGONHEADER *pph;
1525 TTPOLYCURVE *ppc;
1526 GLdouble *vertices = NULL;
1527 int vertex_total = -1;
1529 if(unicode)
1530 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1531 else
1532 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1534 if(needed == GDI_ERROR)
1535 goto error;
1537 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1539 if(unicode)
1540 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1541 else
1542 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1544 TRACE("glyph %d\n", glyph);
1546 if(lpgmf)
1548 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1549 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1550 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1551 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1552 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1553 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1555 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1556 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1557 lpgmf++;
1560 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1561 funcs->gl.p_glFrontFace(GL_CCW);
1562 if(format == WGL_FONT_POLYGONS)
1564 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1565 pgluTessNormal(tess, 0, 0, 1);
1566 pgluTessBeginPolygon(tess, NULL);
1569 while(!vertices)
1571 if(vertex_total != -1)
1572 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1573 vertex_total = 0;
1575 pph = (TTPOLYGONHEADER*)buf;
1576 while((BYTE*)pph < buf + needed)
1578 GLdouble previous[3];
1579 fixed_to_double(pph->pfxStart, em_size, previous);
1581 if(vertices)
1582 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1584 if(format == WGL_FONT_POLYGONS)
1585 pgluTessBeginContour(tess);
1586 else
1587 funcs->gl.p_glBegin(GL_LINE_LOOP);
1589 if(vertices)
1591 fixed_to_double(pph->pfxStart, em_size, vertices);
1592 if(format == WGL_FONT_POLYGONS)
1593 pgluTessVertex(tess, vertices, vertices);
1594 else
1595 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1596 vertices += 3;
1598 vertex_total++;
1600 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1601 while((char*)ppc < (char*)pph + pph->cb)
1603 int i, j;
1604 int num;
1606 switch(ppc->wType) {
1607 case TT_PRIM_LINE:
1608 for(i = 0; i < ppc->cpfx; i++)
1610 if(vertices)
1612 TRACE("\t\tline to %d, %d\n",
1613 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1614 fixed_to_double(ppc->apfx[i], em_size, vertices);
1615 if(format == WGL_FONT_POLYGONS)
1616 pgluTessVertex(tess, vertices, vertices);
1617 else
1618 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1619 vertices += 3;
1621 fixed_to_double(ppc->apfx[i], em_size, previous);
1622 vertex_total++;
1624 break;
1626 case TT_PRIM_QSPLINE:
1627 for(i = 0; i < ppc->cpfx-1; i++)
1629 bezier_vector curve[3];
1630 bezier_vector *points;
1631 GLdouble curve_vertex[3];
1633 if(vertices)
1634 TRACE("\t\tcurve %d,%d %d,%d\n",
1635 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1636 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1638 curve[0].x = previous[0];
1639 curve[0].y = previous[1];
1640 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1641 curve[1].x = curve_vertex[0];
1642 curve[1].y = curve_vertex[1];
1643 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1644 curve[2].x = curve_vertex[0];
1645 curve[2].y = curve_vertex[1];
1646 if(i < ppc->cpfx-2)
1648 curve[2].x = (curve[1].x + curve[2].x)/2;
1649 curve[2].y = (curve[1].y + curve[2].y)/2;
1651 num = bezier_approximate(curve, NULL, deviation);
1652 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1653 num = bezier_approximate(curve, points, deviation);
1654 vertex_total += num;
1655 if(vertices)
1657 for(j=0; j<num; j++)
1659 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1660 vertices[0] = points[j].x;
1661 vertices[1] = points[j].y;
1662 vertices[2] = 0.0;
1663 if(format == WGL_FONT_POLYGONS)
1664 pgluTessVertex(tess, vertices, vertices);
1665 else
1666 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1667 vertices += 3;
1670 HeapFree(GetProcessHeap(), 0, points);
1671 previous[0] = curve[2].x;
1672 previous[1] = curve[2].y;
1674 break;
1675 default:
1676 ERR("\t\tcurve type = %d\n", ppc->wType);
1677 if(format == WGL_FONT_POLYGONS)
1678 pgluTessEndContour(tess);
1679 else
1680 funcs->gl.p_glEnd();
1681 goto error_in_list;
1684 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1685 (ppc->cpfx - 1) * sizeof(POINTFX));
1687 if(format == WGL_FONT_POLYGONS)
1688 pgluTessEndContour(tess);
1689 else
1690 funcs->gl.p_glEnd();
1691 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1695 error_in_list:
1696 if(format == WGL_FONT_POLYGONS)
1697 pgluTessEndPolygon(tess);
1698 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1699 funcs->gl.p_glEndList();
1700 HeapFree(GetProcessHeap(), 0, buf);
1701 HeapFree(GetProcessHeap(), 0, vertices);
1704 error:
1705 DeleteObject(SelectObject(hdc, old_font));
1706 if(format == WGL_FONT_POLYGONS)
1707 pgluDeleteTess(tess);
1708 return TRUE;
1712 /***********************************************************************
1713 * wglUseFontOutlinesA (OPENGL32.@)
1715 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1716 DWORD first,
1717 DWORD count,
1718 DWORD listBase,
1719 FLOAT deviation,
1720 FLOAT extrusion,
1721 int format,
1722 LPGLYPHMETRICSFLOAT lpgmf)
1724 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1727 /***********************************************************************
1728 * wglUseFontOutlinesW (OPENGL32.@)
1730 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1731 DWORD first,
1732 DWORD count,
1733 DWORD listBase,
1734 FLOAT deviation,
1735 FLOAT extrusion,
1736 int format,
1737 LPGLYPHMETRICSFLOAT lpgmf)
1739 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1742 /***********************************************************************
1743 * glDebugEntry (OPENGL32.@)
1745 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1747 return 0;
1750 static GLubyte *filter_extensions_list(const char *extensions, const char *disabled)
1752 char *p, *str = NULL;
1753 const char *end;
1755 p = str = HeapAlloc(GetProcessHeap(), 0, strlen(extensions) + 2);
1756 if (!str)
1757 return NULL;
1758 for (;;)
1760 while (*extensions == ' ')
1761 extensions++;
1762 if (!*extensions)
1763 break;
1764 if (!(end = strchr(extensions, ' ')))
1765 end = extensions + strlen(extensions);
1766 memcpy(p, extensions, end - extensions);
1767 p[end - extensions] = 0;
1768 if (!has_extension(disabled, p, strlen(p)))
1770 TRACE("++ %s\n", p);
1771 p += end - extensions;
1772 *p++ = ' ';
1774 else
1776 TRACE("-- %s (disabled by config)\n", p);
1778 extensions = end;
1780 *p = 0;
1781 return (GLubyte *)str;
1784 static GLuint *filter_extensions_index(const char *disabled)
1786 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1787 const char *ext, *end, *gl_ext;
1788 GLuint *disabled_exts = NULL, *new_disabled_exts;
1789 unsigned int i = 0, j, disabled_size;
1790 GLint extensions_count;
1792 if (!funcs->ext.p_glGetStringi)
1794 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
1796 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
1797 if (!funcs->ext.p_glGetStringi)
1798 return NULL;
1801 funcs->gl.p_glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
1802 disabled_size = 2;
1803 disabled_exts = HeapAlloc(GetProcessHeap(), 0, disabled_size * sizeof(*disabled_exts));
1804 if (!disabled_exts)
1805 return NULL;
1806 for (j = 0; j < extensions_count; ++j)
1808 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, j);
1809 ext = disabled;
1810 for (;;)
1812 while (*ext == ' ')
1813 ext++;
1814 if (!*ext)
1816 TRACE("++ %s\n", gl_ext);
1817 break;
1819 if (!(end = strchr(ext, ' ')))
1820 end = ext + strlen(ext);
1822 if (!strncmp(gl_ext, ext, end - ext) && !gl_ext[end - ext])
1824 if (i + 1 == disabled_size)
1826 disabled_size *= 2;
1827 new_disabled_exts = HeapReAlloc(GetProcessHeap(), 0, disabled_exts,
1828 disabled_size * sizeof(*disabled_exts));
1829 if (!new_disabled_exts)
1831 disabled_exts[i] = ~0u;
1832 return disabled_exts;
1834 disabled_exts = new_disabled_exts;
1836 TRACE("-- %s (disabled by config)\n", gl_ext);
1837 disabled_exts[i++] = j;
1838 break;
1840 ext = end;
1843 disabled_exts[i] = ~0u;
1844 return disabled_exts;
1847 /* build the extension string by filtering out the disabled extensions */
1848 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts)
1850 static const char *disabled;
1852 TRACE( "GL_EXTENSIONS:\n" );
1854 if (!disabled)
1856 HKEY hkey;
1857 DWORD size;
1858 char *str = NULL;
1860 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1861 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1863 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1865 str = HeapAlloc( GetProcessHeap(), 0, size );
1866 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1868 RegCloseKey( hkey );
1870 if (str)
1872 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1873 HeapFree( GetProcessHeap(), 0, str );
1875 else disabled = "";
1878 if (!disabled[0])
1879 return FALSE;
1881 if (extensions && !*exts_list)
1882 *exts_list = filter_extensions_list(extensions, disabled);
1884 if (!*disabled_exts)
1885 *disabled_exts = filter_extensions_index(disabled);
1887 return (exts_list && *exts_list) || *disabled_exts;
1890 /***********************************************************************
1891 * glGetString (OPENGL32.@)
1893 const GLubyte * WINAPI glGetString( GLenum name )
1895 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1896 const GLubyte *ret = funcs->gl.p_glGetString( name );
1898 if (name == GL_EXTENSIONS && ret)
1900 struct wgl_handle *ptr = get_current_context_ptr();
1901 if (ptr->u.context->extensions ||
1902 filter_extensions((const char *)ret, &ptr->u.context->extensions, &ptr->u.context->disabled_exts))
1903 ret = ptr->u.context->extensions;
1905 return ret;
1908 /***********************************************************************
1909 * OpenGL initialisation routine
1911 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1913 switch(reason)
1915 case DLL_PROCESS_ATTACH:
1916 NtCurrentTeb()->glTable = &null_opengl_funcs;
1917 break;
1918 case DLL_THREAD_ATTACH:
1919 NtCurrentTeb()->glTable = &null_opengl_funcs;
1920 break;
1922 return TRUE;