using System;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Shaders.Loaders;
using ComputeSharp.D2D1.Shaders.Translation;
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
{
Unsafe.SkipInit(out T shader);
// This method is the one used by the built-in ID2D1Effect, so it has special optimization
// to help both performance and especially trimming. That is, we first check whether the
// HLSL code is already available, which should get completely inlined when using the
// generated code. This allows the entire fallback path below to be trimmed out.
if (shader.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode)
{
return hlslBytecode;
}
return CompileBytecode(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 _);
}
///
/// 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.
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe ReadOnlyMemory CompileBytecode(
D2D1ShaderProfile? requestedShaderProfile,
D2D1CompileOptions? requestedCompileOptions,
out D2D1ShaderProfile effectiveShaderProfile,
out D2D1CompileOptions effectiveCompileOptions)
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
string hlslSource = shader.HlslSource;
// Set the effective profile and option to the requested ones or the default values
effectiveShaderProfile = requestedShaderProfile ?? shader.ShaderProfile;
effectiveCompileOptions = requestedCompileOptions ?? shader.CompileOptions;
// Compile the shader with the current settings
using ComPtr dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), effectiveShaderProfile, effectiveCompileOptions);
byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer();
int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize();
return new ReadOnlySpan(bytecodePtr, bytecodeSize).ToArray();
}
///
/// 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.
private static unsafe ReadOnlyMemory LoadOrCompileBytecode(
D2D1ShaderProfile? requestedShaderProfile,
D2D1CompileOptions? requestedCompileOptions,
out D2D1ShaderProfile effectiveShaderProfile,
out D2D1CompileOptions effectiveCompileOptions)
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
// Check whether there is precompiled bytecode we can use
if (shader.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode &&
(requestedShaderProfile is null || shader.ShaderProfile == requestedShaderProfile.GetValueOrDefault()) &&
(requestedCompileOptions is null || shader.CompileOptions == requestedCompileOptions.GetValueOrDefault()))
{
effectiveShaderProfile = shader.ShaderProfile;
effectiveCompileOptions = shader.CompileOptions;
return hlslBytecode;
}
// Fallback path just compiling the HLSL code on the fly
return CompileBytecode(requestedShaderProfile, requestedCompileOptions, out effectiveShaderProfile, out effectiveCompileOptions);
}
///
/// 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 shader.PixelOptions;
}
///
/// 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 shader.InputCount;
}
///
/// Gets the number of resource textures for a D2D1 pixel shader.
///
/// The type of D2D1 pixel shader to get the resource texture count for.
/// The number of resource texture for the D2D1 pixel shader of type .
public static int GetResourceTextureCount()
where T : unmanaged, ID2D1PixelShader
{
Unsafe.SkipInit(out T shader);
return shader.ResourceTextureCount;
}
///
/// 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
{
Unsafe.SkipInit(out T shader);
return shader.InputDescriptions;
}
///
/// 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
{
Unsafe.SkipInit(out T shader);
return shader.ResourceTextureDescriptions;
}
///
/// 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);
return shader.BufferPrecision;
}
///
/// 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);
return shader.ChannelDepth;
}
///
/// 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
{
Unsafe.SkipInit(out T shader);
return shader.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);
}
}