When should I use STD140 in OpenGL?

[亡魂溺海] 提交于 2021-02-17 15:46:48

问题


When do I use the STD140 for uniform blocks in OpenGL?

Although I am not a 100% sure, I believe there is an alternative to it which can achieve the same thing, called "Shared".

Is it just preference for the coder? Or are there reasons to use one over the other?


回答1:


Uniform buffer objects are described in http://www.opengl.org/registry/specs/ARB/uniform_buffer_object.txt

The data storage for a uniform block can be declared to use one of three layouts in memory: packed, shared, or std140.

  1. packed uniform blocks have an implementation-dependent data layout for efficiency, and unused uniforms may be eliminated by the compiler to save space.

  2. shared uniform blocks, the default layout, have an implementation-dependent data layout for efficiency, but the layout will be uniquely determined by the structure of the block, allowing data storage to be shared across programs.

  3. std140 uniform blocks have a standard cross-platform cross-vendor layout. Unused uniforms will not be eliminated.

The std140 uniform block layout, which guarantees specific packing behavior and does not require the application to query for offsets and strides. In this case, the minimum size may still be queried, even though it is determined in advance based only on the uniform block declaration. The offset of each uniform in a uniform block can be derived from the definition of the uniform block by applying the set of rules described in the OpenGL specification.




回答2:


std140 is most useful when you have a uniform block that you update all at once, for example a collection of matrix and lighting values for rendering a scene. Declare the block with std140 in your shader(s), and you can replicate the memory layout in C with a struct. Instead of having to query and save the offsets for every individual value within the block from C, you can just glBufferData(GL_UNIFORM_BUFFER, sizeof(my_struct), &my_struct, with one call.

You do need to be a little careful with alignment in C, for instance, a vec3 will take up 4 floats, not 3, but it is still much easier IMHO.



来源:https://stackoverflow.com/questions/16270846/when-should-i-use-std140-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!