From b7e05ec85bad13df76a218d8e87499f77464bef8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 11:47:26 +0200 Subject: [PATCH] Added initial Buffer2 implementation --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 109 +++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Buffers/Buffer2.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs new file mode 100644 index 00000000..df75a923 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -0,0 +1,109 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Buffers.Abstract; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class Buffer2 : GraphicsResource where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + /// The heap type for the current buffer + public Buffer2(GraphicsDevice device, int size, HeapType heapType) : base(device) + { + Size = size; + ElementSizeInBytes = Unsafe.SizeOf(); + SizeInBytes = Size * ElementSizeInBytes; + HeapType = heapType; + + ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None; + ResourceDescription description = ResourceDescription.Buffer(ElementSizeInBytes, flags); + ResourceStates resourceStates = heapType switch + { + HeapType.Upload => ResourceStates.GenericRead, + HeapType.Readback => ResourceStates.CopyDestination, + _ => ResourceStates.CopyDestination + }; + + NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates); + + (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch + { + HeapType.Default => CreateUnorderedAccessView(), + HeapType.Upload => CreateConstantBufferView(), + _ => default + }; + } + + /// + /// Creates the descriptors for a constant buffer + /// + /// The CPU and GPU handles for a constant buffer + [Pure] + private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView() + { + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + + int constantBufferSize = (SizeInBytes + 255) & ~255; + + ConstantBufferViewDescription description = new ConstantBufferViewDescription + { + BufferLocation = NativeResource!.GPUVirtualAddress, + SizeInBytes = constantBufferSize + }; + + GraphicsDevice.NativeDevice.CreateConstantBufferView(description, cpuHandle); + + return (cpuHandle, gpuHandle); + } + + /// + /// Creates the descriptors for a read write buffer + /// + /// The CPU and GPU handles for a read write buffer + [Pure] + private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView() + { + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + + UnorderedAccessViewDescription description = new UnorderedAccessViewDescription + { + Format = SharpDX.DXGI.Format.R32_Float, + Dimension = UnorderedAccessViewDimension.Buffer, + Buffer = { ElementCount = Size } + }; + + GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle); + + return (cpuHandle, gpuHandle); + } + + /// + /// Gets the size of the current buffer, as in the number of values it contains + /// + public int Size { get; } + + /// + /// Gets the size in bytes of the current buffer + /// + public int SizeInBytes { get; } + + /// + /// Gets the size in bytes of each value contained in the buffer + /// + public int ElementSizeInBytes { get; } + + /// + /// Gets the heap type being targeted by the current buffer + /// + public HeapType HeapType { get; } + } +}