using System;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Shaders.Interop.Buffers;
using ComputeSharp.D2D1.Shaders.Loaders;
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 class D2D1PixelShader
{
///
/// 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 LoadBytecode()
where T : unmanaged, ID2D1PixelShader
{
return LoadOrCompileBytecode(null, 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 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)
where T : unmanaged, ID2D1PixelShader
{
return LoadOrCompileBytecode(shaderProfile, null);
}
///
/// 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.
/// A instance with the resulting shader bytecode.
///
///
/// 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 options)
where T : unmanaged, ID2D1PixelShader
{
return LoadOrCompileBytecode(null, options);
}
///
/// 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 compile options 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 LoadBytecode(D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
where T : unmanaged, ID2D1PixelShader
{
return LoadOrCompileBytecode(shaderProfile, options);
}
///
/// 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.
/// 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)
where T : unmanaged, ID2D1PixelShader
{
D2D1ShaderBytecodeLoader bytecodeLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadBytecode(ref bytecodeLoader, shaderProfile, options);
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 pixel options from an input D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the pixel options for.
/// The pixel options for the D2D1 pixel shader of type .
public static D2D1PixelOptions GetPixelOptions()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
return (D2D1PixelOptions)shader.GetPixelOptions();
}
///
/// Gets the number of inputs from an input D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the input count for.
/// The number of inputs for the D2D1 pixel shader of type .
public static int GetInputCount()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
return (int)shader.GetInputCount();
}
///
/// Gets the type of a given input for a D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the input type for.
/// The index of the input to get the type for.
/// The type of the input of the target D2D1 pixel shader at the specified index.
/// Thrown if is not in range for the available inputs for the shader type.
public static D2D1PixelShaderInputType GetInputType(int index)
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
if ((uint)index >= shader.GetInputCount())
{
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is outside of range for the target pixel shader type.");
}
return (D2D1PixelShaderInputType)shader.GetInputType((uint)index);
}
///
/// Gets the available input descriptions for a D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the input descriptions for.
/// A with the available input descriptions for the shader.
public static ReadOnlyMemory GetInputDescriptions()
where T : unmanaged, ID2D1PixelShader
{
D2D1ByteArrayInputDescriptionsLoader inputDescriptionsLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadInputDescriptions(ref inputDescriptionsLoader);
return inputDescriptionsLoader.GetResultingInputDescriptions();
}
///
/// Gets the buffer precision for the output buffer of a D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the output buffer precision for.
/// The output buffer precision for the D2D1 pixel shader of type .
public static D2D1BufferPrecision GetOutputBufferPrecision()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
shader.GetOutputBuffer(out uint precision, out _);
return (D2D1BufferPrecision)precision;
}
///
/// Gets the channel depth for the output buffer of a D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the output buffer channel depth for.
/// The output buffer channel depth for the D2D1 pixel shader of type .
public static D2D1ChannelDepth GetOutputBufferChannelDepth()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
shader.GetOutputBuffer(out _, out uint depth);
return (D2D1ChannelDepth)depth;
}
///
/// 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 GetConstantBuffer(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 unsafe void SetConstantBufferForD2D1DrawInfo(in T shader, void* d2D1DrawInfo)
where T : unmanaged, ID2D1PixelShader
{
D2D1DrawInfoDispatchDataLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo);
Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader);
}
}