Texture optimization and Memory Optimization for iOS and Android games (cocos2d-x)

Source: Internet
Author: User
Tags transparent color android games

(Unfinished)

1. The most memory occupied by 2D games is undoubtedly image resources.

2. different platforms of cocos2d-x read different texture mechanisms. Cgimage is used in iOS, While PNG is directly called in Android and windows. I tested that using the PNG Library to directly read PNG would save about 1 MB of memory than cgimage (4 MB of memory for images), but the speed would be twice slower than that of cgimage. How to choose between time and space depends on the actual situation. However, the best choice seems to be PVR (even if the Android version does not use pvrtc4 ).

3. In general, we can directly use w * H * BPP to get the memory occupied by a texture. For example, the 1024*1024 format is argb8888, the memory occupied is 1024*1024*4 = 4 MB. Previously I saw a blog saying that JPG would open up three times the memory (first convert to PNG, then parse PNG), but the new iOS system does not seem to have this problem. JPG and PNG consume almost the same memory, and the JPG resolution speed is faster (almost all are 4 MB resolution + 4 MB texture data, while JPG resolution time is half of PNG ), however, this is quite weird, because JPG is not transparent, with a maximum of 3 bytes per pixel, While PNG is 4 bytes per pixel, and jpg texture should occupy a smaller memory, later, I saw the cocos2d IOS image loadingCodeIt converts all textures to the rgba8888 format. Therefore, both jpg and PNG occupy 4 bytes. PVR is so efficient because cocos2d does not support other textures well.

4. The PVR format can be recognized by the video card without the need to open up temporary memory for reading. Therefore, even images in the same format as argb8888 will be more efficient than PNG, although it will not saveProgramStable Running memory, but it will avoid soaring memory when loading a large number of images. If you are using an iOS device, you can use an image in pvrtc4 format. This format is equivalent to a DDS image in windows and can be directly supported by the video card. It is lossy compression, with only four pixels. However, if there is no gradient translucent color, the general effect is acceptable, and the memory and CPU time saved is very significant.

5. PVR is not gold. Although the PVR format can be used on Android devices, pvrtc4 cannot be used. It is not feasible to reduce the game memory using PVR as on iOS devices.

6. PVR. CCZ is actually the PVR image zip package. When the program reads the file, it first decompress the PVR resource and then read the PVR. However, compression can greatly reduce the image size, so although you know more about the pressure process, there will not be a lot of CPU consumption.

7. memory consumption during actual loading of a jpg image. Take a 1024*1024 argb8888 500k jpg image as an example:. reading image files (consuming image size memory, 500 k) B. parsing JPG data (cgimage, 4 MB) C. Releasing K Image Memory D. OpenGL texture data (4 MB) e. Release the 4 MB memory of cgimage. Note: This process is not necessarily executed sequentially. The release of cgimage memory is actually determined by the system and will be executed quickly, but not necessarily immediately. Therefore, the memory will soar by about 9 MB in an instant, and then decrease by 5 MB to about 4 MB.

The loading process of PNG images is the same as that of PNG images.

PVR images save the cost of parsing image data to textures. That is to say, read PVR image resources (equivalent to decompressing PVR. CCZ to memory. If the format is 1024*1024 argb8888, the image size is 4 MB. After the CCZ compression, the image size is about 1 MB.) 4 MB is consumed, it consumes 4 MB to submit PVR image data to the video card. Then, 4 MB of file data is released. In this case, it seems that PNG is not very advantageous in terms of memory usage. (Note that PVR refers to the argb8888 encapsulated by PVR, which is significantly different from pvrtc4)

8. As the final memory consumption is texture data, as long as the texture data format is certain, No matter what format the image consumes, the memory is the same. For example, if you use a png8 image, the size will be reduced by 70%, but the memory usage is equivalent to that of png24/png32 (the color palette will be restored to a true color during reading, that is, although png8 is a pixel that only occupies 8 places, the color of the color palette is restored when it is read to the memory. It still needs to open up 1024*1024*4 bytes of space to store texture data ). Of course there is no transparent color, cocos2d processing is still different. If there is no transparent color, you can use png24, then the texture space to be opened is 3 MB.

