Skip to content

Commit

Permalink
Add default constructors to VkOffset2D, VkOffset3D, VkExtend2D, VkExt…
Browse files Browse the repository at this point in the history
…end3D, VkRect2D
  • Loading branch information
xoofx committed Jun 23, 2024
1 parent 5dea6a4 commit 10fa1f3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
23 changes: 19 additions & 4 deletions src/codegen/XenoAtom.Interop.CodeGen/vulkan/VulkanGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ internal partial class VulkanGenerator(LibDescriptor descriptor) : GeneratorBase
private readonly List<int> _tempOptionalParameterIndexList = new();
private readonly Dictionary<string, Dictionary<string, string>> _mapStructToFieldsWithDefaultValue = new();

private readonly HashSet<string> _structsAsRecord = new()
{
"VkOffset2D",
"VkOffset3D",
"VkExtent2D",
"VkExtent3D",
"VkRect2D",
};

public override async Task Initialize(ApkManager apkHelper)
{
await base.Initialize(apkHelper);
Expand Down Expand Up @@ -151,7 +160,7 @@ public override async Task Initialize(ApkManager apkHelper)
{
ApplyDocumentation(csStruct);
AddVulkanVersionAndExtensionInfoToCSharpElement(csStruct);
AddDefaultFields(csStruct);
ProcessStruct(csStruct);

// Associate Enum XXXFlagBits with Struct XXXFlags
if (csStruct.Name.Contains("Flags", StringComparison.Ordinal))
Expand Down Expand Up @@ -213,10 +222,16 @@ public override async Task Initialize(ApkManager apkHelper)
return csCompilation;
}

private void AddDefaultFields(CSharpStruct csStruct)
private void ProcessStruct(CSharpStruct csStruct)
{
var cppType = ((ICppMember)csStruct.CppElement!).Name;
if (!_mapStructToFieldsWithDefaultValue.TryGetValue(cppType, out var fieldsWithDefaultValue))
var cppName = ((ICppMember)csStruct.CppElement!).Name;

if (_structsAsRecord.Contains(cppName))
{
csStruct.IsRecord = true;
}

if (!_mapStructToFieldsWithDefaultValue.TryGetValue(cppName, out var fieldsWithDefaultValue))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21471,7 +21471,7 @@ public enum VkShaderGroupShaderKHR : uint
/// <remarks>
/// <para>API Version: 1.0</para>
/// </remarks>
public partial struct VkExtent2D
public partial record struct VkExtent2D
{
/// <summary>
/// The width of the extent.
Expand All @@ -21490,7 +21490,7 @@ public partial struct VkExtent2D
/// <remarks>
/// <para>API Version: 1.0</para>
/// </remarks>
public partial struct VkExtent3D
public partial record struct VkExtent3D
{
/// <summary>
/// The width of the extent.
Expand All @@ -21514,7 +21514,7 @@ public partial struct VkExtent3D
/// <remarks>
/// <para>API Version: 1.0</para>
/// </remarks>
public partial struct VkOffset2D
public partial record struct VkOffset2D
{
/// <summary>
/// The x offset.
Expand All @@ -21533,7 +21533,7 @@ public partial struct VkOffset2D
/// <remarks>
/// <para>API Version: 1.0</para>
/// </remarks>
public partial struct VkOffset3D
public partial record struct VkOffset3D
{
/// <summary>
/// The x offset.
Expand All @@ -21557,7 +21557,7 @@ public partial struct VkOffset3D
/// <remarks>
/// <para>API Version: 1.0</para>
/// </remarks>
public partial struct VkRect2D
public partial record struct VkRect2D
{
/// <summary>
/// A <see cref="T:VkOffset2D"/> specifying the rectangle offset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -73,4 +74,68 @@ partial struct VkBool32

public static implicit operator vulkan.VkBool32(bool from) => new(from ? 1U : 0U);
}

partial struct VkClearColorValue
{
public VkClearColorValue(float r, float g, float b, float a)
{
float32[0] = r;
float32[1] = g;
float32[2] = b;
float32[3] = a;
}
}

partial record struct VkOffset2D
{
public VkOffset2D(int x, int y)
{
this.x = x;
this.y = y;
}
}

partial record struct VkOffset3D
{
public VkOffset3D(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
}

partial record struct VkExtent2D
{
public VkExtent2D(uint width, uint height)
{
this.width = width;
this.height = height;
}
}

partial record struct VkExtent3D
{
public VkExtent3D(uint width, uint height, uint depth)
{
this.width = width;
this.height = height;
this.depth = depth;
}
}

partial record struct VkRect2D
{
public VkRect2D(int x, int y, uint width, uint height)
{
this.offset = new(x, y);
this.extent = new(width, height);
}

public VkRect2D(VkOffset2D offset, VkExtent2D extent)
{
this.offset = offset;
this.extent = extent;
}
}
}

0 comments on commit 10fa1f3

Please sign in to comment.