diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 644f16c7..1ca9843d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -63,8 +63,12 @@ public static class D2D1PixelShader /// Loads the bytecode from an input D2D1 pixel shader. /// /// The type of D2D1 pixel shader to load the bytecode for. - /// The compile options to use to get the shader bytecode. + /// + /// The compile options to use to get the shader bytecode. + /// For consistency with , will be automatically added. + /// /// A instance with the resulting shader bytecode. + /// Thrown if is specified within . /// /// /// If precompiled shader with the requested options does not exist, the shader will be compiled with the input options. If additional compile @@ -81,7 +85,12 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode(D2D1CompileOptions options) where T : unmanaged, ID2D1PixelShader { - return LoadOrCompileBytecode(null, options); + if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + { + ThrowHelper.ThrowArgumentException(nameof(options), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); + } + + return LoadOrCompileBytecode(null, options | D2D1CompileOptions.PackMatrixRowMajor); } /// @@ -89,8 +98,12 @@ public static class D2D1PixelShader /// /// The type of D2D1 pixel shader to load the bytecode for. /// The shader profile to use to get the shader bytecode. - /// The compile options to use to get the shader bytecode. + /// + /// The compile options to use to get the shader bytecode. + /// For consistency with , will be automatically added. + /// /// A instance with the resulting shader bytecode. + /// Thrown if is specified within . /// /// If the input shader was precompiled, the returned instance will wrap a pinned memory buffer (from the PE section). /// If the shader was compiled at runtime, the returned instance will wrap a array with the bytecode. @@ -98,7 +111,12 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) where T : unmanaged, ID2D1PixelShader { - return LoadOrCompileBytecode(shaderProfile, options); + if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + { + ThrowHelper.ThrowArgumentException(nameof(options), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); + } + + return LoadOrCompileBytecode(shaderProfile, options | D2D1CompileOptions.PackMatrixRowMajor); } /// 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]