Remove allocation and indirection for built-in transforms

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-06 02:32:58 +02:00
1 親 d127a9f556
コミット 05846d7877
2ファイル変更
+46 -24

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

+11 -2
ファイルの表示
@@ -61,14 +61,23 @@ internal abstract class D2D1TransformMapper
/// </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)
public static D2D1TransformMapper? Get(ID2D1TransformMapper<T>? transformMapper)
{
if (transformMapper is null)
{
return null;
}
return new(transformMapper);
// If one of the built-in transform mapper factories from D2D1TransformMapperFactory<T> was used,
// the returned ID2D1TransformMapper<T> instance will already inherit from D2D1TransformMapper.
// If that's the case, that object can be returned directly, saving one allocation and avoiding
// an extra interface stub dispatch call every time one of the transform mapping APIs is called.
if (transformMapper is D2D1TransformMapper d2D1TransformMapper)
{
return d2D1TransformMapper;
}
return new For<T>(transformMapper);
}
/// <inheritdoc/>
@@ -1,8 +1,11 @@
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.Interop.Effects;
using ComputeSharp.D2D1.Shaders.Interop.Helpers;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract;
/// <summary>
@@ -10,7 +13,7 @@ namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract;
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader associated to the transform mapper.</typeparam>
/// <typeparam name="TParameters">The type of parameters that the transform mapper will use.</typeparam>
internal abstract class D2D1TransformMapper<T, TParameters> : ID2D1TransformMapper<T>
internal abstract class D2D1TransformMapper<T, TParameters> : D2D1TransformMapper, ID2D1TransformMapper<T>
where T : unmanaged, ID2D1PixelShader
where TParameters : unmanaged
{
@@ -37,7 +40,37 @@ internal abstract class D2D1TransformMapper<T, TParameters> : ID2D1TransformMapp
protected abstract void TransformOutputToInput(in TParameters parameters, ref Rectangle64 rectangle);
/// <inheritdoc/>
void ID2D1TransformMapper<T>.MapInputsToOutput(in T shader, ReadOnlySpan<Rectangle> inputs, ReadOnlySpan<Rectangle> opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
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);
MapInputsToOutput(in shader, inputs, opaqueInputs, out output, out opaqueOutput);
}
/// <inheritdoc/>
public override void MapOutputToInputs(in Rectangle output, Span<Rectangle> inputs)
{
Rectangle64 output64 = Rectangle64.FromRectangle(output);
TransformOutputToInput(in this.parameters, ref output64);
inputs.Fill(output64.ToRectangleWithD2D1LogicallyInfiniteClamping());
}
/// <inheritdoc/>
public override void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput)
{
Rectangle64 output64 = Rectangle64.FromRectangle(invalidInput);
TransformInputToOutput(in this.parameters, ref output64);
invalidOutput = output64.ToRectangleWithD2D1LogicallyInfiniteClamping();
}
/// <inheritdoc/>
public void MapInputsToOutput(in T shader, ReadOnlySpan<Rectangle> inputs, ReadOnlySpan<Rectangle> opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
{
this.parameters = Parameters!.Get(in shader);
@@ -63,24 +96,4 @@ internal abstract class D2D1TransformMapper<T, TParameters> : ID2D1TransformMapp
opaqueOutput = Rectangle.Empty;
}
}
/// <inheritdoc/>
void ID2D1TransformMapper<T>.MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput)
{
Rectangle64 output64 = Rectangle64.FromRectangle(invalidInput);
TransformInputToOutput(in this.parameters, ref output64);
invalidOutput = output64.ToRectangleWithD2D1LogicallyInfiniteClamping();
}
/// <inheritdoc/>
void ID2D1TransformMapper<T>.MapOutputToInputs(in Rectangle output, Span<Rectangle> inputs)
{
Rectangle64 output64 = Rectangle64.FromRectangle(output);
TransformOutputToInput(in this.parameters, ref output64);
inputs.Fill(output64.ToRectangleWithD2D1LogicallyInfiniteClamping());
}
}