From 5dd39fb9cc6af05ebb67738ab739e4e5f29766b5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 19 Aug 2022 12:40:56 +0200 Subject: [PATCH] Fix D2D1 bokeh blur effect transforms --- .../Effects/BokehBlurEffect.cs | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index 2de73bc0..c2df970b 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -384,9 +384,9 @@ public sealed partial class BokehBlurEffect // Set the constant buffers for (int i = 0; i < numberOfComponents; i++) { - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new VerticalConvolution.Shader((int)width, (int)height), verticalConvolutionEffectsForReals[i].Get()); - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new VerticalConvolution.Shader((int)width, (int)height), verticalConvolutionEffectsForImaginaries[i].Get()); - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new HorizontalConvolutionAndAccumulatePartials.Shader((int)width, (int)height, KernelParameters[i].Z, KernelParameters[i].W), horizontalConvolutionEffects[i].Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new VerticalConvolution.Shader((int)kernelSize), verticalConvolutionEffectsForReals[i].Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new VerticalConvolution.Shader((int)kernelSize), verticalConvolutionEffectsForImaginaries[i].Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new HorizontalConvolutionAndAccumulatePartials.Shader((int)kernelSize, KernelParameters[i].Z, KernelParameters[i].W), horizontalConvolutionEffects[i].Get()); } // Build the effect graph: @@ -454,10 +454,17 @@ public sealed partial class BokehBlurEffect /// private sealed partial class VerticalConvolution : ID2D1TransformMapper { + /// + /// The length of the convolution kernel. + /// + private int kernelLength; + /// public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) { - output = Rectangle.Inflate(inputs[0], 0, 100); + this.kernelLength = shader.kernelLength; + + output = inputs[0]; opaqueOutput = Rectangle.Empty; } @@ -470,7 +477,9 @@ public sealed partial class BokehBlurEffect /// public void MapOutputToInputs(in Rectangle output, Span inputs) { - inputs.Fill(output); + Rectangle input = Rectangle.Inflate(output, 0, this.kernelLength); + + inputs.Fill(input); } /// @@ -485,8 +494,7 @@ public sealed partial class BokehBlurEffect [AutoConstructor] public partial struct Shader : ID2D1PixelShader { - private readonly int width; - private readonly int height; + public readonly int kernelLength; [D2DResourceTextureIndex(1)] private readonly D2D1ResourceTexture1D kernel; @@ -494,19 +502,13 @@ public sealed partial class BokehBlurEffect /// public float4 Execute() { - int2 position = (int2)D2D.GetScenePosition().XY; float4 result = float4.Zero; - int maxY = this.height; - int maxX = this.width; - int kernelLength = this.kernel.Width; - int radiusY = kernelLength >> 1; - int offsetX = Hlsl.Clamp(position.X, 0, maxX); + int length = this.kernelLength; + int radiusY = length >> 1; - for (int i = 0; i < kernelLength; i++) + for (int i = 0; i < length; i++) { - int offsetY = Hlsl.Clamp(position.Y + i - radiusY, 0, maxY); - float2 uv = new((float)offsetX / maxX, (float)offsetY / maxY); - float4 color = D2D.SampleInput(0, uv); + float4 color = D2D.SampleInputAtOffset(0, new float2(0, i - radiusY)); float factor = this.kernel[i]; result += factor * color; @@ -522,10 +524,17 @@ public sealed partial class BokehBlurEffect /// private sealed partial class HorizontalConvolutionAndAccumulatePartials : ID2D1TransformMapper { + /// + /// The length of the convolution kernel. + /// + private int kernelLength; + /// public void MapInputsToOutput(in Shader shader, ReadOnlySpan inputs, ReadOnlySpan opaqueInputs, out Rectangle output, out Rectangle opaqueOutput) { - output = Rectangle.Inflate(inputs[0], 100, 0); + this.kernelLength = shader.kernelLength; + + output = inputs[0]; opaqueOutput = Rectangle.Empty; } @@ -538,7 +547,9 @@ public sealed partial class BokehBlurEffect /// public void MapOutputToInputs(in Rectangle output, Span inputs) { - inputs.Fill(output); + Rectangle input = Rectangle.Inflate(output, this.kernelLength, 0); + + inputs.Fill(input); } /// @@ -555,8 +566,7 @@ public sealed partial class BokehBlurEffect [AutoConstructor] public partial struct Shader : ID2D1PixelShader { - private readonly int width; - private readonly int height; + public readonly int kernelLength; private readonly float z; private readonly float w; @@ -569,21 +579,15 @@ public sealed partial class BokehBlurEffect /// public float4 Execute() { - int2 position = (int2)D2D.GetScenePosition().XY; float4 real = float4.Zero; float4 imaginary = float4.Zero; - int maxY = this.width; - int maxX = this.width; - int kernelLength = this.kernelReals.Width; - int radiusX = kernelLength >> 1; - int offsetY = Hlsl.Clamp(position.Y, 0, maxY); + int length = this.kernelLength; + int radiusX = length >> 1; - for (int i = 0; i < kernelLength; i++) + for (int i = 0; i < length; i++) { - int offsetX = Hlsl.Clamp(position.X + i - radiusX, 0, maxX); - float2 uv = new((float)offsetX / maxX, (float)offsetY / maxY); - float4 sourceReal = D2D.SampleInput(0, uv); - float4 sourceImaginary = D2D.SampleInput(1, uv); + float4 sourceReal = D2D.SampleInputAtOffset(0, new float2(i - radiusX, 0)); + float4 sourceImaginary = D2D.SampleInputAtOffset(1, new float2(i - radiusX, 0)); float realFactor = kernelReals[i]; float imaginaryFactor = kernelImaginaries[i];