示例#1
0
        /// <summary>Writes a handle to a <see cref="CsWriter"/>.</summary>
        /// <param name="csWriter">The <see cref="CsWriter"/> to write the handle to.</param>
        /// <param name="handleInfo">The handle to write to <paramref name="csWriter"/>.</param>
        private static void WriteHandle(CsWriter csWriter, HandleInfo handleInfo)
        {
            var handleType = handleInfo.Dispatchable ? "UIntPtr" : "ulong";
            var nullValue  = handleInfo.Dispatchable ? "UIntPtr.Zero" : "0";

            csWriter.WriteLine($"public struct {handleInfo.Name}");
            csWriter.WriteScope(() =>
            {
                // fields
                csWriter.WriteLine($"public readonly {handleType} Handle;");

                // accessors
                csWriter.WriteLine($"public bool IsNull => Handle == {nullValue};");
                csWriter.WriteLine($"public static {handleInfo.Name} Null => new({nullValue});");

                // public methods
                csWriter.WriteLine($"public {handleInfo.Name}({handleType} handle)");
                csWriter.WriteScope(() =>
                {
                    csWriter.WriteLine("Handle = handle;");
                });
                csWriter.WriteLine($"public override bool Equals(object obj) => obj is {handleInfo.Name} handle && this == handle;");
                csWriter.WriteLine($"public override int GetHashCode() => Handle.GetHashCode();");

                // operators
                csWriter.WriteLine($"public static bool operator ==({handleInfo.Name} left, {handleInfo.Name} right) => left.Handle == right.Handle;");
                csWriter.WriteLine($"public static bool operator !=({handleInfo.Name} left, {handleInfo.Name} right) => left.Handle != right.Handle;");
                csWriter.WriteLine($"public static bool operator ==({handleInfo.Name} left, {handleType} right) => left.Handle == right;");
                csWriter.WriteLine($"public static bool operator !=({handleInfo.Name} left, {handleType} right) => left.Handle != right;");
                csWriter.WriteLine($"public static bool operator ==({handleType} left, {handleInfo.Name} right) => left == right.Handle;");
                csWriter.WriteLine($"public static bool operator !=({handleType} left, {handleInfo.Name} right) => left != right.Handle;");
            });
        }
示例#2
0
        /// <summary>Writes an enum to a <see cref="CsWriter"/>.</summary>
        /// <param name="csWriter">The <see cref="CsWriter"/> to write the handle to.</param>
        /// <param name="enumInfo">The enum to write to <paramref name="csWriter"/>.</param>
        private static void WriteEnum(CsWriter csWriter, EnumInfo enumInfo)
        {
            if (enumInfo.Type == EnumType.Bitmask)
            {
                csWriter.WriteLine($"[Flags]");
            }

            csWriter.WriteLine($"public enum {enumInfo.Name}{((enumInfo.BitWidth == 64) ? " : long" : "")}");
            csWriter.WriteScope(() =>
            {
                foreach (var enumValue in enumInfo.Values.Distinct(new EnumFieldInfoDisplayNameEqualityComparer()))
                {
                    csWriter.WriteLine(enumValue.ToString());
                }
            });
        }
示例#3
0
        /// <summary>Generates the C# file containing the Vulkan enums.</summary>
        /// <param name="handleInfos">The handles to generate.</param>
        /// <param name="file">The handles file.</param>
        private static void GenerateHandlesFile(List <HandleInfo> handleInfos, string file)
        {
            using (var streamWriter = File.CreateText(file))
            {
                var csWriter = new CsWriter(streamWriter);

                csWriter.WriteHeader();
                csWriter.WriteLine();

                csWriter.WriteUsing("System");
                csWriter.WriteLine();

                csWriter.WriteLine($"namespace Vulkan");
                csWriter.WriteScope(() =>
                {
                    foreach (var handleInfo in handleInfos)
                    {
                        WriteHandle(csWriter, handleInfo);
                    }
                });
            }
        }
示例#4
0
        /// <summary>Generates the C# file containing the Vulkan structures.</summary>
        /// <param name="structureInfos">The structures to generate.</param>
        /// <param name="constantInfos">The constants to use when resolving array lengths for fields.</param>
        /// <param name="file">The structures file.</param>
        private static void GenerateStructuresFile(List <StructureInfo> structureInfos, List <ConstantInfo> constantInfos, string file)
        {
            using (var streamWriter = File.CreateText(file))
            {
                var csWriter = new CsWriter(streamWriter);

                csWriter.WriteHeader();
                csWriter.WriteLine();

                csWriter.WriteUsing("System");
                csWriter.WriteUsing("System.Runtime.InteropServices");
                csWriter.WriteLine();

                csWriter.WriteLine($"namespace Vulkan");
                csWriter.WriteScope(() =>
                {
                    foreach (var structureInfo in structureInfos.Where(structureInfo => structureInfo.Fields.Count > 0))
                    {
                        WriteStructure(csWriter, structureInfo, constantInfos);
                    }
                });
            }
        }
示例#5
0
        /*********
        ** Private Methods
        *********/
        /// <summary>Generates the C# file containing the Vulkan constants and commands.</summary>
        /// <param name="constantInfos">The constants to generate.</param>
        /// <param name="commandInfos">The commands to generate.</param>
        /// <param name="file">The VK file.</param>
        private static void GenerateVKFile(List <ConstantInfo> constantInfos, List <CommandInfo> commandInfos, string file)
        {
            using (var streamWriter = File.CreateText(file))
            {
                var csWriter = new CsWriter(streamWriter);

                csWriter.WriteHeader();
                csWriter.WriteLine();

                csWriter.WriteUsing("System");
                csWriter.WriteUsing("System.Runtime.InteropServices");
                csWriter.WriteLine();

                csWriter.WriteLine($"namespace Vulkan");
                csWriter.WriteScope(() =>
                {
                    csWriter.WriteLine($"public static class VK");
                    csWriter.WriteScope(() =>
                    {
                        // create constants
                        foreach (var constantInfo in constantInfos)
                        {
                            WriteConstant(csWriter, constantInfo);
                        }

                        // create delegates
                        foreach (var commandInfo in commandInfos)
                        {
                            WriteDelegates(csWriter, commandInfo);
                        }

                        // create fields
                        csWriter.WriteLine("private static readonly OSVulkanLibraryBase VulkanLibrary;");
                        foreach (var commandInfo in commandInfos)
                        {
                            WriteFields(csWriter, commandInfo);
                        }

                        // create class initialiser to retrieve the function pointers
                        csWriter.WriteLine("static VK()");
                        csWriter.WriteScope(() =>
                        {
                            csWriter.WriteLine("VulkanLibrary = OSVulkanLibraryBase.CreateOSVulkanLibrary();");
                            csWriter.WriteLine("InitialiseRequiredMethods();");
                        });

                        // create methods
                        void PopulateDelegateOverloads(CsWriter csWriter, CommandInfo commandInfo)
                        {
                            var overloads = Utilities.GenerateAllOverloads(commandInfo);
                            for (int i = 0; i < overloads.Count; i++)
                            {
                                csWriter.WriteLine($"{commandInfo.DisplayName}_{i} = Marshal.GetDelegateForFunctionPointer<{commandInfo.DelegateDisplayName}_{i}>({commandInfo.PointerDisplayName});");
                            }
                        }

                        var requiredMethods = new[] { "vkCreateInstance", "vkEnumerateInstanceExtensionProperties", "vkEnumerateInstanceLayerProperties", "vkGetInstanceProcAddr" };

                        csWriter.WriteLine("private static void InitialiseRequiredMethods()");
                        csWriter.WriteScope(() =>
                        {
                            foreach (var commandInfo in commandInfos.Where(commandInfo => requiredMethods.Contains(commandInfo.Name)))
                            {
                                csWriter.WriteLine($"{commandInfo.PointerDisplayName} = VulkanLibrary.GetFunctionPointer(\"{commandInfo.Name}\");");
                                PopulateDelegateOverloads(csWriter, commandInfo);
                            }
                        });

                        csWriter.WriteLine("public static void InitialiseInstanceMethods(VkInstance instance)");
                        csWriter.WriteScope(() =>
                        {
                            foreach (var commandInfo in commandInfos.Where(commandInfo => !requiredMethods.Contains(commandInfo.Name)))
                            {
                                if (commandInfo.Alias != null) // aliases don't have delegates
                                {
                                    continue;
                                }

                                csWriter.WriteLine($"{commandInfo.PointerDisplayName} = GetInstancePrecedureAddress(instance, \"{commandInfo.Name}\");");
                                csWriter.WriteLine($"if ({commandInfo.PointerDisplayName} != IntPtr.Zero)");
                                csWriter.WriteScope(() => PopulateDelegateOverloads(csWriter, commandInfo));
                            }
                        });

                        foreach (var commandInfo in commandInfos)
                        {
                            WriteMethods(csWriter, commandInfo, commandInfos);
                        }
                    });
                });
            }
        }
