Skip to content

4. D2D1 support πŸ“š

Sergio Pedri edited this page Apr 30, 2022 · 5 revisions

ComputeSharp also offers D2D1 support, through the dedicated package. This shares the same base APIs (primitives, intrinsics, etc.) from ComputeSharp.Core, but then adds D2D1 specific support, instead of using DX12 like the main package. This means it offers the ability to implement D2D1 pixel shaders, and to then either load them manually, or to register a D2D1 effect using them, optionally with a custom draw transform as well.

For instance, here's a simple D2D1 pixel shader:

[D2DInputCount(1)]
[D2DInputSimple(0)]
[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)]
[AutoConstructor]
public partial struct DifferenceEffect : ID2D1PixelShader
{
    public float amount;

    /// <inheritdoc/>
    public float4 Execute()
    {
        float4 color = D2D.GetInput(0);
        float3 rgb = Hlsl.Saturate(this.amount - color.RGB);

        return new(rgb, 1);
    }
}

This can then be used directly to get the shader bytecode and the buffer, like so:

ReadOnlyMemory<byte> bytecode = D2D1InteropServices.LoadShaderBytecode<DifferenceEffect>();
ReadOnlyMemory<byte> buffer = D2D1InteropServices.GetPixelShaderConstantBuffer(new DifferenceEffect(1));

There are also several other APIs to easily register a pixel shader effect from a shader written using ComputeSharp.D2D1, and to then create an ID2D1Effect instance from it (from the D2D1PixelShaderEffect type), as well as for reflecting into a shader and extract information about it, such as its HLSL source code (from the D2D1ReflectionServices type).

Clone this wiki locally