Esempio n. 1
0
            public override void Replace(string FileName)
            {
                var bntxFile = new BNTX();
                var tex      = new TextureData();

                tex.Replace(FileName, MipCount, 0, Format);

                //If it's null, the operation is cancelled
                if (tex.Texture == null)
                {
                    return;
                }

                var surfacesNew = tex.GetSurfaces();
                var surfaces    = GetSurfaces();

                ImageData = tex.Texture.TextureData[0][0];

                Width    = tex.Texture.Width;
                Height   = tex.Texture.Height;
                MipCount = tex.Texture.MipCount;

                Format    = tex.Format;
                XTXFormat = ConvertFromGenericFormat(tex.Format);

                MipOffsets = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length)[0];

                surfacesNew.Clear();
                surfaces.Clear();

                UpdateEditor();
            }
Esempio n. 2
0
        public override void Replace(string FileName)
        {
            var bntxFile = new BNTX();
            var tex      = new TextureData();

            tex.Replace(FileName, MipCount, 0, Format);

            //If it's null, the operation is cancelled
            if (tex.Texture == null)
            {
                return;
            }

            var surfacesNew = tex.GetSurfaces();
            var surfaces    = GetSurfaces();

            if (LimitFileSize)
            {
                if (surfaces[0].mipmaps[0].Length != surfacesNew[0].mipmaps[0].Length)
                {
                    throw new Exception($"Image must be the same size! {surfaces[0].mipmaps[0].Length}");
                }

                if (mipSizes[0].Length != surfacesNew[0].mipmaps.Count)
                {
                    throw new Exception($"Mip map count must be the same! {mipSizes[0].Length}");
                }

                if (Width != tex.Texture.Width || Height != tex.Texture.Height)
                {
                    throw new Exception("Image size must be the same!");
                }

                ImageData = tex.Texture.TextureData[0][0];

                Width    = tex.Texture.Width;
                Height   = tex.Texture.Height;
                MipCount = tex.Texture.MipCount;
            }
            else
            {
                ImageData = tex.Texture.TextureData[0][0];

                Width    = tex.Texture.Width;
                Height   = tex.Texture.Height;
                MipCount = tex.Texture.MipCount;

                Format    = tex.Format;
                NutFormat = ConvertGenericToNutFormat(tex.Format);

                mipSizes = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length);
            }

            surfacesNew.Clear();
            surfaces.Clear();

            UpdateEditor();
        }
Esempio n. 3
0
        public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
        {
            if (!Runtime.NUTEXBSettings.IsSwizzled)
            {
                return(DDS.GetArrayFaces(this, ImageData, ArrayCount)[ArrayLevel].mipmaps[MipLevel]);
            }

            return(TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, DepthLevel, 1));
        }
Esempio n. 4
0
        public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
        {
            if (!IsSwizzled)
            {
                return(DDS.GetArrayFaces(this, ImageData, 1)[ArrayLevel].mipmaps[0]);
            }

            return(TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, DepthLevel, 1));
        }
Esempio n. 5
0
        public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
        {
            if (Alignment == 0)
            {
                return(DDS.GetArrayFaces(this, ImageData, ArrayCount)[ArrayLevel].mipmaps[MipLevel]);
            }

            return(TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, DepthLevel, 1));
        }
Esempio n. 6
0
            public void LoadTexture(int target = 1)
            {
                mipmaps.Clear();

                uint blk_dim   = blk_dims(Format);
                uint blkWidth  = blk_dim >> 4;
                uint blkHeight = blk_dim & 0xF;

                uint blockHeight     = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
                uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
                uint tileMode        = 0;

                int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;

                uint bpp = bpps(Format);

                for (int arrayLevel = 0; arrayLevel < blocksCompressed.Count; arrayLevel++)
                {
                    int blockHeightShift = 0;

                    List <byte[]> mips = new List <byte[]>();
                    for (int mipLevel = 0; mipLevel < blocksCompressed[arrayLevel].Count; mipLevel++)
                    {
                        uint width  = (uint)Math.Max(1, Width >> mipLevel);
                        uint height = (uint)Math.Max(1, 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;
                        }

                        Console.WriteLine($"{blk_dim.ToString("x")} {bpp} {width} {height} {linesPerBlockHeight} {blkWidth} {blkHeight} {size} { blocksCompressed[arrayLevel][mipLevel].Length}");

                        try
                        {
                            byte[] result = TegraX1Swizzle.deswizzle(width, height, blkWidth, blkHeight, target, bpp, tileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), blocksCompressed[arrayLevel][mipLevel]);
                            //Create a copy and use that to remove uneeded data
                            byte[] result_ = new byte[size];
                            Array.Copy(result, 0, result_, 0, size);

                            mips.Add(result_);
                        }
                        catch (Exception e)
                        {
                            System.Windows.Forms.MessageBox.Show("Failed to swizzle texture!");
                            Console.WriteLine(e);
                            BadSwizzle = true;
                            break;
                        }
                    }
                    mipmaps.Add(mips);
                }
            }
Esempio n. 7
0
 public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
 {
     if (PlatformSwizzle == PlatformSwizzle.Platform_Switch)
     {
         return(TegraX1Swizzle.GetImageData(this, IGZStructure.TextureInfo.ImageData, ArrayLevel, MipLevel, 1));
     }
     else
     {
         return(IGZStructure.TextureInfo.ImageData);
     }
 }
Esempio n. 8
0
 public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
 {
     if (PlatformSwizzle == PlatformSwizzle.Platform_Switch)
     {
         return(TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, (int)Target));
     }
     else
     {
         return(ImageData);
     }
 }
Esempio n. 9
0
        public void LoadTexture(Texture tex, int target = 1)
        {
            Surfaces.Clear();

            try
            {
                uint blk_dim   = Formats.blk_dims((uint)((int)tex.Format >> 8));
                uint blkWidth  = blk_dim >> 4;
                uint blkHeight = blk_dim & 0xF;

                int linesPerBlockHeight = (1 << (int)tex.BlockHeightLog2) * 8;

                uint bpp = Formats.bpps((uint)((int)tex.Format >> 8));
                for (int arrayLevel = 0; arrayLevel < tex.ArrayLength; arrayLevel++)
                {
                    int           blockHeightShift = 0;
                    List <byte[]> mips             = new List <byte[]>();
                    for (int mipLevel = 0; mipLevel < tex.TextureData[arrayLevel].Count; mipLevel++)
                    {
                        uint width  = (uint)Math.Max(1, tex.Width >> mipLevel);
                        uint height = (uint)Math.Max(1, tex.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)tex.TileMode, (int)Math.Max(0, tex.BlockHeightLog2 - blockHeightShift), tex.TextureData[arrayLevel][mipLevel]);
                        //Create a copy and use that to remove uneeded data
                        byte[] result_ = new byte[size];
                        Array.Copy(result, 0, result_, 0, size);
                        mips.Add(result_);
                    }
                    Surfaces.Add(new Surface()
                    {
                        mipmaps = mips
                    });
                }

                Texture = tex;
            }
            catch (Exception e)
            {
                MessageBox.Show($"Failed to swizzle texture {Text}! Exception: {e}");
            }
        }
Esempio n. 10
0
                public void LoadTexture()
                {
                    mipmaps.Clear();

                    Console.WriteLine(Format);

                    uint blk_dim   = XTXFormats.blk_dims((uint)((int)Format >> 8));
                    uint blkWidth  = blk_dim >> 4;
                    uint blkHeight = blk_dim & 0xF;

                    uint blockHeight     = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
                    uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;

                    int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;

                    int TileMode = 0;

                    uint bpp = XTXFormats.bpps((uint)Format);

                    int blockHeightShift = 0;

                    for (int mipLevel = 0; mipLevel < MipCount; mipLevel++)
                    {
                        uint width  = (uint)Math.Max(1, Width >> mipLevel);
                        uint height = (uint)Math.Max(1, Height >> mipLevel);

                        //  uint size = width * height * bpp;
                        uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;

                        byte[] mipData = GetMipBlock(MipOffsets[mipLevel], size);

                        if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
                        {
                            blockHeightShift += 1;
                        }

                        byte[] result = TegraX1Swizzle.deswizzle(width, height, blkWidth, blkHeight, (int)Target, bpp, (uint)TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), mipData);
                        //Create a copy and use that to remove uneeded data
                        byte[] result_ = new byte[size];
                        Array.Copy(result, 0, result_, 0, size);

                        mipmaps.Add(result_);
                        Console.WriteLine("bpp " + bpp);
                        Console.WriteLine("result_ " + size);
                        Console.WriteLine("width " + width);
                        Console.WriteLine("height " + height);
                    }
                }
Esempio n. 11
0
            public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
            {
                if (Texture.ImageData == null)
                {
                    if (!hasShownDialog)
                    {
                        MessageBox.Show("No image data found! Put this next to it's data file (.wtp)");
                        hasShownDialog = true;
                    }
                    return(new byte[Texture.Info.ImageSize]);
                }

                Console.WriteLine($" Texture.ImageData " + Texture.ImageData.Length);

                var BlockHeightLog2 = Texture.Info.textureLayout & 7;

                return(TegraX1Swizzle.GetImageData(this, Texture.ImageData, ArrayLevel, MipLevel, DepthLevel, BlockHeightLog2, 1));
            }
Esempio n. 12
0
            public override void Replace(string FileName)
            {
                var tex = new TextureData();

                tex.Replace(FileName, MipCount, 0, Format);

                //If it's null, the operation is cancelled
                if (tex.Texture == null)
                {
                    return;
                }

                var surfacesNew = tex.GetSurfaces();
                var surfaces    = GetSurfaces();

                ImageData = tex.Texture.TextureData[0][0];

                Width           = tex.Texture.Width;
                Height          = tex.Texture.Height;
                MipCount        = tex.Texture.MipCount;
                TextureLayout1  = tex.Texture.textureLayout;
                TextureLayout2  = tex.Texture.textureLayout2;
                BlockHeightLog2 = tex.Texture.BlockHeightLog2;

                Format    = tex.Format;
                XTXFormat = ConvertFromGenericFormat(tex.Format);

                uint[] mips = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length)[0];
                MipOffsets = new uint[17];

                for (int i = 0; i < mips.Length; i++)
                {
                    MipOffsets[i] = mips[i];
                }

                surfacesNew.Clear();
                surfaces.Clear();

                UpdateEditor();
            }
Esempio n. 13
0
        public override void Replace(string FileName)
        {
            var bntxFile = new BNTX();
            var tex      = new TextureData();

            tex.Replace(FileName, MipCount, 0, Format);

            //If it's null, the operation is cancelled
            if (tex.Texture == null)
            {
                return;
            }

            List <byte[]> data = new List <byte[]>();

            foreach (var array in tex.Texture.TextureData)
            {
                data.Add(array[0]);
            }

            var output = Utils.CombineByteArray(data.ToArray());

            Width    = tex.Texture.Width;
            Height   = tex.Texture.Height;
            MipCount = tex.Texture.MipCount;
            // ArrayCount = tex.Texture.ArrayLength;
            // Depth = tex.Texture.Depth;

            Format    = tex.Format;
            NutFormat = ConvertGenericToNutFormat(tex.Format);

            mipSizes = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length);

            ImageData = SetImageData(output);

            data.Clear();
            UpdateEditor();
        }
