diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs index 3902eea9..bdb9ea37 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs @@ -1,4 +1,3 @@ -using System; using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; @@ -93,11 +92,11 @@ partial class D2DPixelShaderDescriptorGenerator return; } - usingDirectives.Add("global::System.CodeDom.Compiler"); - usingDirectives.Add("global::System.Diagnostics"); - usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); - usingDirectives.Add("global::System.Runtime.CompilerServices"); - usingDirectives.Add("global::System.Runtime.InteropServices"); + _ = usingDirectives.Add("global::System.CodeDom.Compiler"); + _ = usingDirectives.Add("global::System.Diagnostics"); + _ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); + _ = usingDirectives.Add("global::System.Runtime.CompilerServices"); + _ = usingDirectives.Add("global::System.Runtime.InteropServices"); // Declare the ConstantBuffer type static void ConstantBufferCallback(D2D1ShaderInfo info, IndentedTextWriter writer) @@ -121,27 +120,27 @@ partial class D2DPixelShaderDescriptorGenerator case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive: // Append a field as a global::ComputeSharp.Bool value (will use the implicit conversion from bool values) - writer.WriteLine($"""/// """); + writer.WriteLine($"""/// path.Name))}"/>"""); writer.WriteLine($"""[FieldOffset({primitive.Offset})]"""); - writer.WriteLine($"""public global::ComputeSharp.Bool {string.Join("_", primitive.FieldPath)};"""); + writer.WriteLine($"""public global::ComputeSharp.Bool {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};"""); break; case FieldInfo.Primitive primitive: // Append primitive fields of other types with their mapped names - writer.WriteLine($"""/// """); + writer.WriteLine($"""/// path.Name))}"/>"""); writer.WriteLine($"""[FieldOffset({primitive.Offset})]"""); - writer.WriteLine($"""public {HlslKnownTypes.GetMappedName(primitive.TypeName)} {string.Join("_", primitive.FieldPath)};"""); + writer.WriteLine($"""public {HlslKnownTypes.GetMappedName(primitive.TypeName)} {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};"""); break; case FieldInfo.NonLinearMatrix matrix: string rowTypeName = HlslKnownTypes.GetMappedName($"ComputeSharp.{matrix.ElementName}{matrix.Columns}"); - string fieldNamePrefix = string.Join("_", matrix.FieldPath); + string fieldNamePrefix = string.Join("_", matrix.FieldPath.Select(static path => path.Name)); // Declare a field for every row of the matrix type for (int j = 0; j < matrix.Rows; j++) { writer.WriteLineIf(j > 0); - writer.WriteLine($"""/// Row {j} of ."""); + writer.WriteLine($"""/// Row {j} of path.Name))}"/>."""); writer.WriteLine($"""[FieldOffset({matrix.Offsets[j]})]"""); writer.WriteLine($"""public {rowTypeName} {fieldNamePrefix}_{j};"""); } @@ -186,22 +185,20 @@ partial class D2DPixelShaderDescriptorGenerator switch (fieldInfo) { case FieldInfo.Primitive primitive: - string fieldName = primitive.FieldPath[0]; // Read a nested primitive value - writer.WriteLine($"{fieldName}(ref shader){string.Join(".", primitive.FieldPath.Skip(1))} = buffer.{string.Join("_", primitive.FieldPath)};"); + writer.WriteLine($"{GetFieldAccessExpression(fieldInfo, true)} = buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))};"); break; case FieldInfo.NonLinearMatrix matrix: string primitiveTypeName = matrix.ElementName.ToLowerInvariant(); string matrixHlslTypeName = $"{primitiveTypeName}{matrix.Rows}x{matrix.Columns}"; string rowTypeName = HlslKnownTypes.GetMappedName($"ComputeSharp.{matrix.ElementName}{matrix.Columns}"); - string fieldPath = string.Join(".", matrix.FieldPath); - string fieldNamePrefix = string.Join("_", matrix.FieldPath); + string fieldNamePrefix = string.Join("_", matrix.FieldPath.Select(static path => path.Name)); // Get a reference to the whole matrix field, just once for all rows writer.WriteLine(skipIfPresent: true); - writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {matrix.FieldPath[0]}(ref shader){string.Join(".", matrix.FieldPath.Skip(1))};"); + writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {GetFieldAccessExpression(fieldInfo, true)};"); writer.WriteLine(); // Read all rows of a given matrix type @@ -240,19 +237,18 @@ partial class D2DPixelShaderDescriptorGenerator case FieldInfo.Primitive primitive: // Assign a primitive value - writer.WriteLine($"buffer.{string.Join("_", primitive.FieldPath)} = {primitive.FieldPath[0]}(in shader){string.Join(".", primitive.FieldPath.Skip(1))};"); + writer.WriteLine($"buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))} = {GetFieldAccessExpression(fieldInfo, false)};"); break; case FieldInfo.NonLinearMatrix matrix: string primitiveTypeName = matrix.ElementName.ToLowerInvariant(); string matrixHlslTypeName = $"{primitiveTypeName}{matrix.Rows}x{matrix.Columns}"; string rowTypeName = HlslKnownTypes.GetMappedName($"ComputeSharp.{matrix.ElementName}{matrix.Columns}"); - string fieldPath = string.Join(".", matrix.FieldPath); - string fieldNamePrefix = string.Join("_", matrix.FieldPath); + string fieldNamePrefix = string.Join("_", matrix.FieldPath.Select(static path => path.Name)); // Get a reference to the whole matrix field, just once for all rows writer.WriteLine(skipIfPresent: true); - writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {matrix.FieldPath[0]}(in shader){string.Join(".", matrix.FieldPath.Skip(1))};"); + writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {GetFieldAccessExpression(fieldInfo, true)};"); writer.WriteLine(); // Assign all rows of a given matrix type @@ -267,36 +263,55 @@ partial class D2DPixelShaderDescriptorGenerator } } - using ImmutableArrayBuilder topLevelFieldNames = new(); + using ImmutableHashSetBuilder<(string ContainerType, string FieldName)> generatedAccessors = new(); // Define all field accessors foreach (FieldInfo fieldInfo in info.Fields) { - string fieldName = fieldInfo.FieldPath[0]; - - // Only generate accessors for top level fields, just once. That is, if they have - // multiple nested items, only generate one accessor for the top level field. We - // can simply use a linear scan here, as we expect fields to not be that many. - if (topLevelFieldNames.WrittenSpan.IndexOf(fieldName) >= 0) + for (int i = 0; i < fieldInfo.FieldPath.Length; i++) { - continue; + FieldPathPart pathPart = fieldInfo.FieldPath[i]; + + // Only generate field accessors for fields that are not accessible + if (pathPart.IsAccessible) + { + continue; + } + + // Get the friendly type name for the field: + // - If the part path is a nested struct, the type name is embedded + // - Otherwise, get the correct HLSL mapped type name for the field + string typeName = pathPart switch + { + FieldPathPart.Nested nested => nested.TypeName, + FieldPathPart.Leaf when fieldInfo.TypeName is "System.Boolean" => "global::ComputeSharp.Bool", + _ => HlslKnownTypes.GetMappedName(fieldInfo.TypeName) + }; + + // Get the friendly type name for the field. If this nested field is the + // first path, then the parent is the shader type itself. Otherwise, the + // container path is the type of the previous containing type of the field. + string containingTypeName = i == 0 + ? fullyQualifiedTypeName + : ((FieldPathPart.Nested)fieldInfo.FieldPath[i - 1]).TypeName; + + // Make sure to skip repeated field accessors. It's possible for some accessors to end up + // being required, in case a shader type had multiple fields of the same nested struct type. + // To avoid so, we just add the containing type and name to an hashset that we update. + if (!generatedAccessors.Add((containingTypeName, pathPart.Name))) + { + continue; + } + + writer.WriteLine(); + writer.WriteLine($""" + /// + /// The input shader instance. + /// A mutable reference to . + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{pathPart.Name}")] + private static extern ref readonly {typeName} {pathPart.Name}(ref readonly {containingTypeName} shader); + """, isMultiline: true); } - - topLevelFieldNames.Add(fieldName); - - // Get the friendly type name for the field - string typeName = fieldInfo.TypeName is "System.Boolean" - ? "global::ComputeSharp.Bool" - : HlslKnownTypes.GetMappedName(fieldInfo.TypeName); - - writer.WriteLine(); - writer.WriteLine($""" - /// - /// The input shader instance. - /// A mutable reference to . - [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{fieldName}")] - private static extern ref {typeName} {fieldName}(ref readonly {fullyQualifiedTypeName} shader); - """, isMultiline: true); } } } @@ -304,5 +319,34 @@ partial class D2DPixelShaderDescriptorGenerator callbacks.Add(ConstantBufferCallback); callbacks.Add(ConstantBufferMarshallerCallback); } + + /// + /// Gets an expression to access a given field. + /// + /// The input field to generate the expression for. + /// Whether to return the expression as a mutable . + /// An expression to access a given field. + private static string GetFieldAccessExpression(FieldInfo fieldInfo, bool isRef) + { + string fieldExpression = "shader"; + + // Format the field access expression, there's two options: + // - If the field is accessible, just access it directly + // - If the field is not accessible, use the unsafe accessor + foreach (FieldPathPart part in fieldInfo.FieldPath) + { + fieldExpression = part.IsAccessible + ? $"{fieldExpression}.{part.Name}" + : $"{part.Name}(in {fieldExpression})"; + } + + // Return a mutable ref if needed (the expression is always readonly here) + if (isRef) + { + return $"Unsafe.AsRef(in {fieldExpression})"; + } + + return fieldExpression; + } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs index 3edb5c57..6133ee9d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs @@ -39,7 +39,7 @@ partial class D2DPixelShaderDescriptorGenerator static IEnumerable GetCapturedFieldInfos( Compilation compilation, ITypeSymbol currentTypeSymbol, - ImmutableArray fieldPath, + ImmutableArray fieldPath, StrongBox rawDataOffset) { bool isFirstField = true; @@ -58,6 +58,7 @@ partial class D2DPixelShaderDescriptorGenerator string fieldName = fieldSymbol.Name; string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName(); + bool isAccessible = fieldSymbol.IsAccessibleFromCompilationAssembly(compilation); // The first item in each nested struct needs to be aligned to 16 bytes if (isFirstField && fieldPath.Length > 0) @@ -69,12 +70,17 @@ partial class D2DPixelShaderDescriptorGenerator if (HlslKnownTypes.IsKnownHlslType(typeName)) { - yield return GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset.Value); + ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName, isAccessible)); + + yield return GetHlslKnownTypeFieldInfo(nestedFieldPath, typeName, ref rawDataOffset.Value); } else if (fieldSymbol.Type.IsUnmanagedType) { + string nestedTypeName = fieldSymbol.Type.GetFullyQualifiedName(includeGlobal: true); + ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Nested(fieldName, isAccessible, nestedTypeName)); + // Custom struct type defined by the user - foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(compilation, fieldSymbol.Type, fieldPath.Add(fieldName), rawDataOffset)) + foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(compilation, fieldSymbol.Type, nestedFieldPath, rawDataOffset)) { yield return fieldInfo; } @@ -89,7 +95,7 @@ partial class D2DPixelShaderDescriptorGenerator ImmutableArray fieldInfos = GetCapturedFieldInfos( compilation, structDeclarationSymbol, - ImmutableArray.Empty, + ImmutableArray.Empty, rawDataOffsetAsBox).ToImmutableArray(); constantBufferSizeInBytes = rawDataOffsetAsBox.Value; @@ -114,7 +120,7 @@ partial class D2DPixelShaderDescriptorGenerator /// The type name currently being read. /// The current offset within the loaded data buffer. private static FieldInfo GetHlslKnownTypeFieldInfo( - ImmutableArray fieldPath, + ImmutableArray fieldPath, string typeName, ref int rawDataOffset) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs index fe88365a..836ddd60 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs @@ -85,13 +85,13 @@ partial class D2DPixelShaderDescriptorGenerator return; } - usingDirectives.Add("global::System"); - usingDirectives.Add("global::System.Buffers"); - usingDirectives.Add("global::System.CodeDom.Compiler"); - usingDirectives.Add("global::System.Diagnostics"); - usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); - usingDirectives.Add("global::System.Runtime.CompilerServices"); - usingDirectives.Add("global::System.Runtime.InteropServices"); + _ = usingDirectives.Add("global::System"); + _ = usingDirectives.Add("global::System.Buffers"); + _ = usingDirectives.Add("global::System.CodeDom.Compiler"); + _ = usingDirectives.Add("global::System.Diagnostics"); + _ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); + _ = usingDirectives.Add("global::System.Runtime.CompilerServices"); + _ = usingDirectives.Add("global::System.Runtime.InteropServices"); // Declare the HlslBytecodeMemoryManager custom memory manager type static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs index ce1f1096..ca098146 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs @@ -53,9 +53,9 @@ partial class D2DPixelShaderDescriptorGenerator return; } - usingDirectives.Add("global::System.CodeDom.Compiler"); - usingDirectives.Add("global::System.Diagnostics"); - usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); + _ = usingDirectives.Add("global::System.CodeDom.Compiler"); + _ = usingDirectives.Add("global::System.Diagnostics"); + _ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); AddAdditionalDataMemberUsingDirectives(info, usingDirectives); ResourceTextureDescriptions.AddAdditionalDataMemberUsingDirectives(info, usingDirectives); @@ -103,8 +103,8 @@ partial class D2DPixelShaderDescriptorGenerator return; } - usingDirectives.Add("global::ComputeSharp.D2D1"); - usingDirectives.Add("global::ComputeSharp.D2D1.Interop"); + _ = usingDirectives.Add("global::ComputeSharp.D2D1"); + _ = usingDirectives.Add("global::ComputeSharp.D2D1.Interop"); } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs index 0829cdc5..5ed34b34 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs @@ -50,14 +50,14 @@ partial class D2DPixelShaderDescriptorGenerator return; } - usingDirectives.Add("global::System"); - usingDirectives.Add("global::System.Buffers"); - usingDirectives.Add("global::System.CodeDom.Compiler"); - usingDirectives.Add("global::System.Diagnostics"); - usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); - usingDirectives.Add("global::System.Runtime.CompilerServices"); - usingDirectives.Add("global::System.Runtime.InteropServices"); - usingDirectives.Add("global::ComputeSharp.D2D1.Interop"); + _ = usingDirectives.Add("global::System"); + _ = usingDirectives.Add("global::System.Buffers"); + _ = usingDirectives.Add("global::System.CodeDom.Compiler"); + _ = usingDirectives.Add("global::System.Diagnostics"); + _ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); + _ = usingDirectives.Add("global::System.Runtime.CompilerServices"); + _ = usingDirectives.Add("global::System.Runtime.InteropServices"); + _ = usingDirectives.Add("global::ComputeSharp.D2D1.Interop"); // Declare the InputTypesMemoryManager custom memory manager type static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs index 1ec6ee18..218a00ae 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs @@ -47,7 +47,7 @@ partial class D2DPixelShaderDescriptorGenerator return; } - usingDirectives.Add("global::ComputeSharp.D2D1.Interop"); + _ = usingDirectives.Add("global::ComputeSharp.D2D1.Interop"); } /// diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs index 10da8292..db30dd33 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs @@ -7,7 +7,7 @@ namespace ComputeSharp.SourceGeneration.Models; /// /// The path of the field with respect to the shader instance. /// The full metadata name for the primitive type. -internal abstract record FieldInfo(EquatableArray FieldPath, string TypeName) +internal abstract record FieldInfo(EquatableArray FieldPath, string TypeName) { /// /// A captured primitive value (either a scalar, a vector, or a linear matrix). @@ -15,7 +15,7 @@ internal abstract record FieldInfo(EquatableArray FieldPath, string Type /// /// /// The byte offset of the value within the root signature. - public sealed record Primitive(EquatableArray FieldPath, string TypeName, int Offset) : FieldInfo(FieldPath, TypeName); + public sealed record Primitive(EquatableArray FieldPath, string TypeName, int Offset) : FieldInfo(FieldPath, TypeName); /// /// A captured non linear matrix value. @@ -27,10 +27,33 @@ internal abstract record FieldInfo(EquatableArray FieldPath, string Type /// The number of columns in the matrix. /// The sequence of byte offsets for each row within the root signature. public sealed record NonLinearMatrix( - EquatableArray FieldPath, + EquatableArray FieldPath, string TypeName, string ElementName, int Rows, int Columns, EquatableArray Offsets) : FieldInfo(FieldPath, TypeName); +} + +/// +/// A model for a part of a field path, ie. the name of the immediate parent field, and whether it's accessible. +/// +/// The name of the immediate parent field to access (or self). +/// Whether the current path part is accessible from the compilation. +internal abstract record FieldPathPart(string Name, bool IsAccessible) +{ + /// + /// A model for a leaf part of a field path. + /// + /// + /// + public sealed record Leaf(string Name, bool IsAccessible) : FieldPathPart(Name, IsAccessible); + + /// + /// A model for a nested field part path (ie. going through a nested struct type). + /// + /// + /// + /// The fully qualified type name of the containing type. + public sealed record Nested(string Name, bool IsAccessible, string TypeName) : FieldPathPart(Name, IsAccessible); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 52063552..a7c58ed1 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -49,10 +49,13 @@ internal static class ISymbolExtensions /// Gets the fully qualified name for a given symbol. /// /// The input instance. + /// Whether to include the global:: prefix. /// The fully qualified name for . - public static string GetFullyQualifiedName(this ISymbol symbol) + public static string GetFullyQualifiedName(this ISymbol symbol, bool includeGlobal = false) { - return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); + return includeGlobal + ? symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) + : symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); } /// diff --git a/src/ComputeSharp.SourceGeneration/Helpers/ImmutableHashSetBuilder{T}.cs b/src/ComputeSharp.SourceGeneration/Helpers/ImmutableHashSetBuilder{T}.cs index e96aa224..fe6a92a7 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/ImmutableHashSetBuilder{T}.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/ImmutableHashSetBuilder{T}.cs @@ -44,9 +44,9 @@ internal struct ImmutableHashSetBuilder : IDisposable } /// - public readonly void Add(T item) + public readonly bool Add(T item) { - _ = this.set!.Add(item); + return this.set!.Add(item); } /// diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs index 87ed9c4d..b3300ffd 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs @@ -1,3 +1,4 @@ +using System.Linq; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; @@ -44,19 +45,19 @@ partial class ComputeShaderDescriptorGenerator // Read a boolean value and cast it to Bool first, which will apply the correct size expansion writer.WriteLine( $"global::System.Runtime.CompilerServices.Unsafe.As(ref span[{primitive.Offset}]) = " + - $"(global::ComputeSharp.Bool)shader.{string.Join(".", primitive.FieldPath)};"); + $"(global::ComputeSharp.Bool)shader.{string.Join(".", primitive.FieldPath.Select(static path => path.Name))};"); break; case FieldInfo.Primitive primitive: // Read a primitive value and serialize it into the target buffer writer.WriteLine( $"global::System.Runtime.CompilerServices.Unsafe.As(ref span[{primitive.Offset}]) = " + - $"shader.{string.Join(".", primitive.FieldPath)};"); + $"shader.{string.Join(".", primitive.FieldPath.Select(static path => path.Name))};"); break; case FieldInfo.NonLinearMatrix matrix: string rowTypeName = $"global::ComputeSharp.{matrix.ElementName}{matrix.Columns}"; - string rowLocalName = $"__{string.Join("_", matrix.FieldPath)}__row0"; + string rowLocalName = $"__{string.Join("_", matrix.FieldPath.Select(static path => path.Name))}__row0"; // Declare a local to index into individual rows. This will generate: // @@ -64,7 +65,7 @@ partial class ComputeShaderDescriptorGenerator // ref global::System.Runtime.CompilerServices.Unsafe.AsRef(in )); writer.WriteLine( $"ref {rowTypeName} {rowLocalName} = ref global::System.Runtime.CompilerServices.Unsafe.As(" + - $"ref global::System.Runtime.CompilerServices.Unsafe.AsRef(in shader.{string.Join(".", matrix.FieldPath)}));"); + $"ref global::System.Runtime.CompilerServices.Unsafe.AsRef(in shader.{string.Join(".", matrix.FieldPath.Select(static path => path.Name))}));"); // Generate the loading code for each individual row, with proper alignment. // This will result in the following (assuming Float2x3 m): diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs index c9c25e3f..acb1e7b2 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs @@ -43,7 +43,7 @@ partial class ComputeShaderDescriptorGenerator static void GetInfo( Compilation compilation, ITypeSymbol currentTypeSymbol, - ImmutableArray fieldPath, + ImmutableArray fieldPath, ref int resourceOffset, ref int rawDataOffset, ImmutableArrayBuilder fields, @@ -82,12 +82,13 @@ partial class ComputeShaderDescriptorGenerator } else if (HlslKnownTypes.IsKnownHlslType(typeName)) { - fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset)); + // TODO: share the logic and fix the field path parts (here they just always return accessible leaves for now) + fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(new FieldPathPart.Leaf(fieldName, true)), typeName, ref rawDataOffset)); } else if (fieldSymbol.Type.IsUnmanagedType) { // Custom struct type defined by the user - GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(fieldName), ref resourceOffset, ref rawDataOffset, fields, resources); + GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(new FieldPathPart.Leaf(fieldName, true)), ref resourceOffset, ref rawDataOffset, fields, resources); } } } @@ -104,7 +105,7 @@ partial class ComputeShaderDescriptorGenerator GetInfo( compilation, structDeclarationSymbol, - ImmutableArray.Empty, + ImmutableArray.Empty, ref resourceOffset, ref rawDataOffset, fieldBuilder, @@ -144,7 +145,7 @@ partial class ComputeShaderDescriptorGenerator /// The type name currently being read. /// The current offset within the loaded data buffer. private static FieldInfo GetHlslKnownTypeFieldInfo( - ImmutableArray fieldPath, + ImmutableArray fieldPath, string typeName, ref int rawDataOffset) { diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs index 15b1ee5c..c8a1a0ca 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs @@ -50,13 +50,13 @@ partial class ComputeShaderDescriptorGenerator return; } - usingDirectives.Add("global::System"); - usingDirectives.Add("global::System.Buffers"); - usingDirectives.Add("global::System.CodeDom.Compiler"); - usingDirectives.Add("global::System.Diagnostics"); - usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); - usingDirectives.Add("global::System.Runtime.CompilerServices"); - usingDirectives.Add("global::System.Runtime.InteropServices"); + _ = usingDirectives.Add("global::System"); + _ = usingDirectives.Add("global::System.Buffers"); + _ = usingDirectives.Add("global::System.CodeDom.Compiler"); + _ = usingDirectives.Add("global::System.Diagnostics"); + _ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); + _ = usingDirectives.Add("global::System.Runtime.CompilerServices"); + _ = usingDirectives.Add("global::System.Runtime.InteropServices"); // Declare the HlslBytecodeMemoryManager custom memory manager type static void Callback(ShaderInfo info, IndentedTextWriter writer) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs index 56e237c1..015c91a2 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs @@ -49,10 +49,10 @@ partial class ComputeShaderDescriptorGenerator return; } - usingDirectives.Add("global::System.CodeDom.Compiler"); - usingDirectives.Add("global::System.Diagnostics"); - usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); - usingDirectives.Add("global::ComputeSharp.Interop"); + _ = usingDirectives.Add("global::System.CodeDom.Compiler"); + _ = usingDirectives.Add("global::System.Diagnostics"); + _ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis"); + _ = usingDirectives.Add("global::ComputeSharp.Interop"); // Declare the additional data type and the shared array with resource descriptor ranges static void Callback(ShaderInfo info, IndentedTextWriter writer) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 15524477..bf40275e 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -314,6 +314,7 @@ public partial class D2D1PixelShaderTests float4 pixel6 = this.myTexture3.Sample(0.4f, 0.5f, 0.6f); float number1 = this.myTexture4[0, 2]; float number2 = this.myTexture4.Sample(0.4f, 0.6f); + float number3 = this.number; return 0; } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 91767537..b3001bc0 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -574,7 +574,7 @@ public partial class D2D1ResourceTextureManagerTests D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - CopyFromResourceTexture1DShader shader = new(width); + CopyFromResourceTexture1DShader shader = new(); D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader); @@ -626,8 +626,6 @@ public partial class D2D1ResourceTextureManagerTests [AutoConstructor] internal partial struct CopyFromResourceTexture1DShader : ID2D1PixelShader { - private int width; - [D2DResourceTextureIndex(0)] private D2D1ResourceTexture1D source; @@ -663,7 +661,7 @@ public partial class D2D1ResourceTextureManagerTests D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - CopyFromResourceTexture2DShader shader = new(width, height); + CopyFromResourceTexture2DShader shader = new(); D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader); @@ -735,7 +733,7 @@ public partial class D2D1ResourceTextureManagerTests D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - CopyFromResourceTexture2DShader shader = new(width, height); + CopyFromResourceTexture2DShader shader = new(); D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader); @@ -796,9 +794,6 @@ public partial class D2D1ResourceTextureManagerTests [AutoConstructor] internal partial struct CopyFromResourceTexture2DShader : ID2D1PixelShader { - private int width; - private int height; - [D2DResourceTextureIndex(0)] private D2D1ResourceTexture2D source; @@ -839,7 +834,7 @@ public partial class D2D1ResourceTextureManagerTests D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - CopyFromResourceTexture3DShader shader = new(width, height, depth); + CopyFromResourceTexture3DShader shader = new(height); D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader); @@ -903,9 +898,7 @@ public partial class D2D1ResourceTextureManagerTests [AutoConstructor] internal partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader { - private int width; private int height; - private int depth; [D2DResourceTextureIndex(0)] private D2D1ResourceTexture3D source;