Merge pull request #187 from Sergio0694/feature/default-wavefront-size

Add DispatchAxis option for [EmbeddedBytecode]
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-01-05 14:55:43 +01:00
コミット 1477e50c01
20ファイル変更
+295 -28

検索条件に一致するファイルはありません

+1 -1
ファイルの表示
@@ -8,7 +8,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct ColorfulInfinity : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct ContouredLayers : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct ExtrudedTruchetPattern : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -8,7 +8,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct FourColorGradient : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -8,7 +8,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct FractalTiling : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -6,7 +6,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct HelloWorld : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct MengerJourney : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct Octagrams : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -8,7 +8,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct ProteanClouds : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -8,7 +8,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct PyramidPattern : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct TriangleGridContouring : IPixelShader<float4>
{
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@
/// </summary>
[AutoConstructor]
#if SAMPLE_APP
[EmbeddedBytecode(8, 8, 1)]
[EmbeddedBytecode(DispatchAxis.XY)]
#endif
internal readonly partial struct TwoTiledTruchet : IPixelShader<float4>
{
+1
ファイルの表示
@@ -54,3 +54,4 @@ CMPS0044 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentatio
CMPS0045 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0046 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0047 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0048 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+2 -1
ファイルの表示
@@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<PackSymbols>false</PackSymbols>
<Description>ComputeSharp.SourceGenerators</Description>
<NoWarn>$(NoWarn);CS1591;CS0649</NoWarn>
<NoWarn>$(NoWarn);CS1591;CS0649;CS1574</NoWarn>
</PropertyGroup>
<PropertyGroup>
@@ -20,6 +20,7 @@
<Compile Include="..\ComputeSharp\Core\Attributes\GroupSharedAttribute.cs" Link="ComputeSharp\Core\Attributes\GroupSharedAttribute.cs" />
<Compile Include="..\ComputeSharp.Dynamic\Attributes\ShaderMethodAttribute.cs" Link="ComputeSharp\Core\Attributes\ShaderMethodAttribute.cs" />
<Compile Include="..\ComputeSharp.Dynamic\Attributes\__Internals\ShaderMethodSourceAttribute.cs" Link="ComputeSharp\Core\Attributes\__Internals\ShaderMethodSourceAttribute.cs" />
<Compile Include="..\ComputeSharp\Core\Dispatch\DispatchAxis.cs" Link="ComputeSharp\Core\Dispatch\DispatchAxis.cs" />
<Compile Include="..\ComputeSharp\Core\Dispatch\DispatchSize.cs" Link="ComputeSharp\Core\Dispatch\DispatchSize.cs" />
<Compile Include="..\ComputeSharp\Core\Dispatch\GridIds.cs" Link="ComputeSharp\Core\Dispatch\GridIds.cs" />
<Compile Include="..\ComputeSharp\Core\Dispatch\GroupIds.cs" Link="ComputeSharp\Core\Dispatch\GroupIds.cs" />
+16
ファイルの表示
@@ -662,4 +662,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "When dynamic shader compilation is not supported (ie. when ComputeSharp.Dynamic is not referenced), all shaders need to be annotated as having embedded bytecode (using the [EmbeddedBytecode] attribute).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an embedded bytecode shader with an invalid dispatch axis value.
/// <para>
/// Format: <c>"The shader of type {0} is annotated with an invalid dispatch axis value"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidEmbeddedBytecodeDispatchAxis = new(
id: "CMPS0048",
title: "Invalid dispatch axis for shader with embedded bytecode",
messageFormat: "The shader of type {0} is annotated with with an invalid dispatch axis value",
category: typeof(IShaderGenerator).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The dispatch axis value for a shader marked as embedded bytecode have to be valid (flags are not supported).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+46 -8
ファイルの表示
@@ -59,21 +59,59 @@ public sealed partial class IShaderGenerator
return new(true, 0, 0, 0);
}
// Validate the thread number arguments
if (attribute.ConstructorArguments.Length != 3 ||
attribute.ConstructorArguments[0].Value is not int threadsX ||
attribute.ConstructorArguments[1].Value is not int threadsY ||
attribute.ConstructorArguments[2].Value is not int threadsZ ||
threadsX is < 1 or > 1024 ||
threadsY is < 1 or > 1024 ||
threadsZ is < 1 or > 64)
int threadsX;
int threadsY;
int threadsZ;
// Check for a dispatch axis argument first
if (attribute.ConstructorArguments.Length == 1)
{
int dispatchAxis = (int)attribute.ConstructorArguments[0].Value!;
(threadsX, threadsY, threadsZ) = (DispatchAxis)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),
_ => (0, 0, 0)
};
// Validate the dispatch axis argument
if ((threadsX, threadsY, threadsZ) is (0, 0, 0))
{
builder.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol);
diagnostics = builder.ToImmutable();
return new(true, 0, 0, 0);
}
}
else if (
attribute.ConstructorArguments.Length != 3 ||
attribute.ConstructorArguments[0].Value is not int explicitThreadsX ||
attribute.ConstructorArguments[1].Value is not int explicitThreadsY ||
attribute.ConstructorArguments[2].Value is not int explicitThreadsZ ||
explicitThreadsX is < 1 or > 1024 ||
explicitThreadsY is < 1 or > 1024 ||
explicitThreadsZ is < 1 or > 64)
{
// Failed to validate the thread number arguments
builder.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol);
diagnostics = builder.ToImmutable();
return new(true, 0, 0, 0);
}
else
{
threadsX = explicitThreadsX;
threadsY = explicitThreadsY;
threadsZ = explicitThreadsZ;
}
// If the current shader is dynamic, emit a diagnostic error
if (isDynamicShader)
+38 -1
ファイルの表示
@@ -1,4 +1,7 @@
using System;
#if !SOURCE_GENERATOR
using Microsoft.Toolkit.Diagnostics;
#endif
namespace ComputeSharp;
@@ -8,11 +11,24 @@ namespace ComputeSharp;
/// <para>
/// This attribute can be used to annotate shader types as follows:
/// <code>
/// [EmbeddedBytecode(8, 8, 1)]
/// // A compute shader that is dispatched on a target buffer
/// [EmbeddedBytecode(DispatchAxis.X)]
/// struct MyShader : IComputeShader
/// {
/// }
/// </code>
/// Or similarly, for a pixel shader:
/// <code>
/// // A pixel shader that is dispatched on a target texture
/// [EmbeddedBytecode(DispatchAxis.XY)]
/// struct MyShader : IPixelShader&lt;float4&gt;
/// {
/// }
/// </code>
/// </para>
/// <para>
/// Using <see cref="DispatchAxis"/> 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 <see cref="EmbeddedBytecodeAttribute(int, int, int)"/>.
/// </para>
/// <para>
/// </para>
@@ -32,6 +48,27 @@ namespace ComputeSharp;
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
public sealed class EmbeddedBytecodeAttribute : Attribute
{
/// <summary>
/// Creates a new <see cref="EmbeddedBytecodeAttribute"/> instance with the specified parameters.
/// </summary>
/// <param name="dispatchAxis">The target dispatch axes for the shader to run.</param>
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
}
/// <summary>
/// Creates a new <see cref="EmbeddedBytecodeAttribute"/> instance with the specified parameters.
/// </summary>
+69
ファイルの表示
@@ -0,0 +1,69 @@
namespace ComputeSharp;
/// <summary>
/// A <see langword="enum"/> to be used with <see cref="EmbeddedBytecodeAttribute"/> to indicate the dispatch
/// </summary>
public enum DispatchAxis
{
/// <summary>
/// Indicates a shader dispatch only along the X axis.
/// </summary>
/// <remarks>
/// This applies to using <see cref="GraphicsDeviceExtensions.For{T}(GraphicsDevice, int, in T)"/> or <see cref="ComputeContextExtensions.For{T}(in ComputeContext, int, in T)"/>.
/// </remarks>
X,
/// <summary>
/// Indicates a shader dispatch only along the Y axis.
/// </summary>
/// <remarks>
/// This applies to using <see cref="GraphicsDeviceExtensions.For{T}(GraphicsDevice, int, int, int, in T)"/> or <see cref="ComputeContextExtensions.For{T}(in ComputeContext, int, int, int, in T)"/>,
/// but only as long as the input dispatch size on both the X and Z axes is <c>1</c>. Using any other combination will not be able to leverage the precompiled shader bytecode.
/// </remarks>
Y,
/// <summary>
/// Indicates a shader dispatch only along the Z axis.
/// </summary>
/// <remarks>
/// This applies to using <see cref="GraphicsDeviceExtensions.For{T}(GraphicsDevice, int, int, int, in T)"/> or <see cref="ComputeContextExtensions.For{T}(in ComputeContext, int, int, int, in T)"/>,
/// but only as long as the input dispatch size on both the X and Y axes is <c>1</c>. Using any other combination will not be able to leverage the precompiled shader bytecode.
/// </remarks>
Z,
/// <summary>
/// Indicates a shader dispatch along the X and Y axes.
/// </summary>
/// <remarks>
/// This applies to using <see cref="GraphicsDeviceExtensions.For{T}(GraphicsDevice, int, int, in T)"/>, <see cref="GraphicsDeviceExtensions.ForEach{T, TPixel}(GraphicsDevice, IReadWriteTexture2D{TPixel})"/>,
/// <see cref="GraphicsDeviceExtensions.ForEach{T, TPixel}(GraphicsDevice, IReadWriteTexture2D{TPixel}, in T)"/>, <see cref="ComputeContextExtensions.For{T}(in ComputeContext, int, int, in T)"/>,
/// <see cref="ComputeContextExtensions.ForEach{T, TPixel}(in ComputeContext, IReadWriteTexture2D{TPixel})"/> or <see cref="ComputeContextExtensions.ForEach{T, TPixel}(in ComputeContext, IReadWriteTexture2D{TPixel}, in T)"/>.
/// </remarks>
XY,
/// <summary>
/// Indicates a shader dispatch along the X and Z axes.
/// </summary>
/// <remarks>
/// This applies to using <see cref="GraphicsDeviceExtensions.For{T}(GraphicsDevice, int, int, int, in T)"/> or <see cref="ComputeContextExtensions.For{T}(in ComputeContext, int, int, int, in T)"/>,
/// but only as long as the input dispatch size on the Y axis <c>1</c>. Using any other combination will not be able to leverage the precompiled shader bytecode.
/// </remarks>
XZ,
/// <summary>
/// Indicates a shader dispatch along the Y and Z axes.
/// </summary>
/// <remarks>
/// This applies to using <see cref="GraphicsDeviceExtensions.For{T}(GraphicsDevice, int, int, int, in T)"/> or <see cref="ComputeContextExtensions.For{T}(in ComputeContext, int, int, int, in T)"/>,
/// but only as long as the input dispatch size on the X axis <c>1</c>. Using any other combination will not be able to leverage the precompiled shader bytecode.
/// </remarks>
YZ,
/// <summary>
/// Indicates a shader dispatch along the X, Y and Z axes.
/// </summary>
/// <remarks>
/// This applies to using <see cref="GraphicsDeviceExtensions.For{T}(GraphicsDevice, int, int, int, in T)"/> or <see cref="ComputeContextExtensions.For{T}(in ComputeContext, int, int, int, in T)"/>.
/// </remarks>
XYZ
}
+9 -6
ファイルの表示
@@ -38,9 +38,12 @@ internal static class ShaderRunner<T>
{
// Here we calculate the optimized [numthreads] values. Using small thread group sizes leads to the
// best average performance due to better occupancy of the GPU with different shaders, using any
// number of registers and any amount of thread local storage. We use the wave size of the device in
// use for 1D dispatches, otherwise a multiple of 32 that is greater than or equal to 64, which still
// results in an evenly divisible number of waves per thread group on all existing GPU devices.
// number of registers and any amount of thread local storage. We use 64 for 1D dispatches, otherwise
// a multiple of 32 that is greater than or equal to 64, which still results in an evenly divisible
// number of waves per thread group on all existing GPU devices. All GPUs will generally have a wavefront
// size of either 16 (eg. Intel mobile GPUs), 32 (nvidia GPUs) or 64 (AMD GPUs), so in all cases 64 will
// be a multiple of that, guaranteeing that all thread waves will be saturated when dispatching shaders.
bool xIs1 = x == 1;
bool yIs1 = y == 1;
bool zIs1 = z == 1;
@@ -60,7 +63,7 @@ internal static class ShaderRunner<T>
threadsY = 1;
break;
case 2: // (_, 1, 1)
threadsX = (int)device.WavefrontSize;
threadsX = 64;
threadsY = threadsZ = 1;
break;
case 3: // (1, _, _)
@@ -69,11 +72,11 @@ internal static class ShaderRunner<T>
break;
case 4: // (1, _, 1)
threadsX = threadsZ = 1;
threadsY = (int)device.WavefrontSize;
threadsY = 64;
break;
case 5: // (1, 1, _)
threadsX = threadsY = 1;
threadsZ = (int)device.WavefrontSize;
threadsZ = 64;
break;
default: // (_, _, _)
threadsX = threadsY = threadsZ = 4;
+102
ファイルの表示
@@ -1472,6 +1472,108 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0044");
}
[TestMethod]
public void InvalidEmbeddedBytecodeDispatchSize_Flags()
{
string source = $@"
using System;
using ComputeSharp;
namespace ComputeSharp
{{
public interface IComputeShader {{ }}
public class ReadWriteBuffer<T> {{ }}
public enum DispatchAxis {{ X, Y, Z, XY, XZ, YZ, XYZ }}
public class EmbeddedBytecodeAttribute : Attribute
{{
public EmbeddedBytecodeAttribute(DispatchAxis dispatchAxis) {{ }}
}}
}}
namespace MyFancyApp.Sample
{{
[EmbeddedBytecode(DispatchAxis.XYZ | DispatchAxis.Y)]
public struct MyShader : IComputeShader
{{
public ReadWriteBuffer<float> buffer;
public void Execute()
{{
}}
}}
}}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0048");
}
[TestMethod]
public void InvalidEmbeddedBytecodeDispatchSize_ExplicitValue()
{
string source = $@"
using System;
using ComputeSharp;
namespace ComputeSharp
{{
public interface IComputeShader {{ }}
public class ReadWriteBuffer<T> {{ }}
public enum DispatchAxis {{ X, Y, Z, XY, XZ, YZ, XYZ }}
public class EmbeddedBytecodeAttribute : Attribute
{{
public EmbeddedBytecodeAttribute(DispatchAxis dispatchAxis) {{ }}
}}
}}
namespace MyFancyApp.Sample
{{
[EmbeddedBytecode((DispatchAxis)243712)]
public struct MyShader : IComputeShader
{{
public ReadWriteBuffer<float> buffer;
public void Execute()
{{
}}
}}
}}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0048");
}
[TestMethod]
public void InvalidEmbeddedBytecodeDispatchSize_ExplicitValue_Negative()
{
string source = $@"
using System;
using ComputeSharp;
namespace ComputeSharp
{{
public interface IComputeShader {{ }}
public class ReadWriteBuffer<T> {{ }}
public enum DispatchAxis {{ X, Y, Z, XY, XZ, YZ, XYZ }}
public class EmbeddedBytecodeAttribute : Attribute
{{
public EmbeddedBytecodeAttribute(DispatchAxis dispatchAxis) {{ }}
}}
}}
namespace MyFancyApp.Sample
{{
[EmbeddedBytecode((DispatchAxis)(-289))]
public struct MyShader : IComputeShader
{{
public ReadWriteBuffer<float> buffer;
public void Execute()
{{
}}
}}
}}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0048");
}
/// <summary>
/// Verifies the output of a source generator.
/// </summary>