Esempio n. 14
0
        public override void Replace(string FileName)
        {
            var bntxFile = new BNTX();
            var tex      = new TextureData();

            tex.Replace(FileName, MipCount, 0, Format);

            //If it's null, the operation is cancelled
            if (tex.Texture == null)
            {
                return;
            }

            var surfacesNew = tex.GetSurfaces();
            var surfaces    = GetSurfaces();

            if (LimitFileSize)
            {
                if (surfaces[0].mipmaps[0].Length < surfacesNew[0].mipmaps[0].Length)
                {
                    if (surfaces[0].mipmaps[0].Length != surfacesNew[0].mipmaps[0].Length)
                    {
                        throw new Exception($"Image must be the same size! {surfaces[0].mipmaps[0].Length}");
                    }

                    if (mipSizes[0].Length != surfacesNew[0].mipmaps.Count)
                    {
                        throw new Exception($"Mip map count must be the same! {mipSizes[0].Length}");
                    }

                    if (Width != tex.Texture.Width || Height != tex.Texture.Height)
                    {
                        throw new Exception("Image size must be the same!");
                    }
                }

                Width    = tex.Texture.Width;
                Height   = tex.Texture.Height;
                MipCount = tex.Texture.MipCount;
            }
            else
            {
                Width      = tex.Texture.Width;
                Height     = tex.Texture.Height;
                MipCount   = tex.Texture.MipCount;
                ArrayCount = tex.Texture.ArrayLength;
                Depth      = tex.Texture.Depth;

                Format    = tex.Format;
                NutFormat = ConvertGenericToNutFormat(tex.Format);

                mipSizes = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length);
            }

            List <byte[]> data = new List <byte[]>();

            foreach (var array in tex.Texture.TextureData)
            {
                data.Add(array[0]);
            }

            var output = Utils.CombineByteArray(data.ToArray());

            ImageData = SetImageData(output);

            data.Clear();
            surfacesNew.Clear();
            surfaces.Clear();

            UpdateEditor();
        }
Esempio n. 15
0
 public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
 {
     return(TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, (int)Target));
 }
Esempio n. 16
0
            public void LoadTexture()
            {
                Surfaces.Clear();

                Console.WriteLine(Format);

                uint blk_dim   = XTXFormats.blk_dims((uint)((int)Format >> 8));
                uint blkWidth  = blk_dim >> 4;
                uint blkHeight = blk_dim & 0xF;

                uint blockHeight     = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
                uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;

                int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;

                int TileMode = 0;

                uint          bpp  = XTXFormats.bpps((uint)Format);
                List <byte[]> mips = new List <byte[]>();

                int blockHeightShift = 0;

                for (int mipLevel = 0; mipLevel < MipOffsets.Length; mipLevel++)
                {
                    uint width  = (uint)Math.Max(1, Width >> mipLevel);
                    uint height = (uint)Math.Max(1, Height >> mipLevel);

                    //  uint size = width * height * bpp;
                    uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;

                    byte[] Output = new byte[size];

                    uint mipOffset;
                    if (mipLevel != 0)
                    {
                        mipOffset = (MipOffsets[mipLevel - 1]);
                        if (mipLevel == 1)
                        {
                            mipOffset -= (uint)size;
                        }

                        Array.Copy(ImageData, mipOffset, Output, 0, size);
                    }
                    else
                    {
                        Output = ImageData;
                    }

                    byte[] output = new byte[size];
                    Console.WriteLine(mipLevel + " " + size);

                    if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
                    {
                        blockHeightShift += 1;
                    }

                    byte[] result = TegraX1Swizzle.deswizzle(width, height, blkWidth, blkHeight, (int)Target, bpp, (uint)TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), Output);
                    //Create a copy and use that to remove uneeded data
                    byte[] result_ = new byte[size];
                    Array.Copy(result, 0, result_, 0, size);

                    mips.Add(result_);
                }
                Surfaces.Add(new Surface()
                {
                    mipmaps = mips
                });
            }
Esempio n. 17
0
            public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
            {
                Console.WriteLine($" Texture.ImageData " + Texture.ImageData.Length);

                return(TegraX1Swizzle.GetImageData(this, Texture.ImageData, ArrayLevel, MipLevel, 1));
            }
Esempio n. 18
0
 public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
 {
     return(TegraX1Swizzle.GetImageData(this, ImageData, ArrayLevel, MipLevel, DepthLevel, BlockHeightLog2, (int)Target));
 }
Esempio n. 19
0
        public void LoadTexture(int target = 1)
        {
            Surfaces.Clear();

            uint blk_dim   = blk_dims((byte)Format);
            uint blkWidth  = blk_dim >> 4;
            uint blkHeight = blk_dim & 0xF;

            uint blockHeight     = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
            uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
            uint tileMode        = 0;

            int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;

            uint bpp = bpps((byte)Format);

            for (int arrayLevel = 0; arrayLevel < mipSizes.Count; arrayLevel++)
            {
                int  blockHeightShift = 0;
                uint mipOffset        = 0;

                List <byte[]> mips = new List <byte[]>();
                for (int mipLevel = 0; mipLevel < mipSizes[arrayLevel].Length; mipLevel++)
                {
                    //Get the size from the size array
                    int MipSize = (int)mipSizes[arrayLevel][mipLevel];

                    //Align the size
                    if (mipLevel == 0)
                    {
                        if (MipSize % Alignment != 0)
                        {
                            MipSize = MipSize + (Alignment - (MipSize % Alignment));
                        }
                    }

                    uint width  = (uint)Math.Max(1, Width >> mipLevel);
                    uint height = (uint)Math.Max(1, 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;
                    }

                    Console.WriteLine($"{blk_dim.ToString("x")} {bpp} {width} {height} {linesPerBlockHeight} {blkWidth} {blkHeight} {size} { ImageData.Length}");

                    try
                    {
                        byte[] result = TegraX1Swizzle.deswizzle(width, height, blkWidth, blkHeight, target, bpp, tileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), ImageData);
                        //Create a copy and use that to remove uneeded data
                        byte[] result_ = new byte[size];
                        Array.Copy(result, mipOffset, result_, 0, size);

                        mips.Add(result_);
                    }
                    catch (Exception e)
                    {
                        System.Windows.Forms.MessageBox.Show($"Failed to swizzle texture {Text}!");
                        Console.WriteLine(e);
                        BadSwizzle = true;
                        break;
                    }
                    mipOffset += (uint)MipSize;
                    break;
                }
                Surfaces.Add(new Surface()
                {
                    mipmaps = mips
                });
            }
        }
        public static List <byte[]> SwizzleSurfaceMipMaps(Texture tex, byte[] data, TileMode TileMode)
        {
            int  blockHeightShift    = 0;
            int  target              = 1;
            uint Pitch               = 0;
            uint SurfaceSize         = 0;
            uint blockHeight         = 0;
            uint blk_dim             = Formats.blk_dims((uint)((int)tex.Format >> 8));
            uint blkWidth            = blk_dim >> 4;
            uint blkHeight           = blk_dim & 0xF;
            uint linesPerBlockHeight = 0;

            uint bpp = Formats.bpps((uint)((int)tex.Format >> 8));

            if ((int)TileMode == 1)
            {
                blockHeight         = 1;
                tex.BlockHeightLog2 = 0;
                tex.Alignment       = 1;

                linesPerBlockHeight   = 1;
                tex.ReadTextureLayout = 0;
            }
            else
            {
                blockHeight         = TegraX1Swizzle.GetBlockHeight(DIV_ROUND_UP(tex.Height, blkHeight));
                tex.BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
                Console.WriteLine("BlockHeightLog2 " + tex.BlockHeightLog2);
                Console.WriteLine("blockHeight " + blockHeight);
                tex.Alignment         = 512;
                tex.ReadTextureLayout = 1;

                linesPerBlockHeight = blockHeight * 8;
            }

            List <byte[]> mipmaps = new List <byte[]>();

            for (int mipLevel = 0; mipLevel < tex.MipCount; mipLevel++)
            {
                var  result = GetCurrentMipSize(tex.Width, tex.Height, blkWidth, blkHeight, bpp, mipLevel);
                uint offset = result.Item1;
                uint size   = result.Item2;

                byte[] data_ = SubArray(data, offset, size);

                uint width_  = Math.Max(1, tex.Width >> mipLevel);
                uint height_ = Math.Max(1, tex.Height >> mipLevel);

                uint width__  = DIV_ROUND_UP(width_, blkWidth);
                uint height__ = DIV_ROUND_UP(height_, blkHeight);


                byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, (uint)tex.Alignment) - SurfaceSize)];
                SurfaceSize += (uint)AlignedData.Length;

                Console.WriteLine("SurfaceSize Aligned " + AlignedData);

                Console.WriteLine("MipOffsets " + SurfaceSize);

                tex.MipOffsets[mipLevel] = SurfaceSize;
                if (tex.TileMode == TileMode.LinearAligned)
                {
                    Pitch = width__ * bpp;

                    Console.WriteLine("Pitch 1 " + Pitch);

                    if (target == 1)
                    {
                        Pitch = TegraX1Swizzle.round_up(width__ * bpp, 32);
                        Console.WriteLine("Pitch 2 " + Pitch);
                    }

                    SurfaceSize += Pitch * height__;
                }
                else
                {
                    if (TegraX1Swizzle.pow2_round_up(height__) < linesPerBlockHeight)
                    {
                        blockHeightShift += 1;
                    }

                    Pitch = TegraX1Swizzle.round_up(width__ * bpp, 64);

                    Console.WriteLine("Pitch 1 " + Pitch);
                    Console.WriteLine("blockHeightShift " + blockHeightShift);

                    SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8);

                    Console.WriteLine("SurfaceSize " + SurfaceSize);

                    byte[] SwizzledData = TegraX1Swizzle.swizzle(width_, height_, blkWidth, blkHeight, target, bpp, (uint)tex.TileMode, (int)Math.Max(0, tex.BlockHeightLog2 - blockHeightShift), data_);

                    mipmaps.Add(AlignedData.Concat(SwizzledData).ToArray());
                }
            }
            tex.ImageSize = SurfaceSize;

            return(mipmaps);
        }
