Merge pull request #28 from Sergio0694/feature_any-type-properties
Support for static properties
このコミットが含まれているのは:
8ファイル変更
+272
-53
検索条件に一致するファイルはありません
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="MemberAccessExpressionSyntax"/> to check and modify if needed</param>
|
||||
/// <param name="semanticModel">The <see cref="SemanticModel"/> to use to load symbols for the input node</param>
|
||||
/// <param name="variable">The info on parsed static fields, if any</param>
|
||||
/// <param name="variable">The info on parsed static members, if any</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
|
||||
[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
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that wraps a readable member of another <see langword="class"/> and provides access to it
|
||||
/// </summary>
|
||||
internal sealed class ReadableMember
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="FieldInfo"/> object wrapped by the current instance, if present
|
||||
/// </summary>
|
||||
private readonly FieldInfo? Field;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="PropertyInfo"/> object wrapped by the current instance, if present
|
||||
/// </summary>
|
||||
private readonly PropertyInfo? Property;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadableMember"/> instance with the given parameters
|
||||
/// </summary>
|
||||
/// <param name="field">The optional <see cref="FieldInfo"/> object wrapped by the current instance</param>
|
||||
/// <param name="property">The optional <see cref="PropertyInfo"/> object wrapped by the current instance</param>
|
||||
private ReadableMember(FieldInfo? field, PropertyInfo? property)
|
||||
{
|
||||
Field = field;
|
||||
Property = property;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="Type"/> of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
public string Name => (Field?.Name ?? Property?.Name) ?? throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
/// <param name="instance">The target instance to use to read the value from</param>
|
||||
[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");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="FieldInfo"/> object into a <see cref="ReadableMember"/> instance
|
||||
/// </summary>
|
||||
/// <param name="field">The input <see cref="FieldInfo"/> object to wrap</param>
|
||||
public static implicit operator ReadableMember(FieldInfo field) => new ReadableMember(field, null);
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="PropertyInfo"/> object into a <see cref="ReadableMember"/> instance
|
||||
/// </summary>
|
||||
/// <param name="property">The input <see cref="PropertyInfo"/> object to wrap</param>
|
||||
public static implicit operator ReadableMember(PropertyInfo property) => new ReadableMember(null, property);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> of <see cref="FieldInfo"/> instances mapping the captured buffers in the current shader
|
||||
/// The <see cref="List{T}"/> of <see cref="ReadableMember"/> instances mapping the captured buffers in the current shader
|
||||
/// </summary>
|
||||
private readonly List<FieldInfo> _BufferFields = new List<FieldInfo>();
|
||||
private readonly List<ReadableMember> _BufferMembers = new List<ReadableMember>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ordered collection of buffers used as fields in the current shader
|
||||
/// </summary>
|
||||
/// <param name="action">The <see cref="Action{T}"/> to use to build the shader</param>
|
||||
public IEnumerable<(int Index, GraphicsResource Resource)> GetBuffers(Action<ThreadIds> action) => _BufferFields.Select((field, i) => (i + 1, (GraphicsResource)field.GetValue(action.Target)));
|
||||
public IEnumerable<(int Index, GraphicsResource Resource)> GetBuffers(Action<ThreadIds> action) => _BufferMembers.Select((field, i) => (i + 1, (GraphicsResource)field.GetValue(action.Target)));
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> of <see cref="FieldInfo"/> instances mapping the captured scalar/vector variables in the current shader
|
||||
/// The <see cref="List{T}"/> of <see cref="ReadableMember"/> instances mapping the captured scalar/vector variables in the current shader
|
||||
/// </summary>
|
||||
private readonly List<FieldInfo> _VariableFields = new List<FieldInfo>();
|
||||
private readonly List<ReadableMember> _VariableMembers = new List<ReadableMember>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of values of the captured fields for the current shader
|
||||
/// </summary>
|
||||
/// <param name="action">The <see cref="Action{T}"/> to use to build the shader</param>
|
||||
public IEnumerable<object> GetVariables(Action<ThreadIds> action) => _VariableFields.Select(field => field.GetValue(action.Target));
|
||||
public IEnumerable<object> GetVariables(Action<ThreadIds> action) => _VariableMembers.Select(field => field.GetValue(action.Target));
|
||||
|
||||
private readonly List<HlslBufferInfo> _BuffersList = new List<HlslBufferInfo>();
|
||||
|
||||
@@ -149,14 +150,14 @@ namespace ComputeSharp.Shaders.Translation
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a specified <see cref="FieldInfo"/> and adds it to the shader model
|
||||
/// Loads a specified <see cref="ReadableMember"/> and adds it to the shader model
|
||||
/// </summary>
|
||||
/// <param name="fieldInfo">The target <see cref="FieldInfo"/> to load</param>
|
||||
/// <param name="memberInfo">The target <see cref="ReadableMember"/> to load</param>
|
||||
/// <param name="name">The optional explicit name to use for the field</param>
|
||||
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));
|
||||
}
|
||||
@@ -214,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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
/// <param name="semanticModel"></param>
|
||||
public ShaderSyntaxRewriter(SemanticModel semanticModel) => SemanticModel = semanticModel;
|
||||
|
||||
private readonly Dictionary<string, FieldInfo> _StaticFields = new Dictionary<string, FieldInfo>();
|
||||
private readonly Dictionary<string, ReadableMember> _StaticMembers = new Dictionary<string, ReadableMember>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapping of captured static fields used by the target code
|
||||
/// Gets the mapping of captured static members used by the target code
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, FieldInfo> StaticFields => _StaticFields;
|
||||
public IReadOnlyDictionary<string, ReadableMember> StaticMembers => _StaticMembers;
|
||||
|
||||
/// <inheritdoc/>
|
||||
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.MemberInfo);
|
||||
}
|
||||
|
||||
return syntaxNode;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace ComputeSharp.Tests
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("ShaderClosure")]
|
||||
public partial class ShaderClosureTests
|
||||
public class ShaderClosureTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void LocalScalarAssignToBuffer()
|
||||
|
||||
+11
-9
@@ -6,7 +6,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
namespace ComputeSharp.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// A container <see langword="class"/> for static fields and properties to test
|
||||
/// A container <see langword="class"/> for static fields to test
|
||||
/// </summary>
|
||||
public sealed class StaticFieldsContainer
|
||||
{
|
||||
@@ -23,7 +23,7 @@ namespace ComputeSharp.Tests
|
||||
/// <summary>
|
||||
/// A <see langword="static"/> <see langword="readonly"/> <see langword="Vector2"/> field
|
||||
/// </summary>
|
||||
public static Vector2 ReadonlyVector2 = new Vector2(3.14f, 7.77f);
|
||||
public static readonly Vector2 ReadonlyVector2 = new Vector2(3.14f, 7.77f);
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="static"/> <see langword="Vector2"/> field
|
||||
@@ -36,10 +36,12 @@ namespace ComputeSharp.Tests
|
||||
public static ReadOnlyBuffer<float> StaticBuffer = Gpu.Default.AllocateReadOnlyBuffer(new[] { 3.14f, 7.77f });
|
||||
}
|
||||
|
||||
public partial class ShaderClosureTests
|
||||
[TestClass]
|
||||
[TestCategory("StaticFields")]
|
||||
public class StaticFieldsTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void StaticReadonlyScalarAssignToBuffer()
|
||||
public void StaticReadonlyScalarFieldAssignToBuffer()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
@@ -53,7 +55,7 @@ namespace ComputeSharp.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StaticScalarAssignToBuffer()
|
||||
public void StaticScalarFieldAssignToBuffer()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
@@ -67,7 +69,7 @@ namespace ComputeSharp.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StaticScalarRepeatedAssignToBuffer()
|
||||
public void StaticScalarFieldRepeatedAssignToBuffer()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
@@ -81,7 +83,7 @@ namespace ComputeSharp.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StaticReadonlyVectorAssignToBuffer()
|
||||
public void StaticReadonlyVectorFieldAssignToBuffer()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(2);
|
||||
|
||||
@@ -100,7 +102,7 @@ namespace ComputeSharp.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StaticVectorAssignToBuffer()
|
||||
public void StaticVectorFieldAssignToBuffer()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(2);
|
||||
|
||||
@@ -119,7 +121,7 @@ namespace ComputeSharp.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StaticReadOnlyBufferAssignToBuffer()
|
||||
public void StaticReadOnlyBufferFieldAssignToBuffer()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(2);
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// A container <see langword="class"/> for static properties to test
|
||||
/// </summary>
|
||||
public sealed class StaticPropertiesContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="static"/> <see langword="readonly"/> <see langword="float"/> property
|
||||
/// </summary>
|
||||
public static float ReadonlyFloat => MathF.PI;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="static"/> <see langword="int"/> property
|
||||
/// </summary>
|
||||
public static float StaticFloat { get; set; } = MathF.PI;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="static"/> <see langword="readonly"/> <see langword="Vector2"/> property
|
||||
/// </summary>
|
||||
public static Vector2 ReadonlyVector2 { get; } = new Vector2(3.14f, 7.77f);
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="static"/> <see langword="Vector2"/> property
|
||||
/// </summary>
|
||||
public static Vector2 StaticVector2 { get; set; } = new Vector2(3.14f, 7.77f);
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="static"/> <see cref="ReadOnlyBuffer{T}"/> property
|
||||
/// </summary>
|
||||
public static ReadOnlyBuffer<float> StaticBuffer { get; set; } = Gpu.Default.AllocateReadOnlyBuffer(new[] { 3.14f, 7.77f });
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
[TestCategory("StaticProperties")]
|
||||
public class StaticPropertiesTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void StaticReadonlyScalarPropertyAssignToBuffer()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Action<ThreadIds> 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<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Action<ThreadIds> 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<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Action<ThreadIds> 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<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(2);
|
||||
|
||||
Action<ThreadIds> 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<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(2);
|
||||
|
||||
Action<ThreadIds> 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<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(2);
|
||||
|
||||
Action<ThreadIds> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする