Move 'ComputeSharp.D2D1.WinUI' files to shared project

このコミットが含まれているのは:
Sergio Pedri committed 2024-08-13 18:02:58 -07:00
1 親 c106c0fd50
コミット a44b585927
46ファイル変更
+49 -6

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

+77
ファイルの表示
@@ -0,0 +1,77 @@
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.Win32;
namespace ComputeSharp.D2D1.WinUI.Extensions;
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="ICanvasDevice"/> type.
/// </summary>
internal static unsafe class ICanvasDeviceExtensions
{
/// <summary>
/// Gets the underlying <see cref="ID2D1Device1"/> object from an <see cref="ICanvasDevice"/> object.
/// </summary>
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object.</param>
/// <param name="d2D1Device1">The underlying <see cref="ID2D1Device1"/> object for <paramref name="canvasDevice"/>.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
public static HRESULT GetD2DDevice(this ref ICanvasDevice canvasDevice, ID2D1Device1** d2D1Device1)
{
using ComPtr<ICanvasResourceWrapperNative> canvasDeviceResourceWrapperNative = default;
// Get the ICanvasResourceWrapperNative wrapper around the input device
canvasDevice.QueryInterface(
riid: Win32.Windows.__uuidof<ICanvasResourceWrapperNative>(),
ppvObject: (void**)canvasDeviceResourceWrapperNative.GetAddressOf()).Assert();
// Get the underlying ID2D1Device1 instance in use
HRESULT hresult = canvasDeviceResourceWrapperNative.Get()->GetNativeResource(
device: null,
dpi: 0,
iid: Win32.Windows.__uuidof<ID2D1Device1>(),
resource: (void**)d2D1Device1);
return hresult;
}
/// <summary>
/// Retrieves a usable <see cref="ID2D1DeviceContext"/> from an input <see cref="ICanvasDevice"/> if one is not available.
/// If a new one had to be rented from the pool, it also returns the owning <see cref="ID2D1DeviceContextLease"/> instance.
/// </summary>
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object.</param>
/// <param name="d2D1DeviceContext">The current <see cref="ID2D1DeviceContext"/> object, if available.</param>
/// <param name="d2D1DeviceContextEffective">The resulting <see cref="ID2D1DeviceContext"/> instance to use.</param>
/// <param name="d2D1DeviceContextLease">The resulting <see cref="ID2D1DeviceContextLease"/> object, if used.</param>
/// <remarks>
/// When this method returns, <paramref name="d2D1DeviceContextEffective"/> is guaranteed to not be <see langword="null"/>.
/// </remarks>
public static void GetEffectiveD2DDeviceContextWithOptionalLease(
this ref ICanvasDevice canvasDevice,
ID2D1DeviceContext* d2D1DeviceContext,
ID2D1DeviceContext** d2D1DeviceContextEffective,
ID2D1DeviceContextLease** d2D1DeviceContextLease)
{
// If there is no input device context, just create a new one from the input canvas device
if (d2D1DeviceContext is null)
{
using (ComPtr<ID2D1DeviceContextPool> d2D1DeviceContextPool = default)
{
// Get the ID2D1DeviceContextPool interface reference
canvasDevice.QueryInterface(
riid: Win32.Windows.__uuidof<ID2D1DeviceContextPool>(),
ppvObject: (void**)d2D1DeviceContextPool.GetAddressOf()).Assert();
// Get a new ID2D1DeviceContextLease object from the retrieved pool
d2D1DeviceContextPool.Get()->GetDeviceContextLease(d2D1DeviceContextLease).Assert();
}
// Get the underlying device context from the lease (this is much faster than creating a new device context)
(*d2D1DeviceContextLease)->GetD2DDeviceContext(d2D1DeviceContextEffective).Assert();
}
else
{
// Otherwise, just use the input device context
*d2D1DeviceContextEffective = new ComPtr<ID2D1DeviceContext>(d2D1DeviceContext).Get();
}
}
}
+79
ファイルの表示
@@ -0,0 +1,79 @@
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.Win32;
namespace ComputeSharp.D2D1.WinUI.Extensions;
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="ID2D1DeviceContext"/> type.
/// </summary>
internal static unsafe class ID2D1DeviceContextExtensions
{
/// <summary>
/// Checks whether a given <see cref="ID2D1DeviceContext"/> has an <see cref="ID2D1CommandList"/> as render target.
/// </summary>
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance to check.</param>
/// <returns>Whether the target of <paramref name="deviceContext"/> is an <see cref="ID2D1CommandList"/>.</returns>
public static bool HasCommandListTarget(this ref ID2D1DeviceContext deviceContext)
{
using ComPtr<ID2D1Image> d2D1Image = default;
// First get the target and check that it is not null
deviceContext.GetTarget(d2D1Image.GetAddressOf());
if (d2D1Image.Get() is null)
{
return false;
}
using ComPtr<ID2D1CommandList> d2D1CommandList = default;
// If there is a target, check if it is an ID2D1CommandList
HRESULT hresult = d2D1Image.CopyTo(d2D1CommandList.GetAddressOf());
// Special case E_NOINTERFACE: this should return false and it's not an error
if (hresult == E.E_NOINTERFACE)
{
return false;
}
// Throw for any other failure
hresult.Assert();
return true;
}
/// <summary>
/// Reads the DPI value from a target <see cref="ID2D1DeviceContext"/> object.
/// </summary>
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance.</param>
/// <returns>The DPIs from <paramref name="deviceContext"/>.</returns>
public static float GetDpi(this ref ID2D1DeviceContext deviceContext)
{
float dpiX;
float dpiY;
deviceContext.GetDpi(&dpiX, &dpiY);
return dpiX;
}
/// <summary>
/// Gets an <see cref="ID2D1Factory1"/> object from a given <see cref="ID2D1DeviceContext"/>.
/// </summary>
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance.</param>
/// <param name="d2D1Factory1">The resulting <see cref="ID2D1Factory1"/> object.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
/// <remarks>
/// The <see cref="Interop.D2D1PixelShaderEffect"/> APIs specifically need an <see cref="ID2D1Factory1"/> (as <see cref="ID2D1Factory1.RegisterEffectFromString"/> is used).
/// </remarks>
public static HRESULT GetFactory1(this ref ID2D1DeviceContext deviceContext, ID2D1Factory1** d2D1Factory1)
{
using ComPtr<ID2D1Factory> d2D1Factory = default;
// Get the underlying ID2D1Factory from the input context, which can be used to register effects
deviceContext.GetFactory(d2D1Factory.GetAddressOf());
// QueryInterface for the ID2D1Factory1 object (which should always be present)
return d2D1Factory.CopyTo(d2D1Factory1);
}
}
+233
ファイルの表示
@@ -0,0 +1,233 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.Descriptors;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
using ComputeSharp.Win32;
namespace ComputeSharp.D2D1.WinUI.Extensions;
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="ID2D1Effect"/> type.
/// </summary>
internal static unsafe class ID2D1EffectExtensions
{
/// <summary>
/// Gets the constant buffer of type <typeparamref name="T"/> from a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <typeparam name="T">The type of shader being used.</typeparam>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <returns>The constant buffer of type <typeparamref name="T"/> for <paramref name="d2D1Effect"/>.</returns>
public static T GetConstantBuffer<T>(this ref ID2D1Effect d2D1Effect)
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
int constantBufferSize = T.ConstantBufferSize;
// Special cases the constant buffer type being empty. If that's the case, there is no
// need to try to retrieve the constant buffer from the underlying effect anyway, and
// most importantly, the ID2D1EffectImpl from ComputeSharp.D2D1 will return E_INVALIDARG.
if (constantBufferSize == 0)
{
return default;
}
byte[]? bufferArray = null;
Span<byte> buffer = constantBufferSize <= 64
? stackalloc byte[64]
: bufferArray = ArrayPool<byte>.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();
}
T shader = T.CreateFromConstantBuffer(buffer);
if (bufferArray is not null)
{
ArrayPool<byte>.Shared.Return(bufferArray);
}
return shader;
}
/// <summary>
/// Sets the constant buffer of type <typeparamref name="T"/> from a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <typeparam name="T">The type of shader being used.</typeparam>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="value">The constant buffer value to set.</param>
public static void SetConstantBuffer<T>(this ref ID2D1Effect d2D1Effect, in T value)
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
// Same as above, simply do nothing for empty types. This case is still handled to
// avoid crashing when someone's just setting an empty constant buffer, which is
// unnecessary but still valid (eg. a stateless shader). This could be the case if
// someone was eg. binding or using some other automated system to set constant buffer.
if (T.ConstantBufferSize == 0)
{
return;
}
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect((ID2D1Effect*)Unsafe.AsPointer(ref d2D1Effect), in value);
}
/// <summary>
/// Gets the <see cref="D2D1DrawTransformMapper{T}"/> instance from a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <typeparam name="T">The type of shader being used.</typeparam>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <returns>The <see cref="D2D1DrawTransformMapper{T}"/> instance for <paramref name="d2D1Effect"/>.</returns>
public static D2D1DrawTransformMapper<T>? GetTransformMapper<T>(this ref ID2D1Effect d2D1Effect)
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
using ComPtr<ID2D1DrawTransformMapper> d2D1TransformMapper = default;
// Get the ID2D1DrawTransformMapper object from the effect
d2D1Effect.GetValue(
index: D2D1PixelShaderEffectProperty.TransformMapper,
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN,
data: (byte*)d2D1TransformMapper.GetAddressOf(),
dataSize: (uint)sizeof(void*)).Assert();
// If there is no transform, just return null (this is the default state)
if (d2D1TransformMapper.Get() is null)
{
return null;
}
// Otherwise, check if it's an internal CCW and retrieve the managed wrapper
using ComPtr<ID2D1DrawTransformMapperInternal> d2D1TransformMapperInternal = default;
d2D1TransformMapper.CopyTo(d2D1TransformMapperInternal.GetAddressOf()).Assert();
IntPtr handlePtr;
// Get the underlying GCHandle value
d2D1TransformMapperInternal.Get()->GetManagedWrapperHandle((void**)&handlePtr).Assert();
// Retrieve the managed wrapper from the GCHandle
return (D2D1DrawTransformMapper<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.Get()->IsSameInstance(d2D1ResourceTextureManagerSource.Get()))
{
return source;
}
}
// There was no way to retrieve the correct managed wrapper
return default(InvalidOperationException).Throw<D2D1ResourceTextureManager?>();
}
/// <summary>
/// Gets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> property for a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <returns>The <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> value for <paramref name="d2D1Effect"/>.</returns>
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;
}
/// <summary>
/// Sets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> property for a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="value">The property value to set.</param>
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();
}
/// <summary>
/// Gets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> property for a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <returns>The <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> value for <paramref name="d2D1Effect"/>.</returns>
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;
}
/// <summary>
/// Sets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> property for a given <see cref="ID2D1Effect"/> object.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="value">The property value to set.</param>
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();
}
}
+80
ファイルの表示
@@ -0,0 +1,80 @@
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.Win32;
namespace ComputeSharp.D2D1.WinUI.Extensions;
/// <summary>
/// Helper methods for working with the <see cref="IUnknown"/> type.
/// </summary>
internal static unsafe class IUnknownExtensions
{
/// <summary>
/// Checks whether two <see cref="IUnknown"/> values have the same underlying COM object.
/// </summary>
/// <typeparam name="T">The type of the first value to check.</typeparam>
/// <typeparam name="U">The type of the second value to check.</typeparam>
/// <param name="left">The first <see cref="IUnknown"/> object to compare.</param>
/// <param name="right">The second <see cref="IUnknown"/> object to compare.</param>
/// <returns>Whether <paramref name="left"/> and <paramref name="right"/> point to the same COM object.</returns>
public static bool IsSameInstance<T, U>(this ref T left, U* right)
where T : unmanaged // IUnknown
where U : unmanaged // IUnknown
{
if (Unsafe.AreSame(ref left, ref *(T*)right))
{
return true;
}
if (Unsafe.IsNullRef(ref left) || right is null)
{
return false;
}
using ComPtr<IUnknown> leftUnknown = default;
using ComPtr<IUnknown> rightUnknown = default;
((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.Windows.__uuidof<IUnknown>(), (void**)leftUnknown.GetAddressOf()).Assert();
((IUnknown*)right)->QueryInterface(Win32.Windows.__uuidof<IUnknown>(), (void**)rightUnknown.GetAddressOf()).Assert();
return leftUnknown.Get() == rightUnknown.Get();
}
/// <summary>
/// Copies the source <see cref="IUnknown"/> object onto a target <see cref="ComPtr{T}"/> destination.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The source <see cref="IUnknown"/> object.</param>
/// <param name="destination">The destination <see cref="ComPtr{T}"/> location.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
public static HRESULT CopyTo<T>(this ref T source, ref ComPtr<T> destination)
where T : unmanaged // IUnknown
{
// Note: this explicitly needs a temporary copy to avoid issues in case source and destination were
// pointers to the same object (which can be the case even if the two pointers are not identical).
// To double check, a full check through an IUnknown* cast would be needed, but to avoid the extra
// QueryInterface call on both objects, we can just always do one AddRef call which avoids issues in
// all cases as well. The situation we need to avoid is that without a temporary copy, releasing the
// target object might actually destroy the object, causing the following AddRef call to AV.
using ComPtr<T> temporary = new((T*)Unsafe.AsPointer(ref source));
return temporary.CopyTo(ref destination);
}
/// <summary>
/// Copies the source <see cref="IUnknown"/> object onto a target <typeparamref name="U"/> location.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <typeparam name="U">The type of destination object being copied.</typeparam>
/// <param name="source">The source <see cref="IUnknown"/> object.</param>
/// <param name="destination">The destination <typeparamref name="U"/> location.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
public static HRESULT CopyTo<T, U>(this ref T source, U** destination)
where T : unmanaged // IUnknown
where U : unmanaged, IComObject
{
using ComPtr<T> temporary = new((T*)Unsafe.AsPointer(ref source));
return temporary.CopyTo(destination);
}
}