Skip to content

Commit

Permalink
Add D2D tests for shader rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 18, 2023
1 parent fc567fc commit f677198
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void GetShaderInfo()
float4 value4 = __reserved__texture.Sample(__sampler____reserved__texture, float2(0, 0.5));
return input0 + input1 + input2 + value3 + value4;
}
""".Replace("\r\n", "\n"), shaderInfo.HlslSource);
""", shaderInfo.HlslSource);

CollectionAssert.AreEqual(D2D1PixelShader.LoadBytecode<ReflectedShader>().ToArray(), shaderInfo.HlslBytecode.ToArray());
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public void GetShaderInfoWithDoublePrecisionFeature()
{
return (float4)(D2DGetInput(0) + (double4)amount);
}
""".Replace("\r\n", "\n"), shaderInfo.HlslSource);
""", shaderInfo.HlslSource);

CollectionAssert.AreEqual(D2D1PixelShader.LoadBytecode<ReflectedShaderWithDoubleOperations>().ToArray(), shaderInfo.HlslBytecode.ToArray());
}
Expand Down
64 changes: 64 additions & 0 deletions tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using ComputeSharp.D2D1.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#pragma warning disable CA1822

namespace ComputeSharp.D2D1.Tests;

[TestClass]
public partial class ShaderRewriterTests
{
// See https://github.com/Sergio0694/ComputeSharp/issues/725
[TestMethod]
public void ShaderWithStructAndInstanceMethodUsingIt_IsRewrittenCorrectly()
{
D2D1ShaderInfo shaderInfo = D2D1ReflectionServices.GetShaderInfo<ClassWithShader.ShaderWithStructAndInstanceMethodUsingIt>();

Assert.AreEqual("""
// ================================================
// AUTO GENERATED
// ================================================
// This shader was created by ComputeSharp.
// See: https://github.com/Sergio0694/ComputeSharp.
#define D2D_INPUT_COUNT 1
#include "d2d1effecthelpers.hlsli"
double amount;
D2D_PS_ENTRY(Execute)
{
return (float4)(D2DGetInput(0) + (double4)amount);
}
""", shaderInfo.HlslSource);
}

internal sealed partial class ClassWithShader
{
[D2DInputCount(0)]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[D2DGeneratedPixelShaderDescriptor]
internal readonly partial struct ShaderWithStructAndInstanceMethodUsingIt : ID2D1PixelShader
{
private struct Data
{
public int value;
}

public float4 Execute()
{
Data data = default;

UseData(ref data);

return new float4(data.value, data.value, data.value, data.value);
}

private void UseData(ref Data data)
{
++data.value;
}
}
}
}

0 comments on commit f677198

Please sign in to comment.