make_opengl 26.2 KB
Newer Older
1
#!/usr/bin/perl -w
2
use strict;
3 4 5 6 7 8

# This script is called thus :
#
#   make_opengl path_to_spec_file opengl_version
#
#     - path_to_spec_file is the path to the directory where the OpenGL
9 10 11 12 13 14 15
#       spec files are located. These files are hosted in the OpenGL
#       extension registry at opengl.org:
#       http://www.opengl.org/registry/api/gl.spec
#       http://www.opengl.org/registry/api/gl.tm
#
#       The files used to be hosted and maintained by SGI. You can still find
#       find them in the sample implementation CVS tree which is located at
16
#       CVS_ROOT/projects/ogl-sample/main/doc/registry/specs.
17
#       You can also find them on the web at the following URL :
18
#         http://oss.sgi.com/cgi-bin/cvsweb.cgi/projects/ogl-sample/main/doc/registry/specs/
19 20
#
#     - opengl_version is the OpenGL version emulated by the library
21
#       (can be 1.0 to 1.5).
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#
# 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).
#
43 44 45 46 47 48 49 50 51 52 53 54 55 56
# Copyright 2000 Lionel Ulmer
#
# 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
57
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
58
#
59

60 61 62
#
# Files to generate
#
63 64 65
my $spec_file = "opengl32.spec";
my $norm_file = "opengl_norm.c";
my $ext_file  = "opengl_ext.c";
66 67

# Set to 0 for removing the ENTER / LEAVE GL calls
68
my $gen_thread_safe = 1;
69
# Prefix used for the local variables
70
my $ext_prefix = "func_";
71
# If set to 1, generate TRACEs for each OpenGL function
72
my $gen_traces = 1;
73 74 75 76

#
# List of categories to put in the 'opengl_norm.c' file
#
77
my %cat_1_0 = ( "display-list" => 1,
78 79 80 81 82 83 84 85 86
	     "drawing" => 1,
	     "drawing-control" => 1,
	     "feedback" => 1,
	     "framebuf" => 1,
	     "misc" => 1,
	     "modeling" => 1,
	     "pixel-op" => 1,
	     "pixel-rw" => 1,
	     "state-req" => 1,
87
	     "xform" => 1 );
88
my %cat_1_1 = ( %cat_1_0,
89
	     "1_1" => 1 );
90
my %cat_1_2 = ( %cat_1_1,
91
	     "VERSION_1_2" => 1 );
92
my %cat_1_3 = ( %cat_1_2,
93
	     "VERSION_1_3" => 1 );
94
my %cat_1_4 = ( %cat_1_3,
95
	     "VERSION_1_4" => 1 );
96
my %cat_1_5 = ( %cat_1_4,
97
	     "VERSION_1_5" => 1 );
98

99
my %norm_categories = ();
100 101 102 103 104

#
# This hash table gives the conversion between OpenGL types and what
# is used by the TRACE printfs
#
105
my %debug_conv =
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    ("GLbitfield" => "%d",
     "GLboolean" => "%d",
     "GLbyte" => "%d",
     "GLclampd" => "%f",
     "GLclampf" => "%f",
     "GLdouble" => "%f",
     "GLenum" => "%d",
     "GLfloat" => "%f",
     "GLint" => "%d",
     "GLshort" => "%d",
     "GLsizei" => "%d",
     "GLstring" => "%s",
     "GLubyte" => "%d",
     "GLuint" => "%d",
     "GLushort" => "%d",
121 122 123
     "GLhalfNV" => "%d",
     "GLintptrARB" => "%d",
     "GLsizeiptrARB" => "%d",
124 125 126 127
     "GLintptr" => "%d",
     "GLsizeiptr" => "%d",
     "GLhandleARB" => "%d",
     "GLcharARB" => "%c",
128 129 130 131 132 133 134
     "GLvoid" => "(void)",
     "_GLfuncptr" => "%p");

#
# This hash table gives the conversion between OpenGL types and what
# is used in the .spec file
#
135
my %arg_conv =
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    ("GLbitfield" => [ "long", 4 ],
     "GLboolean" => [ "long", 4 ],
     "GLbyte" => [ "long", 4 ],
     "GLclampd" => [ "double", 8 ],
     "GLclampf" => [ "long", 4 ],
     "GLdouble" => [ "double", 8 ],
     "GLenum" => [ "long", 4 ],
     "GLfloat" => [ "long", 4 ],
     "GLint" => [ "long", 4 ],
     "GLshort" => [ "long", 4 ],
     "GLsizei" => [ "long", 4 ],
     "GLstring" => [ "str", 4 ],
     "GLubyte" => [ "long", 4 ],
     "GLuint" => [ "long", 4 ],
     "GLushort" => [ "long", 4 ],
151 152 153
     "GLhalfNV" => [ "long", 4 ],
     "GLintptrARB" => [ "long", 4 ],
     "GLsizeiptrARB" => [ "long", 4 ],
154 155 156 157
     "GLhandleARB" => [ "long", 4 ],
     "GLcharARB" => [ "long", 4 ],
     "GLintptr" => [ "long", 4 ],
     "GLsizeiptr" => [ "long", 4 ],
158 159 160
     "GLvoid" => [ "void", 4 ],
     "_GLfuncptr" => [ "ptr", 4 ]);

161 162 163
#
# Used to convert some types
#
164 165
sub ConvertType($)
{
166 167
    my ($type) = @_;

168
    my %hash = ( "GLstring" => "const GLubyte *",
169 170
	      "GLintptrARB" => "ptrdiff_t",
	      "GLsizeiptrARB" => "ptrdiff_t",
171 172 173 174
	      "GLintptr" => "ptrdiff_t",
	      "GLsizeiptr" => "ptrdiff_t",
	      "GLhandleARB" => "unsigned int",
	      "GLcharARB" => "char",
175
	      "GLchar" => "char",
176 177
	      "GLhalfNV" => "unsigned short" );

178
    foreach my $org (reverse sort keys %hash) {
179
	if ($type =~ /$org/) {
180
	    my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
181 182 183 184 185 186
	    return "$before$hash{$org}$after";
	}
    }
    return $type;
}

187 188 189
#
# Used to convert some variable names
#
190 191
sub ConvertVarName($)
{
192 193
    my ($type) = @_;

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

197
    foreach my $org (keys %hash) {
198
	if ($type =~ /$org/) {
199
	    my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
200 201 202 203 204 205
	    return "$before$hash{$org}$after";
	}
    }
    return $type;
}

