Skip to content

Commit c10d8f6

Browse files
committed
Handle Device and Instance as parameter in VkInstanceApi and VkDeviceApi, bump version to 3.2.0
1 parent b9b56b6 commit c10d8f6

15 files changed

Lines changed: 1793 additions & 1719 deletions

src/Generator/CsCodeGenerator.Commands.cs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -341,22 +341,22 @@ [.. usings]
341341
CppFunction cppFunction = command.Value;
342342

343343
bool canUseOut = _outReturnFunctions.Contains(cppFunction.Name);
344-
WriteFunctionInvocation(writer, cppFunction, false, instance: true);
344+
WriteFunctionInvocation(writer, cppFunction, false, instance: true, firstParameterType: "VkInstance", firstParameterValue: "Instance");
345345

346346
if (command.Key.StartsWith("vkCreate")
347347
&& command.Key != "vkCreateDeferredOperationKHR")
348348
{
349-
WriteFunctionInvocation(writer, cppFunction, false, true, instance: true);
349+
WriteFunctionInvocation(writer, cppFunction, false, true, instance: true, firstParameterType: "VkInstance", firstParameterValue: "Instance");
350350
}
351351

352352
if (canUseOut)
353353
{
354-
WriteFunctionInvocation(writer, cppFunction, true, instance: true);
354+
WriteFunctionInvocation(writer, cppFunction, true, instance: true, firstParameterType: "VkInstance", firstParameterValue: "Instance");
355355

356356
if (command.Key.StartsWith("vkCreate")
357357
&& command.Key != "vkCreateDeferredOperationKHR")
358358
{
359-
WriteFunctionInvocation(writer, cppFunction, true, true, instance: true);
359+
WriteFunctionInvocation(writer, cppFunction, true, true, instance: true, firstParameterType: "VkInstance", firstParameterValue: "Instance");
360360
}
361361
}
362362
}
@@ -388,22 +388,22 @@ [.. usings]
388388
CppFunction cppFunction = command.Value;
389389

390390
bool canUseOut = _outReturnFunctions.Contains(cppFunction.Name);
391-
WriteFunctionInvocation(writer, cppFunction, false, instance: true);
391+
WriteFunctionInvocation(writer, cppFunction, false, instance: true, firstParameterType: "VkDevice", firstParameterValue: "Device");
392392

393393
if (command.Key.StartsWith("vkCreate")
394394
&& command.Key != "vkCreateDeferredOperationKHR")
395395
{
396-
WriteFunctionInvocation(writer, cppFunction, false, true, instance: true);
396+
WriteFunctionInvocation(writer, cppFunction, false, true, instance: true, firstParameterType: "VkDevice", firstParameterValue: "Device");
397397
}
398398

399399
if (canUseOut)
400400
{
401-
WriteFunctionInvocation(writer, cppFunction, true, instance: true);
401+
WriteFunctionInvocation(writer, cppFunction, true, instance: true, firstParameterType: "VkDevice", firstParameterValue: "Device");
402402

403403
if (command.Key.StartsWith("vkCreate")
404404
&& command.Key != "vkCreateDeferredOperationKHR")
405405
{
406-
WriteFunctionInvocation(writer, cppFunction, true, true, instance: true);
406+
WriteFunctionInvocation(writer, cppFunction, true, true, instance: true, firstParameterType: "VkDevice", firstParameterValue: "Device");
407407
}
408408
}
409409
}
@@ -458,14 +458,16 @@ private void WriteFunctionInvocation(CodeWriter writer,
458458
CppFunction cppFunction,
459459
bool canUseOut,
460460
bool inParameters = false,
461-
bool instance = false)
461+
bool instance = false,
462+
string? firstParameterType = default,
463+
string? firstParameterValue = default)
462464
{
463465
bool hasAllocationCallbacks = _options.IsVulkan ? HasAllocationCallbacks(cppFunction) : false;
464-
WriteFunctionInvocationInner(writer, cppFunction, canUseOut, inParameters, instance, true);
466+
WriteFunctionInvocationInner(writer, cppFunction, canUseOut, inParameters, instance, true, firstParameterType, firstParameterValue);
465467

466468
if (hasAllocationCallbacks)
467469
{
468-
WriteFunctionInvocationInner(writer, cppFunction, canUseOut, inParameters, instance, false);
470+
WriteFunctionInvocationInner(writer, cppFunction, canUseOut, inParameters, instance, false, firstParameterType, firstParameterValue);
469471
}
470472
}
471473

