From 2b2ec28597ec071a0a3f4c999fde2dfb7cb7858f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 28 Dec 2020 19:38:41 +0100 Subject: [PATCH] Implemented initial dispatch data loader SG logic --- .../IComputeShaderDataLoaderGenerator.cs | 32 +++++++++++++++++-- .../IComputeShaderHashCodeGenerator.cs | 1 - .../Mappings/HlslKnownSizes.cs | 16 ++++++++-- .../Mappings/HlslKnownTypes.cs | 12 +++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 321a72c7..c911e3f7 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -2,6 +2,7 @@ using System.Collections.Immutable; using System.Diagnostics.Contracts; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using ComputeSharp.SourceGenerators.Extensions; using ComputeSharp.SourceGenerators.Mappings; @@ -39,8 +40,34 @@ namespace ComputeSharp.SourceGenerators if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString()); - BlockSyntax block = Block(); + int + resourceOffset = 0, + rawDataOffset = 0; + + foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) + { + INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; + string typeName = typeSymbol.GetFullMetadataName(); + + if (HlslKnownTypes.IsTypedResourceType(typeName)) + { + block = block.AddStatements(ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + ParseExpression($"Unsafe.Add(ref r0, {resourceOffset++})"), + ParseExpression($"GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle(shader.{fieldSymbol.Name}, device)")))); + } + else if (HlslKnownTypes.IsScalarOrVectorType(typeName)) + { + var info = HlslKnownSizes.GetTypeInfo(typeName); + + block = block.AddStatements(ExpressionStatement( + ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})"))); + + rawDataOffset += info.Size; + } + } // 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: @@ -63,7 +90,8 @@ namespace ComputeSharp.SourceGenerators var source = CompilationUnit().AddUsings( UsingDirective(IdentifierName("System")), - UsingDirective(IdentifierName("System.ComponentModel"))).AddMembers( + UsingDirective(IdentifierName("System.ComponentModel")), + UsingDirective(IdentifierName("System.Runtime.CompilerServices"))).AddMembers( NamespaceDeclaration(IdentifierName("ComputeSharp.__Internals")).AddMembers( ClassDeclaration("DispatchDataLoader").AddModifiers( Token(SyntaxKind.InternalKeyword), diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs index 11c178f6..bd2fbc6e 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs @@ -39,7 +39,6 @@ namespace ComputeSharp.SourceGenerators if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString()); - BlockSyntax block = Block(); // Process all the captured delegate types diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs index 0736932b..e05ff7a8 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Numerics; namespace ComputeSharp.SourceGenerators.Mappings @@ -9,9 +10,9 @@ namespace ComputeSharp.SourceGenerators.Mappings internal static class HlslKnownSizes { /// - /// Gets the mapping of supported known sizes to HLSL type names. + /// The mapping of supported known sizes to HLSL type names. /// - public static IReadOnlyDictionary KnownSizes { get; } = new Dictionary + private static IReadOnlyDictionary KnownSizes = new Dictionary { [typeof(bool).FullName] = (4, 4), [typeof(int).FullName] = (4, 4), @@ -43,5 +44,16 @@ namespace ComputeSharp.SourceGenerators.Mappings [typeof(Double3).FullName] = (24, 8), [typeof(Double3).FullName] = (32, 8) }; + + /// + /// Gets the size and alignment info for a specified primitive type. + /// + /// The type name to get info for. + /// The size and and alignment for the input type. + [Pure] + public static (int Size, int Pack) GetTypeInfo(string typeName) + { + return KnownSizes[typeName]; + } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index b4332b8e..43ba20b4 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -78,6 +78,18 @@ namespace ComputeSharp.SourceGenerators.Mappings }; } + /// + /// Checks whether or not a given type name matches a typed resource type. + /// + /// The input type name to check. + /// Whether or not represents a typed resource type. + public static bool IsScalarOrVectorType(string typeName) + { + return + KnownTypes.ContainsKey(typeName) && + typeName != typeof(ThreadIds).FullName; + } + /// /// Gets the mapped HLSL-compatible type name for the input type symbol. ///