Added TerraFX Buffer<T> type

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-14 22:55:01 +01:00
1 親 d4752ef1b0
コミット fdf9c07e37
5ファイル変更
+208 -9

検索条件に一致するファイルはありません

+2 -2
ファイルの表示
@@ -20,7 +20,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// Creates a new <see cref="GraphicsResource"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
protected GraphicsResource2(GraphicsDevice device, ID3D12Resource* d3d12resource)
protected GraphicsResource2(GraphicsDevice2 device, ID3D12Resource* d3d12resource)
{
D3D12Resource = d3d12resource;
@@ -30,7 +30,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <summary>
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
/// </summary>
public GraphicsDevice GraphicsDevice { get; }
public GraphicsDevice2 GraphicsDevice { get; }
/// <summary>
/// Maps the current resource to a specified subresource.
+154
ファイルの表示
@@ -0,0 +1,154 @@
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Extensions;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_HEAP_TYPE;
using static TerraFX.Interop.D3D12_RESOURCE_FLAGS;
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
namespace ComputeSharp.Graphics.Buffers
{
/// <summary>
/// A <see langword="class"/> representing a typed buffer stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
public unsafe class Buffer2<T> : GraphicsResource2
where T : unmanaged
{
/// <summary>
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
protected internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
protected internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <summary>
/// The size in bytes of the current buffer.
/// </summary>
protected readonly int SizeInBytes;
/// <summary>
/// The size in bytes of each <typeparamref name="T"/> value contained in the buffer.
/// </summary>
protected readonly int ElementSizeInBytes;
/// <summary>
/// The size in bytes of the current buffer.
/// </summary>
internal readonly int PaddedElementSizeInBytes;
/// <summary>
/// The buffer type for the current <see cref="Buffer{T}"/> instance.
/// </summary>
internal readonly BufferType BufferType;
/// <summary>
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
/// <param name="size">The number of items to store in the current buffer</param>
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
/// <param name="bufferType">The buffer type for the current buffer</param>
internal Buffer2(GraphicsDevice2 device, int size, int sizeInBytes, BufferType bufferType)
: base(device, CreateCommittedResource(device.D3D12Device, bufferType, sizeInBytes))
{
Size = size;
SizeInBytes = sizeInBytes;
ElementSizeInBytes = Unsafe.SizeOf<T>();
BufferType = bufferType;
PaddedElementSizeInBytes = sizeInBytes / size;
if (bufferType is BufferType.Constant or BufferType.ReadOnly or BufferType.ReadWrite)
{
GraphicsDevice.ShaderResourceViewDescriptorAllocator.Allocate(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (bufferType)
{
case BufferType.Constant: CreateConstantBufferView(); break;
case BufferType.ReadOnly: CreateShaderResourceView(); break;
case BufferType.ReadWrite: CreateUnorderedAccessView(); break;
}
}
}
/// <summary>
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
/// </summary>
public int Size { get; }
/// <summary>
/// Gets whether or not there is some padding between elements in the current buffer
/// </summary>
internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes;
/// <summary>
/// Creates a committed resource for a given buffer type.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="bufferType">The buffer type currently in use.</param>
/// <param name="sizeInBytes">The size in bytes of the current buffer.</param>
/// <returns>An <see cref="ID3D12Resource"/> reference for the current buffer.</returns>
private static ID3D12Resource* CreateCommittedResource(ID3D12Device* d3D12Device, BufferType bufferType, int sizeInBytes)
{
(D3D12_HEAP_TYPE d3D12HeapType,
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
D3D12_RESOURCE_STATES d3D12ResourceStates) = bufferType switch
{
BufferType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
BufferType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
BufferType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
BufferType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
BufferType.Transfer => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
_ => throw null!
};
return d3D12Device->CreateCommittedResource(d3D12HeapType, (uint)sizeInBytes, d3D12ResourceFlags, d3D12ResourceStates);
}
/// <summary>
/// Creates a view for a constant buffer.
/// </summary>
private void CreateConstantBufferView()
{
uint constantBufferSize = (uint)((SizeInBytes + 255) & ~255);
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
d3D12ConstantBufferViewDescription.BufferLocation = D3D12Resource->GetGPUVirtualAddress();
d3D12ConstantBufferViewDescription.SizeInBytes = constantBufferSize;
GraphicsDevice.D3D12Device->CreateConstantBufferView(&d3D12ConstantBufferViewDescription, D3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a view for a readonly buffer.
/// </summary>
private void CreateShaderResourceView()
{
D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default;
d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
d3D12ShaderResourceViewDescription.Shader4ComponentMapping = 5768;
d3D12ShaderResourceViewDescription.Buffer.NumElements = (uint)Size;
d3D12ShaderResourceViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
GraphicsDevice.D3D12Device->CreateShaderResourceView(D3D12Resource, &d3D12ShaderResourceViewDescription, D3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a view for a buffer than be both read and written to.
/// </summary>
private void CreateUnorderedAccessView()
{
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION.D3D12_UAV_DIMENSION_BUFFER;
d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Size;
d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes;
GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle);
}
}
}
+1 -1
ファイルの表示
@@ -10,7 +10,7 @@ namespace ComputeSharp.Graphics.Commands
/// <summary>
/// A type that acts as a pool to get new <see cref="ID3D12CommandAllocator"/> instances wheen needed.
/// </summary>
internal unsafe struct CommandAllocatorPool2
internal readonly unsafe struct CommandAllocatorPool2
{
/// <summary>
/// The command list type being used by the current instance.
+50 -5
ファイルの表示
@@ -2,6 +2,13 @@
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS;
using static TerraFX.Interop.D3D12_CPU_PAGE_PROPERTY;
using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_FLAGS;
using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE;
using static TerraFX.Interop.D3D12_FENCE_FLAGS;
using static TerraFX.Interop.D3D12_MEMORY_POOL;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Extensions
@@ -21,9 +28,9 @@ namespace ComputeSharp.Graphics.Extensions
public static ID3D12CommandQueue* CreateCommandQueue(this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE type)
{
D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc;
d3d12CommandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE;
d3d12CommandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE;
d3d12CommandQueueDesc.Priority = 0;
d3d12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE;
d3d12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
d3d12CommandQueueDesc.NodeMask = 0;
Guid d3d12CommandQueueDescGuid = FX.IID_ID3D12CommandQueue;
ID3D12CommandQueue* commandQueue;
@@ -51,7 +58,7 @@ namespace ComputeSharp.Graphics.Extensions
int result = d3d12device.CreateFence(
0,
D3D12_FENCE_FLAGS.D3D12_FENCE_FLAG_NONE,
D3D12_FENCE_FLAG_NONE,
&d3D12FenceGuid,
(void**)&d3d12Fence);
@@ -70,9 +77,9 @@ namespace ComputeSharp.Graphics.Extensions
public static ID3D12DescriptorHeap* CreateDescriptorHeap(this ref ID3D12Device d3d12device, uint descriptorsCount)
{
D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc;
d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
d3d12DescriptorHeapDesc.NumDescriptors = descriptorsCount;
d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
d3d12DescriptorHeapDesc.NodeMask = 0;
Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap;
ID3D12DescriptorHeap* d3d12DescriptorHeap;
@@ -87,6 +94,44 @@ namespace ComputeSharp.Graphics.Extensions
return d3d12DescriptorHeap;
}
/// <summary>
/// Creates a new <see cref="ID3D12DescriptorHeap"/> for a given device.
/// </summary>
/// <param name="d3d12device">The target <see cref="ID3D12Device"/> to use to create the descriptor heap.</param>
/// <param name="descriptorsCount">The number of descriptors to allocate.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12DescriptorHeap"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12Resource* CreateCommittedResource(
this ref ID3D12Device d3d12device,
D3D12_HEAP_TYPE d3D12HeapType,
ulong width,
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
d3D12HeapProperties.Type = d3D12HeapType;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(width, d3D12ResourceFlags);
Guid d3D12ResourceGuid = FX.IID_ID3D12Resource;
ID3D12Resource* d3D12Resource;
int result = d3d12device.CreateCommittedResource(
&d3D12HeapProperties,
D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
&d3D12ResourceGuid,
(void**)&d3D12Resource);
if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result);
return d3D12Resource;
}
/// <summary>
/// Checks the feature support of a given type for a given device.
/// </summary>
+1 -1
ファイルの表示
@@ -48,7 +48,7 @@ namespace ComputeSharp.Graphics
/// <summary>
/// The <see cref="DescriptorAllocator2"/> instance to use when allocating new buffers.
/// </summary>
internal readonly DescriptorAllocator2 ShaderResourceViewDescriptorAllocator;
internal DescriptorAllocator2 ShaderResourceViewDescriptorAllocator;
/// <summary>
/// The next fence value for compute operations using <see cref="D3D12ComputeCommandQueue"/>.