Refactor D2D sources logic, implement Unrealize()

このコミットが含まれているのは:
Sergio Pedri committed 2022-12-22 10:56:03 -08:00
1 親 eacda866a2
コミット a33f4ddbb7
5ファイル変更
+255 -213

検索条件に一致するファイルはありません

+11 -1
ファイルの表示
@@ -38,7 +38,7 @@ namespace ComputeSharp.D2D1.Uwp.Buffers;
/// if one is needed. For unrealized effects, the resource is <see langword="null"/> and only the wrapper part is used.
/// </para>
/// </remarks>
internal unsafe struct SourceReference
internal unsafe struct SourceReference : IDisposable
{
/// <summary>
/// The <see cref="ID2D1Image"/> produced by calling <see cref="ICanvasImageInterop.Interface.GetD2DImage"/>
@@ -107,6 +107,8 @@ internal unsafe struct SourceReference
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetWrapper(IGraphicsEffectSource? wrapper)
{
this.d2D1ImageSource.Dispose();
this.graphicsEffectSourceWrapper = wrapper;
}
@@ -151,4 +153,12 @@ internal unsafe struct SourceReference
return this.graphicsEffectSourceWrapper;
}
/// <inheritdoc/>
public void Dispose()
{
this.d2D1ImageSource.Dispose();
this.d2D1EffectDpiCompensation.Dispose();
this.graphicsEffectSourceWrapper = null;
}
}
-145
ファイルの表示
@@ -2,18 +2,13 @@ using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
using ComputeSharp.D2D1.Uwp.Buffers;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Windows.Graphics.Effects;
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
using Win32 = TerraFX.Interop.Windows.Windows;
#pragma warning disable CS0618
@@ -91,146 +86,6 @@ internal static unsafe class ID2D1EffectExtensions
return (D2D1TransformMapper<T>)GCHandle.FromIntPtr(handlePtr).Target!;
}
/// <summary>
/// Gets the <see cref="IGraphicsEffectSource"/> source from a given <see cref="ID2D1Effect"/> object, at a specified index.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="canvasDevice">The realization device currently in use.</param>
/// <param name="source">The input <see cref="SourceReference"/> object for the target index.</param>
/// <param name="index">The index for the source to retrieve.</param>
/// <returns>The <see cref="IGraphicsEffectSource"/> source from a given <see cref="ID2D1Effect"/> object, at a specified index.</returns>
public static IGraphicsEffectSource? GetSource(
this ref ID2D1Effect d2D1Effect,
ICanvasDevice* canvasDevice,
ref SourceReference source,
int index)
{
using ComPtr<ID2D1Image> d2D1Image = default;
// Get the ID2D1Image input from the effect
d2D1Effect.GetInput(
index: (uint)index,
input: d2D1Image.GetAddressOf());
// Check whether the input had a DPI compensation effect added to it, and skip it if so
if (source.HasDpiCompensationEffect)
{
// Since a realized effect is the only source of authority for the effect state, we also
// need to check that the actual source is in fact the same that was previous set by the
// managed wrapper. If it is, we get the input and return that (ie. the underlying image).
// If not, we just return it directly and reset the locally stored DPI compensation effect.
if (d2D1Image.Get()->IsSameInstance(source.GetDpiCompensationEffect()))
{
source.GetDpiCompensationEffect()->GetInput(0, d2D1Image.ReleaseAndGetAddressOf());
}
else
{
source.SetDpiCompensationEffect(null);
}
}
// Get or create a wrapper for the input image
return source.GetOrCreateWrapper(null, d2D1Image.Get());
}
/// <summary>
/// Sets the <see cref="IGraphicsEffectSource"/> source for a given <see cref="ID2D1Effect"/> object, at a specified index.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="canvasDevice">The realization device currently in use.</param>
/// <param name="deviceContext">The device context currently in use.</param>
/// <param name="flags">The realization flags to use.</param>
/// <param name="targetDpi">The target DPI value for the source.</param>
/// <param name="value">The input <see cref="IGraphicsEffectSource"/> value to set.</param>
/// <param name="source">The target <see cref="SourceReference"/> value, for caching.</param>
/// <param name="index">The index of the source to set.</param>
/// <returns>Whether the operation succeeded (if it didn't the effect should be unrealized).</returns>
public static bool TrySetSource(
this ref ID2D1Effect d2D1Effect,
ICanvasDevice* canvasDevice,
ID2D1DeviceContext* deviceContext,
WIN2D_GET_D2D_IMAGE_FLAGS flags,
float targetDpi,
IGraphicsEffectSource? value,
ref SourceReference source,
int index)
{
using ComPtr<ID2D1Image> d2D1Image = default;
float realizedDpi = 0;
if (value is not null)
{
using ComPtr<IUnknown> canvasImageUnknown = default;
// Get the underlying IUnknown object for the input source
canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value));
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
// Try to get the ICanvasImageInterop interface from the input source
HRESULT hresult = canvasImageUnknown.CopyTo(canvasImageInterop.GetAddressOf());
if (!Win32.SUCCEEDED(hresult))
{
// If unrealization isn't requested for failures, just throw
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is not valid (it must implement ICanvasImageInterop).");
}
return false;
}
using ComPtr<ICanvasDevice> sourceCanvasDevice = default;
// Get the canvas device the source is realized on, if any
canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf()).Assert();
// If a device was retrieved, it must be the same as the input one. If the source has no device,
// that is fine, as it just means that its underlying D2D image has not been realized yet.
if (sourceCanvasDevice.Get() is not null && !canvasDevice->IsSameInstance(sourceCanvasDevice.Get()))
{
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is realized on a different canvas device.");
}
return false;
}
// Try to realize the image from the input source
hresult = canvasImageInterop.Get()->GetD2DImage(
device: canvasDevice,
deviceContext: deviceContext,
flags: flags,
targetDpi: targetDpi,
realizeDpi: &realizedDpi,
ppImage: d2D1Image.GetAddressOf());
// Unless requested, a failure to retrieve the image should just bubble up the HRESULT in an exception
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
hresult.Assert();
}
}
else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
// If the source is null and this is not explicitly allowed, the invocation is not valid
ThrowHelper.ThrowArgumentNullException(nameof(value), "The effect source cannot be null.");
}
// Save the managed wrapper and realized image
source.SetWrapperAndResource(value, d2D1Image.Get());
// TODO: handle DPI compensation
// Set the actual effect input
d2D1Effect.SetInput(index: (uint)index, input: d2D1Image.Get());
return true;
}
/// <summary>
/// Gets the <see cref="D2D1ResourceTextureManager"/> instance from a given <see cref="ID2D1Effect"/> object.
/// </summary>
+33 -2
ファイルの表示
@@ -8,6 +8,7 @@ using ComputeSharp.D2D1.Uwp.Extensions;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Windows.Graphics.Effects;
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
using Win32 = TerraFX.Interop.Windows.Windows;
@@ -159,7 +160,22 @@ unsafe partial class PixelShaderEffect<T>
/// <summary>
/// Unrealizes the currently realized <see cref="ID2D1Effect"/> object.
/// </summary>
private void Unrealize()
/// <param name="source">The value to set for <see cref="Sources"/>.</param>
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
private void Unrealize(IGraphicsEffectSource source, int index)
{
// Unrealize the effect and indicate to the logic to skip the source at the current index.
// That is, there is no need to marshal it back to the cache, as we're about to do that here.
Unrealize(index);
// Once the effect is unrealized, also update the cached source managed wrapper
Sources.Storage[index].SetWrapper(source);
}
/// <summary>
/// Unrealizes the currently realized <see cref="ID2D1Effect"/> object.
/// </summary>
private void Unrealize(int? sourceIndexToSkip = null)
{
// If there is no ID2D1Effect object, there is nothing left to do
if (this.d2D1Effect.Get() is null)
@@ -176,7 +192,22 @@ unsafe partial class PixelShaderEffect<T>
this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty();
this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty();
// TODO: read back D2D sources from the effect
// Loop over all effect inputs and update the cache back as well
for (int i = 0; i < 16; i++)
{
// If this index has been explicitly requested to be skipped, just dispose the source reference.
// This is the case when the effect is being unrealized, and callers will set this value on their own.
if (i == sourceIndexToSkip)
{
Sources.Storage[i].Dispose();
}
else
{
// Otherwise, just get the input, which will automatically update the cached values. The
// returned IGraphicsEffectSource managed wrapper is not used here and is safe to ignore.
_ = GetD2DInput(i);
}
}
// Finally release the effect as well
this.d2D1Effect.Dispose();
+211
ファイルの表示
@@ -0,0 +1,211 @@
using System.Runtime.InteropServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Uwp.Buffers;
using ComputeSharp.D2D1.Uwp.Extensions;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Windows.Graphics.Effects;
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
using Win32 = TerraFX.Interop.Windows.Windows;
namespace ComputeSharp.D2D1.Uwp;
/// <inheritdoc/>
unsafe partial class PixelShaderEffect<T>
{
/// <summary>
/// Gets the marshalled value for <see cref="Sources"/>.
/// </summary>
/// <param name="index">The index of the <see cref="Sources"/> source to get or set.</param>
/// <returns>The marshalled value for <see cref="Sources"/>.</returns>
private IGraphicsEffectSource? GetSource(int index)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
lock (this.lockObject)
{
return this.d2D1Effect.Get() switch
{
not null => GetD2DInput(index),
_ => Sources.Storage[index].GetWrapper()
};
}
}
/// <summary>
/// Sets the marshalled value for <see cref="Sources"/>.
/// </summary>
/// <param name="value">The value to set for <see cref="Sources"/>.</param>
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
private void SetSource(IGraphicsEffectSource? value, int index)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
lock (this.lockObject)
{
ref SourceReference source = ref Sources.Storage[index];
if (this.d2D1Effect.Get() is not null)
{
const WIN2D_GET_D2D_IMAGE_FLAGS flags =
WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION |
WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS |
WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE;
// The WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE flag is passed, so we just let the effect
// unrealize in case of failure and take no other action (including throwing) if that happens.
_ = SetD2DInput(index, value, flags, targetDpi: 0.0f, d2D1DeviceContext: null);
}
else
{
source.SetWrapper(value);
}
}
}
/// <remarks>This method assumes a lock is taken and the effect is already realized.</remarks>
/// <inheritdoc cref="GetSource"/>
private IGraphicsEffectSource? GetD2DInput(int index)
{
using ComPtr<ID2D1Image> d2D1Image = default;
// Get the ID2D1Image input from the effect
this.d2D1Effect.Get()->GetInput(
index: (uint)index,
input: d2D1Image.GetAddressOf());
ref SourceReference sourceReference = ref Sources.Storage[index];
// Check whether the input had a DPI compensation effect added to it, and skip it if so
if (sourceReference.HasDpiCompensationEffect)
{
// Since a realized effect is the only source of authority for the effect state, we also
// need to check that the actual source is in fact the same that was previous set by the
// managed wrapper. If it is, we get the input and return that (ie. the underlying image).
// If not, we just return it directly and reset the locally stored DPI compensation effect.
if (d2D1Image.Get()->IsSameInstance(sourceReference.GetDpiCompensationEffect()))
{
sourceReference.GetDpiCompensationEffect()->GetInput(0, d2D1Image.ReleaseAndGetAddressOf());
}
else
{
sourceReference.SetDpiCompensationEffect(null);
}
}
// Get or create a wrapper for the input image
return sourceReference.GetOrCreateWrapper(null, d2D1Image.Get());
}
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
/// <param name="value">The value to set for <see cref="Sources"/>.</param>
/// <param name="flags">The <see cref="WIN2D_GET_D2D_IMAGE_FLAGS"/> flags for <paramref name="value"/>.</param>
/// <param name="targetDpi">The target DPI for the realized image, if present.</param>
/// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> object to use, if present.</param>
/// <returns>Whether the input was set successfully or not.</returns>
/// <remarks>This method assumes a lock is taken and the effect is already realized.</remarks>
/// <inheritdoc cref="SetSource"/>
private bool SetD2DInput(
int index,
IGraphicsEffectSource? value,
WIN2D_GET_D2D_IMAGE_FLAGS flags,
float targetDpi,
ID2D1DeviceContext* d2D1DeviceContext)
{
using ComPtr<ID2D1Image> d2D1Image = default;
float realizedDpi = 0;
if (value is not null)
{
using ComPtr<IUnknown> canvasImageUnknown = default;
// Get the underlying IUnknown object for the input source
canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value));
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
// Try to get the ICanvasImageInterop interface from the input source
HRESULT hresult = canvasImageUnknown.CopyTo(canvasImageInterop.GetAddressOf());
if (!Win32.SUCCEEDED(hresult))
{
// Only potentially handle E_NOINTERFACE, always throw in other cases
if (hresult != E.E_NOINTERFACE)
{
Marshal.ThrowExceptionForHR(hresult);
}
// If unrealization isn't requested for failures, just throw
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is not valid (it must implement ICanvasImageInterop).");
}
Unrealize(value, index);
return false;
}
using ComPtr<ICanvasDevice> sourceCanvasDevice = default;
// Get the canvas device the source is realized on, if any
canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf()).Assert();
// If a device was retrieved, it must be the same as the input one. If the source has no device,
// that is fine, as it just means that its underlying D2D image has not been realized yet.
if (sourceCanvasDevice.Get() is not null && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get()))
{
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is realized on a different canvas device.");
}
Unrealize(value, index);
return false;
}
// Try to realize the image from the input source (this will recurse through the effect graph)
hresult = canvasImageInterop.Get()->GetD2DImage(
device: this.canvasDevice.Get(),
deviceContext: d2D1DeviceContext,
flags: flags,
targetDpi: targetDpi,
realizeDpi: &realizedDpi,
ppImage: d2D1Image.GetAddressOf());
// Unless requested, a failure to retrieve the image should just bubble up the HRESULT in an exception
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
hresult.Assert();
}
// If no realized image is available, unrealize the effect
if (d2D1Image.Get() is null)
{
Unrealize(value, index);
return false;
}
}
else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{
// If the source is null and this is not explicitly allowed, the invocation is not valid
ThrowHelper.ThrowArgumentNullException(nameof(value), "The effect source cannot be null.");
}
// Save the managed wrapper and realized image
Sources.Storage[index].SetWrapperAndResource(value, d2D1Image.Get());
// TODO: handle DPI compensation
// Set the actual effect input
this.d2D1Effect.Get()->SetInput(index: (uint)index, input: d2D1Image.Get());
return true;
}
}
-65
ファイルの表示
@@ -1,12 +1,8 @@
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Uwp.Buffers;
using ComputeSharp.D2D1.Uwp.Extensions;
using ComputeSharp.Interop;
using Microsoft.Graphics.Canvas;
using TerraFX.Interop.DirectX;
using Windows.Graphics.Effects;
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
namespace ComputeSharp.D2D1.Uwp;
@@ -93,67 +89,6 @@ unsafe partial class PixelShaderEffect<T>
}
}
/// <summary>
/// Gets the marshalled value for <see cref="Sources"/>.
/// </summary>
/// <param name="index">The index of the <see cref="Sources"/> source to get or set.</param>
/// <returns>The marshalled value for <see cref="Sources"/>.</returns>
private IGraphicsEffectSource? GetSource(int index)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
lock (this.lockObject)
{
ref SourceReference source = ref Sources.Storage[index];
return this.d2D1Effect.Get() switch
{
not null => this.d2D1Effect.Get()->GetSource(this.canvasDevice.Get(), ref source, index),
_ => source.GetWrapper()
};
}
}
/// <summary>
/// Sets the marshalled value for <see cref="Sources"/>.
/// </summary>
/// <param name="value">The value to set for <see cref="Sources"/>.</param>
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
private void SetSource(IGraphicsEffectSource? value, int index)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
lock (this.lockObject)
{
ref SourceReference source = ref Sources.Storage[index];
if (this.d2D1Effect.Get() is not null)
{
const WIN2D_GET_D2D_IMAGE_FLAGS sourceFlags =
WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION |
WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS |
WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE;
// Try to set the source, and unrealize if the operation failed
if (!this.d2D1Effect.Get()->TrySetSource(
canvasDevice: this.canvasDevice.Get(),
deviceContext: null,
flags: sourceFlags,
targetDpi: 0,
value: value,
source: ref source,
index: index))
{
Unrealize();
}
}
else
{
source.SetWrapper(value);
}
}
}
/// <summary>
/// Gets the marshalled value for <see cref="ResourceTextureManagers"/>.
/// </summary>