【发布时间】:2016-06-28 13:28:31
【问题描述】:
我想测量 opengl 中渲染函数的运行时间。
我在 Visual Studio 2015 上使用 nupengl Nuget 插件。
在下面的代码中,我想测量绘制三角形的时间。
但是,它在 glGenQueries(2, queryID) 处崩溃
请在下面或github查看我的代码
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
void display(void)
{
GLuint64 startTime, stopTime;
unsigned int queryID[2];
// generate two queries
glGenQueries(2, queryID);
// issue the first query
// Records the time only after all previous
// commands have been completed
glQueryCounter(queryID[0], GL_TIMESTAMP);
// call a sequence of OpenGL commands
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f); //Defining color (white)
glBegin(GL_LINE_LOOP);
glVertex3f(5.0f, 5.0f, 0.0f);
glVertex3f(25.0f, 5.0f, 0.0f);
glVertex3f(25.0f, 25.0f, 0.0f);
glEnd();
glFlush();
// issue the second query
// records the time when the sequence of OpenGL
// commands has been fully executed
glQueryCounter(queryID[1], GL_TIMESTAMP);
// wait until the results are available
GLint stopTimerAvailable = 0;
while (!stopTimerAvailable) {
glGetQueryObjectiv(queryID[1],
GL_QUERY_RESULT_AVAILABLE,
&stopTimerAvailable);
}
// get query results
glGetQueryObjectui64v(queryID[0], GL_QUERY_RESULT, &startTime);
glGetQueryObjectui64v(queryID[1], GL_QUERY_RESULT, &stopTime);
printf("Time spent on the GPU: %f ms\n", (stopTime - startTime) / 1000000.0);
}
void init(void)
{
/* select clearing color */
glClearColor(0.5, 0.5, 0.5, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 30.0, 0.0, 35.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("TRIANGLE");
init();
const char* version = (const char*)glGetString(GL_VERSION);
std::cout << version;
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ANSI C requires main to return an int. */
}
【问题讨论】:
-
首先在调试器中运行以捕捉“正在运行”的崩溃。然后您将能够找到它在您的代码中发生的位置,并且您可以告诉我们。
-
亲爱的@JoachimPileborg,感谢您的评论。在上面的屏幕截图中,我运行了调试器和 glGenQueries(2, queryID)。这就是我所知道的。
-
似乎 glGenQueries 没有加载。您需要 OpenGL 1.5 上下文。检查函数的地址并使用GLEW(或其他加载器)并尝试加载它。
标签: c++ windows visual-studio opengl visual-studio-2015