Exemple #1
0
        public Generator(CSSpec spec, Offsets offsets)
        {
            this.spec    = spec;
            this.offsets = offsets;
            //Console.WriteLine("{0} enums, {1} included", spec.Enums.Count, spec.IncludedEnums.Count);
            //Console.WriteLine("{0} commands, {1} included", spec.Commands.Count, spec.IncludedCommands.Count);
            //Console.WriteLine("{0} structs, {1} included", spec.Structs.Count, spec.IncludedTypes.Count);

            time = DateTime.Now;
        }
Exemple #2
0
 public string GetType(string type)
 {
     type = CSSpec.GetType(type);
     if (type.Contains("*"))
     {
         return("IntPtr");
     }
     if (type.Contains("PFN_"))
     {
         return("IntPtr");
     }
     return(type);
 }
Exemple #3
0
        static void Main(string[] args)
        {
            List <string> extensions = new List <string> {
                "VK_KHR_surface",
                "VK_KHR_swapchain",
                //"VK_KHR_display",
                //"VK_KHR_display_swapchain",
                //"VK_KHR_win32_surface",
                "VK_EXT_debug_report",
            };
            //string output = @"..\..\..\..\CSharpGameLibrary\CSharpGameLibrary\Vulkan";
            //string output2 = @"..\..\..\..\CSharpGameLibrary\CSharpGameLibrary\Vulkan\Unmanaged";
            string output = "output";

            Offsets offsets;

            using (var stream = File.Open("offsets.txt", FileMode.Open)) {
                offsets = new Offsets(stream);
            }

            if (!Directory.Exists(output))
            {
                Directory.CreateDirectory(output);
            }
            Spec  spec;
            Patch patch;

            using (var reader = File.Open("vk.xml", FileMode.Open)) {
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                spec = new Spec(doc, 1, 0, extensions);
            }

            //CPP_Offsets cpp = new CPP_Offsets(spec);
            //CS_Offsets cs = new CS_Offsets(spec, output);

            using (var reader = File.Open("patch.xml", FileMode.Open)) {
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                patch = new Patch(doc);
            }
            CSSpec    csSpec = new CSSpec(spec, patch);
            Generator g      = new Generator(csSpec, offsets);

            g.WriteEnums(output, "CSGL.Vulkan");
            g.WriteDelegates(output, "CSGL.Vulkan.Unmanaged");
            g.WriteCommands(output, "CSGL.Vulkan.Unmanaged");
            g.WriteStructs(output, "CSGL.Vulkan.Unmanaged");
            //g.WriteLoader(output, "CSGL.Vulkan.Unmanaged");
            //g.WriteFunctionNames(output);
        }
Exemple #4
0
 string GetType(string input)
 {
     if (input == "char*")
     {
         return("byte[]");
     }
     input = CSSpec.GetType(input);
     if (input.Contains("*"))
     {
         string type = input.Substring(0, input.Length - 1);
         if (CSSpec.IsPrimitive(type))
         {
             return("ref " + type);
         }
     }
     if (input.Contains("*"))
     {
         return("IntPtr");
     }
     return(input);
 }
Exemple #5
0
        string GetType(string input)
        {
            if (input == "char**")
            {
                return("string[]");
            }
            if (input == "void*")
            {
                return("IntPtr");
            }

            if (input.Contains("*"))
            {
                string underType = input.Substring(0, input.Length - 1);
                UnderlyingType = GetType(underType);
                Array          = true;
                return(UnderlyingType + "[]");
            }

            return(CSSpec.GetType(input));
        }
Exemple #6
0
        public void WriteStructs(string output, string _namespace)
        {
            string path = Path.Combine(output, "structs.cs");

            using (var writer = File.CreateText(path)) {
                writer.WriteLine("//autogenerated on {0}", time.ToString());
                writer.WriteLine("using System;");
                writer.WriteLine("using System.Runtime.InteropServices;");
                writer.WriteLine();
                writer.WriteLine("namespace {0} {{", _namespace);

                foreach (var s in spec.Structs)
                {
                    if (s.Handle)
                    {
                        WriteHandle(writer, s);
                        continue;
                    }

                    if (offsets.Contains(s.Name))
                    {
                        writer.WriteLine("    [StructLayout(LayoutKind.Explicit, Size = {0})]",
                                         offsets[s.Name].size);
                    }
                    writer.Write("    public");
                    if (s.Unsafe)
                    {
                        writer.Write(" unsafe");
                    }
                    writer.WriteLine(" partial struct {0} {{", s.Name);

                    for (int i = 0; i < s.Fields.Count; i++)
                    {
                        var    f    = s.Fields[i];
                        string type = f.Type;
                        if (spec.FlagsMap.ContainsKey(type))
                        {
                            type = spec.FlagsMap[type];
                        }

                        int fieldSize = CSSpec.GetSize(type);

                        if (offsets.Contains(type))
                        {
                            fieldSize = offsets[type].size;
                        }


                        if (f.ArraySize > 0 && offsets.Contains(s.Name) && type != "byte")
                        {
                            Offset o = offsets[s.Name];
                            for (int j = 0; j < f.ArraySize; j++)
                            {
                                writer.WriteLine("        [FieldOffset({0})]", offsets[s.Name].offsets[i] + (j * fieldSize));
                                writer.WriteLine("        public {0} {1}_{2};", type, f.Name, j);
                            }

                            writer.WriteLine();
                            writer.WriteLine("        public {0} Get{1}{2}(int i) {{", f.Type, char.ToUpper(f.Name[0]), f.Name.Substring(1));
                            writer.WriteLine("            switch (i) {");
                            for (int j = 0; j < f.ArraySize; j++)
                            {
                                writer.WriteLine("                case {0}: return {1}_{0};", j, f.Name);
                            }
                            writer.WriteLine("                default: return default({0});", f.Type);
                            writer.WriteLine("            }");
                            writer.WriteLine("        }");

                            writer.WriteLine();
                            writer.WriteLine("        public void Set{1}{2}(int i, {0} value) {{", f.Type, char.ToUpper(f.Name[0]), f.Name.Substring(1));
                            writer.WriteLine("            switch (i) {");
                            for (int j = 0; j < f.ArraySize; j++)
                            {
                                writer.WriteLine("                case {0}: {1}_{0} = value; break;", j, f.Name);
                            }
                            writer.WriteLine("                default: return;", f.Type);
                            writer.WriteLine("            }");
                            writer.WriteLine("        }");
                        }
                        else
                        {
                            if (offsets.Contains(s.Name))
                            {
                                writer.WriteLine("        [FieldOffset({0})]", offsets[s.Name].offsets[i]);
                            }
                            writer.WriteLine("        public {0} {1};", type, f.Name);
                        }
                    }

                    writer.WriteLine("    }");
                    writer.WriteLine();
                }

                writer.WriteLine("}");
            }
        }