diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs index c344e728..4d0b8b2e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs @@ -1,6 +1,7 @@ using System; using System.Buffers; using System.Diagnostics.Contracts; +using ComputeSharp.Graphics.Buffers.Enums; using SharpDX.Direct3D12; namespace ComputeSharp.Graphics.Buffers.Abstract @@ -17,8 +18,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The associated with the current instance /// The number of items to store in the current buffer /// The size in bytes for the current buffer - /// The heap type for the current buffer - protected internal HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, HeapType heapType) : base(device, size, sizeInBytes, heapType) { } + /// The buffer type for the current buffer + private protected HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) : base(device, size, sizeInBytes, bufferType) { } /// /// Reads the contents of the current instance and returns an array diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs index 2891999a..2be4b4b0 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -1,6 +1,8 @@ -using System.Diagnostics.Contracts; +using System; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; using SharpDX.Direct3D12; namespace ComputeSharp.Graphics.Buffers @@ -17,29 +19,35 @@ namespace ComputeSharp.Graphics.Buffers /// The associated with the current instance /// The number of items to store in the current buffer /// The size in bytes for the current buffer - /// The heap type for the current buffer - protected internal Buffer(GraphicsDevice device, int size, int sizeInBytes, HeapType heapType) : base(device) + /// The buffer type for the current buffer + internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) : base(device) { Size = size; SizeInBytes = sizeInBytes; // Not necessarily a multiple of the element size, as there could be padding ElementSizeInBytes = Unsafe.SizeOf(); - HeapType = heapType; + BufferType = bufferType; - ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None; - ResourceDescription description = ResourceDescription.Buffer(SizeInBytes, flags); - ResourceStates resourceStates = heapType switch + // Determine the right heap type and flags + (HeapType heapType, ResourceFlags flags, ResourceStates states) = bufferType switch { - HeapType.Upload => ResourceStates.GenericRead, - HeapType.Readback => ResourceStates.CopyDestination, - _ => ResourceStates.Common + BufferType.Constant => (HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead), + BufferType.ReadOnly => (HeapType.Default, ResourceFlags.None, ResourceStates.Common), + BufferType.ReadWrite => (HeapType.Default, ResourceFlags.AllowUnorderedAccess, ResourceStates.Common), + BufferType.ReadBack => (HeapType.Readback, ResourceFlags.None, ResourceStates.CopyDestination), + BufferType.Transfer => (HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead), + _ => throw new ArgumentException($"Invalid buffer type {bufferType}", nameof(bufferType)) }; - NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates); + // Create the native resource + ResourceDescription description = ResourceDescription.Buffer(sizeInBytes, flags); + NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, states); - (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch + // Create the resource handles, if needed + (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = bufferType switch { - HeapType.Default => CreateUnorderedAccessView(), - HeapType.Upload => CreateConstantBufferView(), + BufferType.Constant => CreateConstantBufferView(), + BufferType.ReadOnly => CreateShaderResourceView(), + BufferType.ReadWrite => CreateUnorderedAccessView(), _ => default }; } @@ -60,9 +68,9 @@ namespace ComputeSharp.Graphics.Buffers protected int ElementSizeInBytes { get; } /// - /// Gets the heap type being targeted by the current buffer + /// Gets the buffer type for the current instance /// - internal HeapType HeapType { get; } + internal BufferType BufferType { get; } /// /// Creates the descriptors for a constant buffer @@ -86,6 +94,22 @@ namespace ComputeSharp.Graphics.Buffers return (cpuHandle, gpuHandle); } + private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView() + { + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(); + + ShaderResourceViewDescription description = new ShaderResourceViewDescription + { + Shader4ComponentMapping = 5768, + Dimension = ShaderResourceViewDimension.Buffer, + Buffer = { ElementCount = Size, StructureByteStride = ElementSizeInBytes } + }; + + GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, description, cpuHandle); + + return (cpuHandle, gpuHandle); + } + /// /// Creates the descriptors for a read write buffer /// @@ -98,11 +122,7 @@ namespace ComputeSharp.Graphics.Buffers UnorderedAccessViewDescription description = new UnorderedAccessViewDescription { Dimension = UnorderedAccessViewDimension.Buffer, - Buffer = - { - ElementCount = Size, - StructureByteStride = ElementSizeInBytes - } + Buffer = { ElementCount = 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 a91e4aaf..633f0ce8 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -2,6 +2,7 @@ using System.Buffers; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Helpers; using SharpDX.Direct3D12; using CommandList = ComputeSharp.Graphics.Commands.CommandList; @@ -19,7 +20,7 @@ namespace ComputeSharp.Graphics.Buffers /// /// The associated with the current instance /// The number of items to store in the current buffer - internal ConstantBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf() / 16 + 1) * 16, HeapType.Upload) + internal ConstantBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf() / 16 + 1) * 16, BufferType.Constant) { PaddedElementSizeInBytes = SizeInBytes / Size; IsPaddingPresent = PaddedElementSizeInBytes > ElementSizeInBytes; diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index fb62c357..82dbc173 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Helpers; using SharpDX.Direct3D12; using CommandList = ComputeSharp.Graphics.Commands.CommandList; @@ -18,7 +19,7 @@ namespace ComputeSharp.Graphics.Buffers /// /// The associated with the current instance /// The number of items to store in the current buffer - internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, size * Unsafe.SizeOf(), HeapType.Default) { } + internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, size * Unsafe.SizeOf(), BufferType.ReadWrite) { } /// /// Gets or sets a single value from the current read write buffer @@ -34,7 +35,7 @@ namespace ComputeSharp.Graphics.Buffers /// public override void GetData(Span span) { - using Buffer readbackBaffer = new Buffer(GraphicsDevice, Size, SizeInBytes, HeapType.Readback); + using Buffer readbackBaffer = new Buffer(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes); @@ -48,7 +49,7 @@ namespace ComputeSharp.Graphics.Buffers /// public override void SetData(Span span) { - using Buffer uploadBuffer = new Buffer(GraphicsDevice, Size, SizeInBytes, HeapType.Upload); + using Buffer uploadBuffer = new Buffer(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer); uploadBuffer.Map(0); MemoryHelper.Copy(span, uploadBuffer.MappedResource);