More generated and prettier name

This commit is contained in:
Amer Koleci
2022-09-01 16:28:28 +02:00
parent e898628a2e
commit 36c2f20ea0
6 changed files with 575 additions and 411 deletions

View File

@@ -51,7 +51,7 @@ public class ApiType
public bool Flags { get; set; } public bool Flags { get; set; }
public bool Scoped { get; set; } public bool Scoped { get; set; }
public string IntegerBase { get; set; } public string IntegerBase { get; set; }
public ApiEnumValue[] Values { get; set; } public IList<ApiEnumValue> Values { get; set; } = new List<ApiEnumValue>();
// Struct // Struct
public int Size { get; set; } public int Size { get; set; }

View File

@@ -3,7 +3,6 @@
using System.Text; using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Generator; namespace Generator;
@@ -58,11 +57,23 @@ public static class Program
{ "DXGI_RECLAIM_RESOURCE_RESULTS", "DXGI_RECLAIM_RESOURCE_RESULT" }, { "DXGI_RECLAIM_RESOURCE_RESULTS", "DXGI_RECLAIM_RESOURCE_RESULT" },
{ "DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAGS", "DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG" }, { "DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAGS", "DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG" },
{ "DXGI_DEBUG_RLO_FLAGS", "DXGI_DEBUG_RLO" }, { "DXGI_DEBUG_RLO_FLAGS", "DXGI_DEBUG_RLO" },
{ "DXGI_OFFER_RESOURCE_FLAGS", "DXGI_OFFER_RESOURCE_FLAG" },
}; };
private static readonly Dictionary<string, string> s_knownEnumValueNames = new() private static readonly Dictionary<string, string> s_knownEnumValueNames = new()
{ {
{ "DXGI_FORMAT_420_OPAQUE", "Opaque420" } { "DXGI_FORMAT_420_OPAQUE", "Opaque420" },
{ "DXGI_OUTDUPL_COMPOSITED_UI_CAPTURE_ONLY", "CompositedUICaptureOnly" },
};
private static readonly Dictionary<string, bool> s_generatedEnums = new()
{
{"DXGI_CPU_ACCESS", false },
{"DXGI_USAGE", true },
{"DXGI_MAP", true },
{"DXGI_PRESENT", true },
{"DXGI_MWA", true },
{"DXGI_ENUM_MODES", true },
}; };
private static readonly HashSet<string> s_ignoredParts = new(StringComparer.OrdinalIgnoreCase) private static readonly HashSet<string> s_ignoredParts = new(StringComparer.OrdinalIgnoreCase)
@@ -81,7 +92,12 @@ public static class Program
{ {
{ "DXGI_ADAPTER_FLAG", "AdapterFlags" }, { "DXGI_ADAPTER_FLAG", "AdapterFlags" },
{ "DXGI_ADAPTER_FLAG3", "AdapterFlags3" }, { "DXGI_ADAPTER_FLAG3", "AdapterFlags3" },
{ "DXGI_SWAP_CHAIN_FLAG", "SwapChainFlags" } { "DXGI_SWAP_CHAIN_FLAG", "SwapChainFlags" },
// Generated
{ "DXGI_MAP", "MapFlags" },
{ "DXGI_ENUM_MODES", "EnumModesFlags" },
{ "DXGI_MWA", "WindowAssociationFlags" },
}; };
private static readonly Dictionary<string, string> s_structFieldTypeRemap = new() private static readonly Dictionary<string, string> s_structFieldTypeRemap = new()
@@ -94,6 +110,8 @@ public static class Program
{ "DXGI_SWAP_CHAIN_DESC1::Flags", "DXGI_SWAP_CHAIN_FLAG" }, { "DXGI_SWAP_CHAIN_DESC1::Flags", "DXGI_SWAP_CHAIN_FLAG" },
}; };
private static bool s_generateUnmanagedDocs = true;
public static int Main(string[] args) public static int Main(string[] args)
{ {
string outputPath = AppContext.BaseDirectory; string outputPath = AppContext.BaseDirectory;
@@ -165,6 +183,19 @@ public static class Program
if (ShouldSkipConstant(constant)) if (ShouldSkipConstant(constant))
continue; continue;
bool skipValue = false;
foreach (var enumToGenerate in s_generatedEnums)
{
if (constant.Name.StartsWith(enumToGenerate.Key))
{
skipValue = true;
break;
}
}
if (skipValue)
continue;
string typeName = GetTypeName(constant.Type); string typeName = GetTypeName(constant.Type);
if (typeName == "Guid") if (typeName == "Guid")
{ {
@@ -193,6 +224,59 @@ public static class Program
writer.WriteLine($"#endregion Enums"); writer.WriteLine($"#endregion Enums");
writer.WriteLine(); writer.WriteLine();
// Generated enums -> from constants
writer.WriteLine($"#region Generated Enums");
var createdEnums = new Dictionary<string, ApiType>();
foreach (var constant in api.Constants)
{
if (ShouldSkipConstant(constant))
continue;
foreach (var enumToGenerate in s_generatedEnums)
{
if (constant.Name.StartsWith(enumToGenerate.Key))
{
ApiType createdEnumType;
if (!createdEnums.ContainsKey(enumToGenerate.Key))
{
ApiType apiType = new()
{
Name = enumToGenerate.Key,
Kind = "Enum",
Flags = enumToGenerate.Value,
Scoped = false,
IntegerBase = constant.Type.Name
};
createdEnums.Add(enumToGenerate.Key, apiType);
createdEnumType = apiType;
}
else
{
createdEnumType = createdEnums[enumToGenerate.Key];
}
ApiEnumValue enumValue = new ApiEnumValue
{
Name = constant.Name,
Value = constant.Value
};
createdEnumType.Values.Add(enumValue);
//string enumValueName = GetPrettyFieldName(constant.Name, createdEnumName);
//writer.WriteLine($"{enumValueName} = {constant.Value},");
}
}
}
foreach (ApiType enumType in createdEnums.Values)
{
GenerateEnum(writer, enumType);
}
writer.WriteLine($"#endregion Generated Enums");
writer.WriteLine();
writer.WriteLine($"#region Structs"); writer.WriteLine($"#region Structs");
foreach (ApiType structType in api.Types.Where(item => item.Kind.ToLowerInvariant() == "struct")) foreach (ApiType structType in api.Types.Where(item => item.Kind.ToLowerInvariant() == "struct"))
{ {
@@ -208,14 +292,24 @@ public static class Program
string baseTypeName = GetTypeName(enumType.IntegerBase); string baseTypeName = GetTypeName(enumType.IntegerBase);
AddCsMapping(writer.Api, enumType.Name, csTypeName); AddCsMapping(writer.Api, enumType.Name, csTypeName);
writer.WriteLine($"/// <unmanaged>{enumType.Name}</unmanaged>"); if (s_generateUnmanagedDocs)
writer.WriteLine($"/// <unmanaged>{enumType.Name}</unmanaged>");
if (enumType.Flags) if (enumType.Flags)
{ {
writer.WriteLine("[Flags]"); writer.WriteLine("[Flags]");
} }
bool noneAdded = false;
using (writer.PushBlock($"public enum {csTypeName} : {baseTypeName}")) using (writer.PushBlock($"public enum {csTypeName} : {baseTypeName}"))
{ {
if (enumType.Flags &&
!enumType.Values.Any(item => GetPrettyFieldName(item.Name, enumPrefix) == "None"))
{
writer.WriteLine("None = 0,");
noneAdded = true;
}
foreach (ApiEnumValue value in enumType.Values) foreach (ApiEnumValue value in enumType.Values)
{ {
if (value.Name.EndsWith("_FORCE_DWORD") || if (value.Name.EndsWith("_FORCE_DWORD") ||
@@ -223,7 +317,16 @@ public static class Program
continue; continue;
string enumValueName = GetPrettyFieldName(value.Name, enumPrefix); string enumValueName = GetPrettyFieldName(value.Name, enumPrefix);
writer.WriteLine($"/// <unmanaged>{value.Name}</unmanaged>"); if (s_generateUnmanagedDocs)
{
writer.WriteLine($"/// <unmanaged>{value.Name}</unmanaged>");
}
if (enumValueName.StartsWith("DXGI_MSG_"))
{
enumValueName = enumValueName.Substring("DXGI_MSG_".Length);
}
writer.WriteLine($"{enumValueName} = {value.Value},"); writer.WriteLine($"{enumValueName} = {value.Value},");
} }
} }
@@ -236,7 +339,11 @@ public static class Program
string csTypeName = GetDataTypeName(structType.Name, out string structPrefix); string csTypeName = GetDataTypeName(structType.Name, out string structPrefix);
AddCsMapping(writer.Api, structType.Name, csTypeName); AddCsMapping(writer.Api, structType.Name, csTypeName);
writer.WriteLine($"/// <unmanaged>{structType.Name}</unmanaged>"); if (s_generateUnmanagedDocs)
{
writer.WriteLine($"/// <unmanaged>{structType.Name}</unmanaged>");
}
using (writer.PushBlock($"public partial struct {csTypeName}")) using (writer.PushBlock($"public partial struct {csTypeName}"))
{ {
foreach (ApiStructField field in structType.Fields) foreach (ApiStructField field in structType.Fields)
@@ -246,7 +353,10 @@ public static class Program
string fieldValueName = GetPrettyFieldName(field.Name, structPrefix); string fieldValueName = GetPrettyFieldName(field.Name, structPrefix);
string fieldTypeName = GetTypeName(field.Type); string fieldTypeName = GetTypeName(field.Type);
//writer.WriteLine($"/// <unmanaged>{field.Name}</unmanaged>"); if (s_generateUnmanagedDocs)
{
//writer.WriteLine($"/// <unmanaged>{field.Name}</unmanaged>");
}
string remapFieldLookUp = $"{structType.Name}::{field.Name}"; string remapFieldLookUp = $"{structType.Name}::{field.Name}";
if (s_structFieldTypeRemap.TryGetValue(remapFieldLookUp, out string? remapType)) if (s_structFieldTypeRemap.TryGetValue(remapFieldLookUp, out string? remapType))
@@ -321,7 +431,8 @@ public static class Program
private static bool ShouldSkipConstant(ApiDataConstant constant) private static bool ShouldSkipConstant(ApiDataConstant constant)
{ {
if (constant.Name == "_FACDXGI") if (constant.Name == "_FACDXGI" ||
constant.Name == "DXGI_FORMAT_DEFINED")
{ {
return true; return true;
} }

View File

@@ -19,12 +19,6 @@ namespace Win32.Graphics.Dxgi.Common;
public static partial class Apis public static partial class Apis
{ {
public const uint DXGI_CPU_ACCESS_NONE = 0;
public const uint DXGI_CPU_ACCESS_DYNAMIC = 1;
public const uint DXGI_CPU_ACCESS_READ_WRITE = 2;
public const uint DXGI_CPU_ACCESS_SCRATCH = 3;
public const uint DXGI_CPU_ACCESS_FIELD = 15;
public const uint DXGI_FORMAT_DEFINED = 1;
public const uint DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN = 4294967295; public const uint DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN = 4294967295;
public const uint DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN = 4294967294; public const uint DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN = 4294967294;
} }
@@ -388,6 +382,24 @@ public enum AlphaMode : uint
#endregion Enums #endregion Enums
#region Generated Enums
/// <unmanaged>DXGI_CPU_ACCESS</unmanaged>
public enum CpuAccess : uint
{
/// <unmanaged>DXGI_CPU_ACCESS_NONE</unmanaged>
None = 0,
/// <unmanaged>DXGI_CPU_ACCESS_DYNAMIC</unmanaged>
Dynamic = 1,
/// <unmanaged>DXGI_CPU_ACCESS_READ_WRITE</unmanaged>
ReadWrite = 2,
/// <unmanaged>DXGI_CPU_ACCESS_SCRATCH</unmanaged>
Scratch = 3,
/// <unmanaged>DXGI_CPU_ACCESS_FIELD</unmanaged>
Field = 15,
}
#endregion Generated Enums
#region Structs #region Structs
/// <unmanaged>DXGI_RATIONAL</unmanaged> /// <unmanaged>DXGI_RATIONAL</unmanaged>
public partial struct Rational public partial struct Rational

File diff suppressed because it is too large Load Diff

View File

@@ -4,23 +4,3 @@
using static Win32.Graphics.Dxgi.Apis; using static Win32.Graphics.Dxgi.Apis;
namespace Win32.Graphics.Dxgi; namespace Win32.Graphics.Dxgi;
[Flags]
public enum Usage : uint
{
ShaderInput = DXGI_USAGE_SHADER_INPUT,
RenderTargetOutput = DXGI_USAGE_RENDER_TARGET_OUTPUT,
Backbuffer = DXGI_USAGE_BACK_BUFFER,
Shared = DXGI_USAGE_SHARED,
ReadOnly = DXGI_USAGE_READ_ONLY,
DiscardOnPresent = DXGI_USAGE_DISCARD_ON_PRESENT,
UnorderedAccess = DXGI_USAGE_UNORDERED_ACCESS
}
[Flags]
public enum MapFlags : uint
{
Read = DXGI_MAP_READ,
Write = DXGI_MAP_WRITE,
Discard = DXGI_MAP_DISCARD
}

View File

@@ -16,20 +16,20 @@ public readonly struct Luid : IEquatable<Luid>
/// <summary> /// <summary>
/// The low bits of the luid. /// The low bits of the luid.
/// </summary> /// </summary>
private readonly uint lowPart; private readonly uint _lowPart;
/// <summary> /// <summary>
/// The high bits of the luid. /// The high bits of the luid.
/// </summary> /// </summary>
private readonly int highPart; private readonly int _highPart;
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Luid other) public bool Equals(Luid other)
{ {
return return
this.lowPart == other.lowPart && _lowPart == other._lowPart &&
this.highPart == other.highPart; _highPart == other._highPart;
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -42,26 +42,26 @@ public readonly struct Luid : IEquatable<Luid>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() public override int GetHashCode()
{ {
return HashCode.Combine(lowPart, highPart); return HashCode.Combine(_lowPart, _highPart);
} }
/// <inheritdoc/> /// <inheritdoc/>
public override string ToString() public override string ToString()
{ {
return (((long)this.highPart) << 32 | this.lowPart).ToString(); return (((long)this._highPart) << 32 | this._lowPart).ToString();
} }
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
/// <inheritdoc/> /// <inheritdoc/>
public string ToString(string? format, IFormatProvider? formatProvider) public string ToString(string? format, IFormatProvider? formatProvider)
{ {
return (((long)this.highPart) << 32 | this.lowPart).ToString(format, formatProvider); return (((long)_highPart) << 32 | _lowPart).ToString(format, formatProvider);
} }
/// <inheritdoc/> /// <inheritdoc/>
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{ {
return (((long)this.highPart) << 32 | this.lowPart).TryFormat(destination, out charsWritten, format, provider); return (((long)_highPart) << 32 | _lowPart).TryFormat(destination, out charsWritten, format, provider);
} }
#endif #endif
@@ -72,10 +72,7 @@ public readonly struct Luid : IEquatable<Luid>
/// <param name="b">The second <see cref="Luid"/> value to compare.</param> /// <param name="b">The second <see cref="Luid"/> value to compare.</param>
/// <returns>Whether <paramref name="a"/> and <paramref name="b"/> are the same.</returns> /// <returns>Whether <paramref name="a"/> and <paramref name="b"/> are the same.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Luid a, Luid b) public static bool operator ==(Luid a, Luid b) => a.Equals(b);
{
return a.Equals(b);
}
/// <summary> /// <summary>
/// Check whether two <see cref="Luid"/> values are different. /// Check whether two <see cref="Luid"/> values are different.
@@ -84,8 +81,5 @@ public readonly struct Luid : IEquatable<Luid>
/// <param name="b">The second <see cref="Luid"/> value to compare.</param> /// <param name="b">The second <see cref="Luid"/> value to compare.</param>
/// <returns>Whether <paramref name="a"/> and <paramref name="b"/> are different.</returns> /// <returns>Whether <paramref name="a"/> and <paramref name="b"/> are different.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Luid a, Luid b) public static bool operator !=(Luid a, Luid b) => !a.Equals(b);
{
return !a.Equals(b);
}
} }