From e7273d23c66788defc7eceb37fe7e7be56731c96 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Oct 2022 00:26:52 +0200 Subject: [PATCH] Add CacheOutput and BufferPrecision properties --- .../PixelShaderEffect{T}.cs | 182 +++++++++++++++++- .../DirectX/um/d2d1_1/D2D1_PROPERTY.cs | 21 ++ .../DirectX/um/d2d1_1/D2D1_PROPERTY_TYPE.cs | 2 + .../DirectX/um/d2d1_1/ID2D1Effect.cs | 7 + .../TerraFX.Interop.Windows.D2D1.projitems | 1 + 5 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY.cs diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs index d8b95f47..42cb34b2 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -51,11 +51,142 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan /// private ComPtr d2D1Effect; + /// + /// The backing field for . + /// + private int cacheOutput; + + /// + /// The backing field for . + /// + private D2D1_BUFFER_PRECISION d2D1BufferPrecision; + /// /// The current value in use. /// public T Value; + /// + /// Gets or sets a value indicating whether to enable caching the output from drawing this effect. + /// + public unsafe bool CacheOutput + { + get + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + // If the effect is realized, get the latest value from it + if (this.d2D1Effect.Get() is not null) + { + int cacheOutput; + + this.d2D1Effect.Get()->GetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, + data: (byte*)&cacheOutput, + dataSize: sizeof(int)).Assert(); + + this.cacheOutput = cacheOutput; + } + + return this.cacheOutput != 0; + } + } + set + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + this.cacheOutput = value ? 1 : 0; + + // If the effect is realized, set the property on the underlying ID2D1Effect object too + if (this.d2D1Effect.Get() is not null) + { + int cacheOutput = this.cacheOutput; + + this.d2D1Effect.Get()->SetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, + data: (byte*)&cacheOutput, + dataSize: sizeof(int)).Assert(); + } + } + } + } + + /// + /// Gets or sets the precision to use for intermediate buffers when drawing this effect. + /// + /// If , the default precision for effects will be used. + public unsafe CanvasBufferPrecision? BufferPrecision + { + get + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + // If the effect is realized, get the latest value from it + if (this.d2D1Effect.Get() is not null) + { + D2D1_BUFFER_PRECISION d2D1BufferPrecision; + + this.d2D1Effect.Get()->GetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&d2D1BufferPrecision, + dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert(); + + this.d2D1BufferPrecision = d2D1BufferPrecision; + } + + // Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN + return this.d2D1BufferPrecision switch + { + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM => CanvasBufferPrecision.Precision8UIntNormalized, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB => CanvasBufferPrecision.Precision8UIntNormalizedSrgb, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM => CanvasBufferPrecision.Precision16UIntNormalized, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT => CanvasBufferPrecision.Precision16Float, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT => CanvasBufferPrecision.Precision32Float, + _ => null + }; + } + } + set + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + // Map back from CanvasBufferPrecision? to D2D1_BUFFER_PRECISION (null results in D2D1_BUFFER_PRECISION_UNKNOWN) + this.d2D1BufferPrecision = value switch + { + CanvasBufferPrecision.Precision8UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM, + CanvasBufferPrecision.Precision8UIntNormalizedSrgb => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB, + CanvasBufferPrecision.Precision16UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM, + CanvasBufferPrecision.Precision16Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT, + CanvasBufferPrecision.Precision32Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT, + _ => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN + }; + + // If the effect is realized, set the property on the underlying ID2D1Effect object too + if (this.d2D1Effect.Get() is not null) + { + D2D1_BUFFER_PRECISION d2D1BufferPrecision = this.d2D1BufferPrecision; + + this.d2D1Effect.Get()->SetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&d2D1BufferPrecision, + dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert(); + } + } + } + } + /// Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator) { @@ -219,7 +350,32 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan return; } - // TODO + // Cache the last update value of the cache property + int cacheOutput; + + this.d2D1Effect.Get()->GetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, + data: (byte*)&cacheOutput, + dataSize: sizeof(int)).Assert(); + + this.cacheOutput = cacheOutput; + + // Do the same for the buffer precision + D2D1_BUFFER_PRECISION d2D1BufferPrecision; + + this.d2D1Effect.Get()->GetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&d2D1BufferPrecision, + dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert(); + + this.d2D1BufferPrecision = d2D1BufferPrecision; + + // TODO: read back D2D sources from the effect + + // Finally release the effect as well + this.d2D1Effect.Dispose(); } /// @@ -249,6 +405,30 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(deviceContext, (void**)d2D1Effect); } + // If cache input has been set, forward that to the new effect + if (this.cacheOutput != 0) + { + int cacheOutput = this.cacheOutput; + + this.d2D1Effect.Get()->GetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, + data: (byte*)&cacheOutput, + dataSize: sizeof(int)).Assert(); + } + + // If the buffer precision isn't unknown, forward that as well + if (this.d2D1BufferPrecision != D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN) + { + D2D1_BUFFER_PRECISION d2D1BufferPrecision = this.d2D1BufferPrecision; + + this.d2D1Effect.Get()->SetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&d2D1BufferPrecision, + dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert(); + } + // TODO: port base CanvasEffect::Realize logic // Also set the current constants for the effect diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY.cs new file mode 100644 index 00000000..03315307 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY.cs @@ -0,0 +1,21 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22621.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.DirectX; + +internal enum D2D1_PROPERTY : uint +{ + D2D1_PROPERTY_CLSID = 0x80000000, + D2D1_PROPERTY_DISPLAYNAME = 0x80000001, + D2D1_PROPERTY_AUTHOR = 0x80000002, + D2D1_PROPERTY_CATEGORY = 0x80000003, + D2D1_PROPERTY_DESCRIPTION = 0x80000004, + D2D1_PROPERTY_INPUTS = 0x80000005, + D2D1_PROPERTY_CACHED = 0x80000006, + D2D1_PROPERTY_PRECISION = 0x80000007, + D2D1_PROPERTY_MIN_INPUTS = 0x80000008, + D2D1_PROPERTY_MAX_INPUTS = 0x80000009, + D2D1_PROPERTY_FORCE_DWORD = 0xffffffff +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY_TYPE.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY_TYPE.cs index 0ec753c4..afab61b9 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY_TYPE.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_PROPERTY_TYPE.cs @@ -7,6 +7,8 @@ namespace TerraFX.Interop.DirectX; internal enum D2D1_PROPERTY_TYPE : uint { + D2D1_PROPERTY_TYPE_UNKNOWN = 0, + D2D1_PROPERTY_TYPE_BOOL = 2, D2D1_PROPERTY_TYPE_BLOB = 9, D2D1_PROPERTY_TYPE_IUNKNOWN = 10 } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs index 3050f3c1..1d5dcb4a 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs @@ -25,4 +25,11 @@ internal unsafe partial struct ID2D1Effect { return ((delegate* unmanaged[Stdcall])(lpVtbl[9]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, type, data, dataSize); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(11)] + public HRESULT GetValue([NativeTypeName("UINT32")] uint index, D2D1_PROPERTY_TYPE type, byte* data, [NativeTypeName("UINT32")] uint dataSize) + { + return ((delegate* unmanaged[Stdcall])(lpVtbl[11]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, type, data, dataSize); + } } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems index 537c5701..977bcb66 100644 --- a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems +++ b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems @@ -32,6 +32,7 @@ +