<p>It works almost exactly the same way as vertex shader inputs.</p>

<p>In vertex shaders, you have inputs. And these inputs represent vertex attribute indices, the numbers you pass to <code>glVertexAttribPointer</code> and <code>glEnableVertexAttribArray</code> and so forth. You set up which input pulls from which attribute. In GLSL 3.30, you use this syntax:</p>

<pre><code>layout(location = 2) in color;
</code></pre>

<p>This sets the <code>color</code> 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 <a href="http://www.opengl.org/wiki/GLAPI/glBindAttribLocation" rel="noreferrer"><code>glBindAttrbLocation</code></a> before linking or query the program for the attribute index with <a href="http://www.opengl.org/wiki/GLAPI/glGetAttribLocation" rel="noreferrer"><code>glGetAttribLocation</code></a>. If you don't explicitly provide an attribute location, GLSL will assign a location <em>arbitrarily</em> (ie: in an implementation-defined manner).</p>

<p>Setting it in the shader is almost always the better option.</p>

<p>In any case, fragment shader outputs work almost exactly the same way. Fragment shaders can write to <a href="https://www.khronos.org/opengl/wiki/Fragment#Fragment_shader_outputs" rel="noreferrer">multiple output colors</a>, which themselves get mapped to <a href="https://www.khronos.org/opengl/wiki/Framebuffer_Draw_Buffer" rel="noreferrer">multiple buffers in the framebuffer</a>. Therefore, you need to indicate which output goes to which fragment output color.</p>

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

<pre><code>layout(location = 1) out secColor;
</code></pre>

<p>There are also the API functions <a href="http://www.opengl.org/wiki/GLAPI/glBindFragDataLocation" rel="noreferrer"><code>glBindFragDataLocation</code></a> and <a href="http://www.opengl.org/wiki/GLAPI/glGetFragDataLocation" rel="noreferrer"><code>glGetFragDataLocation</code></a>, which are analogous to <code>glBindAttribLocation</code> and <code>glGetAttribLocation</code>.</p>

<p>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 <em>require</em> this behavior, so you should not depend on it either.</p>

CC BY-SA 3.0
2017-01-22T03:05:38.667Z
2017-01-22T03:12:36.043Z
2017-01-22T03:12:36.043Z
Nicol Bolas
<p><strong>EDIT:</strong> This is a duplicate of a question from stack overflow:
<a href="https://stackoverflow.com/questions/9222217/how-does-the-fragment-shader-know-what-variable-to-use-for-the-color-of-a-pixel">https://stackoverflow.com/questions/9222217/how-does-the-fragment-shader-know-what-variable-to-use-for-the-color-of-a-pixel</a></p>

<p>If you want to send a value from vertex shader to fragment shader, the vertex shader should declare one variable as <code>out</code>, the fragment shader should declare other variable as <code>in</code> and both variables must have their names and types equal. But this doesn't happen when you want to send the color value from the fragment shader. In fact, you can call the output variable as you want. Why? In older version there was one variable called <code>gl_FragColor</code><br />
How the program links your output fragment variable with gl_FragColor?</p>

CC BY-SA 3.0
2017-01-22T14:26:03.410Z
[Discussion on meta about cross site duplicates and whether they are a problem](http://meta.computergraphics.stackexchange.com/questions/250/how-do-we-deal-with-duplicates-of-so-questions)
trichoplax is on Codidact now
CC BY-SA 3.0
2017-01-22T17:39:32.763Z
If you have additional questions, please post them as new questions rather than editing them into an existing one. This way the answers to separate questions can be compared independently. You can still include links between the questions if they are related.
trichoplax is on Codidact now
<opengl><shader><fragment-shader>
Fragment shader's output variables
Adrian
# Fragment shader's output variables

(Asked by: username_0 on 2017-01-22)

**EDIT:** This is a duplicate of a question from stack overflow:
<https://stackoverflow.com/questions/9222217/how-does-the-fragment-shader-know-what-variable-to-use-for-the-color-of-a-pixel>

If you want to send a value from vertex shader to fragment shader, the vertex shader should declare one variable as `out`, the fragment shader should declare other variable as `in` and both variables must have their names and types equal. But this doesn't happen when you want to send the color value from the fragment shader. In fact, you can call the output variable as you want. Why? In older version there was one variable called `gl_FragColor`  
How the program links your output fragment variable with gl\_FragColor?

**Tags:** opengl, shader, fragment-shader

> **Comments:**
> **username_1**: [Discussion on meta about cross site duplicates and whether they are a problem](http://meta.computergraphics.stackexchange.com/questions/250/how-do-we-deal-with-duplicates-of-so-questions)  
> **username_1**: If you have additional questions, please post them as new questions rather than editing them into an existing one. This way the answers to separate questions can be compared independently. You can still include links between the questions if they are related.  

---

## 1 answers

### Answer by username_2 on 2017-01-22. Score: 7 | Accepted answer

It works almost exactly the same way as vertex shader inputs.

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`](http://www.opengl.org/wiki/GLAPI/glBindAttribLocation) before linking or query the program for the attribute index with [`glGetAttribLocation`](http://www.opengl.org/wiki/GLAPI/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](https://www.khronos.org/opengl/wiki/Fragment#Fragment_shader_outputs), which themselves get mapped to [multiple buffers in the framebuffer](https://www.khronos.org/opengl/wiki/Framebuffer_Draw_Buffer). 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`](http://www.opengl.org/wiki/GLAPI/glBindFragDataLocation) and [`glGetFragDataLocation`](http://www.opengl.org/wiki/GLAPI/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.


---

