Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use STD140 in OpenGL?

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?

like image 224
Dan Webster Avatar asked Apr 29 '13 03:04

Dan Webster


People also ask

What is Std140?

Std140 is a standardized memory layout for GLSL shader interface blocks (e.g. uniform blocks). An interface block is a group op typed GLSL variables. For details on the layout rules for std140, please refer to the section 2.12. 6.4 “Standard Uniform Block Layout” of the OpenGL ES 3.0 Specification.

How do you use uniforms in OpenGL?

Uniforms are intended to be set by the user from OpenGL, rather than within the shader. However, you can initialize them to a default value using standard GLSL initalizer syntax: uniform vec3 initialUniform = vec3(1.0, 0.0, 0.0); This will cause the uniform to have this vector as its value, until the user changes it.

What is uniform buffer object?

A Buffer Object that is used to store uniform data for a shader program is called a Uniform Buffer Object. They can be used to share uniforms between different programs, as well as quickly change between sets of uniforms for the same program object.


1 Answers

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.

like image 71
Hugh Fisher Avatar answered Oct 05 '22 21:10

Hugh Fisher