Here, we also need to note that when processing the DDS texture in windows, we are used to putting it in the power of 2, although the image content is only 900*900, however, the image size is 1024*1024. The memory consumed by reading this image is 4 MB. The power of 2 helps improve the running efficiency, but it is not very necessary. Both iOS and Android devices support non-2 full-power textures. So if it is a PNG image, it will be as big as it is. The memory consumed is only 900*900*4 = 3 MB.

9. Do not use the so-called alpha channel to save memory. This will analyze the specific results. I tested (with cocos2d-x and Ghost fire 3D engines respectively), Rgba8888 and rgb888 format. PNG Image Display consumes the same memory. Although the memory opened up for reading 24-bit images is only 3 MB (1024*1024*3), note that if cgimage is used for reading, the value is 4 MB ), however, after glteximage2d is submitted to the video card, the memory size increases by 4 MB. It may be related to the Data Alignment of the video card.

Here I want to test another strange thing. If npot images of PVR are used, rgb888 will consume less memory than rgba8888, however, pot images are the same (PNG images are the same in both cases ). It may be that the powervr video card has special processing.

10. Images of rgb565 and rgb5551 consume half the memory of rgba8888. If there is no transparent gradient, there is no visual difference. This format is preferred for some large background images.

11. PVR image loading speed is 3 ~ Faster than PNG and jpg ~ 5 times (also 1024*1024 argb8888), PNG may consume about ms, but PVR only needs about Ms. For PVR. CCZ compression, the time consumed is about Ms. It can be seen that PVR has great advantages in loading speed. This should be because PNG and jpg need to restore the image data to rgba, but PVR can directly transmit the image data to the video card. Pvrtc4 images can be directly supported by powervr graphics cards.

 

Summary:

1. The final decision of the image's memory usage is its pixel format and size, regardless of its extension. Png8 png32 jpg pvr as long as the pixel format is argb8888, the final image occupies the same memory.

2. If it is not in pvrtc4 format, do not extend it to the full power of 2, because the smaller the image, the smaller the memory usage

3. Removing transparent channels alone does not reduce the memory consumed by images. PNG and jpg images cannot reduce the image size. Therefore, the rgb888 format is not recommended. Select rgb565 and rgb5551.

5. When loading images with caution, the memory volume caused by the texture data temporarily opened is too high. You can consider adding the memory pool to promptly open and release the buffer zone.

6. If you want to reduce the image size, you can choose: 1. jpg-the highest compression ratio and good quality, but not translucent 2. png8-the same picture will be slightly larger than JPG, using imagealpha for conversion makes almost no visual difference. The two image formats can greatly reduce the image size (by 70% ~ 80%), but does not help reduce memory

