【问题标题】:Measure running time in Opengl on Windows? [duplicate]在 Windows 上测量 Opengl 的运行时间? [复制]
【发布时间】: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


【解决方案1】:

这个错误表明函数指针glGenQueries 是一个空指针。这通常在 windows 下无法加载特定扩展,或者请求/支持的 OpenGL 版本不够高时发生。

您要使用的函数从 OpenGL 1.5 开始可用,因此您必须确保上下文至少具有此版本。使用 freeglut,可以使用glutInitContextVersionglutInitContextFlags 请求特定版本和配置文件。另请注意,在 windows 下,OpenGL > 1.1 的所有功能都必须手动加载或通过上下文管理库(如 glew)加载。

编辑:在glutCreateWindow之前直接添加以下代码:

glutInitContextVersion (1, 5);

这在glutCreateWindow之后。

glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
  /* Problem: glewInit failed, something is seriously wrong. */
  fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}

他们的网站上给出了有关 glew 的构建和链接的详细信息。

【讨论】:

  • 亲爱的@BDL,感谢您的回答。你能补充更多细节吗? (代码示例)。我是opengl的新手。
  • The function you want to use is available starting from OpenGL 1.5 请注意,这实质上意味着,给定任何合理的驱动程序,您将永远不会得到不支持此功能的 OpenGL 上下文。仅在 OpenGL-3.3 中引入了核心/兼容性配置文件,如果它具有核心上下文能力,则特别要求 而默认的 Windows 驱动程序就是这种情况。* 解决方案:从 GPU 供应商网站下载并安装驱动程序。
猜你喜欢
  • 1970-01-01
  • 2015-07-18
  • 2013-03-21
  • 2012-08-15
  • 1970-01-01
  • 2013-12-28
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多