Switched ThreadIds to static type

このコミットが含まれているのは:
Sergio Pedri committed 2021-01-13 19:07:53 +01:00
1 親 bbc8832023
コミット c6aa194749
6ファイル変更
+73 -78

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

+1 -2
ファイルの表示
@@ -8,7 +8,6 @@
/// <summary>
/// Executes the current compute shader.
/// </summary>
/// <param name="ids">The <see cref="ThreadIds"/> value with the current execution indices.</param>
void Execute(ThreadIds ids);
void Execute();
}
}
+41 -41
ファイルの表示
@@ -3,203 +3,203 @@
namespace ComputeSharp
{
/// <summary>
/// A <see langword="struct"/> that indicates the ids of a given GPU thread running a compute shader.
/// A <see langword="class"/> that indicates the ids of a given GPU thread running a compute shader.
/// </summary>
public readonly ref struct ThreadIds
public static class ThreadIds
{
/// <summary>
/// Gets the X id of the current thread.
/// </summary>
public int X => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(X)}");
public static int X => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(X)}");
/// <summary>
/// Gets the Y id of the current thread.
/// </summary>
public int Y => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Y)}");
public static int Y => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Y)}");
/// <summary>
/// Gets the Z id of the current thread.
/// </summary>
public int Z => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Z)}");
public static int Z => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Z)}");
/// <summary>
/// Gets an <see cref="Int2"/> value with the <see cref="X"/> value for all components.
/// </summary>
public Int2 XX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XX)}");
public static Int2 XX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XX)}");
/// <summary>
/// Gets or sets an <see cref="Int2"/> value with the <see cref="X"/> and <see cref="Y"/> values.
/// </summary>
public Int2 XY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XY)}");
public static Int2 XY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XY)}");
/// <summary>
/// Gets or sets an <see cref="Int2"/> value with the <see cref="X"/> and <see cref="Z"/> values.
/// </summary>
public Int2 XZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZ)}");
public static Int2 XZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZ)}");
/// <summary>
/// Gets or sets an <see cref="Int2"/> value with the <see cref="Y"/> and <see cref="X"/> values.
/// </summary>
public Int2 YX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YX)}");
public static Int2 YX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YX)}");
/// <summary>
/// Gets an <see cref="Int2"/> value with the <see cref="Y"/> value for all components.
/// </summary>
public Int2 YY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YY)}");
public static Int2 YY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YY)}");
/// <summary>
/// Gets or sets an <see cref="Int2"/> value with the <see cref="Y"/> and <see cref="Z"/> values.
/// </summary>
public Int2 YZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZ)}");
public static Int2 YZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZ)}");
/// <summary>
/// Gets or sets an <see cref="Int2"/> value with the <see cref="Z"/> and <see cref="X"/> values.
/// </summary>
public Int2 ZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZX)}");
public static Int2 ZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZX)}");
/// <summary>
/// Gets or sets an <see cref="Int2"/> value with the <see cref="Z"/> and <see cref="Y"/> values.
/// </summary>
public Int2 ZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZY)}");
public static Int2 ZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZY)}");
/// <summary>
/// Gets an <see cref="Int2"/> value with the <see cref="Z"/> value for all components.
/// </summary>
public Int2 ZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZ)}");
public static Int2 ZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZ)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="X"/> value for all components.
/// </summary>
public Int3 XXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXX)}");
public static Int3 XXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXX)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="X"/> and <see cref="Y"/> values.
/// </summary>
public Int3 XXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXY)}");
public static Int3 XXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXY)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="X"/> and <see cref="Z"/> values.
/// </summary>
public Int3 XXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXZ)}");
public static Int3 XXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXZ)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="Y"/> and <see cref="X"/> values.
/// </summary>
public Int3 XYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYX)}");
public static Int3 XYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYX)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="Y"/> and <see cref="Y"/> values.
/// </summary>
public Int3 XYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYY)}");
public static Int3 XYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYY)}");
/// <summary>
/// Gets or sets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="Y"/> and <see cref="Z"/> values.
/// </summary>
public Int3 XYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYZ)}");
public static Int3 XYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYZ)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="Z"/> and <see cref="X"/> values.
/// </summary>
public Int3 XZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZX)}");
public static Int3 XZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZX)}");
/// <summary>
/// Gets or sets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="Z"/> and <see cref="Y"/> values.
/// </summary>
public Int3 XZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZY)}");
public static Int3 XZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZY)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="X"/>, <see cref="Z"/> and <see cref="Z"/> values.
/// </summary>
public Int3 XZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZZ)}");
public static Int3 XZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZZ)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="X"/> and <see cref="X"/> values.
/// </summary>
public Int3 YXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXX)}");
public static Int3 YXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXX)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="X"/> and <see cref="Y"/> values.
/// </summary>
public Int3 YXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXY)}");
public static Int3 YXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXY)}");
/// <summary>
/// Gets or sets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="X"/> and <see cref="Z"/> values.
/// </summary>
public Int3 YXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXZ)}");
public static Int3 YXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXZ)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="Y"/> and <see cref="X"/> values.
/// </summary>
public Int3 YYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYX)}");
public static Int3 YYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYX)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Y"/> value for all components.
/// </summary>
public Int3 YYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYY)}");
public static Int3 YYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYY)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="Y"/> and <see cref="Z"/> values.
/// </summary>
public Int3 YYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYZ)}");
public static Int3 YYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYZ)}");
/// <summary>
/// Gets or sets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="Z"/> and <see cref="X"/> values.
/// </summary>
public Int3 YZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZX)}");
public static Int3 YZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZX)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="Z"/> and <see cref="Y"/> values.
/// </summary>
public Int3 YZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZY)}");
public static Int3 YZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZY)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Y"/>, <see cref="Z"/> and <see cref="Z"/> values.
/// </summary>
public Int3 YZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZZ)}");
public static Int3 YZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZZ)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="Z"/> and <see cref="Z"/> values.
/// </summary>
public Int3 ZXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXX)}");
public static Int3 ZXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXX)}");
/// <summary>
/// Gets or sets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="X"/> and <see cref="Y"/> values.
/// </summary>
public Int3 ZXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXY)}");
public static Int3 ZXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXY)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="X"/> and <see cref="Z"/> values.
/// </summary>
public Int3 ZXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXZ)}");
public static Int3 ZXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXZ)}");
/// <summary>
/// Gets or sets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="Y"/> and <see cref="X"/> values.
/// </summary>
public Int3 ZYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYX)}");
public static Int3 ZYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYX)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="Y"/> and <see cref="Y"/> values.
/// </summary>
public Int3 ZYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYY)}");
public static Int3 ZYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYY)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="Y"/> and <see cref="Z"/> values.
/// </summary>
public Int3 ZYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYZ)}");
public static Int3 ZYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYZ)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="Z"/> and <see cref="X"/> values.
/// </summary>
public Int3 ZZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZX)}");
public static Int3 ZZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZX)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Z"/>, <see cref="Z"/> and <see cref="Y"/> values.
/// </summary>
public Int3 ZZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZY)}");
public static Int3 ZZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZY)}");
/// <summary>
/// Gets an <see cref="Int3"/> value with the <see cref="Z"/> value for all components.
/// </summary>
public Int3 ZZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZZ)}");
public static Int3 ZZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZZ)}");
}
}
+3 -6
ファイルの表示
@@ -171,18 +171,15 @@ namespace ComputeSharp.SourceGenerators
ShaderSourceRewriter shaderSourceRewriter = new(structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
// Rewrite the method syntax tree
var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
// If the method is the shader entry point, do additional processing
if (methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) &&
methodDeclarationSymbol.ReturnsVoid &&
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 1 &&
methodDeclarationSymbol.Parameters[0].Type.ToDisplayString() == typeof(ThreadIds).FullName)
methodDeclarationSymbol.Parameters.Length == 0)
{
var parameterName = methodDeclarationSymbol.Parameters[0].Name;
processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!;
processedMethod = new ExecuteMethodRewriter().Visit(processedMethod)!;
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
}
+9
ファイルの表示
@@ -190,6 +190,15 @@ namespace ComputeSharp.SourceGenerators.Mappings
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Property.Name.ToLower()}");
}
// Programmatically load mappings for the dispatch types
foreach (var item in
from type in HlslKnownTypes.HlslDispatchTypes
from property in type.GetProperties(BindingFlags.Static | BindingFlags.Public)
select (Type: type, Property: property))
{
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Type.Name}.{item.Property.Name.ToLower()}");
}
return knownMembers;
}
+10 -6
ファイルの表示
@@ -41,8 +41,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
[typeof(double).FullName] = "double",
[typeof(Double2).FullName] = "double2",
[typeof(Double3).FullName] = "double3",
[typeof(Double4).FullName] = "double4",
[typeof(ThreadIds).FullName] = "int3"
[typeof(Double4).FullName] = "double4"
};
/// <summary>
@@ -50,7 +49,6 @@ namespace ComputeSharp.SourceGenerators.Mappings
/// </summary>
public static IReadOnlyCollection<Type> HlslMappedVectorTypes { get; } = new[]
{
typeof(ThreadIds),
typeof(Bool2), typeof(Bool3), typeof(Bool4),
typeof(Int2), typeof(Int3), typeof(Int4),
typeof(UInt2), typeof(UInt3), typeof(UInt4),
@@ -58,6 +56,14 @@ namespace ComputeSharp.SourceGenerators.Mappings
typeof(Double2), typeof(Double3), typeof(Double4)
};
/// <summary>
/// Gets the known HLSL dispatch types.
/// </summary>
public static IReadOnlyCollection<Type> HlslDispatchTypes { get; } = new[]
{
typeof(ThreadIds)
};
/// <summary>
/// Checks whether or not a given type name matches a typed resource type.
/// </summary>
@@ -94,9 +100,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
/// <returns>Whether or not <paramref name="typeName"/> represents a typed resource type.</returns>
public static bool IsScalarOrVectorType(string typeName)
{
return
KnownTypes.ContainsKey(typeName) &&
typeName != typeof(ThreadIds).FullName;
return KnownTypes.ContainsKey(typeName);
}
/// <summary>
+9 -23
ファイルの表示
@@ -10,20 +10,6 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// </summary>
internal sealed class ExecuteMethodRewriter : CSharpSyntaxRewriter
{
/// <summary>
/// The identifier name for the input <see langword="uint3"/> parameter.
/// </summary>
private readonly string threadIdsIdentifier;
/// <summary>
/// Creates a new <see cref="ExecuteMethodRewriter"/> instance with the specified parameters.
/// </summary>
/// <param name="threadIdsIdentifier">The identifier name for the input <see langword="uint3"/> parameter.</param>
public ExecuteMethodRewriter(string threadIdsIdentifier)
{
this.threadIdsIdentifier = threadIdsIdentifier;
}
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/>
public TNode? Visit<TNode>(TNode? node)
where TNode : SyntaxNode
@@ -32,11 +18,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
}
/// <inheritdoc/>
public override SyntaxNode VisitParameter(ParameterSyntax node)
public override SyntaxNode? VisitParameterList(ParameterListSyntax node)
{
var updatedNode = (ParameterSyntax)base.VisitParameter(node)!;
var updatedNode = (ParameterListSyntax)base.VisitParameterList(node)!;
return updatedNode.WithIdentifier(Identifier($"{updatedNode.Identifier.Text} : SV_DispatchThreadId"));
return updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID")));
}
/// <inheritdoc/>
@@ -48,9 +34,9 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
// check to ensure that invocation outside of the requested range are discarded.
// The following snippet creates this prologue before the user provided body:
//
// if (ids.X < __x &&
// ids.Y < __y &&
// ids.Z < __z)
// if (ThreadIds.x < __x &&
// ThreadIds.y < __y &&
// ThreadIds.z < __z)
// {
// <body>
// }
@@ -60,19 +46,19 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
BinaryExpression(SyntaxKind.LessThanExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(this.threadIdsIdentifier),
IdentifierName(nameof(ThreadIds)),
IdentifierName("x")),
IdentifierName("__x")),
BinaryExpression(SyntaxKind.LessThanExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(this.threadIdsIdentifier),
IdentifierName(nameof(ThreadIds)),
IdentifierName("y")),
IdentifierName("__y"))),
BinaryExpression(SyntaxKind.LessThanExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(this.threadIdsIdentifier),
IdentifierName(nameof(ThreadIds)),
IdentifierName("z")),
IdentifierName("__z")));