msvcp: Sync fpos usage and istream<>::tellg.
[wine] / dlls / opengl32 / make_opengl
1 #!/usr/bin/perl -w
2 use strict;
3
4 # This script is called thus :
5 #
6 #   make_opengl [opengl_version]
7 #
8 #     - It needs files from the OpenGL extension registry:
9 #
10 #       http://www.opengl.org/registry/api/gl.spec
11 #       http://www.opengl.org/registry/api/gl.tm
12 #       http://www.opengl.org/registry/api/wgl.tm
13 #       http://www.opengl.org/registry/api/wglext.spec
14 #
15 #       If they are not found in the current directory the script will
16 #       attempt to download them from there.
17 #
18 #     - opengl_version is the OpenGL version emulated by the library
19 #       (can be 1.0 to 1.5). The default is 1.1.
20 #
21 # This script generates the three following files :
22 #
23 #     - opengl32.spec : the spec file giving all the exported functions
24 #       of the OpenGL32.DLL library. These functions are the one an
25 #       application can directly link to (and are all the functions
26 #       defined in the OpenGL core for the version defined by
27 #       'opengl_version').
28 #
29 #     - opengl_norm.c : this file contains the thunks for all OpenGL
30 #       functions that are defined in 'opengl32.spec'. The corresponding
31 #       functions NEED to be defined in Linux's libGL or the library
32 #       won't be able to be linked in.
33 #
34 #     - opengl_ext.c : in this file are stored thunks for ALL possible
35 #       OpenGL extensions (at least, all the extensions that are defined
36 #       in the OpenGL extension registry). Contrary to 'opengl_norm.c',
37 #       you do not need to have these extensions in your libGL to have
38 #       OpenGL work (as they are resolved at run-time using
39 #       glXGetProcAddressARB).
40 #
41 #     - include/wine/wgl_driver.h: definitions for the tables of OpenGL functions.
42 #
43 #
44 # Copyright 2000 Lionel Ulmer
45 # Copyright 2012 Alexandre Julliard
46 #
47 # This library is free software; you can redistribute it and/or
48 # modify it under the terms of the GNU Lesser General Public
49 # License as published by the Free Software Foundation; either
50 # version 2.1 of the License, or (at your option) any later version.
51 #
52 # This library is distributed in the hope that it will be useful,
53 # but WITHOUT ANY WARRANTY; without even the implied warranty of
54 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
55 # Lesser General Public License for more details.
56 #
57 # You should have received a copy of the GNU Lesser General Public
58 # License along with this library; if not, write to the Free Software
59 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
60 #
61
62 #
63 # Files to generate
64 #
65 my $spec_file = "opengl32.spec";
66 my $norm_file = "opengl_norm.c";
67 my $ext_file  = "opengl_ext.c";
68
69 # Set to 0 for removing the ENTER / LEAVE GL calls
70 my $gen_thread_safe = 0;
71 # Prefix used for the local variables
72 my $ext_prefix = "func_";
73 # If set to 1, generate TRACEs for each OpenGL function
74 my $gen_traces = 1;
75
76 #
77 # List of categories to put in the 'opengl_norm.c' file
78 #
79 my %cat_1_0 = ( "display-list" => 1,
80              "drawing" => 1,
81              "drawing-control" => 1,
82              "feedback" => 1,
83              "framebuf" => 1,
84              "misc" => 1,
85              "modeling" => 1,
86              "pixel-op" => 1,
87              "pixel-rw" => 1,
88              "state-req" => 1,
89              "xform" => 1,
90              "VERSION_1_0" => 1,
91              "VERSION_1_0_DEPRECATED" => 1 );
92 my %cat_1_1 = ( %cat_1_0,
93              "VERSION_1_1" => 1,
94              "VERSION_1_1_DEPRECATED" => 1 );
95 my %cat_1_2 = ( %cat_1_1,
96              "VERSION_1_2" => 1,
97              "VERSION_1_2_DEPRECATED" => 1 );
98 my %cat_1_3 = ( %cat_1_2,
99              "VERSION_1_3" => 1,
100              "VERSION_1_3_DEPRECATED" => 1 );
101 my %cat_1_4 = ( %cat_1_3,
102              "VERSION_1_4" => 1,
103              "VERSION_1_4_DEPRECATED" => 1 );
104 my %cat_1_5 = ( %cat_1_4,
105              "VERSION_1_5" => 1,
106              "VERSION_1_5_DEPRECATED" => 1 );
107
108 my %norm_categories = ();
109
110 #
111 # This hash table gives the conversion between OpenGL types and what
112 # is used by the TRACE printfs
113 #
114 my %debug_conv =
115     ("GLbitfield" => "%d",
116      "GLboolean" => "%d",
117      "GLbyte" => "%d",
118      "GLclampd" => "%f",
119      "GLclampf" => "%f",
120      "GLdouble" => "%f",
121      "GLenum" => "%d",
122      "GLfloat" => "%f",
123      "GLint" => "%d",
124      "GLshort" => "%d",
125      "GLsizei" => "%d",
126      "GLstring" => "%s",
127      "GLubyte" => "%d",
128      "GLuint" => "%d",
129      "GLushort" => "%d",
130      "GLhalfNV" => "%d",
131      "GLintptrARB" => "%ld",
132      "GLsizeiptrARB" => "%ld",
133      "GLintptr" => "%ld",
134      "GLsizeiptr" => "%ld",
135      "GLhandleARB" => "%d",
136      "GLcharARB" => "%c",
137      "GLvoid" => "(void)",
138      "_GLfuncptr" => "%p",
139      "GLDEBUGPROC" => "%p",
140      "GLDEBUGPROCARB" => "%p",
141      "GLDEBUGPROCAMD" => "%p",
142      "GLvdpauSurfaceNV" => "%ld",
143      "int" => "%d",
144      "unsigned int" => "%u",
145      "UINT" => "%u",
146      "DWORD" => "%u",
147      "BOOL" => "%u",
148      "INT64" => "%s,wine_dbgstr_longlong(%s)",
149      "UINT64" => "%s,wine_dbgstr_longlong(%s)",
150      "LPVOID" => "%p",
151      "HANDLE" => "%p",
152      "HDC" => "%p",
153      "HGLRC" => "%p",
154      "HPBUFFERARB" => "%p",
155      "HPBUFFEREXT" => "%p",
156     );
157
158 #
159 # This hash table gives the conversion between OpenGL types and what
160 # is used in the .spec file
161 #
162 my %arg_conv =
163     ("GLbitfield" => [ "long", 4 ],
164      "GLboolean" => [ "long", 4 ],
165      "GLbyte" => [ "long", 4 ],
166      "GLclampd" => [ "double", 8 ],
167      "GLclampf" => [ "float", 4 ],
168      "GLdouble" => [ "double", 8 ],
169      "GLenum" => [ "long", 4 ],
170      "GLfloat" => [ "float", 4 ],
171      "GLint" => [ "long", 4 ],
172      "GLshort" => [ "long", 4 ],
173      "GLsizei" => [ "long", 4 ],
174      "GLstring" => [ "str", 4 ],
175      "GLubyte" => [ "long", 4 ],
176      "GLuint" => [ "long", 4 ],
177      "GLushort" => [ "long", 4 ],
178      "GLhalfNV" => [ "long", 4 ],
179      "GLintptrARB" => [ "long", 4 ],
180      "GLsizeiptrARB" => [ "long", 4 ],
181      "GLhandleARB" => [ "long", 4 ],
182      "GLcharARB" => [ "long", 4 ],
183      "GLintptr" => [ "long", 4 ],
184      "GLsizeiptr" => [ "long", 4 ],
185      "_GLfuncptr" => [ "ptr", 4 ]);
186
187 #
188 # Used to convert some types
189 #
190 sub ConvertType($)
191 {
192     my ($type) = @_;
193
194     my %hash = ( "GLstring" => "const GLubyte *",
195               "GLintptrARB" => "INT_PTR",
196               "GLsizeiptrARB" => "INT_PTR",
197               "GLintptr" => "INT_PTR",
198               "GLsizeiptr" => "INT_PTR",
199               "GLhandleARB" => "unsigned int",
200               "GLcharARB" => "char",
201               "GLchar" => "char",
202               "GLhalfNV" => "unsigned short",
203               "GLvdpauSurfaceNV" => "INT_PTR",
204               "struct _cl_context" => "void",
205               "struct _cl_event" => "void",
206               "HGLRC" => "struct wgl_context *",
207               "GLDEBUGPROC" => "void *",
208               "GLDEBUGPROCARB" => "void *",
209               "GLDEBUGPROCAMD" => "void *",
210               "HPBUFFERARB" => "struct wgl_pbuffer *",
211               "HPBUFFEREXT" => "struct wgl_pbuffer *",
212         );
213
214     foreach my $org (reverse sort keys %hash) {
215         if ($type =~ /$org/) {
216             my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
217             return "$before$hash{$org}$after";
218         }
219     }
220     return $type;
221 }
222
223 #
224 # Used to convert some variable names
225 #
226 sub ConvertVarName($)
227 {
228     my ($type) = @_;
229
230     my %hash = ( "near" => "nearParam",
231                  "far"  => "farParam" );
232
233     foreach my $org (keys %hash) {
234         if ($type =~ /$org/) {
235             my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
236             return "$before$hash{$org}$after";
237         }
238     }
239     return $type;
240 }
241
242 #
243 # This functions generates the thunk for a given function.
244 #
245 sub GenerateThunk($$$$)
246 {
247     my ($name, $func_ref, $comment, $prefix) = @_;
248     my $ret = "";
249     my $call_arg = "";
250     my $trace_call_arg = "";
251     my $trace_arg = "";
252
253     return "" if $name eq "glDebugEntry";
254     return "" if $name eq "glGetString";
255     return "" if $func_ref->[2] && $func_ref->[2] =~ /WGL_/;
256
257     # If for opengl_norm.c, generate a nice heading otherwise Patrik won't be happy :-)
258     # Patrik says: Well I would be even happier if a (OPENGL32.@) was added as well. Done. :-)
259     if ($comment eq 1) {
260         $ret = "$ret/***********************************************************************\n";
261         $ret = "$ret *              $name (OPENGL32.\@)\n";
262         $ret = "$ret */\n";
263     }
264     $ret = $ret . ConvertType($func_ref->[0]) . " WINAPI wine_$name( ";
265     for (my $i = 0; $i < @{$func_ref->[1]}; $i++) {
266         ## Quick debug code :-)
267         ## print $func_ref->[1]->[$i]->[1] . "\n";
268         my $type = $func_ref->[1]->[$i]->[0];
269         my $name = ConvertVarName($func_ref->[1]->[$i]->[1]);
270         $ret .= ConvertType($type) . " $name";
271         $call_arg .= $name;
272         if ($type =~ /\*/) {
273             $trace_arg .= "%p";
274             $trace_call_arg .= $name;
275         } elsif (defined $debug_conv{$type}) {
276             if ($debug_conv{$type} =~ /(.*),(.*)/)
277             {
278                 $trace_arg .= $1;
279                 $trace_call_arg .= sprintf $2, $name;
280             }
281             else
282             {
283                 $trace_arg .= $debug_conv{$type};
284                 $trace_call_arg .= $name;
285             }
286         }
287         else { printf "Unknown type %s\n", $type; }
288         if ($i+1 < @{$func_ref->[1]}) {
289             $ret .= ", ";
290             $call_arg .= ", ";
291             $trace_call_arg .= ", ";
292             $trace_arg .= ", ";
293         } else {
294             $ret .= " ";
295             $call_arg .= " ";
296             $trace_call_arg .= " ";
297         }
298     }
299     $ret .= 'void ' if (!@{$func_ref->[1]});
300     $ret .= ") {\n";
301     $ret .= "  const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;\n";
302     if ($func_ref->[0] ne "void" && $gen_thread_safe) {
303         $ret = "$ret  " . ConvertType($func_ref->[0]) . " ret_value;\n";
304     }
305     if ($gen_traces) {
306         $ret = "$ret  TRACE(\"($trace_arg)\\n\"";
307         if ($trace_arg ne "") {
308             $ret .= ", $trace_call_arg";
309         }
310         $ret = "$ret);\n";
311     }
312     if ($gen_thread_safe) {
313         $ret .= "  ENTER_GL();\n";
314         $ret .= "  ";
315         if ($func_ref->[0] ne "void") {
316             $ret .= "ret_value = ";
317         }
318         $ret .= "funcs->$prefix.p_$name( $call_arg);\n";
319         $ret .= "  LEAVE_GL();\n";
320         if ($func_ref->[0] ne "void") {
321             $ret .= "  return ret_value;\n"
322         }
323     }
324     else {
325         $ret .= "  ";
326         if ($func_ref->[0] ne "void") {
327             $ret .= "return ";
328         }
329         $ret .= "funcs->$prefix.p_$name( $call_arg);\n";
330     }
331     $ret = "$ret}\n";
332
333     # Return this string....
334     return $ret;
335 }
336
337 sub generate_null_func($$)
338 {
339     my ($name, $func_ref) = @_;
340     my $ret;
341
342     return "" if $name eq "glDebugEntry";
343
344     $ret = "static " . ConvertType($func_ref->[0]) . " null_$name( ";
345     for (my $i = 0; $i < @{$func_ref->[1]}; $i++) {
346         my $type = $func_ref->[1]->[$i]->[0];
347         my $name = ConvertVarName($func_ref->[1]->[$i]->[1]);
348         $ret .= ConvertType($type) . " $name";
349         $ret .= "," if ($i+1 < @{$func_ref->[1]});
350         $ret .= " ";
351     }
352     $ret .= 'void ' if (!@{$func_ref->[1]});
353     $ret .= ") {";
354     if ($name eq "glGetError")
355     {
356         $ret .= " return GL_INVALID_OPERATION;";
357     }
358     elsif ($func_ref->[0] ne "void")
359     {
360         $ret .= " return 0;";
361     }
362     $ret .= " }\n";
363     return $ret;
364 }
365
366 sub get_func_proto($$)
367 {
368     my ($name, $func) = @_;
369     my $ret = sprintf "%-10s", ConvertType($func->[0]);
370     $ret .= " (WINE_GLAPI *p_$name)(";
371     for (my $i = 0; $i < @{$func->[1]}; $i++)
372     {
373         $ret .= ConvertType($func->[1]->[$i]->[0]);
374         $ret .= "," if ($i+1 < @{$func->[1]});
375     }
376     $ret .= "void" unless @{$func->[1]};
377     $ret .= ")";
378     return $ret;
379 }
380
381 sub get_func_link_name($$)
382 {
383     my ($name, $func) = @_;
384     return $name if ($func->[2] =~ /^WGL_/);
385     return "wine_$name";
386 }
387
388 #
389 # Extract and checks the number of arguments
390 #
391 if (@ARGV > 1) {
392     my $name0=$0;
393     $name0=~s%^.*/%%;
394     die "Usage: $name0 [version]\n";
395 }
396 my $version = $ARGV[0] || "1.1";
397 if ($version eq "1.0") {
398     %norm_categories = %cat_1_0;
399 } elsif ($version eq "1.1") {
400     %norm_categories = %cat_1_1;
401 } elsif ($version eq "1.2") {
402     %norm_categories = %cat_1_2;
403 } elsif ($version eq "1.3") {
404     %norm_categories = %cat_1_3;
405 } elsif ($version eq "1.4") {
406     %norm_categories = %cat_1_4;
407 } elsif ($version eq "1.5") {
408     %norm_categories = %cat_1_5;
409 } else {
410     die "Incorrect OpenGL version.\n";
411 }
412
413 #
414 # Fetch the registry files
415 #
416 -f "gl.spec" || system "wget http://www.opengl.org/registry/api/gl.spec" || die "cannot download gl.spec";
417 -f "gl.tm" || system "wget http://www.opengl.org/registry/api/gl.tm" || die "cannot download gl.tm";
418 -f "wgl.tm" || system "wget http://www.opengl.org/registry/api/wgl.tm" || die "cannot download wgl.tm";
419 -f "wglext.spec" || system "wget http://www.opengl.org/registry/api/wglext.spec" || die "cannot download wglext.spec";
420
421 #
422 # First, create a mapping between the pseudo types used in the spec file
423 # and OpenGL types using the 'gl.tm' file.
424 #
425 my %pseudo_to_opengl = ();
426
427 sub load_types($)
428 {
429     my $file = shift;
430     open TYPES, "<$file" or die "Could not open $file";
431     while (my $line = <TYPES>) {
432         if ($line !~ /\w*\#/) {
433             my ($pseudo, $opengl) = ($line =~ /(\w*),\*,\*,\s*(.*),\*,\*/);
434             $pseudo_to_opengl{$pseudo} = $opengl;
435         }
436     }
437     close TYPES;
438 }
439
440 load_types( "wgl.tm" );
441 load_types( "gl.tm" );
442
443 # This is to override the 'void' -> '*' bogus conversion
444 $pseudo_to_opengl{"void"} = "void";
445 $pseudo_to_opengl{"sync"} = "GLvoid*";
446 $pseudo_to_opengl{"Int64"} = "INT64";
447 $pseudo_to_opengl{"UInt64"} = "UINT64";
448 $pseudo_to_opengl{"Int64EXT"} = "INT64";
449 $pseudo_to_opengl{"UInt64EXT"} = "UINT64";
450
451 #
452 # Then, create the list of all OpenGL functions using the 'gl.spec'
453 # file. This will create two hash-tables, one with all the function
454 # whose category matches the one listed in '@norm_categories', the other
455 # with all other functions.
456 #
457 # An element of the hash table is a reference to an array with these
458 # elements :
459 #
460 #  - function name
461 #
462 #  - return type
463 #
464 #  - reference to an array giving the list of arguments (an empty array
465 #    for a 'void' function).
466 #
467 # The list of arguments is itself an array of reference to arrays. Each
468 # of these arrays represents the argument type and the argument name.
469 #
470 # An example :
471 #
472 # void glBitmap( GLsizei width, GLsizei height,
473 #                GLfloat xorig, GLfloat yorig,
474 #                GLfloat xmove, GLfloat ymove,
475 #                const GLubyte *bitmap );
476 #
477 # Would give something like that :
478 #
479 # [ "glBitmap",
480 #   "void",
481 #   [ [ "GLsizei", "width" ],
482 #     [ "GLsizei", "height" ],
483 #     [ "GLfloat", "xorig" ],
484 #     [ "GLfloat", "yorig" ],
485 #     [ "GLfloat", "xmove" ],
486 #     [ "GLfloat", "ymove" ],
487 #     [ "GLubyte *", "bitmap"] ] ];
488 #
489 my %norm_functions = ( "glDebugEntry" => [ "GLint", [[ "GLint", "unknown1" ],
490                                                      [ "GLint", "unknown2" ]] ] );
491
492 #
493 # This stores various extensions NOT part of the GL extension registry but still
494 # implemented by most OpenGL libraries out there...
495 #
496
497 my %ext_functions  =
498    (
499     "glDeleteBufferRegion" => [ "void", [ [ "GLenum", "region" ] ], "GL_KTX_buffer_region" ],
500     "glReadBufferRegion" => [ "void", [ [ "GLenum", "region" ],
501                                         [ "GLint", "x" ],
502                                         [ "GLint", "y" ],
503                                         [ "GLsizei", "width" ],
504                                         [ "GLsizei", "height" ] ], "GL_KTX_buffer_region" ],
505     "glDrawBufferRegion" => [ "void", [ [ "GLenum", "region" ],
506                                         [ "GLint", "x" ],
507                                         [ "GLint", "y" ],
508                                         [ "GLsizei", "width" ],
509                                         [ "GLsizei", "height" ],
510                                         [ "GLint", "xDest" ],
511                                         [ "GLint", "yDest" ] ], "GL_KTX_buffer_region" ],
512     "glBufferRegionEnabled" => [ "GLuint", [ ], "GL_KTX_buffer_region" ],
513     "glNewBufferRegion" => [ "GLuint", [ [ "GLenum", "type" ] ], "GL_KTX_buffer_region" ],
514     "glMTexCoord2fSGIS" => [ "void", [ [ "GLenum", "target" ],
515                                        [ "GLfloat", "s" ],
516                                        [ "GLfloat", "t" ] ], "GL_SGIS_multitexture" ],
517     "glMTexCoord2fvSGIS" => [ "void", [ [ "GLenum", "target" ],
518                                         [ "GLfloat *", "v" ] ], "GL_SGIS_multitexture" ],
519     "glMultiTexCoord1dSGIS" => [ "void", [ [ "GLenum", "target" ],
520                                            [ "GLdouble", "s" ] ],  "GL_SGIS_multitexture" ],
521     "glMultiTexCoord1dvSGIS" => [ "void", [ [ "GLenum", "target" ],
522                                             [ "GLdouble *", "v" ] ], "GL_SGIS_multitexture" ],
523     "glMultiTexCoord1fSGIS" => [ "void", [ [ "GLenum", "target" ],
524                                            [ "GLfloat", "s" ] ], "GL_SGIS_multitexture" ],
525     "glMultiTexCoord1fvSGIS" => [ "void", [ [ "GLenum", "target" ],
526                                             [ "const GLfloat *", "v" ] ], "GL_SGIS_multitexture" ],
527     "glMultiTexCoord1iSGIS" => [ "void", [ [ "GLenum", "target" ],
528                                            [ "GLint", "s" ] ], "GL_SGIS_multitexture" ],
529     "glMultiTexCoord1ivSGIS" => [ "void", [ [ "GLenum", "target" ],
530                                             [ "GLint *", "v" ] ], "GL_SGIS_multitexture" ],
531     "glMultiTexCoord1sSGIS" => [ "void", [ [ "GLenum", "target" ],
532                                            [ "GLshort", "s" ] ], "GL_SGIS_multitexture" ],
533     "glMultiTexCoord1svSGIS" => [ "void", [ [ "GLenum", "target" ],
534                                             [ "GLshort *", "v" ] ], "GL_SGIS_multitexture" ],
535     "glMultiTexCoord2dSGIS" => [ "void", [ [ "GLenum", "target" ],
536                                            [ "GLdouble", "s"],
537                                            [ "GLdouble", "t" ] ], "GL_SGIS_multitexture" ],
538     "glMultiTexCoord2dvSGIS" => [ "void", [ [ "GLenum", "target" ],
539                                             [ "GLdouble *", "v" ] ], "GL_SGIS_multitexture" ],
540     "glMultiTexCoord2fSGIS" => [ "void", [ [ "GLenum", "target" ],
541                                            [ "GLfloat", "s" ],
542                                            [ "GLfloat", "t" ] ], "GL_SGIS_multitexture" ],
543     "glMultiTexCoord2fvSGIS" => [ "void", [ [ "GLenum", "target" ],
544                                             [ "GLfloat *", "v" ] ], "GL_SGIS_multitexture" ],
545     "glMultiTexCoord2iSGIS" => [ "void", [ [ "GLenum", "target" ],
546                                            [ "GLint", "s" ],
547                                            [ "GLint", "t" ] ], "GL_SGIS_multitexture" ],
548     "glMultiTexCoord2ivSGIS" => [ "void", [ [ "GLenum", "target" ],
549                                             [ "GLint *", "v" ] ], "GL_SGIS_multitexture" ],
550     "glMultiTexCoord2sSGIS" => [ "void", [ [ "GLenum", "target" ],
551                                            [ "GLshort", "s" ],
552                                            [ "GLshort", "t" ] ], "GL_SGIS_multitexture" ],
553     "glMultiTexCoord2svSGIS" => [ "void", [ [ "GLenum", "target" ],
554                                             [ "GLshort *", "v" ] ], "GL_SGIS_multitexture" ],
555     "glMultiTexCoord3dSGIS" => [ "void", [ [ "GLenum", "target" ],
556                                            [ "GLdouble", "s" ],
557                                            [ "GLdouble", "t" ],
558                                            [ "GLdouble", "r" ] ], "GL_SGIS_multitexture" ],
559     "glMultiTexCoord3dvSGIS" => [ "void", [ [ "GLenum", "target" ],
560                                             [ "GLdouble *", "v" ] ], "GL_SGIS_multitexture" ],
561     "glMultiTexCoord3fSGIS" => [ "void", [ [ "GLenum", "target" ],
562                                            [ "GLfloat", "s" ],
563                                            [ "GLfloat", "t" ],
564                                            [ "GLfloat", "r" ] ], "GL_SGIS_multitexture" ],
565     "glMultiTexCoord3fvSGIS" => [ "void", [ [ "GLenum", "target" ],
566                                             [ "GLfloat *", "v" ] ], "GL_SGIS_multitexture" ],
567     "glMultiTexCoord3iSGIS" => [ "void", [ [ "GLenum", "target" ],
568                                            [ "GLint", "s" ],
569                                            [ "GLint", "t" ],
570                                            [ "GLint", "r" ] ], "GL_SGIS_multitexture" ],
571     "glMultiTexCoord3ivSGIS" => [ "void", [ [ "GLenum", "target" ],
572                                             [ "GLint *", "v" ] ], "GL_SGIS_multitexture" ],
573     "glMultiTexCoord3sSGIS" => [ "void", [ [ "GLenum", "target" ],
574                                            [ "GLshort", "s" ],
575                                            [ "GLshort", "t" ],
576                                            [ "GLshort", "r" ] ], "GL_SGIS_multitexture" ],
577     "glMultiTexCoord3svSGIS" => [ "void", [ [ "GLenum", "target" ],
578                                             [ "GLshort *", "v" ] ], "GL_SGIS_multitexture" ],
579     "glMultiTexCoord4dSGIS" => [ "void", [ [ "GLenum", "target" ],
580                                            [ "GLdouble", "s" ],
581                                            [ "GLdouble", "t" ],
582                                            [ "GLdouble", "r" ],
583                                            [ "GLdouble", "q" ] ], "GL_SGIS_multitexture" ],
584     "glMultiTexCoord4dvSGIS" => [ "void", [ [ "GLenum", "target" ],
585                                             [ "GLdouble *", "v" ] ], "GL_SGIS_multitexture" ],
586     "glMultiTexCoord4fSGIS" => [ "void", [ [ "GLenum", "target" ],
587                                            [ "GLfloat", "s" ],
588                                            [ "GLfloat", "t" ],
589                                            [ "GLfloat", "r" ],
590                                            [ "GLfloat", "q" ] ], "GL_SGIS_multitexture" ],
591     "glMultiTexCoord4fvSGIS" => [ "void", [ [ "GLenum", "target" ],
592                                             [ "GLfloat *", "v" ] ], "GL_SGIS_multitexture" ],
593     "glMultiTexCoord4iSGIS" => [ "void", [ [ "GLenum", "target" ],
594                                            [ "GLint", "s" ],
595                                            [ "GLint", "t" ],
596                                            [ "GLint", "r" ],
597                                            [ "GLint", "q" ] ], "GL_SGIS_multitexture" ],
598     "glMultiTexCoord4ivSGIS" => [ "void", [ [ "GLenum", "target" ],
599                                             [ "GLint *", "v" ] ], "GL_SGIS_multitexture" ],
600     "glMultiTexCoord4sSGIS" => [ "void", [ [ "GLenum", "target" ],
601                                            [ "GLshort", "s" ],
602                                            [ "GLshort", "t" ],
603                                            [ "GLshort", "r" ],
604                                            [ "GLshort", "q" ] ], "GL_SGIS_multitexture" ],
605     "glMultiTexCoord4svSGIS" => [ "void", [ [ "GLenum", "target" ],
606                                             [ "GLshort *", "v" ] ], "GL_SGIS_multitexture" ],
607     "glMultiTexCoordPointerSGIS" => [ "void", [ [ "GLenum", "target" ],
608                                                 [ "GLint", "size" ],
609                                                 [ "GLenum", "type" ],
610                                                 [ "GLsizei", "stride" ],
611                                                 [ "GLvoid *", "pointer" ] ], "GL_SGIS_multitexture" ],
612     "glSelectTextureSGIS" => [ "void", [ [ "GLenum", "target" ] ], "GL_SGIS_multitexture" ],
613     "glSelectTextureCoordSetSGIS" => [ "void", [ [ "GLenum", "target" ] ], "GL_SGIS_multitexture" ],
614     "glDeleteObjectBufferATI" => [ "void", [ [ "GLuint", "buffer" ] ], "GL_ATI_vertex_array_object" ],
615     "wglSetPixelFormatWINE" => [ "BOOL", [ [ "HDC", "hdc" ],
616                                            [ "int", "format" ] ], "WGL_WINE_pixel_format_passthrough" ],
617    );
618
619
620 my %wgl_functions =
621    (
622     "wglCopyContext" => [ "BOOL", [ [ "struct wgl_context *", "src" ],
623                                     [ "struct wgl_context *", "dst" ],
624                                     [ "UINT", "mask" ] ] ],
625     "wglCreateContext" => [ "struct wgl_context *", [ [ "HDC", "hdc" ] ] ],
626     "wglDeleteContext" => [ "void", [ [ "struct wgl_context *", "context" ] ] ],
627     "wglDescribePixelFormat" => [ "INT", [ [ "HDC", "hdc" ],
628                                            [ "INT", "format" ],
629                                            [ "UINT", "size" ],
630                                            [ "PIXELFORMATDESCRIPTOR *", "descr" ] ] ],
631     "wglGetPixelFormat" => [ "INT", [ [ "HDC", "hdc" ] ] ],
632     "wglGetProcAddress" => [ "PROC", [ [ "LPCSTR", "name" ] ] ],
633     "wglMakeCurrent" => [ "BOOL", [ [ "HDC", "hdc" ],
634                                     [ "struct wgl_context *", "context" ] ] ],
635     "wglSetPixelFormat" => [ "BOOL", [ [ "HDC", "hdc" ],
636                                        [ "INT", "format" ],
637                                        [ "const PIXELFORMATDESCRIPTOR *", "descr" ] ] ],
638     "wglShareLists" => [ "BOOL", [ [ "struct wgl_context *", "org" ],
639                                    [ "struct wgl_context *", "dst" ] ] ],
640     "wglSwapBuffers" => [ "BOOL", [ [ "HDC", "hdc" ] ] ],
641    );
642
643 my %supported_wgl_extensions =
644    (
645     "WGL_ARB_create_context" => 1,
646     "WGL_ARB_extensions_string" => 1,
647     "WGL_ARB_make_current_read" => 1,
648     "WGL_ARB_pbuffer" => 1,
649     "WGL_ARB_pixel_format" => 1,
650     "WGL_ARB_render_texture" => 1,
651     "WGL_EXT_extensions_string" => 1,
652     "WGL_EXT_swap_control" => 1,
653     "WGL_NV_vertex_array_range" => 1,
654     "WGL_WINE_pixel_format_passthrough" => 1,
655    );
656
657 sub parse_registry_file($)
658 {
659     my $file = shift;
660     my @arg_names;
661     my %arg_types;
662     open REGISTRY, "<$file" or die "cannot open $file";
663     while (my $line = <REGISTRY>) {
664         next unless ($line =~ /^\w*\(.*\)/);
665
666         # Get the function name (NOTE: the 'gl' prefix needs to be added later)
667         my ($funcname, $args) = ($line =~ /^(\w*)\((.*)\)/);
668         # and the argument names
669         @arg_names = split /\s*,\s*/, $args;
670
671         # After get :
672         #  - the return type
673         #  - category (the extension the function is part of)
674         #  - the argument types
675         #  - the category the function belongs
676         %arg_types = ();
677         my $category = "";
678         my $ret_type = "";
679         while (1) {
680             $line = <REGISTRY>;
681             unless (defined($line)) {
682                 last;
683             } elsif ($line =~ /^\s*$/) {
684                 if (($category eq "") || ($ret_type eq "")) {
685                     die "Missing 'category' line in function $funcname.\n";
686                 }
687                 last;
688             } elsif ($line =~ /\t*return\t+(\w*)/) {
689                 ($ret_type) = ($line =~ /\t*return\s*(\w*)/);
690                 $ret_type = $pseudo_to_opengl{$ret_type};
691                 unless (defined($ret_type)) {
692                     die "Unsupported return type in function $funcname\n";
693                 }
694             } elsif ($line =~ /^\t*category/) {
695                 ($category) = ($line =~ /^\t*category\s*([\w-]*)/);
696             } elsif ($line =~ /^\t*param/) {
697                 my ($name, $base_type, $dir, $ext) = ($line =~ /\t*param\s*(\w*)\s*(\w*) (in|out)\s+(.*)/);
698                 my $ptr = 0;
699                 unless (defined($name)) {
700                     chomp $line;
701                     die "Broken spec file line $line in function $funcname\n";
702                 }
703
704                 if ($ext =~ /array/) {
705                     # This is a pointer
706                     $ptr = 1;
707                 } elsif ($ext =~ /reference/) {
708                     # This is a pointer
709                     $ptr = 1;
710                 } elsif ($ext =~ /value/) {
711                     # And this a 'normal' value
712                     $ptr = 0;
713                 } else {
714                     chomp $line;
715                     die "Unsupported type : $line in function $funcname\n";
716                 }
717                 # Get the 'real' type and append a '*' in case of a pointer
718                 my $type = $pseudo_to_opengl{$base_type};
719                 unless (defined($type)) {
720                     chomp $line;
721                     die "Unsupported return type in function $funcname for type $base_type (line $line)\n";
722                 }
723                 if ($ptr) {
724                     $type .= "*";
725                     $type = "const $type" if $dir eq "in";
726                 }
727
728                 $arg_types{$name} = $type;
729             }
730         }
731
732         # Now, build the argument reference
733         my $arg_ref = [ ];
734         for (my $i = 0; $i < @arg_names; $i++) {
735             unless (defined($arg_types{$arg_names[$i]})) {
736                 print "@arg_names\n";
737                 foreach (sort keys %arg_types) {
738                     print "$_ => $arg_types{$_}\n";
739                 }
740                 die "Undefined type for $arg_names[$i] in function $funcname\n";
741             }
742
743             push @$arg_ref, [ $arg_types{$arg_names[$i]}, $arg_names[$i] ];
744         }
745
746         # Now, put in one or the other hash table
747         if ($norm_categories{$category}) {
748             $norm_functions{"gl$funcname"} = [ $ret_type, $arg_ref ];
749         } elsif ($file =~ /^wgl/) {
750             if (defined $supported_wgl_extensions{"WGL_$category"}) {
751                 $ext_functions{"wgl$funcname"} = [ $ret_type, $arg_ref, "WGL_$category" ];
752             }
753         } else {
754             $ext_functions{"gl$funcname"} = [ $ret_type, $arg_ref, "GL_$category" ];
755         }
756     }
757     close REGISTRY;
758 }
759
760 parse_registry_file( "gl.spec" );
761 parse_registry_file( "wglext.spec" );
762
763 #
764 # Get the current wgl_driver.h version
765 #
766 my $wgl_version = 0;
767 open HEADER, "<../../include/wine/wgl_driver.h" or die "cannot open wgl_driver.h";
768 while (<HEADER>)
769 {
770     next unless /^#define WINE_WGL_DRIVER_VERSION (\d+)/;
771     $wgl_version = $1;
772     last;
773 }
774 close HEADER;
775
776 #
777 # Generate the wgl_driver.h file
778 #
779 open HEADER, ">../../include/wine/wgl_driver.h" or die "cannot create wgl_driver.h";
780 print HEADER "/* Automatically generated from http://www.opengl.org/registry/api files; DO NOT EDIT! */\n\n";
781 print HEADER "#ifndef __WINE_WGL_DRIVER_H\n";
782 print HEADER "#define __WINE_WGL_DRIVER_H\n\n";
783 print HEADER "#ifndef WINE_GLAPI\n";
784 print HEADER "#define WINE_GLAPI\n";
785 print HEADER "#endif\n\n";
786
787 printf HEADER "#define WINE_WGL_DRIVER_VERSION %u\n\n", $wgl_version + 1;
788
789 print HEADER "struct wgl_context;\n";
790 print HEADER "struct wgl_pbuffer;\n\n";
791
792 print HEADER "struct opengl_funcs\n{\n";
793 print HEADER "    struct\n    {\n";
794 foreach (sort keys %wgl_functions)
795 {
796     printf HEADER "        %s;\n", get_func_proto($_, $wgl_functions{$_});
797 }
798 print HEADER "    } wgl;\n\n";
799
800 print HEADER "    struct\n    {\n";
801 foreach (sort keys %norm_functions)
802 {
803     next if $_ eq "glDebugEntry";
804     printf HEADER "        %s;\n", get_func_proto($_, $norm_functions{$_});
805 }
806 print HEADER "    } gl;\n\n";
807
808 print HEADER "    struct\n    {\n";
809 foreach (sort keys %ext_functions)
810 {
811     printf HEADER "        %s;\n", get_func_proto($_, $ext_functions{$_});
812 }
813 print HEADER "    } ext;\n";
814 print HEADER "};\n\n";
815
816 print HEADER "#define ALL_WGL_FUNCS";
817 foreach (sort keys %norm_functions)
818 {
819     next if $_ eq "glDebugEntry";
820     printf HEADER " \\\n    USE_GL_FUNC(\%s)", $_;
821 }
822 print HEADER "\n\n";
823
824 print HEADER "extern struct opengl_funcs * CDECL __wine_get_wgl_driver( HDC hdc, UINT version );\n\n";
825 print HEADER "#endif /* __WINE_WGL_DRIVER_H */\n";
826 close HEADER;
827
828 #
829 # Now, generate the output files. First, the spec file.
830 #
831 open(SPEC, ">$spec_file");
832
833 foreach (sort keys %norm_functions) {
834     print SPEC "@  stdcall $_( ";
835     for (my $i = 0; $i < @{$norm_functions{$_}->[1]}; $i++) {
836         my $type = $norm_functions{$_}->[1]->[$i]->[0];
837         if ($type =~ /\*/) {
838             print SPEC "ptr ";
839         } elsif (defined($arg_conv{$type})) {
840             print SPEC "$@$arg_conv{$type}[0] ";
841         } else {
842             die "No conversion for GL type $type...\n";
843         }
844     }
845     print SPEC ") wine_$_\n";
846 }
847
848 print SPEC "@  stdcall wglChoosePixelFormat(long ptr)
849 @  stdcall wglCopyContext(long long long)
850 @  stdcall wglCreateContext(long)
851 @  stdcall wglCreateLayerContext(long long)
852 @  stdcall wglDeleteContext(long)
853 @  stdcall wglDescribeLayerPlane(long long long long ptr)
854 @  stdcall wglDescribePixelFormat(long long long ptr)
855 @  stdcall wglGetCurrentContext()
856 @  stdcall wglGetCurrentDC()
857 @  stub    wglGetDefaultProcAddress
858 @  stdcall wglGetLayerPaletteEntries(long long long long ptr)
859 @  stdcall wglGetPixelFormat(long)
860 @  stdcall wglGetProcAddress(str)
861 @  stdcall wglMakeCurrent(long long)
862 @  stdcall wglRealizeLayerPalette(long long long)
863 @  stdcall wglSetLayerPaletteEntries(long long long long ptr)
864 @  stdcall wglSetPixelFormat(long long ptr)
865 @  stdcall wglShareLists(long long)
866 @  stdcall wglSwapBuffers(long)
867 @  stdcall wglSwapLayerBuffers(long long)
868 @  stdcall wglUseFontBitmapsA(long long long long)
869 @  stdcall wglUseFontBitmapsW(long long long long)
870 @  stdcall wglUseFontOutlinesA(long long long long long long long ptr)
871 @  stdcall wglUseFontOutlinesW(long long long long long long long ptr)
872 ";
873
874 close(SPEC);
875
876 #
877 # After the spec file, the opengl_norm.c file
878 #
879 open(NORM, ">$norm_file");
880 print NORM "
881 /* Auto-generated file... Do not edit ! */
882
883 #include \"config.h\"
884 #include <stdarg.h>
885 #include \"opengl_ext.h\"
886 #include \"winternl.h\"
887 #include \"wingdi.h\"
888 #include \"wine/wgl_driver.h\"
889 #include \"wine/debug.h\"
890
891 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
892 ";
893
894 foreach (sort keys %norm_functions) {
895     my $string = GenerateThunk($_, $norm_functions{$_}, 1, "gl");
896     print NORM "\n$string" if $string;
897 }
898
899 foreach (sort keys %wgl_functions) {
900     print NORM generate_null_func($_, $wgl_functions{$_});
901 }
902 foreach (sort keys %norm_functions) {
903     print NORM generate_null_func($_, $norm_functions{$_});
904 }
905 foreach (sort keys %ext_functions) {
906     print NORM generate_null_func($_, $ext_functions{$_});
907 }
908
909 print NORM "\nstruct opengl_funcs null_opengl_funcs =\n{\n    {\n";
910 foreach (sort keys %wgl_functions) { print NORM "        null_$_,\n"; }
911 print NORM "    },\n    {\n";
912 foreach (sort keys %norm_functions) { print NORM "        null_$_,\n" unless $_ eq "glDebugEntry"; }
913 print NORM "    },\n    {\n";
914 foreach (sort keys %ext_functions) { print NORM "        null_$_,\n"; }
915 print NORM "    }\n};\n";
916
917 close(NORM);
918
919 #
920 # Finally, more complex, the opengl_ext.c file
921 #
922 open(EXT, ">$ext_file");
923 print EXT "
924 /* Auto-generated file... Do not edit ! */
925
926 #include \"config.h\"
927 #include <stdarg.h>
928 #include \"opengl_ext.h\"
929 #include \"winternl.h\"
930 #include \"wingdi.h\"
931 #define WGL_WGLEXT_PROTOTYPES
932 #include \"wine/wglext.h\"
933 #include \"wine/wgl_driver.h\"
934 #include \"wine/debug.h\"
935
936 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
937
938 ";
939
940 # The thunks themselves....
941 my $count = keys %ext_functions;
942 print EXT "const int extension_registry_size = $count;\n";
943 foreach (sort keys %ext_functions) {
944     my $string = GenerateThunk($_, $ext_functions{$_}, 0, "ext");
945     print EXT "\nstatic $string" if $string;
946 }
947
948 # Then the table giving the string <-> function correspondence */
949 print EXT "\nconst OpenGL_extension extension_registry[$count] = {\n";
950 my $i = 0;
951 foreach (sort keys %ext_functions) {
952     my $func_ref = $ext_functions{$_};
953     my $link_name = get_func_link_name( $_, $func_ref );
954     print EXT "  { \"$_\", \"$func_ref->[2]\", $link_name }";
955     if ($i != $count-1) {
956         print EXT ",";
957     }
958     $i++;
959     print EXT "\n";
960 }
961 print EXT "};\n";
962
963 close(EXT);