简体   繁体   English

有没有更快的方法在 OpenGL 中指定立方体贴图纹理?

[英]Is there a quicker way to specify cubemap texture in OpenGL?

I'm specifying cubemap texture for my skybox in the following way:我通过以下方式为我的天空盒指定立方体贴图纹理:

 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 0, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texData(0));
 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 1, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texData(1));
 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 2, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texData(2));
 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 3, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texData(3));
 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 4, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texData(4));
 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 5, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texData(5));

texData is an unsigned char* vector . texData是一个unsigned char* vector

Using Visual Studio Debugger I found that each line takes about 4ms to run, so using 6 lines to specify the cubemap texture takes about 20-25ms in total.使用 Visual Studio Debugger 我发现每行大约需要 4 毫秒来运行,所以使用 6 行来指定立方体贴图纹理总共需要大约 20-25 毫秒。 I update this cubemap texture in each iteration of my main loop, and it is slowing down my main loop considerably.我在主循环的每次迭代中更新这个立方体贴图纹理,它大大减慢了我的主循环。 I know skyboxes are tradionally static but my application needs the skybox to be updated because I'm creating a 360 video viewer.我知道天空盒传统上是静态的,但我的应用程序需要更新天空盒,因为我正在创建 360 度视频查看器。

Is there another way to specify the cubemap texture that could be faster?有没有另一种方法可以更快地指定立方体贴图纹理? I have checked OpenGL's docs already but I don't see a faster way.我已经检查了 OpenGL 的文档,但我没有看到更快的方法。

UPDATE: I replaced glTexImage2D with glTexSubImage2D for all iterations except the 0th iteration and now the total time taken by 6 glTexSubImage2D lines is under 5ms.更新:除了第0th次迭代之外,我用glTexSubImage2D替换了所有迭代的glTexImage2D ,现在 6 glTexSubImage2D行所花费的总时间低于 5ms。 This is satisfactory for me but I guess I'll leave the question open because technically there's no answer yet.这对我来说是令人满意的,但我想我会留下这个问题,因为从技术上讲还没有答案。

glTexImage is slower as every time you call it, it will allocate memory on driver side and copy pixel data from your image from CPU to GPU, which happens over the bus. glTexImage速度较慢,因为每次调用它时,它都会在驱动程序端分配内存,并将图像中的像素数据从 CPU 复制到 GPU,这发生在总线上。

On the other hand, glTexSubImage is not allocating memory every time.另一方面, glTexSubImage不是每次都分配内存。 On first call it allocates memory and holds the pointer to it.第一次调用时,它分配内存并保存指向它的指针。 Later it just copies directly to the memory via that pointer.稍后它只是通过该指针直接复制到内存中。

I think depending on filtering flags you mention, OpenGL might be creating different texture levels.我认为根据您提到的过滤标志,OpenGL 可能会创建不同的纹理级别。

Try using glTexStorage2D with level 1 .尝试使用级别为1 glTexStorage2D

Also, try using SOIL library - it has one function with simple API to load cubemap.此外,尝试使用 SOIL 库 - 它具有一个带有简单 API 的函数来加载立方体贴图。

One other thing you can try is compressing textures and then profile your program - I am sure this option should give you the best performance.您可以尝试的另一件事是压缩纹理,然后分析您的程序 - 我相信这个选项应该会给您最好的性能。

glTexImage2D is slow because it is copying large amounts of dqta from CPU memory to GPU memory. glTexImage2D 很慢,因为它将大量 dqta 从 CPU 内存复制到 GPU 内存。 If the images are coming from a video decoder, they are possibly already in GPU memory.如果图像来自视频解码器,它们可能已经在 GPU 内存中。 In such a case you might be able to texturize then using OpenGL extensions.在这种情况下,您可以使用 OpenGL 扩展进行纹理化。

These tend to be platform specific though.不过,这些往往是特定于平台的。

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

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