1
0
mirror of synced 2024-09-24 19:48:21 +02:00

Cleanup swizzle function. Remove old one

This commit is contained in:
KillzXGaming 2019-04-08 20:09:17 -04:00
parent 53edd7e12b
commit 353e8588d2
12 changed files with 14 additions and 206 deletions

Binary file not shown.

View File

@ -513,7 +513,7 @@ namespace Bfres.Structs
if (setting.DataBlockOutput != null)
{
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
var tex = ftex.FromGx2Surface(surface, setting);
ftex.UpdateTex(tex);
@ -566,7 +566,7 @@ namespace Bfres.Structs
{
FTEX ftex = new FTEX();
ftex.texture = new ResU.Texture();
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
var tex = ftex.FromGx2Surface(surface, setting);
ftex.UpdateTex(tex);
ftex.IsEdited = true;
@ -640,7 +640,7 @@ namespace Bfres.Structs
GTXImporterSettings setting = new GTXImporterSettings();
setting.LoadDDS(TextureName, data);
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
FTEX ftex = new FTEX();
ftex.texture = new ResU.Texture();
ftex.texture = ftex.FromGx2Surface(surface, setting);

View File

@ -149,7 +149,7 @@ namespace Bfres.Structs
if (setting.DataBlockOutput != null)
{
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
var tex = FromGx2Surface(surface, setting);
UpdateTex(tex);
@ -210,7 +210,7 @@ namespace Bfres.Structs
{
if (setting.DataBlockOutput != null)
{
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
var tex = FromGx2Surface(surface, setting);
UpdateTex(tex);
texture.Name = Text;
@ -233,7 +233,7 @@ namespace Bfres.Structs
if (setting.DataBlockOutput != null)
{
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
var tex = FromGx2Surface(surface, setting);
UpdateTex(tex);
texture.Name = Text;

View File

@ -526,7 +526,7 @@ namespace FirstPlugin
{
if (setting.DataBlockOutput != null)
{
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
var newData = Utils.CombineByteArray(surface.data, surface.mipData);
}
}
@ -544,7 +544,7 @@ namespace FirstPlugin
if (setting.DataBlockOutput != null)
{
var surface = setting.CreateGx2Texture(setting.DataBlockOutput[0]);
var surface = GTXSwizzle.CreateGx2Texture(setting.DataBlockOutput[0], setting);
var newData = Utils.CombineByteArray(surface.data, surface.mipData);
if (newData.Length != data.Length)
throw new Exception($"Image is not the correct size {newData.Length}. Should be {data.Length}");

View File

@ -10,8 +10,11 @@ namespace FirstPlugin
{
public class GTXSwizzle
{
public static GX2.GX2Surface CreateGx2Texture(byte[] imageData, GTXImporterSettings setting, uint tileMode, uint AAMode)
public static GX2.GX2Surface CreateGx2Texture(byte[] imageData, GTXImporterSettings setting)
{
uint tileMode = setting.tileMode;
uint aaMode = (uint)setting.AAMode;
var Format = (GX2.GX2SurfaceFormat)setting.Format;
Console.WriteLine("Format " + Format + " " + setting.TexName);
@ -46,7 +49,7 @@ namespace FirstPlugin
}
if (tileMode == 0)
tileMode = GX2.getDefaultGX2TileMode(1, setting.TexWidth, setting.TexHeight, 1,(uint)setting.Format, 0, 1);
tileMode = GX2.getDefaultGX2TileMode((uint)setting.SurfaceDim, setting.TexWidth, setting.TexHeight, 1,(uint)setting.Format, 0, 1);
int tiling1dLevel = 0;
bool tiling1dLevelSet = false;
@ -126,7 +129,7 @@ namespace FirstPlugin
surf.bpp = bpp;
surf.format = (uint)setting.Format;
surf.numMips = setting.MipCount;
surf.aa = AAMode;
surf.aa = aaMode;
surf.mipOffset = mipOffsets.ToArray();
surf.numMips = (uint)Swizzled.Count;
surf.alignment = alignment;

View File

@ -159,200 +159,5 @@ namespace FirstPlugin
FTEX.ConvertFromGx2Format((GX2SurfaceFormat)Format), alphaRef));
}
}
public GX2.GX2Surface CreateGx2Texture(byte[] imageData)
{
Console.WriteLine("Format " + Format);
var surfOut = GX2.getSurfaceInfo(Format, TexWidth, 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 = GX2.surfaceGetBitsPerPixel((uint)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 | swizzle << 8;
break;
}
uint blkWidth, blkHeight;
if (GX2.IsFormatBCN(Format))
{
blkWidth = 4;
blkHeight = 4;
}
else
{
blkWidth = 1;
blkHeight = 1;
}
if (MipCount <= 0)
MipCount = 1;
List<uint> mipOffsets = new List<uint>();
List<byte[]> Swizzled = new List<byte[]>();
for (int mipLevel = 0; mipLevel < MipCount; mipLevel++)
{
var result = TextureHelper.GetCurrentMipSize(TexWidth, 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 " + TexWidth);
Console.WriteLine("TexHeight " + 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, TexWidth >> mipLevel);
uint height_ = Math.Max(1, TexHeight >> mipLevel);
if (mipLevel != 0)
{
surfOut = GX2.getSurfaceInfo(Format, TexWidth, 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 = GX2.swizzle(width_, height_, surfOut.depth, surfOut.height, (uint)Format, surfOut.tileMode, s,
surfOut.pitch, surfOut.bpp, data_, 1);
Swizzled.Add(dataAlignBytes.Concat(SwizzledData).ToArray());
}
GX2.GX2Surface surf = new GX2.GX2Surface();
surf.depth = Depth;
surf.width = TexWidth;
surf.height = TexHeight;
surf.depth = 1;
surf.use = 1;
surf.dim = (uint)SurfaceDim;
surf.tileMode = tileMode;
surf.swizzle = s;
surf.resourceFlags = 0;
surf.pitch = pitch;
surf.bpp = bpp;
surf.format = (uint)Format;
surf.numMips = MipCount;
surf.aa = (uint)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 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("");
Console.WriteLine(" GX2 Component Selector:");
Console.WriteLine(" Red Channel: " + compSel[0]);
Console.WriteLine(" Green Channel: " + compSel[1]);
Console.WriteLine(" Blue Channel: " + compSel[2]);
Console.WriteLine(" Alpha Channel: " + compSel[3]);
Console.WriteLine("");
Console.WriteLine(" bits per pixel = " + (surf.bpp << 3));
Console.WriteLine(" bytes per pixel = " + surf.bpp);
Console.WriteLine(" realSize = " + imageData.Length);
return surf;
}
private static Tuple<uint, uint> GetCurrentMipSize(uint width, uint height, uint bpp, int CurLevel, bool IsCompressed)
{
uint offset = 0;
uint size = 0;
for (int mipLevel = 0; mipLevel < CurLevel; mipLevel++)
{
int level = mipLevel + 1;
if (IsCompressed)
offset += ((Math.Max(1, width >> level) + 3) >> 2) * ((Math.Max(1, height >> level) + 3) >> 2) * bpp;
else
offset += Math.Max(1, width >> level) * Math.Max(1, height >> level) * bpp;
}
if (IsCompressed)
size = ((Math.Max(1, width >> CurLevel) + 3) >> 2) * ((Math.Max(1, height >> CurLevel) + 3) >> 2) * bpp;
else
size = Math.Max(1, width >> CurLevel) * Math.Max(1, height >> CurLevel) * bpp;
return Tuple.Create(offset, size);
}
private uint getAlignBlockSize(uint dataOffset, uint alignment)
{
uint alignSize = RoundUp(dataOffset, alignment) - dataOffset - 32;
uint z = 1;
while (alignSize <= 0)
{
alignSize = RoundUp(dataOffset + (alignment * z), alignment) - dataOffset - 32;
z += 1;
}
return alignSize;
}
private int RoundUp(int X, int Y)
{
return ((X - 1) | (Y - 1)) + 1;
}
private uint RoundUp(uint X, uint Y)
{
return ((X - 1) | (Y - 1)) + 1;
}
}
}