From 74793ab9f02f1f8abc6910d686dadb066e5d19d8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 Jan 2022 13:07:12 +0100 Subject: [PATCH] Add DispatchAxis type and new [EmbeddedBytecode] attribute --- .../ComputeSharp.SourceGenerators.csproj | 3 +- .../Attributes/EmbeddedBytecodeAttribute.cs | 39 ++++++++++- .../Core/Dispatch/DispatchAxis.cs | 69 +++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp/Core/Dispatch/DispatchAxis.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index c1dd63a2..bfd2581e 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -4,7 +4,7 @@ netstandard2.0 false ComputeSharp.SourceGenerators - $(NoWarn);CS1591;CS0649 + $(NoWarn);CS1591;CS0649;CS1574 @@ -20,6 +20,7 @@ + diff --git a/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs b/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs index f980a118..5211f07a 100644 --- a/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs +++ b/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs @@ -1,4 +1,7 @@ using System; +#if !SOURCE_GENERATOR +using Microsoft.Toolkit.Diagnostics; +#endif namespace ComputeSharp; @@ -8,11 +11,24 @@ namespace ComputeSharp; /// /// This attribute can be used to annotate shader types as follows: /// -/// [EmbeddedBytecode(8, 8, 1)] +/// // A compute shader that is dispatched on a target buffer +/// [EmbeddedBytecode(DispatchAxis.X)] /// struct MyShader : IComputeShader /// { /// } /// +/// Or similarly, for a pixel shader: +/// +/// // A pixel shader that is dispatched on a target texture +/// [EmbeddedBytecode(DispatchAxis.XY)] +/// struct MyShader : IPixelShader<float4> +/// { +/// } +/// +/// +/// +/// Using is an easier way to precompile shaders when dispatching them over known dimensions. For more +/// fine grained control over the thread size values when dispatching, use . /// /// /// @@ -32,6 +48,27 @@ namespace ComputeSharp; [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] public sealed class EmbeddedBytecodeAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The target dispatch axes for the shader to run. + public EmbeddedBytecodeAttribute(DispatchAxis dispatchAxis) + { +#if !SOURCE_GENERATOR + (ThreadsX, ThreadsY, ThreadsZ) = dispatchAxis switch + { + DispatchAxis.X => (64, 1, 1), + DispatchAxis.Y => (1, 64, 1), + DispatchAxis.Z => (1, 1, 64), + DispatchAxis.XY => (8, 8, 1), + DispatchAxis.XZ => (8, 1, 8), + DispatchAxis.YZ => (1, 8, 8), + DispatchAxis.XYZ => (4, 4, 4), + _ => ThrowHelper.ThrowArgumentException<(int, int, int)>(nameof(dispatchAxis), "Invalid dispatch axis value.") + }; +#endif + } + /// /// Creates a new instance with the specified parameters. /// diff --git a/src/ComputeSharp/Core/Dispatch/DispatchAxis.cs b/src/ComputeSharp/Core/Dispatch/DispatchAxis.cs new file mode 100644 index 00000000..b31e9184 --- /dev/null +++ b/src/ComputeSharp/Core/Dispatch/DispatchAxis.cs @@ -0,0 +1,69 @@ +namespace ComputeSharp; + +/// +/// A to be used with to indicate the dispatch +/// +public enum DispatchAxis +{ + /// + /// Indicates a shader dispatch only along the X axis. + /// + /// + /// This applies to using or . + /// + X, + + /// + /// Indicates a shader dispatch only along the Y axis. + /// + /// + /// This applies to using or , + /// but only as long as the input dispatch size on both the X and Z axes is 1. Using any other combination will not be able to leverage the precompiled shader bytecode. + /// + Y, + + /// + /// Indicates a shader dispatch only along the Z axis. + /// + /// + /// This applies to using or , + /// but only as long as the input dispatch size on both the X and Y axes is 1. Using any other combination will not be able to leverage the precompiled shader bytecode. + /// + Z, + + /// + /// Indicates a shader dispatch along the X and Y axes. + /// + /// + /// This applies to using , , + /// , , + /// or . + /// + XY, + + /// + /// Indicates a shader dispatch along the X and Z axes. + /// + /// + /// This applies to using or , + /// but only as long as the input dispatch size on the Y axis 1. Using any other combination will not be able to leverage the precompiled shader bytecode. + /// + XZ, + + /// + /// Indicates a shader dispatch along the Y and Z axes. + /// + /// + /// This applies to using or , + /// but only as long as the input dispatch size on the X axis 1. Using any other combination will not be able to leverage the precompiled shader bytecode. + /// + YZ, + + /// + /// Indicates a shader dispatch along the X, Y and Z axes. + /// + /// + /// This applies to using or . + /// + XYZ +}