Skip to content

Commit 0d6b402

Browse files
committed
Finish implementing TransformVulkan
1 parent b187f27 commit 0d6b402

File tree

5 files changed

+108
-81
lines changed

5 files changed

+108
-81
lines changed

.silktouch/d48a9fc4a502f7c6.stout

0 Bytes
Binary file not shown.

generator.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@
177177
"TransformFunctions",
178178
"TransformProperties",
179179
"TransformEnums",
180-
"AddVTables"
181180
"TransformVulkan",
181+
"AddVTables"
182182
],
183183
"ClangScraper": {
184184
"ClangSharpResponseFiles": [

sources/SilkTouch/SilkTouch/Mods/TransformVulkan.cs

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ private class Rewriter : CSharpSyntaxRewriter
4343
private const string MethodClassName = "Vk";
4444

4545
private const string InstanceTypeName = "InstanceHandle";
46+
private const string InstanceNativeTypeName = "VkInstance";
4647
private const string InstanceFieldName = "_currentInstance";
4748
private const string InstancePropertyName = "CurrentInstance";
4849

4950
private const string DeviceTypeName = "DeviceHandle";
51+
private const string DeviceNativeTypeName = "VkDevice";
5052
private const string DeviceFieldName = "_currentDevice";
5153
private const string DevicePropertyName = "CurrentDevice";
5254

@@ -93,25 +95,25 @@ private IEnumerable<MemberDeclarationSyntax> RewriteMember(MemberDeclarationSynt
9395
{
9496
if (member is not MethodDeclarationSyntax method)
9597
{
96-
// yield return member;
98+
yield return member;
9799
yield break;
98100
}
99101

100102
if (!method.AttributeLists.GetNativeFunctionInfo(out _, out var entryPoint, out _) || entryPoint == null)
101103
{
102-
// yield return member;
104+
yield return member;
103105
yield break;
104106
}
105107

106108
if (!method.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ExternKeyword)))
107109
{
108-
// yield return member;
110+
yield return member;
109111
yield break;
110112
}
111113

112114
if (entryPoint != VkCreateInstanceNativeName && entryPoint != VkCreateDeviceNativeName)
113115
{
114-
// yield return member;
116+
yield return member;
115117
yield break;
116118
}
117119

@@ -127,6 +129,12 @@ private IEnumerable<MemberDeclarationSyntax> RewriteMember(MemberDeclarationSynt
127129
!SyntaxFacts.IsAccessibilityModifier(modifier.Kind()))
128130
]);
129131

132+
var handlePropertyName = entryPoint == VkCreateInstanceNativeName ? InstancePropertyName : DevicePropertyName;
133+
var handleNativeTypeName = entryPoint == VkCreateInstanceNativeName ? InstanceNativeTypeName : DeviceNativeTypeName;
134+
var handleParameterName = method.ParameterList.Parameters
135+
.FirstOrDefault(parameter => parameter.AttributeLists.GetNativeElementTypeName(out _) == handleNativeTypeName)?.Identifier.ValueText
136+
?? throw new InvalidOperationException($"Failed to determine the parameter that contains the created handle type: {handleNativeTypeName}");
137+
130138
// Add a new public method that stores the VkInstance or VkDevice
131139
var resultName = "result";
132140
yield return method
@@ -142,9 +150,17 @@ private IEnumerable<MemberDeclarationSyntax> RewriteMember(MemberDeclarationSynt
142150
.WithModifiers([
143151
..member.Modifiers.Where(modifier => !modifier.IsKind(SyntaxKind.ExternKeyword))
144152
])
153+
.WithSemicolonToken(Token(SyntaxKind.None))
145154
.WithBody(
146155
Block(
147-
LocalDeclarationStatement(VariableDeclaration(IdentifierName(VkResultName))),
156+
LocalDeclarationStatement(
157+
VariableDeclaration(IdentifierName(VkResultName))
158+
.AddVariables(
159+
VariableDeclarator(resultName)
160+
.WithInitializer(
161+
EqualsValueClause(
162+
InvocationExpression(IdentifierName(privateMethodName))
163+
.WithArgumentList(ArgumentList([..method.ParameterList.Parameters.Select(parameter => Argument(IdentifierName(parameter.Identifier.ValueText)))])))))),
148164
IfStatement(
149165
BinaryExpression(
150166
SyntaxKind.EqualsExpression,
@@ -159,67 +175,12 @@ private IEnumerable<MemberDeclarationSyntax> RewriteMember(MemberDeclarationSynt
159175
ExpressionStatement(
160176
AssignmentExpression(
161177
SyntaxKind.SimpleAssignmentExpression,
162-
IdentifierName(DevicePropertyName),
163-
IdentifierName(DevicePropertyName))))
178+
IdentifierName(handlePropertyName),
179+
PrefixUnaryExpression(
180+
SyntaxKind.PointerIndirectionExpression,
181+
IdentifierName(handleParameterName)))))
164182
),
165183
ReturnStatement(IdentifierName(resultName))));
166-
// List(new StatementSyntax[]
167-
// {
168-
// // var result = CreateDeviceInternal(physicalDevice, pCreateInfo, pAllocator, pDevice);
169-
// LocalDeclarationStatement(
170-
// VariableDeclaration(
171-
// IdentifierName("var")
172-
// ).WithVariables(
173-
// SingletonSeparatedList(
174-
// VariableDeclarator(
175-
// Identifier("result")
176-
// ).WithInitializer(
177-
// EqualsValueClause(
178-
// InvocationExpression(
179-
// IdentifierName("CreateDeviceInternal")
180-
// ).WithArgumentList(
181-
// ArgumentList(SeparatedList<ArgumentSyntax>(new SyntaxNodeOrToken[]
182-
// {
183-
// Argument(IdentifierName("physicalDevice")),
184-
// Token(SyntaxKind.CommaToken),
185-
// Argument(IdentifierName("pCreateInfo")),
186-
// Token(SyntaxKind.CommaToken),
187-
// Argument(IdentifierName("pAllocator")),
188-
// Token(SyntaxKind.CommaToken),
189-
// Argument(IdentifierName("pDevice"))
190-
// }))
191-
// )
192-
// )
193-
// )
194-
// )
195-
// )
196-
// ),
197-
// // if (result != 0) { CurrentDevice = *pDevice; }
198-
// IfStatement(
199-
// BinaryExpression(
200-
// SyntaxKind.NotEqualsExpression,
201-
// IdentifierName("result"),
202-
// LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))
203-
// ),
204-
// Block(
205-
// SingletonList<StatementSyntax>(
206-
// ExpressionStatement(
207-
// AssignmentExpression(
208-
// SyntaxKind.SimpleAssignmentExpression,
209-
// IdentifierName("CurrentDevice"),
210-
// PrefixUnaryExpression(
211-
// SyntaxKind.PointerIndirectionExpression,
212-
// IdentifierName("pDevice")
213-
// )
214-
// )
215-
// )
216-
// )
217-
// )
218-
// ),
219-
// // return result;
220-
// ReturnStatement(IdentifierName("result"))
221-
// })
222-
// ));
223184
}
224185

225186
/// <summary>

sources/Vulkan/Vulkan/Vulkan/IVk.gen.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8449,7 +8449,7 @@ Ref<DescriptorUpdateTemplateHandle> pDescriptorUpdateTemplate
84498449
MinVersion = "1.0"
84508450
)]
84518451
[NativeFunction("vulkan", EntryPoint = "vkCreateDevice")]
8452-
static abstract Result CreateDevice(
8452+
static abstract Result CreateDeviceInternal(
84538453
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
84548454
[NativeTypeName("const VkDeviceCreateInfo *")] DeviceCreateInfo* pCreateInfo,
84558455
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
@@ -8934,7 +8934,7 @@ Ref<IndirectExecutionSetEXTHandle> pIndirectExecutionSet
89348934
MinVersion = "1.0"
89358935
)]
89368936
[NativeFunction("vulkan", EntryPoint = "vkCreateInstance")]
8937-
static abstract Result CreateInstance(
8937+
static abstract Result CreateInstanceInternal(
89388938
[NativeTypeName("const VkInstanceCreateInfo *")] InstanceCreateInfo* pCreateInfo,
89398939
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
89408940
[NativeTypeName("VkInstance *")] InstanceHandle* pInstance
@@ -26670,7 +26670,7 @@ Ref<DescriptorUpdateTemplateHandle> pDescriptorUpdateTemplate
2667026670
MinVersion = "1.0"
2667126671
)]
2667226672
[NativeFunction("vulkan", EntryPoint = "vkCreateDevice")]
26673-
Result CreateDevice(
26673+
Result CreateDeviceInternal(
2667426674
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
2667526675
[NativeTypeName("const VkDeviceCreateInfo *")] DeviceCreateInfo* pCreateInfo,
2667626676
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
@@ -27061,7 +27061,7 @@ Ref<IndirectExecutionSetEXTHandle> pIndirectExecutionSet
2706127061
MinVersion = "1.0"
2706227062
)]
2706327063
[NativeFunction("vulkan", EntryPoint = "vkCreateInstance")]
27064-
Result CreateInstance(
27064+
Result CreateInstanceInternal(
2706527065
[NativeTypeName("const VkInstanceCreateInfo *")] InstanceCreateInfo* pCreateInfo,
2706627066
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
2706727067
[NativeTypeName("VkInstance *")] InstanceHandle* pInstance

sources/Vulkan/Vulkan/Vulkan/Vk.gen.cs

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11299,7 +11299,7 @@ Ref<DescriptorUpdateTemplateHandle> pDescriptorUpdateTemplate
1129911299
],
1130011300
MinVersion = "1.0"
1130111301
)]
11302-
public static extern Result CreateDevice(
11302+
private static extern Result CreateDeviceInternal(
1130311303
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
1130411304
[NativeTypeName("const VkDeviceCreateInfo *")] DeviceCreateInfo* pCreateInfo,
1130511305
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
@@ -12023,7 +12023,7 @@ Ref<IndirectExecutionSetEXTHandle> pIndirectExecutionSet
1202312023
],
1202412024
MinVersion = "1.0"
1202512025
)]
12026-
public static extern Result CreateInstance(
12026+
private static extern Result CreateInstanceInternal(
1202712027
[NativeTypeName("const VkInstanceCreateInfo *")] InstanceCreateInfo* pCreateInfo,
1202812028
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
1202912029
[NativeTypeName("VkInstance *")] InstanceHandle* pInstance
@@ -37797,12 +37797,12 @@ Ref<DescriptorUpdateTemplateHandle> pDescriptorUpdateTemplate
3779737797
[MethodImpl(
3779837798
MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization
3779937799
)]
37800-
public Result CreateDevice(
37800+
public Result CreateDeviceInternal(
3780137801
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
3780237802
[NativeTypeName("const VkDeviceCreateInfo *")] DeviceCreateInfo* pCreateInfo,
3780337803
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
3780437804
[NativeTypeName("VkDevice *")] DeviceHandle* pDevice
37805-
) => T.CreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
37805+
) => T.CreateDeviceInternal(physicalDevice, pCreateInfo, pAllocator, pDevice);
3780637806

3780737807
[SupportedApiProfile(
3780837808
"vulkan",
@@ -38408,11 +38408,11 @@ Ref<IndirectExecutionSetEXTHandle> pIndirectExecutionSet
3840838408
[MethodImpl(
3840938409
MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization
3841038410
)]
38411-
public Result CreateInstance(
38411+
public Result CreateInstanceInternal(
3841238412
[NativeTypeName("const VkInstanceCreateInfo *")] InstanceCreateInfo* pCreateInfo,
3841338413
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
3841438414
[NativeTypeName("VkInstance *")] InstanceHandle* pInstance
38415-
) => T.CreateInstance(pCreateInfo, pAllocator, pInstance);
38415+
) => T.CreateInstanceInternal(pCreateInfo, pAllocator, pInstance);
3841638416

3841738417
[SupportedApiProfile(
3841838418
"vulkan",
@@ -51462,6 +51462,33 @@ public Result WriteMicromapsPropertiesEXT(
5146251462
);
5146351463
}
5146451464

51465+
private InstanceHandle? _currentInstance;
51466+
private DeviceHandle? _currentDevice;
51467+
public InstanceHandle? CurrentInstance
51468+
{
51469+
get => _currentInstance;
51470+
set
51471+
{
51472+
if (_currentInstance != null && _currentInstance != value)
51473+
throw new InvalidOperationException(
51474+
"CurrentInstance has already been set. Please create a new API instance so that the loaded function pointers can be kept separate."
51475+
);
51476+
_currentInstance = value;
51477+
}
51478+
}
51479+
public DeviceHandle? CurrentDevice
51480+
{
51481+
get => _currentDevice;
51482+
set
51483+
{
51484+
if (_currentDevice != null && _currentDevice != value)
51485+
throw new InvalidOperationException(
51486+
"CurrentDevice has already been set. Please create a new API instance so that the loaded function pointers can be kept separate."
51487+
);
51488+
_currentDevice = value;
51489+
}
51490+
}
51491+
5146551492
[NativeTypeName("const VkTensorCreateFlagBitsARM")]
5146651493
[SupportedApiProfile("vulkan")]
5146751494
public const ulong TensorCreateMutableFormatBitARM = 0x00000001UL;
@@ -59601,6 +59628,45 @@ public Result WriteMicromapsPropertiesEXT(
5960159628
)]
5960259629
public static Utf8String ExtMeshShaderExtensionName => "VK_EXT_mesh_shader"u8;
5960359630

59631+
[SupportedApiProfile(
59632+
"vulkan",
59633+
["VK_VERSION_1_0", "VK_VERSION_1_1", "VK_VERSION_1_2", "VK_VERSION_1_3", "VK_VERSION_1_4"],
59634+
MinVersion = "1.0"
59635+
)]
59636+
public static Result CreateDevice(
59637+
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
59638+
[NativeTypeName("const VkDeviceCreateInfo *")] DeviceCreateInfo* pCreateInfo,
59639+
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
59640+
[NativeTypeName("VkDevice *")] DeviceHandle* pDevice
59641+
)
59642+
{
59643+
Result result = CreateDeviceInternal(physicalDevice, pCreateInfo, pAllocator, pDevice);
59644+
if (result == Result.Success)
59645+
{
59646+
CurrentDevice = *pDevice;
59647+
}
59648+
return result;
59649+
}
59650+
59651+
[SupportedApiProfile(
59652+
"vulkan",
59653+
["VK_VERSION_1_0", "VK_VERSION_1_1", "VK_VERSION_1_2", "VK_VERSION_1_3", "VK_VERSION_1_4"],
59654+
MinVersion = "1.0"
59655+
)]
59656+
public static Result CreateInstance(
59657+
[NativeTypeName("const VkInstanceCreateInfo *")] InstanceCreateInfo* pCreateInfo,
59658+
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
59659+
[NativeTypeName("VkInstance *")] InstanceHandle* pInstance
59660+
)
59661+
{
59662+
Result result = CreateInstanceInternal(pCreateInfo, pAllocator, pInstance);
59663+
if (result == Result.Success)
59664+
{
59665+
CurrentInstance = *pInstance;
59666+
}
59667+
return result;
59668+
}
59669+
5960459670
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
5960559671
Result IVk.AcquireDrmDisplayEXT(
5960659672
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
@@ -79237,7 +79303,7 @@ Ref<DescriptorUpdateTemplateHandle> pDescriptorUpdateTemplate
7923779303
);
7923879304

7923979305
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
79240-
Result IVk.CreateDevice(
79306+
Result IVk.CreateDeviceInternal(
7924179307
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
7924279308
[NativeTypeName("const VkDeviceCreateInfo *")] DeviceCreateInfo* pCreateInfo,
7924379309
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
@@ -79263,12 +79329,12 @@ _slots[325] is not null and var loadedFnPtr
7926379329
)]
7926479330
[NativeFunction("vulkan", EntryPoint = "vkCreateDevice")]
7926579331
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
79266-
public static Result CreateDevice(
79332+
private static Result CreateDeviceInternal(
7926779333
[NativeTypeName("VkPhysicalDevice")] PhysicalDeviceHandle physicalDevice,
7926879334
[NativeTypeName("const VkDeviceCreateInfo *")] DeviceCreateInfo* pCreateInfo,
7926979335
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
7927079336
[NativeTypeName("VkDevice *")] DeviceHandle* pDevice
79271-
) => DllImport.CreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
79337+
) => DllImport.CreateDeviceInternal(physicalDevice, pCreateInfo, pAllocator, pDevice);
7927279338

7927379339
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
7927479340
Result IVk.CreateDevice(
@@ -80341,7 +80407,7 @@ Ref<IndirectExecutionSetEXTHandle> pIndirectExecutionSet
8034180407
);
8034280408

8034380409
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
80344-
Result IVk.CreateInstance(
80410+
Result IVk.CreateInstanceInternal(
8034580411
[NativeTypeName("const VkInstanceCreateInfo *")] InstanceCreateInfo* pCreateInfo,
8034680412
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
8034780413
[NativeTypeName("VkInstance *")] InstanceHandle* pInstance
@@ -80365,11 +80431,11 @@ _slots[339] is not null and var loadedFnPtr
8036580431
)]
8036680432
[NativeFunction("vulkan", EntryPoint = "vkCreateInstance")]
8036780433
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
80368-
public static Result CreateInstance(
80434+
private static Result CreateInstanceInternal(
8036980435
[NativeTypeName("const VkInstanceCreateInfo *")] InstanceCreateInfo* pCreateInfo,
8037080436
[NativeTypeName("const VkAllocationCallbacks *")] AllocationCallbacks* pAllocator,
8037180437
[NativeTypeName("VkInstance *")] InstanceHandle* pInstance
80372-
) => DllImport.CreateInstance(pCreateInfo, pAllocator, pInstance);
80438+
) => DllImport.CreateInstanceInternal(pCreateInfo, pAllocator, pInstance);
8037380439

8037480440
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
8037580441
Result IVk.CreateInstance(

0 commit comments

Comments
 (0)