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
22 #include "wine/port.h"
28 #include "opengl_ext.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
54 HANDLE_PBUFFER
= 0 << 12,
55 HANDLE_CONTEXT
= 1 << 12,
56 HANDLE_CONTEXT_V3
= 3 << 12,
57 HANDLE_TYPE_MASK
= 15 << 12
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 */
73 struct opengl_funcs
*funcs
;
76 struct opengl_context
*context
; /* for HANDLE_CONTEXT */
77 struct wgl_pbuffer
*pbuffer
; /* for HANDLE_PBUFFER */
78 struct wgl_handle
*next
; /* for free handles */
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
=
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
;
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
);
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
)
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
++];
149 ptr
->u
.context
= user_ptr
;
150 handle
= next_handle( ptr
, type
);
152 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
153 LeaveCriticalSection( &wgl_section
);
157 static void free_handle_ptr( struct wgl_handle
*ptr
)
159 ptr
->handle
|= 0xffff;
160 ptr
->u
.next
= next_free
;
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
;
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
);
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
);
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
);
216 /***********************************************************************
217 * wglMakeCurrent (OPENGL32.@)
219 BOOL WINAPI
wglMakeCurrent(HDC hdc
, HGLRC hglrc
)
222 struct wgl_handle
*ptr
, *prev
= get_current_context_ptr();
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
);
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
;
242 SetLastError( ERROR_BUSY
);
245 release_handle_ptr( ptr
);
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
;
256 SetLastError( ERROR_INVALID_HANDLE
);
262 /***********************************************************************
263 * wglCreateContextAttribsARB
265 * Provided by the WGL_ARB_create_context extension.
267 HGLRC WINAPI
wglCreateContextAttribsARB( HDC hdc
, HGLRC share
, const int *attribs
)
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
;
288 if (attribs
[0] == WGL_CONTEXT_MAJOR_VERSION_ARB
)
291 type
= HANDLE_CONTEXT_V3
;
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
);
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
)
317 struct wgl_handle
*ptr
, *prev
= get_current_context_ptr();
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
));
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
;
339 SetLastError( ERROR_BUSY
);
342 release_handle_ptr( ptr
);
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
;
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();
364 return ptr
->u
.context
->read_dc
;
367 /***********************************************************************
368 * wglShareLists (OPENGL32.@)
370 BOOL WINAPI
wglShareLists(HGLRC hglrcSrc
, HGLRC hglrcDst
)
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
);
386 /***********************************************************************
387 * wglGetCurrentDC (OPENGL32.@)
389 HDC WINAPI
wglGetCurrentDC(void)
391 struct wgl_handle
*ptr
= get_current_context_ptr();
394 return ptr
->u
.context
->draw_dc
;
397 /***********************************************************************
398 * wglCreateContext (OPENGL32.@)
400 HGLRC WINAPI
wglCreateContext(HDC hdc
)
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
);
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;
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
);
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
);
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
)))
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
)))
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
)))
533 if (best
.cColorBits
!= format
.cColorBits
) /* Do further checks if the format is compatible */
535 TRACE( "color mismatch for iPixelFormat=%d\n", i
);
539 if (ppfd
->cAlphaBits
)
541 if (((ppfd
->cAlphaBits
> best
.cAlphaBits
) && (format
.cAlphaBits
> best
.cAlphaBits
)) ||
542 ((format
.cAlphaBits
>= ppfd
->cAlphaBits
) && (format
.cAlphaBits
< best
.cAlphaBits
)))
545 if (best
.cAlphaBits
!= format
.cAlphaBits
)
547 TRACE( "alpha mismatch for iPixelFormat=%d\n", i
);
551 if (ppfd
->cDepthBits
)
553 if (((ppfd
->cDepthBits
> best
.cDepthBits
) && (format
.cDepthBits
> best
.cDepthBits
)) ||
554 ((format
.cDepthBits
>= ppfd
->cDepthBits
) && (format
.cDepthBits
< best
.cDepthBits
)))
557 if (best
.cDepthBits
!= format
.cDepthBits
)
559 TRACE( "depth mismatch for iPixelFormat=%d\n", i
);
563 if (ppfd
->cStencilBits
)
565 if (((ppfd
->cStencilBits
> best
.cStencilBits
) && (format
.cStencilBits
> best
.cStencilBits
)) ||
566 ((format
.cStencilBits
>= ppfd
->cStencilBits
) && (format
.cStencilBits
< best
.cStencilBits
)))
569 if (best
.cStencilBits
!= format
.cStencilBits
)
571 TRACE( "stencil mismatch for iPixelFormat=%d\n", i
);
575 if (ppfd
->cAuxBuffers
)
577 if (((ppfd
->cAuxBuffers
> best
.cAuxBuffers
) && (format
.cAuxBuffers
> best
.cAuxBuffers
)) ||
578 ((format
.cAuxBuffers
>= ppfd
->cAuxBuffers
) && (format
.cAuxBuffers
< best
.cAuxBuffers
)))
581 if (best
.cAuxBuffers
!= format
.cAuxBuffers
)
583 TRACE( "aux mismatch for iPixelFormat=%d\n", i
);
592 bestDBuffer
= format
.dwFlags
& PFD_DOUBLEBUFFER
;
593 bestStereo
= format
.dwFlags
& PFD_STEREO
;
596 TRACE( "returning %u\n", 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
;
632 static long prev_time
, start_time
;
633 static unsigned long frames
, frames_total
;
635 DWORD time
= GetTickCount();
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
));
645 if (start_time
== 0) start_time
= time
;
651 /***********************************************************************
652 * wglCreateLayerContext (OPENGL32.@)
654 HGLRC WINAPI
wglCreateLayerContext(HDC hdc
,
656 TRACE("(%p,%d)\n", hdc
, iLayerPlane
);
658 if (iLayerPlane
== 0) {
659 return wglCreateContext(hdc
);
661 FIXME("no handler for layer %d\n", iLayerPlane
);
666 /***********************************************************************
667 * wglDescribeLayerPlane (OPENGL32.@)
669 BOOL WINAPI
wglDescribeLayerPlane(HDC hdc
,
673 LPLAYERPLANEDESCRIPTOR plpd
) {
674 FIXME("(%p,%d,%d,%d,%p)\n", hdc
, iPixelFormat
, iLayerPlane
, nBytes
, plpd
);
679 /***********************************************************************
680 * wglGetLayerPaletteEntries (OPENGL32.@)
682 int WINAPI
wglGetLayerPaletteEntries(HDC hdc
,
686 const COLORREF
*pcr
) {
687 FIXME("(): stub!\n");
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)
712 *data
= count
- disabled_count
;
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
++)
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
)
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
])
770 while (*list
== ' ') list
++;
771 if (!strncmp( list
, ext
, len
) && (!list
[len
] || list
[len
] == ' ')) return TRUE
;
772 list
= strchr( list
, ' ' );
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
;
790 TRACE("Checking for extension '%s'\n", extension
);
792 if (type
== HANDLE_CONTEXT
)
794 gl_ext_string
= (const char*)glGetString(GL_EXTENSIONS
);
797 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
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
))
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_' */
824 ERR("No OpenGL version found!\n");
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])) ) {
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
++;
843 /***********************************************************************
844 * wglGetProcAddress (OPENGL32.@)
846 PROC WINAPI
wglGetProcAddress( LPCSTR name
)
848 struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
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");
865 ext_ret
= bsearch(&ext
, extension_registry
, extension_registry_size
, sizeof(ext
), compar
);
868 WARN("Function %s unknown\n", name
);
872 func_ptr
= (void **)&funcs
->ext
+ (ext_ret
- extension_registry
);
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
);
883 if (driver_func
== NULL
)
885 WARN("Function %s not supported by driver\n", name
);
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
,
906 /***********************************************************************
907 * wglSetLayerPaletteEntries (OPENGL32.@)
909 int WINAPI
wglSetLayerPaletteEntries(HDC hdc
,
913 const COLORREF
*pcr
) {
914 FIXME("(): stub!\n");
919 /***********************************************************************
920 * wglSwapLayerBuffers (OPENGL32.@)
922 BOOL WINAPI
wglSwapLayerBuffers(HDC hdc
,
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
;
932 WARN("Following layers unhandled: %08x\n", fuPlanes
);
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 /***********************************************************************
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 /***********************************************************************
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
);
973 if (!ptr
) return FALSE
;
974 ret
= ptr
->funcs
->ext
.p_wglBindTexImageARB( ptr
->u
.pbuffer
, buffer
);
975 release_handle_ptr( ptr
);
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
);
989 if (!ptr
) return FALSE
;
990 ret
= ptr
->funcs
->ext
.p_wglReleaseTexImageARB( ptr
->u
.pbuffer
, buffer
);
991 release_handle_ptr( ptr
);
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
);
1005 if (!ptr
) return FALSE
;
1006 ret
= ptr
->funcs
->ext
.p_wglSetPbufferAttribARB( ptr
->u
.pbuffer
, attribs
);
1007 release_handle_ptr( ptr
);
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
,
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
,
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
);
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
);
1082 ret
= ptr
->funcs
->ext
.p_wglGetPbufferDCARB( ptr
->u
.pbuffer
);
1083 release_handle_ptr( ptr
);
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
);
1097 if (!ptr
) return FALSE
;
1098 ret
= ptr
->funcs
->ext
.p_wglReleasePbufferDCARB( ptr
->u
.pbuffer
, hdc
);
1099 release_handle_ptr( ptr
);
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
);
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
);
1128 if (!ptr
) return FALSE
;
1129 ret
= ptr
->funcs
->ext
.p_wglQueryPbufferARB( ptr
->u
.pbuffer
, attrib
, value
);
1130 release_handle_ptr( ptr
);
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
;
1206 unsigned int glyph
, size
= 0;
1207 void *bitmap
= NULL
, *gl_bitmap
= NULL
;
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
;
1219 needed_size
= GetGlyphOutlineW(hdc
, glyph
, GGO_BITMAP
, &gm
, 0, NULL
, &identity
);
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
) {
1229 if (needed_size
> 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) {
1238 ret
= (GetGlyphOutlineW(hdc
, glyph
, GGO_BITMAP
, &gm
,
1239 size
, bitmap
, &identity
) != GDI_ERROR
);
1241 ret
= (GetGlyphOutlineA(hdc
, glyph
, GGO_BITMAP
, &gm
,
1242 size
, bitmap
, &identity
) != GDI_ERROR
);
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
++) {
1257 for (width
= 0, bitmask
= 0x80; width
< gm
.gmBlackBoxX
; width
++, bitmask
>>= 1) {
1262 if (*bitmap_
& bitmask
)
1267 bitmap_
+= (4 - ((UINT_PTR
)bitmap_
& 0x03));
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
,
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
);
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
);
1353 WARN("Failed to load glu32\n");
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
);
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
;
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
;
1396 funcs
->gl
.p_glEnd();
1399 typedef struct _bezier_vector
{
1404 static double bezier_deviation_squared(const bezier_vector
*p
)
1406 bezier_vector deviation
;
1407 bezier_vector vertex
;
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
);
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
;
1440 if(bezier_deviation_squared(p
) <= deviation
*deviation
)
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
);
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
,
1477 LPGLYPHMETRICSFLOAT lpgmf
,
1480 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1482 const MAT2 identity
= {{0,1},{0,0},{0,0},{0,1}};
1483 GLUtesselator
*tess
= NULL
;
1485 HFONT old_font
, unscaled_font
;
1486 UINT em_size
= 1024;
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
)
1499 ERR("glu32 is required for this function but isn't available\n");
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;
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
++)
1524 TTPOLYGONHEADER
*pph
;
1526 GLdouble
*vertices
= NULL
;
1527 int vertex_total
= -1;
1530 needed
= GetGlyphOutlineW(hdc
, glyph
, GGO_NATIVE
, &gm
, 0, NULL
, &identity
);
1532 needed
= GetGlyphOutlineA(hdc
, glyph
, GGO_NATIVE
, &gm
, 0, NULL
, &identity
);
1534 if(needed
== GDI_ERROR
)
1537 buf
= HeapAlloc(GetProcessHeap(), 0, needed
);
1540 GetGlyphOutlineW(hdc
, glyph
, GGO_NATIVE
, &gm
, needed
, buf
, &identity
);
1542 GetGlyphOutlineA(hdc
, glyph
, GGO_NATIVE
, &gm
, needed
, buf
, &identity
);
1544 TRACE("glyph %d\n", glyph
);
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
);
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
);
1571 if(vertex_total
!= -1)
1572 vertices
= HeapAlloc(GetProcessHeap(), 0, vertex_total
* 3 * sizeof(GLdouble
));
1575 pph
= (TTPOLYGONHEADER
*)buf
;
1576 while((BYTE
*)pph
< buf
+ needed
)
1578 GLdouble previous
[3];
1579 fixed_to_double(pph
->pfxStart
, em_size
, previous
);
1582 TRACE("\tstart %d, %d\n", pph
->pfxStart
.x
.value
, pph
->pfxStart
.y
.value
);
1584 if(format
== WGL_FONT_POLYGONS
)
1585 pgluTessBeginContour(tess
);
1587 funcs
->gl
.p_glBegin(GL_LINE_LOOP
);
1591 fixed_to_double(pph
->pfxStart
, em_size
, vertices
);
1592 if(format
== WGL_FONT_POLYGONS
)
1593 pgluTessVertex(tess
, vertices
, vertices
);
1595 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1600 ppc
= (TTPOLYCURVE
*)((char*)pph
+ sizeof(*pph
));
1601 while((char*)ppc
< (char*)pph
+ pph
->cb
)
1606 switch(ppc
->wType
) {
1608 for(i
= 0; i
< ppc
->cpfx
; i
++)
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
);
1618 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1621 fixed_to_double(ppc
->apfx
[i
], em_size
, previous
);
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];
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];
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
;
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
;
1663 if(format
== WGL_FONT_POLYGONS
)
1664 pgluTessVertex(tess
, vertices
, vertices
);
1666 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1670 HeapFree(GetProcessHeap(), 0, points
);
1671 previous
[0] = curve
[2].x
;
1672 previous
[1] = curve
[2].y
;
1676 ERR("\t\tcurve type = %d\n", ppc
->wType
);
1677 if(format
== WGL_FONT_POLYGONS
)
1678 pgluTessEndContour(tess
);
1680 funcs
->gl
.p_glEnd();
1684 ppc
= (TTPOLYCURVE
*)((char*)ppc
+ sizeof(*ppc
) +
1685 (ppc
->cpfx
- 1) * sizeof(POINTFX
));
1687 if(format
== WGL_FONT_POLYGONS
)
1688 pgluTessEndContour(tess
);
1690 funcs
->gl
.p_glEnd();
1691 pph
= (TTPOLYGONHEADER
*)((char*)pph
+ pph
->cb
);
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
);
1705 DeleteObject(SelectObject(hdc
, old_font
));
1706 if(format
== WGL_FONT_POLYGONS
)
1707 pgluDeleteTess(tess
);
1712 /***********************************************************************
1713 * wglUseFontOutlinesA (OPENGL32.@)
1715 BOOL WINAPI
wglUseFontOutlinesA(HDC hdc
,
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
,
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
)
1750 static GLubyte
*filter_extensions_list(const char *extensions
, const char *disabled
)
1752 char *p
, *str
= NULL
;
1755 p
= str
= HeapAlloc(GetProcessHeap(), 0, strlen(extensions
) + 2);
1760 while (*extensions
== ' ')
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
;
1776 TRACE("-- %s (disabled by config)\n", p
);
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
)
1801 funcs
->gl
.p_glGetIntegerv(GL_NUM_EXTENSIONS
, &extensions_count
);
1803 disabled_exts
= HeapAlloc(GetProcessHeap(), 0, disabled_size
* sizeof(*disabled_exts
));
1806 for (j
= 0; j
< extensions_count
; ++j
)
1808 gl_ext
= (const char *)funcs
->ext
.p_glGetStringi(GL_EXTENSIONS
, j
);
1816 TRACE("++ %s\n", gl_ext
);
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
)
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
;
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" );
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
);
1872 if (InterlockedCompareExchangePointer( (void **)&disabled
, str
, NULL
))
1873 HeapFree( GetProcessHeap(), 0, str
);
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
;
1908 /***********************************************************************
1909 * OpenGL initialisation routine
1911 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
1915 case DLL_PROCESS_ATTACH
:
1916 NtCurrentTeb()->glTable
= &null_opengl_funcs
;
1918 case DLL_THREAD_ATTACH
:
1919 NtCurrentTeb()->glTable
= &null_opengl_funcs
;