Add PixelShaderEffect<T>.ResourceTextureManagers property

このコミットが含まれているのは:
Sergio Pedri committed 2022-12-22 10:56:03 -08:00
1 親 4a5bf75db6
コミット e64b638454
6ファイル変更
+282 -47

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

+55 -6
ファイルの表示
@@ -1,12 +1,14 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System;
using ComputeSharp.D2D1.Extensions;
using TerraFX.Interop.DirectX;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
using TerraFX.Interop.Windows;
using System.Runtime.InteropServices;
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 TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#pragma warning disable CS0618
@@ -84,6 +86,53 @@ internal static unsafe class ID2D1EffectExtensions
return (D2D1TransformMapper<T>)GCHandle.FromIntPtr(handlePtr).Target!;
}
/// <summary>
/// Gets the <see cref="D2D1ResourceTextureManager"/> instance from a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="source">The current cached source instance.</param>
/// <param name="index">The index of the resource texture manager to retrieve.</param>
/// <returns>The <see cref="D2D1ResourceTextureManager"/> instance for <paramref name="d2D1Effect"/>.</returns>
public static D2D1ResourceTextureManager? GetResourceTextureManager(
this ref ID2D1Effect d2D1Effect,
D2D1ResourceTextureManager? source,
int index)
{
using ComPtr<ID2D1ResourceTextureManager> d2D1ResourceTextureManager = default;
// Get the ID2D1ResourceTextureManager object from the effect
d2D1Effect.GetValue(
index: (uint)(D2D1PixelShaderEffectProperty.ResourceTextureManager0 + index),
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN,
data: (byte*)d2D1ResourceTextureManager.GetAddressOf(),
dataSize: (uint)sizeof(void*)).Assert();
// If there is no resource texture manager, just return null (this is the default state)
if (d2D1ResourceTextureManager.Get() is null)
{
return null;
}
// Otherwise, the source must not be null and wrapping the same CCW
if (source is not null)
{
using ComPtr<ID2D1ResourceTextureManager> d2D1ResourceTextureManagerSource = default;
source.GetD2D1ResourceTextureManager(d2D1ResourceTextureManagerSource.GetAddressOf());
// Check if the instances match, and return the managed wrapper we already have
if (d2D1ResourceTextureManager.IsSameInstance(in d2D1ResourceTextureManagerSource))
{
return source;
}
}
// There was no way to retrieve the correct managed wrapper
ThrowHelper.ThrowInvalidOperationException("Cannot retrieve a D2D1ResourceTextureManager instance wrapping a native object that was set externally.");
return null;
}
/// <summary>
/// Gets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> property for a given <see cref="ID2D1Effect"/> object.
/// </summary>
+13 -1
ファイルの表示
@@ -166,7 +166,10 @@ unsafe partial class PixelShaderEffect<T>
return;
}
// Cache the last update values for the constant buffer, cache output and buffer precision properties
// Cache the last update values for the constant buffer, cache output, buffer precision properties, etc.
// Note: the resource texture managers are deliberately not read back, as the only supported way to set
// those is directly from the current effect instance. As such, they'd all either be the same instances
// that are already stored locally, or trying to retrieve them would just throw an exception.
this.value = this.d2D1Effect.Get()->GetConstantBuffer<T>();
this.transformMapper = this.d2D1Effect.Get()->GetTransformMapper<T>();
this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty();
@@ -240,6 +243,15 @@ unsafe partial class PixelShaderEffect<T>
this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision);
}
// Set all available resource texture managers (only set those that are not null, as they're all null by default anyway)
for (int i = 0; i < 16; i++)
{
if (ResourceTextureManagers.Storage[i] is D2D1ResourceTextureManager resourceTextureManager)
{
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), resourceTextureManager, i);
}
}
// TODO: port base CanvasEffect::Realize logic
// TODO: transfer properties (eg. D2D1ResourceTextureManager-s)
+43
ファイルの表示
@@ -89,6 +89,49 @@ unsafe partial class PixelShaderEffect<T>
}
}
/// <summary>
/// Gets the marshalled value for <see cref="ResourceTextureManagers"/>.
/// </summary>
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
/// <returns>The marshalled value for <see cref="ResourceTextureManagers"/>.</returns>
private D2D1ResourceTextureManager? GetResourceTextureManager(int index)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
lock (this.lockObject)
{
D2D1ResourceTextureManager? resourceTextureManager = ResourceTextureManagers.Storage[index];
return this.d2D1Effect.Get() switch
{
not null => this.d2D1Effect.Get()->GetResourceTextureManager(resourceTextureManager, index),
_ => resourceTextureManager
};
}
}
/// <summary>
/// Sets the marshalled value for <see cref="ResourceTextureManagers"/>.
/// </summary>
/// <param name="value">The value to set for <see cref="ResourceTextureManagers"/>.</param>
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
private void SetResourceTextureManager(D2D1ResourceTextureManager value, int index)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
lock (this.lockObject)
{
if (this.d2D1Effect.Get() is not null)
{
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), value, index);
}
else
{
ResourceTextureManagers.Storage[index] = value;
}
}
}
/// <summary>
/// Gets the marshalled value for <see cref="CacheOutput"/>.
/// </summary>
@@ -0,0 +1,165 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Interop;
namespace ComputeSharp.D2D1.Uwp;
/// <inheritdoc/>
partial class PixelShaderEffect<T>
{
/// <summary>
/// Represents the collection of <see cref="D2D1ResourceTextureManager"/> objects in a <see cref="PixelShaderEffect{T}"/> instance.
/// </summary>
public sealed class ResourceTextureManagerCollection
{
/// <summary>
/// The fixed buffer of <see cref="D2D1ResourceTextureManager"/> instances.
/// </summary>
private FixedBuffer fixedBuffer;
/// <summary>
/// Creates a new <see cref="ResourceTextureManagerCollection"/> instance with the specified parameters.
/// </summary>
/// <param name="owner">The owning <see cref="PixelShaderEffect{T}"/> instance.</param>
internal ResourceTextureManagerCollection(PixelShaderEffect<T> owner)
{
Owner = owner;
}
/// <summary>
/// The owning <see cref="PixelShaderEffect{T}"/> instance.
/// </summary>
internal PixelShaderEffect<T> Owner { get; }
/// <summary>
/// Gets a reference to the <see cref="FixedBuffer"/> value containing the available <see cref="D2D1ResourceTextureManager"/>-s.
/// </summary>
internal ref FixedBuffer Storage => ref this.fixedBuffer;
/// <summary>
/// Gets or sets the <see cref="D2D1ResourceTextureManager"/> object at a specified index.
/// </summary>
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
/// <returns>The <see cref="D2D1ResourceTextureManager"/> object at the specified index.</returns>
/// <exception cref="ArgumentNullException">Thrown if the value is set to <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
[DisallowNull]
public D2D1ResourceTextureManager? this[int index]
{
get => Owner.GetResourceTextureManager(index);
set => Owner.SetResourceTextureManager(value, index);
}
/// <summary>
/// A fixed buffer type containing 16 <see cref="D2D1ResourceTextureManager"/> fields.
/// </summary>
internal struct FixedBuffer
{
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 0.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager0;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 1.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager1;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 2.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager2;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 3.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager3;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 4.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager4;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 5.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager5;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 6.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager6;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 7.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager7;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 8.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager8;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 9.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager9;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 10.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager10;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 11.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager11;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 12.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager12;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 13.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager13;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 14.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager14;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 15.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager15;
/// <summary>
/// Gets a reference to the <see cref="D2D1ResourceTextureManager"/> value at a given index.
/// </summary>
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> value to get a reference to.</param>
/// <returns>A reference to the <see cref="D2D1ResourceTextureManager"/> value at a given index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
[UnscopedRef]
public ref D2D1ResourceTextureManager? this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((uint)index >= 16)
{
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range.");
}
ref D2D1ResourceTextureManager? r0 = ref Unsafe.As<FixedBuffer, D2D1ResourceTextureManager?>(ref this);
ref D2D1ResourceTextureManager? r1 = ref Unsafe.Add(ref r0, index);
return ref r1;
}
}
}
}
}
-34
ファイルの表示
@@ -89,38 +89,4 @@ partial class PixelShaderEffect<T>
set { }
}
}
/// <summary>
/// Represents the collection of <see cref="D2D1ResourceTextureManager"/> objects in a <see cref="PixelShaderEffect{T}"/> instance.
/// </summary>
public sealed class ResourceTextureManagerCollection
{
/// <summary>
/// Creates a new <see cref="ResourceTextureManagerCollection"/> instance with the specified parameters.
/// </summary>
/// <param name="owner">The owning <see cref="PixelShaderEffect{T}"/> instance.</param>
internal ResourceTextureManagerCollection(PixelShaderEffect<T> owner)
{
Owner = owner;
}
/// <summary>
/// The owning <see cref="PixelShaderEffect{T}"/> instance.
/// </summary>
internal PixelShaderEffect<T> Owner { get; }
/// <summary>
/// Gets or sets the <see cref="D2D1ResourceTextureManager"/> object at a specified index.
/// </summary>
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
/// <returns>The <see cref="D2D1ResourceTextureManager"/> object at the specified index.</returns>
/// <exception cref="ArgumentNullException">Thrown if the value is set to <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
[DisallowNull]
public D2D1ResourceTextureManager this[int index]
{
get => null!;
set { }
}
}
}
+6 -6
ファイルの表示
@@ -95,10 +95,10 @@ internal struct Rectangle64
/// <param name="rectangle">The input <see cref="Rectangle64"/> instance to compute the union with.</param>
public void Union(Rectangle64 rectangle)
{
long left = Math.Min(this.Left, rectangle.Left);
long top = Math.Min(this.Top, rectangle.Top);
long right = Math.Max(this.Right, rectangle.Right);
long bottom = Math.Max(this.Bottom, rectangle.Bottom);
long left = Math.Min(Left, rectangle.Left);
long top = Math.Min(Top, rectangle.Top);
long right = Math.Max(Right, rectangle.Right);
long bottom = Math.Max(Bottom, rectangle.Bottom);
this.x = left;
this.y = top;
@@ -114,8 +114,8 @@ internal struct Rectangle64
{
long left = Math.Max(this.x, rectangle.x);
long top = Math.Max(this.y, rectangle.y);
long right = Math.Min(this.Right, rectangle.Right);
long bottom = Math.Min(this.Bottom, rectangle.Bottom);
long right = Math.Min(Right, rectangle.Right);
long bottom = Math.Min(Bottom, rectangle.Bottom);
this.x = left;
this.y = top;