-
-
Notifications
You must be signed in to change notification settings - Fork 889
Add AVX version of CollectColorBlueTransforms and CollectColorRedTransforms #1824
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a2c0900
Avx version of CollectColorBlueTransforms
brianpopow c15e62c
Avoid pinning
brianpopow 8806d6b
Add Avx version of CollectColorRedTransforms
brianpopow 0e3eda9
Add tests with and without avx
brianpopow 03c2c22
Fix shuffle high mask
brianpopow 37189bf
Merge branch 'master' into bp/ColorBlueTransformAvx
brianpopow 55f04f6
Update PredictorEncoder.cs
JimBobSquarePants f585870
Merge branch 'master' into bp/ColorBlueTransformAvx
JimBobSquarePants 345e7c6
Move color space transform methods into own class
brianpopow 4d5af9c
Additional tests for color transforms
brianpopow 7959d0b
Apply suggestions from code review
brianpopow c491cbb
Use nint for inner loop x variable
brianpopow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
268 changes: 268 additions & 0 deletions
268
src/ImageSharp/Formats/Webp/Lossless/ColorSpaceTransformUtils.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| #if SUPPORTS_RUNTIME_INTRINSICS | ||
| using System.Runtime.Intrinsics; | ||
| using System.Runtime.Intrinsics.X86; | ||
| #endif | ||
|
|
||
| namespace SixLabors.ImageSharp.Formats.Webp.Lossless | ||
| { | ||
| internal static class ColorSpaceTransformUtils | ||
| { | ||
| #if SUPPORTS_RUNTIME_INTRINSICS | ||
| private static readonly Vector128<byte> CollectColorRedTransformsGreenMask = Vector128.Create(0x00ff00).AsByte(); | ||
|
|
||
| private static readonly Vector128<byte> CollectColorRedTransformsAndMask = Vector128.Create((short)0xff).AsByte(); | ||
|
|
||
| private static readonly Vector256<byte> CollectColorRedTransformsGreenMask256 = Vector256.Create(0x00ff00).AsByte(); | ||
|
|
||
| private static readonly Vector256<byte> CollectColorRedTransformsAndMask256 = Vector256.Create((short)0xff).AsByte(); | ||
|
|
||
| private static readonly Vector128<byte> CollectColorBlueTransformsGreenMask = Vector128.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255); | ||
|
|
||
| private static readonly Vector128<byte> CollectColorBlueTransformsGreenBlueMask = Vector128.Create(255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0); | ||
|
|
||
| private static readonly Vector128<byte> CollectColorBlueTransformsBlueMask = Vector128.Create(255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0); | ||
|
|
||
| private static readonly Vector128<byte> CollectColorBlueTransformsShuffleLowMask = Vector128.Create(255, 2, 255, 6, 255, 10, 255, 14, 255, 255, 255, 255, 255, 255, 255, 255); | ||
|
|
||
| private static readonly Vector128<byte> CollectColorBlueTransformsShuffleHighMask = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 2, 255, 6, 255, 10, 255, 14); | ||
|
|
||
| private static readonly Vector256<byte> CollectColorBlueTransformsShuffleLowMask256 = Vector256.Create(255, 2, 255, 6, 255, 10, 255, 14, 255, 255, 255, 255, 255, 255, 255, 255, 255, 18, 255, 22, 255, 26, 255, 30, 255, 255, 255, 255, 255, 255, 255, 255); | ||
|
|
||
| private static readonly Vector256<byte> CollectColorBlueTransformsShuffleHighMask256 = Vector256.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 2, 255, 6, 255, 10, 255, 14, 255, 255, 255, 255, 255, 255, 255, 255, 255, 18, 255, 22, 255, 26, 255, 30); | ||
|
|
||
| private static readonly Vector256<byte> CollectColorBlueTransformsGreenBlueMask256 = Vector256.Create(255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0); | ||
|
|
||
| private static readonly Vector256<byte> CollectColorBlueTransformsBlueMask256 = Vector256.Create(255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0); | ||
|
|
||
| private static readonly Vector256<byte> CollectColorBlueTransformsGreenMask256 = Vector256.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255); | ||
| #endif | ||
|
|
||
| public static void CollectColorBlueTransforms(Span<uint> bgra, int stride, int tileWidth, int tileHeight, int greenToBlue, int redToBlue, Span<int> histo) | ||
| { | ||
| #if SUPPORTS_RUNTIME_INTRINSICS | ||
| if (Avx2.IsSupported && tileWidth >= 16) | ||
| { | ||
| const int span = 16; | ||
| Span<ushort> values = stackalloc ushort[span]; | ||
| var multsr = Vector256.Create(LosslessUtils.Cst5b(redToBlue)); | ||
| var multsg = Vector256.Create(LosslessUtils.Cst5b(greenToBlue)); | ||
| for (int y = 0; y < tileHeight; y++) | ||
| { | ||
| Span<uint> srcSpan = bgra.Slice(y * stride); | ||
| ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); | ||
| for (nint x = 0; x <= tileWidth - span; x += span) | ||
| { | ||
| nint input0Idx = x; | ||
| nint input1Idx = x + (span / 2); | ||
| Vector256<byte> input0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); | ||
| Vector256<byte> input1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); | ||
| Vector256<byte> r0 = Avx2.Shuffle(input0, CollectColorBlueTransformsShuffleLowMask256); | ||
| Vector256<byte> r1 = Avx2.Shuffle(input1, CollectColorBlueTransformsShuffleHighMask256); | ||
| Vector256<byte> r = Avx2.Or(r0, r1); | ||
| Vector256<byte> gb0 = Avx2.And(input0, CollectColorBlueTransformsGreenBlueMask256); | ||
| Vector256<byte> gb1 = Avx2.And(input1, CollectColorBlueTransformsGreenBlueMask256); | ||
| Vector256<ushort> gb = Avx2.PackUnsignedSaturate(gb0.AsInt32(), gb1.AsInt32()); | ||
| Vector256<byte> g = Avx2.And(gb.AsByte(), CollectColorBlueTransformsGreenMask256); | ||
| Vector256<short> a = Avx2.MultiplyHigh(r.AsInt16(), multsr); | ||
| Vector256<short> b = Avx2.MultiplyHigh(g.AsInt16(), multsg); | ||
| Vector256<byte> c = Avx2.Subtract(gb.AsByte(), b.AsByte()); | ||
| Vector256<byte> d = Avx2.Subtract(c, a.AsByte()); | ||
| Vector256<byte> e = Avx2.And(d, CollectColorBlueTransformsBlueMask256); | ||
|
|
||
| ref ushort outputRef = ref MemoryMarshal.GetReference(values); | ||
| Unsafe.As<ushort, Vector256<ushort>>(ref outputRef) = e.AsUInt16(); | ||
|
|
||
| for (int i = 0; i < span; i++) | ||
| { | ||
| ++histo[values[i]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int leftOver = tileWidth & (span - 1); | ||
| if (leftOver > 0) | ||
| { | ||
| CollectColorBlueTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToBlue, redToBlue, histo); | ||
| } | ||
| } | ||
| else if (Sse41.IsSupported) | ||
| { | ||
| const int span = 8; | ||
| Span<ushort> values = stackalloc ushort[span]; | ||
| var multsr = Vector128.Create(LosslessUtils.Cst5b(redToBlue)); | ||
| var multsg = Vector128.Create(LosslessUtils.Cst5b(greenToBlue)); | ||
| for (int y = 0; y < tileHeight; y++) | ||
| { | ||
| Span<uint> srcSpan = bgra.Slice(y * stride); | ||
| ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); | ||
| for (nint x = 0; x <= tileWidth - span; x += span) | ||
| { | ||
| nint input0Idx = x; | ||
| nint input1Idx = x + (span / 2); | ||
| Vector128<byte> input0 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); | ||
| Vector128<byte> input1 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); | ||
| Vector128<byte> r0 = Ssse3.Shuffle(input0, CollectColorBlueTransformsShuffleLowMask); | ||
| Vector128<byte> r1 = Ssse3.Shuffle(input1, CollectColorBlueTransformsShuffleHighMask); | ||
| Vector128<byte> r = Sse2.Or(r0, r1); | ||
| Vector128<byte> gb0 = Sse2.And(input0, CollectColorBlueTransformsGreenBlueMask); | ||
| Vector128<byte> gb1 = Sse2.And(input1, CollectColorBlueTransformsGreenBlueMask); | ||
| Vector128<ushort> gb = Sse41.PackUnsignedSaturate(gb0.AsInt32(), gb1.AsInt32()); | ||
| Vector128<byte> g = Sse2.And(gb.AsByte(), CollectColorBlueTransformsGreenMask); | ||
| Vector128<short> a = Sse2.MultiplyHigh(r.AsInt16(), multsr); | ||
| Vector128<short> b = Sse2.MultiplyHigh(g.AsInt16(), multsg); | ||
| Vector128<byte> c = Sse2.Subtract(gb.AsByte(), b.AsByte()); | ||
| Vector128<byte> d = Sse2.Subtract(c, a.AsByte()); | ||
| Vector128<byte> e = Sse2.And(d, CollectColorBlueTransformsBlueMask); | ||
|
|
||
| ref ushort outputRef = ref MemoryMarshal.GetReference(values); | ||
| Unsafe.As<ushort, Vector128<ushort>>(ref outputRef) = e.AsUInt16(); | ||
|
|
||
| for (int i = 0; i < span; i++) | ||
| { | ||
| ++histo[values[i]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int leftOver = tileWidth & (span - 1); | ||
| if (leftOver > 0) | ||
| { | ||
| CollectColorBlueTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToBlue, redToBlue, histo); | ||
| } | ||
| } | ||
| else | ||
| #endif | ||
| { | ||
| CollectColorBlueTransformsNoneVectorized(bgra, stride, tileWidth, tileHeight, greenToBlue, redToBlue, histo); | ||
| } | ||
| } | ||
|
|
||
| private static void CollectColorBlueTransformsNoneVectorized(Span<uint> bgra, int stride, int tileWidth, int tileHeight, int greenToBlue, int redToBlue, Span<int> histo) | ||
| { | ||
| int pos = 0; | ||
| while (tileHeight-- > 0) | ||
| { | ||
| for (int x = 0; x < tileWidth; x++) | ||
| { | ||
| int idx = LosslessUtils.TransformColorBlue((sbyte)greenToBlue, (sbyte)redToBlue, bgra[pos + x]); | ||
| ++histo[idx]; | ||
| } | ||
|
|
||
| pos += stride; | ||
| } | ||
| } | ||
|
|
||
| public static void CollectColorRedTransforms(Span<uint> bgra, int stride, int tileWidth, int tileHeight, int greenToRed, Span<int> histo) | ||
| { | ||
| #if SUPPORTS_RUNTIME_INTRINSICS | ||
| if (Avx2.IsSupported && tileWidth >= 16) | ||
| { | ||
| var multsg = Vector256.Create(LosslessUtils.Cst5b(greenToRed)); | ||
| const int span = 16; | ||
| Span<ushort> values = stackalloc ushort[span]; | ||
| for (int y = 0; y < tileHeight; y++) | ||
| { | ||
| Span<uint> srcSpan = bgra.Slice(y * stride); | ||
| ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); | ||
| for (nint x = 0; x <= tileWidth - span; x += span) | ||
| { | ||
| nint input0Idx = x; | ||
| nint input1Idx = x + (span / 2); | ||
| Vector256<byte> input0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); | ||
| Vector256<byte> input1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); | ||
| Vector256<byte> g0 = Avx2.And(input0, CollectColorRedTransformsGreenMask256); // 0 0 | g 0 | ||
| Vector256<byte> g1 = Avx2.And(input1, CollectColorRedTransformsGreenMask256); | ||
| Vector256<ushort> g = Avx2.PackUnsignedSaturate(g0.AsInt32(), g1.AsInt32()); // g 0 | ||
| Vector256<int> a0 = Avx2.ShiftRightLogical(input0.AsInt32(), 16); // 0 0 | x r | ||
| Vector256<int> a1 = Avx2.ShiftRightLogical(input1.AsInt32(), 16); | ||
| Vector256<ushort> a = Avx2.PackUnsignedSaturate(a0, a1); // x r | ||
| Vector256<short> b = Avx2.MultiplyHigh(g.AsInt16(), multsg); // x dr | ||
| Vector256<byte> c = Avx2.Subtract(a.AsByte(), b.AsByte()); // x r' | ||
| Vector256<byte> d = Avx2.And(c, CollectColorRedTransformsAndMask256); // 0 r' | ||
|
|
||
| ref ushort outputRef = ref MemoryMarshal.GetReference(values); | ||
| Unsafe.As<ushort, Vector256<ushort>>(ref outputRef) = d.AsUInt16(); | ||
|
|
||
| for (int i = 0; i < span; i++) | ||
| { | ||
| ++histo[values[i]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int leftOver = tileWidth & (span - 1); | ||
| if (leftOver > 0) | ||
| { | ||
| CollectColorRedTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToRed, histo); | ||
| } | ||
| } | ||
| else if (Sse41.IsSupported) | ||
| { | ||
| var multsg = Vector128.Create(LosslessUtils.Cst5b(greenToRed)); | ||
| const int span = 8; | ||
| Span<ushort> values = stackalloc ushort[span]; | ||
| for (int y = 0; y < tileHeight; y++) | ||
| { | ||
| Span<uint> srcSpan = bgra.Slice(y * stride); | ||
| ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); | ||
| for (nint x = 0; x <= tileWidth - span; x += span) | ||
| { | ||
| nint input0Idx = x; | ||
| nint input1Idx = x + (span / 2); | ||
| Vector128<byte> input0 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); | ||
| Vector128<byte> input1 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); | ||
| Vector128<byte> g0 = Sse2.And(input0, CollectColorRedTransformsGreenMask); // 0 0 | g 0 | ||
| Vector128<byte> g1 = Sse2.And(input1, CollectColorRedTransformsGreenMask); | ||
| Vector128<ushort> g = Sse41.PackUnsignedSaturate(g0.AsInt32(), g1.AsInt32()); // g 0 | ||
| Vector128<int> a0 = Sse2.ShiftRightLogical(input0.AsInt32(), 16); // 0 0 | x r | ||
| Vector128<int> a1 = Sse2.ShiftRightLogical(input1.AsInt32(), 16); | ||
| Vector128<ushort> a = Sse41.PackUnsignedSaturate(a0, a1); // x r | ||
| Vector128<short> b = Sse2.MultiplyHigh(g.AsInt16(), multsg); // x dr | ||
| Vector128<byte> c = Sse2.Subtract(a.AsByte(), b.AsByte()); // x r' | ||
| Vector128<byte> d = Sse2.And(c, CollectColorRedTransformsAndMask); // 0 r' | ||
|
|
||
| ref ushort outputRef = ref MemoryMarshal.GetReference(values); | ||
| Unsafe.As<ushort, Vector128<ushort>>(ref outputRef) = d.AsUInt16(); | ||
|
|
||
| for (int i = 0; i < span; i++) | ||
| { | ||
| ++histo[values[i]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int leftOver = tileWidth & (span - 1); | ||
| if (leftOver > 0) | ||
| { | ||
| CollectColorRedTransformsNoneVectorized(bgra.Slice(tileWidth - leftOver), stride, leftOver, tileHeight, greenToRed, histo); | ||
| } | ||
| } | ||
| else | ||
| #endif | ||
| { | ||
| CollectColorRedTransformsNoneVectorized(bgra, stride, tileWidth, tileHeight, greenToRed, histo); | ||
| } | ||
| } | ||
|
|
||
| private static void CollectColorRedTransformsNoneVectorized(Span<uint> bgra, int stride, int tileWidth, int tileHeight, int greenToRed, Span<int> histo) | ||
| { | ||
| int pos = 0; | ||
| while (tileHeight-- > 0) | ||
| { | ||
| for (int x = 0; x < tileWidth; x++) | ||
| { | ||
| int idx = LosslessUtils.TransformColorRed((sbyte)greenToRed, bgra[pos + x]); | ||
| ++histo[idx]; | ||
| } | ||
|
|
||
| pos += stride; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
NoneVectorized->NonVectorized. Or even better:CollectColorBlueTransformsScalar.Don't bother to raise a separate PR, just include it to some other one if you can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, will do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've already changed it in #1830