Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resize pad color application for non Nearest Neighbor samplers #2052

Merged
merged 8 commits into from
Mar 15, 2022

Conversation

JimBobSquarePants
Copy link
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

The new ResizeOptions.PadColor property fill operation was being overwritten by the ResizeWorker. This changes fixes the worker to only allocate buffers matching the target rectangle of interest writing them to the correct target span.

@JimBobSquarePants JimBobSquarePants requested a review from a team March 9, 2022 15:43
@@ -101,7 +101,7 @@ protected virtual void Dispose(bool disposing)
/// Returns a <see cref="ResizeKernel"/> for an index value between 0 and DestinationSize - 1.
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
internal ref ResizeKernel GetKernel(int destIdx) => ref this.kernels[destIdx];
internal ref ResizeKernel GetKernel(nint destIdx) => ref this.kernels[destIdx];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the plain array access nint doesn't bring any advantage.
Here the JIT misses a optimization (a bug is tracked, but can't find it at the moment).

If you want best machine code here, look at Sharplab method GetKernelC.
It's more C# code and I doubt it's needed here. I'd just go with int (or nint as you have it now) and wait for the JIT fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No MemoryMarshal.GetArrayDataReference for us yet unfortunately..... One day!

ref Vector4 fpBase = ref transposedFirstPassBufferSpan[top];

for (int x = 0; x < this.destWidth; x++)
for (nint x = left; x < right; x++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's good to have nint, as in the Unsafe.Adds below this removes the sign extending move movsxd which is beneficial.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to have the loop as

Suggested change
for (nint x = left; x < right; x++)
for (nint x = 0; x < (right - left); x++)

so that the subtractions below x - left can be eliminated (shifted to the loop variable), so that it's just x.
I assume current JIT won't (completely) CSE that expression.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot. Not sure how I missed that.

@@ -184,13 +186,13 @@ private void CalculateFirstPassValues(RowInterval calculationInterval)
// Span<Vector4> firstPassSpan = transposedFirstPassBufferSpan.Slice(y - this.currentWindow.Min);
ref Vector4 firstPassBaseRef = ref transposedFirstPassBufferSpan[y - this.currentWindow.Min];

for (int x = this.targetWorkingRect.Left; x < this.targetWorkingRect.Right; x++)
for (nint x = left; x < right; x++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

Suggested change
for (nint x = left; x < right; x++)
for (nint x = 0; x < (right - left); x++)

?
And adjust below to just x instead of x - left.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I have to add an extra incremental here. I need both x & x - left.

@JimBobSquarePants JimBobSquarePants merged commit 7bd0e03 into main Mar 15, 2022
@JimBobSquarePants JimBobSquarePants deleted the js/fix-resize-pad-color branch March 15, 2022 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants