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
);
277 if (!funcs
|| !funcs
->ext
.p_wglCreateContextAttribsARB
) return 0;
278 if (share
&& !(share_ptr
= get_handle_ptr( share
, HANDLE_CONTEXT
))) return 0;
279 if ((drv_ctx
= funcs
->ext
.p_wglCreateContextAttribsARB( hdc
,
280 share_ptr
? share_ptr
->u
.context
->drv_ctx
: NULL
, attribs
)))
282 if ((context
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*context
) )))
284 enum wgl_handle_type type
= HANDLE_CONTEXT
;
290 if (attribs
[0] == WGL_CONTEXT_MAJOR_VERSION_ARB
)
293 type
= HANDLE_CONTEXT_V3
;
300 context
->drv_ctx
= drv_ctx
;
301 if (!(ret
= alloc_handle( type
, funcs
, context
)))
302 HeapFree( GetProcessHeap(), 0, context
);
304 if (!ret
) funcs
->wgl
.p_wglDeleteContext( drv_ctx
);
306 release_handle_ptr( share_ptr
);
311 /***********************************************************************
312 * wglMakeContextCurrentARB
314 * Provided by the WGL_ARB_make_current_read extension.
316 BOOL WINAPI
wglMakeContextCurrentARB( HDC draw_hdc
, HDC read_hdc
, HGLRC hglrc
)
319 struct wgl_handle
*ptr
, *prev
= get_current_context_ptr();
323 if (!(ptr
= get_handle_ptr( hglrc
, HANDLE_CONTEXT
))) return FALSE
;
324 if (!ptr
->u
.context
->tid
|| ptr
->u
.context
->tid
== GetCurrentThreadId())
326 ret
= (ptr
->funcs
->ext
.p_wglMakeContextCurrentARB
&&
327 ptr
->funcs
->ext
.p_wglMakeContextCurrentARB( draw_hdc
, read_hdc
,
328 ptr
->u
.context
->drv_ctx
));
331 if (prev
) prev
->u
.context
->tid
= 0;
332 ptr
->u
.context
->tid
= GetCurrentThreadId();
333 ptr
->u
.context
->draw_dc
= draw_hdc
;
334 ptr
->u
.context
->read_dc
= read_hdc
;
335 NtCurrentTeb()->glCurrentRC
= hglrc
;
336 NtCurrentTeb()->glTable
= ptr
->funcs
;
341 SetLastError( ERROR_BUSY
);
344 release_handle_ptr( ptr
);
348 if (!prev
->funcs
->wgl
.p_wglMakeCurrent( 0, NULL
)) return FALSE
;
349 prev
->u
.context
->tid
= 0;
350 NtCurrentTeb()->glCurrentRC
= 0;
351 NtCurrentTeb()->glTable
= &null_opengl_funcs
;
356 /***********************************************************************
357 * wglGetCurrentReadDCARB
359 * Provided by the WGL_ARB_make_current_read extension.
361 HDC WINAPI
wglGetCurrentReadDCARB(void)
363 struct wgl_handle
*ptr
= get_current_context_ptr();
366 return ptr
->u
.context
->read_dc
;
369 /***********************************************************************
370 * wglShareLists (OPENGL32.@)
372 BOOL WINAPI
wglShareLists(HGLRC hglrcSrc
, HGLRC hglrcDst
)
375 struct wgl_handle
*src
, *dst
;
377 if (!(src
= get_handle_ptr( hglrcSrc
, HANDLE_CONTEXT
))) return FALSE
;
378 if ((dst
= get_handle_ptr( hglrcDst
, HANDLE_CONTEXT
)))
380 if (src
->funcs
!= dst
->funcs
) SetLastError( ERROR_INVALID_HANDLE
);
381 else ret
= src
->funcs
->wgl
.p_wglShareLists( src
->u
.context
->drv_ctx
, dst
->u
.context
->drv_ctx
);
383 release_handle_ptr( dst
);
384 release_handle_ptr( src
);
388 /***********************************************************************
389 * wglGetCurrentDC (OPENGL32.@)
391 HDC WINAPI
wglGetCurrentDC(void)
393 struct wgl_handle
*ptr
= get_current_context_ptr();
396 return ptr
->u
.context
->draw_dc
;
399 /***********************************************************************
400 * wglCreateContext (OPENGL32.@)
402 HGLRC WINAPI
wglCreateContext(HDC hdc
)
405 struct wgl_context
*drv_ctx
;
406 struct opengl_context
*context
;
407 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
409 if (!funcs
) return 0;
410 if (!(drv_ctx
= funcs
->wgl
.p_wglCreateContext( hdc
))) return 0;
411 if ((context
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*context
) )))
413 context
->drv_ctx
= drv_ctx
;
414 if (!(ret
= alloc_handle( HANDLE_CONTEXT
, funcs
, context
)))
415 HeapFree( GetProcessHeap(), 0, context
);
417 if (!ret
) funcs
->wgl
.p_wglDeleteContext( drv_ctx
);
421 /***********************************************************************
422 * wglGetCurrentContext (OPENGL32.@)
424 HGLRC WINAPI
wglGetCurrentContext(void)
426 return NtCurrentTeb()->glCurrentRC
;
429 /***********************************************************************
430 * wglDescribePixelFormat (OPENGL32.@)
432 INT WINAPI
wglDescribePixelFormat(HDC hdc
, INT format
, UINT size
, PIXELFORMATDESCRIPTOR
*descr
)
434 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
435 if (!funcs
) return 0;
436 return funcs
->wgl
.p_wglDescribePixelFormat( hdc
, format
, size
, descr
);
439 /***********************************************************************
440 * wglChoosePixelFormat (OPENGL32.@)
442 INT WINAPI
wglChoosePixelFormat(HDC hdc
, const PIXELFORMATDESCRIPTOR
* ppfd
)
444 PIXELFORMATDESCRIPTOR format
, best
;
445 int i
, count
, best_format
;
446 int bestDBuffer
= -1, bestStereo
= -1;
448 TRACE_(wgl
)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
449 "accum %u depth %u stencil %u aux %u\n",
450 hdc
, ppfd
, ppfd
->nSize
, ppfd
->nVersion
, ppfd
->dwFlags
, ppfd
->iPixelType
,
451 ppfd
->cColorBits
, ppfd
->cRedBits
, ppfd
->cGreenBits
, ppfd
->cBlueBits
, ppfd
->cAlphaBits
,
452 ppfd
->cAccumBits
, ppfd
->cDepthBits
, ppfd
->cStencilBits
, ppfd
->cAuxBuffers
);
454 count
= wglDescribePixelFormat( hdc
, 0, 0, NULL
);
455 if (!count
) return 0;
459 best
.cAlphaBits
= -1;
460 best
.cColorBits
= -1;
461 best
.cDepthBits
= -1;
462 best
.cStencilBits
= -1;
463 best
.cAuxBuffers
= -1;
465 for (i
= 1; i
<= count
; i
++)
467 if (!wglDescribePixelFormat( hdc
, i
, sizeof(format
), &format
)) continue;
469 if (ppfd
->iPixelType
!= format
.iPixelType
)
471 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i
);
475 /* only use bitmap capable for formats for bitmap rendering */
476 if( (ppfd
->dwFlags
& PFD_DRAW_TO_BITMAP
) != (format
.dwFlags
& PFD_DRAW_TO_BITMAP
))
478 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i
);
482 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
483 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
484 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
485 * formats without the given flag set.
486 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
487 * has indicated that a format without stereo is returned when stereo is unavailable.
488 * So in case PFD_STEREO is set, formats that support it should have priority above formats
489 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
491 * To summarize the following is most likely the correct behavior:
492 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
493 * stereo set -> prefer stereo formats, but also accept non-stereo formats
494 * stereo don't care -> it doesn't matter whether we get stereo or not
496 * In Wine we will treat non-stereo the same way as don't care because it makes
497 * format selection even more complicated and second drivers with Stereo advertise
498 * each format twice anyway.
501 /* Doublebuffer, see the comments above */
502 if (!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
))
504 if (((ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) != bestDBuffer
) &&
505 ((format
.dwFlags
& PFD_DOUBLEBUFFER
) == (ppfd
->dwFlags
& PFD_DOUBLEBUFFER
)))
508 if (bestDBuffer
!= -1 && (format
.dwFlags
& PFD_DOUBLEBUFFER
) != bestDBuffer
) continue;
511 /* Stereo, see the comments above. */
512 if (!(ppfd
->dwFlags
& PFD_STEREO_DONTCARE
))
514 if (((ppfd
->dwFlags
& PFD_STEREO
) != bestStereo
) &&
515 ((format
.dwFlags
& PFD_STEREO
) == (ppfd
->dwFlags
& PFD_STEREO
)))
518 if (bestStereo
!= -1 && (format
.dwFlags
& PFD_STEREO
) != bestStereo
) continue;
521 /* Below we will do a number of checks to select the 'best' pixelformat.
522 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
523 * The code works by trying to match the most important options as close as possible.
524 * When a reasonable format is found, we will try to match more options.
525 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
526 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
527 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
529 if (ppfd
->cColorBits
)
531 if (((ppfd
->cColorBits
> best
.cColorBits
) && (format
.cColorBits
> best
.cColorBits
)) ||
532 ((format
.cColorBits
>= ppfd
->cColorBits
) && (format
.cColorBits
< best
.cColorBits
)))
535 if (best
.cColorBits
!= format
.cColorBits
) /* Do further checks if the format is compatible */
537 TRACE( "color mismatch for iPixelFormat=%d\n", i
);
541 if (ppfd
->cAlphaBits
)
543 if (((ppfd
->cAlphaBits
> best
.cAlphaBits
) && (format
.cAlphaBits
> best
.cAlphaBits
)) ||
544 ((format
.cAlphaBits
>= ppfd
->cAlphaBits
) && (format
.cAlphaBits
< best
.cAlphaBits
)))
547 if (best
.cAlphaBits
!= format
.cAlphaBits
)
549 TRACE( "alpha mismatch for iPixelFormat=%d\n", i
);
553 if (ppfd
->cDepthBits
)
555 if (((ppfd
->cDepthBits
> best
.cDepthBits
) && (format
.cDepthBits
> best
.cDepthBits
)) ||
556 ((format
.cDepthBits
>= ppfd
->cDepthBits
) && (format
.cDepthBits
< best
.cDepthBits
)))
559 if (best
.cDepthBits
!= format
.cDepthBits
)
561 TRACE( "depth mismatch for iPixelFormat=%d\n", i
);
565 if (ppfd
->cStencilBits
)
567 if (((ppfd
->cStencilBits
> best
.cStencilBits
) && (format
.cStencilBits
> best
.cStencilBits
)) ||
568 ((format
.cStencilBits
>= ppfd
->cStencilBits
) && (format
.cStencilBits
< best
.cStencilBits
)))
571 if (best
.cStencilBits
!= format
.cStencilBits
)
573 TRACE( "stencil mismatch for iPixelFormat=%d\n", i
);
577 if (ppfd
->cAuxBuffers
)
579 if (((ppfd
->cAuxBuffers
> best
.cAuxBuffers
) && (format
.cAuxBuffers
> best
.cAuxBuffers
)) ||
580 ((format
.cAuxBuffers
>= ppfd
->cAuxBuffers
) && (format
.cAuxBuffers
< best
.cAuxBuffers
)))
583 if (best
.cAuxBuffers
!= format
.cAuxBuffers
)
585 TRACE( "aux mismatch for iPixelFormat=%d\n", i
);
594 bestDBuffer
= format
.dwFlags
& PFD_DOUBLEBUFFER
;
595 bestStereo
= format
.dwFlags
& PFD_STEREO
;
598 TRACE( "returning %u\n", best_format
);
602 /***********************************************************************
603 * wglGetPixelFormat (OPENGL32.@)
605 INT WINAPI
wglGetPixelFormat(HDC hdc
)
607 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
608 if (!funcs
) return 0;
609 return funcs
->wgl
.p_wglGetPixelFormat( hdc
);
612 /***********************************************************************
613 * wglSetPixelFormat(OPENGL32.@)
615 BOOL WINAPI
wglSetPixelFormat( HDC hdc
, INT format
, const PIXELFORMATDESCRIPTOR
*descr
)
617 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
618 if (!funcs
) return FALSE
;
619 return funcs
->wgl
.p_wglSetPixelFormat( hdc
, format
, descr
);
622 /***********************************************************************
623 * wglSwapBuffers (OPENGL32.@)
625 BOOL WINAPI DECLSPEC_HOTPATCH
wglSwapBuffers( HDC hdc
)
627 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
629 if (!funcs
|| !funcs
->wgl
.p_wglSwapBuffers
) return FALSE
;
630 if (!funcs
->wgl
.p_wglSwapBuffers( hdc
)) return FALSE
;
634 static long prev_time
, start_time
;
635 static unsigned long frames
, frames_total
;
637 DWORD time
= GetTickCount();
640 /* every 1.5 seconds */
641 if (time
- prev_time
> 1500)
643 TRACE_(fps
)("@ approx %.2ffps, total %.2ffps\n",
644 1000.0*frames
/(time
- prev_time
), 1000.0*frames_total
/(time
- start_time
));
647 if (start_time
== 0) start_time
= time
;
653 /***********************************************************************
654 * wglCreateLayerContext (OPENGL32.@)
656 HGLRC WINAPI
wglCreateLayerContext(HDC hdc
,
658 TRACE("(%p,%d)\n", hdc
, iLayerPlane
);
660 if (iLayerPlane
== 0) {
661 return wglCreateContext(hdc
);
663 FIXME("no handler for layer %d\n", iLayerPlane
);
668 /***********************************************************************
669 * wglDescribeLayerPlane (OPENGL32.@)
671 BOOL WINAPI
wglDescribeLayerPlane(HDC hdc
,
675 LPLAYERPLANEDESCRIPTOR plpd
) {
676 FIXME("(%p,%d,%d,%d,%p)\n", hdc
, iPixelFormat
, iLayerPlane
, nBytes
, plpd
);
681 /***********************************************************************
682 * wglGetLayerPaletteEntries (OPENGL32.@)
684 int WINAPI
wglGetLayerPaletteEntries(HDC hdc
,
688 const COLORREF
*pcr
) {
689 FIXME("(): stub!\n");
694 static BOOL
filter_extensions(const char *extensions
, GLubyte
**exts_list
, GLuint
**disabled_exts
);
696 void WINAPI
glGetIntegerv(GLenum pname
, GLint
*data
)
698 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
700 TRACE("(%d, %p)\n", pname
, data
);
701 if (pname
== GL_NUM_EXTENSIONS
)
703 struct wgl_handle
*ptr
= get_current_context_ptr();
705 if (ptr
->u
.context
->disabled_exts
||
706 filter_extensions(NULL
, NULL
, &ptr
->u
.context
->disabled_exts
))
708 const GLuint
*disabled_exts
= ptr
->u
.context
->disabled_exts
;
709 GLint count
, disabled_count
= 0;
711 funcs
->gl
.p_glGetIntegerv(pname
, &count
);
712 while (*disabled_exts
++ != ~0u)
714 *data
= count
- disabled_count
;
718 funcs
->gl
.p_glGetIntegerv(pname
, data
);
721 const GLubyte
* WINAPI
glGetStringi(GLenum name
, GLuint index
)
723 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
725 TRACE("(%d, %d)\n", name
, index
);
726 if (!funcs
->ext
.p_glGetStringi
)
728 void **func_ptr
= (void **)&funcs
->ext
.p_glGetStringi
;
730 *func_ptr
= funcs
->wgl
.p_wglGetProcAddress("glGetStringi");
733 if (name
== GL_EXTENSIONS
)
735 struct wgl_handle
*ptr
= get_current_context_ptr();
737 if (ptr
->u
.context
->disabled_exts
||
738 filter_extensions(NULL
, NULL
, &ptr
->u
.context
->disabled_exts
))
740 const GLuint
*disabled_exts
= ptr
->u
.context
->disabled_exts
;
741 unsigned int disabled_count
= 0;
743 while (index
+ disabled_count
>= *disabled_exts
++)
745 return funcs
->ext
.p_glGetStringi(name
, index
+ disabled_count
);
748 return funcs
->ext
.p_glGetStringi(name
, index
);
751 /* check if the extension is present in the list */
752 static BOOL
has_extension( const char *list
, const char *ext
, size_t len
)
758 GLint extensions_count
;
760 glGetIntegerv(GL_NUM_EXTENSIONS
, &extensions_count
);
761 for (i
= 0; i
< extensions_count
; ++i
)
763 gl_ext
= (const char *)glGetStringi(GL_EXTENSIONS
, i
);
764 if (!strncmp(gl_ext
, ext
, len
) && !gl_ext
[len
])
772 while (*list
== ' ') list
++;
773 if (!strncmp( list
, ext
, len
) && (!list
[len
] || list
[len
] == ' ')) return TRUE
;
774 list
= strchr( list
, ' ' );
779 static int compar(const void *elt_a
, const void *elt_b
) {
780 return strcmp(((const OpenGL_extension
*) elt_a
)->name
,
781 ((const OpenGL_extension
*) elt_b
)->name
);
784 /* Check if a GL extension is supported */
785 static BOOL
is_extension_supported(const char* extension
)
787 enum wgl_handle_type type
= get_current_context_type();
788 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
789 const char *gl_ext_string
= NULL
;
792 TRACE("Checking for extension '%s'\n", extension
);
794 if (type
== HANDLE_CONTEXT
)
796 gl_ext_string
= (const char*)glGetString(GL_EXTENSIONS
);
799 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
804 /* We use the GetProcAddress function from the display driver to retrieve function pointers
805 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
806 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
807 * require the function to return NULL when an extension isn't found. For this reason we check
808 * if the OpenGL extension required for the function we are looking up is supported. */
810 while ((len
= strcspn(extension
, " ")) != 0)
812 /* Check if the extension is part of the GL extension string to see if it is supported. */
813 if (has_extension(gl_ext_string
, extension
, len
))
816 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
817 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
818 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
819 * Check if we are searching for a core GL function */
820 if(strncmp(extension
, "GL_VERSION_", 11) == 0)
822 const GLubyte
*gl_version
= funcs
->gl
.p_glGetString(GL_VERSION
);
823 const char *version
= extension
+ 11; /* Move past 'GL_VERSION_' */
826 ERR("No OpenGL version found!\n");
830 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
831 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
832 if( (gl_version
[0] > version
[0]) || ((gl_version
[0] == version
[0]) && (gl_version
[2] >= version
[2])) ) {
835 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]);
838 if (extension
[len
] == ' ') len
++;
845 /***********************************************************************
846 * wglGetProcAddress (OPENGL32.@)
848 PROC WINAPI
wglGetProcAddress( LPCSTR name
)
850 struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
852 OpenGL_extension ext
;
853 const OpenGL_extension
*ext_ret
;
855 if (!name
) return NULL
;
857 /* Without an active context opengl32 doesn't know to what
858 * driver it has to dispatch wglGetProcAddress.
860 if (!get_current_context_ptr())
862 WARN("No active WGL context found\n");
867 ext_ret
= bsearch(&ext
, extension_registry
, extension_registry_size
, sizeof(ext
), compar
);
870 WARN("Function %s unknown\n", name
);
874 func_ptr
= (void **)&funcs
->ext
+ (ext_ret
- extension_registry
);
877 void *driver_func
= funcs
->wgl
.p_wglGetProcAddress( name
);
879 if (!is_extension_supported(ext_ret
->extension
))
882 static const struct { const char *name
, *alt
; } alternatives
[] =
884 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
885 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
888 for (i
= 0; i
< sizeof(alternatives
)/sizeof(alternatives
[0]); i
++)
890 if (strcmp( name
, alternatives
[i
].name
)) continue;
891 WARN("Extension %s required for %s not supported, trying %s\n",
892 ext_ret
->extension
, name
, alternatives
[i
].alt
);
893 return wglGetProcAddress( alternatives
[i
].alt
);
895 WARN("Extension %s required for %s not supported\n", ext_ret
->extension
, name
);
899 if (driver_func
== NULL
)
901 WARN("Function %s not supported by driver\n", name
);
904 *func_ptr
= driver_func
;
907 TRACE("returning %s -> %p\n", name
, ext_ret
->func
);
908 return ext_ret
->func
;
911 /***********************************************************************
912 * wglRealizeLayerPalette (OPENGL32.@)
914 BOOL WINAPI
wglRealizeLayerPalette(HDC hdc
,
922 /***********************************************************************
923 * wglSetLayerPaletteEntries (OPENGL32.@)
925 int WINAPI
wglSetLayerPaletteEntries(HDC hdc
,
929 const COLORREF
*pcr
) {
930 FIXME("(): stub!\n");
935 /***********************************************************************
936 * wglSwapLayerBuffers (OPENGL32.@)
938 BOOL WINAPI
wglSwapLayerBuffers(HDC hdc
,
940 TRACE("(%p, %08x)\n", hdc
, fuPlanes
);
942 if (fuPlanes
& WGL_SWAP_MAIN_PLANE
) {
943 if (!wglSwapBuffers( hdc
)) return FALSE
;
944 fuPlanes
&= ~WGL_SWAP_MAIN_PLANE
;
948 WARN("Following layers unhandled: %08x\n", fuPlanes
);
954 /***********************************************************************
955 * wglAllocateMemoryNV
957 * Provided by the WGL_NV_vertex_array_range extension.
959 void * WINAPI
wglAllocateMemoryNV( GLsizei size
, GLfloat readfreq
, GLfloat writefreq
, GLfloat priority
)
961 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
963 if (!funcs
->ext
.p_wglAllocateMemoryNV
) return NULL
;
964 return funcs
->ext
.p_wglAllocateMemoryNV( size
, readfreq
, writefreq
, priority
);
967 /***********************************************************************
970 * Provided by the WGL_NV_vertex_array_range extension.
972 void WINAPI
wglFreeMemoryNV( void *pointer
)
974 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
976 if (funcs
->ext
.p_wglFreeMemoryNV
) funcs
->ext
.p_wglFreeMemoryNV( pointer
);
979 /***********************************************************************
982 * Provided by the WGL_ARB_render_texture extension.
984 BOOL WINAPI
wglBindTexImageARB( 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_wglBindTexImageARB( ptr
->u
.pbuffer
, buffer
);
991 release_handle_ptr( ptr
);
995 /***********************************************************************
996 * wglReleaseTexImageARB
998 * Provided by the WGL_ARB_render_texture extension.
1000 BOOL WINAPI
wglReleaseTexImageARB( HPBUFFERARB handle
, int buffer
)
1002 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1005 if (!ptr
) return FALSE
;
1006 ret
= ptr
->funcs
->ext
.p_wglReleaseTexImageARB( ptr
->u
.pbuffer
, buffer
);
1007 release_handle_ptr( ptr
);
1011 /***********************************************************************
1012 * wglSetPbufferAttribARB
1014 * Provided by the WGL_ARB_render_texture extension.
1016 BOOL WINAPI
wglSetPbufferAttribARB( HPBUFFERARB handle
, const int *attribs
)
1018 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1021 if (!ptr
) return FALSE
;
1022 ret
= ptr
->funcs
->ext
.p_wglSetPbufferAttribARB( ptr
->u
.pbuffer
, attribs
);
1023 release_handle_ptr( ptr
);
1027 /***********************************************************************
1028 * wglChoosePixelFormatARB
1030 * Provided by the WGL_ARB_pixel_format extension.
1032 BOOL WINAPI
wglChoosePixelFormatARB( HDC hdc
, const int *iattribs
, const FLOAT
*fattribs
,
1033 UINT max
, int *formats
, UINT
*count
)
1035 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1037 if (!funcs
|| !funcs
->ext
.p_wglChoosePixelFormatARB
) return FALSE
;
1038 return funcs
->ext
.p_wglChoosePixelFormatARB( hdc
, iattribs
, fattribs
, max
, formats
, count
);
1041 /***********************************************************************
1042 * wglGetPixelFormatAttribivARB
1044 * Provided by the WGL_ARB_pixel_format extension.
1046 BOOL WINAPI
wglGetPixelFormatAttribivARB( HDC hdc
, int format
, int layer
, UINT count
, const int *attribs
,
1049 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1051 if (!funcs
|| !funcs
->ext
.p_wglGetPixelFormatAttribivARB
) return FALSE
;
1052 return funcs
->ext
.p_wglGetPixelFormatAttribivARB( hdc
, format
, layer
, count
, attribs
, values
);
1055 /***********************************************************************
1056 * wglGetPixelFormatAttribfvARB
1058 * Provided by the WGL_ARB_pixel_format extension.
1060 BOOL WINAPI
wglGetPixelFormatAttribfvARB( HDC hdc
, int format
, int layer
, UINT count
, const int *attribs
,
1063 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1065 if (!funcs
|| !funcs
->ext
.p_wglGetPixelFormatAttribfvARB
) return FALSE
;
1066 return funcs
->ext
.p_wglGetPixelFormatAttribfvARB( hdc
, format
, layer
, count
, attribs
, values
);
1069 /***********************************************************************
1070 * wglCreatePbufferARB
1072 * Provided by the WGL_ARB_pbuffer extension.
1074 HPBUFFERARB WINAPI
wglCreatePbufferARB( HDC hdc
, int format
, int width
, int height
, const int *attribs
)
1077 struct wgl_pbuffer
*pbuffer
;
1078 struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1080 if (!funcs
|| !funcs
->ext
.p_wglCreatePbufferARB
) return 0;
1081 if (!(pbuffer
= funcs
->ext
.p_wglCreatePbufferARB( hdc
, format
, width
, height
, attribs
))) return 0;
1082 ret
= alloc_handle( HANDLE_PBUFFER
, funcs
, pbuffer
);
1083 if (!ret
) funcs
->ext
.p_wglDestroyPbufferARB( pbuffer
);
1087 /***********************************************************************
1088 * wglGetPbufferDCARB
1090 * Provided by the WGL_ARB_pbuffer extension.
1092 HDC WINAPI
wglGetPbufferDCARB( HPBUFFERARB handle
)
1094 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1098 ret
= ptr
->funcs
->ext
.p_wglGetPbufferDCARB( ptr
->u
.pbuffer
);
1099 release_handle_ptr( ptr
);
1103 /***********************************************************************
1104 * wglReleasePbufferDCARB
1106 * Provided by the WGL_ARB_pbuffer extension.
1108 int WINAPI
wglReleasePbufferDCARB( HPBUFFERARB handle
, HDC hdc
)
1110 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1113 if (!ptr
) return FALSE
;
1114 ret
= ptr
->funcs
->ext
.p_wglReleasePbufferDCARB( ptr
->u
.pbuffer
, hdc
);
1115 release_handle_ptr( ptr
);
1119 /***********************************************************************
1120 * wglDestroyPbufferARB
1122 * Provided by the WGL_ARB_pbuffer extension.
1124 BOOL WINAPI
wglDestroyPbufferARB( HPBUFFERARB handle
)
1126 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1128 if (!ptr
) return FALSE
;
1129 ptr
->funcs
->ext
.p_wglDestroyPbufferARB( ptr
->u
.pbuffer
);
1130 free_handle_ptr( ptr
);
1134 /***********************************************************************
1135 * wglQueryPbufferARB
1137 * Provided by the WGL_ARB_pbuffer extension.
1139 BOOL WINAPI
wglQueryPbufferARB( HPBUFFERARB handle
, int attrib
, int *value
)
1141 struct wgl_handle
*ptr
= get_handle_ptr( handle
, HANDLE_PBUFFER
);
1144 if (!ptr
) return FALSE
;
1145 ret
= ptr
->funcs
->ext
.p_wglQueryPbufferARB( ptr
->u
.pbuffer
, attrib
, value
);
1146 release_handle_ptr( ptr
);
1150 /***********************************************************************
1151 * wglGetExtensionsStringARB
1153 * Provided by the WGL_ARB_extensions_string extension.
1155 const char * WINAPI
wglGetExtensionsStringARB( HDC hdc
)
1157 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1159 if (!funcs
|| !funcs
->ext
.p_wglGetExtensionsStringARB
) return NULL
;
1160 return (const char *)funcs
->ext
.p_wglGetExtensionsStringARB( hdc
);
1163 /***********************************************************************
1164 * wglGetExtensionsStringEXT
1166 * Provided by the WGL_EXT_extensions_string extension.
1168 const char * WINAPI
wglGetExtensionsStringEXT(void)
1170 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1172 if (!funcs
->ext
.p_wglGetExtensionsStringEXT
) return NULL
;
1173 return (const char *)funcs
->ext
.p_wglGetExtensionsStringEXT();
1176 /***********************************************************************
1177 * wglSwapIntervalEXT
1179 * Provided by the WGL_EXT_swap_control extension.
1181 BOOL WINAPI
wglSwapIntervalEXT( int interval
)
1183 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1185 if (!funcs
->ext
.p_wglSwapIntervalEXT
) return FALSE
;
1186 return funcs
->ext
.p_wglSwapIntervalEXT( interval
);
1189 /***********************************************************************
1190 * wglGetSwapIntervalEXT
1192 * Provided by the WGL_EXT_swap_control extension.
1194 int WINAPI
wglGetSwapIntervalEXT(void)
1196 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1198 if (!funcs
->ext
.p_wglGetSwapIntervalEXT
) return FALSE
;
1199 return funcs
->ext
.p_wglGetSwapIntervalEXT();
1202 /***********************************************************************
1203 * wglSetPixelFormatWINE
1205 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1207 BOOL WINAPI
wglSetPixelFormatWINE( HDC hdc
, int format
)
1209 const struct opengl_funcs
*funcs
= get_dc_funcs( hdc
);
1211 if (!funcs
|| !funcs
->ext
.p_wglSetPixelFormatWINE
) return FALSE
;
1212 return funcs
->ext
.p_wglSetPixelFormatWINE( hdc
, format
);
1215 /***********************************************************************
1216 * wglQueryCurrentRendererIntegerWINE
1218 * Provided by the WGL_WINE_query_renderer extension.
1220 BOOL WINAPI
wglQueryCurrentRendererIntegerWINE( GLenum attribute
, GLuint
*value
)
1222 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1224 if (!funcs
->ext
.p_wglQueryCurrentRendererIntegerWINE
) return FALSE
;
1225 return funcs
->ext
.p_wglQueryCurrentRendererIntegerWINE( attribute
, value
);
1228 /***********************************************************************
1229 * wglQueryCurrentRendererStringWINE
1231 * Provided by the WGL_WINE_query_renderer extension.
1233 const GLchar
* WINAPI
wglQueryCurrentRendererStringWINE( GLenum attribute
)
1235 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1237 if (!funcs
->ext
.p_wglQueryCurrentRendererStringWINE
) return NULL
;
1238 return funcs
->ext
.p_wglQueryCurrentRendererStringWINE( attribute
);
1241 /***********************************************************************
1242 * wglQueryRendererIntegerWINE
1244 * Provided by the WGL_WINE_query_renderer extension.
1246 BOOL WINAPI
wglQueryRendererIntegerWINE( HDC dc
, GLint renderer
, GLenum attribute
, GLuint
*value
)
1248 const struct opengl_funcs
*funcs
= get_dc_funcs( dc
);
1250 if (!funcs
|| !funcs
->ext
.p_wglQueryRendererIntegerWINE
) return FALSE
;
1251 return funcs
->ext
.p_wglQueryRendererIntegerWINE( dc
, renderer
, attribute
, value
);
1254 /***********************************************************************
1255 * wglQueryRendererStringWINE
1257 * Provided by the WGL_WINE_query_renderer extension.
1259 const GLchar
* WINAPI
wglQueryRendererStringWINE( HDC dc
, GLint renderer
, GLenum attribute
)
1261 const struct opengl_funcs
*funcs
= get_dc_funcs( dc
);
1263 if (!funcs
|| !funcs
->ext
.p_wglQueryRendererStringWINE
) return NULL
;
1264 return funcs
->ext
.p_wglQueryRendererStringWINE( dc
, renderer
, attribute
);
1267 /***********************************************************************
1268 * wglUseFontBitmaps_common
1270 static BOOL
wglUseFontBitmaps_common( HDC hdc
, DWORD first
, DWORD count
, DWORD listBase
, BOOL unicode
)
1272 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1274 unsigned int glyph
, size
= 0;
1275 void *bitmap
= NULL
, *gl_bitmap
= NULL
;
1279 funcs
->gl
.p_glGetIntegerv(GL_UNPACK_ALIGNMENT
, &org_alignment
);
1280 funcs
->gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 4);
1282 for (glyph
= first
; glyph
< first
+ count
; glyph
++) {
1283 unsigned int needed_size
, height
, width
, width_int
;
1286 needed_size
= GetGlyphOutlineW(hdc
, glyph
, GGO_BITMAP
, &gm
, 0, NULL
, &identity
);
1288 needed_size
= GetGlyphOutlineA(hdc
, glyph
, GGO_BITMAP
, &gm
, 0, NULL
, &identity
);
1290 TRACE("Glyph: %3d / List: %d size %d\n", glyph
, listBase
, needed_size
);
1291 if (needed_size
== GDI_ERROR
) {
1296 if (needed_size
> size
) {
1298 HeapFree(GetProcessHeap(), 0, bitmap
);
1299 HeapFree(GetProcessHeap(), 0, gl_bitmap
);
1300 bitmap
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
1301 gl_bitmap
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
1303 if (needed_size
!= 0) {
1305 ret
= (GetGlyphOutlineW(hdc
, glyph
, GGO_BITMAP
, &gm
,
1306 size
, bitmap
, &identity
) != GDI_ERROR
);
1308 ret
= (GetGlyphOutlineA(hdc
, glyph
, GGO_BITMAP
, &gm
,
1309 size
, bitmap
, &identity
) != GDI_ERROR
);
1313 if (TRACE_ON(wgl
)) {
1314 unsigned int bitmask
;
1315 unsigned char *bitmap_
= bitmap
;
1317 TRACE(" - bbox: %d x %d\n", gm
.gmBlackBoxX
, gm
.gmBlackBoxY
);
1318 TRACE(" - origin: (%d, %d)\n", gm
.gmptGlyphOrigin
.x
, gm
.gmptGlyphOrigin
.y
);
1319 TRACE(" - increment: %d - %d\n", gm
.gmCellIncX
, gm
.gmCellIncY
);
1320 if (needed_size
!= 0) {
1321 TRACE(" - bitmap:\n");
1322 for (height
= 0; height
< gm
.gmBlackBoxY
; height
++) {
1324 for (width
= 0, bitmask
= 0x80; width
< gm
.gmBlackBoxX
; width
++, bitmask
>>= 1) {
1329 if (*bitmap_
& bitmask
)
1334 bitmap_
+= (4 - ((UINT_PTR
)bitmap_
& 0x03));
1340 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1341 * glyph for it to be drawn properly.
1343 if (needed_size
!= 0) {
1344 width_int
= (gm
.gmBlackBoxX
+ 31) / 32;
1345 for (height
= 0; height
< gm
.gmBlackBoxY
; height
++) {
1346 for (width
= 0; width
< width_int
; width
++) {
1347 ((int *) gl_bitmap
)[(gm
.gmBlackBoxY
- height
- 1) * width_int
+ width
] =
1348 ((int *) bitmap
)[height
* width_int
+ width
];
1353 funcs
->gl
.p_glNewList(listBase
++, GL_COMPILE
);
1354 if (needed_size
!= 0) {
1355 funcs
->gl
.p_glBitmap(gm
.gmBlackBoxX
, gm
.gmBlackBoxY
,
1356 0 - gm
.gmptGlyphOrigin
.x
, (int) gm
.gmBlackBoxY
- gm
.gmptGlyphOrigin
.y
,
1357 gm
.gmCellIncX
, gm
.gmCellIncY
,
1360 /* This is the case of 'empty' glyphs like the space character */
1361 funcs
->gl
.p_glBitmap(0, 0, 0, 0, gm
.gmCellIncX
, gm
.gmCellIncY
, NULL
);
1363 funcs
->gl
.p_glEndList();
1366 funcs
->gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, org_alignment
);
1367 HeapFree(GetProcessHeap(), 0, bitmap
);
1368 HeapFree(GetProcessHeap(), 0, gl_bitmap
);
1372 /***********************************************************************
1373 * wglUseFontBitmapsA (OPENGL32.@)
1375 BOOL WINAPI
wglUseFontBitmapsA(HDC hdc
, DWORD first
, DWORD count
, DWORD listBase
)
1377 return wglUseFontBitmaps_common( hdc
, first
, count
, listBase
, FALSE
);
1380 /***********************************************************************
1381 * wglUseFontBitmapsW (OPENGL32.@)
1383 BOOL WINAPI
wglUseFontBitmapsW(HDC hdc
, DWORD first
, DWORD count
, DWORD listBase
)
1385 return wglUseFontBitmaps_common( hdc
, first
, count
, listBase
, TRUE
);
1388 /* FIXME: should probably have a glu.h header */
1390 typedef struct GLUtesselator GLUtesselator
;
1391 typedef void (WINAPI
*_GLUfuncptr
)(void);
1393 #define GLU_TESS_BEGIN 100100
1394 #define GLU_TESS_VERTEX 100101
1395 #define GLU_TESS_END 100102
1397 static GLUtesselator
* (WINAPI
*pgluNewTess
)(void);
1398 static void (WINAPI
*pgluDeleteTess
)(GLUtesselator
*tess
);
1399 static void (WINAPI
*pgluTessNormal
)(GLUtesselator
*tess
, GLdouble x
, GLdouble y
, GLdouble z
);
1400 static void (WINAPI
*pgluTessBeginPolygon
)(GLUtesselator
*tess
, void *polygon_data
);
1401 static void (WINAPI
*pgluTessEndPolygon
)(GLUtesselator
*tess
);
1402 static void (WINAPI
*pgluTessCallback
)(GLUtesselator
*tess
, GLenum which
, _GLUfuncptr fn
);
1403 static void (WINAPI
*pgluTessBeginContour
)(GLUtesselator
*tess
);
1404 static void (WINAPI
*pgluTessEndContour
)(GLUtesselator
*tess
);
1405 static void (WINAPI
*pgluTessVertex
)(GLUtesselator
*tess
, GLdouble
*location
, GLvoid
* data
);
1407 static HMODULE
load_libglu(void)
1409 static const WCHAR glu32W
[] = {'g','l','u','3','2','.','d','l','l',0};
1410 static BOOL already_loaded
;
1411 static HMODULE module
;
1413 if (already_loaded
) return module
;
1414 already_loaded
= TRUE
;
1416 TRACE("Trying to load GLU library\n");
1417 module
= LoadLibraryW( glu32W
);
1420 WARN("Failed to load glu32\n");
1423 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1424 LOAD_FUNCPTR(gluNewTess
);
1425 LOAD_FUNCPTR(gluDeleteTess
);
1426 LOAD_FUNCPTR(gluTessBeginContour
);
1427 LOAD_FUNCPTR(gluTessNormal
);
1428 LOAD_FUNCPTR(gluTessBeginPolygon
);
1429 LOAD_FUNCPTR(gluTessCallback
);
1430 LOAD_FUNCPTR(gluTessEndContour
);
1431 LOAD_FUNCPTR(gluTessEndPolygon
);
1432 LOAD_FUNCPTR(gluTessVertex
);
1437 static void fixed_to_double(POINTFX fixed
, UINT em_size
, GLdouble vertex
[3])
1439 vertex
[0] = (fixed
.x
.value
+ (GLdouble
)fixed
.x
.fract
/ (1 << 16)) / em_size
;
1440 vertex
[1] = (fixed
.y
.value
+ (GLdouble
)fixed
.y
.fract
/ (1 << 16)) / em_size
;
1444 static void WINAPI
tess_callback_vertex(GLvoid
*vertex
)
1446 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1447 GLdouble
*dbl
= vertex
;
1448 TRACE("%f, %f, %f\n", dbl
[0], dbl
[1], dbl
[2]);
1449 funcs
->gl
.p_glVertex3dv(vertex
);
1452 static void WINAPI
tess_callback_begin(GLenum which
)
1454 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1455 TRACE("%d\n", which
);
1456 funcs
->gl
.p_glBegin(which
);
1459 static void WINAPI
tess_callback_end(void)
1461 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1463 funcs
->gl
.p_glEnd();
1466 typedef struct _bezier_vector
{
1471 static double bezier_deviation_squared(const bezier_vector
*p
)
1473 bezier_vector deviation
;
1474 bezier_vector vertex
;
1479 vertex
.x
= (p
[0].x
+ p
[1].x
*2 + p
[2].x
)/4 - p
[0].x
;
1480 vertex
.y
= (p
[0].y
+ p
[1].y
*2 + p
[2].y
)/4 - p
[0].y
;
1482 base
.x
= p
[2].x
- p
[0].x
;
1483 base
.y
= p
[2].y
- p
[0].y
;
1485 base_length
= sqrt(base
.x
*base
.x
+ base
.y
*base
.y
);
1486 base
.x
/= base_length
;
1487 base
.y
/= base_length
;
1489 dot
= base
.x
*vertex
.x
+ base
.y
*vertex
.y
;
1490 dot
= min(max(dot
, 0.0), base_length
);
1494 deviation
.x
= vertex
.x
-base
.x
;
1495 deviation
.y
= vertex
.y
-base
.y
;
1497 return deviation
.x
*deviation
.x
+ deviation
.y
*deviation
.y
;
1500 static int bezier_approximate(const bezier_vector
*p
, bezier_vector
*points
, FLOAT deviation
)
1502 bezier_vector first_curve
[3];
1503 bezier_vector second_curve
[3];
1504 bezier_vector vertex
;
1507 if(bezier_deviation_squared(p
) <= deviation
*deviation
)
1514 vertex
.x
= (p
[0].x
+ p
[1].x
*2 + p
[2].x
)/4;
1515 vertex
.y
= (p
[0].y
+ p
[1].y
*2 + p
[2].y
)/4;
1517 first_curve
[0] = p
[0];
1518 first_curve
[1].x
= (p
[0].x
+ p
[1].x
)/2;
1519 first_curve
[1].y
= (p
[0].y
+ p
[1].y
)/2;
1520 first_curve
[2] = vertex
;
1522 second_curve
[0] = vertex
;
1523 second_curve
[1].x
= (p
[2].x
+ p
[1].x
)/2;
1524 second_curve
[1].y
= (p
[2].y
+ p
[1].y
)/2;
1525 second_curve
[2] = p
[2];
1527 total_vertices
= bezier_approximate(first_curve
, points
, deviation
);
1529 points
+= total_vertices
;
1530 total_vertices
+= bezier_approximate(second_curve
, points
, deviation
);
1531 return total_vertices
;
1534 /***********************************************************************
1535 * wglUseFontOutlines_common
1537 static BOOL
wglUseFontOutlines_common(HDC hdc
,
1544 LPGLYPHMETRICSFLOAT lpgmf
,
1547 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1549 GLUtesselator
*tess
= NULL
;
1551 HFONT old_font
, unscaled_font
;
1552 UINT em_size
= 1024;
1555 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc
, first
, count
,
1556 listBase
, deviation
, extrusion
, format
, lpgmf
, unicode
? "W" : "A");
1558 if(deviation
<= 0.0)
1559 deviation
= 1.0/em_size
;
1561 if(format
== WGL_FONT_POLYGONS
)
1565 ERR("glu32 is required for this function but isn't available\n");
1569 tess
= pgluNewTess();
1570 if(!tess
) return FALSE
;
1571 pgluTessCallback(tess
, GLU_TESS_VERTEX
, (_GLUfuncptr
)tess_callback_vertex
);
1572 pgluTessCallback(tess
, GLU_TESS_BEGIN
, (_GLUfuncptr
)tess_callback_begin
);
1573 pgluTessCallback(tess
, GLU_TESS_END
, tess_callback_end
);
1576 GetObjectW(GetCurrentObject(hdc
, OBJ_FONT
), sizeof(lf
), &lf
);
1577 rc
.left
= rc
.right
= rc
.bottom
= 0;
1579 DPtoLP(hdc
, (POINT
*)&rc
, 2);
1580 lf
.lfHeight
= -abs(rc
.top
- rc
.bottom
);
1581 lf
.lfOrientation
= lf
.lfEscapement
= 0;
1582 unscaled_font
= CreateFontIndirectW(&lf
);
1583 old_font
= SelectObject(hdc
, unscaled_font
);
1585 for (glyph
= first
; glyph
< first
+ count
; glyph
++)
1590 TTPOLYGONHEADER
*pph
;
1592 GLdouble
*vertices
= NULL
;
1593 int vertex_total
= -1;
1596 needed
= GetGlyphOutlineW(hdc
, glyph
, GGO_NATIVE
, &gm
, 0, NULL
, &identity
);
1598 needed
= GetGlyphOutlineA(hdc
, glyph
, GGO_NATIVE
, &gm
, 0, NULL
, &identity
);
1600 if(needed
== GDI_ERROR
)
1603 buf
= HeapAlloc(GetProcessHeap(), 0, needed
);
1606 GetGlyphOutlineW(hdc
, glyph
, GGO_NATIVE
, &gm
, needed
, buf
, &identity
);
1608 GetGlyphOutlineA(hdc
, glyph
, GGO_NATIVE
, &gm
, needed
, buf
, &identity
);
1610 TRACE("glyph %d\n", glyph
);
1614 lpgmf
->gmfBlackBoxX
= (float)gm
.gmBlackBoxX
/ em_size
;
1615 lpgmf
->gmfBlackBoxY
= (float)gm
.gmBlackBoxY
/ em_size
;
1616 lpgmf
->gmfptGlyphOrigin
.x
= (float)gm
.gmptGlyphOrigin
.x
/ em_size
;
1617 lpgmf
->gmfptGlyphOrigin
.y
= (float)gm
.gmptGlyphOrigin
.y
/ em_size
;
1618 lpgmf
->gmfCellIncX
= (float)gm
.gmCellIncX
/ em_size
;
1619 lpgmf
->gmfCellIncY
= (float)gm
.gmCellIncY
/ em_size
;
1621 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf
->gmfBlackBoxX
, lpgmf
->gmfBlackBoxY
,
1622 lpgmf
->gmfptGlyphOrigin
.x
, lpgmf
->gmfptGlyphOrigin
.y
, lpgmf
->gmfCellIncX
, lpgmf
->gmfCellIncY
);
1626 funcs
->gl
.p_glNewList(listBase
++, GL_COMPILE
);
1627 funcs
->gl
.p_glFrontFace(GL_CCW
);
1628 if(format
== WGL_FONT_POLYGONS
)
1630 funcs
->gl
.p_glNormal3d(0.0, 0.0, 1.0);
1631 pgluTessNormal(tess
, 0, 0, 1);
1632 pgluTessBeginPolygon(tess
, NULL
);
1637 if(vertex_total
!= -1)
1638 vertices
= HeapAlloc(GetProcessHeap(), 0, vertex_total
* 3 * sizeof(GLdouble
));
1641 pph
= (TTPOLYGONHEADER
*)buf
;
1642 while((BYTE
*)pph
< buf
+ needed
)
1644 GLdouble previous
[3];
1645 fixed_to_double(pph
->pfxStart
, em_size
, previous
);
1648 TRACE("\tstart %d, %d\n", pph
->pfxStart
.x
.value
, pph
->pfxStart
.y
.value
);
1650 if(format
== WGL_FONT_POLYGONS
)
1651 pgluTessBeginContour(tess
);
1653 funcs
->gl
.p_glBegin(GL_LINE_LOOP
);
1657 fixed_to_double(pph
->pfxStart
, em_size
, vertices
);
1658 if(format
== WGL_FONT_POLYGONS
)
1659 pgluTessVertex(tess
, vertices
, vertices
);
1661 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1666 ppc
= (TTPOLYCURVE
*)((char*)pph
+ sizeof(*pph
));
1667 while((char*)ppc
< (char*)pph
+ pph
->cb
)
1672 switch(ppc
->wType
) {
1674 for(i
= 0; i
< ppc
->cpfx
; i
++)
1678 TRACE("\t\tline to %d, %d\n",
1679 ppc
->apfx
[i
].x
.value
, ppc
->apfx
[i
].y
.value
);
1680 fixed_to_double(ppc
->apfx
[i
], em_size
, vertices
);
1681 if(format
== WGL_FONT_POLYGONS
)
1682 pgluTessVertex(tess
, vertices
, vertices
);
1684 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1687 fixed_to_double(ppc
->apfx
[i
], em_size
, previous
);
1692 case TT_PRIM_QSPLINE
:
1693 for(i
= 0; i
< ppc
->cpfx
-1; i
++)
1695 bezier_vector curve
[3];
1696 bezier_vector
*points
;
1697 GLdouble curve_vertex
[3];
1700 TRACE("\t\tcurve %d,%d %d,%d\n",
1701 ppc
->apfx
[i
].x
.value
, ppc
->apfx
[i
].y
.value
,
1702 ppc
->apfx
[i
+ 1].x
.value
, ppc
->apfx
[i
+ 1].y
.value
);
1704 curve
[0].x
= previous
[0];
1705 curve
[0].y
= previous
[1];
1706 fixed_to_double(ppc
->apfx
[i
], em_size
, curve_vertex
);
1707 curve
[1].x
= curve_vertex
[0];
1708 curve
[1].y
= curve_vertex
[1];
1709 fixed_to_double(ppc
->apfx
[i
+ 1], em_size
, curve_vertex
);
1710 curve
[2].x
= curve_vertex
[0];
1711 curve
[2].y
= curve_vertex
[1];
1714 curve
[2].x
= (curve
[1].x
+ curve
[2].x
)/2;
1715 curve
[2].y
= (curve
[1].y
+ curve
[2].y
)/2;
1717 num
= bezier_approximate(curve
, NULL
, deviation
);
1718 points
= HeapAlloc(GetProcessHeap(), 0, num
*sizeof(bezier_vector
));
1719 num
= bezier_approximate(curve
, points
, deviation
);
1720 vertex_total
+= num
;
1723 for(j
=0; j
<num
; j
++)
1725 TRACE("\t\t\tvertex at %f,%f\n", points
[j
].x
, points
[j
].y
);
1726 vertices
[0] = points
[j
].x
;
1727 vertices
[1] = points
[j
].y
;
1729 if(format
== WGL_FONT_POLYGONS
)
1730 pgluTessVertex(tess
, vertices
, vertices
);
1732 funcs
->gl
.p_glVertex3d(vertices
[0], vertices
[1], vertices
[2]);
1736 HeapFree(GetProcessHeap(), 0, points
);
1737 previous
[0] = curve
[2].x
;
1738 previous
[1] = curve
[2].y
;
1742 ERR("\t\tcurve type = %d\n", ppc
->wType
);
1743 if(format
== WGL_FONT_POLYGONS
)
1744 pgluTessEndContour(tess
);
1746 funcs
->gl
.p_glEnd();
1750 ppc
= (TTPOLYCURVE
*)((char*)ppc
+ sizeof(*ppc
) +
1751 (ppc
->cpfx
- 1) * sizeof(POINTFX
));
1753 if(format
== WGL_FONT_POLYGONS
)
1754 pgluTessEndContour(tess
);
1756 funcs
->gl
.p_glEnd();
1757 pph
= (TTPOLYGONHEADER
*)((char*)pph
+ pph
->cb
);
1762 if(format
== WGL_FONT_POLYGONS
)
1763 pgluTessEndPolygon(tess
);
1764 funcs
->gl
.p_glTranslated((GLdouble
)gm
.gmCellIncX
/ em_size
, (GLdouble
)gm
.gmCellIncY
/ em_size
, 0.0);
1765 funcs
->gl
.p_glEndList();
1766 HeapFree(GetProcessHeap(), 0, buf
);
1767 HeapFree(GetProcessHeap(), 0, vertices
);
1771 DeleteObject(SelectObject(hdc
, old_font
));
1772 if(format
== WGL_FONT_POLYGONS
)
1773 pgluDeleteTess(tess
);
1778 /***********************************************************************
1779 * wglUseFontOutlinesA (OPENGL32.@)
1781 BOOL WINAPI
wglUseFontOutlinesA(HDC hdc
,
1788 LPGLYPHMETRICSFLOAT lpgmf
)
1790 return wglUseFontOutlines_common(hdc
, first
, count
, listBase
, deviation
, extrusion
, format
, lpgmf
, FALSE
);
1793 /***********************************************************************
1794 * wglUseFontOutlinesW (OPENGL32.@)
1796 BOOL WINAPI
wglUseFontOutlinesW(HDC hdc
,
1803 LPGLYPHMETRICSFLOAT lpgmf
)
1805 return wglUseFontOutlines_common(hdc
, first
, count
, listBase
, deviation
, extrusion
, format
, lpgmf
, TRUE
);
1808 /***********************************************************************
1809 * glDebugEntry (OPENGL32.@)
1811 GLint WINAPI
glDebugEntry( GLint unknown1
, GLint unknown2
)
1816 static GLubyte
*filter_extensions_list(const char *extensions
, const char *disabled
)
1821 p
= str
= HeapAlloc(GetProcessHeap(), 0, strlen(extensions
) + 2);
1825 TRACE( "GL_EXTENSIONS:\n" );
1829 while (*extensions
== ' ')
1833 if (!(end
= strchr(extensions
, ' ')))
1834 end
= extensions
+ strlen(extensions
);
1835 memcpy(p
, extensions
, end
- extensions
);
1836 p
[end
- extensions
] = 0;
1837 if (!has_extension(disabled
, p
, strlen(p
)))
1839 TRACE("++ %s\n", p
);
1840 p
+= end
- extensions
;
1845 TRACE("-- %s (disabled by config)\n", p
);
1850 return (GLubyte
*)str
;
1853 static GLuint
*filter_extensions_index(const char *disabled
)
1855 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1856 const char *ext
, *end
, *gl_ext
;
1857 GLuint
*disabled_exts
, *new_disabled_exts
;
1858 unsigned int i
= 0, j
, disabled_size
;
1859 GLint extensions_count
;
1861 if (!funcs
->ext
.p_glGetStringi
)
1863 void **func_ptr
= (void **)&funcs
->ext
.p_glGetStringi
;
1865 *func_ptr
= funcs
->wgl
.p_wglGetProcAddress("glGetStringi");
1866 if (!funcs
->ext
.p_glGetStringi
)
1870 funcs
->gl
.p_glGetIntegerv(GL_NUM_EXTENSIONS
, &extensions_count
);
1872 disabled_exts
= HeapAlloc(GetProcessHeap(), 0, disabled_size
* sizeof(*disabled_exts
));
1876 TRACE( "GL_EXTENSIONS:\n" );
1878 for (j
= 0; j
< extensions_count
; ++j
)
1880 gl_ext
= (const char *)funcs
->ext
.p_glGetStringi(GL_EXTENSIONS
, j
);
1888 TRACE("++ %s\n", gl_ext
);
1891 if (!(end
= strchr(ext
, ' ')))
1892 end
= ext
+ strlen(ext
);
1894 if (!strncmp(gl_ext
, ext
, end
- ext
) && !gl_ext
[end
- ext
])
1896 if (i
+ 1 == disabled_size
)
1899 new_disabled_exts
= HeapReAlloc(GetProcessHeap(), 0, disabled_exts
,
1900 disabled_size
* sizeof(*disabled_exts
));
1901 if (!new_disabled_exts
)
1903 disabled_exts
[i
] = ~0u;
1904 return disabled_exts
;
1906 disabled_exts
= new_disabled_exts
;
1908 TRACE("-- %s (disabled by config)\n", gl_ext
);
1909 disabled_exts
[i
++] = j
;
1915 disabled_exts
[i
] = ~0u;
1916 return disabled_exts
;
1919 /* build the extension string by filtering out the disabled extensions */
1920 static BOOL
filter_extensions(const char *extensions
, GLubyte
**exts_list
, GLuint
**disabled_exts
)
1922 static const char *disabled
;
1930 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1931 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\OpenGL", &hkey
))
1933 if (!RegQueryValueExA( hkey
, "DisabledExtensions", 0, NULL
, NULL
, &size
))
1935 str
= HeapAlloc( GetProcessHeap(), 0, size
);
1936 if (RegQueryValueExA( hkey
, "DisabledExtensions", 0, NULL
, (BYTE
*)str
, &size
)) *str
= 0;
1938 RegCloseKey( hkey
);
1942 if (InterlockedCompareExchangePointer( (void **)&disabled
, str
, NULL
))
1943 HeapFree( GetProcessHeap(), 0, str
);
1951 if (extensions
&& !*exts_list
)
1952 *exts_list
= filter_extensions_list(extensions
, disabled
);
1954 if (!*disabled_exts
)
1955 *disabled_exts
= filter_extensions_index(disabled
);
1957 return (exts_list
&& *exts_list
) || *disabled_exts
;
1960 /***********************************************************************
1961 * glGetString (OPENGL32.@)
1963 const GLubyte
* WINAPI
glGetString( GLenum name
)
1965 const struct opengl_funcs
*funcs
= NtCurrentTeb()->glTable
;
1966 const GLubyte
*ret
= funcs
->gl
.p_glGetString( name
);
1968 if (name
== GL_EXTENSIONS
&& ret
)
1970 struct wgl_handle
*ptr
= get_current_context_ptr();
1971 if (ptr
->u
.context
->extensions
||
1972 filter_extensions((const char *)ret
, &ptr
->u
.context
->extensions
, &ptr
->u
.context
->disabled_exts
))
1973 ret
= ptr
->u
.context
->extensions
;
1978 /***********************************************************************
1979 * OpenGL initialisation routine
1981 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
1985 case DLL_PROCESS_ATTACH
:
1986 NtCurrentTeb()->glTable
= &null_opengl_funcs
;
1988 case DLL_THREAD_ATTACH
:
1989 NtCurrentTeb()->glTable
= &null_opengl_funcs
;