Merge pull request #283 from Sergio0694/dev/embedded-bytecode-tests

Add unit tests for embedded bytecode validation
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-05-23 06:15:31 -07:00
コミット 3be9a68090
3ファイル変更
+134 -8

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

+22 -4
ファイルの表示
@@ -63,8 +63,12 @@ public static class D2D1PixelShader
/// Loads the bytecode from an input D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to load the bytecode for.</typeparam>
/// <param name="options">The compile options to use to get the shader bytecode.</param>
/// <param name="options">
/// <para>The compile options to use to get the shader bytecode.</para>
/// <para>For consistency with <see cref="D2DCompileOptionsAttribute"/>, <see cref="D2D1CompileOptions.PackMatrixRowMajor"/> will be automatically added.</para>
/// </param>
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
/// <exception cref="ArgumentException">Thrown if <see cref="D2D1CompileOptions.PackMatrixColumnMajor"/> is specified within <paramref name="options"/>.</exception>
/// <remarks>
/// <para>
/// 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<byte> LoadBytecode<T>(D2D1CompileOptions options)
where T : unmanaged, ID2D1PixelShader
{
return LoadOrCompileBytecode<T>(null, options);
if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0)
{
ThrowHelper.ThrowArgumentException(nameof(options), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders.");
}
return LoadOrCompileBytecode<T>(null, options | D2D1CompileOptions.PackMatrixRowMajor);
}
/// <summary>
@@ -89,8 +98,12 @@ public static class D2D1PixelShader
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to load the bytecode for.</typeparam>
/// <param name="shaderProfile">The shader profile to use to get the shader bytecode.</param>
/// <param name="options">The compile options to use to get the shader bytecode.</param>
/// <param name="options">
/// <para>The compile options to use to get the shader bytecode.</para>
/// <para>For consistency with <see cref="D2DCompileOptionsAttribute"/>, <see cref="D2D1CompileOptions.PackMatrixRowMajor"/> will be automatically added.</para>
/// </param>
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
/// <exception cref="ArgumentException">Thrown if <see cref="D2D1CompileOptions.PackMatrixColumnMajor"/> is specified within <paramref name="options"/>.</exception>
/// <remarks>
/// If the input shader was precompiled, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a pinned memory buffer (from the PE section).
/// If the shader was compiled at runtime, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a <see cref="byte"/> array with the bytecode.
@@ -98,7 +111,12 @@ public static class D2D1PixelShader
public static ReadOnlyMemory<byte> LoadBytecode<T>(D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
where T : unmanaged, ID2D1PixelShader
{
return LoadOrCompileBytecode<T>(shaderProfile, options);
if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0)
{
ThrowHelper.ThrowArgumentException(nameof(options), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders.");
}
return LoadOrCompileBytecode<T>(shaderProfile, options | D2D1CompileOptions.PackMatrixRowMajor);
}
/// <summary>
@@ -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<byte> bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecode>();
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager<byte>? manager));
Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name);
// Matching shader profile
bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecode>(D2D1ShaderProfile.PixelShader40Level91);
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name);
// Matching compile options
bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecode>(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<ShaderWithEmbeddedBytecode>(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<ShaderWithEmbeddedBytecode>(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public unsafe void GetBytecode_FromEmbeddedBytecode_WithTargetProfileAndPackMatrixColumnMajor()
{
_ = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecode>(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<byte> bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecodeAndCompileOptions>();
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager<byte>? manager));
Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name);
// Matching shader profile
bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecodeAndCompileOptions>(D2D1ShaderProfile.PixelShader40Level91);
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name);
// Matching compile options
bytecode = D2D1PixelShader.LoadBytecode<ShaderWithEmbeddedBytecodeAndCompileOptions>(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<ShaderWithEmbeddedBytecodeAndCompileOptions>(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;
}
}
}
+1 -2
ファイルの表示
@@ -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]