Implemented support for GetDimensions properties

このコミットが含まれているのは:
Sergio Pedri committed 2021-01-09 19:38:27 +01:00
1 親 b26fda5e8f
コミット e67d1161f3
3ファイル変更
+102 -44

検索条件に一致するファイルはありません

+1 -43
ファイルの表示
@@ -78,7 +78,7 @@ namespace ComputeSharp.SourceGenerators
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.ToFullString()).ToArray();
var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray();
// Create the compilation unit with the source attribute
var source =
@@ -226,48 +226,6 @@ namespace ComputeSharp.SourceGenerators
.ToFullString();
}
}
/// <summary>
/// Creates a method to get a specific dimension size for a specified resource type.
/// </summary>
/// <param name="resourceType">The input resource type (without generic parameters).</param>
/// <param name="itemType">The type of items in the input resource.</param>
/// <param name="rank">The rank of the resource (must be 2 for structured buffers).</param>
/// <param name="axis">The target axis to get the dimension for.</param>
/// <returns>A <see cref="MethodDeclarationSyntax"/> instance with the requested tree.</returns>
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<T> resources).
// For instance, this code will generate the following tree for Texture2D<float>.Width:
//
// static int __get_Dimension0(Texture2D<float> 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()))));
}
}
}
+44
ファイルの表示
@@ -31,6 +31,29 @@ namespace ComputeSharp.SourceGenerators.Mappings
[$"ComputeSharp.IReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3"
};
/// <summary>
/// The mapping of supported known size accessors for HLSL resource types.
/// </summary>
private static readonly IReadOnlyDictionary<string, (int Rank, int Axis)> KnownSizeAccessors = new Dictionary<string, (int, int)>
{
["ComputeSharp.Resources.Buffer`1.Length"] = (2, 0),
["ComputeSharp.Resources.Texture2D`1.Width"] = (2, 0),
["ComputeSharp.Resources.Texture2D`1.Height"] = (2, 1),
["ComputeSharp.Resources.Texture3D`1.Width"] = (3, 0),
["ComputeSharp.Resources.Texture3D`1.Height"] = (3, 1),
["ComputeSharp.Resources.Texture3D`1.Depth"] = (3, 2),
["ComputeSharp.IReadOnlyTexture2D`1.Width"] = (2, 0),
["ComputeSharp.IReadOnlyTexture2D`1.Height"] = (2, 1),
["ComputeSharp.IReadOnlyTexture3D`1.Width"] = (3, 0),
["ComputeSharp.IReadOnlyTexture3D`1.Height"] = (3, 1),
["ComputeSharp.IReadOnlyTexture3D`1.Depth"] = (3, 2),
["ComputeSharp.IReadWriteTexture2D`1.Width"] = (2, 0),
["ComputeSharp.IReadWriteTexture2D`1.Height"] = (2, 1),
["ComputeSharp.IReadWriteTexture3D`1.Width"] = (3, 0),
["ComputeSharp.IReadWriteTexture3D`1.Height"] = (3, 1),
["ComputeSharp.IReadWriteTexture3D`1.Depth"] = (3, 2)
};
/// <summary>
/// The mapping of supported known members to HLSL names.
/// </summary>
@@ -193,5 +216,26 @@ namespace ComputeSharp.SourceGenerators.Mappings
{
return KnownIndexers.TryGetValue(name, out mapped);
}
/// <summary>
/// Tries to get the mapped HLSL-compatible indexer vector type name for the input indexer name.
/// </summary>
/// <param name="name">The input fully qualified indexer name.</param>
/// <param name="mapped">The mapped type name, if one is found.</param>
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given indexer.</returns>
[Pure]
public static bool TryGetAccessorRankAndAxis(string name, out int rank, out int axis)
{
if (KnownSizeAccessors.TryGetValue(name, out var info))
{
(rank, axis) = info;
return true;
}
(rank, axis) = default((int, int));
return false;
}
}
}
+57 -1
ファイルの表示
@@ -306,7 +306,23 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
true => ParseExpression(mapping!),
false => updatedNode.WithName(IdentifierName(mapping!))
};
}
}
// If the current member access is an access to any of the size properties in the available
// resource types (either buffer or texture types), then rewrite the subtree to transform the
// property access into an invocation of an autogenerated static helper method to get the target
// dimension for the buffer in use. The helper method is added to the list of static helpers.
if (operation is IPropertyReferenceOperation propertyOperation &&
HlslKnownMembers.TryGetAccessorRankAndAxis(propertyOperation.Property.GetFullMetadataName(), out int rank, out int axis))
{
INamedTypeSymbol resourceType = (INamedTypeSymbol)this.semanticModel.GetTypeInfo(node.Expression).Type!;
string resourceName = HlslKnownTypes.GetMappedName(resourceType);
MethodDeclarationSyntax methodSyntax = CreateResourceDimensionAccessorMethod(resourceName, rank, axis);
this.staticMethods.Add(propertyOperation.Property.GetMethod!, methodSyntax);
return InvocationExpression(IdentifierName(methodSyntax.Identifier)).AddArgumentListArguments(Argument(updatedNode.Expression));
}
}
return updatedNode;
@@ -416,5 +432,45 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return updatedToken.WithoutTrivia();
}
/// <summary>
/// Creates a method to get a specific dimension size for a specified resource type.
/// </summary>
/// <param name="resourceType">The input resource type.</param>
/// <param name="rank">The rank of the resource (must be 2 for structured buffers).</param>
/// <param name="axis">The target axis to get the dimension for.</param>
/// <returns>A <see cref="MethodDeclarationSyntax"/> instance with the requested tree.</returns>
private static MethodDeclarationSyntax CreateResourceDimensionAccessorMethod(string resourceType, 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<T> resources).
// For instance, this code will generate the following tree for Texture2D<float>.Width:
//
// static int __get_Dimension0(Texture2D<float> texture)
// {
// uint _0, _1;
// texture.GetDimensions(_0, _1);
//
// return _0;
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), $"__get_Dimension{axis}")
.AddModifiers(Token(SyntaxKind.StaticKeyword))
.AddParameterListParameters(
Parameter(Identifier("resource"))
.WithType(IdentifierName(resourceType)))
.WithBody(Block(
LocalDeclarationStatement(
VariableDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)))
.AddVariables(Enumerable.Range(0, rank).Select(static i => VariableDeclarator(Identifier("_" + i))).ToArray())),
ExpressionStatement(
InvocationExpression(MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("resource"),
IdentifierName("GetDimensions")))
.AddArgumentListArguments(Enumerable.Range(0, rank).Select(static i => Argument(IdentifierName("_" + i))).ToArray())),
ReturnStatement(IdentifierName("_" + axis))));
}
}
}