-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathwgl.c
1764 lines (1541 loc) · 58 KB
/
wgl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Window-specific OpenGL functions implementation.
*
* Copyright (c) 1999 Lionel Ulmer
* Copyright (c) 2005 Raphael Junqueira
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winreg.h"
#include "wingdi.h"
#include "winternl.h"
#include "winnt.h"
#include "opengl_ext.h"
#include "wine/gdi_driver.h"
#include "wine/glu.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(wgl);
WINE_DECLARE_DEBUG_CHANNEL(fps);
/* handle management */
#define MAX_WGL_HANDLES 1024
enum wgl_handle_type
{
HANDLE_PBUFFER = 0 << 12,
HANDLE_CONTEXT = 1 << 12,
HANDLE_CONTEXT_V3 = 3 << 12,
HANDLE_TYPE_MASK = 15 << 12
};
struct opengl_context
{
DWORD tid; /* thread that the context is current in */
HDC draw_dc; /* current drawing DC */
HDC read_dc; /* current reading DC */
GLubyte *extensions; /* extension string */
GLuint *disabled_exts; /* indices of disabled extensions */
struct wgl_context *drv_ctx; /* driver context */
};
struct wgl_handle
{
UINT handle;
struct opengl_funcs *funcs;
union
{
struct opengl_context *context; /* for HANDLE_CONTEXT */
struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
struct wgl_handle *next; /* for free handles */
} u;
};
static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
static struct wgl_handle *next_free;
static unsigned int handle_count;
static CRITICAL_SECTION wgl_section;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &wgl_section,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
};
static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
{
WORD generation = HIWORD( ptr->handle ) + 1;
if (!generation) generation++;
ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
return ULongToHandle( ptr->handle );
}
/* the current context is assumed valid and doesn't need locking */
static inline struct wgl_handle *get_current_context_ptr(void)
{
if (!NtCurrentTeb()->glCurrentRC) return NULL;
return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
}
static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
{
unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
EnterCriticalSection( &wgl_section );
if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
return &wgl_handles[index];
LeaveCriticalSection( &wgl_section );
SetLastError( ERROR_INVALID_HANDLE );
return NULL;
}
static void release_handle_ptr( struct wgl_handle *ptr )
{
if (ptr) LeaveCriticalSection( &wgl_section );
}
static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
{
HANDLE handle = 0;
struct wgl_handle *ptr = NULL;
EnterCriticalSection( &wgl_section );
if ((ptr = next_free))
next_free = next_free->u.next;
else if (handle_count < MAX_WGL_HANDLES)
ptr = &wgl_handles[handle_count++];
if (ptr)
{
ptr->funcs = funcs;
ptr->u.context = user_ptr;
handle = next_handle( ptr, type );
}
else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
LeaveCriticalSection( &wgl_section );
return handle;
}
static void free_handle_ptr( struct wgl_handle *ptr )
{
ptr->handle |= 0xffff;
ptr->u.next = next_free;
ptr->funcs = NULL;
next_free = ptr;
LeaveCriticalSection( &wgl_section );
}
static inline enum wgl_handle_type get_current_context_type(void)
{
if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
}
/***********************************************************************
* wglCopyContext (OPENGL32.@)
*/
BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
{
struct wgl_handle *src, *dst;
BOOL ret = FALSE;
if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
{
if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
dst->u.context->drv_ctx, mask );
}
release_handle_ptr( dst );
release_handle_ptr( src );
return ret;
}
/***********************************************************************
* wglDeleteContext (OPENGL32.@)
*/
BOOL WINAPI wglDeleteContext(HGLRC hglrc)
{
struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
if (!ptr) return FALSE;
if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
{
SetLastError( ERROR_BUSY );
release_handle_ptr( ptr );
return FALSE;
}
if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
HeapFree( GetProcessHeap(), 0, ptr->u.context->disabled_exts );
HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
HeapFree( GetProcessHeap(), 0, ptr->u.context );
free_handle_ptr( ptr );
return TRUE;
}
/***********************************************************************
* wglMakeCurrent (OPENGL32.@)
*/
BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
{
BOOL ret = TRUE;
struct wgl_handle *ptr, *prev = get_current_context_ptr();
if (hglrc)
{
if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
{
ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
if (ret)
{
if (prev) prev->u.context->tid = 0;
ptr->u.context->tid = GetCurrentThreadId();
ptr->u.context->draw_dc = hdc;
ptr->u.context->read_dc = hdc;
NtCurrentTeb()->glCurrentRC = hglrc;
NtCurrentTeb()->glTable = ptr->funcs;
}
}
else
{
SetLastError( ERROR_BUSY );
ret = FALSE;
}
release_handle_ptr( ptr );
}
else if (prev)
{
if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
prev->u.context->tid = 0;
NtCurrentTeb()->glCurrentRC = 0;
NtCurrentTeb()->glTable = &null_opengl_funcs;
}
else if (!hdc)
{
SetLastError( ERROR_INVALID_HANDLE );
ret = FALSE;
}
return ret;
}
/***********************************************************************
* wglCreateContextAttribsARB
*
* Provided by the WGL_ARB_create_context extension.
*/
HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
{
HGLRC ret = 0;
struct wgl_context *drv_ctx;
struct wgl_handle *share_ptr = NULL;
struct opengl_context *context;
struct opengl_funcs *funcs = get_dc_funcs( hdc );
if (!funcs)
{
SetLastError( ERROR_DC_NOT_FOUND );
return 0;
}
if (!funcs->ext.p_wglCreateContextAttribsARB) return 0;
if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT )))
{
SetLastError( ERROR_INVALID_OPERATION );
return 0;
}
if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
{
if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
{
enum wgl_handle_type type = HANDLE_CONTEXT;
if (attribs)
{
while (*attribs)
{
if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
{
if (attribs[1] >= 3)
type = HANDLE_CONTEXT_V3;
break;
}
attribs += 2;
}
}
context->drv_ctx = drv_ctx;
if (!(ret = alloc_handle( type, funcs, context )))
HeapFree( GetProcessHeap(), 0, context );
}
if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
}
release_handle_ptr( share_ptr );
return ret;
}
/***********************************************************************
* wglMakeContextCurrentARB
*
* Provided by the WGL_ARB_make_current_read extension.
*/
BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
{
BOOL ret = TRUE;
struct wgl_handle *ptr, *prev = get_current_context_ptr();
if (hglrc)
{
if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
{
ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
ptr->u.context->drv_ctx ));
if (ret)
{
if (prev) prev->u.context->tid = 0;
ptr->u.context->tid = GetCurrentThreadId();
ptr->u.context->draw_dc = draw_hdc;
ptr->u.context->read_dc = read_hdc;
NtCurrentTeb()->glCurrentRC = hglrc;
NtCurrentTeb()->glTable = ptr->funcs;
}
}
else
{
SetLastError( ERROR_BUSY );
ret = FALSE;
}
release_handle_ptr( ptr );
}
else if (prev)
{
if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
prev->u.context->tid = 0;
NtCurrentTeb()->glCurrentRC = 0;
NtCurrentTeb()->glTable = &null_opengl_funcs;
}
return ret;
}
/***********************************************************************
* wglGetCurrentReadDCARB
*
* Provided by the WGL_ARB_make_current_read extension.
*/
HDC WINAPI wglGetCurrentReadDCARB(void)
{
struct wgl_handle *ptr = get_current_context_ptr();
if (!ptr) return 0;
return ptr->u.context->read_dc;
}
/***********************************************************************
* wglShareLists (OPENGL32.@)
*/
BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
{
BOOL ret = FALSE;
struct wgl_handle *src, *dst;
if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
{
if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
}
release_handle_ptr( dst );
release_handle_ptr( src );
return ret;
}
/***********************************************************************
* wglGetCurrentDC (OPENGL32.@)
*/
HDC WINAPI wglGetCurrentDC(void)
{
struct wgl_handle *ptr = get_current_context_ptr();
if (!ptr) return 0;
return ptr->u.context->draw_dc;
}
/***********************************************************************
* wglCreateContext (OPENGL32.@)
*/
HGLRC WINAPI wglCreateContext(HDC hdc)
{
HGLRC ret = 0;
struct wgl_context *drv_ctx;
struct opengl_context *context;
struct opengl_funcs *funcs = get_dc_funcs( hdc );
if (!funcs) return 0;
if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
{
context->drv_ctx = drv_ctx;
if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
HeapFree( GetProcessHeap(), 0, context );
}
if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
return ret;
}
/***********************************************************************
* wglGetCurrentContext (OPENGL32.@)
*/
HGLRC WINAPI wglGetCurrentContext(void)
{
return NtCurrentTeb()->glCurrentRC;
}
/***********************************************************************
* wglDescribePixelFormat (OPENGL32.@)
*/
INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
{
struct opengl_funcs *funcs = get_dc_funcs( hdc );
if (!funcs) return 0;
return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
}
/***********************************************************************
* wglChoosePixelFormat (OPENGL32.@)
*/
INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
{
PIXELFORMATDESCRIPTOR format, best;
int i, count, best_format;
int bestDBuffer = -1, bestStereo = -1;
TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
"accum %u depth %u stencil %u aux %u\n",
hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
count = wglDescribePixelFormat( hdc, 0, 0, NULL );
if (!count) return 0;
best_format = 0;
best.dwFlags = 0;
best.cAlphaBits = -1;
best.cColorBits = -1;
best.cDepthBits = -1;
best.cStencilBits = -1;
best.cAuxBuffers = -1;
for (i = 1; i <= count; i++)
{
if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
if (ppfd->iPixelType != format.iPixelType)
{
TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
continue;
}
/* only use bitmap capable for formats for bitmap rendering */
if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
{
TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
continue;
}
/* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
* is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
* with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
* formats without the given flag set.
* A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
* has indicated that a format without stereo is returned when stereo is unavailable.
* So in case PFD_STEREO is set, formats that support it should have priority above formats
* without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
*
* To summarize the following is most likely the correct behavior:
* stereo not set -> prefer non-stereo formats, but also accept stereo formats
* stereo set -> prefer stereo formats, but also accept non-stereo formats
* stereo don't care -> it doesn't matter whether we get stereo or not
*
* In Wine we will treat non-stereo the same way as don't care because it makes
* format selection even more complicated and second drivers with Stereo advertise
* each format twice anyway.
*/
/* Doublebuffer, see the comments above */
if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
{
if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
goto found;
if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
}
else if (!best_format)
goto found;
/* Stereo, see the comments above. */
if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
{
if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
goto found;
if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
}
else if (!best_format)
goto found;
/* Below we will do a number of checks to select the 'best' pixelformat.
* We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
* The code works by trying to match the most important options as close as possible.
* When a reasonable format is found, we will try to match more options.
* It appears (see the opengl32 test) that Windows opengl drivers ignore options
* like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
* as DONTCARE. At least Serious Sam TSE relies on this behavior. */
if (ppfd->cColorBits)
{
if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
goto found;
if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
{
TRACE( "color mismatch for iPixelFormat=%d\n", i );
continue;
}
}
if (ppfd->cAlphaBits)
{
if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
goto found;
if (best.cAlphaBits != format.cAlphaBits)
{
TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
continue;
}
}
if (ppfd->cDepthBits)
{
if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
goto found;
if (best.cDepthBits != format.cDepthBits)
{
TRACE( "depth mismatch for iPixelFormat=%d\n", i );
continue;
}
}
if (ppfd->cStencilBits)
{
if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
goto found;
if (best.cStencilBits != format.cStencilBits)
{
TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
continue;
}
}
if (ppfd->cAuxBuffers)
{
if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
goto found;
if (best.cAuxBuffers != format.cAuxBuffers)
{
TRACE( "aux mismatch for iPixelFormat=%d\n", i );
continue;
}
}
continue;
found:
best_format = i;
best = format;
bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
bestStereo = format.dwFlags & PFD_STEREO;
}
TRACE( "returning %u\n", best_format );
return best_format;
}
/***********************************************************************
* wglGetPixelFormat (OPENGL32.@)
*/
INT WINAPI wglGetPixelFormat(HDC hdc)
{
struct opengl_funcs *funcs = get_dc_funcs( hdc );
if (!funcs)
{
SetLastError( ERROR_INVALID_PIXEL_FORMAT );
return 0;
}
return funcs->wgl.p_wglGetPixelFormat( hdc );
}
/***********************************************************************
* wglSetPixelFormat(OPENGL32.@)
*/
BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
{
struct opengl_funcs *funcs = get_dc_funcs( hdc );
if (!funcs) return FALSE;
return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
}
/***********************************************************************
* wglSwapBuffers (OPENGL32.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
{
const struct opengl_funcs *funcs = get_dc_funcs( hdc );
if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
if (TRACE_ON(fps))
{
static long prev_time, start_time;
static unsigned long frames, frames_total;
DWORD time = GetTickCount();
frames++;
frames_total++;
/* every 1.5 seconds */
if (time - prev_time > 1500)
{
TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
prev_time = time;
frames = 0;
if (start_time == 0) start_time = time;
}
}
return TRUE;
}
/***********************************************************************
* wglCreateLayerContext (OPENGL32.@)
*/
HGLRC WINAPI wglCreateLayerContext(HDC hdc,
int iLayerPlane) {
TRACE("(%p,%d)\n", hdc, iLayerPlane);
if (iLayerPlane == 0) {
return wglCreateContext(hdc);
}
FIXME("no handler for layer %d\n", iLayerPlane);
return NULL;
}
/***********************************************************************
* wglDescribeLayerPlane (OPENGL32.@)
*/
BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
int iPixelFormat,
int iLayerPlane,
UINT nBytes,
LPLAYERPLANEDESCRIPTOR plpd) {
FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
return FALSE;
}
/***********************************************************************
* wglGetLayerPaletteEntries (OPENGL32.@)
*/
int WINAPI wglGetLayerPaletteEntries(HDC hdc,
int iLayerPlane,
int iStart,
int cEntries,
const COLORREF *pcr) {
FIXME("(): stub!\n");
return 0;
}
static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
void WINAPI glGetIntegerv(GLenum pname, GLint *data)
{
const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
TRACE("(%d, %p)\n", pname, data);
if (pname == GL_NUM_EXTENSIONS)
{
struct wgl_handle *ptr = get_current_context_ptr();
if (ptr->u.context->disabled_exts ||
filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
{
const GLuint *disabled_exts = ptr->u.context->disabled_exts;
GLint count, disabled_count = 0;
funcs->gl.p_glGetIntegerv(pname, &count);
while (*disabled_exts++ != ~0u)
disabled_count++;
*data = count - disabled_count;
return;
}
}
funcs->gl.p_glGetIntegerv(pname, data);
}
const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
{
const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
TRACE("(%d, %d)\n", name, index);
if (!funcs->ext.p_glGetStringi)
{
void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
*func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
}
if (name == GL_EXTENSIONS)
{
struct wgl_handle *ptr = get_current_context_ptr();
if (ptr->u.context->disabled_exts ||
filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
{
const GLuint *disabled_exts = ptr->u.context->disabled_exts;
unsigned int disabled_count = 0;
while (index + disabled_count >= *disabled_exts++)
disabled_count++;
return funcs->ext.p_glGetStringi(name, index + disabled_count);
}
}
return funcs->ext.p_glGetStringi(name, index);
}
/* check if the extension is present in the list */
static BOOL has_extension( const char *list, const char *ext, size_t len )
{
if (!list)
{
const char *gl_ext;
unsigned int i;
GLint extensions_count;
glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
for (i = 0; i < extensions_count; ++i)
{
gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
return TRUE;
}
return FALSE;
}
while (list)
{
while (*list == ' ') list++;
if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
list = strchr( list, ' ' );
}
return FALSE;
}
static int compar(const void *elt_a, const void *elt_b) {
return strcmp(((const OpenGL_extension *) elt_a)->name,
((const OpenGL_extension *) elt_b)->name);
}
/* Check if a GL extension is supported */
static BOOL is_extension_supported(const char* extension)
{
enum wgl_handle_type type = get_current_context_type();
const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
const char *gl_ext_string = NULL;
size_t len;
TRACE("Checking for extension '%s'\n", extension);
if (type == HANDLE_CONTEXT)
{
gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
if (!gl_ext_string)
{
ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
return FALSE;
}
}
/* We use the GetProcAddress function from the display driver to retrieve function pointers
* for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
* using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
* require the function to return NULL when an extension isn't found. For this reason we check
* if the OpenGL extension required for the function we are looking up is supported. */
while ((len = strcspn(extension, " ")) != 0)
{
/* Check if the extension is part of the GL extension string to see if it is supported. */
if (has_extension(gl_ext_string, extension, len))
return TRUE;
/* In general an OpenGL function starts as an ARB/EXT extension and at some stage
* it becomes part of the core OpenGL library and can be reached without the ARB/EXT
* suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
* Check if we are searching for a core GL function */
if(strncmp(extension, "GL_VERSION_", 11) == 0)
{
const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
const char *version = extension + 11; /* Move past 'GL_VERSION_' */
if(!gl_version) {
ERR("No OpenGL version found!\n");
return FALSE;
}
/* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
* The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
if( (gl_version[0] > version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
return TRUE;
}
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]);
}
if (extension[len] == ' ') len++;
extension += len;
}
return FALSE;
}
/***********************************************************************
* wglGetProcAddress (OPENGL32.@)
*/
PROC WINAPI wglGetProcAddress( LPCSTR name )
{
struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
void **func_ptr;
OpenGL_extension ext;
const OpenGL_extension *ext_ret;
if (!name) return NULL;
/* Without an active context opengl32 doesn't know to what
* driver it has to dispatch wglGetProcAddress.
*/
if (!get_current_context_ptr())
{
WARN("No active WGL context found\n");
return NULL;
}
ext.name = name;
ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
if (!ext_ret)
{
WARN("Function %s unknown\n", name);
return NULL;
}
func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
if (!*func_ptr)
{
void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
if (!is_extension_supported(ext_ret->extension))
{
unsigned int i;
static const struct { const char *name, *alt; } alternatives[] =
{
{ "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
{ "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
};
for (i = 0; i < sizeof(alternatives)/sizeof(alternatives[0]); i++)
{
if (strcmp( name, alternatives[i].name )) continue;
WARN("Extension %s required for %s not supported, trying %s\n",
ext_ret->extension, name, alternatives[i].alt );
return wglGetProcAddress( alternatives[i].alt );
}
WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
return NULL;
}
if (driver_func == NULL)
{
WARN("Function %s not supported by driver\n", name);
return NULL;
}
*func_ptr = driver_func;
}
TRACE("returning %s -> %p\n", name, ext_ret->func);
return ext_ret->func;
}
/***********************************************************************
* wglRealizeLayerPalette (OPENGL32.@)
*/
BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
int iLayerPlane,
BOOL bRealize) {
FIXME("()\n");
return FALSE;
}
/***********************************************************************
* wglSetLayerPaletteEntries (OPENGL32.@)
*/
int WINAPI wglSetLayerPaletteEntries(HDC hdc,
int iLayerPlane,
int iStart,
int cEntries,
const COLORREF *pcr) {
FIXME("(): stub!\n");
return 0;
}
/***********************************************************************
* wglSwapLayerBuffers (OPENGL32.@)
*/
BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
UINT fuPlanes) {
TRACE("(%p, %08x)\n", hdc, fuPlanes);
if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
if (!wglSwapBuffers( hdc )) return FALSE;
fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
}
if (fuPlanes) {
WARN("Following layers unhandled: %08x\n", fuPlanes);
}
return TRUE;
}
/***********************************************************************
* wglBindTexImageARB
*
* Provided by the WGL_ARB_render_texture extension.
*/
BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
{
struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
BOOL ret;
if (!ptr) return FALSE;
ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
release_handle_ptr( ptr );
return ret;
}
/***********************************************************************
* wglReleaseTexImageARB
*
* Provided by the WGL_ARB_render_texture extension.
*/
BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
{
struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
BOOL ret;
if (!ptr) return FALSE;
ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
release_handle_ptr( ptr );
return ret;
}
/***********************************************************************
* wglSetPbufferAttribARB
*
* Provided by the WGL_ARB_render_texture extension.
*/
BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
{
struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
BOOL ret;