diff --git a/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs index 13cbc13f..1f607349 100644 --- a/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1Interop/Interfaces/__Internals/ID2D1Shader.cs @@ -11,14 +11,6 @@ namespace ComputeSharp.D2D1Interop.__Internals; [Obsolete("This interface is not intended to be used directly by user code")] public interface ID2D1Shader { - /// - /// Gets a unique dispatch identifier for the shader. - /// - /// The unique dispatch identifier for the shader. - [EditorBrowsable(EditorBrowsableState.Never)] - [Obsolete("This method is not intended to be used directly by user code")] - int GetDispatchId(); - /// /// Loads the dispatch data for the shader. /// @@ -29,21 +21,14 @@ public interface ID2D1Shader void LoadDispatchData(ref TLoader loader) where TLoader : struct, ID2D1DispatchDataLoader; - /// - /// Builds the HLSL source code for the current shader instance. - /// - /// The resulting instance containing the HLSL source. - [EditorBrowsable(EditorBrowsableState.Never)] - [Obsolete("This method is not intended to be called directly by user code")] - void BuildHlslString(out ArrayPoolStringBuilder builder); - /// /// Loads the bytecode for the current shader. /// /// The type of bytecode loader being used. /// The instance to use to load the bytecode. + /// The HLSL source that was compiled. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - void LoadBytecode(ref TLoader loader) + void LoadBytecode(ref TLoader loader, out string hlslSource) where TLoader : struct, ID2D1BytecodeLoader; } diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs index 4ac188b8..dfd7ea58 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs @@ -17,10 +17,6 @@ public static unsafe class D2D1InteropServices { /// /// Loads the bytecode from an input D2D1 pixel shader. - /// - /// This overload can be used for simplicity when the D2D1 shader does not rely on captured objects to - /// be processed correctly. This is the case when it does not contain any -s. - /// /// /// The type of D2D1 pixel shader to load the bytecode for. /// A instance with the resulting shader bytecode. @@ -30,26 +26,10 @@ public static unsafe class D2D1InteropServices /// public static void LoadShaderBytecode(out ReadOnlyMemory bytecode) where T : unmanaged, ID2D1PixelShader - { - LoadShaderBytecode(default(T), out bytecode); - } - - /// - /// Loads the bytecode from an input D2D1 pixel shader. - /// - /// The type of D2D1 pixel shader to load the bytecode for. - /// The input D2D1 pixel shader to load the bytecode for. - /// A instance with the resulting 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(in T shader, out ReadOnlyMemory bytecode) - where T : unmanaged, ID2D1PixelShader { D2D1ShaderBytecodeLoader bytecodeLoader = default; - Unsafe.AsRef(in shader).LoadBytecode(ref bytecodeLoader); + Unsafe.NullRef().LoadBytecode(ref bytecodeLoader, 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 a7124848..f0e4f0ac 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs @@ -1,9 +1,9 @@ using System; using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; +using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; using ComputeSharp.D2D1Interop; -using ComputeSharp.D2D1Interop.Shaders.Translation; +using ComputeSharp.Shaders.Dispatching; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -18,39 +18,37 @@ public static class D2D1ReflectionServices { /// /// Gets the shader info associated with a given D2D1 shader. - /// - /// This overload can be used for simplicity when the D2D1 shader being inspected does not rely on captured - /// objects to be processed correctly. This is the case when it does not contain any -s. - /// /// /// The type of D2D1 shader to retrieve info for. /// The resulting instance. - public static D2D1ShaderInfo GetShaderInfo() + public static unsafe D2D1ShaderInfo GetShaderInfo() where T : struct, ID2D1PixelShader { - return GetShaderInfo(default(T)); - } + D2D1ShaderBytecodeLoader bytecodeLoader = default; - /// - /// Gets the shader info associated with a given D2D1 shader. - /// - /// The type of D2D1 shader to retrieve info for. - /// The input D2D1 shader to retrieve info for. - /// The resulting instance. - public static unsafe D2D1ShaderInfo GetShaderInfo(in T shader) - where T : struct, ID2D1PixelShader - { - Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder shaderSource); + Unsafe.NullRef().LoadBytecode(ref bytecodeLoader, out string hlslSource); - using ComPtr d3DBlobBytecode = D2D1ShaderCompiler.Compile(shaderSource.WrittenSpan, enableLinkingSupport: false); + using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); - shaderSource.Dispose(); + byte* bytecodePtr; + int bytecodeSize; + + if (!precompiledBytecode.IsEmpty) + { + bytecodePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(precompiledBytecode)); + bytecodeSize = precompiledBytecode.Length; + } + else + { + bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer(); + bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize(); + } using ComPtr d3D11ShaderReflection = default; DirectX.D3DReflect( - pSrcData: d3DBlobBytecode.Get()->GetBufferPointer(), - SrcDataSize: d3DBlobBytecode.Get()->GetBufferSize(), + pSrcData: bytecodePtr, + SrcDataSize: (nuint)bytecodeSize, pInterface: Windows.__uuidof(), ppReflector: d3D11ShaderReflection.GetVoidAddressOf()).Assert(); @@ -60,7 +58,7 @@ public static class D2D1ReflectionServices return new( CompilerVersion: new string(d3D11ShaderDescription.Creator), - HlslSource: shaderSource.WrittenSpan.ToString(), + HlslSource: hlslSource, ConstantBufferCount: d3D11ShaderDescription.ConstantBuffers, BoundResourceCount: d3D11ShaderDescription.BoundResources, InstructionCount: d3D11ShaderDescription.InstructionCount,