diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs index 1751bb9b..1e70dc88 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs @@ -1,28 +1,108 @@ using System; using System.Buffers; using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using ComputeSharp.Core.Interop; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Extensions; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_SRV_DIMENSION; +using static TerraFX.Interop.D3D12_UAV_DIMENSION; +using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics.Buffers.Abstract { /// - /// A representing a typed buffer stored on GPU memory, that can be used from an HLSL shader + /// A representing a typed buffer stored on GPU memory. /// - /// The type of items stored on the buffer - public abstract class HlslBuffer : Buffer where T : unmanaged + /// The type of items stored on the buffer. + public unsafe abstract class HlslBuffer : NativeObject + where T : unmanaged { /// - /// Creates a new instance with the specified parameters + /// The instance currently mapped. /// - /// 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 - private protected HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) - : base(device, size, sizeInBytes, bufferType) + private ComPtr d3D12Resource; + + /// + /// 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 HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) { + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, sizeInBytes); + + Size = size; + SizeInBytes = sizeInBytes; + ElementSizeInBytes = Unsafe.SizeOf(); + BufferType = bufferType; + PaddedElementSizeInBytes = sizeInBytes / size; + + GraphicsDevice = device; + + GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + + switch (bufferType) + { + case BufferType.Constant: CreateConstantBufferView(); break; + case BufferType.ReadOnly: CreateShaderResourceView(); break; + case BufferType.ReadWrite: CreateUnorderedAccessView(); break; + } } + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice { get; } + + /// + /// 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; + + /// + /// Gets the instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// /// Reads the contents of the current instance and returns an array /// @@ -111,5 +191,52 @@ namespace ComputeSharp.Graphics.Buffers.Abstract ArrayPool.Shared.Return(array); } + + /// + /// 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 = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + 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_BUFFER; + d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Size; + d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes; + + GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle); + } + + /// + protected override void OnDispose() + { + this.d3D12Resource.Dispose(); + } } } diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs deleted file mode 100644 index 77aa09c8..00000000 --- a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System.Runtime.CompilerServices; -using ComputeSharp.Core.Interop; -using ComputeSharp.Graphics.Buffers.Enums; -using ComputeSharp.Graphics.Extensions; -using TerraFX.Interop; -using static TerraFX.Interop.D3D12_SRV_DIMENSION; -using static TerraFX.Interop.D3D12_UAV_DIMENSION; -using FX = TerraFX.Interop.Windows; - -namespace ComputeSharp.Graphics.Buffers -{ - /// - /// A representing a typed buffer stored on GPU memory. - /// - /// The type of items stored on the buffer. - public unsafe class Buffer : NativeObject - where T : unmanaged - { - /// - /// The instance currently mapped. - /// - private ComPtr d3D12Resource; - - /// - /// 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 Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) - { - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, sizeInBytes); - - Size = size; - SizeInBytes = sizeInBytes; - ElementSizeInBytes = Unsafe.SizeOf(); - BufferType = bufferType; - PaddedElementSizeInBytes = sizeInBytes / size; - - GraphicsDevice = device; - - GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); - - switch (bufferType) - { - case BufferType.Constant: CreateConstantBufferView(); break; - case BufferType.ReadOnly: CreateShaderResourceView(); break; - case BufferType.ReadWrite: CreateUnorderedAccessView(); break; - } - } - - /// - /// Gets the associated with the current instance. - /// - public GraphicsDevice GraphicsDevice { get; } - - /// - /// 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; - - /// - /// Gets the instance currently mapped. - /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - - /// - /// 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 = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; - 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_BUFFER; - d3D12UnorderedAccessViewDescription.Buffer.NumElements = (uint)Size; - d3D12UnorderedAccessViewDescription.Buffer.StructureByteStride = (uint)ElementSizeInBytes; - - GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle); - } - - /// - protected override void OnDispose() - { - this.d3D12Resource.Dispose(); - } - } -} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 78ab2c13..a64c7682 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -6,7 +6,7 @@ using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Graphics.Buffers; +using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Translation.Models; using TerraFX.Interop; @@ -97,7 +97,7 @@ namespace ComputeSharp.Shaders.Translation // Access Buffer.D3D12GpuDescriptorHandle FieldInfo gpuDescriptorInfo = member.MemberType.GetField( - nameof(Buffer.D3D12GpuDescriptorHandle), + nameof(HlslBuffer.D3D12GpuDescriptorHandle), BindingFlags.NonPublic | BindingFlags.Instance)!; il.EmitReadMember(gpuDescriptorInfo);