Esempio n. 21
0
        public override void Replace(string FileName)
        {
            if (Runtime.NUTEXBSettings.IsSwizzled)
            {
                var tex = new TextureData();
                tex.Replace(FileName, MipCount, 0, Format);

                //If it's null, the operation is cancelled
                if (tex.Texture == null)
                {
                    return;
                }

                List <byte[]> data = new List <byte[]>();
                foreach (var array in tex.Texture.TextureData)
                {
                    data.Add(array[0]);
                }

                var output = CreateBuffer(data);

                Width    = tex.Texture.Width;
                Height   = tex.Texture.Height;
                MipCount = tex.Texture.MipCount;
                // ArrayCount = tex.Texture.ArrayLength;
                // Depth = tex.Texture.Depth;

                Format    = tex.Format;
                NutFormat = ConvertGenericToNutFormat(tex.Format);

                mipSizes = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length);

                ImageData = SetImageData(output);

                data.Clear();
            }
            else
            {
                GenericTextureImporterList     importer = new GenericTextureImporterList(SupportedFormats);
                GenericTextureImporterSettings settings = new GenericTextureImporterSettings();

                if (Utils.GetExtension(FileName) == ".dds" ||
                    Utils.GetExtension(FileName) == ".dds2")
                {
                    settings.LoadDDS(FileName);
                    importer.LoadSettings(new List <GenericTextureImporterSettings>()
                    {
                        settings,
                    });
                    ApplySettings(settings);
                    UpdateEditor();
                }
                else
                {
                    settings.LoadBitMap(FileName);
                    importer.LoadSettings(new List <GenericTextureImporterSettings>()
                    {
                        settings,
                    });

                    if (importer.ShowDialog() == DialogResult.OK)
                    {
                        if (settings.GenerateMipmaps && !settings.IsFinishedCompressing)
                        {
                            settings.DataBlockOutput.Clear();
                            settings.DataBlockOutput.Add(settings.GenerateMips(importer.CompressionMode, importer.MultiThreading));
                        }

                        ApplySettings(settings);
                        UpdateEditor();
                    }
                }
            }

            UpdateEditor();
        }
Esempio n. 22
0
        private List <uint[]> GenerateMipSizes(uint SurfaceCount = 1)
        {
            List <uint[]> MipMapSizes = new List <uint[]>();

            uint bpp       = GetBytesPerPixel(Format);
            uint blkWidth  = GetBlockWidth(Format);
            uint blkHeight = GetBlockHeight(Format);
            uint blkDepth  = GetBlockDepth(Format);

            uint blockHeight     = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
            uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;

            uint Pitch         = 0;
            uint DataAlignment = 512;

            int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;

            uint ArrayCount = (uint)mipSizes.Count;

            uint ArrayOffset = 0;

            for (int arrayLevel = 0; arrayLevel < ArrayCount; arrayLevel++)
            {
                uint SurfaceSize      = 0;
                int  blockHeightShift = 0;

                uint[] MipOffsets = new uint[MipCount];

                for (int mipLevel = 0; mipLevel < MipCount; mipLevel++)
                {
                    uint width  = (uint)Math.Max(1, Width >> mipLevel);
                    uint height = (uint)Math.Max(1, Height >> mipLevel);
                    uint depth  = (uint)Math.Max(1, Depth >> 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;
                    }

                    uint width__  = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth);
                    uint height__ = TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight);

                    //Calculate the mip size instead
                    byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, DataAlignment) - SurfaceSize)];
                    SurfaceSize         += (uint)AlignedData.Length;
                    MipOffsets[mipLevel] = (SurfaceSize);

                    //Get the first mip offset and current one and the total image size
                    int msize = (int)((MipOffsets[0] + ImageData.Length - MipOffsets[mipLevel]) / ArrayCount);

                    Pitch        = TegraX1Swizzle.round_up(width__ * bpp, 64);
                    SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8);
                }
                ArrayOffset += (uint)(ImageData.Length / ArrayCount);

                MipMapSizes.Add(MipOffsets);
            }

            return(MipMapSizes);
        }
 public static uint DIV_ROUND_UP(uint value1, uint value2)
 {
     return(TegraX1Swizzle.DIV_ROUND_UP(value1, value2));
 }
Esempio n. 24
0
        public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
        {
            if (!IsSwizzled)
            {
                return(ImageData);
            }

            int  target    = 1;
            uint bpp       = GetBytesPerPixel(Format);
            uint blkWidth  = GetBlockWidth(Format);
            uint blkHeight = GetBlockHeight(Format);
            uint blkDepth  = GetBlockDepth(Format);

            uint blockHeight     = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
            uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;

            uint Pitch         = 0;
            uint DataAlignment = 512;
            uint TileMode      = 0;

            int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;

            uint ArrayCount = (uint)mipSizes.Count;

            uint ArrayOffset = 0;

            for (int arrayLevel = 0; arrayLevel < ArrayCount; arrayLevel++)
            {
                uint SurfaceSize      = 0;
                int  blockHeightShift = 0;

                List <uint> MipOffsets = new List <uint>();

                for (int mipLevel = 0; mipLevel < mipSizes[arrayLevel].Length; mipLevel++)
                {
                    uint width  = (uint)Math.Max(1, Width >> mipLevel);
                    uint height = (uint)Math.Max(1, Height >> mipLevel);
                    uint depth  = (uint)Math.Max(1, Depth >> 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;
                    }


                    uint width__  = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth);
                    uint height__ = TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight);

                    //Calculate the mip size instead
                    byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, DataAlignment) - SurfaceSize)];
                    SurfaceSize += (uint)AlignedData.Length;
                    MipOffsets.Add(SurfaceSize);

                    //Get the first mip offset and current one and the total image size
                    int msize = (int)((MipOffsets[0] + ImageData.Length - MipOffsets[mipLevel]) / ArrayCount);

                    byte[] data_ = Utils.SubArray(ImageData, ArrayOffset + MipOffsets[mipLevel], (uint)msize);

                    try
                    {
                        Pitch        = TegraX1Swizzle.round_up(width__ * bpp, 64);
                        SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8);

                        Console.WriteLine($"{width} {height} {blkWidth} {blkHeight} {target} {bpp} {TileMode} {(int)Math.Max(0, BlockHeightLog2 - blockHeightShift)} {data_.Length}");
                        byte[] result = TegraX1Swizzle.deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, target, bpp, TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), data_);
                        //Create a copy and use that to remove uneeded data
                        byte[] result_ = new byte[size];
                        Array.Copy(result, 0, result_, 0, size);
                        result = null;

                        if (ArrayLevel == arrayLevel && MipLevel == mipLevel)
                        {
                            return(result_);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Windows.Forms.MessageBox.Show($"Failed to swizzle texture {Text}!");
                        Console.WriteLine(e);

                        return(new byte[0]);
                    }
                }

                ArrayOffset += (uint)(ImageData.Length / ArrayCount);
            }
            return(new byte[0]);
        }
