From 07b3608fe0adf41fcf97e6e9f383ec6046543712 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 26 Aug 2019 19:57:43 +0200 Subject: [PATCH] ComputeSharp.Graphics project switched to Vortice.Windows --- .../Buffers/Abstract/GraphicsResource.cs | 6 +- .../Abstract/HlslStructuredBuffer{T}.cs | 2 +- .../Buffers/Buffer{T}.cs | 10 +-- .../Buffers/ConstantBuffer{T}.cs | 2 +- .../Commands/Abstract/CommandController.cs | 2 +- .../Commands/CommandAllocatorPool.cs | 24 +++---- .../Commands/CommandList.cs | 16 ++--- .../Commands/PipelineState.cs | 18 ++--- .../ComputeSharp.Graphics.csproj | 2 +- .../DescriptorAllocator.cs | 14 ++-- src/ComputeSharp.Graphics/GraphicsDevice.cs | 67 +++++++++---------- .../Helpers/DeviceHelper.cs | 42 ++++++------ src/ComputeSharp/Gpu.cs | 6 +- 13 files changed, 103 insertions(+), 108 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs index cc32bf05..184df405 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs @@ -1,5 +1,5 @@ using System; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; namespace ComputeSharp.Graphics.Buffers.Abstract { @@ -28,9 +28,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract internal IntPtr MappedResource { get; private set; } /// - /// Gets the object currently mapped + /// Gets the object currently mapped /// - protected internal Resource? NativeResource { get; set; } + protected internal ID3D12Resource? NativeResource { get; set; } /// /// Gets the instance for the current resource diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs index 29a747f7..d4fea65b 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs @@ -2,7 +2,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Helpers; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; using CommandList = ComputeSharp.Graphics.Commands.CommandList; namespace ComputeSharp.Graphics.Buffers.Abstract diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs index c08a8304..7ccd975e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs @@ -3,7 +3,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; namespace ComputeSharp.Graphics.Buffers { @@ -112,8 +112,8 @@ namespace ComputeSharp.Graphics.Buffers ShaderResourceViewDescription description = new ShaderResourceViewDescription { Shader4ComponentMapping = 5768, - Dimension = ShaderResourceViewDimension.Buffer, - Buffer = { ElementCount = Size, StructureByteStride = ElementSizeInBytes } + ViewDimension = ShaderResourceViewDimension.Buffer, + Buffer = { NumElements = Size, StructureByteStride = ElementSizeInBytes } }; GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, description, cpuHandle); @@ -132,8 +132,8 @@ namespace ComputeSharp.Graphics.Buffers UnorderedAccessViewDescription description = new UnorderedAccessViewDescription { - Dimension = UnorderedAccessViewDimension.Buffer, - Buffer = { ElementCount = Size, StructureByteStride = ElementSizeInBytes } + ViewDimension = UnorderedAccessViewDimension.Buffer, + Buffer = { NumElements = Size, StructureByteStride = ElementSizeInBytes } }; GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle); diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 7302eee3..ae4c072d 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -7,7 +7,7 @@ using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Helpers; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; using CommandList = ComputeSharp.Graphics.Commands.CommandList; namespace ComputeSharp diff --git a/src/ComputeSharp.Graphics/Commands/Abstract/CommandController.cs b/src/ComputeSharp.Graphics/Commands/Abstract/CommandController.cs index b2f13e42..2c0d895e 100644 --- a/src/ComputeSharp.Graphics/Commands/Abstract/CommandController.cs +++ b/src/ComputeSharp.Graphics/Commands/Abstract/CommandController.cs @@ -1,5 +1,5 @@ using System; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; namespace ComputeSharp.Graphics.Commands.Abstract { diff --git a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs index a5bcb7a0..8beff52f 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs @@ -2,29 +2,29 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using ComputeSharp.Graphics.Commands.Abstract; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; namespace ComputeSharp.Graphics.Commands { /// - /// A that acts as a pool to get new instances wheen needed + /// A that acts as a pool to get new instances wheen needed /// internal sealed class CommandAllocatorPool : CommandController { /// - /// The queue of currently pooled instances + /// The queue of currently pooled instances /// - private readonly Queue<(CommandAllocator CommandAllocator, long FenceValue)> CommandAllocatorQueue = new Queue<(CommandAllocator, long)>(); + private readonly Queue<(ID3D12CommandAllocator CommandAllocator, long FenceValue)> CommandAllocatorQueue = new Queue<(ID3D12CommandAllocator, long)>(); /// public CommandAllocatorPool(GraphicsDevice device, CommandListType commandListType) : base(device, commandListType) { } /// - /// Adds a new item to the internal pool queue + /// Adds a new item to the internal pool queue /// - /// The input to enqueue - /// The fence value for the input - public void Enqueue(CommandAllocator commandAllocator, long fenceValue) + /// The input to enqueue + /// The fence value for the input + public void Enqueue(ID3D12CommandAllocator commandAllocator, long fenceValue) { lock (CommandAllocatorQueue) { @@ -33,16 +33,16 @@ namespace ComputeSharp.Graphics.Commands } /// - /// Gets a new or reused item to use to issue GPU commands + /// Gets a new or reused item to use to issue GPU commands /// [Pure] - public CommandAllocator GetCommandAllocator() + public ID3D12CommandAllocator GetCommandAllocator() { lock (CommandAllocatorQueue) { if (CommandAllocatorQueue.Count > 0) { - (CommandAllocator commandAllocator, long fenceValue) = CommandAllocatorQueue.Peek(); + (ID3D12CommandAllocator commandAllocator, long fenceValue) = CommandAllocatorQueue.Peek(); long completedValue = CommandListType switch { @@ -70,7 +70,7 @@ namespace ComputeSharp.Graphics.Commands { lock (CommandAllocatorQueue) { - foreach ((CommandAllocator commandAllocator, long _) in CommandAllocatorQueue) + foreach ((ID3D12CommandAllocator commandAllocator, long _) in CommandAllocatorQueue) { commandAllocator.Dispose(); } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index b26c690f..de0a4d99 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -1,7 +1,7 @@ using System; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Commands.Abstract; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; namespace ComputeSharp.Graphics.Commands { @@ -22,23 +22,23 @@ namespace ComputeSharp.Graphics.Commands }; NativeCommandList = GraphicsDevice.NativeDevice.CreateCommandList(CommandListType, CommandAllocator, null); - // Set the heap descriptor if the command list is for copy operations + // Set the heap descriptor if the command list is not for copy operations if (CommandListType != CommandListType.Copy) { - NativeCommandList.SetDescriptorHeaps(GraphicsDevice.ShaderResourceViewAllocator.DescriptorHeap); + NativeCommandList.SetDescriptorHeaps(1, new[] { GraphicsDevice.ShaderResourceViewAllocator.DescriptorHeap }); } } /// - /// Gets the object in use by the current instance + /// Gets the object in use by the current instance /// - public CommandAllocator CommandAllocator { get; } + public ID3D12CommandAllocator CommandAllocator { get; } /// - /// Gets the object in use by the current instance + /// Gets the object in use by the current instance /// - public GraphicsCommandList NativeCommandList { get; } + public ID3D12GraphicsCommandList NativeCommandList { get; } /// /// Copies a memory region from one resource to another @@ -71,7 +71,7 @@ namespace ComputeSharp.Graphics.Commands public void SetPipelineState(PipelineState pipelineState) { NativeCommandList.SetComputeRootSignature(pipelineState.RootSignature); - NativeCommandList.PipelineState = pipelineState.NativePipelineState; + NativeCommandList.SetPipelineState(pipelineState.NativePipelineState); } /// diff --git a/src/ComputeSharp.Graphics/Commands/PipelineState.cs b/src/ComputeSharp.Graphics/Commands/PipelineState.cs index 33bc2e86..64350e46 100644 --- a/src/ComputeSharp.Graphics/Commands/PipelineState.cs +++ b/src/ComputeSharp.Graphics/Commands/PipelineState.cs @@ -1,4 +1,4 @@ -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; namespace ComputeSharp.Graphics.Commands { @@ -11,25 +11,19 @@ namespace ComputeSharp.Graphics.Commands /// Creates a new instance with the specified parameters /// /// The to use - /// The value for the current shader + /// The value for the current shader /// The bytecode for the compute shader to run - public PipelineState(GraphicsDevice device, RootSignature rootSignature, ShaderBytecode computeShader) + public PipelineState(GraphicsDevice device, ID3D12RootSignature rootSignature, ShaderBytecode computeShader) { - RootSignaturePointer = rootSignature; + RootSignature = rootSignature; ComputeShader = computeShader; - RootSignature = RootSignaturePointer; NativePipelineState = device.NativeDevice.CreateComputePipelineState(this); } /// - /// Gets the value for the current shader + /// Gets the instance for the current object /// - public RootSignature RootSignature { get; } - - /// - /// Gets the instance for the current object - /// - public SharpDX.Direct3D12.PipelineState NativePipelineState { get; } + public ID3D12PipelineState NativePipelineState { get; } } } diff --git a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index 6452507c..e369ab7f 100644 --- a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -8,8 +8,8 @@ - + diff --git a/src/ComputeSharp.Graphics/DescriptorAllocator.cs b/src/ComputeSharp.Graphics/DescriptorAllocator.cs index ee34f95f..b222f967 100644 --- a/src/ComputeSharp.Graphics/DescriptorAllocator.cs +++ b/src/ComputeSharp.Graphics/DescriptorAllocator.cs @@ -1,5 +1,5 @@ using System; -using SharpDX.Direct3D12; +using Vortice.DirectX.Direct3D12; namespace ComputeSharp.Graphics { @@ -56,14 +56,14 @@ namespace ComputeSharp.Graphics DescriptorHeap = device.NativeDevice.CreateDescriptorHeap(descriptorHeapDescription); _RemainingHandles = DescriptorsPerHeap; - _CurrentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart; - _CurrentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart; + _CurrentCpuHandle = DescriptorHeap.GetCPUDescriptorHandleForHeapStart(); + _CurrentGpuHandle = DescriptorHeap.GetGPUDescriptorHandleForHeapStart(); } /// - /// Gets the object in use for the current instance + /// Gets the object in use for the current instance /// - public DescriptorHeap DescriptorHeap { get; } + public ID3D12DescriptorHeap DescriptorHeap { get; } /// /// Allocates a new CPU and GPU handle pair to use in a memory buffer @@ -76,8 +76,8 @@ namespace ComputeSharp.Graphics if (_RemainingHandles == 0) { _RemainingHandles = DescriptorHeap.Description.DescriptorCount; - _CurrentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart; - _CurrentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart; + _CurrentCpuHandle = DescriptorHeap.GetCPUDescriptorHandleForHeapStart(); + _CurrentGpuHandle = DescriptorHeap.GetGPUDescriptorHandleForHeapStart(); } CpuDescriptorHandle cpuDescriptorHandle = _CurrentCpuHandle; diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index dc3983d2..8094c30a 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -1,11 +1,10 @@ using System; using System.Threading; using ComputeSharp.Graphics.Commands; -using SharpDX.Direct3D; -using SharpDX.Direct3D12; -using SharpDX.DXGI; +using Vortice.DirectX.Direct3D; +using Vortice.DirectX.Direct3D12; +using Vortice.DirectX.DXGI; using CommandList = ComputeSharp.Graphics.Commands.CommandList; -using Device = SharpDX.Direct3D12.Device; namespace ComputeSharp.Graphics { @@ -15,9 +14,9 @@ namespace ComputeSharp.Graphics public sealed class GraphicsDevice : IDisposable { /// - /// Gets the value that needs to be supported by GPUs mapped to a instance + /// Gets the value that needs to be supported by GPUs mapped to a instance /// - public const FeatureLevel FeatureLevel = SharpDX.Direct3D.FeatureLevel.Level_12_1; + public const FeatureLevel FeatureLevel = Vortice.DirectX.Direct3D.FeatureLevel.Level_12_1; /// /// The instance used to wait for completion when executing commands @@ -25,26 +24,26 @@ namespace ComputeSharp.Graphics private readonly AutoResetEvent AutoResetEvent = new AutoResetEvent(false); /// - /// The instance to use for compute operations + /// The instance to use for compute operations /// - private readonly CommandQueue NativeComputeCommandQueue; + private readonly ID3D12CommandQueue NativeComputeCommandQueue; /// - /// The instance to use for copy operations + /// The instance to use for copy operations /// - private readonly CommandQueue NativeCopyCommandQueue; + private readonly ID3D12CommandQueue NativeCopyCommandQueue; /// - /// The instance to use for direct operations + /// The instance to use for direct operations /// - private readonly CommandQueue NativeDirectCommandQueue; + private readonly ID3D12CommandQueue NativeDirectCommandQueue; /// - /// Creates a new instance for the input + /// Creates a new instance for the input /// - /// The to use for the new instance + /// The to use for the new instance /// The available info for the new instance - public GraphicsDevice(Device device, AdapterDescription description) + public GraphicsDevice(ID3D12Device device, AdapterDescription description) { NativeDevice = device; Description = description; @@ -58,9 +57,9 @@ namespace ComputeSharp.Graphics CopyAllocatorPool = new CommandAllocatorPool(this, CommandListType.Copy); DirectAllocatorPool = new CommandAllocatorPool(this, CommandListType.Direct); - NativeComputeFence = NativeDevice.CreateFence(0, FenceFlags.None); - NativeCopyFence = NativeDevice.CreateFence(0, FenceFlags.None); - NativeDirectFence = NativeDevice.CreateFence(0, FenceFlags.None); + NativeComputeFence = NativeDevice.CreateFence(0); + NativeCopyFence = NativeDevice.CreateFence(0); + NativeDirectFence = NativeDevice.CreateFence(0); ShaderResourceViewAllocator = new DescriptorAllocator(this); } @@ -76,9 +75,9 @@ namespace ComputeSharp.Graphics public int WavefrontSize { get; } /// - /// Gets the object wrapped by the current instance + /// Gets the object wrapped by the current instance /// - internal Device NativeDevice { get; } + internal ID3D12Device NativeDevice { get; } /// /// Gets the object for the current instance, used when allocating new buffers @@ -101,19 +100,19 @@ namespace ComputeSharp.Graphics internal CommandAllocatorPool DirectAllocatorPool { get; } /// - /// Gets the instance used for compute operations + /// Gets the instance used for compute operations /// - internal Fence NativeComputeFence { get; } + internal ID3D12Fence NativeComputeFence { get; } /// - /// Gets the instance used for copy operations + /// Gets the instance used for copy operations /// - internal Fence NativeCopyFence { get; } + internal ID3D12Fence NativeCopyFence { get; } /// - /// Gets the instance used for direct operations + /// Gets the instance used for direct operations /// - internal Fence NativeDirectFence { get; } + internal ID3D12Fence NativeDirectFence { get; } /// /// Gets the next fence value for compute operations @@ -131,13 +130,13 @@ namespace ComputeSharp.Graphics internal long NextDirectFenceValue { get; private set; } = 1; /// - /// Creates a instance from a description + /// Creates a instance from a description /// - /// A instance with the info for the new object to create - /// A new instance to use in a compute shader - internal RootSignature CreateRootSignature(RootSignatureDescription rootSignatureDescription) + /// A instance with the info for the new object to create + /// A new instance to use in a compute shader + internal ID3D12RootSignature CreateRootSignature(VersionedRootSignatureDescription rootSignatureDescription) { - return NativeDevice.CreateRootSignature(rootSignatureDescription.Serialize()); + return NativeDevice.CreateRootSignature(rootSignatureDescription); } /// @@ -146,7 +145,7 @@ namespace ComputeSharp.Graphics /// The input to execute internal void ExecuteCommandList(CommandList commandList) { - Fence fence = commandList.CommandListType switch + ID3D12Fence fence = commandList.CommandListType switch { CommandListType.Direct => NativeDirectFence, CommandListType.Compute => NativeComputeFence, @@ -173,8 +172,8 @@ namespace ComputeSharp.Graphics private long GetFenceValueForCommandList(CommandList commandList) { CommandAllocatorPool commandAllocatorPool; - CommandQueue commandQueue; - Fence fence; + ID3D12CommandQueue commandQueue; + ID3D12Fence fence; long fenceValue; switch (commandList.CommandListType) diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index 4bcd4b67..7b19ebd9 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; -using SharpDX.DXGI; -using Device = SharpDX.Direct3D12.Device; +using Vortice.DirectX.Direct3D; +using Vortice.DirectX.Direct3D12; +using Vortice.DirectX.DXGI; namespace ComputeSharp.Graphics.Helpers { @@ -13,28 +14,29 @@ namespace ComputeSharp.Graphics.Helpers /// /// Gets a collection of all the supported devices on the current machine /// - /// The collection of objects with support for DX12.1 + /// The collection of objects with support for DX12.1 [Pure] - public static IReadOnlyList<(Device, AdapterDescription)> QueryAllSupportedDevices() + public static IReadOnlyList<(ID3D12Device, AdapterDescription)> QueryAllSupportedDevices() { - using Factory factory = new Factory1(); - List<(Device, AdapterDescription)> devices = new List<(Device, AdapterDescription)>(); - - foreach (Adapter adapter in factory.Adapters) + if (!DXGI.CreateDXGIFactory1(out IDXGIFactory1 factory).Success) return new (ID3D12Device, AdapterDescription)[0]; + try { - if (adapter.Description.DedicatedVideoMemory == 0) continue; - try - { - Device device = new Device(adapter, GraphicsDevice.FeatureLevel); - devices.Add((device, adapter.Description)); - } - catch - { - // Unsupported device - } - } + List<(ID3D12Device, AdapterDescription)> devices = new List<(ID3D12Device, AdapterDescription)>(); - return devices; + foreach (IDXGIAdapter1 adapter in factory.EnumAdapters1()) + { + if (adapter.Description.DedicatedVideoMemory == 0) continue; + if (D3D12.D3D12CreateDevice(adapter, FeatureLevel.Level_12_1, out ID3D12Device device).Success) + devices.Add((device, adapter.Description)); + } + + return devices; + } + finally + { + // Explicit using statement + factory?.Dispose(); + } } } } diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index 0bfe9b60..ed09bbcf 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -4,8 +4,8 @@ using System.Diagnostics.Contracts; using System.Linq; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Helpers; -using SharpDX.DXGI; -using Device = SharpDX.Direct3D12.Device; +using Vortice.DirectX.Direct3D12; +using Vortice.DirectX.DXGI; namespace ComputeSharp { @@ -42,6 +42,6 @@ namespace ComputeSharp public static IEnumerable EnumerateDevices() => _Devices.Select(device => new GraphicsDevice(device.Device, device.Description)); // The loaded collection of supported devices - private static IReadOnlyList<(Device Device, AdapterDescription Description)> _Devices; + private static IReadOnlyList<(ID3D12Device Device, AdapterDescription Description)> _Devices; } }