diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index d1c1e343..07b57fba 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -64,7 +64,7 @@ namespace DirectX12ComputeShaderSample array[i] = i + 1; } - using Buffer inputBuffer = Buffer.New(device, array.AsSpan(), BufferFlags.UnorderedAccess, GraphicsHeapType.Default); + using Buffer gpuBuffer = Buffer.UnorderedAccess.New(device, array.AsSpan()); // Execute computer shader @@ -73,28 +73,18 @@ namespace DirectX12ComputeShaderSample commandList.SetPipelineState(pipelineState); commandList.SetComputeRoot32BitConstant(0, width, 0); - commandList.SetComputeRootDescriptorTable(1, inputBuffer); + commandList.SetComputeRootDescriptorTable(1, gpuBuffer); commandList.Dispatch(2, 2, 1); commandList.Flush(true); } - // Copy data back to CPU - - using Buffer outputBuffer = Buffer.New(device, array.Length, BufferFlags.None, GraphicsHeapType.Readback); - - using (CommandList copyCommandList = new CommandList(device, CommandListType.Copy)) - { - copyCommandList.CopyResource(inputBuffer, outputBuffer); - copyCommandList.Flush(true); - } - // Print matrix Console.WriteLine("Before:"); PrintMatrix(array, width, height); - outputBuffer.GetData(array.AsSpan()); + gpuBuffer.GetData(array.AsSpan()); Console.WriteLine(); Console.WriteLine("After:"); @@ -118,3 +108,4 @@ namespace DirectX12ComputeShaderSample } } } + diff --git a/src/DirectX12GameEngine.Graphics/Buffer.UnorderedAccess.cs b/src/DirectX12GameEngine.Graphics/Buffer.UnorderedAccess.cs new file mode 100644 index 00000000..8fd99873 --- /dev/null +++ b/src/DirectX12GameEngine.Graphics/Buffer.UnorderedAccess.cs @@ -0,0 +1,25 @@ +using System; + +namespace DirectX12GameEngine.Graphics +{ + public partial class Buffer + { + public static class UnorderedAccess + { + public static unsafe Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Default) + { + return Buffer.New(device, size, BufferFlags.UnorderedAccess, heapType); + } + + public static unsafe Buffer New(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged + { + return Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType); + } + + public static unsafe Buffer New(GraphicsDevice device, Span data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged + { + return Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType); + } + } + } +} \ No newline at end of file diff --git a/src/DirectX12GameEngine.Graphics/Buffer.cs b/src/DirectX12GameEngine.Graphics/Buffer.cs index 4899a292..8fe4b741 100644 --- a/src/DirectX12GameEngine.Graphics/Buffer.cs +++ b/src/DirectX12GameEngine.Graphics/Buffer.cs @@ -33,39 +33,53 @@ namespace DirectX12GameEngine.Graphics return data; } - public void GetData(ref T data) where T : unmanaged + public unsafe void GetData(ref T data, int offsetInBytes = 0) where T : unmanaged { - if (Usage != GraphicsHeapType.Default) + fixed (T* pointer = &data) + { + GetData(new Span(pointer, 1), offsetInBytes); + } + } + + public void GetData(Span data, int offsetInBytes = 0) where T : unmanaged + { + if (Usage == GraphicsHeapType.Default) + { + using Buffer readbackBaffer = New(GraphicsDevice, data.Length, BufferFlags.None, GraphicsHeapType.Readback); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + copyCommandList.CopyBufferRegion(this, offsetInBytes, readbackBaffer, 0, data.Length * Unsafe.SizeOf()); + copyCommandList.Flush(true); + + readbackBaffer.GetData(data); + } + else { Map(0); - MemoryHelper.Copy(MappedResource, ref data); + MemoryHelper.Copy(MappedResource + offsetInBytes, data); Unmap(0); } } - public void GetData(Span data) where T : unmanaged + public unsafe void SetData(in T data, int offsetInBytes = 0) where T : unmanaged { - if (Usage != GraphicsHeapType.Default) + fixed (T* pointer = &data) { - Map(0); - MemoryHelper.Copy(MappedResource, data); - Unmap(0); - } - } - - public void SetData(in T data, int offsetInBytes = 0) where T : unmanaged - { - if (Usage != GraphicsHeapType.Default) - { - Map(0); - MemoryHelper.Copy(data, MappedResource + offsetInBytes); - Unmap(0); + SetData(new Span(pointer, 1), offsetInBytes); } } public void SetData(Span data, int offsetInBytes = 0) where T : unmanaged { - if (Usage != GraphicsHeapType.Default) + if (Usage == GraphicsHeapType.Default) + { + using Buffer uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, offsetInBytes, data.Length * Unsafe.SizeOf()); + copyCommandList.Flush(true); + } + else { Map(0); MemoryHelper.Copy(data, MappedResource + offsetInBytes); @@ -73,6 +87,15 @@ namespace DirectX12GameEngine.Graphics } } + public Buffer ToReadback() + { + BufferDescription description = Description; + description.HeapType = GraphicsHeapType.Readback; + description.Flags = BufferFlags.None; + + return New(GraphicsDevice, description); + } + public static Buffer New(GraphicsDevice device, BufferDescription description) { return new Buffer(device).InitializeFrom(description); @@ -101,7 +124,7 @@ namespace DirectX12GameEngine.Graphics public static Buffer New(GraphicsDevice device, Span data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged { Buffer buffer = New(device, data.Length, bufferFlags, heapType); - buffer.Recreate(data); + buffer.SetData(data); return buffer; } @@ -136,25 +159,6 @@ namespace DirectX12GameEngine.Graphics return this; } - protected internal void Recreate(Span data) where T : unmanaged - { - if (Description.HeapType == GraphicsHeapType.Upload) - { - SetData(data); - } - else - { - using Buffer uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload); - - CommandList copyCommandList = GraphicsDevice.GetOrCreateCopyCommandList(); - - copyCommandList.CopyResource(uploadBuffer, this); - copyCommandList.Flush(true); - - GraphicsDevice.EnqueueCopyCommandList(copyCommandList); - } - } - private static ResourceDescription ConvertToNativeDescription(BufferDescription description) { int size = description.SizeInBytes; diff --git a/src/DirectX12GameEngine.Graphics/GraphicsDevice.cs b/src/DirectX12GameEngine.Graphics/GraphicsDevice.cs index 7ec618fe..1a30ff15 100644 --- a/src/DirectX12GameEngine.Graphics/GraphicsDevice.cs +++ b/src/DirectX12GameEngine.Graphics/GraphicsDevice.cs @@ -44,18 +44,21 @@ namespace DirectX12GameEngine.Graphics CommandList = new CommandList(this, CommandListType.Direct); CommandList.Close(); + + CopyCommandList = new CommandList(this, CommandListType.Copy); + CopyCommandList.Close(); } public CommandList CommandList { get; } + public CommandList CopyCommandList { get; } + public ICollection Disposables { get; } = new List(); public FeatureLevel FeatureLevel { get; } public GraphicsPresenter? Presenter { get; set; } - internal Queue CopyCommandLists { get; } = new Queue(); - internal Device NativeDevice { get; } internal SharpDX.Direct3D11.Device NativeDirect3D11Device => NativeDirect3D11Device ?? (nativeDirect3D11Device = SharpDX.Direct3D11.Device.CreateFromDirect3D12( @@ -141,6 +144,7 @@ namespace DirectX12GameEngine.Graphics NativeDirectCommandQueue.Wait(NativeDirectFence, NextDirectFenceValue); CommandList.Dispose(); + CopyCommandList.Dispose(); DepthStencilViewAllocator.Dispose(); RenderTargetViewAllocator.Dispose(); @@ -159,11 +163,6 @@ namespace DirectX12GameEngine.Graphics NativeDirectFence.Dispose(); NativeDirectFence.Dispose(); - foreach (CommandList commandList in CopyCommandLists) - { - commandList.Dispose(); - } - foreach (IDisposable disposable in Disposables) { disposable.Dispose(); @@ -243,34 +242,6 @@ namespace DirectX12GameEngine.Graphics return fenceValue; } - public CommandList GetOrCreateCopyCommandList() - { - CommandList commandList; - - lock (CopyCommandLists) - { - if (CopyCommandLists.Count > 0) - { - commandList = CopyCommandLists.Dequeue(); - commandList.Reset(); - } - else - { - commandList = new CommandList(this, CommandListType.Copy); - } - } - - return commandList; - } - - public void EnqueueCopyCommandList(CommandList commandList) - { - lock (CopyCommandLists) - { - CopyCommandLists.Enqueue(commandList); - } - } - internal bool IsFenceComplete(Fence fence, long fenceValue) { return fenceValue <= fence.CompletedValue; diff --git a/src/DirectX12GameEngine.Graphics/Texture.cs b/src/DirectX12GameEngine.Graphics/Texture.cs index 0f36367e..3879ce41 100644 --- a/src/DirectX12GameEngine.Graphics/Texture.cs +++ b/src/DirectX12GameEngine.Graphics/Texture.cs @@ -65,12 +65,10 @@ namespace DirectX12GameEngine.Graphics textureUploadBuffer.NativeResource.WriteToSubresource(0, null, (IntPtr)pointer, ((Format)Description.Format).SizeOfInBytes() * Width, data.Length * Unsafe.SizeOf()); } - CommandList copyCommandList = GraphicsDevice.GetOrCreateCopyCommandList(); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); copyCommandList.CopyResource(textureUploadBuffer, this); copyCommandList.Flush(true); - - GraphicsDevice.EnqueueCopyCommandList(copyCommandList); } public Texture InitializeFrom(TextureDescription description)