简体   繁体   中英

Names of `out` variables in a fragment shader

I'm having some problem understanding one line in the most basic (flat) shader example while reading OpenGL SuperBible.

In chapter 6, Listing 6.4 and 6.5 it introduces the following two very basic shaders.

6.4 Vertex Shader :

// Flat Shader
// Vertex Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 130

// Transformation Matrix
uniform mat4    mvpMatrix;

// Incoming per vertex
in vec4 vVertex;

void main(void) 
    { 
    // This is pretty much it, transform the geometry
    gl_Position = mvpMatrix * vVertex;
    }

6.5 Fragment Shader :

// Flat Shader
// Fragment Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 130

// Make geometry solid
uniform vec4 vColorValue;

// Output fragment color
out vec4 vFragColor;


void main(void)
   { 
   gl_FragColor = vColorValue;
   }

My confusion is that it says vFragColor in the out declaration while saying gl_FragColor in main().

On the other hand, in code from the website, it has been corrected to 'vFragColor = vColorValue;' in the main loop.

What my question is that other then being a typo in the book, what is the rule for naming out values of shaders? Do they have to follow specific names?

On OpenGL.org I've found that gl_Position is required for the out of the vertex shader. Is there any such thing for the fragment shader? Or it is just that if there is only one output, then it will be the color in the buffer?

What happens when there is more then one out of a fragment shader? How does the GLSL compiler know which one to use in the buffer?

As stated in the GLSL specification for version 1.3 , the use of gl_FragColor in the fragment shader is deprecated. Instead, you should use a user defined output variable like the vFragColor variable described in your fragment shader. As you said, it's a typo.

What is the rule for naming out values of shaders?

The variable name can be anything you like, unless it collides with any existing names.

What happens when there is more then one out of a fragment shader? How does the GLSL compiler know which one to use in the buffer?

When there is more than one out in the fragment shader, you should assign slots to the fragment shader outputs by calling BindFragDataLocation . You can then say which slots will render to which render target by calling DrawBuffers .

The specification states that if you have one output variable in the fragment shader defined, it will be assigned to index 0 and output 0. For more information, I recommend you take a look at it yourself.

gl_FragColor was the original output variable in early versions of GLSL. This was the color of the fragment that was to be drawn.

Your initial confusion is justified, as there's no reason to declare that out variable and then write to glFragColor.

In later versions it became customizable, such that you could give arbitrary names to your output variables. You can map these arbitrary outputs to specific buffers with the command glBindFragDataLocation .

I'm not 100% positive, but I believe if you don't call this function before linking, then your output variables will be randomly assigned to buffers. If you only have one output, then it should always be assigned to buffer 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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