我遇到了一个非常奇怪的性能问题.到目前为止,我已经将问题简化为:我在网格中渲染20x20x20立方体,使用glDrawElementsInstanced,只要我的相机远离原点就可以正常工作,但是当它接近原点时,它会开始磨削停了下来.
我通过以下方式定义模型视图投影矩阵:
float distance=3.8;
Projection = glm::perspective(65.0f, (float)(width)/height, 0.1f, 300.0f);
View = glm::lookAt( glm::vec3(0,0,-distance),
glm::vec3(0,0,10),
glm::vec3(0,1,0));
Model = glm::rotate(glm::mat4(1.0f), 0.0f, glm::vec3(0.25f, 1.0f,0.75f));
Run Code Online (Sandbox Code Playgroud)
距离为40,没有问题,但当距离减少到大约3.8和更低时,一切都停止了.
实际的渲染调用是通过以下方式执行的:
glBindVertexArray(cubeVAO);
glDrawElementsInstanced(GL_TRIANGLES, indices.size(),GL_UNSIGNED_INT,(GLvoid*)(0),latticePoints.size());
Run Code Online (Sandbox Code Playgroud)
将所有顶点放在一个缓冲区中并通过调用渲染:
glBindVertexArray(nonInstancedVAO);
glDrawArrays(GL_TRIANGLES, 0,vertices.size() );
Run Code Online (Sandbox Code Playgroud)
完全删除行为.任何经历过类似行为的人都能指出我的解决方案吗?如果失败了,任何人都知道如何追踪这样的事情?我希望我能够使用gDEBugger确定导致速度减慢的原因,但是这只是重新确认没有任何其他opengl调用,并且无法确定是什么占用了所有处理时间.
另一个注意事项是glDrawArraysInstanced也显示相同的减速,并且将调用分成4个单独的调用,每个几何的四分之一也会停止减速.
更新
这是对问题的最小说明的尝试.
//Minimal reproduction of problem
#include <stdio.h>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <vector>
#include <iostream>
#include <stdio.h>
//Set to true to use instanced rendering (glDrawElementsInstanced), false …Run Code Online (Sandbox Code Playgroud)