简体   繁体   English

是否可以在OpenGL中创建几个Texture2D objs的Texture2D_Array?

[英]Is it possible to create a Texture2D_Array off several Texture2D objs in OpenGL?

About my intend: 关于我的意图:

I have an image stack and for several reasons I need them all to be separate 2D-textures. 我有一个图像堆栈,由于几个原因,我需要它们都是单独的2D纹理。 Then for some composed display I need like 5-10 of them to be fused together in one display (perfect for Texture2d_Array). 然后对于一些合成的显示器,我需要将它们中的5-10个融合在一起(适用于Texture2d_Array)。 My idea was to take the already uploaded 2D textures and form them into a Texture2d-array. 我的想法是获取已经上传的2D纹理并将它们组成一个Texture2d数组。 Is that possible with the functions that OpenGL gives me? 这可能与OpenGL给我的功能有关吗?

Remark: I want to avoid re-loading the image data and for efficientcy reasons the already created mip-maps are very useful and should be retained. 备注:我想避免重新加载图像数据,并且出于效率原因,已经创建的mip-maps非常有用并且应该保留。 Therefore actions like "Get data off Tex2d create new Tex2Darray-layer" are stupid. 因此,诸如“从Tex2d获取数据创建新的Tex2Darray层”之类的操作是愚蠢的。

If that fails, my fallback would be to have around 10 samplers in my Shader and add/average them together before display. 如果失败了,我的后备将是在我的着色器中有大约10个采样器并在显示之前将它们加在一起/平均。

Anything is possible! 一切皆有可能! Well, almost anything... 好吧,差不多......

Depending on the OpenGL version you have available, there are a few options you can consider. 根据您提供的OpenGL版本,您可以考虑一些选项。 This is if you really need to copy the data. 这是你真的需要复制数据。 Of course by far the best option would be to load the texture data into an array texture in the first place, and avoid the extra copy. 当然到目前为止,最好的选择是首先将纹理数据加载到数组纹理中,并避免额外的复制。

glCopyImageSubData()

The glCopyImageSubData() is a very direct match for the functionality you want. glCopyImageSubData()与您想要的功能非常直接匹配。 The only downside is that it's only available in OpenGL 4.3 and later. 唯一的缺点是它只在OpenGL 4.3及更高版本中可用。

glCopyTexImage2D()

I'm mentioning this just for completeness sake, in case somebody looks at this answer for a slightly different use case. 我只是为了完整起见而提到这一点,以防有人看到这个答案略有不同的用例。 glCopyTexImage2D() can generally be used to copy texture data by setting up the source texture as an FBO attachment. glCopyTexImage2D()通常可用于通过将源纹理设置为FBO附件来复制纹理数据。

But it turns out that it does not support GL_TEXTURE_2D_ARRAY , so it can't be used to copy from a regular texture to an array texture. 但事实证明它不支持GL_TEXTURE_2D_ARRAY ,因此它不能用于从常规纹理复制到数组纹理。 The other direction would work. 另一个方向是有效的。

glBlitFramebuffer()

glBlitFramebuffer() is available in OpenGL 3.0 and later, and can be used to copy almost any kind of framebuffer/texture data. glBlitFramebuffer()在OpenGL 3.0及更高版本中可用,可用于复制几乎任何类型的帧缓冲/纹理数据。

To copy from texture to texture, you create two FBOs. 要从纹理复制到纹理,可以创建两个FBO。 You bind one of them as GL_READ_FRAMEBUFFER , and attach the source texture to it. 您将其中一个绑定为GL_READ_FRAMEBUFFER ,并将源纹理附加到它。 You bind the second one as GL_DRAW_FRAMEBUFFER , and attach the target texture to it. 将第二个绑定为GL_DRAW_FRAMEBUFFER ,并将目标纹理附加到它。 Then you call glBlitFramebuffer() . 然后你调用glBlitFramebuffer()

You might be able to bypass your limitation to have each texture in a separate 2D texture using a combination of the following two techniques: 您可以绕过限制,使用以下两种技术的组合将每个纹理放在单独的2D纹理中:

glTextureView()

Create a single texture array and then use glTextureView() (core in OpenGL 4.3+) to create separate 2D textures which behave exactly as your currently existing ones. 创建单个纹理数组,然后使用glTextureView() (OpenGL 4.3+中的核心)创建单独的2D纹理,其行为与您当前存在的纹理完全相同。 The difference compared to the functions outlined by Reto Koradi is that texture views do not duplicate memory on the GPU which might be more efficient depending on your needs. Reto Koradi概述的功能相比,差异在于纹理视图不会复制GPU上的内存,这可能会更高效,具体取决于您的需求。

However, you cannot use this function to map several existing 2D textures together into a new 2D array as you requested. 但是,您无法使用此功能将多个现有2D纹理一起映射到您请求的新2D阵列中。 The reason for that is simply because there is not enough control over GPU memory mapping in OpenGL, although this would be possible in theory at least in some cases (given that the size of each slice is dividable by the page size of your GPU's memory controller). 原因很简单,因为对OpenGL中的GPU内存映射没有足够的控制,尽管至少在某些情况下这在理论上是可行的(假设每个切片的大小可以通过GPU的内存控制器的页面大小来分割) )。

Texture Atlas 纹理图集

You don't necessarily need to create a new texture array with the exact set of 2D texture slices for each of your special use cases, but instead bind the large texture array containing all textures to a single (or multiple) sampler(s) and pass it along with an array of slice indices (selecting only your textures you need) to your shader. 您不一定需要为每个特殊用例创建一个具有精确2D纹理切片集的新纹理数组,而是将包含所有纹理的大纹理数组绑定到单个(或多个)采样器和将它与一组切片索引(仅选择您需要的纹理)一起传递给着色器。 This option is obviously only feasible if you are willing/able to change the shader source code. 如果您愿意/能够更改着色器源代码,则此选项显然是可行的。

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

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