opengl32: Don't fail with both PFD_DOUBLEBUFFER_DONTCARE and PFD_STEREO_DONTCARE.
[wine.git] / dlls / opengl32 / wgl.c
blobdafafb665075a379939bf4e303314f3873798fda
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/glu.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
45 WINE_DECLARE_DEBUG_CHANNEL(fps);
47 extern struct opengl_funcs null_opengl_funcs;
49 /* handle management */
51 #define MAX_WGL_HANDLES 1024
53 enum wgl_handle_type
55 HANDLE_PBUFFER = 0 << 12,
56 HANDLE_CONTEXT = 1 << 12,
57 HANDLE_CONTEXT_V3 = 3 << 12,
58 HANDLE_TYPE_MASK = 15 << 12
61 struct opengl_context
63 DWORD tid; /* thread that the context is current in */
64 HDC draw_dc; /* current drawing DC */
65 HDC read_dc; /* current reading DC */
66 GLubyte *extensions; /* extension string */
67 GLuint *disabled_exts; /* indices of disabled extensions */
68 struct wgl_context *drv_ctx; /* driver context */
71 struct wgl_handle
73 UINT handle;
74 struct opengl_funcs *funcs;
75 union
77 struct opengl_context *context; /* for HANDLE_CONTEXT */
78 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
79 struct wgl_handle *next; /* for free handles */
80 } u;
83 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
84 static struct wgl_handle *next_free;
85 static unsigned int handle_count;
87 static CRITICAL_SECTION wgl_section;
88 static CRITICAL_SECTION_DEBUG critsect_debug =
90 0, 0, &wgl_section,
91 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
92 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
94 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
96 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
98 static inline struct opengl_funcs *get_dc_funcs( HDC hdc )
100 struct opengl_funcs *funcs = __wine_get_wgl_driver( hdc, WINE_WGL_DRIVER_VERSION );
101 if (funcs == (void *)-1) funcs = &null_opengl_funcs;
102 return funcs;
105 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
107 WORD generation = HIWORD( ptr->handle ) + 1;
108 if (!generation) generation++;
109 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
110 return ULongToHandle( ptr->handle );
113 /* the current context is assumed valid and doesn't need locking */
114 static inline struct wgl_handle *get_current_context_ptr(void)
116 if (!NtCurrentTeb()->glCurrentRC) return NULL;
117 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
120 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
122 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
124 EnterCriticalSection( &wgl_section );
125 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
126 return &wgl_handles[index];
128 LeaveCriticalSection( &wgl_section );
129 SetLastError( ERROR_INVALID_HANDLE );
130 return NULL;
133 static void release_handle_ptr( struct wgl_handle *ptr )
135 if (ptr) LeaveCriticalSection( &wgl_section );
138 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
140 HANDLE handle = 0;
141 struct wgl_handle *ptr = NULL;
143 EnterCriticalSection( &wgl_section );
144 if ((ptr = next_free))
145 next_free = next_free->u.next;
146 else if (handle_count < MAX_WGL_HANDLES)
147 ptr = &wgl_handles[handle_count++];
149 if (ptr)
151 ptr->funcs = funcs;
152 ptr->u.context = user_ptr;
153 handle = next_handle( ptr, type );
155 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
156 LeaveCriticalSection( &wgl_section );
157 return handle;
160 static void free_handle_ptr( struct wgl_handle *ptr )
162 ptr->handle |= 0xffff;
163 ptr->u.next = next_free;
164 ptr->funcs = NULL;
165 next_free = ptr;
166 LeaveCriticalSection( &wgl_section );
169 static inline enum wgl_handle_type get_current_context_type(void)
171 if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
172 return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
175 /***********************************************************************
176 * wglCopyContext (OPENGL32.@)
178 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
180 struct wgl_handle *src, *dst;
181 BOOL ret = FALSE;
183 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
184 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
186 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
187 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
188 dst->u.context->drv_ctx, mask );
190 release_handle_ptr( dst );
191 release_handle_ptr( src );
192 return ret;
195 /***********************************************************************
196 * wglDeleteContext (OPENGL32.@)
198 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
200 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
202 if (!ptr) return FALSE;
204 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
206 SetLastError( ERROR_BUSY );
207 release_handle_ptr( ptr );
208 return FALSE;
210 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
211 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
212 HeapFree( GetProcessHeap(), 0, ptr->u.context->disabled_exts );
213 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
214 HeapFree( GetProcessHeap(), 0, ptr->u.context );
215 free_handle_ptr( ptr );
216 return TRUE;
219 /***********************************************************************
220 * wglMakeCurrent (OPENGL32.@)
222 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
224 BOOL ret = TRUE;
225 struct wgl_handle *ptr, *prev = get_current_context_ptr();
227 if (hglrc)
229 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
230 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
232 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
233 if (ret)
235 if (prev) prev->u.context->tid = 0;
236 ptr->u.context->tid = GetCurrentThreadId();
237 ptr->u.context->draw_dc = hdc;
238 ptr->u.context->read_dc = hdc;
239 NtCurrentTeb()->glCurrentRC = hglrc;
240 NtCurrentTeb()->glTable = ptr->funcs;
243 else
245 SetLastError( ERROR_BUSY );
246 ret = FALSE;
248 release_handle_ptr( ptr );
250 else if (prev)
252 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
253 prev->u.context->tid = 0;
254 NtCurrentTeb()->glCurrentRC = 0;
255 NtCurrentTeb()->glTable = &null_opengl_funcs;
257 else if (!hdc)
259 SetLastError( ERROR_INVALID_HANDLE );
260 ret = FALSE;
262 return ret;
265 /***********************************************************************
266 * wglCreateContextAttribsARB
268 * Provided by the WGL_ARB_create_context extension.
270 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
272 HGLRC ret = 0;
273 struct wgl_context *drv_ctx;
274 struct wgl_handle *share_ptr = NULL;
275 struct opengl_context *context;
276 struct opengl_funcs *funcs = get_dc_funcs( hdc );
278 if (!funcs)
280 SetLastError( ERROR_DC_NOT_FOUND );
281 return 0;
283 if (!funcs->ext.p_wglCreateContextAttribsARB) return 0;
284 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT )))
286 SetLastError( ERROR_INVALID_OPERATION );
287 return 0;
289 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
290 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
292 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
294 enum wgl_handle_type type = HANDLE_CONTEXT;
296 if (attribs)
298 while (*attribs)
300 if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
302 if (attribs[1] >= 3)
303 type = HANDLE_CONTEXT_V3;
304 break;
306 attribs += 2;
310 context->drv_ctx = drv_ctx;
311 if (!(ret = alloc_handle( type, funcs, context )))
312 HeapFree( GetProcessHeap(), 0, context );
314 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
316 release_handle_ptr( share_ptr );
317 return ret;
321 /***********************************************************************
322 * wglMakeContextCurrentARB
324 * Provided by the WGL_ARB_make_current_read extension.
326 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
328 BOOL ret = TRUE;
329 struct wgl_handle *ptr, *prev = get_current_context_ptr();
331 if (hglrc)
333 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
334 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
336 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
337 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
338 ptr->u.context->drv_ctx ));
339 if (ret)
341 if (prev) prev->u.context->tid = 0;
342 ptr->u.context->tid = GetCurrentThreadId();
343 ptr->u.context->draw_dc = draw_hdc;
344 ptr->u.context->read_dc = read_hdc;
345 NtCurrentTeb()->glCurrentRC = hglrc;
346 NtCurrentTeb()->glTable = ptr->funcs;
349 else
351 SetLastError( ERROR_BUSY );
352 ret = FALSE;
354 release_handle_ptr( ptr );
356 else if (prev)
358 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
359 prev->u.context->tid = 0;
360 NtCurrentTeb()->glCurrentRC = 0;
361 NtCurrentTeb()->glTable = &null_opengl_funcs;
363 return ret;
366 /***********************************************************************
367 * wglGetCurrentReadDCARB
369 * Provided by the WGL_ARB_make_current_read extension.
371 HDC WINAPI wglGetCurrentReadDCARB(void)
373 struct wgl_handle *ptr = get_current_context_ptr();
375 if (!ptr) return 0;
376 return ptr->u.context->read_dc;
379 /***********************************************************************
380 * wglShareLists (OPENGL32.@)
382 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
384 BOOL ret = FALSE;
385 struct wgl_handle *src, *dst;
387 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
388 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
390 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
391 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
393 release_handle_ptr( dst );
394 release_handle_ptr( src );
395 return ret;
398 /***********************************************************************
399 * wglGetCurrentDC (OPENGL32.@)
401 HDC WINAPI wglGetCurrentDC(void)
403 struct wgl_handle *ptr = get_current_context_ptr();
405 if (!ptr) return 0;
406 return ptr->u.context->draw_dc;
409 /***********************************************************************
410 * wglCreateContext (OPENGL32.@)
412 HGLRC WINAPI wglCreateContext(HDC hdc)
414 HGLRC ret = 0;
415 struct wgl_context *drv_ctx;
416 struct opengl_context *context;
417 struct opengl_funcs *funcs = get_dc_funcs( hdc );
419 if (!funcs) return 0;
420 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
421 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
423 context->drv_ctx = drv_ctx;
424 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
425 HeapFree( GetProcessHeap(), 0, context );
427 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
428 return ret;
431 /***********************************************************************
432 * wglGetCurrentContext (OPENGL32.@)
434 HGLRC WINAPI wglGetCurrentContext(void)
436 return NtCurrentTeb()->glCurrentRC;
439 /***********************************************************************
440 * wglDescribePixelFormat (OPENGL32.@)
442 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
444 struct opengl_funcs *funcs = get_dc_funcs( hdc );
445 if (!funcs) return 0;
446 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
449 /***********************************************************************
450 * wglChoosePixelFormat (OPENGL32.@)
452 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
454 PIXELFORMATDESCRIPTOR format, best;
455 int i, count, best_format;
456 int bestDBuffer = -1, bestStereo = -1;
458 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
459 "accum %u depth %u stencil %u aux %u\n",
460 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
461 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
462 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
464 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
465 if (!count) return 0;
467 best_format = 0;
468 best.dwFlags = 0;
469 best.cAlphaBits = -1;
470 best.cColorBits = -1;
471 best.cDepthBits = -1;
472 best.cStencilBits = -1;
473 best.cAuxBuffers = -1;
475 for (i = 1; i <= count; i++)
477 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
479 if (ppfd->iPixelType != format.iPixelType)
481 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
482 continue;
485 /* only use bitmap capable for formats for bitmap rendering */
486 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
488 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
489 continue;
492 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
493 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
494 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
495 * formats without the given flag set.
496 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
497 * has indicated that a format without stereo is returned when stereo is unavailable.
498 * So in case PFD_STEREO is set, formats that support it should have priority above formats
499 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
501 * To summarize the following is most likely the correct behavior:
502 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
503 * stereo set -> prefer stereo formats, but also accept non-stereo formats
504 * stereo don't care -> it doesn't matter whether we get stereo or not
506 * In Wine we will treat non-stereo the same way as don't care because it makes
507 * format selection even more complicated and second drivers with Stereo advertise
508 * each format twice anyway.
511 /* Doublebuffer, see the comments above */
512 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
514 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
515 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
516 goto found;
518 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
520 else if (!best_format)
521 goto found;
523 /* Stereo, see the comments above. */
524 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
526 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
527 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
528 goto found;
530 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
532 else if (!best_format)
533 goto found;
535 /* Below we will do a number of checks to select the 'best' pixelformat.
536 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
537 * The code works by trying to match the most important options as close as possible.
538 * When a reasonable format is found, we will try to match more options.
539 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
540 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
541 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
543 if (ppfd->cColorBits)
545 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
546 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
547 goto found;
549 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
551 TRACE( "color mismatch for iPixelFormat=%d\n", i );
552 continue;
555 if (ppfd->cAlphaBits)
557 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
558 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
559 goto found;
561 if (best.cAlphaBits != format.cAlphaBits)
563 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
564 continue;
567 if (ppfd->cDepthBits)
569 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
570 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
571 goto found;
573 if (best.cDepthBits != format.cDepthBits)
575 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
576 continue;
579 if (ppfd->cStencilBits)
581 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
582 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
583 goto found;
585 if (best.cStencilBits != format.cStencilBits)
587 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
588 continue;
591 if (ppfd->cAuxBuffers)
593 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
594 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
595 goto found;
597 if (best.cAuxBuffers != format.cAuxBuffers)
599 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
600 continue;
603 continue;
605 found:
606 best_format = i;
607 best = format;
608 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
609 bestStereo = format.dwFlags & PFD_STEREO;
612 TRACE( "returning %u\n", best_format );
613 return best_format;
616 /***********************************************************************
617 * wglGetPixelFormat (OPENGL32.@)
619 INT WINAPI wglGetPixelFormat(HDC hdc)
621 struct opengl_funcs *funcs = get_dc_funcs( hdc );
622 if (!funcs) return 0;
623 return funcs->wgl.p_wglGetPixelFormat( hdc );
626 /***********************************************************************
627 * wglSetPixelFormat(OPENGL32.@)
629 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
631 struct opengl_funcs *funcs = get_dc_funcs( hdc );
632 if (!funcs) return FALSE;
633 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
636 /***********************************************************************
637 * wglSwapBuffers (OPENGL32.@)
639 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
641 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
643 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
644 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
646 if (TRACE_ON(fps))
648 static long prev_time, start_time;
649 static unsigned long frames, frames_total;
651 DWORD time = GetTickCount();
652 frames++;
653 frames_total++;
654 /* every 1.5 seconds */
655 if (time - prev_time > 1500)
657 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
658 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
659 prev_time = time;
660 frames = 0;
661 if (start_time == 0) start_time = time;
664 return TRUE;
667 /***********************************************************************
668 * wglCreateLayerContext (OPENGL32.@)
670 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
671 int iLayerPlane) {
672 TRACE("(%p,%d)\n", hdc, iLayerPlane);
674 if (iLayerPlane == 0) {
675 return wglCreateContext(hdc);
677 FIXME("no handler for layer %d\n", iLayerPlane);
679 return NULL;
682 /***********************************************************************
683 * wglDescribeLayerPlane (OPENGL32.@)
685 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
686 int iPixelFormat,
687 int iLayerPlane,
688 UINT nBytes,
689 LPLAYERPLANEDESCRIPTOR plpd) {
690 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
692 return FALSE;
695 /***********************************************************************
696 * wglGetLayerPaletteEntries (OPENGL32.@)
698 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
699 int iLayerPlane,
700 int iStart,
701 int cEntries,
702 const COLORREF *pcr) {
703 FIXME("(): stub!\n");
705 return 0;
708 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
710 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
712 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
714 TRACE("(%d, %p)\n", pname, data);
715 if (pname == GL_NUM_EXTENSIONS)
717 struct wgl_handle *ptr = get_current_context_ptr();
719 if (ptr->u.context->disabled_exts ||
720 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
722 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
723 GLint count, disabled_count = 0;
725 funcs->gl.p_glGetIntegerv(pname, &count);
726 while (*disabled_exts++ != ~0u)
727 disabled_count++;
728 *data = count - disabled_count;
729 return;
732 funcs->gl.p_glGetIntegerv(pname, data);
735 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
737 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
739 TRACE("(%d, %d)\n", name, index);
740 if (!funcs->ext.p_glGetStringi)
742 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
744 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
747 if (name == GL_EXTENSIONS)
749 struct wgl_handle *ptr = get_current_context_ptr();
751 if (ptr->u.context->disabled_exts ||
752 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
754 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
755 unsigned int disabled_count = 0;
757 while (index + disabled_count >= *disabled_exts++)
758 disabled_count++;
759 return funcs->ext.p_glGetStringi(name, index + disabled_count);
762 return funcs->ext.p_glGetStringi(name, index);
765 /* check if the extension is present in the list */
766 static BOOL has_extension( const char *list, const char *ext, size_t len )
768 if (!list)
770 const char *gl_ext;
771 unsigned int i;
772 GLint extensions_count;
774 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
775 for (i = 0; i < extensions_count; ++i)
777 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
778 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
779 return TRUE;
781 return FALSE;
784 while (list)
786 while (*list == ' ') list++;
787 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
788 list = strchr( list, ' ' );
790 return FALSE;
793 static int compar(const void *elt_a, const void *elt_b) {
794 return strcmp(((const OpenGL_extension *) elt_a)->name,
795 ((const OpenGL_extension *) elt_b)->name);
798 /* Check if a GL extension is supported */
799 static BOOL is_extension_supported(const char* extension)
801 enum wgl_handle_type type = get_current_context_type();
802 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
803 const char *gl_ext_string = NULL;
804 size_t len;
806 TRACE("Checking for extension '%s'\n", extension);
808 if (type == HANDLE_CONTEXT)
810 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
811 if (!gl_ext_string)
813 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
814 return FALSE;
818 /* We use the GetProcAddress function from the display driver to retrieve function pointers
819 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
820 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
821 * require the function to return NULL when an extension isn't found. For this reason we check
822 * if the OpenGL extension required for the function we are looking up is supported. */
824 while ((len = strcspn(extension, " ")) != 0)
826 /* Check if the extension is part of the GL extension string to see if it is supported. */
827 if (has_extension(gl_ext_string, extension, len))
828 return TRUE;
830 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
831 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
832 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
833 * Check if we are searching for a core GL function */
834 if(strncmp(extension, "GL_VERSION_", 11) == 0)
836 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
837 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
839 if(!gl_version) {
840 ERR("No OpenGL version found!\n");
841 return FALSE;
844 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
845 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
846 if( (gl_version[0] > version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
847 return TRUE;
849 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]);
852 if (extension[len] == ' ') len++;
853 extension += len;
856 return FALSE;
859 /***********************************************************************
860 * wglGetProcAddress (OPENGL32.@)
862 PROC WINAPI wglGetProcAddress( LPCSTR name )
864 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
865 void **func_ptr;
866 OpenGL_extension ext;
867 const OpenGL_extension *ext_ret;
869 if (!name) return NULL;
871 /* Without an active context opengl32 doesn't know to what
872 * driver it has to dispatch wglGetProcAddress.
874 if (!get_current_context_ptr())
876 WARN("No active WGL context found\n");
877 return NULL;
880 ext.name = name;
881 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
882 if (!ext_ret)
884 WARN("Function %s unknown\n", name);
885 return NULL;
888 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
889 if (!*func_ptr)
891 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
893 if (!is_extension_supported(ext_ret->extension))
895 unsigned int i;
896 static const struct { const char *name, *alt; } alternatives[] =
898 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
899 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
902 for (i = 0; i < sizeof(alternatives)/sizeof(alternatives[0]); i++)
904 if (strcmp( name, alternatives[i].name )) continue;
905 WARN("Extension %s required for %s not supported, trying %s\n",
906 ext_ret->extension, name, alternatives[i].alt );
907 return wglGetProcAddress( alternatives[i].alt );
909 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
910 return NULL;
913 if (driver_func == NULL)
915 WARN("Function %s not supported by driver\n", name);
916 return NULL;
918 *func_ptr = driver_func;
921 TRACE("returning %s -> %p\n", name, ext_ret->func);
922 return ext_ret->func;
925 /***********************************************************************
926 * wglRealizeLayerPalette (OPENGL32.@)
928 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
929 int iLayerPlane,
930 BOOL bRealize) {
931 FIXME("()\n");
933 return FALSE;
936 /***********************************************************************
937 * wglSetLayerPaletteEntries (OPENGL32.@)
939 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
940 int iLayerPlane,
941 int iStart,
942 int cEntries,
943 const COLORREF *pcr) {
944 FIXME("(): stub!\n");
946 return 0;
949 /***********************************************************************
950 * wglSwapLayerBuffers (OPENGL32.@)
952 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
953 UINT fuPlanes) {
954 TRACE("(%p, %08x)\n", hdc, fuPlanes);
956 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
957 if (!wglSwapBuffers( hdc )) return FALSE;
958 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
961 if (fuPlanes) {
962 WARN("Following layers unhandled: %08x\n", fuPlanes);
965 return TRUE;
968 /***********************************************************************
969 * wglAllocateMemoryNV
971 * Provided by the WGL_NV_vertex_array_range extension.
973 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
975 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
977 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
978 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
981 /***********************************************************************
982 * wglFreeMemoryNV
984 * Provided by the WGL_NV_vertex_array_range extension.
986 void WINAPI wglFreeMemoryNV( void *pointer )
988 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
990 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
993 /***********************************************************************
994 * wglBindTexImageARB
996 * Provided by the WGL_ARB_render_texture extension.
998 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
1000 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1001 BOOL ret;
1003 if (!ptr) return FALSE;
1004 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
1005 release_handle_ptr( ptr );
1006 return ret;
1009 /***********************************************************************
1010 * wglReleaseTexImageARB
1012 * Provided by the WGL_ARB_render_texture extension.
1014 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
1016 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1017 BOOL ret;
1019 if (!ptr) return FALSE;
1020 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
1021 release_handle_ptr( ptr );
1022 return ret;
1025 /***********************************************************************
1026 * wglSetPbufferAttribARB
1028 * Provided by the WGL_ARB_render_texture extension.
1030 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
1032 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1033 BOOL ret;
1035 if (!ptr) return FALSE;
1036 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
1037 release_handle_ptr( ptr );
1038 return ret;
1041 /***********************************************************************
1042 * wglChoosePixelFormatARB
1044 * Provided by the WGL_ARB_pixel_format extension.
1046 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
1047 UINT max, int *formats, UINT *count )
1049 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1051 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
1052 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
1055 /***********************************************************************
1056 * wglGetPixelFormatAttribivARB
1058 * Provided by the WGL_ARB_pixel_format extension.
1060 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
1061 int *values )
1063 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1065 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
1066 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
1069 /***********************************************************************
1070 * wglGetPixelFormatAttribfvARB
1072 * Provided by the WGL_ARB_pixel_format extension.
1074 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
1075 FLOAT *values )
1077 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1079 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
1080 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
1083 /***********************************************************************
1084 * wglCreatePbufferARB
1086 * Provided by the WGL_ARB_pbuffer extension.
1088 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1090 HPBUFFERARB ret;
1091 struct wgl_pbuffer *pbuffer;
1092 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1094 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1095 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1096 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1097 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1098 return ret;
1101 /***********************************************************************
1102 * wglGetPbufferDCARB
1104 * Provided by the WGL_ARB_pbuffer extension.
1106 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1108 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1109 HDC ret;
1111 if (!ptr) return 0;
1112 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1113 release_handle_ptr( ptr );
1114 return ret;
1117 /***********************************************************************
1118 * wglReleasePbufferDCARB
1120 * Provided by the WGL_ARB_pbuffer extension.
1122 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1124 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1125 BOOL ret;
1127 if (!ptr) return FALSE;
1128 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1129 release_handle_ptr( ptr );
1130 return ret;
1133 /***********************************************************************
1134 * wglDestroyPbufferARB
1136 * Provided by the WGL_ARB_pbuffer extension.
1138 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1140 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1142 if (!ptr) return FALSE;
1143 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1144 free_handle_ptr( ptr );
1145 return TRUE;
1148 /***********************************************************************
1149 * wglQueryPbufferARB
1151 * Provided by the WGL_ARB_pbuffer extension.
1153 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1155 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1156 BOOL ret;
1158 if (!ptr) return FALSE;
1159 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1160 release_handle_ptr( ptr );
1161 return ret;
1164 /***********************************************************************
1165 * wglGetExtensionsStringARB
1167 * Provided by the WGL_ARB_extensions_string extension.
1169 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1171 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1173 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1174 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1177 /***********************************************************************
1178 * wglGetExtensionsStringEXT
1180 * Provided by the WGL_EXT_extensions_string extension.
1182 const char * WINAPI wglGetExtensionsStringEXT(void)
1184 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1186 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1187 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1190 /***********************************************************************
1191 * wglSwapIntervalEXT
1193 * Provided by the WGL_EXT_swap_control extension.
1195 BOOL WINAPI wglSwapIntervalEXT( int interval )
1197 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1199 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1200 return funcs->ext.p_wglSwapIntervalEXT( interval );
1203 /***********************************************************************
1204 * wglGetSwapIntervalEXT
1206 * Provided by the WGL_EXT_swap_control extension.
1208 int WINAPI wglGetSwapIntervalEXT(void)
1210 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1212 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1213 return funcs->ext.p_wglGetSwapIntervalEXT();
1216 /***********************************************************************
1217 * wglSetPixelFormatWINE
1219 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1221 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1223 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1225 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1226 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1229 /***********************************************************************
1230 * wglQueryCurrentRendererIntegerWINE
1232 * Provided by the WGL_WINE_query_renderer extension.
1234 BOOL WINAPI wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
1236 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1238 if (!funcs->ext.p_wglQueryCurrentRendererIntegerWINE) return FALSE;
1239 return funcs->ext.p_wglQueryCurrentRendererIntegerWINE( attribute, value );
1242 /***********************************************************************
1243 * wglQueryCurrentRendererStringWINE
1245 * Provided by the WGL_WINE_query_renderer extension.
1247 const GLchar * WINAPI wglQueryCurrentRendererStringWINE( GLenum attribute )
1249 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1251 if (!funcs->ext.p_wglQueryCurrentRendererStringWINE) return NULL;
1252 return funcs->ext.p_wglQueryCurrentRendererStringWINE( attribute );
1255 /***********************************************************************
1256 * wglQueryRendererIntegerWINE
1258 * Provided by the WGL_WINE_query_renderer extension.
1260 BOOL WINAPI wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
1262 const struct opengl_funcs *funcs = get_dc_funcs( dc );
1264 if (!funcs || !funcs->ext.p_wglQueryRendererIntegerWINE) return FALSE;
1265 return funcs->ext.p_wglQueryRendererIntegerWINE( dc, renderer, attribute, value );
1268 /***********************************************************************
1269 * wglQueryRendererStringWINE
1271 * Provided by the WGL_WINE_query_renderer extension.
1273 const GLchar * WINAPI wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
1275 const struct opengl_funcs *funcs = get_dc_funcs( dc );
1277 if (!funcs || !funcs->ext.p_wglQueryRendererStringWINE) return NULL;
1278 return funcs->ext.p_wglQueryRendererStringWINE( dc, renderer, attribute );
1281 /***********************************************************************
1282 * wglUseFontBitmaps_common
1284 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1286 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1287 GLYPHMETRICS gm;
1288 unsigned int glyph, size = 0;
1289 void *bitmap = NULL, *gl_bitmap = NULL;
1290 int org_alignment;
1291 BOOL ret = TRUE;
1293 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1294 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1296 for (glyph = first; glyph < first + count; glyph++) {
1297 unsigned int needed_size, height, width, width_int;
1299 if (unicode)
1300 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1301 else
1302 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1304 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1305 if (needed_size == GDI_ERROR) {
1306 ret = FALSE;
1307 break;
1310 if (needed_size > size) {
1311 size = needed_size;
1312 HeapFree(GetProcessHeap(), 0, bitmap);
1313 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1314 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1315 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1317 if (needed_size != 0) {
1318 if (unicode)
1319 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1320 size, bitmap, &identity) != GDI_ERROR);
1321 else
1322 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1323 size, bitmap, &identity) != GDI_ERROR);
1324 if (!ret) break;
1327 if (TRACE_ON(wgl)) {
1328 unsigned int bitmask;
1329 unsigned char *bitmap_ = bitmap;
1331 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1332 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1333 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1334 if (needed_size != 0) {
1335 TRACE(" - bitmap:\n");
1336 for (height = 0; height < gm.gmBlackBoxY; height++) {
1337 TRACE(" ");
1338 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1339 if (bitmask == 0) {
1340 bitmap_ += 1;
1341 bitmask = 0x80;
1343 if (*bitmap_ & bitmask)
1344 TRACE("*");
1345 else
1346 TRACE(" ");
1348 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1349 TRACE("\n");
1354 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1355 * glyph for it to be drawn properly.
1357 if (needed_size != 0) {
1358 width_int = (gm.gmBlackBoxX + 31) / 32;
1359 for (height = 0; height < gm.gmBlackBoxY; height++) {
1360 for (width = 0; width < width_int; width++) {
1361 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1362 ((int *) bitmap)[height * width_int + width];
1367 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1368 if (needed_size != 0) {
1369 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1370 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1371 gm.gmCellIncX, gm.gmCellIncY,
1372 gl_bitmap);
1373 } else {
1374 /* This is the case of 'empty' glyphs like the space character */
1375 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1377 funcs->gl.p_glEndList();
1380 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1381 HeapFree(GetProcessHeap(), 0, bitmap);
1382 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1383 return ret;
1386 /***********************************************************************
1387 * wglUseFontBitmapsA (OPENGL32.@)
1389 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1391 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1394 /***********************************************************************
1395 * wglUseFontBitmapsW (OPENGL32.@)
1397 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1399 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1402 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1404 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1405 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1406 vertex[2] = 0.0;
1409 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1411 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1412 GLdouble *dbl = vertex;
1413 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1414 funcs->gl.p_glVertex3dv(vertex);
1417 static void WINAPI tess_callback_begin(GLenum which)
1419 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1420 TRACE("%d\n", which);
1421 funcs->gl.p_glBegin(which);
1424 static void WINAPI tess_callback_end(void)
1426 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1427 TRACE("\n");
1428 funcs->gl.p_glEnd();
1431 typedef struct _bezier_vector {
1432 GLdouble x;
1433 GLdouble y;
1434 } bezier_vector;
1436 static double bezier_deviation_squared(const bezier_vector *p)
1438 bezier_vector deviation;
1439 bezier_vector vertex;
1440 bezier_vector base;
1441 double base_length;
1442 double dot;
1444 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1445 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1447 base.x = p[2].x - p[0].x;
1448 base.y = p[2].y - p[0].y;
1450 base_length = sqrt(base.x*base.x + base.y*base.y);
1451 base.x /= base_length;
1452 base.y /= base_length;
1454 dot = base.x*vertex.x + base.y*vertex.y;
1455 dot = min(max(dot, 0.0), base_length);
1456 base.x *= dot;
1457 base.y *= dot;
1459 deviation.x = vertex.x-base.x;
1460 deviation.y = vertex.y-base.y;
1462 return deviation.x*deviation.x + deviation.y*deviation.y;
1465 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1467 bezier_vector first_curve[3];
1468 bezier_vector second_curve[3];
1469 bezier_vector vertex;
1470 int total_vertices;
1472 if(bezier_deviation_squared(p) <= deviation*deviation)
1474 if(points)
1475 *points = p[2];
1476 return 1;
1479 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1480 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1482 first_curve[0] = p[0];
1483 first_curve[1].x = (p[0].x + p[1].x)/2;
1484 first_curve[1].y = (p[0].y + p[1].y)/2;
1485 first_curve[2] = vertex;
1487 second_curve[0] = vertex;
1488 second_curve[1].x = (p[2].x + p[1].x)/2;
1489 second_curve[1].y = (p[2].y + p[1].y)/2;
1490 second_curve[2] = p[2];
1492 total_vertices = bezier_approximate(first_curve, points, deviation);
1493 if(points)
1494 points += total_vertices;
1495 total_vertices += bezier_approximate(second_curve, points, deviation);
1496 return total_vertices;
1499 /***********************************************************************
1500 * wglUseFontOutlines_common
1502 static BOOL wglUseFontOutlines_common(HDC hdc,
1503 DWORD first,
1504 DWORD count,
1505 DWORD listBase,
1506 FLOAT deviation,
1507 FLOAT extrusion,
1508 int format,
1509 LPGLYPHMETRICSFLOAT lpgmf,
1510 BOOL unicode)
1512 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1513 UINT glyph;
1514 GLUtesselator *tess = NULL;
1515 LOGFONTW lf;
1516 HFONT old_font, unscaled_font;
1517 UINT em_size = 1024;
1518 RECT rc;
1520 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1521 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1523 if(deviation <= 0.0)
1524 deviation = 1.0/em_size;
1526 if(format == WGL_FONT_POLYGONS)
1528 tess = gluNewTess();
1529 if(!tess)
1531 ERR("glu32 is required for this function but isn't available\n");
1532 return FALSE;
1534 gluTessCallback(tess, GLU_TESS_VERTEX, (void *)tess_callback_vertex);
1535 gluTessCallback(tess, GLU_TESS_BEGIN, (void *)tess_callback_begin);
1536 gluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1539 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1540 rc.left = rc.right = rc.bottom = 0;
1541 rc.top = em_size;
1542 DPtoLP(hdc, (POINT*)&rc, 2);
1543 lf.lfHeight = -abs(rc.top - rc.bottom);
1544 lf.lfOrientation = lf.lfEscapement = 0;
1545 unscaled_font = CreateFontIndirectW(&lf);
1546 old_font = SelectObject(hdc, unscaled_font);
1548 for (glyph = first; glyph < first + count; glyph++)
1550 DWORD needed;
1551 GLYPHMETRICS gm;
1552 BYTE *buf;
1553 TTPOLYGONHEADER *pph;
1554 TTPOLYCURVE *ppc;
1555 GLdouble *vertices = NULL;
1556 int vertex_total = -1;
1558 if(unicode)
1559 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1560 else
1561 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1563 if(needed == GDI_ERROR)
1564 goto error;
1566 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1568 if(unicode)
1569 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1570 else
1571 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1573 TRACE("glyph %d\n", glyph);
1575 if(lpgmf)
1577 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1578 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1579 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1580 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1581 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1582 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1584 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1585 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1586 lpgmf++;
1589 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1590 funcs->gl.p_glFrontFace(GL_CCW);
1591 if(format == WGL_FONT_POLYGONS)
1593 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1594 gluTessNormal(tess, 0, 0, 1);
1595 gluTessBeginPolygon(tess, NULL);
1598 while(!vertices)
1600 if(vertex_total != -1)
1601 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1602 vertex_total = 0;
1604 pph = (TTPOLYGONHEADER*)buf;
1605 while((BYTE*)pph < buf + needed)
1607 GLdouble previous[3];
1608 fixed_to_double(pph->pfxStart, em_size, previous);
1610 if(vertices)
1611 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1613 if(format == WGL_FONT_POLYGONS)
1614 gluTessBeginContour(tess);
1615 else
1616 funcs->gl.p_glBegin(GL_LINE_LOOP);
1618 if(vertices)
1620 fixed_to_double(pph->pfxStart, em_size, vertices);
1621 if(format == WGL_FONT_POLYGONS)
1622 gluTessVertex(tess, vertices, vertices);
1623 else
1624 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1625 vertices += 3;
1627 vertex_total++;
1629 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1630 while((char*)ppc < (char*)pph + pph->cb)
1632 int i, j;
1633 int num;
1635 switch(ppc->wType) {
1636 case TT_PRIM_LINE:
1637 for(i = 0; i < ppc->cpfx; i++)
1639 if(vertices)
1641 TRACE("\t\tline to %d, %d\n",
1642 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1643 fixed_to_double(ppc->apfx[i], em_size, vertices);
1644 if(format == WGL_FONT_POLYGONS)
1645 gluTessVertex(tess, vertices, vertices);
1646 else
1647 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1648 vertices += 3;
1650 fixed_to_double(ppc->apfx[i], em_size, previous);
1651 vertex_total++;
1653 break;
1655 case TT_PRIM_QSPLINE:
1656 for(i = 0; i < ppc->cpfx-1; i++)
1658 bezier_vector curve[3];
1659 bezier_vector *points;
1660 GLdouble curve_vertex[3];
1662 if(vertices)
1663 TRACE("\t\tcurve %d,%d %d,%d\n",
1664 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1665 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1667 curve[0].x = previous[0];
1668 curve[0].y = previous[1];
1669 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1670 curve[1].x = curve_vertex[0];
1671 curve[1].y = curve_vertex[1];
1672 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1673 curve[2].x = curve_vertex[0];
1674 curve[2].y = curve_vertex[1];
1675 if(i < ppc->cpfx-2)
1677 curve[2].x = (curve[1].x + curve[2].x)/2;
1678 curve[2].y = (curve[1].y + curve[2].y)/2;
1680 num = bezier_approximate(curve, NULL, deviation);
1681 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1682 num = bezier_approximate(curve, points, deviation);
1683 vertex_total += num;
1684 if(vertices)
1686 for(j=0; j<num; j++)
1688 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1689 vertices[0] = points[j].x;
1690 vertices[1] = points[j].y;
1691 vertices[2] = 0.0;
1692 if(format == WGL_FONT_POLYGONS)
1693 gluTessVertex(tess, vertices, vertices);
1694 else
1695 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1696 vertices += 3;
1699 HeapFree(GetProcessHeap(), 0, points);
1700 previous[0] = curve[2].x;
1701 previous[1] = curve[2].y;
1703 break;
1704 default:
1705 ERR("\t\tcurve type = %d\n", ppc->wType);
1706 if(format == WGL_FONT_POLYGONS)
1707 gluTessEndContour(tess);
1708 else
1709 funcs->gl.p_glEnd();
1710 goto error_in_list;
1713 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1714 (ppc->cpfx - 1) * sizeof(POINTFX));
1716 if(format == WGL_FONT_POLYGONS)
1717 gluTessEndContour(tess);
1718 else
1719 funcs->gl.p_glEnd();
1720 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1724 error_in_list:
1725 if(format == WGL_FONT_POLYGONS)
1726 gluTessEndPolygon(tess);
1727 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1728 funcs->gl.p_glEndList();
1729 HeapFree(GetProcessHeap(), 0, buf);
1730 HeapFree(GetProcessHeap(), 0, vertices);
1733 error:
1734 DeleteObject(SelectObject(hdc, old_font));
1735 if(format == WGL_FONT_POLYGONS)
1736 gluDeleteTess(tess);
1737 return TRUE;
1741 /***********************************************************************
1742 * wglUseFontOutlinesA (OPENGL32.@)
1744 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1745 DWORD first,
1746 DWORD count,
1747 DWORD listBase,
1748 FLOAT deviation,
1749 FLOAT extrusion,
1750 int format,
1751 LPGLYPHMETRICSFLOAT lpgmf)
1753 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1756 /***********************************************************************
1757 * wglUseFontOutlinesW (OPENGL32.@)
1759 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1760 DWORD first,
1761 DWORD count,
1762 DWORD listBase,
1763 FLOAT deviation,
1764 FLOAT extrusion,
1765 int format,
1766 LPGLYPHMETRICSFLOAT lpgmf)
1768 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1771 /***********************************************************************
1772 * glDebugEntry (OPENGL32.@)
1774 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1776 return 0;
1779 static GLubyte *filter_extensions_list(const char *extensions, const char *disabled)
1781 char *p, *str;
1782 const char *end;
1784 p = str = HeapAlloc(GetProcessHeap(), 0, strlen(extensions) + 2);
1785 if (!str)
1786 return NULL;
1788 TRACE( "GL_EXTENSIONS:\n" );
1790 for (;;)
1792 while (*extensions == ' ')
1793 extensions++;
1794 if (!*extensions)
1795 break;
1796 if (!(end = strchr(extensions, ' ')))
1797 end = extensions + strlen(extensions);
1798 memcpy(p, extensions, end - extensions);
1799 p[end - extensions] = 0;
1800 if (!has_extension(disabled, p, strlen(p)))
1802 TRACE("++ %s\n", p);
1803 p += end - extensions;
1804 *p++ = ' ';
1806 else
1808 TRACE("-- %s (disabled by config)\n", p);
1810 extensions = end;
1812 *p = 0;
1813 return (GLubyte *)str;
1816 static GLuint *filter_extensions_index(const char *disabled)
1818 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1819 const char *ext, *end, *gl_ext;
1820 GLuint *disabled_exts, *new_disabled_exts;
1821 unsigned int i = 0, j, disabled_size;
1822 GLint extensions_count;
1824 if (!funcs->ext.p_glGetStringi)
1826 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
1828 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
1829 if (!funcs->ext.p_glGetStringi)
1830 return NULL;
1833 funcs->gl.p_glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
1834 disabled_size = 2;
1835 disabled_exts = HeapAlloc(GetProcessHeap(), 0, disabled_size * sizeof(*disabled_exts));
1836 if (!disabled_exts)
1837 return NULL;
1839 TRACE( "GL_EXTENSIONS:\n" );
1841 for (j = 0; j < extensions_count; ++j)
1843 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, j);
1844 ext = disabled;
1845 for (;;)
1847 while (*ext == ' ')
1848 ext++;
1849 if (!*ext)
1851 TRACE("++ %s\n", gl_ext);
1852 break;
1854 if (!(end = strchr(ext, ' ')))
1855 end = ext + strlen(ext);
1857 if (!strncmp(gl_ext, ext, end - ext) && !gl_ext[end - ext])
1859 if (i + 1 == disabled_size)
1861 disabled_size *= 2;
1862 new_disabled_exts = HeapReAlloc(GetProcessHeap(), 0, disabled_exts,
1863 disabled_size * sizeof(*disabled_exts));
1864 if (!new_disabled_exts)
1866 disabled_exts[i] = ~0u;
1867 return disabled_exts;
1869 disabled_exts = new_disabled_exts;
1871 TRACE("-- %s (disabled by config)\n", gl_ext);
1872 disabled_exts[i++] = j;
1873 break;
1875 ext = end;
1878 disabled_exts[i] = ~0u;
1879 return disabled_exts;
1882 /* build the extension string by filtering out the disabled extensions */
1883 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts)
1885 static const char *disabled;
1887 if (!disabled)
1889 HKEY hkey;
1890 DWORD size;
1891 char *str = NULL;
1893 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1894 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1896 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1898 str = HeapAlloc( GetProcessHeap(), 0, size );
1899 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1901 RegCloseKey( hkey );
1903 if (str)
1905 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1906 HeapFree( GetProcessHeap(), 0, str );
1908 else disabled = "";
1911 if (!disabled[0])
1912 return FALSE;
1914 if (extensions && !*exts_list)
1915 *exts_list = filter_extensions_list(extensions, disabled);
1917 if (!*disabled_exts)
1918 *disabled_exts = filter_extensions_index(disabled);
1920 return (exts_list && *exts_list) || *disabled_exts;
1923 /***********************************************************************
1924 * glGetString (OPENGL32.@)
1926 const GLubyte * WINAPI glGetString( GLenum name )
1928 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1929 const GLubyte *ret = funcs->gl.p_glGetString( name );
1931 if (name == GL_EXTENSIONS && ret)
1933 struct wgl_handle *ptr = get_current_context_ptr();
1934 if (ptr->u.context->extensions ||
1935 filter_extensions((const char *)ret, &ptr->u.context->extensions, &ptr->u.context->disabled_exts))
1936 ret = ptr->u.context->extensions;
1938 return ret;
1941 /***********************************************************************
1942 * OpenGL initialisation routine
1944 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1946 switch(reason)
1948 case DLL_PROCESS_ATTACH:
1949 NtCurrentTeb()->glTable = &null_opengl_funcs;
1950 break;
1951 case DLL_THREAD_ATTACH:
1952 NtCurrentTeb()->glTable = &null_opengl_funcs;
1953 break;
1955 return TRUE;