diff --git a/AssetRipper.TextureDecoder.ColorGenerator/Program.cs b/AssetRipper.TextureDecoder.ColorGenerator/Program.cs index 162425e..dc37c83 100644 --- a/AssetRipper.TextureDecoder.ColorGenerator/Program.cs +++ b/AssetRipper.TextureDecoder.ColorGenerator/Program.cs @@ -1,8 +1,8 @@ using AssetRipper.Text.SourceGeneration; -using AssetRipper.TextureDecoder.SourceGeneration.Common; +using AssetRipper.TextureDecoder.SourceGeneration.Common; using System.CodeDom.Compiler; -using System.Text; - +using System.Text; + namespace AssetRipper.TextureDecoder.ColorGenerator; internal static partial class Program @@ -22,7 +22,6 @@ internal static partial class Program ( "ColorRGB16", typeof(byte), true, true, true, false, false ), ( "ColorRGB9e5", typeof(double), true, true, true, false, false ), ( "ColorRGBA16", typeof(byte), true, true, true, true, false ), - ( "ColorRGB32Half", typeof(Half), true, true, true, false, false ), }; /// @@ -97,43 +96,43 @@ internal static partial class Program { writer.WriteLine("public override string ToString()"); using (new CurlyBrackets(writer)) - { - StringBuilder sb = new(); - sb.Append("return $\"{{ "); - bool first = true; - if (hasRed) - { - sb.Append("R: {R}"); - first = false; - } - if (hasGreen) - { - if (!first) - { - sb.Append(", "); - } - sb.Append("G: {G}"); - first = false; - } - if (hasBlue) - { - if (!first) - { - sb.Append(", "); - } - sb.Append("B: {B}"); - first = false; - } - if (hasAlpha) - { - if (!first) - { - sb.Append(", "); - } - sb.Append("A: {A}"); - } - sb.Append(" }}\";"); - writer.WriteLine(sb.ToString()); + { + StringBuilder sb = new(); + sb.Append("return $\"{{ "); + bool first = true; + if (hasRed) + { + sb.Append("R: {R}"); + first = false; + } + if (hasGreen) + { + if (!first) + { + sb.Append(", "); + } + sb.Append("G: {G}"); + first = false; + } + if (hasBlue) + { + if (!first) + { + sb.Append(", "); + } + sb.Append("B: {B}"); + first = false; + } + if (hasAlpha) + { + if (!first) + { + sb.Append(", "); + } + sb.Append("A: {A}"); + } + sb.Append(" }}\";"); + writer.WriteLine(sb.ToString()); } } @@ -180,8 +179,8 @@ internal static partial class Program writer.WriteLineNoTabs(); WriteSetChannels(writer, typeName, hasRed, hasGreen, hasBlue, hasAlpha); writer.WriteLineNoTabs(); - WriteHasChannelStaticProperties(writer, hasRed, hasGreen, hasBlue, hasAlpha, typeName); - writer.WriteLineNoTabs(); + WriteHasChannelStaticProperties(writer, hasRed, hasGreen, hasBlue, hasAlpha, typeName); + writer.WriteLineNoTabs(); WriteToString(writer, hasRed, hasGreen, hasBlue, hasAlpha); } } diff --git a/AssetRipper.TextureDecoder.ConsoleApp/Program.g.cs b/AssetRipper.TextureDecoder.ConsoleApp/Program.g.cs index af67f6b..25524c2 100644 --- a/AssetRipper.TextureDecoder.ConsoleApp/Program.g.cs +++ b/AssetRipper.TextureDecoder.ConsoleApp/Program.g.cs @@ -73,9 +73,8 @@ static partial class Program Console.WriteLine(" 61 - ARGB32"); Console.WriteLine(" 62 - BGRA32"); Console.WriteLine(" 63 - RGB16"); - Console.WriteLine(" 64 - RGB32Half"); - Console.WriteLine(" 65 - RGB9e5"); - Console.WriteLine(" 66 - RGBA16"); + Console.WriteLine(" 64 - RGB9e5"); + Console.WriteLine(" 65 - RGBA16"); int mode = int.Parse(modeString); switch(mode) { @@ -272,12 +271,9 @@ static partial class Program RgbConverter.Convert(input, width, height, output); break; case 64: - RgbConverter.Convert(input, width, height, output); - break; - case 65: RgbConverter.Convert(input, width, height, output); break; - case 66: + case 65: RgbConverter.Convert(input, width, height, output); break; default: diff --git a/AssetRipper.TextureDecoder.SourceGeneration.Common/CSharpPrimitives.cs b/AssetRipper.TextureDecoder.SourceGeneration.Common/CSharpPrimitives.cs index 1fb2ac5..9f965c4 100644 --- a/AssetRipper.TextureDecoder.SourceGeneration.Common/CSharpPrimitives.cs +++ b/AssetRipper.TextureDecoder.SourceGeneration.Common/CSharpPrimitives.cs @@ -1,4 +1,4 @@ -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace AssetRipper.TextureDecoder.SourceGeneration.Common; diff --git a/AssetRipper.TextureDecoder.TestGenerator/Program.cs b/AssetRipper.TextureDecoder.TestGenerator/Program.cs index 7f1ceb2..c25b9d3 100644 --- a/AssetRipper.TextureDecoder.TestGenerator/Program.cs +++ b/AssetRipper.TextureDecoder.TestGenerator/Program.cs @@ -1,7 +1,7 @@ using AssetRipper.Text.SourceGeneration; using AssetRipper.TextureDecoder.Rgb; using AssetRipper.TextureDecoder.Rgb.Formats; -using AssetRipper.TextureDecoder.SourceGeneration.Common; +using AssetRipper.TextureDecoder.SourceGeneration.Common; using System.CodeDom.Compiler; namespace AssetRipper.TextureDecoder.TestGenerator @@ -14,7 +14,6 @@ namespace AssetRipper.TextureDecoder.TestGenerator new GenerationData(typeof(ColorARGB32), 4), new GenerationData(typeof(ColorBGRA32), 4), new GenerationData(typeof(ColorRGB16), 2), - new GenerationData(typeof(ColorRGB32Half), 4), new GenerationData(typeof(ColorRGB9e5), 4), new GenerationData(typeof(ColorRGBA16), 2), }; @@ -39,57 +38,57 @@ namespace AssetRipper.TextureDecoder.TestGenerator writer.WriteFileScopedNamespace("AssetRipper.TextureDecoder.ConsoleApp"); writer.WriteLine("static partial class Program"); - using (new CurlyBrackets(writer)) - { - writer.WriteLine("private static void DecodeRgb(ReadOnlySpan input, int width, int height, string modeString, Span output)"); - using (new CurlyBrackets(writer)) - { - List<(string, string, string)> list = GetColorNames().ToList(); - writer.WriteLine("Console.WriteLine(\"Arg at index 5 : mode\");"); - for (int i = 0; i < list.Count; i++) - { - writer.WriteLine($"Console.WriteLine(\" {i} - {list[i].Item1.Substring(5)}\");"); - } - writer.WriteLine("int mode = int.Parse(modeString);"); - writer.WriteLine("switch(mode)"); - using (new CurlyBrackets(writer)) - { - for (int i = 0 ; i < list.Count; i++) - { - (string prettyName, string colorName, string channelName) = list[i]; - writer.WriteLine($"case {i}:"); - using (new IndentedBlock(writer)) - { - writer.WriteLine($"RgbConverter.Convert<{colorName}, {channelName}, ColorBGRA32, byte>(input, width, height, output);"); - writer.WriteLine("break;"); - } - } - { - writer.WriteLine("default:"); - using (new IndentedBlock(writer)) - { - writer.WriteLine("throw new NotSupportedException(mode.ToString());"); - } - } - } - } + using (new CurlyBrackets(writer)) + { + writer.WriteLine("private static void DecodeRgb(ReadOnlySpan input, int width, int height, string modeString, Span output)"); + using (new CurlyBrackets(writer)) + { + List<(string, string, string)> list = GetColorNames().ToList(); + writer.WriteLine("Console.WriteLine(\"Arg at index 5 : mode\");"); + for (int i = 0; i < list.Count; i++) + { + writer.WriteLine($"Console.WriteLine(\" {i} - {list[i].Item1.Substring(5)}\");"); + } + writer.WriteLine("int mode = int.Parse(modeString);"); + writer.WriteLine("switch(mode)"); + using (new CurlyBrackets(writer)) + { + for (int i = 0 ; i < list.Count; i++) + { + (string prettyName, string colorName, string channelName) = list[i]; + writer.WriteLine($"case {i}:"); + using (new IndentedBlock(writer)) + { + writer.WriteLine($"RgbConverter.Convert<{colorName}, {channelName}, ColorBGRA32, byte>(input, width, height, output);"); + writer.WriteLine("break;"); + } + } + { + writer.WriteLine("default:"); + using (new IndentedBlock(writer)) + { + writer.WriteLine("throw new NotSupportedException(mode.ToString());"); + } + } + } + } } - static IEnumerable<(string, string, string)> GetColorNames() - { - foreach ((Type primitiveType, string primitiveName) in CSharpPrimitives.TypeNames) - { - int primitiveBitSize = CSharpPrimitives.Sizes[primitiveType] * 8; - foreach (string genericColorName in TestClassGenerator.GetGenericColorNames()) - { - string prettyName = $"{genericColorName}{primitiveBitSize}{primitiveType.Name}"; - yield return (prettyName, $"{genericColorName}<{primitiveName}>", primitiveName); - } - } - foreach (GenerationData data in dataList) - { - yield return (data.ColorType.Name, data.ColorType.Name, data.ChannelTypeName); - } + static IEnumerable<(string, string, string)> GetColorNames() + { + foreach ((Type primitiveType, string primitiveName) in CSharpPrimitives.TypeNames) + { + int primitiveBitSize = CSharpPrimitives.Sizes[primitiveType] * 8; + foreach (string genericColorName in TestClassGenerator.GetGenericColorNames()) + { + string prettyName = $"{genericColorName}{primitiveBitSize}{primitiveType.Name}"; + yield return (prettyName, $"{genericColorName}<{primitiveName}>", primitiveName); + } + } + foreach (GenerationData data in dataList) + { + yield return (data.ColorType.Name, data.ColorType.Name, data.ChannelTypeName); + } } } } diff --git a/AssetRipper.TextureDecoder.Tests/Formats/ColorRGB32HalfTests.cs b/AssetRipper.TextureDecoder.Tests/Formats/ColorRGB32HalfTests.cs deleted file mode 100644 index 992e7db..0000000 --- a/AssetRipper.TextureDecoder.Tests/Formats/ColorRGB32HalfTests.cs +++ /dev/null @@ -1,21 +0,0 @@ -using AssetRipper.TextureDecoder.Rgb.Formats; -using System.Runtime.CompilerServices; - -namespace AssetRipper.TextureDecoder.Tests.Formats; - -[Ignore($"Ignore. Bugs in {nameof(ColorRGB32Half)}")] -public partial class ColorRGB32HalfTests -{ - [Test] - public void MaxValueIsOne() - { - Assert.Multiple(() => - { - ColorRGB32Half color = Unsafe.As(ref Unsafe.AsRef(uint.MaxValue)); - Assert.That(color.R, Is.EqualTo(Half.One), "R"); - Assert.That(color.G, Is.EqualTo(Half.One), "G"); - Assert.That(color.B, Is.EqualTo(Half.One), "B"); - Assert.That(color.A, Is.EqualTo(Half.One), "A"); - }); - } -} diff --git a/AssetRipper.TextureDecoder.Tests/Formats/ColorRGB32HalfTests.g.cs b/AssetRipper.TextureDecoder.Tests/Formats/ColorRGB32HalfTests.g.cs deleted file mode 100644 index 4023c80..0000000 --- a/AssetRipper.TextureDecoder.Tests/Formats/ColorRGB32HalfTests.g.cs +++ /dev/null @@ -1,184 +0,0 @@ -// Auto-generated code. Do not modify manually. - -using AssetRipper.TextureDecoder.Rgb.Formats; -using System.Runtime.CompilerServices; - -namespace AssetRipper.TextureDecoder.Tests.Formats; - -public partial class ColorRGB32HalfTests -{ - [Test] - public void CorrectSizeTest() - { - Assert.That(Unsafe.SizeOf(), Is.EqualTo(4)); - } - - [Test] - public void PropertyIsSymmetric_R() - { - var color = MakeRandomColor(); - var r = color.R; - color.R = r; - Assert.That(color.R, Is.EqualTo(r)); - } - - [Test] - public void PropertyIsSymmetric_G() - { - var color = MakeRandomColor(); - var g = color.G; - color.G = g; - Assert.That(color.G, Is.EqualTo(g)); - } - - [Test] - public void PropertyIsSymmetric_B() - { - var color = MakeRandomColor(); - var b = color.B; - color.B = b; - Assert.That(color.B, Is.EqualTo(b)); - } - - [Test] - public void PropertyIsSymmetric_A() - { - var color = MakeRandomColor(); - var a = color.A; - color.A = a; - Assert.That(color.A, Is.EqualTo(a)); - } - - [Test] - public void ChannelsAreIndependent_R() - { - var color = MakeRandomColor(); - var g = color.G; - var b = color.B; - var a = color.A; - color.R = MakeRandomValue(); - Assert.Multiple(() => - { - Assert.That(color.G, Is.EqualTo(g)); - Assert.That(color.B, Is.EqualTo(b)); - Assert.That(color.A, Is.EqualTo(a)); - }); - } - - [Test] - public void ChannelsAreIndependent_G() - { - var color = MakeRandomColor(); - var r = color.R; - var b = color.B; - var a = color.A; - color.G = MakeRandomValue(); - Assert.Multiple(() => - { - Assert.That(color.R, Is.EqualTo(r)); - Assert.That(color.B, Is.EqualTo(b)); - Assert.That(color.A, Is.EqualTo(a)); - }); - } - - [Test] - public void ChannelsAreIndependent_B() - { - var color = MakeRandomColor(); - var r = color.R; - var g = color.G; - var a = color.A; - color.B = MakeRandomValue(); - Assert.Multiple(() => - { - Assert.That(color.R, Is.EqualTo(r)); - Assert.That(color.G, Is.EqualTo(g)); - Assert.That(color.A, Is.EqualTo(a)); - }); - } - - [Test] - public void ChannelsAreIndependent_A() - { - var color = MakeRandomColor(); - var r = color.R; - var g = color.G; - var b = color.B; - color.A = MakeRandomValue(); - Assert.Multiple(() => - { - Assert.That(color.R, Is.EqualTo(r)); - Assert.That(color.G, Is.EqualTo(g)); - Assert.That(color.B, Is.EqualTo(b)); - }); - } - - [Test] - public void GetMethodMatchesProperties() - { - var color = MakeRandomColor(); - color.GetChannels(out var r, out var g, out var b, out var a); - Assert.Multiple(() => - { - Assert.That(color.R, Is.EqualTo(r)); - Assert.That(color.G, Is.EqualTo(g)); - Assert.That(color.B, Is.EqualTo(b)); - Assert.That(color.A, Is.EqualTo(a)); - }); - } - - [Test] - public void MethodsAreSymmetric() - { - var color = MakeRandomColor(); - color.GetChannels(out var r, out var g, out var b, out var a); - color.SetChannels(r, g, b, a); - Assert.Multiple(() => - { - Assert.That(color.R, Is.EqualTo(r)); - Assert.That(color.G, Is.EqualTo(g)); - Assert.That(color.B, Is.EqualTo(b)); - Assert.That(color.A, Is.EqualTo(a)); - }); - } - - public static ColorRGB32Half MakeRandomColor() => ColorRandom.MakeRandomColor(); - - public static Half MakeRandomValue() => ColorRandom.MakeRandomValue(); - - [Test] - public void ConversionIsLosslessToColorRGBA_Half() - { - LosslessConversion.Assert, Half>(); - } - - [Test] - public void ConversionIsLosslessToColorRGB_Half() - { - LosslessConversion.Assert, Half>(); - } - - [Test] - public void ConversionIsLosslessToColorRGBA_float() - { - LosslessConversion.Assert, float>(); - } - - [Test] - public void ConversionIsLosslessToColorRGB_float() - { - LosslessConversion.Assert, float>(); - } - - [Test] - public void ConversionIsLosslessToColorRGBA_double() - { - LosslessConversion.Assert, double>(); - } - - [Test] - public void ConversionIsLosslessToColorRGB_double() - { - LosslessConversion.Assert, double>(); - } -} diff --git a/AssetRipper.TextureDecoder.Tests/NewRgbTests.cs b/AssetRipper.TextureDecoder.Tests/NewRgbTests.cs index f823a19..5621482 100644 --- a/AssetRipper.TextureDecoder.Tests/NewRgbTests.cs +++ b/AssetRipper.TextureDecoder.Tests/NewRgbTests.cs @@ -167,21 +167,6 @@ namespace AssetRipper.TextureDecoder.Tests Assert.That(originalDecodedData, Is.EqualTo(newDecodedData)); } - [Test] - public void ConvertRGB32HalfTest() - { - ReadOnlySpan data = File.ReadAllBytes(PathConstants.RgbTestFilesFolder + "test.rgb24"); - RgbConverter.Convert, byte, ColorRGB32Half, Half>(data, 256, 256, out var halfData); - int legacyBytesRead = RgbConverter.R11G11B10FloatToBGRA32(halfData, 256, 256, out var legacyRgbData); - int bytesRead = RgbConverter.Convert(halfData, 256, 256, out var rgbData); - Assert.Multiple(() => - { - Assert.That(bytesRead, Is.EqualTo(halfData.Length)); - Assert.That(legacyBytesRead, Is.EqualTo(halfData.Length)); - Assert.That(rgbData, Is.EqualTo(legacyRgbData)); - }); - } - [Test] public void ConvertRGBAHalfTest() { diff --git a/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB32Half.cs b/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB32Half.cs deleted file mode 100644 index bbb4b34..0000000 --- a/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB32Half.cs +++ /dev/null @@ -1,74 +0,0 @@ -namespace AssetRipper.TextureDecoder.Rgb.Formats -{ - /// - /// Also called R11G11B10_FLOAT - /// - /// - /// - /// - public partial struct ColorRGB32Half : IColor - { - private uint bits; - - /// - /// 11 bits - /// - public Half R - { - readonly get - { - ushort value = (ushort)((bits << 4) & 0x7FF0u); - return ToHalf(value); - } - set => bits = (bits & ~0x7FFu) | (ToUInt32(value) & 0x7FF0u) >> 4; - } - - /// - /// 11 bits - /// - public Half G - { - readonly get - { - ushort value = (ushort)((bits >> 7) & 0x7FF0); - return ToHalf(value); - } - set => bits = (bits & ~0x3FF800u) | ((ToUInt32(value) >> 4) & 0x7FF0u) << 11; - } - - /// - /// 10 bits - /// - public Half B - { - readonly get - { - ushort value = (ushort)((bits >> 17) & 0x7FE0); - return ToHalf(value); - } - set => bits = (bits & ~0xFFC00000u) | (((ToUInt32(value) >> 5) & 0x3FFu) << 22); - } - - public readonly Half A - { - get => Half.One; - set { } - } - - public readonly void GetChannels(out Half r, out Half g, out Half b, out Half a) - { - DefaultColorMethods.GetChannels(this, out r, out g, out b, out a); - } - - public void SetChannels(Half r, Half g, Half b, Half a) - { - bits = ((ToUInt32(r) >> 4) & 0x7FF) | (((ToUInt32(g) >> 4) & 0x7FF) << 11) | (((ToUInt32(b) >> 5) & 0x3FF) << 22); - } - - private static Half ToHalf(ushort value) => Unsafe.As(ref Unsafe.AsRef(value)); - - private static ushort ToUInt16(Half value) => Unsafe.As(ref Unsafe.AsRef(value)); - - private static uint ToUInt32(Half value) => ToUInt16(value); - } -} diff --git a/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB32Half.g.cs b/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB32Half.g.cs deleted file mode 100644 index fed3357..0000000 --- a/AssetRipper.TextureDecoder/Rgb/Formats/ColorRGB32Half.g.cs +++ /dev/null @@ -1,20 +0,0 @@ -//This code is source generated. Do not edit manually. - -using AssetRipper.TextureDecoder.Attributes; - -namespace AssetRipper.TextureDecoder.Rgb.Formats -{ - [RgbaAttribute(RedChannel = true, GreenChannel = true, BlueChannel = true, AlphaChannel = false, FullyUtilizedChannels = false)] - public partial struct ColorRGB32Half : IColor - { - static bool IColor.HasRedChannel => true; - static bool IColor.HasGreenChannel => true; - static bool IColor.HasBlueChannel => true; - static bool IColor.HasAlphaChannel => false; - - public override string ToString() - { - return $"{{ R: {R}, G: {G}, B: {B} }}"; - } - } -} diff --git a/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs b/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs index 937866a..efc3b00 100644 --- a/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs +++ b/AssetRipper.TextureDecoder/Rgb/RgbConverter.cs @@ -405,38 +405,6 @@ namespace AssetRipper.TextureDecoder.Rgb return io; } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] - public static int R11G11B10FloatToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) - { - output = new byte[width * height * 4]; - return R11G11B10FloatToBGRA32(input, width, height, output); - } - - // reference: https://github.com/microsoft/DirectX-Graphics-Samples/blob/e5ea2ac7430ce39e6f6d619fd85ae32581931589/MiniEngine/Core/Shaders/PixelPacking_R11G11B10.hlsli#L31-L37 - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] - public static int R11G11B10FloatToBGRA32(ReadOnlySpan input, int width, int height, Span output) - { - int io = 0; - int oo = 0; - for (int i = 0; i < width; i++) - { - for (int j = 0; j < height; j++) - { - uint value = BinaryPrimitives.ReadUInt32LittleEndian(input.Slice(io, 4)); - ushort r = (ushort)((value << 4) & 0x7FF0); - ushort g = (ushort)((value >> 7) & 0x7FF0); - ushort b = (ushort)((value >> 17) & 0x7FE0); - output[oo + 0] = ClampByte((float) Unsafe.As(ref b) * 255f); // b - output[oo + 1] = ClampByte((float) Unsafe.As(ref g) * 255f); // g - output[oo + 2] = ClampByte((float) Unsafe.As(ref r) * 255f); // r - output[oo + 3] = 255; // a - io += 4; - oo += 4; - } - } - return io; - } - [MethodImpl(OptimizationConstants.AggressiveInliningAndOptimization)] public static int RG32ToBGRA32(ReadOnlySpan input, int width, int height, out byte[] output) {