File: make_opengl

package info (click to toggle)
wine-development 2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 187,144 kB
  • ctags: 315,546
  • sloc: ansic: 2,640,448; perl: 18,914; yacc: 15,420; makefile: 8,447; objc: 6,157; lex: 4,268; sh: 886; cpp: 816; awk: 69; xml: 69
file content (995 lines) | stat: -rwxr-xr-x 37,659 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl -w
use strict;
use XML::Simple;

# This script is called thus :
#
#   make_opengl [opengl_version]
#
#     - It needs files from the OpenGL extension registry:
#
#       https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/gl.xml
#       https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/wgl.xml
#
#       If they are not found in the current directory the script will
#       attempt to download them from there.
#
#     - opengl_version is the OpenGL version emulated by the library
#       (can be 1.0 to 1.5). The default is 1.1.
#
# This script generates the three following files :
#
#     - opengl32.spec : the spec file giving all the exported functions
#       of the OpenGL32.DLL library. These functions are the one an
#       application can directly link to (and are all the functions
#       defined in the OpenGL core for the version defined by
#       'opengl_version').
#
#     - opengl_norm.c : this file contains the thunks for all OpenGL
#       functions that are defined in 'opengl32.spec'. The corresponding
#       functions NEED to be defined in Linux's libGL or the library
#       won't be able to be linked in.
#
#     - opengl_ext.c : in this file are stored thunks for ALL possible
#       OpenGL extensions (at least, all the extensions that are defined
#       in the OpenGL extension registry). Contrary to 'opengl_norm.c',
#       you do not need to have these extensions in your libGL to have
#       OpenGL work (as they are resolved at run-time using
#       glXGetProcAddressARB).
#
#     - include/wine/wgl_driver.h: definitions for the tables of OpenGL functions.
#
#
# Copyright 2000 Lionel Ulmer
# Copyright 2012 Alexandre Julliard
#
# 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
#

#
# Files to generate
#
my $spec_file = "opengl32.spec";
my $norm_file = "opengl_norm.c";
my $ext_file  = "opengl_ext.c";
my $wgl_driver_file = "../../include/wine/wgl_driver.h";
my $wgl_file = "../../include/wine/wgl.h";

# Set to 0 for removing the ENTER / LEAVE GL calls
my $gen_thread_safe = 0;
# Prefix used for the local variables
my $ext_prefix = "func_";
# If set to 1, generate TRACEs for each OpenGL function
my $gen_traces = 1;

#
# List of categories to put in the 'opengl_norm.c' file
#
my %cat_1_0 = ( "GL_VERSION_1_0" => 1 );
my %cat_1_1 = ( %cat_1_0, "GL_VERSION_1_1" => 1 );
my %cat_1_2 = ( %cat_1_1, "GL_VERSION_1_2" => 1 );
my %cat_1_3 = ( %cat_1_2, "GL_VERSION_1_3" => 1 );
my %cat_1_4 = ( %cat_1_3, "GL_VERSION_1_4" => 1 );
my %cat_1_5 = ( %cat_1_4, "GL_VERSION_1_5" => 1 );

my %norm_categories = ();

#
# This hash table gives the conversion between OpenGL types and what
# is used by the TRACE printfs
#
my %debug_conv =
    ("GLbitfield" => "%d",
     "GLboolean" => "%d",
     "GLbyte" => "%d",
     "GLclampd" => "%f",
     "GLclampf" => "%f",
     "GLclampx" => "%d",
     "GLdouble" => "%f",
     "GLenum" => "%d",
     "GLfloat" => "%f",
     "GLfixed" => "%d",
     "GLint" => "%d",
     "GLshort" => "%d",
     "GLsizei" => "%d",
     "GLstring" => "%s",
     "GLsync" => "%p",
     "GLubyte" => "%d",
     "GLuint" => "%d",
     "GLushort" => "%d",
     "GLhalfNV" => "%d",
     "GLintptrARB" => "%ld",
     "GLsizeiptrARB" => "%ld",
     "GLintptr" => "%ld",
     "GLsizeiptr" => "%ld",
     "GLhandleARB" => "%d",
     "GLcharARB" => "%c",
     "GLuint64" => "%s,wine_dbgstr_longlong(%s)",
     "GLint64" => "%s,wine_dbgstr_longlong(%s)",
     "GLuint64EXT" => "%s,wine_dbgstr_longlong(%s)",
     "GLint64EXT" => "%s,wine_dbgstr_longlong(%s)",
     "GLvoid" => "(void)",
     "_GLfuncptr" => "%p",
     "GLDEBUGPROC" => "%p",
     "GLDEBUGPROCARB" => "%p",
     "GLDEBUGPROCAMD" => "%p",
     "GLDEBUGPROCKHR" => "%p",
     "GLvdpauSurfaceNV" => "%ld",
     "int" => "%d",
     "unsigned int" => "%u",
     "UINT" => "%u",
     "DWORD" => "%u",
     "BOOL" => "%u",
     "INT64" => "%s,wine_dbgstr_longlong(%s)",
     "UINT64" => "%s,wine_dbgstr_longlong(%s)",
     "LPVOID" => "%p",
     "HANDLE" => "%p",
     "HDC" => "%p",
     "HGLRC" => "%p",
     "HPBUFFERARB" => "%p",
     "HPBUFFEREXT" => "%p",
    );

#
# This hash table gives the conversion between OpenGL types and what
# is used in the .spec and header files.
#
my %arg_conv =
    ("GLbitfield" =>       [ "long", "unsigned int" ],
     "GLboolean" =>        [ "long", "unsigned char" ],
     "GLbyte" =>           [ "long", "signed char" ],
     "GLchar" =>           [ "long", "char" ],
     "GLclampd" =>         [ "double", "double" ],
     "GLclampf" =>         [ "float", "float" ],
     "GLclampx" =>         [ "long", "int" ],
     "GLdouble" =>         [ "double", "double" ],
     "GLenum" =>           [ "long", "unsigned int" ],
     "GLfloat" =>          [ "float", "float" ],
     "GLfixed" =>          [ "long", "int" ],
     "GLint" =>            [ "long", "int" ],
     "GLint64" =>          [ "int64", "INT64" ],
     "GLint64EXT" =>       [ "int64", "INT64" ],
     "GLintptr" =>         [ "long", "INT_PTR" ],
     "GLshort" =>          [ "long", "short" ],
     "GLsizei" =>          [ "long", "int" ],
     "GLsizeiptr" =>       [ "long", "INT_PTR" ],
     "GLstring" =>         [ "str", "const unsigned char *" ],
     "GLsync" =>           [ "ptr", "struct __GLsync *" ],
     "GLubyte" =>          [ "long", "unsigned char" ],
     "GLuint" =>           [ "long", "unsigned int" ],
     "GLuint64" =>         [ "int64", "UINT64" ],
     "GLuint64EXT" =>      [ "int64", "UINT64" ],
     "GLushort" =>         [ "long", "unsigned short" ],
     "GLvoid" =>           [ "void", "void" ],
     "GLcharARB" =>        [ "long", "char" ],
     "GLhandleARB" =>      [ "long", "unsigned int" ],
     "GLintptrARB" =>      [ "long", "INT_PTR" ],
     "GLsizeiptrARB" =>    [ "long", "INT_PTR" ],
     "GLhalfNV" =>         [ "long", "unsigned short" ],
     "GLvdpauSurfaceNV" => [ "long", "INT_PTR" ]);

#
# Used to convert some types
#
sub ConvertType($)
{
    my ($type) = @_;

    my %hash = (
              "struct _cl_context" => "void",
              "struct _cl_event" => "void",
              "HGLRC" => "struct wgl_context *",
              "GLDEBUGPROC" => "void *",
              "GLDEBUGPROCARB" => "void *",
              "GLDEBUGPROCAMD" => "void *",
              "GLDEBUGPROCKHR" => "void *",
              "HPBUFFERARB" => "struct wgl_pbuffer *",
              "HPBUFFEREXT" => "struct wgl_pbuffer *",
        );

    foreach my $org (reverse sort keys %hash) {
        if ($type =~ /^(.*)$org(.*)$/) {
            return "$1$hash{$org}$2";
        }
    }
    return $type;
}

#
# Used to convert some variable names
#
sub ConvertVarName($)
{
    my ($type) = @_;

    my %hash = ( "near" => "nearParam",
                 "far"  => "farParam" );

    foreach my $org (keys %hash) {
        if ($type =~ /^(.*)$org(.*)$/) {
            return "$1$hash{$org}$2";
        }
    }
    return $type;
}

#
# This functions generates the thunk for a given function.
#
sub GenerateThunk($$$$)
{
    my ($name, $func_ref, $comment, $prefix) = @_;
    my $ret = "";
    my $call_arg = "";
    my $trace_call_arg = "";
    my $trace_arg = "";

    return "" if $name eq "glDebugEntry";
    return "" if $name eq "glGetIntegerv";
    return "" if $name eq "glGetString";
    return "" if $func_ref->[2] && $func_ref->[2]->[0] =~ /WGL_/;

    # If for opengl_norm.c, generate a nice heading otherwise Patrik won't be happy :-)
    # Patrik says: Well I would be even happier if a (OPENGL32.@) was added as well. Done. :-)
    if ($comment eq 1) {
        $ret .= "/***********************************************************************\n";
        $ret .= " *              $name (OPENGL32.\@)\n";
        $ret .= " */\n";
    }
    $ret .= ConvertType($func_ref->[0]) . " WINAPI $name( ";
    for (my $i = 0; $i < @{$func_ref->[1]}; $i++) {
        my $type = $func_ref->[1]->[$i]->[0];
        my $name = ConvertVarName($func_ref->[1]->[$i]->[1]);
        my ($base_type, $type_specifier, $name_specifier) = ($type, $type, $name);
        if ($type =~ /(.*) (.*)/) {
            $base_type = $2;
        } elsif ($type =~ /(.*)(\[.*)/) {
            $base_type = $1;
            $type_specifier = $1;
            $name_specifier = $name . $2
        }
        $ret .= ConvertType($type_specifier) . " $name_specifier";
        $call_arg .= $name;
        if ($type =~ /\*/ || $type =~ /\[/) {
            $trace_arg .= "%p";
            $trace_call_arg .= $name;
        } elsif (defined $debug_conv{$base_type}) {
            if ($debug_conv{$base_type} =~ /(.*),(.*)/) {
                $trace_arg .= $1;
                $trace_call_arg .= sprintf $2, $name;
            } else {
                $trace_arg .= $debug_conv{$base_type};
                $trace_call_arg .= $name;
            }
        }
        else { printf "Unknown type %s\n", $type; }
        if ($i+1 < @{$func_ref->[1]}) {
            $ret .= ", ";
            $call_arg .= ", ";
            $trace_call_arg .= ", ";
            $trace_arg .= ", ";
        } else {
            $ret .= " ";
            $call_arg .= " ";
            $trace_call_arg .= " ";
        }
    }
    $ret .= 'void ' if (!@{$func_ref->[1]});
    return "$ret) DECLSPEC_HIDDEN;\n" if $name eq "glGetStringi";
    $ret .= ") {\n";
    $ret .= "  const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;\n";
    if ($func_ref->[0] ne "void" && $gen_thread_safe) {
        $ret .= "  " . ConvertType($func_ref->[0]) . " ret_value;\n";
    }
    if ($gen_traces) {
        $ret .= "  TRACE(\"($trace_arg)\\n\"";
        if ($trace_arg ne "") {
            $ret .= ", $trace_call_arg";
        }
        $ret .= ");\n";
    }
    if ($gen_thread_safe) {
        $ret .= "  ENTER_GL();\n";
        $ret .= "  ";
        if ($func_ref->[0] ne "void") {
            $ret .= "ret_value = ";
        }
        $ret .= "funcs->$prefix.p_$name( $call_arg);\n";
        $ret .= "  LEAVE_GL();\n";
        if ($func_ref->[0] ne "void") {
            $ret .= "  return ret_value;\n"
        }
    }
    else {
        $ret .= "  ";
        if ($func_ref->[0] ne "void") {
            $ret .= "return ";
        }
        $ret .= "funcs->$prefix.p_$name( $call_arg);\n";
    }
    $ret .= "}\n";

    # Return this string....
    return $ret;
}

sub generate_null_func($$)
{
    my ($name, $func_ref) = @_;
    my $ret;

    return "" if $name eq "glDebugEntry";

    $ret = "static " . ConvertType($func_ref->[0]) . " null_$name( ";
    for (my $i = 0; $i < @{$func_ref->[1]}; $i++) {
        my $type = $func_ref->[1]->[$i]->[0];
        my $name = ConvertVarName($func_ref->[1]->[$i]->[1]);
        my $base_type;
        if ($type =~ /(.*)(\[.*)/) {
            $base_type = $1;
            $name .= $2;
        } else {
            $base_type = $type;
        }
        $ret .= ConvertType($base_type) . " $name";
        $ret .= "," if ($i+1 < @{$func_ref->[1]});
        $ret .= " ";
    }
    $ret .= 'void ' if (!@{$func_ref->[1]});
    $ret .= ") {";
    if ($name eq "glGetError")
    {
        $ret .= " return GL_INVALID_OPERATION;";
    }
    elsif ($func_ref->[0] ne "void")
    {
        $ret .= " return 0;";
    }
    $ret .= " }\n";
    return $ret;
}

sub get_func_proto($$$)
{
    my ($format, $name, $func) = @_;
    my $ret = sprintf "%-10s", ConvertType($func->[0]);
    $ret .= " " . sprintf($format,$name) . "(";
    for (my $i = 0; $i < @{$func->[1]}; $i++)
    {
        $ret .= ConvertType($func->[1]->[$i]->[0]);
        $ret .= "," if ($i+1 < @{$func->[1]});
    }
    $ret .= "void" unless @{$func->[1]};
    $ret .= ")";
    return $ret;
}

#
# Extract and checks the number of arguments
#
if (@ARGV > 1) {
    my $name0=$0;
    $name0=~s%^.*/%%;
    die "Usage: $name0 [version]\n";
}
my $version = $ARGV[0] || "1.1";
if ($version eq "1.0") {
    %norm_categories = %cat_1_0;
} elsif ($version eq "1.1") {
    %norm_categories = %cat_1_1;
} elsif ($version eq "1.2") {
    %norm_categories = %cat_1_2;
} elsif ($version eq "1.3") {
    %norm_categories = %cat_1_3;
} elsif ($version eq "1.4") {
    %norm_categories = %cat_1_4;
} elsif ($version eq "1.5") {
    %norm_categories = %cat_1_5;
} else {
    die "Incorrect OpenGL version.\n";
}

#
# Then, create the list of all OpenGL functions using the registry
# files. This will create two hash-tables, one with all the function
# whose category matches the one listed in '@norm_categories', the other
# with all other functions.
#
# An element of the hash table is a reference to an array with these
# elements :
#
#  - function name
#
#  - return type
#
#  - reference to an array giving the list of arguments (an empty array
#    for a 'void' function).
#
# The list of arguments is itself an array of reference to arrays. Each
# of these arrays represents the argument type and the argument name.
#
# An example :
#
# void glBitmap( GLsizei width, GLsizei height,
#                GLfloat xorig, GLfloat yorig,
#                GLfloat xmove, GLfloat ymove,
#                const GLubyte *bitmap );
#
# Would give something like that :
#
# [ "glBitmap",
#   "void",
#   [ [ "GLsizei", "width" ],
#     [ "GLsizei", "height" ],
#     [ "GLfloat", "xorig" ],
#     [ "GLfloat", "yorig" ],
#     [ "GLfloat", "xmove" ],
#     [ "GLfloat", "ymove" ],
#     [ "GLubyte *", "bitmap"] ] ];
#
my %norm_functions = ( "glDebugEntry" => [ "GLint", [[ "GLint", "unknown1" ],
                                                     [ "GLint", "unknown2" ]] ] );

#
# This stores various extensions NOT part of the GL extension registry but still
# implemented by most OpenGL libraries out there...
#

my %ext_functions  =
   (
    "glDeleteBufferRegion" => [ "void", [ [ "GLenum", "region" ] ], [ "GL_KTX_buffer_region" ] ],
    "glReadBufferRegion" => [ "void", [ [ "GLenum", "region" ],
                                        [ "GLint", "x" ],
                                        [ "GLint", "y" ],
                                        [ "GLsizei", "width" ],
                                        [ "GLsizei", "height" ] ], [ "GL_KTX_buffer_region" ] ],
    "glDrawBufferRegion" => [ "void", [ [ "GLenum", "region" ],
                                        [ "GLint", "x" ],
                                        [ "GLint", "y" ],
                                        [ "GLsizei", "width" ],
                                        [ "GLsizei", "height" ],
                                        [ "GLint", "xDest" ],
                                        [ "GLint", "yDest" ] ], [ "GL_KTX_buffer_region" ] ],
    "glBufferRegionEnabled" => [ "GLuint", [ ], [ "GL_KTX_buffer_region" ] ],
    "glNewBufferRegion" => [ "GLuint", [ [ "GLenum", "type" ] ], [ "GL_KTX_buffer_region" ] ],
    "glMTexCoord2fSGIS" => [ "void", [ [ "GLenum", "target" ],
                                       [ "GLfloat", "s" ],
                                       [ "GLfloat", "t" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMTexCoord2fvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                        [ "GLfloat *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1dSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLdouble", "s" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1dvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLdouble *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1fSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLfloat", "s" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1fvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "const GLfloat *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1iSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLint", "s" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1ivSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLint *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1sSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLshort", "s" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord1svSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLshort *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2dSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLdouble", "s"],
                                           [ "GLdouble", "t" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2dvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLdouble *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2fSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLfloat", "s" ],
                                           [ "GLfloat", "t" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2fvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLfloat *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2iSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLint", "s" ],
                                           [ "GLint", "t" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2ivSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLint *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2sSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLshort", "s" ],
                                           [ "GLshort", "t" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord2svSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLshort *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3dSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLdouble", "s" ],
                                           [ "GLdouble", "t" ],
                                           [ "GLdouble", "r" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3dvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLdouble *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3fSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLfloat", "s" ],
                                           [ "GLfloat", "t" ],
                                           [ "GLfloat", "r" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3fvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLfloat *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3iSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLint", "s" ],
                                           [ "GLint", "t" ],
                                           [ "GLint", "r" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3ivSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLint *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3sSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLshort", "s" ],
                                           [ "GLshort", "t" ],
                                           [ "GLshort", "r" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord3svSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLshort *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4dSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLdouble", "s" ],
                                           [ "GLdouble", "t" ],
                                           [ "GLdouble", "r" ],
                                           [ "GLdouble", "q" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4dvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLdouble *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4fSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLfloat", "s" ],
                                           [ "GLfloat", "t" ],
                                           [ "GLfloat", "r" ],
                                           [ "GLfloat", "q" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4fvSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLfloat *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4iSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLint", "s" ],
                                           [ "GLint", "t" ],
                                           [ "GLint", "r" ],
                                           [ "GLint", "q" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4ivSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLint *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4sSGIS" => [ "void", [ [ "GLenum", "target" ],
                                           [ "GLshort", "s" ],
                                           [ "GLshort", "t" ],
                                           [ "GLshort", "r" ],
                                           [ "GLshort", "q" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoord4svSGIS" => [ "void", [ [ "GLenum", "target" ],
                                            [ "GLshort *", "v" ] ], [ "GL_SGIS_multitexture" ] ],
    "glMultiTexCoordPointerSGIS" => [ "void", [ [ "GLenum", "target" ],
                                                [ "GLint", "size" ],
                                                [ "GLenum", "type" ],
                                                [ "GLsizei", "stride" ],
                                                [ "GLvoid *", "pointer" ] ], [ "GL_SGIS_multitexture" ] ],
    "glSelectTextureSGIS" => [ "void", [ [ "GLenum", "target" ] ], [ "GL_SGIS_multitexture" ] ],
    "glSelectTextureCoordSetSGIS" => [ "void", [ [ "GLenum", "target" ] ], [ "GL_SGIS_multitexture" ] ],
    "glDeleteObjectBufferATI" => [ "void", [ [ "GLuint", "buffer" ] ], [ "GL_ATI_vertex_array_object" ] ],
    "wglSetPixelFormatWINE" => [ "BOOL", [ [ "HDC", "hdc" ],
                                           [ "int", "format" ] ], [ "WGL_WINE_pixel_format_passthrough" ] ],
    "wglQueryCurrentRendererIntegerWINE" => [ "BOOL", [ [ "GLenum", "attribute" ],
                                                        [ "GLuint *", "value" ] ], [ "WGL_WINE_query_renderer" ] ],
    "wglQueryCurrentRendererStringWINE" => [ "const GLchar *", [ [ "GLenum", "attribute" ] ],
                                             [ "WGL_WINE_query_renderer" ] ],
    "wglQueryRendererIntegerWINE" => [ "BOOL", [ [ "HDC", "dc" ],
                                                 [ "GLint", "renderer" ],
                                                 [ "GLenum", "attribute" ],
                                                 [ "GLuint *", "value" ] ], [ "WGL_WINE_query_renderer" ] ],
    "wglQueryRendererStringWINE" => [ "const GLchar *", [ [ "HDC", "dc" ],
                                                          [ "GLint", "renderer" ],
                                                          [ "GLenum", "attribute" ] ], [ "WGL_WINE_query_renderer" ] ],
   );


my %wgl_functions =
   (
    "wglCopyContext" => [ "BOOL", [ [ "struct wgl_context *", "src" ],
                                    [ "struct wgl_context *", "dst" ],
                                    [ "UINT", "mask" ] ] ],
    "wglCreateContext" => [ "struct wgl_context *", [ [ "HDC", "hdc" ] ] ],
    "wglDeleteContext" => [ "void", [ [ "struct wgl_context *", "context" ] ] ],
    "wglDescribePixelFormat" => [ "INT", [ [ "HDC", "hdc" ],
                                           [ "INT", "format" ],
                                           [ "UINT", "size" ],
                                           [ "PIXELFORMATDESCRIPTOR *", "descr" ] ] ],
    "wglGetPixelFormat" => [ "INT", [ [ "HDC", "hdc" ] ] ],
    "wglGetProcAddress" => [ "PROC", [ [ "LPCSTR", "name" ] ] ],
    "wglMakeCurrent" => [ "BOOL", [ [ "HDC", "hdc" ],
                                    [ "struct wgl_context *", "context" ] ] ],
    "wglSetPixelFormat" => [ "BOOL", [ [ "HDC", "hdc" ],
                                       [ "INT", "format" ],
                                       [ "const PIXELFORMATDESCRIPTOR *", "descr" ] ] ],
    "wglShareLists" => [ "BOOL", [ [ "struct wgl_context *", "org" ],
                                   [ "struct wgl_context *", "dst" ] ] ],
    "wglSwapBuffers" => [ "BOOL", [ [ "HDC", "hdc" ] ] ],
   );

my %supported_wgl_extensions =
   (
    "WGL_ARB_create_context" => 1,
    "WGL_ARB_extensions_string" => 1,
    "WGL_ARB_make_current_read" => 1,
    "WGL_ARB_pbuffer" => 1,
    "WGL_ARB_pixel_format" => 1,
    "WGL_ARB_render_texture" => 1,
    "WGL_EXT_extensions_string" => 1,
    "WGL_EXT_swap_control" => 1,
    "WGL_NV_vertex_array_range" => 1,
    "WGL_WINE_pixel_format_passthrough" => 1,
   );

my %enums =
   (
    "WGL_RENDERER_VENDOR_ID_WINE" => "0x8183",
    "WGL_RENDERER_DEVICE_ID_WINE" => "0x8184",
    "WGL_RENDERER_VERSION_WINE" => "0x8185",
    "WGL_RENDERER_ACCELERATED_WINE" => "0x8186",
    "WGL_RENDERER_VIDEO_MEMORY_WINE" => "0x8187",
    "WGL_RENDERER_UNIFIED_MEMORY_ARCHITECTURE_WINE" => "0x8188",
    "WGL_RENDERER_PREFERRED_PROFILE_WINE" => "0x8189",
    "WGL_RENDERER_OPENGL_CORE_PROFILE_VERSION_WINE" => "0x818A",
    "WGL_RENDERER_OPENGL_COMPATIBILITY_PROFILE_VERSION_WINE" => "0x818B",
    "WGL_RENDERER_OPENGL_ES_PROFILE_VERSION_WINE" => "0x818C",
    "WGL_RENDERER_OPENGL_ES2_PROFILE_VERSION_WINE" => "0x818D",
    "WGL_RENDERER_ID_WINE" => "0x818E",
   );

sub parse_variable($)
{
    my $p = shift;
    my $ptype = '';
    my $pname = '';
    my $pnamebefore = '';
    my $pnameafter = '';

    while (my ($k, $v) = each(%$p)){
        if ($k eq 'ptype') {
            $ptype = ${$v}[0];
        } elsif ($k eq 'name') {
            $pname = ${$v}[0];
        } elsif ($k eq 'content') {
            if (ref($v) eq 'ARRAY') {
                my @n = @{$v};
                $pnamebefore = $n[0];
                $pnameafter = $n[1] if (@n > 0);
            } elsif ($v eq 'const ') {
                $pnamebefore = $v;
            } else {
                $pnameafter = $v;
            }
        }
    }
    $ptype = $pnamebefore . $ptype . $pnameafter;
    $ptype =~ s/ \*/\*/g;
    $ptype =~ s/ $//g;
    return [ $ptype, $pname ];
}

sub parse_file($$)
{
    my ($file, $generate_enums) = @_;
    my $xml = new XML::Simple;
    my $data = $xml->XMLin($file, ForceArray => 1);
    my %functions;

    # save all functions
    for my $command ( @{${$data->{commands}}[0]->{'command'}} ) {
        my $name = '';
        my $ret = '';
        my $params = [];
        my @alias = '';
        while (my ($k, $v) = each(%$command)){
            if ($k eq 'param') {
                push(@$params, parse_variable($_)) for (@{$v});
            } elsif ($k eq 'proto') {
                ($ret, $name) = @{parse_variable(${$v}[0])};
            }
        }
        $functions{$name} = [ $ret, $params ];
    }

    # save all enums (only GL)
    if ($generate_enums) {
        for my $enum ( @{$data->{'enums'}} ) {
            if (ref($enum->{'enum'}) eq "HASH") {
                while (my ($k, $v) = each(%{$enum->{'enum'}})){
                    $enums{$k} = $v->{'value'};
                }
            }
        }
    }

    # generate norm functions
    while (my ($k, $v) = each(%{$data->{feature}})) {
        if ($norm_categories{$k}) {
            for my $req (@{$v->{require}}) {
                for(keys %{$req->{command}}) {
                    $norm_functions{$_} = $functions{$_};
                }
            }
        }
    }

    # generate extension functions from norm functions, if they are newer than the category
    my %features = %{$data->{feature}};
    foreach (sort keys %features) {
        my ($k, $v) = %features{$_};
        if (!$norm_categories{$k} && $v->{api} =~ /^gl(\||$)/)
        {
            for my $req (@{$v->{require}}) {
                for (keys %{$req->{command}}) {
                    if (!$ext_functions{$_} && !$norm_functions{$_}) {
                        $ext_functions{$_} = [ $functions{$_}[0], $functions{$_}[1], [ $k ] ];
                    }
                }
            }
        }
    }

    # generate extension functions
    while (my ($k, $v) = each(%{${$data->{extensions}}[0]->{extension}})) {
        if ($v->{supported} =~ /^gl(\||$)/) {
            for my $req (@{$v->{require}}) {
                if (!defined $req->{api} || $req->{api} =~ /^gl(\||$)/) {
                    for (keys %{$req->{command}}) {
                        if (!$ext_functions{$_} && !$norm_functions{$_}) {
                            $ext_functions{$_} = [$functions{$_}[0], $functions{$_}[1], [ $k ]];
                        }
                        elsif ($ext_functions{$_}) {
                            push @{$ext_functions{$_}->[2]}, $k;
                        }
                    }
                }
            }
        }
        elsif ($v->{supported} =~ /^wgl$/) {
            for (keys %{${$v->{require}}[0]->{command}}) {
                if (defined $supported_wgl_extensions{$k}) {
                    $ext_functions{$_} = [ $functions{$_}[0], $functions{$_}[1], [ $k ] ];
                }
            }
        }
    }
}

parse_file( "/usr/share/khronos-api/gl.xml", 1 );
parse_file( "/usr/share/khronos-api/wgl.xml", 0 );

my $wgl_version = 11;

#
# Generate the wglext.h file
#
my $wglext_file = "../../include/wine/wglext.h";
open HEADER, ">$wglext_file" or die "cannot create $wglext_file";
print HEADER "#include <khronos-api/GL/wglext.h>";
close HEADER;

#
# Generate the wgl_driver.h file
#
open HEADER, ">$wgl_driver_file" or die "cannot create $wgl_driver_file";
print HEADER "/* Automatically generated from http://www.opengl.org/registry files; DO NOT EDIT! */\n\n";
print HEADER "#ifndef __WINE_WGL_DRIVER_H\n";
print HEADER "#define __WINE_WGL_DRIVER_H\n\n";
print HEADER "#ifndef WINE_GLAPI\n";
print HEADER "#define WINE_GLAPI\n";
print HEADER "#endif\n\n";

printf HEADER "#define WINE_WGL_DRIVER_VERSION %u\n\n", $wgl_version + 1;

print HEADER "struct wgl_context;\n";
print HEADER "struct wgl_pbuffer;\n\n";

print HEADER "struct opengl_funcs\n{\n";
print HEADER "    struct\n    {\n";
foreach (sort keys %wgl_functions)
{
    printf HEADER "        %s;\n", get_func_proto("(WINE_GLAPI *p_%s)", $_, $wgl_functions{$_});
}
print HEADER "    } wgl;\n\n";

print HEADER "    struct\n    {\n";
foreach (sort keys %norm_functions)
{
    next if $_ eq "glDebugEntry";
    printf HEADER "        %s;\n", get_func_proto("(WINE_GLAPI *p_%s)", $_, $norm_functions{$_});
}
print HEADER "    } gl;\n\n";

print HEADER "    struct\n    {\n";
foreach (sort keys %ext_functions)
{
    printf HEADER "        %s;\n", get_func_proto("(WINE_GLAPI *p_%s)", $_, $ext_functions{$_});
}
print HEADER "    } ext;\n";
print HEADER "};\n\n";

print HEADER "#define ALL_WGL_FUNCS";
foreach (sort keys %norm_functions)
{
    next if $_ eq "glDebugEntry";
    printf HEADER " \\\n    USE_GL_FUNC(\%s)", $_;
}
print HEADER "\n\n";

print HEADER "extern struct opengl_funcs * CDECL __wine_get_wgl_driver( HDC hdc, UINT version );\n";
print HEADER "extern BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format );\n\n";
print HEADER "#endif /* __WINE_WGL_DRIVER_H */\n";
close HEADER;

#
# Generate the wgl.h file
#
open HEADER, ">$wgl_file" or die "cannot create $wgl_file";
print HEADER "/* Automatically generated from http://www.opengl.org/registry files; DO NOT EDIT! */\n\n";
print HEADER "#ifndef __WINE_WGL_H\n";
print HEADER "#define __WINE_WGL_H\n\n";

print HEADER "#ifndef GLAPIENTRY\n";
print HEADER "#define GLAPIENTRY __stdcall\n";
print HEADER "#endif\n\n";

foreach (sort keys %arg_conv)
{
    printf HEADER "typedef %-22s %s;\n", $arg_conv{$_}[1], $_;
}
print HEADER "\n";

my $maxlen = 1;
foreach (keys %enums) { $maxlen = length($_) if length($_) > $maxlen; }
foreach (sort keys %enums)
{
    printf HEADER "#define %-*s %s\n", $maxlen, $_, $enums{$_};
}
print HEADER "\n";

foreach (sort keys %norm_functions)
{
    printf HEADER "%s;\n", get_func_proto("GLAPIENTRY %s", $_, $norm_functions{$_});
}

print HEADER "\n#endif /* __WINE_WGL_H */\n";
close HEADER;

#
# Now, generate the output files. First, the spec file.
#
open(SPEC, ">$spec_file") or die "cannot create $spec_file";

foreach (sort keys %norm_functions) {
    my $args=" ";
    for (my $i = 0; $i < @{$norm_functions{$_}->[1]}; $i++) {
        my $type = $norm_functions{$_}->[1]->[$i]->[0];
        if ($type =~ /\*/) {
            $args .= "ptr ";
        } elsif (defined($arg_conv{$type})) {
            $args .= "$@$arg_conv{$type}[0] ";
        } else {
            die "No conversion for GL type $type...\n";
        }
    }
    $args = substr($args,1,-1);
    print SPEC "@ stdcall $_($args)\n";
}

print SPEC "@ stdcall wglChoosePixelFormat(long ptr)
@ stdcall wglCopyContext(long long long)
@ stdcall wglCreateContext(long)
@ stdcall wglCreateLayerContext(long long)
@ stdcall wglDeleteContext(long)
@ stdcall wglDescribeLayerPlane(long long long long ptr)
@ stdcall wglDescribePixelFormat(long long long ptr)
@ stdcall wglGetCurrentContext()
@ stdcall wglGetCurrentDC()
@ stub    wglGetDefaultProcAddress
@ stdcall wglGetLayerPaletteEntries(long long long long ptr)
@ stdcall wglGetPixelFormat(long)
@ stdcall wglGetProcAddress(str)
@ stdcall wglMakeCurrent(long long)
@ stdcall wglRealizeLayerPalette(long long long)
@ stdcall wglSetLayerPaletteEntries(long long long long ptr)
@ stdcall wglSetPixelFormat(long long ptr)
@ stdcall wglShareLists(long long)
@ stdcall wglSwapBuffers(long)
@ stdcall wglSwapLayerBuffers(long long)
@ stdcall wglUseFontBitmapsA(long long long long)
@ stdcall wglUseFontBitmapsW(long long long long)
@ stdcall wglUseFontOutlinesA(long long long long long long long ptr)
@ stdcall wglUseFontOutlinesW(long long long long long long long ptr)
";

close(SPEC);

#
# After the spec file, the opengl_norm.c file
#
open(NORM, ">$norm_file") or die "cannot create $norm_file";
print NORM "
/* Auto-generated file... Do not edit ! */

#include \"config.h\"
#include <stdarg.h>
#include \"winternl.h\"
#include \"wingdi.h\"
#include \"wine/wgl.h\"
#include \"wine/wgl_driver.h\"
#include \"wine/debug.h\"

WINE_DEFAULT_DEBUG_CHANNEL(opengl);
";

foreach (sort keys %norm_functions) {
    my $string = GenerateThunk($_, $norm_functions{$_}, 1, "gl");
    print NORM "\n$string" if $string;
}

foreach (sort keys %wgl_functions) {
    print NORM generate_null_func($_, $wgl_functions{$_});
}
foreach (sort keys %norm_functions) {
    print NORM generate_null_func($_, $norm_functions{$_});
}
foreach (sort keys %ext_functions) {
    print NORM generate_null_func($_, $ext_functions{$_});
}

print NORM "\nstruct opengl_funcs null_opengl_funcs =\n{\n    {\n";
foreach (sort keys %wgl_functions) { print NORM "        null_$_,\n"; }
print NORM "    },\n    {\n";
foreach (sort keys %norm_functions) { print NORM "        null_$_,\n" unless $_ eq "glDebugEntry"; }
print NORM "    },\n    {\n";
foreach (sort keys %ext_functions) { print NORM "        null_$_,\n"; }
print NORM "    }\n};\n";

close(NORM);

#
# Finally, more complex, the opengl_ext.c file
#
open(EXT, ">$ext_file") or die "cannot create $ext_file";
print EXT "
/* Auto-generated file... Do not edit ! */

#include \"config.h\"
#include <stdarg.h>
#include \"opengl_ext.h\"
#include \"winternl.h\"
#include \"wingdi.h\"
#include \"wine/wgl.h\"
#define WGL_WGLEXT_PROTOTYPES
#include \"wine/wglext.h\"
#include \"wine/wgl_driver.h\"
#include \"wine/debug.h\"

WINE_DEFAULT_DEBUG_CHANNEL(opengl);

";

# The thunks themselves....
my $count = keys %ext_functions;
print EXT "const int extension_registry_size = $count;\n";
foreach (sort keys %ext_functions) {
    my $string = GenerateThunk($_, $ext_functions{$_}, 0, "ext");
    if ($string =~ /DECLSPEC_HIDDEN/) {
        print EXT "\n$string";
    } else {
        print EXT "\nstatic $string" if $string;
    }
}

# Then the table giving the string <-> function correspondence */
print EXT "\nconst OpenGL_extension extension_registry[$count] = {\n";
my $i = 0;
foreach (sort keys %ext_functions) {
    my $func_ref = $ext_functions{$_};
    printf EXT "  { \"%s\", \"%s\", %s }", $_, join(" ", sort @{$func_ref->[2]}), $_;
    if ($i != $count-1) {
        print EXT ",";
    }
    $i++;
    print EXT "\n";
}
print EXT "};\n";

close(EXT);