@@ -474,10 +476,12 @@ private void WriteFunctionInvocationInner(CodeWriter writer,
474476
bool canUseOut,
475477
bool inParameters,
476478
bool instance,
477-
bool skipAllocationCallbacks)
479+
bool skipAllocationCallbacks,
480+
string? firstParameterType = default,
481+
string? firstParameterValue = default)
478482
{
479483
string returnCsName = GetCsTypeName(cppFunction.ReturnType);
480-
string argumentsString = GetParameterSignature(cppFunction, canUseOut, inParameters, skipAllocationCallbacks);
484+
string argumentsString = GetParameterSignature(cppFunction, canUseOut, inParameters, skipAllocationCallbacks, firstParameterType);
481485
string modifier = "public";
482486
if (instance == false)
483487
modifier += " static";
@@ -584,7 +588,17 @@ private void WriteFunctionInvocationInner(CodeWriter writer,
584588
}
585589
else
586590
{
587-
writer.Write($"{paramCsName}");
591+
if (index == 0
592+
&& !string.IsNullOrEmpty(firstParameterType)
593+
&& !string.IsNullOrEmpty(firstParameterValue)
594+
&& paramCsTypeName == firstParameterType)
595+
{
596+
writer.Write($"{firstParameterValue}");
597+
}
598+
else
599+
{
600+
writer.Write($"{paramCsName}");
601+
}
588602
}
589603

590604
if (index < cppFunction.Parameters.Count - 1)
@@ -665,18 +679,22 @@ private bool IsInstanceFunction(string name)
665679
return _instanceFunctions.Contains(name);
666680
}
667681

668-
public static string GetParameterSignature(CppFunction cppFunction, bool canUseOut, bool inParameters, bool skipAllocationCallbacks = false)
682+
public static string GetParameterSignature(CppFunction cppFunction,
683+
bool canUseOut,
684+
bool inParameters,
685+
bool skipAllocationCallbacks = false,
686+
string? firstParameterType = default)
669687
{
670-
return GetParameterSignature(cppFunction.Parameters, canUseOut, inParameters, skipAllocationCallbacks);
688+
return GetParameterSignature(cppFunction.Parameters, canUseOut, inParameters, skipAllocationCallbacks, firstParameterType);
671689
}
672690

