using System;
using System.Runtime.CompilerServices;
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, 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);
}
///
/// 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, 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 .
///
///
/// 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)
where T : unmanaged, ID2D1PixelShader
{
default(ArgumentException).ThrowIf((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0, nameof(compileOptions));
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
{
default(ArgumentException).ThrowIf((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0, nameof(compileOptions));
return LoadOrCompileBytecode(null, compileOptions | D2D1CompileOptions.PackMatrixRowMajor, out shaderProfile, 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 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 .
///
/// 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 compileOptions)
where T : unmanaged, ID2D1PixelShader
{
default(ArgumentException).ThrowIf((compileOptions & D2D1CompileOptions.PackMatrixColumnMajor) != 0, nameof(compileOptions));
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 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? 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, ref requestedShaderProfile, ref requestedCompileOptions);
effectiveShaderProfile = requestedShaderProfile.GetValueOrDefault();
effectiveCompileOptions = requestedCompileOptions.GetValueOrDefault();
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 input types for a D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the input types for.
/// The input types of the target D2D1 pixel shader.
public static ReadOnlyMemory GetInputTypes()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
return shader.InputTypes;
}
///
/// 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 available resource texture descriptions for a D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the resource texture descriptions for.
/// A with the available resource texture descriptions for the shader.
public static ReadOnlyMemory GetResourceTextureDescriptions()
where T : unmanaged, ID2D1PixelShader
{
D2D1ByteArrayResourceTextureDescriptionsLoader resourceTextureDescriptionsLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadResourceTextureDescriptions(ref resourceTextureDescriptionsLoader);
return resourceTextureDescriptionsLoader.GetResultingResourceTextureDescriptions();
}
///
/// 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();
}
///
/// Gets the size of the constant buffer for a D2D1 pixel shader of a specified type.
///
/// The type of D2D1 pixel shader to retrieve info for.
/// The size of the constant buffer for a D2D1 pixel shader of type .
public static int GetConstantBufferSize()
where T : unmanaged, ID2D1PixelShader
{
return D2D1BufferSizeDispatchDataLoader.For.ConstantBufferSize;
}
///
/// 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.
/// The target to write the constant buffer to.
/// The number of bytes written into .
/// Thrown if is not large enough to contain the constant buffer.
public static int GetConstantBuffer(in T shader, Span span)
where T : unmanaged, ID2D1PixelShader
{
if (!TryGetConstantBuffer(in shader, span, out int writtenBytes))
{
default(ArgumentException).Throw(nameof(span));
}
return writtenBytes;
}
///
/// Tries to get 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.
/// The target to write the constant buffer to.
/// The number of bytes written into .
/// Whether or not the constant buffer was retrieved successfully.
public static unsafe bool TryGetConstantBuffer(in T shader, Span span, out int bytesWritten)
where T : unmanaged, ID2D1PixelShader
{
fixed (byte* buffer = span)
{
D2D1ByteBufferDispatchDataLoader dataLoader = new(buffer, span.Length);
Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader);
return dataLoader.TryGetWrittenBytes(out bytesWritten);
}
}
///
/// 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.
/// A pointer to the ID2D1DrawInfo instance to use.
/// The input D2D1 pixel shader to set the contant buffer for.
/// Thrown if is .
/// For more info, see .
public static unsafe void SetConstantBufferForD2D1DrawInfo(void* d2D1DrawInfo, in T shader)
where T : unmanaged, ID2D1PixelShader
{
default(ArgumentNullException).ThrowIfNull(d2D1DrawInfo);
D2D1DrawInfoDispatchDataLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo);
Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader);
}
}