206 207 208
#
# This functions generates the thunk for a given function.
#
209
sub GenerateThunk($$$$$)
210
{
211
    my ($func_ref, $comment, $prefix, $thread_safe, $local_var) = @_;
212 213 214
    my $ret = "";
    my $call_arg = "";
    my $trace_arg = "";
215 216 217

    return "" if $func_ref->[0] eq "glGetString";
    return "" if $func_ref->[0] eq "glGetIntegerv";
218 219
    return "" if $func_ref->[0] eq "glFinish";
    return "" if $func_ref->[0] eq "glFlush";
220 221

    # If for opengl_norm.c, generate a nice heading otherwise Patrik won't be happy :-)
222
    # Patrik says: Well I would be even happier if a (OPENGL32.@) was added as well. Done. :-)
223
    if ($comment eq 1) {
224 225 226
	$ret = "$ret/***********************************************************************\n";
	$ret = "$ret *              $func_ref->[0] (OPENGL32.\@)\n";
	$ret = "$ret */\n";
227
    }
228 229
    $ret = $ret . ConvertType($func_ref->[1]) . " WINAPI wine_$func_ref->[0]( ";
    for (my $i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
230 231
	## Quick debug code :-)
	## print $func_ref->[2]->[$i]->[1] . "\n";
232 233
	my $type = $func_ref->[2]->[$i]->[0];
	my $name = ConvertVarName($func_ref->[2]->[$i]->[1]);
234
	$ret = $ret . ConvertType($type) . " $name";
235
	$call_arg = "$call_arg$name";
236
	if ($type =~ /\*/) {
237
	    $trace_arg = "$trace_arg\%p";
238
	} else {
239
	    $trace_arg = "$trace_arg$debug_conv{$type}";
240
	}
241
	if ($i != $#{@{$func_ref->[2]}}) {
242 243 244
	    $ret = "$ret, ";
	    $call_arg = "$call_arg, ";
	    $trace_arg = "$trace_arg, ";
245
	} else {
246 247
	    $ret = "$ret ";
	    $call_arg = "$call_arg ";
248 249
	}
    }
250
    $ret .= 'void ' if ($#{@{$func_ref->[2]}} < 0);
251
    $ret = "$ret) {\n";
252
    if ($func_ref->[1] ne "void") {
253
	$ret = "$ret  " . ConvertType($func_ref->[1]) . " ret_value;\n";
254
    }
255
    $ret .= $local_var;
256
    if ($gen_traces) {
257
	$ret = "$ret  TRACE(\"($trace_arg)\\n\"";
258
	if ($trace_arg ne "") {
259
	    $ret = "$ret, $call_arg";
260
	}
261
	$ret = "$ret);\n";
262
    }
263
    if ($thread_safe) {
264
	$ret = "$ret  ENTER_GL();\n";
265
    }
266
    $ret = "$ret  ";
267 268 269
    if ($func_ref->[1] ne "void") {
	$ret = $ret . "ret_value = ";
    }
270
    $ret = "$ret$prefix$func_ref->[0]( $call_arg);\n";
271
    if ($thread_safe) {
272
	$ret = "$ret  LEAVE_GL();\n";
273 274
    }
    if ($func_ref->[1] ne "void") {
275
	$ret = "$ret  return ret_value;\n"
276
    }
277
    $ret = "$ret}\n";
278 279

    # Return this string....
280
    return $ret;
281 282 283 284 285
}

#
# Extract and checks the number of arguments
#
286 287 288 289
if (@ARGV != 2) {
    my $name0=$0;
    $name0=~s%^.*/%%;
    die "Usage: $name0 OpenGL_registry_location OpenGL_version\n";
290
}
291 292
my $registry_path = shift @ARGV;
my $version       = shift @ARGV;
293 294 295 296 297 298
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;
299 300 301 302 303 304
} 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;
305
} else {
306
    die "Incorrect OpenGL version.\n";
307 308 309 310 311
}

#
# Open the registry files
#
312 313
open(TYPES,    "$registry_path/gl.tm")   || die "Could not open 'gl.tm'. Please check your path in the registry files.\n";
open(REGISTRY, "$registry_path/gl.spec") || die "Could not open 'gl.spec'. Please check your path in the registry files.\n";
314 315 316 317 318

#
# First, create a mapping between the pseudo types used in the spec file
# and OpenGL types using the 'gl.tm' file.
#
319 320
my %pseudo_to_opengl = ();
while (my $line = <TYPES>) {
321
    if ($line !~ /\w*\#/) {
322
	my ($pseudo, $opengl) = ($line =~ /(\w*),\*,\*,\s*(.*),\*,\*/);
323 324
	$pseudo_to_opengl{$pseudo} = $opengl;
    }
325 326 327
}
# This is to override the 'void' -> '*' bogus conversion
$pseudo_to_opengl{"void"} = "void";
328 329
$pseudo_to_opengl{"Int64EXT"} = "INT64";
$pseudo_to_opengl{"UInt64EXT"} = "UINT64";
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

#
# Then, create the list of all OpenGL functions using the 'gl.spec'
# file. 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"] ] ];
#
369
my %norm_functions = ();
370 371 372 373 374

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

