The spritesheet generated by zwoptex can export images in PNG format but also in PVR format. The PVR format can be directly read by the IOS display chip and can be directly displayed without parsing. Therefore, the rendering speed is faster and the memory is saved.
I specifically tested cocos2d 2.0 RC1: An empty cocos2d template project took about 4 MB of memory after running. After a 2048*1024 PNG Image in the format of rgba565 is displayed with ccsprite, the memory usage reaches 20 mb. In the same case, the memory usage is 16 MB after the format is changed to PVR. That is to say, images in PNG format occupy 20-4 = 16 MB, while those in PVR format occupy 16-4 = 12 Mb. Reduced by 25%. Zwoptex also has an option called "CCZ compression". After selection, the image size can be reduced by almost half. The format of this file is XXX. PVR. CCZ, which can be recognized by cocos2d. Pvrtc2 and pvrtc4 are two PVR compression image formats, both of which are PVR files. These two image formats provide faster loading speed and smaller memory usage than normal images. Pvrtc4: Compressed Format, 4 bits per pixel, OK image qualitypvrtc2: Compressed Format, 2 bits per pixel, poor image quality generally, PVR files are in the format of rgba8888: 32-bit texture with alpha channel, best image qualityrgba4444: 16-bit texture with alpha channel, good image qualityrgb565: 16-bit texture without alpha channel, good image quality but no alpha (transparency) formula for memory usage of images is: numbytes = width * height * bitsperpixel/8 that is to say 2048*2048 rgba8888 occupies 16 MB of memory, pvrtc4 only occupies 2 MB
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, after reading the cocos2d IOS image loading code, it converts all textures into the rgba8888 format, so whether it is jpg or PNG, it occupies 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 argb8888 format, PVR is more efficient than PNG, although it will not save the memory for stable program running, it will avoid the memory Spike 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 images display the same memory consumption. 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.
Memory usage of PVR and PNG