From 34cbd2d83f5496c9579f3187be270a806e7e2023 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Oct 2022 00:38:47 +0200 Subject: [PATCH] Move ID2D1Effect property getters to extensions --- .../Extensions/ID2D1EffectExtensions.cs | 76 +++++++++++++++++ .../PixelShaderEffect{T}.cs | 84 +++---------------- 2 files changed, 89 insertions(+), 71 deletions(-) create mode 100644 src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs new file mode 100644 index 00000000..ffd7a850 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -0,0 +1,76 @@ +using ComputeSharp.D2D1.Extensions; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.D2D1.Uwp.Extensions; + +/// +/// A with extensions for the type. +/// +internal static unsafe class ID2D1EffectExtensions +{ + /// + /// Gets the property for a given object. + /// + /// The input instance. + /// The value for . + public static bool GetCachedProperty(this ref ID2D1Effect d2D1Effect) + { + int cacheOutput; + + d2D1Effect.GetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, + data: (byte*)&cacheOutput, + dataSize: sizeof(int)).Assert(); + + return cacheOutput != 0; + } + + /// + /// Sets the property for a given object. + /// + /// The input instance. + /// The property value to set. + public static void SetCachedProperty(this ref ID2D1Effect d2D1Effect, bool value) + { + int cacheOutput = value ? 1 : 0; + + d2D1Effect.SetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, + data: (byte*)&cacheOutput, + dataSize: sizeof(int)).Assert(); + } + + /// + /// Gets the property for a given object. + /// + /// The input instance. + /// The value for . + public static D2D1_BUFFER_PRECISION GetPrecisionProperty(this ref ID2D1Effect d2D1Effect) + { + D2D1_BUFFER_PRECISION d2D1BufferPrecision; + + d2D1Effect.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(); + + return d2D1BufferPrecision; + } + + /// + /// Sets the property for a given object. + /// + /// The input instance. + /// The property value to set. + public static void SetPrecisionProperty(this ref ID2D1Effect d2D1Effect, D2D1_BUFFER_PRECISION value) + { + d2D1Effect.SetValue( + index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&value, + dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert(); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs index 42cb34b2..a8b8fc47 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -54,7 +54,7 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan /// /// The backing field for . /// - private int cacheOutput; + private bool cacheOutput; /// /// The backing field for . @@ -80,18 +80,10 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan // 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; + this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); } - return this.cacheOutput != 0; + return this.cacheOutput; } } set @@ -100,18 +92,12 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan lock (this.lockObject) { - this.cacheOutput = value ? 1 : 0; + this.cacheOutput = value; // 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(); + this.d2D1Effect.Get()->SetCachedProperty(value); } } } @@ -132,15 +118,7 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan // 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; + this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); } // Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN @@ -175,13 +153,7 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan // 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(); + this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision); } } } @@ -350,27 +322,9 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan return; } - // 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; + // Cache the last update value of the cache and precision properties + this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); + this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); // TODO: read back D2D sources from the effect @@ -406,27 +360,15 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan } // If cache input has been set, forward that to the new effect - if (this.cacheOutput != 0) + if (this.cacheOutput) { - 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(); + this.d2D1Effect.Get()->SetCachedProperty(true); } // 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(); + this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision); } // TODO: port base CanvasEffect::Realize logic