我最近发现 glDrawArrays 在每一帧上分配和释放大量内存。 我怀疑它与 openGL 探查器报告的“Shaders compiled outside initialization”问题有关。这发生在每一帧!是不是应该只有一次,着色器编译完成后就消失了?
编辑:我还仔细检查了我的顶点是否正确对齐。所以我真的很困惑内存驱动程序需要在每一帧上分配什么。
编辑 #2:我正在使用 VBO 和退化的三角形带来渲染 Sprite 和 .我在每一帧上传递几何图形 (GL_STREAM_DRAW)。
编辑#3:
我认为我已接近问题但仍无法解决。 如果我将相同的纹理 ID 值传递给着色器,问题就会消失(请参阅源代码注释)。我认为这个问题不知何故与片段着色器有关。
在我的 sprite 批处理中,我有 sprite 列表,我通过纹理 ID 和 FIFO 队列渲染它们。
这是我的 Sprite 批处理类的源代码:
void spriteBatch::renderInRange(shader& prog, int start, int count){
int curTexture = textures[start];
int startFrom = start;
//Looping through all vertexes and rendering them by texture id's
for(int i=start;i<start+count;++i){
if(textures[i] != curTexture || i == (start + count) -1){
//Problem occurs after decommenting this line
// prog.setUniform("texture", curTexture-1);
prog.setUniform("texture", 0); // if I pass same texture id everything is OK
int startVertex = startFrom * vertexesPerSprite;
int cnt = ((i - startFrom) * vertexesPerSprite);
//If last one has same texture we just adding it
//to last render call
if(i == (start + count) - 1 && textures[i] == curTexture)
cnt = ((i + 1) - startFrom) * vertexesPerSprite;
render(vbo, GL_TRIANGLE_STRIP, startVertex+1, cnt-1);
//if last element has different texture
//we need to render it separately
if(i == (start + count) - 1 && textures[i] != curTexture){
// prog.setUniform("texture", textures[i]-1);
render(vbo, GL_TRIANGLE_STRIP, (i * vertexesPerSprite) + 1, 5);
}
curTexture = textures[i];
startFrom = i;
}
}
inline GLint getUniformLocation(GLuint shaderID, const string& name) {
GLint iLocation = glGetUniformLocation(shaderID, name.data());
if(iLocation == -1){ // shader variable not found
stringstream errorText;
errorText << "Uniform \"" << name << " was not found!";
throw logic_error(errorText.str());
}
return iLocation;
}
void shader::setUniform(const string& name, const matrix& value) {
GLint location = getUniformLocation(this->programID, name.data());
glUniformMatrix4fv(location, 1, GL_FALSE, &(value[0]));
}
void shader::setUniform(const string& name, int value) {
GLint iLocation = getUniformLocation(this->programID, name.data());
//GLenum error = glGetError();
glUniform1i(iLocation, value);
// error = glGetError();
}
编辑#4:我尝试在 IOS 6 和 Iphone5 上分析应用程序,分配的空间要大得多。但是在这种情况下方法不同。我正在附上新的屏幕截图。
最佳答案
问题已通过为每个纹理创建单独的着色器 得到解决。 看起来驱动程序实现中的错误确实发生在所有 IOS 设备上(我在 IOS 5/6 上测试过)。然而,在更高型号的 iPhone 上,它并不那么明显。
在 iPhone4 上,性能从 60 FPS 到 38 FPS 非常显着!
关于iphone - glDrawArrays 在每一帧上分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12884143/