From b4a0f0a0afd4a8964095fecd61a831bafc199a60 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 6 Sep 2022 03:25:49 +0200 Subject: [PATCH] Reduce generic type instantiations --- .../Interop/D2D1TransformMapperFactory{T}.cs | 4 +- .../D2D1AffineTransformMapperFactory.cs | 205 +++++++++--------- 2 files changed, 109 insertions(+), 100 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs index b693fb3b..5c411d0d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs @@ -60,7 +60,7 @@ public static class D2D1TransformMapperFactory /// The resulting instance for an affine transform. public static ID2D1TransformMapperFactory Transform(Matrix3x2 matrix) { - return new D2D1AffineTransformMapperFactory { ParametersAccessor = new D2D1AffineTransformMapperFactory.ConstantMatrix { Amount = matrix } }; + return new D2D1AffineTransformMapperFactory.For { ParametersAccessor = new D2D1AffineTransformMapperFactory.For.ConstantMatrix { Amount = matrix } }; } /// @@ -70,7 +70,7 @@ public static class D2D1TransformMapperFactory /// The resulting instance for an affine transform. public static ID2D1TransformMapperFactory Transform(Accessor accessor) { - return new D2D1AffineTransformMapperFactory { ParametersAccessor = new D2D1AffineTransformMapperFactory.DynamicMatrix { Accessor = accessor } }; + return new D2D1AffineTransformMapperFactory.For { ParametersAccessor = new D2D1AffineTransformMapperFactory.For.DynamicMatrix { Accessor = accessor } }; } /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs index d857e566..9b9f662c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs @@ -8,130 +8,139 @@ using ComputeSharp.D2D1.Shaders.Interop.Helpers; namespace ComputeSharp.D2D1.Shaders.Interop.Factories; /// -/// A custom implementation for an affine transform. +/// A container for shader-specific affine transform mapper factory. /// -/// The type of D2D1 pixel shader associated to the transform mapper. -internal sealed class D2D1AffineTransformMapperFactory : D2D1TransformMapperFactory.TransformMapper> - where T : unmanaged, ID2D1PixelShader +/// +/// This non-generic type is used so that and are not recompiled for each generic type instantiation. +/// +internal sealed class D2D1AffineTransformMapperFactory { /// - /// A implementation for a constant affine transform matrix. + /// Transforms a target rectangle using a given transform matrix. /// - public sealed class ConstantMatrix : D2D1TransformMapperParametersAccessor + /// The transform matrix to use. + /// The rectangle to transform. + private static void Transform(in Matrix3x2 matrix, ref Rectangle64 rectangle) { - /// - /// Gets the fixed affine transform matrix. - /// - 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); - /// - 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); } /// - /// A implementation for a dynamic affine transform matrix. + /// Subtracts two values and clamps to if there is an overflow. /// - public sealed class DynamicMatrix : D2D1TransformMapperParametersAccessor + /// The first value. + /// The value to subtract. + /// The result of the subtraction, clamped to . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static long SubtractAndClampToMaxIfOverflow(long x, long y) { - /// - /// Gets the instance to get the dynamic affine transform matrix. - /// - public D2D1TransformMapperFactory.Accessor? Accessor { get; init; } + long z = unchecked(x - y); - /// - 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; } /// - /// A custom implementation for an affine transform. + /// A custom implementation for an affine transform. /// - public sealed class TransformMapper : D2D1TransformMapper + /// The type of D2D1 pixel shader associated to the transform mapper. + public sealed class For : D2D1TransformMapperFactory.TransformMapper> + where T : unmanaged, ID2D1PixelShader { - /// - protected override void TransformInputToOutput(in Matrix3x2 parameters, ref Rectangle64 rectangle) + /// + /// A implementation for a constant affine transform matrix. + /// + public sealed class ConstantMatrix : D2D1TransformMapperParametersAccessor { - Transform(in parameters, ref rectangle); - } + /// + /// Gets the fixed affine transform matrix. + /// + public Matrix3x2 Amount { get; init; } - /// - protected override void TransformOutputToInput(in Matrix3x2 parameters, ref Rectangle64 rectangle) - { - if (!Matrix3x2.Invert(parameters, out Matrix3x2 matrix)) + /// + public override Matrix3x2 Get(in T shader) { - static void Throw() + return Amount; + } + } + + /// + /// A implementation for a dynamic affine transform matrix. + /// + public sealed class DynamicMatrix : D2D1TransformMapperParametersAccessor + { + /// + /// Gets the instance to get the dynamic affine transform matrix. + /// + public D2D1TransformMapperFactory.Accessor? Accessor { get; init; } + + /// + public override Matrix3x2 Get(in T shader) + { + return Accessor!(in shader); + } + } + + /// + /// A custom implementation for an affine transform. + /// + public sealed class TransformMapper : D2D1TransformMapper + { + /// + protected override void TransformInputToOutput(in Matrix3x2 parameters, ref Rectangle64 rectangle) + { + Transform(in parameters, ref rectangle); + } + + /// + 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); - } - - /// - /// Transforms a target rectangle using a given transform matrix. - /// - /// The transform matrix to use. - /// The rectangle to transform. - 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); - } - - /// - /// Subtracts two values and clamps to if there is an overflow. - /// - /// The first value. - /// The value to subtract. - /// The result of the subtraction, clamped to . - [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; } } } \ No newline at end of file