From 94a1f85415811464a9bb3bac7b8b9eeff6a773fe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 Jun 2024 19:32:00 -0700 Subject: [PATCH] Don't rewrite 'ConstantBuffer.Length' --- .../SyntaxRewriters/ShaderSourceRewriter.cs | 8 ++++++++ .../Mappings/HlslKnownProperties.cs | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 8fea6fea..685435fa 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -451,6 +451,14 @@ internal sealed partial class ShaderSourceRewriter( if (!this.staticMethods.TryGetValue(key, out MethodDeclarationSyntax? methodSyntax)) { INamedTypeSymbol resourceType = (INamedTypeSymbol)SemanticModel.For(node).GetTypeInfo(node.Expression, CancellationToken).Type!; + + // Explicitly ignore 'ConstantBuffer.Length', as that is not valid in HLSL. + // The type should just be reworked entirely at some point, but not today. + if (resourceType.Name == "ConstantBuffer") + { + return updatedNode; + } + string resourceName = HlslKnownTypes.GetMappedName(resourceType); // Create a static method to get a specified dimension for a target resource type. diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs index c3ff1ab2..0dd34a5e 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs @@ -35,6 +35,10 @@ partial class HlslKnownProperties /// private static partial Dictionary BuildKnownSizeAccessors() { + // For 'Buffer`1', which is used by the '[RW]StructuredBuffer' types in HLSL, the rank is + // 2 because 'GetDimensions' still has two parameters, even if the actual rank of the array + // is just 1. That is because the length is the first output, and the second is the stride. + // The latter will always be ignored by the generated code. return new() { ["ComputeSharp.Resources.Buffer`1.Length"] = (2, 0),