From 13ee3e942209d4c23f23a68cd8bdf4b4c3fd01d5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:48:35 +0200 Subject: [PATCH] Added specific writeable and readonly buffer types --- src/ComputeSharp.Graphics/Buffers/Buffer.cs | 15 ++-------- .../Buffers/BufferExtensions.cs | 18 +++++------ .../Buffers/ReadOnlyBuffer{T}.cs | 26 ++++++++++++++++ .../Buffers/ReadWriteBuffer{T}.cs | 30 +++++++++++++++++++ .../Mappings/HlslKnownTypes.cs | 10 +++---- src/ComputeSharp.Shaders/ShaderLoader.cs | 4 +-- src/DirectX12ComputeShaderSample/Program.cs | 2 +- 7 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs index 1cbc6ad9..703bc324 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -11,7 +11,7 @@ namespace ComputeSharp.Graphics.Buffers /// A representing a typed buffer stored on GPU memory /// /// The type of items stored on the buffer - public sealed class Buffer : GraphicsResource where T : unmanaged + public class Buffer : GraphicsResource where T : unmanaged { /// /// Creates a new instance with the specified parameters @@ -19,7 +19,7 @@ namespace ComputeSharp.Graphics.Buffers /// The associated with the current instance /// The number of items to store in the current buffer /// The heap type for the current buffer - internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device) + protected internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device) { Size = size; ElementSizeInBytes = Unsafe.SizeOf(); @@ -108,17 +108,6 @@ namespace ComputeSharp.Graphics.Buffers /// public HeapType HeapType { get; } - /// - /// Gets or sets a single value from the current buffer - /// - /// The index of the value to get or set - /// This API can only be used from a compute shader, and will always throw if used anywhere else - public T this[uint i] - { - get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); - set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); - } - /// /// Reads the contents of the current instance and returns an array /// diff --git a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs index e424961e..ae6e41c5 100644 --- a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs @@ -16,12 +16,12 @@ namespace ComputeSharp.Graphics.Buffers /// The type of items to store in the buffer /// The instance to use to allocate the buffer /// The size of the buffer to allocate - /// A zeroed read write instance of size + /// A zeroed instance of size [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged { - return new Buffer(device, size, HeapType.Default); + return new ReadWriteBuffer(device, size); } /// @@ -30,12 +30,12 @@ namespace ComputeSharp.Graphics.Buffers /// The type of items to store in the buffer /// The instance to use to allocate the buffer /// The input with the data to copy on the allocated buffer - /// A read write instance with the contents of the input + /// A read write instance with the contents of the input [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged { - Buffer buffer = new Buffer(device, span.Length, HeapType.Default); + ReadWriteBuffer buffer = new ReadWriteBuffer(device, span.Length); buffer.SetData(span); return buffer; @@ -47,12 +47,12 @@ namespace ComputeSharp.Graphics.Buffers /// The type of items to store in the buffer /// The instance to use to allocate the buffer /// The input with the data to copy on the allocated buffer - /// A constant instance with the contents of the input + /// A constant instance with the contents of the input [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged + public static ReadOnlyBuffer AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged { - Buffer buffer = new Buffer(device, span.Length, HeapType.Upload); + ReadOnlyBuffer buffer = new ReadOnlyBuffer(device, span.Length); buffer.SetData(span); return buffer; diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs new file mode 100644 index 00000000..c86d4084 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -0,0 +1,26 @@ +using System; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadOnlyBuffer : Buffer 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 + internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Upload) { } + + /// + /// Gets a single value from the current read write buffer + /// + /// The index of the value to get + /// This API can only be used from a compute shader, and will always throw if used anywhere else + public T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs new file mode 100644 index 00000000..ded6d1e9 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -0,0 +1,30 @@ +using System; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadWriteBuffer : Buffer 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 + internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Default) { } + + /// + /// Gets or sets a single value from the current read write buffer + /// + /// The index of the value to get or set + /// This API can only be used from a compute shader, and will always throw if used anywhere else + public T this[uint i] + { + get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + } + } +} diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index e1847415..32bf790d 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -27,7 +27,7 @@ namespace ComputeSharp.Shaders.Mappings [typeof(Vector4).FullName] = "float4", [typeof(Matrix4x4).FullName] = "float4x4", [typeof(ThreadIds).FullName] = "uint3", - [typeof(Buffer<>).FullName] = "RWBuffer" + [typeof(ReadWriteBuffer<>).FullName] = "RWBuffer" }; /// @@ -54,13 +54,13 @@ namespace ComputeSharp.Shaders.Mappings type == typeof(Matrix4x4); /// - /// Checks whether or not the input type is a value + /// Checks whether or not the input type is a value /// /// The input instance to check - /// A indicating whether the input is in fact a instance + /// A indicating whether the input is in fact a instance [Pure] - public static bool IsBufferType(Type type) => type.IsGenericType && - type.GetGenericTypeDefinition() == typeof(Buffer<>); + public static bool IsReadWriteBufferType(Type type) => type.IsGenericType && + type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>); /// /// Checks whether or not a given is an HLSL known type diff --git a/src/ComputeSharp.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs index a0f362db..aabd5bb8 100644 --- a/src/ComputeSharp.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/ShaderLoader.cs @@ -4,7 +4,6 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; -using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models.Fields; @@ -116,8 +115,7 @@ namespace ComputeSharp.Shaders FieldInfoBase processedFieldInfo; // Read write buffer - if (HlslKnownTypes.IsBufferType(fieldType) && - (HeapType)fieldType.GetProperty(nameof(Buffer.HeapType)).GetValue(fieldValue) == HeapType.Default) + if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) { // Root parameter for a read write buffer DescriptorRange range = new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount); diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 78958d04..ed6ba932 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -18,7 +18,7 @@ namespace DirectX12ComputeShaderSample array[i] = i + 1; } - using Buffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); + using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); // Shader body Action action = id =>