#!/usr/bin/perl -w
use strict;
use XML::LibXML;

# This script is called thus :
#
#   make_opengl [opengl_version]
#
#     - It needs files from the OpenGL extension registry:
#
#       https://raw.github.com/KhronosGroup/OpenGL-Registry/master/xml/gl.xml
#       https://raw.github.com/KhronosGroup/OpenGL-Registry/master/xml/wgl.xml
#
#       If they are not found in the current directory the script will
#       attempt to download them from there.
#
#     - opengl_version is the OpenGL version emulated by the library
#       (can be 1.0 to 1.5). The default is 1.1.
#
# 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).
#
#     - include/wine/wgl_driver.h: definitions for the tables of OpenGL functions.
#
#
# Copyright 2000 Lionel Ulmer
# Copyright 2012 Alexandre Julliard
# Copyright 2019 Conor McCarthy for Codeweavers
#
# 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
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#

#
# Files to generate
#
my $spec_file = "opengl32.spec";
my $norm_file = "opengl_norm.c";
my $ext_file  = "opengl_ext.c";
my $wgl_driver_file = "../../include/wine/wgl_driver.h";
my $wgl_file = "../../include/wine/wgl.h";

# If set to 1, generate TRACEs for each OpenGL function
my $gen_traces = 1;

#
# List of categories to put in the 'opengl_norm.c' file
#
my %cat_1_0 = ( "GL_VERSION_1_0" => 1 );
my %cat_1_1 = ( %cat_1_0, "GL_VERSION_1_1" => 1 );
my %cat_1_2 = ( %cat_1_1, "GL_VERSION_1_2" => 1 );
my %cat_1_3 = ( %cat_1_2, "GL_VERSION_1_3" => 1 );
my %cat_1_4 = ( %cat_1_3, "GL_VERSION_1_4" => 1 );
my %cat_1_5 = ( %cat_1_4, "GL_VERSION_1_5" => 1 );

my %norm_categories = ();

#
# This hash table gives the conversion between OpenGL types and
# the .spec type and debug format
#
my %arg_types =
    (
     "GLbitfield" =>           [ "long",   "%d" ],
     "GLboolean" =>            [ "long",   "%d" ],
     "GLbyte" =>               [ "long",   "%d" ],
     "GLchar" =>               [ "long",   "%c" ],
     "GLcharARB" =>            [ "long",   "%c" ],
     "GLclampd" =>             [ "double", "%f" ],
     "GLclampf" =>             [ "float",  "%f" ],
     "GLclampx" =>             [ "long",   "%d" ],
     "GLdouble" =>             [ "double", "%f" ],
     "GLeglClientBufferEXT" => [ "ptr",    "%p" ],
     "GLeglImageOES" =>        [ "ptr",    "%p" ],
     "GLenum" =>               [ "long",   "%d" ],
     "GLfixed" =>              [ "long",   "%d" ],
     "GLfloat" =>              [ "float",  "%f" ],
     "GLhalfNV" =>             [ "long",   "%d" ],
     "GLhandleARB" =>          [ "long",   "%d" ],
     "GLint" =>                [ "long",   "%d" ],
     "GLint64" =>              [ "int64",  "wine_dbgstr_longlong(%s)" ],
     "GLint64EXT" =>           [ "int64",  "wine_dbgstr_longlong(%s)" ],
     "GLintptr" =>             [ "long",   "%ld" ],
     "GLintptrARB" =>          [ "long",   "%ld" ],
     "GLshort" =>              [ "long",   "%d" ],
     "GLsizei" =>              [ "long",   "%d" ],
     "GLsizeiptr" =>           [ "long",   "%ld" ],
     "GLsizeiptrARB" =>        [ "long",   "%ld" ],
     "GLstring" =>             [ "str",    "wine_dbgstr_a(%s)" ],
     "GLsync" =>               [ "ptr",    "%p" ],
     "GLubyte" =>              [ "long",   "%d" ],
     "GLuint" =>               [ "long",   "%d" ],
     "GLuint64" =>             [ "int64",  "wine_dbgstr_longlong(%s)" ],
     "GLuint64EXT" =>          [ "int64",  "wine_dbgstr_longlong(%s)" ],
     "GLushort" =>             [ "long",   "%d" ],
     "GLvdpauSurfaceNV" =>     [ "long",   "%ld" ],
     "GLDEBUGPROC" =>          [ "ptr",    "%p" ],
     "GLDEBUGPROCARB" =>       [ "ptr",    "%p" ],
     "GLDEBUGPROCAMD" =>       [ "ptr",    "%p" ],
     "GLDEBUGPROCKHR" =>       [ "ptr",    "%p" ],
     "GLVULKANPROCNV" =>       [ "ptr",    "%p" ],
     "HDC" =>                  [ "long",   "%p" ],
     "HGLRC" =>                [ "long",   "%p" ],
     "HPBUFFERARB" =>          [ "long",   "%p" ],
     "HENHMETAFILE" =>         [ "long",   "%p" ],
     "LPGLYPHMETRICSFLOAT" =>  [ "ptr",    "%p" ],
     "LPCSTR" =>               [ "str",    "wine_dbgstr_a(%s)" ],
     "UINT" =>                 [ "long",   "%u" ],
     "DWORD" =>                [ "long",   "%u" ],
     "BOOL" =>                 [ "long",   "%u" ],
     "FLOAT" =>                [ "float",  "%f" ],
    );

