Skip to content

Commit

Permalink
Fold DXT10 header writing into DDSHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
mspielberg committed Mar 5, 2024
1 parent 2836250 commit f08c728
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions SkinManagerMod/TextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,13 @@ private static int Mipmap0SizeInBytes(int width, int height, TextureFormat textu
return blockWidth * blockHeight * bytesPerBlock;
}

private const int DDS_HEADER_SIZE = 128;
private const int DDS_HEADER_DXT10_SIZE = 20;
private static byte[] DDSHeader(int width, int height, TextureFormat textureFormat, int numMipmaps)
{
var header = new byte[128];
var needsDXGIHeader = textureFormat != TextureFormat.DXT1 && textureFormat != TextureFormat.DXT5;
var headerSize = needsDXGIHeader ? DDS_HEADER_SIZE + DDS_HEADER_DXT10_SIZE : DDS_HEADER_SIZE;
var header = new byte[headerSize];
using (var stream = new MemoryStream(header))
{
stream.Write(Encoding.ASCII.GetBytes("DDS "), 0, 4);
Expand All @@ -172,6 +176,9 @@ private static byte[] DDSHeader(int width, int height, TextureFormat textureForm
stream.Write(pixelFormat, 0, pixelFormat.Length);
// dwCaps = COMPLEX | MIPMAP | TEXTURE
stream.Write(BitConverter.GetBytes(0x401008), 0, 4);

if (needsDXGIHeader)
stream.Write(DDSHeaderDXT10(textureFormat), 0, DDS_HEADER_DXT10_SIZE);
}
return header;
}
Expand Down Expand Up @@ -214,7 +221,7 @@ private static int DXGIFormat(TextureFormat textureFormat)

private static byte[] DDSHeaderDXT10(TextureFormat textureFormat)
{
var headerDXT10 = new byte[20];
var headerDXT10 = new byte[DDS_HEADER_DXT10_SIZE];
using (var stream = new MemoryStream(headerDXT10))
{
stream.Write(BitConverter.GetBytes(DXGIFormat(textureFormat)), 0, 4); // dxgiFormat
Expand All @@ -234,12 +241,7 @@ public static void WriteDDSGz(FileInfo fileInfo, Texture2D texture)
{
var header = DDSHeader(texture.width, texture.height, texture.format, texture.mipmapCount);
outfile.Write(header, 0, header.Length);
if (texture.format != TextureFormat.DXT1 && texture.format != TextureFormat.DXT5)
{
// compressed formats other than DXT1-5 require DX10
var headerDXT10 = DDSHeaderDXT10(texture.format);
outfile.Write(headerDXT10, 0, headerDXT10.Length);
}

var data = texture.GetRawTextureData<byte>().ToArray();
outfile.Write(data, 0, data.Length);
}
Expand All @@ -248,7 +250,7 @@ public static void WriteDDSGz(FileInfo fileInfo, Texture2D texture)
private static Texture2D ReadDDSHeader(Stream infile, bool linear)
{
var buf = new byte[4096];
var bytesRead = infile.Read(buf, 0, 128);
var bytesRead = infile.Read(buf, 0, DDS_HEADER_SIZE);
if (bytesRead != 128 || Encoding.ASCII.GetString(buf, 0, 4) != "DDS ")
throw new DDSReadException("File is not a DDS file");

Expand All @@ -270,8 +272,8 @@ private static Texture2D ReadDDSHeader(Stream infile, bool linear)
break;
case "DX10":
// read DDS_HEADER_DXT10 header extension
bytesRead = infile.Read(buf, 0, 20);
if (bytesRead != 20)
bytesRead = infile.Read(buf, 0, DDS_HEADER_DXT10_SIZE);
if (bytesRead != DDS_HEADER_DXT10_SIZE)
throw new DDSReadException("Could not read DXT10 header from DDS file");
int dxgiFormat = BitConverter.ToInt32(buf, 0);
switch (dxgiFormat)
Expand Down

0 comments on commit f08c728

Please sign in to comment.