Memory Optimization in Cocos2d-x WP using compressed textures

Datetime:2016-08-23 04:31:37          Topic: Cocos2d-X           Share

While developing games for Windows Phone using Cocos2d-x, memory optimization is always a tough challenge. This blog will help you learn how to use DDS textures in order to reduce memory usage in Cocos2d-x v3.2 .

GPUs and Compressed Textures

Each GPU supports different compressed texture formats. The following table shows compressed textures supported on Lumia devices:

More info:

DirectDraw Surface (DDS)

In Windows Phone and Windows Store apps, the DirectDraw Surface file format (.dds) is used to store uncompressed and compressed (DXTn) textures. The file format supports mipmaps, cube maps, and volume maps.

These formats are supported by broad hardware:

DXGI_FORMAT_R8G8B8A8_UNORM

DXGI_FORMAT_R8G8B8A8_UNORM_SRGB

DXGI_FORMAT_R8G8B8A8_SNORM

DXGI_FORMAT_B8G8R8A8_UNORM

DXGI_FORMAT_R16G16_SNORM

DXGI_FORMAT_R8G8_SNORM

DXGI_FORMAT_R8_UNORM

DXGI_FORMAT_BC1_UNORM

DXGI_FORMAT_BC1_UNORM_SRGB

DXGI_FORMAT_BC2_UNORM

DXGI_FORMAT_BC2_UNORM_SRGB

DXGI_FORMAT_BC3_UNORM

DXGI_FORMAT_BC3_UNORM_SRGB

More info:

Why using compressed textures?

Performance Test (Lumia 625)

  1. Cache 100 PNG / DDS-DXT5 textures (512x512).
  2. Create 100 sprites from those textures.
  3. Add all sprites to the scene node.
  4. Run and measure.

Test xap files are available here .

Prerequisites

  1. Cocos2d-x v3.2
  2. Crunch (DXT texture compression)
  3. Angle Project v2422 (WinRT/WP8)

How to use DDS textures

Using Crunch to convert PNG files to DDS. The following command would help convert PNG to DDS (DXT1 format):

FOR %%A IN (*.png) DO crunch -file %%A -fileformat dds /DXT1 /rescalemode nearest /mipMode None

Creating sprites from DDS instead of PNG files:

auto sprite = Sprite::create("grossini_dance_atlas.dds", Rect(x, y, 85, 121));

Important things to keep in mind:

  • DXT1 texture format doesn’t support transparency so it’s used for opaque bitmaps only
  • Texture size must be power of two

Enable DXT support in Cocos2d-x v3.2

Cocos2d-x v3.2 has a bug that it always uses software decoder for DDS files. In order to fix this issue, making changes to the following files:

CCConfiguration.cpp: also checks for dxt1/dxt3/dxt5 GLExtensions

_supportsS3TC = checkForGLExtension("GL_EXT_texture_compression_s3tc") || checkForGLExtension("GL_EXT_texture_compression_dxt1") || checkForGLExtension("GL_ANGLE_texture_compression_dxt3") || checkForGLExtension("GL_ANGLE_texture_compression_dxt5");

CCTexture2D.cpp: add new defines for dxt3/dxt5

#ifdef GL_ANGLE_texture_compression_dxt3
	PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT3, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
#endif
#ifdef GL_ANGLE_texture_compression_dxt5
	PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT5, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
#endif

Enable DXT5 format in Angle v2422

Prebuilt Angle libs/dlls in Cocos2d-x v3.2 has a bug that DXT5 texture format isn’t supported. In order to use DXT5 texture format, you’ll need to modify Angle project and rebuild all libs/dlls files:

Renderer11.cpp: DXT3 => BC2, DXT5 => BC3

if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC2_UNORM, &formatSupport)))
{
  mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
}
else
{
  mDXT3TextureSupport = false;
}

if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
{
  mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
}
else
{
  mDXT5TextureSupport = false;
}

Summary

  1. Use Crunch to convert all images to DDS:

    a. Make sure texture width and height are power of two

    b. Use DXT1 for opaque textures

    c. Use DXT3/DXT5 for textures with alpha channel

  2. Update Cocos2d-x source to enable compressed texture

  3. Update Angle source and rebuild libs/dlls to enable DXT5 texture format (optional)

  4. Create sprites from DDS instead of PNG files





About List