Add ID2D1Shader.InitializeFromDispatchData API to support transforms
このコミットが含まれているのは:
1 親
a36bae8e5a
コミット
d0dfd82f06
10ファイル変更
+339
-55
検索条件に一致するファイルはありません
@@ -34,13 +34,13 @@ unsafe partial class D2D1InteropServices
|
||||
/// 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="ID2D1DrawTransformMapper"/> implementation 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>
|
||||
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
|
||||
public static unsafe void RegisterPixelShaderEffectForD2D1Factory1<T, TMapper>(void* d2D1Factory1, out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
where TMapper : class, ID2D1DrawTransformMapper, new()
|
||||
where TMapper : class, ID2D1TransformMapper<T>, new()
|
||||
{
|
||||
RegisterPixelShaderEffectForD2D1Factory1<T>(d2D1Factory1, static () => new TMapper(), out effectId);
|
||||
}
|
||||
@@ -50,10 +50,10 @@ unsafe partial class D2D1InteropServices
|
||||
/// </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="ID2D1DrawTransformMapper"/> instances to use for the transform.</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>
|
||||
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.</remarks>
|
||||
public static unsafe void RegisterPixelShaderEffectForD2D1Factory1<T>(void* d2D1Factory1, Func<ID2D1DrawTransformMapper>? mapperFactory, out Guid effectId)
|
||||
public static unsafe void RegisterPixelShaderEffectForD2D1Factory1<T>(void* d2D1Factory1, Func<ID2D1TransformMapper<T>>? mapperFactory, out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
effectId = default;
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Interop.Effects;
|
||||
|
||||
/// <summary>
|
||||
/// A base type to support non-generic <see cref="ID2D1TransformMapper{T}"/> uses.
|
||||
/// </summary>
|
||||
internal abstract class D2D1TransformMapper
|
||||
{
|
||||
/// <inheritdoc cref="ID2D1TransformMapper{T}.MapInputsToOutput"/>
|
||||
/// <param name="buffer">The buffer with the serialized shader dispatch data, if any.</param>
|
||||
/// <param name="inputs">The input rectangles to be mapped to the output rectangle. This parameter is always equal to the input bounds.</param>
|
||||
/// <param name="opaqueInputs">The input rectangles to be mapped to the opaque output rectangle.</param>
|
||||
/// <param name="output">The output rectangle that maps to the corresponding input rectangle.</param>
|
||||
/// <param name="opaqueOutput">The output rectangle that maps to the corresponding opaque input rectangle.</param>
|
||||
public abstract void MapInputsToOutput(
|
||||
ReadOnlySpan<byte> buffer,
|
||||
ReadOnlySpan<Rectangle> inputs,
|
||||
ReadOnlySpan<Rectangle> opaqueInputs,
|
||||
out Rectangle output,
|
||||
out Rectangle opaqueOutput);
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper{T}.MapOutputToInputs"/>
|
||||
/// <param name="buffer">The buffer with the serialized shader dispatch data, if any.</param>
|
||||
/// <param name="output">The output rectangle from which the inputs must be mapped.</param>
|
||||
/// <param name="inputs">The corresponding set of inputs. The inputs will directly correspond to the transform inputs.</param>
|
||||
public abstract void MapOutputToInputs(ReadOnlySpan<byte> buffer, in Rectangle output, Span<Rectangle> inputs);
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper{T}.MapInvalidOutput"/>
|
||||
/// <param name="buffer">The buffer with the serialized shader dispatch data, if any.</param>
|
||||
/// <param name="inputIndex">The index of the input rectangle.</param>
|
||||
/// <param name="invalidInput">The invalid input rectangle.</param>
|
||||
/// <param name="invalidOutput">The output rectangle to which the input rectangle must be mapped.</param>
|
||||
public abstract void MapInvalidOutput(ReadOnlySpan<byte> buffer, int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput);
|
||||
|
||||
/// <summary>
|
||||
/// A generic <see cref="D2D1TransformMapper"/> implementation for a specific shader type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The target shader type.</typeparam>
|
||||
public sealed class For<T> : D2D1TransformMapper
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ID2D1TransformMapper{T}"/> instance to use.
|
||||
/// </summary>
|
||||
private readonly ID2D1TransformMapper<T> transformMapper;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="For{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="transformMapper">The <see cref="ID2D1TransformMapper{T}"/> instance to use.</param>
|
||||
private For(ID2D1TransformMapper<T> transformMapper)
|
||||
{
|
||||
this.transformMapper = transformMapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="For{T}"/> instance for a given transform mapper, if there is one.
|
||||
/// </summary>
|
||||
/// <param name="transformMapper">The <see cref="ID2D1TransformMapper{T}"/> instance to usse, if there is one.</param>
|
||||
/// <returns>A new <see cref="For{T}"/> instance wrapping <paramref name="transformMapper"/>, or <see langword="null"/>.</returns>
|
||||
public static For<T>? Get(ID2D1TransformMapper<T>? transformMapper)
|
||||
{
|
||||
if (transformMapper is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new(transformMapper);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void MapInputsToOutput(ReadOnlySpan<byte> buffer, ReadOnlySpan<Rectangle> inputs, ReadOnlySpan<Rectangle> opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.InitializeFromDispatchData(buffer);
|
||||
|
||||
this.transformMapper.MapInputsToOutput(in shader, inputs, opaqueInputs, out output, out opaqueOutput);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void MapOutputToInputs(ReadOnlySpan<byte> buffer, in Rectangle output, Span<Rectangle> inputs)
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.InitializeFromDispatchData(buffer);
|
||||
|
||||
this.transformMapper.MapOutputToInputs(in shader, in output, inputs);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void MapInvalidOutput(ReadOnlySpan<byte> buffer, int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput)
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.InitializeFromDispatchData(buffer);
|
||||
|
||||
this.transformMapper.MapInvalidOutput(in shader, inputIndex, invalidInput, out invalidOutput);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-6
@@ -127,7 +127,7 @@ partial struct PixelShaderEffect
|
||||
return E.E_INVALIDARG;
|
||||
}
|
||||
|
||||
if (@this->d2D1DrawTransformMapperFactoryHandle.Target is ID2D1DrawTransformMapper d2D1DrawTransformMapper)
|
||||
if (@this->d2D1TransformMapperHandle.Target is D2D1TransformMapper d2D1TransformMapper)
|
||||
{
|
||||
Rectangle output = outputRect->ToRectangle();
|
||||
Span<Rectangle> inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectsCount);
|
||||
@@ -137,10 +137,12 @@ partial struct PixelShaderEffect
|
||||
inputs[i] = inputRects[i].ToRectangle();
|
||||
}
|
||||
|
||||
ReadOnlySpan<byte> buffer = new(@this->constantBuffer, @this->constantBufferSize);
|
||||
|
||||
// Invoke MapOutputToInputs and handle exceptions so they don't cross the ABI boundary
|
||||
try
|
||||
{
|
||||
d2D1DrawTransformMapper.MapOutputToInputs(in output, inputs);
|
||||
d2D1TransformMapper.MapOutputToInputs(buffer, in output, inputs);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -175,7 +177,7 @@ partial struct PixelShaderEffect
|
||||
return E.E_INVALIDARG;
|
||||
}
|
||||
|
||||
if (@this->d2D1DrawTransformMapperFactoryHandle.Target is ID2D1DrawTransformMapper d2D1DrawTransformMapper)
|
||||
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);
|
||||
@@ -186,13 +188,15 @@ partial struct PixelShaderEffect
|
||||
opaqueInputs[i] = inputOpaqueSubRects[i].ToRectangle();
|
||||
}
|
||||
|
||||
ReadOnlySpan<byte> buffer = new(@this->constantBuffer, @this->constantBufferSize);
|
||||
|
||||
Rectangle output;
|
||||
Rectangle opaqueOutput;
|
||||
|
||||
// Handle exceptions, as mentioned above
|
||||
try
|
||||
{
|
||||
d2D1DrawTransformMapper.MapInputsToOutput(inputs, opaqueInputs, out output, out opaqueOutput);
|
||||
d2D1TransformMapper.MapInputsToOutput(buffer, inputs, opaqueInputs, out output, out opaqueOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -230,15 +234,17 @@ partial struct PixelShaderEffect
|
||||
{
|
||||
@this = (PixelShaderEffect*)&((void**)@this)[-1];
|
||||
|
||||
if (@this->d2D1DrawTransformMapperFactoryHandle.Target is ID2D1DrawTransformMapper d2D1DrawTransformMapper)
|
||||
if (@this->d2D1TransformMapperHandle.Target is D2D1TransformMapper d2D1TransformMapper)
|
||||
{
|
||||
Rectangle invalidInput = invalidInputRect.ToRectangle();
|
||||
Rectangle invalidOutput;
|
||||
|
||||
ReadOnlySpan<byte> buffer = new(@this->constantBuffer, @this->constantBufferSize);
|
||||
|
||||
// Handle exceptions once again
|
||||
try
|
||||
{
|
||||
d2D1DrawTransformMapper.MapInvalidOutput((int)inputIndex, invalidInput, out invalidOutput);
|
||||
d2D1TransformMapper.MapInvalidOutput(buffer, (int)inputIndex, invalidInput, out invalidOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -97,16 +97,16 @@ internal unsafe partial struct PixelShaderEffect
|
||||
private static int bytecodeSize;
|
||||
|
||||
/// <summary>
|
||||
/// The factory of <see cref="ID2D1DrawTransformMapper"/> instances to use for each created effect.
|
||||
/// The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.
|
||||
/// </summary>
|
||||
private static Func<ID2D1DrawTransformMapper>? d2D1DrawTransformMapperFactory;
|
||||
private static Func<ID2D1TransformMapper<T>>? d2D1DrawTransformMapperFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="For{T}"/> shared state.
|
||||
/// </summary>
|
||||
/// <param name="factory">The factory of <see cref="ID2D1DrawTransformMapper"/> instances to use for each created effect.</param>
|
||||
/// <param name="factory">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(Func<ID2D1DrawTransformMapper>? factory)
|
||||
public static void Initialize(Func<ID2D1TransformMapper<T>>? factory)
|
||||
{
|
||||
// This conceptually acts as a static constructor, and this type is
|
||||
// internal, so in this very specific case locking on the type is fine.
|
||||
@@ -161,9 +161,15 @@ internal unsafe partial struct PixelShaderEffect
|
||||
/// <inheritdoc cref="FactoryDelegate"/>
|
||||
private static int CreateEffect(IUnknown** effectImpl)
|
||||
{
|
||||
ID2D1DrawTransformMapper? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Invoke();
|
||||
ID2D1TransformMapper<T>? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Invoke();
|
||||
|
||||
return PixelShaderEffect.Factory(shaderId, numberOfInputs, bytecode, bytecodeSize, d2D1DrawTransformMapper, effectImpl);
|
||||
return PixelShaderEffect.Factory(
|
||||
shaderId,
|
||||
numberOfInputs,
|
||||
bytecode,
|
||||
bytecodeSize,
|
||||
D2D1TransformMapper.For<T>.Get(d2D1DrawTransformMapper),
|
||||
effectImpl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,9 +278,9 @@ internal unsafe partial struct PixelShaderEffect
|
||||
private int bytecodeSize;
|
||||
|
||||
/// <summary>
|
||||
/// The handle for the <see cref="ID2D1DrawTransformMapper"/> instance in use, if any.
|
||||
/// The handle for the <see cref="D2D1TransformMapper"/> instance in use, if any.
|
||||
/// </summary>
|
||||
private GCHandle d2D1DrawTransformMapperFactoryHandle;
|
||||
private GCHandle d2D1TransformMapperHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The current input rectangle.
|
||||
@@ -293,7 +299,7 @@ internal unsafe partial struct PixelShaderEffect
|
||||
/// <param name="numberOfInputs">The number of inputs for the shader.</param>
|
||||
/// <param name="bytecode">The shader bytecode.</param>
|
||||
/// <param name="bytecodeSize">The size of <paramref name="bytecode"/>.</param>
|
||||
/// <param name="d2D1DrawTransformMapper">The <see cref="ID2D1DrawTransformMapper"/> instance to use for the effect.</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(
|
||||
@@ -301,7 +307,7 @@ internal unsafe partial struct PixelShaderEffect
|
||||
int numberOfInputs,
|
||||
byte* bytecode,
|
||||
int bytecodeSize,
|
||||
ID2D1DrawTransformMapper? d2D1DrawTransformMapper,
|
||||
D2D1TransformMapper? d2D1TransformMapper,
|
||||
IUnknown** effectImpl)
|
||||
{
|
||||
PixelShaderEffect* @this = (PixelShaderEffect*)NativeMemory.Alloc((nuint)sizeof(PixelShaderEffect));
|
||||
@@ -315,7 +321,7 @@ internal unsafe partial struct PixelShaderEffect
|
||||
@this->numberOfInputs = numberOfInputs;
|
||||
@this->bytecode = bytecode;
|
||||
@this->bytecodeSize = bytecodeSize;
|
||||
@this->d2D1DrawTransformMapperFactoryHandle = GCHandle.Alloc(d2D1DrawTransformMapper);
|
||||
@this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper);
|
||||
|
||||
*effectImpl = (IUnknown*)@this;
|
||||
|
||||
@@ -419,7 +425,7 @@ internal unsafe partial struct PixelShaderEffect
|
||||
NativeMemory.Free(this.constantBuffer);
|
||||
}
|
||||
|
||||
this.d2D1DrawTransformMapperFactoryHandle.Free();
|
||||
this.d2D1TransformMapperHandle.Free();
|
||||
|
||||
NativeMemory.Free(Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする