From 65667e855f442282a9f31e3b588c0dbfcc105d2b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 12:45:23 +0200 Subject: [PATCH 1/8] Added ReadableMember class --- .../Translation/Models/ReadableMember.cs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs new file mode 100644 index 00000000..46468367 --- /dev/null +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs @@ -0,0 +1,58 @@ +using System; +using System.Diagnostics.Contracts; +using System.Reflection; + +namespace ComputeSharp.Shaders.Translation.Models +{ + /// + /// A that wraps a readable member of another and provides access to it + /// + internal sealed class ReadableMember + { + /// + /// The object wrapped by the current instance, if present + /// + private readonly FieldInfo? Field; + + /// + /// The object wrapped by the current instance, if present + /// + private readonly PropertyInfo? Property; + + /// + /// Creates a new instance with the given parameters + /// + /// The optional object wrapped by the current instance + /// The optional object wrapped by the current instance + private ReadableMember(FieldInfo? field, PropertyInfo? property) + { + Field = field; + Property = property; + } + + /// + /// Returns the value of the wrapped member for the current instance + /// + /// The target instance to use to read the value from + [Pure] + public object GetValue(object instance) + { + if (Field != null) return Field.GetValue(instance); + if (Property != null) return Property.GetValue(instance); + + throw new InvalidOperationException("Field and property can't both be null at the same time"); + } + + /// + /// Converts a object into a instance + /// + /// The input object to wrap + public static implicit operator ReadableMember(FieldInfo field) => new ReadableMember(field, null); + + /// + /// Converts a object into a instance + /// + /// The input object to wrap + public static implicit operator ReadableMember(PropertyInfo property) => new ReadableMember(null, property); + } +} From 8a156e76bbaa4b7a2d6674d97341e5867452ea05 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 12:48:08 +0200 Subject: [PATCH 2/8] Added type and name properties to readable member class --- .../Translation/Models/ReadableMember.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs index 46468367..4210d242 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs @@ -30,6 +30,25 @@ namespace ComputeSharp.Shaders.Translation.Models Property = property; } + /// + /// Gets the of the wrapped member for the current instance + /// + public Type MemberType + { + get + { + if (Field != null) return Field.FieldType; + if (Property != null) return Property.PropertyType; + + throw new InvalidOperationException("Field and property can't both be null at the same time"); + } + } + + /// + /// Gets the name of the wrapped member for the current instance + /// + public string Name => (Field?.Name ?? Property?.Name) ?? throw new InvalidOperationException("Field and property can't both be null at the same time"); + /// /// Returns the value of the wrapped member for the current instance /// From 59df32464d19c873245e261e44022771202a35ea Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 12:50:25 +0200 Subject: [PATCH 3/8] Refactored ShaderLoader class to use new ReadableMember type --- .../Translation/ShaderLoader.cs | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 65ff18c2..b6ba034a 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -8,6 +8,7 @@ using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models.Fields; using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; +using ComputeSharp.Shaders.Translation.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using SharpDX.Direct3D12; @@ -69,26 +70,26 @@ namespace ComputeSharp.Shaders.Translation public RootParameter[] RootParameters => _RootParameters ??= DescriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray(); /// - /// The of instances mapping the captured buffers in the current shader + /// The of instances mapping the captured buffers in the current shader /// - private readonly List _BufferFields = new List(); + private readonly List _BufferMembers = new List(); /// /// Gets the ordered collection of buffers used as fields in the current shader /// /// The to use to build the shader - public IEnumerable<(int Index, GraphicsResource Resource)> GetBuffers(Action action) => _BufferFields.Select((field, i) => (i + 1, (GraphicsResource)field.GetValue(action.Target))); + public IEnumerable<(int Index, GraphicsResource Resource)> GetBuffers(Action action) => _BufferMembers.Select((field, i) => (i + 1, (GraphicsResource)field.GetValue(action.Target))); /// - /// The of instances mapping the captured scalar/vector variables in the current shader + /// The of instances mapping the captured scalar/vector variables in the current shader /// - private readonly List _VariableFields = new List(); + private readonly List _VariableMembers = new List(); /// /// Gets the collection of values of the captured fields for the current shader /// /// The to use to build the shader - public IEnumerable GetVariables(Action action) => _VariableFields.Select(field => field.GetValue(action.Target)); + public IEnumerable GetVariables(Action action) => _VariableMembers.Select(field => field.GetValue(action.Target)); private readonly List _BuffersList = new List(); @@ -149,14 +150,14 @@ namespace ComputeSharp.Shaders.Translation } /// - /// Loads a specified and adds it to the shader model + /// Loads a specified and adds it to the shader model /// - /// The target to load + /// The target to load /// The optional explicit name to use for the field - private void LoadFieldInfo(FieldInfo fieldInfo, string? name = null) + private void LoadFieldInfo(ReadableMember memberInfo, string? name = null) { - Type fieldType = fieldInfo.FieldType; - string fieldName = name ?? fieldInfo.Name; + Type fieldType = memberInfo.MemberType; + string fieldName = name ?? memberInfo.Name; // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) @@ -164,7 +165,7 @@ namespace ComputeSharp.Shaders.Translation DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, _ConstantBuffersCount)); // Track the buffer field - _BufferFields.Add(fieldInfo); + _BufferMembers.Add(memberInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); _BuffersList.Add(new ConstantBufferFieldInfo(fieldType, typeName, fieldName, _ConstantBuffersCount++)); @@ -175,7 +176,7 @@ namespace ComputeSharp.Shaders.Translation DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, _ReadOnlyBuffersCount)); // Track the buffer field - _BufferFields.Add(fieldInfo); + _BufferMembers.Add(memberInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); _BuffersList.Add(new ReadOnlyBufferFieldInfo(fieldType, typeName, fieldName, _ReadOnlyBuffersCount++)); @@ -186,7 +187,7 @@ namespace ComputeSharp.Shaders.Translation DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, _ReadWriteBuffersCount)); // Track the buffer field - _BufferFields.Add(fieldInfo); + _BufferMembers.Add(memberInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); _BuffersList.Add(new ReadWriteBufferFieldInfo(fieldType, typeName, fieldName, _ReadWriteBuffersCount++)); @@ -194,7 +195,7 @@ namespace ComputeSharp.Shaders.Translation else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { // Register the captured field - _VariableFields.Add(fieldInfo); + _VariableMembers.Add(memberInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); _FieldsList.Add(new CapturedFieldInfo(fieldType, typeName, fieldName)); } From 2eb2ade5ef30c7c0eb5a7f648b6c21b314ad96eb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 12:51:43 +0200 Subject: [PATCH 4/8] Minor code refactoring --- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 2 +- .../Translation/ShaderSyntaxRewriter.cs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index b6ba034a..5398fff1 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -215,7 +215,7 @@ namespace ComputeSharp.Shaders.Translation root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root); // Register the captured static fields - foreach (var item in syntaxRewriter.StaticFields) + foreach (var item in syntaxRewriter.StaticMembers) { LoadFieldInfo(item.Value, item.Key); } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs index cbe726c5..5d243958 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Reflection; using ComputeSharp.Shaders.Extensions; +using ComputeSharp.Shaders.Translation.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -23,12 +24,12 @@ namespace ComputeSharp.Shaders.Translation /// public ShaderSyntaxRewriter(SemanticModel semanticModel) => SemanticModel = semanticModel; - private readonly Dictionary _StaticFields = new Dictionary(); + private readonly Dictionary _StaticMembers = new Dictionary(); /// - /// Gets the mapping of captured static fields used by the target code + /// Gets the mapping of captured static members used by the target code /// - public IReadOnlyDictionary StaticFields => _StaticFields; + public IReadOnlyDictionary StaticMembers => _StaticMembers; /// public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) @@ -101,9 +102,9 @@ namespace ComputeSharp.Shaders.Translation SyntaxNode syntaxNode = node.ReplaceMember(SemanticModel, out var variable); // Register the captured member, if any - if (variable.HasValue && !_StaticFields.ContainsKey(variable.Value.Name)) + if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name)) { - _StaticFields.Add(variable.Value.Name, variable.Value.FieldInfo); + _StaticMembers.Add(variable.Value.Name, variable.Value.FieldInfo); } return syntaxNode; From 44179ebeb772e8f50fbc20b94785868a0c39583d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 12:56:09 +0200 Subject: [PATCH 5/8] Implemented support for static properties --- .../Extensions/SyntaxNodeExtensions.cs | 35 ++++++++----------- .../Translation/Models/ReadableMember.cs | 2 +- .../Translation/ShaderSyntaxRewriter.cs | 2 +- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs index 20b6364f..24b2eb8e 100644 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using System.Reflection; using ComputeSharp.Shaders.Mappings; +using ComputeSharp.Shaders.Translation.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -40,10 +41,10 @@ namespace ComputeSharp.Shaders.Extensions /// /// The input to check and modify if needed /// The to use to load symbols for the input node - /// The info on parsed static fields, if any + /// The info on parsed static members, if any /// A instance that is compatible with HLSL [Pure] - public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel, out (string Name, FieldInfo FieldInfo)? variable) + public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel, out (string Name, ReadableMember MemberInfo)? variable) { // Set the variable to null, replace it later on if needed variable = null; @@ -86,46 +87,38 @@ namespace ComputeSharp.Shaders.Extensions // Retrieve the field or property info bool isReadonly; - object memberValue; - Type memberType; - FieldInfo? fieldInfo; + ReadableMember memberInfo; switch (memberSymbol.Kind) { case SymbolKind.Field: - fieldInfo = fieldDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + FieldInfo fieldInfo = fieldDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); isReadonly = fieldInfo.IsInitOnly; - memberValue = fieldInfo.GetValue(null); - memberType = fieldInfo.FieldType; + memberInfo = fieldInfo; break; case SymbolKind.Property: PropertyInfo propertyInfo = fieldDeclaringType.GetProperty(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); isReadonly = !propertyInfo.CanWrite; - memberValue = propertyInfo.GetValue(null); - memberType = propertyInfo.PropertyType; - fieldInfo = null; + memberInfo = propertyInfo; break; default: throw new InvalidOperationException($"Invalid symbol kind: {memberSymbol.Kind}"); } // Constant replacement if the value is a readonly scalar value - if (isReadonly && HlslKnownTypes.IsKnownScalarType(memberType)) + if (isReadonly && HlslKnownTypes.IsKnownScalarType(memberInfo.MemberType)) { - return memberValue switch + return memberInfo.GetValue(null) switch { true => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), false => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), IFormattable scalar => SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(scalar.ToString(null, CultureInfo.InvariantCulture))), - _ => throw new InvalidOperationException($"Invalid field of type {memberType}") + _ => throw new InvalidOperationException($"Invalid field of type {memberInfo.MemberType}") }; } - // Captured field, treat it like any other captured variable in the closure - if (fieldInfo != null) - { - string name = $"{containingMemberSymbolInfo.Symbol.Name}_{fieldInfo.Name}"; - variable = (name, fieldInfo); - return SyntaxFactory.IdentifierName(name); - } + // Captured member, treat it like any other captured variable in the closure + string name = $"{containingMemberSymbolInfo.Symbol.Name}_{memberInfo.Name}"; + variable = (name, memberInfo); + return SyntaxFactory.IdentifierName(name); } // Process the input node if it's a known method invocation diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs index 4210d242..09507d2e 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs @@ -54,7 +54,7 @@ namespace ComputeSharp.Shaders.Translation.Models /// /// The target instance to use to read the value from [Pure] - public object GetValue(object instance) + public object GetValue(object? instance) { if (Field != null) return Field.GetValue(instance); if (Property != null) return Property.GetValue(instance); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs index 5d243958..466e4a24 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs @@ -104,7 +104,7 @@ namespace ComputeSharp.Shaders.Translation // Register the captured member, if any if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name)) { - _StaticMembers.Add(variable.Value.Name, variable.Value.FieldInfo); + _StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo); } return syntaxNode; From f02bd37b2823c17eba09594b40b65e4ea76139f3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 13:00:33 +0200 Subject: [PATCH 6/8] Added tests for captured properties --- .../ShaderClosureTests.StaticFields.cs | 16 +- .../ShaderClosureTests.StaticProperties.cs | 141 ++++++++++++++++++ 2 files changed, 149 insertions(+), 8 deletions(-) create mode 100644 tests/ComputeSharp.Tests/ShaderClosureTests.StaticProperties.cs diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.StaticFields.cs b/tests/ComputeSharp.Tests/ShaderClosureTests.StaticFields.cs index f0c97dc2..99ebb36e 100644 --- a/tests/ComputeSharp.Tests/ShaderClosureTests.StaticFields.cs +++ b/tests/ComputeSharp.Tests/ShaderClosureTests.StaticFields.cs @@ -6,7 +6,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests { /// - /// A container for static fields and properties to test + /// A container for static fields to test /// public sealed class StaticFieldsContainer { @@ -23,7 +23,7 @@ namespace ComputeSharp.Tests /// /// A field /// - public static Vector2 ReadonlyVector2 = new Vector2(3.14f, 7.77f); + public static readonly Vector2 ReadonlyVector2 = new Vector2(3.14f, 7.77f); /// /// A field @@ -39,7 +39,7 @@ namespace ComputeSharp.Tests public partial class ShaderClosureTests { [TestMethod] - public void StaticReadonlyScalarAssignToBuffer() + public void StaticReadonlyScalarFieldAssignToBuffer() { using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); @@ -53,7 +53,7 @@ namespace ComputeSharp.Tests } [TestMethod] - public void StaticScalarAssignToBuffer() + public void StaticScalarFieldAssignToBuffer() { using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); @@ -67,7 +67,7 @@ namespace ComputeSharp.Tests } [TestMethod] - public void StaticScalarRepeatedAssignToBuffer() + public void StaticScalarFieldRepeatedAssignToBuffer() { using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); @@ -81,7 +81,7 @@ namespace ComputeSharp.Tests } [TestMethod] - public void StaticReadonlyVectorAssignToBuffer() + public void StaticReadonlyVectorFieldAssignToBuffer() { using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(2); @@ -100,7 +100,7 @@ namespace ComputeSharp.Tests } [TestMethod] - public void StaticVectorAssignToBuffer() + public void StaticVectorFieldAssignToBuffer() { using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(2); @@ -119,7 +119,7 @@ namespace ComputeSharp.Tests } [TestMethod] - public void StaticReadOnlyBufferAssignToBuffer() + public void StaticReadOnlyBufferFieldAssignToBuffer() { using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(2); diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.StaticProperties.cs b/tests/ComputeSharp.Tests/ShaderClosureTests.StaticProperties.cs new file mode 100644 index 00000000..810cfa23 --- /dev/null +++ b/tests/ComputeSharp.Tests/ShaderClosureTests.StaticProperties.cs @@ -0,0 +1,141 @@ +using System; +using System.Numerics; +using ComputeSharp.Graphics.Buffers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + /// + /// A container for static properties to test + /// + public sealed class StaticPropertiesContainer + { + /// + /// A property + /// + public static float ReadonlyFloat => MathF.PI; + + /// + /// A property + /// + public static float StaticFloat { get; set; } = MathF.PI; + + /// + /// A property + /// + public static Vector2 ReadonlyVector2 { get; } = new Vector2(3.14f, 7.77f); + + /// + /// A property + /// + public static Vector2 StaticVector2 { get; set; } = new Vector2(3.14f, 7.77f); + + /// + /// A property + /// + public static ReadOnlyBuffer StaticBuffer { get; set; } = Gpu.Default.AllocateReadOnlyBuffer(new[] { 3.14f, 7.77f }); + } + + public partial class ShaderClosureTests + { + [TestMethod] + public void StaticReadonlyScalarPropertyAssignToBuffer() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Action action = id => buffer[0] = StaticPropertiesContainer.ReadonlyFloat; + + Gpu.Default.For(1, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - StaticPropertiesContainer.ReadonlyFloat) < 0.0001f); + } + + [TestMethod] + public void StaticScalarPropertyAssignToBuffer() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Action action = id => buffer[0] = StaticPropertiesContainer.StaticFloat; + + Gpu.Default.For(1, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - StaticPropertiesContainer.StaticFloat) < 0.0001f); + } + + [TestMethod] + public void StaticScalarPropertyRepeatedAssignToBuffer() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Action action = id => buffer[0] = StaticPropertiesContainer.StaticFloat + StaticPropertiesContainer.StaticFloat; + + Gpu.Default.For(1, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - StaticPropertiesContainer.StaticFloat * 2) < 0.0001f); + } + + [TestMethod] + public void StaticReadonlyVectorPropertyAssignToBuffer() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(2); + + Action action = id => + { + if (id.X == 0) buffer[0] = StaticPropertiesContainer.ReadonlyVector2.X; + else buffer[1] = StaticPropertiesContainer.ReadonlyVector2.Y; + }; + + Gpu.Default.For(2, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - StaticPropertiesContainer.ReadonlyVector2.X) < 0.0001f); + Assert.IsTrue(MathF.Abs(result[1] - StaticPropertiesContainer.ReadonlyVector2.Y) < 0.0001f); + } + + [TestMethod] + public void StaticVectorPropertyAssignToBuffer() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(2); + + Action action = id => + { + if (id.X == 0) buffer[0] = StaticPropertiesContainer.StaticVector2.X; + else buffer[1] = StaticPropertiesContainer.StaticVector2.Y; + }; + + Gpu.Default.For(2, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - StaticPropertiesContainer.StaticVector2.X) < 0.0001f); + Assert.IsTrue(MathF.Abs(result[1] - StaticPropertiesContainer.StaticVector2.Y) < 0.0001f); + } + + [TestMethod] + public void StaticReadOnlyBufferPropertyAssignToBuffer() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(2); + + Action action = id => + { + if (id.X == 0) buffer[0] = StaticPropertiesContainer.StaticBuffer[0]; + else buffer[1] = StaticPropertiesContainer.StaticBuffer[1]; + }; + + Gpu.Default.For(2, action); + + float[] result = buffer.GetData(); + float[] expected = StaticPropertiesContainer.StaticBuffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - expected[0]) < 0.0001f); + Assert.IsTrue(MathF.Abs(result[1] - expected[1]) < 0.0001f); + } + } +} From 31d0558a176ab498905c7c10dd677a92402e76ca Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 13:04:30 +0200 Subject: [PATCH 7/8] Code refactoring in the test project --- tests/ComputeSharp.Tests/ShaderClosureTests.cs | 2 +- ...haderClosureTests.StaticFields.cs => StaticFieldsTests.cs} | 4 +++- ...sureTests.StaticProperties.cs => StaticPropertiesTests.cs} | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) rename tests/ComputeSharp.Tests/{ShaderClosureTests.StaticFields.cs => StaticFieldsTests.cs} (98%) rename tests/ComputeSharp.Tests/{ShaderClosureTests.StaticProperties.cs => StaticPropertiesTests.cs} (98%) diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.Tests/ShaderClosureTests.cs index cf789e92..cd5b979b 100644 --- a/tests/ComputeSharp.Tests/ShaderClosureTests.cs +++ b/tests/ComputeSharp.Tests/ShaderClosureTests.cs @@ -9,7 +9,7 @@ namespace ComputeSharp.Tests { [TestClass] [TestCategory("ShaderClosure")] - public partial class ShaderClosureTests + public class ShaderClosureTests { [TestMethod] public void LocalScalarAssignToBuffer() diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.StaticFields.cs b/tests/ComputeSharp.Tests/StaticFieldsTests.cs similarity index 98% rename from tests/ComputeSharp.Tests/ShaderClosureTests.StaticFields.cs rename to tests/ComputeSharp.Tests/StaticFieldsTests.cs index 99ebb36e..a9e2a862 100644 --- a/tests/ComputeSharp.Tests/ShaderClosureTests.StaticFields.cs +++ b/tests/ComputeSharp.Tests/StaticFieldsTests.cs @@ -36,7 +36,9 @@ namespace ComputeSharp.Tests public static ReadOnlyBuffer StaticBuffer = Gpu.Default.AllocateReadOnlyBuffer(new[] { 3.14f, 7.77f }); } - public partial class ShaderClosureTests + [TestClass] + [TestCategory("StaticFields")] + public class StaticFieldsTests { [TestMethod] public void StaticReadonlyScalarFieldAssignToBuffer() diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.StaticProperties.cs b/tests/ComputeSharp.Tests/StaticPropertiesTests.cs similarity index 98% rename from tests/ComputeSharp.Tests/ShaderClosureTests.StaticProperties.cs rename to tests/ComputeSharp.Tests/StaticPropertiesTests.cs index 810cfa23..7c2a2b94 100644 --- a/tests/ComputeSharp.Tests/ShaderClosureTests.StaticProperties.cs +++ b/tests/ComputeSharp.Tests/StaticPropertiesTests.cs @@ -36,7 +36,9 @@ namespace ComputeSharp.Tests public static ReadOnlyBuffer StaticBuffer { get; set; } = Gpu.Default.AllocateReadOnlyBuffer(new[] { 3.14f, 7.77f }); } - public partial class ShaderClosureTests + [TestClass] + [TestCategory("StaticProperties")] + public class StaticPropertiesTests { [TestMethod] public void StaticReadonlyScalarPropertyAssignToBuffer() From 2696158615e50268e54895c32084f5a0bb5e710b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 13:06:27 +0200 Subject: [PATCH 8/8] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e7f5b32..dbca212a 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,9 @@ If the shader in C# is capturing some local variable, those will be automaticall ✅ Local vector types: `System.Numerics.Vector2`, `Vector3`, `Vector4` -✅ `static` fields of both scalar and vector types (parent class must be `public`) +✅ `static` fields of both scalar, vector or buffer types (parent class must be `public`) + +✅ `static` properties, same as with fields ## Advanced usage