diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs similarity index 57% rename from tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs rename to tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index b5fbbdd4..909edd46 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -1,4 +1,6 @@ using System; +using System.Buffers; +using System.Runtime.InteropServices; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -6,8 +8,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1.Tests; [TestClass] -[TestCategory("D2D1InteropServices")] -public partial class D2D1InteropServicesTests +[TestCategory("D2D1PixelShader")] +public partial class D2D1PixelShaderTests { [TestMethod] public unsafe void GetInputCount() @@ -166,4 +168,111 @@ public partial class D2D1InteropServicesTests return 0; } } + + [TestMethod] + public unsafe void GetBytecode_FromEmbeddedBytecode() + { + // Bytecode with no parameters + ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + + // Matching shader profile + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + + // Matching compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + + // Matching shader profile and compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public unsafe void GetBytecode_FromEmbeddedBytecode_WithPackMatrixColumnMajor() + { + _ = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public unsafe void GetBytecode_FromEmbeddedBytecode_WithTargetProfileAndPackMatrixColumnMajor() + { + _ = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor); + } + + [D2DInputCount(3)] + [D2DInputSimple(0)] + [D2DInputSimple(1)] + [D2DInputSimple(2)] + [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader40Level91)] + private readonly partial struct ShaderWithEmbeddedBytecode : ID2D1PixelShader + { + public float4 Execute() + { + float4 dst = D2D.GetInput(0); + float4 src = D2D.GetInput(1); + float4 srcMask = D2D.GetInput(2); + float4 result = ((1 - srcMask) * dst) + (srcMask * src); + + return result; + } + } + + [TestMethod] + public unsafe void GetBytecode_FromEmbeddedBytecode_WithCompileOptions() + { + // Bytecode with no parameters + ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + + // Matching shader profile + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + + // Matching compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + + // Matching shader profile and compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); + } + + [D2DInputCount(3)] + [D2DInputSimple(0)] + [D2DInputSimple(1)] + [D2DInputSimple(2)] + [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader40Level91)] + [D2DCompileOptions(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2)] + private readonly partial struct ShaderWithEmbeddedBytecodeAndCompileOptions : ID2D1PixelShader + { + public float4 Execute() + { + float4 dst = D2D.GetInput(0); + float4 src = D2D.GetInput(1); + float4 srcMask = D2D.GetInput(2); + float4 result = ((1 - srcMask) * dst) + (srcMask * src); + + return result; + } + } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs index 9a7424b9..a3c14922 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs @@ -1,13 +1,12 @@ using System; using ComputeSharp.D2D1.Exceptions; using ComputeSharp.D2D1.Interop; -using ComputeSharp.D2D1.Tests.Effects; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1.Tests; [TestClass] -[TestCategory("D2D1ShaderCompilerTests")] +[TestCategory("D2D1ShaderCompiler")] public partial class D2D1ShaderCompilerTests { [TestMethod]