From 9437fd971c879b63c2c857b55429f32e0d5048da Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 15 Jan 2022 16:40:15 +0100 Subject: [PATCH 1/2] Fix Hlsl.Normalize intrinsic overloads --- src/ComputeSharp/Core/Intrinsics/Hlsl.g.cs | 15 ++++++++++++--- src/ComputeSharp/Core/Intrinsics/Hlsl.ttinclude | 7 ++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp/Core/Intrinsics/Hlsl.g.cs b/src/ComputeSharp/Core/Intrinsics/Hlsl.g.cs index c15abc27..e74bba24 100644 --- a/src/ComputeSharp/Core/Intrinsics/Hlsl.g.cs +++ b/src/ComputeSharp/Core/Intrinsics/Hlsl.g.cs @@ -12016,7 +12016,7 @@ public static partial class Hlsl /// The normalized parameter. If the length of the parameter is 0, the result is indefinite. /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. [HlslIntrinsicName("normalize")] - public static float Normalize(Float2 x) => default; + public static float Normalize(float x) => default; /// /// Normalizes the specified floating-point vector according to x / length(x). @@ -12025,7 +12025,7 @@ public static partial class Hlsl /// The normalized parameter. If the length of the parameter is 0, the result is indefinite. /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. [HlslIntrinsicName("normalize")] - public static float Normalize(Float3 x) => default; + public static Float2 Normalize(Float2 x) => default; /// /// Normalizes the specified floating-point vector according to x / length(x). @@ -12034,7 +12034,16 @@ public static partial class Hlsl /// The normalized parameter. If the length of the parameter is 0, the result is indefinite. /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. [HlslIntrinsicName("normalize")] - public static float Normalize(Float4 x) => default; + public static Float3 Normalize(Float3 x) => default; + + /// + /// Normalizes the specified floating-point vector according to x / length(x). + /// + /// The specified floating-point vector. + /// The normalized parameter. If the length of the parameter is 0, the result is indefinite. + /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. + [HlslIntrinsicName("normalize")] + public static Float4 Normalize(Float4 x) => default; /// /// Returns the specified value raised to the specified power. diff --git a/src/ComputeSharp/Core/Intrinsics/Hlsl.ttinclude b/src/ComputeSharp/Core/Intrinsics/Hlsl.ttinclude index 0195c977..c6b47260 100644 --- a/src/ComputeSharp/Core/Intrinsics/Hlsl.ttinclude +++ b/src/ComputeSharp/Core/Intrinsics/Hlsl.ttinclude @@ -1916,9 +1916,10 @@ var Intrinsics = new[] Returns = "The normalized {x} parameter. If the length of the {x} parameter is 0, the result is indefinite.", Overloads = new[] { - new { Return = "float", Params = new[] { "Float2" } }, - new { Return = "float", Params = new[] { "Float3" } }, - new { Return = "float", Params = new[] { "Float4" } } + new { Return = "float", Params = new[] { "float" } }, + new { Return = "Float2", Params = new[] { "Float2" } }, + new { Return = "Float3", Params = new[] { "Float3" } }, + new { Return = "Float4", Params = new[] { "Float4" } } } }, new From 8241e56b5524c16aba2c502a7b05584338a5497f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 15 Jan 2022 17:42:47 +0100 Subject: [PATCH 2/2] Fix HLSL generation for early returns in pixel shaders --- .../Translation/__Internals/ShaderCompiler.cs | 8 +-- .../SyntaxRewriters/ExecuteMethodRewriter.cs | 29 ++++++----- tests/ComputeSharp.Tests/IPixelShaderTests.cs | 50 +++++++++++++++++++ 3 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 tests/ComputeSharp.Tests/IPixelShaderTests.cs diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs index 59c02eec..487d5e08 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs @@ -16,15 +16,15 @@ public static class ShaderCompiler /// /// Compiles a new HLSL shader from the input source code. /// - /// The type of bytecode loader being used. + /// The type of bytecode loader being used. /// The type of shader being dispatched. - /// The instance to use to load the bytecode. + /// The instance to use to load the bytecode. /// The number of threads in each thread group for the X axis. /// The number of threads in each thread group for the Y axis. /// The number of threads in each thread group for the Z axis. /// The input instance representing the compute shader to run. - public static unsafe void LoadDynamicBytecode(ref TLoadeer loader, int threadsX, int threadsY, int threadsZ, in T shader) - where TLoadeer : struct, IBytecodeLoader + public static unsafe void LoadDynamicBytecode(ref TLoader loader, int threadsX, int threadsY, int threadsZ, in T shader) + where TLoader : struct, IBytecodeLoader where T : struct, IShader { Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ); diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs index 1972ce8f..f3e20a2e 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs @@ -130,19 +130,24 @@ internal abstract class ExecuteMethodRewriter : CSharpSyntaxRewriter { var updatedNode = (ReturnStatementSyntax)base.VisitReturnStatement(node)!; - // __outputTexture[ThreadIds.xy] = ; + // { + // __outputTexture[ThreadIds.xy] = ; + // return; + // } return - ExpressionStatement( - AssignmentExpression( - SyntaxKind.SimpleAssignmentExpression, - ElementAccessExpression(IdentifierName("__outputTexture")) - .AddArgumentListArguments( - Argument( - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("ThreadIds"), - IdentifierName("xy")))), - updatedNode.Expression!)); + Block( + ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + ElementAccessExpression(IdentifierName("__outputTexture")) + .AddArgumentListArguments( + Argument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("ThreadIds"), + IdentifierName("xy")))), + updatedNode.Expression!)), + ReturnStatement()); } /// diff --git a/tests/ComputeSharp.Tests/IPixelShaderTests.cs b/tests/ComputeSharp.Tests/IPixelShaderTests.cs new file mode 100644 index 00000000..b41a62d5 --- /dev/null +++ b/tests/ComputeSharp.Tests/IPixelShaderTests.cs @@ -0,0 +1,50 @@ +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests; + +[TestClass] +[TestCategory("IPixelShader")] +public partial class IPixelShaderTests +{ + [CombinatorialTestMethod] + [AllDevices] + public unsafe void PixelShader_EarlyReturn(Device device) + { + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + device.Get().ForEach(texture); + + Rgba32[,] result = texture.ToArray(); + + for (int i = 0; i < texture.Height; i++) + { + for (int j = 0; j < texture.Width; j++) + { + if (j % 2 == 0) + { + Assert.AreEqual(result[i, j], new Rgba32(255, 0, 0, 0)); + } + else + { + Assert.AreEqual(result[i, j], new Rgba32(0, 255, 0, 0)); + } + } + } + } + + [AutoConstructor] + internal readonly partial struct EarlyReturnShader : IPixelShader + { + public float4 Execute() + { + if (ThreadIds.X % 2 == 0) + { + return float4.UnitX; + } + + return float4.UnitY; + } + } +}