GLSL – Using custom output attribute instead of gl_Position

This was mostly covered by the comments, but in the interest of having an answer in place, let me summarize which pre-defined GLSL variables are gone in the core profile, and which are still there.

In most cases, pre-defined variables were removed from core profile shaders as a direct consequence of large parts of the fixed function pipeline being removed from the API. There were a lot of uniform variables that reflected state that simply does not exist anymore. Typical examples include:

  • Fixed function transformation state (gl_ModelViewMatrix, gl_ProjectionMatrix, etc.).
  • Lighting parameters (gl_Light*).
  • Material parameters (gl_Material*).
  • Clip planes (gl_Clip*).

Similarly, vertex shader inputs that received fixed function vertex attributes are gone because only generic attributes are supported in the core profile. These include gl_Vertex, gl_Normal, gl_Color, etc.

There also used to be some pre-defined varying variables like gl_FrontColor, gl_BackColor, and gl_TexCoord that are all gone, and can easily be replaced by defining your own out/in variables.

Also gone are the pre-defined fragment shader outputs gl_FragColor and gl_FragData. Those are an interesting case since the reason is not feature deprecation. My understanding is that they were deprecated because they were not flexible enough to accommodate current functionality. Since the type of gl_FragColor was vec4, the output of the fragment shader would have to be vectors with float components. This does not work well if your render target has a different type, e.g. if you are rendering to an integer texture.

So what pre-defined variables are still left? While much less than before, there are still a few, which all relate to fixed function that is still in place in the programmable pipeline. Examples include:

  • The vertex coordinate interface between vertex shader and fragment shader, which primarily consists of the gl_Position output in the vertex shader and the gl_FragCoord input in the fragment shader. There are still a number of fixed function blocks sitting between vertex and fragment shaders, like clipping and rasterization. Those fixed function blocks operate on vertex coordinates, so the pre-defined variables still make sense.
  • Some variables related to instancing (gl_VertexID, gl_InstanceID).
  • gl_ClipDistance to support clipping with arbitrary planes, which is also still present in fixed function form.
  • gl_FrontFacing. The winding order of triangles is evaluated by fixed function, and made available to the fragment shader.

None of these lists are close to exhaustive, but I hope it’s a useful overview, and provides some background. As always, the spec documents provide the final and full answer.

Leave a Comment