Esempio n. 25
0
        private void Replace(object sender, EventArgs args)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = FileFilters.NUTEXB;

            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                var bntxFile = new BNTX();
                var tex      = new TextureData();
                tex.Replace(ofd.FileName, MipCount, Format);

                //If it's null, the operation is cancelled
                if (tex.Texture == null)
                {
                    return;
                }

                var surfacesNew = tex.GetSurfaces();
                var surfaces    = GetSurfaces();

                if (LimitFileSize)
                {
                    if (surfaces[0].mipmaps[0].Length != surfacesNew[0].mipmaps[0].Length)
                    {
                        throw new Exception($"Image must be the same size! {surfaces[0].mipmaps[0].Length}");
                    }

                    if (mipSizes[0].Length != surfacesNew[0].mipmaps.Count)
                    {
                        throw new Exception($"Mip map count must be the same! {mipSizes[0].Length}");
                    }

                    if (Width != tex.Texture.Width || Height != tex.Texture.Height)
                    {
                        throw new Exception("Image size must be the same!");
                    }

                    ImageData = tex.Texture.TextureData[0][0];

                    Width    = tex.Texture.Width;
                    Height   = tex.Texture.Height;
                    MipCount = tex.Texture.MipCount;
                }
                else
                {
                    ImageData = tex.Texture.TextureData[0][0];

                    Width    = tex.Texture.Width;
                    Height   = tex.Texture.Height;
                    MipCount = tex.Texture.MipCount;

                    Format    = tex.Format;
                    NutFormat = ConvertGenericToNutFormat(tex.Format);

                    mipSizes = TegraX1Swizzle.GenerateMipSizes(tex.Format, tex.Width, tex.Height, tex.Depth, tex.ArrayCount, tex.MipCount, (uint)ImageData.Length);
                }

                surfacesNew.Clear();
                surfaces.Clear();

                UpdateEditor();
            }
        }
Esempio n. 26
0
            public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
            {
                uint bpp       = GetBytesPerPixel(Format);
                uint blkWidth  = GetBlockHeight(Format);
                uint blkHeight = GetBlockWidth(Format);
                uint blkDepth  = GetBlockDepth(Format);

                uint blockHeight     = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
                uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;

                int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;

                int TileMode = 0;


                List <byte[]> mips = new List <byte[]>();

                int blockHeightShift = 0;

                for (int mipLevel = 0; mipLevel < MipOffsets.Length; mipLevel++)
                {
                    uint width  = (uint)Math.Max(1, Width >> mipLevel);
                    uint height = (uint)Math.Max(1, Height >> mipLevel);
                    uint depth  = (uint)Math.Max(1, Depth >> mipLevel);

                    //  uint size = width * height * bpp;
                    uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;

                    byte[] Output = new byte[size];

                    uint mipOffset;
                    if (mipLevel != 0)
                    {
                        mipOffset = (MipOffsets[mipLevel - 1]);
                        if (mipLevel == 1)
                        {
                            mipOffset -= (uint)size;
                        }

                        Array.Copy(ImageData, mipOffset, Output, 0, size);
                    }
                    else
                    {
                        Output = ImageData;
                    }

                    byte[] output = new byte[size];
                    Console.WriteLine(mipLevel + " " + size);

                    if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
                    {
                        blockHeightShift += 1;
                    }

                    byte[] result = TegraX1Swizzle.deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, (int)Target, bpp, (uint)TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), Output);
                    //Create a copy and use that to remove uneeded data
                    byte[] result_ = new byte[size];
                    Array.Copy(result, 0, result_, 0, size);

                    result = null;

                    if (MipLevel == mipLevel)
                    {
                        return(result_);
                    }

                    Console.WriteLine("bpp " + bpp);
                    Console.WriteLine("result_ " + size);
                    Console.WriteLine("width " + width);
                    Console.WriteLine("height " + height);

                    break;
                }
                return(new byte[0]);
            }