673691
private static string GetParameterSignature(IList<CppParameter> parameters,
674692
bool canUseOut,
675693
bool inParameters,
676-
bool skipAllocationCallbacks = false)
694+
bool skipAllocationCallbacks = false,
695+
string? firstParameterType = default)
677696
{
678697
StringBuilder argumentBuilder = new();
679-
int index = 0;
680698

681699
IList<CppParameter> processParameters;
682700
if (skipAllocationCallbacks)
@@ -687,6 +705,13 @@ private static string GetParameterSignature(IList<CppParameter> parameters,
687705
{
688706
string paramCsTypeName = GetCsTypeName(cppParameter.Type);
689707

708+
if (processParameters.Count == 0
709+
&& !string.IsNullOrEmpty(firstParameterType)
710+
&& paramCsTypeName == firstParameterType)
711+
{
712+
continue;
713+
}
714+
690715
if (paramCsTypeName == "VkAllocationCallbacks*")
691716
{
692717
continue;
@@ -695,11 +720,30 @@ private static string GetParameterSignature(IList<CppParameter> parameters,
695720
processParameters.Add(cppParameter);
696721
}
697722
}
723+
else if (!string.IsNullOrEmpty(firstParameterType))
724+
{
725+
processParameters = [];
726+
727+
foreach (CppParameter cppParameter in parameters)
728+
{
729+
string paramCsTypeName = GetCsTypeName(cppParameter.Type);
730+
731+
if (processParameters.Count == 0 &&
732+
paramCsTypeName == firstParameterType)
733+
{
734+
continue;
735+
}
736+
737+
processParameters.Add(cppParameter);
738+
}
739+
}
698740
else
699741
{
700742
processParameters = parameters;
701743
}
702744

745+
746+
int index = 0;
703747
foreach (CppParameter cppParameter in processParameters)
704748
{
705749
string direction = string.Empty;

src/Generator/CsCodeGenerator.FormatHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ partial class CsCodeGenerator
77
{
88
private void GenerateFormatHelpers()
99
{
10-
if (_vulkanSpecification == null)
11-
throw new InvalidOperationException();
10+
if (_vulkanSpecification is null)
11+
return;
1212

1313
// Generate Functions
1414
using CodeWriter writer = new(Path.Combine(_options.OutputPath, "VkFormatUtils.cs"),

src/Generator/CsCodeGenerator.HelperCommands.cs

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ private void GenerateHelperCommands(CppCompilation compilation)
101101
private void GenerateHelpers(List<CppFunction> commands, bool instance, string className, string fileName)
102102
{
103103
string methodModifier = instance ? string.Empty : "static ";
104+
string? firstParameterType = default;
105+
if (instance)
106+
{
107+
firstParameterType = className == "VkInstanceApi" ? "VkInstance" : "VkDevice";
108+
}
104109

105110
// Generate Functions
106111
using (CodeWriter writer = new(Path.Combine(_options.OutputPath, $"{fileName}.cs"),
@@ -122,35 +127,48 @@ private void GenerateHelpers(List<CppFunction> commands, bool instance, string c
122127
bool hasArrayReturn = false;
123128
int countArgumentArrayIndex = 0;
124129

125-
foreach (CppParameter parameter in function.Parameters)
130+
foreach (CppParameter cppParameter in function.Parameters)
126131
{
127-
if (parameter.Name.EndsWith("count", StringComparison.OrdinalIgnoreCase))
132+
if (cppParameter.Name.EndsWith("count", StringComparison.OrdinalIgnoreCase))
128133
{
129-
countParameterName = GetParameterName(parameter.Name);
134+
countParameterName = GetParameterName(cppParameter.Name);
130135
continue;
131136
}
132137

133-
if (CanBeUsedAsInOut(parameter.Type, true, out CppType? cppTypeDeclaration))
138+
if (CanBeUsedAsInOut(cppParameter.Type, true, out CppType? cppTypeDeclaration))
134139
{
135-
returnVariableName = GetParameterName(parameter.Name);
140+
returnVariableName = GetParameterName(cppParameter.Name);
136141
returnArrayTypeName = GetCsTypeName(cppTypeDeclaration);
137142
hasArrayReturn = true;
138-
countArgumentArrayIndex = function.Parameters.IndexOf(parameter) - 1;
143+
countArgumentArrayIndex = function.Parameters.IndexOf(cppParameter) - 1;
139144
continue;
140145
}
141146

142-
if (parameter.Type is CppPointerType pointerType
147+
if (cppParameter.Type is CppPointerType pointerType
143148
&& pointerType.ElementType is CppQualifiedType qualifiedType
144149
&& !string.IsNullOrEmpty(countParameterName))
145150
{
146-
returnVariableName = GetParameterName(parameter.Name);
151+
returnVariableName = GetParameterName(cppParameter.Name);
147152
returnArrayTypeName = GetCsTypeName(qualifiedType);
148153
hasArrayReturn = false;
149-
countArgumentArrayIndex = function.Parameters.IndexOf(parameter) - 1;
154+
countArgumentArrayIndex = function.Parameters.IndexOf(cppParameter) - 1;
150155
continue;
151156
}
152157

153-
newParameters.Add(parameter);
158+
string paramCsTypeName = GetCsTypeName(cppParameter.Type);
159+
if (newParameters.Count == 0
160+
&& !string.IsNullOrEmpty(firstParameterType)
161+
&& firstParameterType == paramCsTypeName)
162+
{
163+
if (className == "VkInstanceApi")
164+
newParameters.Add(new CppParameter(cppParameter.Type, "Instance"));
165+
else if (className == "VkDeviceApi")
166+
newParameters.Add(new CppParameter(cppParameter.Type, "Device"));
167+
}
168+
else
169+
{
170+
newParameters.Add(cppParameter);
171+
}
154172
}
155173

156174
string csCountParameterType = "uint";
@@ -166,6 +184,11 @@ private void GenerateHelpers(List<CppFunction> commands, bool instance, string c
166184
List<string> invokeSingleElementParameters = [];
167185
List<string> invokeElementsParameters = [];
168186

187+
if (function.Name == "vkFlushMappedMemoryRanges")
188+
{
189+
190+
}
191+
169192
foreach (CppParameter cppParameter in newParameters)
170193
{
171194
string paramCsTypeName = GetCsTypeName(cppParameter.Type);
@@ -181,6 +204,16 @@ private void GenerateHelpers(List<CppFunction> commands, bool instance, string c
181204
csCountParameterType);
182205
}
183206

207+
if (index == 0
208+
&& !string.IsNullOrEmpty(firstParameterType)
209+
&& firstParameterType == paramCsTypeName)
210+
{
211+
invokeSingleElementParameters.Add(paramCsName);
212+
invokeElementsParameters.Add(paramCsName);
213+
index++;
214+
continue;
215+
}
216+
184217
argumentsSingleElementBuilder.Append(argumentSignature);
185218
argumentsSpanBuilder.Append(argumentSignature);
186219
if (index < newParameters.Count - 1)
@@ -235,7 +268,7 @@ private void GenerateHelpers(List<CppFunction> commands, bool instance, string c
235268
}
236269
else
237270
{
238-
string argumentsString = GetParameterSignature(newParameters, false, false);
271+
string argumentsString = GetParameterSignature(newParameters, false, false, firstParameterType: firstParameterType);
239272
string extraArgs = string.Empty;
240273
if (!string.IsNullOrEmpty(argumentsString))
241274
{
@@ -250,11 +283,12 @@ private void GenerateHelpers(List<CppFunction> commands, bool instance, string c
250283
writer.WriteLine($"{countParameterName} = default;");
251284
using (writer.PushBlock($"fixed ({csCountParameterType}* {countParameterName}Ptr = &{countParameterName})"))
252285
{
253-
List<string> invokeParameters = new(newParameters.Select(item => GetParameterName(item.Name)))
254-
{
255-
$"{countParameterName}Ptr",
256-
"default"
257-
};
286+
List<string> invokeParameters =
287+
[
288+
.. newParameters.Select(item => GetParameterName(item.Name)),
289+
$"{countParameterName}Ptr",
290+
"default"
291+
];
258292
EmitInvoke(writer, function, invokeParameters,
259293
handleCheckResult: false,
260294
emitReturn: true);
@@ -298,7 +332,7 @@ private static void AppendCountParameter(
298332
string csCountParameterType)
299333
{
300334
var singleName = GetSingleName(returnVariableName);
301-
if (singleElement)
335+
if (singleElement && argumentsSingleElementBuilder.Length > 0)
302336
{
303337
argumentsSingleElementBuilder.Append(", ");
304338
argumentsSpanBuilder.Append(", ");

src/Generator/CsCodeGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ public void Generate(CppCompilation compilation)
127127
GenerateCommands(compilation);
128128

129129
if (_options.IsVulkan)
130+
{
130131
GenerateHelperCommands(compilation);
132+
}
131133

132-
if (_vulkanSpecification != null)
133-
GenerateFormatHelpers();
134+
GenerateFormatHelpers();
134135
}
135136

136137
public static void AddCsMapping(string typeName, string csTypeName)

0 commit comments

Comments
 (0)