From 4fefd50cd2ee68eb8fd16cb8ba67ff6d8f1ee1b3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Aug 2019 13:43:56 +0200 Subject: [PATCH 1/3] Added GraphicsResourceExtensions class --- .../Extensions/GraphicsResourceExtensions.cs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs new file mode 100644 index 00000000..842ba39c --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Reflection; +using ComputeSharp.Graphics.Buffers.Abstract; + +namespace ComputeSharp.Graphics.Buffers.Extensions +{ + /// + /// A with extension methods for the type to allocate generic resources + /// + internal static class GraphicsResourceExtensions + { + /// + /// Allocates a new constant buffer with the specified generic value + /// + /// The instance to use to allocate the buffer + /// The input to copy to the allocated buffer + /// A constant instance (as a ) with the input data + [Pure] + public static GraphicsResource AllocateReadOnlyBufferFromReflectedSingleValue(this GraphicsDevice device, object data) + { + // Create the generic constant buffer + Type + dataType = data.GetType(), + bufferType = typeof(ReadOnlyBuffer<>).MakeGenericType(dataType); + ConstructorInfo constructor = bufferType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First(); + GraphicsResource buffer = (GraphicsResource)constructor.Invoke(new object[] { device, 1 }); + + // Create the array with the input value + Type arrayType = dataType.MakeArrayType(); + ConstructorInfo[] arrayConstructors = arrayType.GetConstructors(BindingFlags.CreateInstance + | BindingFlags.Public + | BindingFlags.Instance + | BindingFlags.OptionalParamBinding); + Array array = (Array)arrayConstructors[0].Invoke(new object[] { 1 }); + + // Use the IList indexer to set the generic object + IList ilist = array; + ilist[0] = data; + + // Set the input data to the new buffer + MethodInfo setDataMethod = bufferType.GetMethod(nameof(HlslBuffer.SetData), new[] { arrayType }); + setDataMethod.Invoke(buffer, new object[] { array }); + + return buffer; + } + } +} From d5a80657d8f2e1f69666f0ce2ea8e0e65da3cf66 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Aug 2019 14:44:48 +0200 Subject: [PATCH 2/3] Added tests for new reflected buffer API --- .../Properties/AssemblyInfo.cs | 1 + .../ComputeSharp.NetCore.Tests/BufferTests.cs | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs b/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs index 8fb2fba6..2ccbb996 100644 --- a/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs +++ b/src/ComputeSharp.Graphics/Properties/AssemblyInfo.cs @@ -1,3 +1,4 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("ComputeSharp.Shaders")] +[assembly: InternalsVisibleTo("ComputeSharp.NetCore.Tests")] diff --git a/tests/ComputeSharp.NetCore.Tests/BufferTests.cs b/tests/ComputeSharp.NetCore.Tests/BufferTests.cs index 5040ea07..e2d4c621 100644 --- a/tests/ComputeSharp.NetCore.Tests/BufferTests.cs +++ b/tests/ComputeSharp.NetCore.Tests/BufferTests.cs @@ -1,6 +1,8 @@ using System; using System.Linq; using ComputeSharp.Graphics.Buffers; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Extensions; using ComputeSharp.NetCore.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -10,6 +12,30 @@ namespace ComputeSharp.NetCore.Tests [TestCategory("Buffers")] public class BufferTests { + [TestMethod] + public void ReadWriteBufferGetSetDataFromSingleValueInt() + { + object value = 77; + + using ReadOnlyBuffer buffer = (ReadOnlyBuffer)Gpu.Default.AllocateReadOnlyBufferFromReflectedSingleValue(value); + int[] result = buffer.GetData(); + + Assert.IsTrue(result.Length == 1); + Assert.IsTrue((int)value == result[0]); + } + + [TestMethod] + public void ReadWriteBufferGetSetDataFromSingleValueFloat() + { + object value = 3.14f; + + using ReadOnlyBuffer buffer = (ReadOnlyBuffer)Gpu.Default.AllocateReadOnlyBufferFromReflectedSingleValue(value); + float[] result = buffer.GetData(); + + Assert.IsTrue(result.Length == 1); + Assert.IsTrue(MathF.Abs(result[0] - (float)value) < 0.0001f); + } + [TestMethod] public void ReadWriteBufferGetSetDataFromArray() { From 34b0a59fc3351b952e9291664517e6540877e8e7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Aug 2019 15:06:37 +0200 Subject: [PATCH 3/3] Implemented automatic allocation for captured variables --- src/ComputeSharp.Shaders/ShaderRunner.cs | 19 +++++++++++++++- .../Translation/ShaderLoader.cs | 22 +++++++++++++++---- .../ShaderClosureTests.cs | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index dd91a4e4..b59b6ffe 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -1,6 +1,8 @@ using System; -using System.Linq; +using System.Collections.Generic; using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Extensions; using ComputeSharp.Shaders.Renderer; using ComputeSharp.Shaders.Renderer.Models; using ComputeSharp.Shaders.Translation; @@ -70,9 +72,24 @@ namespace ComputeSharp.Shaders commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource); } + // Load the captured variables + List buffers = new List(); + foreach (var variable in shaderLoader.CapturedConstantBufferValues) + { + GraphicsResource resource = device.AllocateReadOnlyBufferFromReflectedSingleValue(variable.Value); + commandList.SetComputeRootDescriptorTable(variable.Index, resource); + buffers.Add(resource); + } + // Dispatch and wait for completion commandList.Dispatch(groupsX, groupsY, groupsZ); commandList.Flush(); + + // Free the allocated resources + foreach (GraphicsResource resource in buffers) + { + resource.Dispose(); + } } } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 797d21fb..26bcf29a 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -63,6 +63,11 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList<(int Index, GraphicsResource Resource)> Buffers { get; private set; } + /// + /// Gets the ordered collection of captured variables that need to be assigned to constant buffers + /// + public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues { get; private set; } + private readonly List _FieldsInfo = new List(); /// @@ -107,6 +112,7 @@ namespace ComputeSharp.Shaders.Translation List descriptorRanges = new List(); List<(int, GraphicsResource)> buffers = new List<(int, GraphicsResource)>(); + List<(int, object)> variables = new List<(int, object)>(); int readWriteBuffersCount = 0; int readOnlyBuffersCount = 0; @@ -125,7 +131,7 @@ namespace ComputeSharp.Shaders.Translation descriptorRanges.Add(range); // Reference to the underlying buffer - buffers.Add((readWriteBuffersCount, (GraphicsResource)fieldValue)); + buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType); processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++); @@ -137,16 +143,23 @@ namespace ComputeSharp.Shaders.Translation descriptorRanges.Add(range); // Reference to the underlying buffer - buffers.Add((readOnlyBuffersCount, (GraphicsResource)fieldValue)); + buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++, false); } else if (HlslKnownTypes.IsKnownScalarType(fieldType)) { - // Static scalar field + // Read only buffer + DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, readOnlyBuffersCount); + descriptorRanges.Add(range); + + // Register the captured value + variables.Add((descriptorRanges.Count - 1, fieldValue)); + + // Constant buffer field string typeName = HlslKnownTypes.GetMappedName(fieldType); - processedFieldInfo = new StaticScalarFieldInfo(typeName, fieldName, fieldValue); + processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++, true); } else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); @@ -155,6 +168,7 @@ namespace ComputeSharp.Shaders.Translation RootParameters = descriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray(); Buffers = buffers; + CapturedConstantBufferValues = variables; } /// diff --git a/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs index 18bad3b9..abda8eb8 100644 --- a/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs +++ b/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs @@ -23,7 +23,7 @@ namespace ComputeSharp.NetCore.Tests float[] result = buffer.GetData(); - Assert.IsTrue((int)result[0] == (int)value); + Assert.IsTrue(MathF.Abs(result[0] - value) < 0.0001f); } [TestMethod]