我正在 iPhone 上开发一款使用 C++ 和 OpenGL ES 1.x 库的游戏。它在模拟器上运行良好。但是当我将它安装在真机上时,我发现在 iPhone 原版上,渲染一帧大约需要 20 毫秒。然而,在 iPhone 3GS 上渲染一帧需要 35~40 毫秒。
我尝试过各种操作系统,包括 3GS + iOS 3.1.2、3G + iOS 4.0、3GS + iOS 4.1、iPad + iOS 3.2。它们的渲染速度都比 iPhone 原版慢得多,这对我来说听起来很荒谬。我用谷歌搜索了我能想到的任何东西,修复了所有可能与之相关的问题,但没有任何改变。
我有 2 台机器,这些代码的渲染速度更快:1) 带有 iOS 3.1.3 的 iPhone 原版,2) 带有 iOS 3.1.3 的 iPod Touch。两者都花了大约 20 毫秒来渲染一帧。还有 4 台机器的渲染速度出奇地慢:1) iPhone 3G 运行 iOS 4.0,2) iPhone 3GS 运行 iOS 3.1.2,3) iPhone 3GS 运行 iOS 4.1,4) iPad 运行 iOS 3.2。iPhone 大约需要 35-40 毫秒来渲染一帧,而 iPad 大约需要 25 毫秒。
我使用 PVRTC 作为质地,首先将其煮熟并制成一束。它总共使用十个 512x512 纹理,三个 1024x1024 纹理。绑定纹理的代码如下:
GLenum internalFormat = 0;
GLenum pixelType = 0;
// resolve type
ResetFlags_();
assert(2==attr.Dimension && 1==attr.Depth);
switch (attr.Format)
{
case FORMAT_PVRTC2:
assert(attr.Width==attr.Height);
if (attr.AlphaBits>0)
internalFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
else
internalFormat = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
break;
case FORMAT_PVRTC4:
assert(attr.Width==attr.Height);
if (attr.AlphaBits>0)
internalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
else
internalFormat = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
break;
... other formats ...
}
// prepare temp buffer to load
MemoryBuffer tmpBuffer(true);
uint8* buffer = tmpBuffer.GetWritePtr(attr.TextureSize);
// read data
stream.Read(buffer, attr.TextureSize);
if (stream.Fail())
return false;
// init
width_ = attr.Width;
height_ = attr.Height;
LODs_ = attr.LODs;
alphaBits_ = attr.AlphaBits;
// create and upload texture
glGenTextures(1, &glTexture_);
glBindTexture(GL_TEXTURE_2D, glTexture_);
uint32 offset = 0;
uint32 dim = width_; // = height
uint32 w, h;
switch (internalFormat)
{
case GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
case GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
case GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
case GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
for (uint32 i=0; i<LODs_; ++i) {
assert(offset<attr.TextureSize);
w = dim >> ((FORMAT_PVRTC2==attr.Format) ? 3:2);
h = dim >> 2;
// Clamp to minimum number of blocks
if (w<2) w = 2;
if (h<2) h = 2;
uint32 const image_size = w * h * 8; // 8 bytes for each block
glCompressedTexImage2D(GL_TEXTURE_2D, i, internalFormat, dim, dim, 0, image_size, buffer+offset);
dim >>= 1;
offset += image_size;
break;
... other formats ...
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); // tri-linear?
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
SetContext_(&glTexture_);
return true;
渲染部分很大,因为它使用了其他人开发的引擎。据我所知,它使用 glDrawArrays 并且没有使用着色器。
以前有人遇到过同样的问题吗?我真的不明白为什么 iPhone 原来的渲染速度比 iPhone 3GS 快得多。
ps 我忘了说。我只绘制带有纹理的 2D 矩形。在我的游戏中大约有 20 个矩形(一个背景和一个 480x360 大小的 UI。其他通常是 64x64 单位。)