From f2c7ebdcff073255316836f80ab92d28f552b013 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 5 Sep 2022 01:51:19 +0200 Subject: [PATCH] Add D2D1TransformMapperFactory.Transform APIs --- .../Interop/D2D1TransformMapperFactory{T}.cs | 23 +++- .../D2D1AffineTransformMapperFactory.cs | 104 ++++++++++++++++++ .../D2D1InflateTransformMapperFactory.cs | 6 + .../Shaders/Interop/Helpers/Point64.cs | 87 +++++++++++++++ .../Shaders/Interop/Helpers/Rectangle64.cs | 13 --- 5 files changed, 219 insertions(+), 14 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs create mode 100644 src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs index 0796e88f..846ed352 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs @@ -1,4 +1,5 @@ -using ComputeSharp.D2D1.Shaders.Interop.Factories; +using System.Numerics; +using ComputeSharp.D2D1.Shaders.Interop.Factories; namespace ComputeSharp.D2D1; @@ -52,6 +53,26 @@ public static class D2D1TransformMapperFactory return new D2D1InflateTransformMapperFactory { Parameters = new D2D1InflateTransformMapperFactory.DynamicLeftTopRightBottomAmount { Accessor = accessor } }; } + /// + /// Creates an instance for an affine transform. + /// + /// The fixed transformation matrix to use. + /// The resulting instance for an affine transform. + public static ID2D1TransformMapperFactory Transform(Matrix3x2 matrix) + { + return new D2D1AffineTransformMapperFactory { Parameters = new D2D1AffineTransformMapperFactory.FixedMatrix { Amount = matrix } }; + } + + /// + /// Creates an instance for an affine transform. + /// + /// The input instance to retrieve the transformation matrix to use. + /// The resulting instance for an affine transform. + public static ID2D1TransformMapperFactory Transform(Accessor accessor) + { + return new D2D1AffineTransformMapperFactory { Parameters = new D2D1AffineTransformMapperFactory.DynamicMatrix { Accessor = accessor } }; + } + /// /// An accessor of parameters to be used in D2D1 transform mapping implementations. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs new file mode 100644 index 00000000..636a9586 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs @@ -0,0 +1,104 @@ +using System; +using System.Numerics; +using ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract; +using ComputeSharp.D2D1.Shaders.Interop.Helpers; + +namespace ComputeSharp.D2D1.Shaders.Interop.Factories; + +/// +/// A custom implementation for an affine transform. +/// +/// The type of D2D1 pixel shader associated to the transform mapper. +internal sealed class D2D1AffineTransformMapperFactory : D2D1TransformMapperFactory, Matrix3x2, D2D1AffineTransformMapperFactory.TransformMapper> + where T : unmanaged, ID2D1PixelShader +{ + /// + /// A implementation for a fixed affine transform matrix. + /// + public sealed class FixedMatrix : D2D1TransformMapperParametersAccessor + { + /// + /// Gets the fixed affine transform matrix. + /// + public Matrix3x2 Amount { get; init; } + + /// + public override Matrix3x2 Get(in T shader) + { + 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(Matrix3x2 parameters, ref Rectangle64 rectangle) + { + Transform(parameters, ref rectangle); + } + + /// + protected override void TransformOutputToInput(Matrix3x2 parameters, ref Rectangle64 rectangle) + { + if (!Matrix3x2.Invert(parameters, out Matrix3x2 matrix)) + { + 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(); + } + + Transform(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(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 + topLeft.Transform(in matrix); + topRight.Transform(in matrix); + bottomLeft.Transform(in matrix); + bottomRight.Transform(in matrix); + + // Calculate the bounding box of the transformed points + long x = Math.Min(Math.Min(topLeft.X, topRight.X), Math.Min(bottomLeft.X, bottomRight.X)); + long y = Math.Min(Math.Min(topLeft.Y, topRight.Y), Math.Min(bottomLeft.Y, bottomRight.Y)); + long width = Math.Max(Math.Max(topLeft.X, topRight.X), Math.Max(bottomLeft.X, bottomRight.X)) - rectangle.Left; + long height = Math.Max(Math.Max(topLeft.Y, topRight.Y), Math.Max(bottomLeft.Y, bottomRight.Y)) - rectangle.Top; + + rectangle = new(x, y, width, height); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1InflateTransformMapperFactory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1InflateTransformMapperFactory.cs index 314736dd..5e628d82 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1InflateTransformMapperFactory.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1InflateTransformMapperFactory.cs @@ -64,6 +64,9 @@ internal sealed class D2D1InflateTransformMapperFactory : D2D1TransformMapper /// public sealed class DynamicAmount : D2D1TransformMapperParametersAccessor { + /// + /// Gets the instance to get the dynamic inflate amount. + /// public D2D1TransformMapperFactory.Accessor? Accessor { get; init; } /// @@ -80,6 +83,9 @@ internal sealed class D2D1InflateTransformMapperFactory : D2D1TransformMapper /// public sealed class DynamicLeftTopRightBottomAmount : D2D1TransformMapperParametersAccessor { + /// + /// Gets the instance to get the dynamic inflate LTRB amount. + /// public D2D1TransformMapperFactory.Accessor<(int, int, int, int)>? Accessor { get; init; } /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs new file mode 100644 index 00000000..fb17ee14 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs @@ -0,0 +1,87 @@ +using System; +using System.Drawing; +using System.Numerics; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.D2D1.Shaders.Interop.Helpers; + +/// +/// A 64-bit version of the type. +/// +internal struct Point64 : IEquatable +{ + /// + /// The left coordinate of the current point. + /// + private long x; + + /// + /// The top coordinate of the current point. + /// + private long y; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The horizontal coordinate of the point. + /// The vertical coordinate of the point. + public Point64(long x, long y) + { + this.x = x; + this.y = y; + } + + /// + /// Gets the horizontal coordinate of the current point. + /// + public readonly long X + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.x; + } + + /// + /// Gets the vertical coordinate of the current point. + /// + public readonly long Y + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.y; + } + + /// + /// Transforms the current point using an input matrix. + /// + /// The transformation matrix to use. + public void Transform(in Matrix3x2 matrix) + { + // A 3x2 transformation matrix can be thought of a 2x2 matrix with an extra + // 1x2 row representing absolute offsets. As such, these do not compute into + // the actual cross product with the point (which would not be doable otherwise). + // + // Consider a 3x2 matrix as follows: + // + // [ M11 , M12 ] + // [ M21 , M22 ] + // [ OffsetX (M31), OffsetY (M32) ] + // + // We first transform the point by doint the cross product with the 2x2 sub-matrix: + // + // [ M11, M12 ] + // [ x, y] [ M21, M22 ] = [ x * M11 + y * M21, x * M12 + y * M22] + // + // Then we sum the absolute offsets, which results in the following formula: + // + // [ M11, M12 ] + // [ M21, M22 ] + // [ x, y] [ M31, M32 ] = [ x * M11 + y * M21 + M31, x * M12 + y * M22 + m32] + this.x = (long)Math.Round(this.x * matrix.M11 + this.y * matrix.M21 + matrix.M31); + this.y = (long)Math.Round(this.x * matrix.M12 + this.y * matrix.M22 + matrix.M32); + } + + /// + public readonly bool Equals(Point64 other) + { + return this.x == other.x && this.y == other.y; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Rectangle64.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Rectangle64.cs index f07dcc39..375fef1a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Rectangle64.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Rectangle64.cs @@ -132,19 +132,6 @@ internal struct Rectangle64 : IEquatable this.height = height; } - /// - /// Inflates the current rectangle by a specified amount. - /// - /// The horizontal inflation amount. - /// The vertical inflation amount. - public void Inflate(long x, long y) - { - this.x -= x; - this.y -= y; - this.width += (2 * x); - this.height += (2 * y); - } - /// /// Inflates the current rectangle by a specified amount. ///