opengl 纹理 blit 速度慢

发布于 2024-07-11 01:11:25 字数 1389 浏览 4 评论 0原文

我每帧调用此函数一次,它使我的 FPS 从 >400 降至 33。为什么?

sw blt(const PtRect *dstRect, Texture *src, const PtRect *srcRect, RenderDevice::bltFlags flags=RenderDevice::bltDefault)
{
    assert(src);
    GL_Texture *glsrc = dynamic_cast<GL_Texture*>(src);
    if (glsrc == 0)
        return -1;

    PtRect srcRect2(0, 0, src->width, src->height);
    if (srcRect == 0)
        srcRect = &srcRect2;

    PtRect dstRect2(0, 0, srcRect->makeWidth(), srcRect->makeHeight());
    if (dstRect == 0)
        dstRect = &dstRect2;

    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, *glsrc->getTex());

    glBegin( GL_QUADS );
    glNormal3f( 0.0f, 0.0f, 1.0f );
    for (size_t i=0; i<350; i++)
    {
        glTexCoord2f( srcRect->left /src->width, srcRect->top/src->height);    glVertex2f(dstRect->left,  dstRect->top);
        glTexCoord2f( srcRect->right/src->width, srcRect->top/src->height);    glVertex2f(dstRect->right, dstRect->top);
        glTexCoord2f( srcRect->right/src->width, srcRect->bottom/src->height); glVertex2f(dstRect->right, dstRect->bottom);
        glTexCoord2f( srcRect->left /src->width, srcRect->bottom/src->height); glVertex2f(dstRect->left,  dstRect->bottom);
    }
    glEnd();
    return 0;
}

I called this function once per frame and it took my FPS from >400 to 33. Why?

sw blt(const PtRect *dstRect, Texture *src, const PtRect *srcRect, RenderDevice::bltFlags flags=RenderDevice::bltDefault)
{
    assert(src);
    GL_Texture *glsrc = dynamic_cast<GL_Texture*>(src);
    if (glsrc == 0)
        return -1;

    PtRect srcRect2(0, 0, src->width, src->height);
    if (srcRect == 0)
        srcRect = &srcRect2;

    PtRect dstRect2(0, 0, srcRect->makeWidth(), srcRect->makeHeight());
    if (dstRect == 0)
        dstRect = &dstRect2;

    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, *glsrc->getTex());

    glBegin( GL_QUADS );
    glNormal3f( 0.0f, 0.0f, 1.0f );
    for (size_t i=0; i<350; i++)
    {
        glTexCoord2f( srcRect->left /src->width, srcRect->top/src->height);    glVertex2f(dstRect->left,  dstRect->top);
        glTexCoord2f( srcRect->right/src->width, srcRect->top/src->height);    glVertex2f(dstRect->right, dstRect->top);
        glTexCoord2f( srcRect->right/src->width, srcRect->bottom/src->height); glVertex2f(dstRect->right, dstRect->bottom);
        glTexCoord2f( srcRect->left /src->width, srcRect->bottom/src->height); glVertex2f(dstRect->left,  dstRect->bottom);
    }
    glEnd();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

全部不再 2024-07-18 01:11:26

如果没有更多细节,很难说,潜在的问题可能是使用太大的纹理,导致驱动程序中的软件回退。 另外,您似乎无缘无故地循环了 350 次。

您可以通过构建数据数组然后调用 glDrawArrays 而不是单独发出每个 glTexCoord/glVertex 对来提高性能

Hard to say without more details, potential problems could be using a too large texture, causing software fallbacks in your driver. Also you appear to loop 350 times for no obvious reason.

You may be able to improve performance by building an array of data and then calling glDrawArrays instead of issuing each glTexCoord/glVertex pair individually

怎言笑 2024-07-18 01:11:26

为了提高性能,您应该在 init 函数中加载纹理并使用列表来显示,然后在主渲染函数上显示,例如:

// Generate texture object ID
glGenTextures(1, img);
glBindTexture(GL_TEXTURE_2D, img);

// Set Texture mapping parameters
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);

LoadBMP("texture.bmp");

然后在主渲染函数上您有类似的内容:

// Front face of Cube
glBindTexture(GL_TEXTURE_2D, img);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-fSize, fSize, fSize);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-fSize, -fSize, fSize);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(fSize,-fSize, fSize);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(fSize,fSize, fSize);
glEnd();

如您所见,您可以将纹理包装在参数而不是在循环中一遍又一遍地重复它。

For performance you should load your textures in an init function and use a list to display then later on the main render function, for example:

// Generate texture object ID
glGenTextures(1, img);
glBindTexture(GL_TEXTURE_2D, img);

// Set Texture mapping parameters
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);

LoadBMP("texture.bmp");

Then on the main render function you have something like:

// Front face of Cube
glBindTexture(GL_TEXTURE_2D, img);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-fSize, fSize, fSize);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-fSize, -fSize, fSize);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(fSize,-fSize, fSize);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(fSize,fSize, fSize);
glEnd();

As you can see you can wrap the texture on the parameters instead of repeating it over and over in a loop.

黑色毁心梦 2024-07-18 01:11:26

也许您没有使用 2 次幂的纹理(例如 1024x768 而不是 1024x512)?

Maybe you are not using Textures with of a power of 2 (e.g. 1024x768 instead of 1024x512)?

故事↓在人 2024-07-18 01:11:26

看起来您出于某种原因在屏幕上绘制了四边形 350 次。 如果这是全屏,那么将屏幕上的每个像素纹理化一帧 350 次必然会损害您的性能。

It looks like you draw the quad onto the screen 350 times for some reason. If this is full screen, then texturing every pixel on the screen 350 times a frame is bound to damage your performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文