diff --git a/src/ComputeSharp.D2D1/Interfaces/ID2D1TransformMapperFactory{T}.cs b/src/ComputeSharp.D2D1/Interfaces/ID2D1TransformMapperFactory{T}.cs new file mode 100644 index 00000000..16b10fbf --- /dev/null +++ b/src/ComputeSharp.D2D1/Interfaces/ID2D1TransformMapperFactory{T}.cs @@ -0,0 +1,15 @@ +namespace ComputeSharp.D2D1; + +/// +/// An acting as a factory for instances. +/// +/// The type of shader this factory creates transform mapper instances for. +public interface ID2D1TransformMapperFactory + where T : unmanaged, ID2D1PixelShader +{ + /// + /// Creates a new instance for a given shader. + /// + /// A new instance for the input shader. + ID2D1TransformMapper Create(); +} diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs index ebef74a0..dfc2c329 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs @@ -1,6 +1,9 @@ using System; using System.Buffers; +using System.Diagnostics.CodeAnalysis; +#if NET6_0_OR_GREATER using System.Numerics; +#endif using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -55,7 +58,7 @@ internal ref struct ArrayPoolBinaryWriter ThrowObjectDisposedException(); } - return array!.AsSpan(0, this.index); + return array.AsSpan(0, this.index); } } @@ -108,7 +111,7 @@ internal ref struct ArrayPoolBinaryWriter ThrowArgumentOutOfRangeExceptionForNegativeCount(); } - if (this.index > array!.Length - count) + if (this.index > array.Length - count) { ThrowArgumentExceptionForAdvancedTooFar(); } @@ -152,7 +155,7 @@ internal ref struct ArrayPoolBinaryWriter sizeHint = 1; } - if (sizeHint > array!.Length - this.index) + if (sizeHint > array.Length - this.index) { ResizeBuffer(sizeHint); } @@ -204,6 +207,7 @@ internal ref struct ArrayPoolBinaryWriter /// /// Throws an when the requested count is negative. /// + [DoesNotReturn] private static void ThrowArgumentOutOfRangeExceptionForNegativeCount() { throw new ArgumentOutOfRangeException("count", "The count can't be a negative value."); @@ -212,6 +216,7 @@ internal ref struct ArrayPoolBinaryWriter /// /// Throws an when the size hint is negative. /// + [DoesNotReturn] private static void ThrowArgumentOutOfRangeExceptionForNegativeSizeHint() { throw new ArgumentOutOfRangeException("sizeHint", "The size hint can't be a negative value."); @@ -220,6 +225,7 @@ internal ref struct ArrayPoolBinaryWriter /// /// Throws an when the requested count is negative. /// + [DoesNotReturn] private static void ThrowArgumentExceptionForAdvancedTooFar() { throw new ArgumentException("The buffer writer has advanced too far."); @@ -228,6 +234,7 @@ internal ref struct ArrayPoolBinaryWriter /// /// Throws an when is . /// + [DoesNotReturn] private static void ThrowObjectDisposedException() { throw new ObjectDisposedException("The current buffer has already been disposed."); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index cd73c442..583abe7c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -59,7 +59,7 @@ public static unsafe class D2D1PixelShaderEffect ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); } - RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId); + RegisterForD2D1Factory1(d2D1Factory1, D2D1TransformMapper.FactoryOf.Instance, out effectId); } /// @@ -72,7 +72,7 @@ public static unsafe class D2D1PixelShaderEffect /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . - public static void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) + public static void RegisterForD2D1Factory1(void* d2D1Factory1, ID2D1TransformMapperFactory? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { if (d2D1Factory1 is null) @@ -258,7 +258,7 @@ public static unsafe class D2D1PixelShaderEffect /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. - /// For more info and for details on the binary format, see . + /// For more info and for details on the binary format, see . public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader { @@ -273,12 +273,12 @@ public static unsafe class D2D1PixelShaderEffect /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. - /// For more info and for details on the binary format, see . + /// For more info and for details on the binary format, see . public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { - return GetRegistrationBlob(static () => new TMapper(), out effectId); + return GetRegistrationBlob(D2D1TransformMapper.FactoryOf.Instance, out effectId); } /// @@ -321,7 +321,7 @@ public static unsafe class D2D1PixelShaderEffect /// For more info, see . /// /// - public static ReadOnlyMemory GetRegistrationBlob(Func>? mapperFactory, out Guid effectId) + public static ReadOnlyMemory GetRegistrationBlob(ID2D1TransformMapperFactory? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs new file mode 100644 index 00000000..1b914639 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapperFactory{T}.cs @@ -0,0 +1,99 @@ +using System.Numerics; +using ComputeSharp.D2D1.Helpers; +using ComputeSharp.D2D1.Shaders.Interop.Factories; + +namespace ComputeSharp.D2D1.Interop; + +/// +/// Provides built-in transform mapper factories for common transform mapping operations. +/// +/// The type of D2D1 pixel shader associated to the transform mapper. +public static class D2D1TransformMapperFactory + where T : unmanaged, ID2D1PixelShader +{ + /// + /// Creates an instance for an inflate transform. + /// + /// The fixed inflate amount to use (applies to all edges of the input area). + /// The resulting instance for an inflate transform. + public static ID2D1TransformMapperFactory Inflate(int amount) + { + return new D2D1InflateTransformMapperFactory { ParametersAccessor = new D2D1InflateTransformMapperFactory.ConstantAmount { Amount = amount } }; + } + + /// + /// Creates an instance for an inflate transform. + /// + /// The fixed left inflate amount to use. + /// The fixed top inflate amount to use. + /// The fixed right inflate amount to use. + /// The fixed bottom inflate amount to use. + /// The resulting instance for an inflate transform. + public static ID2D1TransformMapperFactory Inflate(int left, int top, int right, int bottom) + { + return new D2D1InflateTransformMapperFactory { ParametersAccessor = new D2D1InflateTransformMapperFactory.ConstantLeftTopRightBottomAmount { Left = left, Top = top, Right = right, Bottom = bottom } }; + } + + /// + /// Creates an instance for an inflate transform. + /// + /// The input instance to retrieve the inflate amount to use (applies to all edges of the input area). + /// The resulting instance for an inflate transform. + public static ID2D1TransformMapperFactory Inflate(Accessor accessor) + { + if (accessor is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(accessor), "The input D2D1TransformMapperFactory.Accessor object cannot be null."); + } + + return new D2D1InflateTransformMapperFactory { ParametersAccessor = new D2D1InflateTransformMapperFactory.DynamicAmount { Accessor = accessor } }; + } + + /// + /// Creates an instance for an inflate transform. + /// + /// The input instance to retrieve the inflate amount to use. + /// The resulting instance for an inflate transform. + public static ID2D1TransformMapperFactory Inflate(Accessor<(int Left, int Top, int Right, int Bottom)> accessor) + { + if (accessor is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(accessor), "The input D2D1TransformMapperFactory.Accessor<(int Left, int Top, int Right, int Bottom)> object cannot be null."); + } + + return new D2D1InflateTransformMapperFactory { ParametersAccessor = 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.For { ParametersAccessor = new D2D1AffineTransformMapperFactory.For.ConstantMatrix { 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) + { + if (accessor is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(accessor), "The input D2D1TransformMapperFactory.Accessor object cannot be null."); + } + + return new D2D1AffineTransformMapperFactory.For { ParametersAccessor = new D2D1AffineTransformMapperFactory.For.DynamicMatrix { Accessor = accessor } }; + } + + /// + /// An accessor of parameters to be used in D2D1 transform mapping implementations. + /// + /// The type of parameters that the transform mapper will use. + /// 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 delegate TResult Accessor(in T shader); +} diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs index 787411ef..e23c9b8b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/D2D1TransformMapper.cs @@ -61,14 +61,23 @@ internal abstract class D2D1TransformMapper /// /// The instance to usse, if there is one. /// A new instance wrapping , or . - public static For? Get(ID2D1TransformMapper? transformMapper) + public static D2D1TransformMapper? Get(ID2D1TransformMapper? transformMapper) { if (transformMapper is null) { return null; } - return new(transformMapper); + // If one of the built-in transform mapper factories from D2D1TransformMapperFactory was used, + // the returned ID2D1TransformMapper instance will already inherit from D2D1TransformMapper. + // If that's the case, that object can be returned directly, saving one allocation and avoiding + // an extra interface stub dispatch call every time one of the transform mapping APIs is called. + if (transformMapper is D2D1TransformMapper d2D1TransformMapper) + { + return d2D1TransformMapper; + } + + return new For(transformMapper); } /// @@ -93,4 +102,32 @@ internal abstract class D2D1TransformMapper this.transformMapper.MapInvalidOutput(inputIndex, invalidInput, out invalidOutput); } } + + /// + /// A generic implementation for a mapper with a parameterless constructor. + /// + /// The type of D2D1 pixel shader to register. + /// The type of implementation to register. + public sealed class FactoryOf : ID2D1TransformMapperFactory + where T : unmanaged, ID2D1PixelShader + where TMapper : class, ID2D1TransformMapper, new() + { + /// + /// Creates a new instance. + /// + private FactoryOf() + { + } + + /// + /// Gets the shared instance for a given transform mapper. + /// + public static FactoryOf Instance { get; } = new(); + + /// + public ID2D1TransformMapper Create() + { + return new TMapper(); + } + } } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs index 7a694f5d..d8639943 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs @@ -107,14 +107,14 @@ internal unsafe partial struct PixelShaderEffect /// /// The factory of instances to use for each created effect. /// - private static Func>? d2D1DrawTransformMapperFactory; + private static ID2D1TransformMapperFactory? d2D1DrawTransformMapperFactory; /// /// Initializes the shared state. /// /// The factory of instances to use for each created effect. /// Thrown if initialization is attempted with a mismatched transform factory. - public static void Initialize(Func>? d2D1DrawTransformMapperFactory) + public static void Initialize(ID2D1TransformMapperFactory? d2D1DrawTransformMapperFactory) { // This conceptually acts as a static constructor, and this type is // internal, so in this very specific case locking on the type is fine. @@ -253,7 +253,7 @@ internal unsafe partial struct PixelShaderEffect // and managed exceptions should never cross the ABI boundary. If it throws, just return the HRESULT. try { - ID2D1TransformMapper? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Invoke(); + ID2D1TransformMapper? d2D1DrawTransformMapper = d2D1DrawTransformMapperFactory?.Create(); d2D1TransformMapper = D2D1TransformMapper.For.Get(d2D1DrawTransformMapper); } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperFactory{T, TParameters, TTransformMapper}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperFactory{T, TParameters, TTransformMapper}.cs new file mode 100644 index 00000000..ec21e16b --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapperFactory{T, TParameters, TTransformMapper}.cs @@ -0,0 +1,24 @@ +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 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 TParameters : unmanaged + where TTransformMapper : D2D1TransformMapper, new() +{ + /// + ID2D1TransformMapper ID2D1TransformMapperFactory.Create() + { + return new TTransformMapper { ParametersAccessor = ParametersAccessor }; + } + + /// + /// The instance to use to retrieve parameters for the transform mapper. + /// + public D2D1TransformMapperParametersAccessor? ParametersAccessor { 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..903c0a0d --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/Abstract/D2D1TransformMapper{T, TParameters}.cs @@ -0,0 +1,99 @@ +using System; +using System.Drawing; +using System.Runtime.CompilerServices; +using ComputeSharp.D2D1.Interop.Effects; +using ComputeSharp.D2D1.Shaders.Interop.Helpers; + +#pragma warning disable CS0618 + +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 : D2D1TransformMapper, ID2D1TransformMapper + where T : unmanaged, ID2D1PixelShader + where TParameters : unmanaged +{ + /// + /// The parameters to use in this transform mapper. + /// + private TParameters parameters; + + /// + public D2D1TransformMapperParametersAccessor? ParametersAccessor { 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(in 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(in TParameters parameters, ref Rectangle64 rectangle); + + /// + public override void MapInputsToOutput(ReadOnlySpan buffer, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) + { + Unsafe.SkipInit(out T shader); + + shader.InitializeFromDispatchData(buffer); + + MapInputsToOutput(in shader, inputs, opaqueInputs, out output, out opaqueOutput); + } + + /// + public override void MapOutputToInputs(in Rectangle output, Span inputs) + { + Rectangle64 output64 = Rectangle64.FromRectangle(output); + + TransformOutputToInput(in this.parameters, ref output64); + + inputs.Fill(output64.ToRectangleWithD2D1LogicallyInfiniteClamping()); + } + + /// + public override void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) + { + Rectangle64 output64 = Rectangle64.FromRectangle(invalidInput); + + TransformInputToOutput(in this.parameters, ref output64); + + invalidOutput = output64.ToRectangleWithD2D1LogicallyInfiniteClamping(); + } + + /// + public void MapInputsToOutput(in T shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) + { + this.parameters = ParametersAccessor!.Get(in 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(in this.parameters, ref output64); + + output = output64.ToRectangleWithD2D1LogicallyInfiniteClamping(); + opaqueOutput = Rectangle.Empty; + } + } +} \ No newline at end of file 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..9b9f662c --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs @@ -0,0 +1,146 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract; +using ComputeSharp.D2D1.Shaders.Interop.Helpers; + +namespace ComputeSharp.D2D1.Shaders.Interop.Factories; + +/// +/// A container for shader-specific affine transform mapper factory. +/// +/// +/// This non-generic type is used so that and are not recompiled for each generic type instantiation. +/// +internal sealed class D2D1AffineTransformMapperFactory +{ + /// + /// 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; + } + + /// + /// A custom implementation for an affine transform. + /// + /// The type of D2D1 pixel shader associated to the transform mapper. + public sealed class For : D2D1TransformMapperFactory.TransformMapper> + where T : unmanaged, ID2D1PixelShader + { + /// + /// A implementation for a constant affine transform matrix. + /// + public sealed class ConstantMatrix : 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(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)) + { + 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(in matrix, ref rectangle); + } + } + } +} \ 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 new file mode 100644 index 00000000..819390d9 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1InflateTransformMapperFactory.cs @@ -0,0 +1,116 @@ +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract; +using ComputeSharp.D2D1.Shaders.Interop.Helpers; + +namespace ComputeSharp.D2D1.Shaders.Interop.Factories; + +/// +/// A custom implementation for an inflate transform. +/// +/// The type of D2D1 pixel shader associated to the transform mapper. +internal sealed class D2D1InflateTransformMapperFactory : D2D1TransformMapperFactory.TransformMapper> + where T : unmanaged, ID2D1PixelShader +{ + /// + /// A implementation for a constant inflate amount. + /// + public sealed class ConstantAmount : D2D1TransformMapperParametersAccessor + { + /// + /// Gets the fixed inflate amount. + /// + public int Amount { get; init; } + + /// + public override (int, int, int, int) Get(in T shader) + { + return (Amount, Amount, Amount, Amount); + } + } + + /// + /// A implementation for a constant inflate LTRB amount. + /// + public sealed class ConstantLeftTopRightBottomAmount : D2D1TransformMapperParametersAccessor + { + /// + /// The constant left inflate amount. + /// + public int Left { get; init; } + + /// + /// The constant top inflate amount. + /// + public int Top { get; init; } + + /// + /// The constant right inflate amount. + /// + public int Right { get; init; } + + /// + /// The constant bottom inflate amount. + /// + public int Bottom { get; init; } + + /// + public override (int, int, int, int) Get(in T shader) + { + return (Left, Top, Right, Bottom); + } + } + + /// + /// A implementation for a dynamic inflate amount. + /// + public sealed class DynamicAmount : D2D1TransformMapperParametersAccessor + { + /// + /// Gets the instance to get the dynamic inflate amount. + /// + public D2D1TransformMapperFactory.Accessor? Accessor { get; init; } + + /// + public override (int, int, int, int) Get(in T shader) + { + int amount = Accessor!(in shader); + + return (amount, amount, amount, amount); + } + } + + /// + /// A implementation for a dynamic inflate LTRB amount. + /// + 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; } + + /// + public override (int, int, int, int) Get(in T shader) + { + return Accessor!(in shader); + } + } + + /// + /// A custom implementation for an inflate transform. + /// + public sealed class TransformMapper : D2D1TransformMapper + { + /// + protected override void TransformInputToOutput(in (int Left, int Top, int Right, int Bottom) parameters, ref Rectangle64 rectangle) + { + rectangle.Inflate(parameters.Left, parameters.Top, parameters.Right, parameters.Bottom); + } + + /// + protected override void TransformOutputToInput(in (int Left, int Top, int Right, int Bottom) parameters, ref Rectangle64 rectangle) + { + rectangle.Inflate(parameters.Left, parameters.Top, parameters.Right, parameters.Bottom); + } + } +} \ No newline at end of file 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..6bd7dec5 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs @@ -0,0 +1,83 @@ +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 +{ + /// + /// 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. + /// The transformed points, with no rounding. + public (double X, double Y) 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] + return ( + Math.Round((double)this.x * matrix.M11 + (double)this.y * matrix.M21 + matrix.M31), + Math.Round((double)this.x * matrix.M12 + (double)this.y * matrix.M22 + matrix.M32)); + } +} \ 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..7a87df79 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Rectangle64.cs @@ -0,0 +1,162 @@ +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 +{ + /// + /// 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 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 left = Math.Max(this.x, rectangle.x); + long top = Math.Max(this.y, rectangle.y); + long right = Math.Min(this.Right, rectangle.Right); + long bottom = Math.Min(this.Bottom, rectangle.Bottom); + + this.x = left; + this.y = top; + this.width = Math.Max(right - left, 0); + this.height = Math.Max(bottom - top, 0); + } + + /// + /// 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); + } + + /// + /// 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 diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/Landscape_Pixelate.png b/tests/ComputeSharp.D2D1.Tests/Assets/Landscape_Pixelate.png index ef3e318b..8048c32c 100644 Binary files a/tests/ComputeSharp.D2D1.Tests/Assets/Landscape_Pixelate.png and b/tests/ComputeSharp.D2D1.Tests/Assets/Landscape_Pixelate.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 9238932b..63443f23 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Drawing; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; @@ -24,14 +25,37 @@ public partial class D2D1PixelShaderEffectTests [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] public unsafe void RegisterForD2D1Factory1_WithTransformMapper_NullD2D1Factory1() { - D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, out _); + } + + public sealed partial class DummyTransformMapper : ID2D1TransformMapper + { + public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) + { + output = default; + opaqueOutput = default; + } + + public void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) + { + invalidOutput = default; + } + + public void MapOutputToInputs(in Rectangle output, Span inputs) + { + } + + public partial struct Shader : ID2D1PixelShader + { + public float4 Execute() => default; + } } [TestMethod] [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] public unsafe void RegisterForD2D1Factory1_WithTransformMapperFactory_NullD2D1Factory1() { - D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, () => new PixelateEffect(), out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, null, out _); } [TestMethod] diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 27fea62a..ba821605 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -15,7 +15,7 @@ public partial class D2D1PixelShaderTests public unsafe void GetInputCount() { Assert.AreEqual(D2D1PixelShader.GetInputCount(), 1); - Assert.AreEqual(D2D1PixelShader.GetInputCount(), 1); + Assert.AreEqual(D2D1PixelShader.GetInputCount(), 1); Assert.AreEqual(D2D1PixelShader.GetInputCount(), 0); Assert.AreEqual(D2D1PixelShader.GetInputCount(), 7); } @@ -109,7 +109,7 @@ public partial class D2D1PixelShaderTests Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); @@ -175,7 +175,7 @@ public partial class D2D1PixelShaderTests Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index bcecb144..c19876d7 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -303,8 +303,8 @@ public sealed partial class BokehBlurEffect d2D1DeviceContext.Get()->SetRenderingControls(&d2D1RenderingControls); // Register all necessary effects - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), static () => new VerticalConvolution(), out _); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), static () => new HorizontalConvolutionAndAccumulatePartials(), out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), VerticalConvolution.Transform, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), HorizontalConvolutionAndAccumulatePartials.Transform, out _); D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); @@ -477,35 +477,12 @@ public sealed partial class BokehBlurEffect /// /// The vertical convolution shader and transform. /// - private sealed partial class VerticalConvolution : ID2D1TransformMapper + private sealed partial class VerticalConvolution { /// - /// The length of the convolution kernel. + /// The for the shader. /// - private int kernelLength; - - /// - public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) - { - this.kernelLength = shader.kernelLength; - - output = inputs[0]; - opaqueOutput = Rectangle.Empty; - } - - /// - public void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) - { - invalidOutput = invalidInput; - } - - /// - public void MapOutputToInputs(in Rectangle output, Span inputs) - { - Rectangle input = Rectangle.Inflate(output, 0, this.kernelLength); - - inputs.Fill(input); - } + public static ID2D1TransformMapperFactory Transform { get; } = D2D1TransformMapperFactory.Inflate(static (in Shader shader) => (0, shader.kernelLength, 0, shader.kernelLength)); /// /// Kernel for the vertical convolution pass for real or imaginary components. @@ -547,35 +524,12 @@ public sealed partial class BokehBlurEffect /// /// The horizontal convolutin and partial accumulation effect and transform. /// - private sealed partial class HorizontalConvolutionAndAccumulatePartials : ID2D1TransformMapper + private sealed partial class HorizontalConvolutionAndAccumulatePartials { /// - /// The length of the convolution kernel. + /// The for the shader. /// - private int kernelLength; - - /// - public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) - { - this.kernelLength = shader.kernelLength; - - output = inputs[0]; - opaqueOutput = Rectangle.Empty; - } - - /// - public void MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) - { - invalidOutput = invalidInput; - } - - /// - public void MapOutputToInputs(in Rectangle output, Span inputs) - { - Rectangle input = Rectangle.Inflate(output, this.kernelLength, 0); - - inputs.Fill(input); - } + public static ID2D1TransformMapperFactory Transform { get; } = D2D1TransformMapperFactory.Inflate(static (in Shader shader) => (shader.kernelLength, 0, shader.kernelLength, 0)); /// /// Kernel for the horizontal convolution pass. diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs index 84aa3544..bf330069 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/PixelateEffect.cs @@ -1,69 +1,44 @@ -using System; -using System.Drawing; +namespace ComputeSharp.D2D1.Tests.Effects; -namespace ComputeSharp.D2D1.Tests.Effects; - -public sealed partial class PixelateEffect : ID2D1TransformMapper +[D2DInputCount(1)] +[D2DInputComplex(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader40)] +[AutoConstructor] +public partial struct PixelateEffect : ID2D1PixelShader { - /// - void ID2D1TransformMapper.MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) - { - output = inputs[0]; - opaqueOutput = Rectangle.Empty; - } - - /// - void ID2D1TransformMapper.MapInvalidOutput(int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput) - { - invalidOutput = invalidInput; - } - - /// - void ID2D1TransformMapper.MapOutputToInputs(in Rectangle output, Span inputs) - { - inputs.Fill(output); - } - - [D2DInputCount(1)] - [D2DInputComplex(0)] - [D2DRequiresScenePosition] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader40)] [AutoConstructor] - public partial struct Shader : ID2D1PixelShader + public partial struct Constants { - [AutoConstructor] - public partial struct Constants - { - public readonly int inputWidth; - public readonly int inputHeight; - public readonly int cellSize; - } + public readonly int inputWidth; + public readonly int inputHeight; + public readonly int cellSize; + } - private readonly Constants constants; + private readonly Constants constants; - public float4 Execute() - { - float2 scenePos = D2D.GetScenePosition().XY; - uint x = (uint)Hlsl.Floor(scenePos.X); - uint y = (uint)Hlsl.Floor(scenePos.Y); + public float4 Execute() + { + float2 scenePos = D2D.GetScenePosition().XY; + uint x = (uint)Hlsl.Floor(scenePos.X); + uint y = (uint)Hlsl.Floor(scenePos.Y); - int cellX = (int)Hlsl.Floor(x / this.constants.cellSize); - int cellY = (int)Hlsl.Floor(y / this.constants.cellSize); + int cellX = (int)Hlsl.Floor(x / this.constants.cellSize); + int cellY = (int)Hlsl.Floor(y / this.constants.cellSize); - int x0 = cellX * this.constants.cellSize; - int y0 = cellY * this.constants.cellSize; + int x0 = cellX * this.constants.cellSize; + int y0 = cellY * this.constants.cellSize; - int x1 = Hlsl.Min(this.constants.inputWidth, x0 + this.constants.cellSize) - 1; - int y1 = Hlsl.Min(this.constants.inputHeight, y0 + this.constants.cellSize) - 1; + int x1 = Hlsl.Min(this.constants.inputWidth, x0 + this.constants.cellSize) - 1; + int y1 = Hlsl.Min(this.constants.inputHeight, y0 + this.constants.cellSize) - 1; - float4 sample0 = D2D.SampleInputAtPosition(0, new int2(x0, y0)); - float4 sample1 = D2D.SampleInputAtPosition(0, new int2(x1, y0)); - float4 sample2 = D2D.SampleInputAtPosition(0, new int2(x0, y1)); - float4 sample3 = D2D.SampleInputAtPosition(0, new int2(x1, y1)); + float4 sample0 = D2D.SampleInputAtPosition(0, new float2(x0 + 0.5f, y0 + 0.5f)); + float4 sample1 = D2D.SampleInputAtPosition(0, new float2(x1 + 0.5f, y0 + 0.5f)); + float4 sample2 = D2D.SampleInputAtPosition(0, new float2(x0 + 0.5f, y1 + 0.5f)); + float4 sample3 = D2D.SampleInputAtPosition(0, new float2(x1 + 0.5f, y1 + 0.5f)); - float4 color = (sample0 + sample1 + sample2 + sample3) / 4; + float4 color = (sample0 + sample1 + sample2 + sample3) / 4; - return color; - } + return color; } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs index b6bcd911..72dc3dbf 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs @@ -1,6 +1,7 @@ using System.IO; using System.Reflection; using ComputeSharp.BokehBlur.Processors; +using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; using ComputeSharp.Tests.Helpers; @@ -37,8 +38,8 @@ public class EndToEndTests public unsafe void Pixelate() { D2D1TestRunner.RunAndCompareShader( - new PixelateEffect.Shader(new PixelateEffect.Shader.Constants(1280, 840, 16)), - static () => new PixelateEffect(), + new PixelateEffect(new PixelateEffect.Constants(1280, 840, 16)), + D2D1TransformMapperFactory.Inflate(16), "Landscape.png", "Landscape_Pixelate.png"); } @@ -46,7 +47,7 @@ public class EndToEndTests [TestMethod] public unsafe void ZonePlate() { - D2D1TestRunner.RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), null, 1280, 720, "ZonePlate.png"); + D2D1TestRunner.RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), 1280, 720, "ZonePlate.png"); } [TestMethod] diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index dfe1d5e9..f18b683a 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -25,7 +25,7 @@ internal static class D2D1TestRunner /// The shader to run. public static void RunAndCompareShader( in T shader, - Func>? transformMapperFactory, + ID2D1TransformMapperFactory? transformMapperFactory, string originalFileName, string expectedFileName, [CallerMemberName] string destinationFileName = "") @@ -55,7 +55,6 @@ internal static class D2D1TestRunner /// Executes a pixel shader and compares the expected results. /// /// The type of pixel shader to run. - /// A custom factory for the effect. /// The resulting width. /// The resulting height. /// The name of the expected result image. @@ -63,7 +62,6 @@ internal static class D2D1TestRunner /// The shader to run. public static void RunAndCompareShader( in T shader, - Func>? transformMapperFactory, int width, int height, string expectedFileName, @@ -81,7 +79,7 @@ internal static class D2D1TestRunner // Run the shader ExecutePixelShaderAndSaveToFile( in shader, - transformMapperFactory, + null, width, height, destinationPath); @@ -98,9 +96,9 @@ internal static class D2D1TestRunner /// A custom factory for the effect. /// The source path for the image to run the shader on. /// The destination path for the result. - public static unsafe void ExecutePixelShaderAndSaveToFile( + private static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, - Func>? transformMapperFactory, + ID2D1TransformMapperFactory? transformMapperFactory, string sourcePath, string destinationPath) where T : unmanaged, ID2D1PixelShader @@ -139,9 +137,9 @@ internal static class D2D1TestRunner /// The resulting height. /// A custom factory for the effect. /// The destination path for the result. - public static unsafe void ExecutePixelShaderAndSaveToFile( + private static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, - Func>? transformMapperFactory, + ID2D1TransformMapperFactory? transformMapperFactory, int width, int height, string destinationPath)