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 21, 2023
1 parent fc22292 commit f9f619e
Show file tree
Hide file tree
Showing 2 changed files with 78 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
76 changes: 76 additions & 0 deletions tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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 0
#include "d2d1effecthelpers.hlsli"
struct ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data
{
int value;
};
void UseData(inout ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data data);
void UseData(inout ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data data)
{
++data.value;
}
D2D_PS_ENTRY(Execute)
{
ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data data = (ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data)0;
UseData(data);
return float4(data.value, data.value, data.value, data.value);
}
""", 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 f9f619e

Please sign in to comment.