private static void WriteMemberSymbolicCount(CsCodeWriter cw, TypeNameMappings tnm, MemberSpec member, ConstantDefinition[] constants) { if (!CanUseFixed(member.Type.MapTypeSpec(tnm))) { int count = GetSymbolValue(member.ElementCountSymbolic, constants); for (int i = 0; i < count; i++) { WriteMember(cw, tnm, member, "_" + i); } } else { if (!string.IsNullOrEmpty(member.Comment)) { cw.WriteLine($"///<summary>{member.Comment}</summary>"); } string mappedSymbolicName = EnumHelpers.GetPrettyEnumName(member.ElementCountSymbolic, "VK_"); cw.WriteLine($"public fixed {member.Type.MapTypeSpec(tnm)} {Util.NormalizeFieldName(member.Name)}[(int)VulkanNative.{mappedSymbolicName}];"); } }
private static void GenerateAllTypes(VulkanSpecification spec, TypeNameMappings tnm, string path) { using (StreamWriter sw = File.CreateText(Path.Combine(path, "Structures.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(sw); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Structures, structure => { StructureHelpers.WriteStructure(cw, structure, tnm, spec.Constants); }); } } } using (StreamWriter enumWriter = File.CreateText(Path.Combine(path, "Enums.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(enumWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Enums, enumDef => { EnumHelpers.WriteEnum(cw, enumDef, tnm); }); } } } CommandDefinition[] allVariants = spec.Commands.SelectMany(cd => VariantGenerator.GenerateVariants(cd)).ToArray(); CommandDefinition[] allCommandsWithVariants = spec.Commands.Concat(allVariants).OrderBy(cd => cd.Name).ToArray(); using (StreamWriter commandWriter = File.CreateText(Path.Combine(path, "Commands.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(commandWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) using (cw.PushBlock("public static unsafe partial class VulkanNative")) { SpaceSeparatedList(cw, spec.Constants, constant => { ConstantHelpers.WriteConstant(cw, tnm, constant); }); cw.WriteLine(); SpaceSeparatedList(cw, allCommandsWithVariants, command => { CommandHelpers.WriteCommand(cw, tnm, command); }); cw.WriteLine(); using (cw.PushBlock("private static void LoadFunctionPointers()")) { foreach (CommandDefinition command in spec.Commands) { if (Configuration.GenerateCalliStubs) { cw.WriteLine($"{command.Name}_ptr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); } else { cw.WriteLine($"IntPtr {command.Name}_nativePtr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); using (cw.PushBlock($"if ({command.Name}_nativePtr != IntPtr.Zero)")) { cw.WriteLine($"{command.Name}_ptr = Marshal.GetDelegateForFunctionPointer<{command.Name}_delegate>({command.Name}_nativePtr);"); } using (cw.PushBlock("else")) { string invocation = string.Join(", ", command.Parameters.Select(pd => Util.NormalizeFieldName(pd.Name))); cw.WriteLine($"{command.Name}_ptr = ({invocation}) => {{ throw CreateMissingFunctionException(); }};"); } } } } } } } using (StreamWriter handleWriter = File.CreateText(Path.Combine(path, "Handles.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(handleWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Handles, handle => { HandleHelpers.WriteHandle(cw, handle); }); } } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Unions.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Unions, union => { UnionHelpers.WriteUnion(cw, tnm, union); }); } } } }
public static void WriteAllConstants(CsCodeWriter cw, TypeNameMappings tnm, ConstantDefinition[] constants) { using (cw.PushBlock("public static partial class VulkanNative")) { foreach (ConstantDefinition constant in constants) { if (constant.Comment != null) { cw.WriteLine($"///<summary>{constant.Comment}</summary>"); } cw.WriteLine($"public const {GetCSharpNameForConstantType(constant.Type)} {EnumHelpers.GetPrettyEnumName(constant.Name, "VK_")} = {NormalizeValue(constant.Value)};"); } } cw.WriteLine(); using (cw.PushBlock("public static partial class RawConstants")) { foreach (ConstantDefinition constant in constants) { if (constant.Comment != null) { cw.WriteLine($"///<summary>{constant.Comment}</summary>"); } cw.WriteLine($"public const {GetCSharpNameForConstantType(constant.Type)} {constant.Name} = VulkanNative.{EnumHelpers.GetPrettyEnumName(constant.Name, "VK_")};"); } } }
private static void GenerateAllTypes(VulkanSpecification spec, TypeNameMappings tnm, string path) { using (StreamWriter sw = File.CreateText(Path.Combine(path, "Structures.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(sw); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Structures, structure => { StructureHelpers.WriteStructure(cw, structure, tnm, spec.Constants); }); } } using (StreamWriter enumWriter = File.CreateText(Path.Combine(path, "Enums.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(enumWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Enums, enumDef => { EnumHelpers.WriteEnum(cw, enumDef, tnm); }); } } CommandDefinition[] allVariants = spec.Commands.SelectMany(cd => VariantGenerator.GenerateVariants(cd)).ToArray(); CommandDefinition[] allCommandsWithVariants = spec.Commands.Concat(allVariants).OrderBy(cd => cd.Name).ToArray(); using (StreamWriter commandWriter = File.CreateText(Path.Combine(path, "Commands.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(commandWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) using (cw.PushBlock("public static unsafe partial class VulkanNative")) { Util.SpaceSeparatedList(cw, allCommandsWithVariants, command => { CommandHelpers.WriteCommand(cw, tnm, command); }); cw.WriteLine(); using (cw.PushBlock("private static void LoadFunctionPointers()")) { foreach (CommandDefinition command in spec.Commands) { cw.WriteLine($"{command.Name}_ptr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); } } } } using (StreamWriter handleWriter = File.CreateText(Path.Combine(path, "Handles.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(handleWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Handles, handle => { HandleHelpers.WriteHandle(cw, handle); }); } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Unions.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Unions, union => { UnionHelpers.WriteUnion(cw, tnm, union); }); } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Constants.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { ConstantHelpers.WriteAllConstants(cw, tnm, spec.Constants); } } }
public static void WriteConstant(CsCodeWriter cw, TypeNameMappings tnm, ConstantDefinition constant) { if (constant.Comment != null) { cw.WriteLine($"///<summary>{constant.Comment}</summary>"); } cw.WriteLine($"public const {GetCSharpNameForConstantType(constant.Type)} {EnumHelpers.GetPrettyEnumName(constant.Name, "VK_")} = {NormalizeValue(constant.Value)};"); }