Fix HLSL generation for early returns in pixel shaders

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-15 17:42:47 +01:00
1 親 9437fd971c
コミット 8241e56b55
3ファイル変更
+71 -16

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

+4 -4
ファイルの表示
@@ -16,15 +16,15 @@ public static class ShaderCompiler
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <typeparam name="TLoadeer">The type of bytecode loader being used.</typeparam>
/// <typeparam name="TLoader">The type of bytecode loader being used.</typeparam>
/// <typeparam name="T">The type of shader being dispatched.</typeparam>
/// <param name="loader">The <typeparamref name="TLoadeer"/> instance to use to load the bytecode.</param>
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the bytecode.</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static unsafe void LoadDynamicBytecode<TLoadeer, T>(ref TLoadeer loader, int threadsX, int threadsY, int threadsZ, in T shader)
where TLoadeer : struct, IBytecodeLoader
public static unsafe void LoadDynamicBytecode<TLoader, T>(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);
+17 -12
ファイルの表示
@@ -130,19 +130,24 @@ internal abstract class ExecuteMethodRewriter : CSharpSyntaxRewriter
{
var updatedNode = (ReturnStatementSyntax)base.VisitReturnStatement(node)!;
// __outputTexture[ThreadIds.xy] = <RETURN_EXPRESSION>;
// {
// __outputTexture[ThreadIds.xy] = <RETURN_EXPRESSION>;
// 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());
}
/// <inheritdoc/>
+50
ファイルの表示
@@ -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<Rgba32, float4> texture = device.Get().AllocateReadWriteTexture2D<Rgba32, float4>(128, 128);
device.Get().ForEach<EarlyReturnShader, float4>(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<float4>
{
public float4 Execute()
{
if (ThreadIds.X % 2 == 0)
{
return float4.UnitX;
}
return float4.UnitY;
}
}
}