3 # This script is called thus :
5 # make_opengl path_to_spec_file opengl_version
7 # - path_to_spec_file is the path to the directory where the OpenGL
8 # spec files are located. These files are part of the OpenGL
9 # sample implementation CVS tree and are located in
10 # CVS_ROOT/projects/ogl-sample/main/doc/registry/specs.
11 # You can find them on the web at the following URL :
12 # http://oss.sgi.com/cgi-bin/cvsweb.cgi/projects/ogl-sample/main/doc/registry/specs/
14 # - opengl_version is the OpenGL version emulated by the library
15 # (can be 1.0 to 1.2).
17 # This script generates the three following files :
19 # - opengl32.spec : the spec file giving all the exported functions
20 # of the OpenGL32.DLL library. These functions are the one an
21 # application can directly link to (and are all the functions
22 # defined in the OpenGL core for the version defined by
25 # - opengl_norm.c : this file contains the thunks for all OpenGL
26 # functions that are defined in 'opengl32.spec'. The corresponding
27 # functions NEED to be defined in Linux's libGL or the library
28 # won't be able to be linked in.
30 # - opengl_ext.c : in this file are stored thunks for ALL possible
31 # OpenGL extensions (at least, all the extensions that are defined
32 # in the OpenGL extension registry). Contrary to 'opengl_norm.c',
33 # you do not need to have these extensions in your libGL to have
34 # OpenGL work (as they are resolved at run-time using
35 # glXGetProcAddressARB).
41 $spec_file = "opengl32.spec";
42 $norm_file = "opengl_norm.c";
43 $ext_file = "opengl_ext.c";
45 # Set to 0 for removing the ENTER / LEAVE GL calls
47 # Prefix used for the local variables
48 $ext_prefix = "func_";
49 # If set to 1, generate TRACEs for each OpenGL function
53 # List of categories to put in the 'opengl_norm.c' file
55 %cat_1_0 = ( "display-list" => 1,
57 "drawing-control" => 1,
66 %cat_1_1 = ( %cat_1_0,
68 %cat_1_2 = ( %cat_1_1,
70 "ARB_multitexture" => 1 );
72 %norm_categories = ();
75 # This hash table gives the conversion between OpenGL types and what
76 # is used by the TRACE printfs
79 ("GLbitfield" => "%d",
95 "_GLfuncptr" => "%p");
98 # This hash table gives the conversion between OpenGL types and what
99 # is used in the .spec file
102 ("GLbitfield" => [ "long", 4 ],
103 "GLboolean" => [ "long", 4 ],
104 "GLbyte" => [ "long", 4 ],
105 "GLclampd" => [ "double", 8 ],
106 "GLclampf" => [ "long", 4 ],
107 "GLdouble" => [ "double", 8 ],
108 "GLenum" => [ "long", 4 ],
109 "GLfloat" => [ "long", 4 ],
110 "GLint" => [ "long", 4 ],
111 "GLshort" => [ "long", 4 ],
112 "GLsizei" => [ "long", 4 ],
113 "GLstring" => [ "str", 4 ],
114 "GLubyte" => [ "long", 4 ],
115 "GLuint" => [ "long", 4 ],
116 "GLushort" => [ "long", 4 ],
117 "GLvoid" => [ "void", 4 ],
118 "_GLfuncptr" => [ "ptr", 4 ]);
121 # This functions generates the thunk for a given function.
124 my ($func_ref, $comment, $prefix, $thread_safe) = @_;
126 my ($call_arg) = ("");
127 my ($trace_arg) = ("");
129 # If for opengl_norm.c, generate a nice heading otherwise Patrik won't be happy :-)
130 # Patrik says: Well I would be even happier if a (OPENGL.@) was added as well. Done. :-)
132 $ret = $ret . "/***********************************************************************\n";
133 $ret = $ret . " * " . $func_ref->[0] . " (OPENGL.@)\n";
134 $ret = $ret . " */\n";
136 $ret = $ret . $func_ref->[1] . " WINAPI wine_" . $func_ref->[0] . "( ";
137 for ($i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
138 $type = $func_ref->[2]->[$i]->[0];
139 $name = $func_ref->[2]->[$i]->[1];
140 $ret = $ret . "$type $name";
141 $call_arg = $call_arg . "$name";
143 $trace_arg = $trace_arg . "%p";
145 $trace_arg = $trace_arg . $debug_conv{$type};
147 if ($i != $#{@{$func_ref->[2]}}) {
149 $call_arg = $call_arg . ", ";
150 $trace_arg = $trace_arg . ", ";
153 $call_arg = $call_arg . " ";
156 $ret = $ret . ") {\n";
157 if ($func_ref->[1] ne "void") {
158 $ret = $ret . " " . $func_ref->[1] . " ret_value;\n";
161 $ret = $ret . " TRACE(\"(" . $trace_arg . ")\\n\"";
162 if ($trace_arg ne "") {
163 $ret = $ret . ", " . $call_arg;
165 $ret = $ret . ");\n";
168 $ret = $ret . " ENTER_GL();\n";
171 if ($func_ref->[1] ne "void") {
172 $ret = $ret . "ret_value = ";
174 $ret = $ret . $prefix . $func_ref->[0] . "( " . $call_arg . ");\n";
176 $ret = $ret . " LEAVE_GL();\n";
178 if ($func_ref->[1] ne "void") {
179 $ret = $ret . " return ret_value;\n"
183 # Return this string....
188 # Extract and checks the number of arguments
191 die "Usage : make_opengl OpenGL_registry_location OpenGL_version.\n";
193 $registry_path = shift @ARGV;
194 $version = shift @ARGV;
195 if ($version eq "1.0") {
196 %norm_categories = %cat_1_0;
197 } elsif ($version eq "1.1") {
198 %norm_categories = %cat_1_1;
199 } elsif ($version eq "1.2") {
200 %norm_categories = %cat_1_2;
202 die "OpenGL version incorrect. Should be one of '1.0', '1.1' or '1.2'.\n";
206 # Open the registry files
208 open(TYPES, $registry_path . "/gl.tm") || die "Could not open 'gl.tm'. Please check your path the the registry files.\n";
209 open(REGISTRY, $registry_path . "/gl.spec") || die "Could not open 'gl.spec'. Please check your path the the registry files.\n";
212 # First, create a mapping between the pseudo types used in the spec file
213 # and OpenGL types using the 'gl.tm' file.
215 %pseudo_to_opengl = ();
216 while ($line = <TYPES>) {
217 ($pseudo, $opengl) = ($line =~ /(\w*),\*,\*,\s*(.*),\*,\*/);
218 $pseudo_to_opengl{$pseudo} = $opengl;
220 # This is to override the 'void' -> '*' bogus conversion
221 $pseudo_to_opengl{"void"} = "void";
222 # This is a bug in the spec file...
223 $pseudo_to_opengl{"FfdTargetSGIX"} = "GLint";
224 $pseudo_to_opengl{"FfdMaskSGIX"} = "GLint";
225 $pseudo_to_opengl{"IglooFunctionSelectSGIX"} = "GLint";
226 $pseudo_to_opengl{"IglooParameterSGIX"} = "GLint";
229 # Then, create the list of all OpenGL functions using the 'gl.spec'
230 # file. This will create two hash-tables, one with all the function
231 # whose category matches the one listed in '@norm_categories', the other
232 # with all other functions.
234 # An element of the hash table is a reference to an array with these
241 # - reference to an array giving the list of arguments (an empty array
242 # for a 'void' function).
244 # The list of arguments is itself an array of reference to arrays. Each
245 # of these arrays represents the argument type and the argument name.
249 # void glBitmap( GLsizei width, GLsizei height,
250 # GLfloat xorig, GLfloat yorig,
251 # GLfloat xmove, GLfloat ymove,
252 # const GLubyte *bitmap );
254 # Would give something like that :
258 # [ [ "GLsizei", "width" ],
259 # [ "GLsizei", "height" ],
260 # [ "GLfloat", "xorig" ],
261 # [ "GLfloat", "yorig" ],
262 # [ "GLfloat", "xmove" ],
263 # [ "GLfloat", "ymove" ],
264 # [ "GLubyte *", "bitmap"] ] ];
266 %norm_functions = ();
269 while ($line = <REGISTRY>) {
270 if ($line =~ /^\w*\(.*\)/) {
271 # Get the function name (NOTE: the 'gl' prefix needs to be added later)
272 ($funcname, $args) = ($line =~ /^(\w*)\((.*)\)/);
273 # and the argument names
274 @arg_names = split /\s*,\s*/, $args;
278 # - the argument types
279 # - the category the function belongs
285 unless (defined($line)) {
287 } elsif ($line =~ /^\s*$/) {
288 if (($category eq "") || ($ret_type eq "")) {
289 die "Missing 'category' line in function $funcname.\n";
292 } elsif ($line =~ /\t*return\t*(\w*)/) {
293 ($ret_type) = ($line =~ /\t*return\s*(\w*)/);
294 $ret_type = $pseudo_to_opengl{$ret_type};
295 unless (defined($ret_type)) {
296 die "Unsupported return type in function $funcname\n";
298 } elsif ($line =~ /^\t*category/) {
299 ($category) = ($line =~ /^\t*category\s*([\w-]*)/);
300 } elsif ($line =~ /^\t*param/) {
301 ($name, $base_type, $ext) = ($line =~ /\t*param\s*(\w*)\s*(\w*) (.*)/);
303 unless (defined($name)) {
305 die "Broken spec file line $line in function $funcname\n";
308 if ($ext =~ /array/) {
311 } elsif ($ext =~ /value/) {
312 # And this a 'normal' value
316 die "Unsupported type : $line in function $funcname\n";
318 # Get the 'real' type and append a '*' in case of a pointer
319 $type = $pseudo_to_opengl{$base_type};
320 unless (defined($type)) {
322 die "Unsupported return type in function $funcname for type $base_type (line $line)\n";
328 $arg_types{$name} = $type;
332 # Now, build the argument reference
334 for ($i = 0; $i <= $#arg_names; $i++) {
335 unless (defined($arg_types{$arg_names[$i]})) {
336 print "@arg_names\n";
337 foreach (sort keys %arg_types) {
338 print "$_ => $arg_types{$_}\n";
340 die "Undefined type for $arg_names[$i] in function $funcname\n";
343 push @$arg_ref, [ $arg_types{$arg_names[$i]}, $arg_names[$i] ];
345 $func_ref = [ "gl" . $funcname,
349 # Now, put in one or the other hash table
350 if ($norm_categories{$category}) {
351 $norm_functions{$funcname} = $func_ref;
353 $ext_functions{$funcname} = $func_ref;
359 # Clean up the input files
365 # Now, generate the output files. First, the spec file.
367 open(SPEC, ">" . $spec_file);
377 debug_channels (opengl)
379 @ stdcall wglCreateContext(long) wglCreateContext
380 @ stdcall wglCreateLayerContext(long long) wglCreateLayerContext
381 @ stdcall wglCopyContext(long long long) wglCopyContext
382 @ stdcall wglDeleteContext(long) wglDeleteContext
383 @ stdcall wglDescribeLayerPlane(long long long long ptr) wglDescribeLayerPlane
384 @ stdcall wglGetCurrentContext() wglGetCurrentContext
385 @ stdcall wglGetCurrentDC() wglGetCurrentDC
386 @ stdcall wglGetLayerPaletteEntries(long long long long ptr) wglGetLayerPaletteEntries
387 @ stdcall wglGetProcAddress(str) wglGetProcAddress
388 @ stdcall wglMakeCurrent(long long) wglMakeCurrent
389 @ stdcall wglRealizeLayerPalette(long long long) wglRealizeLayerPalette
390 @ stdcall wglSetLayerPaletteEntries(long long long long ptr) wglSetLayerPaletteEntries
391 @ stdcall wglShareLists(long long) wglShareLists
392 @ stdcall wglSwapLayerBuffers(long long) wglSwapLayerBuffers
393 @ stdcall wglUseFontBitmapsA(long long long long) wglUseFontBitmapsA
394 @ stdcall wglUseFontOutlinesA(long long long long long long long ptr) wglUseFontOutlinesA
395 @ stub glGetLevelParameterfv
396 @ stub glGetLevelParameteriv
397 @ stub wglUseFontBitmapsW
398 @ stub wglUseFontOutlinesW
399 @ forward wglChoosePixelFormat GDI32.ChoosePixelFormat
400 @ forward wglDescribePixelFormat GDI32.DescribePixelFormat
401 @ forward wglGetPixelFormat GDI32.GetPixelFormat
402 @ forward wglSetPixelFormat GDI32.SetPixelFormat
403 @ forward wglSwapBuffers GDI32.SwapBuffers
406 foreach (sort keys %norm_functions) {
407 $func_name = $norm_functions{$_}->[0];
408 print SPEC "@ stdcall $func_name( ";
409 for ($i = 0; $i <= $#{@{$norm_functions{$_}->[2]}}; $i++) {
410 $type = $norm_functions{$_}->[2]->[$i]->[0];
413 } elsif (defined($arg_conv{$type})) {
414 print SPEC "$@$arg_conv{$type}[0] ";
416 die "No convertion for GL type $type...\n";
419 print SPEC ") wine_$func_name\n";
424 # After the spec file, the opengl_norm.c file
426 open(NORM, ">" . $norm_file);
428 /* Auto-generated file... Do not edit ! */
430 #include \"config.h\"
431 #include \"wine_gl.h\"
432 #include \"debugtools.h\"
434 typedef const GLubyte * GLstring;
436 DEFAULT_DEBUG_CHANNEL(opengl);
439 foreach (sort keys %norm_functions) {
440 $string = GenerateThunk($norm_functions{$_}, 1, "", $gen_thread_safe);
442 print NORM "$string\n";
447 # Finally, more complex, the opengl_ext.c file
449 open(EXT, ">" . $ext_file);
451 /* Auto-generated file... Do not edit ! */
453 #include \"config.h\"
454 #include \"wine_gl.h\"
455 #include \"debugtools.h\"
457 typedef const GLubyte * GLstring;
459 #include \"opengl_ext.h\"
461 DEFAULT_DEBUG_CHANNEL(opengl);
465 # First, generate the function pointers
466 foreach (sort keys %ext_functions) {
467 $func_ref = $ext_functions{$_};
468 print EXT $func_ref->[1] . " (*" . $ext_prefix . $func_ref->[0] . ")( ";
469 for ($i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
470 $type = $func_ref->[2]->[$i]->[0];
472 if ($i != $#{@{$func_ref->[2]}}) {
478 print EXT ") = (void *) 0xdeadbeef;\n";
481 # Then, the function prototypes
482 print EXT "\n\n/* The function prototypes */\n";
483 foreach (sort keys %ext_functions) {
484 $func_ref = $ext_functions{$_};
485 print EXT $func_ref->[1] . " WINAPI " . "wine_" . $func_ref->[0] . "( ";
486 for ($i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
487 $type = $func_ref->[2]->[$i]->[0];
489 if ($i != $#{@{$func_ref->[2]}}) {
498 # Then the table giving the string <-> function correspondance */
499 print EXT "\n\n/* The table giving the correspondance between names and functions */\n";
500 @tmp = keys %ext_functions;
501 print EXT "int extension_registry_size = " . ($#tmp + 1) . ";\n";
502 print EXT "OpenGL_extension extension_registry[" . ($#tmp + 1) . "] = {\n";
504 foreach (sort keys %ext_functions) {
505 $func_ref = $ext_functions{$_};
506 print EXT " { \"" . $func_ref->[0] . "\", (void *) wine_" . $func_ref->[0] . ", (void **) (&" . $ext_prefix . $func_ref->[0] . ") }";
515 # And, finally, the thunks themselves....
516 print EXT "\n/* The thunks themselves....*/\n";
517 foreach (sort keys %ext_functions) {
518 $string = GenerateThunk($ext_functions{$_}, 0, $ext_prefix, $gen_thread_safe);
520 print EXT "$string\n";