7. If you want to reduce the memory, you can select: 1. images without transparent colors are converted to the rgb565 format in a unified manner, so png8 cannot be used at this time, so PNG and PVR. the CCZ image size is almost the same, PVR. the CCZ speed is faster, so PVR is recommended. CCZ rgb565 Format 2. If the transparent color is only for key color labeling, but not gradient mixing, we recommend the PVR of rgb5551 (r5_a1. CCZ format

8. You can write a packaging system to package resource files in a unified manner, instead of using PVR. CCZ for zip compression for a single file, which improves efficiency. (For example, I encapsulated Blizzard's mpq package, and its reading speed is equivalent to that of local files, so as to achieve the best read efficiency)

 

 

The data log of my test is attached. The image is a 1024*1024 image. The actual image size is 960*700. The code for loading iPhone 4, png jpg, and other images on the test device is changed to Windows. Tex is followed by the image loading time. The brackets after over indicate the memory consumed by image loading.

[Plain] View plaincopyprint?
  1. 14:28:54. 614 hellocpp [4939: 707] cocos2d: Surface Size: 960x640
  2. Cocos2d: cocos2d: cocos2d-2.1beta3-x-2.1.0
  3. Cocos2d: cocos2d: gl_vendor: Imagination Technologies
  4. Cocos2d: cocos2d: gl_renderer: powervr SGX 535
  5. Cocos2d: cocos2d: gl_version: OpenGL ES 2.0 IMGSGX535-63.24
  6. Cocos2d: cocos2d: gl_max_texture_size: 2048
  7. Cocos2d: cocos2d: gl_max_texture_units: 8
  8. Cocos2d: cocos2d: Gl supports pvrtc: Yes
  9. Cocos2d: cocos2d: Gl supports bgra8888 textures: No
  10. Cocos2d: cocos2d: Gl supports npot textures: Yes
  11. Cocos2d: cocos2d: Gl supports discard_framebuffer: Yes
  12. Cocos2d: cocos2d: Gl supports shareable VAO: Yes
  13. Cocos2d: cocos2d: compiled with profiling support: No
  14. Tex 195 map_0020.bg.pvr
  15. Map_0020.bg.pvr
  16. --- Over proess: 11.0 MB (4.0 MB) Free: 274.6 MB
  17. Tex 159 map_0020.bg_rgb.pvr
  18. Map_001_bg_rgb.pvr
  19. --- Over proess: 15.0 MB (4.0 MB) Free: 270.6 MB
  20. Tex 711 map_0020.bg.jpg
  21. Map_0020.bg.jpg
  22. --- Over proess: 19.1 MB (4.1 MB) Free: 266.7 MB
  23. Tex 653 map_0020.bg_rgb.jpg
  24. Map_001_bg_rgb.jpg
  25. --- Over proess: 23.1 MB (4.0 MB) Free: 262.6 MB
  26. Tex 670 map_0020.bg.png
  27. Map_0020.bg.png
  28. --- Over proess: 27.1 MB (4.1 MB) Free: 258.7 MB
  29. Tex 739 map_0020.bg_rgb.png
  30. Map_0020.bg_rgb.png
  31. --- Over proess: 31.1 MB (4.0 MB) Free: 254.3 MB
  32. Tex 240 map_0020.bg.pvr.ccz
  33. Map_0020.bg.pvr.ccz
  34. --- Over proess: 35.1 MB (4.0 MB) Free: 250.4 MB
  35. Tex 204 map_0020.bg_rgb.pvr.ccz
  36. Map_001_bg_rgb.pvr.ccz
  37. --- Over proess: 39.2 MB (4.0 MB) Free: 246.5 MB
  38. Tex 97 map_0020.bg_rgb565.pvr
  39. Map_001_bg_rgb565.pvr
  40. --- Over proess: 41.2 MB (2.0 MB) Free: 244.6 MB
  41. Tex 710 map_0020.bg_rgb565.png
  42. Map_001_bg_rgb565.png
  43. --- Over proess: 45.2 MB (4.0 MB) Free: 241.1 MB
  44. Tex 591 map_0020.bg_rgba8888f.png
  45. Map_0020.bg_rgba8888f.png
  46. --- Over proess: 47.8 MB (2.6 MB) Free: 238.3 MB
  47. Tex 484 map_0020.bg_rgba8888f.jpg
  48. Map_0020.bg_rgba8888f.jpg
  49. --- Over proess: 49.7 MB (1.9 MB) Free: 236.5 MB
  50. Tex 123 map_0020.bg_rgba8888f.pvr
  51. Map_0020.bg_rgba8888f.pvr
  52. --- Over proess: 52.4 MB (2.7 MB) Free: 234.1 MB
  53. Tex 589 map_0020.bg_rgb888f.png
  54. Map_0020.bg_rgb888f.png
  55. --- Over proess: 55.1 MB (2.7 MB) Free: 231.2 MB
  56. Tex 478 map_0020.bg_rgb888f.jpg
  57. Map_0020.bg_rgb888f.jpg
  58. --- Over proess: 57.0 MB (1.9 MB) Free: 229.4 MB
  59. Tex 92 map_0020.bg_rgb888f.pvr
  60. Map_001_bg_rgb888f.pvr
  61. --- Over proess: 59.0 MB (2.0 MB) Free: 227.8 MB
  62. (Lldb)
14:28:54. 614 hellocpp [4939: 707] cocos2d: Surface Size: Drawing: cocos2d: cocos2d-2.1beta3-x-2.1.0Cocos2d: cocos2d: gl_vendor: Imagination 2D: cocos2d: gl_renderer: powervr SGX 535cocos2d: cocos2d: gl_version: openGL ES 2.0 IMGSGX535-63.24Cocos2d: cocos2d: Drawing: 2048cocos2d: cocos2d: Drawing: 8cocos2d: cocos2d: Gl supports pvrtc: yescocos2d: cocos2d: Gl supports bgra8888 textures: nococos2d: cocos2d: GL supports npot textures: yescocos2d: cocos2d: Gl supports logs: yescocos2d: cocos2d: Gl supports available VAO: yescocos2d: cocos2d: compiled with profiling support: notex 195 certificate --- over proess: 11.0 MB (4.0 MB) Free: 274.6 mbtex 159 bytes --- over proess: 15.0 MB (4.0 MB) Free: 270.6 mbtex 711 map_0020.bg.jpgmap_0020.bg.jpg --- over proess: 19.1 MB (4.1 MB) free: 266.7 mbtex 653 running --- over proess: 23.1 MB (4.0 MB) Free: 262.6 mbtex 670 map_0020.bg.pngmap_0020.bg.png --- over proess: 27.1 MB (4.1 MB) Free: 258.7 mbtex 739 running --- over proess: 31.1 MB (4.0 MB) Free: 254.3 mbtex 240 map_0020.bg.pvr.cczmap_0020.bg.pvr.ccz --- over proess: 35.1 MB (4.0 MB) Free: 250.4 mbtex 204 bytes --- over proess: 39.2 MB (4.0 MB) free: 246.5 mbtex 97 running --- over proess: 41.2 MB (2.0 MB) Free: 244.6 mbtex 710 map_000000bg_rgb565.pngmap_000000bg_rgb565.png --- over proess: 45.2 MB (4.0 MB) Free: 241.1 mbtex 591 running --- over proess: 47.8 MB (2.6 MB) Free: 238.3 mbtex 484 map_0020.bg_rgba8888f.jpgmap_0020.bg_rgba8888f.jpg --- over proess: 49.7 MB (1.9 MB) Free: 236.5 mbtex 123 bytes --- over proess: 52.4 MB (2.7 MB) free: 234.1 mbtex 589 running --- over proess: 55.1 MB (2.7 MB) Free: 231.2 mbtex 478 map_000000bg_rgb888f.jpgmap_000000bg_rgb888f.jpg --- over proess: 57.0 MB (1.9 MB) Free: 229.4 mbtex 92 running --- over proess: 59.0 MB (2.0 MB) Free: 227.8 MB (lldb)

 

The second log is for image loading in IOS. The other log is the same as the previous log. It can be seen that the speed is twice faster than that of PNG in windows, but the memory consumption is higher.

[Plain] View plaincopyprint?
  1. 15:36:10. 330 hellocpp [4979: 707] cocos2d: Surface Size: 960x640
  2. Cocos2d: cocos2d: cocos2d-2.1beta3-x-2.1.0
  3. Cocos2d: cocos2d: gl_vendor: Imagination Technologies
  4. Cocos2d: cocos2d: gl_renderer: powervr SGX 535
  5. Cocos2d: cocos2d: gl_version: OpenGL ES 2.0 IMGSGX535-63.24
  6. Cocos2d: cocos2d: gl_max_texture_size: 2048
  7. Cocos2d: cocos2d: gl_max_texture_units: 8
  8. Cocos2d: cocos2d: Gl supports pvrtc: Yes
  9. Cocos2d: cocos2d: Gl supports bgra8888 textures: No
  10. Cocos2d: cocos2d: Gl supports npot textures: Yes
  11. Cocos2d: cocos2d: Gl supports discard_framebuffer: Yes
  12. Cocos2d: cocos2d: Gl supports shareable VAO: Yes
  13. Cocos2d: cocos2d: compiled with profiling support: No
  14. Tex 196 map_0020.bg.pvr
  15. Map_0020.bg.pvr
  16. --- Over proess: 11.0 MB (4.0 MB) Free: 275.8 MB
  17. Tex 160 map_0020.bg_rgb.pvr
  18. Map_001_bg_rgb.pvr
  19. --- Over proess: 15.0 MB (4.0 MB) Free: 271.9 MB
  20. Tex 130 map_0020.bg.jpg
  21. Map_0020.bg.jpg
  22. --- Over proess: 19.3 MB (4.3 MB) Free: 267.6 MB
  23. Tex 151 map_0020.bg_rgb.jpg
  24. Map_001_bg_rgb.jpg
  25. --- Over proess: 23.5 MB (4.2 MB) Free: 263.8 MB
  26. Tex 344 map_0020.bg.png
  27. Map_0020.bg.png
  28. --- Over proess: 28.7 MB (5.3 MB) Free: 258.7 MB
  29. Tex 328 map_0020.bg_rgb.png
  30. Map_0020.bg_rgb.png
  31. --- Over proess: 34.0 MB (5.3 MB) Free: 253.6 MB
  32. Tex 237 map_0020.bg.pvr.ccz
  33. Map_0020.bg.pvr.ccz
  34. --- Over proess: 38.0 MB (4.0 MB) Free: 249.6 MB
  35. Tex 221 map_0020.bg_rgb.pvr.ccz
  36. Map_001_bg_rgb.pvr.ccz
  37. --- Over proess: 42.0 MB (4.0 MB) Free: 245.2 MB
  38. Tex 98 map_001_bg_rgb565.pvr
  39. Map_001_bg_rgb565.pvr
  40. --- Over proess: 44.0 MB (2.0 MB) Free: 243.2 MB
  41. Tex 300 map_0020.bg_rgb565.png
  42. Map_001_bg_rgb565.png
  43. --- Over proess: 48.9 MB (4.9 MB) Free: 238.2 MB
  44. Tex 293 map_0020.bg_rgba8888f.png
  45. Map_0020.bg_rgba8888f.png
  46. --- Over proess: 52.8 MB (3.9 MB) Free: 234.2 MB
  47. Tex 87 map_0020.bg_rgba8888f.jpg
  48. Map_0020.bg_rgba8888f.jpg
  49. --- Over proess: 55.7 MB (2.9 MB) Free: 231.7 MB
  50. Tex 143 map_0020.bg_rgba8888f.pvr
  51. Map_0020.bg_rgba8888f.pvr
  52. --- Over proess: 58.3 MB (2.7 MB) Free: 228.8 MB
  53. Tex 300 map_0020.bg_rgb888f.png
  54. Map_0020.bg_rgb888f.png
  55. --- Over proess: 62.2 MB (3.9 MB) Free: 225.3 MB
  56. Tex 87 map_0020.bg_rgb888f.jpg
  57. Map_0020.bg_rgb888f.jpg
  58. --- Over proess: 65.1 MB (2.9 MB) Free: 222.7 MB
  59. Tex 91 map_0020.bg_rgb888f.pvr
  60. Map_001_bg_rgb888f.pvr
  61. --- Over proess: 67.0 MB (1.9 MB) Free: 220.7 MB
  62. (Lldb)
Related Article

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.