Skip to content

Instantly share code, notes, and snippets.

@HeicoDev
Last active July 18, 2020 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HeicoDev/8eb8428543278d68dfa5e3711b373ee7 to your computer and use it in GitHub Desktop.
Save HeicoDev/8eb8428543278d68dfa5e3711b373ee7 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
/* C# code snippet to calculate the mipmap sizes for the BFBC2 textures, by Heico */
/* In this case we use a DXT1 texture with a resolution of 512 * 512 and 9 mipmaps */
/* For DXT2-5 textures the code must be slightly edited, see Microsoft Docs below */
//Read the integers below from file
int mipmapCount = 9;
int width = 512;
int height = 512;
//Create array to store all mipmap sizes
int[] mipmaps = new int[mipmapCount];
//Calculate the size of the first mipmap with this formula. Source can be found below.
int mipmapSize = Math.Max(1, ((width + 3) / 4)) * Math.Max(1, ((height + 3) / 4)) * 8;
//Store mipmap size in array
mipmaps[0] = mipmapSize;
Console.WriteLine("Mipmap 1: " + mipmapSize);
//Calculate the size of the remaining mipmaps.
//We start the loop at i = 1 because we have calculated the first mipmap already
for (int i = 1; i < mipmapCount; i++)
{
//Only divide by 4 if minimum of 8 bytes (DXT1) has not been reached
if (mipmapSize != 8)
mipmapSize = mipmapSize / 4;
mipmaps[i] = mipmapSize;
Console.WriteLine("Mipmap " + (i + 1) + ": " + mipmapSize);
}
//Now you can start to work with the sizes stored in the integer array...
Console.WriteLine("Press any key to close the window...");
Console.ReadKey();
}
//Console Output:
/*
Mipmap 1: 131072
Mipmap 2: 32768
Mipmap 3: 8192
Mipmap 4: 2048
Mipmap 5: 512
Mipmap 6: 128
Mipmap 7: 32
Mipmap 8: 8
Mipmap 9: 8
*/
//Source (Microsoft Docs): https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-file-layout-for-textures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment