From b1ce0e9c43ddfccc6ffffb6214b1cd2b6fb9e39f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 21 Oct 2022 16:24:45 +0200 Subject: [PATCH] Add APIs to get effective shader profile and compile options --- .../Interfaces/__Internals/ID2D1Shader.cs | 6 +- .../Shaders/Interop/D2D1PixelShader.cs | 127 +++++++++++++++--- .../Shaders/Interop/D2D1ReflectionServices.cs | 5 +- .../D2D1PixelShaderTests.cs | 8 +- 4 files changed, 121 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index e4e84512..e4c9ae28 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; namespace ComputeSharp.D2D1.__Internals; @@ -98,10 +99,11 @@ public interface ID2D1Shader /// The type of bytecode loader being used. /// The instance to use to load the bytecode. /// The shader profile to use to get the shader bytecode (if , the precompiled shader will be used). - /// The compile options to use to get the shader bytecode (if , the precompiled shader will be used). + /// The compile options to use to get the shader bytecode (if , the precompiled shader will be used). /// Thrown if a precompiled bytecode was requested ( is ), but it wasn't availablle. + /// When this method returns, and will be set to the effective laues used to create the shader bytecode that was loaded. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - void LoadBytecode(ref TLoader loader, D2D1ShaderProfile? shaderProfile, D2D1CompileOptions? options) + void LoadBytecode(ref TLoader loader, [NotNull] ref D2D1ShaderProfile? shaderProfile, [NotNull] ref D2D1CompileOptions? compileOptions) where TLoader : struct, ID2D1BytecodeLoader; } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 134616a9..4203a859 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -30,7 +30,27 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode() where T : unmanaged, ID2D1PixelShader { - return LoadOrCompileBytecode(null, null); + return LoadOrCompileBytecode(null, null, out _, out _); + } + + /// + /// Loads the bytecode from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to load the bytecode for. + /// The resulting that was used to compile the returned bytecode. + /// The resulting that were used to compile the returned bytecode. + /// A instance with the resulting shader bytecode. + /// + /// This method will only compile the shader using if no precompiled shader is available. + /// + /// 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. + /// + /// + public static ReadOnlyMemory LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions) + where T : unmanaged, ID2D1PixelShader + { + return LoadOrCompileBytecode(null, null, out shaderProfile, out compileOptions); } /// @@ -56,19 +76,46 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile) where T : unmanaged, ID2D1PixelShader { - return LoadOrCompileBytecode(shaderProfile, null); + return LoadOrCompileBytecode(shaderProfile, null, out _, out _); } /// /// Loads the bytecode from an input D2D1 pixel shader. /// /// The type of D2D1 pixel shader to load the bytecode for. - /// + /// The shader profile to use to get the shader bytecode. + /// The resulting that were used to compile the returned bytecode. + /// A instance with the resulting shader bytecode. + /// + /// + /// If precompiled shader for the profile does not exist, the shader will be compiled with either the custom compile options specified on the shader + /// type (through ), or using otherwise. + /// + /// + /// Additionally, in case no custom compile options are specified and the the shader type + /// supports linking, will also be automatically added. + /// + /// + /// 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. + /// + /// + public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions) + where T : unmanaged, ID2D1PixelShader + { + return LoadOrCompileBytecode(shaderProfile, null, out _, out compileOptions); + } + + /// + /// 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. /// For consistency with , will be automatically added. /// /// A instance with the resulting shader bytecode. - /// Thrown if is specified within . + /// 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 @@ -82,15 +129,50 @@ public static class D2D1PixelShader /// If the shader was compiled at runtime, the returned instance will wrap a array with the bytecode. /// /// - public static ReadOnlyMemory LoadBytecode(D2D1CompileOptions options) + public static ReadOnlyMemory LoadBytecode(D2D1CompileOptions compileOptions) where T : unmanaged, ID2D1PixelShader { - if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + if ((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0) { - ThrowHelper.ThrowArgumentException(nameof(options), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); + ThrowHelper.ThrowArgumentException(nameof(compileOptions), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); } - return LoadOrCompileBytecode(null, options | D2D1CompileOptions.PackMatrixRowMajor); + return LoadOrCompileBytecode(null, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out _, out _); + } + + /// + /// 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. + /// For consistency with , will be automatically added. + /// + /// The resulting that was used to compile the returned bytecode. + /// 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 + /// options have been specified on the shader type (through ), they will be ignored. + /// + /// + /// If the shader needs to be recompiled, the shader profile that will be used is . + /// + /// + /// 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. + /// + /// + public static ReadOnlyMemory LoadBytecode(D2D1CompileOptions compileOptions, out D2D1ShaderProfile shaderProfile) + where T : unmanaged, ID2D1PixelShader + { + if ((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + { + ThrowHelper.ThrowArgumentException(nameof(compileOptions), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); + } + + return LoadOrCompileBytecode(null, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out shaderProfile, out _); } /// @@ -98,43 +180,52 @@ 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. /// For consistency with , will be automatically added. /// /// A instance with the resulting shader bytecode. - /// Thrown if is specified within . + /// 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. /// - public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile, D2D1CompileOptions options) + public static ReadOnlyMemory LoadBytecode(D2D1ShaderProfile shaderProfile, D2D1CompileOptions compileOptions) where T : unmanaged, ID2D1PixelShader { - if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + if ((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0) { - ThrowHelper.ThrowArgumentException(nameof(options), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); + ThrowHelper.ThrowArgumentException(nameof(compileOptions), "The PackMatrixColumnMajor compile options is not compatible with ComputeSharp.D2D1 shaders."); } - return LoadOrCompileBytecode(shaderProfile, options | D2D1CompileOptions.PackMatrixRowMajor); + return LoadOrCompileBytecode(shaderProfile, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out _, out _); } /// /// Loads or compiles the bytecode from an input D2D1 pixel shader. /// /// 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 requested shader profile to use to get the shader bytecode. + /// The requested compile options to use to get the shader bytecode. + /// The effective shader profile that was used to get the shader bytecode. + /// The effective compile options that were used to get the shader bytecode. /// A instance with the resulting shader bytecode. /// Thrown if the input shader has not been precompiled. - private static unsafe ReadOnlyMemory LoadOrCompileBytecode(D2D1ShaderProfile? shaderProfile, D2D1CompileOptions? options) + private static unsafe ReadOnlyMemory LoadOrCompileBytecode( + D2D1ShaderProfile? requestedShaderProfile, + D2D1CompileOptions? requestedCompileOptions, + out D2D1ShaderProfile effectiveShaderProfile, + out D2D1CompileOptions effectiveCompileOptions) where T : unmanaged, ID2D1PixelShader { D2D1ShaderBytecodeLoader bytecodeLoader = default; Unsafe.SkipInit(out T shader); - shader.LoadBytecode(ref bytecodeLoader, shaderProfile, options); + shader.LoadBytecode(ref bytecodeLoader, ref requestedShaderProfile, ref requestedCompileOptions); + + effectiveShaderProfile = requestedShaderProfile.GetValueOrDefault(); + effectiveCompileOptions = requestedCompileOptions.GetValueOrDefault(); using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs index 64c066df..958e4c5d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs @@ -37,7 +37,10 @@ public static class D2D1ReflectionServices D2D1ShaderBytecodeLoader bytecodeLoader = default; - shader.LoadBytecode(ref bytecodeLoader, null, null); + D2D1ShaderProfile? shaderProfile = null; + D2D1CompileOptions? compileOptions = null; + + shader.LoadBytecode(ref bytecodeLoader, ref shaderProfile, ref compileOptions); using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); diff --git a/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs index 4a26be0f..9c3b0f3e 100644 --- a/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests.AssemblyLevelAttributes/D2D1PixelShaderTests.cs @@ -21,7 +21,7 @@ public partial class D2D1PixelShaderTests _ = D2D1PixelShader.LoadBytecode( shaderProfile: D2D1ShaderProfile.PixelShader41, - options: D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PartialPrecision); + compileOptions: D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PartialPrecision); // Verify the expected options were used Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); @@ -55,7 +55,7 @@ public partial class D2D1PixelShaderTests _ = D2D1PixelShader.LoadBytecode( shaderProfile: D2D1ShaderProfile.PixelShader50, - options: D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PartialPrecision); + compileOptions: D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PartialPrecision); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); @@ -89,7 +89,7 @@ public partial class D2D1PixelShaderTests _ = D2D1PixelShader.LoadBytecode( shaderProfile: D2D1ShaderProfile.PixelShader41, - options: D2D1CompileOptions.Debug | D2D1CompileOptions.AvoidFlowControl | D2D1CompileOptions.PartialPrecision); + compileOptions: D2D1CompileOptions.Debug | D2D1CompileOptions.AvoidFlowControl | D2D1CompileOptions.PartialPrecision); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name); @@ -123,7 +123,7 @@ public partial class D2D1PixelShaderTests _ = D2D1PixelShader.LoadBytecode( shaderProfile: D2D1ShaderProfile.PixelShader50, - options: D2D1CompileOptions.Debug | D2D1CompileOptions.AvoidFlowControl | D2D1CompileOptions.PartialPrecision); + compileOptions: D2D1CompileOptions.Debug | D2D1CompileOptions.AvoidFlowControl | D2D1CompileOptions.PartialPrecision); Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); Assert.AreEqual("PinnedBufferMemoryManager", manager!.GetType().Name);