25

There seems to be a lot of ambiguity about gl_FragColor being deprecated. For example, it is missing in the GLSL 4.40 specification, but it is included in the GLSL 4.60 specification.

What is the safest, most compatible, most supported strategy? Using gl_FragColor or defining a shader output like out vec4 color?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
IntellectualKitty
  • 523
  • 1
  • 6
  • 12

2 Answers2

10

Yes, gl_FragColor is deprecated. You should use the following syntax:

layout(location = 0) out vec4 diffuseColor;

It is included in the GLSL 4.60 spec under the section 7.1.7. Compatibility Profile Built-In Language Variables. That means, if you create a core context this variable will not be available.

dari
  • 2,255
  • 14
  • 21
  • 11
    you didn't specify what "out vec4 color" means, you instead talking about something else called "diffuseColor" which i don't know what it is either – TheNegative Aug 11 '19 at 04:11
  • diffuseColor is literally just a variable name. All OpenGL cares about is that there be a vec4 at output layout location 0 – Moosa Nov 12 '22 at 11:56
7

If you would read the the GLSL 4.40 specification carefully, then you would find gl_FragCoord in chapter "7.1.1 Compatibility Profile Built-In Language Variables", as it is in the GLSL 4.60 specification.

The following fragment output variables are available in a fragment shader when using the compatibility profile:

out vec4 gl_FragColor;
out vec4 gl_FragData[gl_MaxDrawBuffers];

Writing to gl_FragColor specifies the fragment color that will be used by the subsequent fixed functionality pipeline. If subsequent fixed functionality consumes fragment color and an execution of the fragment shader executable does not write a value to gl_FragColor then the fragment color consumed is undefined.

This means you can't use gl_FragColor, in an OpenGL Core profile Context, because it is deprecated, but it would still be available in a compatibility profile.

The modern way of writing to the output buffers from a fragment shader, is to declare user-defined output variables and to use Layout Qualifiers.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174