From 918840434512a99cb46c95147a3c3166b5395834 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jan 2021 13:37:02 +0100 Subject: [PATCH 01/14] Add Allocator initialization to GraphicsDevice --- ComputeSharp.sln | 10 ++++++++ src/ComputeSharp/Graphics/GraphicsDevice.cs | 23 ++++++++++++++++++- .../Helpers/DeviceHelper.Enumerator.cs | 4 ++-- .../Graphics/Helpers/DeviceHelper.Factory.cs | 5 ++-- .../Graphics/Helpers/DeviceHelper.cs | 21 +++++++++++------ .../ShaderLoader{T}.DispatchData.cs | 2 +- .../Shaders/Translation/ShaderLoader{T}.cs | 2 +- 7 files changed, 53 insertions(+), 14 deletions(-) diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 24463506..19c244ce 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.NativeLi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.SwapChain", "samples\ComputeSharp.SwapChain\ComputeSharp.SwapChain.csproj", "{E7C9247D-016B-4819-AF9F-0E9B3591E04B}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TerraFX.Interop.D3D12MemoryAllocator", "..\terrafx.interop.d3d12memoryallocator\sources\Interop\D3D12MemoryAllocator\TerraFX.Interop.D3D12MemoryAllocator.csproj", "{DB64E751-125D-44E2-8EA9-F5B085714007}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{7048deea-0a6f-46a0-b951-b682a8f1cb75}*SharedItemsImports = 5 @@ -152,6 +154,14 @@ Global {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|Any CPU.Build.0 = Release|Any CPU {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.ActiveCfg = Release|Any CPU {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.Build.0 = Release|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|x64.ActiveCfg = Debug|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|x64.Build.0 = Debug|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|Any CPU.Build.0 = Release|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|x64.ActiveCfg = Release|Any CPU + {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index b2c12d7e..a3f1e576 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -72,12 +72,18 @@ namespace ComputeSharp /// private ulong nextD3D12CopyFenceValue = 1; + /// + /// The in use associated to the current device. + /// + internal Allocator* allocator; + /// /// Creates a new instance for the input . /// /// The to use for the new instance. + /// The that was created from. /// The available info for the new instance. - internal GraphicsDevice(ComPtr d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1) + internal GraphicsDevice(ComPtr d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { this.d3D12Device = d3D12Device; this.d3D12ComputeCommandQueue = d3D12Device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE); @@ -103,6 +109,15 @@ namespace ComputeSharp var d3D12Architecture1Data = d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE1); IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; + + ALLOCATOR_DESC allocatorDesc = default; + allocatorDesc.pDevice = d3D12Device.Get(); + allocatorDesc.pAdapter = dxgiAdapter; + + fixed (Allocator** allocator = &this.allocator) + { + D3D12MemoryAllocator.CreateAllocator(&allocatorDesc, allocator).Assert(); + } } /// @@ -317,6 +332,12 @@ namespace ComputeSharp this.copyCommandAllocatorPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); + if (this.allocator != null) + { + this.allocator->Release(); + this.allocator = null; + } + return true; } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index be3c34f6..ffd72f65 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -141,7 +141,7 @@ namespace ComputeSharp.Graphics.Helpers FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); - this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1); + this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); this.isCompleted = true; return true; @@ -182,7 +182,7 @@ namespace ComputeSharp.Graphics.Helpers FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); - this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1); + this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); return true; } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index e2665b77..809ab3ba 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -25,9 +25,10 @@ namespace ComputeSharp.Graphics.Helpers /// Retrieves a instance for an object. /// /// The to use to get a instance. + /// The that was created from. /// The available info for the instance. /// A instance for the input device. - private static unsafe GraphicsDevice GetOrCreateDevice(ComPtr d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1) + private static unsafe GraphicsDevice GetOrCreateDevice(ComPtr d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { lock (DevicesCache) { @@ -35,7 +36,7 @@ namespace ComputeSharp.Graphics.Helpers if (!DevicesCache.TryGetValue(luid, out GraphicsDevice? device)) { - device = new GraphicsDevice(d3D12Device, dxgiDescription1); + device = new GraphicsDevice(d3D12Device, dxgiAdapter, dxgiDescription1); DevicesCache.Add(luid, device); diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index 8913be29..c6b5d716 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -33,7 +33,7 @@ namespace ComputeSharp.Graphics.Helpers { DXGI_ADAPTER_DESC1 dxgiDescription1; - return TryGetDefaultDevice(null, &dxgiDescription1) || TryGetWarpDevice(null, &dxgiDescription1); + return TryGetDefaultDevice(null, null, &dxgiDescription1) || TryGetWarpDevice(null, null, &dxgiDescription1); } /// @@ -46,7 +46,7 @@ namespace ComputeSharp.Graphics.Helpers { DXGI_ADAPTER_DESC1 dxgiDescription1; - _ = TryGetDefaultDevice(null, &dxgiDescription1) || TryGetWarpDevice(null, &dxgiDescription1); + _ = TryGetDefaultDevice(null, null, &dxgiDescription1) || TryGetWarpDevice(null, null, &dxgiDescription1); return Luid.FromLUID(dxgiDescription1.AdapterLuid); } @@ -59,13 +59,14 @@ namespace ComputeSharp.Graphics.Helpers public static unsafe GraphicsDevice GetDefaultDevice() { using ComPtr d3D12Device = default; + using ComPtr dxgiAdapter = default; DXGI_ADAPTER_DESC1 dxgiDescription1; - if (TryGetDefaultDevice(d3D12Device.GetAddressOf(), &dxgiDescription1) || - TryGetWarpDevice(d3D12Device.GetAddressOf(), &dxgiDescription1)) + if (TryGetDefaultDevice(d3D12Device.GetAddressOf(), dxgiAdapter.GetAddressOf(), &dxgiDescription1) || + TryGetWarpDevice(d3D12Device.GetAddressOf(), dxgiAdapter.GetAddressOf(), &dxgiDescription1)) { - return GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1); + return GetOrCreateDevice(d3D12Device.Move(), dxgiAdapter.Get(), &dxgiDescription1); } return ThrowHelper.ThrowNotSupportedException("There isn't a supported GPU device on the current machine"); @@ -75,9 +76,10 @@ namespace ComputeSharp.Graphics.Helpers /// Tries to check or create a default object. /// /// A pointer to the object to create, or . + /// A pointer to the object used to create , or . /// A pointer to the value for the device found. /// Whether a default device was found with the requested feature level. - private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1) + private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { using ComPtr dxgiFactory4 = default; @@ -116,6 +118,8 @@ namespace ComputeSharp.Graphics.Helpers if (FX.SUCCEEDED(createDeviceResult)) { + dxgiAdapter1.CopyTo(dxgiAdapter); + return true; } } @@ -125,9 +129,10 @@ namespace ComputeSharp.Graphics.Helpers /// Tries to check or create a warp object. /// /// A pointer to the object to create, or . + /// A pointer to the object used to create , or . /// A pointer to the value for the device found. /// Whether a warp device was created successfully. - private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1) + private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { using ComPtr dxgiFactory4 = default; @@ -147,6 +152,8 @@ namespace ComputeSharp.Graphics.Helpers FX.__uuidof(), (void**)d3D12Device); + dxgiAdapter1.CopyTo(dxgiAdapter); + return FX.SUCCEEDED(createDeviceResult); } diff --git a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs index 2922a85f..80b0d868 100644 --- a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs +++ b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.DispatchData.cs @@ -9,7 +9,7 @@ using ComputeSharp.__Internals; using ComputeSharp.Shaders.Translation.Models; using TerraFX.Interop; -#pragma warning disable CS0618 +#pragma warning disable CS0419, CS0618 namespace ComputeSharp.Shaders.Translation { diff --git a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs index de9cfcef..e0495df5 100644 --- a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs @@ -10,7 +10,7 @@ using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_TYPE; -#pragma warning disable CS0618, CS8618 +#pragma warning disable CS0419, CS0618, CS8618 namespace ComputeSharp.Shaders.Translation { From adad3cccd952bc37bf57d4816849ab8763097b1a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jan 2021 13:41:08 +0100 Subject: [PATCH 02/14] Minor tweaks to ComPtr ownership --- .../Graphics/Commands/PipelineData.cs | 6 +++--- src/ComputeSharp/Graphics/GraphicsDevice.cs | 18 +++++++++--------- .../Helpers/DeviceHelper.Enumerator.cs | 4 ++-- .../Graphics/Helpers/DeviceHelper.Factory.cs | 2 +- .../Graphics/Helpers/DeviceHelper.cs | 2 +- src/ComputeSharp/Shaders/ShaderRunner{T}.cs | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ComputeSharp/Graphics/Commands/PipelineData.cs b/src/ComputeSharp/Graphics/Commands/PipelineData.cs index be6ad0eb..7f0e16d1 100644 --- a/src/ComputeSharp/Graphics/Commands/PipelineData.cs +++ b/src/ComputeSharp/Graphics/Commands/PipelineData.cs @@ -23,10 +23,10 @@ namespace ComputeSharp.Graphics.Commands /// /// The value for the current shader. /// The compiled pipeline state to reuse for the current shader. - public PipelineData(ComPtr d3D12RootSignature, ComPtr d3D12PipelineState) + public PipelineData(ID3D12RootSignature* d3D12RootSignature, ID3D12PipelineState* d3D12PipelineState) { - this.d3D12RootSignature = d3D12RootSignature; - this.d3D12PipelineState = d3D12PipelineState; + this.d3D12RootSignature = new ComPtr(d3D12RootSignature); + this.d3D12PipelineState = new ComPtr(d3D12PipelineState); } /// diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index a3f1e576..1611fd49 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -83,13 +83,13 @@ namespace ComputeSharp /// The to use for the new instance. /// The that was created from. /// The available info for the new instance. - internal GraphicsDevice(ComPtr d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) + internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { - this.d3D12Device = d3D12Device; - this.d3D12ComputeCommandQueue = d3D12Device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE); - this.d3D12CopyCommandQueue = d3D12Device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY); - this.d3D12ComputeFence = d3D12Device.Get()->CreateFence(); - this.d3D12CopyFence = d3D12Device.Get()->CreateFence(); + this.d3D12Device = new ComPtr(d3D12Device); + this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE); + this.d3D12CopyCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY); + this.d3D12ComputeFence = d3D12Device->CreateFence(); + this.d3D12CopyFence = d3D12Device->CreateFence(); this.computeCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COMPUTE); this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY); @@ -101,17 +101,17 @@ namespace ComputeSharp SharedMemorySize = dxgiDescription1->SharedSystemMemory; IsHardwareAccelerated = (dxgiDescription1->Flags & (uint)DXGI_ADAPTER_FLAG_SOFTWARE) == 0; - var d3D12Options1Data = d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1); + var d3D12Options1Data = d3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1); ComputeUnits = d3D12Options1Data.TotalLaneCount; WavefrontSize = d3D12Options1Data.WaveLaneCountMin; - var d3D12Architecture1Data = d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE1); + var d3D12Architecture1Data = d3D12Device->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE1); IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; ALLOCATOR_DESC allocatorDesc = default; - allocatorDesc.pDevice = d3D12Device.Get(); + allocatorDesc.pDevice = d3D12Device; allocatorDesc.pAdapter = dxgiAdapter; fixed (Allocator** allocator = &this.allocator) diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index ffd72f65..72ab52b1 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -141,7 +141,7 @@ namespace ComputeSharp.Graphics.Helpers FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); - this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); + this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); this.isCompleted = true; return true; @@ -182,7 +182,7 @@ namespace ComputeSharp.Graphics.Helpers FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); - this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); + this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); return true; } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index 809ab3ba..aad7db4f 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -28,7 +28,7 @@ namespace ComputeSharp.Graphics.Helpers /// The that was created from. /// The available info for the instance. /// A instance for the input device. - private static unsafe GraphicsDevice GetOrCreateDevice(ComPtr d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) + private static unsafe GraphicsDevice GetOrCreateDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { lock (DevicesCache) { diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index c6b5d716..def2b0c0 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -66,7 +66,7 @@ namespace ComputeSharp.Graphics.Helpers if (TryGetDefaultDevice(d3D12Device.GetAddressOf(), dxgiAdapter.GetAddressOf(), &dxgiDescription1) || TryGetWarpDevice(d3D12Device.GetAddressOf(), dxgiAdapter.GetAddressOf(), &dxgiDescription1)) { - return GetOrCreateDevice(d3D12Device.Move(), dxgiAdapter.Get(), &dxgiDescription1); + return GetOrCreateDevice(d3D12Device.Get(), dxgiAdapter.Get(), &dxgiDescription1); } return ThrowHelper.ThrowNotSupportedException("There isn't a supported GPU device on the current machine"); diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index ad13f33f..ba045dce 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -212,7 +212,7 @@ namespace ComputeSharp.Shaders using ComPtr d3D12RootSignature = device.D3D12Device->CreateRootSignature(shaderData.Loader.D3D12Root32BitConstantsCount, shaderData.Loader.D3D12DescriptorRanges1); using ComPtr d3D12PipelineState = device.D3D12Device->CreateComputePipelineState(d3D12RootSignature.Get(), shaderData.Bytecode.D3D12ShaderBytecode); - pipelineData = new PipelineData(d3D12RootSignature.Move(), d3D12PipelineState.Move()); + pipelineData = new PipelineData(d3D12RootSignature.Get(), d3D12PipelineState.Get()); shaderData.CachedPipelines.Add(device, pipelineData); } From f14772b5d04023675dad5269f117be65d590d3f1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jan 2021 15:30:05 +0100 Subject: [PATCH 03/14] Added safe Ptr types for Allocation/Allocator --- .../Graphics/Interop/AllocationPtr.cs | 96 +++++++++++++++++++ .../Graphics/Interop/AllocatorPtr.cs | 96 +++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 src/ComputeSharp/Graphics/Interop/AllocationPtr.cs create mode 100644 src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs diff --git a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs new file mode 100644 index 00000000..48f31b83 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs @@ -0,0 +1,96 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Interop +{ + /// + /// A -equivalent type to safely work with pointers. + /// + /// + /// While this type is not marked as so that it can also be used in fields, make sure to + /// keep the reference counts properly tracked if you do store instances on the heap. + /// + internal unsafe struct AllocationPtr : IDisposable + { + /// + /// The raw pointer to an object, if existing. + /// + private Allocation* pointer; + + /// + /// Creates a new instance from a raw pointer. + /// + /// The raw pointer to wrap. + public AllocationPtr(Allocation* other) + { + pointer = other; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + Allocation* pointer = this.pointer; + + if (pointer != null) + { + this.pointer = null; + + pointer->Release(); + } + } + + /// + /// Gets the currently wrapped raw pointer to an object. + /// + /// The raw pointer wrapped by the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocation* Get() + { + return this.pointer; + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// This method is only valid when the current instance is on the stack or pinned. + /// + /// The raw pointer to the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocation** GetAddressOf() + { + return (Allocation**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void** GetVoidAddressOf() + where T : unmanaged + { + return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public AllocationPtr Move() + { + AllocationPtr copy = this; + + this = default; + + return copy; + } + } +} diff --git a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs new file mode 100644 index 00000000..97b75867 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs @@ -0,0 +1,96 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Interop +{ + /// + /// A -equivalent type to safely work with pointers. + /// + /// + /// While this type is not marked as so that it can also be used in fields, make sure to + /// keep the reference counts properly tracked if you do store instances on the heap. + /// + internal unsafe struct AllocatorPtr : IDisposable + { + /// + /// The raw pointer to an object, if existing. + /// + private Allocator* pointer; + + /// + /// Creates a new instance from a raw pointer. + /// + /// The raw pointer to wrap. + public AllocatorPtr(Allocator* other) + { + pointer = other; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + Allocator* pointer = this.pointer; + + if (pointer != null) + { + this.pointer = null; + + pointer->Release(); + } + } + + /// + /// Gets the currently wrapped raw pointer to an object. + /// + /// The raw pointer wrapped by the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocator* Get() + { + return this.pointer; + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// This method is only valid when the current instance is on the stack or pinned. + /// + /// The raw pointer to the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocator** GetAddressOf() + { + return (Allocator**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void** GetVoidAddressOf() + where T : unmanaged + { + return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public AllocatorPtr Move() + { + AllocatorPtr copy = this; + + this = default; + + return copy; + } + } +} From c263489b240d42ae9f0a1998e7072f3bf1f206df Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jan 2021 16:04:07 +0100 Subject: [PATCH 04/14] Minor code refactoring --- src/ComputeSharp/Graphics/GraphicsDevice.cs | 17 +++++------ .../Graphics/Interop/AllocationPtr.cs | 28 +++++++++++++------ .../Graphics/Interop/AllocatorPtr.cs | 28 +++++++++++++------ 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1611fd49..8462f7ce 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -2,6 +2,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; @@ -75,7 +76,7 @@ namespace ComputeSharp /// /// The in use associated to the current device. /// - internal Allocator* allocator; + internal AllocatorPtr allocator; /// /// Creates a new instance for the input . @@ -114,7 +115,7 @@ namespace ComputeSharp allocatorDesc.pDevice = d3D12Device; allocatorDesc.pAdapter = dxgiAdapter; - fixed (Allocator** allocator = &this.allocator) + fixed (Allocator** allocator = this.allocator) { D3D12MemoryAllocator.CreateAllocator(&allocatorDesc, allocator).Assert(); } @@ -161,6 +162,11 @@ namespace ComputeSharp /// internal ID3D12Device* D3D12Device => this.d3D12Device; + /// + /// Gets the underlying wrapped by the current instance. + /// + internal Allocator* Allocator => this.allocator; + /// /// Gets whether or not the current device has a cache coherent UMA architecture. /// @@ -331,12 +337,7 @@ namespace ComputeSharp this.computeCommandAllocatorPool.Dispose(); this.copyCommandAllocatorPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); - - if (this.allocator != null) - { - this.allocator->Release(); - this.allocator = null; - } + this.allocator.Dispose(); return true; } diff --git a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs index 48f31b83..e9a04e54 100644 --- a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs +++ b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs @@ -1,9 +1,10 @@ using System; +using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; -namespace ComputeSharp.Graphics.Interop +namespace ComputeSharp.Graphics { /// /// A -equivalent type to safely work with pointers. @@ -28,6 +29,16 @@ namespace ComputeSharp.Graphics.Interop pointer = other; } + /// + /// Unwraps a instance and returns the internal raw pointer. + /// + /// The instance to unwrap. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Allocation*(AllocationPtr other) + { + return other.Get(); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() @@ -66,16 +77,17 @@ namespace ComputeSharp.Graphics.Interop } /// - /// Gets the address of the current instance as a raw double pointer. + /// Gets the address of the current instance as a managed reference to pointer. /// - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. - [Pure] + /// The reference to the current instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly void** GetVoidAddressOf() - where T : unmanaged + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly ref Allocation* GetPinnableReference() { - return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + fixed (Allocation** ptr = &this.pointer) + { + return ref *ptr; + } } /// diff --git a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs index 97b75867..1d7033a4 100644 --- a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs +++ b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs @@ -1,9 +1,10 @@ using System; +using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; -namespace ComputeSharp.Graphics.Interop +namespace ComputeSharp.Graphics { /// /// A -equivalent type to safely work with pointers. @@ -28,6 +29,16 @@ namespace ComputeSharp.Graphics.Interop pointer = other; } + /// + /// Unwraps a instance and returns the internal raw pointer. + /// + /// The instance to unwrap. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Allocator*(AllocatorPtr other) + { + return other.Get(); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() @@ -66,16 +77,17 @@ namespace ComputeSharp.Graphics.Interop } /// - /// Gets the address of the current instance as a raw double pointer. + /// Gets the address of the current instance as a managed reference to pointer. /// - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. - [Pure] + /// The reference to the current instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly void** GetVoidAddressOf() - where T : unmanaged + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly ref Allocator* GetPinnableReference() { - return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + fixed (Allocator** ptr = &this.pointer) + { + return ref *ptr; + } } /// From 57c4d816c1ca33be28956ddcbd3c3880d8a3d272 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jan 2021 16:51:51 +0100 Subject: [PATCH 05/14] Enabled Allocator utilization in resource types --- .../Extensions/Interop/AllocatorExtensions.cs | 166 ++++++++++++++++++ src/ComputeSharp/Graphics/GraphicsDevice.cs | 2 +- .../Graphics/Interop/AllocationPtr.cs | 15 -- .../Graphics/Interop/AllocatorPtr.cs | 27 --- .../Graphics/Resources/Abstract/Buffer{T}.cs | 18 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 15 +- .../Resources/Abstract/Texture2D{T}.cs | 63 +++++-- .../Resources/Abstract/Texture3D{T}.cs | 65 +++++-- 8 files changed, 299 insertions(+), 72 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs new file mode 100644 index 00000000..d3ee4e58 --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -0,0 +1,166 @@ +using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics.Resources.Enums; +using Microsoft.Toolkit.Diagnostics; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_HEAP_TYPE; +using static TerraFX.Interop.D3D12_RESOURCE_FLAGS; +using static TerraFX.Interop.D3D12_RESOURCE_STATES; +using static TerraFX.Interop.ALLOCATION_FLAGS; + +namespace ComputeSharp.Graphics.Extensions +{ + /// + /// A with extensions for the type. + /// + internal static unsafe class AllocatorExtensions + { + /// + /// Creates a resource for a given buffer type. + /// + /// The instance in use. + /// The resource type currently in use. + /// The allocation mode to use for the new resource. + /// The size in bytes of the current buffer. + /// An reference for the current buffer. + public static AllocationPtr CreateResource( + this ref Allocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + ulong sizeInBytes) + { + ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? ALLOCATION_FLAG_NONE : ALLOCATION_FLAG_COMMITTED; + (D3D12_HEAP_TYPE d3D12HeapType, + D3D12_RESOURCE_FLAGS d3D12ResourceFlags, + D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch + { + ResourceType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), + ResourceType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), + ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON), + ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST), + ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), + _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + }; + + using AllocationPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); + + ALLOCATION_DESC allocationDesc = default; + allocationDesc.HeapType = d3D12HeapType; + allocationDesc.Flags = allocationFlags; + + allocator.CreateResource( + &allocationDesc, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + allocation.GetAddressOf(), + null, + null).Assert(); + + return allocation.Move(); + } + + /// + /// Creates a resource for a given 2D texture type. + /// + /// The instance in use. + /// The resource type currently in use. + /// The allocation mode to use for the new resource. + /// The value to use. + /// The width of the texture resource. + /// The height of the texture resource. + /// The default value for the resource. + /// An reference for the current texture. + public static AllocationPtr CreateResource( + this ref Allocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? ALLOCATION_FLAG_NONE : ALLOCATION_FLAG_COMMITTED; + D3D12_RESOURCE_FLAGS d3D12ResourceFlags; + + (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch + { + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), + ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), + _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + }; + + using AllocationPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags); + + ALLOCATION_DESC allocationDesc = default; + allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; + allocationDesc.Flags = allocationFlags; + + allocator.CreateResource( + &allocationDesc, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + allocation.GetAddressOf(), + null, + null); + + return allocation.Move(); + } + + /// + /// Creates a resource for a given 3D texture type. + /// + /// The instance in use. + /// The resource type currently in use. + /// The allocation mode to use for the new resource. + /// The value to use. + /// The width of the texture resource. + /// The height of the texture resource. + /// The depth of the texture resource. + /// The default value for the resource. + /// An reference for the current texture. + public static AllocationPtr CreateResource( + this ref Allocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + ushort depth, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? ALLOCATION_FLAG_NONE : ALLOCATION_FLAG_COMMITTED; + D3D12_RESOURCE_FLAGS d3D12ResourceFlags; + + (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch + { + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), + ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), + _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + }; + + using AllocationPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags); + + ALLOCATION_DESC allocationDesc = default; + allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; + allocationDesc.Flags = allocationFlags; + + allocator.CreateResource( + &allocationDesc, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + allocation.GetAddressOf(), + null, + null); + + return allocation.Move(); + } + } +} diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 8462f7ce..b4879f2b 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -76,7 +76,7 @@ namespace ComputeSharp /// /// The in use associated to the current device. /// - internal AllocatorPtr allocator; + private AllocatorPtr allocator; /// /// Creates a new instance for the input . diff --git a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs index e9a04e54..9d49141d 100644 --- a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs +++ b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; @@ -76,20 +75,6 @@ namespace ComputeSharp.Graphics return (Allocation**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); } - /// - /// Gets the address of the current instance as a managed reference to pointer. - /// - /// The reference to the current instance. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - [EditorBrowsable(EditorBrowsableState.Never)] - public readonly ref Allocation* GetPinnableReference() - { - fixed (Allocation** ptr = &this.pointer) - { - return ref *ptr; - } - } - /// /// Gets the address of the current instance as a raw double pointer. /// diff --git a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs index 1d7033a4..bee25d48 100644 --- a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs +++ b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs @@ -64,18 +64,6 @@ namespace ComputeSharp.Graphics return this.pointer; } - /// - /// Gets the address of the current instance as a raw double pointer. - /// This method is only valid when the current instance is on the stack or pinned. - /// - /// The raw pointer to the current instance. - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly Allocator** GetAddressOf() - { - return (Allocator**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); - } - /// /// Gets the address of the current instance as a managed reference to pointer. /// @@ -89,20 +77,5 @@ namespace ComputeSharp.Graphics return ref *ptr; } } - - /// - /// Gets the address of the current instance as a raw double pointer. - /// - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public AllocatorPtr Move() - { - AllocatorPtr copy = this; - - this = default; - - return copy; - } } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index fbb8ba9b..37164e8f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; @@ -38,6 +39,12 @@ namespace ComputeSharp.Resources /// protected readonly nint SizeInBytes; + /// + /// The instance used to retrieve , if any. + /// + /// This will be if the owning device has set. + private AllocationPtr allocation; + /// /// Creates a new instance with the specified parameters. /// @@ -64,7 +71,15 @@ namespace ComputeSharp.Resources GraphicsDevice = device; Length = length; - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, (ulong)SizeInBytes, device.IsCacheCoherentUMA); + if (device.IsCacheCoherentUMA) + { + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, (ulong)SizeInBytes, true); + } + else + { + this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, (ulong)SizeInBytes); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + } device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); @@ -156,6 +171,7 @@ namespace ComputeSharp.Resources protected override bool OnDispose() { this.d3D12Resource.Dispose(); + this.allocation.Dispose(); if (GraphicsDevice?.IsDisposed == false) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 5c25645e..b37a9ad4 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; @@ -58,15 +59,15 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength, false); + using AllocationPtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength); + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } - using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map(); fixed (void* destinationPointer = &destination) { @@ -156,9 +157,9 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength, false); + using AllocationPtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize); - using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) fixed (void* sourcePointer = &source) { MemoryHelper.Copy( @@ -171,7 +172,7 @@ namespace ComputeSharp.Resources using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength); + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 9510905c..ca07242b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -1,6 +1,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -56,6 +57,11 @@ namespace ComputeSharp.Resources /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// The instance used to retrieve , if any. + /// + /// This will be if the owning device has set. + private AllocationPtr allocation; + /// /// Creates a new instance with the specified parameters. /// @@ -79,14 +85,28 @@ namespace ComputeSharp.Resources GraphicsDevice = device; - this.d3D12Resource = device.D3D12Device->CreateCommittedResource( - resourceType, - allocationMode, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - device.IsCacheCoherentUMA, - out this.d3D12ResourceState); + if (device.IsCacheCoherentUMA) + { + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + allocationMode, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + true, + out this.d3D12ResourceState); + } + else + { + this.allocation = device.Allocator->CreateResource( + resourceType, + allocationMode, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + out this.d3D12ResourceState); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + } this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON ? D3D12_COMMAND_LIST_TYPE_COPY @@ -166,7 +186,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using AllocationPtr allocation = default; + using ComPtr d3D12Resource = default; + + if (GraphicsDevice.IsCacheCoherentUMA) + { + *&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, true); + } + else + { + *&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes); + *&d3D12Resource = new ComPtr(allocation.Get()->GetResource()); + } using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -305,7 +336,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using AllocationPtr allocation = default; + using ComPtr d3D12Resource = default; + + if (GraphicsDevice.IsCacheCoherentUMA) + { + *&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, true); + } + else + { + *&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes); + *&d3D12Resource = new ComPtr(allocation.Get()->GetResource()); + } using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) @@ -415,6 +457,7 @@ namespace ComputeSharp.Resources protected override bool OnDispose() { this.d3D12Resource.Dispose(); + this.allocation.Dispose(); if (GraphicsDevice?.IsDisposed == false) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 3b1c9253..8de2ee64 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -1,6 +1,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -56,6 +57,11 @@ namespace ComputeSharp.Resources /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// The instance used to retrieve , if any. + /// + /// This will be if the owning device has set. + private AllocationPtr allocation; + /// /// Creates a new instance with the specified parameters. /// @@ -81,15 +87,30 @@ namespace ComputeSharp.Resources GraphicsDevice = device; - this.d3D12Resource = device.D3D12Device->CreateCommittedResource( - resourceType, - allocationMode, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - device.IsCacheCoherentUMA, - out this.d3D12ResourceState); + if (device.IsCacheCoherentUMA) + { + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + allocationMode, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + (ushort)depth, + true, + out this.d3D12ResourceState); + } + else + { + this.allocation = device.Allocator->CreateResource( + resourceType, + allocationMode, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + (ushort)depth, + out this.d3D12ResourceState); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + } this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON ? D3D12_COMMAND_LIST_TYPE_COPY @@ -181,7 +202,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using AllocationPtr allocation = default; + using ComPtr d3D12Resource = default; + + if (GraphicsDevice.IsCacheCoherentUMA) + { + *&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, true); + } + else + { + *&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes); + *&d3D12Resource = new ComPtr(allocation.Get()->GetResource()); + } using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -337,7 +369,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using AllocationPtr allocation = default; + using ComPtr d3D12Resource = default; + + if (GraphicsDevice.IsCacheCoherentUMA) + { + *&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, true); + } + else + { + *&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes); + *&d3D12Resource = new ComPtr(allocation.Get()->GetResource()); + } using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) From c6991cdf0eb8c1d03d404c5b1dc41d5649590cd8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jan 2021 21:00:24 +0100 Subject: [PATCH 06/14] Skipped Allocator creation for UMA devices --- src/ComputeSharp/Graphics/GraphicsDevice.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index b4879f2b..afdf3cf6 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -115,9 +115,12 @@ namespace ComputeSharp allocatorDesc.pDevice = d3D12Device; allocatorDesc.pAdapter = dxgiAdapter; - fixed (Allocator** allocator = this.allocator) + if (!IsCacheCoherentUMA) { - D3D12MemoryAllocator.CreateAllocator(&allocatorDesc, allocator).Assert(); + fixed (Allocator** allocator = this.allocator) + { + D3D12MemoryAllocator.CreateAllocator(&allocatorDesc, allocator).Assert(); + } } } From e08eb53cd5ffbb26a7af34f6127313bbb8f81038 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jan 2021 14:50:20 +0100 Subject: [PATCH 07/14] Added UniquePtr type for D3D12MA types --- .../Extensions/Interop/AllocatorExtensions.cs | 18 +-- .../Extensions/Interop/ComPtrExtensions.cs | 7 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 2 +- .../Graphics/Interop/UniquePtr{T}.cs | 113 ++++++++++++++++++ .../Graphics/Resources/Abstract/Buffer{T}.cs | 2 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 4 +- .../Resources/Abstract/Texture2D{T}.cs | 6 +- .../Resources/Abstract/Texture3D{T}.cs | 6 +- 8 files changed, 135 insertions(+), 23 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index d3ee4e58..12a4d875 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -21,8 +21,8 @@ namespace ComputeSharp.Graphics.Extensions /// The resource type currently in use. /// The allocation mode to use for the new resource. /// The size in bytes of the current buffer. - /// An reference for the current buffer. - public static AllocationPtr CreateResource( + /// An reference for the current object. + public static UniquePtr CreateResource( this ref Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, @@ -41,7 +41,7 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using AllocationPtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); @@ -71,8 +71,8 @@ namespace ComputeSharp.Graphics.Extensions /// The width of the texture resource. /// The height of the texture resource. /// The default value for the resource. - /// An reference for the current texture. - public static AllocationPtr CreateResource( + /// An reference for the current object. + public static UniquePtr CreateResource( this ref Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, @@ -91,7 +91,7 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using AllocationPtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags); @@ -122,8 +122,8 @@ namespace ComputeSharp.Graphics.Extensions /// The height of the texture resource. /// The depth of the texture resource. /// The default value for the resource. - /// An reference for the current texture. - public static AllocationPtr CreateResource( + /// An reference for the current object. + public static UniquePtr CreateResource( this ref Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, @@ -143,7 +143,7 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using AllocationPtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags); diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs index 7388fdb8..7458b7d4 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs @@ -43,12 +43,11 @@ namespace ComputeSharp.Core.Extensions } /// - /// Gets the address of the current instance as a raw double pointer. + /// Moves the current instance and resets it without releasing the reference. /// /// The type to wrap in the current instance. - /// The input instance to get the address for. - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. + /// The input instance to move. + /// The moved instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ComPtr Move(this in ComPtr ptr) where T : unmanaged diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index afdf3cf6..8acf2ef6 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -76,7 +76,7 @@ namespace ComputeSharp /// /// The in use associated to the current device. /// - private AllocatorPtr allocator; + private UniquePtr allocator; /// /// Creates a new instance for the input . diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs new file mode 100644 index 00000000..69e0d3b4 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs @@ -0,0 +1,113 @@ +using System; +using System.ComponentModel; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics +{ + /// + /// A -equivalent type to safely work with pointers to public APIs. + /// + /// Must be either , , or . + /// + /// While this type is not marked as so that it can also be used in fields, make sure to + /// keep the reference counts properly tracked if you do store instances on the heap. + /// + internal unsafe struct UniquePtr : IDisposable + where T : unmanaged + { + /// + /// The raw pointer to an object, if existing. + /// + private T* pointer; + + /// + /// Creates a new instance from a raw pointer. + /// + /// The raw pointer to wrap. + public UniquePtr(T* other) + { + pointer = other; + } + + /// + /// Unwraps a instance and returns the internal raw pointer. + /// + /// The instance to unwrap. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator T*(UniquePtr other) + { + return other.Get(); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + T* pointer = this.pointer; + + if (pointer != null) + { + this.pointer = null; + + if (typeof(T) == typeof(Allocator)) ((Allocator*)pointer)->Release(); + else if (typeof(T) == typeof(Allocation)) ((Allocation*)pointer)->Release(); + else if (typeof(T) == typeof(Pool)) ((Pool*)pointer)->Release(); + else if (typeof(T) == typeof(VirtualBlock)) ((VirtualBlock*)pointer)->Release(); + else throw new ArgumentException("Invalid pointer type"); + } + } + + /// + /// Gets the currently wrapped raw pointer to an object. + /// + /// The raw pointer wrapped by the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T* Get() + { + return this.pointer; + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the current instance. + /// This method is only valid when the current instance is on the stack or pinned. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T** GetAddressOf() + { + return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a managed reference to pointer. + /// + /// The reference to the current instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly ref T* GetPinnableReference() + { + fixed (T** ptr = &this.pointer) + { + return ref *ptr; + } + } + + /// + /// Moves the current instance and resets it without releasing the reference. + /// + /// The moved instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public UniquePtr Move() + { + UniquePtr copy = this; + + this = default; + + return copy; + } + } +} diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 37164e8f..5e4a5b42 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -43,7 +43,7 @@ namespace ComputeSharp.Resources /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private AllocationPtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index b37a9ad4..81018c39 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -59,7 +59,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using AllocationPtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { @@ -157,7 +157,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using AllocationPtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize); using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index ca07242b..817fc478 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -60,7 +60,7 @@ namespace ComputeSharp.Resources /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private AllocationPtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. @@ -186,7 +186,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) @@ -336,7 +336,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 8de2ee64..1380156c 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -60,7 +60,7 @@ namespace ComputeSharp.Resources /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private AllocationPtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. @@ -202,7 +202,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) @@ -369,7 +369,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) From 9048571616121d1234cc21eb5c21eb427042540d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jan 2021 16:38:47 +0100 Subject: [PATCH 08/14] Minor code tweaks --- src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs | 4 ++-- src/ComputeSharp/Shaders/ShaderRunner{T}.cs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs index 69e0d3b4..eb4b81e8 100644 --- a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs +++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs @@ -11,8 +11,8 @@ namespace ComputeSharp.Graphics /// /// Must be either , , or . /// - /// While this type is not marked as so that it can also be used in fields, make sure to - /// keep the reference counts properly tracked if you do store instances on the heap. + /// This type is not marked as so that it can also be used in fields, so make sure to + /// avoid copying it and disposing it multiple times, as that would lead to a access violation exceptions. /// internal unsafe struct UniquePtr : IDisposable where T : unmanaged diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index ba045dce..eb03e3ff 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Shaders.Extensions; From ed952e0fdb7022c7d7553940d1fb6031d5f1bb39 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Feb 2021 17:53:49 +0100 Subject: [PATCH 09/14] Fixed some rebase errors --- .../Graphics/Resources/Abstract/StructuredBuffer{T}.cs | 8 ++++---- .../Graphics/Resources/Abstract/Texture2D{T}.cs | 1 + .../Graphics/Resources/Abstract/Texture3D{T}.cs | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 81018c39..6ed24acc 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -59,11 +59,11 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize); + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength); copyCommandList.ExecuteAndWaitForCompletion(); } @@ -157,7 +157,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) fixed (void* sourcePointer = &source) @@ -172,7 +172,7 @@ namespace ComputeSharp.Resources using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteSize); + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteLength); copyCommandList.ExecuteAndWaitForCompletion(); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 817fc478..f80e792e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -57,6 +57,7 @@ namespace ComputeSharp.Resources /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 1380156c..6b5871f8 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -57,6 +57,7 @@ namespace ComputeSharp.Resources /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. From e0350ccb0ceadcbfa2157959e73d14bcbc7c0cce Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Feb 2021 18:12:58 +0100 Subject: [PATCH 10/14] Added D3D12MA support to transfer resource types --- .../Resources/Abstract/Texture2D{T}.cs | 2 +- .../Resources/Abstract/Texture3D{T}.cs | 3 ++- .../Resources/Abstract/TransferBuffer{T}.cs | 19 ++++++++++++++++- .../Abstract/TransferTexture2D{T}.cs | 21 +++++++++++++++++-- .../Abstract/TransferTexture3D{T}.cs | 20 ++++++++++++++++-- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index f80e792e..398a93a9 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -113,7 +113,7 @@ namespace ComputeSharp.Resources ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; - GraphicsDevice.D3D12Device->GetCopyableFootprint( + device.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 6b5871f8..ba1d3035 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -117,7 +117,7 @@ namespace ComputeSharp.Resources ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; - GraphicsDevice.D3D12Device->GetCopyableFootprint( + device.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -502,6 +502,7 @@ namespace ComputeSharp.Resources protected override bool OnDispose() { this.d3D12Resource.Dispose(); + this.allocation.Dispose(); if (GraphicsDevice?.IsDisposed == false) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 9c52d2a3..922b6e79 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -2,6 +2,7 @@ using System.Buffers; using System.Runtime.CompilerServices; using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; @@ -27,6 +28,12 @@ namespace ComputeSharp.Resources /// private readonly T* mappedData; + /// + /// The instance used to retrieve , if any. + /// + /// This will be if the owning device has set. + private UniquePtr allocation; + /// /// Creates a new instance with the specified parameters. /// @@ -46,7 +53,16 @@ namespace ComputeSharp.Resources ulong sizeInBytes = (uint)length * (uint)sizeof(T); - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, sizeInBytes, device.IsCacheCoherentUMA); + if (device.IsCacheCoherentUMA) + { + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, sizeInBytes, true); + } + else + { + this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, sizeInBytes); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + } + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; this.d3D12Resource.Get()->SetName(this); @@ -100,6 +116,7 @@ namespace ComputeSharp.Resources protected override bool OnDispose() { this.d3D12Resource.Dispose(); + this.allocation.Dispose(); return true; } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 003a04c2..bd2f4318 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -1,5 +1,6 @@ using System.Runtime.CompilerServices; using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; @@ -33,6 +34,12 @@ namespace ComputeSharp.Resources /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// + /// The instance used to retrieve , if any. + /// + /// This will be if the owning device has set. + private UniquePtr allocation; + /// /// Creates a new instance with the specified parameters. /// @@ -55,7 +62,7 @@ namespace ComputeSharp.Resources GraphicsDevice = device; - GraphicsDevice.D3D12Device->GetCopyableFootprint( + device.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -63,7 +70,16 @@ namespace ComputeSharp.Resources out _, out ulong totalSizeInBytes); - this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + if (device.IsCacheCoherentUMA) + { + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, true); + } + else + { + this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, totalSizeInBytes); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + } + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; this.d3D12Resource.Get()->SetName(this); @@ -110,6 +126,7 @@ namespace ComputeSharp.Resources protected override bool OnDispose() { this.d3D12Resource.Dispose(); + this.allocation.Dispose(); return true; } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 3e92d0e5..ce51f3e8 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -33,6 +33,12 @@ namespace ComputeSharp.Resources /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// + /// The instance used to retrieve , if any. + /// + /// This will be if the owning device has set. + private UniquePtr allocation; + /// /// Creates a new instance with the specified parameters. /// @@ -57,7 +63,7 @@ namespace ComputeSharp.Resources GraphicsDevice = device; - GraphicsDevice.D3D12Device->GetCopyableFootprint( + device.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -66,7 +72,16 @@ namespace ComputeSharp.Resources out _, out ulong totalSizeInBytes); - this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + if (device.IsCacheCoherentUMA) + { + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, true); + } + else + { + this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, totalSizeInBytes); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + } + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; this.d3D12Resource.Get()->SetName(this); @@ -117,6 +132,7 @@ namespace ComputeSharp.Resources protected override bool OnDispose() { this.d3D12Resource.Dispose(); + this.allocation.Dispose(); return true; } From 349a54e43d028af7aa6f867f334bdf0b9418dbaf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 10 Feb 2021 13:58:41 +0100 Subject: [PATCH 11/14] Removed unnecessary texture mipmap allocations --- .../Graphics/Extensions/Interop/AllocatorExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index 12a4d875..d44772da 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -93,7 +93,7 @@ namespace ComputeSharp.Graphics.Extensions using UniquePtr allocation = default; - D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags); + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags); ALLOCATION_DESC allocationDesc = default; allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; @@ -145,7 +145,7 @@ namespace ComputeSharp.Graphics.Extensions using UniquePtr allocation = default; - D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags); + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags); ALLOCATION_DESC allocationDesc = default; allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; From 194dae1f50171ff7f7cff6f6e7e8fe24a6294fed Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 16 Feb 2021 13:50:52 +0100 Subject: [PATCH 12/14] Fixed some more rebase errors --- .../Graphics/Extensions/Interop/AllocatorExtensions.cs | 1 + src/ComputeSharp/Graphics/GraphicsDevice.cs | 1 - src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs | 2 +- src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs | 2 +- src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs | 1 - .../Graphics/Resources/Abstract/StructuredBuffer{T}.cs | 4 ++-- src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs | 1 - src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs | 1 - .../Graphics/Resources/Abstract/TransferBuffer{T}.cs | 1 - .../Graphics/Resources/Abstract/TransferTexture2D{T}.cs | 1 - 10 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index d44772da..b9d7c8c4 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -1,5 +1,6 @@ using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Resources.Enums; +using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_HEAP_TYPE; diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 8acf2ef6..d4f2a75e 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -2,7 +2,6 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index aad7db4f..cf5d55f9 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -41,7 +41,7 @@ namespace ComputeSharp.Graphics.Helpers DevicesCache.Add(luid, device); #if DEBUG - D3D12InfoQueueMap.Add(luid, d3D12Device.Get()->CreateInfoQueue()); + D3D12InfoQueueMap.Add(luid, d3D12Device->CreateInfoQueue()); #endif } diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs index eb4b81e8..071d6f3b 100644 --- a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs +++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs @@ -4,7 +4,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; -namespace ComputeSharp.Graphics +namespace ComputeSharp.Interop { /// /// A -equivalent type to safely work with pointers to public APIs. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 5e4a5b42..e459ccc4 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -2,7 +2,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Exceptions; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 6ed24acc..32f0630e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -1,8 +1,8 @@ -using ComputeSharp.Graphics; -using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; +using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 398a93a9..ead1d3e9 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -1,7 +1,6 @@ using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index ba1d3035..7fb6b115 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -1,7 +1,6 @@ using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 922b6e79..f4489052 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -2,7 +2,6 @@ using System.Buffers; using System.Runtime.CompilerServices; using ComputeSharp.Exceptions; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index bd2f4318..8a2a8292 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -1,6 +1,5 @@ using System.Runtime.CompilerServices; using ComputeSharp.Exceptions; -using ComputeSharp.Graphics; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; From cb0268286ebad757b2b4e6c60c6f0c97d2bdc394 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 25 Feb 2021 19:42:52 +0100 Subject: [PATCH 13/14] Minor rebase fixes --- src/ComputeSharp/ComputeSharp.csproj | 12 ++- .../Extensions/Interop/AllocatorExtensions.cs | 2 +- .../Graphics/Interop/AllocationPtr.cs | 93 ------------------- .../Graphics/Interop/AllocatorPtr.cs | 81 ---------------- 4 files changed, 9 insertions(+), 179 deletions(-) delete mode 100644 src/ComputeSharp/Graphics/Interop/AllocationPtr.cs delete mode 100644 src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index c35b06c5..eb294ea0 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -7,10 +7,14 @@ ComputeSharp - - - - + + + + + + + + - /// A -equivalent type to safely work with pointers. - /// - /// - /// While this type is not marked as so that it can also be used in fields, make sure to - /// keep the reference counts properly tracked if you do store instances on the heap. - /// - internal unsafe struct AllocationPtr : IDisposable - { - /// - /// The raw pointer to an object, if existing. - /// - private Allocation* pointer; - - /// - /// Creates a new instance from a raw pointer. - /// - /// The raw pointer to wrap. - public AllocationPtr(Allocation* other) - { - pointer = other; - } - - /// - /// Unwraps a instance and returns the internal raw pointer. - /// - /// The instance to unwrap. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Allocation*(AllocationPtr other) - { - return other.Get(); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Dispose() - { - Allocation* pointer = this.pointer; - - if (pointer != null) - { - this.pointer = null; - - pointer->Release(); - } - } - - /// - /// Gets the currently wrapped raw pointer to an object. - /// - /// The raw pointer wrapped by the current instance. - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly Allocation* Get() - { - return this.pointer; - } - - /// - /// Gets the address of the current instance as a raw double pointer. - /// This method is only valid when the current instance is on the stack or pinned. - /// - /// The raw pointer to the current instance. - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly Allocation** GetAddressOf() - { - return (Allocation**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); - } - - /// - /// Gets the address of the current instance as a raw double pointer. - /// - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public AllocationPtr Move() - { - AllocationPtr copy = this; - - this = default; - - return copy; - } - } -} diff --git a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs deleted file mode 100644 index bee25d48..00000000 --- a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.ComponentModel; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using TerraFX.Interop; - -namespace ComputeSharp.Graphics -{ - /// - /// A -equivalent type to safely work with pointers. - /// - /// - /// While this type is not marked as so that it can also be used in fields, make sure to - /// keep the reference counts properly tracked if you do store instances on the heap. - /// - internal unsafe struct AllocatorPtr : IDisposable - { - /// - /// The raw pointer to an object, if existing. - /// - private Allocator* pointer; - - /// - /// Creates a new instance from a raw pointer. - /// - /// The raw pointer to wrap. - public AllocatorPtr(Allocator* other) - { - pointer = other; - } - - /// - /// Unwraps a instance and returns the internal raw pointer. - /// - /// The instance to unwrap. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Allocator*(AllocatorPtr other) - { - return other.Get(); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Dispose() - { - Allocator* pointer = this.pointer; - - if (pointer != null) - { - this.pointer = null; - - pointer->Release(); - } - } - - /// - /// Gets the currently wrapped raw pointer to an object. - /// - /// The raw pointer wrapped by the current instance. - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly Allocator* Get() - { - return this.pointer; - } - - /// - /// Gets the address of the current instance as a managed reference to pointer. - /// - /// The reference to the current instance. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - [EditorBrowsable(EditorBrowsableState.Never)] - public readonly ref Allocator* GetPinnableReference() - { - fixed (Allocator** ptr = &this.pointer) - { - return ref *ptr; - } - } - } -} From 7c8c37e6ddeca916628865f1d9dfcf9e5109de9a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 6 Mar 2021 19:29:18 +0100 Subject: [PATCH 14/14] Switched to NuGet package for D3D12MA --- ComputeSharp.sln | 10 ---- src/ComputeSharp/ComputeSharp.csproj | 5 +- .../Extensions/Interop/AllocatorExtensions.cs | 46 +++++++++---------- src/ComputeSharp/Graphics/GraphicsDevice.cs | 14 +++--- .../Graphics/Interop/UniquePtr{T}.cs | 12 ++--- .../Graphics/Resources/Abstract/Buffer{T}.cs | 4 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 4 +- .../Resources/Abstract/Texture2D{T}.cs | 9 ++-- .../Resources/Abstract/Texture3D{T}.cs | 9 ++-- .../Resources/Abstract/TransferBuffer{T}.cs | 4 +- .../Abstract/TransferTexture2D{T}.cs | 4 +- .../Abstract/TransferTexture3D{T}.cs | 4 +- 12 files changed, 57 insertions(+), 68 deletions(-) diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 19c244ce..24463506 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -43,8 +43,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.NativeLi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.SwapChain", "samples\ComputeSharp.SwapChain\ComputeSharp.SwapChain.csproj", "{E7C9247D-016B-4819-AF9F-0E9B3591E04B}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TerraFX.Interop.D3D12MemoryAllocator", "..\terrafx.interop.d3d12memoryallocator\sources\Interop\D3D12MemoryAllocator\TerraFX.Interop.D3D12MemoryAllocator.csproj", "{DB64E751-125D-44E2-8EA9-F5B085714007}" -EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{7048deea-0a6f-46a0-b951-b682a8f1cb75}*SharedItemsImports = 5 @@ -154,14 +152,6 @@ Global {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|Any CPU.Build.0 = Release|Any CPU {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.ActiveCfg = Release|Any CPU {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.Build.0 = Release|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|x64.ActiveCfg = Debug|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Debug|x64.Build.0 = Debug|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|Any CPU.Build.0 = Release|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|x64.ActiveCfg = Release|Any CPU - {DB64E751-125D-44E2-8EA9-F5B085714007}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index eb294ea0..2119803e 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -10,10 +10,7 @@ - - - - + diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index 76f1847f..1e77beb6 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -5,31 +5,31 @@ using TerraFX.Interop; using static TerraFX.Interop.D3D12_HEAP_TYPE; using static TerraFX.Interop.D3D12_RESOURCE_FLAGS; using static TerraFX.Interop.D3D12_RESOURCE_STATES; -using static TerraFX.Interop.ALLOCATION_FLAGS; +using static TerraFX.Interop.D3D12MA_ALLOCATION_FLAGS; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; namespace ComputeSharp.Graphics.Extensions { /// - /// A with extensions for the type. + /// A with extensions for the type. /// internal static unsafe class AllocatorExtensions { /// /// Creates a resource for a given buffer type. /// - /// The instance in use. + /// The instance in use. /// The resource type currently in use. /// The allocation mode to use for the new resource. /// The size in bytes of the current buffer. - /// An reference for the current object. - public static UniquePtr CreateResource( - this ref Allocator allocator, + /// An reference for the current object. + public static UniquePtr CreateResource( + this ref D3D12MA_Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, ulong sizeInBytes) { - ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? ALLOCATION_FLAG_NONE : ALLOCATION_FLAG_COMMITTED; + D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED; (D3D12_HEAP_TYPE d3D12HeapType, D3D12_RESOURCE_FLAGS d3D12ResourceFlags, D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch @@ -42,11 +42,11 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using UniquePtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); - ALLOCATION_DESC allocationDesc = default; + D3D12MA_ALLOCATION_DESC allocationDesc = default; allocationDesc.HeapType = d3D12HeapType; allocationDesc.Flags = allocationFlags; @@ -65,16 +65,16 @@ namespace ComputeSharp.Graphics.Extensions /// /// Creates a resource for a given 2D texture type. /// - /// The instance in use. + /// The instance in use. /// The resource type currently in use. /// The allocation mode to use for the new resource. /// The value to use. /// The width of the texture resource. /// The height of the texture resource. /// The default value for the resource. - /// An reference for the current object. - public static UniquePtr CreateResource( - this ref Allocator allocator, + /// An reference for the current object. + public static UniquePtr CreateResource( + this ref D3D12MA_Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, DXGI_FORMAT dxgiFormat, @@ -82,7 +82,7 @@ namespace ComputeSharp.Graphics.Extensions uint height, out D3D12_RESOURCE_STATES d3D12ResourceStates) { - ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? ALLOCATION_FLAG_NONE : ALLOCATION_FLAG_COMMITTED; + D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -92,11 +92,11 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using UniquePtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags); - ALLOCATION_DESC allocationDesc = default; + D3D12MA_ALLOCATION_DESC allocationDesc = default; allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; allocationDesc.Flags = allocationFlags; @@ -115,7 +115,7 @@ namespace ComputeSharp.Graphics.Extensions /// /// Creates a resource for a given 3D texture type. /// - /// The instance in use. + /// The instance in use. /// The resource type currently in use. /// The allocation mode to use for the new resource. /// The value to use. @@ -123,9 +123,9 @@ namespace ComputeSharp.Graphics.Extensions /// The height of the texture resource. /// The depth of the texture resource. /// The default value for the resource. - /// An reference for the current object. - public static UniquePtr CreateResource( - this ref Allocator allocator, + /// An reference for the current object. + public static UniquePtr CreateResource( + this ref D3D12MA_Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, DXGI_FORMAT dxgiFormat, @@ -134,7 +134,7 @@ namespace ComputeSharp.Graphics.Extensions ushort depth, out D3D12_RESOURCE_STATES d3D12ResourceStates) { - ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? ALLOCATION_FLAG_NONE : ALLOCATION_FLAG_COMMITTED; + D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -144,11 +144,11 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using UniquePtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags); - ALLOCATION_DESC allocationDesc = default; + D3D12MA_ALLOCATION_DESC allocationDesc = default; allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; allocationDesc.Flags = allocationFlags; diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index d4f2a75e..affde884 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -73,9 +73,9 @@ namespace ComputeSharp private ulong nextD3D12CopyFenceValue = 1; /// - /// The in use associated to the current device. + /// The in use associated to the current device. /// - private UniquePtr allocator; + private UniquePtr allocator; /// /// Creates a new instance for the input . @@ -110,15 +110,15 @@ namespace ComputeSharp IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; - ALLOCATOR_DESC allocatorDesc = default; + D3D12MA_ALLOCATOR_DESC allocatorDesc = default; allocatorDesc.pDevice = d3D12Device; allocatorDesc.pAdapter = dxgiAdapter; if (!IsCacheCoherentUMA) { - fixed (Allocator** allocator = this.allocator) + fixed (D3D12MA_Allocator** allocator = this.allocator) { - D3D12MemoryAllocator.CreateAllocator(&allocatorDesc, allocator).Assert(); + D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator).Assert(); } } } @@ -165,9 +165,9 @@ namespace ComputeSharp internal ID3D12Device* D3D12Device => this.d3D12Device; /// - /// Gets the underlying wrapped by the current instance. + /// Gets the underlying wrapped by the current instance. /// - internal Allocator* Allocator => this.allocator; + internal D3D12MA_Allocator* Allocator => this.allocator; /// /// Gets whether or not the current device has a cache coherent UMA architecture. diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs index 071d6f3b..2cec96e1 100644 --- a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs +++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs @@ -7,9 +7,9 @@ using TerraFX.Interop; namespace ComputeSharp.Interop { /// - /// A -equivalent type to safely work with pointers to public APIs. + /// A -equivalent type to safely work with pointers to public APIs. /// - /// Must be either , , or . + /// Must be either , , or . /// /// This type is not marked as so that it can also be used in fields, so make sure to /// avoid copying it and disposing it multiple times, as that would lead to a access violation exceptions. @@ -51,10 +51,10 @@ namespace ComputeSharp.Interop { this.pointer = null; - if (typeof(T) == typeof(Allocator)) ((Allocator*)pointer)->Release(); - else if (typeof(T) == typeof(Allocation)) ((Allocation*)pointer)->Release(); - else if (typeof(T) == typeof(Pool)) ((Pool*)pointer)->Release(); - else if (typeof(T) == typeof(VirtualBlock)) ((VirtualBlock*)pointer)->Release(); + if (typeof(T) == typeof(D3D12MA_Allocator)) ((D3D12MA_Allocator*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_Allocation)) ((D3D12MA_Allocation*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_Pool)) ((D3D12MA_Pool*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_VirtualBlock)) ((D3D12MA_VirtualBlock*)pointer)->Release(); else throw new ArgumentException("Invalid pointer type"); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index e459ccc4..f1cca2ad 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -39,10 +39,10 @@ namespace ComputeSharp.Resources protected readonly nint SizeInBytes; /// - /// The instance used to retrieve , if any. + /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private UniquePtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 32f0630e..1266141d 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -59,7 +59,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { @@ -157,7 +157,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index ead1d3e9..5cfdce19 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -57,10 +57,10 @@ namespace ComputeSharp.Resources private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; /// - /// The instance used to retrieve , if any. + /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private UniquePtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. @@ -105,6 +105,7 @@ namespace ComputeSharp.Resources (uint)width, (uint)height, out this.d3D12ResourceState); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); } @@ -186,7 +187,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) @@ -336,7 +337,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 7fb6b115..f29c2db8 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -57,10 +57,10 @@ namespace ComputeSharp.Resources private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; /// - /// The instance used to retrieve , if any. + /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private UniquePtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. @@ -109,6 +109,7 @@ namespace ComputeSharp.Resources (uint)height, (ushort)depth, out this.d3D12ResourceState); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); } @@ -202,7 +203,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) @@ -369,7 +370,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index f4489052..66887f0c 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -28,10 +28,10 @@ namespace ComputeSharp.Resources private readonly T* mappedData; /// - /// The instance used to retrieve , if any. + /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private UniquePtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 8a2a8292..4a5e2d61 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -34,10 +34,10 @@ namespace ComputeSharp.Resources private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; /// - /// The instance used to retrieve , if any. + /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private UniquePtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index ce51f3e8..42e4b092 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -34,10 +34,10 @@ namespace ComputeSharp.Resources private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; /// - /// The instance used to retrieve , if any. + /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private UniquePtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters.