Exemple #1
0
        public void Generate(string outputFileName, IGenerator generator, IGLSpecification spec, GLFeature feature, GLFeature.GLProfile profile, string namesapceName)
        {
            string outputDir = feature.Name;

            if (!Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }
            using (var fs = new FileStream(Path.Combine(outputDir, outputFileName), FileMode.Create))
                using (var streamWriter = new StreamWriter(fs))
                    using (var indentedTextWriter = new IndentedTextWriter(streamWriter))
                    {
                        indentedTextWriter.WriteLine("using System;");
                        indentedTextWriter.WriteLine("using System.Runtime.InteropServices;");


                        indentedTextWriter.WriteLine("namespace OpenTK.Graphics" + (string.IsNullOrWhiteSpace(namesapceName) ? string.Empty : "." + namesapceName));
                        indentedTextWriter.WriteLine("{");
                        using (indentedTextWriter.Indentation())
                        {
                            generator.Generate(indentedTextWriter, spec, feature, profile);
                        }

                        indentedTextWriter.WriteLine("}");
                    }
        }
Exemple #2
0
        public void LoadPart(IGLSpecification context, XElement rootNode)
        {
            const string UNKNOWN_GROUP_NAME = "UNKNOWN_GROUP";
            var          enumTypes          = context.Enums;

            var unknownEnumGroup = new GLEnum(UNKNOWN_GROUP_NAME, null);

            enumTypes.Add(UNKNOWN_GROUP_NAME, unknownEnumGroup);

            foreach (var e in rootNode.Elements("enums"))
            {
                var vendorName   = e.Attribute("vendor")?.Value;
                var parentGroups = e.Attribute("group");
                foreach (var enumEntry in e.Elements("enum"))
                {
                    var enumEntryName               = enumEntry.Attribute("name")?.Value;
                    var enumEntryValue              = enumEntry.Attribute("value")?.Value;
                    var parentGroupValue            = parentGroups?.Value;
                    var enumGroupValue              = enumEntry.Attribute("group")?.Value;
                    IEnumerable <string> enumGroups = (enumGroupValue)?.Split(',');
                    if (enumGroups != null)
                    {
                        if (parentGroupValue != null && !enumGroups.Contains(parentGroupValue))
                        {
                            enumGroups = enumGroups.Append(parentGroupValue);
                        }
                    }
                    else
                    {
                        enumGroups = new[] { parentGroupValue ?? UNKNOWN_GROUP_NAME };
                    }

                    if (enumEntryName == null || enumEntryValue == null)
                    {
                        throw new ArgumentNullException();
                    }

                    foreach (var enumGroup in enumGroups)
                    {
                        if (!enumTypes.TryGetValue(enumGroup, out var enumType))
                        {
                            enumType = new GLEnum(enumGroup, vendorName);
                            enumTypes.Add(enumGroup, enumType);
                        }

                        enumType.AddEntry(enumEntryName, ConvertToUInt64(enumEntryValue));
                    }
                }
            }
        }
Exemple #3
0
        public void LoadPart(IGLSpecification context, XElement rootNode)
        {
            var methodTypes = context.Methods;

            var commands = rootNode.Elements("commands");

            foreach (var c in commands)
            {
                foreach (var command in c.Elements("command"))
                {
                    var prototypeBase  = command.Element("proto");
                    var methodName     = prototypeBase.Element("name")?.Value;
                    var returnTypeName = prototypeBase.Element("ptype")?.Value
                                         ?? prototypeBase.GetXmlText(element => string.Empty);//string.Join("", prototypeBase.Elements().Select(x => x.Value));

                    if (methodName == null)
                    {
                        throw new ArgumentNullException();
                    }

                    returnTypeName = returnTypeName.Trim();

                    if (string.IsNullOrEmpty(returnTypeName))
                    {
                        throw new ArgumentException();
                    }

                    var returnType = context.GetType(returnTypeName);

                    var method = new GLMethod(returnType, methodName);

                    foreach (var p in command.Elements("param"))
                    {
                        var group = p.Attribute("group")?.Value;

                        var paramName = p.Element("name")?.Value;

                        var paramTypeName = p.GetXmlText(element => element.Name == "name" ? string.Empty : element.Value);

                        var paramType = context.GetType(paramTypeName);

                        method.Parameters.Add(new GLParameter(paramType, paramName));
                    }

                    methodTypes.Add(methodName, method);
                }
            }
        }
