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 class Buffer : 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 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)
{
Size = size;
ElementSizeInBytes = Unsafe.SizeOf();
SizeInBytes = sizeInBytes; // Not necessarily a multiple of the element size, as there could be padding
HeapType = heapType;
ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None;
ResourceDescription description = ResourceDescription.Buffer(SizeInBytes, flags);
ResourceStates resourceStates = heapType switch
{
HeapType.Upload => ResourceStates.GenericRead,
HeapType.Readback => ResourceStates.CopyDestination,
_ => ResourceStates.Common
};
NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates);
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch
{
HeapType.Default => CreateUnorderedAccessView(),
HeapType.Upload => CreateConstantBufferView(),
_ => default
};
}
///
/// 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
///
protected int SizeInBytes { get; }
///
/// Gets the size in bytes of each value contained in the buffer
///
protected int ElementSizeInBytes { get; }
///
/// 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 heap type being targeted by the current buffer
///
public HeapType HeapType { get; }
}
}