OS and Android game texture optimization and memory optimization (COCOS2D-X)

Source: Internet
Author: User


Note: The original address is unknown!


1, 2d game the most memory is undoubtedly the picture resources.

2, Cocos2d-x different platform to read the texture of different mechanisms.

iOS below use cgimage,android and Windows is directly calling the PNG library. I tested it. Using PNG libraries to read PNG directly will save about 1MB of memory (image occupies 4mb of memory) than Cgimage, but it is one times slower than Cgimage.

Time and space how to choose to see the actual situation. Just the best option seems to be PVR (even Android version number.) Even if you do not use PVRTC4).

3, in general, we can directly use the W * H * bpp to get a texture of the memory, for example, a 1024*1024 format of argb8888, then he accounted for the memory is 1024*1024*4=4MB.

Before seeing a blog mention jpg will open up 3 times times with this memory (first convert to PNG, then parse PNG). But the new iOS system doesn't seem to have this problem. JPG and PNG consume nearly the same amount of memory, and JPG parsing is faster (almost all 4MB parsing +4MB texture data, and JPG parsing time is half of PNG). But this is very strange, because JPG is not transparent color, a pixel up to 3 bytes, and png a pixel 4 bytes, JPG texture should occupy less memory. Later, I looked at the code of the cocos2d iOS loaded image, which converted all textures into rgba8888 format. So whether it's JPG or PNG, it takes 4 bytes. Because cocos2d support for other textures is not good enough, PVR will appear so efficient.

4, PVR format can be recognized by the graphics card. Without the need to open up temporary memory to read, so even if the same as the argb8888 format of the picture, PVR will be more efficient than PNG.  While it does not save memory when the program is executing stably, it avoids a spike in memory when loading large numbers of images. And assuming that it is an iOS device, you can use the PVRTC4 format image, which is equivalent to the DDS image under Windows, can be directly supported by the graphics card. It is lossy compression, a pixel is only 4 bits, just assume that there is no gradient translucent color, the general effect can be accepted. and its savings in memory and CPU time is very significant.

5, PVR is not a balm.

Despite being able to use the PVR format under Android devices. However, it is not feasible to use PVRTC4 to really reduce the memory of the game via PVR like on iOS devices.

6, Pvr.ccz In fact is the PVR Picture Zip packaging, the program read the time or the first to extract the PVR resources. Then read the PVR again.

Just because compression can greatly reduce the volume of the picture. So even if you know more about the pressure process there will be no extra CPU consumption.

7, a JPG image of the actual loading process memory consumption, a 1024*1024 argb8888 500k jpg Image for example: A. Read picture file (memory consumption, 500k) B, parse jpg data (cgimage, 4MB) C,      Release 500k of image memory D, OpenGL texture data (4MB) e, release cgimage 4MB of memory. Attention.  This process is not necessarily sequential operation, the release of cgimage memory is actually a system-determined, will be very fast, but not necessarily run immediately. So the memory instantly soared around 9mb, then lowered by 5MB. Stable to around 4MB

The loading process for PNG images is the same

       PVR pictures can save the cost of parsing image data to textures. That is to read the PVR Picture resource (equivalent to extracting PVR.CCZ to memory, assuming 1024*1024 argb8888 format. Then the image size is 4mb,ccz compressed image around 1MB) consumption of 4MB, the PVR picture data submitted to the graphics card consumption 4mb. Then release the file data 4mb. It seems that this is not an advantage compared to PNG's memory footprint. (Note that the PVR referred to here refers to the PVR package argb8888, and the performance of the PVRTC4 is very poor)

8, because the memory is finally consumed by the texture data. So just the texture data format is certain, no matter what the image format consumes the memory is the same. Using PNG8 images, for example, will reduce the volume by 70%. However, memory consumption is equivalent to PNG24/PNG32 (the color palette is restored to true color when it is read.) That is, although PNG8 is a pixel that occupies only 8 bits. However, the color palette is restored when it is read into memory. Still need to open up 1024*1024*4 bytes of space to store texture data). Of course there is no transparent color, the treatment of cocos2d is still different.

Assume that there is no transparent color. With the ability to use PNG24, the required texture space is 3MB.

        Here's another point to note that when we deal with DDS textures under windows, we're accustomed to pressing them to the power of 2, even though the image content is only 900*900. But the size of the picture is 1024*1024. The memory we use to read this image is 4MB. A power of 2 is useful for improving execution efficiency. But it's not really necessary. Both iOS and Android devices support textures that are not 2 full power. So if it's a PNG picture, then how big it is. The memory consumed at this time is only 900*900*4=3MB.

9, do not be too superstitious so-called removal of the alpha channel to save memory.  This will also be a practical analysis of the detailed results. I tested (using the cocos2d-x and the Wisp 3d engine respectively ), and the PNG images in rgba8888 and rgb888 format showed the same amount of memory consumed.

24-bit image Although the memory opened up at the time of reading is only 3MB (1024*1024*3, which is assumed to be read with Cgimage, then this value is 4MB), but Glteximage2d submitted to the video card will still add 4MB of memory.

may be related to the data alignment of the video card.

I'm here to test another weird place. Assuming that the npot picture of PVR, rgb888 than rgba8888 consumes less memory, but pot pictures are the same (PNG image two cases are the same). It is possible that the PowerVR graphics card has special handling.

10. rgb565 and rgb5551 's images consume half the memory of rgba8888, assuming there is no transparent gradient. There is no difference in sight. Some of the big background graphs will give preference to this format.

11, PVR pictures load faster than PNG and JPG (same 1024*1024 argb8888). PNG may consume about 700ms, but the PVR only needs about 100ms. The assumption is PVR.CCZ compression. The time consumed is around 200ms. It is obvious that PVR has a great advantage in loading speed. This should be because PNG and JPG need to restore the image data to Rgba. But PVR can pass the image data directly to the graphics card. PVRTC4 images can be directly supported by the PowerVR graphics card.


Summary below:

1, finally decided that the image occupies memory is its pixel format and size, regardless of its extension.

Png8 png32 jpg PVR just want its pixel format is argb8888, then finally the image occupies the same memory.

2, assuming that the format is not PVRTC4. Then do not extend the power of 2 to the whole. The smaller the picture, the smaller the memory footprint

3, only remove the transparent channel will not reduce the memory consumed by the image, PNG and JPG pictures can not reduce the volume of the picture, so the rgb888 format is not recommended.

Alternative options for rgb565 and rgb5551.

5, carefully load the image when the temporary opening of the texture data caused by high memory, can consider increasing the memory pool. Timely opening and releasing of buffers.

6, the hypothesis is to reduce the picture volume can choose: 1, jpg--compression ratio is the highest, the quality is better. But does not support translucent 2, png8--the same picture will be slightly larger than JPG, using Imagealpha to convert. There is almost no difference in sight.

Both of these image formats can greatly reduce the volume of the image (reducing 70%~80%). But it's not helping to reduce memory

7, the assumption is to reduce the memory can choose: 1, no transparent color of the picture unified conversion to rgb565 format. It is not possible to use PNG8 at this time, so PNG and PVR.CCZ pictures are nearly the same size, PVR.CCZ is faster, so it is recommended to PVR.CCZ rgb565 format 2, assuming that the transparent color is not a key color annotation, but no gradient blending. Then recommend the PVR.CCZ format for rgb5551 (R5_A1)

8, can consider writing a packaging system, unified resource file packaging, rather than a single file with PVR.CCZ for zip compression. This allows for higher efficiency. (for example, I encapsulated the MPQ package for Blizzard, which reads as fast as the local file reads.) This allows for optimal read efficiency)



Finally, I enclose my test data log. The picture is a picture of a 1024*1024. The actual picture content size is 960*700. Test equipment iphone4,png jpg and other image loading code changes to the Windows version number. Tex behind is the loading moment of the picture.

The over surface brackets are loaded into memory.

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

OS and Android game texture optimization and memory optimization (COCOS2D-X)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.