From 862995196fd093b3e48f9136dfd22b3607962699 Mon Sep 17 00:00:00 2001 From: Jeremy Pritts <49847914+ds5678@users.noreply.github.com> Date: Thu, 29 Jun 2023 22:21:45 -0400 Subject: [PATCH] Use stackalloc in Bc6h --- AssetRipper.TextureDecoder/Bc/BcHelpers.cs | 44 +++++++++++----------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/AssetRipper.TextureDecoder/Bc/BcHelpers.cs b/AssetRipper.TextureDecoder/Bc/BcHelpers.cs index 8f4ed3a..c46b0b3 100644 --- a/AssetRipper.TextureDecoder/Bc/BcHelpers.cs +++ b/AssetRipper.TextureDecoder/Bc/BcHelpers.cs @@ -64,13 +64,10 @@ internal unsafe static class BcHelpers public static void DecompressBc6h_Half(ReadOnlySpan compressedBlock, Span decompressedBlock, int destinationPitch, bool isSigned) { - BitStream bstream = new BitStream(); - int[] r = new int[4]; // wxyz - int[] g = new int[4]; - int[] b = new int[4]; - int[] epR = new int[2]; // endpoints A and B - int[] epG = new int[2]; - int[] epB = new int[2]; + BitStream bstream = new(); + Span r = stackalloc int[4]; // wxyz + Span g = stackalloc int[4]; + Span b = stackalloc int[4]; int decompressedOffset = 0; @@ -78,9 +75,9 @@ internal unsafe static class BcHelpers bstream.low = compressedBlockSpan[0]; bstream.high = compressedBlockSpan[1]; - r[0] = r[1] = r[2] = r[3] = 0; - g[0] = g[1] = g[2] = g[3] = 0; - b[0] = b[1] = b[2] = b[3] = 0; + r.Clear(); + g.Clear(); + b.Clear(); int mode = bstream.ReadBits(2); if (mode > 1) @@ -536,7 +533,7 @@ internal unsafe static class BcHelpers g[i] = TransformInverse(g[i], g[0], Bc6hTables.ActualBitsCount[0][mode], isSigned); b[i] = TransformInverse(b[i], b[0], Bc6hTables.ActualBitsCount[0][mode], isSigned); } - } + } for (int i = 0; i < 4; ++i) { @@ -553,18 +550,19 @@ internal unsafe static class BcHelpers } partitionSet &= 0x01; - int index = bstream.ReadBits(indexBits); + int index = bstream.ReadBits(indexBits); + + // endpoints A and B + int epR0 = Unquantize(r[(partitionSet * 2) + 0], Bc6hTables.ActualBitsCount[0][mode], isSigned); + int epG0 = Unquantize(g[(partitionSet * 2) + 0], Bc6hTables.ActualBitsCount[0][mode], isSigned); + int epB0 = Unquantize(b[(partitionSet * 2) + 0], Bc6hTables.ActualBitsCount[0][mode], isSigned); + int epR1 = Unquantize(r[(partitionSet * 2) + 1], Bc6hTables.ActualBitsCount[0][mode], isSigned); + int epG1 = Unquantize(g[(partitionSet * 2) + 1], Bc6hTables.ActualBitsCount[0][mode], isSigned); + int epB1 = Unquantize(b[(partitionSet * 2) + 1], Bc6hTables.ActualBitsCount[0][mode], isSigned); - epR[0] = Unquantize(r[(partitionSet * 2) + 0], Bc6hTables.ActualBitsCount[0][mode], isSigned); - epG[0] = Unquantize(g[(partitionSet * 2) + 0], Bc6hTables.ActualBitsCount[0][mode], isSigned); - epB[0] = Unquantize(b[(partitionSet * 2) + 0], Bc6hTables.ActualBitsCount[0][mode], isSigned); - epR[1] = Unquantize(r[(partitionSet * 2) + 1], Bc6hTables.ActualBitsCount[0][mode], isSigned); - epG[1] = Unquantize(g[(partitionSet * 2) + 1], Bc6hTables.ActualBitsCount[0][mode], isSigned); - epB[1] = Unquantize(b[(partitionSet * 2) + 1], Bc6hTables.ActualBitsCount[0][mode], isSigned); - - decompressedBlock[decompressedOffset + (j * 3) + 0] = FinishUnquantize(Interpolate(epR[0], epR[1], (mode >= 10) ? Bc6hTables.AWeight4 : Bc6hTables.AWeight3, index), isSigned); - decompressedBlock[decompressedOffset + (j * 3) + 1] = FinishUnquantize(Interpolate(epG[0], epG[1], (mode >= 10) ? Bc6hTables.AWeight4 : Bc6hTables.AWeight3, index), isSigned); - decompressedBlock[decompressedOffset + (j * 3) + 2] = FinishUnquantize(Interpolate(epB[0], epB[1], (mode >= 10) ? Bc6hTables.AWeight4 : Bc6hTables.AWeight3, index), isSigned); + decompressedBlock[decompressedOffset + (j * 3) + 0] = FinishUnquantize(Interpolate(epR0, epR1, (mode >= 10) ? Bc6hTables.AWeight4 : Bc6hTables.AWeight3, index), isSigned); + decompressedBlock[decompressedOffset + (j * 3) + 1] = FinishUnquantize(Interpolate(epG0, epG1, (mode >= 10) ? Bc6hTables.AWeight4 : Bc6hTables.AWeight3, index), isSigned); + decompressedBlock[decompressedOffset + (j * 3) + 2] = FinishUnquantize(Interpolate(epB0, epB1, (mode >= 10) ? Bc6hTables.AWeight4 : Bc6hTables.AWeight3, index), isSigned); } decompressedOffset += destinationPitch; @@ -573,7 +571,7 @@ internal unsafe static class BcHelpers public static void DecompressBc7(ReadOnlySpan compressedBlock, Span decompressedBlock, int destinationPitch) { - BitStream bstream = new BitStream(); + BitStream bstream = new(); int[][] endpoints = CreateRectangularArray(6, 4); int[][] indices = CreateRectangularArray(4, 4);