From de4731bd90d094731ddb4cbeaec2536ef9d6eb76 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 16 Aug 2019 23:45:30 +0200 Subject: [PATCH 01/15] Code refactoring in the shader loader --- .../Translation/ShaderLoader.cs | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index feeba23f..52efa099 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -57,15 +57,19 @@ namespace ComputeSharp.Shaders.Translation /// public RootParameter[] RootParameters { get; private set; } + private readonly List<(int, GraphicsResource)> _Buffers = new List<(int, GraphicsResource)>(); + /// /// Gets the ordered collection of buffers used as fields in the current shader /// - public IReadOnlyList<(int Index, GraphicsResource Resource)> Buffers { get; private set; } + public IReadOnlyList<(int Index, GraphicsResource Resource)> Buffers => _Buffers; + + private readonly List<(int, object)> _CapturedConstantBufferValues = new List<(int, object)>(); /// /// 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; } + public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues => _CapturedConstantBufferValues; private readonly List _FieldsInfo = new List(); @@ -74,6 +78,11 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList FieldsInfo => _FieldsInfo; + /// + /// gets the number of constant buffers in the current instance + /// + public int ConstantBuffersCount { get; private set; } + /// /// Gets the name of the variable used as input for the shader method /// @@ -110,9 +119,6 @@ namespace ComputeSharp.Shaders.Translation if (ShaderFields.Any(fieldInfo => fieldInfo.IsStatic)) throw new InvalidOperationException("Empty shader body"); List descriptorRanges = new List(); - List<(int, GraphicsResource)> buffers = new List<(int, GraphicsResource)>(); - List<(int, object)> variables = new List<(int, object)>(); - int constantBuffersCount = 0; int readOnlyBuffersCount = 0; int readWriteBuffersCount = 0; @@ -126,14 +132,14 @@ namespace ComputeSharp.Shaders.Translation // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) { - DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount); + DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount); descriptorRanges.Add(range); // Reference to the underlying buffer - buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); + _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); - processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, constantBuffersCount++, false); + processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++, false); } else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) { @@ -142,7 +148,7 @@ namespace ComputeSharp.Shaders.Translation descriptorRanges.Add(range); // Reference to the underlying buffer - buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); + _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType); processedFieldInfo = new ReadOnlyBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++); @@ -154,7 +160,7 @@ namespace ComputeSharp.Shaders.Translation descriptorRanges.Add(range); // Reference to the underlying buffer - buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); + _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType); processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++); @@ -162,15 +168,15 @@ namespace ComputeSharp.Shaders.Translation else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { // Constant buffer - DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount); + DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount); descriptorRanges.Add(range); // Register the captured value - variables.Add((descriptorRanges.Count - 1, fieldValue)); + _CapturedConstantBufferValues.Add((descriptorRanges.Count - 1, fieldValue)); // Constant buffer field string typeName = HlslKnownTypes.GetMappedName(fieldType); - processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, constantBuffersCount++, true); + processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++, true); } else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); @@ -178,8 +184,6 @@ namespace ComputeSharp.Shaders.Translation } RootParameters = descriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray(); - Buffers = buffers; - CapturedConstantBufferValues = variables; } /// From ac2b962c98ae9ba3417662f6be9126cbfbcc064f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 00:23:53 +0200 Subject: [PATCH 02/15] Updated mustache template --- .../Renderer/Models/ShaderInfo.cs | 14 ++------------ .../Renderer/Templates/ShaderTemplate.mustache | 14 +++++++++++--- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 255c797a..f9e42c47 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -16,19 +16,9 @@ namespace ComputeSharp.Shaders.Renderer.Models public IReadOnlyList FieldsList { get; set; } /// - /// Gets or sets the total number of threads to run on the X axis + /// Gets or sets the index for the constant buffer with the info on the target number of iterations on the X, Y and Z axes /// - public int ThreadsX { get; set; } - - /// - /// Gets or sets the total number of threads to run on the Y axis - /// - public int ThreadsY { get; set; } - - /// - /// Gets or sets the total number of threads to run on the Z axis - /// - public int ThreadsZ { get; set; } + public int ThreadsXYZConstantBufferIndex { get; set; } /// /// Gets or sets the number of threads in the X group diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 382629c2..8e12dd05 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -20,13 +20,21 @@ cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) {{/IsReadWriteBuffer}} {{/FieldsList}} +{{!Threads XYZ targets buffer}} +cbuffer __XYZ___Buffer : register(b{{ThreadsXYZConstantBufferIndex}}) +{ + uint __x__; + uint __y__; + uint __z__; +} + {{!Shader entry point}} [Shader("compute")] [NumThreads({{NumThreadsX}}, {{NumThreadsY}}, {{NumThreadsZ}})] void CSMain(uint3 {{ThreadsIdsVariableName}} : SV_DispatchThreadId) { - if ({{ThreadsIdsVariableName}}.x < {{ThreadsX}} && - {{ThreadsIdsVariableName}}.y < {{ThreadsY}} && - {{ThreadsIdsVariableName}}.z < {{ThreadsZ}}) + if ({{ThreadsIdsVariableName}}.x < __x__ && + {{ThreadsIdsVariableName}}.y < __y__ && + {{ThreadsIdsVariableName}}.z < __z__) {{&ShaderBody}} } From c858b67c8394e1ad49773645ee6003cff072df65 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 00:25:08 +0200 Subject: [PATCH 03/15] Updated shader runner to support new bounds checking --- src/ComputeSharp.Shaders/ShaderRunner.cs | 11 ++++++++--- .../Translation/ShaderLoader.cs | 15 +++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 5acaac28..6caf6aa7 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Extensions; using ComputeSharp.Shaders.Renderer; @@ -66,9 +67,7 @@ namespace ComputeSharp.Shaders ShaderInfo shaderInfo = new ShaderInfo { FieldsList = shaderLoader.FieldsInfo, - ThreadsX = x, - ThreadsY = y, - ThreadsZ = z, + ThreadsXYZConstantBufferIndex = shaderLoader.ConstantBuffersCount, NumThreadsX = threadsX, NumThreadsY = threadsY, NumThreadsZ = threadsZ, @@ -104,6 +103,12 @@ namespace ComputeSharp.Shaders buffers.Add(resource); } + // Initialize the loop targets + Span xyzSpan = stackalloc uint[] { (uint)x, (uint)y, (uint)z }; + ConstantBuffer xyzBuffer = device.AllocateConstantBuffer(xyzSpan); + commandList.SetComputeRootDescriptorTable(shaderLoader.RootParameters.Length - 1, xyzBuffer); + buffers.Add(xyzBuffer); + // Dispatch and wait for completion commandList.Dispatch(groupsX, groupsY, groupsZ); commandList.Flush(); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 52efa099..bbd8cbf8 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -132,8 +132,7 @@ namespace ComputeSharp.Shaders.Translation // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) { - DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount); - descriptorRanges.Add(range); + descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount)); // Reference to the underlying buffer _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); @@ -144,8 +143,7 @@ namespace ComputeSharp.Shaders.Translation else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) { // Root parameter for a readonly buffer - DescriptorRange range = new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, readOnlyBuffersCount); - descriptorRanges.Add(range); + descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, readOnlyBuffersCount)); // Reference to the underlying buffer _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); @@ -156,8 +154,7 @@ namespace ComputeSharp.Shaders.Translation else if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) { // Root parameter for a read write buffer - DescriptorRange range = new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount); - descriptorRanges.Add(range); + descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount)); // Reference to the underlying buffer _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); @@ -168,8 +165,7 @@ namespace ComputeSharp.Shaders.Translation else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { // Constant buffer - DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount); - descriptorRanges.Add(range); + descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount)); // Register the captured value _CapturedConstantBufferValues.Add((descriptorRanges.Count - 1, fieldValue)); @@ -183,6 +179,9 @@ namespace ComputeSharp.Shaders.Translation _FieldsInfo.Add(processedFieldInfo); } + // Descriptor for the X, Y and Z target variables + descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount)); + RootParameters = descriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray(); } From 82479fcae9668a1994dab80af5e8213cab60e2c3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 02:02:27 +0200 Subject: [PATCH 04/15] Removed Matrix4x4 struct support --- .../Mappings/HlslKnownMethods.cs | 23 +------------------ .../Mappings/HlslKnownTypes.cs | 4 +--- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs index 143e087e..f06116e5 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs @@ -59,28 +59,7 @@ namespace ComputeSharp.Shaders.Mappings ["System.Numerics.Vector4.Transform"] = "mul", ["System.Numerics.Vector4.Normalize"] = "normalize", ["System.Numerics.Vector4.Zero"] = "(float4)0", - ["System.Numerics.Vector4.One"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)", - - // Matrix4x4 - ["System.Numerics.Matrix4x4.Multiply"] = "mul", - ["System.Numerics.Matrix4x4.Transpose"] = "transpose", - ["System.Numerics.Matrix4x4.Translation"] = "[3].xyz", - ["System.Numerics.Matrix4x4.M11"] = "[0][0]", - ["System.Numerics.Matrix4x4.M12"] = "[0][1]", - ["System.Numerics.Matrix4x4.M13"] = "[0][2]", - ["System.Numerics.Matrix4x4.M14"] = "[0][3]", - ["System.Numerics.Matrix4x4.M21"] = "[1][0]", - ["System.Numerics.Matrix4x4.M22"] = "[1][1]", - ["System.Numerics.Matrix4x4.M23"] = "[1][2]", - ["System.Numerics.Matrix4x4.M24"] = "[1][3]", - ["System.Numerics.Matrix4x4.M31"] = "[2][0]", - ["System.Numerics.Matrix4x4.M32"] = "[2][1]", - ["System.Numerics.Matrix4x4.M33"] = "[2][2]", - ["System.Numerics.Matrix4x4.M34"] = "[2][3]", - ["System.Numerics.Matrix4x4.M41"] = "[3][0]", - ["System.Numerics.Matrix4x4.M42"] = "[3][1]", - ["System.Numerics.Matrix4x4.M43"] = "[3][2]", - ["System.Numerics.Matrix4x4.M44"] = "[3][3]" + ["System.Numerics.Vector4.One"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)" }; /// diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index 56a9a633..0140ed18 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -25,7 +25,6 @@ namespace ComputeSharp.Shaders.Mappings [typeof(Vector2).FullName] = "float2", [typeof(Vector3).FullName] = "float3", [typeof(Vector4).FullName] = "float4", - [typeof(Matrix4x4).FullName] = "float4x4", [typeof(ThreadIds).FullName] = "uint3", [typeof(ReadOnlyBuffer<>).FullName] = "StructuredBuffer", [typeof(ReadWriteBuffer<>).FullName] = "RWStructuredBuffer" @@ -51,8 +50,7 @@ namespace ComputeSharp.Shaders.Mappings [Pure] public static bool IsKnownVectorType(Type type) => type == typeof(Vector2) || type == typeof(Vector3) || - type == typeof(Vector4) || - type == typeof(Matrix4x4); + type == typeof(Vector4); /// /// Checks whether or not the input type is a value From 0fa28d089f9de2249c90bbdfaaec4e1698796921 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 02:22:06 +0200 Subject: [PATCH 05/15] Added extension method to create packed buffer from values --- .../Extensions/GraphicsResourceExtensions.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs index 7ab8bb98..2c596832 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs @@ -3,7 +3,9 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; +using System.Numerics; using System.Reflection; +using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; namespace ComputeSharp.Graphics.Buffers.Extensions @@ -56,5 +58,51 @@ namespace ComputeSharp.Graphics.Buffers.Extensions return buffer; } + + /// + /// Allocates a new constant buffer with the specified generic values + /// + /// The instance to use to allocate the buffer + /// The input array to copy to the allocated buffer + /// A constant instance (as a ) with the input data + [Pure] + public static GraphicsResource AllocateConstantBufferFromReflectedValues(this GraphicsDevice device, object[] data) + { + Span temporarySpan = stackalloc Vector4[data.Length]; + ref byte r0 = ref Unsafe.As(ref temporarySpan.GetPinnableReference()); + int offset = 0; + + // Local method to write a value in the temporary buffer + void WriteValue(T value, ref byte target) where T : unmanaged + { + int size = Unsafe.SizeOf(); + if (offset % 16 > 16 - size) offset += 16 - offset % 16; + Unsafe.As(ref Unsafe.Add(ref target, offset)) = value; + offset += size; + } + + // Iterate on the input values and write them with the proper padding + for (int j = 0; j < data.Length; j++) + { + switch (data[j]) + { + case bool b: WriteValue(b, ref r0); break; + case int i: WriteValue(i, ref r0); break; + case uint u: WriteValue(u, ref r0); break; + case float f: WriteValue(f, ref r0); break; + case double d: WriteValue(d, ref r0); break; + case Vector2 v2: WriteValue(v2, ref r0); break; + case Vector3 v3: WriteValue(v3, ref r0); break; + case Vector4 v4: WriteValue(v4, ref r0); break; + default: throw new InvalidOperationException($"Invalid item of type {data[j].GetType()}"); + } + } + + // Create the array to return + ConstantBuffer buffer = new ConstantBuffer(device, data.Length); + buffer.SetData(temporarySpan); + + return buffer; + } } } From 7cc729601a50acf728e15a84f2efcecdd252c06f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:04:43 +0200 Subject: [PATCH 06/15] Fixed initialization of bool variables --- .../Buffers/Extensions/GraphicsResourceExtensions.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs index 2c596832..bdde644f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs @@ -86,7 +86,11 @@ namespace ComputeSharp.Graphics.Buffers.Extensions { switch (data[j]) { - case bool b: WriteValue(b, ref r0); break; + case bool b: + uint ub = default; + Unsafe.As(ref ub) = b; + WriteValue(ub, ref r0); + break; case int i: WriteValue(i, ref r0); break; case uint u: WriteValue(u, ref r0); break; case float f: WriteValue(f, ref r0); break; From 6a34a86aa58854e1b23c7ac2abf94b482a6223a3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:06:36 +0200 Subject: [PATCH 07/15] Fixed initialization of XYZ variables --- src/ComputeSharp.Shaders/ShaderRunner.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 6caf6aa7..d5063917 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -104,8 +104,7 @@ namespace ComputeSharp.Shaders } // Initialize the loop targets - Span xyzSpan = stackalloc uint[] { (uint)x, (uint)y, (uint)z }; - ConstantBuffer xyzBuffer = device.AllocateConstantBuffer(xyzSpan); + var xyzBuffer = device.AllocateConstantBufferFromReflectedValues(new object[] { (uint)x, (uint)y, (uint)z }); commandList.SetComputeRootDescriptorTable(shaderLoader.RootParameters.Length - 1, xyzBuffer); buffers.Add(xyzBuffer); From 508cdc3911e43683900c7ccb9b6e1b7f72ab613e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:13:56 +0200 Subject: [PATCH 08/15] Removed unnecessary static field model --- .../Models/Fields/StaticScalarFieldInfo.cs | 31 ------------------- .../Templates/ShaderTemplate.mustache | 4 --- 2 files changed, 35 deletions(-) delete mode 100644 src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs deleted file mode 100644 index 670abc20..00000000 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; - -namespace ComputeSharp.Shaders.Renderer.Models.Fields -{ - /// - /// A that contains info on a static scalar field - /// - internal sealed class StaticScalarFieldInfo : FieldInfo - { - /// - /// Gets whether or not the current instance represents a static scalar value (always ) - /// - public bool IsStaticScalarField { get; } = true; - - /// - /// Gets or sets the value to set for the current field - /// - public object FieldValue { get; set; } - - /// - /// Creates a new instance with the specified parameters - /// - /// The type of the current field - /// The name of the current field - /// The static value to assign to the current field - public StaticScalarFieldInfo(string fieldType, string fieldName, object fieldValue) : base(fieldType, fieldName) - { - FieldValue = fieldValue; - } - } -} diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 8e12dd05..735850a5 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -1,8 +1,4 @@ {{#FieldsList}} -{{!Static scalar}} -{{#IsStaticScalarField}} -static {{&FieldType}} {{FieldName}} = {{FieldValue}}; -{{/IsStaticScalarField}} {{!Constant buffer}} {{#IsConstantBuffer}} cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) From 0cc9acb923645049875384e4851a721771dbcac6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:19:58 +0200 Subject: [PATCH 09/15] Code refactoring --- .../Renderer/Models/Fields/Abstract/HlslBufferInfo.cs | 2 +- .../{Abstract/FieldInfo.cs => CapturedFieldInfo.cs} | 8 ++++---- .../Renderer/Models/Fields/ConstantBufferFieldInfo.cs | 2 +- .../Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs | 2 +- .../Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs | 2 +- src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs | 3 ++- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 9 ++++----- 7 files changed, 14 insertions(+), 14 deletions(-) rename src/ComputeSharp.Shaders/Renderer/Models/Fields/{Abstract/FieldInfo.cs => CapturedFieldInfo.cs} (73%) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs index e5c0044e..cc41114a 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs @@ -3,7 +3,7 @@ /// /// A that contains info on a shader buffer field /// - internal abstract class HlslBufferInfo : FieldInfo + internal abstract class HlslBufferInfo : CapturedFieldInfo { /// /// Gets or sets the index of the current buffer, relative to its own type diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs similarity index 73% rename from src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs index 8c60411e..a037c8b4 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs @@ -1,9 +1,9 @@ -namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a shader field /// - internal abstract class FieldInfo + internal class CapturedFieldInfo { /// /// Gets or sets the type of the current field @@ -16,11 +16,11 @@ public string FieldName { get; set; } /// - /// Creates a new instance with the specified parameters + /// Creates a new instance with the specified parameters /// /// The type of the current field /// The name of the current field - protected FieldInfo(string fieldType, string fieldName) + public CapturedFieldInfo(string fieldType, string fieldName) { FieldType = fieldType; FieldName = fieldName; diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs index ca47d290..cb87c67d 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields internal sealed class ConstantBufferFieldInfo : HlslBufferInfo { /// - /// Gets whether or not the current instance represents a constant buffer (always ) + /// Gets whether or not the current instance represents a constant buffer (always ) /// public bool IsConstantBuffer { get; } = true; diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs index a8d826ee..6bba7e0a 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields internal sealed class ReadOnlyBufferFieldInfo : HlslBufferInfo { /// - /// Gets whether or not the current instance represents a readonly buffer (always ) + /// Gets whether or not the current instance represents a readonly buffer (always ) /// public bool IsReadOnlyBuffer { get; } = true; diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs index 170260dc..5dbf8d02 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields internal sealed class ReadWriteBufferFieldInfo : HlslBufferInfo { /// - /// Gets whether or not the current instance represents a read write buffer (always ) + /// Gets whether or not the current instance represents a read write buffer (always ) /// public bool IsReadWriteBuffer { get; } = true; diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index f9e42c47..17bc18cd 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using ComputeSharp.Shaders.Renderer.Models.Fields; using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; #pragma warning disable CS8618 // Non-nullable field is uninitialized @@ -13,7 +14,7 @@ namespace ComputeSharp.Shaders.Renderer.Models /// /// Gets the list of fields being present in the current shader /// - public IReadOnlyList FieldsList { get; set; } + public IReadOnlyList FieldsList { get; set; } /// /// Gets or sets the index for the constant buffer with the info on the target number of iterations on the X, Y and Z axes diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index bbd8cbf8..a6b9fc64 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -9,7 +9,6 @@ using ComputeSharp.Shaders.Renderer.Models.Fields; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using SharpDX.Direct3D12; -using FieldInfo = ComputeSharp.Shaders.Renderer.Models.Fields.Abstract.FieldInfo; #pragma warning disable CS8618 // Non-nullable field is uninitialized @@ -71,12 +70,12 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues => _CapturedConstantBufferValues; - private readonly List _FieldsInfo = new List(); + private readonly List _FieldsInfo = new List(); /// - /// Gets the collection of items for the shader fields + /// Gets the collection of items for the shader fields /// - public IReadOnlyList FieldsInfo => _FieldsInfo; + public IReadOnlyList FieldsInfo => _FieldsInfo; /// /// gets the number of constant buffers in the current instance @@ -127,7 +126,7 @@ namespace ComputeSharp.Shaders.Translation Type fieldType = fieldInfo.FieldType; string fieldName = fieldInfo.Name; object fieldValue = fieldInfo.GetValue(ShaderInstance); - FieldInfo processedFieldInfo; + CapturedFieldInfo processedFieldInfo; // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) From 1cf603e2af839157875b2fd24d4a05c177a4dca3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:29:50 +0200 Subject: [PATCH 10/15] Separated captured buffers and variables --- .../Renderer/Models/ShaderInfo.cs | 11 ++++++-- src/ComputeSharp.Shaders/ShaderRunner.cs | 6 ++-- .../Translation/ShaderLoader.cs | 28 +++++++++---------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 17bc18cd..01d1ed99 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -12,14 +12,19 @@ namespace ComputeSharp.Shaders.Renderer.Models internal sealed class ShaderInfo { /// - /// Gets the list of fields being present in the current shader + /// Gets the list of captured buffers being present in the current shader + /// + public IReadOnlyList BuffersList { get; set; } + + /// + /// Gets the list of captured variables being present in the current shader /// public IReadOnlyList FieldsList { get; set; } /// - /// Gets or sets the index for the constant buffer with the info on the target number of iterations on the X, Y and Z axes + /// Gets or sets the index for the constant buffer with the captured scalar and vector variables /// - public int ThreadsXYZConstantBufferIndex { get; set; } + public int VariablesConstantBufferIndex { get; set; } /// /// Gets or sets the number of threads in the X group diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index d5063917..c6771b1b 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using ComputeSharp.Graphics; -using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Extensions; using ComputeSharp.Shaders.Renderer; @@ -66,8 +65,9 @@ namespace ComputeSharp.Shaders // Render the loaded shader ShaderInfo shaderInfo = new ShaderInfo { - FieldsList = shaderLoader.FieldsInfo, - ThreadsXYZConstantBufferIndex = shaderLoader.ConstantBuffersCount, + BuffersList = shaderLoader.BuffersList, + FieldsList = shaderLoader.FieldsList, + VariablesConstantBufferIndex = shaderLoader.ConstantBuffersCount, NumThreadsX = threadsX, NumThreadsY = threadsY, NumThreadsZ = threadsZ, diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index a6b9fc64..75cd3cbc 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -6,6 +6,7 @@ using System.Text.RegularExpressions; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models.Fields; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using SharpDX.Direct3D12; @@ -70,12 +71,19 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues => _CapturedConstantBufferValues; - private readonly List _FieldsInfo = new List(); + private readonly List _BuffersList = new List(); + + /// + /// Gets the collection of items for the shader fields + /// + public IReadOnlyList BuffersList => _BuffersList; + + private readonly List _FieldsList = new List(); /// /// Gets the collection of items for the shader fields /// - public IReadOnlyList FieldsInfo => _FieldsInfo; + public IReadOnlyList FieldsList => _FieldsList; /// /// gets the number of constant buffers in the current instance @@ -113,8 +121,6 @@ namespace ComputeSharp.Shaders.Translation /// private void LoadFieldsInfo() { - if (FieldsInfo.Count > 0) throw new InvalidOperationException("Shader fields already loaded"); - if (ShaderFields.Any(fieldInfo => fieldInfo.IsStatic)) throw new InvalidOperationException("Empty shader body"); List descriptorRanges = new List(); @@ -126,7 +132,6 @@ namespace ComputeSharp.Shaders.Translation Type fieldType = fieldInfo.FieldType; string fieldName = fieldInfo.Name; object fieldValue = fieldInfo.GetValue(ShaderInstance); - CapturedFieldInfo processedFieldInfo; // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) @@ -137,7 +142,7 @@ namespace ComputeSharp.Shaders.Translation _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); - processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++, false); + _BuffersList.Add(new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++, false)); } else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) { @@ -148,7 +153,7 @@ namespace ComputeSharp.Shaders.Translation _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType); - processedFieldInfo = new ReadOnlyBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++); + _BuffersList.Add(new ReadOnlyBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++)); } else if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) { @@ -159,7 +164,7 @@ namespace ComputeSharp.Shaders.Translation _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType); - processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++); + _BuffersList.Add(new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++)); } else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { @@ -171,16 +176,11 @@ namespace ComputeSharp.Shaders.Translation // Constant buffer field string typeName = HlslKnownTypes.GetMappedName(fieldType); - processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++, true); + _FieldsList.Add(new CapturedFieldInfo(typeName, fieldName)); } else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); - - _FieldsInfo.Add(processedFieldInfo); } - // Descriptor for the X, Y and Z target variables - descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount)); - RootParameters = descriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray(); } From c74e0a0a075887f1ef13537c6a15f7b112b33ec6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:31:41 +0200 Subject: [PATCH 11/15] Removed mustache code for single variable buffers --- .../Renderer/Models/Fields/ConstantBufferFieldInfo.cs | 11 +---------- .../Renderer/Templates/ShaderTemplate.mustache | 6 +++--- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 2 +- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs index cb87c67d..5dff2be0 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs @@ -12,21 +12,12 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// public bool IsConstantBuffer { get; } = true; - /// - /// Gets whether or not this constant buffer actually contains a single, explicit variable - /// - public bool IsSingleValue { get; } - /// /// Creates a new instance with the specified parameters /// /// The type of the current field /// The name of the current field /// The index of the current constant buffer field - /// Whether or not this constant buffer actually contains a single, explicit variable - public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex, bool singleValue) : base(fieldType, fieldName, bufferIndex) - { - IsSingleValue = singleValue; - } + public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { } } } diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 735850a5..1c2c1f94 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -3,7 +3,7 @@ {{#IsConstantBuffer}} cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) { - {{&FieldType}} {{FieldName}}{{^IsSingleValue}}[2]{{/IsSingleValue}}; + {{&FieldType}} {{FieldName}}[2]; } {{/IsConstantBuffer}} {{!Readonly buffer}} @@ -16,8 +16,8 @@ cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) {{/IsReadWriteBuffer}} {{/FieldsList}} -{{!Threads XYZ targets buffer}} -cbuffer __XYZ___Buffer : register(b{{ThreadsXYZConstantBufferIndex}}) +{{!Captured variables buffer}} +cbuffer __Variables__Buffer : register(b{{ThreadsXYZConstantBufferIndex}}) { uint __x__; uint __y__; diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 75cd3cbc..1cb1d820 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -142,7 +142,7 @@ namespace ComputeSharp.Shaders.Translation _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); - _BuffersList.Add(new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++, false)); + _BuffersList.Add(new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++)); } else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) { From a436486dec5b306eef5d72e2e41ef14a6490f111 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:42:16 +0200 Subject: [PATCH 12/15] Merged captured variables into single buffer --- .../Templates/ShaderTemplate.mustache | 9 +++++--- src/ComputeSharp.Shaders/ShaderRunner.cs | 22 ++++-------------- .../Translation/ShaderLoader.cs | 23 ++++++++----------- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 1c2c1f94..aa99824c 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -1,4 +1,4 @@ -{{#FieldsList}} +{{#BuffersList}} {{!Constant buffer}} {{#IsConstantBuffer}} cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) @@ -15,13 +15,16 @@ cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) {{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}}); {{/IsReadWriteBuffer}} -{{/FieldsList}} +{{/BuffersList}} {{!Captured variables buffer}} -cbuffer __Variables__Buffer : register(b{{ThreadsXYZConstantBufferIndex}}) +cbuffer __Variables__Buffer : register(b{{VariablesConstantBufferIndex}}) { uint __x__; uint __y__; uint __z__; +{{#FieldsList}} + {{&FieldType}} {{FieldName}}; +{{/FieldsList}} } {{!Shader entry point}} diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index c6771b1b..34c9830b 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Extensions; @@ -94,29 +95,14 @@ 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.AllocateConstantBufferFromReflectedSingleValue(variable.Value); - commandList.SetComputeRootDescriptorTable(variable.Index, resource); - buffers.Add(resource); - } - // Initialize the loop targets - var xyzBuffer = device.AllocateConstantBufferFromReflectedValues(new object[] { (uint)x, (uint)y, (uint)z }); - commandList.SetComputeRootDescriptorTable(shaderLoader.RootParameters.Length - 1, xyzBuffer); - buffers.Add(xyzBuffer); + IReadOnlyList variables = new object[] { (uint)x, (uint)y, (uint)z }.Concat(shaderLoader.FieldValuesList).ToArray(); + using GraphicsResource variablesBuffer = device.AllocateConstantBufferFromReflectedValues(variables); + commandList.SetComputeRootDescriptorTable(shaderLoader.RootParameters.Length - 1, variablesBuffer); // 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 1cb1d820..575e8e91 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -64,13 +64,6 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList<(int Index, GraphicsResource Resource)> Buffers => _Buffers; - private readonly List<(int, object)> _CapturedConstantBufferValues = new List<(int, object)>(); - - /// - /// Gets the ordered collection of captured variables that need to be assigned to constant buffers - /// - public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues => _CapturedConstantBufferValues; - private readonly List _BuffersList = new List(); /// @@ -85,6 +78,13 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList FieldsList => _FieldsList; + private readonly List _FieldValuesList = new List(); + + /// + /// Gets the collection of values of the captured fields for the current shader + /// + public IReadOnlyList FieldValuesList => _FieldValuesList; + /// /// gets the number of constant buffers in the current instance /// @@ -168,13 +168,8 @@ namespace ComputeSharp.Shaders.Translation } else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { - // Constant buffer - descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount)); - - // Register the captured value - _CapturedConstantBufferValues.Add((descriptorRanges.Count - 1, fieldValue)); - - // Constant buffer field + // Register the captured field + _FieldValuesList.Add(fieldValue); string typeName = HlslKnownTypes.GetMappedName(fieldType); _FieldsList.Add(new CapturedFieldInfo(typeName, fieldName)); } From d38549a92c451a367f1e291da80cde3835bffb75 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:52:24 +0200 Subject: [PATCH 13/15] Removed unnecessary buffer extension method --- .../Extensions/GraphicsResourceExtensions.cs | 57 ++----------------- 1 file changed, 5 insertions(+), 52 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs index bdde644f..d454a8f7 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs @@ -1,10 +1,7 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; -using System.Linq; using System.Numerics; -using System.Reflection; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; @@ -15,60 +12,16 @@ namespace ComputeSharp.Graphics.Buffers.Extensions /// internal static class GraphicsResourceExtensions { - /// - /// The that maps types to the necessary data to create buffers, for quick lookup - /// - private static readonly Dictionary TypeMapping = new Dictionary(); - - /// - /// 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 AllocateConstantBufferFromReflectedSingleValue(this GraphicsDevice device, object data) - { - Type dataType = data.GetType(); - if (!TypeMapping.TryGetValue(dataType, out var info)) - { - // Get the generic buffer constructor - Type bufferType = typeof(ConstantBuffer<>).MakeGenericType(dataType); - info.Constructor = bufferType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First(); - - // Create the reusable array - Type arrayType = dataType.MakeArrayType(); - ConstructorInfo[] arrayConstructors = arrayType.GetConstructors(BindingFlags.CreateInstance - | BindingFlags.Public - | BindingFlags.Instance - | BindingFlags.OptionalParamBinding); - info.Array = (IList)arrayConstructors[0].Invoke(new object[] { 1 }); - - // Set the input data to the new buffer - info.Setter = bufferType.GetMethod(nameof(HlslBuffer.SetData), new[] { arrayType }); - - // Cache for later reuse - TypeMapping.Add(dataType, info); - } - - // Create the buffer and set the content - GraphicsResource buffer = (GraphicsResource)info.Constructor.Invoke(new object[] { device, 1 }); - info.Array[0] = data; - info.Setter.Invoke(buffer, new object[] { info.Array }); - - return buffer; - } - /// /// Allocates a new constant buffer with the specified generic values /// /// The instance to use to allocate the buffer - /// The input array to copy to the allocated buffer + /// The input sequence to copy to the allocated buffer /// A constant instance (as a ) with the input data [Pure] - public static GraphicsResource AllocateConstantBufferFromReflectedValues(this GraphicsDevice device, object[] data) + public static GraphicsResource AllocateConstantBufferFromReflectedValues(this GraphicsDevice device, IReadOnlyList data) { - Span temporarySpan = stackalloc Vector4[data.Length]; + Span temporarySpan = stackalloc Vector4[data.Count]; ref byte r0 = ref Unsafe.As(ref temporarySpan.GetPinnableReference()); int offset = 0; @@ -82,7 +35,7 @@ namespace ComputeSharp.Graphics.Buffers.Extensions } // Iterate on the input values and write them with the proper padding - for (int j = 0; j < data.Length; j++) + for (int j = 0; j < data.Count; j++) { switch (data[j]) { @@ -103,7 +56,7 @@ namespace ComputeSharp.Graphics.Buffers.Extensions } // Create the array to return - ConstantBuffer buffer = new ConstantBuffer(device, data.Length); + ConstantBuffer buffer = new ConstantBuffer(device, data.Count); buffer.SetData(temporarySpan); return buffer; From f15112b85ffbcd0b534b030d50e9b98836f33e3d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 10:52:34 +0200 Subject: [PATCH 14/15] Updated internal buffer tests --- .../BufferTests.cs | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tests/ComputeSharp.NetCore.Tests.Internals/BufferTests.cs b/tests/ComputeSharp.NetCore.Tests.Internals/BufferTests.cs index cf24f066..cf67acc9 100644 --- a/tests/ComputeSharp.NetCore.Tests.Internals/BufferTests.cs +++ b/tests/ComputeSharp.NetCore.Tests.Internals/BufferTests.cs @@ -1,4 +1,6 @@ using System; +using System.Numerics; +using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -10,27 +12,23 @@ namespace ComputeSharp.NetCore.Tests.Internals public class BufferTests { [TestMethod] - public void ReadWriteBufferGetSetDataFromSingleValueInt() + public void ConstantBufferGetSetFromGenericValues() { - object value = 77; + object[] values = { 7, 3.14, Vector2.UnitX, 3.14f }; - using ConstantBuffer buffer = (ConstantBuffer)Gpu.Default.AllocateConstantBufferFromReflectedSingleValue(value); - int[] result = buffer.GetData(); + using ConstantBuffer buffer = (ConstantBuffer)Gpu.Default.AllocateConstantBufferFromReflectedValues(values); + Vector4[] data = buffer.GetData(); + ref byte r0 = ref Unsafe.As(ref data[0]); - Assert.IsTrue(result.Length == 1); - Assert.IsTrue((int)value == result[0]); - } + int i = Unsafe.As(ref Unsafe.Add(ref r0, 0)); // 0 -> 3, sizeof(int) == 4 + double d = Unsafe.As(ref Unsafe.Add(ref r0, 4)); // 4 -> 11, sizeof(double) == 8 + Vector2 v2 = Unsafe.As(ref Unsafe.Add(ref r0, 16)); // 16 -> 23, sizeof(Vector2) == 8 + float f = Unsafe.As(ref Unsafe.Add(ref r0, 024)); // 24 -> 27, sizeof(float) == 4 - [TestMethod] - public void ReadWriteBufferGetSetDataFromSingleValueFloat() - { - object value = 3.14f; - - using ConstantBuffer buffer = (ConstantBuffer)Gpu.Default.AllocateConstantBufferFromReflectedSingleValue(value); - float[] result = buffer.GetData(); - - Assert.IsTrue(result.Length == 1); - Assert.IsTrue(MathF.Abs(result[0] - (float)value) < 0.0001f); + Assert.IsTrue((int)values[0] == i); + Assert.IsTrue(Math.Abs((double)values[1] - d) < 0.0001); + Assert.IsTrue((Vector2)values[2] == v2); + Assert.IsTrue(MathF.Abs((float)values[3] - f) < 0.0001); } } } From e56bce0384cc43b1e7a29de0dbad9f6920e44f8a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 11:12:58 +0200 Subject: [PATCH 15/15] Code refactoring, moved aggregated buffer to top --- .../Renderer/Models/ShaderInfo.cs | 5 --- .../Templates/ShaderTemplate.mustache | 42 ++++++++++--------- src/ComputeSharp.Shaders/ShaderRunner.cs | 3 +- .../Translation/ShaderLoader.cs | 18 ++++---- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 01d1ed99..8dc33e8b 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -21,11 +21,6 @@ namespace ComputeSharp.Shaders.Renderer.Models /// public IReadOnlyList FieldsList { get; set; } - /// - /// Gets or sets the index for the constant buffer with the captured scalar and vector variables - /// - public int VariablesConstantBufferIndex { get; set; } - /// /// Gets or sets the number of threads in the X group /// diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index aa99824c..f0910d08 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -1,23 +1,5 @@ -{{#BuffersList}} -{{!Constant buffer}} -{{#IsConstantBuffer}} -cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) -{ - {{&FieldType}} {{FieldName}}[2]; -} -{{/IsConstantBuffer}} -{{!Readonly buffer}} -{{#IsReadOnlyBuffer}} -{{&FieldType}} {{FieldName}} : register(t{{BufferIndex}}); -{{/IsReadOnlyBuffer}} -{{!Read write buffer}} -{{#IsReadWriteBuffer}} -{{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}}); -{{/IsReadWriteBuffer}} - -{{/BuffersList}} -{{!Captured variables buffer}} -cbuffer __Variables__Buffer : register(b{{VariablesConstantBufferIndex}}) +{{!Captured variables buffer}} +cbuffer __Variables__Buffer : register(b0) { uint __x__; uint __y__; @@ -26,6 +8,26 @@ cbuffer __Variables__Buffer : register(b{{VariablesConstantBufferIndex}}) {{&FieldType}} {{FieldName}}; {{/FieldsList}} } +{{#BuffersList}} +{{#IsConstantBuffer}} + +{{!Constant buffer}} +cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) +{ + {{&FieldType}} {{FieldName}}[2]; +} +{{/IsConstantBuffer}} +{{#IsReadOnlyBuffer}} + +{{!Readonly buffer}} +{{&FieldType}} {{FieldName}} : register(t{{BufferIndex}}); +{{/IsReadOnlyBuffer}} +{{#IsReadWriteBuffer}} + +{{!Read write buffer}} +{{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}}); +{{/IsReadWriteBuffer}} +{{/BuffersList}} {{!Shader entry point}} [Shader("compute")] diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 34c9830b..09cf2fd2 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -68,7 +68,6 @@ namespace ComputeSharp.Shaders { BuffersList = shaderLoader.BuffersList, FieldsList = shaderLoader.FieldsList, - VariablesConstantBufferIndex = shaderLoader.ConstantBuffersCount, NumThreadsX = threadsX, NumThreadsY = threadsY, NumThreadsZ = threadsZ, @@ -98,7 +97,7 @@ namespace ComputeSharp.Shaders // Initialize the loop targets IReadOnlyList variables = new object[] { (uint)x, (uint)y, (uint)z }.Concat(shaderLoader.FieldValuesList).ToArray(); using GraphicsResource variablesBuffer = device.AllocateConstantBufferFromReflectedValues(variables); - commandList.SetComputeRootDescriptorTable(shaderLoader.RootParameters.Length - 1, variablesBuffer); + commandList.SetComputeRootDescriptorTable(0, variablesBuffer); // Dispatch and wait for completion commandList.Dispatch(groupsX, groupsY, groupsZ); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 575e8e91..3ac67495 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; +using System.Reflection; using System.Text.RegularExpressions; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; @@ -85,11 +86,6 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList FieldValuesList => _FieldValuesList; - /// - /// gets the number of constant buffers in the current instance - /// - public int ConstantBuffersCount { get; private set; } - /// /// Gets the name of the variable used as input for the shader method /// @@ -124,10 +120,15 @@ namespace ComputeSharp.Shaders.Translation if (ShaderFields.Any(fieldInfo => fieldInfo.IsStatic)) throw new InvalidOperationException("Empty shader body"); List descriptorRanges = new List(); + int constantBuffersCount = 0; int readOnlyBuffersCount = 0; int readWriteBuffersCount = 0; - foreach (System.Reflection.FieldInfo fieldInfo in ShaderFields) + // Descriptor for the buffer for captured scalar/vector variables + descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount++)); + + // Inspect the captured fields + foreach (FieldInfo fieldInfo in ShaderFields) { Type fieldType = fieldInfo.FieldType; string fieldName = fieldInfo.Name; @@ -136,13 +137,13 @@ namespace ComputeSharp.Shaders.Translation // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) { - descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, ConstantBuffersCount)); + descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount)); // Reference to the underlying buffer _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); - _BuffersList.Add(new ConstantBufferFieldInfo(typeName, fieldName, ConstantBuffersCount++)); + _BuffersList.Add(new ConstantBufferFieldInfo(typeName, fieldName, constantBuffersCount++)); } else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) { @@ -175,7 +176,6 @@ namespace ComputeSharp.Shaders.Translation } else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); } - RootParameters = descriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray(); }