diff --git a/SpectralConvolution/SpectralConvolution.Tests/FftShaderMathTests.cs b/SpectralConvolution/SpectralConvolution.Tests/FftShaderMathTests.cs new file mode 100644 index 0000000..63ae6cd --- /dev/null +++ b/SpectralConvolution/SpectralConvolution.Tests/FftShaderMathTests.cs @@ -0,0 +1,85 @@ +using System.Numerics; + +namespace SpectralConvolution.Tests; + +public sealed class FftShaderMathTests +{ + [Theory] + [InlineData(64)] + [InlineData(128)] + [InlineData(256)] + [InlineData(512)] + public void EveryRadix2StageTouchesEachElementOfTwoRowsOnce(int size) + { + var log2Size = BitOperations.Log2((uint)size); + for (var stage = 0; stage < log2Size; stage++) + { + var touched = new int[size * 2]; + for (var index = 0; index < size; index++) + { + var even = FftShaderMath.Radix2Even(index, log2Size, stage); + touched[even]++; + touched[even + FftShaderMath.Pow2(stage)]++; + } + + Assert.All(touched, count => Assert.Equal(1, count)); + } + } + + [Theory] + [InlineData(64)] + [InlineData(128)] + [InlineData(256)] + [InlineData(512)] + public void EveryRadix4StageTouchesEachElementOfFourRowsOnce(int size) + { + var log2Size = BitOperations.Log2((uint)size); + for (var stage = 0; stage + 1 < log2Size; stage++) + { + var step = FftShaderMath.Pow2(stage); + var touched = new int[size * FftShaderMath.Radix4Elements]; + for (var index = 0; index < size; index++) + { + var first = FftShaderMath.Radix4First(index, log2Size, stage); + for (var member = 0; member < FftShaderMath.Radix4Elements; member++) + touched[first + member * step]++; + } + + Assert.All(touched, count => Assert.Equal(1, count)); + } + } + + [Theory] + [InlineData(64)] + [InlineData(128)] + [InlineData(256)] + [InlineData(512)] + public void EveryTwiddleIndexStaysInsideTheTable(int size) + { + var log2Size = BitOperations.Log2((uint)size); + for (var stage = 0; stage < log2Size; stage++) + { + for (var index = 0; index < size; index++) + Assert.InRange(FftShaderMath.TwiddleIndex(FftShaderMath.Position(index, stage), log2Size, stage), 0, size / 2 - 1); + } + + for (var stage = 0; stage + 1 < log2Size; stage++) + { + var step = FftShaderMath.Pow2(stage); + for (var index = 0; index < size; index++) + { + var position = FftShaderMath.Position(index, stage); + Assert.InRange(FftShaderMath.TwiddleIndex(position, log2Size, stage + 1), 0, size / 2 - 1); + Assert.InRange(FftShaderMath.TwiddleIndex(position + step, log2Size, stage + 1), 0, size / 2 - 1); + } + } + } + + [Fact] + public void AGroupHoldsOneRadix4UnitForEveryFourElements() + { + Assert.Equal(4, FftShaderMath.Radix4Elements); + Assert.Equal(TilePlan.GroupThreads / 2, FftShaderMath.StageUnitsPerGroup); + Assert.Equal(TilePlan.GroupThreads, TilePlan.ChromaticGroupElements / FftShaderMath.Radix4Elements); + } +} diff --git a/SpectralConvolution/SpectralConvolution/FftShaderMath.cs b/SpectralConvolution/SpectralConvolution/FftShaderMath.cs new file mode 100644 index 0000000..ed17c04 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution/FftShaderMath.cs @@ -0,0 +1,83 @@ +using ComputeWeave; + +namespace SpectralConvolution; + +internal static class FftShaderMath +{ + public const float Forward = 1f; + public const float Inverse = -1f; + public const int Radix4Elements = 1 << Radix4Bits; + public const int StageUnitsPerGroup = TilePlan.GroupElements / Radix4Elements; + + const int UIntBits = 32; + const int Radix2Bits = 1; + const int Radix4Bits = 2; + + public static int Pow2(int exponent) => (int)(1u << exponent); + + public static int BitReverse(int index, int log2Size) => (int)(Hlsl.ReverseBits((uint)index) >> (UIntBits - log2Size)); + + public static int Position(int index, int stage) => index & (Pow2(stage) - 1); + + public static int TwiddleIndex(int position, int log2Size, int stage) => position << (log2Size - 1 - stage); + + public static int Radix2Even(int index, int log2Size, int stage) => Locate(index, log2Size, stage, Radix2Bits); + + public static int Radix4First(int index, int log2Size, int stage) => Locate(index, log2Size, stage, Radix4Bits); + + public static Float4 Rotate(Float4 value, float real, float imaginary) + => new( + value.X * real - value.Y * imaginary, + value.X * imaginary + value.Y * real, + value.Z * real - value.W * imaginary, + value.Z * imaginary + value.W * real); + + public static Float4 Multiply(Float4 value, Float2 weight) => Rotate(value, weight.X, weight.Y); + + public static Float4 Multiply(Float4 value, Float2 firstWeight, Float2 secondWeight) + => new( + value.X * firstWeight.X - value.Y * firstWeight.Y, + value.X * firstWeight.Y + value.Y * firstWeight.X, + value.Z * secondWeight.X - value.W * secondWeight.Y, + value.Z * secondWeight.Y + value.W * secondWeight.X); + + public static void Radix2(ref Float4 even, ref Float4 odd, Float2 twiddle, float direction) + { + var product = Rotate(odd, twiddle.X, twiddle.Y * direction); + var evenValue = even; + even = evenValue + product; + odd = evenValue - product; + } + + public static void Radix4( + ref Float4 first, + ref Float4 second, + ref Float4 third, + ref Float4 fourth, + Float2 stageTwiddle, + Float2 nextEvenTwiddle, + Float2 nextOddTwiddle, + float direction) + { + var secondProduct = Rotate(second, stageTwiddle.X, stageTwiddle.Y * direction); + var fourthProduct = Rotate(fourth, stageTwiddle.X, stageTwiddle.Y * direction); + var firstSum = first + secondProduct; + var secondSum = first - secondProduct; + var thirdSum = third + fourthProduct; + var fourthSum = third - fourthProduct; + var evenProduct = Rotate(thirdSum, nextEvenTwiddle.X, nextEvenTwiddle.Y * direction); + var oddProduct = Rotate(fourthSum, nextOddTwiddle.X, nextOddTwiddle.Y * direction); + first = firstSum + evenProduct; + third = firstSum - evenProduct; + second = secondSum + oddProduct; + fourth = secondSum - oddProduct; + } + + static int Locate(int index, int log2Size, int stage, int radixBits) + { + var laneBits = log2Size - radixBits; + var row = (index >> laneBits) << log2Size; + var lane = index & (Pow2(laneBits) - 1); + return row + ((lane >> stage) << (stage + radixBits)) + Position(lane, stage); + } +} diff --git a/SpectralConvolution/SpectralConvolution/GpuChromaticShaders.cs b/SpectralConvolution/SpectralConvolution/GpuChromaticShaders.cs index 30fb537..93eaafd 100644 --- a/SpectralConvolution/SpectralConvolution/GpuChromaticShaders.cs +++ b/SpectralConvolution/SpectralConvolution/GpuChromaticShaders.cs @@ -25,7 +25,6 @@ internal readonly partial struct ColumnChromaticShader( var group = GridIds.X; var size = (int)(1u << log2Size); var mask = size - 1; - var shift = 32 - log2Size; var halfColumns = ChromaticKernelSpectrum.HalfColumnsOf(size); var unitsPerGroup = TilePlan.GroupElements / size; var groupsPerTile = (halfColumns + unitsPerGroup - 1) / unitsPerGroup; @@ -50,7 +49,7 @@ internal readonly partial struct ColumnChromaticShader( blueAlpha = new Float4(near.Z + far.Z, near.W - far.W, near.W + far.W, far.Z - near.Z) * 0.5f; } - var reversed = (int)(Hlsl.ReverseBits((uint)y) >> shift); + var reversed = FftShaderMath.BitReverse(y, log2Size); var redGreenRow = (unit * TilePlan.ChromaticPlanes) << log2Size; row[redGreenRow + reversed] = redGreen; row[redGreenRow + size + reversed] = blueAlpha; @@ -58,20 +57,20 @@ internal readonly partial struct ColumnChromaticShader( Hlsl.GroupMemoryBarrierWithGroupSync(); - Butterflies(local, 1f); + Butterflies(local, FftShaderMath.Forward); var product0 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 0); var product1 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 1); var product2 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 2); var product3 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 3); Hlsl.GroupMemoryBarrierWithGroupSync(); - row[ReversedIndex(local, shift, mask, 0)] = product0; - row[ReversedIndex(local, shift, mask, 1)] = product1; - row[ReversedIndex(local, shift, mask, 2)] = product2; - row[ReversedIndex(local, shift, mask, 3)] = product3; + row[ReversedIndex(local, mask, 0)] = product0; + row[ReversedIndex(local, mask, 1)] = product1; + row[ReversedIndex(local, mask, 2)] = product2; + row[ReversedIndex(local, mask, 3)] = product3; Hlsl.GroupMemoryBarrierWithGroupSync(); - Butterflies(local, -1f); + Butterflies(local, FftShaderMath.Inverse); for (var step = 0; step < TilePlan.ValuesPerThread; step++) { @@ -103,10 +102,10 @@ internal readonly partial struct ColumnChromaticShader( } } - private int ReversedIndex(int local, int shift, int mask, int part) + private int ReversedIndex(int local, int mask, int part) { var element = local + part * TilePlan.GroupThreads; - return (element & ~mask) + (int)(Hlsl.ReverseBits((uint)(element & mask)) >> shift); + return (element & ~mask) + FftShaderMath.BitReverse(element & mask, log2Size); } private Float4 Multiply(int local, int planeLength, int halfColumns, int firstColumn, int mask, int part) @@ -123,74 +122,56 @@ internal readonly partial struct ColumnChromaticShader( var firstChannel = line % TilePlan.ChromaticPlanes == 0 ? ChromaticChannels.Red : ChromaticChannels.Blue; var first = spectrum[firstChannel * planeLength + index]; var second = spectrum[ChromaticChannels.Green * planeLength + index]; - return new Float4( - value.X * first.X - value.Y * first.Y, - value.X * first.Y + value.Y * first.X, - value.Z * second.X - value.W * second.Y, - value.Z * second.Y + value.W * second.X); + return FftShaderMath.Multiply(value, first, second); } - private static Float4 Rotate(Float4 value, float real, float imaginary) - => new( - value.X * real - value.Y * imaginary, - value.X * imaginary + value.Y * real, - value.Z * real - value.W * imaginary, - value.Z * imaginary + value.W * real); - private void Butterflies(int local, float direction) { - var halfSize = (int)(1u << (log2Size - 1)); - var unitRowBase = (local >> (log2Size - 2)) << log2Size; - var unit = local & ((int)(1u << (log2Size - 2)) - 1); var stage = 0; for (; stage + 1 < log2Size; stage += 2) { - var step = (int)(1u << stage); - var position = unit & (step - 1); - var first = unitRowBase + ((unit >> stage) << (stage + 2)) + position; + var step = FftShaderMath.Pow2(stage); + var position = FftShaderMath.Position(local, stage); + var first = FftShaderMath.Radix4First(local, log2Size, stage); var second = first + step; var third = second + step; var fourth = third + step; - var firstTwiddle = twiddles[position << (log2Size - 1 - stage)]; - var firstReal = firstTwiddle.X; - var firstImaginary = firstTwiddle.Y * direction; - var secondEvenTwiddle = twiddles[position << (log2Size - 2 - stage)]; - var secondOddTwiddle = twiddles[(position + step) << (log2Size - 2 - stage)]; var value0 = row[first]; var value1 = row[second]; var value2 = row[third]; var value3 = row[fourth]; - var product1 = Rotate(value1, firstReal, firstImaginary); - var product3 = Rotate(value3, firstReal, firstImaginary); - var sum0 = value0 + product1; - var sum1 = value0 - product1; - var sum2 = value2 + product3; - var sum3 = value2 - product3; - var evenProduct = Rotate(sum2, secondEvenTwiddle.X, secondEvenTwiddle.Y * direction); - var oddProduct = Rotate(sum3, secondOddTwiddle.X, secondOddTwiddle.Y * direction); - row[first] = sum0 + evenProduct; - row[third] = sum0 - evenProduct; - row[second] = sum1 + oddProduct; - row[fourth] = sum1 - oddProduct; + FftShaderMath.Radix4( + ref value0, + ref value1, + ref value2, + ref value3, + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage)], + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage + 1)], + twiddles[FftShaderMath.TwiddleIndex(position + step, log2Size, stage + 1)], + direction); + row[first] = value0; + row[second] = value1; + row[third] = value2; + row[fourth] = value3; Hlsl.GroupMemoryBarrierWithGroupSync(); } for (; stage < log2Size; stage++) { - var half = (int)(1u << stage); for (var step = 0; step < TilePlan.ValuesPerThread; step++) { var index = local + step * TilePlan.GroupThreads; - var rowBase = (index >> (log2Size - 1)) << log2Size; - var pair = index & (halfSize - 1); - var position = pair & (half - 1); - var even = rowBase + ((pair >> stage) << (stage + 1)) + position; - var odd = even + half; - var twiddle = twiddles[position << (log2Size - 1 - stage)]; - var product = Rotate(row[odd], twiddle.X, twiddle.Y * direction); + var even = FftShaderMath.Radix2Even(index, log2Size, stage); + var odd = even + FftShaderMath.Pow2(stage); var evenValue = row[even]; - row[even] = evenValue + product; - row[odd] = evenValue - product; + var oddValue = row[odd]; + FftShaderMath.Radix2( + ref evenValue, + ref oddValue, + twiddles[FftShaderMath.TwiddleIndex(FftShaderMath.Position(index, stage), log2Size, stage)], + direction); + row[even] = evenValue; + row[odd] = oddValue; } Hlsl.GroupMemoryBarrierWithGroupSync(); diff --git a/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs b/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs index f8e2925..6e2e43b 100644 --- a/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs +++ b/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs @@ -178,7 +178,6 @@ internal readonly partial struct ForwardRowLightShader( var groupBase = GridIds.X * TilePlan.GroupElements; var size = (int)(1u << log2Size); var mask = size - 1; - var shift = 32 - log2Size; var tileIndex = TileIndexOfGroup(groupBase); var tileRow = tileIndex / tilesX; @@ -210,7 +209,7 @@ internal readonly partial struct ForwardRowLightShader( sums = sums + value; } - var reversed = (int)(Hlsl.ReverseBits((uint)x) >> shift); + var reversed = FftShaderMath.BitReverse(x, log2Size); row[((element >> log2Size) << log2Size) + reversed] = value; } @@ -239,7 +238,7 @@ internal readonly partial struct ForwardRowLightShader( report[offset + 5] = Hlsl.AsUInt(partialSquares[0].Y); } - Butterflies(local, 1f); + Butterflies(local, FftShaderMath.Forward); var element0 = local * TilePlan.ValuesPerThread; var element1 = element0 + 1; @@ -290,52 +289,36 @@ internal readonly partial struct ForwardRowLightShader( return batchStart + (groupBase >> (log2Size * 2)); } - private static Float4 Rotate(Float4 value, float real, float imaginary) - => new( - value.X * real - value.Y * imaginary, - value.X * imaginary + value.Y * real, - value.Z * real - value.W * imaginary, - value.Z * imaginary + value.W * real); - private void Butterflies(int local, float direction) { - var halfSize = (int)(1u << (log2Size - 1)); - var rowBase = (local >> (log2Size - 1)) << log2Size; - var pair = local & (halfSize - 1); - var unitRowBase = (local >> (log2Size - 2)) << log2Size; - var unit = local & ((int)(1u << (log2Size - 2)) - 1); var stage = 0; for (; stage + 1 < log2Size; stage += 2) { - if (local < 128) + if (local < FftShaderMath.StageUnitsPerGroup) { - var step = (int)(1u << stage); - var position = unit & (step - 1); - var first = unitRowBase + ((unit >> stage) << (stage + 2)) + position; + var step = FftShaderMath.Pow2(stage); + var position = FftShaderMath.Position(local, stage); + var first = FftShaderMath.Radix4First(local, log2Size, stage); var second = first + step; var third = second + step; var fourth = third + step; - var firstTwiddle = twiddles[position << (log2Size - 1 - stage)]; - var firstReal = firstTwiddle.X; - var firstImaginary = firstTwiddle.Y * direction; - var secondEvenTwiddle = twiddles[position << (log2Size - 2 - stage)]; - var secondOddTwiddle = twiddles[(position + step) << (log2Size - 2 - stage)]; var value0 = row[first]; var value1 = row[second]; var value2 = row[third]; var value3 = row[fourth]; - var product1 = Rotate(value1, firstReal, firstImaginary); - var product3 = Rotate(value3, firstReal, firstImaginary); - var sum0 = value0 + product1; - var sum1 = value0 - product1; - var sum2 = value2 + product3; - var sum3 = value2 - product3; - var evenProduct = Rotate(sum2, secondEvenTwiddle.X, secondEvenTwiddle.Y * direction); - var oddProduct = Rotate(sum3, secondOddTwiddle.X, secondOddTwiddle.Y * direction); - row[first] = sum0 + evenProduct; - row[third] = sum0 - evenProduct; - row[second] = sum1 + oddProduct; - row[fourth] = sum1 - oddProduct; + FftShaderMath.Radix4( + ref value0, + ref value1, + ref value2, + ref value3, + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage)], + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage + 1)], + twiddles[FftShaderMath.TwiddleIndex(position + step, log2Size, stage + 1)], + direction); + row[first] = value0; + row[second] = value1; + row[third] = value2; + row[fourth] = value3; } Hlsl.GroupMemoryBarrierWithGroupSync(); @@ -343,15 +326,17 @@ internal readonly partial struct ForwardRowLightShader( for (; stage < log2Size; stage++) { - var half = (int)(1u << stage); - var position = pair & (half - 1); - var even = rowBase + ((pair >> stage) << (stage + 1)) + position; - var odd = even + half; - var twiddle = twiddles[position << (log2Size - 1 - stage)]; - var product = Rotate(row[odd], twiddle.X, twiddle.Y * direction); + var even = FftShaderMath.Radix2Even(local, log2Size, stage); + var odd = even + FftShaderMath.Pow2(stage); var evenValue = row[even]; - row[even] = evenValue + product; - row[odd] = evenValue - product; + var oddValue = row[odd]; + FftShaderMath.Radix2( + ref evenValue, + ref oddValue, + twiddles[FftShaderMath.TwiddleIndex(FftShaderMath.Position(local, stage), log2Size, stage)], + direction); + row[even] = evenValue; + row[odd] = oddValue; Hlsl.GroupMemoryBarrierWithGroupSync(); } } @@ -425,14 +410,13 @@ internal readonly partial struct InverseRowLightShader( var groupBase = GridIds.X * TilePlan.GroupElements; var size = (int)(1u << log2Size); var mask = size - 1; - var shift = 32 - log2Size; var element0 = local * TilePlan.ValuesPerThread; var element1 = element0 + 1; - row[((element0 >> log2Size) << log2Size) + (int)(Hlsl.ReverseBits((uint)(element0 & mask)) >> shift)] = tiles[groupBase + element0]; - row[((element1 >> log2Size) << log2Size) + (int)(Hlsl.ReverseBits((uint)(element1 & mask)) >> shift)] = tiles[groupBase + element1]; + row[((element0 >> log2Size) << log2Size) + FftShaderMath.BitReverse(element0 & mask, log2Size)] = tiles[groupBase + element0]; + row[((element1 >> log2Size) << log2Size) + FftShaderMath.BitReverse(element1 & mask, log2Size)] = tiles[groupBase + element1]; Hlsl.GroupMemoryBarrierWithGroupSync(); - Butterflies(local, -1f); + Butterflies(local, FftShaderMath.Inverse); var tileIndex = TileIndexOfGroup(groupBase); var tileRow = tileIndex / tilesX; @@ -514,52 +498,36 @@ internal readonly partial struct InverseRowLightShader( private static bool IsNonFinite(float value) => (Hlsl.AsUInt(value) & 0x7F800000u) == 0x7F800000u; - private static Float4 Rotate(Float4 value, float real, float imaginary) - => new( - value.X * real - value.Y * imaginary, - value.X * imaginary + value.Y * real, - value.Z * real - value.W * imaginary, - value.Z * imaginary + value.W * real); - private void Butterflies(int local, float direction) { - var halfSize = (int)(1u << (log2Size - 1)); - var rowBase = (local >> (log2Size - 1)) << log2Size; - var pair = local & (halfSize - 1); - var unitRowBase = (local >> (log2Size - 2)) << log2Size; - var unit = local & ((int)(1u << (log2Size - 2)) - 1); var stage = 0; for (; stage + 1 < log2Size; stage += 2) { - if (local < 128) + if (local < FftShaderMath.StageUnitsPerGroup) { - var step = (int)(1u << stage); - var position = unit & (step - 1); - var first = unitRowBase + ((unit >> stage) << (stage + 2)) + position; + var step = FftShaderMath.Pow2(stage); + var position = FftShaderMath.Position(local, stage); + var first = FftShaderMath.Radix4First(local, log2Size, stage); var second = first + step; var third = second + step; var fourth = third + step; - var firstTwiddle = twiddles[position << (log2Size - 1 - stage)]; - var firstReal = firstTwiddle.X; - var firstImaginary = firstTwiddle.Y * direction; - var secondEvenTwiddle = twiddles[position << (log2Size - 2 - stage)]; - var secondOddTwiddle = twiddles[(position + step) << (log2Size - 2 - stage)]; var value0 = row[first]; var value1 = row[second]; var value2 = row[third]; var value3 = row[fourth]; - var product1 = Rotate(value1, firstReal, firstImaginary); - var product3 = Rotate(value3, firstReal, firstImaginary); - var sum0 = value0 + product1; - var sum1 = value0 - product1; - var sum2 = value2 + product3; - var sum3 = value2 - product3; - var evenProduct = Rotate(sum2, secondEvenTwiddle.X, secondEvenTwiddle.Y * direction); - var oddProduct = Rotate(sum3, secondOddTwiddle.X, secondOddTwiddle.Y * direction); - row[first] = sum0 + evenProduct; - row[third] = sum0 - evenProduct; - row[second] = sum1 + oddProduct; - row[fourth] = sum1 - oddProduct; + FftShaderMath.Radix4( + ref value0, + ref value1, + ref value2, + ref value3, + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage)], + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage + 1)], + twiddles[FftShaderMath.TwiddleIndex(position + step, log2Size, stage + 1)], + direction); + row[first] = value0; + row[second] = value1; + row[third] = value2; + row[fourth] = value3; } Hlsl.GroupMemoryBarrierWithGroupSync(); @@ -567,15 +535,17 @@ internal readonly partial struct InverseRowLightShader( for (; stage < log2Size; stage++) { - var half = (int)(1u << stage); - var position = pair & (half - 1); - var even = rowBase + ((pair >> stage) << (stage + 1)) + position; - var odd = even + half; - var twiddle = twiddles[position << (log2Size - 1 - stage)]; - var product = Rotate(row[odd], twiddle.X, twiddle.Y * direction); + var even = FftShaderMath.Radix2Even(local, log2Size, stage); + var odd = even + FftShaderMath.Pow2(stage); var evenValue = row[even]; - row[even] = evenValue + product; - row[odd] = evenValue - product; + var oddValue = row[odd]; + FftShaderMath.Radix2( + ref evenValue, + ref oddValue, + twiddles[FftShaderMath.TwiddleIndex(FftShaderMath.Position(local, stage), log2Size, stage)], + direction); + row[even] = evenValue; + row[odd] = oddValue; Hlsl.GroupMemoryBarrierWithGroupSync(); } } diff --git a/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs b/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs index 7d63a84..5fe26a4 100644 --- a/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs +++ b/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs @@ -152,7 +152,6 @@ internal readonly partial struct ForwardRowShader( var groupBase = GridIds.X * TilePlan.GroupElements; var size = (int)(1u << log2Size); var mask = size - 1; - var shift = 32 - log2Size; if (local < GpuTileLayout.StatisticsPerGroup) statistics[local] = 0u; Hlsl.GroupMemoryBarrierWithGroupSync(); @@ -199,7 +198,7 @@ internal readonly partial struct ForwardRowShader( } } - var reversed = (int)(Hlsl.ReverseBits((uint)x) >> shift); + var reversed = FftShaderMath.BitReverse(x, log2Size); row[((element >> log2Size) << log2Size) + reversed] = value; } @@ -213,7 +212,7 @@ internal readonly partial struct ForwardRowShader( if (local < GpuTileLayout.StatisticsPerGroup) report[statisticsOffset + (groupStart + GridIds.X) * GpuTileLayout.StatisticsPerGroup + local] = statistics[local]; - Butterflies(local, 1f); + Butterflies(local, FftShaderMath.Forward); var element0 = local * TilePlan.ValuesPerThread; var element1 = element0 + 1; @@ -226,52 +225,36 @@ internal readonly partial struct ForwardRowShader( return batchStart + (groupBase >> (log2Size * 2)); } - private static Float4 Rotate(Float4 value, float real, float imaginary) - => new( - value.X * real - value.Y * imaginary, - value.X * imaginary + value.Y * real, - value.Z * real - value.W * imaginary, - value.Z * imaginary + value.W * real); - private void Butterflies(int local, float direction) { - var halfSize = (int)(1u << (log2Size - 1)); - var rowBase = (local >> (log2Size - 1)) << log2Size; - var pair = local & (halfSize - 1); - var unitRowBase = (local >> (log2Size - 2)) << log2Size; - var unit = local & ((int)(1u << (log2Size - 2)) - 1); var stage = 0; for (; stage + 1 < log2Size; stage += 2) { - if (local < 128) + if (local < FftShaderMath.StageUnitsPerGroup) { - var step = (int)(1u << stage); - var position = unit & (step - 1); - var first = unitRowBase + ((unit >> stage) << (stage + 2)) + position; + var step = FftShaderMath.Pow2(stage); + var position = FftShaderMath.Position(local, stage); + var first = FftShaderMath.Radix4First(local, log2Size, stage); var second = first + step; var third = second + step; var fourth = third + step; - var firstTwiddle = twiddles[position << (log2Size - 1 - stage)]; - var firstReal = firstTwiddle.X; - var firstImaginary = firstTwiddle.Y * direction; - var secondEvenTwiddle = twiddles[position << (log2Size - 2 - stage)]; - var secondOddTwiddle = twiddles[(position + step) << (log2Size - 2 - stage)]; var value0 = row[first]; var value1 = row[second]; var value2 = row[third]; var value3 = row[fourth]; - var product1 = Rotate(value1, firstReal, firstImaginary); - var product3 = Rotate(value3, firstReal, firstImaginary); - var sum0 = value0 + product1; - var sum1 = value0 - product1; - var sum2 = value2 + product3; - var sum3 = value2 - product3; - var evenProduct = Rotate(sum2, secondEvenTwiddle.X, secondEvenTwiddle.Y * direction); - var oddProduct = Rotate(sum3, secondOddTwiddle.X, secondOddTwiddle.Y * direction); - row[first] = sum0 + evenProduct; - row[third] = sum0 - evenProduct; - row[second] = sum1 + oddProduct; - row[fourth] = sum1 - oddProduct; + FftShaderMath.Radix4( + ref value0, + ref value1, + ref value2, + ref value3, + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage)], + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage + 1)], + twiddles[FftShaderMath.TwiddleIndex(position + step, log2Size, stage + 1)], + direction); + row[first] = value0; + row[second] = value1; + row[third] = value2; + row[fourth] = value3; } Hlsl.GroupMemoryBarrierWithGroupSync(); @@ -279,15 +262,17 @@ internal readonly partial struct ForwardRowShader( for (; stage < log2Size; stage++) { - var half = (int)(1u << stage); - var position = pair & (half - 1); - var even = rowBase + ((pair >> stage) << (stage + 1)) + position; - var odd = even + half; - var twiddle = twiddles[position << (log2Size - 1 - stage)]; - var product = Rotate(row[odd], twiddle.X, twiddle.Y * direction); + var even = FftShaderMath.Radix2Even(local, log2Size, stage); + var odd = even + FftShaderMath.Pow2(stage); var evenValue = row[even]; - row[even] = evenValue + product; - row[odd] = evenValue - product; + var oddValue = row[odd]; + FftShaderMath.Radix2( + ref evenValue, + ref oddValue, + twiddles[FftShaderMath.TwiddleIndex(FftShaderMath.Position(local, stage), log2Size, stage)], + direction); + row[even] = evenValue; + row[odd] = oddValue; Hlsl.GroupMemoryBarrierWithGroupSync(); } } @@ -315,7 +300,6 @@ internal readonly partial struct ColumnShader( var local = GroupIds.X; var group = GridIds.X; var mask = (int)(1u << log2Size) - 1; - var shift = 32 - log2Size; var log2Columns = TilePlan.GroupElementsLog2 - log2Size; var columnMask = (int)(1u << log2Columns) - 1; var groupsPerTile = (int)(1u << (log2Size - log2Columns)); @@ -331,80 +315,57 @@ internal readonly partial struct ColumnShader( var column1 = element1 & columnMask; var global0 = tileBase + (y0 << log2Size) + firstColumn + column0; var global1 = tileBase + (y1 << log2Size) + firstColumn + column1; - row[(column0 << log2Size) + (int)(Hlsl.ReverseBits((uint)y0) >> shift)] = tiles[global0]; - row[(column1 << log2Size) + (int)(Hlsl.ReverseBits((uint)y1) >> shift)] = tiles[global1]; + row[(column0 << log2Size) + FftShaderMath.BitReverse(y0, log2Size)] = tiles[global0]; + row[(column1 << log2Size) + FftShaderMath.BitReverse(y1, log2Size)] = tiles[global1]; Hlsl.GroupMemoryBarrierWithGroupSync(); - Butterflies(local, 1f); + Butterflies(local, FftShaderMath.Forward); var frequency0 = element0 & mask; var frequency1 = element1 & mask; - var product0 = Multiply(row[element0], spectrum[((firstColumn + (element0 >> log2Size)) << log2Size) + frequency0]); - var product1 = Multiply(row[element1], spectrum[((firstColumn + (element1 >> log2Size)) << log2Size) + frequency1]); + var product0 = FftShaderMath.Multiply(row[element0], spectrum[((firstColumn + (element0 >> log2Size)) << log2Size) + frequency0]); + var product1 = FftShaderMath.Multiply(row[element1], spectrum[((firstColumn + (element1 >> log2Size)) << log2Size) + frequency1]); Hlsl.GroupMemoryBarrierWithGroupSync(); - row[((element0 >> log2Size) << log2Size) + (int)(Hlsl.ReverseBits((uint)frequency0) >> shift)] = product0; - row[((element1 >> log2Size) << log2Size) + (int)(Hlsl.ReverseBits((uint)frequency1) >> shift)] = product1; + row[((element0 >> log2Size) << log2Size) + FftShaderMath.BitReverse(frequency0, log2Size)] = product0; + row[((element1 >> log2Size) << log2Size) + FftShaderMath.BitReverse(frequency1, log2Size)] = product1; Hlsl.GroupMemoryBarrierWithGroupSync(); - Butterflies(local, -1f); + Butterflies(local, FftShaderMath.Inverse); tiles[global0] = row[(column0 << log2Size) + y0]; tiles[global1] = row[(column1 << log2Size) + y1]; } - private static Float4 Multiply(Float4 value, Float2 weight) - => new( - value.X * weight.X - value.Y * weight.Y, - value.X * weight.Y + value.Y * weight.X, - value.Z * weight.X - value.W * weight.Y, - value.Z * weight.Y + value.W * weight.X); - - private static Float4 Rotate(Float4 value, float real, float imaginary) - => new( - value.X * real - value.Y * imaginary, - value.X * imaginary + value.Y * real, - value.Z * real - value.W * imaginary, - value.Z * imaginary + value.W * real); - private void Butterflies(int local, float direction) { - var halfSize = (int)(1u << (log2Size - 1)); - var rowBase = (local >> (log2Size - 1)) << log2Size; - var pair = local & (halfSize - 1); - var unitRowBase = (local >> (log2Size - 2)) << log2Size; - var unit = local & ((int)(1u << (log2Size - 2)) - 1); var stage = 0; for (; stage + 1 < log2Size; stage += 2) { - if (local < 128) + if (local < FftShaderMath.StageUnitsPerGroup) { - var step = (int)(1u << stage); - var position = unit & (step - 1); - var first = unitRowBase + ((unit >> stage) << (stage + 2)) + position; + var step = FftShaderMath.Pow2(stage); + var position = FftShaderMath.Position(local, stage); + var first = FftShaderMath.Radix4First(local, log2Size, stage); var second = first + step; var third = second + step; var fourth = third + step; - var firstTwiddle = twiddles[position << (log2Size - 1 - stage)]; - var firstReal = firstTwiddle.X; - var firstImaginary = firstTwiddle.Y * direction; - var secondEvenTwiddle = twiddles[position << (log2Size - 2 - stage)]; - var secondOddTwiddle = twiddles[(position + step) << (log2Size - 2 - stage)]; var value0 = row[first]; var value1 = row[second]; var value2 = row[third]; var value3 = row[fourth]; - var product1 = Rotate(value1, firstReal, firstImaginary); - var product3 = Rotate(value3, firstReal, firstImaginary); - var sum0 = value0 + product1; - var sum1 = value0 - product1; - var sum2 = value2 + product3; - var sum3 = value2 - product3; - var evenProduct = Rotate(sum2, secondEvenTwiddle.X, secondEvenTwiddle.Y * direction); - var oddProduct = Rotate(sum3, secondOddTwiddle.X, secondOddTwiddle.Y * direction); - row[first] = sum0 + evenProduct; - row[third] = sum0 - evenProduct; - row[second] = sum1 + oddProduct; - row[fourth] = sum1 - oddProduct; + FftShaderMath.Radix4( + ref value0, + ref value1, + ref value2, + ref value3, + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage)], + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage + 1)], + twiddles[FftShaderMath.TwiddleIndex(position + step, log2Size, stage + 1)], + direction); + row[first] = value0; + row[second] = value1; + row[third] = value2; + row[fourth] = value3; } Hlsl.GroupMemoryBarrierWithGroupSync(); @@ -412,15 +373,17 @@ internal readonly partial struct ColumnShader( for (; stage < log2Size; stage++) { - var half = (int)(1u << stage); - var position = pair & (half - 1); - var even = rowBase + ((pair >> stage) << (stage + 1)) + position; - var odd = even + half; - var twiddle = twiddles[position << (log2Size - 1 - stage)]; - var product = Rotate(row[odd], twiddle.X, twiddle.Y * direction); + var even = FftShaderMath.Radix2Even(local, log2Size, stage); + var odd = even + FftShaderMath.Pow2(stage); var evenValue = row[even]; - row[even] = evenValue + product; - row[odd] = evenValue - product; + var oddValue = row[odd]; + FftShaderMath.Radix2( + ref evenValue, + ref oddValue, + twiddles[FftShaderMath.TwiddleIndex(FftShaderMath.Position(local, stage), log2Size, stage)], + direction); + row[even] = evenValue; + row[odd] = oddValue; Hlsl.GroupMemoryBarrierWithGroupSync(); } } @@ -486,14 +449,13 @@ internal readonly partial struct InverseRowShader( var groupBase = GridIds.X * TilePlan.GroupElements; var size = (int)(1u << log2Size); var mask = size - 1; - var shift = 32 - log2Size; var element0 = local * TilePlan.ValuesPerThread; var element1 = element0 + 1; - row[((element0 >> log2Size) << log2Size) + (int)(Hlsl.ReverseBits((uint)(element0 & mask)) >> shift)] = tiles[groupBase + element0]; - row[((element1 >> log2Size) << log2Size) + (int)(Hlsl.ReverseBits((uint)(element1 & mask)) >> shift)] = tiles[groupBase + element1]; + row[((element0 >> log2Size) << log2Size) + FftShaderMath.BitReverse(element0 & mask, log2Size)] = tiles[groupBase + element0]; + row[((element1 >> log2Size) << log2Size) + FftShaderMath.BitReverse(element1 & mask, log2Size)] = tiles[groupBase + element1]; Hlsl.GroupMemoryBarrierWithGroupSync(); - Butterflies(local, -1f); + Butterflies(local, FftShaderMath.Inverse); var tileIndex = TileIndexOfGroup(groupBase); var tileRow = tileIndex / tilesX; @@ -575,52 +537,36 @@ internal readonly partial struct InverseRowShader( private static bool IsNonFinite(float value) => (Hlsl.AsUInt(value) & 0x7F800000u) == 0x7F800000u; - private static Float4 Rotate(Float4 value, float real, float imaginary) - => new( - value.X * real - value.Y * imaginary, - value.X * imaginary + value.Y * real, - value.Z * real - value.W * imaginary, - value.Z * imaginary + value.W * real); - private void Butterflies(int local, float direction) { - var halfSize = (int)(1u << (log2Size - 1)); - var rowBase = (local >> (log2Size - 1)) << log2Size; - var pair = local & (halfSize - 1); - var unitRowBase = (local >> (log2Size - 2)) << log2Size; - var unit = local & ((int)(1u << (log2Size - 2)) - 1); var stage = 0; for (; stage + 1 < log2Size; stage += 2) { - if (local < 128) + if (local < FftShaderMath.StageUnitsPerGroup) { - var step = (int)(1u << stage); - var position = unit & (step - 1); - var first = unitRowBase + ((unit >> stage) << (stage + 2)) + position; + var step = FftShaderMath.Pow2(stage); + var position = FftShaderMath.Position(local, stage); + var first = FftShaderMath.Radix4First(local, log2Size, stage); var second = first + step; var third = second + step; var fourth = third + step; - var firstTwiddle = twiddles[position << (log2Size - 1 - stage)]; - var firstReal = firstTwiddle.X; - var firstImaginary = firstTwiddle.Y * direction; - var secondEvenTwiddle = twiddles[position << (log2Size - 2 - stage)]; - var secondOddTwiddle = twiddles[(position + step) << (log2Size - 2 - stage)]; var value0 = row[first]; var value1 = row[second]; var value2 = row[third]; var value3 = row[fourth]; - var product1 = Rotate(value1, firstReal, firstImaginary); - var product3 = Rotate(value3, firstReal, firstImaginary); - var sum0 = value0 + product1; - var sum1 = value0 - product1; - var sum2 = value2 + product3; - var sum3 = value2 - product3; - var evenProduct = Rotate(sum2, secondEvenTwiddle.X, secondEvenTwiddle.Y * direction); - var oddProduct = Rotate(sum3, secondOddTwiddle.X, secondOddTwiddle.Y * direction); - row[first] = sum0 + evenProduct; - row[third] = sum0 - evenProduct; - row[second] = sum1 + oddProduct; - row[fourth] = sum1 - oddProduct; + FftShaderMath.Radix4( + ref value0, + ref value1, + ref value2, + ref value3, + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage)], + twiddles[FftShaderMath.TwiddleIndex(position, log2Size, stage + 1)], + twiddles[FftShaderMath.TwiddleIndex(position + step, log2Size, stage + 1)], + direction); + row[first] = value0; + row[second] = value1; + row[third] = value2; + row[fourth] = value3; } Hlsl.GroupMemoryBarrierWithGroupSync(); @@ -628,15 +574,17 @@ internal readonly partial struct InverseRowShader( for (; stage < log2Size; stage++) { - var half = (int)(1u << stage); - var position = pair & (half - 1); - var even = rowBase + ((pair >> stage) << (stage + 1)) + position; - var odd = even + half; - var twiddle = twiddles[position << (log2Size - 1 - stage)]; - var product = Rotate(row[odd], twiddle.X, twiddle.Y * direction); + var even = FftShaderMath.Radix2Even(local, log2Size, stage); + var odd = even + FftShaderMath.Pow2(stage); var evenValue = row[even]; - row[even] = evenValue + product; - row[odd] = evenValue - product; + var oddValue = row[odd]; + FftShaderMath.Radix2( + ref evenValue, + ref oddValue, + twiddles[FftShaderMath.TwiddleIndex(FftShaderMath.Position(local, stage), log2Size, stage)], + direction); + row[even] = evenValue; + row[odd] = oddValue; Hlsl.GroupMemoryBarrierWithGroupSync(); } }