Reduce generic type instantiations

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-06 03:25:49 +02:00
1 親 01c5c39be8
コミット b4a0f0a0af
2ファイル変更
+109 -100

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

+2 -2
ファイルの表示
@@ -60,7 +60,7 @@ public static class D2D1TransformMapperFactory<T>
/// <returns>The resulting <see cref="ID2D1TransformMapperFactory{T}"/> instance for an affine transform.</returns>
public static ID2D1TransformMapperFactory<T> Transform(Matrix3x2 matrix)
{
return new D2D1AffineTransformMapperFactory<T> { ParametersAccessor = new D2D1AffineTransformMapperFactory<T>.ConstantMatrix { Amount = matrix } };
return new D2D1AffineTransformMapperFactory.For<T> { ParametersAccessor = new D2D1AffineTransformMapperFactory.For<T>.ConstantMatrix { Amount = matrix } };
}
/// <summary>
@@ -70,7 +70,7 @@ public static class D2D1TransformMapperFactory<T>
/// <returns>The resulting <see cref="ID2D1TransformMapperFactory{T}"/> instance for an affine transform.</returns>
public static ID2D1TransformMapperFactory<T> Transform(Accessor<Matrix3x2> accessor)
{
return new D2D1AffineTransformMapperFactory<T> { ParametersAccessor = new D2D1AffineTransformMapperFactory<T>.DynamicMatrix { Accessor = accessor } };
return new D2D1AffineTransformMapperFactory.For<T> { ParametersAccessor = new D2D1AffineTransformMapperFactory.For<T>.DynamicMatrix { Accessor = accessor } };
}
/// <summary>
+107 -98
ファイルの表示
@@ -8,130 +8,139 @@ using ComputeSharp.D2D1.Shaders.Interop.Helpers;
namespace ComputeSharp.D2D1.Shaders.Interop.Factories;
/// <summary>
/// A custom <see cref="D2D1TransformMapperFactory{T, TParameters, TTransformMapper}"/> implementation for an affine transform.
/// A container for shader-specific affine transform mapper factory.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader associated to the transform mapper.</typeparam>
internal sealed class D2D1AffineTransformMapperFactory<T> : D2D1TransformMapperFactory<T, Matrix3x2, D2D1AffineTransformMapperFactory<T>.TransformMapper>
where T : unmanaged, ID2D1PixelShader
/// <remarks>
/// This non-generic type is used so that <see cref="Transform"/> and <see cref="SubtractAndClampToMaxIfOverflow"/> are not recompiled for each generic type instantiation.
/// </remarks>
internal sealed class D2D1AffineTransformMapperFactory
{
/// <summary>
/// A <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> implementation for a constant affine transform matrix.
/// Transforms a target rectangle using a given transform matrix.
/// </summary>
public sealed class ConstantMatrix : D2D1TransformMapperParametersAccessor<T, Matrix3x2>
/// <param name="matrix">The transform matrix to use.</param>
/// <param name="rectangle">The rectangle to transform.</param>
private static void Transform(in Matrix3x2 matrix, ref Rectangle64 rectangle)
{
/// <summary>
/// Gets the fixed affine transform matrix.
/// </summary>
public Matrix3x2 Amount { get; init; }
// Get the rectangle corners
Point64 topLeft = new(rectangle.Left, rectangle.Top);
Point64 topRight = new(rectangle.Right, rectangle.Top);
Point64 bottomLeft = new(rectangle.Left, rectangle.Bottom);
Point64 bottomRight = new(rectangle.Right, rectangle.Bottom);
/// <inheritdoc/>
public override Matrix3x2 Get(in T shader)
{
return Amount;
}
// Transform them with the current matrix
var transformedTopLeft = topLeft.Transform(in matrix);
var transformedTopRight = topRight.Transform(in matrix);
var transformedBottomLeft = bottomLeft.Transform(in matrix);
var transformedBottomRight = bottomRight.Transform(in matrix);
// Calculate the bounding box of the transformed points
double transformedLeft = Math.Min(Math.Min(transformedTopLeft.X, transformedTopRight.X), Math.Min(transformedBottomLeft.X, transformedBottomRight.X));
double transformedTop = Math.Min(Math.Min(transformedTopLeft.Y, transformedTopRight.Y), Math.Min(transformedBottomLeft.Y, transformedBottomRight.Y));
double transformedRight = Math.Max(Math.Max(transformedTopLeft.X, transformedTopRight.X), Math.Max(transformedBottomLeft.X, transformedBottomRight.X));
double transformedBottom = Math.Max(Math.Max(transformedTopLeft.Y, transformedTopRight.Y), Math.Max(transformedBottomLeft.Y, transformedBottomRight.Y));
// Round each coordinate as needed and compute the rectangle bounds
long left = (long)Math.Floor(Math.Max(transformedLeft, long.MinValue));
long top = (long)Math.Floor(Math.Max(transformedTop, long.MinValue));
long right = (long)Math.Ceiling(Math.Min(transformedRight, long.MaxValue));
long bottom = (long)Math.Ceiling(Math.Min(transformedBottom, long.MaxValue));
// Calculate the width and height, with clamping in case of overflows
long width = SubtractAndClampToMaxIfOverflow(right, left);
long height = SubtractAndClampToMaxIfOverflow(bottom, top);
rectangle = new(left, top, width, height);
}
/// <summary>
/// A <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> implementation for a dynamic affine transform matrix.
/// Subtracts two values and clamps to <see cref="long.MaxValue"/> if there is an overflow.
/// </summary>
public sealed class DynamicMatrix : D2D1TransformMapperParametersAccessor<T, Matrix3x2>
/// <param name="x">The first value.</param>
/// <param name="y">The value to subtract.</param>
/// <returns>The result of the subtraction, clamped to <see cref="long.MaxValue"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long SubtractAndClampToMaxIfOverflow(long x, long y)
{
/// <summary>
/// Gets the <see cref="D2D1TransformMapperFactory{T}.Accessor{TResult}"/> instance to get the dynamic affine transform matrix.
/// </summary>
public D2D1TransformMapperFactory<T>.Accessor<Matrix3x2>? Accessor { get; init; }
long z = unchecked(x - y);
/// <inheritdoc/>
public override Matrix3x2 Get(in T shader)
// Overflow in (x - y) with carry occurs if and only if x and y have opposite signs
// and the sign of (x - y) with carry is opposite of x (or equivalently same as y).
if (((x ^ y) & (z ^ x)) < 0)
{
return Accessor!(in shader);
return long.MaxValue;
}
return z;
}
/// <summary>
/// A custom <see cref="D2D1TransformMapper{T, TParameters}"/> implementation for an affine transform.
/// A custom <see cref="D2D1TransformMapperFactory{T, TParameters, TTransformMapper}"/> implementation for an affine transform.
/// </summary>
public sealed class TransformMapper : D2D1TransformMapper<T, Matrix3x2>
/// <typeparam name="T">The type of D2D1 pixel shader associated to the transform mapper.</typeparam>
public sealed class For<T> : D2D1TransformMapperFactory<T, Matrix3x2, For<T>.TransformMapper>
where T : unmanaged, ID2D1PixelShader
{
/// <inheritdoc/>
protected override void TransformInputToOutput(in Matrix3x2 parameters, ref Rectangle64 rectangle)
/// <summary>
/// A <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> implementation for a constant affine transform matrix.
/// </summary>
public sealed class ConstantMatrix : D2D1TransformMapperParametersAccessor<T, Matrix3x2>
{
Transform(in parameters, ref rectangle);
}
/// <summary>
/// Gets the fixed affine transform matrix.
/// </summary>
public Matrix3x2 Amount { get; init; }
/// <inheritdoc/>
protected override void TransformOutputToInput(in Matrix3x2 parameters, ref Rectangle64 rectangle)
{
if (!Matrix3x2.Invert(parameters, out Matrix3x2 matrix))
/// <inheritdoc/>
public override Matrix3x2 Get(in T shader)
{
static void Throw()
return Amount;
}
}
/// <summary>
/// A <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> implementation for a dynamic affine transform matrix.
/// </summary>
public sealed class DynamicMatrix : D2D1TransformMapperParametersAccessor<T, Matrix3x2>
{
/// <summary>
/// Gets the <see cref="D2D1TransformMapperFactory{T}.Accessor{TResult}"/> instance to get the dynamic affine transform matrix.
/// </summary>
public D2D1TransformMapperFactory<T>.Accessor<Matrix3x2>? Accessor { get; init; }
/// <inheritdoc/>
public override Matrix3x2 Get(in T shader)
{
return Accessor!(in shader);
}
}
/// <summary>
/// A custom <see cref="D2D1TransformMapper{T, TParameters}"/> implementation for an affine transform.
/// </summary>
public sealed class TransformMapper : D2D1TransformMapper<T, Matrix3x2>
{
/// <inheritdoc/>
protected override void TransformInputToOutput(in Matrix3x2 parameters, ref Rectangle64 rectangle)
{
Transform(in parameters, ref rectangle);
}
/// <inheritdoc/>
protected override void TransformOutputToInput(in Matrix3x2 parameters, ref Rectangle64 rectangle)
{
if (!Matrix3x2.Invert(parameters, out Matrix3x2 matrix))
{
throw new ArgumentException("The input transform matrix can't be inverted, so it can't be used for a transform mapper.");
static void Throw()
{
throw new ArgumentException("The input transform matrix can't be inverted, so it can't be used for a transform mapper.");
}
Throw();
}
Throw();
Transform(in matrix, ref rectangle);
}
Transform(in matrix, ref rectangle);
}
/// <summary>
/// Transforms a target rectangle using a given transform matrix.
/// </summary>
/// <param name="matrix">The transform matrix to use.</param>
/// <param name="rectangle">The rectangle to transform.</param>
private static void Transform(in Matrix3x2 matrix, ref Rectangle64 rectangle)
{
// Get the rectangle corners
Point64 topLeft = new(rectangle.Left, rectangle.Top);
Point64 topRight = new(rectangle.Right, rectangle.Top);
Point64 bottomLeft = new(rectangle.Left, rectangle.Bottom);
Point64 bottomRight = new(rectangle.Right, rectangle.Bottom);
// Transform them with the current matrix
var transformedTopLeft = topLeft.Transform(in matrix);
var transformedTopRight = topRight.Transform(in matrix);
var transformedBottomLeft = bottomLeft.Transform(in matrix);
var transformedBottomRight = bottomRight.Transform(in matrix);
// Calculate the bounding box of the transformed points
double transformedLeft = Math.Min(Math.Min(transformedTopLeft.X, transformedTopRight.X), Math.Min(transformedBottomLeft.X, transformedBottomRight.X));
double transformedTop = Math.Min(Math.Min(transformedTopLeft.Y, transformedTopRight.Y), Math.Min(transformedBottomLeft.Y, transformedBottomRight.Y));
double transformedRight = Math.Max(Math.Max(transformedTopLeft.X, transformedTopRight.X), Math.Max(transformedBottomLeft.X, transformedBottomRight.X));
double transformedBottom = Math.Max(Math.Max(transformedTopLeft.Y, transformedTopRight.Y), Math.Max(transformedBottomLeft.Y, transformedBottomRight.Y));
// Round each coordinate as needed and compute the rectangle bounds
long left = (long)Math.Floor(Math.Max(transformedLeft, long.MinValue));
long top = (long)Math.Floor(Math.Max(transformedTop, long.MinValue));
long right = (long)Math.Ceiling(Math.Min(transformedRight, long.MaxValue));
long bottom = (long)Math.Ceiling(Math.Min(transformedBottom, long.MaxValue));
// Calculate the width and height, with clamping in case of overflows
long width = SubtractAndClampToMaxIfOverflow(right, left);
long height = SubtractAndClampToMaxIfOverflow(bottom, top);
rectangle = new(left, top, width, height);
}
/// <summary>
/// Subtracts two values and clamps to <see cref="long.MaxValue"/> if there is an overflow.
/// </summary>
/// <param name="x">The first value.</param>
/// <param name="y">The value to subtract.</param>
/// <returns>The result of the subtraction, clamped to <see cref="long.MaxValue"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long SubtractAndClampToMaxIfOverflow(long x, long y)
{
long z = unchecked(x - y);
// Overflow in (x - y) with carry occurs if and only if x and y have opposite signs
// and the sign of (x - y) with carry is opposite of x (or equivalently same as y).
if (((x ^ y) & (z ^ x)) < 0)
{
return long.MaxValue;
}
return z;
}
}
}