OpenGL Fragment Shader Reading Color Data

Started by
4 comments, last by vstrakh 4 years, 5 months ago

I am having difficulties trying to understand how a fragment shader (GLSL) works in OpenGL. To be more specific, how does OpenGL reads the color/texture data?

In the vertex Shader (GLSL) there is a pre-defined variable that sets the vertex data called gl_Position.

So basically I am asking, what is the variable/method that sends color/texture data to the OpenGL program in the fragment shader or do i just type

out vec4 fragColor;

does that work? (above code)

Advertisement

Yes, your assumption is correct. The vertex and fragment shaders are communicating through variables declared as 'in' and 'out'.  It was 'varying' in older GLSL (deprecated as of GLSL 1.30) and it's still there in  OpenGL ES 2.0, since the GLSL version is limited to 1.10 there.

The vertex shader does its computations, and writes the data into 'out'/'varying' variable, and fragment shader picks up that data from the 'in' / 'varying' variable of the same name and type.

As for the outputs, earlier the fragment shader would write its output into predefined gl_FragColor or gl_FragData[buffer] variables. That's deprecated now, and you have to explicitly specify the layout/location of output variable, and bind that variable to a fragment shader color number.

On 11/18/2019 at 4:58 AM, vstrakh said:

Yes, your assumption is correct. The vertex and fragment shaders are communicating through variables declared as 'in' and 'out'.  It was 'varying' in older GLSL (deprecated as of GLSL 1.30) and it's still there in  OpenGL ES 2.0, since the GLSL version is limited to 1.10 there.

The vertex shader does its computations, and writes the data into 'out'/'varying' variable, and fragment shader picks up that data from the 'in' / 'varying' variable of the same name and type.

As for the outputs, earlier the fragment shader would write its output into predefined gl_FragColor or gl_FragData[buffer] variables. That's deprecated now, and you have to explicitly specify the layout/location of output variable, and bind that variable to a fragment shader color number.

oh I understand a bit now. Why did they get rid of the predefined variable gl_FragColor?

If i am not mistaken gl_FragColor is fixed function pipeline functionality and is available in "compatibility profile" only.

Often times one wants the fragment shader to do more than just output a color, so one would define ones own output variables and the locations or data structures where they should be written to.

9 minutes ago, Green_Baron said:

If i am not mistaken gl_FragColor is fixed function pipeline functionality

Well, since it's in GL Shading Language, it's definitely not a fixed function pipeline stuff :)

But it's from the ages when there were only two shader stages.

When new shader types came into existence, they had to clean it up, unifying/generalizing stuff. It's easier to extend the rendering pipeline later, when shader does not cling to some fixed predefined identifiers.

This topic is closed to new replies.

Advertisement