my %ext_functions  =
377
    ( "glDeleteBufferRegion" => [ "glDeleteBufferRegion", "void", [ [ "GLenum", "region" ] ], "glDeleteBufferRegion", "GL_KTX_buffer_region" ],
378 379 380 381
      "glReadBufferRegion" => [ "glReadBufferRegion", "void", [ [ "GLenum", "region" ],
								[ "GLint", "x" ],
								[ "GLint", "y" ],
								[ "GLsizei", "width" ],
382
								[ "GLsizei", "height" ] ], "glReadBufferRegion", "GL_KTX_buffer_region" ],
383 384 385 386 387 388
      "glDrawBufferRegion" => [ "glDrawBufferRegion", "void", [ [ "GLenum", "region" ],
								[ "GLint", "x" ],
								[ "GLint", "y" ],
								[ "GLsizei", "width" ],
								[ "GLsizei", "height" ],
								[ "GLint", "xDest" ],
389 390 391
								[ "GLint", "yDest" ] ], "glDrawBufferRegion", "GL_KTX_buffer_region" ],
      "glBufferRegionEnabled" => [ "glBufferRegionEnabled", "GLuint", [ ], "glBufferRegionEnabled",  "GL_KTX_buffer_region" ],
      "glNewBufferRegion" => [ "glNewBufferRegion", "GLuint", [ [ "GLenum", "type" ] ], "glNewBufferRegion", "GL_KTX_buffer_region" ],
392
      "glMTexCoord2fSGIS" => [ "glMTexCoord2fSGIS", "void", [ [ "GLenum", "target" ],
393
							      [ "GLfloat", "s" ],
394
							      [ "GLfloat", "t" ] ], "glMTexCoord2fSGIS", "GL_SGIS_multitexture" ],
395
      "glMTexCoord2fvSGIS" => [ "glMTexCoord2fvSGIS", "void", [ [ "GLenum", "target" ],
396
								[ "GLfloat *", "v" ] ], "glMTexCoord2fvSGIS", "GL_SGIS_multitexture" ],
397
      "glMultiTexCoord1dSGIS" => [ "glMultiTexCoord1dSGIS", "void", [ [ "GLenum", "target" ],
398
								      [ "GLdouble", "s" ] ],  "glMultiTexCoord1dSGIS", "GL_SGIS_multitexture" ],
399
      "glMultiTexCoord1dvSGIS" => [ "glMultiTexCoord1dvSGIS", "void", [ [ "GLenum", "target" ],
400
									[ "GLdouble *", "v" ] ], "glMultiTexCoord1dvSGIS", "GL_SGIS_multitexture" ],
401
      "glMultiTexCoord1fSGIS" => [ "glMultiTexCoord1fSGIS", "void", [ [ "GLenum", "target" ],
402
								      [ "GLfloat", "s" ] ], "glMultiTexCoord1fSGIS", "GL_SGIS_multitexture" ],
403
      "glMultiTexCoord1fvSGIS" => [ "glMultiTexCoord1fvSGIS", "void", [ [ "GLenum", "target" ],
404
									[ "const GLfloat *", "v" ] ], "glMultiTexCoord1fvSGIS", "GL_SGIS_multitexture" ],
405
      "glMultiTexCoord1iSGIS" => [ "glMultiTexCoord1iSGIS", "void", [ [ "GLenum", "target" ],
406
								      [ "GLint", "s" ] ], "glMultiTexCoord1iSGIS", "GL_SGIS_multitexture" ],
407
      "glMultiTexCoord1ivSGIS" => [ "glMultiTexCoord1ivSGIS", "void", [ [ "GLenum", "target" ],
408
									[ "GLint *", "v" ] ], "glMultiTexCoord1ivSGIS", "GL_SGIS_multitexture" ],
409
      "glMultiTexCoord1sSGIS" => [ "glMultiTexCoord1sSGIS", "void", [ [ "GLenum", "target" ],
410
								      [ "GLshort", "s" ] ], "glMultiTexCoord1sSGIS", "GL_SGIS_multitexture" ],
411
      "glMultiTexCoord1svSGIS" => [ "glMultiTexCoord1svSGIS", "void", [ [ "GLenum", "target" ],
412
									[ "GLshort *", "v" ] ], "glMultiTexCoord1svSGIS", "GL_SGIS_multitexture" ],
413 414
      "glMultiTexCoord2dSGIS" => [ "glMultiTexCoord2dSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLdouble", "s"],
415
								      [ "GLdouble", "t" ] ], "glMultiTexCoord2dSGIS", "GL_SGIS_multitexture" ],
416
      "glMultiTexCoord2dvSGIS" => [ "glMultiTexCoord2dvSGIS", "void", [ [ "GLenum", "target" ],
417
									[ "GLdouble *", "v" ] ], "glMultiTexCoord2dvSGIS", "GL_SGIS_multitexture" ],
418 419
      "glMultiTexCoord2fSGIS" => [ "glMultiTexCoord2fSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLfloat", "s" ],
420
								      [ "GLfloat", "t" ] ], "glMultiTexCoord2fSGIS", "GL_SGIS_multitexture" ],
