diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs new file mode 100644 index 00000000..6e727c0b --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -0,0 +1,93 @@ +using ABI.Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas; +using System.Numerics; +using System; +using Windows.Foundation; +using TerraFX.Interop.Windows; +using TerraFX.Interop.DirectX; +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Extensions; + +namespace ComputeSharp.D2D1.Uwp; + +/// +/// A custom implementation powered by a supplied shader type. +/// +/// The type of shader to use to render frames. +public sealed class PixelShaderEffect : ICanvasImage, ICanvasImageInternal + where T : unmanaged, ID2D1PixelShader +{ + /// + /// The current value in use. + /// + public T Value; + + private ComPtr effect; + + /// + public void Dispose() + { + } + + /// + Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator) + { + throw new NotImplementedException(); + } + + /// + Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) + { + throw new NotImplementedException(); + } + + /// + unsafe ID2D1Image** ICanvasImageInternal.GetD2DImage( + ID2D1Image** retBuf, + IUnknown* device, + ID2D1DeviceContext* deviceContext, + GetImageFlags flags, + float targetDpi, + float* realizeDpi) + { + if (realizeDpi is not null) + { + *realizeDpi = targetDpi; + } + + using ComPtr d2D1Image = default; + + if (this.effect.Get() is null) + { + using ComPtr d2D1Factory = default; + + deviceContext->GetFactory(d2D1Factory.GetAddressOf()); + + using ComPtr d2D1Factory1 = default; + + d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert(); + + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), out _); + + using ComPtr d2D1Effect = default; + + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(deviceContext, (void**)d2D1Effect.GetAddressOf()); + + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, d2D1Effect.Get()); + + this.effect = new(d2D1Effect.Get()); + + d2D1Effect.CopyTo(d2D1Image.GetAddressOf()).Assert(); + } + else + { + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, this.effect.Get()); + + this.effect.CopyTo(d2D1Image.GetAddressOf()).Assert(); + } + + *retBuf = d2D1Image.Detach(); + + return retBuf; + } +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1DeviceContext.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1DeviceContext.cs index 2e70f810..f2c97599 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1DeviceContext.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1DeviceContext.cs @@ -19,6 +19,13 @@ internal unsafe partial struct ID2D1DeviceContext { public void** lpVtbl; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(3)] + public void GetFactory(ID2D1Factory** factory) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[3]))((ID2D1DeviceContext*)Unsafe.AsPointer(ref this), factory); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] [VtblIndex(63)] public HRESULT CreateEffect([NativeTypeName("const IID &")] Guid* effectId, ID2D1Effect** effect)