From e5da67340bee2cc6c883a29ebdcc662cad0a2236 Mon Sep 17 00:00:00 2001 From: Jeremy Pritts <49847914+ds5678@users.noreply.github.com> Date: Wed, 2 Aug 2023 20:14:28 -0400 Subject: [PATCH] Aggressive Optimization --- .../Astc/AstcDecoder.cs | 24 ++--- AssetRipper.TextureDecoder/Atc/AtcDecoder.cs | 10 +- AssetRipper.TextureDecoder/Dxt/DxtDecoder.cs | 10 +- AssetRipper.TextureDecoder/Etc/EtcDecoder.cs | 18 ++-- .../OptimizationConstants.cs | 11 --- .../Pvrtc/PvrtcDecoder.cs | 10 +- .../Rgb/DefaultColorMethods.cs | 4 +- .../Rgb/Formats/ColorRGB9e5.cs | 8 +- AssetRipper.TextureDecoder/Rgb/IColor.cs | 4 +- .../Rgb/NumericConversion.cs | 10 +- .../Rgb/RgbConverter.cs | 92 +++++++++---------- AssetRipper.TextureDecoder/Rgb/UInt4Pair.cs | 2 +- .../Yuy2/Yuy2Decoder.cs | 12 +-- 13 files changed, 98 insertions(+), 117 deletions(-) delete mode 100644 AssetRipper.TextureDecoder/OptimizationConstants.cs diff --git a/AssetRipper.TextureDecoder/Astc/AstcDecoder.cs b/AssetRipper.TextureDecoder/Astc/AstcDecoder.cs index ca23602..faea440 100644 --- a/AssetRipper.TextureDecoder/Astc/AstcDecoder.cs +++ b/AssetRipper.TextureDecoder/Astc/AstcDecoder.cs @@ -855,19 +855,19 @@ namespace AssetRipper.TextureDecoder.Astc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint Color(uint r, uint g, uint b, uint a) { return r << 16 | g << 8 | b | a << 24; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static byte BitReverseU8(byte c, int bits) { return (byte)(BitReverseTable[c] >> (8 - bits)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static ulong BitReverseU64(ulong d, int bits) { ulong ret; @@ -886,13 +886,13 @@ namespace AssetRipper.TextureDecoder.Astc return ret >> (64 - bits); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe static int GetBits(byte* input, int bit, int len) { return (*((int*)(input + bit / 8)) >> (bit % 8)) & ((1 << len) - 1); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe static ulong GetBits64(byte* input, int bit, int len) { ulong mask = len == 64 ? 0xffffffffffffffff : (1UL << len) - 1; @@ -918,13 +918,13 @@ namespace AssetRipper.TextureDecoder.Astc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static byte Clamp(int n) { return n < 0 ? byte.MinValue : n > 255 ? byte.MaxValue : (byte)n; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static unsafe void BitTransferSigned(int* a, int* b) { *b = (*b >> 1) | (*a & 0x80); @@ -935,7 +935,7 @@ namespace AssetRipper.TextureDecoder.Astc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe static void SetEndpoint(int* endpoint, int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2) { endpoint[0] = r1; @@ -948,7 +948,7 @@ namespace AssetRipper.TextureDecoder.Astc endpoint[7] = a2; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe static void SetEndpointClamp(int* endpoint, int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2) { endpoint[0] = Clamp(r1); @@ -961,7 +961,7 @@ namespace AssetRipper.TextureDecoder.Astc endpoint[7] = Clamp(a2); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe static void SetEndpointBlue(int* endpoint, int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2) { endpoint[0] = (r1 + b1) >> 1; @@ -974,7 +974,7 @@ namespace AssetRipper.TextureDecoder.Astc endpoint[7] = a2; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe static void SetEndpointBlueClamp(int* endpoint, int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2) { endpoint[0] = Clamp((r1 + b1) >> 1); @@ -987,7 +987,7 @@ namespace AssetRipper.TextureDecoder.Astc endpoint[7] = Clamp(a2); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static byte SelectColor(int v0, int v1, int weight) { return (byte)(((((v0 << 8 | v0) * (64 - weight) + (v1 << 8 | v1) * weight + 32) >> 6) * 255 + 32768) / 65536); diff --git a/AssetRipper.TextureDecoder/Atc/AtcDecoder.cs b/AssetRipper.TextureDecoder/Atc/AtcDecoder.cs index 6ed559a..2a08a77 100644 --- a/AssetRipper.TextureDecoder/Atc/AtcDecoder.cs +++ b/AssetRipper.TextureDecoder/Atc/AtcDecoder.cs @@ -143,13 +143,13 @@ namespace AssetRipper.TextureDecoder.Atc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static T ReadAtOffset(this ReadOnlySpan input, int offset) where T : unmanaged { return MemoryMarshal.Read(input.Slice(offset)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void DecodeColors(Span colors, int c0, int c1) { if ((c0 & 0x8000) == 0) @@ -190,7 +190,7 @@ namespace AssetRipper.TextureDecoder.Atc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void DecodeAlphas(Span alphas, int a0, int a1) { alphas[0] = a0; @@ -215,14 +215,14 @@ namespace AssetRipper.TextureDecoder.Atc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static int Extend(int value, int from, int to) { // bit-pattern replicating scaling (can at most double the bits) return (value << (to - from)) | (value >> (from * 2 - to)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint Color(int r, int g, int b, int a) { return unchecked((uint)(r << 16 | g << 8 | b | a << 24)); diff --git a/AssetRipper.TextureDecoder/Dxt/DxtDecoder.cs b/AssetRipper.TextureDecoder/Dxt/DxtDecoder.cs index 226acca..08aaee2 100644 --- a/AssetRipper.TextureDecoder/Dxt/DxtDecoder.cs +++ b/AssetRipper.TextureDecoder/Dxt/DxtDecoder.cs @@ -251,7 +251,7 @@ namespace AssetRipper.TextureDecoder.Dxt return offset; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void Rgb565(int c, out int r, out int g, out int b) { r = (c & 0xf800) >> 8; @@ -262,7 +262,7 @@ namespace AssetRipper.TextureDecoder.Dxt b |= b >> 5; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static int Color(int r, int g, int b, int a) { return r << 16 | g << 8 | b | a << 24; @@ -276,19 +276,19 @@ namespace AssetRipper.TextureDecoder.Dxt /// The destination buffer. /// The zero-based byte offset into destination. /// The number of bytes to copy. - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void BlockCopy(ReadOnlySpan source, int sourceOffset, Span destination, int destinationOffset, int count) { source.Slice(sourceOffset, count).CopyTo(destination.Slice(destinationOffset)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint ToUInt32(ReadOnlySpan input, int offset) { return BinaryPrimitives.ReadUInt32LittleEndian(input.Slice(offset)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static ulong ToUInt64(ReadOnlySpan input, int offset) { return BinaryPrimitives.ReadUInt64LittleEndian(input.Slice(offset)); diff --git a/AssetRipper.TextureDecoder/Etc/EtcDecoder.cs b/AssetRipper.TextureDecoder/Etc/EtcDecoder.cs index 3a445b5..f11c20f 100644 --- a/AssetRipper.TextureDecoder/Etc/EtcDecoder.cs +++ b/AssetRipper.TextureDecoder/Etc/EtcDecoder.cs @@ -640,7 +640,7 @@ namespace AssetRipper.TextureDecoder.Etc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void DecodeEtc2a1Block(ReadOnlySpan input, Span output) { if ((input[3] & 2) != 0) @@ -856,7 +856,7 @@ namespace AssetRipper.TextureDecoder.Etc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void DecodeEac11Block(Span output, int @base) { for (int i = 0; i < 16; i++) @@ -865,7 +865,7 @@ namespace AssetRipper.TextureDecoder.Etc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void DecodeEac11Block(Span output, int @base, int ti, int mul, ulong l) { ReadOnlySpan table = Etc2AlphaModTable.AsSpan(ti * 8, 8); @@ -876,37 +876,37 @@ namespace AssetRipper.TextureDecoder.Etc } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint Color(int r, int g, int b, int a) { return unchecked((uint)(r << 16 | g << 8 | b | a << 24)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static byte Clamp255(int n) { return n < 0 ? (byte)0 : n > 255 ? (byte)255 : unchecked((byte)n); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint ApplicateColor(ReadOnlySpan c, int o, int m) { return Color(Clamp255(c[o * 3 + 0] + m), Clamp255(c[o * 3 + 1] + m), Clamp255(c[o * 3 + 2] + m), 255); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint ApplicateColor(ReadOnlySpan c, int o, int m) { return Color(Clamp255(c[o * 3 + 0] + m), Clamp255(c[o * 3 + 1] + m), Clamp255(c[o * 3 + 2] + m), 255); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint ApplicateColorRaw(ReadOnlySpan c, int o) { return Color(c[o * 3 + 0], c[o * 3 + 1], c[o * 3 + 2], 255); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static ulong Get6SwapedBytes(ReadOnlySpan data) { return data[7] | (uint)data[6] << 8 | (uint)data[5] << 16 | (uint)data[4] << 24 | (ulong)data[3] << 32 | (ulong)data[2] << 40; diff --git a/AssetRipper.TextureDecoder/OptimizationConstants.cs b/AssetRipper.TextureDecoder/OptimizationConstants.cs deleted file mode 100644 index edb3e56..0000000 --- a/AssetRipper.TextureDecoder/OptimizationConstants.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace AssetRipper.TextureDecoder -{ - internal static class OptimizationConstants - { -#if NETCOREAPP - internal const MethodImplOptions AggressiveInliningAndOptimization = MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization; -#else - internal const MethodImplOptions AggressiveInliningAndOptimization = MethodImplOptions.AggressiveInlining; -#endif - } -} diff --git a/AssetRipper.TextureDecoder/Pvrtc/PvrtcDecoder.cs b/AssetRipper.TextureDecoder/Pvrtc/PvrtcDecoder.cs index 1f4bf70..696d852 100644 --- a/AssetRipper.TextureDecoder/Pvrtc/PvrtcDecoder.cs +++ b/AssetRipper.TextureDecoder/Pvrtc/PvrtcDecoder.cs @@ -16,7 +16,7 @@ namespace AssetRipper.TextureDecoder.Pvrtc /// The decompressed texture data /// Signifies whether the data is PVRTC2 or PVRTC4 /// The number of bytes read from - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int DecompressPVRTC(ReadOnlySpan input, int xDim, int yDim, bool do2bitMode, out byte[] output) { output = new byte[xDim * yDim * sizeof(uint)]; @@ -32,7 +32,7 @@ namespace AssetRipper.TextureDecoder.Pvrtc /// The decompressed texture data /// Signifies whether the data is PVRTC2 or PVRTC4 /// The number of bytes read from - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int DecompressPVRTC(ReadOnlySpan input, int xDim, int yDim, bool do2bitMode, Span output) { int xBlockSize = do2bitMode ? BlockX2bpp : BlockX4bpp; @@ -455,7 +455,7 @@ namespace AssetRipper.TextureDecoder.Pvrtc } } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static Span GetIntSpanForColor(this Span colorSpan, int index) { return MemoryMarshal.Cast(colorSpan.Slice(index, 1)); @@ -466,7 +466,7 @@ namespace AssetRipper.TextureDecoder.Pvrtc /// /// A number /// True if the number is an integer power of two, else false - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static bool IsPowerOf2(uint input) { if (input == 0) @@ -481,7 +481,7 @@ namespace AssetRipper.TextureDecoder.Pvrtc /// /// Define an expression to either wrap or clamp large or small vals to the legal coordinate range /// - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static int LimitCoord(int value, int size) { #if ASSUME_IMAGE_TILING diff --git a/AssetRipper.TextureDecoder/Rgb/DefaultColorMethods.cs b/AssetRipper.TextureDecoder/Rgb/DefaultColorMethods.cs index 777808e..5efd3e0 100644 --- a/AssetRipper.TextureDecoder/Rgb/DefaultColorMethods.cs +++ b/AssetRipper.TextureDecoder/Rgb/DefaultColorMethods.cs @@ -2,7 +2,7 @@ { internal static class DefaultColorMethods { - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] internal static void GetChannels(T color, out TArg r, out TArg g, out TArg b, out TArg a) where TArg : unmanaged where T : unmanaged, IColor @@ -13,7 +13,7 @@ a = color.A; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] internal static void SetChannels(ref T color, TArg r, TArg g, TArg b, TArg a) where TArg : unmanaged where T : unmanaged, IColor diff --git a/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB9e5.cs b/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB9e5.cs index 79aeac1..5b3a76a 100644 --- a/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB9e5.cs +++ b/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB9e5.cs @@ -43,7 +43,7 @@ set { } } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public readonly void GetChannels(out double r, out double g, out double b, out double a) { double scale = Scale; @@ -53,13 +53,13 @@ a = 1; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public void SetChannels(double r, double g, double b, double a) { SetChannels(r, g, b); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public void SetChannels(double r, double g, double b) { int exponent = CalculateExponent(r, g, b); @@ -80,7 +80,7 @@ private readonly uint GBits => (bits >> GreenOffset) & ChannelBitMask; private readonly uint BBits => (bits >> BlueOffset) & ChannelBitMask; - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static int CalculateExponent(double r, double g, double b) { double maxChannel = double.Max(r, double.Max(g, b)); diff --git a/AssetRipper.TextureDecoder/Rgb/IColor.cs b/AssetRipper.TextureDecoder/Rgb/IColor.cs index 0c6de21..f1dcf28 100644 --- a/AssetRipper.TextureDecoder/Rgb/IColor.cs +++ b/AssetRipper.TextureDecoder/Rgb/IColor.cs @@ -68,7 +68,7 @@ public static class ColorExtensions { - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] internal static void SetConvertedChannels(this ref TThis color, TSourceArg r, TSourceArg g, TSourceArg b, TSourceArg a) where TThisArg : unmanaged where TSourceArg : unmanaged @@ -81,7 +81,7 @@ NumericConversion.Convert(a)); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static TTarget Convert(this TThis color) where TThisArg : unmanaged where TTargetArg : unmanaged diff --git a/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs b/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs index 45b1908..65beb9a 100644 --- a/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs +++ b/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs @@ -25,8 +25,9 @@ public static partial class NumericConversion #else return default; //exceptions prevent inlining #endif - } - + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static TTo ToSignedNumber(TFrom value) where TFrom : unmanaged, IUnsignedNumber, IShiftOperators, IBitwiseOperators where TTo : unmanaged, ISignedNumber @@ -39,8 +40,9 @@ public static partial class NumericConversion TFrom SignBit = TFrom.One << (Unsafe.SizeOf() * 8 - 1); TFrom converted = (SignBit ^ value); return Unsafe.As(ref converted); - } - + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static TTo ToUnsignedNumber(TFrom value) where TFrom : unmanaged, ISignedNumber where TTo : unmanaged, IUnsignedNumber, IShiftOperators, IBitwiseOperators diff --git a/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs b/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs index efc3b00..d8247d6 100644 --- a/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs +++ b/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs @@ -5,14 +5,14 @@ namespace AssetRipper.TextureDecoder.Rgb { public static class RgbConverter { - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int A8ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return A8ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int A8ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -32,14 +32,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB24ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGB24ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB24ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -59,14 +59,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBA32ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGBA32ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBA32ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -86,14 +86,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int ARGB32ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return ARGB32ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int ARGB32ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -113,14 +113,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB16ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGB16ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB16ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -143,14 +143,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RG16ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RG16ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RG16ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -170,14 +170,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int R8ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return R8ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int R8ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -197,14 +197,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RHalfToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RHalfToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RHalfToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -225,14 +225,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGHalfToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGHalfToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGHalfToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -254,14 +254,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBAHalfToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGBAHalfToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBAHalfToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -285,14 +285,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RFloatToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RFloatToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RFloatToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -313,14 +313,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGFloatToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGFloatToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGFloatToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -342,14 +342,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBAFloatToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGBAFloatToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBAFloatToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -373,14 +373,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB9e5FloatToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGB9e5FloatToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB9e5FloatToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -405,14 +405,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RG32ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RG32ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RG32ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -432,14 +432,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB48ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGB48ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGB48ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -459,14 +459,14 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBA64ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * 4]; return RGBA64ToBGRA32(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int RGBA64ToBGRA32(ReadOnlySpan input, int width, int height, Span output) { int io = 0; @@ -486,7 +486,7 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int Convert(ReadOnlySpan input, int width, int height, out byte[] output) where TSourceArg : unmanaged where TSource : unmanaged, IColor @@ -497,7 +497,7 @@ namespace AssetRipper.TextureDecoder.Rgb return Convert(input, width, height, output); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int Convert(ReadOnlySpan input, int width, int height, Span output) where TSourceArg : unmanaged where TSource : unmanaged, IColor @@ -510,7 +510,7 @@ namespace AssetRipper.TextureDecoder.Rgb return width * height * Unsafe.SizeOf(); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static void Convert(ReadOnlySpan sourceSpan, Span destinationSpan) where TSourceArg : unmanaged where TSource : unmanaged, IColor @@ -523,35 +523,25 @@ namespace AssetRipper.TextureDecoder.Rgb } } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static float ToHalf(ReadOnlySpan input, int offset) { -#if NET6_0_OR_GREATER return (float)BinaryPrimitives.ReadHalfLittleEndian(input.Slice(offset, 2)); -#else - ushort bits = BinaryPrimitives.ReadUInt16LittleEndian(input.Slice(offset, 2)); - return (float)Unsafe.As(ref bits); -#endif } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static float ToSingle(ReadOnlySpan input, int offset) { -#if NET5_0_OR_GREATER return BinaryPrimitives.ReadSingleLittleEndian(input.Slice(offset, 4)); -#else - uint bits = BinaryPrimitives.ReadUInt32LittleEndian(input.Slice(offset, 4)); - return Unsafe.As(ref bits); -#endif } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static byte ClampByte(float x) { return byte.MaxValue < x ? byte.MaxValue : (x > byte.MinValue ? (byte)x : byte.MinValue); } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static byte ClampByte(double x) { return byte.MaxValue < x ? byte.MaxValue : (x > byte.MinValue ? (byte)x : byte.MinValue); diff --git a/AssetRipper.TextureDecoder/Rgb/UInt4Pair.cs b/AssetRipper.TextureDecoder/Rgb/UInt4Pair.cs index 33aacb3..27cceef 100644 --- a/AssetRipper.TextureDecoder/Rgb/UInt4Pair.cs +++ b/AssetRipper.TextureDecoder/Rgb/UInt4Pair.cs @@ -54,7 +54,7 @@ } } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static uint Convert8BitsTo4Bits(byte value) { //See https://github.com/AssetRipper/TextureDecoder/issues/19 diff --git a/AssetRipper.TextureDecoder/Yuy2/Yuy2Decoder.cs b/AssetRipper.TextureDecoder/Yuy2/Yuy2Decoder.cs index 256f5d2..9e0db25 100644 --- a/AssetRipper.TextureDecoder/Yuy2/Yuy2Decoder.cs +++ b/AssetRipper.TextureDecoder/Yuy2/Yuy2Decoder.cs @@ -13,7 +13,7 @@ namespace AssetRipper.TextureDecoder.Yuy2 /// Pixel height of the image. /// An output buffer of size 4 * width * height. /// Number of bytes read from - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int DecompressYUY2(ReadOnlySpan input, int width, int height, out byte[] output) { output = new byte[width * height * sizeof(uint)]; @@ -28,7 +28,7 @@ namespace AssetRipper.TextureDecoder.Yuy2 /// Pixel height of the image. /// An output buffer. Must be at least 4 * width * height. /// Number of bytes read from - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int DecompressYUY2(ReadOnlySpan input, int width, int height, Span output) { ThrowHelper.ThrowIfNotEnoughSpace(output, width, height); @@ -72,7 +72,7 @@ namespace AssetRipper.TextureDecoder.Yuy2 /// Pixel height of the image. /// An output buffer. Must be at least width * height * pixelSize. /// Number of bytes read from - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int DecompressYUY2(ReadOnlySpan input, int width, int height, out byte[] output) where TOutputArg : unmanaged where TOutput : unmanaged, IColor @@ -91,7 +91,7 @@ namespace AssetRipper.TextureDecoder.Yuy2 /// Pixel height of the image. /// An output buffer. Must be at least width * height * pixelSize. /// Number of bytes read from - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int DecompressYUY2(ReadOnlySpan input, int width, int height, Span output) where TOutputArg : unmanaged where TOutput : unmanaged, IColor @@ -109,7 +109,7 @@ namespace AssetRipper.TextureDecoder.Yuy2 /// Pixel height of the image. /// An output buffer. Must be at least width * height. /// Number of bytes read from - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int DecompressYUY2(ReadOnlySpan input, int width, int height, Span output) where TOutputArg : unmanaged where TOutput : unmanaged, IColor @@ -145,7 +145,7 @@ namespace AssetRipper.TextureDecoder.Yuy2 return p; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static byte ClampByte(int x) { return (byte)(byte.MaxValue < x ? byte.MaxValue : (x > byte.MinValue ? x : byte.MinValue));