Add base types for built-in transform mappers
このコミットが含まれているのは:
1 親
2c9dad8c98
コミット
d2f19f7c3f
4ファイル変更
+325
検索条件に一致するファイルはありません
+35
@@ -0,0 +1,35 @@
|
||||
namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract;
|
||||
|
||||
/// <summary>
|
||||
/// A base type for a transform mapper factory to be used in a D2D1 pixel shader effect.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader associated to the transform mapper.</typeparam>
|
||||
/// <typeparam name="TSelf">The type of concrete transform mapper factory inheriting from this base type.</typeparam>
|
||||
/// <typeparam name="TParameters">The type of parameters that the transform mapper will use.</typeparam>
|
||||
/// <typeparam name="TTransformMapper">The type of concrete transform mapper to produce from this factory.</typeparam>
|
||||
internal abstract class D2D1TransformMapperFactory<T, TSelf, TParameters, TTransformMapper> : ID2D1TransformMapperFactory<T>
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
where TSelf : D2D1TransformMapperFactory<T, TSelf, TParameters, TTransformMapper>, new()
|
||||
where TTransformMapper : D2D1TransformMapper<T, TParameters>, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ID2D1TransformMapperFactory{T}"/> instance with the specified accessor.
|
||||
/// </summary>
|
||||
/// <param name="accessor">The input <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> instance to be used to retrieve parameters.</param>
|
||||
/// <returns>The new <see cref="ID2D1TransformMapperFactory{T}"/> instance with the specified accessor.</returns>
|
||||
public ID2D1TransformMapperFactory<T> Create(D2D1TransformMapperParametersAccessor<T, TParameters> accessor)
|
||||
{
|
||||
return new TSelf { Parameters = accessor };
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ID2D1TransformMapper<T> ID2D1TransformMapperFactory<T>.Create()
|
||||
{
|
||||
return new TTransformMapper { Parameters = Parameters };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> instance to use to retrieve parameters for the transform mapper.
|
||||
/// </summary>
|
||||
public D2D1TransformMapperParametersAccessor<T, TParameters>? Parameters { get; init; }
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract;
|
||||
|
||||
/// <summary>
|
||||
/// A base type for an accessor of parameters to be used in D2D1 transform mapping implementations.
|
||||
/// </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 D2D1TransformMapperParametersAccessor<T, TParameters>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the parameters to be used by the transform mapper.
|
||||
/// </summary>
|
||||
/// <param name="shader">The input D2D1 pixel shader being executed on the current effect (this can be used to retrieve shader properties).</param>
|
||||
/// <returns>The resulting parameters to be used by the transform mapper.</returns>
|
||||
public abstract TParameters Get(in T shader);
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract;
|
||||
|
||||
/// <summary>
|
||||
/// A base type for a transform mapper to be used in a D2D1 pixel shader effect.
|
||||
/// </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>
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The current pixel shader value, which can be used to extract parameters from.
|
||||
/// </summary>
|
||||
private T shader;
|
||||
|
||||
/// <inheritdoc cref="D2D1TransformMapperFactory{T, TSelf, TParameters, TTransformMapper}.Parameters"/>
|
||||
public D2D1TransformMapperParametersAccessor<T, TParameters>? Parameters { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Transforms an input rectangle to an output rectangle.
|
||||
/// </summary>
|
||||
/// <param name="parameters">The parameters to be used to inform the transformation.</param>
|
||||
/// <param name="rectangle">The input rectangle to transform to output.</param>
|
||||
protected abstract void TransformInputToOutput(TParameters parameters, ref Rectangle64 rectangle);
|
||||
|
||||
/// <summary>
|
||||
/// Transforms an output rectangle to an input rectangle.
|
||||
/// </summary>
|
||||
/// <param name="parameters">The parameters to be used to inform the transformation.</param>
|
||||
/// <param name="rectangle">The output rectangle to transform to input.</param>
|
||||
protected abstract void TransformOutputToInput(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)
|
||||
{
|
||||
this.shader = shader;
|
||||
|
||||
if (inputs.IsEmpty)
|
||||
{
|
||||
Unsafe.SkipInit(out output);
|
||||
|
||||
output.ToD2D1Infinite();
|
||||
opaqueOutput = Rectangle.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
Rectangle64 output64 = Rectangle64.Empty;
|
||||
|
||||
foreach (ref readonly Rectangle input in inputs)
|
||||
{
|
||||
output64.Union(Rectangle64.FromRectangle(input));
|
||||
}
|
||||
|
||||
TransformInputToOutput(Parameters!.Get(in shader), ref output64);
|
||||
|
||||
output = output64.ToRectangleWithD2D1LogicallyInfiniteClamping();
|
||||
opaqueOutput = Rectangle.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ID2D1TransformMapper<T>.MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput)
|
||||
{
|
||||
Rectangle64 output64 = Rectangle64.FromRectangle(invalidInput);
|
||||
|
||||
TransformInputToOutput(Parameters!.Get(in shader), ref output64);
|
||||
|
||||
invalidOutput = output64.ToRectangleWithD2D1LogicallyInfiniteClamping();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ID2D1TransformMapper<T>.MapOutputToInputs(in Rectangle output, Span<Rectangle> inputs)
|
||||
{
|
||||
Rectangle64 output64 = Rectangle64.FromRectangle(output);
|
||||
|
||||
TransformOutputToInput(Parameters!.Get(in shader), ref output64);
|
||||
|
||||
inputs.Fill(output64.ToRectangleWithD2D1LogicallyInfiniteClamping());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Interop.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A 64-bit version of the <see cref="Rectangle"/> type.
|
||||
/// </summary>
|
||||
internal struct Rectangle64 : IEquatable<Rectangle64>
|
||||
{
|
||||
/// <summary>
|
||||
/// The left coordinate of the current rectangle.
|
||||
/// </summary>
|
||||
private long x;
|
||||
|
||||
/// <summary>
|
||||
/// The top coordinate of the current rectangle.
|
||||
/// </summary>
|
||||
private long y;
|
||||
|
||||
/// <summary>
|
||||
/// The width of the current rectangle.
|
||||
/// </summary>
|
||||
private long width;
|
||||
|
||||
/// <summary>
|
||||
/// The height of the current rectangle.
|
||||
/// </summary>
|
||||
private long height;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Rectangle64"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="x">The left coordinate of the rectangle.</param>
|
||||
/// <param name="y">The top coordinate of the rectangle.</param>
|
||||
/// <param name="width">The width of the rectangle.</param>
|
||||
/// <param name="height">The height of the rectangle.</param>
|
||||
public Rectangle64(long x, long y, long width, long height)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the left coordinate of the current rectangle.
|
||||
/// </summary>
|
||||
public readonly long Left
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the top coordinate of the current rectangle.
|
||||
/// </summary>
|
||||
public readonly long Top
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the right coordinate of the current rectangle.
|
||||
/// </summary>
|
||||
public readonly long Right
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.x + this.width;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bottom coordinate of the current rectangle.
|
||||
/// </summary>
|
||||
public readonly long Bottom
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.y + this.height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the current instance represents an empty rectangle.
|
||||
/// </summary>
|
||||
public bool IsEmpty
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.x == long.MaxValue && this.y == long.MaxValue && this.width == long.MinValue && this.height == long.MinValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="Rectangle64"/> instance representing an empty rectangle.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The returned instance is such that computing the union of it with any other rectangle yields the other
|
||||
/// rectangle, and computing the intersection of it with any other rectangle yields the empty rectangle.
|
||||
/// </remarks>
|
||||
public static Rectangle64 Empty => new(long.MaxValue, long.MaxValue, long.MinValue, long.MinValue);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the current rectangle to be the union of itself and a given input rectangle.
|
||||
/// </summary>
|
||||
/// <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);
|
||||
|
||||
this.x = left;
|
||||
this.y = top;
|
||||
this.width = right - left;
|
||||
this.height = bottom - top;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the current rectangle to be the intersection of itself and a given input rectangle.
|
||||
/// </summary>
|
||||
/// <param name="rectangle">The input <see cref="Rectangle64"/> instance to compute the intersection with.</param>
|
||||
public void Intersect(Rectangle64 rectangle)
|
||||
{
|
||||
long maxLeft = Math.Max(this.x, rectangle.x);
|
||||
long maxTop = Math.Max(this.y, rectangle.y);
|
||||
long width = Math.Max(Math.Min(this.Right, rectangle.Right) - maxLeft, 0);
|
||||
long height = Math.Max(Math.Min(this.Bottom, rectangle.Bottom) - maxTop, 0);
|
||||
|
||||
this.x = maxLeft;
|
||||
this.y = maxTop;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inflates the current rectangle by a specified amount.
|
||||
/// </summary>
|
||||
/// <param name="x">The horizontal inflation amount.</param>
|
||||
/// <param name="y">The vertical inflation amount.</param>
|
||||
public void Inflate(long x, long y)
|
||||
{
|
||||
this.x -= x;
|
||||
this.y -= y;
|
||||
this.width += (2 * x);
|
||||
this.height += (2 * y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inflates the current rectangle by a specified amount.
|
||||
/// </summary>
|
||||
/// <param name="left">The amount fo use to inflate the left edge.</param>
|
||||
/// <param name="top"></param>
|
||||
/// <param name="right"></param>
|
||||
/// <param name="bottom"></param>
|
||||
public void Inflate(long left, long top, long right, long bottom)
|
||||
{
|
||||
this.x -= left;
|
||||
this.y -= top;
|
||||
this.width += left + right;
|
||||
this.height += top + bottom;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Rectangle"/> from the current <see cref="Rectangle64"/> instance, clamping to a D2D1 logically infinite rectangle.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="Rectangle"/> equivalent of the current instance.</returns>
|
||||
public Rectangle ToRectangleWithD2D1LogicallyInfiniteClamping()
|
||||
{
|
||||
Intersect(new Rectangle64(int.MinValue / 2, int.MinValue / 2, int.MaxValue, int.MaxValue));
|
||||
|
||||
return new((int)this.x, (int)this.y, (int)this.width, (int)this.height);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public readonly bool Equals(Rectangle64 other)
|
||||
{
|
||||
return this.x == other.x && this.y == other.y && this.width == other.width && this.height == other.height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Rectangle64"/> instance from an input <see cref="Rectangle"/> value.
|
||||
/// </summary>
|
||||
/// <param name="rectangle">The input <see cref="Rectangle"/> value.</param>
|
||||
/// <returns>The resulting <see cref="Rectangle64"/> instance representing <paramref name="rectangle"/>.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Rectangle64 FromRectangle(Rectangle rectangle)
|
||||
{
|
||||
return new(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする