diff options
Diffstat (limited to 'Ryujinx.Graphics/Texture')
| -rw-r--r-- | Ryujinx.Graphics/Texture/ASTCDecoder.cs | 1384 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/ASTCPixel.cs | 138 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/BitArrayStream.cs | 121 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs | 59 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/ISwizzle.cs | 7 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/ImageConverter.cs | 24 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/ImageUtils.cs | 357 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/IntegerEncoded.cs | 269 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/LinearSwizzle.cs | 19 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/TextureFactory.cs | 132 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/TextureHelper.cs | 45 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/TextureInfo.cs | 60 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/TextureReader.cs | 398 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/TextureSwizzle.cs | 11 | ||||
| -rw-r--r-- | Ryujinx.Graphics/Texture/TextureWriter.cs | 35 |
15 files changed, 3059 insertions, 0 deletions
diff --git a/Ryujinx.Graphics/Texture/ASTCDecoder.cs b/Ryujinx.Graphics/Texture/ASTCDecoder.cs new file mode 100644 index 00000000..1efa0255 --- /dev/null +++ b/Ryujinx.Graphics/Texture/ASTCDecoder.cs @@ -0,0 +1,1384 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; + +namespace Ryujinx.Graphics.Texture +{ + public class ASTCDecoderException : Exception + { + public ASTCDecoderException(string ExMsg) : base(ExMsg) { } + } + + //https://github.com/GammaUNC/FasTC/blob/master/ASTCEncoder/src/Decompressor.cpp + public static class ASTCDecoder + { + struct TexelWeightParams + { + public int Width; + public int Height; + public bool DualPlane; + public int MaxWeight; + public bool Error; + public bool VoidExtentLDR; + public bool VoidExtentHDR; + + public int GetPackedBitSize() + { + // How many indices do we have? + int Indices = Height * Width; + + if (DualPlane) + { + Indices *= 2; + } + + IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(MaxWeight); + + return IntEncoded.GetBitLength(Indices); + } + + public int GetNumWeightValues() + { + int Ret = Width * Height; + + if (DualPlane) + { + Ret *= 2; + } + + return Ret; + } + } + + public static byte[] DecodeToRGBA8888( + byte[] InputBuffer, + int BlockX, + int BlockY, + int BlockZ, + int X, + int Y, + int Z) + { + using (MemoryStream InputStream = new MemoryStream(InputBuffer)) + { + BinaryReader BinReader = new BinaryReader(InputStream); + + if (BlockX > 12 || BlockY > 12) + { + throw new ASTCDecoderException("Block size unsupported!"); + } + + if (BlockZ != 1 || Z != 1) + { + throw new ASTCDecoderException("3D compressed textures unsupported!"); + } + + using (MemoryStream OutputStream = new MemoryStream()) + { + int BlockIndex = 0; + + for (int j = 0; j < Y; j += BlockY) + { + for (int i = 0; i < X; i += BlockX) + { + int[] DecompressedData = new int[144]; + + DecompressBlock(BinReader.ReadBytes(0x10), DecompressedData, BlockX, BlockY); + + int DecompressedWidth = Math.Min(BlockX, X - i); + int DecompressedHeight = Math.Min(BlockY, Y - j); + int BaseOffsets = (j * X + i) * 4; + + for (int jj = 0; jj < DecompressedHeight; jj++) + { + OutputStream.Seek(BaseOffsets + jj * X * 4, SeekOrigin.Begin); + + byte[] OutputBuffer = new byte[DecompressedData.Length * sizeof(int)]; + Buffer.BlockCopy(DecompressedData, 0, OutputBuffer, 0, OutputBuffer.Length); + + OutputStream.Write(OutputBuffer, jj * BlockX * 4, DecompressedWidth * 4); + } + + BlockIndex++; + } + } + + return OutputStream.ToArray(); + } + } + } + + public static bool DecompressBlock( + byte[] InputBuffer, + int[] OutputBuffer, + int BlockWidth, + int BlockHeight) + { + BitArrayStream BitStream = new BitArrayStream(new BitArray(InputBuffer)); + TexelWeightParams TexelParams = DecodeBlockInfo(BitStream); + + if (TexelParams.Error) + { + throw new ASTCDecoderException("Invalid block mode"); + } + + if (TexelParams.VoidExtentLDR) + { + FillVoidExtentLDR(BitStream, OutputBuffer, BlockWidth, BlockHeight); + + return true; + } + + if (TexelParams.VoidExtentHDR) + { + throw new ASTCDecoderException("HDR void extent blocks are unsupported!"); + } + + if (TexelParams.Width > BlockWidth) + { + throw new ASTCDecoderException("Texel weight grid width should be smaller than block width"); + } + + if (TexelParams.Height > BlockHeight) + { + throw new ASTCDecoderException("Texel weight grid height should be smaller than block height"); + } + + // Read num partitions + int NumberPartitions = BitStream.ReadBits(2) + 1; + Debug.Assert(NumberPartitions <= 4); + + if (NumberPartitions == 4 && TexelParams.DualPlane) + { + throw new ASTCDecoderException("Dual plane mode is incompatible with four partition blocks"); + } + + // Based on the number of partitions, read the color endpoint mode for + // each partition. + + // Determine partitions, partition index, and color endpoint modes + int PlaneIndices = -1; + int PartitionIndex; + uint[] ColorEndpointMode = { 0, 0, 0, 0 }; + + BitArrayStream ColorEndpointStream = new BitArrayStream(new BitArray(16 * 8)); + + // Read extra config data... + uint BaseColorEndpointMode = 0; + + if (NumberPartitions == 1) + { + ColorEndpointMode[0] = (uint)BitStream.ReadBits(4); + PartitionIndex = 0; + } + else + { + PartitionIndex = BitStream.ReadBits(10); + BaseColorEndpointMode = (uint)BitStream.ReadBits(6); + } + + uint BaseMode = (BaseColorEndpointMode & 3); + + // Remaining bits are color endpoint data... + int NumberWeightBits = TexelParams.GetPackedBitSize(); + int RemainingBits = 128 - NumberWeightBits - BitStream.Position; + + // Consider extra bits prior to texel data... + uint ExtraColorEndpointModeBits = 0; + + if (BaseMode != 0) + { + switch (NumberPartitions) + { + case 2: ExtraColorEndpointModeBits += 2; break; + case 3: ExtraColorEndpointModeBits += 5; break; + case 4: ExtraColorEndpointModeBits += 8; break; + default: Debug.Assert(false); break; + } + } + + RemainingBits -= (int)ExtraColorEndpointModeBits; + + // Do we have a dual plane situation? + int PlaneSelectorBits = 0; + + if (TexelParams.DualPlane) + { + PlaneSelectorBits = 2; + } + + RemainingBits -= PlaneSelectorBits; + + // Read color data... + int ColorDataBits = RemainingBits; + + while (RemainingBits > 0) + { + int NumberBits = Math.Min(RemainingBits, 8); + int Bits = BitStream.ReadBits(NumberBits); + ColorEndpointStream.WriteBits(Bits, NumberBits); + RemainingBits -= 8; + } + + // Read the plane selection bits + PlaneIndices = BitStream.ReadBits(PlaneSelectorBits); + + // Read the rest of the CEM + if (BaseMode != 0) + { + uint ExtraColorEndpointMode = (uint)BitStream.ReadBits((int)ExtraColorEndpointModeBits); + uint TempColorEndpointMode = (ExtraColorEndpointMode << 6) | BaseColorEndpointMode; + TempColorEndpointMode >>= 2; + + bool[] C = new bool[4]; + + for (int i = 0; i < NumberPartitions; i++) + { + C[i] = (TempColorEndpointMode & 1) != 0; + TempColorEndpointMode >>= 1; + } + + byte[] M = new byte[4]; + + for (int i = 0; i < NumberPartitions; i++) + { + M[i] = (byte)(TempColorEndpointMode & 3); + TempColorEndpointMode >>= 2; + Debug.Assert(M[i] <= 3); + } + + for (int i = 0; i < NumberPartitions; i++) + { + ColorEndpointMode[i] = BaseMode; + if (!(C[i])) ColorEndpointMode[i] -= 1; + ColorEndpointMode[i] <<= 2; + ColorEndpointMode[i] |= M[i]; + } + } + else if (NumberPartitions > 1) + { + uint TempColorEndpointMode = BaseColorEndpointMode >> 2; + + for (uint i = 0; i < NumberPartitions; i++) + { + ColorEndpointMode[i] = TempColorEndpointMode; + } + } + + // Make sure everything up till here is sane. + for (int i = 0; i < NumberPartitions; i++) + { + Debug.Assert(ColorEndpointMode[i] < 16); + } + Debug.Assert(BitStream.Position + TexelParams.GetPackedBitSize() == 128); + + // Decode both color data and texel weight data + int[] ColorValues = new int[32]; // Four values * two endpoints * four maximum partitions + DecodeColorValues(ColorValues, ColorEndpointStream.ToByteArray(), ColorEndpointMode, NumberPartitions, ColorDataBits); + + ASTCPixel[][] EndPoints = new ASTCPixel[4][]; + EndPoints[0] = new ASTCPixel[2]; + EndPoints[1] = new ASTCPixel[2]; + EndPoints[2] = new ASTCPixel[2]; + EndPoints[3] = new ASTCPixel[2]; + + int ColorValuesPosition = 0; + + for (int i = 0; i < NumberPartitions; i++) + { + ComputeEndpoints(EndPoints[i], ColorValues, ColorEndpointMode[i], ref ColorValuesPosition); + } + + // Read the texel weight data. + byte[] TexelWeightData = (byte[])InputBuffer.Clone(); + + // Reverse everything + for (int i = 0; i < 8; i++) + { + byte a = ReverseByte(TexelWeightData[i]); + byte b = ReverseByte(TexelWeightData[15 - i]); + + TexelWeightData[i] = b; + TexelWeightData[15 - i] = a; + } + + // Make sure that higher non-texel bits are set to zero + int ClearByteStart = (TexelParams.GetPackedBitSize() >> 3) + 1; + TexelWeightData[ClearByteStart - 1] &= (byte)((1 << (TexelParams.GetPackedBitSize() % 8)) - 1); + + int cLen = 16 - ClearByteStart; + for (int i = ClearByteStart; i < ClearByteStart + cLen; i++) TexelWeightData[i] = 0; + + List<IntegerEncoded> TexelWeightValues = new List<IntegerEncoded>(); + BitArrayStream WeightBitStream = new BitArrayStream(new BitArray(TexelWeightData)); + + IntegerEncoded.DecodeIntegerSequence(TexelWeightValues, WeightBitStream, TexelParams.MaxWeight, TexelParams.GetNumWeightValues()); + + // Blocks can be at most 12x12, so we can have as many as 144 weights + int[][] Weights = new int[2][]; + Weights[0] = new int[144]; + Weights[1] = new int[144]; + + UnquantizeTexelWeights(Weights, TexelWeightValues, TexelParams, BlockWidth, BlockHeight); + + // Now that we have endpoints and weights, we can interpolate and generate + // the proper decoding... + for (int j = 0; j < BlockHeight; j++) + { + for (int i = 0; i < BlockWidth; i++) + { + int Partition = Select2DPartition(PartitionIndex, i, j, NumberPartitions, ((BlockHeight * BlockWidth) < 32)); + Debug.Assert(Partition < NumberPartitions); + + ASTCPixel Pixel = new ASTCPixel(0, 0, 0, 0); + for (int Component = 0; Component < 4; Component++) + { + int Component0 = EndPoints[Partition][0].GetComponent(Component); + Component0 = BitArrayStream.Replicate(Component0, 8, 16); + int Component1 = EndPoints[Partition][1].GetComponent(Component); + Component1 = BitArrayStream.Replicate(Component1, 8, 16); + + int Plane = 0; + + if (TexelParams.DualPlane && (((PlaneIndices + 1) & 3) == Component)) + { + Plane = 1; + } + + int Weight = Weights[Plane][j * BlockWidth + i]; + int FinalComponent = (Component0 * (64 - Weight) + Component1 * Weight + 32) / 64; + + if (FinalComponent == 65535) + { + Pixel.SetComponent(Component, 255); + } + else + { + double FinalComponentFloat = FinalComponent; + Pixel.SetComponent(Component, (int)(255.0 * (FinalComponentFloat / 65536.0) + 0.5)); + } + } + + OutputBuffer[j * BlockWidth + i] = Pixel.Pack(); + } + } + + return true; + } + + private static int Select2DPartition(int Seed, int X, int Y, int PartitionCount, bool IsSmallBlock) + { + return SelectPartition(Seed, X, Y, 0, PartitionCount, IsSmallBlock); + } + + private static int SelectPartition(int Seed, int X, int Y, int Z, int PartitionCount, bool IsSmallBlock) + { + if (PartitionCount == 1) + { + return 0; + } + + if (IsSmallBlock) + { + X <<= 1; + Y <<= 1; + Z <<= 1; + } + + Seed += (PartitionCount - 1) * 1024; + + int RightNum = Hash52((uint)Seed); + byte Seed01 = (byte)(RightNum & 0xF); + byte Seed02 = (byte)((RightNum >> 4) & 0xF); + byte Seed03 = (byte)((RightNum >> 8) & 0xF); + byte Seed04 = (byte)((RightNum >> 12) & 0xF); + byte Seed05 = (byte)((RightNum >> 16) & 0xF); + byte Seed06 = (byte)((RightNum >> 20) & 0xF); + byte Seed07 = (byte)((RightNum >> 24) & 0xF); + byte Seed08 = (byte)((RightNum >> 28) & 0xF); + byte Seed09 = (byte)((RightNum >> 18) & 0xF); + byte Seed10 = (byte)((RightNum >> 22) & 0xF); + byte Seed11 = (byte)((RightNum >> 26) & 0xF); + byte Seed12 = (byte)(((RightNum >> 30) | (RightNum << 2)) & 0xF); + + Seed01 *= Seed01; Seed02 *= Seed02; + Seed03 *= Seed03; Seed04 *= Seed04; + Seed05 *= Seed05; Seed06 *= Seed06; + Seed07 *= Seed07; Seed08 *= Seed08; + Seed09 *= Seed09; Seed10 *= Seed10; + Seed11 *= Seed11; Seed12 *= Seed12; + + int SeedHash1, SeedHash2, SeedHash3; + + if ((Seed & 1) != 0) + { + SeedHash1 = (Seed & 2) != 0 ? 4 : 5; + SeedHash2 = (PartitionCount == 3) ? 6 : 5; + } + else + { + SeedHash1 = (PartitionCount == 3) ? 6 : 5; + SeedHash2 = (Seed & 2) != 0 ? 4 : 5; + } + + SeedHash3 = (Seed & 0x10) != 0 ? SeedHash1 : SeedHash2; + + Seed01 >>= SeedHash1; Seed02 >>= SeedHash2; Seed03 >>= SeedHash1; Seed04 >>= SeedHash2; + Seed05 >>= SeedHash1; Seed06 >>= SeedHash2; Seed07 >>= SeedHash1; Seed08 >>= SeedHash2; + Seed09 >>= SeedHash3; Seed10 >>= SeedHash3; Seed11 >>= SeedHash3; Seed12 >>= SeedHash3; + + int a = Seed01 * X + Seed02 * Y + Seed11 * Z + (RightNum >> 14); + int b = Seed03 * X + Seed04 * Y + Seed12 * Z + (RightNum >> 10); + int c = Seed05 * X + Seed06 * Y + Seed09 * Z + (RightNum >> 6); + int d = Seed07 * X + Seed08 * Y + Seed10 * Z + (RightNum >> 2); + + a &= 0x3F; b &= 0x3F; c &= 0x3F; d &= 0x3F; + + if (PartitionCount < 4) d = 0; + if (PartitionCount < 3) c = 0; + + if (a >= b && a >= c && a >= d) return 0; + else if (b >= c && b >= d) return 1; + else if (c >= d) return 2; + return 3; + } + + static int Hash52(uint Val) + { + Val ^= Val >> 15; Val -= Val << 17; Val += Val << 7; Val += Val << 4; + Val ^= Val >> 5; Val += Val << 16; Val ^= Val >> 7; Val ^= Val >> 3; + Val ^= Val << 6; Val ^= Val >> 17; + + return (int)Val; + } + + static void UnquantizeTexelWeights( + int[][] OutputBuffer, + List<IntegerEncoded> Weights, + TexelWeightParams TexelParams, + int BlockWidth, + int BlockHeight) + { + int WeightIndices = 0; + int[][] Unquantized = new int[2][]; + Unquantized[0] = new int[144]; + Unquantized[1] = new int[144]; + + for (int i = 0; i < Weights.Count; i++) + { + Unquantized[0][WeightIndices] = UnquantizeTexelWeight(Weights[i]); + + if (TexelParams.DualPlane) + { + i++; + Unquantized[1][WeightIndices] = UnquantizeTexelWeight(Weights[i]); + + if (i == Weights.Count) + { + break; + } + } + + if (++WeightIndices >= (TexelParams.Width * TexelParams.Height)) break; + } + + // Do infill if necessary (Section C.2.18) ... + int Ds = (1024 + (BlockWidth / 2)) / (BlockWidth - 1); + int Dt = (1024 + (BlockHeight / 2)) / (BlockHeight - 1); + + int PlaneScale = TexelParams.DualPlane ? 2 : 1; + + for (int Plane = 0; Plane < PlaneScale; Plane++) + { + for (int t = 0; t < BlockHeight; t++) + { + for (int s = 0; s < BlockWidth; s++) + { + int cs = Ds * s; + int ct = Dt * t; + + int gs = (cs * (TexelParams.Width - 1) + 32) >> 6; + int gt = (ct * (TexelParams.Height - 1) + 32) >> 6; + + int js = gs >> 4; + int fs = gs & 0xF; + + int jt = gt >> 4; + int ft = gt & 0x0F; + + int w11 = (fs * ft + 8) >> 4; + int w10 = ft - w11; + int w01 = fs - w11; + int w00 = 16 - fs - ft + w11; + + int v0 = js + jt * TexelParams.Width; + + int p00 = 0; + int p01 = 0; + int p10 = 0; + int p11 = 0; + + if (v0 < (TexelParams.Width * TexelParams.Height)) + { + p00 = Unquantized[Plane][v0]; + } + + if (v0 + 1 < (TexelParams.Width * TexelParams.Height)) + { + p01 = Unquantized[Plane][v0 + 1]; + } + + if (v0 + TexelParams.Width < (TexelParams.Width * TexelParams.Height)) + { + p10 = Unquantized[Plane][v0 + TexelParams.Width]; + } + + if (v0 + TexelParams.Width + 1 < (TexelParams.Width * TexelParams.Height)) + { + p11 = Unquantized[Plane][v0 + TexelParams.Width + 1]; + } + + OutputBuffer[Plane][t * BlockWidth + s] = (p00 * w00 + p01 * w01 + p10 * w10 + p11 * w11 + 8) >> 4; + } + } + } + } + + static int UnquantizeTexelWeight(IntegerEncoded IntEncoded) + { + int BitValue = IntEncoded.BitValue; + int BitLength = IntEncoded.NumberBits; + + int A = BitArrayStream.Replicate(BitValue & 1, 1, 7); + int B = 0, C = 0, D = 0; + + int Result = 0; + + switch (IntEncoded.GetEncoding()) + { + case IntegerEncoded.EIntegerEncoding.JustBits: + Result = BitArrayStream.Replicate(BitValue, BitLength, 6); + break; + + case IntegerEncoded.EIntegerEncoding.Trit: + { + D = IntEncoded.TritValue; + Debug.Assert(D < 3); + + switch (BitLength) + { + case 0: + { + int[] Results = { 0, 32, 63 }; + Result = Results[D]; + + break; + } + + case 1: + { + C = 50; + break; + } + + case 2: + { + C = 23; + int b = (BitValue >> 1) & 1; + B = (b << 6) | (b << 2) | b; + + break; + } + + case 3: + { + C = 11; + int cb = (BitValue >> 1) & 3; + B = (cb << 5) | cb; + + break; + } + + default: + throw new ASTCDecoderException("Invalid trit encoding for texel weight"); + } + + break; + } + + case IntegerEncoded.EIntegerEncoding.Quint: + { + D = IntEncoded.QuintValue; + Debug.Assert(D < 5); + + switch (BitLength) + { + case 0: + { + int[] Results = { 0, 16, 32, 47, 63 }; + Result = Results[D]; + + break; + } + + case 1: + { + C = 28; + + break; + } + + case 2: + { + C = 13; + int b = (BitValue >> 1) & 1; + B = (b << 6) | (b << 1); + + break; + } + + default: + throw new ASTCDecoderException("Invalid quint encoding for texel weight"); + } + + break; + } + } + + if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && BitLength > 0) + { + // Decode the value... + Result = D * C + B; + Result ^= A; + Result = (A & 0x20) | (Result >> 2); + } + + Debug.Assert(Result < 64); + + // Change from [0,63] to [0,64] + if (Result > 32) + { + Result += 1; + } + + return Result; + } + + static byte ReverseByte(byte b) + { + // Taken from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits + return (byte)((((b) * 0x80200802L) & 0x0884422110L) * 0x0101010101L >> 32); + } + + static uint[] ReadUintColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) + { + uint[] Ret = new uint[Number]; + + for (int i = 0; i < Number; i++) + { + Ret[i] = (uint)ColorValues[ColorValuesPosition++]; + } + + return Ret; + } + + static int[] ReadIntColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) + { + int[] Ret = new int[Number]; + + for (int i = 0; i < Number; i++) + { + Ret[i] = ColorValues[ColorValuesPosition++]; + } + + return Ret; + } + + static void ComputeEndpoints( + ASTCPixel[] EndPoints, + int[] ColorValues, + uint ColorEndpointMode, + ref int ColorValuesPosition) + { + switch (ColorEndpointMode) + { + case 0: + { + uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[0], (short)Val[0]); + EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[1], (short)Val[1]); + + break; + } + + + case 1: + { + uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); + int L0 = (int)((Val[0] >> 2) | (Val[1] & 0xC0)); + int L1 = (int)Math.Max(L0 + (Val[1] & 0x3F), 0xFFU); + + EndPoints[0] = new ASTCPixel(0xFF, (short)L0, (short)L0, (short)L0); + EndPoints[1] = new ASTCPixel(0xFF, (short)L1, (short)L1, (short)L1); + + break; + } + + case 4: + { + uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); + EndPoints[1] = new ASTCPixel((short)Val[3], (short)Val[1], (short)Val[1], (short)Val[1]); + + break; + } + + case 5: + { + int[] Val = ReadIntColorValues(4, ColorValues, ref ColorValuesPosition); + + BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); + BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); + + EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); + EndPoints[1] = new ASTCPixel((short)(Val[2] + Val[3]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1])); + + EndPoints[0].ClampByte(); + EndPoints[1].ClampByte(); + + break; + } + + case 6: + { + uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel(0xFF, (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); + EndPoints[1] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[1], (short)Val[2]); + + break; + } + + case 8: + { + uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); + + if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) + { + EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); + EndPoints[1] = ASTCPixel.BlueContract(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); + } + + break; + } + + case 9: + { + int[] Val = ReadIntColorValues(6, ColorValues, ref ColorValuesPosition); + + BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); + BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); + BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); + + if (Val[1] + Val[3] + Val[5] >= 0) + { + EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel(0xFF, (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract(0xFF, Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); + EndPoints[1] = ASTCPixel.BlueContract(0xFF, Val[0], Val[2], Val[4]); + } + + EndPoints[0].ClampByte(); + EndPoints[1].ClampByte(); + + break; + } + + case 10: + { + uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel((short)Val[4], (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); + EndPoints[1] = new ASTCPixel((short)Val[5], (short)Val[0], (short)Val[1], (short)Val[2]); + + break; + } + + case 12: + { + uint[] Val = ReadUintColorValues(8, ColorValues, ref ColorValuesPosition); + + if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) + { + EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); + EndPoints[1] = ASTCPixel.BlueContract((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); + } + + break; + } + + case 13: + { + int[] Val = ReadIntColorValues(8, ColorValues, ref ColorValuesPosition); + + BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); + BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); + BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); + BitArrayStream.BitTransferSigned(ref Val[7], ref Val[6]); + + if (Val[1] + Val[3] + Val[5] >= 0) + { + EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel((short)(Val[7] + Val[6]), (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract(Val[6] + Val[7], Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); + EndPoints[1] = ASTCPixel.BlueContract(Val[6], Val[0], Val[2], Val[4]); + } + + EndPoints[0].ClampByte(); + EndPoints[1].ClampByte(); + + break; + } + + default: + throw new ASTCDecoderException("Unsupported color endpoint mode (is it HDR?)"); + } + } + + static void DecodeColorValues( + int[] OutputValues, + byte[] InputData, + uint[] Modes, + int NumberPartitions, + int NumberBitsForColorData) + { + // First figure out how many color values we have + int NumberValues = 0; + + for (int i = 0; i < NumberPartitions; i++) + { + NumberValues += (int)((Modes[i] >> 2) + 1) << 1; + } + + // Then based on the number of values and the remaining number of bits, + // figure out the max value for each of them... + int Range = 256; + + while (--Range > 0) + { + IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(Range); + int BitLength = IntEncoded.GetBitLength(NumberValues); + + if (BitLength <= NumberBitsForColorData) + { + // Find the smallest possible range that matches the given encoding + while (--Range > 0) + { + IntegerEncoded NewIntEncoded = IntegerEncoded.CreateEncoding(Range); + if (!NewIntEncoded.MatchesEncoding(IntEncoded)) + { + break; + } + } + + // Return to last matching range. + Range++; + break; + } + } + + // We now have enough to decode our integer sequence. + List<IntegerEncoded> IntegerEncodedSequence = new List<IntegerEncoded>(); + BitArrayStream ColorBitStream = new BitArrayStream(new BitArray(InputData)); + + IntegerEncoded.DecodeIntegerSequence(IntegerEncodedSequence, ColorBitStream, Range, NumberValues); + + // Once we have the decoded values, we need to dequantize them to the 0-255 range + // This procedure is outlined in ASTC spec C.2.13 + int OutputIndices = 0; + + foreach (IntegerEncoded IntEncoded in IntegerEncodedSequence) + { + int BitLength = IntEncoded.NumberBits; + int BitValue = IntEncoded.BitValue; + + Debug.Assert(BitLength >= 1); + + int A = 0, B = 0, C = 0, D = 0; + // A is just the lsb replicated 9 times. + A = BitArrayStream.Replicate(BitValue & 1, 1, 9); + + switch (IntEncoded.GetEncoding()) + { + case IntegerEncoded.EIntegerEncoding.JustBits: + { + OutputValues[OutputIndices++] = BitArrayStream.Replicate(BitValue, BitLength, 8); + + break; + } + + case IntegerEncoded.EIntegerEncoding.Trit: + { + D = IntEncoded.TritValue; + + switch (BitLength) + { + case 1: + { + C = 204; + + break; + } + + case 2: + { + C = 93; + // B = b000b0bb0 + int b = (BitValue >> 1) & 1; + B = (b << 8) | (b << 4) | (b << 2) | (b << 1); + + break; + } + + case 3: + { + C = 44; + // B = cb000cbcb + int cb = (BitValue >> 1) & 3; + B = (cb << 7) | (cb << 2) | cb; + + break; + } + + + case 4: + { + C = 22; + // B = dcb000dcb + int dcb = (BitValue >> 1) & 7; + B = (dcb << 6) | dcb; + + break; + } + + case 5: + { + C = 11; + // B = edcb000ed + int edcb = (BitValue >> 1) & 0xF; + B = (edcb << 5) | (edcb >> 2); + + break; + } + + case 6: + { + C = 5; + // B = fedcb000f + int fedcb = (BitValue >> 1) & 0x1F; + B = (fedcb << 4) | (fedcb >> 4); + + break; + } + + default: + throw new ASTCDecoderException("Unsupported trit encoding for color values!"); + } + + break; + } + + case IntegerEncoded.EIntegerEncoding.Quint: + { + D = IntEncoded.QuintValue; + + switch (BitLength) + { + case 1: + { + C = 113; + + break; + } + + case 2: + { + C = 54; + // B = b0000bb00 + int b = (BitValue >> 1) & 1; + B = (b << 8) | (b << 3) | (b << 2); + + break; + } + + case 3: + { + C = 26; + // B = cb0000cbc + int cb = (BitValue >> 1) & 3; + B = (cb << 7) | (cb << 1) | (cb >> 1); + + break; + } + + case 4: + { + C = 13; + // B = dcb0000dc + int dcb = (BitValue >> 1) & 7; + B = (dcb << 6) | (dcb >> 1); + + break; + } + + case 5: + { + C = 6; + // B = edcb0000e + int edcb = (BitValue >> 1) & 0xF; + B = (edcb << 5) | (edcb >> 3); + + break; + } + + default: + throw new ASTCDecoderException("Unsupported quint encoding for color values!"); + } + break; + } + } + + if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits) + { + int T = D * C + B; + T ^= A; + T = (A & 0x80) | (T >> 2); + + OutputValues[OutputIndices++] = T; + } + } + + // Make sure that each of our values is in the proper range... + for (int i = 0; i < NumberValues; i++) + { + Debug.Assert(OutputValues[i] <= 255); + } + } + + static void FillVoidExtentLDR(BitArrayStream BitStream, int[] OutputBuffer, int BlockWidth, int BlockHeight) + { + // Don't actually care about the void extent, just read the bits... + for (int i = 0; i < 4; ++i) + { + BitStream.ReadBits(13); + } + + // Decode the RGBA components and renormalize them to the range [0, 255] + ushort R = (ushort)BitStream.ReadBits(16); + ushort G = (ushort)BitStream.ReadBits(16); + ushort B = (ushort)BitStream.ReadBits(16); + ushort A = (ushort)BitStream.ReadBits(16); + + int RGBA = (R >> 8) | (G & 0xFF00) | ((B) & 0xFF00) << 8 | ((A) & 0xFF00) << 16; + + for (int j = 0; j < BlockHeight; j++) + { + for (int i = 0; i < BlockWidth; i++) + { + OutputBuffer[j * BlockWidth + i] = RGBA; + } + } + } + + static TexelWeightParams DecodeBlockInfo(BitArrayStream BitStream) + { + TexelWeightParams TexelParams = new TexelWeightParams(); + + // Read the entire block mode all at once + ushort ModeBits = (ushort)BitStream.ReadBits(11); + + // Does this match the void extent block mode? + if ((ModeBits & 0x01FF) == 0x1FC) + { + if ((ModeBits & 0x200) != 0) + { + TexelParams.VoidExtentHDR = true; + } + else + { + TexelParams.VoidExtentLDR = true; + } + + // Next two bits must be one. + if ((ModeBits & 0x400) == 0 || BitStream.ReadBits(1) == 0) + { + TexelParams.Error = true; + } + + return TexelParams; + } + + // First check if the last four bits are zero + if ((ModeBits & 0xF) == 0) + { + TexelParams.Error = true; + return TexelParams; + } + + // If the last two bits are zero, then if bits + // [6-8] are all ones, this is also reserved. + if ((ModeBits & 0x3) == 0 && (ModeBits & 0x1C0) == 0x1C0) + { + TexelParams.Error = true; + + return TexelParams; + } + + // Otherwise, there is no error... Figure out the layout + // of the block mode. Layout is determined by a number + // between 0 and 9 corresponding to table C.2.8 of the + // ASTC spec. + int Layout = 0; + + if ((ModeBits & 0x1) != 0 || (ModeBits & 0x2) != 0) + { + // layout is in [0-4] + if ((ModeBits & 0x8) != 0) + { + // layout is in [2-4] + if ((ModeBits & 0x4) != 0) + { + // layout is in [3-4] + if ((ModeBits & 0x100) != 0) + { + Layout = 4; + } + else + { + Layout = 3; + } + } + else + { + Layout = 2; + } + } + else + { + // layout is in [0-1] + if ((ModeBits & 0x4) != 0) + { + Layout = 1; + } + else + { + Layout = 0; + } + } + } + else + { + // layout is in [5-9] + if ((ModeBits & 0x100) != 0) + { + // layout is in [7-9] + if ((ModeBits & 0x80) != 0) + { + // layout is in [7-8] + Debug.Assert((ModeBits & 0x40) == 0); + + if ((ModeBits & 0x20) != 0) + { + Layout = 8; + } + else + { + Layout = 7; + } + } + else + { + Layout = 9; + } + } + else + { + // layout is in [5-6] + if ((ModeBits & 0x80) != 0) + { + Layout = 6; + } + else + { + Layout = 5; + } + } + } + + Debug.Assert(Layout < 10); + + // Determine R + int R = (ModeBits >> 4) & 1; + if (Layout < 5) + { + R |= (ModeBits & 0x3) << 1; + } + else + { + R |= (ModeBits & 0xC) >> 1; + } + + Debug.Assert(2 <= R && R <= 7); + + // Determine width & height + switch (Layout) + { + case 0: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x3; + + TexelParams.Width = B + 4; + TexelParams.Height = A + 2; + + break; + } + + case 1: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x3; + + TexelParams.Width = B + 8; + TexelParams.Height = A + 2; + + break; + } + + case 2: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x3; + + TexelParams.Width = A + 2; + TexelParams.Height = B + 8; + + break; + } + + case 3: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x1; + + TexelParams.Width = A + 2; + TexelParams.Height = B + 6; + + break; + } + + case 4: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x1; + + TexelParams.Width = B + 2; + TexelParams.Height = A + 2; + + break; + } + + case 5: + { + int A = (ModeBits >> 5) & 0x3; + + TexelParams.Width = 12; + TexelParams.Height = A + 2; + + break; + } + + case 6: + { + int A = (ModeBits >> 5) & 0x3; + + TexelParams.Width = A + 2; + TexelParams.Height = 12; + + break; + } + + case 7: + { + TexelParams.Width = 6; + TexelParams.Height = 10; + + break; + } + + case 8: + { + TexelParams.Width = 10; + TexelParams.Height = 6; + break; + } + + case 9: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 9) & 0x3; + + TexelParams.Width = A + 6; + TexelParams.Height = B + 6; + + break; + } + + default: + //Don't know this layout... + TexelParams.Error = true; + break; + } + + // Determine whether or not we're using dual planes + // and/or high precision layouts. + bool D = ((Layout != 9) && ((ModeBits & 0x400) != 0)); + bool H = (Layout != 9) && ((ModeBits & 0x200) != 0); + + if (H) + { + int[] MaxWeights = { 9, 11, 15, 19, 23, 31 }; + TexelParams.MaxWeight = MaxWeights[R - 2]; + } + else + { + int[] MaxWeights = { 1, 2, 3, 4, 5, 7 }; + TexelParams.MaxWeight = MaxWeights[R - 2]; + } + + TexelParams.DualPlane = D; + + return TexelParams; + } + } +} diff --git a/Ryujinx.Graphics/Texture/ASTCPixel.cs b/Ryujinx.Graphics/Texture/ASTCPixel.cs new file mode 100644 index 00000000..c43eaf93 --- /dev/null +++ b/Ryujinx.Graphics/Texture/ASTCPixel.cs @@ -0,0 +1,138 @@ +using System; +using System.Diagnostics; + +namespace Ryujinx.Graphics.Texture +{ + class ASTCPixel + { + public short R { get; set; } + public short G { get; set; } + public short B { get; set; } + public short A { get; set; } + + byte[] BitDepth = new byte[4]; + + public ASTCPixel(short _A, short _R, short _G, short _B) + { + A = _A; + R = _R; + G = _G; + B = _B; + + for (int i = 0; i < 4; i++) + BitDepth[i] = 8; + } + + public void ClampByte() + { + R = Math.Min(Math.Max(R, (short)0), (short)255); + G = Math.Min(Math.Max(G, (short)0), (short)255); + B = Math.Min(Math.Max(B, (short)0), (short)255); + A = Math.Min(Math.Max(A, (short)0), (short)255); + } + + public short GetComponent(int Index) + { + switch(Index) + { + case 0: return A; + case 1: return R; + case 2: return G; + case 3: return B; + } + + return 0; + } + + public void SetComponent(int Index, int Value) + { + switch (Index) + { + case 0: + A = (short)Value; + break; + case 1: + R = (short)Value; + break; + case 2: + G = (short)Value; + break; + case 3: + B = (short)Value; + break; + } + } + + public void ChangeBitDepth(byte[] Depth) + { + for(int i = 0; i< 4; i++) + { + int Value = ChangeBitDepth(GetComponent(i), BitDepth[i], Depth[i]); + + SetComponent(i, Value); + BitDepth[i] = Depth[i]; + } + } + + short ChangeBitDepth(short Value, byte OldDepth, byte NewDepth) + { + Debug.Assert(NewDepth <= 8); + Debug.Assert(OldDepth <= 8); + + if (OldDepth == NewDepth) + { + // Do nothing + return Value; + } + else if (OldDepth == 0 && NewDepth != 0) + { + return (short)((1 << NewDepth) - 1); + } + else if (NewDepth > OldDepth) + { + return (short)BitArrayStream.Replicate(Value, OldDepth, NewDepth); + } + else + { + // oldDepth > newDepth + if (NewDepth == 0) + { + return 0xFF; + } + else + { + byte BitsWasted = (byte)(OldDepth - NewDepth); + short TempValue = Value; + + TempValue = (short)((TempValue + (1 << (BitsWasted - 1))) >> BitsWasted); + TempValue = Math.Min(Math.Max((short)0, TempValue), (short)((1 << NewDepth) - 1)); + + return (byte)(TempValue); + } + } + } + + public int Pack() + { + ASTCPixel NewPixel = new ASTCPixel(A, R, G, B); + byte[] eightBitDepth = { 8, 8, 8, 8 }; + + NewPixel.ChangeBitDepth(eightBitDepth); + + return (byte)NewPixel.A << 24 | + (byte)NewPixel.B << 16 | + (byte)NewPixel.G << 8 | + (byte)NewPixel.R << 0; + } + + // Adds more precision to the blue channel as described + // in C.2.14 + public static ASTCPixel BlueContract(int a, int r, int g, int b) + { + return new ASTCPixel((short)(a), + (short)((r + b) >> 1), + (short)((g + b) >> 1), + (short)(b)); + } + } +} diff --git a/Ryujinx.Graphics/Texture/BitArrayStream.cs b/Ryujinx.Graphics/Texture/BitArrayStream.cs new file mode 100644 index 00000000..2a8ed091 --- /dev/null +++ b/Ryujinx.Graphics/Texture/BitArrayStream.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections; + +namespace Ryujinx.Graphics.Texture +{ + public class BitArrayStream + { + public BitArray BitsArray; + + public int Position { get; private set; } + + public BitArrayStream(BitArray BitArray) + { + BitsArray = BitArray; + Position = 0; + } + + public short ReadBits(int Length) + { + int RetValue = 0; + for (int i = Position; i < Position + Length; i++) + { + if (BitsArray[i]) + { + RetValue |= 1 << (i - Position); + } + } + + Position += Length; + return (short)RetValue; + } + + public int ReadBits(int Start, int End) + { + int RetValue = 0; + for (int i = Start; i <= End; i++) + { + if (BitsArray[i]) + { + RetValue |= 1 << (i - Start); + } + } + + return RetValue; + } + + public int ReadBit(int Index) + { + return Convert.ToInt32(BitsArray[Index]); + } + + public void WriteBits(int Value, int Length) + { + for (int i = Position; i < Position + Length; i++) + { + BitsArray[i] = ((Value >> (i - Position)) & 1) != 0; + } + + Position += Length; + } + + public byte[] ToByteArray() + { + byte[] RetArray = new byte[(BitsArray.Length + 7) / 8]; + BitsArray.CopyTo(RetArray, 0); + return RetArray; + } + + public static int Replicate(int Value, int NumberBits, int ToBit) + { + if (NumberBits == 0) return 0; + if (ToBit == 0) return 0; + + int TempValue = Value & ((1 << NumberBits) - 1); + int RetValue = TempValue; + int ResLength = NumberBits; + + while (ResLength < ToBit) + { + int Comp = 0; + if (NumberBits > ToBit - ResLength) + { + int NewShift = ToBit - ResLength; + Comp = NumberBits - NewShift; + NumberBits = NewShift; + } + RetValue <<= NumberBits; + RetValue |= TempValue >> Comp; + ResLength += NumberBits; + } + return RetValue; + } + + public static int PopCnt(int Number) + { + int Counter; + for (Counter = 0; Number != 0; Counter++) + { + Number &= Number - 1; + } + return Counter; + } + + public static void Swap<T>(ref T lhs, ref T rhs) + { + T Temp = lhs; + lhs = rhs; + rhs = Temp; + } + + // Transfers a bit as described in C.2.14 + public static void BitTransferSigned(ref int a, ref int b) + { + b >>= 1; + b |= a & 0x80; + a >>= 1; + a &= 0x3F; + if ((a & 0x20) != 0) a -= 0x40; + } + } +} diff --git a/Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs b/Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs new file mode 100644 index 00000000..9451291e --- /dev/null +++ b/Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs @@ -0,0 +1,59 @@ +using System; + +namespace Ryujinx.Graphics.Texture +{ + class BlockLinearSwizzle : ISwizzle + { + private int BhShift; + private int BppShift; + private int BhMask; + + private int XShift; + private int GobStride; + + public BlockLinearSwizzle(int Width, int Bpp, int BlockHeight = 16) + { + BhMask = (BlockHeight * 8) - 1; + + BhShift = CountLsbZeros(BlockHeight * 8); + BppShift = CountLsbZeros(Bpp); + + int WidthInGobs = (int)MathF.Ceiling(Width * Bpp / 64f); + + GobStride = 512 * BlockHeight * WidthInGobs; + + XShift = CountLsbZeros(512 * BlockHeight); + } + + private int CountLsbZeros(int Value) + { + int Count = 0; + + while (((Value >> Count) & 1) == 0) + { + Count++; + } + + return Count; + } + + public int GetSwizzleOffset(int X, int Y) + { + X <<= BppShift; + + int Position = (Y >> BhShift) * GobStride; + + Position += (X >> 6) << XShift; + + Position += ((Y & BhMask) >> 3) << 9; + + Position += ((X & 0x3f) >> 5) << 8; + Position += ((Y & 0x07) >> 1) << 6; + Position += ((X & 0x1f) >> 4) << 5; + Position += ((Y & 0x01) >> 0) << 4; + Position += ((X & 0x0f) >> 0) << 0; + + return Position; + } + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/ISwizzle.cs b/Ryujinx.Graphics/Texture/ISwizzle.cs new file mode 100644 index 00000000..583fc20c --- /dev/null +++ b/Ryujinx.Graphics/Texture/ISwizzle.cs @@ -0,0 +1,7 @@ +namespace Ryujinx.Graphics.Texture +{ + interface ISwizzle + { + int GetSwizzleOffset(int X, int Y); + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/ImageConverter.cs b/Ryujinx.Graphics/Texture/ImageConverter.cs new file mode 100644 index 00000000..89529061 --- /dev/null +++ b/Ryujinx.Graphics/Texture/ImageConverter.cs @@ -0,0 +1,24 @@ +namespace Ryujinx.Graphics.Texture +{ + static class ImageConverter + { + public static byte[] G8R8ToR8G8( + byte[] Data, + int Width, + int Height, + int Depth) + { + int Texels = Width * Height * Depth; + + byte[] Output = new byte[Texels * 2]; + + for (int Texel = 0; Texel < Texels; Texel++) + { + Output[Texel * 2 + 0] = Data[Texel * 2 + 1]; + Output[Texel * 2 + 1] = Data[Texel * 2 + 0]; + } + + return Output; + } + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/ImageUtils.cs b/Ryujinx.Graphics/Texture/ImageUtils.cs new file mode 100644 index 00000000..ccea43ec --- /dev/null +++ b/Ryujinx.Graphics/Texture/ImageUtils.cs @@ -0,0 +1,357 @@ +using Ryujinx.Graphics.Gal; +using System; +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Texture +{ + static class ImageUtils + { + struct ImageDescriptor + { + public TextureReaderDelegate Reader; + + public bool HasColor; + public bool HasDepth; + public bool HasStencil; + + public bool Compressed; + + public ImageDescriptor( + TextureReaderDelegate Reader, + bool HasColor, + bool HasDepth, + bool HasStencil, + bool Compressed) + { + this.Reader = Reader; + this.HasColor = HasColor; + this.HasDepth = HasDepth; + this.HasStencil = HasStencil; + this.Compressed = Compressed; + } + } + + private const GalImageFormat Snorm = GalImageFormat.Snorm; + private const GalImageFormat Unorm = GalImageFormat.Unorm; + private const GalImageFormat Sint = GalImageFormat.Sint; + private const GalImageFormat Uint = GalImageFormat.Uint; + private const GalImageFormat Sfloat = GalImageFormat.Sfloat; + + private static readonly Dictionary<GalTextureFormat, GalImageFormat> s_TextureTable = + new Dictionary<GalTextureFormat, GalImageFormat>() + { + { GalTextureFormat.R32G32B32A32, GalImageFormat.R32G32B32A32 | Sint | Uint | Sfloat }, + { GalTextureFormat.R16G16B16A16, GalImageFormat.R16G16B16A16 | Snorm | Unorm | Sint | Uint | Sfloat }, + { GalTextureFormat.R32G32, GalImageFormat.R32G32 | Sint | Uint | Sfloat }, + { GalTextureFormat.A8B8G8R8, GalImageFormat.A8B8G8R8 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.A2B10G10R10, GalImageFormat.A2B10G10R10 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.G8R8, GalImageFormat.G8R8 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.R16, GalImageFormat.R16 | Snorm | Unorm | Sint | Uint | Sfloat }, + { GalTextureFormat.R8, GalImageFormat.R8 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.R32, GalImageFormat.R32 | Sint | Uint | Sfloat }, + { GalTextureFormat.A4B4G4R4, GalImageFormat.A4B4G4R4 | Unorm }, + { GalTextureFormat.A1B5G5R5, GalImageFormat.A1R5G5B5 | Unorm }, + { GalTextureFormat.B5G6R5, GalImageFormat.B5G6R5 | Unorm }, + { GalTextureFormat.BF10GF11RF11, GalImageFormat.B10G11R11 | Sfloat }, + { GalTextureFormat.Z24S8, GalImageFormat.D24_S8 | Unorm }, + { GalTextureFormat.ZF32, GalImageFormat.D32 | Sfloat }, + { GalTextureFormat.ZF32_X24S8, GalImageFormat.D32_S8 | Unorm }, + + //Compressed formats + { GalTextureFormat.BC6H_SF16, GalImageFormat.BC6H_SF16 | Unorm }, + { GalTextureFormat.BC6H_UF16, GalImageFormat.BC6H_UF16 | Unorm }, + { GalTextureFormat.BC7U, GalImageFormat.BC7 | Unorm }, + { GalTextureFormat.BC1, GalImageFormat.BC1_RGBA | Unorm }, + { GalTextureFormat.BC2, GalImageFormat.BC2 | Unorm }, + { GalTextureFormat.BC3, GalImageFormat.BC3 | Unorm }, + { GalTextureFormat.BC4, GalImageFormat.BC4 | Unorm | Snorm }, + { GalTextureFormat.BC5, GalImageFormat.BC5 | Unorm | Snorm }, + { GalTextureFormat.Astc2D4x4, GalImageFormat.ASTC_4x4 | Unorm }, + { GalTextureFormat.Astc2D5x5, GalImageFormat.ASTC_5x5 | Unorm }, + { GalTextureFormat.Astc2D6x6, GalImageFormat.ASTC_6x6 | Unorm }, + { GalTextureFormat.Astc2D8x8, GalImageFormat.ASTC_8x8 | Unorm }, + { GalTextureFormat.Astc2D10x10, GalImageFormat.ASTC_10x10 | Unorm }, + { GalTextureFormat.Astc2D12x12, GalImageFormat.ASTC_12x12 | Unorm }, + { GalTextureFormat.Astc2D5x4, GalImageFormat.ASTC_5x4 | Unorm }, + { GalTextureFormat.Astc2D6x5, GalImageFormat.ASTC_6x5 | Unorm }, + { GalTextureFormat.Astc2D8x6, GalImageFormat.ASTC_8x6 | Unorm }, + { GalTextureFormat.Astc2D10x8, GalImageFormat.ASTC_10x8 | Unorm }, + { GalTextureFormat.Astc2D12x10, GalImageFormat.ASTC_12x10 | Unorm }, + { GalTextureFormat.Astc2D8x5, GalImageFormat.ASTC_8x5 | Unorm }, + { GalTextureFormat.Astc2D10x5, GalImageFormat.ASTC_10x5 | Unorm }, + { GalTextureFormat.Astc2D10x6, GalImageFormat.ASTC_10x6 | Unorm } + }; + + private static readonly Dictionary<GalImageFormat, ImageDescriptor> s_ImageTable = + new Dictionary<GalImageFormat, ImageDescriptor>() + { + { GalImageFormat.R32G32B32A32, new ImageDescriptor(TextureReader.Read16Bpp, true, false, false, false) }, + { GalImageFormat.R16G16B16A16, new ImageDescriptor(TextureReader.Read8Bpp, true, false, false, false) }, + { GalImageFormat.R32G32, new ImageDescriptor(TextureReader.Read8Bpp, true, false, false, false) }, + { GalImageFormat.A8B8G8R8, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.A2B10G10R10, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.R32, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.A4B4G4R4, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.BC6H_SF16, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.BC6H_UF16, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.A1R5G5B5, new ImageDescriptor(TextureReader.Read5551, true, false, false, false) }, + { GalImageFormat.B5G6R5, new ImageDescriptor(TextureReader.Read565, true, false, false, false) }, + { GalImageFormat.BC7, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.R16G16, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.R8G8, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.G8R8, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.R16, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.R8, new ImageDescriptor(TextureReader.Read1Bpp, true, false, false, false) }, + { GalImageFormat.B10G11R11, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.A8B8G8R8_SRGB, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.BC1_RGBA, new ImageDescriptor(TextureReader.Read8Bpt4x4, true, false, false, true) }, + { GalImageFormat.BC2, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.BC3, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.BC4, new ImageDescriptor(TextureReader.Read8Bpt4x4, true, false, false, true) }, + { GalImageFormat.BC5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.ASTC_4x4, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.ASTC_5x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture5x5, true, false, false, true) }, + { GalImageFormat.ASTC_6x6, new ImageDescriptor(TextureReader.Read16BptCompressedTexture6x6, true, false, false, true) }, + { GalImageFormat.ASTC_8x8, new ImageDescriptor(TextureReader.Read16BptCompressedTexture8x8, true, false, false, true) }, + { GalImageFormat.ASTC_10x10, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x10, true, false, false, true) }, + { GalImageFormat.ASTC_12x12, new ImageDescriptor(TextureReader.Read16BptCompressedTexture12x12, true, false, false, true) }, + { GalImageFormat.ASTC_5x4, new ImageDescriptor(TextureReader.Read16BptCompressedTexture5x4, true, false, false, true) }, + { GalImageFormat.ASTC_6x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture6x5, true, false, false, true) }, + { GalImageFormat.ASTC_8x6, new ImageDescriptor(TextureReader.Read16BptCompressedTexture8x6, true, false, false, true) }, + { GalImageFormat.ASTC_10x8, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x8, true, false, false, true) }, + { GalImageFormat.ASTC_12x10, new ImageDescriptor(TextureReader.Read16BptCompressedTexture12x10, true, false, false, true) }, + { GalImageFormat.ASTC_8x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture8x5, true, false, false, true) }, + { GalImageFormat.ASTC_10x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x5, true, false, false, true) }, + { GalImageFormat.ASTC_10x6, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x6, true, false, false, true) }, + + { GalImageFormat.D24_S8, new ImageDescriptor(TextureReader.Read4Bpp, false, true, true, false) }, + { GalImageFormat.D32, new ImageDescriptor(TextureReader.Read4Bpp, false, true, false, false) }, + { GalImageFormat.D16, new ImageDescriptor(TextureReader.Read2Bpp, false, true, false, false) }, + { GalImageFormat.D32_S8, new ImageDescriptor(TextureReader.Read8Bpp, false, true, true, false) }, + }; + + public static GalImageFormat ConvertTexture( + GalTextureFormat Format, + GalTextureType RType, + GalTextureType GType, + GalTextureType BType, + GalTextureType AType) + { + if (RType != GType || RType != BType || RType != AType) + { + throw new NotImplementedException("Per component types are not implemented"); + } + + if (!s_TextureTable.TryGetValue(Format, out GalImageFormat ImageFormat)) + { + throw new NotImplementedException("Texture with format " + ((int)Format).ToString("x2") + " not implemented"); + } + + GalTextureType Type = RType; + + GalImageFormat FormatType = GetFormatType(RType); + + if (ImageFormat.HasFlag(FormatType)) + { + return (ImageFormat & GalImageFormat.FormatMask) | FormatType; + } + else + { + throw new NotImplementedException("Texture with format " + Format + + " and component type " + Type + " is not implemented"); + } + } + + public static GalImageFormat ConvertSurface(GalSurfaceFormat Format) + { + switch (Format) + { + case GalSurfaceFormat.RGBA32Float: return GalImageFormat.R32G32B32A32 | Sfloat; + case GalSurfaceFormat.RGBA16Float: return GalImageFormat.R16G16B16A16 | Sfloat; + case GalSurfaceFormat.RG32Float: return GalImageFormat.R32G32 | Sfloat; + case GalSurfaceFormat.RG32Sint: return GalImageFormat.R32G32 | Sint; + case GalSurfaceFormat.RG32Uint: return GalImageFormat.R32G32 | Uint; + case GalSurfaceFormat.BGRA8Unorm: return GalImageFormat.R8G8B8A8 | Unorm; //Is this right? + case GalSurfaceFormat.BGRA8Srgb: return GalImageFormat.A8B8G8R8_SRGB; //This one might be wrong + case GalSurfaceFormat.RGB10A2Unorm: return GalImageFormat.A2B10G10R10 | Unorm; + case GalSurfaceFormat.RGBA8Unorm: return GalImageFormat.A8B8G8R8 | Unorm; + case GalSurfaceFormat.RGBA8Srgb: return GalImageFormat.A8B8G8R8_SRGB; + case GalSurfaceFormat.RGBA8Snorm: return GalImageFormat.A8B8G8R8 | Snorm; + case GalSurfaceFormat.RG16Snorm: return GalImageFormat.R16G16 | Snorm; + case GalSurfaceFormat.RG16Float: return GalImageFormat.R16G16 | Sfloat; + case GalSurfaceFormat.R11G11B10Float: return GalImageFormat.B10G11R11 | Sfloat; + case GalSurfaceFormat.R32Float: return GalImageFormat.R32 | Sfloat; + case GalSurfaceFormat.RG8Unorm: return GalImageFormat.R8G8 | Unorm; + case GalSurfaceFormat.RG8Snorm: return GalImageFormat.R8 | Snorm; + case GalSurfaceFormat.R16Float: return GalImageFormat.R16 | Sfloat; + case GalSurfaceFormat.R8Unorm: return GalImageFormat.R8 | Unorm; + } + + throw new NotImplementedException(Format.ToString()); + } + + public static GalImageFormat ConvertZeta(GalZetaFormat Format) + { + switch (Format) + { + case GalZetaFormat.Z32Float: return GalImageFormat.D32 | Sfloat; + case GalZetaFormat.S8Z24Unorm: return GalImageFormat.D24_S8 | Unorm; + case GalZetaFormat.Z16Unorm: return GalImageFormat.D16 | Unorm; + //This one might not be Uint, change when a texture uses this format + case GalZetaFormat.Z32S8X24Float: return GalImageFormat.D32_S8 | Uint; + } + + throw new NotImplementedException(Format.ToString()); + } + + public static TextureReaderDelegate GetReader(GalImageFormat Format) + { + return GetImageDescriptor(Format).Reader; + } + + public static int GetSize(GalImage Image) + { + switch (Image.Format & GalImageFormat.FormatMask) + { + case GalImageFormat.R32G32B32A32: + return Image.Width * Image.Height * 16; + + case GalImageFormat.R16G16B16A16: + case GalImageFormat.D32_S8: + case GalImageFormat.R32G32: + return Image.Width * Image.Height * 8; + + case GalImageFormat.A8B8G8R8: + case GalImageFormat.A8B8G8R8_SRGB: + case GalImageFormat.A2B10G10R10: + case GalImageFormat.R16G16: + case GalImageFormat.R32: + case GalImageFormat.D32: + case GalImageFormat.B10G11R11: + case GalImageFormat.D24_S8: + return Image.Width * Image.Height * 4; + + case GalImageFormat.B4G4R4A4: + case GalImageFormat.A1R5G5B5: + case GalImageFormat.B5G6R5: + case GalImageFormat.R8G8: + case GalImageFormat.R16: + case GalImageFormat.D16: + return Image.Width * Image.Height * 2; + + case GalImageFormat.R8: + return Image.Width * Image.Height; + + case GalImageFormat.BC1_RGBA: + case GalImageFormat.BC4: + { + return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 8); + } + + case GalImageFormat.BC6H_SF16: + case GalImageFormat.BC6H_UF16: + case GalImageFormat.BC7: + case GalImageFormat.BC2: + case GalImageFormat.BC3: + case GalImageFormat.BC5: + case GalImageFormat.ASTC_4x4: + return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 16); + + case GalImageFormat.ASTC_5x5: + return CompressedTextureSize(Image.Width, Image.Height, 5, 5, 16); + + case GalImageFormat.ASTC_6x6: + return CompressedTextureSize(Image.Width, Image.Height, 6, 6, 16); + + case GalImageFormat.ASTC_8x8: + return CompressedTextureSize(Image.Width, Image.Height, 8, 8, 16); + + case GalImageFormat.ASTC_10x10: + return CompressedTextureSize(Image.Width, Image.Height, 10, 10, 16); + + case GalImageFormat.ASTC_12x12: + return CompressedTextureSize(Image.Width, Image.Height, 12, 12, 16); + + case GalImageFormat.ASTC_5x4: + return CompressedTextureSize(Image.Width, Image.Height, 5, 4, 16); + + case GalImageFormat.ASTC_6x5: + return CompressedTextureSize(Image.Width, Image.Height, 6, 5, 16); + + case GalImageFormat.ASTC_8x6: + return CompressedTextureSize(Image.Width, Image.Height, 8, 6, 16); + + case GalImageFormat.ASTC_10x8: + return CompressedTextureSize(Image.Width, Image.Height, 10, 8, 16); + + case GalImageFormat.ASTC_12x10: + return CompressedTextureSize(Image.Width, Image.Height, 12, 10, 16); + + case GalImageFormat.ASTC_8x5: + return CompressedTextureSize(Image.Width, Image.Height, 8, 5, 16); + + case GalImageFormat.ASTC_10x5: + return CompressedTextureSize(Image.Width, Image.Height, 10, 5, 16); + + case GalImageFormat.ASTC_10x6: + return CompressedTextureSize(Image.Width, Image.Height, 10, 6, 16); + } + + throw new NotImplementedException((Image.Format & GalImageFormat.FormatMask).ToString()); + } + + public static bool HasColor(GalImageFormat Format) + { + return GetImageDescriptor(Format).HasColor; + } + + public static bool HasDepth(GalImageFormat Format) + { + return GetImageDescriptor(Format).HasDepth; + } + + public static bool HasStencil(GalImageFormat Format) + { + return GetImageDescriptor(Format).HasStencil; + } + + public static bool IsCompressed(GalImageFormat Format) + { + return GetImageDescriptor(Format).Compressed; + } + + private static ImageDescriptor GetImageDescriptor(GalImageFormat Format) + { + GalImageFormat TypeLess = (Format & GalImageFormat.FormatMask); + + if (s_ImageTable.TryGetValue(TypeLess, out ImageDescriptor Descriptor)) + { + return Descriptor; + } + + throw new NotImplementedException("Image with format " + TypeLess.ToString() + " not implemented"); + } + + private static GalImageFormat GetFormatType(GalTextureType Type) + { + switch (Type) + { + case GalTextureType.Snorm: return Snorm; + case GalTextureType.Unorm: return Unorm; + case GalTextureType.Sint: return Sint; + case GalTextureType.Uint: return Uint; + case GalTextureType.Float: return Sfloat; + + default: throw new NotImplementedException(((int)Type).ToString()); + } + } + + private static int CompressedTextureSize(int TextureWidth, int TextureHeight, int BlockWidth, int BlockHeight, int Bpb) + { + int W = (TextureWidth + (BlockWidth - 1)) / BlockWidth; + int H = (TextureHeight + (BlockHeight - 1)) / BlockHeight; + + return W * H * Bpb; + } + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/IntegerEncoded.cs b/Ryujinx.Graphics/Texture/IntegerEncoded.cs new file mode 100644 index 00000000..683cb770 --- /dev/null +++ b/Ryujinx.Graphics/Texture/IntegerEncoded.cs @@ -0,0 +1,269 @@ +using System.Collections; +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Texture +{ + public struct IntegerEncoded + { + public enum EIntegerEncoding + { + JustBits, + Quint, + Trit + } + + EIntegerEncoding Encoding; + public int NumberBits { get; private set; } + public int BitValue { get; private set; } + public int TritValue { get; private set; } + public int QuintValue { get; private set; } + + public IntegerEncoded(EIntegerEncoding _Encoding, int NumBits) + { + Encoding = _Encoding; + NumberBits = NumBits; + BitValue = 0; + TritValue = 0; + QuintValue = 0; + } + + public bool MatchesEncoding(IntegerEncoded Other) + { + return Encoding == Other.Encoding && NumberBits == Other.NumberBits; + } + + public EIntegerEncoding GetEncoding() + { + return Encoding; + } + + public int GetBitLength(int NumberVals) + { + int TotalBits = NumberBits * NumberVals; + if (Encoding == EIntegerEncoding.Trit) + { + TotalBits += (NumberVals * 8 + 4) / 5; + } + else if (Encoding == EIntegerEncoding.Quint) + { + TotalBits += (NumberVals * 7 + 2) / 3; + } + return TotalBits; + } + + public static IntegerEncoded CreateEncoding(int MaxVal) + { + while (MaxVal > 0) + { + int Check = MaxVal + 1; + + // Is maxVal a power of two? + if ((Check & (Check - 1)) == 0) + { + return new IntegerEncoded(EIntegerEncoding.JustBits, BitArrayStream.PopCnt(MaxVal)); + } + + // Is maxVal of the type 3*2^n - 1? + if ((Check % 3 == 0) && ((Check / 3) & ((Check / 3) - 1)) == 0) + { + return new IntegerEncoded(EIntegerEncoding.Trit, BitArrayStream.PopCnt(Check / 3 - 1)); + } + + // Is maxVal of the type 5*2^n - 1? + if ((Check % 5 == 0) && ((Check / 5) & ((Check / 5) - 1)) == 0) + { + return new IntegerEncoded(EIntegerEncoding.Quint, BitArrayStream.PopCnt(Check / 5 - 1)); + } + + // Apparently it can't be represented with a bounded integer sequence... + // just iterate. + MaxVal--; + } + + return new IntegerEncoded(EIntegerEncoding.JustBits, 0); + } + + public static void DecodeTritBlock( + BitArrayStream BitStream, + List<IntegerEncoded> ListIntegerEncoded, + int NumberBitsPerValue) + { + // Implement the algorithm in section C.2.12 + int[] m = new int[5]; + int[] t = new int[5]; + int T; + + // Read the trit encoded block according to + // table C.2.14 + m[0] = BitStream.ReadBits(NumberBitsPerValue); + T = BitStream.ReadBits(2); + m[1] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(2) << 2; + m[2] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(1) << 4; + m[3] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(2) << 5; + m[4] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(1) << 7; + + int C = 0; + + BitArrayStream Tb = new BitArrayStream(new BitArray(new int[] { T })); + if (Tb.ReadBits(2, 4) == 7) + { + C = (Tb.ReadBits(5, 7) << 2) | Tb.ReadBits(0, 1); + t[4] = t[3] = 2; + } + else + { + C = Tb.ReadBits(0, 4); + if (Tb.ReadBits(5, 6) == 3) + { + t[4] = 2; + t[3] = Tb.ReadBit(7); + } + else + { + t[4] = Tb.ReadBit(7); + t[3] = Tb.ReadBits(5, 6); + } + } + + BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); + if (Cb.ReadBits(0, 1) == 3) + { + t[2] = 2; + t[1] = Cb.ReadBit(4); + t[0] = (Cb.ReadBit(3) << 1) | (Cb.ReadBit(2) & ~Cb.ReadBit(3)); + } + else if (Cb.ReadBits(2, 3) == 3) + { + t[2] = 2; + t[1] = 2; + t[0] = Cb.ReadBits(0, 1); + } + else + { + t[2] = Cb.ReadBit(4); + t[1] = Cb.ReadBits(2, 3); + t[0] = (Cb.ReadBit(1) << 1) | (Cb.ReadBit(0) & ~Cb.ReadBit(1)); + } + + for (int i = 0; i < 5; i++) + { + IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Trit, NumberBitsPerValue) + { + BitValue = m[i], + TritValue = t[i] + }; + ListIntegerEncoded.Add(IntEncoded); + } + } + + public static void DecodeQuintBlock( + BitArrayStream BitStream, + List<IntegerEncoded> ListIntegerEncoded, + int NumberBitsPerValue) + { + // Implement the algorithm in section C.2.12 + int[] m = new int[3]; + int[] q = new int[3]; + int Q; + + // Read the trit encoded block according to + // table C.2.15 + m[0] = BitStream.ReadBits(NumberBitsPerValue); + Q = BitStream.ReadBits(3); + m[1] = BitStream.ReadBits(NumberBitsPerValue); + Q |= BitStream.ReadBits(2) << 3; + m[2] = BitStream.ReadBits(NumberBitsPerValue); + Q |= BitStream.ReadBits(2) << 5; + + BitArrayStream Qb = new BitArrayStream(new BitArray(new int[] { Q })); + if (Qb.ReadBits(1, 2) == 3 && Qb.ReadBits(5, 6) == 0) + { + q[0] = q[1] = 4; + q[2] = (Qb.ReadBit(0) << 2) | ((Qb.ReadBit(4) & ~Qb.ReadBit(0)) << 1) | (Qb.ReadBit(3) & ~Qb.ReadBit(0)); + } + else + { + int C = 0; + if (Qb.ReadBits(1, 2) == 3) + { + q[2] = 4; + C = (Qb.ReadBits(3, 4) << 3) | ((~Qb.ReadBits(5, 6) & 3) << 1) | Qb.ReadBit(0); + } + else + { + q[2] = Qb.ReadBits(5, 6); + C = Qb.ReadBits(0, 4); + } + + BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); + if (Cb.ReadBits(0, 2) == 5) + { + q[1] = 4; + q[0] = Cb.ReadBits(3, 4); + } + else + { + q[1] = Cb.ReadBits(3, 4); + q[0] = Cb.ReadBits(0, 2); + } + } + + for (int i = 0; i < 3; i++) + { + IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Quint, NumberBitsPerValue) + { + BitValue = m[i], + QuintValue = q[i] + }; + ListIntegerEncoded.Add(IntEncoded); + } + } + + public static void DecodeIntegerSequence( + List<IntegerEncoded> DecodeIntegerSequence, + BitArrayStream BitStream, + int MaxRange, + int NumberValues) + { + // Determine encoding parameters + IntegerEncoded IntEncoded = CreateEncoding(MaxRange); + + // Start decoding + int NumberValuesDecoded = 0; + while (NumberValuesDecoded < NumberValues) + { + switch (IntEncoded.GetEncoding()) + { + case EIntegerEncoding.Quint: + { + DecodeQuintBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); + NumberValuesDecoded += 3; + + break; + } + + case EIntegerEncoding.Trit: + { + DecodeTritBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); + NumberValuesDecoded += 5; + + break; + } + + case EIntegerEncoding.JustBits: + { + IntEncoded.BitValue = BitStream.ReadBits(IntEncoded.NumberBits); + DecodeIntegerSequence.Add(IntEncoded); + NumberValuesDecoded++; + + break; + } + } + } + } + } +} diff --git a/Ryujinx.Graphics/Texture/LinearSwizzle.cs b/Ryujinx.Graphics/Texture/LinearSwizzle.cs new file mode 100644 index 00000000..ef468e27 --- /dev/null +++ b/Ryujinx.Graphics/Texture/LinearSwizzle.cs @@ -0,0 +1,19 @@ +namespace Ryujinx.Graphics.Texture +{ + class LinearSwizzle : ISwizzle + { + private int Pitch; + private int Bpp; + + public LinearSwizzle(int Pitch, int Bpp) + { + this.Pitch = Pitch; + this.Bpp = Bpp; + } + + public int GetSwizzleOffset(int X, int Y) + { + return X * Bpp + Y * Pitch; + } + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureFactory.cs b/Ryujinx.Graphics/Texture/TextureFactory.cs new file mode 100644 index 00000000..fa7a0f80 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureFactory.cs @@ -0,0 +1,132 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; +using System; + +namespace Ryujinx.Graphics.Texture +{ + static class TextureFactory + { + public static GalImage MakeTexture(NvGpuVmm Vmm, long TicPosition) + { + int[] Tic = ReadWords(Vmm, TicPosition, 8); + + GalImageFormat Format = GetImageFormat(Tic); + + GalTextureSource XSource = (GalTextureSource)((Tic[0] >> 19) & 7); + GalTextureSource YSource = (GalTextureSource)((Tic[0] >> 22) & 7); + GalTextureSource ZSource = (GalTextureSource)((Tic[0] >> 25) & 7); + GalTextureSource WSource = (GalTextureSource)((Tic[0] >> 28) & 7); + + int Width = (Tic[4] & 0xffff) + 1; + int Height = (Tic[5] & 0xffff) + 1; + + return new GalImage( + Width, + Height, + Format, + XSource, + YSource, + ZSource, + WSource); + } + + public static byte[] GetTextureData(NvGpuVmm Vmm, long TicPosition) + { + int[] Tic = ReadWords(Vmm, TicPosition, 8); + + GalImageFormat Format = GetImageFormat(Tic); + + long TextureAddress = (uint)Tic[1]; + + TextureAddress |= (long)((ushort)Tic[2]) << 32; + + TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7); + + if (Swizzle == TextureSwizzle.BlockLinear || + Swizzle == TextureSwizzle.BlockLinearColorKey) + { + TextureAddress &= ~0x1ffL; + } + else if (Swizzle == TextureSwizzle.Pitch || + Swizzle == TextureSwizzle.PitchColorKey) + { + TextureAddress &= ~0x1fL; + } + + int Pitch = (Tic[3] & 0xffff) << 5; + + int BlockHeightLog2 = (Tic[3] >> 3) & 7; + int TileWidthLog2 = (Tic[3] >> 10) & 7; + + int BlockHeight = 1 << BlockHeightLog2; + int TileWidth = 1 << TileWidthLog2; + + int Width = (Tic[4] & 0xffff) + 1; + int Height = (Tic[5] & 0xffff) + 1; + + TextureInfo Texture = new TextureInfo( + TextureAddress, + Width, + Height, + Pitch, + BlockHeight, + TileWidth, + Swizzle, + Format); + + return TextureReader.Read(Vmm, Texture); + } + + public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition) + { + int[] Tsc = ReadWords(Vmm, TscPosition, 8); + + GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7); + GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7); + GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7); + + GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3); + GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3); + GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3); + + GalColorF BorderColor = new GalColorF( + BitConverter.Int32BitsToSingle(Tsc[4]), + BitConverter.Int32BitsToSingle(Tsc[5]), + BitConverter.Int32BitsToSingle(Tsc[6]), + BitConverter.Int32BitsToSingle(Tsc[7])); + + return new GalTextureSampler( + AddressU, + AddressV, + AddressP, + MinFilter, + MagFilter, + MipFilter, + BorderColor); + } + + private static GalImageFormat GetImageFormat(int[] Tic) + { + GalTextureType RType = (GalTextureType)((Tic[0] >> 7) & 7); + GalTextureType GType = (GalTextureType)((Tic[0] >> 10) & 7); + GalTextureType BType = (GalTextureType)((Tic[0] >> 13) & 7); + GalTextureType AType = (GalTextureType)((Tic[0] >> 16) & 7); + + GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); + + return ImageUtils.ConvertTexture(Format, RType, GType, BType, AType); + } + + private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count) + { + int[] Words = new int[Count]; + + for (int Index = 0; Index < Count; Index++, Position += 4) + { + Words[Index] = Vmm.ReadInt32(Position); + } + + return Words; + } + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureHelper.cs b/Ryujinx.Graphics/Texture/TextureHelper.cs new file mode 100644 index 00000000..8130ab41 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureHelper.cs @@ -0,0 +1,45 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; +using System; + +namespace Ryujinx.Graphics.Texture +{ + static class TextureHelper + { + public static ISwizzle GetSwizzle(TextureInfo Texture, int BlockWidth, int Bpp) + { + int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth; + + int AlignMask = Texture.TileWidth * (64 / Bpp) - 1; + + Width = (Width + AlignMask) & ~AlignMask; + + switch (Texture.Swizzle) + { + case TextureSwizzle._1dBuffer: + case TextureSwizzle.Pitch: + case TextureSwizzle.PitchColorKey: + return new LinearSwizzle(Texture.Pitch, Bpp); + + case TextureSwizzle.BlockLinear: + case TextureSwizzle.BlockLinearColorKey: + return new BlockLinearSwizzle(Width, Bpp, Texture.BlockHeight); + } + + throw new NotImplementedException(Texture.Swizzle.ToString()); + } + + public static (AMemory Memory, long Position) GetMemoryAndPosition( + IAMemory Memory, + long Position) + { + if (Memory is NvGpuVmm Vmm) + { + return (Vmm.Memory, Vmm.GetPhysicalAddress(Position)); + } + + return ((AMemory)Memory, Position); + } + } +} diff --git a/Ryujinx.Graphics/Texture/TextureInfo.cs b/Ryujinx.Graphics/Texture/TextureInfo.cs new file mode 100644 index 00000000..66445dcc --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureInfo.cs @@ -0,0 +1,60 @@ +using Ryujinx.Graphics.Gal; + +namespace Ryujinx.Graphics.Texture +{ + public struct TextureInfo + { + public long Position { get; private set; } + + public int Width { get; private set; } + public int Height { get; private set; } + public int Pitch { get; private set; } + + public int BlockHeight { get; private set; } + public int TileWidth { get; private set; } + + public TextureSwizzle Swizzle { get; private set; } + + public GalImageFormat Format { get; private set; } + + public TextureInfo( + long Position, + int Width, + int Height) + { + this.Position = Position; + this.Width = Width; + this.Height = Height; + + Pitch = 0; + + BlockHeight = 16; + + TileWidth = 1; + + Swizzle = TextureSwizzle.BlockLinear; + + Format = GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm; + } + + public TextureInfo( + long Position, + int Width, + int Height, + int Pitch, + int BlockHeight, + int TileWidth, + TextureSwizzle Swizzle, + GalImageFormat Format) + { + this.Position = Position; + this.Width = Width; + this.Height = Height; + this.Pitch = Pitch; + this.BlockHeight = BlockHeight; + this.TileWidth = TileWidth; + this.Swizzle = Swizzle; + this.Format = Format; + } + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureReader.cs b/Ryujinx.Graphics/Texture/TextureReader.cs new file mode 100644 index 00000000..dbaed1a8 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureReader.cs @@ -0,0 +1,398 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using System; + +namespace Ryujinx.Graphics.Texture +{ + delegate byte[] TextureReaderDelegate(IAMemory Memory, TextureInfo Texture); + + public static class TextureReader + { + public static byte[] Read(IAMemory Memory, TextureInfo Texture) + { + TextureReaderDelegate Reader = ImageUtils.GetReader(Texture.Format); + + return Reader(Memory, Texture); + } + + internal unsafe static byte[] Read1Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 1); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + byte Pixel = CpuMem.ReadByte(Position + Offset); + + *(BuffPtr + OutOffs) = Pixel; + + OutOffs++; + } + } + + return Output; + } + + internal unsafe static byte[] Read5551(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset); + + Pixel = (Pixel & 0x001f) << 11 | + (Pixel & 0x03e0) << 1 | + (Pixel & 0x7c00) >> 9 | + (Pixel & 0x8000) >> 15; + + *(short*)(BuffPtr + OutOffs) = (short)Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + internal unsafe static byte[] Read565(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset); + + Pixel = (Pixel & 0x001f) << 11 | + (Pixel & 0x07e0) | + (Pixel & 0xf800) >> 11; + + *(short*)(BuffPtr + OutOffs) = (short)Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + internal unsafe static byte[] Read2Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + short Pixel = CpuMem.ReadInt16(Position + Offset); + + *(short*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + internal unsafe static byte[] Read4Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 4]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + int Pixel = CpuMem.ReadInt32(Position + Offset); + + *(int*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 4; + } + } + + return Output; + } + + internal unsafe static byte[] Read8Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 8]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 8); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Pixel = CpuMem.ReadInt64(Position + Offset); + + *(long*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 8; + } + } + + return Output; + } + + internal unsafe static byte[] Read16Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 16]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 16); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long PxLow = CpuMem.ReadInt64(Position + Offset + 0); + long PxHigh = CpuMem.ReadInt64(Position + Offset + 8); + + *(long*)(BuffPtr + OutOffs + 0) = PxLow; + *(long*)(BuffPtr + OutOffs + 8) = PxHigh; + + OutOffs += 16; + } + } + + return Output; + } + + internal unsafe static byte[] Read8Bpt4x4(IAMemory Memory, TextureInfo Texture) + { + int Width = (Texture.Width + 3) / 4; + int Height = (Texture.Height + 3) / 4; + + byte[] Output = new byte[Width * Height * 8]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 4, 8); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Tile = CpuMem.ReadInt64(Position + Offset); + + *(long*)(BuffPtr + OutOffs) = Tile; + + OutOffs += 8; + } + } + + return Output; + } + + internal unsafe static byte[] Read16BptCompressedTexture(IAMemory Memory, TextureInfo Texture, int BlockWidth, int BlockHeight) + { + int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth; + int Height = (Texture.Height + (BlockHeight - 1)) / BlockHeight; + + byte[] Output = new byte[Width * Height * 16]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, BlockWidth, 16); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Tile0 = CpuMem.ReadInt64(Position + Offset + 0); + long Tile1 = CpuMem.ReadInt64(Position + Offset + 8); + + *(long*)(BuffPtr + OutOffs + 0) = Tile0; + *(long*)(BuffPtr + OutOffs + 8) = Tile1; + + OutOffs += 16; + } + } + + return Output; + } + + internal static byte[] Read16BptCompressedTexture4x4(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 4, 4); + } + + internal static byte[] Read16BptCompressedTexture5x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 5, 5); + } + + internal static byte[] Read16BptCompressedTexture6x6(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 6, 6); + } + + internal static byte[] Read16BptCompressedTexture8x8(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 8, 8); + } + + internal static byte[] Read16BptCompressedTexture10x10(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 10); + } + + internal static byte[] Read16BptCompressedTexture12x12(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 12, 12); + } + + internal static byte[] Read16BptCompressedTexture5x4(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 5, 4); + } + + internal static byte[] Read16BptCompressedTexture6x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 6, 5); + } + + internal static byte[] Read16BptCompressedTexture8x6(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 8, 6); + } + + internal static byte[] Read16BptCompressedTexture10x8(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 8); + } + + internal static byte[] Read16BptCompressedTexture12x10(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 12, 10); + } + + internal static byte[] Read16BptCompressedTexture8x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 5, 5); + } + + internal static byte[] Read16BptCompressedTexture10x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 5); + } + + internal static byte[] Read16BptCompressedTexture10x6(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 6); + } + } +} diff --git a/Ryujinx.Graphics/Texture/TextureSwizzle.cs b/Ryujinx.Graphics/Texture/TextureSwizzle.cs new file mode 100644 index 00000000..c67a5367 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureSwizzle.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.Graphics.Texture +{ + public enum TextureSwizzle + { + _1dBuffer = 0, + PitchColorKey = 1, + Pitch = 2, + BlockLinear = 3, + BlockLinearColorKey = 4 + } +}
\ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureWriter.cs b/Ryujinx.Graphics/Texture/TextureWriter.cs new file mode 100644 index 00000000..16e78c56 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureWriter.cs @@ -0,0 +1,35 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; + +namespace Ryujinx.Graphics.Texture +{ + static class TextureWriter + { + public unsafe static void Write(IAMemory Memory, TextureInfo Texture, byte[] Data) + { + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Data) + { + long InOffs = 0; + + for (int Y = 0; Y < Texture.Height; Y++) + for (int X = 0; X < Texture.Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + int Pixel = *(int*)(BuffPtr + InOffs); + + CpuMem.WriteInt32(Position + Offset, Pixel); + + InOffs += 4; + } + } + } + } +} |
