Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion generator.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,23 @@
"mode": "Default",
"path": "src/Vulkan",
"licenseFile": "build/LICENSE_HEADER.txt",
"props": "build/props/bindings.props"
"props": "build/props/bindings.props",
"inject": [
{
"functions": [
"vkCreateInstance"
],
"stage": "end",
"code": "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }"
},
{
"functions": [
"vkCreateDevice"
],
"stage": "end",
"code": "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }"
}
]
},
"namespace": "Silk.NET.Vulkan",
"extensionsNamespace": "Silk.NET.Vulkan.Extensions",
Expand Down
25 changes: 25 additions & 0 deletions src/Core/Silk.NET.BuildTools/Bind/ClassWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public static void WriteMixedModeClasses(this Project project, Profile profile,
.ToArray();
foreach (var function in allFunctions)
{
AddInjectionAttributes(function, task);

if (!string.IsNullOrWhiteSpace(function.PreprocessorConditions))
{
sw.WriteLine($"#if {function.PreprocessorConditions}");
Expand Down Expand Up @@ -289,6 +291,8 @@ public static void WriteMixedModeClasses(this Project project, Profile profile,
sw.WriteLine($" public const string ExtensionName = \"{key}\";");
foreach (var function in i.Functions)
{
AddInjectionAttributes(function, task);

if (!string.IsNullOrWhiteSpace(function.PreprocessorConditions))
{
sw.WriteLine($"#if {function.PreprocessorConditions}");
Expand Down Expand Up @@ -418,5 +422,26 @@ static void FinishOverloadsFile(StreamWriter? swOverloads)
swOverloads?.Dispose();
}
}

private static void AddInjectionAttributes(Function function, BindState state)
{
foreach (var injection in state.Task.OutputOpts.Injections ?? Enumerable.Empty<Injection>())
{
if (injection.FunctionNativeNames.Contains(function.NativeName))
{
function.Attributes.Add
(
new()
{
Name = "Inject", Arguments = new()
{
$"(SilkTouchStage) {(int) injection.Stage}",
$"\"{injection.Code.Replace("\"", "\\\"").Replace("\\", "\\\\")}\""
}
}
);
}
}
}
}
}
15 changes: 15 additions & 0 deletions src/Core/Silk.NET.BuildTools/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Silk.NET.BuildTools.Common;
using Silk.NET.Core.Attributes;

namespace Silk.NET.BuildTools
{
Expand Down Expand Up @@ -98,6 +99,20 @@ public struct OutputOptions
public string Props { get; set; }

[JsonProperty("conditional")] public Dictionary<string, string[]> ConditionalFunctions { get; set; }
[JsonProperty("inject")] public Injection[]? Injections { get; set; }
}

public struct Injection
{
[JsonProperty("functions")]
public string[] FunctionNativeNames { get; set; }

[JsonProperty("stage")]
[JsonConverter(typeof(StringEnumConverter))]
public SilkTouchStage Stage { get; set; }

[JsonProperty("code")]
public string Code { get; set; }
}

public enum ConverterMode
Expand Down
1 change: 1 addition & 0 deletions src/Core/Silk.NET.BuildTools/Silk.NET.BuildTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<Folder Include="Pipeline" />
<Compile Include="..\Silk.NET.Core\Attributes\InjectAttribute.cs" />
</ItemGroup>

<Import Project="..\..\..\build\props\common.props" />
Expand Down
23 changes: 17 additions & 6 deletions src/Core/Silk.NET.SilkTouch/Middlewares/InjectMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Silk.NET.Core.Attributes;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Silk.NET.SilkTouch
{
Expand All @@ -26,19 +27,29 @@ public static void InjectMiddleware(ref IMarshalContext ctx, Action next)
{
var injectPoint = (SilkTouchStage) injectData.ConstructorArguments[0].Value!;
var code = (string)injectData.ConstructorArguments[1].Value!;
var syntax = SyntaxFactory.ParseStatement(code);
ctx.AddSideEffectToStage(injectPoint, ctx =>
{
if (ctx.ResultVariable.HasValue)
{
var c = code.Replace
code = code.Replace
(
"{RESULT}",
SyntaxFactory.ParenthesizedExpression(ctx.ResolveVariable(ctx.ResultVariable.Value).Value).NormalizeWhitespace().ToFullString()
"%$RESULT$%",
ParenthesizedExpression(ctx.ResolveVariable(ctx.ResultVariable.Value).Value).NormalizeWhitespace().ToFullString()
);
return SyntaxFactory.ParseStatement(c);
}
return syntax;

for (var i = 0; i < ctx.ParameterVariables.Length; i++)
{
code = code.Replace
(
$"%$PARAM({ctx.MethodSymbol.Parameters[i].Name})$%",
ParenthesizedExpression(ctx.ResolveVariable(ctx.ParameterVariables[i]).Value)
.NormalizeWhitespace()
.ToFullString()
);
}

return ParseStatement(code);
});
}

Expand Down
7 changes: 4 additions & 3 deletions src/Core/Silk.NET.SilkTouch/NameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ private static string ToAlphabetString(int i)

public static string Name(string suggestion)
{
return ToAlphabetString(_c++);
#if DEBUG
// BUG the name shortener for some reason is causing duplicate member bugs. such as:
// GL.3491994.gen.cs(107452,35): Error CS0111 : Type 'GL.TXDY' already defines a member called 'VNWU' with
// the same parameter types
// return ToAlphabetString(_c++);
return suggestion;
#endif
}
}
}
2 changes: 2 additions & 0 deletions src/Lab/VulkanTriangle/HelloTriangleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ private unsafe void CreateLogicalDevice()
{
throw new NotSupportedException("KHR_swapchain extension not found.");
}

Console.WriteLine($"{_vk.CurrentInstance?.Handle} {_vk.CurrentDevice?.Handle}");
}

private unsafe void CreateSwapChain()
Expand Down
1 change: 1 addition & 0 deletions src/Lab/VulkanTriangle/VulkanTriangle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>9</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 9 additions & 2 deletions src/Vulkan/Silk.NET.Vulkan/Vk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public partial class Vk
public Instance? CurrentInstance
{
get => _currentInstance;
set => SwapVTable(_vTables.GetOrAdd((_currentInstance = value, _currentDevice), _ => CreateVTable()));
set => SwapVTable(_vTables.GetOrAdd((_currentInstance = value, _currentDevice), _ => VulkanCreateVTable()));
}
public Device? CurrentDevice
{
get => _currentDevice;
set => SwapVTable(_vTables.GetOrAdd((_currentInstance, _currentDevice = value), _ => CreateVTable()));
set => SwapVTable(_vTables.GetOrAdd((_currentInstance, _currentDevice = value), _ => VulkanCreateVTable()));
}
public static Version32 Version10 => new Version32(1, 0, 0);
public static Version32 Version11 => new Version32(1, 1, 0);
Expand Down Expand Up @@ -362,5 +362,12 @@ public void PurgeExtensionCache()
_cachedDeviceExtensionsLock.ExitWriteLock();
}
}

private IVTable VulkanCreateVTable()
{
var ret = CreateVTable();
ret.Initialize(Context, CoreGetSlotCount());
return ret;
}
}
}
16 changes: 16 additions & 0 deletions src/Vulkan/Silk.NET.Vulkan/Vk.gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,34 +1268,42 @@ public unsafe partial class Vk : NativeAPI
public partial Result CreateDescriptorSetLayout([Count(Count = 0)] Device device, [Count(Count = 0), Flow(FlowDirection.In)] in DescriptorSetLayoutCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out DescriptorSetLayout pSetLayout);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public unsafe partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] DeviceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Device* pDevice);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public unsafe partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] DeviceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Device pDevice);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public unsafe partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] DeviceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Device* pDevice);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public unsafe partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] DeviceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Device pDevice);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public unsafe partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] in DeviceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Device* pDevice);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public unsafe partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] in DeviceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Device pDevice);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public unsafe partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] in DeviceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Device* pDevice);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentDevice = *%$PARAM(pDevice)$%; }")]
[NativeApi(EntryPoint = "vkCreateDevice")]
public partial Result CreateDevice([Count(Count = 0)] PhysicalDevice physicalDevice, [Count(Count = 0), Flow(FlowDirection.In)] in DeviceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Device pDevice);

Expand Down Expand Up @@ -1492,34 +1500,42 @@ public unsafe partial class Vk : NativeAPI
public partial Result CreateImageView([Count(Count = 0)] Device device, [Count(Count = 0), Flow(FlowDirection.In)] in ImageViewCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out ImageView pView);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public unsafe partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] InstanceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Instance* pInstance);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public unsafe partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] InstanceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Instance pInstance);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public unsafe partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] InstanceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Instance* pInstance);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public unsafe partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] InstanceCreateInfo* pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Instance pInstance);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public unsafe partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] in InstanceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Instance* pInstance);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public unsafe partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] in InstanceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] AllocationCallbacks* pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Instance pInstance);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public unsafe partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] in InstanceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] Instance* pInstance);

/// <summary>To be documented.</summary>
[Inject((SilkTouchStage) 6, "if (%$RESULT$% == Result.Success) { CurrentInstance = *%$PARAM(pInstance)$%; }")]
[NativeApi(EntryPoint = "vkCreateInstance")]
public partial Result CreateInstance([Count(Count = 0), Flow(FlowDirection.In)] in InstanceCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.In)] in AllocationCallbacks pAllocator, [Count(Count = 0), Flow(FlowDirection.Out)] out Instance pInstance);

Expand Down
Loading