Skip to content

Commit

Permalink
Add support for "switch swizzle"
Browse files Browse the repository at this point in the history
  • Loading branch information
Xzonn committed Mar 23, 2024
1 parent 980717c commit 863ed0c
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
@@ -0,0 +1,5 @@
[*.{cs,vb}]
end_of_line = lf
insert_final_newline = true
tab_width = 4
indent_size = 4
3 changes: 2 additions & 1 deletion AssetStudio/Classes/Texture2D.cs
Expand Up @@ -62,6 +62,7 @@ public sealed class Texture2D : Texture
public GLTextureSettings m_TextureSettings;
public ResourceReader image_data;
public StreamingInfo m_StreamData;
public byte[] m_PlatformBlob;

public Texture2D(ObjectReader reader) : base(reader)
{
Expand Down Expand Up @@ -122,7 +123,7 @@ public Texture2D(ObjectReader reader) : base(reader)
}
if (version[0] > 2020 || (version[0] == 2020 && version[1] >= 2)) //2020.2 and up
{
var m_PlatformBlob = reader.ReadUInt8Array();
m_PlatformBlob = reader.ReadUInt8Array();
reader.AlignStream();
}
var image_data_size = reader.ReadInt32();
Expand Down
8 changes: 4 additions & 4 deletions AssetStudioUtility/Texture2DConverter.cs
Expand Up @@ -14,12 +14,12 @@ public class Texture2DConverter
private BuildTarget platform;
private int outPutSize;

public Texture2DConverter(Texture2D m_Texture2D)
public Texture2DConverter(Texture2D m_Texture2D, int width, int height, TextureFormat format)
{
reader = m_Texture2D.image_data;
m_Width = m_Texture2D.m_Width;
m_Height = m_Texture2D.m_Height;
m_TextureFormat = m_Texture2D.m_TextureFormat;
m_Width = width;
m_Height = height;
m_TextureFormat = format;
version = m_Texture2D.version;
platform = m_Texture2D.platform;
outPutSize = m_Width * m_Height * 4;
Expand Down
76 changes: 67 additions & 9 deletions AssetStudioUtility/Texture2DExtensions.cs
@@ -1,4 +1,30 @@
using SixLabors.ImageSharp;
/*
Reference: https://github.com/nesrak1/UABEA/blob/239a112cc31cb46f9da235471a7b4d74894640fb/TexturePlugin/Texture2DSwitchDeswizzler.cs
MIT License
Copyright (c) 2021 nesrak1
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.IO;
Expand All @@ -9,20 +35,52 @@ public static class Texture2DExtensions
{
public static Image<Bgra32> ConvertToImage(this Texture2D m_Texture2D, bool flip)
{
var converter = new Texture2DConverter(m_Texture2D);
var buff = BigArrayPool<byte>.Shared.Rent(m_Texture2D.m_Width * m_Texture2D.m_Height * 4);
int width = m_Texture2D.m_Width;
int height = m_Texture2D.m_Height;
TextureFormat format = m_Texture2D.m_TextureFormat;
byte[] platformBlob = m_Texture2D.m_PlatformBlob;

int originalWidth = width;
int originalHeight = height;
bool isSwitch = m_Texture2D.platform == BuildTarget.Switch && platformBlob != null && platformBlob.Length > 0;
Size blockSize = new Size(1, 1), newSize = new Size(width, height);
int gobsPerBlock = 0;

if (isSwitch)
{
gobsPerBlock = Texture2DSwitchDeswizzler.GetSwitchGobsPerBlock(platformBlob);
blockSize = Texture2DSwitchDeswizzler.TextureFormatToBlockSize(format);
newSize = Texture2DSwitchDeswizzler.GetPaddedTextureSize(width, height, blockSize.Width, blockSize.Height, gobsPerBlock);
width = newSize.Width;
height = newSize.Height;
}

var converter = new Texture2DConverter(m_Texture2D, width, height, format);
var buff = BigArrayPool<byte>.Shared.Rent(width * height * 4);
try
{
if (converter.DecodeTexture2D(buff))
if (!converter.DecodeTexture2D(buff))
{
return null;
}

var image = Image.LoadPixelData<Bgra32>(buff, width, height);

if (isSwitch)
{
var image = Image.LoadPixelData<Bgra32>(buff, m_Texture2D.m_Width, m_Texture2D.m_Height);
if (flip)
image = Texture2DSwitchDeswizzler.SwitchUnswizzle(image, blockSize, gobsPerBlock);
if (originalWidth != width || originalHeight != height)
{
image.Mutate(x => x.Flip(FlipMode.Vertical));
image.Mutate(x => x.Crop(originalWidth, originalHeight));
}
return image;
}
return null;

if (flip)
{
image.Mutate(x => x.Flip(FlipMode.Vertical));
}

return image;
}
finally
{
Expand Down
216 changes: 216 additions & 0 deletions AssetStudioUtility/Texture2DSwitchDeswizzler.cs
@@ -0,0 +1,216 @@
/*
Reference: https://github.com/nesrak1/UABEA/blob/239a112cc31cb46f9da235471a7b4d74894640fb/TexturePlugin/Texture2DSwitchDeswizzler.cs
MIT License
Copyright (c) 2021 nesrak1
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System;

namespace AssetStudio
{
public class Texture2DSwitchDeswizzler
{
// referring to block here as a compressed texture block, not a gob one
const int GOB_X_BLOCK_COUNT = 4;
const int GOB_Y_BLOCK_COUNT = 8;
const int BLOCKS_IN_GOB = GOB_X_BLOCK_COUNT * GOB_Y_BLOCK_COUNT;

/*
sector:
A
B
gob (made of sectors):
ABIJ
CDKL
EFMN
GHOP
gob blocks (example with height 2):
ACEGIK... from left to right of image
BDFHJL...
--------- start new row of blocks
MOQSUW...
NPRTVX...
*/

private static void CopyBlock(Image<Bgra32> srcImage, Image<Bgra32> dstImage, int sbx, int sby, int dbx, int dby, int blockSizeW, int blockSizeH)
{
for (int i = 0; i < blockSizeW; i++)
{
for (int j = 0; j < blockSizeH; j++)
{
dstImage[dbx * blockSizeW + i, dby * blockSizeH + j] = srcImage[sbx * blockSizeW + i, sby * blockSizeH + j];
}
}
}

private static int CeilDivide(int a, int b)
{
return (a + b - 1) / b;
}

internal static Image<Bgra32> SwitchUnswizzle(Image<Bgra32> srcImage, Size blockSize, int gobsPerBlock)
{
Image<Bgra32> dstImage = new Image<Bgra32>(srcImage.Width, srcImage.Height);

int width = srcImage.Width;
int height = srcImage.Height;

int blockCountX = CeilDivide(width, blockSize.Width);
int blockCountY = CeilDivide(height, blockSize.Height);

int gobCountX = blockCountX / GOB_X_BLOCK_COUNT;
int gobCountY = blockCountY / GOB_Y_BLOCK_COUNT;

int srcX = 0;
int srcY = 0;
for (int i = 0; i < gobCountY / gobsPerBlock; i++)
{
for (int j = 0; j < gobCountX; j++)
{
for (int k = 0; k < gobsPerBlock; k++)
{
for (int l = 0; l < BLOCKS_IN_GOB; l++)
{
// todo: use table for speedy boi
int gobX = ((l >> 3) & 0b10) | ((l >> 1) & 0b1);
int gobY = ((l >> 1) & 0b110) | (l & 0b1);
int gobDstX = j * GOB_X_BLOCK_COUNT + gobX;
int gobDstY = (i * gobsPerBlock + k) * GOB_Y_BLOCK_COUNT + gobY;
CopyBlock(srcImage, dstImage, srcX, srcY, gobDstX, gobDstY, blockSize.Width, blockSize.Height);

srcX++;
if (srcX >= blockCountX)
{
srcX = 0;
srcY++;
}
}
}
}
}

return dstImage;
}

internal static Image<Bgra32> SwitchSwizzle(Image<Bgra32> srcImage, Size blockSize, int gobsPerBlock)
{
Image<Bgra32> dstImage = new Image<Bgra32>(srcImage.Width, srcImage.Height);

int width = srcImage.Width;
int height = srcImage.Height;

int blockCountX = CeilDivide(width, blockSize.Width);
int blockCountY = CeilDivide(height, blockSize.Height);

int gobCountX = blockCountX / GOB_X_BLOCK_COUNT;
int gobCountY = blockCountY / GOB_Y_BLOCK_COUNT;

int dstX = 0;
int dstY = 0;
for (int i = 0; i < gobCountY / gobsPerBlock; i++)
{
for (int j = 0; j < gobCountX; j++)
{
for (int k = 0; k < gobsPerBlock; k++)
{
for (int l = 0; l < BLOCKS_IN_GOB; l++)
{
// todo: use table for speedy boi
int gobX = ((l >> 3) & 0b10) | ((l >> 1) & 0b1);
int gobY = ((l >> 1) & 0b110) | (l & 0b1);
int gobSrcX = j * GOB_X_BLOCK_COUNT + gobX;
int gobSrcY = (i * gobsPerBlock + k) * GOB_Y_BLOCK_COUNT + gobY;
CopyBlock(srcImage, dstImage, gobSrcX, gobSrcY, dstX, dstY, blockSize.Width, blockSize.Height);

dstX++;
if (dstX >= blockCountX)
{
dstX = 0;
dstY++;
}
}
}
}
}

return dstImage;
}

// this should be the amount of pixels that can fit 16 bytes
internal static Size TextureFormatToBlockSize(TextureFormat m_TextureFormat)
{
switch (m_TextureFormat)
{
case TextureFormat.Alpha8: return new Size(16, 1); // 1 byte per pixel
case TextureFormat.ARGB4444: return new Size(8, 1); // 2 bytes per pixel
case TextureFormat.RGBA32: return new Size(4, 1); // 4 bytes per pixel
case TextureFormat.ARGB32: return new Size(4, 1); // 4 bytes per pixel
case TextureFormat.ARGBFloat: return new Size(1, 1); // 16 bytes per pixel (?)
case TextureFormat.RGB565: return new Size(8, 1); // 2 bytes per pixel
case TextureFormat.R16: return new Size(8, 1); // 2 bytes per pixel
case TextureFormat.DXT1: return new Size(8, 4); // 8 bytes per 4x4=16 pixels
case TextureFormat.DXT5: return new Size(4, 4); // 16 bytes per 4x4=16 pixels
case TextureFormat.RGBA4444: return new Size(8, 1); // 2 bytes per pixel
case TextureFormat.BGRA32: return new Size(4, 1); // 4 bytes per pixel
case TextureFormat.BC6H: return new Size(4, 4); // 16 bytes per 4x4=16 pixels
case TextureFormat.BC7: return new Size(4, 4); // 16 bytes per 4x4=16 pixels
case TextureFormat.BC4: return new Size(8, 4); // 8 bytes per 4x4=16 pixels
case TextureFormat.BC5: return new Size(4, 4); // 16 bytes per 4x4=16 pixels
case TextureFormat.ASTC_RGB_4x4: return new Size(4, 4); // 16 bytes per 4x4=16 pixels
case TextureFormat.ASTC_RGB_5x5: return new Size(5, 5); // 16 bytes per 5x5=25 pixels
case TextureFormat.ASTC_RGB_6x6: return new Size(6, 6); // 16 bytes per 6x6=36 pixels
case TextureFormat.ASTC_RGB_8x8: return new Size(8, 8); // 16 bytes per 8x8=64 pixels
case TextureFormat.ASTC_RGB_10x10: return new Size(10, 10); // 16 bytes per 10x10=100 pixels
case TextureFormat.ASTC_RGB_12x12: return new Size(12, 12); // 16 bytes per 12x12=144 pixels
case TextureFormat.ASTC_RGBA_4x4: return new Size(4, 4); // 16 bytes per 4x4=16 pixels
case TextureFormat.ASTC_RGBA_5x5: return new Size(5, 5); // 16 bytes per 5x5=25 pixels
case TextureFormat.ASTC_RGBA_6x6: return new Size(6, 6); // 16 bytes per 6x6=36 pixels
case TextureFormat.ASTC_RGBA_8x8: return new Size(8, 8); // 16 bytes per 8x8=64 pixels
case TextureFormat.ASTC_RGBA_10x10: return new Size(10, 10); // 16 bytes per 10x10=100 pixels
case TextureFormat.ASTC_RGBA_12x12: return new Size(12, 12); // 16 bytes per 12x12=144 pixels
case TextureFormat.RG16: return new Size(8, 1); // 2 bytes per pixel
case TextureFormat.R8: return new Size(16, 1); // 1 byte per pixel
default: throw new NotImplementedException();
};
}

internal static Size GetPaddedTextureSize(int width, int height, int blockWidth, int blockHeight, int gobsPerBlock)
{
width = CeilDivide(width, blockWidth * GOB_X_BLOCK_COUNT) * blockWidth * GOB_X_BLOCK_COUNT;
height = CeilDivide(height, blockHeight * GOB_Y_BLOCK_COUNT * gobsPerBlock) * blockHeight * GOB_Y_BLOCK_COUNT * gobsPerBlock;
return new Size(width, height);
}

internal static int GetSwitchGobsPerBlock(byte[] platformBlob)
{
// apparently there is another value to worry about, but seeing as it's
// always 0 and I have nothing else to test against, this will probably
// work fine for now
return 1 << BitConverter.ToInt32(platformBlob, 8);
}
}
}

0 comments on commit 863ed0c

Please sign in to comment.