Why is it that I assign outputs to color numbers via glFragDataLocation first and then define a mapping to the drawbuffers anyway?
Many of the annoying things in OpenGL can be summed up by the following two phrases:
1: Maximize backwards compatibility.
2: It sounded like a good idea at the time.
First, there came ARB_draw_buffers. This came around 2004, so FBOs didn’t exist yet and even GLSL was only 1.10. The idea was to simply allow shaders to render to multiple buffers in the default framebuffer, like the GL_AUXi buffers and such.
It gave GLSL fragment shaders new outputs: gl_FragData, which was an array of some number of possible outputs. Since this was an array, you still needed some way to say where each array entry went. And thus, you needed glDrawBuffers: a mapping from array index to an actual buffer name.
EXT_framebuffer_objects gave us FBOs, but really that didn’t affect this; all it gave us was a different set of values for glDrawBuffers.
The next major change came with EXT_texture_integer combined with EXT_fbo. Now, it became desirable to write to integer textures. gl_FragData is ultimately defined as follows:
out vec4 gl_FragData[#];
That’s a real problem, because a
vec4 is a floating-point vector. And GL 3.0 was introducing integer textures and render targets. You needed a way to write to an integer output. Which now means that you need to be able to define an integer output.
Enter EXT_gpu_shader4, which is where we see the first appearance of glBindFragDataLocation (in EXT form, of course). Thanks to point 1, the ARB wasn’t willing to fundamentally rewrite how buffer draw mapping worked. So they took the route of least resistence.
Each fragment shader output in the gl_FragData days had an index. Therefore, in the post-gl_FragData days, it will have an index again. Except now, it’s arbitrarily defined by the user. It causes the least change to the API. Shaders can define these outputs, and outside of some glBindFragDataLocation work, no other part of the user’s code would have to change to make everything work under the new system. Everything would work as it did before.
And 5 years later, we would look back and go, “this seems silly.”