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
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
new file mode 100644
index 00000000..09507d2e
--- /dev/null
+++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs
@@ -0,0 +1,77 @@
+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;
+ }
+
+ ///
+ /// 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
+ ///
+ /// 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);
+ }
+}
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs
index 65ff18c2..5398fff1 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