public BRTI(FileData f) //Docs thanks to gdkchan!! { ImageKey = "texture"; SelectedImageKey = "texture"; f.skip(4); int BRTISize1 = f.readInt(); long BRTISize2 = (f.readInt() | f.readInt() << 32); surf = new Swizzle.Surface(); surf.tileMode = (sbyte)f.readByte(); surf.dim = (sbyte)f.readByte(); ushort Flags = (ushort)f.readShort(); surf.swizzle = (ushort)f.readShort(); surf.numMips = (ushort)f.readShort(); uint unk18 = (uint)f.readInt(); surf.format = (uint)f.readInt(); DataType = (byte)(surf.format & 0xFF); uint unk20 = (uint)f.readInt(); surf.width = f.readInt(); surf.height = f.readInt(); surf.depth = f.readInt(); int FaceCount = f.readInt(); surf.sizeRange = f.readInt(); uint unk38 = (uint)f.readInt(); uint unk3C = (uint)f.readInt(); uint unk40 = (uint)f.readInt(); uint unk44 = (uint)f.readInt(); uint unk48 = (uint)f.readInt(); uint unk4C = (uint)f.readInt(); surf.imageSize = f.readInt(); surf.alignment = f.readInt(); int ChannelType = f.readInt(); int TextureType = f.readInt(); Text = f.readString((f.readInt() | f.readInt() << 32) + BNTX.temp + 2, -1); long ParentOffset = f.readInt() | f.readInt() << 32; long PtrsOffset = f.readInt() | f.readInt() << 32; format = surf.format; f.seek((int)PtrsOffset + BNTX.temp); long dataOff = f.readInt() | f.readInt() << 32; surf.data = f.getSection((int)dataOff + BNTX.temp, surf.imageSize); //Console.WriteLine(surf.data.Length + " " + dataOff.ToString("x") + " " + surf.imageSize); uint blk_dim = Formats.blk_dims(surf.format >> 8); blkWidth = blk_dim >> 4; blkHeight = blk_dim & 0xF; bpp = Formats.bpps(surf.format >> 8); // Console.WriteLine($"{Name} Height {surf.height}wdith = {surf.width}allignment = {surf.alignment}blkwidth = {blkWidth}blkheight = {blkHeight}blkdims = {blk_dim} format = {surf.format} datatype = {DataType} dataoffset = {dataOff}"); // byte[] result = surf.data; byte[] result = Swizzle.deswizzle((uint)surf.width, (uint)surf.height, blkWidth, blkHeight, bpp, (uint)surf.tileMode, (uint)surf.alignment, surf.sizeRange, surf.data, 0); uint width = Swizzle.DIV_ROUND_UP((uint)surf.width, blkWidth); uint height = Swizzle.DIV_ROUND_UP((uint)surf.height, blkHeight); result_ = new byte[width * height * bpp]; Array.Copy(result, 0, result_, 0, width * height * bpp); byte[] Swizzled = Swizzle.swizzle((uint)surf.width, (uint)surf.height, blkWidth, blkHeight, bpp, (uint)surf.tileMode, (uint)surf.alignment, surf.sizeRange, surf.data, 1); texture.data = result_; texture.width = surf.width; texture.height = surf.height; Width = surf.width; Height = surf.height; texture.mipmaps.Add(result_); switch (surf.format >> 8) { case ((uint)Formats.BNTXImageFormat.IMAGE_FORMAT_BC1): if (DataType == (byte)Formats.BNTXImageTypes.UNORM) { texture.type = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext; } else if (DataType == (byte)Formats.BNTXImageTypes.SRGB) { byte[] fixBC1 = DDS_Decompress.DecompressBC1(texture.data, texture.width, texture.height, true); texture.data = fixBC1; texture.type = PixelInternalFormat.Rgba; texture.utype = OpenTK.Graphics.OpenGL.PixelFormat.Rgba; } else { throw new Exception("Unsupported data type for BC1"); } break; case ((uint)Formats.BNTXImageFormat.IMAGE_FORMAT_BC2): if (DataType == (byte)Formats.BNTXImageTypes.UNORM) { texture.type = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext; } else if (DataType == (byte)Formats.BNTXImageTypes.SRGB) { texture.type = PixelInternalFormat.CompressedSrgbAlphaS3tcDxt3Ext; } else { throw new Exception("Unsupported data type for BC2"); } break; case ((uint)Formats.BNTXImageFormat.IMAGE_FORMAT_BC3): if (DataType == (byte)Formats.BNTXImageTypes.UNORM) { texture.type = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext; } else if (DataType == (byte)Formats.BNTXImageTypes.SRGB) { texture.type = PixelInternalFormat.CompressedSrgbAlphaS3tcDxt5Ext; } else { throw new Exception("Unsupported data type for BC3"); } break; case ((uint)Formats.BNTXImageFormat.IMAGE_FORMAT_BC4): if (DataType == (byte)Formats.BNTXImageTypes.UNORM) { texture.type = PixelInternalFormat.CompressedRedRgtc1; // byte[] fixBC4 = DecompressBC4(texture.data, texture.width, texture.height, false); // texture.data = fixBC4; // texture.type = PixelInternalFormat.Rgba; // texture.utype = OpenTK.Graphics.OpenGL.PixelFormat.Rgba; } else if (DataType == (byte)Formats.BNTXImageTypes.SNORM) { texture.type = PixelInternalFormat.CompressedSignedRedRgtc1; // byte[] fixBC4 = DecompressBC4(texture.data, texture.width, texture.height, true); // texture.data = fixBC4; // texture.type = PixelInternalFormat.Rgba; // texture.utype = OpenTK.Graphics.OpenGL.PixelFormat.Rgba; } else { throw new Exception("Unsupported data type for BC4"); } break; case ((uint)Formats.BNTXImageFormat.IMAGE_FORMAT_BC5): if (DataType == (byte)Formats.BNTXImageTypes.UNORM) { // byte[] fixBC5 = DecompressBC5(texture.data, texture.width, texture.height, false); // texture.data = fixBC5; texture.type = PixelInternalFormat.CompressedRgRgtc2; // texture.utype = OpenTK.Graphics.OpenGL.PixelFormat.Rgba; } else if (DataType == (byte)Formats.BNTXImageTypes.SNORM) { byte[] fixBC5 = DDS_Decompress.DecompressBC5(texture.data, texture.width, texture.height, true); texture.data = fixBC5; texture.type = PixelInternalFormat.Rgba; texture.utype = OpenTK.Graphics.OpenGL.PixelFormat.Rgba; } else { Console.WriteLine("Unsupported data type for BC5"); } break; case ((uint)Formats.BNTXImageFormat.IMAGE_FORMAT_BC7): if (DataType == (byte)Formats.BNTXImageTypes.UNORM || DataType == (byte)Formats.BNTXImageTypes.SRGB) { texture.type = PixelInternalFormat.CompressedRgbaBptcUnorm; } break; case ((uint)Formats.BNTXImageFormat.IMAGE_FORMAT_R8_G8_B8_A8): if (DataType == (byte)Formats.BNTXImageTypes.UNORM || DataType == (byte)Formats.BNTXImageTypes.SRGB) { texture.type = PixelInternalFormat.Rgba; texture.utype = OpenTK.Graphics.OpenGL.PixelFormat.Rgba; } else { throw new Exception("Unsupported data type for R8_G8_B8_A8"); } break; } texture.display = loadImage(texture); display = texture.display; RenderableTex.Add(texture); }
public void Read(FileData f, BNTX bntx) //Docs thanks to AboodXD!! { ImageKey = "texture"; SelectedImageKey = "texture"; f.skip(4); int BRTISize1 = f.readInt(); long BRTISize2 = f.readInt64(); surf = new TegraX1Swizzle.Surface(); ushort Flags = (ushort)f.readShort(); surf.dim = (sbyte)f.readByte(); surf.tileMode = (sbyte)f.readByte(); surf.swizzle = (ushort)f.readShort(); surf.numMips = (ushort)f.readShort(); uint numSamples = (uint)f.readInt(); surf.format = (uint)f.readInt(); DataType = (byte)(surf.format & 0xFF); uint accessFlags = (uint)f.readInt(); surf.width = f.readInt(); surf.height = f.readInt(); surf.depth = f.readInt(); int FaceCount = f.readInt(); surf.sizeRange = f.readInt(); uint unk38 = (uint)f.readInt(); uint unk3C = (uint)f.readInt(); uint unk40 = (uint)f.readInt(); uint unk44 = (uint)f.readInt(); uint unk48 = (uint)f.readInt(); uint unk4C = (uint)f.readInt(); surf.imageSize = f.readInt(); surf.alignment = f.readInt(); int ChannelType = f.readInt(); int TextureType = f.readInt(); Text = f.readString((int)f.readInt64() + BNTX.temp + 2, -1); long ParentOffset = f.readInt64(); long PtrsOffset = f.readInt64(); format = surf.format; surf.data = new List <byte[]>(); uint blk_dim = Formats.blk_dims(surf.format >> 8); blkWidth = blk_dim >> 4; blkHeight = blk_dim & 0xF; f.seek((int)PtrsOffset + BNTX.temp); long firstMipOffset = f.readInt64(); surf.data.Add(f.getSection((int)firstMipOffset + BNTX.temp, surf.imageSize)); for (int mipLevel = 1; mipLevel < surf.numMips; mipLevel++) { long dataOff = f.readInt64(); surf.data.Add(f.getSection((int)dataOff + BNTX.temp, (int)firstMipOffset + surf.imageSize - (int)dataOff)); // Debug.WriteLine($"{Name} Height {surf.height}wdith = {surf.width}allignment = {surf.alignment}blkwidth = {blkWidth}blkheight = {blkHeight}blkdims = {blk_dim} format = {surf.format} datatype = {DataType} dataoffset = {dataOff}"); } bpp = Formats.bpps(surf.format >> 8); int target = 0; if (bntx.target == "NX ") { target = 1; } int blockHeightLog2 = surf.sizeRange & 7; int linesPerBlockHeight = (1 << blockHeightLog2) * 8; int blockHeightShift = 0; for (int mipLevel = 0; mipLevel < surf.numMips; mipLevel++) { uint width = (uint)Math.Max(1, surf.width >> mipLevel); uint height = (uint)Math.Max(1, surf.height >> mipLevel); uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp; if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight) { blockHeightShift += 1; } byte[] result = TegraX1Swizzle.deswizzle(width, height, blkWidth, blkHeight, target, bpp, (uint)surf.tileMode, (uint)surf.alignment, Math.Max(0, blockHeightLog2 - blockHeightShift), surf.data[mipLevel], 0); //Create a copy and use that to remove uneeded data result_ = new byte[size]; Array.Copy(result, 0, result_, 0, size); texture.mipmaps.Add(result_); } LoadFormats(texture, surf.format, 0, surf.width, surf.height); texture.width = surf.width; texture.height = surf.height; Width = surf.width; Height = surf.height; }