diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index c35b06c5..2119803e 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -7,10 +7,11 @@ ComputeSharp - - - - + + + + + /// 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/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs new file mode 100644 index 00000000..1e77beb6 --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -0,0 +1,167 @@ +using ComputeSharp.Core.Extensions; +using ComputeSharp.Interop; +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.D3D12MA_ALLOCATION_FLAGS; +using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; + +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 object. + public static UniquePtr CreateResource( + this ref D3D12MA_Allocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + ulong sizeInBytes) + { + 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 + { + 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 UniquePtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); + + D3D12MA_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 object. + public static UniquePtr CreateResource( + this ref D3D12MA_Allocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_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 UniquePtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags); + + D3D12MA_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 object. + public static UniquePtr CreateResource( + this ref D3D12MA_Allocator allocator, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + ushort depth, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_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 UniquePtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags); + + D3D12MA_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/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 b2c12d7e..affde884 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -72,18 +72,24 @@ namespace ComputeSharp /// private ulong nextD3D12CopyFenceValue = 1; + /// + /// The in use associated to the current device. + /// + private UniquePtr 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(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); @@ -95,14 +101,26 @@ 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; + + D3D12MA_ALLOCATOR_DESC allocatorDesc = default; + allocatorDesc.pDevice = d3D12Device; + allocatorDesc.pAdapter = dxgiAdapter; + + if (!IsCacheCoherentUMA) + { + fixed (D3D12MA_Allocator** allocator = this.allocator) + { + D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator).Assert(); + } + } } /// @@ -146,6 +164,11 @@ namespace ComputeSharp /// internal ID3D12Device* D3D12Device => this.d3D12Device; + /// + /// Gets the underlying wrapped by the current instance. + /// + internal D3D12MA_Allocator* Allocator => this.allocator; + /// /// Gets whether or not the current device has a cache coherent UMA architecture. /// @@ -316,6 +339,7 @@ namespace ComputeSharp this.computeCommandAllocatorPool.Dispose(); this.copyCommandAllocatorPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); + this.allocator.Dispose(); return true; } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index be3c34f6..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(), &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(), &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 e2665b77..cf5d55f9 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(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { lock (DevicesCache) { @@ -35,12 +36,12 @@ 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); #if DEBUG - D3D12InfoQueueMap.Add(luid, d3D12Device.Get()->CreateInfoQueue()); + D3D12InfoQueueMap.Add(luid, d3D12Device->CreateInfoQueue()); #endif } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index 8913be29..def2b0c0 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.Get(), 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/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs new file mode 100644 index 00000000..2cec96e1 --- /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.Interop +{ + /// + /// A -equivalent type to safely work with pointers to public APIs. + /// + /// 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. + /// + 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(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"); + } + } + + /// + /// 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 fbb8ba9b..f1cca2ad 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -38,6 +38,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 UniquePtr allocation; + /// /// Creates a new instance with the specified parameters. /// @@ -64,7 +70,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 +170,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..1266141d 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -2,6 +2,7 @@ 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; @@ -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 UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); 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)byteLength); 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 UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); - 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)byteLength); copyCommandList.ExecuteAndWaitForCompletion(); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 9510905c..5cfdce19 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -56,6 +56,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. /// @@ -79,20 +85,35 @@ 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 : D3D12_COMMAND_LIST_TYPE_COMPUTE; - GraphicsDevice.D3D12Device->GetCopyableFootprint( + device.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -166,7 +187,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using UniquePtr 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 +337,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using UniquePtr 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 +458,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..f29c2db8 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -56,6 +56,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. /// @@ -81,21 +87,37 @@ 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 : D3D12_COMMAND_LIST_TYPE_COMPUTE; - GraphicsDevice.D3D12Device->GetCopyableFootprint( + device.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -181,7 +203,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using UniquePtr 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 +370,18 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using UniquePtr 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) @@ -458,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..66887f0c 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -27,6 +27,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 +52,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 +115,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..4a5e2d61 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{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. /// @@ -55,7 +61,7 @@ namespace ComputeSharp.Resources GraphicsDevice = device; - GraphicsDevice.D3D12Device->GetCopyableFootprint( + device.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -63,7 +69,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 +125,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..42e4b092 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; } diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index ad13f33f..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; @@ -212,7 +211,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); } 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 {