96

I see a lot of different fragment shaders,

#version 130

out vec4 flatColor;

void main(void)
{
    flatColor = vec4(0.0,1.0,0.0,0.5);
}

And they all use a different variable for the "out color" (in this case flatColor). So how does OpenGL know what you're trying to do?

I'm guessing this works because flatColor is the only variable defined as out, but you're allowed to add more out variables aren't you? Or would that just crash?


Actually, as a test, I just ran this:

#version 330

in vec2 TexCoord0;

uniform sampler2D TexSampler;

out vec4 x;
out vec4 y;

void main()
{
    y = texture2D(TexSampler, TexCoord0.xy);
}

It worked fine whether I used x or y.

(answer: the compiler is optimizing out the unused var, so the remaining var is assigned to location 0)


Furthermore, we have a predefined gl_FragColor. What's the difference, and why do people usually insist on using their own variables?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

158

Furthermore, we have a predefined gl_FragColor.

Let's start with this. No, you don't have the predefined gl_FragColor. That was removed from core OpenGL 3.1 and above. Unless you're using compatibility (in which case, your 3.30 shaders should say #version 330 compatibility at the top), you should never use this.

Now, back to user-defined fragment shader outputs. But first, a quick analogy.

Remember how, in vertex shaders, you have inputs? And these inputs represent vertex attribute indices, the numbers you pass to glVertexAttribPointer and glEnableVertexAttribArray and so forth? You set up which input pulls from which attribute. In GLSL 3.30, you use this syntax:

layout(location = 2) in color;

This sets the color vertex shader input to come from attribute location 2. Before 3.30 (or without ARB_explicit_attrib_location), you would have to either set this up explicitly with glBindAttrbLocation before linking or query the program for the attribute index with glGetAttribLocation. If you don't explicitly provide an attribute location, GLSL will assign a location arbitrarily (ie: in an implementation-defined manner).

Setting it in the shader is almost always the better option.

In any case, fragment shader outputs work almost exactly the same way. Fragment shaders can write to multiple output colors, which themselves get mapped to multiple buffers in the framebuffer. Therefore, you need to indicate which output goes to which fragment output color.

This process begins with the fragment output location value. It's set very similarly to vertex shader input locations:

layout(location = 1) out secColor;

There are also the API functions glBindFragDataLocation and glGetFragDataLocation, which are analogous to glBindAttribLocation and glGetAttribLocation.

If you don't do any explicit assignments, implementations usually will assign one of your output variables to location 0. However, the OpenGL standard does not require this behavior, so you should not depend on it either.

Now to be fair, your program should have failed to link when you used two outputs that didn't get different output locations. What probably happened was that your compiler optimized the one you didn't write to out, so it kinda forgot about it when it came time to check for linker errors.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 4
    Wish I could give more than +1. Spectacular answer. I was wondering how fragment output locations worked. :) – kevintodisco Feb 10 '12 at 06:07
  • Ahh.. so they deprecated `gl_FragColor` in favor of giving you more flexibility to choose which buffer to write to? Makes sense. Thanks again! – mpen Feb 10 '12 at 06:08
  • 3
    @Mark: "Deprecation" means "you can still use it, but it may be removed in later versions." "Removed" means *removed*. There is a difference. That which was marked deprecated in GL 3.0 was **removed** in 3.1 (save for a couple of things). – Nicol Bolas Feb 10 '12 at 06:58
  • @NicolBolas: Removed from the spec, maybe, but evidently not from my driver :P Anyway, wrong choice of words. – mpen Feb 10 '12 at 07:17
  • @NicolBolas is this only applicable if we aren't drawing to the screen? i.e. when drawing to an FBO? If we're just drawing to the screen, do we need to specify an output index (or use `glBindFragDataLocation`)? – Mark Ingram Jul 02 '13 at 19:43
  • 3
    @NicolBolas: "GL 3.3 and above (that's a change from prior versions) will assign them all to location 0". I wonder where this is guaranteed by the spec. 3.3 says "When a program is linked, any varying out variables without a binding specified [...] or explicitly set within the shader text will automatically be bound to fragment colors and indices by the GL. All such assignments will use color indices of zero.". The last sentence is not present in 3.2. However, index does not refer to color _number_ but only to the dual source blending index (which was added in 3.3). – derhass Dec 21 '13 at 19:09
  • 2
    Thank you @NicolBolas, great answer! So I haven't quite understood it fully yet, and just want to make it "without question". In our shaders, we always specify `layout (location = 0) out vec4 outColor` when drawing to whatever is bound (most often "the screen" and not an FBO). So as default, if we haven't bound an FBO (or other buffer) using glDrawBuffers, and we specify `location = 0`, it will always draw correctly to the screen? – AzP Jun 15 '16 at 09:01
  • this was a bit confusing. i found out that there should be such "https://www.khronos.org/opengl/wiki/Default_Framebuffer" thing and there was. – TheNegative Aug 12 '19 at 13:36
  • Actually, pretty major nitpick: `gl_FragColor` and `gl_FragData` have only been removed from the core profile in GLSL 4.20, as the section 1.2.1 of [the GLSL 4.20 spec](https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.20.pdf) explicitly says. To my best understanding, this means that they *are* guaranteed to exist in earlier versions of the core GLSL, as long as the forward compatibility mode, which removes all deprecated features (and is unfortunately mandatory in Apple devices to obtain core OpenGL) is not used. – S. Exchange Considered Harmful Jan 02 '21 at 19:52
10

I'd like to specify this for OpenGLES 3.1 which uses GLSL_ES_3.10 link:

§4.4.2

If there is only a single output [in the fragment shader], the location does not need to be specified, in which case it defaults to zero.

Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46