Skip to content

Commit

Permalink
Merge branch 'main' into hotfix/scalar_as_optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Perksey authored Jan 15, 2023
2 parents 1c78d04 + db1de41 commit b5cd661
Show file tree
Hide file tree
Showing 33 changed files with 1,454 additions and 59 deletions.
1 change: 1 addition & 0 deletions .github/workflows/glfw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- "main"
paths:
- "build/submodules/GLFW"
- "build/nuke/Build.Native.cs"
jobs:
Build:
strategy:
Expand Down
78 changes: 44 additions & 34 deletions build/nuke/Build.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ string AndroidHome
.AssertZeroExitCode();
InheritedShell(build, GLFWPath)
.AssertZeroExitCode();
CopyAll(@out.GlobFiles("src/Release/glfw3.dll"), runtimes / "win-x64" / "native");
EnsureCleanDirectory(@out);
Expand All @@ -308,6 +309,15 @@ string AndroidHome
.AssertZeroExitCode();
CopyAll(@out.GlobFiles("src/Release/glfw3.dll"), runtimes / "win-x86" / "native");
EnsureCleanDirectory(@out);
InheritedShell($"{prepare} -A arm64", GLFWPath)
.AssertZeroExitCode();
InheritedShell(build, GLFWPath)
.AssertZeroExitCode();
CopyAll(@out.GlobFiles("src/Release/glfw3.dll"), runtimes / "win-arm64" / "native");
}
else if (OperatingSystem.IsLinux())
{
Expand Down Expand Up @@ -345,40 +355,40 @@ string AndroidHome
Target VulkanLoader => CommonTarget
(
x => x.Before(Compile)
.After(Clean)
.Executes
(
() =>
{
var @out = VulkanLoaderPath / "build";
EnsureCleanDirectory(@out);
var abi = OperatingSystem.IsWindows() ? " -DCMAKE_GENERATOR_PLATFORM=Win32" : string.Empty;
InheritedShell
(
$"cmake -S. -Bbuild -DUPDATE_DEPS=On -DCMAKE_BUILD_TYPE=Release{abi}",
VulkanLoaderPath
)
.AssertZeroExitCode();
InheritedShell($"cmake --build build --config Release{JobsArg}", VulkanLoaderPath)
.AssertZeroExitCode();
var runtimes = RootDirectory / "src" / "Native" / "Silk.NET.Vulkan.Loader.Native" / "runtimes";
if (OperatingSystem.IsWindows())
{
CopyAll(@out.GlobFiles("loader/Release/vulkan-1.dll"), runtimes / "win-x64" / "native");
CopyAll(@out.GlobFiles("loader/Release/vulkan-1.dll"), runtimes / "win-x86" / "native");
}
else
{
CopyAll
(
@out.GlobFiles("loader/libvulkan.so", "loader/libvulkan.dylib"),
runtimes / (OperatingSystem.IsMacOS() ? "osx-x64" : "linux-x64") / "native"
);
}
PrUpdatedNativeBinary("Vulkan Loader");
}
)
.After(Clean)
.Executes
(
() =>
{
var @out = VulkanLoaderPath / "build";
EnsureCleanDirectory(@out);
var abi = OperatingSystem.IsWindows() ? " -DCMAKE_GENERATOR_PLATFORM=Win32" : string.Empty;
InheritedShell
(
$"cmake -S. -Bbuild -DUPDATE_DEPS=On -DCMAKE_BUILD_TYPE=Release{abi}",
VulkanLoaderPath
)
.AssertZeroExitCode();
InheritedShell($"cmake --build build --config Release{JobsArg}", VulkanLoaderPath)
.AssertZeroExitCode();
var runtimes = RootDirectory / "src" / "Native" / "Silk.NET.Vulkan.Loader.Native" / "runtimes";
if (OperatingSystem.IsWindows())
{
CopyAll(@out.GlobFiles("loader/Release/vulkan-1.dll"), runtimes / "win-x64" / "native");
CopyAll(@out.GlobFiles("loader/Release/vulkan-1.dll"), runtimes / "win-x86" / "native");
}
else
{
CopyAll
(
@out.GlobFiles("loader/libvulkan.so", "loader/libvulkan.dylib"),
runtimes / (OperatingSystem.IsMacOS() ? "osx-x64" : "linux-x64") / "native"
);
}
PrUpdatedNativeBinary("Vulkan Loader");
}
)
);

AbsolutePath AssimpPath => RootDirectory / "build" / "submodules" / "Assimp";
Expand Down
2 changes: 1 addition & 1 deletion build/submodules/SwiftShader
Submodule SwiftShader updated 726 files
106 changes: 106 additions & 0 deletions documentation/proposals/Proposal - Chain Polymorphism.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Summary

Using the managed `Silk.NET.Vulkan.Chain` and its generically-typed descendants can be unwieldy in the specific case of recieving chains
with known starting element(s) but potential unknown later elements, e.g. a `SwapchainCreateInfoKHR` which may or may not have an
`ImageCreateInfo` later in the chain. If use of the managed chain api is desired in this case, the untyped `Chain` type must be used
and the result of its indexer cast to the desired chain start type. This, combined with the public api surface on ``Chain`2`` being
a strict superset of ``Chain`1`` etc., a polymorphic interface is desired for handling variable-size chains.

# Contributors

- [Khitiara](https://github.com/Khitiara)

# Current Status

- [x] Proposed
- [x] Discussed with API Review Board (ARB)
- [x] Approved
- [ ] Implemented

# Design Decisions

- This is mainly useful in cases where the managed api and its automatic management of chain memory is desired. The unmanaged non-generic API
already supports this use-case, so this function is mainly beneficial as method return types where chain extensions may not be known ahead of
time, and thus `out` parameters for all unmanaged chain elements are not suitable.

# Proposed API

A series of generic interfaces are created to accompany the generic `Chain<...>` classes:

```csharp
public unsafe interface IChain<TChain> : IDisposable
where TChain : unmanaged, IChainable {
public BaseInStructure* HeadPtr { get; }
public TChain Head { get; set; }
}

public unsafe interface IChain<TChain, T1> : IChain<TChain>
where TChain : unmanaged, IChainable
where T1 : unmanaged, IChainable {
public BaseInStructure* Item1Ptr { get; }
public T1 Item1 { get; set; }
}

public unsafe interface IChain<TChain, T1, T2> : IChain<TChain, T1>
where TChain : unmanaged, IChainable
where T1 : unmanaged, IChainable
where T2 : unmanaged, IChainable {
public BaseInStructure* Item2Ptr { get; }
public T2 Item2 { get; set; }
}

...
```

and the existing generic `Chain<...>` types are modified to implement these new interfaces:

```csharp
public unsafe sealed class Chain<TChain> : Chain, IEquatable<Chain<TChain>>, IChain<TChain>
where TChain : unmanaged, IChainable {
...
}

public unsafe sealed class Chain<TChain, T1> : Chain, IEquatable<Chain<TChain, T1>>, IChain<TChain, T1>
where TChain : unmanaged, IChainable
where T1 : unmanaged, IChainable {
...
}

public unsafe sealed class Chain<TChain, T1, T2> : Chain, IEquatable<Chain<TChain, T1, T2>>, IChain<TChain, T1, T2>
where TChain : unmanaged, IChainable
where T1 : unmanaged, IChainable
where T2 : unmanaged, IChainable {
...
}

...
```

Usage:

```csharp
public IChain<ImageCreateInfo> DeduceImageCreateInfo(IChain<SwapchainCreateInfoKHR> swapchainCreateInfo) {
ImageCreateInfo createInfo = ...;

// condition here used for example - a TryFind extension or similar may be desirable in the long term
if (swapchainCreateInfo is Chain<SwapchainCreateInfoKHR>(_, formatListCreateInfo)) {
// Chain<TChain, T1> : IChain<TChain, T1> : IChain<TChain>
return Chain.Create(createInfo, formatListCreateInfo);
}

// Chain<TChain> : IChain<TChain>
return Chain.Create(createInfo);
}

...

public void Foo() {
SwapchainCreateInfoKHR sci = ...;
using IChain<ImageCreateInfo> ici = DeduceImageCreateInfo(sci);
ReadOnlySpan<Format> fmts = ici switch {
Chain<ImageCreateInfo, ImageFormatListCreateInfo>(_, var formatList) =>
new ReadOnlySpan<Format>(formatList.PViewFormats, (int)formatList.ViewFormatCount).ToArray(),
Chain<ImageCreateInfo>(var i) => stackalloc[] { i.Format }
}
}
```
3 changes: 2 additions & 1 deletion src/Core/Silk.NET.Core/Native/SilkMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ public static unsafe int StringIntoSpan
{
fixed (byte* bytes = span)
{
Buffer.MemoryCopy(firstChar, bytes, span.Length, input.Length + 1);
Buffer.MemoryCopy(firstChar, bytes, span.Length, input.Length * 2);
((char*)bytes)[input.Length] = default;
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/Lab/Experiments/ImGuiVulkan/ImGuiVulkan.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<None Remove="shader.frag.spv" />
<EmbeddedResource Include="shader.frag.spv" />
Expand All @@ -15,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.86.0" />
<PackageReference Include="ImGui.NET" Version="1.89.2" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 23 additions & 9 deletions src/Lab/Experiments/ImGuiVulkan/ImGuiVulkanApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -75,8 +76,8 @@ public void Run()
private KhrSwapchain _vkSwapchain;
private ExtDebugUtils _debugUtils;
private string[] _validationLayers = { "VK_LAYER_KHRONOS_validation" };
private string[] _instanceExtensions = { ExtDebugUtils.ExtensionName };
private string[] _deviceExtensions = { KhrSwapchain.ExtensionName };
private List<string> _instanceExtensions = new () { ExtDebugUtils.ExtensionName };
private List<string> _deviceExtensions = new () { KhrSwapchain.ExtensionName };

private void InitWindow()
{
Expand Down Expand Up @@ -319,13 +320,21 @@ private unsafe void CleanupSwapchain()
private unsafe void CreateInstance()
{
_vk = Vk.GetApi();


var isMacOs = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

if (isMacOs)
{
_instanceExtensions.Add("VK_KHR_portability_enumeration");
_deviceExtensions.Add("VK_KHR_portability_subset");
}

if (EnableValidationLayers && !CheckValidationLayerSupport())
{
throw new NotSupportedException("Validation layers requested, but not available!");
}

var appInfo = new ApplicationInfo
var appInfo = new ApplicationInfo
{
SType = StructureType.ApplicationInfo,
PApplicationName = (byte*)Marshal.StringToHGlobalAnsi("Hello Triangle"),
Expand All @@ -340,22 +349,27 @@ private unsafe void CreateInstance()
SType = StructureType.InstanceCreateInfo,
PApplicationInfo = &appInfo
};


if (isMacOs)
{
createInfo.Flags = InstanceCreateFlags.EnumeratePortabilityBitKhr;
}

var extensions = _window.VkSurface!.GetRequiredExtensions(out var extCount);
// TODO Review that this count doesn't realistically exceed 1k (recommended max for stackalloc)
// Should probably be allocated on heap anyway as this isn't super performance critical.
var newExtensions = stackalloc byte*[(int)(extCount + _instanceExtensions.Length)];
var newExtensions = stackalloc byte*[(int)(extCount + _instanceExtensions.Count)];
for (var i = 0; i < extCount; i++)
{
newExtensions[i] = extensions[i];
}

for (var i = 0; i < _instanceExtensions.Length; i++)
for (var i = 0; i < _instanceExtensions.Count; i++)
{
newExtensions[extCount + i] = (byte*)SilkMarshal.StringToPtr(_instanceExtensions[i]);
}

extCount += (uint)_instanceExtensions.Length;
extCount += (uint)_instanceExtensions.Count;
createInfo.EnabledExtensionCount = extCount;
createInfo.PpEnabledExtensionNames = newExtensions;

Expand Down Expand Up @@ -615,7 +629,7 @@ private unsafe void CreateLogicalDevice()
createInfo.QueueCreateInfoCount = (uint)uniqueQueueFamilies.Length;
createInfo.PQueueCreateInfos = queueCreateInfos;
createInfo.PEnabledFeatures = &deviceFeatures;
createInfo.EnabledExtensionCount = (uint)_deviceExtensions.Length;
createInfo.EnabledExtensionCount = (uint)_deviceExtensions.Count;

var enabledExtensionNames = SilkMarshal.StringArrayToPtr(_deviceExtensions);
createInfo.PpEnabledExtensionNames = (byte**)enabledExtensionNames;
Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft/Silk.NET.DXGI/NativeWindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ public static unsafe int CreateDxgiSwapchain
IDXGISwapChain1** ppSwapChain
)
{
if (window.Win32.HasValue)
{
return factory->CreateSwapChainForHwnd
(pDevice, window.Win32.Value.Hwnd, pDesc, pFullscreenDesc, pRestrictToOutput, ppSwapChain);
}

if (window.WinRT.HasValue)
{
return factory->CreateSwapChainForCoreWindow
(pDevice, (IUnknown*) window.WinRT.Value, pDesc, pRestrictToOutput, ppSwapChain);
}

if (window.DXHandle.HasValue)
{
return factory->CreateSwapChainForHwnd
(pDevice, window.DXHandle.Value, pDesc, pFullscreenDesc, pRestrictToOutput, ppSwapChain);
}

Throw();
return -1;

static void Throw() => throw new InvalidOperationException
("The given window is neither a Win32 window nor a WinRT CoreWindow");
("The given window has neither a valid DXHandle nor a valid WinRT CoreWindow.");
}

public static unsafe int CreateDxgiSwapchain
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Vulkan/Silk.NET.Vulkan/Chain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Silk.NET.Vulkan;
/// Base class for all <see cref="Chain{T}">Managed Chains</see>.
/// </summary>
public abstract unsafe partial class Chain : IReadOnlyList<IChainable>, IEquatable<Chain>, IDisposable
{
{
/// <summary>
/// Gets a pointer to the current head.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Vulkan/Silk.NET.Vulkan/VulkanLibraryNameContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class VulkanLibraryNameContainer : SearchPathContainer
public override string Linux => "libvulkan.so.1";

/// <inheritdoc />
public override string MacOS => "libMoltenVK.dylib";
public override string MacOS => "libvulkan.dylib";

/// <inheritdoc />
public override string Android => "libvulkan.so";
Expand Down
Loading

0 comments on commit b5cd661

Please sign in to comment.