diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index 5bab1210..678f245e 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Helpers; using SharpDX.Direct3D12; +using CommandList = ComputeSharp.Graphics.Commands.CommandList; namespace ComputeSharp.Graphics.Buffers { diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index c0d5c50f..165d73b0 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Helpers; using SharpDX.Direct3D12; +using CommandList = ComputeSharp.Graphics.Commands.CommandList; namespace ComputeSharp.Graphics.Buffers { diff --git a/src/ComputeSharp.Graphics/Commands/Abstract/CommandController.cs b/src/ComputeSharp.Graphics/Commands/Abstract/CommandController.cs new file mode 100644 index 00000000..b2f13e42 --- /dev/null +++ b/src/ComputeSharp.Graphics/Commands/Abstract/CommandController.cs @@ -0,0 +1,35 @@ +using System; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Commands.Abstract +{ + /// + /// A base for an instance that deals with GPU commands in some way + /// + internal abstract class CommandController : IDisposable + { + /// + /// Creates a new instance with the specified parameters + /// + /// The input to use to perform requested operations + /// The type of command list to use for the current instance + protected CommandController(GraphicsDevice device, CommandListType commandListType) + { + GraphicsDevice = device; + CommandListType = commandListType; + } + + /// + /// Gets the object targeted by the current instance + /// + public GraphicsDevice GraphicsDevice { get; } + + /// + /// Gets the command list type being used by the current instance + /// + public CommandListType CommandListType { get; } + + /// + public abstract void Dispose(); + } +} diff --git a/src/ComputeSharp.Graphics/CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs similarity index 74% rename from src/ComputeSharp.Graphics/CommandAllocatorPool.cs rename to src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs index 6f802d8a..28fadec8 100644 --- a/src/ComputeSharp.Graphics/CommandAllocatorPool.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs @@ -1,40 +1,23 @@ using System; using System.Collections.Generic; using System.Diagnostics.Contracts; +using ComputeSharp.Graphics.Commands.Abstract; using SharpDX.Direct3D12; -namespace ComputeSharp.Graphics +namespace ComputeSharp.Graphics.Commands { /// /// A that acts as a pool to get new instances wheen needed /// - internal sealed class CommandAllocatorPool : IDisposable + internal sealed class CommandAllocatorPool : CommandController { /// /// The queue of currently pooled instances /// private readonly Queue<(CommandAllocator CommandAllocator, long FenceValue)> CommandAllocatorQueue = new Queue<(CommandAllocator, long)>(); - /// - /// Creates a new instance with the specified parameters - /// - /// The input to use to perform requested operations - /// The type of command list to use for the current instance - public CommandAllocatorPool(GraphicsDevice device, CommandListType commandListType) - { - GraphicsDevice = device; - CommandListType = commandListType; - } - - /// - /// Gets the object targeted by the current instance - /// - public GraphicsDevice GraphicsDevice { get; } - - /// - /// Gets the command list type being used by the current instance - /// - public CommandListType CommandListType { get; } + /// + public CommandAllocatorPool(GraphicsDevice device, CommandListType commandListType) : base(device, commandListType) { } /// /// Adds a new item to the internal pool queue @@ -84,7 +67,7 @@ namespace ComputeSharp.Graphics } /// - public void Dispose() + public override void Dispose() { lock (CommandAllocatorQueue) { diff --git a/src/ComputeSharp.Graphics/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs similarity index 95% rename from src/ComputeSharp.Graphics/CommandList.cs rename to src/ComputeSharp.Graphics/Commands/CommandList.cs index 2502c5ee..b7fc18a7 100644 --- a/src/ComputeSharp.Graphics/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -1,21 +1,20 @@ using System; using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Commands.Abstract; using SharpDX.Direct3D12; -namespace ComputeSharp.Graphics +namespace ComputeSharp.Graphics.Commands { /// /// A that represents a list of commands to issue to a GPU /// - public sealed class CommandList : IDisposable + internal sealed class CommandList : CommandController { private readonly CompiledCommandList currentCommandList; - public CommandList(GraphicsDevice device, CommandListType commandListType) + /// + public CommandList(GraphicsDevice device, CommandListType commandListType) : base(device, commandListType) { - GraphicsDevice = device; - CommandListType = commandListType; - CommandAllocator commandAllocator = GetCommandAllocator(); GraphicsCommandList nativeCommandList = GraphicsDevice.NativeDevice.CreateCommandList((SharpDX.Direct3D12.CommandListType)CommandListType, commandAllocator, null); @@ -24,10 +23,6 @@ namespace ComputeSharp.Graphics SetDescriptorHeaps(GraphicsDevice.ShaderResourceViewAllocator.DescriptorHeap); } - public CommandListType CommandListType { get; } - - public GraphicsDevice GraphicsDevice { get; } - public CompiledCommandList Close() { //foreach (var renderTarget in RenderTargets) @@ -55,29 +50,6 @@ namespace ComputeSharp.Graphics currentCommandList.NativeCommandList.Dispatch(threadGroupCountX, threadGroupCountY, threadGroupCountZ); } - public void Dispose() - { - switch (CommandListType) - { - case CommandListType.Bundle: - GraphicsDevice.BundleAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextDirectFenceValue - 1); - break; - case CommandListType.Direct: - GraphicsDevice.DirectAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextDirectFenceValue - 1); - break; - case CommandListType.Compute: - GraphicsDevice.ComputeAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextComputeFenceValue - 1); - break; - case CommandListType.Copy: - GraphicsDevice.CopyAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextCopyFenceValue - 1); - break; - default: - throw new NotSupportedException("This command list type is not supported."); - } - - currentCommandList.NativeCommandList.Dispose(); - } - public void Flush(bool wait = false) { GraphicsDevice.ExecuteCommandLists(wait, Close()); @@ -168,5 +140,29 @@ namespace ComputeSharp.Graphics CommandListType.Direct => GraphicsDevice.DirectAllocatorPool.GetCommandAllocator(), _ => throw new NotSupportedException("This command list type is not supported.") }; + + /// + public override void Dispose() + { + switch (CommandListType) + { + case CommandListType.Bundle: + GraphicsDevice.BundleAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextDirectFenceValue - 1); + break; + case CommandListType.Direct: + GraphicsDevice.DirectAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextDirectFenceValue - 1); + break; + case CommandListType.Compute: + GraphicsDevice.ComputeAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextComputeFenceValue - 1); + break; + case CommandListType.Copy: + GraphicsDevice.CopyAllocatorPool.Enqueue(currentCommandList.NativeCommandAllocator, GraphicsDevice.NextCopyFenceValue - 1); + break; + default: + throw new NotSupportedException("This command list type is not supported."); + } + + currentCommandList.NativeCommandList.Dispose(); + } } } diff --git a/src/ComputeSharp.Graphics/CompiledCommandList.cs b/src/ComputeSharp.Graphics/CompiledCommandList.cs index 57e2a7ff..8cb61ae1 100644 --- a/src/ComputeSharp.Graphics/CompiledCommandList.cs +++ b/src/ComputeSharp.Graphics/CompiledCommandList.cs @@ -1,4 +1,5 @@ using SharpDX.Direct3D12; +using CommandList = ComputeSharp.Graphics.Commands.CommandList; namespace ComputeSharp.Graphics { diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 0a6adde2..03d4b66c 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -1,8 +1,10 @@ using System; using System.Threading; +using ComputeSharp.Graphics.Commands; using SharpDX.Direct3D; using SharpDX.Direct3D12; using SharpDX.DXGI; +using CommandList = ComputeSharp.Graphics.Commands.CommandList; using Device = SharpDX.Direct3D12.Device; namespace ComputeSharp.Graphics @@ -56,9 +58,9 @@ namespace ComputeSharp.Graphics /// public int WavefrontSize { get; } - public CommandList CommandList { get; } + internal CommandList CommandList { get; } - public CommandList CopyCommandList { get; } + internal CommandList CopyCommandList { get; } public const FeatureLevel FeatureLevel = SharpDX.Direct3D.FeatureLevel.Level_12_1; diff --git a/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs b/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..8fb2fba6 --- /dev/null +++ b/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("ComputeSharp.Shaders")] diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 150a5c2a..5358df9e 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -5,7 +5,7 @@ using ComputeSharp.Shaders.Renderer; using ComputeSharp.Shaders.Renderer.Models; using ComputeSharp.Shaders.Translation; using SharpDX.Direct3D12; -using CommandList = ComputeSharp.Graphics.CommandList; +using CommandList = ComputeSharp.Graphics.Commands.CommandList; using PipelineState = ComputeSharp.Graphics.PipelineState; namespace ComputeSharp.Shaders