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