diff --git a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems index f30f1996..ea973d4d 100644 --- a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems +++ b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.projitems @@ -9,6 +9,7 @@ ComputeSharp.Sample.Shared + \ No newline at end of file diff --git a/samples/ComputeSharp.Sample.Shared/Helpers/Formatting.cs b/samples/ComputeSharp.Sample.Shared/Helpers/Formatting.cs new file mode 100644 index 00000000..6fbe1b6e --- /dev/null +++ b/samples/ComputeSharp.Sample.Shared/Helpers/Formatting.cs @@ -0,0 +1,45 @@ +using System; +using System.Globalization; +using System.Linq; +using CommunityToolkit.Diagnostics; + +namespace ComputeSharp.Sample; + +/// +/// Internal helpers for the sample to format values. +/// +internal static class Formatting +{ + /// + /// Prints a matrix in a properly formatted way. + /// + /// The input array representing the matrix to print. + /// The width of the array to print. + /// The height of the array to print. + /// The name of the matrix to print. + public static void PrintMatrix(float[] array, int width, int height, string name) + { + Guard.IsNotNull(array); + Guard.IsGreaterThan(width, 0); + Guard.IsGreaterThan(height, 0); + Guard.IsEqualTo(array.Length, (long)width * height, nameof(array)); + Guard.IsNotNullOrEmpty(name); + + int pad = 48 - name.Length; + string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}"; + + Console.WriteLine(title); + + int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4); + + for (int i = 0; i < height; i++) + { + var row = array.AsSpan(i * width, width).ToArray(); + var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth))); + + Console.WriteLine(text); + } + + Console.WriteLine(new string('=', 50)); + } +} diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index d52553d1..c1962f0a 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -1,70 +1,36 @@ using System; -using System.Globalization; using System.Linq; +using ComputeSharp; +using ComputeSharp.Sample; -namespace ComputeSharp.Sample; +float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); -partial class Program +// Create the graphics buffer +using ReadWriteBuffer gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array); + +// Run the shader +GraphicsDevice.Default.For(100, new MultiplyByTwo(gpuBuffer)); + +// Print the initial matrix +Formatting.PrintMatrix(array, 10, 10, "BEFORE"); + +// Get the data back +gpuBuffer.CopyTo(array); + +// Print the updated matrix +Formatting.PrintMatrix(array, 10, 10, "AFTER"); + +/// +/// The sample kernel that multiples all items by two. +/// +[AutoConstructor] +internal readonly partial struct MultiplyByTwo : IComputeShader { - static void Main() + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() { - float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); - - // Create the graphics buffer - using ReadWriteBuffer gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array); - - // Run the shader - GraphicsDevice.Default.For(100, new MainKernel(gpuBuffer)); - - // Print the initial matrix - PrintMatrix(array, 10, 10, "BEFORE"); - - // Get the data back - gpuBuffer.CopyTo(array); - - // Print the updated matrix - PrintMatrix(array, 10, 10, "AFTER"); - } - - /// - /// Kernel for . - /// - [AutoConstructor] - internal readonly partial struct MainKernel : IComputeShader - { - public readonly ReadWriteBuffer buffer; - - /// - public void Execute() - { - buffer[ThreadIds.X] *= 2; - } - } - - /// - /// Prints a matrix in a properly formatted way. - /// - /// The input array representing the matrix to print. - /// The width of the array to print. - /// The height of the array to print. - /// The name of the matrix to print. - private static void PrintMatrix(float[] array, int width, int height, string name) - { - int pad = 48 - name.Length; - string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}"; - - Console.WriteLine(title); - - int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4); - - for (int i = 0; i < height; i++) - { - var row = array.AsSpan(i * width, width).ToArray(); - var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth))); - - Console.WriteLine(text); - } - - Console.WriteLine(new string('=', 50)); + buffer[ThreadIds.X] *= 2; } } diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs index f04c3e04..7c3422f8 100644 --- a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs @@ -129,6 +129,24 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator .AddMembers(typeDeclarationSyntax); } + if (hierarchyInfo.Namespace is "") + { + // If there is no namespace, attach the pragma directly to the declared type, + // and skip the namespace declaration. This will produce code as follows: + // + // #pragma warning disable + // + // + return + CompilationUnit().AddMembers( + typeDeclarationSyntax + .WithModifiers(TokenList(Token( + TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), + SyntaxKind.PartialKeyword, + TriviaList())))) + .NormalizeWhitespace(eol: "\n"); + } + // Create the compilation unit with disabled warnings, target namespace and generated type. // This will produce code as follows: // diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs index 4f19441b..0974e9b6 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -43,8 +43,10 @@ internal static class ISymbolExtensions INamespaceSymbol ns when ns.IsGlobalNamespace => builder, INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false } => BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")), - ITypeSymbol ts when ts.ContainingType is ISymbol pt => BuildFrom(pt, builder.Insert(0, $"+{ts.MetadataName}")), - ITypeSymbol ts when ts.ContainingNamespace is ISymbol pn => BuildFrom(pn, builder.Insert(0, $".{ts.MetadataName}")), + ITypeSymbol ts when ts.ContainingType is ISymbol containingType + => BuildFrom(containingType, builder.Insert(0, $"+{ts.MetadataName}")), + ITypeSymbol ts when ts.ContainingNamespace is ISymbol containingNamespace and not INamespaceSymbol { IsGlobalNamespace: true } + => BuildFrom(containingNamespace, builder.Insert(0, $".{ts.MetadataName}")), ISymbol => BuildFrom(symbol.ContainingSymbol, builder.Insert(0, symbol.MetadataName)), _ => builder }; diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 4a869b46..1352edac 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -102,6 +102,25 @@ public sealed partial class IShaderGenerator .AddMembers(typeDeclarationSyntax); } + if (hierarchyInfo.Namespace is "") + { + // If there is no namespace, attach the pragma directly to the declared type, + // and skip the namespace declaration. This is the same additional path also + // used when generating custom constructors. This will produce code as follows: + // + // #pragma warning disable + // + // + return + CompilationUnit().AddMembers( + typeDeclarationSyntax + .WithModifiers(TokenList(Token( + TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), + SyntaxKind.PartialKeyword, + TriviaList())))) + .NormalizeWhitespace(eol: "\n"); + } + // Create the compilation unit with disabled warnings, target namespace and generated type. // This will produce code as follows: //