Added initial IComputeShaderHashCodeGenerator type
このコミットが含まれているのは:
1 親
0f129b2f8b
コミット
e6a1fbc0ba
2ファイル変更
+94
-1
検索条件に一致するファイルはありません
@@ -0,0 +1,93 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators
|
||||
{
|
||||
[Generator]
|
||||
public class IComputeShaderHashCodeGenerator : ISourceGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
// Find all the struct declarations
|
||||
ImmutableArray<StructDeclarationSyntax> structDeclarations = (
|
||||
from tree in context.Compilation.SyntaxTrees
|
||||
from structDeclaration in tree.GetRoot().DescendantNodes().OfType<StructDeclarationSyntax>()
|
||||
select structDeclaration).ToImmutableArray();
|
||||
|
||||
foreach (StructDeclarationSyntax structDeclaration in structDeclarations)
|
||||
{
|
||||
SemanticModel semanticModel = context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree);
|
||||
INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!;
|
||||
|
||||
// Only process compute shader types
|
||||
if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
|
||||
|
||||
TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString());
|
||||
BlockSyntax block = Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, ParseToken("0"))));
|
||||
|
||||
// Create a static method to create the combined hashcode for a given shader type.
|
||||
// This code takes a block syntax and produces a compilation unit as follows:
|
||||
//
|
||||
// using System;
|
||||
// using System.ComponentModel;
|
||||
//
|
||||
// namespace ComputeSharp.__Internals
|
||||
// {
|
||||
// internal static partial class HashCodeProvider
|
||||
// {
|
||||
// [System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
|
||||
// [System.Obsolete("This method is intended for internal usage only")]
|
||||
// public static int CombineHashCode(int hash, in ShaderType shader)
|
||||
// {
|
||||
// return ...;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
var source =
|
||||
CompilationUnit().AddUsings(
|
||||
UsingDirective(IdentifierName("System")),
|
||||
UsingDirective(IdentifierName("System.ComponentModel"))).AddMembers(
|
||||
NamespaceDeclaration(IdentifierName("ComputeSharp.__Internals")).AddMembers(
|
||||
ClassDeclaration("HashCodeProvider").AddModifiers(
|
||||
Token(SyntaxKind.InternalKeyword),
|
||||
Token(SyntaxKind.StaticKeyword),
|
||||
Token(SyntaxKind.PartialKeyword)).AddMembers(
|
||||
MethodDeclaration(
|
||||
PredefinedType(Token(SyntaxKind.IntKeyword)),
|
||||
Identifier("CombineHash")).AddAttributeLists(
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName("EditorBrowsable")).AddArgumentListArguments(
|
||||
AttributeArgument(ParseExpression("EditorBrowsableState.Never"))))),
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName("Obsolete")).AddArgumentListArguments(
|
||||
AttributeArgument(LiteralExpression(
|
||||
SyntaxKind.StringLiteralExpression,
|
||||
Literal("This method is intended for internal usage only"))))))).AddModifiers(
|
||||
Token(SyntaxKind.PublicKeyword),
|
||||
Token(SyntaxKind.StaticKeyword)).AddParameterListParameters(
|
||||
Parameter(Identifier("hash")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
|
||||
Parameter(Identifier("shader")).WithModifiers(TokenList(Token(SyntaxKind.InKeyword))).WithType(shaderType))
|
||||
.WithBody(block))))
|
||||
.NormalizeWhitespace()
|
||||
.ToFullString();
|
||||
|
||||
// Add the method source attribute
|
||||
context.AddSource(structDeclarationSymbol.GetGeneratedFileName<IComputeShaderHashCodeGenerator>(), SourceText.From(source, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする