Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does gl_FragColor do anything that gl_FragData does not?

Tags:

opengl

glsl

As it says on the tin: Is there any reason, ever, to use gl_FragColor instead of gl_FragData[0]? If not, then why does gl_FragColor even exist? Is it mere legacy from a time when gl_FragData did not exist?

(Yes, I know that both are deprecated in the latest GLSL versions, but I still want to write code that can run on older cards.)

like image 944
Dolda2000 Avatar asked Sep 29 '13 06:09

Dolda2000


1 Answers

I will refer you to the OpenGL specification for GLSL 1.1, as it makes very little distinction between the two except to say that they are mutually exclusive.

The OpenGL Shading Language (version 1.1) - 7.2 Fragment Shader Special Variables - pp. 43

If a shader statically assigns a value to gl_FragColor, it may not assign a value to any element of gl_FragData. If a shader statically writes a value to any element of gl_FragData, it may not assign a value to gl_FragColor. That is, a shader may assign values to either gl_FragColor or gl_FragData, but not both.

Given this language, gl_FragColor should probably be preferred in shaders that do not use MRT (multiple render targets). For shaders that output to multiple buffers, use gl_FragData [n]. But never mix-and-match, even though you might logically assume that gl_FragColor is an alias to gl_FragData [0].

The GLSL specification pre-dates FBOs, so having an array of locations to output fragment data did not always make sense. Since GLSL and FBOs are both core in OpenGL 3.0, it is easy to take this for granted. The old ARB extension specification for fragment shaders has a blurb on this very subject:

14) What is the interaction with a possible MRT (Multiple Render Target) extension?

The OpenGL Shading Language defines the array gl_FragData[] to output
values to multiple buffers. There are two situations to consider.

  1) There is no MRT extension support. A shader can statically assign a
     value to either gl_FragColor or gl_FragData[0] (but not both).
     Either way the same buffer will be targeted.
  2) There is MRT support. In this case what happens is defined in the
     relevant MRT extension documentation.

They were thinking ahead, but did not quite have all the pieces in place yet. This is ancient history in any case.

like image 139
Andon M. Coleman Avatar answered Oct 27 '22 00:10

Andon M. Coleman