public static VulkanSpecification LoadFromXmlStream(Stream specFileStream) { var spec = XDocument.Load(specFileStream); var registry = spec.Element("registry"); var commands = registry.Element("commands"); CommandDefinition[] commandDefinitions = commands.Elements("command") .Select(commandx => CommandDefinition.CreateFromXml(commandx)).ToArray(); ConstantDefinition[] constantDefinitions = registry.Elements("enums") .Where(enumx => enumx.Attribute("name").Value == "API Constants") .SelectMany(enumx => enumx.Elements("enum")) .Select(enumxx => ConstantDefinition.CreateFromXml(enumxx)).ToArray(); var types = registry.Elements("types"); TypedefDefinition[] typedefDefinitions = types.Elements("type").Where(xe => xe.Value.Contains("typedef") && xe.HasCategoryAttribute("bitmask")) .Select(xe2 => TypedefDefinition.CreateFromXml(xe2)).ToArray(); EnumDefinition[] enumDefinitions = registry.Elements("enums") .Where(enumx => enumx.GetTypeAttributeOrNull() == "enum" || enumx.GetTypeAttributeOrNull() == "bitmask") .Select(enumx => EnumDefinition.CreateFromXml(enumx)).ToArray(); StructureDefinition[] structures = types.Elements("type").Where(typex => typex.HasCategoryAttribute("struct")) .Select(typex => StructureDefinition.CreateFromXml(typex)).ToArray(); StructureDefinition[] unions = types.Elements("type") .Where(typex => typex.HasCategoryAttribute("union")) .Select(typex => StructureDefinition.CreateFromXml(typex)).ToArray(); HandleDefinition[] handles = types.Elements("type").Where(typex => typex.HasCategoryAttribute("handle")) .Select(typex => HandleDefinition.CreateFromXml(typex)).ToArray(); string[] bitmaskTypes = types.Elements("type").Where(typex => typex.HasCategoryAttribute("bitmask")) .Select(typex => typex.GetNameElement()).ToArray(); Dictionary <string, string> baseTypes = types.Elements("type").Where(typex => typex.HasCategoryAttribute("basetype")) .ToDictionary( typex => typex.GetNameElement(), typex => typex.Element("type").Value); ExtensionDefinition[] extensions = registry.Element("extensions").Elements("extension") .Select(xe => ExtensionDefinition.CreateFromXml(xe)).ToArray(); return(new VulkanSpecification( commandDefinitions, constantDefinitions, typedefDefinitions, enumDefinitions, structures, unions, handles, bitmaskTypes, baseTypes, extensions)); }
public static void WriteStructure(CsCodeWriter cw, StructureDefinition structure, TypeNameMappings tnm, ConstantDefinition[] constants) { using (cw.PushBlock("public unsafe partial struct " + structure.Name)) { foreach (var member in structure.Members) { if (member.ElementCount > 1) { for (int i = 0; i < member.ElementCount; i++) { WriteMember(cw, tnm, member, "_" + i); } } else if (member.ElementCountSymbolic != null) { var validConstant = constants.FirstOrDefault(cd => cd.Name == member.ElementCountSymbolic); if (validConstant != null) { WriteMemberSymbolicCount(cw, tnm, member, constants); } else { WriteMember(cw, tnm, member, string.Empty); } } else { WriteMember(cw, tnm, member, string.Empty); } } if (HasAnyFieldWithSpecifiedValues(structure)) { // Add a helper property which fills in the structure type. using (cw.PushBlock($"public static {structure.Name} New()")) { cw.WriteLine($"{structure.Name} ret = new {structure.Name}();"); foreach (var member in structure.Members.Where(ms => ms.LegalValues != null)) { cw.WriteLine($"ret.{member.Name} = {member.Type}.{GetDefaultValueString(member.Type, member.LegalValues)};"); } cw.WriteLine("return ret;"); } } } }
public static void WriteUnion(CsCodeWriter cw, TypeNameMappings tnm, StructureDefinition union) { cw.WriteLine("[StructLayout(LayoutKind.Explicit)]"); using (cw.PushBlock("public struct " + union.Name)) { foreach (var member in union.Members) { if (member.ElementCount > 1) { for (int i = 0; i < member.ElementCount; i++) { int fieldSize = Util.GetTypeSize(member.Type.MapTypeSpec(tnm)); cw.WriteLine($"[FieldOffset({i * fieldSize})]"); WriteMember(cw, tnm, member, "_" + i); } } else { cw.WriteLine("[FieldOffset(0)]"); WriteMember(cw, tnm, member, string.Empty); } } } }
private static bool HasAnyFieldWithSpecifiedValues(StructureDefinition sd) { return(sd.Members.Any(ms => ms.LegalValues != null)); }