diff --git a/README.md b/README.md index 5b5883b0..68dd1af3 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ The sample app is available in the Microsoft Store, showcasing several pixel sha - [Inspecting shaders](#inspecting-shaders) - [DirectX interop](#directx-interop) - [Precompiled shaders](#precompiled-shaders) + - [D2D1 support](#d2d1-support) - [Requirements](#requirements) - [F# support](#f-support) - [Sponsors](#sponsors) @@ -379,6 +380,41 @@ public readonly partial struct MultiplyByTwo : IPixelShader The values in the `[EmbeddedBytecode]` attribute map to the [`[numthreads]` HLSL attribute](https://docs.microsoft.com/windows/win32/direct3dhlsl/sm5-attributes-numthreads) for the shader entry point. Make sure to use the same values when dispatching the shader at runtime (either using the `GraphicsDevice.For` overload with explicit parameters for the tread numbers, or by relying on `GraphicsDevice.ForEach` to always use `(8, 8, 1)` for thread numbers, as it will be dispatching shaders on a 2D texture). +## D2D1 support + +**ComputeSharp** also offers [D2D1 support](https://docs.microsoft.com/en-us/windows/win32/direct2d/d2d1-namespace), 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: + +```csharp +[D2DInputCount(1)] +[D2DInputSimple(0)] +[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +public partial struct DifferenceEffect : ID2D1PixelShader +{ + public float amount; + + /// + 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: + +```csharp +ReadOnlyMemory bytecode = D2D1InteropServices.LoadShaderBytecode(); +ReadOnlyMemory 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). + # Requirements Building and running **ComputeSharp** from the .NET solution filter has the following prerequisites: diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs index 5442cff8..ab88fa9c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs @@ -86,7 +86,7 @@ public static unsafe class D2D1InteropServices /// /// This method will allocate a buffer every time it is invoked. /// For a zero-allocation alternative, use . - public static ReadOnlyMemory GetPixelShaderConstantBufferForD2D1DrawInfo(in T shader) + public static ReadOnlyMemory GetPixelShaderConstantBuffer(in T shader) where T : unmanaged, ID2D1PixelShader { D2D1ByteArrayDispatchDataLoader dataLoader = default;