简体   繁体   English

OpenGL优化

[英]OpenGL Optimization

I'm trying to render 3 quads (1 bg, 2 sprites) using OpenGL. 我正在尝试使用OpenGL渲染3个四边形(1个bg,2个精灵)。 I have the following code: 我有以下代码:

void GLRenderer::onDrawObjects(long p_dt)
{
    float _log_last_time = ELAPSED_MS;
    fprintf(stdout, "-- starting draw: %.4f, dTime: %.4f\n", ELAPSED_MS, ELAPSED_MS - _log_last_time);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    for(std::vector<queuedRenderable*>::iterator itr = m_displayList.begin();
        itr != m_displayList.end();
        itr++)
    {
        Sprite* spriteToRender = (*itr)->m_sprite;

        float _log_last_time1 = ELAPSED_MS;
        fprintf(stdout, "---- profiling object: %.4f, dTime: %.4f\n", ELAPSED_MS, ELAPSED_MS - _log_last_time1);

        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();

        // glTranslate, glRotate, glScale goes here

        fprintf(stdout, "---- transform done: %.4f, dTime: %.4f\n", ELAPSED_MS, ELAPSED_MS - _log_last_time1);
        _log_last_time1 = ELAPSED_MS;

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, (*itr)->m_material->m_glId);

        fprintf(stdout, "---- set material done: %.4f, dTime: %.4f\n", ELAPSED_MS, ELAPSED_MS - _log_last_time1);
        _log_last_time1 = ELAPSED_MS;

        unsigned listIds[] = {
            (*itr)->m_mesh->m_glId
        };
        glCallLists(1, GL_UNSIGNED_INT, listIds);

        fprintf(stdout, "---- draw mesh done: %.4f, dTime: %.4f\n", ELAPSED_MS, ELAPSED_MS - _log_last_time1);
        _log_last_time1 = ELAPSED_MS;
    }

    fprintf(stdout, "-- flushing to card: %.4f, dTime: %.4f\n", ELAPSED_MS, ELAPSED_MS - _log_last_time);
    _log_last_time = ELAPSED_MS;

    glFlush();
    glFinish();

    fprintf(stdout, "-- flush done: %.4f, dTime: %.4f\n", ELAPSED_MS, ELAPSED_MS - _log_last_time);
    _log_last_time = ELAPSED_MS;
}

And I'm getting the following results in console: 我在控制台中得到以下结果:

-- starting draw: 24000.0000, dTime: 0.0000
---- profiling object: 24014.0000, dTime: 0.0000
---- transform done: 24033.0000, dTime: 19.0000
---- set material done: 24046.0000, dTime: 0.0000
---- draw mesh done: 24066.0000, dTime: 1.0000
---- profiling object: 24084.0000, dTime: 0.0000
---- transform done: 24102.0000, dTime: 18.0000
---- set material done: 24120.0000, dTime: 0.0000
---- draw mesh done: 24305.0000, dTime: 164.0000
---- profiling object: 24319.0000, dTime: 0.0000
---- transform done: 24338.0000, dTime: 19.0000
---- set material done: 24356.0000, dTime: 0.0000
---- draw mesh done: 24375.0000, dTime: 2.0000
-- flushing to card: 24389.0000, dTime: 389.0000
-- flush done: 24424.0000, dTime: 18.0000

As you can see, the drawing of the 2nd quad is taking very long (164ms). 如您所见,第二个四边形的图形花费的时间很长(164毫秒)。 I have no idea why is that as they are all quads. 我不知道为什么是四边形。 I've also checked the texture (if that is relevant), but it is a lot smaller than the bg (1st quad). 我还检查了纹理(如果相关),但它比bg(第一个四边形)小得多。 If it is of any relevance, I think the first quad should draw longer, not the second. 如果有关系,我认为第一个四边形应该画更长的时间,而不是第二个四边形。

I'm fairly new to OpenGL (I have done a lot of opengl in the past, but just beginner stuff) so I have some idea of what I'm doing. 我是OpenGL的新手(过去我做了很多opengl,但只是初学者的东西),所以我对自己的工作有所了解。 I'm just not sure how I can optimize this further to remove that 164ms process. 我只是不确定如何进一步优化以消除164ms的过程。

How can I remove or minimize that 164ms process? 如何删除或最小化这164ms的过程?

I have to note that I am using an Intel Atom with intel GMA 450 machine. 我必须注意,我将Intel Atom与Intel GMA 450计算机一起使用。

Let's assume that this mysterious ELAPSED_MS macro were a cycle-accurate CPU counter. 我们假设这个神秘的ELAPSED_MS宏是周期精确的CPU计数器。 I rather doubt that it is, since it seems to jump in large millisecond increments, but let's pretend that this is the case. 我确实对此表示怀疑,因为它似乎以很大的毫秒为单位跳跃,但让我们假装情况确实如此。

Your methodology for profiling OpenGL is completely wrong . 您用于OpenGL配置文件的方法是完全错误的 You are not measuring the time it takes to render a quad. 您没有测量渲染四边形所花费的时间。 You are measuring the time it takes for glFinish to complete. 您正在测量glFinish完成所需的时间。 There's no guarantee that glFinish will return the moment the quad finishes rendering. 无法保证glFinish将在四边形完成渲染后glFinish返回。 Indeed, it could be waiting quite a while, possibly for a thread task switch or something. 确实,它可能等待了很长时间,可能是等待线程任务切换之类的。 Who knows. 谁知道。

The point is that your methods for profiling are not reliable. 关键是您的剖析方法不可靠。 Please stop using them. 请停止使用它们。 The only accurate ways of getting profiling data for actual rendering operations in OpenGL are to either employ timer queries (which are specifically designed to test how long it takes operations on the GPU to complete) or to use proprietary extensions . 为OpenGL中的实际渲染操作获取性能分析数据的唯一准确方法是采用计时器查询 (专门设计用于测试GPU上的操作完成需要多长时间)或使用专有扩展

Micro-profiling (like profiling the time it takes to draw a quad) is very difficult to do right. 微轮廓分析(例如,绘制四边形所需的时间进行轮廓分析)很难做到正确。 Quad drawing in particular is generally worthless to bother profiling. 尤其是四边形绘图通常对打扰轮廓分析毫无价值。

ELAPSED_MS is ((((float)clock()) / CLOCKS_PER_SEC) * 1000.0f) ELAPSED_MS为((((float)clock())/ CLOCKS_PER_SEC)* 1000.0f)

That's so not helping. 太无助了。 Because even if your methods were good, clock is not a reliable method for doing any form of timing. 因为即使您的方法很好, clock 也不是进行任何形式计时的可靠方法。 You have to use either platform-specific calls , or a platform neutral library like C++11's chrono (or the Boost equivalent). 您必须使用特定平台的 调用 ,或使用平台无关的库,例如C ++ 11的chrono (或Boost等价物)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM