Optimize memory consumption of swizzled textures

This commit is contained in:
VaDiM 2024-04-02 00:30:32 +03:00
parent 4e991d85fb
commit 1fc504e587
2 changed files with 14 additions and 6 deletions

View File

@ -92,7 +92,18 @@ namespace AssetStudio
reader.GetData(buff); reader.GetData(buff);
if (switchSwizzled) if (switchSwizzled)
{ {
buff = Texture2DSwitchDeswizzler.Unswizzle(buff, GetUncroppedSize(), blockSize, gobsPerBlock); var unswizzledData = BigArrayPool<byte>.Shared.Rent(reader.Size);
try
{
Texture2DSwitchDeswizzler.Unswizzle(buff, GetUncroppedSize(), blockSize, gobsPerBlock, unswizzledData);
BigArrayPool<byte>.Shared.Return(buff, clearArray: true);
buff = unswizzledData;
}
catch (Exception e)
{
BigArrayPool<byte>.Shared.Return(unswizzledData, clearArray: true);
Logger.Error(e.Message, e);
}
} }
switch (m_TextureFormat) switch (m_TextureFormat)

View File

@ -43,10 +43,8 @@ namespace AssetStudio
return (a + b - 1) / b; return (a + b - 1) / b;
} }
internal static byte[] Unswizzle(byte[] data, Size imageSize, Size blockSize, int gobsPerBlock) internal static void Unswizzle(byte[] data, Size imageSize, Size blockSize, int gobsPerBlock, byte[] newData)
{ {
byte[] newData = new byte[data.Length];
int width = imageSize.Width; int width = imageSize.Width;
int height = imageSize.Height; int height = imageSize.Height;
@ -71,14 +69,13 @@ namespace AssetStudio
int gobDstY = (i * gobsPerBlock + k) * GOB_Y_TEXEL_COUNT + gobY; int gobDstY = (i * gobsPerBlock + k) * GOB_Y_TEXEL_COUNT + gobY;
int gobDstLinPos = gobDstY * blockCountX * TEXEL_BYTE_SIZE + gobDstX * TEXEL_BYTE_SIZE; int gobDstLinPos = gobDstY * blockCountX * TEXEL_BYTE_SIZE + gobDstX * TEXEL_BYTE_SIZE;
Array.Copy(data, srcPos, newData, gobDstLinPos, TEXEL_BYTE_SIZE); Buffer.BlockCopy(data, srcPos, newData, gobDstLinPos, TEXEL_BYTE_SIZE);
srcPos += TEXEL_BYTE_SIZE; srcPos += TEXEL_BYTE_SIZE;
} }
} }
} }
} }
return newData;
} }
//this should be the amount of pixels that can fit 16 bytes //this should be the amount of pixels that can fit 16 bytes