Remove ID2D1TransformMapperFactory<T> from APIs
このコミットが含まれているのは:
1 親
eaac9269b2
コミット
7d3942611a
4ファイル変更
+160
-341
検索条件に一致するファイルはありません
@@ -35,53 +35,8 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null.");
|
||||
}
|
||||
|
||||
RegisterForD2D1Factory1<T>(d2D1Factory1, null, out effectId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an effect from an input D2D1 pixel shader, by calling <c>ID2D1Factory1::RegisterEffectFromString</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
|
||||
/// <typeparam name="TMapper">The type of <see cref="ID2D1TransformMapper{T}"/> implementation to register.</typeparam>
|
||||
/// <param name="d2D1Factory1">A pointer to the <c>ID2D1Factory1</c> instance to use.</param>
|
||||
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="d2D1Factory1"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
|
||||
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
|
||||
public static void RegisterForD2D1Factory1<T, TMapper>(void* d2D1Factory1, out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
where TMapper : class, ID2D1TransformMapper<T>, new()
|
||||
{
|
||||
if (d2D1Factory1 is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null.");
|
||||
}
|
||||
|
||||
RegisterForD2D1Factory1(d2D1Factory1, D2D1TransformMapper.FactoryOf<T, TMapper>.Instance, out effectId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an effect from an input D2D1 pixel shader, by calling <c>ID2D1Factory1::RegisterEffectFromString</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
|
||||
/// <param name="d2D1Factory1">A pointer to the <c>ID2D1Factory1</c> instance to use.</param>
|
||||
/// <param name="mapperFactory">An optional factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for the transform.</param>
|
||||
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="d2D1Factory1"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
|
||||
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
|
||||
public static void RegisterForD2D1Factory1<T>(void* d2D1Factory1, ID2D1TransformMapperFactory<T>? mapperFactory, out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
if (d2D1Factory1 is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null.");
|
||||
}
|
||||
|
||||
effectId = default;
|
||||
|
||||
PixelShaderEffect.For<T>.Initialize(mapperFactory);
|
||||
|
||||
ArrayPoolBufferWriter<char> writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize);
|
||||
|
||||
// Build the XML text
|
||||
@@ -101,7 +56,7 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
""");
|
||||
|
||||
// Add the input nodes
|
||||
for (int i = 0; i < PixelShaderEffect.For<T>.InputCount; i++)
|
||||
for (int i = 0; i < PixelShaderEffect.For<T>.Instance.InputCount; i++)
|
||||
{
|
||||
writer.WriteRaw(" <Input name='Source");
|
||||
writer.WriteAsUnicode(i);
|
||||
@@ -257,7 +212,7 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
d2D1PropertyBinding[16].setFunction = PixelShaderEffect.SetResourceTextureManager15;
|
||||
d2D1PropertyBinding[17].setFunction = PixelShaderEffect.SetTransformMapper;
|
||||
|
||||
fixed (Guid* pGuid = &PixelShaderEffect.For<T>.Id)
|
||||
fixed (Guid* pGuid = &PixelShaderEffect.For<T>.Instance.Id)
|
||||
{
|
||||
// Register the effect
|
||||
((ID2D1Factory1*)d2D1Factory1)->RegisterEffectFromString(
|
||||
@@ -265,50 +220,19 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
propertyXml: (ushort*)pXml,
|
||||
bindings: d2D1PropertyBinding,
|
||||
bindingsCount: 18,
|
||||
effectFactory: PixelShaderEffect.For<T>.Factory).Assert();
|
||||
effectFactory: PixelShaderEffect.For<T>.Instance.Factory).Assert();
|
||||
}
|
||||
|
||||
effectId = PixelShaderEffect.For<T>.Id;
|
||||
effectId = PixelShaderEffect.For<T>.Instance.Id;
|
||||
}
|
||||
|
||||
writer.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a binary blob containing serialized information that can be used to register an effect, by using <c>ID2D1Factory1::RegisterEffectFromString</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
|
||||
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
|
||||
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
|
||||
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(ID2D1TransformMapperFactory{T}?, out Guid)"/>.</remarks>
|
||||
public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
return GetRegistrationBlob<T>(null, out effectId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a binary blob containing serialized information that can be used to register an effect, by using <c>ID2D1Factory1::RegisterEffectFromString</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to register.</typeparam>
|
||||
/// <typeparam name="TMapper">The type of <see cref="ID2D1TransformMapper{T}"/> implementation to register.</typeparam>
|
||||
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
|
||||
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
|
||||
/// <remarks>For more info and for details on the binary format, see <see cref="GetRegistrationBlob{T}(ID2D1TransformMapperFactory{T}?, out Guid)"/>.</remarks>
|
||||
public static ReadOnlyMemory<byte> GetRegistrationBlob<T, TMapper>(out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
where TMapper : class, ID2D1TransformMapper<T>, new()
|
||||
{
|
||||
return GetRegistrationBlob(D2D1TransformMapper.FactoryOf<T, TMapper>.Instance, out effectId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a binary blob containing serialized information that can be used to register an effect, by using <c>ID2D1Factory1::RegisterEffectFromString</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to get the registration blob for.</typeparam>
|
||||
/// <param name="mapperFactory">An optional factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for the transform.</param>
|
||||
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
|
||||
/// <returns>A blob containing serialized information that can be used to register an effect.</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown if an effect is registered multiple times with different properties.</exception>
|
||||
@@ -344,19 +268,17 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
/// For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(ID2D1TransformMapperFactory<T>? mapperFactory, out Guid effectId)
|
||||
public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
PixelShaderEffect.For<T>.Initialize(mapperFactory);
|
||||
|
||||
ArrayPoolBufferWriter<byte> writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize);
|
||||
|
||||
// Blob id
|
||||
writer.WriteRaw(D2D1EffectRegistrationData.V1.BlobId);
|
||||
|
||||
// Effect id and number of inputs
|
||||
writer.WriteRaw(PixelShaderEffect.For<T>.Id);
|
||||
writer.WriteRaw(PixelShaderEffect.For<T>.InputCount);
|
||||
writer.WriteRaw(PixelShaderEffect.For<T>.Instance.Id);
|
||||
writer.WriteRaw(PixelShaderEffect.For<T>.Instance.InputCount);
|
||||
|
||||
// Build the XML text
|
||||
writer.WriteRaw("""
|
||||
@@ -375,7 +297,7 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
"""u8);
|
||||
|
||||
// Add the input nodes
|
||||
for (int i = 0; i < PixelShaderEffect.For<T>.InputCount; i++)
|
||||
for (int i = 0; i < PixelShaderEffect.For<T>.Instance.InputCount; i++)
|
||||
{
|
||||
writer.WriteRaw(" <Input name='Source"u8);
|
||||
writer.WriteAsUtf8(i);
|
||||
@@ -524,14 +446,14 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
writer.WriteRaw((nint)PixelShaderEffect.SetTransformMapper);
|
||||
|
||||
// Effect factory
|
||||
writer.WriteRaw((nint)PixelShaderEffect.For<T>.Factory);
|
||||
writer.WriteRaw((nint)PixelShaderEffect.For<T>.Instance.Factory);
|
||||
|
||||
byte[] registrationBlob = writer.WrittenSpan.ToArray();
|
||||
|
||||
writer.Dispose();
|
||||
|
||||
// Extract the effect id (the same that was encoded in the registration blob)
|
||||
effectId = PixelShaderEffect.For<T>.Id;
|
||||
effectId = PixelShaderEffect.For<T>.Instance.Id;
|
||||
|
||||
return registrationBlob;
|
||||
}
|
||||
@@ -562,14 +484,12 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The pointer to the target ID2D1Effect result cannot be null.");
|
||||
}
|
||||
|
||||
if (!PixelShaderEffect.For<T>.TryGetId(out Guid id))
|
||||
fixed (Guid* pGuid = &PixelShaderEffect.For<T>.Instance.Id)
|
||||
{
|
||||
ThrowHelper.ThrowInvalidOperationException("The effect for the input shader type has not been initialized yet.");
|
||||
((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect(
|
||||
effectId: pGuid,
|
||||
effect: (ID2D1Effect**)d2D1Effect).Assert();
|
||||
}
|
||||
|
||||
((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect(
|
||||
effectId: &id,
|
||||
effect: (ID2D1Effect**)d2D1Effect).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
#if NET6_0_OR_GREATER
|
||||
@@ -10,8 +9,6 @@ using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers;
|
||||
using RuntimeHelpers = ComputeSharp.D2D1.NetStandard.System.Runtime.CompilerServices.RuntimeHelpers;
|
||||
#endif
|
||||
|
||||
#pragma warning disable CA2002
|
||||
|
||||
namespace ComputeSharp.D2D1.Interop.Effects;
|
||||
|
||||
/// <summary>
|
||||
@@ -31,18 +28,13 @@ internal unsafe partial struct PixelShaderEffect
|
||||
/// A generic pixel shader implementation.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader.</typeparam>
|
||||
public static class For<T>
|
||||
public sealed class For<T>
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="FactoryDelegate"/> wrapper for the shader factory.
|
||||
/// </summary>
|
||||
private static readonly FactoryDelegate EffectFactory = CreateEffect;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether or not initialization has completed.
|
||||
/// </summary>
|
||||
private static bool isInitialized;
|
||||
private readonly FactoryDelegate effectFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Guid"/> for the shader.
|
||||
@@ -52,229 +44,168 @@ internal unsafe partial struct PixelShaderEffect
|
||||
/// being used to pass its address to a native API, but the attribute is incompatible with collectible assemblies.
|
||||
/// Because of that, it can't be used, as this project is explicitly meant to support plugin-like scenarios.
|
||||
/// </remarks>
|
||||
private static Guid shaderId;
|
||||
private readonly Guid shaderId;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the constant buffer for the shader.
|
||||
/// </summary>
|
||||
private static int constantBufferSize;
|
||||
private readonly int constantBufferSize;
|
||||
|
||||
/// <summary>
|
||||
/// The number of inputs for the shader.
|
||||
/// </summary>
|
||||
private static int inputCount;
|
||||
private readonly int inputCount;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer with the types of inputs for the shader.
|
||||
/// </summary>
|
||||
private static D2D1PixelShaderInputType* inputTypes;
|
||||
private readonly D2D1PixelShaderInputType* inputTypes;
|
||||
|
||||
/// <summary>
|
||||
/// The number of available input descriptions.
|
||||
/// </summary>
|
||||
private static int inputDescriptionCount;
|
||||
private readonly int inputDescriptionCount;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer with the available input descriptions for the shader.
|
||||
/// </summary>
|
||||
private static D2D1InputDescription* inputDescriptions;
|
||||
private readonly D2D1InputDescription* inputDescriptions;
|
||||
|
||||
/// <summary>
|
||||
/// The pixel options for the shader.
|
||||
/// </summary>
|
||||
private static D2D1PixelOptions pixelOptions;
|
||||
private readonly D2D1PixelOptions pixelOptions;
|
||||
|
||||
/// <summary>
|
||||
/// The shader bytecode.
|
||||
/// </summary>
|
||||
private static byte* bytecode;
|
||||
private readonly byte* bytecode;
|
||||
|
||||
/// <summary>
|
||||
/// The size of <see cref="bytecode"/>.
|
||||
/// </summary>
|
||||
private static int bytecodeSize;
|
||||
private readonly int bytecodeSize;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer precision for the resulting output buffer.
|
||||
/// </summary>
|
||||
private static D2D1BufferPrecision bufferPrecision;
|
||||
private readonly D2D1BufferPrecision bufferPrecision;
|
||||
|
||||
/// <summary>
|
||||
/// The channel depth for the resulting output buffer.
|
||||
/// </summary>
|
||||
private static D2D1ChannelDepth channelDepth;
|
||||
private readonly D2D1ChannelDepth channelDepth;
|
||||
|
||||
/// <summary>
|
||||
/// The number of available resource texture descriptions.
|
||||
/// </summary>
|
||||
private static int resourceTextureDescriptionCount;
|
||||
private readonly int resourceTextureDescriptionCount;
|
||||
|
||||
/// <summary>
|
||||
/// The buffer with the available resource texture descriptions for the shader.
|
||||
/// </summary>
|
||||
private static D2D1ResourceTextureDescription* resourceTextureDescriptions;
|
||||
private readonly D2D1ResourceTextureDescription* resourceTextureDescriptions;
|
||||
|
||||
/// <summary>
|
||||
/// The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.
|
||||
/// Creates a new <see cref="For{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
private static ID2D1TransformMapperFactory<T>? d2D1DrawTransformMapperFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="For{T}"/> shared state.
|
||||
/// </summary>
|
||||
/// <param name="d2D1DrawTransformMapperFactory">The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if initialization is attempted with a mismatched transform factory.</exception>
|
||||
public static void Initialize(ID2D1TransformMapperFactory<T>? d2D1DrawTransformMapperFactory)
|
||||
/// <param name="effectFactory">The <see cref="FactoryDelegate"/> wrapper for the shader factory.</param>
|
||||
/// <param name="shaderId">The <see cref="Guid"/> for the shader.</param>
|
||||
/// <param name="constantBufferSize">The size of the constant buffer for the shader.</param>
|
||||
/// <param name="inputCount">The number of inputs for the shader.</param>
|
||||
/// <param name="inputTypes">The buffer with the types of inputs for the shader.</param>
|
||||
/// <param name="inputDescriptionCount">The number of available input descriptions.</param>
|
||||
/// <param name="inputDescriptions">The buffer with the available input descriptions for the shader.</param>
|
||||
/// <param name="pixelOptions">The pixel options for the shader.</param>
|
||||
/// <param name="bytecode">The shader bytecode.</param>
|
||||
/// <param name="bytecodeSize">The size of <paramref name="bytecode"/>.</param>
|
||||
/// <param name="bufferPrecision">The buffer precision for the resulting output buffer.</param>
|
||||
/// <param name="channelDepth">The channel depth for the resulting output buffer.</param>
|
||||
/// <param name="resourceTextureDescriptionCount">The number of available resource texture descriptions.</param>
|
||||
/// <param name="resourceTextureDescriptions">The buffer with the available resource texture descriptions for the shader.</param>
|
||||
private For(
|
||||
FactoryDelegate effectFactory,
|
||||
Guid shaderId,
|
||||
int constantBufferSize,
|
||||
int inputCount,
|
||||
D2D1PixelShaderInputType* inputTypes,
|
||||
int inputDescriptionCount,
|
||||
D2D1InputDescription* inputDescriptions,
|
||||
D2D1PixelOptions pixelOptions,
|
||||
byte* bytecode,
|
||||
int bytecodeSize,
|
||||
D2D1BufferPrecision bufferPrecision,
|
||||
D2D1ChannelDepth channelDepth,
|
||||
int resourceTextureDescriptionCount,
|
||||
D2D1ResourceTextureDescription* resourceTextureDescriptions)
|
||||
{
|
||||
// This conceptually acts as a static constructor, and this type is
|
||||
// internal, so in this very specific case locking on the type is fine.
|
||||
lock (typeof(For<T>))
|
||||
{
|
||||
if (isInitialized)
|
||||
{
|
||||
// If the factory is already initialized, ensure the draw transform mapper is the same
|
||||
if (For<T>.d2D1DrawTransformMapperFactory != d2D1DrawTransformMapperFactory)
|
||||
{
|
||||
ThrowHelper.ThrowInvalidOperationException(
|
||||
"Cannot initialize an ID2D1Effect factory for the same shader type with two different transform mapper factories. " +
|
||||
"Make sure to only ever register a pixel shader effect with either no transform, or the same transform mapper factory instance.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Load all shader properties
|
||||
Guid shaderId = typeof(T).GUID;
|
||||
int constantBufferSize = D2D1PixelShader.GetConstantBufferSize<T>();
|
||||
D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision<T>();
|
||||
D2D1ChannelDepth channelDepth = D2D1PixelShader.GetOutputBufferChannelDepth<T>();
|
||||
D2D1PixelOptions pixelOptions = D2D1PixelShader.GetPixelOptions<T>();
|
||||
|
||||
// Prepare the inputs info
|
||||
int inputCount = D2D1PixelShader.GetInputCount<T>();
|
||||
D2D1PixelShaderInputType* inputTypes = (D2D1PixelShaderInputType*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), sizeof(D2D1PixelShaderInputType) * inputCount);
|
||||
|
||||
for (int i = 0; i < inputCount; i++)
|
||||
{
|
||||
inputTypes[i] = D2D1PixelShader.GetInputType<T>(i);
|
||||
}
|
||||
|
||||
// Prepare the input descriptions
|
||||
ReadOnlyMemory<D2D1InputDescription> inputDescriptionsInfo = D2D1PixelShader.GetInputDescriptions<T>();
|
||||
int inputDescriptionCount = inputDescriptionsInfo.Length;
|
||||
D2D1InputDescription* inputDescriptions = (D2D1InputDescription*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), sizeof(D2D1InputDescription) * inputDescriptionCount);
|
||||
|
||||
inputDescriptionsInfo.Span.CopyTo(new Span<D2D1InputDescription>(inputDescriptions, inputDescriptionCount));
|
||||
|
||||
// Prepare the resource texture descriptions
|
||||
ReadOnlyMemory<D2D1ResourceTextureDescription> resourceTextureDescriptionsInfo = D2D1PixelShader.GetResourceTextureDescriptions<T>();
|
||||
int resourceTextureDescriptionCount = resourceTextureDescriptionsInfo.Length;
|
||||
D2D1ResourceTextureDescription* resourceTextureDescriptions = (D2D1ResourceTextureDescription*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), sizeof(D2D1ResourceTextureDescription) * resourceTextureDescriptionCount);
|
||||
|
||||
resourceTextureDescriptionsInfo.Span.CopyTo(new Span<D2D1ResourceTextureDescription>(resourceTextureDescriptions, resourceTextureDescriptionCount));
|
||||
|
||||
// Copy the bytecode to the target buffer
|
||||
ReadOnlyMemory<byte> bytecodeInfo = D2D1PixelShader.LoadBytecode<T>();
|
||||
int bytecodeSize = bytecodeInfo.Length;
|
||||
byte* bytecode = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), bytecodeSize);
|
||||
|
||||
bytecodeInfo.Span.CopyTo(new Span<byte>(bytecode, bytecodeSize));
|
||||
|
||||
// Set the shared state and mark the type as initialized
|
||||
For<T>.shaderId = shaderId;
|
||||
For<T>.constantBufferSize = constantBufferSize;
|
||||
For<T>.inputCount = inputCount;
|
||||
For<T>.inputTypes = inputTypes;
|
||||
For<T>.inputDescriptionCount = inputDescriptionCount;
|
||||
For<T>.inputDescriptions = inputDescriptions;
|
||||
For<T>.pixelOptions = pixelOptions;
|
||||
For<T>.bytecode = bytecode;
|
||||
For<T>.bytecodeSize = bytecodeSize;
|
||||
For<T>.bufferPrecision = bufferPrecision;
|
||||
For<T>.channelDepth = channelDepth;
|
||||
For<T>.resourceTextureDescriptionCount = resourceTextureDescriptionCount;
|
||||
For<T>.resourceTextureDescriptions = resourceTextureDescriptions;
|
||||
For<T>.d2D1DrawTransformMapperFactory = d2D1DrawTransformMapperFactory;
|
||||
|
||||
isInitialized = true;
|
||||
}
|
||||
}
|
||||
this.effectFactory = effectFactory;
|
||||
this.shaderId = shaderId;
|
||||
this.constantBufferSize = constantBufferSize;
|
||||
this.inputCount = inputCount;
|
||||
this.inputTypes = inputTypes;
|
||||
this.inputDescriptionCount = inputDescriptionCount;
|
||||
this.inputDescriptions = inputDescriptions;
|
||||
this.pixelOptions = pixelOptions;
|
||||
this.bytecode = bytecode;
|
||||
this.bytecodeSize = bytecodeSize;
|
||||
this.bufferPrecision = bufferPrecision;
|
||||
this.channelDepth = channelDepth;
|
||||
this.resourceTextureDescriptionCount = resourceTextureDescriptionCount;
|
||||
this.resourceTextureDescriptions = resourceTextureDescriptions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the id of the effect.
|
||||
/// Gets the shader <see cref="For{T}"/> instance.
|
||||
/// </summary>
|
||||
public static ref Guid Id
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref shaderId;
|
||||
}
|
||||
public static For<T> Instance { get; } = CreateInstance();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factory for the current effect.
|
||||
/// Creates a new <see cref="For{T}"/> instance.
|
||||
/// </summary>
|
||||
#if NET6_0_OR_GREATER
|
||||
public static delegate* unmanaged[Stdcall]<IUnknown**, HRESULT> Factory
|
||||
#else
|
||||
public static void* Factory
|
||||
#endif
|
||||
/// <returns>The initialized <see cref="For{T}"/> instance.</returns>
|
||||
private static For<T> CreateInstance()
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#if NET6_0_OR_GREATER
|
||||
get => (delegate* unmanaged[Stdcall]<IUnknown**, HRESULT>)Marshal.GetFunctionPointerForDelegate(EffectFactory);
|
||||
#else
|
||||
get => (void*)Marshal.GetFunctionPointerForDelegate(EffectFactory);
|
||||
#endif
|
||||
}
|
||||
// Load all shader properties
|
||||
Guid shaderId = typeof(T).GUID;
|
||||
int constantBufferSize = D2D1PixelShader.GetConstantBufferSize<T>();
|
||||
D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision<T>();
|
||||
D2D1ChannelDepth channelDepth = D2D1PixelShader.GetOutputBufferChannelDepth<T>();
|
||||
D2D1PixelOptions pixelOptions = D2D1PixelShader.GetPixelOptions<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of inputs for the effect.
|
||||
/// </summary>
|
||||
public static int InputCount => inputCount;
|
||||
// Prepare the inputs info
|
||||
int inputCount = D2D1PixelShader.GetInputCount<T>();
|
||||
D2D1PixelShaderInputType* inputTypes = (D2D1PixelShaderInputType*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), sizeof(D2D1PixelShaderInputType) * inputCount);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the effect id, if it has been initialized.
|
||||
/// </summary>
|
||||
/// <param name="id">The resulting effect id.</param>
|
||||
/// <returns>Whether or not the effect had been initialized.</returns>
|
||||
public static bool TryGetId(out Guid id)
|
||||
{
|
||||
lock (typeof(For<T>))
|
||||
for (int i = 0; i < inputCount; i++)
|
||||
{
|
||||
if (isInitialized)
|
||||
{
|
||||
id = Id;
|
||||
|
||||
return true;
|
||||
}
|
||||
inputTypes[i] = D2D1PixelShader.GetInputType<T>(i);
|
||||
}
|
||||
|
||||
id = default;
|
||||
// Prepare the input descriptions
|
||||
ReadOnlyMemory<D2D1InputDescription> inputDescriptionsInfo = D2D1PixelShader.GetInputDescriptions<T>();
|
||||
int inputDescriptionCount = inputDescriptionsInfo.Length;
|
||||
D2D1InputDescription* inputDescriptions = (D2D1InputDescription*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), sizeof(D2D1InputDescription) * inputDescriptionCount);
|
||||
|
||||
return false;
|
||||
}
|
||||
inputDescriptionsInfo.Span.CopyTo(new Span<D2D1InputDescription>(inputDescriptions, inputDescriptionCount));
|
||||
|
||||
/// <inheritdoc cref="FactoryDelegate"/>
|
||||
private static int CreateEffect(IUnknown** effectImpl)
|
||||
{
|
||||
D2D1TransformMapper? d2D1TransformMapper;
|
||||
// Prepare the resource texture descriptions
|
||||
ReadOnlyMemory<D2D1ResourceTextureDescription> resourceTextureDescriptionsInfo = D2D1PixelShader.GetResourceTextureDescriptions<T>();
|
||||
int resourceTextureDescriptionCount = resourceTextureDescriptionsInfo.Length;
|
||||
D2D1ResourceTextureDescription* resourceTextureDescriptions = (D2D1ResourceTextureDescription*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), sizeof(D2D1ResourceTextureDescription) * resourceTextureDescriptionCount);
|
||||
|
||||
// If there is a custom draw transform factory, run it in a try block and handle exceptions. This
|
||||
// is needed because the factory will be running user code from a method that's invoked by COM,
|
||||
// and managed exceptions should never cross the ABI boundary. If it throws, just return the HRESULT.
|
||||
try
|
||||
{
|
||||
ID2D1TransformMapper<T>? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Create();
|
||||
resourceTextureDescriptionsInfo.Span.CopyTo(new Span<D2D1ResourceTextureDescription>(resourceTextureDescriptions, resourceTextureDescriptionCount));
|
||||
|
||||
d2D1TransformMapper = D2D1TransformMapper.For<T>.Get(d2D1DrawTransformMapper);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
*effectImpl = null;
|
||||
// Copy the bytecode to the target buffer
|
||||
ReadOnlyMemory<byte> bytecodeInfo = D2D1PixelShader.LoadBytecode<T>();
|
||||
int bytecodeSize = bytecodeInfo.Length;
|
||||
byte* bytecode = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), bytecodeSize);
|
||||
|
||||
return e.HResult;
|
||||
}
|
||||
bytecodeInfo.Span.CopyTo(new Span<byte>(bytecode, bytecodeSize));
|
||||
|
||||
return PixelShaderEffect.Factory(
|
||||
// Initialize the shared instance with the computed state
|
||||
return new(
|
||||
CreateEffect,
|
||||
shaderId,
|
||||
constantBufferSize,
|
||||
inputCount,
|
||||
@@ -287,8 +218,59 @@ internal unsafe partial struct PixelShaderEffect
|
||||
bufferPrecision,
|
||||
channelDepth,
|
||||
resourceTextureDescriptionCount,
|
||||
resourceTextureDescriptions,
|
||||
d2D1TransformMapper,
|
||||
resourceTextureDescriptions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the id of the effect.
|
||||
/// </summary>
|
||||
public ref readonly Guid Id
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref this.shaderId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factory for the current effect.
|
||||
/// </summary>
|
||||
#if NET6_0_OR_GREATER
|
||||
public delegate* unmanaged[Stdcall]<IUnknown**, HRESULT> Factory
|
||||
#else
|
||||
public void* Factory
|
||||
#endif
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#if NET6_0_OR_GREATER
|
||||
get => (delegate* unmanaged[Stdcall]<IUnknown**, HRESULT>)Marshal.GetFunctionPointerForDelegate(this.effectFactory);
|
||||
#else
|
||||
get => (void*)Marshal.GetFunctionPointerForDelegate(this.effectFactory);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of inputs for the effect.
|
||||
/// </summary>
|
||||
public int InputCount => this.inputCount;
|
||||
|
||||
/// <inheritdoc cref="FactoryDelegate"/>
|
||||
private static int CreateEffect(IUnknown** effectImpl)
|
||||
{
|
||||
For<T> instance = Instance;
|
||||
|
||||
return PixelShaderEffect.Factory(
|
||||
instance.shaderId,
|
||||
instance.constantBufferSize,
|
||||
instance.inputCount,
|
||||
instance.inputTypes,
|
||||
instance.inputDescriptionCount,
|
||||
instance.inputDescriptions,
|
||||
instance.pixelOptions,
|
||||
instance.bytecode,
|
||||
instance.bytecodeSize,
|
||||
instance.bufferPrecision,
|
||||
instance.channelDepth,
|
||||
instance.resourceTextureDescriptionCount,
|
||||
instance.resourceTextureDescriptions,
|
||||
effectImpl);
|
||||
}
|
||||
}
|
||||
|
||||
-72
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
|
||||
@@ -138,31 +137,6 @@ partial struct PixelShaderEffect
|
||||
return hresult;
|
||||
}
|
||||
}
|
||||
else if (@this->d2D1TransformMapperHandle.Target is D2D1TransformMapper d2D1TransformMapper)
|
||||
{
|
||||
Rectangle output = outputRect->ToRectangle();
|
||||
Span<Rectangle> inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectsCount);
|
||||
|
||||
for (int i = 0; i < (int)inputRectsCount; i++)
|
||||
{
|
||||
inputs[i] = inputRects[i].ToRectangle();
|
||||
}
|
||||
|
||||
// Invoke MapOutputToInputs and handle exceptions so they don't cross the ABI boundary
|
||||
try
|
||||
{
|
||||
d2D1TransformMapper.MapOutputToInputs(in output, inputs);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)inputRectsCount; i++)
|
||||
{
|
||||
inputRects[i] = inputs[i].ToRECT();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If no custom transform is used, apply the default mapping. In this case, the loop will
|
||||
@@ -233,35 +207,6 @@ partial struct PixelShaderEffect
|
||||
return hresult;
|
||||
}
|
||||
}
|
||||
else if (@this->d2D1TransformMapperHandle.Target is D2D1TransformMapper d2D1TransformMapper)
|
||||
{
|
||||
Span<Rectangle> inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount);
|
||||
Span<Rectangle> opaqueInputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount);
|
||||
|
||||
for (int i = 0; i < (int)inputRectCount; i++)
|
||||
{
|
||||
inputs[i] = inputRects[i].ToRectangle();
|
||||
opaqueInputs[i] = inputOpaqueSubRects[i].ToRectangle();
|
||||
}
|
||||
|
||||
ReadOnlySpan<byte> buffer = new(@this->constantBuffer, @this->constantBufferSize);
|
||||
|
||||
Rectangle output;
|
||||
Rectangle opaqueOutput;
|
||||
|
||||
// Handle exceptions, as mentioned above
|
||||
try
|
||||
{
|
||||
d2D1TransformMapper.MapInputsToOutput(buffer, inputs, opaqueInputs, out output, out opaqueOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
}
|
||||
|
||||
*outputRect = output.ToRECT();
|
||||
*outputOpaqueSubRect = opaqueOutput.ToRECT();
|
||||
}
|
||||
else if (inputRectCount == 0)
|
||||
{
|
||||
// If there are no inputs, make the output rectangle infinite. This is useful
|
||||
@@ -332,23 +277,6 @@ partial struct PixelShaderEffect
|
||||
return hresult;
|
||||
}
|
||||
}
|
||||
else if (@this->d2D1TransformMapperHandle.Target is D2D1TransformMapper d2D1TransformMapper)
|
||||
{
|
||||
Rectangle invalidInput = invalidInputRect.ToRectangle();
|
||||
Rectangle invalidOutput;
|
||||
|
||||
// Handle exceptions once again
|
||||
try
|
||||
{
|
||||
d2D1TransformMapper.MapInvalidOutput((int)inputIndex, invalidInput, out invalidOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
}
|
||||
|
||||
*invalidOutputRect = invalidOutput.ToRECT();
|
||||
}
|
||||
else
|
||||
{
|
||||
// The default mapping in this scenario just needs to set the invalid output rect for simple inputs to
|
||||
|
||||
@@ -188,15 +188,9 @@ internal unsafe partial struct PixelShaderEffect
|
||||
/// </remarks>
|
||||
private D2D1ResourceTextureDescription* resourceTextureDescriptions;
|
||||
|
||||
/// <summary>
|
||||
/// The handle for the <see cref="D2D1TransformMapper"/> instance in use, if any.
|
||||
/// </summary>
|
||||
private GCHandle d2D1TransformMapperHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID2D1TransformMapper"/> instance to use, if any.
|
||||
/// </summary>
|
||||
/// <remarks>If present, this takes precedence over <see cref="d2D1TransformMapperHandle"/>.</remarks>
|
||||
private ID2D1TransformMapper* d2D1TransformMapper;
|
||||
|
||||
/// <summary>
|
||||
@@ -230,7 +224,6 @@ internal unsafe partial struct PixelShaderEffect
|
||||
/// <param name="channelDepth">The channel depth for the resulting output buffer.</param>
|
||||
/// <param name="resourceTextureDescriptionCount">The number of available resource texture descriptions.</param>
|
||||
/// <param name="resourceTextureDescriptions">The buffer with the available resource texture descriptions for the shader.</param>
|
||||
/// <param name="d2D1TransformMapper">The <see cref="D2D1TransformMapper"/> instance to use for the effect.</param>
|
||||
/// <param name="effectImpl">The resulting effect instance.</param>
|
||||
/// <returns>This always returns <c>0</c>.</returns>
|
||||
private static int Factory(
|
||||
@@ -247,7 +240,6 @@ internal unsafe partial struct PixelShaderEffect
|
||||
D2D1ChannelDepth channelDepth,
|
||||
int resourceTextureDescriptionCount,
|
||||
D2D1ResourceTextureDescription* resourceTextureDescriptions,
|
||||
D2D1TransformMapper? d2D1TransformMapper,
|
||||
IUnknown** effectImpl)
|
||||
{
|
||||
PixelShaderEffect* @this;
|
||||
@@ -282,7 +274,6 @@ internal unsafe partial struct PixelShaderEffect
|
||||
@this->channelDepth = channelDepth;
|
||||
@this->resourceTextureDescriptionCount = resourceTextureDescriptionCount;
|
||||
@this->resourceTextureDescriptions = resourceTextureDescriptions;
|
||||
@this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper);
|
||||
@this->d2D1TransformMapper = null;
|
||||
@this->d2D1DrawInfo = null;
|
||||
@this->d2D1EffectContext = null;
|
||||
@@ -347,8 +338,6 @@ internal unsafe partial struct PixelShaderEffect
|
||||
NativeMemory.Free(this.constantBuffer);
|
||||
}
|
||||
|
||||
this.d2D1TransformMapperHandle.Free();
|
||||
|
||||
if (this.d2D1TransformMapper is not null)
|
||||
{
|
||||
_ = ((IUnknown*)this.d2D1TransformMapper)->Release();
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする