From fdf9c07e373cc710a296e0a2b43120ec774db743 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 14 Nov 2020 22:55:01 +0100 Subject: [PATCH] Added TerraFX Buffer type --- .../Buffers/Abstract/GraphicsResource2.cs | 4 +- .../Buffers/Buffer2{T}.cs | 154 ++++++++++++++++++ .../Commands/CommandAllocatorPool2.cs | 2 +- .../Extensions/ID3D12DeviceExtensions.cs | 55 ++++++- src/ComputeSharp.Graphics/GraphicsDevice2.cs | 2 +- 5 files changed, 208 insertions(+), 9 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource2.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource2.cs index 593dcc5c..0c1b727c 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource2.cs @@ -20,7 +20,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// Creates a new instance with the specified parameters. /// /// The associated with the current instance. - protected GraphicsResource2(GraphicsDevice device, ID3D12Resource* d3d12resource) + protected GraphicsResource2(GraphicsDevice2 device, ID3D12Resource* d3d12resource) { D3D12Resource = d3d12resource; @@ -30,7 +30,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// Gets the associated with the current instance. /// - public GraphicsDevice GraphicsDevice { get; } + public GraphicsDevice2 GraphicsDevice { get; } /// /// Maps the current resource to a specified subresource. diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs new file mode 100644 index 00000000..907be209 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs @@ -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 +{ + /// + /// A representing a typed buffer stored on GPU memory. + /// + /// The type of items stored on the buffer. + public unsafe class Buffer2 : GraphicsResource2 + where T : unmanaged + { + /// + /// The instance for the current resource. + /// + protected internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + + /// + /// The instance for the current resource. + /// + protected internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + + /// + /// The size in bytes of the current buffer. + /// + protected readonly int SizeInBytes; + + /// + /// The size in bytes of each value contained in the buffer. + /// + protected readonly int ElementSizeInBytes; + + /// + /// The size in bytes of the current buffer. + /// + internal readonly int PaddedElementSizeInBytes; + + /// + /// The buffer type for the current instance. + /// + internal readonly BufferType BufferType; + + /// + /// 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 buffer type for the current buffer + internal Buffer2(GraphicsDevice2 device, int size, int sizeInBytes, BufferType bufferType) + : base(device, CreateCommittedResource(device.D3D12Device, bufferType, sizeInBytes)) + { + Size = size; + SizeInBytes = sizeInBytes; + ElementSizeInBytes = Unsafe.SizeOf(); + 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; + } + } + } + + /// + /// Gets the size of the current buffer, as in the number of values it contains + /// + public int Size { get; } + + /// + /// Gets whether or not there is some padding between elements in the current buffer + /// + internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes; + + /// + /// Creates a committed resource for a given buffer type. + /// + /// The instance in use. + /// The buffer type currently in use. + /// The size in bytes of the current buffer. + /// An reference for the current buffer. + 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); + } + + /// + /// Creates a view for a constant buffer. + /// + 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); + } + + /// + /// Creates a view for a readonly buffer. + /// + 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); + } + + /// + /// Creates a view for a buffer than be both read and written to. + /// + 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); + } + } +} diff --git a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs index 2798dd83..13243008 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Graphics.Commands /// /// A type that acts as a pool to get new instances wheen needed. /// - internal unsafe struct CommandAllocatorPool2 + internal readonly unsafe struct CommandAllocatorPool2 { /// /// The command list type being used by the current instance. diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index c852d8d1..6fa698c7 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -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; } + /// + /// Creates a new for a given device. + /// + /// The target to use to create the descriptor heap. + /// The number of descriptors to allocate. + /// A pointer to the newly allocated instance. + /// Thrown when the creation of the command queue fails. + 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; + } + /// /// Checks the feature support of a given type for a given device. /// diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs index 87107db4..ca3010f2 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice2.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs @@ -48,7 +48,7 @@ namespace ComputeSharp.Graphics /// /// The instance to use when allocating new buffers. /// - internal readonly DescriptorAllocator2 ShaderResourceViewDescriptorAllocator; + internal DescriptorAllocator2 ShaderResourceViewDescriptorAllocator; /// /// The next fence value for compute operations using .