Exemple #4
0
        public void Generate(IndentedTextWriter indentedTextWriter, IGLSpecification spec, GLFeature feature, GLFeature.GLProfile profile)
        {
            var collected = feature.CollectFeatures(profile.Name);

            foreach (var e in spec.Enums.Values)
            {
                if (e.Name == "SpecialNumbers")
                {
                    GenerateSpecialNumbers(indentedTextWriter, e, feature, collected);
                }
                else
                {
                    GenerateEnum(indentedTextWriter, e, feature, collected);
                }
            }
        }
Exemple #5
0
        public void LoadPart(IGLSpecification spec, XElement root)
        {
            var features = spec.Features;

            foreach (var f in root.Elements("feature"))
            {
                var feature = new GLFeature(f.Attribute("api")?.Value, Version.Parse(f.Attribute("number")?.Value), f.Attribute("name")?.Value);

                foreach (var require in f.Elements("require"))
                {
                    string profileName = require.Attribute("profile")?.Value;
                    var    profile     = feature.GetOrCreateProfile(profileName);
                    LoadStuff(profile.Additions, require);
                }

                foreach (var removal in f.Elements("remove"))
                {
                    string profileName = removal.Attribute("profile")?.Value;
                    var    profile     = feature.GetOrCreateProfile(profileName);
                    LoadStuff(profile.Removals, removal);
                }

                features.Add(feature);
            }

            features.Sort(((a, b) =>
            {
                var apiCompare = string.Compare(a.Api, b.Api, StringComparison.Ordinal);
                return(apiCompare == 0 ? a.Number.CompareTo(b.Number) : apiCompare);
            }));

            var previousFeature = features[0];

            for (var i = 1; i < features.Count; i++)
            {
                var feature = features[i];
                if (previousFeature.Api == feature.Api)
                {
                    feature.InitializePreviousFeature(previousFeature);
                }
                previousFeature = feature;
            }
        }
        public void LoadPart(IGLSpecification context, XElement root)
        {
            var basicTypes = context.BasicTypes;
            var types      = root.Elements("types");

            foreach (var tps in types)
            {
                foreach (var t in tps.Elements("type"))
                {
                    var typeDef = t.GetXmlText();

                    var multiTypeDefs = PreprocessRegion(context, typeDef);
                    if (multiTypeDefs == null)
                    {
                        continue;
                    }
                    foreach (var(region, condition) in multiTypeDefs)
                    {
                        if (!ExtractTypeDef(region, out typeDef, out var typeName))
                        {
                            continue;
                        }
                        var basicType = new GLTypeDef(typeName, new GLTypeReference(context, typeDef));

                        switch (condition)
                        {
                        case "TRUE":
                            basicTypes.Add(typeName, basicType);
                            break;

                        case "FALSE":
                            break;

                        default:
                            AddConditionalTypeDef(context, basicType, condition);
                            break;
                        }
                    }
                }
            }
        }
        private static void AddConditionalTypeDef(IGLSpecification context, GLTypeDef basicType, string condition)
        {
            var basicTypes = context.BasicTypes;
            GLConditionalTypeDef conditionalTypeDef;

            if (basicTypes.TryGetValue(basicType.Name, out var alreadyExisting))
            {
                conditionalTypeDef = alreadyExisting as GLConditionalTypeDef;
                if (conditionalTypeDef == null)
                {
                    throw new KeyNotFoundException();
                }
            }
            else
            {
                conditionalTypeDef = new GLConditionalTypeDef(basicType.Name);

                basicTypes.Add(basicType.Name, conditionalTypeDef);
            }

            conditionalTypeDef.TypeRefs.Add(new GLConditionalTypeDef.ConditionalTypeRef(basicType, condition));
        }
