From 3da3500a5d895a03656ba26701f0a667c902722a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 3 Apr 2022 22:05:51 +0200 Subject: [PATCH] Add D2D1ShaderProfile parameter to ID2D1Shader.LoadBytecode --- .../__Internals/ID2D1BytecodeLoader.cs | 2 +- .../Interfaces/__Internals/ID2D1Shader.cs | 4 ++- .../Shaders/Interop/D2D1InteropServices.cs | 36 +++++++++++++++++-- .../Shaders/Interop/D2D1ReflectionServices.cs | 2 +- .../Interfaces/__Internals/IBytecodeLoader.cs | 1 + .../Core/Interfaces/__Internals/IShader.cs | 1 + 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1BytecodeLoader.cs b/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1BytecodeLoader.cs index ad75568d..93a23f45 100644 --- a/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1BytecodeLoader.cs +++ b/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1BytecodeLoader.cs @@ -14,7 +14,7 @@ public interface ID2D1BytecodeLoader /// Loads a dynamic shader bytecode. /// /// An opaque handle to the shader bytecode. - /// Thrown if the shader has already been initialized. + /// Thrown if the shader has already been initialized or a precompiled bytecode was requested when not available.. void LoadDynamicBytecode(IntPtr handle); /// diff --git a/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs index c7559db7..ee6b1402 100644 --- a/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs @@ -33,10 +33,12 @@ 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 HLSL source that was compiled. + /// Thrown if a precompiled bytecode was requested ( is ), but it wasn't availablle. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - void LoadBytecode(ref TLoader loader, out string hlslSource) + void LoadBytecode(ref TLoader loader, D2D1ShaderProfile? shaderProfile, out string hlslSource) where TLoader : struct, ID2D1BytecodeLoader #if NET6_0_OR_GREATER { diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs index dfd7ea58..6b7e1374 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs @@ -20,16 +20,46 @@ public static unsafe class D2D1InteropServices /// /// The type of D2D1 pixel shader to load the bytecode for. /// A instance with the resulting shader bytecode. + /// Thrown if the input shader has not been precompiled. /// - /// If the input shader was precompiled, will wrap a pinned memory buffer (from the PE section). - /// If the shader was compiled at runtime, will wrap a array with the bytecode. + /// This method will only run correctly if the input shader has been precompiled, or it will throw an exception. + /// The returned will wrap a pinned memory buffer (from the PE section). /// public static void LoadShaderBytecode(out ReadOnlyMemory bytecode) where T : unmanaged, ID2D1PixelShader + { + LoadOrCompileShaderBytecode(null, out bytecode); + } + + /// + /// Loads the bytecode from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to load the bytecode for. + /// A instance with the resulting shader bytecode. + /// The shader profile to use to get the shader bytecode. + /// + /// If the input shader was precompiled, will wrap a pinned memory buffer (from the PE section). + /// If the shader was compiled at runtime, will wrap a array with the bytecode. + /// + public static void LoadShaderBytecode(D2D1ShaderProfile shaderProfile, out ReadOnlyMemory bytecode) + where T : unmanaged, ID2D1PixelShader + { + LoadOrCompileShaderBytecode(shaderProfile, out bytecode); + } + + /// + /// 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. + /// A instance with the resulting shader bytecode. + /// Thrown if the input shader has not been precompiled. + private static void LoadOrCompileShaderBytecode(D2D1ShaderProfile? shaderProfile, out ReadOnlyMemory bytecode) + where T : unmanaged, ID2D1PixelShader { D2D1ShaderBytecodeLoader bytecodeLoader = default; - Unsafe.NullRef().LoadBytecode(ref bytecodeLoader, out _); + Unsafe.NullRef().LoadBytecode(ref bytecodeLoader, shaderProfile, out _); using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs index 339a2c0a..64e1c5f5 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs @@ -25,7 +25,7 @@ public static class D2D1ReflectionServices { D2D1ShaderBytecodeLoader bytecodeLoader = default; - Unsafe.NullRef().LoadBytecode(ref bytecodeLoader, out string hlslSource); + Unsafe.NullRef().LoadBytecode(ref bytecodeLoader, D2D1ShaderProfile.PixelShader50, out string hlslSource); using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IBytecodeLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IBytecodeLoader.cs index 943cef72..1ca5ee68 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IBytecodeLoader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IBytecodeLoader.cs @@ -15,6 +15,7 @@ public interface IBytecodeLoader /// /// An opaque handle to the shader bytecode. /// Thrown if the shader has already been initialized. + /// Thrown if is (indicating an unsupported configuration). void LoadDynamicBytecode(IntPtr handle); /// diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index 262b7d0d..7c54eff6 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -90,6 +90,7 @@ public interface IShader /// The number of threads in each thread group for the X axis. /// The number of threads in each thread group for the Y axis. /// The number of threads in each thread group for the Z axis. + /// Thrown if the bytecode required runtime compilation and it wasn't supported on the current configuration. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] void LoadBytecode(ref TLoader loader, int threadsX, int threadsY, int threadsZ)