gpt4 book ai didi

c - glTexImage2D,以随机和偶然的方式,在执行中花费了 800 倍以上

转载 作者:行者123 更新时间:2023-12-04 11:14:16 26 4
gpt4 key购买 nike

我将 glsl 2.0 用于某些 GPGPU 目的(我知道,这不是 GPGPU 的最佳选择)。

我有一个矩阵乘法的减少阶段,我必须不断减少纹理大小(我使用 glTexImage2D)。伪代码是这样的:

// Start reduction
for (int i = 1; i <= it; i++)
{
glViewport(0, 0, x, y);
glDrawArrays(GL_TRIANGLES, 0, 6);

x = resize(it);
if (i % 2 != 0)
{
glUniform1i(tex2_multiply_initialstep, 4);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer3);
// Resize output texture
glActiveTexture(GL_TEXTURE5);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, x, y, 0, GL_RGBA, GL_FLOAT, NULL);
}
else
{
glUniform1i(tex2_multiply_initialstep, 5);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer2);
// Resize output texture
glActiveTexture(GL_TEXTURE4);
// A LOT OF TIME!!!!!!!
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, x, y, 0, GL_RGBA, GL_FLOAT, NULL);
// A LOT OF TIME!!!!!!!
}
}

在某些迭代中,else 分支的 glTexImage2D 花费的时间是其他分支的 800 倍。我对 x 和 y 进行了硬编码测试,但出人意料地在相同的迭代中花费了相似的高时间,因此与 x 值无关。

这里有什么问题吗?没有 glTexImage2D 调整大小的替代方案?

谢谢。

编辑:

我知道 glsl 2.0 对于 GPGPU 来说是一个糟糕的选择,但它对我的项目来说是强制性的。所以我无法使用像 glTexStorage2D 这样的函数,因为它们不包含在 2.0 子集中。

最佳答案

我不确定我是否完全理解您想要实现的目标,但是 glTexImage2D 会在您每次调用它时重新分配内存。你可能想调用 glTexStorage2D,然后调用 glTexSubImage2D .

可以查看Khronos's Common Mistakes page关于那个。相关部分是:

Better code would be to use texture storage functions (if you have OpenGL 4.2 or ARB_texture_storage) to allocate the texture's storage, then upload with glTexSubImage2D:

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width, height);
glTexSubImage2D(GL_TEXTURE_2D, 0​, 0, 0, width​, height​, GL_BGRA, GL_UNSIGNED_BYTE, pixels);

This creates a texture with a single mipmap level, and sets all of the parameters appropriately. If you wanted to have multiple mipmaps, then you should change the 1 to the number of mipmaps you want. You will also need separate glTexSubImage2D calls to upload each mipmap.

If that is unavailable, you can get a similar effect from this code:

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);

Again, if you use more than one mipmaps, you should change the GL_TEXTURE_MAX_LEVEL to state how many you will use (minus 1. The base/max level is a closed range), then perform a glTexImage2D (note the lack of "Sub") for each mipmap.

关于c - glTexImage2D,以随机和偶然的方式,在执行中花费了 800 倍以上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49923901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com