Advertisement
VXP

GetMemRequired

VXP
Jun 28th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. int GetMemRequired( int width, int height, ImageFormat imageFormat, bool mipmap )
  2. {
  3.     if( !mipmap )
  4.     {
  5.         if( imageFormat == IMAGE_FORMAT_DXT1 ||
  6.             imageFormat == IMAGE_FORMAT_DXT3 ||
  7.             imageFormat == IMAGE_FORMAT_DXT5 )
  8.         {
  9. /*
  10.             DDSURFACEDESC desc;
  11.             memset( &desc, 0, sizeof(desc) );
  12.  
  13.             DWORD dwEncodeType;
  14.             dwEncodeType = GetDXTCEncodeType( imageFormat );
  15.             desc.dwSize = sizeof( desc );
  16.             desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
  17.             desc.dwWidth = width;
  18.             desc.dwHeight = height;
  19.             return S3TCgetEncodeSize( &desc, dwEncodeType );
  20. */
  21.             Assert( ( width < 4 ) || !( width % 4 ) );
  22.             Assert( ( height < 4 ) || !( height % 4 ) );
  23.             if( width < 4 && width > 0 )
  24.             {
  25.                 width = 4;
  26.             }
  27.             if( height < 4 && height > 0 )
  28.             {
  29.                 height = 4;
  30.             }
  31.             int numBlocks = ( width * height ) >> 4;
  32.             switch( imageFormat )
  33.             {
  34.             case IMAGE_FORMAT_DXT1:
  35.                 return numBlocks * 8;
  36.                 break;
  37.             case IMAGE_FORMAT_DXT3:
  38.             case IMAGE_FORMAT_DXT5:
  39.                 return numBlocks * 16;
  40.                 break;
  41.             default:
  42.                 Assert( 0 );
  43.                 return 0;
  44.                 break;
  45.             }
  46.         }
  47.         else
  48.         {
  49.             return width * height * SizeInBytes(imageFormat);
  50.         }
  51.     }
  52.     else
  53.     {
  54.         int memSize = 0;
  55.  
  56.         while( 1 )
  57.         {
  58.             memSize += GetMemRequired( width, height, imageFormat, false );
  59.             if( width == 1 && height == 1 )
  60.             {
  61.                 break;
  62.             }
  63.             width >>= 1;
  64.             height >>= 1;
  65.             if( width < 1 )
  66.             {
  67.                 width = 1;
  68.             }
  69.             if( height < 1 )
  70.             {
  71.                 height = 1;
  72.             }
  73.         }
  74.  
  75.         return memSize;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement