Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 520
hyperknot Avatar asked Aug 04 '12 14:08

hyperknot


People also ask

What are the inputs and outputs of a fragment shader?

The output of a fragment shader is a depth value, a possible stencil value (unmodified by the fragment shader), and zero or more color values to be potentially written to the buffers in the current framebuffers. Fragment shaders take a single fragment as input and produce a single fragment as output.

Can uniform variables be declared in a fragment shader?

Fragment shader reads a value from the same variable, automatically interpolated to that fragment. No need to declare these in the Java program.

How many times is the fragment shader called?

Unless you have early depth testing enabled, a fragment is only discarded after you run the fragment shader. In this case, the frag shader would run once for every fragment in both quads, so 20000 times.

What is a uniform variable in a shader?

A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.


1 Answers

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.

like image 122
Oskar Avatar answered Oct 21 '22 03:10

Oskar