gdi32: Add a stub for CancelDC.
[wine] / dlls / opengl32 / make_opengl
1 #!/usr/bin/perl -w
2 use strict;
3
4 # This script is called thus :
5 #
6 #   make_opengl path_to_spec_file opengl_version
7 #
8 #     - path_to_spec_file is the path to the directory where the OpenGL
9 #       spec files are located. These files are part of the OpenGL
10 #       sample implementation CVS tree and are located in
11 #       CVS_ROOT/projects/ogl-sample/main/doc/registry/specs.
12 #       You can find them on the web at the following URL :
13 #         http://oss.sgi.com/cgi-bin/cvsweb.cgi/projects/ogl-sample/main/doc/registry/specs/
14 #       You will need gl.tm and gl.spec.
15 #
16 #     - opengl_version is the OpenGL version emulated by the library
17 #       (can be 1.0 to 1.5).
18 #
19 # This script generates the three following files :
20 #
21 #     - opengl32.spec : the spec file giving all the exported functions
22 #       of the OpenGL32.DLL library. These functions are the one an
23 #       application can directly link to (and are all the functions
24 #       defined in the OpenGL core for the version defined by
25 #       'opengl_version').
26 #
27 #     - opengl_norm.c : this file contains the thunks for all OpenGL
28 #       functions that are defined in 'opengl32.spec'. The corresponding
29 #       functions NEED to be defined in Linux's libGL or the library
30 #       won't be able to be linked in.
31 #
32 #     - opengl_ext.c : in this file are stored thunks for ALL possible
33 #       OpenGL extensions (at least, all the extensions that are defined
34 #       in the OpenGL extension registry). Contrary to 'opengl_norm.c',
35 #       you do not need to have these extensions in your libGL to have
36 #       OpenGL work (as they are resolved at run-time using
37 #       glXGetProcAddressARB).
38 #
39 # Copyright 2000 Lionel Ulmer
40 #
41 # This library is free software; you can redistribute it and/or
42 # modify it under the terms of the GNU Lesser General Public
43 # License as published by the Free Software Foundation; either
44 # version 2.1 of the License, or (at your option) any later version.
45 #
46 # This library is distributed in the hope that it will be useful,
47 # but WITHOUT ANY WARRANTY; without even the implied warranty of
48 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
49 # Lesser General Public License for more details.
50 #
51 # You should have received a copy of the GNU Lesser General Public
52 # License along with this library; if not, write to the Free Software
53 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
54 #
55
56 #
57 # Files to generate
58 #
59 my $spec_file = "opengl32.spec";
60 my $norm_file = "opengl_norm.c";
61 my $ext_file  = "opengl_ext.c";
62
63 # Set to 0 for removing the ENTER / LEAVE GL calls
64 my $gen_thread_safe = 1;
65 # Prefix used for the local variables
66 my $ext_prefix = "func_";
67 # If set to 1, generate TRACEs for each OpenGL function
68 my $gen_traces = 1;
69
70 #
71 # List of categories to put in the 'opengl_norm.c' file
72 #
73 my %cat_1_0 = ( "display-list" => 1,
74              "drawing" => 1,
75              "drawing-control" => 1,
76              "feedback" => 1,
77              "framebuf" => 1,
78              "misc" => 1,
79              "modeling" => 1,
80              "pixel-op" => 1,
81              "pixel-rw" => 1,
82              "state-req" => 1,
83              "xform" => 1 );
84 my %cat_1_1 = ( %cat_1_0,
85              "1_1" => 1 );
86 my %cat_1_2 = ( %cat_1_1,
87              "VERSION_1_2" => 1 );
88 my %cat_1_3 = ( %cat_1_2,
89              "VERSION_1_3" => 1 );
90 my %cat_1_4 = ( %cat_1_3,
91              "VERSION_1_4" => 1 );
92 my %cat_1_5 = ( %cat_1_4,
93              "VERSION_1_5" => 1 );
94
95 my %norm_categories = ();
96
97 #
98 # This hash table gives the conversion between OpenGL types and what
99 # is used by the TRACE printfs
100 #
101 my %debug_conv =
102     ("GLbitfield" => "%d",
103      "GLboolean" => "%d",
104      "GLbyte" => "%d",
105      "GLclampd" => "%f",
106      "GLclampf" => "%f",
107      "GLdouble" => "%f",
108      "GLenum" => "%d",
109      "GLfloat" => "%f",
110      "GLint" => "%d",
111      "GLshort" => "%d",
112      "GLsizei" => "%d",
113      "GLstring" => "%s",
114      "GLubyte" => "%d",
115      "GLuint" => "%d",
116      "GLushort" => "%d",
117      "GLhalfNV" => "%d",
118      "GLintptrARB" => "%d",
119      "GLsizeiptrARB" => "%d",
120      "GLintptr" => "%d",
121      "GLsizeiptr" => "%d",
122      "GLhandleARB" => "%d",
123      "GLcharARB" => "%c",
124      "GLvoid" => "(void)",
125      "_GLfuncptr" => "%p");
126
127 #
128 # This hash table gives the conversion between OpenGL types and what
129 # is used in the .spec file
130 #
131 my %arg_conv =
132     ("GLbitfield" => [ "long", 4 ],
133      "GLboolean" => [ "long", 4 ],
134      "GLbyte" => [ "long", 4 ],
135      "GLclampd" => [ "double", 8 ],
136      "GLclampf" => [ "long", 4 ],
137      "GLdouble" => [ "double", 8 ],
138      "GLenum" => [ "long", 4 ],
139      "GLfloat" => [ "long", 4 ],
140      "GLint" => [ "long", 4 ],
141      "GLshort" => [ "long", 4 ],
142      "GLsizei" => [ "long", 4 ],
143      "GLstring" => [ "str", 4 ],
144      "GLubyte" => [ "long", 4 ],
145      "GLuint" => [ "long", 4 ],
146      "GLushort" => [ "long", 4 ],
147      "GLhalfNV" => [ "long", 4 ],
148      "GLintptrARB" => [ "long", 4 ],
149      "GLsizeiptrARB" => [ "long", 4 ],
150      "GLhandleARB" => [ "long", 4 ],
151      "GLcharARB" => [ "long", 4 ],
152      "GLintptr" => [ "long", 4 ],
153      "GLsizeiptr" => [ "long", 4 ],
154      "GLvoid" => [ "void", 4 ],
155      "_GLfuncptr" => [ "ptr", 4 ]);
156
157 #
158 # Used to convert some types
159 #
160 sub ConvertType($)
161 {
162     my ($type) = @_;
163
164     my %hash = ( "GLstring" => "const GLubyte *",
165               "GLintptrARB" => "ptrdiff_t",
166               "GLsizeiptrARB" => "ptrdiff_t",
167               "GLintptr" => "ptrdiff_t",
168               "GLsizeiptr" => "ptrdiff_t",
169               "GLhandleARB" => "unsigned int",
170               "GLcharARB" => "char",
171               "GLchar" => "char",
172               "GLhalfNV" => "unsigned short" );
173
174     foreach my $org (reverse sort keys %hash) {
175         if ($type =~ /$org/) {
176             my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
177             return "$before$hash{$org}$after";
178         }
179     }
180     return $type;
181 }
182
183 #
184 # Used to convert some variable names
185 #
186 sub ConvertVarName($)
187 {
188     my ($type) = @_;
189
190     my %hash = ( "near" => "nearParam",
191                  "far"  => "farParam" );
192
193     foreach my $org (keys %hash) {
194         if ($type =~ /$org/) {
195             my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
196             return "$before$hash{$org}$after";
197         }
198     }
199     return $type;
200 }
201
202 #
203 # This functions generates the thunk for a given function.
204 #
205 sub GenerateThunk($$$$$)
206 {
207     my ($func_ref, $comment, $prefix, $thread_safe, $local_var) = @_;
208     my $ret = "";
209     my $call_arg = "";
210     my $trace_arg = "";
211
212     return "" if $func_ref->[0] eq "glGetString";
213     return "" if $func_ref->[0] eq "glGetIntegerv";
214     return "" if $func_ref->[0] eq "glEnable";
215     return "" if $func_ref->[0] eq "glIsEnabled";
216     return "" if $func_ref->[0] eq "glDisable";
217     return "" if $func_ref->[0] eq "glScissor";
218     return "" if $func_ref->[0] eq "glViewport";
219
220     # If for opengl_norm.c, generate a nice heading otherwise Patrik won't be happy :-)
221     # Patrik says: Well I would be even happier if a (OPENGL32.@) was added as well. Done. :-)
222     if ($comment eq 1) {
223         $ret = "$ret/***********************************************************************\n";
224         $ret = "$ret *              $func_ref->[0] (OPENGL32.\@)\n";
225         $ret = "$ret */\n";
226     }
227     $ret = $ret . ConvertType($func_ref->[1]) . " WINAPI wine_$func_ref->[0]( ";
228     for (my $i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
229         ## Quick debug code :-)
230         ## print $func_ref->[2]->[$i]->[1] . "\n";
231         my $type = $func_ref->[2]->[$i]->[0];
232         my $name = ConvertVarName($func_ref->[2]->[$i]->[1]);
233         $ret = $ret . ConvertType($type) . " $name";
234         $call_arg = "$call_arg$name";
235         if ($type =~ /\*/) {
236             $trace_arg = "$trace_arg\%p";
237         } else {
238             $trace_arg = "$trace_arg$debug_conv{$type}";
239         }
240         if ($i != $#{@{$func_ref->[2]}}) {
241             $ret = "$ret, ";
242             $call_arg = "$call_arg, ";
243             $trace_arg = "$trace_arg, ";
244         } else {
245             $ret = "$ret ";
246             $call_arg = "$call_arg ";
247         }
248     }
249     $ret .= 'void ' if ($#{@{$func_ref->[2]}} < 0);
250     $ret = "$ret) {\n";
251     if ($func_ref->[1] ne "void") {
252         $ret = "$ret  " . ConvertType($func_ref->[1]) . " ret_value;\n";
253     }
254     $ret .= $local_var;
255     if ($gen_traces) {
256         $ret = "$ret  TRACE(\"($trace_arg)\\n\"";
257         if ($trace_arg ne "") {
258             $ret = "$ret, $call_arg";
259         }
260         $ret = "$ret);\n";
261     }
262     if ($thread_safe) {
263         $ret = "$ret  ENTER_GL();\n";
264     }
265     $ret = "$ret  ";
266     if ($func_ref->[1] ne "void") {
267         $ret = $ret . "ret_value = ";
268     }
269     $ret = "$ret$prefix$func_ref->[0]( $call_arg);\n";
270     if ($thread_safe) {
271         $ret = "$ret  LEAVE_GL();\n";
272     }
273     if ($func_ref->[1] ne "void") {
274         $ret = "$ret  return ret_value;\n"
275     }
276     $ret = "$ret}\n";
277
278     # Return this string....
279     return $ret;
280 }
281
282 #
283 # Extract and checks the number of arguments
284 #
285 if (@ARGV != 2) {
286     my $name0=$0;
287     $name0=~s%^.*/%%;
288     die "Usage: $name0 OpenGL_registry_location OpenGL_version\n";
289 }
290 my $registry_path = shift @ARGV;
291 my $version       = shift @ARGV;
292 if ($version eq "1.0") {
293     %norm_categories = %cat_1_0;
294 } elsif ($version eq "1.1") {
295     %norm_categories = %cat_1_1;
296 } elsif ($version eq "1.2") {
297     %norm_categories = %cat_1_2;
298 } elsif ($version eq "1.3") {
299     %norm_categories = %cat_1_3;
300 } elsif ($version eq "1.4") {
301     %norm_categories = %cat_1_4;
302 } elsif ($version eq "1.5") {
303     %norm_categories = %cat_1_5;
304 } else {
305     die "Incorrect OpenGL version.\n";
306 }
307
308 #
309 # Open the registry files
310 #
311 open(TYPES,    "$registry_path/gl.tm")   || die "Could not open 'gl.tm'. Please check your path the the registry files.\n";
312 open(REGISTRY, "$registry_path/gl.spec") || die "Could not open 'gl.spec'. Please check your path the the registry files.\n";
313
314 #
315 # First, create a mapping between the pseudo types used in the spec file
316 # and OpenGL types using the 'gl.tm' file.
317 #
318 my %pseudo_to_opengl = ();
319 while (my $line = <TYPES>) {
320     if ($line !~ /\w*\#/) {
321         my ($pseudo, $opengl) = ($line =~ /(\w*),\*,\*,\s*(.*),\*,\*/);
322         $pseudo_to_opengl{$pseudo} = $opengl;
323     }
324 }
325 # This is to override the 'void' -> '*' bogus conversion
326 $pseudo_to_opengl{"void"} = "void";
327 $pseudo_to_opengl{"Int64EXT"} = "INT64";
328 $pseudo_to_opengl{"UInt64EXT"} = "UINT64";
329 $pseudo_to_opengl{"BufferAccessARB"} = "GLenum";
330 $pseudo_to_opengl{"BufferOffsetARB"} = "GLintptr";
331 $pseudo_to_opengl{"BufferOffset"} = "GLintptr";
332 $pseudo_to_opengl{"BufferPNameARB"} = "GLenum";
333 $pseudo_to_opengl{"BufferPointerNameARB"} = "GLenum";
334 $pseudo_to_opengl{"BufferSizeARB"} = "GLsizeiptr";
335 $pseudo_to_opengl{"BufferSize"} = "GLsizeiptr";
336 $pseudo_to_opengl{"BufferTargetARB"} = "GLenum";
337 $pseudo_to_opengl{"BufferUsageARB"} = "GLenum";
338 $pseudo_to_opengl{"FramebufferAttachment"} = "GLenum";
339 $pseudo_to_opengl{"FramebufferTarget"} = "GLenum";
340 $pseudo_to_opengl{"ProgramParameterPName"} = "GLenum";
341 $pseudo_to_opengl{"ProgramTarget"} = "GLenum";
342 $pseudo_to_opengl{"RenderbufferTarget"} = "GLenum";
343 $pseudo_to_opengl{"VertexAttribEnum"} = "GLenum";
344 # This is a bug in the spec file...
345 $pseudo_to_opengl{"FfdTargetSGIX"} = "GLint";
346 $pseudo_to_opengl{"FfdMaskSGIX"} = "GLint";
347 $pseudo_to_opengl{"IglooFunctionSelectSGIX"} = "GLint";
348 $pseudo_to_opengl{"IglooParameterSGIX"} = "GLint";
349
350 #
351 # Then, create the list of all OpenGL functions using the 'gl.spec'
352 # file. This will create two hash-tables, one with all the function
353 # whose category matches the one listed in '@norm_categories', the other
354 # with all other functions.
355 #
356 # An element of the hash table is a reference to an array with these
357 # elements :
358 #
359 #  - function name
360 #
361 #  - return type
362 #
363 #  - reference to an array giving the list of arguments (an empty array
364 #    for a 'void' function).
365 #
366 # The list of arguments is itself an array of reference to arrays. Each
367 # of these arrays represents the argument type and the argument name.
368 #
369 # An example :
370 #
371 # void glBitmap( GLsizei width, GLsizei height,
372 #                GLfloat xorig, GLfloat yorig,
373 #                GLfloat xmove, GLfloat ymove,
374 #                const GLubyte *bitmap );
375 #
376 # Would give something like that :
377 #
378 # [ "glBitmap",
379 #   "void",
380 #   [ [ "GLsizei", "width" ],
381 #     [ "GLsizei", "height" ],
382 #     [ "GLfloat", "xorig" ],
383 #     [ "GLfloat", "yorig" ],
384 #     [ "GLfloat", "xmove" ],
385 #     [ "GLfloat", "ymove" ],
386 #     [ "GLubyte *", "bitmap"] ] ];
387 #
388 my %norm_functions = ();
389
390 #
391 # This stores various extensions NOT part of the GL extension registry but still
392 # implemented by most OpenGL libraries out there...
393 #
394
395 my %ext_functions  =
396     ( "glDeleteBufferRegion" => [ "glDeleteBufferRegion", "void", [ [ "GLenum", "region" ] ], "glDeleteBufferRegion", "GL_KTX_buffer_region" ],
397       "glReadBufferRegion" => [ "glReadBufferRegion", "void", [ [ "GLenum", "region" ],
398                                                                 [ "GLint", "x" ],
399                                                                 [ "GLint", "y" ],
400                                                                 [ "GLsizei", "width" ],
401                                                                 [ "GLsizei", "height" ] ], "glReadBufferRegion", "GL_KTX_buffer_region" ],
402       "glDrawBufferRegion" => [ "glDrawBufferRegion", "void", [ [ "GLenum", "region" ],
403                                                                 [ "GLint", "x" ],
404                                                                 [ "GLint", "y" ],
405                                                                 [ "GLsizei", "width" ],
406                                                                 [ "GLsizei", "height" ],
407                                                                 [ "GLint", "xDest" ],
408                                                                 [ "GLint", "yDest" ] ], "glDrawBufferRegion", "GL_KTX_buffer_region" ],
409       "glBufferRegionEnabled" => [ "glBufferRegionEnabled", "GLuint", [ ], "glBufferRegionEnabled",  "GL_KTX_buffer_region" ],
410       "glNewBufferRegion" => [ "glNewBufferRegion", "GLuint", [ [ "GLenum", "type" ] ], "glNewBufferRegion", "GL_KTX_buffer_region" ],
411       "glMTexCoord2fSGIS" => [ "glMTexCoord2fSGIS", "void", [ [ "GLenum", "target" ],
412                                                               [ "GLfloat", "s" ],
413                                                               [ "GLfloat", "t" ] ], "glMTexCoord2fSGIS", "GL_SGIS_multitexture" ],
414       "glMTexCoord2fvSGIS" => [ "glMTexCoord2fvSGIS", "void", [ [ "GLenum", "target" ],
415                                                                 [ "GLfloat *", "v" ] ], "glMTexCoord2fvSGIS", "GL_SGIS_multitexture" ],
416       "glMultiTexCoord1dSGIS" => [ "glMultiTexCoord1dSGIS", "void", [ [ "GLenum", "target" ],
417                                                                       [ "GLdouble", "s" ] ],  "glMultiTexCoord1dSGIS", "GL_SGIS_multitexture" ],
418       "glMultiTexCoord1dvSGIS" => [ "glMultiTexCoord1dvSGIS", "void", [ [ "GLenum", "target" ],
419                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord1dvSGIS", "GL_SGIS_multitexture" ],
420       "glMultiTexCoord1fSGIS" => [ "glMultiTexCoord1fSGIS", "void", [ [ "GLenum", "target" ],
421                                                                       [ "GLfloat", "s" ] ], "glMultiTexCoord1fSGIS", "GL_SGIS_multitexture" ],
422       "glMultiTexCoord1fvSGIS" => [ "glMultiTexCoord1fvSGIS", "void", [ [ "GLenum", "target" ],
423                                                                         [ "const GLfloat *", "v" ] ], "glMultiTexCoord1fvSGIS", "GL_SGIS_multitexture" ],
424       "glMultiTexCoord1iSGIS" => [ "glMultiTexCoord1iSGIS", "void", [ [ "GLenum", "target" ],
425                                                                       [ "GLint", "s" ] ], "glMultiTexCoord1iSGIS", "GL_SGIS_multitexture" ],
426       "glMultiTexCoord1ivSGIS" => [ "glMultiTexCoord1ivSGIS", "void", [ [ "GLenum", "target" ],
427                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord1ivSGIS", "GL_SGIS_multitexture" ],
428       "glMultiTexCoord1sSGIS" => [ "glMultiTexCoord1sSGIS", "void", [ [ "GLenum", "target" ],
429                                                                       [ "GLshort", "s" ] ], "glMultiTexCoord1sSGIS", "GL_SGIS_multitexture" ],
430       "glMultiTexCoord1svSGIS" => [ "glMultiTexCoord1svSGIS", "void", [ [ "GLenum", "target" ],
431                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord1svSGIS", "GL_SGIS_multitexture" ],
432       "glMultiTexCoord2dSGIS" => [ "glMultiTexCoord2dSGIS", "void", [ [ "GLenum", "target" ],
433                                                                       [ "GLdouble", "s"],
434                                                                       [ "GLdouble", "t" ] ], "glMultiTexCoord2dSGIS", "GL_SGIS_multitexture" ],
435       "glMultiTexCoord2dvSGIS" => [ "glMultiTexCoord2dvSGIS", "void", [ [ "GLenum", "target" ],
436                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord2dvSGIS", "GL_SGIS_multitexture" ],
437       "glMultiTexCoord2fSGIS" => [ "glMultiTexCoord2fSGIS", "void", [ [ "GLenum", "target" ],
438                                                                       [ "GLfloat", "s" ],
439                                                                       [ "GLfloat", "t" ] ], "glMultiTexCoord2fSGIS", "GL_SGIS_multitexture" ],
440       "glMultiTexCoord2fvSGIS" => [ "glMultiTexCoord2fvSGIS", "void", [ [ "GLenum", "target" ],
441                                                                         [ "GLfloat *", "v" ] ], "glMultiTexCoord2fvSGIS", "GL_SGIS_multitexture" ],
442       "glMultiTexCoord2iSGIS" => [ "glMultiTexCoord2iSGIS", "void", [ [ "GLenum", "target" ],
443                                                                       [ "GLint", "s" ],
444                                                                       [ "GLint", "t" ] ], "glMultiTexCoord2iSGIS", "GL_SGIS_multitexture" ],
445       "glMultiTexCoord2ivSGIS" => [ "glMultiTexCoord2ivSGIS", "void", [ [ "GLenum", "target" ],
446                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord2ivSGIS", "GL_SGIS_multitexture" ],
447       "glMultiTexCoord2sSGIS" => [ "glMultiTexCoord2sSGIS", "void", [ [ "GLenum", "target" ],
448                                                                       [ "GLshort", "s" ],
449                                                                       [ "GLshort", "t" ] ], "glMultiTexCoord2sSGIS", "GL_SGIS_multitexture" ],
450       "glMultiTexCoord2svSGIS" => [ "glMultiTexCoord2svSGIS", "void", [ [ "GLenum", "target" ],
451                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord2svSGIS", "GL_SGIS_multitexture" ],
452       "glMultiTexCoord3dSGIS" => [ "glMultiTexCoord3dSGIS", "void", [ [ "GLenum", "target" ],
453                                                                       [ "GLdouble", "s" ],
454                                                                       [ "GLdouble", "t" ],
455                                                                       [ "GLdouble", "r" ] ], "glMultiTexCoord3dSGIS", "GL_SGIS_multitexture" ],
456       "glMultiTexCoord3dvSGIS" => [ "glMultiTexCoord3dvSGIS", "void", [ [ "GLenum", "target" ],
457                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord3dvSGIS", "GL_SGIS_multitexture" ],
458       "glMultiTexCoord3fSGIS" => [ "glMultiTexCoord3fSGIS", "void", [ [ "GLenum", "target" ],
459                                                                       [ "GLfloat", "s" ],
460                                                                       [ "GLfloat", "t" ],
461                                                                       [ "GLfloat", "r" ] ], "glMultiTexCoord3fSGIS", "GL_SGIS_multitexture" ],
462       "glMultiTexCoord3fvSGIS" => [ "glMultiTexCoord3fvSGIS", "void", [ [ "GLenum", "target" ],
463                                                                         [ "GLfloat *", "v" ] ], "glMultiTexCoord3fvSGIS", "GL_SGIS_multitexture" ],
464       "glMultiTexCoord3iSGIS" => [ "glMultiTexCoord3iSGIS", "void", [ [ "GLenum", "target" ],
465                                                                       [ "GLint", "s" ],
466                                                                       [ "GLint", "t" ],
467                                                                       [ "GLint", "r" ] ], "glMultiTexCoord3iSGIS", "GL_SGIS_multitexture" ],
468       "glMultiTexCoord3ivSGIS" => [ "glMultiTexCoord3ivSGIS", "void", [ [ "GLenum", "target" ],
469                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord3ivSGIS", "GL_SGIS_multitexture" ],
470       "glMultiTexCoord3sSGIS" => [ "glMultiTexCoord3sSGIS", "void", [ [ "GLenum", "target" ],
471                                                                       [ "GLshort", "s" ],
472                                                                       [ "GLshort", "t" ],
473                                                                       [ "GLshort", "r" ] ], "glMultiTexCoord3sSGIS", "GL_SGIS_multitexture" ],
474       "glMultiTexCoord3svSGIS" => [ "glMultiTexCoord3svSGIS", "void", [ [ "GLenum", "target" ],
475                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord3svSGIS", "GL_SGIS_multitexture" ],
476       "glMultiTexCoord4dSGIS" => [ "glMultiTexCoord4dSGIS", "void", [ [ "GLenum", "target" ],
477                                                                       [ "GLdouble", "s" ],
478                                                                       [ "GLdouble", "t" ],
479                                                                       [ "GLdouble", "r" ],
480                                                                       [ "GLdouble", "q" ] ], "glMultiTexCoord4dSGIS", "GL_SGIS_multitexture" ],
481       "glMultiTexCoord4dvSGIS" => [ "glMultiTexCoord4dvSGIS", "void", [ [ "GLenum", "target" ],
482                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord4dvSGIS", "GL_SGIS_multitexture" ],
483       "glMultiTexCoord4fSGIS" => [ "glMultiTexCoord4fSGIS", "void", [ [ "GLenum", "target" ],
484                                                                       [ "GLfloat", "s" ],
485                                                                       [ "GLfloat", "t" ],
486                                                                       [ "GLfloat", "r" ],
487                                                                       [ "GLfloat", "q" ] ], "glMultiTexCoord4fSGIS", "GL_SGIS_multitexture" ],
488       "glMultiTexCoord4fvSGIS" => [ "glMultiTexCoord4fvSGIS", "void", [ [ "GLenum", "target" ],
489                                                                         [ "GLfloat *", "v" ] ], "glMultiTexCoord4fvSGIS", "GL_SGIS_multitexture" ],
490       "glMultiTexCoord4iSGIS" => [ "glMultiTexCoord4iSGIS", "void", [ [ "GLenum", "target" ],
491                                                                       [ "GLint", "s" ],
492                                                                       [ "GLint", "t" ],
493                                                                       [ "GLint", "r" ],
494                                                                       [ "GLint", "q" ] ], "glMultiTexCoord4iSGIS", "GL_SGIS_multitexture" ],
495       "glMultiTexCoord4ivSGIS" => [ "glMultiTexCoord4ivSGIS", "void", [ [ "GLenum", "target" ],
496                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord4ivSGIS", "GL_SGIS_multitexture" ],
497       "glMultiTexCoord4sSGIS" => [ "glMultiTexCoord4sSGIS", "void", [ [ "GLenum", "target" ],
498                                                                       [ "GLshort", "s" ],
499                                                                       [ "GLshort", "t" ],
500                                                                       [ "GLshort", "r" ],
501                                                                       [ "GLshort", "q" ] ], "glMultiTexCoord4sSGIS", "GL_SGIS_multitexture" ],
502       "glMultiTexCoord4svSGIS" => [ "glMultiTexCoord4svSGIS", "void", [ [ "GLenum", "target" ],
503                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord4svSGIS", "GL_SGIS_multitexture" ],
504       "glMultiTexCoordPointerSGIS" => [ "glMultiTexCoordPointerSGIS", "void", [ [ "GLenum", "target" ],
505                                                                                 [ "GLint", "size" ],
506                                                                                 [ "GLenum", "type" ],
507                                                                                 [ "GLsizei", "stride" ],
508                                                                                 [ "GLvoid *", "pointer" ] ], "glMultiTexCoordPointerSGIS", "GL_SGIS_multitexture" ],
509       "glSelectTextureSGIS" => [ "glSelectTextureSGIS", "void", [ [ "GLenum", "target" ] ], "glSelectTextureSGIS", "GL_SGIS_multitexture" ],
510       "glSelectTextureCoordSetSGIS" => [ "glSelectTextureCoordSetSGIS", "void", [ [ "GLenum", "target" ] ], "glSelectTextureCoordSetSGIS", "GL_SGIS_multitexture" ],
511       "glDeleteObjectBufferATI" => [ "glDeleteObjectBufferATI", "void", [ [ "GLuint", "buffer" ] ], "glDeleteObjectBufferATI", "GL_ATI_vertex_array_object" ]
512       );
513
514 my @arg_names;
515 my %arg_types;
516 while (my $line = <REGISTRY>) {
517     if ($line =~ /^\w*\(.*\)/) {
518         # Get the function name (NOTE: the 'gl' prefix needs to be added later)
519         my ($funcname, $args) = ($line =~ /^(\w*)\((.*)\)/);
520         # and the argument names
521         @arg_names = split /\s*,\s*/, $args;
522
523         # After get :
524         #  - the return type
525         #  - category (the extension the function is part of)
526         #  - the argument types
527         #  - the category the function belongs
528         %arg_types = ();
529         my $category = "";
530         my $ret_type = "";
531         while (1) {
532             $line = <REGISTRY>;
533             unless (defined($line)) {
534                 last;
535             } elsif ($line =~ /^\s*$/) {
536                 if (($category eq "") || ($ret_type eq "")) {
537                     die "Missing 'category' line in function $funcname.\n";
538                 }
539                 last;
540             } elsif ($line =~ /\t*return\t*(\w*)/) {
541                 ($ret_type) = ($line =~ /\t*return\s*(\w*)/);
542                 $ret_type = $pseudo_to_opengl{$ret_type};
543                 unless (defined($ret_type)) {
544                     die "Unsupported return type in function $funcname\n";
545                 }
546             } elsif ($line =~ /^\t*category/) {
547                 ($category) = ($line =~ /^\t*category\s*([\w-]*)/);
548             } elsif ($line =~ /^\t*param/) {
549                 my ($name, $base_type, $ext) = ($line =~ /\t*param\s*(\w*)\s*(\w*) (.*)/);
550                 my $ptr = 0;
551                 unless (defined($name)) {
552                     chomp $line;
553                     die "Broken spec file line $line in function $funcname\n";
554                 }
555
556                 if ($ext =~ /array/) {
557                     # This is a pointer
558                     $ptr = 1;
559                 } elsif ($ext =~ /value/) {
560                     # And this a 'normal' value
561                     $ptr = 0;
562                 } else {
563                     chomp $line;
564                     die "Unsupported type : $line in function $funcname\n";
565                 }
566                 # Get the 'real' type and append a '*' in case of a pointer
567                 my $type = $pseudo_to_opengl{$base_type};
568                 unless (defined($type)) {
569                     chomp $line;
570                     die "Unsupported return type in function $funcname for type $base_type (line $line)\n";
571                 }
572                 if ($ptr) {
573                     $type = "$type*";
574                 }
575
576                 $arg_types{$name} = $type;
577             }
578         }
579
580         # Now, build the argument reference
581         my $arg_ref = [ ];
582         for (my $i = 0; $i <= $#arg_names; $i++) {
583             unless (defined($arg_types{$arg_names[$i]})) {
584                 print "@arg_names\n";
585                 foreach (sort keys %arg_types) {
586                     print "$_ => $arg_types{$_}\n";
587                 }
588                 die "Undefined type for $arg_names[$i] in function $funcname\n";
589             }
590
591             push @$arg_ref, [ $arg_types{$arg_names[$i]}, $arg_names[$i] ];
592         }
593         my $func_ref = [ "gl$funcname",
594                          $ret_type,
595                          $arg_ref,
596                          "gl$funcname",
597                          "GL_$category" ];
598
599         # Now, put in one or the other hash table
600         if ($norm_categories{$category}) {
601             $norm_functions{"gl$funcname"} = $func_ref;
602         } else {
603             $ext_functions{"gl$funcname"} = $func_ref;
604         }
605     }
606 }
607
608 #
609 # Clean up the input files
610 #
611 close(TYPES);
612 close(REGISTRY);
613
614 #
615 # Now, generate the output files. First, the spec file.
616 #
617 open(SPEC, ">$spec_file");
618
619 foreach (sort keys %norm_functions) {
620     my $func_name = $norm_functions{$_}->[0];
621     print SPEC "@  stdcall $func_name( ";
622     for (my $i = 0; $i <= $#{@{$norm_functions{$_}->[2]}}; $i++) {
623         my $type = $norm_functions{$_}->[2]->[$i]->[0];
624         if ($type =~ /\*/) {
625             print SPEC "ptr ";
626         } elsif (defined($arg_conv{$type})) {
627             print SPEC "$@$arg_conv{$type}[0] ";
628         } else {
629             die "No conversion for GL type $type...\n";
630         }
631     }
632     print SPEC ") wine_$func_name\n";
633 }
634
635 print SPEC "@  stub    glGetLevelParameterfv
636 @  stub    glGetLevelParameteriv
637 @  stdcall wglChoosePixelFormat(long ptr) gdi32.ChoosePixelFormat
638 @  stdcall wglCopyContext(long long long)
639 @  stdcall wglCreateContext(long) gdi32.wglCreateContext
640 @  stdcall wglCreateLayerContext(long long)
641 @  stdcall wglDeleteContext(long) gdi32.wglDeleteContext
642 @  stdcall wglDescribeLayerPlane(long long long long ptr)
643 @  stdcall wglDescribePixelFormat(long long long ptr) gdi32.DescribePixelFormat
644 @  stdcall wglGetCurrentContext() gdi32.wglGetCurrentContext
645 @  stdcall wglGetCurrentDC() gdi32.wglGetCurrentDC
646 @  stub    wglGetDefaultProcAddress
647 @  stdcall wglGetLayerPaletteEntries(long long long long ptr)
648 @  stdcall wglGetPixelFormat(long) gdi32.GetPixelFormat
649 @  stdcall wglGetProcAddress(str)
650 @  stdcall wglMakeCurrent(long long) gdi32.wglMakeCurrent
651 @  stdcall wglRealizeLayerPalette(long long long)
652 @  stdcall wglSetLayerPaletteEntries(long long long long ptr)
653 @  stdcall wglSetPixelFormat(long long ptr) gdi32.SetPixelFormat
654 @  stdcall wglShareLists(long long) gdi32.wglShareLists
655 @  stdcall wglSwapBuffers(long) gdi32.SwapBuffers
656 @  stdcall wglSwapLayerBuffers(long long)
657 @  stdcall wglUseFontBitmapsA(long long long long) gdi32.wglUseFontBitmapsA
658 @  stdcall wglUseFontBitmapsW(long long long long) gdi32.wglUseFontBitmapsW
659 @  stdcall wglUseFontOutlinesA(long long long long long long long ptr)
660 @  stdcall wglUseFontOutlinesW(long long long long long long long ptr)
661 ";
662
663 close(SPEC);
664
665 #
666 # After the spec file, the opengl_norm.c file
667 #
668 open(NORM, ">$norm_file");
669 print NORM "
670 /* Auto-generated file... Do not edit ! */
671
672 #include \"config.h\"
673 #include \"opengl_ext.h\"
674 #include \"wine/debug.h\"
675
676 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
677 ";
678 foreach (sort keys %norm_functions) {
679     my $string = GenerateThunk($norm_functions{$_}, 1, "", $gen_thread_safe, "");
680
681     print NORM "\n$string" if $string;
682 }
683 close(NORM);
684
685 #
686 # Finally, more complex, the opengl_ext.c file
687 #
688 open(EXT, ">$ext_file");
689 print EXT "
690 /* Auto-generated file... Do not edit ! */
691
692 #include \"config.h\"
693 #include \"opengl_ext.h\"
694 #include \"wine/debug.h\"
695
696 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
697
698 ";
699
700 # The thunks themselves....
701 my $count = keys %ext_functions;
702 print EXT "const int extension_registry_size = $count;\n";
703 print EXT "void *extension_funcs[$count];\n";
704 print EXT "\n/* The thunks themselves....*/";
705 my $pos = 0;
706 foreach (sort keys %ext_functions) {
707     my $func_ref = $ext_functions{$_};
708     my $local_var = "  " . ConvertType($func_ref->[1]) . " (*$ext_prefix$func_ref->[0])( ";
709     for (my $i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
710         my $type = ConvertType($func_ref->[2]->[$i]->[0]);
711         $local_var .= "$type";
712         if ($i != $#{@{$func_ref->[2]}}) {
713             $local_var .= ", ";
714         } else {
715             $local_var .= " ";
716         }
717     }
718     $local_var .= 'void ' if ($#{@{$func_ref->[2]}} < 0);
719     $local_var .= ") = extension_funcs[$pos];\n";
720     $pos++;
721     print EXT "\nstatic ", GenerateThunk($ext_functions{$_}, 0, $ext_prefix, $gen_thread_safe, $local_var);
722 }
723
724 # Then the table giving the string <-> function correspondance */
725 print EXT "\n\n/* The table giving the correspondance between names and functions */\n";
726 print EXT "const OpenGL_extension extension_registry[$count] = {\n";
727 my $i = 0;
728 foreach (sort keys %ext_functions) {
729     my $func_ref = $ext_functions{$_};
730     if ($func_ref->[0] eq $func_ref->[3])
731     {
732         print EXT "  { \"$func_ref->[0]\", \"$func_ref->[4]\", (void *) wine_$func_ref->[0] }";
733     }
734     if ($i != $count-1) {
735         print EXT ",";
736     }
737     $i++;
738     print EXT "\n";
739 }
740 print EXT "};\n";
741
742 close(EXT);