using System; using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Shaders.Dispatching; using ComputeSharp.D2D1.Shaders.Interop.Buffers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #pragma warning disable CS0618 namespace ComputeSharp.D2D1.Interop; /// /// Provides methods to interop with D2D1 APIs and compile shaders or extract their constant buffer data. /// public static unsafe class D2D1InteropServices { /// /// 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. /// /// 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 LoadShaderBytecode() where T : unmanaged, ID2D1PixelShader { return LoadOrCompileShaderBytecode(null); } /// /// 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. /// A instance with the resulting shader bytecode. /// /// 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 LoadShaderBytecode(D2D1ShaderProfile shaderProfile) where T : unmanaged, ID2D1PixelShader { return LoadOrCompileShaderBytecode(shaderProfile); } /// /// 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 ReadOnlyMemory LoadOrCompileShaderBytecode(D2D1ShaderProfile? shaderProfile) where T : unmanaged, ID2D1PixelShader { D2D1ShaderBytecodeLoader bytecodeLoader = default; Unsafe.NullRef().LoadBytecode(ref bytecodeLoader, shaderProfile); using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); // If a precompiled shader is available, return it if (!precompiledBytecode.IsEmpty) { return new PinnedBufferMemoryManager(precompiledBytecode).Memory; } // Otherwise, return the dynamic shader instead byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer(); int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize(); return new ReadOnlySpan(bytecodePtr, bytecodeSize).ToArray(); } /// /// Gets the constant buffer from an input D2D1 pixel shader. /// /// The type of D2D1 pixel shader to retrieve info for. /// The input D2D1 pixel shader to retrieve info for. /// A instance with the pixel shader constant buffer. /// /// This method will allocate a buffer every time it is invoked. /// For a zero-allocation alternative, use . public static ReadOnlyMemory GetPixelShaderConstantBuffer(in T shader) where T : unmanaged, ID2D1PixelShader { D2D1ByteArrayDispatchDataLoader dataLoader = default; Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); return dataLoader.GetResultingDispatchData(); } /// /// Sets the constant buffer from an input D2D1 pixel shader, by calling ID2D1DrawInfo::SetPixelShaderConstantBuffer. /// /// The type of D2D1 pixel shader to set the constant buffer for. /// The input D2D1 pixel shader to set the contant buffer for. /// A pointer to the ID2D1DrawInfo instance to use. /// For more info, see . public static void SetPixelShaderConstantBufferForD2D1DrawInfo(in T shader, void* d2D1DrawInfo) where T : unmanaged, ID2D1PixelShader { D2D1DrawInfoDispatchDataLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo); Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); } }