简体   繁体   English

与常规缓冲区和制服相比,OpenGL纹理单元背后的原因是什么?

[英]What is the reasoning behind OpenGL texture units as opposed to regular buffers and uniforms?

I am very new to the OpenGL API and just discovered textures and how to use them. 我是OpenGL API的新手,刚刚发现了纹理以及如何使用它们。 The generated texture buffers are not bound in the same way as regular uniforms are bound and instead use glActiveTexture , followed by a bind rather than just supplying the texture to the shaders via glUniform as we do with other constants. 生成的纹理缓冲区的绑定方式与绑定常规制服的方式不同,而是使用glActiveTexture ,然后使用绑定,而不是仅通过glUniform将纹理提供给着色器,就像我们使用其他常量一样。

What is the reasoning behind this convention? 这次大会背后的原因是什么?

The only reason I can think of is to utilize the graphics card's full potential and texture processing capabilities instead of just binding buffers directly. 我能想到的唯一原因是利用显卡的全部潜力和纹理处理能力,而不仅仅是直接绑定缓冲区。 Is this correct reasoning, or is it simply the way the API was implemented? 这是正确的推理,还是仅仅是API的实现方式?

No reasoning is given on the official wiki, just says that it's weird: "Binding textures for use in OpenGL is a little weird" https://www.khronos.org/opengl/wiki/Texture 在官方维基上没有任何推理,只是说这很奇怪:“在OpenGL中使用绑定纹理有点奇怪” https://www.khronos.org/opengl/wiki/Texture

Your question can be interpreted in two ways. 您的问题可以通过两种方式解释。

"Why do we bind textures to the context rather than to the shader?" “为什么我们将纹理绑定到上下文而不是着色器?”

Because that would make it needlessly difficult to have multiple shaders use the same textures. 因为这会让多个着色器使用相同的纹理变得不必要。 Note that pretty much no graphics API directly attaches the texture to the program. 请注意,几乎没有图形API直接将纹理附加到程序。 Not D3D of any version, not Metal, nor even Vulkan. 不是任何版本的D3D,不是Metal,甚至也不是Vulkan。

Textures are resources used by shaders. 纹理是着色器使用的资源。 But they are not part of the shader. 但它们不是着色器的一部分。

"Why do we treat textures differently from a general array of values?" “为什么我们将纹理与一般值数组区别对待?”

In modern OpenGL, shaders have access to several different kinds of resources: UBOs, SSBOs, textures, and images. 在现代OpenGL中,着色器可以访问几种不同类型的资源:UBO,SSBO,纹理和图像。 Each of these kinds of resources ultimately represents a potentially distinct part of the graphics hardware. 这些资源中的每一种最终都代表了图形硬件的潜在不同部分。

A storage block is not just a uniform block that can be bigger. 存储块不仅仅是一个可以更大的统一块。 Storage buffers represent the shader doing global memory accesses, while uniform blocks are often copied directly into the shader execution units. 存储缓冲区表示着色器执行全局内存访问,而统一块通常直接复制到着色器执行单元中。 In the latter case, accessing their data is much faster, but that also means that you're limited by how much storage those execution units can have. 在后一种情况下,访问它们的数据要快得多,但这也意味着您受这些执行单元可以拥有的存储量的限制。

Now, this is not true for all hardware (AMD's GCN hardware treats the two almost identically, which is why their UBO limits are so large ). 现在,并非所有硬件都是如此(AMD的GCN硬件几乎完全相同,这就是为什么他们的UBO限制如此之大 )。 But it is true of much hardware. 但是很多硬件也是如此。

Textures are even more complicated, because implementations need to be able to store their data in an optimal way for performance reasons. 纹理甚至更复杂,因为实现需要能够出于性能原因以最佳方式存储其数据。 As such, texture storage formats are opaque. 因此,纹理存储格式是不透明的。 They're even opaque in ostensibly low-level APIs like Vulkan. 它们甚至在表面上像Vulkan这样的低级API中是不透明的。 Oh sure, linear formats exist, but implementations aren't required to let you read from them at all. 哦,当然,存在线性格式,但是根本不需要实现来让它们从中读取。

So textures are not just constant arrays. 因此纹理不仅仅是常量数组。

You are comparing two completely different things 你正在比较两个完全不同的东西

A texture object can (somehow) be compared to a buffer object. 纹理对象可以(某种方式)与缓冲对象进行比较。 The texture is bound by a combination of glActiveTexture and glBindTexture to a texture unit, whereas a buffer is bound by glBindBuffer which is kind-off similar. 纹理由glActiveTextureglBindTexture的组合绑定到纹理单元,而缓冲区由glBindBuffer绑定,它类似于类似的。

A texture sampler uniform is a uniform in the shader and should thus be compared with other uniforms. 纹理采样器均匀在着色器中是均匀的,因此应与其他制服进行比较。 This sampler is set by a glUniform1i call. 该取样由一组glUniform1i电话。

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

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