示例#1
0
 public void Write(CodeWriter codeWriter)
 {
     _sb.Append(codeWriter._sb);
 }
        public static void GenerateEnums(CppCompilation compilation, string outputPath)
        {
            using var writer = new CodeWriter(Path.Combine(outputPath, "Enumerations.cs"), "System");
            var createdEnums = new Dictionary <string, string>();

            foreach (var cppEnum in compilation.Enums)
            {
                var isBitmask =
                    cppEnum.Name.EndsWith("FlagBits") ||
                    cppEnum.Name.EndsWith("FlagBitsEXT") ||
                    cppEnum.Name.EndsWith("FlagBitsKHR") ||
                    cppEnum.Name.EndsWith("FlagBitsNV") ||
                    cppEnum.Name.EndsWith("FlagBitsAMD") ||
                    cppEnum.Name.EndsWith("FlagBitsMVK") ||
                    cppEnum.Name.EndsWith("FlagBitsNN");
                if (isBitmask)
                {
                    writer.WriteLine("[Flags]");
                }

                string csName         = GetCsCleanName(cppEnum.Name);
                string enumNamePrefix = GetEnumNamePrefix(cppEnum.Name);

                // Rename FlagBits in Flags.
                if (isBitmask)
                {
                    csName = csName.Replace("FlagBits", "Flags");
                    AddCsMapping(cppEnum.Name, csName);
                }

                createdEnums.Add(csName, cppEnum.Name);
                using (writer.PushBlock($"public enum {csName}"))
                {
                    if (isBitmask &&
                        !cppEnum.Items.Any(item => GetPrettyEnumName(item.Name, enumNamePrefix) == "None"))
                    {
                        writer.WriteLine("None = 0,");
                    }

                    foreach (var enumItem in cppEnum.Items)
                    {
                        if (enumItem.Name.EndsWith("_BEGIN_RANGE") ||
                            enumItem.Name.EndsWith("_END_RANGE") ||
                            enumItem.Name.EndsWith("_RANGE_SIZE") ||
                            enumItem.Name.EndsWith("_BEGIN_RANGE_EXT") ||
                            enumItem.Name.EndsWith("_BEGIN_RANGE_KHR") ||
                            enumItem.Name.EndsWith("_BEGIN_RANGE_NV") ||
                            enumItem.Name.EndsWith("_BEGIN_RANGE_AMD") ||
                            enumItem.Name.EndsWith("_END_RANGE_EXT") ||
                            enumItem.Name.EndsWith("_END_RANGE_KHR") ||
                            enumItem.Name.EndsWith("_END_RANGE_NV") ||
                            enumItem.Name.EndsWith("_END_RANGE_AMD") ||
                            enumItem.Name.EndsWith("_RANGE_SIZE_EXT") ||
                            enumItem.Name.EndsWith("_RANGE_SIZE_KHR") ||
                            enumItem.Name.EndsWith("_RANGE_SIZE_NV") ||
                            enumItem.Name.EndsWith("_RANGE_SIZE_AMD") ||
                            enumItem.Name.EndsWith("_MAX_ENUM") ||
                            enumItem.Name.EndsWith("_MAX_ENUM_EXT") ||
                            enumItem.Name.EndsWith("_MAX_ENUM_KHR") ||
                            enumItem.Name.EndsWith("_MAX_ENUM_NV") ||
                            enumItem.Name.EndsWith("_MAX_ENUM_AMD") ||
                            enumItem.Name.EndsWith("_MAX_ENUM_INTEL") ||
                            enumItem.Name == "VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_EXT" ||
                            enumItem.Name == "VK_STENCIL_FRONT_AND_BACK" ||
                            enumItem.Name == "VK_PIPELINE_CREATE_DISPATCH_BASE")
                        {
                            continue;
                        }

                        var enumItemName = GetEnumItemName(cppEnum, enumItem.Name, enumNamePrefix);

                        //writer.WriteLine("/// <summary>");
                        //writer.WriteLine($"/// {enumItem.Name}");
                        //writer.WriteLine("/// </summary>");
                        if (enumItem.ValueExpression is CppRawExpression rawExpression)
                        {
                            var enumValueName = GetEnumItemName(cppEnum, rawExpression.Text, enumNamePrefix);
                            if (enumItemName == "SurfaceCapabilities2EXT")
                            {
                                continue;
                            }

                            writer.WriteLine($"{enumItemName} = {enumValueName},");
                        }
                        else
                        {
                            writer.WriteLine($"{enumItemName} = {enumItem.Value},");
                        }
                    }

                    if (csName == "VkColorComponentFlags")
                    {
                        writer.WriteLine($"All = R | G | B | A");
                    }
                }

                writer.WriteLine();
            }

            // Map missing flags with typedefs to VkFlags
            foreach (var typedef in compilation.Typedefs)
            {
                if (typedef.Name.StartsWith("PFN_") ||
                    typedef.Name.Equals("VkBool32", StringComparison.OrdinalIgnoreCase) ||
                    typedef.Name.Equals("VkFlags", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (typedef.ElementType is CppPointerType)
                {
                    continue;
                }

                if (createdEnums.ContainsKey(typedef.Name))
                {
                    continue;
                }

                if (typedef.Name.EndsWith("Flags", StringComparison.OrdinalIgnoreCase) ||
                    typedef.Name.EndsWith("FlagsKHR", StringComparison.OrdinalIgnoreCase) ||
                    typedef.Name.EndsWith("FlagsEXT", StringComparison.OrdinalIgnoreCase) ||
                    typedef.Name.EndsWith("FlagsNV", StringComparison.OrdinalIgnoreCase) ||
                    typedef.Name.EndsWith("FlagsAMD", StringComparison.OrdinalIgnoreCase) ||
                    typedef.Name.EndsWith("FlagsMVK", StringComparison.OrdinalIgnoreCase) ||
                    typedef.Name.EndsWith("FlagsNN", StringComparison.OrdinalIgnoreCase))
                {
                    writer.WriteLine("[Flags]");
                    using (writer.PushBlock($"public enum {typedef.Name}"))
                    {
                        writer.WriteLine("None = 0,");
                    }
                    writer.WriteLine();
                }
            }
        }
示例#3
0
 public CodeWriter(CodeWriter codeWriter)
 {
     _tab = codeWriter._tab;
 }
        private static void GenerateConstants(CppCompilation compilation, string outputPath)
        {
            using var writer = new CodeWriter(Path.Combine(outputPath, "Constants.cs"));
            writer.WriteLine("/// <summary>");
            writer.WriteLine("/// Provides Vulkan specific constants for special values, layer names and extension names.");
            writer.WriteLine("/// </summary>");
            using (writer.PushBlock("public static partial class Vulkan"))
            {
                foreach (var cppMacro in compilation.Macros)
                {
                    if (string.IsNullOrEmpty(cppMacro.Value) ||
                        cppMacro.Name.EndsWith("_H_", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VKAPI_CALL", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VKAPI_PTR", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VULKAN_CORE_H_", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VK_TRUE", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VK_FALSE", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VK_MAKE_VERSION", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.StartsWith("VK_ENABLE_BETA_EXTENSIONS", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.StartsWith("VK_VERSION_", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.StartsWith("VK_API_VERSION_", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VK_NULL_HANDLE", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VK_DEFINE_HANDLE", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.Equals("VK_DEFINE_NON_DISPATCHABLE_HANDLE", StringComparison.OrdinalIgnoreCase) ||
                        cppMacro.Name.StartsWith("VK_USE_PLATFORM_", StringComparison.OrdinalIgnoreCase)
                        )
                    {
                        continue;
                    }

                    string csName = GetPrettyEnumName(cppMacro.Name, "VK_");

                    string modifier   = "const";
                    string csDataType = "string";
                    var    macroValue = NormalizeEnumValue(cppMacro.Value);
                    if (macroValue.EndsWith("F", StringComparison.OrdinalIgnoreCase))
                    {
                        csDataType = "float";
                    }
                    else if (macroValue.EndsWith("UL", StringComparison.OrdinalIgnoreCase))
                    {
                        csDataType = "ulong";
                    }
                    else if (macroValue.EndsWith("U", StringComparison.OrdinalIgnoreCase))
                    {
                        csDataType = "uint";
                    }
                    else if (uint.TryParse(macroValue, out _))
                    {
                        csDataType = "uint";
                    }

                    if (cppMacro.Name == "VK_QUEUE_FAMILY_EXTERNAL" ||
                        cppMacro.Name == "VK_QUEUE_FAMILY_FOREIGN_EXT")
                    {
                        csDataType = "uint";
                    }
                    else if (cppMacro.Name == "VK_LUID_SIZE_KHR" ||
                             cppMacro.Name == "VK_SHADER_UNUSED_NV" ||
                             cppMacro.Name == "VK_QUEUE_FAMILY_EXTERNAL_KHR" ||
                             cppMacro.Name == "VK_MAX_DRIVER_NAME_SIZE_KHR" ||
                             cppMacro.Name == "VK_MAX_DRIVER_INFO_SIZE_KHR" ||
                             cppMacro.Name == "VK_MAX_DEVICE_GROUP_SIZE_KHR"
                             )
                    {
                        csDataType = "uint";
                        macroValue = GetCsCleanName(cppMacro.Value);
                    }

                    AddCsMapping(cppMacro.Name, csName);

                    writer.WriteLine("/// <summary>");
                    if (cppMacro.Name == "VK_HEADER_VERSION_COMPLETE")
                    {
                        modifier   = "static readonly";
                        csDataType = "VkVersion";
                    }

                    writer.WriteLine($"/// {cppMacro.Name} = {cppMacro.Value}");
                    writer.WriteLine("/// </summary>");
                    if (cppMacro.Name == "VK_HEADER_VERSION_COMPLETE")
                    {
                        writer.WriteLine($"public {modifier} {csDataType} {csName} = new VkVersion({cppMacro.Tokens[2]}, {cppMacro.Tokens[4]}, HeaderVersion);");
                    }
                    else
                    {
                        writer.WriteLine($"public {modifier} {csDataType} {csName} = {macroValue};");
                    }
                }
            }
        }