diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 88c6e92c..fb7f8dcb 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -226,6 +226,48 @@ namespace ComputeSharp.SourceGenerators
.ToFullString();
}
}
+
+ ///
+ /// Creates a method to get a specific dimension size for a specified resource type.
+ ///
+ /// The input resource type (without generic parameters).
+ /// The type of items in the input resource.
+ /// The rank of the resource (must be 2 for structured buffers).
+ /// The target axis to get the dimension for.
+ /// A instance with the requested tree.
+ internal static MethodDeclarationSyntax CreateResourceDimensionAccessorMethod(string resourceType, string itemType, int rank, int axis)
+ {
+ // Create a static method to get a specified dimension for a target resource type.
+ // The method will automatically create the necessary number of temporary variables
+ // depending on the rank of the input resource (eg. 2 for Texture2D resources).
+ // For instance, this code will generate the following tree for Texture2D.Width:
+ //
+ // static int __get_Dimension0(Texture2D texture)
+ // {
+ // uint a, b;
+ // texture.GetDimensions(a, b);
+ //
+ // return a;
+ // }
+ return
+ MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), $"__get_Dimension{axis}")
+ .AddModifiers(Token(SyntaxKind.StaticKeyword))
+ .AddParameterListParameters(
+ Parameter(Identifier("resource"))
+ .WithType(GenericName(Identifier(resourceType))
+ .AddTypeArgumentListArguments(IdentifierName(itemType))))
+ .WithBody(Block(
+ LocalDeclarationStatement(
+ VariableDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)))
+ .AddVariables(Enumerable.Range(0, rank).Select(static i => VariableDeclarator(Identifier(('a' + i).ToString()))).ToArray())),
+ ExpressionStatement(
+ InvocationExpression(MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName("resource"),
+ IdentifierName("GetDimensions")))
+ .AddArgumentListArguments(Enumerable.Range(0, rank).Select(static i => Argument(IdentifierName(('a' + i).ToString()))).ToArray())),
+ ReturnStatement(IdentifierName(('a' + axis).ToString()))));
+ }
}
}