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 const MAT2 identity
= { {0,1},{0,0},{0,0},{0,1} };
97 static inline struct opengl_funcs
*get_dc_funcs( HDC hdc
)
99 struct opengl_funcs
*funcs
= __wine_get_wgl_driver( hdc
, WINE_WGL_DRIVER_VERSION
);
100 if (funcs
== (void *)-1) funcs
= &null_opengl_funcs
;
104 static inline HANDLE
next_handle( struct wgl_handle
*ptr
, enum wgl_handle_type type
)
106 WORD generation
= HIWORD( ptr
->handle
) + 1;
107 if (!generation
) generation
++;
108 ptr
->handle
= MAKELONG( ptr
- wgl_handles
, generation
) | type
;
109 return ULongToHandle( ptr
->handle
);
112 /* the current context is assumed valid and doesn't need locking */
113 static inline struct wgl_handle
*get_current_context_ptr(void)
115 if (!NtCurrentTeb()->glCurrentRC
) return NULL
;
116 return &wgl_handles
[LOWORD(NtCurrentTeb()->glCurrentRC
) & ~HANDLE_TYPE_MASK
];
119 static struct wgl_handle
*get_handle_ptr( HANDLE handle
, enum wgl_handle_type type
)
121 unsigned int index
= LOWORD( handle
) & ~HANDLE_TYPE_MASK
;
123 EnterCriticalSection( &wgl_section
);
124 if (index
< handle_count
&& ULongToHandle(wgl_handles
[index
].handle
) == handle
)
125 return &wgl_handles
[index
];
127 LeaveCriticalSection( &wgl_section
);
128 SetLastError( ERROR_INVALID_HANDLE
);
132 static void release_handle_ptr( struct wgl_handle
*ptr
)
134 if (ptr
) LeaveCriticalSection( &wgl_section
);
137 static HANDLE
alloc_handle( enum wgl_handle_type type
, struct opengl_funcs
*funcs
, void *user_ptr
)
140 struct wgl_handle
*ptr
= NULL
;
142 EnterCriticalSection( &wgl_section
);
143 if ((ptr
= next_free
))
144 next_free
= next_free
->u
.next
;
145 else if (handle_count
< MAX_WGL_HANDLES
)
146 ptr
= &wgl_handles
[handle_count
++];
151 ptr
->u
.context
= user_ptr
;
152 handle
= next_handle( ptr
, type
);
154 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
155 LeaveCriticalSection( &wgl_section
);
159 static void free_handle_ptr( struct wgl_handle
*ptr
)
161 ptr
->handle
|= 0xffff;
162 ptr
->u
.next
= next_free
;
165 LeaveCriticalSection( &wgl_section
);
168 static inline enum wgl_handle_type
get_current_context_type(void)
170 if (!NtCurrentTeb()->glCurrentRC
) return HANDLE_CONTEXT
;
171 return LOWORD(NtCurrentTeb()->glCurrentRC
) & HANDLE_TYPE_MASK
;
174 /***********************************************************************
175 * wglCopyContext (OPENGL32.@)
177 BOOL WINAPI
wglCopyContext(HGLRC hglrcSrc
, HGLRC hglrcDst
, UINT mask
)
179 struct wgl_handle
*src
, *dst
;
182 if (!(src
= get_handle_ptr( hglrcSrc
, HANDLE_CONTEXT
))) return FALSE
;
183 if ((dst
= get_handle_ptr( hglrcDst
, HANDLE_CONTEXT
)))
185 if (src
->funcs
!= dst
->funcs
) SetLastError( ERROR_INVALID_HANDLE
);
186 else ret
= src
->funcs
->wgl
.p_wglCopyContext( src
->u
.context
->drv_ctx
,
187 dst
->u
.context
->drv_ctx
, mask
);
189 release_handle_ptr( dst
);
190 release_handle_ptr( src
);
194 /***********************************************************************
195 * wglDeleteContext (OPENGL32.@)
197 BOOL WINAPI
wglDeleteContext(HGLRC hglrc
)
199 struct wgl_handle
*ptr
= get_handle_ptr( hglrc
, HANDLE_CONTEXT
);
201 if (!ptr
) return FALSE
;
203 if (ptr
->u
.context
->tid
&& ptr
->u
.context
->tid
!= GetCurrentThreadId())
205 SetLastError( ERROR_BUSY
);
206 release_handle_ptr( ptr
);
209 if (hglrc
== NtCurrentTeb()->glCurrentRC
) wglMakeCurrent( 0, 0 );
210 ptr
->funcs
->wgl
.p_wglDeleteContext( ptr
->u
.context
->drv_ctx
);
211 HeapFree( GetProcessHeap(), 0, ptr
->u
.context
->disabled_exts
);
212 HeapFree( GetProcessHeap(), 0, ptr
->u
.context
->extensions
);
213 HeapFree( GetProcessHeap(), 0, ptr
->u
.context
);
214 free_handle_ptr( ptr
);
218 /***********************************************************************
219 * wglMakeCurrent (OPENGL32.@)
221 BOOL WINAPI
wglMakeCurrent(HDC hdc
, HGLRC hglrc
)
224 struct wgl_handle
*ptr
, *prev
= get_current_context_ptr();
228 if (!(ptr
= get_handle_ptr( hglrc
, HANDLE_CONTEXT
))) return FALSE
;
229 if (!ptr
->u
.context
->tid
|| ptr
->u
.context
->tid
== GetCurrentThreadId())
231 ret
= ptr
->funcs
->wgl
.p_wglMakeCurrent( hdc
, ptr
->u
.context
->drv_ctx
);
234 if (prev
) prev
->u
.context
->tid
= 0;
235 ptr
->u
.context
->tid
= GetCurrentThreadId();
236 ptr
->u
.context
->draw_dc
= hdc
;
237 ptr
->u
.context
->read_dc
= hdc
;
238 NtCurrentTeb()->glCurrentRC
= hglrc
;
239 NtCurrentTeb()->glTable
= ptr
->funcs
;
244 SetLastError( ERROR_BUSY
);
247 release_handle_ptr( ptr
);
251 if (!prev
->funcs
->wgl
.p_wglMakeCurrent( 0, NULL
)) return FALSE
;
252 prev
->u
.context
->tid
= 0;
253 NtCurrentTeb()->glCurrentRC
= 0;
254 NtCurrentTeb()->glTable
= &null_opengl_funcs
;
258 SetLastError( ERROR_INVALID_HANDLE
);
264 /***********************************************************************
265 * wglCreateContextAttribsARB
267 * Provided by the WGL_ARB_create_context extension.
269 HGLRC WINAPI
wglCreateContextAttribsARB( HDC hdc
, HGLRC share
, const int *attribs
)
272 struct wgl_context
*drv_ctx
;
273 struct wgl_handle
*share_ptr
= NULL
;
274 struct opengl_context
*context
;
275 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
279 SetLastError( ERROR_DC_NOT_FOUND
);
282 if (!funcs
->ext
.p_wglCreateContextAttribsARB
) return 0;
283 if (share
&& !(share_ptr
= get_handle_ptr( share
, HANDLE_CONTEXT
))) return 0;
284 if ((drv_ctx
= funcs
->ext
.p_wglCreateContextAttribsARB( hdc
,
285 share_ptr
? share_ptr
->u
.context
->drv_ctx
: NULL
, attribs
)))
287 if ((context
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*context
) )))
289 enum wgl_handle_type type
= HANDLE_CONTEXT
;
295 if (attribs
[0] == WGL_CONTEXT_MAJOR_VERSION_ARB
)
298 type
= HANDLE_CONTEXT_V3
;
305 context
->drv_ctx
= drv_ctx
;
306 if (!(ret
= alloc_handle( type
, funcs
, context
)))
307 HeapFree( GetProcessHeap(), 0, context
);
309 if (!ret
) funcs
->wgl
.p_wglDeleteContext( drv_ctx
);
311 release_handle_ptr( share_ptr
);
316 /***********************************************************************
317 * wglMakeContextCurrentARB
319 * Provided by the WGL_ARB_make_current_read extension.
321 BOOL WINAPI
wglMakeContextCurrentARB( HDC draw_hdc
, HDC read_hdc
, HGLRC hglrc
)
324 struct wgl_handle
*ptr
, *prev
= get_current_context_ptr();
328 if (!(ptr
= get_handle_ptr( hglrc
, HANDLE_CONTEXT
))) return FALSE
;
329 if (!ptr
->u
.context
->tid
|| ptr
->u
.context
->tid
== GetCurrentThreadId())
331 ret
= (ptr
->funcs
->ext
.p_wglMakeContextCurrentARB
&&
332 ptr
->funcs
->ext
.p_wglMakeContextCurrentARB( draw_hdc
, read_hdc
,
333 ptr
->u
.context
->drv_ctx
));
336 if (prev
) prev
->u
.context
->tid
= 0;
337 ptr
->u
.context
->tid
= GetCurrentThreadId();
338 ptr
->u
.context
->draw_dc
= draw_hdc
;
339 ptr
->u
.context
->read_dc
= read_hdc
;
340 NtCurrentTeb()->glCurrentRC
= hglrc
;
341 NtCurrentTeb()->glTable
= ptr
->funcs
;
346 SetLastError( ERROR_BUSY
);
349 release_handle_ptr( ptr
);
353 if (!prev
->funcs
->wgl
.p_wglMakeCurrent( 0, NULL
)) return FALSE
;
354 prev
->u
.context
->tid
= 0;
355 NtCurrentTeb()->glCurrentRC
= 0;
356 NtCurrentTeb()->glTable
= &null_opengl_funcs
;
361 /***********************************************************************
362 * wglGetCurrentReadDCARB
364 * Provided by the WGL_ARB_make_current_read extension.
366 HDC WINAPI
wglGetCurrentReadDCARB(void)
368 struct wgl_handle
*ptr
= get_current_context_ptr();
371 return ptr
->u
.context
->read_dc
;
374 /***********************************************************************
375 * wglShareLists (OPENGL32.@)
377 BOOL WINAPI
wglShareLists(HGLRC hglrcSrc
, HGLRC hglrcDst
)
380 struct wgl_handle
*src
, *dst
;
382 if (!(src
= get_handle_ptr( hglrcSrc
, HANDLE_CONTEXT
))) return FALSE
;
383 if ((dst
= get_handle_ptr( hglrcDst
, HANDLE_CONTEXT
)))
385 if (src
->funcs
!= dst
->funcs
) SetLastError( ERROR_INVALID_HANDLE
);
386 else ret
= src
->funcs
->wgl
.p_wglShareLists( src
->u
.context
->drv_ctx
, dst
->u
.context
->drv_ctx
);
388 release_handle_ptr( dst
);
389 release_handle_ptr( src
);
393 /***********************************************************************
394 * wglGetCurrentDC (OPENGL32.@)
396 HDC WINAPI
wglGetCurrentDC(void)
398 struct wgl_handle
*ptr
= get_current_context_ptr();
401 return ptr
->u
.context
->draw_dc
;
404 /***********************************************************************
405 * wglCreateContext (OPENGL32.@)
407 HGLRC WINAPI
wglCreateContext(HDC hdc
)
410 struct wgl_context
*drv_ctx
;
411 struct opengl_context
*context
;
412 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
414 if (!funcs
) return 0;
415 if (!(drv_ctx
= funcs
->wgl
.p_wglCreateContext( hdc
))) return 0;
416 if ((context
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*context
) )))
418 context
->drv_ctx
= drv_ctx
;
419 if (!(ret
= alloc_handle( HANDLE_CONTEXT
, funcs
, context
)))
420 HeapFree( GetProcessHeap(), 0, context
);
422 if (!ret
) funcs
->wgl
.p_wglDeleteContext( drv_ctx
);
426 /***********************************************************************
427 * wglGetCurrentContext (OPENGL32.@)
429 HGLRC WINAPI
wglGetCurrentContext(void)
431 return NtCurrentTeb()->glCurrentRC
;
434 /***********************************************************************
435 * wglDescribePixelFormat (OPENGL32.@)
437 INT WINAPI
wglDescribePixelFormat(HDC hdc
, INT format
, UINT size
, PIXELFORMATDESCRIPTOR
*descr
)
439 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
440 if (!funcs
) return 0;
441 return funcs
->wgl
.p_wglDescribePixelFormat( hdc
, format
, size
, descr
);
444 /***********************************************************************
445 * wglChoosePixelFormat (OPENGL32.@)
447 INT WINAPI
wglChoosePixelFormat(HDC hdc
, const PIXELFORMATDESCRIPTOR
* ppfd
)
449 PIXELFORMATDESCRIPTOR format
, best
;
450 int i
, count
, best_format
;
451 int bestDBuffer
= -1, bestStereo
= -1;
453 TRACE_(wgl
)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
454 "accum %u depth %u stencil %u aux %u\n",
455 hdc
, ppfd
, ppfd
->nSize
, ppfd
->nVersion
, ppfd
->dwFlags
, ppfd
->iPixelType
,
456 ppfd
->cColorBits
, ppfd
->cRedBits
, ppfd
->cGreenBits
, ppfd
->cBlueBits
, ppfd
->cAlphaBits
,
457 ppfd
->cAccumBits
, ppfd
->cDepthBits
, ppfd
->cStencilBits
, ppfd
->cAuxBuffers
);
459 count
= wglDescribePixelFormat( hdc
, 0, 0, NULL
);
460 if (!count
) return 0;
464 best
.cAlphaBits
= -1;
465 best
.cColorBits
= -1;
466 best
.cDepthBits
= -1;
467 best
.cStencilBits
= -1;
468 best
.cAuxBuffers
= -1;
470 for (i
= 1; i
<= count
; i
++)
472 if (!wglDescribePixelFormat( hdc
, i
, sizeof(format
), &format
)) continue;
474 if (ppfd
->iPixelType
!= format
.iPixelType
)
476 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i
);
480 /* only use bitmap capable for formats for bitmap rendering */
481 if( (ppfd
->dwFlags
& PFD_DRAW_TO_BITMAP
) != (format
.dwFlags
& PFD_DRAW_TO_BITMAP
))
483 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i
);
487 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
488 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
489 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
490 * formats without the given flag set.
491 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
492 * has indicated that a format without stereo is returned when stereo is unavailable.
493 * So in case PFD_STEREO is set, formats that support it should have priority above formats
494 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
496 * To summarize the following is most likely the correct behavior:
497 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
498 * stereo set -> prefer stereo formats, but also accept non-stereo formats
499 * stereo don't care -> it doesn't matter whether we get stereo or not
501 * In Wine we will treat non-stereo the same way as don't care because it makes
502 * format selection even more complicated and second drivers with Stereo advertise
503 * each format twice anyway.
506 /* Doublebuffer, see the comments above */
507 if (!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
))
509 if (((ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) != bestDBuffer
) &&
510 ((format
.dwFlags
& PFD_DOUBLEBUFFER
) == (ppfd
->dwFlags
& PFD_DOUBLEBUFFER
)))
513 if (bestDBuffer
!= -1 && (format
.dwFlags
& PFD_DOUBLEBUFFER
) != bestDBuffer
) continue;
516 /* Stereo, see the comments above. */
517 if (!(ppfd
->dwFlags
& PFD_STEREO_DONTCARE
))
519 if (((ppfd
->dwFlags
& PFD_STEREO
) != bestStereo
) &&
520 ((format
.dwFlags
& PFD_STEREO
) == (ppfd
->dwFlags
& PFD_STEREO
)))
523 if (bestStereo
!= -1 && (format
.dwFlags
& PFD_STEREO
) != bestStereo
) continue;
526 /* Below we will do a number of checks to select the 'best' pixelformat.
527 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
528 * The code works by trying to match the most important options as close as possible.
529 * When a reasonable format is found, we will try to match more options.
530 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
531 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
532 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
534 if (ppfd
->cColorBits
)
536 if (((ppfd
->cColorBits
> best
.cColorBits
) && (format
.cColorBits
> best
.cColorBits
)) ||
537 ((format
.cColorBits
>= ppfd
->cColorBits
) && (format
.cColorBits
< best
.cColorBits
)))
540 if (best
.cColorBits
!= format
.cColorBits
) /* Do further checks if the format is compatible */
542 TRACE( "color mismatch for iPixelFormat=%d\n", i
);
546 if (ppfd
->cAlphaBits
)
548 if (((ppfd
->cAlphaBits
> best
.cAlphaBits
) && (format
.cAlphaBits
> best
.cAlphaBits
)) ||
549 ((format
.cAlphaBits
>= ppfd
->cAlphaBits
) && (format
.cAlphaBits
< best
.cAlphaBits
)))
552 if (best
.cAlphaBits
!= format
.cAlphaBits
)
554 TRACE( "alpha mismatch for iPixelFormat=%d\n", i
);
558 if (ppfd
->cDepthBits
)
560 if (((ppfd
->cDepthBits
> best
.cDepthBits
) && (format
.cDepthBits
> best
.cDepthBits
)) ||
561 ((format
.cDepthBits
>= ppfd
->cDepthBits
) && (format
.cDepthBits
< best
.cDepthBits
)))
564 if (best
.cDepthBits
!= format
.cDepthBits
)
566 TRACE( "depth mismatch for iPixelFormat=%d\n", i
);
570 if (ppfd
->cStencilBits
)
572 if (((ppfd
->cStencilBits
> best
.cStencilBits
) && (format
.cStencilBits
> best
.cStencilBits
)) ||
573 ((format
.cStencilBits
>= ppfd
->cStencilBits
) && (format
.cStencilBits
< best
.cStencilBits
)))
576 if (best
.cStencilBits
!= format
.cStencilBits
)
578 TRACE( "stencil mismatch for iPixelFormat=%d\n", i
);
582 if (ppfd
->cAuxBuffers
)
584 if (((ppfd
->cAuxBuffers
> best
.cAuxBuffers
) && (format
.cAuxBuffers
> best
.cAuxBuffers
)) ||
585 ((format
.cAuxBuffers
>= ppfd
->cAuxBuffers
) && (format
.cAuxBuffers
< best
.cAuxBuffers
)))
588 if (best
.cAuxBuffers
!= format
.cAuxBuffers
)
590 TRACE( "aux mismatch for iPixelFormat=%d\n", i
);
599 bestDBuffer
= format
.dwFlags
& PFD_DOUBLEBUFFER
;
600 bestStereo
= format
.dwFlags
& PFD_STEREO
;
603 TRACE( "returning %u\n", best_format
);
607 /***********************************************************************
608 * wglGetPixelFormat (OPENGL32.@)
610 INT WINAPI
wglGetPixelFormat(HDC hdc
)
612 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
613 if (!funcs
) return 0;
614 return funcs
->wgl
.p_wglGetPixelFormat( hdc
);
617 /***********************************************************************
618 * wglSetPixelFormat(OPENGL32.@)
620 BOOL WINAPI
wglSetPixelFormat( HDC hdc
, INT format
, const PIXELFORMATDESCRIPTOR
*descr
)
622 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
623 if (!funcs
) return FALSE
;
624 return funcs
->wgl
.p_wglSetPixelFormat( hdc
, format
, descr
);
627 /***********************************************************************
628 * wglSwapBuffers (OPENGL32.@)
630 BOOL WINAPI DECLSPEC_HOTPATCH
wglSwapBuffers( HDC hdc
)
632 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
634 if (!funcs
|| !funcs
->wgl
.p_wglSwapBuffers
) return FALSE
;
635 if (!funcs
->wgl
.p_wglSwapBuffers( hdc
)) return FALSE
;
639 static long prev_time
, start_time
;
640 static unsigned long frames
, frames_total
;
642 DWORD time
= GetTickCount();
645 /* every 1.5 seconds */
646 if (time
- prev_time
> 1500)
648 TRACE_(fps
)("@ approx %.2ffps, total %.2ffps\n",
649 1000.0*frames
/(time
- prev_time
), 1000.0*frames_total
/(time
- start_time
));
652 if (start_time
== 0) start_time
= time
;
658 /***********************************************************************
659 * wglCreateLayerContext (OPENGL32.@)
661 HGLRC WINAPI
wglCreateLayerContext(HDC hdc
,
663 TRACE("(%p,%d)\n", hdc
, iLayerPlane
);
665 if (iLayerPlane
== 0) {
666 return wglCreateContext(hdc
);
668 FIXME("no handler for layer %d\n", iLayerPlane
);
673 /***********************************************************************
674 * wglDescribeLayerPlane (OPENGL32.@)
676 BOOL WINAPI
wglDescribeLayerPlane(HDC hdc
,
680 LPLAYERPLANEDESCRIPTOR plpd
) {
681 FIXME("(%p,%d,%d,%d,%p)\n", hdc
, iPixelFormat
, iLayerPlane
, nBytes
, plpd
);
686 /***********************************************************************
687 * wglGetLayerPaletteEntries (OPENGL32.@)
689 int WINAPI
wglGetLayerPaletteEntries(HDC hdc
,
693 const COLORREF
*pcr
) {
694 FIXME("(): stub!\n");
699 static BOOL
filter_extensions(const char *extensions
, GLubyte
**exts_list
, GLuint
**disabled_exts
);
701 void WINAPI
glGetIntegerv(GLenum pname
, GLint
*data
)
703 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
705 TRACE("(%d, %p)\n", pname
, data
);
706 if (pname
== GL_NUM_EXTENSIONS
)
708 struct wgl_handle
*ptr
= get_current_context_ptr();
710 if (ptr
->u
.context
->disabled_exts
||
711 filter_extensions(NULL
, NULL
, &ptr
->u
.context
->disabled_exts
))
713 const GLuint
*disabled_exts
= ptr
->u
.context
->disabled_exts
;
714 GLint count
, disabled_count
= 0;
716 funcs
->gl
.p_glGetIntegerv(pname
, &count
);
717 while (*disabled_exts
++ != ~0u)
719 *data
= count
- disabled_count
;
723 funcs
->gl
.p_glGetIntegerv(pname
, data
);
726 const GLubyte
* WINAPI
glGetStringi(GLenum name
, GLuint index
)
728 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
730 TRACE("(%d, %d)\n", name
, index
);
731 if (!funcs
->ext
.p_glGetStringi
)
733 void **func_ptr
= (void **)&funcs
->ext
.p_glGetStringi
;
735 *func_ptr
= funcs
->wgl
.p_wglGetProcAddress("glGetStringi");
738 if (name
== GL_EXTENSIONS
)
740 struct wgl_handle
*ptr
= get_current_context_ptr();
742 if (ptr
->u
.context
->disabled_exts
||
743 filter_extensions(NULL
, NULL
, &ptr
->u
.context
->disabled_exts
))
745 const GLuint
*disabled_exts
= ptr
->u
.context
->disabled_exts
;
746 unsigned int disabled_count
= 0;
748 while (index
+ disabled_count
>= *disabled_exts
++)
750 return funcs
->ext
.p_glGetStringi(name
, index
+ disabled_count
);
753 return funcs
->ext
.p_glGetStringi(name
, index
);
756 /* check if the extension is present in the list */
757 static BOOL
has_extension( const char *list
, const char *ext
, size_t len
)
763 GLint extensions_count
;
765 glGetIntegerv(GL_NUM_EXTENSIONS
, &extensions_count
);
766 for (i
= 0; i
< extensions_count
; ++i
)
768 gl_ext
= (const char *)glGetStringi(GL_EXTENSIONS
, i
);
769 if (!strncmp(gl_ext
, ext
, len
) && !gl_ext
[len
])
777 while (*list
== ' ') list
++;
778 if (!strncmp( list
, ext
, len
) && (!list
[len
] || list
[len
] == ' ')) return TRUE
;
779 list
= strchr( list
, ' ' );
784 static int compar(const void *elt_a
, const void *elt_b
) {
785 return strcmp(((const OpenGL_extension
*) elt_a
)->name
,
786 ((const OpenGL_extension
*) elt_b
)->name
);
789 /* Check if a GL extension is supported */
790 static BOOL
is_extension_supported(const char* extension
)
792 enum wgl_handle_type type
= get_current_context_type();
793 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
794 const char *gl_ext_string
= NULL
;
797 TRACE("Checking for extension '%s'\n", extension
);
799 if (type
== HANDLE_CONTEXT
)
801 gl_ext_string
= (const char*)glGetString(GL_EXTENSIONS
);
804 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
809 /* We use the GetProcAddress function from the display driver to retrieve function pointers
810 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
811 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
812 * require the function to return NULL when an extension isn't found. For this reason we check
813 * if the OpenGL extension required for the function we are looking up is supported. */
815 while ((len
= strcspn(extension
, " ")) != 0)
817 /* Check if the extension is part of the GL extension string to see if it is supported. */
818 if (has_extension(gl_ext_string
, extension
, len
))
821 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
822 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
823 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
824 * Check if we are searching for a core GL function */
825 if(strncmp(extension
, "GL_VERSION_", 11) == 0)
827 const GLubyte
*gl_version
= funcs
->gl
.p_glGetString(GL_VERSION
);
828 const char *version
= extension
+ 11; /* Move past 'GL_VERSION_' */
831 ERR("No OpenGL version found!\n");
835 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
836 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
837 if( (gl_version
[0] > version
[0]) || ((gl_version
[0] == version
[0]) && (gl_version
[2] >= version
[2])) ) {
840 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]);
843 if (extension
[len
] == ' ') len
++;
850 /***********************************************************************
851 * wglGetProcAddress (OPENGL32.@)
853 PROC WINAPI
wglGetProcAddress( LPCSTR name
)
855 struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
857 OpenGL_extension ext
;
858 const OpenGL_extension
*ext_ret
;
860 if (!name
) return NULL
;
862 /* Without an active context opengl32 doesn't know to what
863 * driver it has to dispatch wglGetProcAddress.
865 if (!get_current_context_ptr())
867 WARN("No active WGL context found\n");
872 ext_ret
= bsearch(&ext
, extension_registry
, extension_registry_size
, sizeof(ext
), compar
);
875 WARN("Function %s unknown\n", name
);
879 func_ptr
= (void **)&funcs
->ext
+ (ext_ret
- extension_registry
);
882 void *driver_func
= funcs
->wgl
.p_wglGetProcAddress( name
);
884 if (!is_extension_supported(ext_ret
->extension
))
887 static const struct { const char *name
, *alt
; } alternatives
[] =
889 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
890 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
893 for (i
= 0; i
< sizeof(alternatives
)/sizeof(alternatives
[0]); i
++)
895 if (strcmp( name
, alternatives
[i
].name
)) continue;
896 WARN("Extension %s required for %s not supported, trying %s\n",
897 ext_ret
->extension
, name
, alternatives
[i
].alt
);
898 return wglGetProcAddress( alternatives
[i
].alt
);
900 WARN("Extension %s required for %s not supported\n", ext_ret
->extension
, name
);
904 if (driver_func
== NULL
)
906 WARN("Function %s not supported by driver\n", name
);
909 *func_ptr
= driver_func
;
912 TRACE("returning %s -> %p\n", name
, ext_ret
->func
);
913 return ext_ret
->func
;
916 /***********************************************************************
917 * wglRealizeLayerPalette (OPENGL32.@)
919 BOOL WINAPI
wglRealizeLayerPalette(HDC hdc
,
927 /***********************************************************************
928 * wglSetLayerPaletteEntries (OPENGL32.@)
930 int WINAPI
wglSetLayerPaletteEntries(HDC hdc
,
934 const COLORREF
*pcr
) {
935 FIXME("(): stub!\n");
940 /***********************************************************************
941 * wglSwapLayerBuffers (OPENGL32.@)
943 BOOL WINAPI
wglSwapLayerBuffers(HDC hdc
,
945 TRACE("(%p, %08x)\n", hdc
, fuPlanes
);
947 if (fuPlanes
& WGL_SWAP_MAIN_PLANE
) {
948 if (!wglSwapBuffers( hdc
)) return FALSE
;
949 fuPlanes
&= ~WGL_SWAP_MAIN_PLANE
;
953 WARN("Following layers unhandled: %08x\n", fuPlanes
);
959 /***********************************************************************
960 * wglAllocateMemoryNV
962 * Provided by the WGL_NV_vertex_array_range extension.
964 void * WINAPI
wglAllocateMemoryNV( GLsizei size
, GLfloat readfreq
, GLfloat writefreq
, GLfloat priority
)
966 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
968 if (!funcs
->ext
.p_wglAllocateMemoryNV
) return NULL
;
969 return funcs
->ext
.p_wglAllocateMemoryNV( size
, readfreq
, writefreq
, priority
);
972 /***********************************************************************
975 * Provided by the WGL_NV_vertex_array_range extension.
977 void WINAPI
wglFreeMemoryNV( void *pointer
)
979 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
981 if (funcs
->ext
.p_wglFreeMemoryNV
) funcs
->ext
.p_wglFreeMemoryNV( pointer
);
984 /***********************************************************************
987 * Provided by the WGL_ARB_render_texture extension.
989 BOOL WINAPI
wglBindTexImageARB( HPBUFFERARB handle
, int buffer
)
991 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
994 if (!ptr
) return FALSE
;
995 ret
= ptr
->funcs
->ext
.p_wglBindTexImageARB( ptr
->u
.pbuffer
, buffer
);
996 release_handle_ptr( ptr
);
1000 /***********************************************************************
1001 * wglReleaseTexImageARB
1003 * Provided by the WGL_ARB_render_texture extension.
1005 BOOL WINAPI
wglReleaseTexImageARB( HPBUFFERARB handle
, int buffer
)
1007 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1010 if (!ptr
) return FALSE
;
1011 ret
= ptr
->funcs
->ext
.p_wglReleaseTexImageARB( ptr
->u
.pbuffer
, buffer
);
1012 release_handle_ptr( ptr
);
1016 /***********************************************************************
1017 * wglSetPbufferAttribARB
1019 * Provided by the WGL_ARB_render_texture extension.
1021 BOOL WINAPI
wglSetPbufferAttribARB( HPBUFFERARB handle
, const int *attribs
)
1023 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1026 if (!ptr
) return FALSE
;
1027 ret
= ptr
->funcs
->ext
.p_wglSetPbufferAttribARB( ptr
->u
.pbuffer
, attribs
);
1028 release_handle_ptr( ptr
);
1032 /***********************************************************************
1033 * wglChoosePixelFormatARB
1035 * Provided by the WGL_ARB_pixel_format extension.
1037 BOOL WINAPI
wglChoosePixelFormatARB( HDC hdc
, const int *iattribs
, const FLOAT
*fattribs
,
1038 UINT max
, int *formats
, UINT
*count
)
1040 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1042 if (!funcs
|| !funcs
->ext
.p_wglChoosePixelFormatARB
) return FALSE
;
1043 return funcs
->ext
.p_wglChoosePixelFormatARB( hdc
, iattribs
, fattribs
, max
, formats
, count
);
1046 /***********************************************************************
1047 * wglGetPixelFormatAttribivARB
1049 * Provided by the WGL_ARB_pixel_format extension.
1051 BOOL WINAPI
wglGetPixelFormatAttribivARB( HDC hdc
, int format
, int layer
, UINT count
, const int *attribs
,
1054 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1056 if (!funcs
|| !funcs
->ext
.p_wglGetPixelFormatAttribivARB
) return FALSE
;
1057 return funcs
->ext
.p_wglGetPixelFormatAttribivARB( hdc
, format
, layer
, count
, attribs
, values
);
1060 /***********************************************************************
1061 * wglGetPixelFormatAttribfvARB
1063 * Provided by the WGL_ARB_pixel_format extension.
1065 BOOL WINAPI
wglGetPixelFormatAttribfvARB( HDC hdc
, int format
, int layer
, UINT count
, const int *attribs
,
1068 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1070 if (!funcs
|| !funcs
->ext
.p_wglGetPixelFormatAttribfvARB
) return FALSE
;
1071 return funcs
->ext
.p_wglGetPixelFormatAttribfvARB( hdc
, format
, layer
, count
, attribs
, values
);
1074 /***********************************************************************
1075 * wglCreatePbufferARB
1077 * Provided by the WGL_ARB_pbuffer extension.
1079 HPBUFFERARB WINAPI
wglCreatePbufferARB( HDC hdc
, int format
, int width
, int height
, const int *attribs
)
1082 struct wgl_pbuffer
*pbuffer
;
1083 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1085 if (!funcs
|| !funcs
->ext
.p_wglCreatePbufferARB
) return 0;
1086 if (!(pbuffer
= funcs
->ext
.p_wglCreatePbufferARB( hdc
, format
, width
, height
, attribs
))) return 0;
1087 ret
= alloc_handle( HANDLE_PBUFFER
, funcs
, pbuffer
);
1088 if (!ret
) funcs
->ext
.p_wglDestroyPbufferARB( pbuffer
);
1092 /***********************************************************************
1093 * wglGetPbufferDCARB
1095 * Provided by the WGL_ARB_pbuffer extension.
1097 HDC WINAPI
wglGetPbufferDCARB( HPBUFFERARB handle
)
1099 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1103 ret
= ptr
->funcs
->ext
.p_wglGetPbufferDCARB( ptr
->u
.pbuffer
);
1104 release_handle_ptr( ptr
);
1108 /***********************************************************************
1109 * wglReleasePbufferDCARB
1111 * Provided by the WGL_ARB_pbuffer extension.
1113 int WINAPI
wglReleasePbufferDCARB( HPBUFFERARB handle
, HDC hdc
)
1115 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1118 if (!ptr
) return FALSE
;
1119 ret
= ptr
->funcs
->ext
.p_wglReleasePbufferDCARB( ptr
->u
.pbuffer
, hdc
);
1120 release_handle_ptr( ptr
);
1124 /***********************************************************************
1125 * wglDestroyPbufferARB
1127 * Provided by the WGL_ARB_pbuffer extension.
1129 BOOL WINAPI
wglDestroyPbufferARB( HPBUFFERARB handle
)
1131 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1133 if (!ptr
) return FALSE
;
1134 ptr
->funcs
->ext
.p_wglDestroyPbufferARB( ptr
->u
.pbuffer
);
1135 free_handle_ptr( ptr
);
1139 /***********************************************************************
1140 * wglQueryPbufferARB
1142 * Provided by the WGL_ARB_pbuffer extension.
1144 BOOL WINAPI
wglQueryPbufferARB( HPBUFFERARB handle
, int attrib
, int *value
)
1146 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1149 if (!ptr
) return FALSE
;
1150 ret
= ptr
->funcs
->ext
.p_wglQueryPbufferARB( ptr
->u
.pbuffer
, attrib
, value
);
1151 release_handle_ptr( ptr
);
1155 /***********************************************************************
1156 * wglGetExtensionsStringARB
1158 * Provided by the WGL_ARB_extensions_string extension.
1160 const char * WINAPI
wglGetExtensionsStringARB( HDC hdc
)
1162 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1164 if (!funcs
|| !funcs
->ext
.p_wglGetExtensionsStringARB
) return NULL
;
1165 return (const char *)funcs
->ext
.p_wglGetExtensionsStringARB( hdc
);
1168 /***********************************************************************
1169 * wglGetExtensionsStringEXT
1171 * Provided by the WGL_EXT_extensions_string extension.
1173 const char * WINAPI
wglGetExtensionsStringEXT(void)
1175 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1177 if (!funcs
->ext
.p_wglGetExtensionsStringEXT
) return NULL
;
1178 return (const char *)funcs
->ext
.p_wglGetExtensionsStringEXT();
1181 /***********************************************************************
1182 * wglSwapIntervalEXT
1184 * Provided by the WGL_EXT_swap_control extension.
1186 BOOL WINAPI
wglSwapIntervalEXT( int interval
)
1188 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1190 if (!funcs
->ext
.p_wglSwapIntervalEXT
) return FALSE
;
1191 return funcs
->ext
.p_wglSwapIntervalEXT( interval
);
1194 /***********************************************************************
1195 * wglGetSwapIntervalEXT
1197 * Provided by the WGL_EXT_swap_control extension.
1199 int WINAPI
wglGetSwapIntervalEXT(void)
1201 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1203 if (!funcs
->ext
.p_wglGetSwapIntervalEXT
) return FALSE
;
1204 return funcs
->ext
.p_wglGetSwapIntervalEXT();
1207 /***********************************************************************
1208 * wglSetPixelFormatWINE
1210 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1212 BOOL WINAPI
wglSetPixelFormatWINE( HDC hdc
, int format
)
1214 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1216 if (!funcs
|| !funcs
->ext
.p_wglSetPixelFormatWINE
) return FALSE
;
1217 return funcs
->ext
.p_wglSetPixelFormatWINE( hdc
, format
);
1220 /***********************************************************************
1221 * wglQueryCurrentRendererIntegerWINE
1223 * Provided by the WGL_WINE_query_renderer extension.
1225 BOOL WINAPI
wglQueryCurrentRendererIntegerWINE( GLenum attribute
, GLuint
*value
)
1227 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1229 if (!funcs
->ext
.p_wglQueryCurrentRendererIntegerWINE
) return FALSE
;
1230 return funcs
->ext
.p_wglQueryCurrentRendererIntegerWINE( attribute
, value
);
1233 /***********************************************************************
1234 * wglQueryCurrentRendererStringWINE
1236 * Provided by the WGL_WINE_query_renderer extension.
1238 const GLchar
* WINAPI
wglQueryCurrentRendererStringWINE( GLenum attribute
)
1240 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1242 if (!funcs
->ext
.p_wglQueryCurrentRendererStringWINE
) return NULL
;
1243 return funcs
->ext
.p_wglQueryCurrentRendererStringWINE( attribute
);
1246 /***********************************************************************
1247 * wglQueryRendererIntegerWINE
1249 * Provided by the WGL_WINE_query_renderer extension.
1251 BOOL WINAPI
wglQueryRendererIntegerWINE( HDC dc
, GLint renderer
, GLenum attribute
, GLuint
*value
)
1253 const struct opengl_funcs
*funcs
= get_dc_funcs( dc
);
1255 if (!funcs
|| !funcs
->ext
.p_wglQueryRendererIntegerWINE
) return FALSE
;
1256 return funcs
->ext
.p_wglQueryRendererIntegerWINE( dc
, renderer
, attribute
, value
);
1259 /***********************************************************************
1260 * wglQueryRendererStringWINE
1262 * Provided by the WGL_WINE_query_renderer extension.
1264 const GLchar
* WINAPI
wglQueryRendererStringWINE( HDC dc
, GLint renderer
, GLenum attribute
)
1266 const struct opengl_funcs
*funcs
= get_dc_funcs( dc
);
1268 if (!funcs
|| !funcs
->ext
.p_wglQueryRendererStringWINE
) return NULL
;
1269 return funcs
->ext
.p_wglQueryRendererStringWINE( dc
, renderer
, attribute
);
1272 /***********************************************************************
1273 * wglUseFontBitmaps_common
1275 static BOOL
wglUseFontBitmaps_common( HDC hdc
, DWORD first
, DWORD count
, DWORD listBase
, BOOL unicode
)
1277 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1279 unsigned int glyph
, size
= 0;
1280 void *bitmap
= NULL
, *gl_bitmap
= NULL
;
1284 funcs
->gl
.p_glGetIntegerv(GL_UNPACK_ALIGNMENT
, &org_alignment
);
1285 funcs
->gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 4);
1287 for (glyph
= first
; glyph
< first
+ count
; glyph
++) {
1288 unsigned int needed_size
, height
, width
, width_int
;
1291 needed_size
= GetGlyphOutlineW(hdc
, glyph
, GGO_BITMAP
, &gm
, 0, NULL
, &identity
);
1293 needed_size
= GetGlyphOutlineA(hdc
, glyph
, GGO_BITMAP
, &gm
, 0, NULL
, &identity
);
1295 TRACE("Glyph: %3d / List: %d size %d\n", glyph
, listBase
, needed_size
);
1296 if (needed_size
== GDI_ERROR
) {
1301 if (needed_size
> size
) {
1303 HeapFree(GetProcessHeap(), 0, bitmap
);
1304 HeapFree(GetProcessHeap(), 0, gl_bitmap
);
1305 bitmap
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
1306 gl_bitmap
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
1308 if (needed_size
!= 0) {
1310 ret
= (GetGlyphOutlineW(hdc
, glyph
, GGO_BITMAP
, &gm
,
1311 size
, bitmap
, &identity
) != GDI_ERROR
);
1313 ret
= (GetGlyphOutlineA(hdc
, glyph
, GGO_BITMAP
, &gm
,
1314 size
, bitmap
, &identity
) != GDI_ERROR
);
1318 if (TRACE_ON(wgl
)) {
1319 unsigned int bitmask
;
1320 unsigned char *bitmap_
= bitmap
;
1322 TRACE(" - bbox: %d x %d\n", gm
.gmBlackBoxX
, gm
.gmBlackBoxY
);
1323 TRACE(" - origin: (%d, %d)\n", gm
.gmptGlyphOrigin
.x
, gm
.gmptGlyphOrigin
.y
);
1324 TRACE(" - increment: %d - %d\n", gm
.gmCellIncX
, gm
.gmCellIncY
);
1325 if (needed_size
!= 0) {
1326 TRACE(" - bitmap:\n");
1327 for (height
= 0; height
< gm
.gmBlackBoxY
; height
++) {
1329 for (width
= 0, bitmask
= 0x80; width
< gm
.gmBlackBoxX
; width
++, bitmask
>>= 1) {
1334 if (*bitmap_
& bitmask
)
1339 bitmap_
+= (4 - ((UINT_PTR
)bitmap_
& 0x03));
1345 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1346 * glyph for it to be drawn properly.
1348 if (needed_size
!= 0) {
1349 width_int
= (gm
.gmBlackBoxX
+ 31) / 32;
1350 for (height
= 0; height
< gm
.gmBlackBoxY
; height
++) {
1351 for (width
= 0; width
< width_int
; width
++) {
1352 ((int *) gl_bitmap
)[(gm
.gmBlackBoxY
- height
- 1) * width_int
+ width
] =
1353 ((int *) bitmap
)[height
* width_int
+ width
];
1358 funcs
->gl
.p_glNewList(listBase
++, GL_COMPILE
);
1359 if (needed_size
!= 0) {
1360 funcs
->gl
.p_glBitmap(gm
.gmBlackBoxX
, gm
.gmBlackBoxY
,
1361 0 - gm
.gmptGlyphOrigin
.x
, (int) gm
.gmBlackBoxY
- gm
.gmptGlyphOrigin
.y
,
1362 gm
.gmCellIncX
, gm
.gmCellIncY
,
1365 /* This is the case of 'empty' glyphs like the space character */
1366 funcs
->gl
.p_glBitmap(0, 0, 0, 0, gm
.gmCellIncX
, gm
.gmCellIncY
, NULL
);
1368 funcs
->gl
.p_glEndList();
1371 funcs
->gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, org_alignment
);
1372 HeapFree(GetProcessHeap(), 0, bitmap
);
1373 HeapFree(GetProcessHeap(), 0, gl_bitmap
);
1377 /***********************************************************************
1378 * wglUseFontBitmapsA (OPENGL32.@)
1380 BOOL WINAPI
wglUseFontBitmapsA(HDC hdc
, DWORD first
, DWORD count
, DWORD listBase
)
1382 return wglUseFontBitmaps_common( hdc
, first
, count
, listBase
, FALSE
);
1385 /***********************************************************************
1386 * wglUseFontBitmapsW (OPENGL32.@)
1388 BOOL WINAPI
wglUseFontBitmapsW(HDC hdc
, DWORD first
, DWORD count
, DWORD listBase
)
1390 return wglUseFontBitmaps_common( hdc
, first
, count
, listBase
, TRUE
);
1393 /* FIXME: should probably have a glu.h header */
1395 typedef struct GLUtesselator GLUtesselator
;
1396 typedef void (WINAPI
*_GLUfuncptr
)(void);
1398 #define GLU_TESS_BEGIN 100100
1399 #define GLU_TESS_VERTEX 100101
1400 #define GLU_TESS_END 100102
1402 static GLUtesselator
* (WINAPI
*pgluNewTess
)(void);
1403 static void (WINAPI
*pgluDeleteTess
)(GLUtesselator
*tess
);
1404 static void (WINAPI
*pgluTessNormal
)(GLUtesselator
*tess
, GLdouble x
, GLdouble y
, GLdouble z
);
1405 static void (WINAPI
*pgluTessBeginPolygon
)(GLUtesselator
*tess
, void *polygon_data
);
1406 static void (WINAPI
*pgluTessEndPolygon
)(GLUtesselator
*tess
);
1407 static void (WINAPI
*pgluTessCallback
)(GLUtesselator
*tess
, GLenum which
, _GLUfuncptr fn
);
1408 static void (WINAPI
*pgluTessBeginContour
)(GLUtesselator
*tess
);
1409 static void (WINAPI
*pgluTessEndContour
)(GLUtesselator
*tess
);
1410 static void (WINAPI
*pgluTessVertex
)(GLUtesselator
*tess
, GLdouble
*location
, GLvoid
* data
);
1412 static HMODULE
load_libglu(void)
1414 static const WCHAR glu32W
[] = {'g','l','u','3','2','.','d','l','l',0};
1415 static BOOL already_loaded
;
1416 static HMODULE module
;
1418 if (already_loaded
) return module
;
1419 already_loaded
= TRUE
;
1421 TRACE("Trying to load GLU library\n");
1422 module
= LoadLibraryW( glu32W
);
1425 WARN("Failed to load glu32\n");
1428 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1429 LOAD_FUNCPTR(gluNewTess
);
1430 LOAD_FUNCPTR(gluDeleteTess
);
1431 LOAD_FUNCPTR(gluTessBeginContour
);
1432 LOAD_FUNCPTR(gluTessNormal
);
1433 LOAD_FUNCPTR(gluTessBeginPolygon
);
1434 LOAD_FUNCPTR(gluTessCallback
);
1435 LOAD_FUNCPTR(gluTessEndContour
);
1436 LOAD_FUNCPTR(gluTessEndPolygon
);
1437 LOAD_FUNCPTR(gluTessVertex
);
1442 static void fixed_to_double(POINTFX fixed
, UINT em_size
, GLdouble vertex
[3])
1444 vertex
[0] = (fixed
.x
.value
+ (GLdouble
)fixed
.x
.fract
/ (1 << 16)) / em_size
;
1445 vertex
[1] = (fixed
.y
.value
+ (GLdouble
)fixed
.y
.fract
/ (1 << 16)) / em_size
;
1449 static void WINAPI
tess_callback_vertex(GLvoid
*vertex
)
1451 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1452 GLdouble
*dbl
= vertex
;
1453 TRACE("%f, %f, %f\n", dbl
[0], dbl
[1], dbl
[2]);
1454 funcs
->gl
.p_glVertex3dv(vertex
);
1457 static void WINAPI
tess_callback_begin(GLenum which
)
1459 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1460 TRACE("%d\n", which
);
1461 funcs
->gl
.p_glBegin(which
);
1464 static void WINAPI
tess_callback_end(void)
1466 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1468 funcs
->gl
.p_glEnd();
1471 typedef struct _bezier_vector
{
1476 static double bezier_deviation_squared(const bezier_vector
*p
)
1478 bezier_vector deviation
;
1479 bezier_vector vertex
;
1484 vertex
.x
= (p
[0].x
+ p
[1].x
*2 + p
[2].x
)/4 - p
[0].x
;
1485 vertex
.y
= (p
[0].y
+ p
[1].y
*2 + p
[2].y
)/4 - p
[0].y
;
1487 base
.x
= p
[2].x
- p
[0].x
;
1488 base
.y
= p
[2].y
- p
[0].y
;
1490 base_length
= sqrt(base
.x
*base
.x
+ base
.y
*base
.y
);
1491 base
.x
/= base_length
;
1492 base
.y
/= base_length
;
1494 dot
= base
.x
*vertex
.x
+ base
.y
*vertex
.y
;
1495 dot
= min(max(dot
, 0.0), base_length
);
1499 deviation
.x
= vertex
.x
-base
.x
;
1500 deviation
.y
= vertex
.y
-base
.y
;
1502 return deviation
.x
*deviation
.x
+ deviation
.y
*deviation
.y
;
1505 static int bezier_approximate(const bezier_vector
*p
, bezier_vector
*points
, FLOAT deviation
)
1507 bezier_vector first_curve
[3];
1508 bezier_vector second_curve
[3];
1509 bezier_vector vertex
;
1512 if(bezier_deviation_squared(p
) <= deviation
*deviation
)
1519 vertex
.x
= (p
[0].x
+ p
[1].x
*2 + p
[2].x
)/4;
1520 vertex
.y
= (p
[0].y
+ p
[1].y
*2 + p
[2].y
)/4;
1522 first_curve
[0] = p
[0];
1523 first_curve
[1].x
= (p
[0].x
+ p
[1].x
)/2;
1524 first_curve
[1].y
= (p
[0].y
+ p
[1].y
)/2;
1525 first_curve
[2] = vertex
;
1527 second_curve
[0] = vertex
;
1528 second_curve
[1].x
= (p
[2].x
+ p
[1].x
)/2;
1529 second_curve
[1].y
= (p
[2].y
+ p
[1].y
)/2;
1530 second_curve
[2] = p
[2];
1532 total_vertices
= bezier_approximate(first_curve
, points
, deviation
);
1534 points
+= total_vertices
;
1535 total_vertices
+= bezier_approximate(second_curve
, points
, deviation
);
1536 return total_vertices
;
1539 /***********************************************************************
1540 * wglUseFontOutlines_common
1542 static BOOL
wglUseFontOutlines_common(HDC hdc
,
1549 LPGLYPHMETRICSFLOAT lpgmf
,
1552 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1554 GLUtesselator
*tess
= NULL
;
1556 HFONT old_font
, unscaled_font
;
1557 UINT em_size
= 1024;
1560 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc
, first
, count
,
1561 listBase
, deviation
, extrusion
, format
, lpgmf
, unicode
? "W" : "A");
1563 if(deviation
<= 0.0)
1564 deviation
= 1.0/em_size
;
1566 if(format
== WGL_FONT_POLYGONS
)
1570 ERR("glu32 is required for this function but isn't available\n");
1574 tess
= pgluNewTess();
1575 if(!tess
) return FALSE
;
1576 pgluTessCallback(tess
, GLU_TESS_VERTEX
, (_GLUfuncptr
)tess_callback_vertex
);
1577 pgluTessCallback(tess
, GLU_TESS_BEGIN
, (_GLUfuncptr
)tess_callback_begin
);
1578 pgluTessCallback(tess
, GLU_TESS_END
, tess_callback_end
);
1581 GetObjectW(GetCurrentObject(hdc
, OBJ_FONT
), sizeof(lf
), &lf
);
1582 rc
.left
= rc
.right
= rc
.bottom
= 0;
1584 DPtoLP(hdc
, (POINT
*)&rc
, 2);
1585 lf
.lfHeight
= -abs(rc
.top
- rc
.bottom
);
1586 lf
.lfOrientation
= lf
.lfEscapement
= 0;
1587 unscaled_font
= CreateFontIndirectW(&lf
);
1588 old_font
= SelectObject(hdc
, unscaled_font
);
1590 for (glyph
= first
; glyph
< first
+ count
; glyph
++)
1595 TTPOLYGONHEADER
*pph
;
1597 GLdouble
*vertices
= NULL
;
1598 int vertex_total
= -1;
1601 needed
= GetGlyphOutlineW(hdc
, glyph
, GGO_NATIVE
, &gm
, 0, NULL
, &identity
);
1603 needed
= GetGlyphOutlineA(hdc
, glyph
, GGO_NATIVE
, &gm
, 0, NULL
, &identity
);
1605 if(needed
== GDI_ERROR
)
1608 buf
= HeapAlloc(GetProcessHeap(), 0, needed
);
1611 GetGlyphOutlineW(hdc
, glyph
, GGO_NATIVE
, &gm
, needed
, buf
, &identity
);
1613 GetGlyphOutlineA(hdc
, glyph
, GGO_NATIVE
, &gm
, needed
, buf
, &identity
);
1615 TRACE("glyph %d\n", glyph
);
1619 lpgmf
->gmfBlackBoxX
= (float)gm
.gmBlackBoxX
/ em_size
;
1620 lpgmf
->gmfBlackBoxY
= (float)gm
.gmBlackBoxY
/ em_size
;
1621 lpgmf
->gmfptGlyphOrigin
.x
= (float)gm
.gmptGlyphOrigin
.x
/ em_size
;
1622 lpgmf
->gmfptGlyphOrigin
.y
= (float)gm
.gmptGlyphOrigin
.y
/ em_size
;
1623 lpgmf
->gmfCellIncX
= (float)gm
.gmCellIncX
/ em_size
;
1624 lpgmf
->gmfCellIncY
= (float)gm
.gmCellIncY
/ em_size
;
1626 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf
->gmfBlackBoxX
, lpgmf
->gmfBlackBoxY
,
1627 lpgmf
->gmfptGlyphOrigin
.x
, lpgmf
->gmfptGlyphOrigin
.y
, lpgmf
->gmfCellIncX
, lpgmf
->gmfCellIncY
);
1631 funcs
->gl
.p_glNewList(listBase
++, GL_COMPILE
);
1632 funcs
->gl
.p_glFrontFace(GL_CCW
);
1633 if(format
== WGL_FONT_POLYGONS
)
1635 funcs
->gl
.p_glNormal3d(0.0, 0.0, 1.0);
1636 pgluTessNormal(tess
, 0, 0, 1);
1637 pgluTessBeginPolygon(tess
, NULL
);
1642 if(vertex_total
!= -1)
1643 vertices
= HeapAlloc(GetProcessHeap(), 0, vertex_total
* 3 * sizeof(GLdouble
));
1646 pph
= (TTPOLYGONHEADER
*)buf
;
1647 while((BYTE
*)pph
< buf
+ needed
)
1649 GLdouble previous
[3];
1650 fixed_to_double(pph
->pfxStart
, em_size
, previous
);
1653 TRACE("\tstart %d, %d\n", pph
->pfxStart
.x
.value
, pph
->pfxStart
.y
.value
);
1655 if(format
== WGL_FONT_POLYGONS
)
1656 pgluTessBeginContour(tess
);
1658 funcs
->gl
.p_glBegin(GL_LINE_LOOP
);
1662 fixed_to_double(pph
->pfxStart
, em_size
, vertices
);
1663 if(format
== WGL_FONT_POLYGONS
)
1664 pgluTessVertex(tess
, vertices
, vertices
);
1666 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1671 ppc
= (TTPOLYCURVE
*)((char*)pph
+ sizeof(*pph
));
1672 while((char*)ppc
< (char*)pph
+ pph
->cb
)
1677 switch(ppc
->wType
) {
1679 for(i
= 0; i
< ppc
->cpfx
; i
++)
1683 TRACE("\t\tline to %d, %d\n",
1684 ppc
->apfx
[i
].x
.value
, ppc
->apfx
[i
].y
.value
);
1685 fixed_to_double(ppc
->apfx
[i
], em_size
, vertices
);
1686 if(format
== WGL_FONT_POLYGONS
)
1687 pgluTessVertex(tess
, vertices
, vertices
);
1689 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1692 fixed_to_double(ppc
->apfx
[i
], em_size
, previous
);
1697 case TT_PRIM_QSPLINE
:
1698 for(i
= 0; i
< ppc
->cpfx
-1; i
++)
1700 bezier_vector curve
[3];
1701 bezier_vector
*points
;
1702 GLdouble curve_vertex
[3];
1705 TRACE("\t\tcurve %d,%d %d,%d\n",
1706 ppc
->apfx
[i
].x
.value
, ppc
->apfx
[i
].y
.value
,
1707 ppc
->apfx
[i
+ 1].x
.value
, ppc
->apfx
[i
+ 1].y
.value
);
1709 curve
[0].x
= previous
[0];
1710 curve
[0].y
= previous
[1];
1711 fixed_to_double(ppc
->apfx
[i
], em_size
, curve_vertex
);
1712 curve
[1].x
= curve_vertex
[0];
1713 curve
[1].y
= curve_vertex
[1];
1714 fixed_to_double(ppc
->apfx
[i
+ 1], em_size
, curve_vertex
);
1715 curve
[2].x
= curve_vertex
[0];
1716 curve
[2].y
= curve_vertex
[1];
1719 curve
[2].x
= (curve
[1].x
+ curve
[2].x
)/2;
1720 curve
[2].y
= (curve
[1].y
+ curve
[2].y
)/2;
1722 num
= bezier_approximate(curve
, NULL
, deviation
);
1723 points
= HeapAlloc(GetProcessHeap(), 0, num
*sizeof(bezier_vector
));
1724 num
= bezier_approximate(curve
, points
, deviation
);
1725 vertex_total
+= num
;
1728 for(j
=0; j
<num
; j
++)
1730 TRACE("\t\t\tvertex at %f,%f\n", points
[j
].x
, points
[j
].y
);
1731 vertices
[0] = points
[j
].x
;
1732 vertices
[1] = points
[j
].y
;
1734 if(format
== WGL_FONT_POLYGONS
)
1735 pgluTessVertex(tess
, vertices
, vertices
);
1737 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1741 HeapFree(GetProcessHeap(), 0, points
);
1742 previous
[0] = curve
[2].x
;
1743 previous
[1] = curve
[2].y
;
1747 ERR("\t\tcurve type = %d\n", ppc
->wType
);
1748 if(format
== WGL_FONT_POLYGONS
)
1749 pgluTessEndContour(tess
);
1751 funcs
->gl
.p_glEnd();
1755 ppc
= (TTPOLYCURVE
*)((char*)ppc
+ sizeof(*ppc
) +
1756 (ppc
->cpfx
- 1) * sizeof(POINTFX
));
1758 if(format
== WGL_FONT_POLYGONS
)
1759 pgluTessEndContour(tess
);
1761 funcs
->gl
.p_glEnd();
1762 pph
= (TTPOLYGONHEADER
*)((char*)pph
+ pph
->cb
);
1767 if(format
== WGL_FONT_POLYGONS
)
1768 pgluTessEndPolygon(tess
);
1769 funcs
->gl
.p_glTranslated((GLdouble
)gm
.gmCellIncX
/ em_size
, (GLdouble
)gm
.gmCellIncY
/ em_size
, 0.0);
1770 funcs
->gl
.p_glEndList();
1771 HeapFree(GetProcessHeap(), 0, buf
);
1772 HeapFree(GetProcessHeap(), 0, vertices
);
1776 DeleteObject(SelectObject(hdc
, old_font
));
1777 if(format
== WGL_FONT_POLYGONS
)
1778 pgluDeleteTess(tess
);
1783 /***********************************************************************
1784 * wglUseFontOutlinesA (OPENGL32.@)
1786 BOOL WINAPI
wglUseFontOutlinesA(HDC hdc
,
1793 LPGLYPHMETRICSFLOAT lpgmf
)
1795 return wglUseFontOutlines_common(hdc
, first
, count
, listBase
, deviation
, extrusion
, format
, lpgmf
, FALSE
);
1798 /***********************************************************************
1799 * wglUseFontOutlinesW (OPENGL32.@)
1801 BOOL WINAPI
wglUseFontOutlinesW(HDC hdc
,
1808 LPGLYPHMETRICSFLOAT lpgmf
)
1810 return wglUseFontOutlines_common(hdc
, first
, count
, listBase
, deviation
, extrusion
, format
, lpgmf
, TRUE
);
1813 /***********************************************************************
1814 * glDebugEntry (OPENGL32.@)
1816 GLint WINAPI
glDebugEntry( GLint unknown1
, GLint unknown2
)
1821 static GLubyte
*filter_extensions_list(const char *extensions
, const char *disabled
)
1826 p
= str
= HeapAlloc(GetProcessHeap(), 0, strlen(extensions
) + 2);
1830 TRACE( "GL_EXTENSIONS:\n" );
1834 while (*extensions
== ' ')
1838 if (!(end
= strchr(extensions
, ' ')))
1839 end
= extensions
+ strlen(extensions
);
1840 memcpy(p
, extensions
, end
- extensions
);
1841 p
[end
- extensions
] = 0;
1842 if (!has_extension(disabled
, p
, strlen(p
)))
1844 TRACE("++ %s\n", p
);
1845 p
+= end
- extensions
;
1850 TRACE("-- %s (disabled by config)\n", p
);
1855 return (GLubyte
*)str
;
1858 static GLuint
*filter_extensions_index(const char *disabled
)
1860 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1861 const char *ext
, *end
, *gl_ext
;
1862 GLuint
*disabled_exts
, *new_disabled_exts
;
1863 unsigned int i
= 0, j
, disabled_size
;
1864 GLint extensions_count
;
1866 if (!funcs
->ext
.p_glGetStringi
)
1868 void **func_ptr
= (void **)&funcs
->ext
.p_glGetStringi
;
1870 *func_ptr
= funcs
->wgl
.p_wglGetProcAddress("glGetStringi");
1871 if (!funcs
->ext
.p_glGetStringi
)
1875 funcs
->gl
.p_glGetIntegerv(GL_NUM_EXTENSIONS
, &extensions_count
);
1877 disabled_exts
= HeapAlloc(GetProcessHeap(), 0, disabled_size
* sizeof(*disabled_exts
));
1881 TRACE( "GL_EXTENSIONS:\n" );
1883 for (j
= 0; j
< extensions_count
; ++j
)
1885 gl_ext
= (const char *)funcs
->ext
.p_glGetStringi(GL_EXTENSIONS
, j
);
1893 TRACE("++ %s\n", gl_ext
);
1896 if (!(end
= strchr(ext
, ' ')))
1897 end
= ext
+ strlen(ext
);
1899 if (!strncmp(gl_ext
, ext
, end
- ext
) && !gl_ext
[end
- ext
])
1901 if (i
+ 1 == disabled_size
)
1904 new_disabled_exts
= HeapReAlloc(GetProcessHeap(), 0, disabled_exts
,
1905 disabled_size
* sizeof(*disabled_exts
));
1906 if (!new_disabled_exts
)
1908 disabled_exts
[i
] = ~0u;
1909 return disabled_exts
;
1911 disabled_exts
= new_disabled_exts
;
1913 TRACE("-- %s (disabled by config)\n", gl_ext
);
1914 disabled_exts
[i
++] = j
;
1920 disabled_exts
[i
] = ~0u;
1921 return disabled_exts
;
1924 /* build the extension string by filtering out the disabled extensions */
1925 static BOOL
filter_extensions(const char *extensions
, GLubyte
**exts_list
, GLuint
**disabled_exts
)
1927 static const char *disabled
;
1935 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1936 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\OpenGL", &hkey
))
1938 if (!RegQueryValueExA( hkey
, "DisabledExtensions", 0, NULL
, NULL
, &size
))
1940 str
= HeapAlloc( GetProcessHeap(), 0, size
);
1941 if (RegQueryValueExA( hkey
, "DisabledExtensions", 0, NULL
, (BYTE
*)str
, &size
)) *str
= 0;
1943 RegCloseKey( hkey
);
1947 if (InterlockedCompareExchangePointer( (void **)&disabled
, str
, NULL
))
1948 HeapFree( GetProcessHeap(), 0, str
);
1956 if (extensions
&& !*exts_list
)
1957 *exts_list
= filter_extensions_list(extensions
, disabled
);
1959 if (!*disabled_exts
)
1960 *disabled_exts
= filter_extensions_index(disabled
);
1962 return (exts_list
&& *exts_list
) || *disabled_exts
;
1965 /***********************************************************************
1966 * glGetString (OPENGL32.@)
1968 const GLubyte
* WINAPI
glGetString( GLenum name
)
1970 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1971 const GLubyte
*ret
= funcs
->gl
.p_glGetString( name
);
1973 if (name
== GL_EXTENSIONS
&& ret
)
1975 struct wgl_handle
*ptr
= get_current_context_ptr();
1976 if (ptr
->u
.context
->extensions
||
1977 filter_extensions((const char *)ret
, &ptr
->u
.context
->extensions
, &ptr
->u
.context
->disabled_exts
))
1978 ret
= ptr
->u
.context
->extensions
;
1983 /***********************************************************************
1984 * OpenGL initialisation routine
1986 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
1990 case DLL_PROCESS_ATTACH
:
1991 NtCurrentTeb()->glTable
= &null_opengl_funcs
;
1993 case DLL_THREAD_ATTACH
:
1994 NtCurrentTeb()->glTable
= &null_opengl_funcs
;