From c58695cb5f37f4a8a07b1b76089ea0baec547d49 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 27 Oct 2022 00:36:19 +0200 Subject: [PATCH] Implement PixelShaderEffect.Value property --- .../Extensions/ID2D1EffectExtensions.cs | 34 ++++++++++ ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 11 ++- ...PixelShaderEffect{T}.Properties.Interop.cs | 68 +++++++++++++++---- .../PixelShaderEffect{T}.Properties.cs | 6 +- .../PixelShaderEffect{T}.cs | 5 ++ 5 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs index ffd7a850..38968217 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -1,5 +1,11 @@ +using System.Buffers; +using System.Runtime.CompilerServices; +using System; using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.DirectX; +using ComputeSharp.D2D1.Interop; + +#pragma warning disable CS0618 namespace ComputeSharp.D2D1.Uwp.Extensions; @@ -8,6 +14,34 @@ namespace ComputeSharp.D2D1.Uwp.Extensions; /// internal static unsafe class ID2D1EffectExtensions { + /// + /// Gets the constant buffer of type from a given object. + /// + /// The input instance. + /// The constant buffer of type for . + public static T GetConstantBuffer(this ref ID2D1Effect d2D1Effect) + where T : unmanaged, ID2D1PixelShader + { + int constantBufferSize = D2D1PixelShader.GetConstantBufferSize(); + byte[] buffer = ArrayPool.Shared.Rent(constantBufferSize); + + fixed (byte* p = buffer) + { + // Get the raw constant buffer from the effect + d2D1Effect.GetValue( + index: D2D1PixelShaderEffectProperty.ConstantBuffer, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB, + data: p, + dataSize: (uint)constantBufferSize).Assert(); + } + + Unsafe.SkipInit(out T shader); + + shader.InitializeFromDispatchData(buffer.AsSpan(0, constantBufferSize)); + + return shader; + } + /// /// Gets the property for a given object. /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index 0712a2f6..7e819cba 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -166,7 +166,8 @@ unsafe partial class PixelShaderEffect return; } - // Cache the last update value of the cache and precision properties + // Cache the last update values for the constant buffer, cache output and buffer precision properties + this.value = this.d2D1Effect.Get()->GetConstantBuffer(); this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); @@ -231,7 +232,8 @@ unsafe partial class PixelShaderEffect hresult.Assert(); } - // TODO: transfer properties (eg. D2D1ResourceTextureManager-s) + // Set the constant buffer for the effect + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(this.value, this.d2D1Effect.Get()); // If cache input has been set, forward that to the new effect if (this.cacheOutput) @@ -247,8 +249,7 @@ unsafe partial class PixelShaderEffect // TODO: port base CanvasEffect::Realize logic - // Also set the current constants for the effect - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(Value, this.d2D1Effect.Get()); + // TODO: transfer properties (eg. D2D1ResourceTextureManager-s) } /// @@ -259,8 +260,6 @@ unsafe partial class PixelShaderEffect /// The instance in use. private void RefreshInputs(CanvasImageGetD2DImageFlags flags, float targetDpi, ID2D1DeviceContext* deviceContext) { - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(Value, this.d2D1Effect.Get()); - // TODO } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs index e5a12ea1..324c7a06 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs @@ -1,3 +1,4 @@ +using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; @@ -8,6 +9,47 @@ namespace ComputeSharp.D2D1.Uwp; /// unsafe partial class PixelShaderEffect { + /// + /// Gets the marshalled value for . + /// + /// The marshalled value for . + private T GetConstantBuffer() + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + return this.d2D1Effect.Get() switch + { + not null => this.d2D1Effect.Get()->GetConstantBuffer(), + _ => this.value + }; + } + } + + /// + /// Sets the marshalled value for . + /// + /// The value to set for . + private void SetConstantBuffer(in T value) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + // If the effect is realized, set the property on the underlying ID2D1Effect object + if (this.d2D1Effect.Get() is not null) + { + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in value, this.d2D1Effect.Get()); + } + else + { + // Otherwise, just store the value locally + this.value = value; + } + } + } + /// /// Gets the marshalled value for . /// @@ -18,13 +60,11 @@ unsafe partial class PixelShaderEffect lock (this.lockObject) { - // If the effect is realized, get the latest value from it - if (this.d2D1Effect.Get() is not null) + return this.d2D1Effect.Get() switch { - this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); - } - - return this.cacheOutput; + not null => this.d2D1Effect.Get()->GetCachedProperty(), + _ => this.cacheOutput + }; } } @@ -38,13 +78,15 @@ unsafe partial class PixelShaderEffect lock (this.lockObject) { - this.cacheOutput = value; - - // If the effect is realized, set the property on the underlying ID2D1Effect object too + // If the effect is realized, set the property on the underlying ID2D1Effect object if (this.d2D1Effect.Get() is not null) { this.d2D1Effect.Get()->SetCachedProperty(value); } + else + { + this.cacheOutput = value; + } } } @@ -58,11 +100,11 @@ unsafe partial class PixelShaderEffect 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() switch { - this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); - } + not null => this.d2D1Effect.Get()->GetPrecisionProperty(), + _ => this.d2D1BufferPrecision + }; // Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN return this.d2D1BufferPrecision switch diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs index 2042522d..9904cecc 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs @@ -25,7 +25,11 @@ partial class PixelShaderEffect /// /// Gets or sets the current value in use. /// - public T Value { get; set; } + public T Value + { + get => GetConstantBuffer(); + set => SetConstantBuffer(in value); + } /// /// Gets the object associated with the current instance. diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs index e8a1273c..1b3a1754 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -43,6 +43,11 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan /// private ComPtr d2D1Effect; + /// + /// The backing field for . + /// + private T value; + /// /// The backing field for . ///