421
      "glMultiTexCoord2fvSGIS" => [ "glMultiTexCoord2fvSGIS", "void", [ [ "GLenum", "target" ],
422
									[ "GLfloat *", "v" ] ], "glMultiTexCoord2fvSGIS", "GL_SGIS_multitexture" ],
423 424
      "glMultiTexCoord2iSGIS" => [ "glMultiTexCoord2iSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLint", "s" ],
425
								      [ "GLint", "t" ] ], "glMultiTexCoord2iSGIS", "GL_SGIS_multitexture" ],
426
      "glMultiTexCoord2ivSGIS" => [ "glMultiTexCoord2ivSGIS", "void", [ [ "GLenum", "target" ],
427
									[ "GLint *", "v" ] ], "glMultiTexCoord2ivSGIS", "GL_SGIS_multitexture" ],
428 429
      "glMultiTexCoord2sSGIS" => [ "glMultiTexCoord2sSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLshort", "s" ],
430
								      [ "GLshort", "t" ] ], "glMultiTexCoord2sSGIS", "GL_SGIS_multitexture" ],
431
      "glMultiTexCoord2svSGIS" => [ "glMultiTexCoord2svSGIS", "void", [ [ "GLenum", "target" ],
432
									[ "GLshort *", "v" ] ], "glMultiTexCoord2svSGIS", "GL_SGIS_multitexture" ],
433 434 435
      "glMultiTexCoord3dSGIS" => [ "glMultiTexCoord3dSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLdouble", "s" ],
								      [ "GLdouble", "t" ],
436
								      [ "GLdouble", "r" ] ], "glMultiTexCoord3dSGIS", "GL_SGIS_multitexture" ],
437
      "glMultiTexCoord3dvSGIS" => [ "glMultiTexCoord3dvSGIS", "void", [ [ "GLenum", "target" ],
438
									[ "GLdouble *", "v" ] ], "glMultiTexCoord3dvSGIS", "GL_SGIS_multitexture" ],
439 440 441
      "glMultiTexCoord3fSGIS" => [ "glMultiTexCoord3fSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLfloat", "s" ],
								      [ "GLfloat", "t" ],
442
								      [ "GLfloat", "r" ] ], "glMultiTexCoord3fSGIS", "GL_SGIS_multitexture" ],
443
      "glMultiTexCoord3fvSGIS" => [ "glMultiTexCoord3fvSGIS", "void", [ [ "GLenum", "target" ],
444
									[ "GLfloat *", "v" ] ], "glMultiTexCoord3fvSGIS", "GL_SGIS_multitexture" ],
445 446 447
      "glMultiTexCoord3iSGIS" => [ "glMultiTexCoord3iSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLint", "s" ],
								      [ "GLint", "t" ],
448
								      [ "GLint", "r" ] ], "glMultiTexCoord3iSGIS", "GL_SGIS_multitexture" ],
449
      "glMultiTexCoord3ivSGIS" => [ "glMultiTexCoord3ivSGIS", "void", [ [ "GLenum", "target" ],
450
									[ "GLint *", "v" ] ], "glMultiTexCoord3ivSGIS", "GL_SGIS_multitexture" ],
451 452 453
      "glMultiTexCoord3sSGIS" => [ "glMultiTexCoord3sSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLshort", "s" ],
								      [ "GLshort", "t" ],
454
								      [ "GLshort", "r" ] ], "glMultiTexCoord3sSGIS", "GL_SGIS_multitexture" ],
455
      "glMultiTexCoord3svSGIS" => [ "glMultiTexCoord3svSGIS", "void", [ [ "GLenum", "target" ],
456
									[ "GLshort *", "v" ] ], "glMultiTexCoord3svSGIS", "GL_SGIS_multitexture" ],
457 458 459 460
      "glMultiTexCoord4dSGIS" => [ "glMultiTexCoord4dSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLdouble", "s" ],
								      [ "GLdouble", "t" ],
								      [ "GLdouble", "r" ],
461
								      [ "GLdouble", "q" ] ], "glMultiTexCoord4dSGIS", "GL_SGIS_multitexture" ],
462
      "glMultiTexCoord4dvSGIS" => [ "glMultiTexCoord4dvSGIS", "void", [ [ "GLenum", "target" ],
463
									[ "GLdouble *", "v" ] ], "glMultiTexCoord4dvSGIS", "GL_SGIS_multitexture" ],
464 465 466 467
      "glMultiTexCoord4fSGIS" => [ "glMultiTexCoord4fSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLfloat", "s" ],
								      [ "GLfloat", "t" ],
								      [ "GLfloat", "r" ],
468
								      [ "GLfloat", "q" ] ], "glMultiTexCoord4fSGIS", "GL_SGIS_multitexture" ],
469
      "glMultiTexCoord4fvSGIS" => [ "glMultiTexCoord4fvSGIS", "void", [ [ "GLenum", "target" ],
470
									[ "GLfloat *", "v" ] ], "glMultiTexCoord4fvSGIS", "GL_SGIS_multitexture" ],
471 472 473 474
      "glMultiTexCoord4iSGIS" => [ "glMultiTexCoord4iSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLint", "s" ],
								      [ "GLint", "t" ],
								      [ "GLint", "r" ],
475
								      [ "GLint", "q" ] ], "glMultiTexCoord4iSGIS", "GL_SGIS_multitexture" ],
476
      "glMultiTexCoord4ivSGIS" => [ "glMultiTexCoord4ivSGIS", "void", [ [ "GLenum", "target" ],
477
									[ "GLint *", "v" ] ], "glMultiTexCoord4ivSGIS", "GL_SGIS_multitexture" ],
478 479 480 481
      "glMultiTexCoord4sSGIS" => [ "glMultiTexCoord4sSGIS", "void", [ [ "GLenum", "target" ],
								      [ "GLshort", "s" ],
								      [ "GLshort", "t" ],
								      [ "GLshort", "r" ],
482
								      [ "GLshort", "q" ] ], "glMultiTexCoord4sSGIS", "GL_SGIS_multitexture" ],
483
      "glMultiTexCoord4svSGIS" => [ "glMultiTexCoord4svSGIS", "void", [ [ "GLenum", "target" ],
484
									[ "GLshort *", "v" ] ], "glMultiTexCoord4svSGIS", "GL_SGIS_multitexture" ],
485 486
      "glMultiTexCoordPointerSGIS" => [ "glMultiTexCoordPointerSGIS", "void", [ [ "GLenum", "target" ],
										[ "GLint", "size" ],
487
										[ "GLenum", "type" ],
488
										[ "GLsizei", "stride" ],
489 490 491 492
										[ "GLvoid *", "pointer" ] ], "glMultiTexCoordPointerSGIS", "GL_SGIS_multitexture" ],
      "glSelectTextureSGIS" => [ "glSelectTextureSGIS", "void", [ [ "GLenum", "target" ] ], "glSelectTextureSGIS", "GL_SGIS_multitexture" ],
      "glSelectTextureCoordSetSGIS" => [ "glSelectTextureCoordSetSGIS", "void", [ [ "GLenum", "target" ] ], "glSelectTextureCoordSetSGIS", "GL_SGIS_multitexture" ],
      "glDeleteObjectBufferATI" => [ "glDeleteObjectBufferATI", "void", [ [ "GLuint", "buffer" ] ], "glDeleteObjectBufferATI", "GL_ATI_vertex_array_object" ]
493
      );
494

495 496 497
my @arg_names;
my %arg_types;
while (my $line = <REGISTRY>) {
498 499
    if ($line =~ /^\w*\(.*\)/) {
	# Get the function name (NOTE: the 'gl' prefix needs to be added later)
500
	my ($funcname, $args) = ($line =~ /^(\w*)\((.*)\)/);
501 502
	# and the argument names
	@arg_names = split /\s*,\s*/, $args;
503

504 505
	# After get :
	#  - the return type
506
	#  - category (the extension the function is part of)
507 508 509
	#  - the argument types
	#  - the category the function belongs
	%arg_types = ();
510 511
	my $category = "";
	my $ret_type = "";
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	while (1) {
	    $line = <REGISTRY>;
	    unless (defined($line)) {
		last;
	    } elsif ($line =~ /^\s*$/) {
		if (($category eq "") || ($ret_type eq "")) {
		    die "Missing 'category' line in function $funcname.\n";
		}
		last;
	    } elsif ($line =~ /\t*return\t*(\w*)/) {
		($ret_type) = ($line =~ /\t*return\s*(\w*)/);
		$ret_type = $pseudo_to_opengl{$ret_type};
		unless (defined($ret_type)) {
		    die "Unsupported return type in function $funcname\n";
		}
	    } elsif ($line =~ /^\t*category/) {
		($category) = ($line =~ /^\t*category\s*([\w-]*)/);
	    } elsif ($line =~ /^\t*param/) {
530 531
		my ($name, $base_type, $ext) = ($line =~ /\t*param\s*(\w*)\s*(\w*) (.*)/);
		my $ptr = 0;
532
		unless (defined($name)) {
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
		    chomp $line;
		    die "Broken spec file line $line in function $funcname\n";
		}

		if ($ext =~ /array/) {
		    # This is a pointer
		    $ptr = 1;
		} elsif ($ext =~ /value/) {
		    # And this a 'normal' value
		    $ptr = 0;
		} else {
		    chomp $line;
		    die "Unsupported type : $line in function $funcname\n";
		}
		# Get the 'real' type and append a '*' in case of a pointer
548
		my $type = $pseudo_to_opengl{$base_type};
549 550 551 552 553
		unless (defined($type)) {
		    chomp $line;
		    die "Unsupported return type in function $funcname for type $base_type (line $line)\n";
		}
		if ($ptr) {
554
		    $type = "$type*";
555
		}
556

557 558 559 560 561
		$arg_types{$name} = $type;
	    }
	}

	# Now, build the argument reference
562 563
	my $arg_ref = [ ];
	for (my $i = 0; $i <= $#arg_names; $i++) {
564 565 566 567 568 569 570
	    unless (defined($arg_types{$arg_names[$i]})) {
		print "@arg_names\n";
		foreach (sort keys %arg_types) {
		    print "$_ => $arg_types{$_}\n";
		}
		die "Undefined type for $arg_names[$i] in function $funcname\n";
	    }
571

572 573
	    push @$arg_ref, [ $arg_types{$arg_names[$i]}, $arg_names[$i] ];
	}
574 575 576
	my $func_ref = [ "gl$funcname",
                         $ret_type,
                         $arg_ref,
577 578
                         "gl$funcname",
                         "GL_$category" ];
579

580 581
	# Now, put in one or the other hash table
	if ($norm_categories{$category}) {
582
	    $norm_functions{"gl$funcname"} = $func_ref;
583
	} else {
584
	    $ext_functions{"gl$funcname"} = $func_ref;
585 586 587 588 589 590 591 592 593 594 595 596 597
	}
    }
}

#
# Clean up the input files
#
close(TYPES);
close(REGISTRY);

#
# Now, generate the output files. First, the spec file.
#
598
open(SPEC, ">$spec_file");
599 600

foreach (sort keys %norm_functions) {
601
    my $func_name = $norm_functions{$_}->[0];
602
    print SPEC "@  stdcall $func_name( ";
603 604
    for (my $i = 0; $i <= $#{@{$norm_functions{$_}->[2]}}; $i++) {
	my $type = $norm_functions{$_}->[2]->[$i]->[0];
605 606 607 608 609
	if ($type =~ /\*/) {
	    print SPEC "ptr ";
	} elsif (defined($arg_conv{$type})) {
	    print SPEC "$@$arg_conv{$type}[0] ";
	} else {
610
	    die "No conversion for GL type $type...\n";
611 612 613 614
	}
    }
    print SPEC ") wine_$func_name\n";
}
615 616 617 618

print SPEC "@  stub    glGetLevelParameterfv
@  stub    glGetLevelParameteriv
@  stdcall wglChoosePixelFormat(long ptr) gdi32.ChoosePixelFormat
619
@  stdcall wglCopyContext(long long long) gdi32.wglCopyContext
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
@  stdcall wglCreateContext(long) gdi32.wglCreateContext
@  stdcall wglCreateLayerContext(long long)
@  stdcall wglDeleteContext(long) gdi32.wglDeleteContext
@  stdcall wglDescribeLayerPlane(long long long long ptr)
@  stdcall wglDescribePixelFormat(long long long ptr) gdi32.DescribePixelFormat
@  stdcall wglGetCurrentContext() gdi32.wglGetCurrentContext
@  stdcall wglGetCurrentDC() gdi32.wglGetCurrentDC
@  stub    wglGetDefaultProcAddress
@  stdcall wglGetLayerPaletteEntries(long long long long ptr)
@  stdcall wglGetPixelFormat(long) gdi32.GetPixelFormat
@  stdcall wglGetProcAddress(str)
@  stdcall wglMakeCurrent(long long) gdi32.wglMakeCurrent
@  stdcall wglRealizeLayerPalette(long long long)
@  stdcall wglSetLayerPaletteEntries(long long long long ptr)
@  stdcall wglSetPixelFormat(long long ptr) gdi32.SetPixelFormat
@  stdcall wglShareLists(long long) gdi32.wglShareLists
@  stdcall wglSwapBuffers(long) gdi32.SwapBuffers
@  stdcall wglSwapLayerBuffers(long long)
@  stdcall wglUseFontBitmapsA(long long long long) gdi32.wglUseFontBitmapsA
@  stdcall wglUseFontBitmapsW(long long long long) gdi32.wglUseFontBitmapsW
@  stdcall wglUseFontOutlinesA(long long long long long long long ptr)
@  stdcall wglUseFontOutlinesW(long long long long long long long ptr)
";

644 645 646 647 648
close(SPEC);

#
# After the spec file, the opengl_norm.c file
#
649
open(NORM, ">$norm_file");
650 651 652 653
print NORM "
/* Auto-generated file... Do not edit ! */

#include \"config.h\"
654
#include \"opengl_ext.h\"
655
#include \"wine/debug.h\"
656

657
WINE_DEFAULT_DEBUG_CHANNEL(opengl);
658 659
";
foreach (sort keys %norm_functions) {
660
    my $string = GenerateThunk($norm_functions{$_}, 1, "", $gen_thread_safe, "");
661

662
    print NORM "\n$string" if $string;
663 664 665 666 667 668
}
close(NORM);

#
# Finally, more complex, the opengl_ext.c file
#
669
open(EXT, ">$ext_file");
670 671 672 673
print EXT "
/* Auto-generated file... Do not edit ! */

#include \"config.h\"
674
#include \"opengl_ext.h\"
675
#include \"wine/debug.h\"
676

677
WINE_DEFAULT_DEBUG_CHANNEL(opengl);
678

679 680
";

681 682 683 684 685 686
# The thunks themselves....
my $count = keys %ext_functions;
print EXT "const int extension_registry_size = $count;\n";
print EXT "void *extension_funcs[$count];\n";
print EXT "\n/* The thunks themselves....*/";
my $pos = 0;
687
foreach (sort keys %ext_functions) {
688
    my $func_ref = $ext_functions{$_};
689
    my $local_var = "  " . ConvertType($func_ref->[1]) . " (*$ext_prefix$func_ref->[0])( ";
690 691
    for (my $i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
	my $type = ConvertType($func_ref->[2]->[$i]->[0]);
692
	$local_var .= "$type";
693
	if ($i != $#{@{$func_ref->[2]}}) {
694
	    $local_var .= ", ";
695
	} else {
696
	    $local_var .= " ";
697 698
	}
    }
699 700 701 702
    $local_var .= 'void ' if ($#{@{$func_ref->[2]}} < 0);
    $local_var .= ") = extension_funcs[$pos];\n";
    $pos++;
    print EXT "\nstatic ", GenerateThunk($ext_functions{$_}, 0, $ext_prefix, $gen_thread_safe, $local_var);
703 704
}

705 706
# Then the table giving the string <-> function correspondence */
print EXT "\n\n/* The table giving the correspondence between names and functions */\n";
707
print EXT "const OpenGL_extension extension_registry[$count] = {\n";
708
my $i = 0;
709
foreach (sort keys %ext_functions) {
710
    my $func_ref = $ext_functions{$_};
711 712
    if ($func_ref->[0] eq $func_ref->[3])
    {
713
        print EXT "  { \"$func_ref->[0]\", \"$func_ref->[4]\", (void *) wine_$func_ref->[0] }";
714
    }
715
    if ($i != $count-1) {
716 717 718 719 720 721 722 723
	print EXT ",";
    }
    $i++;
    print EXT "\n";
}
print EXT "};\n";

close(EXT);