Ejemplo n.º 1
0
        public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
        {
            uint bpp = GetBytesPerPixel(Format);

            GTX.GX2Surface surf = new GTX.GX2Surface();
            surf.bpp       = bpp;
            surf.height    = image.Height;
            surf.width     = image.Width;
            surf.aa        = (uint)GTX.GX2AAMode.GX2_AA_MODE_1X;
            surf.alignment = image.Alignment;
            surf.depth     = 1;
            surf.dim       = (uint)GTX.GX2SurfaceDimension.DIM_2D;
            surf.format    = (uint)FTEX.ConvertToGx2Format(Format);
            surf.use       = (uint)GTX.GX2SurfaceUse.USE_COLOR_BUFFER;
            surf.pitch     = 0;
            surf.data      = ImageData;
            surf.numMips   = 1;
            surf.mipOffset = new uint[0];
            surf.mipData   = ImageData;
            surf.tileMode  = (uint)GTX.GX2TileMode.MODE_2D_TILED_THIN1;
            surf.swizzle   = image.swizzle;
            surf.numArray  = 1;

            var surfaces = GTX.Decode(surf);

            return(surfaces[ArrayLevel][MipLevel]);
        }
Ejemplo n.º 2
0
        public void Read(Texture tex)
        {
            ImageKey         = "Texture";
            SelectedImageKey = "Texture";
            Text             = tex.Name;

            ChannelRed   = tex.CompSelR;
            ChannelGreen = tex.CompSelG;
            ChannelBlue  = tex.CompSelB;
            ChannelAlpha = tex.CompSelA;

            renderedTex.width  = (int)tex.Width;
            renderedTex.height = (int)tex.Height;
            format             = (int)tex.Format;
            int  swizzle = (int)tex.Swizzle;
            int  pitch   = (int)tex.Pitch;
            uint bpp     = GTX.surfaceGetBitsPerPixel((uint)format) >> 3;

            GTX.GX2Surface surf = new GTX.GX2Surface();
            surf.bpp = bpp;

            for (int surfaceLevel = 0; surfaceLevel < tex.ArrayLength; surfaceLevel++)
            {
            }
            GTX.Decode(surf, tex.MipData);
        }
Ejemplo n.º 3
0
        public void LoadDDS(string FileName, byte[] FileData = null)
        {
            TexName = Path.GetFileNameWithoutExtension(FileName);

            DDS dds = new DDS();

            if (FileData != null)
            {
                dds.Load(new FileReader(new MemoryStream(FileData)));
            }
            else
            {
                dds.Load(new FileReader(FileName));
            }
            MipCount    = dds.header.mipmapCount;
            TexWidth    = dds.header.width;
            TexHeight   = dds.header.height;
            arrayLength = 1;
            if (dds.header.caps2 == (uint)DDS.DDSCAPS2.CUBEMAP_ALLFACES)
            {
                arrayLength = 6;
            }
            DataBlockOutput.Add(dds.bdata);

            Format = LoadDDSFormat(dds.header.ddspf.fourCC, dds, IsSRGB);

            GTX.GX2Surface tex = CreateGx2Texture(DataBlockOutput[0]);
        }
Ejemplo n.º 4
0
        //We reuse GX2 data as it's the same thing
        public Texture FromGx2Surface(GTX.GX2Surface surf, TextureImporterSettings settings)
        {
            Texture tex = new Texture();

            tex.Name        = settings.TexName;
            tex.AAMode      = (GX2AAMode)surf.aa;
            tex.Alignment   = (uint)surf.alignment;
            tex.ArrayLength = 1;
            tex.Data        = surf.data;
            tex.MipData     = surf.mipData;
            tex.Format      = (GX2SurfaceFormat)surf.format;
            tex.Dim         = (GX2SurfaceDim)surf.dim;
            tex.Use         = (GX2SurfaceUse)surf.use;
            tex.TileMode    = (GX2TileMode)surf.tileMode;
            tex.Swizzle     = surf.swizzle;
            tex.Pitch       = surf.pitch;
            tex.Depth       = surf.depth;
            tex.MipCount    = surf.numMips;

            tex.MipOffsets = new uint[13];
            for (int i = 0; i < 13; i++)
            {
                if (i < surf.mipOffset.Length)
                {
                    tex.MipOffsets[i] = surf.mipOffset[i];
                }
            }
            tex.Height      = surf.height;
            tex.Width       = surf.width;
            tex.Regs        = new uint[5];
            tex.ArrayLength = 1;
            var channels = SetChannelsByFormat((GX2SurfaceFormat)surf.format);

            tex.CompSelR = channels[0];
            tex.CompSelG = channels[1];
            tex.CompSelB = channels[2];
            tex.CompSelA = channels[3];
            tex.UserData = new ResDict <UserData>();
            return(tex);
        }
Ejemplo n.º 5
0
        public static GTX.GX2Surface CreateGx2Texture(byte[] imageData, GTXImporterSettings setting, uint tileMode, uint AAMode)
        {
            var Format = (GTX.GX2SurfaceFormat)setting.Format;

            Console.WriteLine("Format " + Format + " " + setting.TexName);

            var  surfOut   = GTX.getSurfaceInfo(Format, setting.TexWidth, setting.TexHeight, 1, 1, tileMode, 0, 0);
            uint imageSize = (uint)surfOut.surfSize;
            uint alignment = surfOut.baseAlign;
            uint pitch     = surfOut.pitch;
            uint mipSize   = 0;
            uint dataSize  = (uint)imageData.Length;
            uint bpp       = GTX.surfaceGetBitsPerPixel((uint)setting.Format) >> 3;

            if (dataSize <= 0)
            {
                throw new Exception($"Image is empty!!");
            }

            if (surfOut.depth != 1)
            {
                throw new Exception($"Unsupported Depth {surfOut.depth}!");
            }

            uint s = 0;

            switch (tileMode)
            {
            case 1:
            case 2:
            case 3:
            case 16:
                s = 0;
                break;

            default:
                s = 0xd0000 | setting.swizzle << 8;
                break;
            }
            uint blkWidth, blkHeight;

            if (GTX.IsFormatBCN(Format))
            {
                blkWidth  = 4;
                blkHeight = 4;
            }
            else
            {
                blkWidth  = 1;
                blkHeight = 1;
            }
            List <uint>   mipOffsets = new List <uint>();
            List <byte[]> Swizzled   = new List <byte[]>();

            for (int mipLevel = 0; mipLevel < setting.MipCount; mipLevel++)
            {
                var result = TextureHelper.GetCurrentMipSize(setting.TexWidth, setting.TexHeight, blkWidth, blkHeight, bpp, mipLevel);

                uint offset = result.Item1;
                uint size   = result.Item2;

                Console.WriteLine("Swizzle Size " + size);
                Console.WriteLine("Swizzle offset " + offset);
                Console.WriteLine("bpp " + bpp);
                Console.WriteLine("TexWidth " + setting.TexWidth);
                Console.WriteLine("TexHeight " + setting.TexHeight);
                Console.WriteLine("blkWidth " + blkWidth);
                Console.WriteLine("blkHeight " + blkHeight);
                Console.WriteLine("mipLevel " + mipLevel);

                byte[] data_ = new byte[size];
                Array.Copy(imageData, offset, data_, 0, size);

                uint width_  = Math.Max(1, setting.TexWidth >> mipLevel);
                uint height_ = Math.Max(1, setting.TexHeight >> mipLevel);

                if (mipLevel != 0)
                {
                    surfOut = GTX.getSurfaceInfo(Format, setting.TexWidth, setting.TexHeight, 1, 1, tileMode, 0, mipLevel);

                    if (mipLevel == 1)
                    {
                        mipOffsets.Add(imageSize);
                    }
                    else
                    {
                        mipOffsets.Add(mipSize);
                    }
                }

                data_ = Utils.CombineByteArray(data_, new byte[surfOut.surfSize - size]);
                byte[] dataAlignBytes = new byte[RoundUp(mipSize, surfOut.baseAlign) - mipSize];

                if (mipLevel != 0)
                {
                    mipSize += (uint)(surfOut.surfSize + dataAlignBytes.Length);
                }

                byte[] SwizzledData = GTX.swizzle(width_, height_, surfOut.height, (uint)Format, surfOut.tileMode, s,
                                                  surfOut.pitch, surfOut.bpp, data_);

                Swizzled.Add(dataAlignBytes.Concat(SwizzledData).ToArray());
            }

            GTX.GX2Surface surf = new GTX.GX2Surface();
            surf.depth         = setting.Depth;
            surf.width         = setting.TexWidth;
            surf.height        = setting.TexHeight;
            surf.depth         = 1;
            surf.use           = 1;
            surf.dim           = (uint)setting.SurfaceDim;
            surf.tileMode      = tileMode;
            surf.swizzle       = s;
            surf.resourceFlags = 0;
            surf.pitch         = pitch;
            surf.bpp           = bpp;
            surf.format        = (uint)setting.Format;
            surf.numMips       = setting.MipCount;
            surf.aa            = AAMode;
            surf.mipOffset     = mipOffsets.ToArray();
            surf.numMips       = (uint)Swizzled.Count;
            surf.alignment     = alignment;
            surf.mipSize       = mipSize;
            surf.imageSize     = imageSize;
            surf.data          = Swizzled[0];

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

            for (int mipLevel = 1; mipLevel < Swizzled.Count; mipLevel++)
            {
                mips.Add(Swizzled[mipLevel]);
                Console.WriteLine(Swizzled[mipLevel].Length);
            }
            surf.mipData = Utils.CombineByteArray(mips.ToArray());
            mips.Clear();


            Console.WriteLine("");
            Console.WriteLine("// ----- GX2Surface Swizzled Info ----- ");
            Console.WriteLine("  dim             = 1");
            Console.WriteLine("  width           = " + surf.width);
            Console.WriteLine("  height          = " + surf.height);
            Console.WriteLine("  depth           = 1");
            Console.WriteLine("  numMips         = " + surf.numMips);
            Console.WriteLine("  format          = " + surf.format);
            Console.WriteLine("  aa              = 0");
            Console.WriteLine("  use             = 1");
            Console.WriteLine("  imageSize       = " + surf.imageSize);
            Console.WriteLine("  mipSize         = " + surf.mipSize);
            Console.WriteLine("  tileMode        = " + surf.tileMode);
            Console.WriteLine("  swizzle         = " + surf.swizzle);
            Console.WriteLine("  alignment       = " + surf.alignment);
            Console.WriteLine("  pitch           = " + surf.pitch);
            Console.WriteLine("  data            = " + surf.data.Length);
            Console.WriteLine("  mipData         = " + surf.mipData.Length);
            Console.WriteLine("");
            Console.WriteLine("  GX2 Component Selector:");
            Console.WriteLine("");
            Console.WriteLine("  bits per pixel  = " + (surf.bpp << 3));
            Console.WriteLine("  bytes per pixel = " + surf.bpp);
            Console.WriteLine("  realSize        = " + imageData.Length);

            return(surf);
        }
Ejemplo n.º 6
0
        public void Read(Texture tex)
        {
            ImageKey         = "Texture";
            SelectedImageKey = "Texture";
            Text             = tex.Name;

            texture = tex;

            Width  = tex.Width;
            Height = tex.Height;
            Format = ConvertFormat(tex.Format);
            format = (int)tex.Format;
            int  swizzle = (int)tex.Swizzle;
            int  pitch   = (int)tex.Pitch;
            uint bpp     = GTX.surfaceGetBitsPerPixel((uint)format) >> 3;

            GTX.GX2Surface surf = new GTX.GX2Surface();
            surf.bpp       = bpp;
            surf.height    = tex.Height;
            surf.width     = tex.Width;
            surf.aa        = (uint)tex.AAMode;
            surf.alignment = tex.Alignment;
            surf.depth     = tex.Depth;
            surf.dim       = (uint)tex.Dim;
            surf.format    = (uint)tex.Format;
            surf.use       = (uint)tex.Use;
            surf.pitch     = tex.Pitch;
            surf.data      = tex.Data;
            surf.numMips   = tex.MipCount;
            surf.mipOffset = tex.MipOffsets;
            surf.mipData   = tex.MipData;
            surf.tileMode  = (uint)tex.TileMode;
            surf.swizzle   = tex.Swizzle;

            //Determine tex2 botw files to get mip maps
            string Tex1 = GetFilePath();

            if (Tex1.Contains(".Tex1"))
            {
                string Tex2 = Tex1.Replace(".Tex1", ".Tex2");
                Console.WriteLine(Tex2);

                if (System.IO.File.Exists(Tex2))
                {
                    ResFile resFile2 = new ResFile(new System.IO.MemoryStream(
                                                       EveryFileExplorer.YAZ0.Decompress(Tex2)));

                    if (resFile2.Textures.ContainsKey(tex.Name))
                    {
                        surf.mipData   = resFile2.Textures[tex.Name].MipData;
                        surf.mipOffset = resFile2.Textures[tex.Name].MipOffsets;
                    }
                }
            }


            if (surf.mipData == null)
            {
                surf.numMips = 1;
            }

            List <byte[]> mips = GTX.Decode(surf);

            Surfaces.Add(new Surface()
            {
                mipmaps = mips
            });

            RenderableTex.LoadOpenGLTexture(this);
        }