Exemple #8
0
        public void Generate(IGLSpecification spec, GLFeature feature)
        {
            using (var fs = new FileStream(Path.Combine(feature.Name, "OpenGL.csproj"), FileMode.Create))
                using (var str = new StreamWriter(fs))
                {
                    str.WriteLine("<Project Sdk=\"Microsoft.NET.Sdk\">");
                    str.WriteLine("    <PropertyGroup>");
                    str.WriteLine("        <TargetFrameworks>netstandard2.0</TargetFrameworks>");
                    str.WriteLine("        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
                    str.WriteLine("    </PropertyGroup>");
                    str.WriteLine("</Project>");
                }

            foreach (var(profileName, profile) in feature.Profiles)
            {
                const string ext    = ".cs";
                const string prefix = "GL.";

                var profileFileAddition = string.IsNullOrEmpty(profileName) ? string.Empty : ("." + profileName);

                _namespaceGenerator.Generate(prefix + "Enums" + profileFileAddition + ext, _enumGenerator, spec, feature, profile, profileName);
                _namespaceGenerator.Generate(prefix + "Methods" + profileFileAddition + ext, _methodGenerator, spec, feature, profile, string.Empty);
            }
        }
Exemple #9
0
 public GLTypeReference(IGLSpecification specification, string name)
 {
     Context = specification;
     Name    = name;
 }
Exemple #10
0
        public void Generate(IndentedTextWriter indentedTextWriter, IGLSpecification spec, GLFeature feature, GLFeature.GLProfile profile)
        {
            var collected = feature.CollectFeatures(profile.Name);
            List <(GLMethod m, string name)> delegates = new List <(GLMethod m, string name)>();

            foreach (var t in spec.Types.Values.OfType <GLTypeDef>())
            {
                var resolved = t.OriginalType.Resolved();
                if (resolved.IsMethod)
                {
                    var methodType = (GLMethod)resolved;
                    if (string.IsNullOrEmpty(profile.Name)) // Only add delegates in base profile
                    {
                        delegates.Add((methodType, t.Name));
                    }
                    continue;
                }

                while (resolved.IsTypeDef)
                {
                    resolved = ((GLTypeDef)resolved).OriginalType.Resolved();
                }
                indentedTextWriter.WriteLine("using " + t.Name + " = " + resolved + ";");
            }
            indentedTextWriter.WriteLine("public static unsafe partial class GL");
            indentedTextWriter.WriteLine("{");
            using (indentedTextWriter.Indentation())
            {
                foreach (var(del, delName) in delegates)
                {
                    indentedTextWriter.Write("public delegate " + del.ReturnTypeBase + " " + delName + "(");
                    bool first = true;
                    foreach (var p in del.Parameters)
                    {
                        if (!first)
                        {
                            indentedTextWriter.Write(", ");
                        }
                        var pType = p.Type;
                        if (pType.IsConst)
                        {
                            indentedTextWriter.Write("[Const]");
                            pType = ((GLConstType)pType).ElementType;
                        }
                        indentedTextWriter.Write(pType + " " + p.Name);
                        first = false;
                    }
                    indentedTextWriter.WriteLine(");");
                }

                if (!string.IsNullOrEmpty(profile.Name))
                {
                    indentedTextWriter.WriteLine("public static class " + profile.Name);
                    indentedTextWriter.WriteLine("{");
                    indentedTextWriter.Indent++;
                }



                foreach (var e in spec.Methods.Values)
                {
                    GenerateMethod(indentedTextWriter, e, feature, collected);
                }

                if (!string.IsNullOrEmpty(profile.Name))
                {
                    indentedTextWriter.Indent--;
                    indentedTextWriter.WriteLine("}");
                }
            }
            indentedTextWriter.WriteLine("}");
        }