Skip to content

Commit cb18c58

Browse files
committed
Switch to vectorized clamping
1 parent e4ba017 commit cb18c58

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,9 @@ public void Invoke(int y, Span<Vector4> span)
333333
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
334334
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, span.Length), span, PixelConversionModifiers.Premultiply);
335335
ref Vector4 baseRef = ref MemoryMarshal.GetReference(span);
336+
int length = this.bounds.Width;
336337

337-
for (int x = 0; x < this.bounds.Width; x++)
338+
for (int x = 0; x < length; x++)
338339
{
339340
ref Vector4 pixel4 = ref Unsafe.Add(ref baseRef, x);
340341
Vector4 v = pixel4;
@@ -433,23 +434,23 @@ public ApplyInverseGamma3ExposureRowOperation(
433434
[MethodImpl(InliningOptions.ShortMethod)]
434435
public unsafe void Invoke(int y)
435436
{
436-
Vector4 low = Vector4.Zero;
437-
var high = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
437+
Span<Vector4> sourceRowSpan = this.sourceValues.GetRowSpan(y).Slice(this.bounds.X, this.bounds.Width);
438+
ref Vector4 sourceRef = ref MemoryMarshal.GetReference(sourceRowSpan);
439+
440+
Numerics.Clamp(MemoryMarshal.Cast<Vector4, float>(sourceRowSpan), 0, float.PositiveInfinity);
438441

439442
Span<TPixel> targetPixelSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
440-
Span<Vector4> sourceRowSpan = this.sourceValues.GetRowSpan(y).Slice(this.bounds.X);
441-
ref Vector4 sourceRef = ref MemoryMarshal.GetReference(sourceRowSpan);
443+
int length = this.bounds.Width;
442444

443-
for (int x = 0; x < this.bounds.Width; x++)
445+
for (int x = 0; x < length; x++)
444446
{
445447
ref Vector4 v = ref Unsafe.Add(ref sourceRef, x);
446-
Vector4 clamp = Numerics.Clamp(v, low, high);
447448

448449
double
449-
x64 = clamp.X,
450-
y64 = clamp.Y,
451-
z64 = clamp.Z;
452-
float a = clamp.W;
450+
x64 = v.X,
451+
y64 = v.Y,
452+
z64 = v.Z;
453+
float a = v.W;
453454

454455
ulong
455456
xl = *(ulong*)&x64,
@@ -472,8 +473,8 @@ public unsafe void Invoke(int y)
472473
y4.Z = (float)*(double*)&zl;
473474
y4.W = 0;
474475

475-
y4 = (2 / 3f * y4) + (1 / 3f * (clamp / (y4 * y4)));
476-
y4 = (2 / 3f * y4) + (1 / 3f * (clamp / (y4 * y4)));
476+
y4 = (2 / 3f * y4) + (1 / 3f * (v / (y4 * y4)));
477+
y4 = (2 / 3f * y4) + (1 / 3f * (v / (y4 * y4)));
477478
y4.W = a;
478479

479480
v = y4;

0 commit comments

Comments
 (0)