Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 328 lines (276 sloc) 7.904 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
##
## csgen.pl
## Copyright (C) 2004 Vladimir Vukicevic <vladimir@pobox.com>
##
## Portions taken from GLEW (http://glew.sourceforge.net/), which is
## Copyright (C) 2004, 2003 Marcelo E. Magallon <mmagallo[at]debian org>
## Copyright (C) 2004, 2003 Milan Ikits <milan ikits[at]ieee org>
##
## This program is distributed under the terms and conditions of the GNU
## General Public License Version 2 as published by the Free Software
## Foundation or, at your option, any later version.
##
## Please see the file "COPYING" for more information
##
use Data::Dumper;

$strip_prefix = 1;
#$cconv = "GlDetails.GL_NATIVE_CALLCONV";
$cconv = "CallingConvention.Cdecl";

my %regex = (extname => qr/^[A-Z][A-Za-z0-9_]+$/,
exturl => qr/^http.+$/,
function => qr/^(.+) ([a-z][a-z0-9_]*) \((.+)\)( *= .*)?$/i,
token => qr/^([A-Z][A-Z0-9_]*)\s+((?:0x)?[0-9A-F]+|[A-Z][A-Z0-9_]*)$/,
type => qr/^typedef\s+(.+)\s+([\*A-Za-z0-9_]+)$/,
exact => qr/.*;$/,
);

my %typemap = ('GLenum' => 'unsigned int',
'GLboolean' => 'bool', # uchar
'GLbitfield' => 'unsigned int',
'GLvoid' => 'void',
'GLbyte' => 'byte',
'GLshort' => 'short',
'GLint' => 'int',
'GLubyte' => 'unsigned byte',
'GLushort' => 'unsigned short',
'GLuint' => 'unsigned int',
'GLsizei' => 'int',
'GLfloat' => 'float',
'GLdouble' => 'double',
'GLclampd' => 'double',

'GLsizeiptrARB' => 'IntPtr',
'GLintptrARB' => 'IntPtr',
'GLhalf' => 'unsigned short',
'GLcharARB' => 'byte',
'GLhandleARB' => 'int',


'GLXContextID' => 'int',
'GLXFBConfigIDSGIX' => 'int',
'GLXPbufferSGIX' => 'int',
);

# List of things that an ARRAY in a function expands out to
my @ARRAY_expansions = ('bool []',
'byte []',
'short []',
'ushort []',
'int []',
'uint []',
'float []',
'double []',
'IntPtr');

sub parse_ext($)
{
  my $filename = shift;
  my @functions;
  my %tokens = ();
  my %types = ();
  my @exacts = ();
  my $extname = "";
  my $exturl = "";

  open EXT, "<$filename" or return;

  while (<EXT>) {
    chomp;
    if (/$regex{extname}/) {
      $extname = $_;
      next;
    } elsif (/$regex{exturl}/) {
      $exturl = $_;
    } elsif (s/^\s+//) {
      if (/$regex{exact}/) {
push @exacts, $_;
      } elsif (/$regex{type}/) {
my ($value, $name) = ($1, $2);
$types{$name} = $value;
      } elsif (/$regex{token}/) {
my ($name, $value) = ($1, $2);
$tokens{$name} = $value;
      } elsif (/$regex{function}/) {
my ($return, $name, $parms, $realname) = ($1, $2, $3, $4);

$realname =~ s/^ *= *//;
$realname =~ s/ *$//;

if ($realname eq "") {
$realname = $name;
}

push (@functions, {name => $name,
rtype => $return,
parms => $parms,
realname => $realname,
});
      }
    }
  }

  close EXT;

  return ($extname, $exturl, \%types, \%tokens, \@functions, \@exacts);
}

sub output_header ($$)
{
  $extname = shift;
  $exturl = shift;

  print " //\n";
  print " // $extname\n";
  print " // $exturl\n";
  print " //\n";
}

%enum_hash = ();

sub output_enum ($$)
{
  $name = shift;
  $val = shift;

  if ($strip_prefix && !($name =~ /^GL_[0-9]/)) {
    $name =~ s/^GL_//;
  }

  if ($strip_prefix && !($val =~ /^GL_[0-9]/)) {
    $val =~ s/^GL_//;
  }

  if (defined($enum_hash{$name})) {
    if (!($enum_hash{$name} eq $val)) {
      print STDERR "$name previously defined as %enum_hash{$name} and now $val\n";
    }
    return;
  }

  $enum_hash{$name} = $val;
  print " public const uint " . $name . " = " . $val . ";\n";
}

%seenextfields = ();

