From d89bade85ae6906a4183717c5bbdf4a59cc67b80 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 12 Oct 2023 23:47:58 +0200 Subject: [PATCH] Use local D3D12 bindings for ComputeSharp --- ComputeSharp.sln | 1 + .../Windows/um/WinBase/Windows.cs | 2 +- src/ComputeSharp/ComputeSharp.csproj | 7 +++--- .../Extensions/Interop/ComPtrExtensions.cs | 2 +- .../IWICStreamExtensions.IBufferWriter.cs | 9 +++---- .../Interop/IWICStreamExtensions.Stream.cs | 9 +++---- .../Graphics/Helpers/WICHelper.cs | 2 ++ .../Interop/Allocation/ID3D12Allocation.cs | 9 +++---- .../Allocation/ID3D12MemoryAllocator.cs | 9 +++---- .../ID3D12MemoryAllocatorFactory.cs | 9 +++---- .../Interop/ID3D12DeviceExtensions.cs | 25 ++++++++----------- .../Shaders/Loading/PipelineDataLoader{T}.cs | 7 +++--- 12 files changed, 37 insertions(+), 54 deletions(-) diff --git a/ComputeSharp.sln b/ComputeSharp.sln index c1f0b638..ee5e503e 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -491,6 +491,7 @@ Global samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{0800bd51-499d-44c2-9417-fd1f7fdfe9c4}*SharedItemsImports = 13 samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{209c95a3-fa53-431b-b688-3299ca6c29d2}*SharedItemsImports = 5 src\ComputeSharp.CodeFixing\ComputeSharp.CodeFixing.projitems*{29f6e256-ba0e-4f3b-854e-50839d67c6dd}*SharedItemsImports = 13 + src\ComputeSharp.Win32.D3D12\ComputeSharp.Win32.D3D12.projitems*{314eb353-e7e3-4146-8111-685cc83ec1de}*SharedItemsImports = 5 src\ComputeSharp.Win32.D2D1\ComputeSharp.Win32.D2D1.projitems*{4d7d9ba8-a821-4b1c-9ee2-cbcf5849906e}*SharedItemsImports = 5 src\ComputeSharp.Win32.D3D12\ComputeSharp.Win32.D3D12.projitems*{514ff00e-bbf0-4d55-a47c-eeeb6208adce}*SharedItemsImports = 13 src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{51d5a422-d3a5-4c4d-8a6d-a048940bf279}*SharedItemsImports = 13 diff --git a/src/ComputeSharp.Win32.D3D12/Windows/um/WinBase/Windows.cs b/src/ComputeSharp.Win32.D3D12/Windows/um/WinBase/Windows.cs index 80b6e027..f98d2ba3 100644 --- a/src/ComputeSharp.Win32.D3D12/Windows/um/WinBase/Windows.cs +++ b/src/ComputeSharp.Win32.D3D12/Windows/um/WinBase/Windows.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Win32; internal static unsafe partial class Windows { [DllImport("kernel32", ExactSpelling = true)] - public static extern BOOL RegisterWaitForSingleObject([NativeTypeName("PHANDLE")] HANDLE* phNewWaitObject, HANDLE hObject, [NativeTypeName("WAITORTIMERCALLBACK")] void* Callback, [NativeTypeName("PVOID")] void* Context, [NativeTypeName("ULONG")] uint dwMilliseconds, [NativeTypeName("ULONG")] uint dwFlags); + public static extern BOOL RegisterWaitForSingleObject([NativeTypeName("PHANDLE")] HANDLE* phNewWaitObject, HANDLE hObject, [NativeTypeName("WAITORTIMERCALLBACK")] delegate* unmanaged Callback, [NativeTypeName("PVOID")] void* Context, [NativeTypeName("ULONG")] uint dwMilliseconds, [NativeTypeName("ULONG")] uint dwFlags); [DllImport("kernel32", ExactSpelling = true)] public static extern BOOL UnregisterWait(HANDLE WaitHandle); diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index efc44dd0..6817795e 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -16,10 +16,6 @@ - - - - @@ -29,4 +25,7 @@ + + + diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs index fed3d8e3..bbfd6b5c 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs @@ -17,7 +17,7 @@ internal static class ComPtrExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ComPtr Move(this in ComPtr ptr) #if NET6_0_OR_GREATER - where T : unmanaged, IUnknown.Interface + where T : unmanaged//, IUnknown.Interface #else where T : unmanaged #endif diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs index 869d41eb..0e006a02 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.IBufferWriter.cs @@ -3,9 +3,6 @@ using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; -#if NET6_0_OR_GREATER -using TerraFX.Interop; -#endif using TerraFX.Interop.Windows; using static TerraFX.Interop.Windows.E; using static TerraFX.Interop.Windows.IID; @@ -40,12 +37,12 @@ unsafe partial class IWICStreamExtensions /// private unsafe partial struct IBufferWriterWrapper #if NET6_0_OR_GREATER - : IUnknown.Interface + //: IUnknown.Interface #endif { #if NET6_0_OR_GREATER - /// - static Guid* INativeGuid.NativeGuid => (Guid*)default(NotSupportedException).Throw(); + ///// + //static Guid* INativeGuid.NativeGuid => (Guid*)default(NotSupportedException).Throw(); #else /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs index c374551a..b5c633c1 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/IWICStreamExtensions.Stream.cs @@ -3,9 +3,6 @@ using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; -#if NET6_0_OR_GREATER -using TerraFX.Interop; -#endif using TerraFX.Interop.Windows; using static TerraFX.Interop.Windows.E; using static TerraFX.Interop.Windows.IID; @@ -42,12 +39,12 @@ internal static unsafe partial class IWICStreamExtensions /// private unsafe partial struct IStreamWrapper #if NET6_0_OR_GREATER - : IUnknown.Interface + //: IUnknown.Interface #endif { #if NET6_0_OR_GREATER - /// - static Guid* INativeGuid.NativeGuid => (Guid*)default(NotSupportedException).Throw(); + ///// + //static Guid* INativeGuid.NativeGuid => (Guid*)default(NotSupportedException).Throw(); #else /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index 2fdca628..56357049 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -6,6 +6,8 @@ using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop.Windows; +#pragma warning disable CA1416 + namespace ComputeSharp.Graphics.Helpers; /// diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs index 4ddfb0ec..e6771698 100644 --- a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs +++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12Allocation.cs @@ -1,9 +1,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -#if NET6_0_OR_GREATER -using TerraFX.Interop; -#endif using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -17,7 +14,7 @@ namespace ComputeSharp.Interop.Allocation; [Guid("D42D5782-2DE7-4539-A817-482E3AA01E2E")] internal unsafe struct ID3D12Allocation #if NET6_0_OR_GREATER - : IUnknown.Interface +//: IUnknown.Interface #endif { /// @@ -45,8 +42,8 @@ internal unsafe struct ID3D12Allocation } #if NET6_0_OR_GREATER - /// - static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid)); + ///// + //static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid)); #endif /// diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs index 7557a5df..a4b22f26 100644 --- a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs +++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocator.cs @@ -1,9 +1,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -#if NET6_0_OR_GREATER -using TerraFX.Interop; -#endif using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -17,7 +14,7 @@ namespace ComputeSharp.Interop.Allocation; [Guid("2D5E55D2-9244-431F-868E-0D90AAB6E575")] internal unsafe struct ID3D12MemoryAllocator #if NET6_0_OR_GREATER - : IUnknown.Interface +//: IUnknown.Interface #endif { /// @@ -45,8 +42,8 @@ internal unsafe struct ID3D12MemoryAllocator } #if NET6_0_OR_GREATER - /// - static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid)); + ///// + //static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid)); #endif /// diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs index a5496fdb..94da20fa 100644 --- a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs +++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs @@ -1,9 +1,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -#if NET6_0_OR_GREATER -using TerraFX.Interop; -#endif using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -17,7 +14,7 @@ namespace ComputeSharp.Interop.Allocation; [Guid("CC1E74A7-786D-40F4-8AE2-F8B7A255587E")] internal unsafe struct ID3D12MemoryAllocatorFactory #if NET6_0_OR_GREATER - : IUnknown.Interface +//: IUnknown.Interface #endif { /// @@ -45,8 +42,8 @@ internal unsafe struct ID3D12MemoryAllocatorFactory } #if NET6_0_OR_GREATER - /// - static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid)); + ///// + //static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid)); #endif /// diff --git a/src/ComputeSharp/Shaders/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Shaders/Extensions/Interop/ID3D12DeviceExtensions.cs index 3cb499f6..d08b7c97 100644 --- a/src/ComputeSharp/Shaders/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -21,17 +21,17 @@ internal static unsafe class ID3D12DeviceExtensions /// The number of 32 bit root constants to load. /// The input descriptor ranges for the signature to create. /// Indicates whether or not a static sampler is used. - /// A pointer to the newly allocated instance. + /// The newly allocated instance. /// Thrown when the creation of the root signature fails. - public static ComPtr CreateRootSignature( + public static void CreateRootSignature( this ref ID3D12Device d3D12Device, int d3D12Root32BitConstantsCount, ReadOnlySpan d3D12DescriptorRanges1, - bool isStaticSamplerUsed) + bool isStaticSamplerUsed, + ID3D12RootSignature** d3D12RootSignature) { using ComPtr d3D3Blob = default; using ComPtr d3D3BlobError = default; - using ComPtr d3D12RootSignature = default; fixed (D3D12_DESCRIPTOR_RANGE1* d3D12DescriptorRange1 = d3D12DescriptorRanges1) { @@ -83,9 +83,7 @@ internal static unsafe class ID3D12DeviceExtensions d3D3Blob.Get()->GetBufferPointer(), d3D3Blob.Get()->GetBufferSize(), Windows.__uuidof(), - (void**)d3D12RootSignature.GetAddressOf()).Assert(); - - return d3D12RootSignature.Move(); + (void**)d3D12RootSignature).Assert(); } /// @@ -94,15 +92,14 @@ internal static unsafe class ID3D12DeviceExtensions /// The target to use to create the pipeline state. /// The input root signature to use to create the pipeline state. /// The shader bytecode to use for the pipeline state. - /// A pointer to the newly allocated instance. + /// The newly allocated instance. /// Thrown when the creation of the pipeline state fails. - public static ComPtr CreateComputePipelineState( + public static void CreateComputePipelineState( this ref ID3D12Device d3D12Device, ID3D12RootSignature* d3D12RootSignature, - D3D12_SHADER_BYTECODE d3D12ShaderBytecode) + D3D12_SHADER_BYTECODE d3D12ShaderBytecode, + ID3D12PipelineState** d3D12PipelineState) { - using ComPtr d3D12PipelineState = default; - D3D12_COMPUTE_PIPELINE_STATE_DESC d3D12ComputePipelineStateDescription; d3D12ComputePipelineStateDescription.pRootSignature = d3D12RootSignature; d3D12ComputePipelineStateDescription.CS = d3D12ShaderBytecode; @@ -113,8 +110,6 @@ internal static unsafe class ID3D12DeviceExtensions d3D12Device.CreateComputePipelineState( &d3D12ComputePipelineStateDescription, Windows.__uuidof(), - (void**)d3D12PipelineState.GetAddressOf()).Assert(); - - return d3D12PipelineState.Move(); + (void**)d3D12PipelineState).Assert(); } } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs index ee9f2acd..a133ae71 100644 --- a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs @@ -98,10 +98,11 @@ internal static unsafe class PipelineDataLoader int root32BitConstantCount = shader.ConstantBufferSize / sizeof(int); // Create the D3D12 root signature - *d3D12RootSignature = d3D12Device->CreateRootSignature( + d3D12Device->CreateRootSignature( root32BitConstantCount, d3D12DescriptorRanges, - shader.IsStaticSamplerRequired).Get(); + shader.IsStaticSamplerRequired, + d3D12RootSignature); // Return the array to the pool if no exceptions have been thrown if (d3D12DescriptorRangesArray is not null) @@ -124,7 +125,7 @@ internal static unsafe class PipelineDataLoader { D3D12_SHADER_BYTECODE d3D12ShaderBytecode = new(hlslBytecodePtr, (nuint)shader.HlslBytecode.Length); - *d3D12PipelineState = d3D12Device->CreateComputePipelineState(d3D12RootSignature, d3D12ShaderBytecode); + d3D12Device->CreateComputePipelineState(d3D12RootSignature, d3D12ShaderBytecode, d3D12PipelineState); } } } \ No newline at end of file