diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperFactory{T, TSelf, TParameters, TTransformMapper}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperFactory{T, TSelf, TParameters, TTransformMapper}.cs new file mode 100644 index 00000000..11f3e3d0 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperFactory{T, TSelf, TParameters, TTransformMapper}.cs @@ -0,0 +1,35 @@ +namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract; + +/// +/// A base type for a transform mapper factory to be used in a D2D1 pixel shader effect. +/// +/// The type of D2D1 pixel shader associated to the transform mapper. +/// The type of concrete transform mapper factory inheriting from this base type. +/// The type of parameters that the transform mapper will use. +/// The type of concrete transform mapper to produce from this factory. +internal abstract class D2D1TransformMapperFactory : ID2D1TransformMapperFactory + where T : unmanaged, ID2D1PixelShader + where TSelf : D2D1TransformMapperFactory, new() + where TTransformMapper : D2D1TransformMapper, new() +{ + /// + /// Creates a new instance with the specified accessor. + /// + /// The input instance to be used to retrieve parameters. + /// The new instance with the specified accessor. + public ID2D1TransformMapperFactory Create(D2D1TransformMapperParametersAccessor accessor) + { + return new TSelf { Parameters = accessor }; + } + + /// + ID2D1TransformMapper ID2D1TransformMapperFactory.Create() + { + return new TTransformMapper { Parameters = Parameters }; + } + + /// + /// The instance to use to retrieve parameters for the transform mapper. + /// + public D2D1TransformMapperParametersAccessor? Parameters { get; init; } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperParametersAccessor{T, TParameters}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperParametersAccessor{T, TParameters}.cs new file mode 100644 index 00000000..a12df793 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperParametersAccessor{T, TParameters}.cs @@ -0,0 +1,16 @@ +namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract; + +/// +/// A base type for an accessor of parameters to be used in D2D1 transform mapping implementations. +/// +/// The type of D2D1 pixel shader associated to the transform mapper. +/// The type of parameters that the transform mapper will use. +internal abstract class D2D1TransformMapperParametersAccessor +{ + /// + /// Gets the parameters to be used by the transform mapper. + /// + /// The input D2D1 pixel shader being executed on the current effect (this can be used to retrieve shader properties). + /// The resulting parameters to be used by the transform mapper. + public abstract TParameters Get(in T shader); +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapper{T, TParameters}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapper{T, TParameters}.cs new file mode 100644 index 00000000..55293655 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapper{T, TParameters}.cs @@ -0,0 +1,84 @@ +using System; +using System.Drawing; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract; + +/// +/// A base type for a transform mapper to be used in a D2D1 pixel shader effect. +/// +/// The type of D2D1 pixel shader associated to the transform mapper. +/// The type of parameters that the transform mapper will use. +internal abstract class D2D1TransformMapper : ID2D1TransformMapper + where T : unmanaged, ID2D1PixelShader +{ + /// + /// The current pixel shader value, which can be used to extract parameters from. + /// + private T shader; + + /// + public D2D1TransformMapperParametersAccessor? Parameters { get; init; } + + /// + /// Transforms an input rectangle to an output rectangle. + /// + /// The parameters to be used to inform the transformation. + /// The input rectangle to transform to output. + protected abstract void TransformInputToOutput(TParameters parameters, ref Rectangle64 rectangle); + + /// + /// Transforms an output rectangle to an input rectangle. + /// + /// The parameters to be used to inform the transformation. + /// The output rectangle to transform to input. + protected abstract void TransformOutputToInput(TParameters parameters, ref Rectangle64 rectangle); + + /// + void ID2D1TransformMapper.MapInputsToOutput(in T shader, ReadOnlySpan inputs, ReadOnlySpan 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; + } + } + + /// + void ID2D1TransformMapper.MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) + { + Rectangle64 output64 = Rectangle64.FromRectangle(invalidInput); + + TransformInputToOutput(Parameters!.Get(in shader), ref output64); + + invalidOutput = output64.ToRectangleWithD2D1LogicallyInfiniteClamping(); + } + + /// + void ID2D1TransformMapper.MapOutputToInputs(in Rectangle output, Span inputs) + { + Rectangle64 output64 = Rectangle64.FromRectangle(output); + + TransformOutputToInput(Parameters!.Get(in shader), ref output64); + + inputs.Fill(output64.ToRectangleWithD2D1LogicallyInfiniteClamping()); + } +} \ 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 new file mode 100644 index 00000000..f07dcc39 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Rectangle64.cs @@ -0,0 +1,190 @@ +using System; +using System.Drawing; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.D2D1.Shaders.Interop.Helpers; + +/// +/// A 64-bit version of the type. +/// +internal struct Rectangle64 : IEquatable +{ + /// + /// The left coordinate of the current rectangle. + /// + private long x; + + /// + /// The top coordinate of the current rectangle. + /// + private long y; + + /// + /// The width of the current rectangle. + /// + private long width; + + /// + /// The height of the current rectangle. + /// + private long height; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The left coordinate of the rectangle. + /// The top coordinate of the rectangle. + /// The width of the rectangle. + /// The height of the rectangle. + public Rectangle64(long x, long y, long width, long height) + { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + /// + /// Gets the left coordinate of the current rectangle. + /// + public readonly long Left + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.x; + } + + /// + /// Gets the top coordinate of the current rectangle. + /// + public readonly long Top + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.y; + } + + /// + /// Gets the right coordinate of the current rectangle. + /// + public readonly long Right + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.x + this.width; + } + + /// + /// Gets the bottom coordinate of the current rectangle. + /// + public readonly long Bottom + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.y + this.height; + } + + /// + /// Gets whether the current instance represents an empty rectangle. + /// + public bool IsEmpty + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.x == long.MaxValue && this.y == long.MaxValue && this.width == long.MinValue && this.height == long.MinValue; + } + + /// + /// Gets a instance representing an empty rectangle. + /// + /// + /// 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. + /// + public static Rectangle64 Empty => new(long.MaxValue, long.MaxValue, long.MinValue, long.MinValue); + + /// + /// Updates the current rectangle to be the union of itself and a given input rectangle. + /// + /// The input instance to compute the union with. + 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; + } + + /// + /// Updates the current rectangle to be the intersection of itself and a given input rectangle. + /// + /// The input instance to compute the intersection with. + 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; + } + + /// + /// 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. + /// + /// The amount fo use to inflate the left edge. + /// + /// + /// + 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; + } + + /// + /// Creates a new from the current instance, clamping to a D2D1 logically infinite rectangle. + /// + /// The equivalent of the current instance. + 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); + } + + /// + public readonly bool Equals(Rectangle64 other) + { + return this.x == other.x && this.y == other.y && this.width == other.width && this.height == other.height; + } + + /// + /// Creates a new instance from an input value. + /// + /// The input value. + /// The resulting instance representing . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Rectangle64 FromRectangle(Rectangle rectangle) + { + return new(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); + } +} \ No newline at end of file