sub output_func ($$)
{
  my $extname = shift;
  my $fdata = shift;

  $fname = $fdata->{'name'};
  $usefname = $fname;
  $frtype = $fdata->{'rtype'};
  $fallparms = $fdata->{'parms'};
  $arrayexpand = 0;
  $unsafe = "";

  # fix up some things that are keywords, argh
  $fallparms =~ s/GLvoid/void/g;
  $fallparms =~ s/const//g;
  $fallparms =~ s/params/paramz/g;
  $fallparms =~ s/ ref,/ glref,/g;
  $fallparms =~ s/ ref *$/ glref/g;
  $fallparms =~ s/ base/ glbase/g;
  $fallparms =~ s/ out,/ glout,/g;
  $fallparms =~ s/ out *$/ glout/g;
  $fallparms =~ s/ in,/ glin,/g;
  $fallparms =~ s/ in *$/ glin/g;
  $fallparms =~ s/ object/ globj/g;
  $fallparms =~ s/ string/ glstring/g;
  # sigh
  $fallparms =~ s/ m\[16\]/ *m/g;
  $fallparms =~ s/ v\[\]/ *v/g;

  # this function leaves off a name for the last parameter.
  # sigh++
  if ($usefname eq "glWindowPos4dMESA") {
    $fallparms .= " w";
  }

  # nuke const's from the resturn type
  $frtype =~ s/const//g;
  # change GLvoid's to voids, as void can't
  # be aliased
  $frtype =~ s/GLvoid/void/g;

  if ($strip_prefix) {
    $usefname =~ s/^gl//;
  }

  if (defined $fdata->{'realname'}) {
    $fname = $fdata->{'realname'};
  }

  # if the entire param list is void, then we "" it
  if ($fallparms =~ /^ *void *$/i) {
    $fallparms = "";
  }

  if ($fallparms =~ m/\*/ || $frtype =~ m/\*/) {
    $unsafe = "unsafe";
  }

  if ($fallparms =~ m/ARRAY/) {
    $arrayexpand = 1;
  }

  ## The idea here is to automatically take things like
  ## "GLfloat *f" and spit out bindings for
  ## "GLfloat *f" and "GLfloat [] f"; also
  ## if the name ended in something like "3fv", spit out
  ## "ref Vector3 v" too. But, it's probably easiest to
  ## just do these by hand, especially since we don't
  ## really care to support glVertex*, glNormal*, etc.
  if (0) {
    # now rebuild the param lists
    @paramlists = [];
    @fparms = split (/, */, $fallparms);

    foreach my $fp (@fparms) {
      # A little gross -- we want types in $ft, but * placement
      # is inconsistent.. so we strip any leading *'s from $fn
      # and tack them onto the end of $ft
      $fp =~ /(.*) ([^ ]*)/;
      ($ft, $fn) = ($1, $2);
      if ($fn =~ /^(\*+)(.*)/) {
$ft .= $1;
$fn = $2;
      }

      $ft =~ /( *\*+)*$/;
      $stars = $1;
    }
  }

  my $outfunc;

  # if extname is 0, it means it's a core GL function
  # and shouldn't get ExtensionAttribute info, but instead
  # a straight DllImport
  if ($extname eq "CORE") {
    $outfunc = sub
{
$paramargs = shift;
print " [DllImport(GlDetails.GL_NATIVE_LIBRARY, EntryPoint=\"$fname\", CallingConvention=$cconv, ExactSpelling=true)]\n";
print " public static extern $unsafe $frtype $usefname ($paramargs);\n";
      }
  } else {
    # else this is an extension, so we need to not have a DllImport, but an attribute here
    $extfield = "ext__" . $extname . "__" . $fname;
    if (!defined($seenextfields{$extfield})) {
      print " public static IntPtr $extfield = IntPtr.Zero;\n";
      $seenextfields{$extfield} = 1;
    }

    $outfunc = sub
{
$paramargs = shift;
print " [OpenGLExtensionImport(\"$extname\", \"$fname\")]\n";
print " public static $unsafe $frtype $usefname ($paramargs) {\n";
print " throw new InvalidOperationException(\"binding error\");\n";
print " }\n";
      }
  }

  if ($arrayexpand) {
    foreach my $expansion (@ARRAY_expansions) {
      ($newparms = $fallparms) =~ s/ARRAY/$expansion/;
      &$outfunc ($newparms);
    }
  } else {
    &$outfunc ($fallparms);
  }

  print "\n";
}

##
## main
##

$is_core = 0;

foreach my $extfile (@ARGV)
{
  if ($extfile eq "WIN32") {
    $cconv = "CallingConvention.StdCall";
    next;
  } elsif ($extfile eq "CORE") {
    $is_core = 1;
    next;
  } elsif ($extfile eq "EXT") {
    $is_core = 0;
    next;
  }

  my ($extname, $exturl, $types, $tokens, $funcs, $exacts) = parse_ext($extfile);

  # write a header
  output_header ($extname, $exturl);

  print "\n";

  # write the constant tokens
  foreach my $tok (keys %$tokens) {
    output_enum ($tok, $tokens->{$tok});
  }

  print "\n";

  # now write funcs
  foreach my $func (@$funcs) {
    output_func ($is_core ? "CORE" : $extname, $func);
  }
}
Something went wrong with that request. Please try again.