|
13 | 13 |
|
14 | 14 | // <auto-generated />
|
15 | 15 | using System.Numerics;
|
| 16 | +using System.Runtime.CompilerServices; |
| 17 | +using System.Runtime.InteropServices; |
| 18 | +using System.Runtime.Intrinsics; |
| 19 | +using System.Runtime.Intrinsics.X86; |
16 | 20 |
|
17 | 21 | namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders;
|
18 | 22 |
|
@@ -86,18 +90,85 @@ var blenders = new []{
|
86 | 90 | protected override void BlendFunction(Span<Vector4> destination, ReadOnlySpan<Vector4> background, ReadOnlySpan<Vector4> source, float amount)
|
87 | 91 | {
|
88 | 92 | amount = Numerics.Clamp(amount, 0, 1);
|
89 |
| - for (int i = 0; i < destination.Length; i++) |
| 93 | + |
| 94 | + if (Avx2.IsSupported && destination.Length >= 2) |
90 | 95 | {
|
91 |
| - destination[i] = PorterDuffFunctions.<#=blender_composer#>(background[i], source[i], amount); |
| 96 | + // Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float> |
| 97 | + ref Vector256<float> destinationBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(destination)); |
| 98 | + ref Vector256<float> destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u)); |
| 99 | + |
| 100 | + ref Vector256<float> backgroundBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(background)); |
| 101 | + ref Vector256<float> sourceBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(source)); |
| 102 | + Vector256<float> opacity = Vector256.Create(amount); |
| 103 | + |
| 104 | + while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast)) |
| 105 | + { |
| 106 | + destinationBase = PorterDuffFunctions.<#=blender_composer#>(backgroundBase, sourceBase, opacity); |
| 107 | + destinationBase = ref Unsafe.Add(ref destinationBase, 1); |
| 108 | + backgroundBase = ref Unsafe.Add(ref backgroundBase, 1); |
| 109 | + sourceBase = ref Unsafe.Add(ref sourceBase, 1); |
| 110 | + } |
| 111 | + |
| 112 | + if (Numerics.Modulo2(destination.Length) != 0) |
| 113 | + { |
| 114 | + // Vector4 fits neatly in pairs. Any overlap has to be equal to 1. |
| 115 | + int i = destination.Length - 1; |
| 116 | + destination[i] = PorterDuffFunctions.<#=blender_composer#>(background[i], source[i], amount); |
| 117 | + } |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + for (int i = 0; i < destination.Length; i++) |
| 122 | + { |
| 123 | + destination[i] = PorterDuffFunctions.<#=blender_composer#>(background[i], source[i], amount); |
| 124 | + } |
92 | 125 | }
|
93 | 126 | }
|
94 | 127 |
|
95 | 128 | /// <inheritdoc />
|
96 | 129 | protected override void BlendFunction(Span<Vector4> destination, ReadOnlySpan<Vector4> background, ReadOnlySpan<Vector4> source, ReadOnlySpan<float> amount)
|
97 | 130 | {
|
98 |
| - for (int i = 0; i < destination.Length; i++) |
| 131 | + if (Avx2.IsSupported && destination.Length >= 2) |
| 132 | + { |
| 133 | + // Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float> |
| 134 | + ref Vector256<float> destinationBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(destination)); |
| 135 | + ref Vector256<float> destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u)); |
| 136 | + |
| 137 | + ref Vector256<float> backgroundBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(background)); |
| 138 | + ref Vector256<float> sourceBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(source)); |
| 139 | + ref float amountBase = ref MemoryMarshal.GetReference(amount); |
| 140 | + |
| 141 | + Vector256<float> vOne = Vector256.Create(1F); |
| 142 | + |
| 143 | + while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast)) |
| 144 | + { |
| 145 | + // We need to create a Vector256<float> containing the current and next amount values |
| 146 | + // taking up each half of the Vector256<float> and then clamp them. |
| 147 | + Vector256<float> opacity = Vector256.Create( |
| 148 | + Vector128.Create(amountBase), |
| 149 | + Vector128.Create(Unsafe.Add(ref amountBase, 1))); |
| 150 | + opacity = Avx.Min(Avx.Max(Vector256<float>.Zero, opacity), vOne); |
| 151 | + |
| 152 | + destinationBase = PorterDuffFunctions.<#=blender_composer#>(backgroundBase, sourceBase, opacity); |
| 153 | + destinationBase = ref Unsafe.Add(ref destinationBase, 1); |
| 154 | + backgroundBase = ref Unsafe.Add(ref backgroundBase, 1); |
| 155 | + sourceBase = ref Unsafe.Add(ref sourceBase, 1); |
| 156 | + amountBase = ref Unsafe.Add(ref amountBase, 2); |
| 157 | + } |
| 158 | + |
| 159 | + if (Numerics.Modulo2(destination.Length) != 0) |
| 160 | + { |
| 161 | + // Vector4 fits neatly in pairs. Any overlap has to be equal to 1. |
| 162 | + int i = destination.Length - 1; |
| 163 | + destination[i] = PorterDuffFunctions.<#=blender_composer#>(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F)); |
| 164 | + } |
| 165 | + } |
| 166 | + else |
99 | 167 | {
|
100 |
| - destination[i] = PorterDuffFunctions.<#=blender_composer#>(background[i], source[i], Numerics.Clamp(amount[i], 0, 1)); |
| 168 | + for (int i = 0; i < destination.Length; i++) |
| 169 | + { |
| 170 | + destination[i] = PorterDuffFunctions.<#=blender_composer#>(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F)); |
| 171 | + } |
101 | 172 | }
|
102 | 173 | }
|
103 | 174 | }
|
|
0 commit comments