my %specially_handled_types =
    (
     "HGLRC" => 1,
     "HPBUFFERARB" => 1,
    );

my %remap_types =
    (
     "HGLRC" => "struct wgl_context *",
     "HPBUFFERARB" => "struct wgl_pbuffer *",
     "GLintptr" => "WINEGLDEF(GLintptr)",
     "GLsizeiptr" => "WINEGLDEF(GLsizeiptr)",
     "GLintptrARB" =>  "WINEGLDEF(GLintptrARB)",
     "GLsizeiptrARB" =>  "WINEGLDEF(GLsizeiptrARB)",
     "GLvdpauSurfaceNV" => "WINEGLDEF(GLvdpauSurfaceNV)",
     "GLDEBUGPROC" => "void *",
     "GLDEBUGPROCAMD" => "void *",
     "GLDEBUGPROCARB" => "void *",
     "PROC" => "WINEGLDEF(PROC)",
    );

my %khronos_types =
    (
     "khronos_int8_t" => "signed char",
     "khronos_uint8_t" => "unsigned char",
     "khronos_int16_t" => "short",
     "khronos_uint16_t" => "unsigned short",
     "khronos_int32_t" => "int",
     "khronos_uint32_t" => "unsigned int",
     "khronos_float_t" => "float",
    );
#
# Used to convert some types
#
sub ConvertType($$)
{
    my ($arg, $wgl_func) = @_;
    my $ret = $arg->textContent();
    my @type = $arg->findnodes("./ptype");

    if (@type)
    {
        my $type = $type[0]->textContent();
        $ret =~ s/$type/$remap_types{$type}/ if defined $remap_types{$type};
    }
    if ($wgl_func && $ret =~ /([\w\s]+\*)( *)([\w\s]*)/)
    {
        $ret = $1 . "WIN32PTR " . $3;
    }
    return $ret;
}

#
# This functions generates the thunk for a given function.
#
sub GenerateThunk($$$)
{
    # Map values:
    # >= 0 : argument number of the array count
    # < -1 : fixed array count
    # -1   : function has an indirect output argument
    my %indirect_arg_functions =
        (
        # Indirect output
         "glGetPointerv" => -1,
         "glGetBufferPointerv" => -1,
         "glGetBufferPointervARB" => -1,
         "glGetNamedBufferPointerv" => -1,
         "glGetNamedBufferPointervEXT" => -1,
         "glGetPointerIndexedvEXT" => -1,
         "glGetPointeri_vEXT" => -1,
         "glGetPointervEXT" => -1,
         "glGetTexParameterPointervAPPLE" => -1,
         "glGetVariantPointervEXT" => -1,
         "glGetVertexArrayPointeri_vEXT" => -1,
         "glGetVertexArrayPointervEXT" => -1,
         "glGetVertexAttribPointerv" => -1,
         "glGetVertexAttribPointervARB" => -1,
         "glGetVertexAttribPointervNV" => -1,
        # Indirect input
         "glColorPointerListIBM" => 0,
         "glColorPointervINTEL" => 0,
         "glCompileShaderIncludeARB" => 1,
         "glCreateShaderProgramv" => 1,
         "glGetUniformIndices" => 1,
         "glListDrawCommandsStatesClientNV" => 6,
         "glMultiDrawElements" => 4,
         "glMultiDrawElementsBaseVertex" => 4,
         "glMultiDrawElementsEXT" => 4,
         "glMultiModeDrawElementsIBM" => 4,
         "glNormalPointervINTEL" => -3,
         "glSecondaryColorPointerListIBM" => 0,
         "glShaderSource" => 1,
         "glShaderSourceARB" => 1,
         "glTexCoordPointerListIBM" => 0,
         "glTexCoordPointervINTEL" => 0,
         "glTransformFeedbackVaryings" => 1,
         "glTransformFeedbackVaryingsEXT" => 1,
         "glVertexPointerListIBM" => 0,
         "glVertexPointervINTEL" => 0,
        );
    my %intptr_array_functions =
        (
         "glBindBuffersRange" => 2,
         "glBindVertexBuffers" => 1,
         "glDrawCommandsNV" => 4,
         "glDrawCommandsStatesNV" => 5,
         "glVDPAUMapSurfacesNV" => 0,
         "glVDPAUUnmapSurfacesNV" => 0,
         "glVertexArrayVertexBuffers" => 2,
        );

    my ($name, $func_ref, $prefix) = @_;
    my $call_arg = "";
    my $trace_call_arg = "";
    my $trace_arg = "";
    my $indirect_type = "";
    my $indirect_name = "";
    my $indirect_count = "";
    my @intptr_types;
    my @intptr_names;

    my $indirect_arg = $indirect_arg_functions{$name};
    my $intptr_arg = $intptr_array_functions{$name};
    my $wgl_func = $name =~ /^wgl[A-Z]\w+/;

    my $ret = get_func_proto( "%s WINAPI %s(%s)", $name, $func_ref, 0, 0 );
    for (my $i = 0; $i < @{$func_ref->[1]}; $i++) {
        my $arg = @{$func_ref->[1]}[$i];
        my $ptype = get_arg_type( $arg );
        my $pname = get_arg_name( $arg );
        my $param = $arg->textContent();
        if (defined $indirect_arg && ($param =~ /([\w\s]+\*)(\s*const|)\s*\*/)) {
            $indirect_type = $1 . $2;
            $indirect_name = $pname;
            if ($indirect_arg == -1) {
                $call_arg .= " &temp,";
            }
            else {
                $call_arg .= " temp,";
                $indirect_count = $indirect_arg >= 0 ? get_arg_name(@{$func_ref->[1]}[$indirect_arg]) : -$indirect_arg;
            }
        }
        elsif (defined $intptr_arg && ($param =~ /(GL[a-z]+)(ptr\w*|dpauSurfaceNV)\s*\*/))
        {
            $intptr_types[++$#intptr_types] = $1 . $2;
            $intptr_names[++$#intptr_names] = $pname;
            $call_arg .= sprintf(" temp%d,", $#intptr_types);
            $indirect_count = get_arg_name(@{$func_ref->[1]}[$intptr_arg]);
        }
        else {
            $call_arg .= " " . $pname . ",";
        }
        if ($param =~ /\*/ || $param =~ /\[/) {
            $trace_arg .= ", %p";
            $trace_call_arg .= ", " . $pname;
        } elsif (defined $arg_types{$ptype}) {
            my $format = ${$arg_types{$ptype}}[1];
            $trace_arg .= ", " . ($format =~ /^%/ ? $format : "%s");
            $trace_call_arg .= ", " . sprintf $format =~ /^%/ ? "%s" : $format, $pname;
        }
        else { printf "Unknown type %s in %s\n", $param, $name; }
    }
    $call_arg =~ s/,$/ /;
    $trace_arg =~ s/^, //;
    $ret .= "\n{\n";
    # special case for functions that take an HDC as first parameter
    if (@{$func_ref->[1]} && get_arg_type( ${$func_ref->[1]}[0] ) eq "HDC")
    {
        my $pname = get_arg_name( ${$func_ref->[1]}[0] );
        $ret .= "  const struct opengl_funcs *funcs = get_dc_funcs( $pname );\n";
        $ret .= "  TRACE( \"($trace_arg)\\n\"$trace_call_arg );\n" if $gen_traces;
        $ret .= "  if (!funcs || !funcs->$prefix.p_$name) return";
        $ret .= " 0" unless is_void_func( $func_ref );
        $ret .= ";\n";
    }
    else
    {
        $ret .= "  const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;\n";
        $ret .= sprintf("  %s HOSTPTR *temp = mirror_pointer_array((void**)%s, %s);\n", $indirect_type, $indirect_name, $indirect_count) if defined $indirect_arg && $indirect_arg != -1;
        $ret .= sprintf("  %s HOSTPTR temp = NULL;\n", $indirect_type) if defined $indirect_arg && $indirect_arg == -1;
        if (defined $intptr_arg) {
            for my $i (0 .. $#intptr_names) {
                $ret .= sprintf("  WINEGLHOST(%s) *temp%d = mirror_intptr_array(%s, %s);\n", $intptr_types[$i], $i, $intptr_names[$i], $indirect_count);
            }
        }
        $ret .= "  TRACE( \"($trace_arg)\\n\"$trace_call_arg );\n" if $gen_traces;
    }
    $ret .= "  ";
    $ret .= "return " unless is_void_func( $func_ref );
    $ret .= "ADDRSPACECAST(void*, " if is_pointer_func( $func_ref ) && !$wgl_func;
    $ret .= "funcs->$prefix.p_$name($call_arg)" . (is_pointer_func( $func_ref ) && !$wgl_func ? ");\n" : ";\n");
    $ret .= sprintf("  gl_temp_free(temp);\n") if defined $indirect_arg && $indirect_arg != -1;
    $ret .= sprintf("  *%s = ADDRSPACECAST(%s, temp);\n", $indirect_name, $indirect_type) if defined $indirect_arg && $indirect_arg == -1;
    if (defined $intptr_arg) {
        for my $i (0 .. $#intptr_names) {
            $ret .= sprintf("  gl_temp_free(temp%d);\n", $i);
        }
    }
    $ret .= "}\n\n";
    return $ret;
}

sub generate_null_func($$$)
{
    my ($name, $func_ref, $callconv) = @_;
    my $ret;

    return "" if $name eq "glDebugEntry";

    $ret = get_func_proto( "static %s$callconv null_%s(%s)", $name, $func_ref, 1, 0 );
    $ret .= " {";
    if ($name eq "glGetError")
    {
        $ret .= " return GL_INVALID_OPERATION;";
    }
    elsif (!is_void_func( $func_ref ))
    {
        $ret .= " return 0;";
    }
    $ret .= " }\n";
    return $ret;
}

sub generate_spec_entry($$)
{
    my ($name, $func) = @_;
    my $args=" ";
    foreach my $arg (@{$func->[1]})
    {
        my $ptype = get_arg_type( $arg );
        my $param = $arg->textContent();
        if ($param =~ /[[*]/) {
            $args .= "ptr ";
        } elsif (defined($arg_types{$ptype})) {
            $args .= "$@$arg_types{$ptype}[0] ";
        } else {
            die "No conversion for func $name type $param\n";
        }
    }
    $args = substr($args,1,-1);
    return "@ stdcall $_($args)";
}

sub is_void_func($)
{
    my $func = shift;
    return 0 if @{$func->[0]->findnodes("./ptype")};
    return $func->[0]->textContent() eq "void ";
}

sub is_pointer_func($)
{
    my $func = shift;
    return 0 if @{$func->[0]->findnodes("./ptype")};
    return $func->[0]->textContent() =~ /\w+\s*\*/;
}

sub get_arg_type($)
{
    my $p = shift;
    my @type = $p->findnodes("./ptype");
    return @type ? $type[0]->textContent() : "GLint";
}

sub get_arg_name($)
{
    my $p = shift;
    my @name = $p->findnodes("./name");
    return $name[0]->textContent();
}

sub get_func_proto($$$$$)
{
    my ($format, $name, $func, $convert_args, $test_wgl) = @_;
    die "unknown func $name" unless defined $func->[0];
    my $wgl_func = $test_wgl && index($name, "wgl") == 0;
    my $proto = $convert_args ? ConvertType( $func->[0], $wgl_func ) : $func->[0]->textContent();
    $proto =~ s/ $//;
    my $args = "";
    foreach my $arg (@{$func->[1]})
    {
        $args .= " " . ($convert_args ? ConvertType( $arg, $wgl_func ) : $arg->textContent()) . ",";
    }
    $args =~ s/,$/ /;
    $args ||= "void";
    return sprintf $format, $proto, $name, $args;
}

#
# Extract and checks the number of arguments
#
if (@ARGV > 1) {
    my $name0=$0;
    $name0=~s%^.*/%%;
    die "Usage: $name0 [version]\n";
}
my $version = $ARGV[0] || "1.1";
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;
} 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;
} else {
    die "Incorrect OpenGL version.\n";
}

#
# Fetch the registry files
#
my $url="https://raw.githubusercontent.com/KhronosGroup/OpenGL-Registry";
my $commit="03e1bfb87c4664d34dc7822fb591841eec747094";

-f "gl-$commit.xml" || system "wget", "-O", "gl-$commit.xml", "$url/$commit/xml/gl.xml" || die "cannot download gl.xml";
-f "wgl-$commit.xml" || system "wget", "-O", "wgl-$commit.xml", "$url/$commit/xml/wgl.xml" || die "cannot download wgl.xml";


#
# Then, create the list of all OpenGL functions using the registry
# files. 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 :
#
#  - XML node of the function prototype
#
#  - reference to an array of XML nodes giving the list of arguments (an empty array
#    for a 'void' function).
#
my %norm_functions;
my %ext_functions;
my %wgl_functions;
my %gl_enums;
my (%gl_types, @gl_types);  # also use an array to preserve declaration order

my %supported_wgl_functions =
    (
     "wglCopyContext" => 1,
     "wglCreateContext" => 1,
     "wglDeleteContext" => 1,
     "wglDescribePixelFormat" => 1,
     "wglGetPixelFormat" => 1,
     "wglGetProcAddress" => 1,
     "wglMakeCurrent" => 1,
     "wglSetPixelFormat" => 1,
     "wglShareLists" => 1,
     "wglSwapBuffers" => 1,
    );

my %remapped_wgl_functions =
    (
     "ChoosePixelFormat" => "wglChoosePixelFormat",
     "DescribePixelFormat" => "wglDescribePixelFormat",
     "GetPixelFormat" => "wglGetPixelFormat",
     "GetEnhMetaFilePixelFormat" => 0,
     "SetPixelFormat" => "wglSetPixelFormat",
     "SwapBuffers" => "wglSwapBuffers",
     "wglUseFontBitmaps" => 0,
     "wglUseFontOutlines" => 0,
    );

my @extra_wgl_functions = ( "wglGetDefaultProcAddress" );

my %supported_wgl_extensions =
   (
    "WGL_ARB_create_context" => 1,
    "WGL_ARB_create_context_no_error" => 1,
    "WGL_ARB_create_context_profile" => 1,
    "WGL_ARB_extensions_string" => 1,
    "WGL_ARB_make_current_read" => 1,
    "WGL_ARB_multisample" => 1,
    "WGL_ARB_pbuffer" => 1,
    "WGL_ARB_pixel_format" => 1,
    "WGL_ARB_framebuffer_sRGB" => 1,
    "WGL_ARB_pixel_format_float" => 1,
    "WGL_ARB_render_texture" => 1,
    "WGL_ATI_pixel_format_float" => 1,
    "WGL_EXT_create_context_es2_profile" => 1,
    "WGL_EXT_extensions_string" => 1,
    "WGL_EXT_framebuffer_sRGB" => 1,
    "WGL_EXT_pixel_format_packed_float" => 1,
    "WGL_EXT_swap_control" => 1,
    "WGL_EXT_swap_control_tear" => 1,
    "WGL_NV_float_buffer" => 1,
    "WGL_NV_render_depth_texture" => 1,
    "WGL_NV_render_texture_rectangle" => 1,
    "WGL_NV_vertex_array_range" => 1,
    "WGL_WINE_pixel_format_passthrough" => 1,
    "WGL_WINE_query_renderer" => 1,
   );

my %supported_apis =
    (
     "gl" => 1,
    );

sub is_supported_api($)
{
    my $api = shift;
    foreach my $i (split /\|/, $api)
    {
        return 1 if defined $supported_apis{$i};
    }
    return 0;
}

# Not listed for Apple and problematic to implement for 32on64
my %unsupported_functions =
    (
     "glEdgeFlagPointerListIBM" => 1,
     "glFogCoordPointerListIBM" => 1,
     "glIndexPointerListIBM" => 1,
     "glNormalPointerListIBM" => 1,
     "glReplacementCodePointerSUN" => 1,
     "glGetVkProcAddrNV" => 1,
     # We can probably implement the following, but they are part of GL_IBM_vertex_array_lists
     # which is not fully supported and not listed for Apple.
     "glColorPointerListIBM" => 1,
     "glSecondaryColorPointerListIBM" => 1,
     "glTexCoordPointerListIBM" => 1,
     "glVertexPointerListIBM" => 1
    );

my %hybrid_remap_types =
    (
     "GLintptr" => "INT64",
     "GLsizeiptr" => "INT64",
     "GLintptrARB" => "INT64",
     "GLsizeiptrARB" => "INT64",
     "GLvdpauSurfaceNV" => "WINEGLHOST(GLintptr)",
    );

# some functions need a hand-written wrapper
sub needs_wrapper($$)
{
    my %funcs =
        (
         "glDebugEntry" => 1,
         "glDebugMessageCallback" => 1,
         "glDebugMessageCallbackAMD" => 1,
         "glDebugMessageCallbackARB" => 1,
         "glGetIntegerv" => 1,
         "glGetString" => 1,
         "glGetStringi" => 1,
         "wglGetCurrentReadDCARB" => 1,
        );
    my ($name, $func) = @_;

    return 1 if defined $funcs{$name};
    # check if return value needs special handling
    (my $type = $func->[0]->textContent()) =~ s/ $//;
    return 1 if defined $specially_handled_types{$type};
    # check if one of the arguments needs special handling
    foreach (@{$func->[1]})
    {
        $type = get_arg_type( $_ );
        return 1 if defined $specially_handled_types{$type};
    }
    return 0;
}

sub parse_file($)
{
    my $file = shift;
    my $xml = XML::LibXML->load_xml( location => $file );
    my %functions;
    my %enums;

    # save all functions
    foreach my $command ($xml->findnodes("/registry/commands/command"))
    {
        my $proto = @{$command->findnodes("./proto")}[0];
        my $name = @{$command->findnodes("./proto/name")}[0];
        $proto->removeChild( $name );
        my @params = $command->findnodes("./param");
        $functions{$name->textContent()} = [ $proto, \@params ] unless defined $unsupported_functions{$name};
    }

    # save all enums
    foreach my $enum ($xml->findnodes("/registry/enums/enum"))
    {
        $enums{$enum->{name}} = $enum->{value};
    }

    # save all types
    foreach my $type ($xml->findnodes("/registry/types/type"))
    {
        next if $type->{api};
        my $name = @{$type->findnodes("./name")}[0];
        next unless $name;
        $name = $name->textContent;
        push @gl_types, $name unless $gl_types{$name};
        $gl_types{$name} = $type;
    }

    # generate norm functions
    foreach my $feature ($xml->findnodes("/registry/feature"))
    {
        if ($feature->{api} eq "wgl")
        {
            foreach my $cmd ($feature->findnodes("./require/command"))
            {
                my $name = $cmd->{name};
                if (defined $remapped_wgl_functions{$name})
                {
                    next unless $remapped_wgl_functions{$name};
                    $name = $remapped_wgl_functions{$name};
                }
                $wgl_functions{$name} = $functions{$cmd->{name}};
            }
            foreach my $name (@extra_wgl_functions)
            {
                $wgl_functions{$name} = $functions{$name} if defined $functions{$name};
            }
        }
        next unless defined $norm_categories{$feature->{name}};
        foreach my $cmd ($feature->findnodes("./require/command"))
        {
            $norm_functions{$cmd->{name}} = $functions{$cmd->{name}};
        }
        foreach my $enum ($feature->findnodes("./require/enum"))
        {
            $gl_enums{$enum->{name}} = $enums{$enum->{name}};
        }
    }

    # generate extension functions from norm functions, if they are newer than the category
    foreach my $feature ($xml->findnodes("/registry/feature"))
    {
        next if defined $norm_categories{$feature->{name}};
        next unless is_supported_api( $feature->{api} );
        foreach my $cmd ($feature->findnodes("./require/command"))
        {
            my $name = $cmd->{name};
            next if $norm_functions{$name} || $ext_functions{$name};
            $ext_functions{$name} = [ $functions{$name}[0], $functions{$name}[1], [ $feature->{name} ] ];
        }
        foreach my $enum ($feature->findnodes("./require/enum"))
        {
            $gl_enums{$enum->{name}} = $enums{$enum->{name}};
        }
    }

    # generate extension functions
    foreach my $ext ($xml->findnodes("/registry/extensions/extension"))
    {
        if ($ext->{supported} eq "wgl")
        {
            next unless defined $supported_wgl_extensions{$ext->{name}};
            foreach my $cmd ($ext->findnodes("./require/command"))
            {
                my $name = $cmd->{name};
                $ext_functions{$name} = [ $functions{$name}[0], $functions{$name}[1], [ $ext->{name} ] ];
            }
            foreach my $enum ($ext->findnodes("./require/enum"))
            {
                $gl_enums{$enum->{name}} = $enums{$enum->{name}};
            }
            next;
        }
        next unless is_supported_api( $ext->{supported} );
        foreach my $req ($ext->findnodes("./require"))
        {
            next unless !$req->{api} || $req->{api} eq "gl";
            foreach my $cmd ($req->findnodes("./command"))
            {
                my $name = $cmd->{name};
                next if $norm_functions{$name} || defined $unsupported_functions{$name};
                if (!$ext_functions{$name})
                {
                    $ext_functions{$name} = [ $functions{$name}[0], $functions{$name}[1], [ $ext->{name} ] ];
                }
                else
                {
                    push @{$ext_functions{$name}->[2]}, $ext->{name};
                }
            }
        }
        foreach my $enum ($ext->findnodes("./require/enum"))
        {
            $gl_enums{$enum->{name}} = $enums{$enum->{name}};
        }
    }
}

parse_file( "gl-$commit.xml" );
parse_file( "wgl-$commit.xml" );
parse_file( "winegl.xml" );

#
# Get the current wgl_driver.h version
#
my $wgl_version = 0;
open HEADER, "<$wgl_driver_file" or die "cannot open $wgl_driver_file";
while (<HEADER>)
{
    next unless /^#define WINE_WGL_DRIVER_VERSION (\d+)/;
    $wgl_version = $1;
    last;
}
close HEADER;

#
# Generate the wgl_driver.h file
#
open HEADER, ">$wgl_driver_file" or die "cannot create $wgl_driver_file";
print HEADER "/* Automatically generated from http://www.opengl.org/registry files; DO NOT EDIT! */\n\n";
print HEADER "#ifndef __WINE_WGL_DRIVER_H\n";
print HEADER "#define __WINE_WGL_DRIVER_H\n\n";
print HEADER "#ifndef WINE_GLAPI\n";
print HEADER "#define WINE_GLAPI\n";
print HEADER "#endif\n\n";

printf HEADER "#define WINE_WGL_DRIVER_VERSION %u\n\n", $wgl_version + 1;

print HEADER "struct wgl_context;\n";
print HEADER "struct wgl_pbuffer;\n\n";

print HEADER "#ifdef __i386_on_x86_64__\n";
print HEADER "#ifdef WINE_GL_WIN32\n";
print HEADER "#define WINEGLDEF(x) x\n";
print HEADER "#define WINE_OPENGL_FUNCS _wine_win32_opengl_funcs\n";
print HEADER "#else\n";
print HEADER "#define WINEGLDEF(x) _wine_host_##x\n";
print HEADER "#define WINE_OPENGL_FUNCS opengl_funcs\n";
print HEADER "#include \"hostaddrspace_enter.h\"\n";
print HEADER "#endif\n";
print HEADER "#else\n";
print HEADER "#define WINEGLDEF(x) x\n";
print HEADER "#define WINE_OPENGL_FUNCS opengl_funcs\n";
print HEADER "#endif\n\n";

print HEADER "struct opengl_funcs_gl\n{\n";
foreach (sort keys %norm_functions)
{
    next if $_ eq "glDebugEntry";
    print HEADER get_func_proto("    %-10s (WINE_GLAPI *p_%s)(%s);\n", $_, $norm_functions{$_}, 1, 0);
}
print HEADER "};\n\n";

print HEADER "struct opengl_funcs_ext\n{\n";
foreach (sort keys %ext_functions)
{
    print HEADER get_func_proto("    %-10s (WINE_GLAPI *p_%s)(%s);\n", $_, $ext_functions{$_}, 1, 1);
}
print HEADER "};\n\n";

print HEADER "#ifndef WINE_GL_WIN32\n";
print HEADER "#include \"hostaddrspace_exit.h\"\n";
print HEADER "#endif\n\n";

print HEADER "struct opengl_funcs_wgl\n{\n";
foreach (sort keys %wgl_functions)
{
    next unless defined $supported_wgl_functions{$_};
    print HEADER get_func_proto("        %-10s (WINAPI *p_%s)(%s);\n", $_, $wgl_functions{$_}, 1, 0);
}
print HEADER "};\n\n";

print HEADER "struct WINE_OPENGL_FUNCS\n{\n";
print HEADER "    struct opengl_funcs_wgl wgl;\n";
print HEADER "    struct opengl_funcs_gl gl;\n";
print HEADER "    struct opengl_funcs_ext ext;\n";
print HEADER "};\n\n";

print HEADER "#define ALL_WGL_FUNCS";
foreach (sort keys %norm_functions)
{
    next if $_ eq "glDebugEntry";
    printf HEADER " \\\n    USE_GL_FUNC(\%s)", $_;
}
print HEADER "\n\n";

print HEADER "extern struct opengl_funcs * CDECL __wine_get_wgl_driver( HDC hdc, UINT version );\n\n";
print HEADER "#endif /* __WINE_WGL_DRIVER_H */\n";
close HEADER;

#
# Generate the wgl.h file
#
open HEADER, ">$wgl_file" or die "cannot create $wgl_file";
print HEADER "/* Automatically generated from http://www.opengl.org/registry files; DO NOT EDIT! */\n\n";
print HEADER "#ifndef __WINE_WGL_H\n";
print HEADER "#define __WINE_WGL_H\n\n";

print HEADER "#include <stdint.h>\n\n";

print HEADER "#ifndef GLAPIENTRY\n";
print HEADER "#define GLAPIENTRY __stdcall\n";
print HEADER "#endif\n\n";

print HEADER "#ifdef __i386_on_x86_64__\n";
print HEADER "#define WINEGLHOST(x) _wine_host_##x\n";
print HEADER "typedef INT64 (CALLBACK *HOSTPTR _wine_host_PROC)(void);\n";
print HEADER "#else\n";
print HEADER "#define WINEGLHOST(x) x\n";
print HEADER "#endif\n\n";

print HEADER "typedef signed char khronos_int8_t;\n";
print HEADER "typedef unsigned char khronos_uint8_t;\n";
print HEADER "typedef signed short int khronos_int16_t;\n";
print HEADER "typedef unsigned short int khronos_uint16_t;\n";
print HEADER "typedef float khronos_float_t;\n";
print HEADER "typedef int32_t khronos_int32_t;\n";
print HEADER "typedef uint32_t khronos_uint32_t;\n\n";

foreach (@gl_types)
{
    my $type = $gl_types{$_}->textContent();
    foreach my $t (keys %khronos_types) { $type =~ s/\s(\Q$t\E)\s/ $khronos_types{$t} /; }
    printf HEADER $type . "\n";
}
print HEADER "#ifdef __i386_on_x86_64__\n";
foreach (@gl_types)
{
    if (defined $hybrid_remap_types{$_}) {
        printf HEADER "typedef %s WINEGLHOST(%s);\n", $hybrid_remap_types{$_}, $_;
    }
}
print HEADER "#endif\n\n";

my $maxlen = 1;
foreach (keys %gl_enums) { $maxlen = length($_) if length($_) > $maxlen; }
foreach (sort keys %gl_enums)
{
    printf HEADER "#define %-*s %s\n", $maxlen, $_, $gl_enums{$_};
}
print HEADER "\n";

foreach (sort keys %norm_functions)
{
    printf HEADER "%s;\n", get_func_proto("%-10s GLAPIENTRY %s(%s)", $_, $norm_functions{$_}, 0, 0);
}

print HEADER "\n#endif /* __WINE_WGL_H */\n";
close HEADER;

#
# Now, generate the output files. First, the spec file.
#
open(SPEC, ">$spec_file") or die "cannot create $spec_file";

foreach (sort keys %norm_functions) {
    printf SPEC "%s\n", generate_spec_entry( $_, $norm_functions{$_} );
}
foreach (sort keys %wgl_functions) {
    printf SPEC "%s\n", generate_spec_entry( $_, $wgl_functions{$_} );
}

close(SPEC);

my $file_header =
"/* Automatically generated from http://www.opengl.org/registry files; DO NOT EDIT! */\n\n" .
"#include \"config.h\"\n" .
"#include <stdarg.h>\n" .
"#include \"winternl.h\"\n" .
"#include \"opengl_ext.h\"\n" .
"#include \"wine/debug.h\"\n\n";

$file_header .= "WINE_DEFAULT_DEBUG_CHANNEL(opengl);\n\n" if $gen_traces;

#
# After the spec file, the opengl_norm.c file
#
open(NORM, ">$norm_file") or die "cannot create $norm_file";
print NORM $file_header;

foreach (sort keys %norm_functions) {
    next if needs_wrapper( $_, $norm_functions{$_} );
    print NORM GenerateThunk($_, $norm_functions{$_}, "gl");
}

foreach (sort keys %wgl_functions) {
    next unless defined $supported_wgl_functions{$_};
    print NORM generate_null_func($_, $wgl_functions{$_}, " WINAPI");
}

print NORM "\n#include \"wine/hostaddrspace_enter.h\"\n\n";

foreach (sort keys %norm_functions) {
    print NORM generate_null_func($_, $norm_functions{$_}, "");
}
my $exited_host = 0;
foreach (sort keys %ext_functions) {
    if (!$exited_host && index($_, "wgl") == 0) {
        print NORM "\n#include \"wine/hostaddrspace_exit.h\"\n\n";
        $exited_host = 1;
    }
    print NORM generate_null_func($_, $ext_functions{$_}, "");
}

print NORM "\nstruct opengl_funcs null_opengl_funcs =\n{\n    {\n";
foreach (sort keys %wgl_functions)
{
    next unless defined $supported_wgl_functions{$_};
    print NORM "        null_$_,\n";
}
print NORM "    },\n    {\n";
foreach (sort keys %norm_functions) { print NORM "        null_$_,\n" unless $_ eq "glDebugEntry"; }
print NORM "    },\n    {\n";
foreach (sort keys %ext_functions) { print NORM "        null_$_,\n"; }
print NORM "    }\n};\n";

close(NORM);

#
# Finally, more complex, the opengl_ext.c file
#
open(EXT, ">$ext_file") or die "cannot create $ext_file";
print EXT $file_header;

# The thunks themselves....
my $count = keys %ext_functions;
my $wrappers = "";
print EXT "const int extension_registry_size = $count;\n\n";
foreach (sort keys %ext_functions) {
    if (needs_wrapper( $_, $ext_functions{$_} ))
    {
        $wrappers .= get_func_proto("extern %s WINAPI %s(%s) DECLSPEC_HIDDEN;\n", $_, $ext_functions{$_}, 0, 0);
    }
    else
    {
        print EXT "static " . GenerateThunk($_, $ext_functions{$_}, "ext");
    }
}
print EXT $wrappers;

# Then the table giving the string <-> function correspondence */
print EXT "\nconst OpenGL_extension extension_registry[$count] = {\n";
my $i = 0;
foreach (sort keys %ext_functions) {
    my $func_ref = $ext_functions{$_};
    printf EXT "  { \"%s\", \"%s\", %s }", $_, join(" ", sort @{$func_ref->[2]}), $_;
    if ($i != $count-1) {
        print EXT ",";
    }
    $i++;
    print EXT "\n";
}
print EXT "};\n";

close(EXT);