示例#6
0
        /// <summary>Writes a structure to a <see cref="CsWriter"/>.</summary>
        /// <param name="csWriter">The <see cref="CsWriter"/> to write the structure to.</param>
        /// <param name="structureInfo">The structure to write to <paramref name="csWriter"/>.</param>
        /// <param name="constantInfos">The constants to use when resolving array lengths for fields.</param>
        private static void WriteStructure(CsWriter csWriter, StructureInfo structureInfo, List <ConstantInfo> constantInfos)
        {
            if (structureInfo.IsUnion)
            {
                csWriter.WriteLine("[StructLayout(LayoutKind.Explicit)]");
            }
            csWriter.WriteLine($"public unsafe struct {structureInfo.DisplayName}");
            csWriter.WriteScope(() =>
            {
                // fields
                foreach (var fieldInfo in structureInfo.Fields)
                {
                    if (structureInfo.IsUnion)
                    {
                        csWriter.WriteLine("[FieldOffset(0)]");
                    }
                    WriteField(csWriter, fieldInfo, constantInfos);
                }

                var structuresWithConstructors = new[] { "VkOffset2D", "VkOffset3D", "VkExtent2D", "VkExtent3D", "VkRect2D", "VkComponentMapping", "VkImageSubresource", "VkImageSubresourceLayers", "VkImageSubresourceRange", "VkClearDepthStencilValue", "VkClearColorValue" };

                // properties
                if (structureInfo.DisplayName.StartsWith("VkOffset") || structureInfo.DisplayName.StartsWith("VkExtent"))
                {
                    csWriter.WriteLine($"public static {structureInfo.DisplayName} Zero => new();");
                }
                else if (structureInfo.DisplayName == "VkComponentMapping")
                {
                    csWriter.WriteLine("public static VkComponentMapping Identity => new(VkComponentSwizzle.Identity, VkComponentSwizzle.Identity, VkComponentSwizzle.Identity, VkComponentSwizzle.Identity);");
                }

                // constructors
                if (structuresWithConstructors.Contains(structureInfo.DisplayName))
                {
                    // special constructors
                    if (structureInfo.DisplayName == "VkClearColorValue")
                    {
                        var parameterTypes = new[] { "float", "int", "uint" };
                        foreach (var parameterType in parameterTypes)
                        {
                            csWriter.WriteLine($"public VkClearColorValue({parameterType} r, {parameterType} g, {parameterType} b, {parameterType} a)");
                            csWriter.WriteScope(() =>
                            {
                                var fieldName = parameterType.FirstToUpper() + "32";
                                csWriter.WriteLine($"{fieldName}[0] = r;");
                                csWriter.WriteLine($"{fieldName}[1] = g;");
                                csWriter.WriteLine($"{fieldName}[2] = b;");
                                csWriter.WriteLine($"{fieldName}[3] = a;");
                            });
                        }
                    }

                    // auto generated constructor
                    else
                    {
                        var parametersString = string.Join(", ", structureInfo.Fields.Select(fieldInfo => $"{fieldInfo.Type} {fieldInfo.DisplayName.FirstToLower()}"));
                        csWriter.WriteLine($"public {structureInfo.DisplayName}({parametersString})");
                        csWriter.WriteScope(() =>
                        {
                            foreach (var fieldInfo in structureInfo.Fields)
                            {
                                csWriter.WriteLine($"{fieldInfo.DisplayName} = {fieldInfo.DisplayName.FirstToLower()};");
                            }
                        });
                    }
                }

                // operators
                if (structureInfo.DisplayName == "VkClearValue")
                {
                    csWriter.WriteLine("public static implicit operator VkClearValue(VkClearColorValue colour) => new() { Color = colour };");
                    csWriter.WriteLine("public static implicit operator VkClearValue(VkClearDepthStencilValue depthStencil) => new() { DepthStencil = depthStencil };");
                }
            });
        }