ASTC compression (Adaptable Scalable Texture Compression) is the next feature I developed for Castle Game Engine when working on my mobile game.
Why texture compression is so important (especially) on mobile devices?
Slow CPU/GPU
CPUs/GPUs for mobile devices are significantly slower than average CPUs/GPUs on desktops. So you need to know that when you use PNG files, this files must be processed (decompressed) before they will be usable. This kills performance and drains the battery a lot. Opposite to textures compressed for the GPU.
No special GPU memory
In most mobile devices graphics memory is just normal system RAM, in addition, this memory is not too big and not too fast. The PNG file on the disk may seem small, but it takes up a lot more GPU memory (RAM memory for mobile devices) than the compressed texture which can be bigger on disk.
For Example if we have 512×512 RGBA (32bit) PNG image (file size: 11 045 bytes):
In the memory this will be 512 x 512 x 4 = 1 048 576 bytes.
But the same image compressed with ASTC 8×8 will be only (512/8) x (512/8) x 16 = 65 536 bytes (in memory and as file).
As you can see PNG files are better only seemingly. Of course, the quality may be lower (compression is lossy), but the increase in performance is huge and worth the small quality reduction.
Why ASTC?
There are a lot of texture compression formats (supported by CGE):
- PVRTC – supported by PowerVR GPUs
- ATITC – supported by Qualcomm (Adreno chips)
- S3TC – supported by Nvidia (Tegra chips)
- ETC1 – should be supported by all devices, but does not support alpha channel,
So we need to generate and add textures for all this GPUs? Thanks to ASTC, luckily not!
One to rule them all
ASTC (Adaptable Scalable Texture Compression) was created to sort out this mess by providing one flexible format. All new GPUs starting from 2nd generation of Mali-T600 series, Adreno 400, Apple A8 support ASTC. Very old devices are not supported but I think they are not usable in these days (especially for play games). If you want you can add some other compression format to support them.
Different levels of compression
Block size can be from 4 (the best quality) to 12 (the smallest size). Depending on the purpose, you can choose a different quality level.
Any size
The texture size don’t need to be the power of 2. So there is no problem with irregular textures sizes (like GUI elements).
How to use ASTC in Castle Game Engine?
Castle Game Engine allows you to easily generate compressed versions of textures, but first you need configure your environment.
Tools configuration
To make ASTC textures you need two tools in your PATH
environment variable:
-
astcenc
– that’s tool encode your textures, you can get it with Mali Texture Compression Tool, -
PVRTexToolCLI
form PowerVR Tools and SDK – it’s good to choose online installer and only download what you need (full installer is about 2 GB).
On my Windows machine these paths looks like:
C:\Program Files\ARM\Mali Developer Tools\Mali Texture Compression Tool v4.3.0\bin\
and C:\Imagination\PowerVR_Graphics\PowerVR_Tools\PVRTexTool\CLI\Windows_x86_64
.
On Linux machine you can edit your .profile
:
export PATH=$PATH:"/opt/Imagination/PowerVR_Graphics/PowerVR_Tools/PVRTexTool/CLI/Linux_x86_64/" export PATH=$PATH:"/home/user/bin/Mali_Texture_Compression_Tool_v4.3.0.b81c088_Linux_x64/bin/"
Of course this paths is only an example, you should use the paths in which you installed astcenc
and PVRTexToolCLI
.
CGE configuration
First you need to prepare material_properties.xml
file in game data directory, the simplest will look like that:
<?xml version="1.0"?> <properties> <auto_generated_textures> <compress> <format name="ASTC_8x8_RGBA"/> </compress> <scale smallest="1" /> <include path="*" recursive="True" /> </auto_generated_textures> </properties>
More info about this file you can found here.
Next you need to say CGE about textures. To do this just add CastleMaterialProperties
to uses
section and set MaterialProperties.URL
in game initialization code:
MaterialProperties.URL := 'castle-data:/material_properties.xml';
Generating textures
To generate textures call castle-engine
build tool with auto-generate-textures
option:
castle-engine auto-generate-textures
After changing your game data, always call castle-engine auto-generate-textures
. It automatically rebuilds only the necessary textures.
You can clean auto-generated stuffs by:
castle-engine auto-generate-clean
Warning! If you use the CGE release before 24/07/2019, you need to rebuild the castle-engine tool.
What to add to VSC (git, mercurial, svn)?
Generating textures can take some time if you want to avoid this you can attach generated files to VSC. But in that case you should also add castle_engine_auto_generated.xml
. If you do not want to add the generated files, do not add castle_engine_auto_generated.xml
file too.