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 {