Skip to content

Instantly share code, notes, and snippets.

@fenbf
Created October 30, 2013 18:15
Show Gist options
  • Save fenbf/7237365 to your computer and use it in GitHub Desktop.
Save fenbf/7237365 to your computer and use it in GitHub Desktop.
Function that will go through all texture object levels and count pixel counts and memory in bytes. Currently only Compressed textures formats are supported and RGB8 (RGB) or RGBA8 (RGBA)
void CalculateTextureObjectPixels(GLuint texID, unsigned long *pixelCount, unsigned long *memoryUsed)
{
int baseLevel = 0, maxLevel = 0;
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, &baseLevel);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, &maxLevel);
long pixels = 0, bytes = 0, bpp = 0;
int texW = 0, texH = 0, texFmt = 0, compressed = 0, compressedBytes = 0;
for (int level = baseLevel; level <= maxLevel; ++level)
{
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &texW);
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_HEIGHT, &texH);
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_INTERNAL_FORMAT, &texFmt);
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_COMPRESSED, &compressed);
pixels += texW*texH;
if (compressed == GL_TRUE)
{
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &compressedBytes);
bytes += compressedBytes;
}
else
{
if (texFmt == GL_RGB || texFmt == GL_RGB8)
bpp = 3;
else if (texFmt == GL_RGBA || texFmt == GL_RGBA8)
bpp = 4;
else
bpp = 0;
bytes += texW*texH*bpp;
}
if (texW <= 1 && texH <= 1) // break when size is also 0
break;
}
*pixelCount += pixels;
*memoryUsed += bytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment