Allowed Characters for GLSL Identifier Names

In the OpenGL Shading Language (GLSL), identifier names for variables, functions, structures, and type names are strictly governed by the official specification to ensure cross-platform compiler compatibility. Identifiers are restricted to a standard subset of ASCII characters, including letters, digits, and underscores. Understanding these character rules, along with naming restrictions such as reserved prefixes and forbidden starting characters, is critical for writing valid GLSL shader programs.

The Permitted Character Set

GLSL identifiers must be constructed exclusively from the following ASCII character set:

  • Uppercase English letters: A through Z
  • Lowercase English letters: a through z
  • Decimal digits: 0 through 9
  • Underscore: _

No other characters, such as hyphens, spaces, special symbols ($, @, #), or non-ASCII Unicode characters, are permitted.

Structural Rules for Identifiers

To form a valid identifier, the permitted characters must follow specific structural constraints:

  • Initial Character: An identifier cannot start with a digit (0–9). It must begin with either an uppercase letter, a lowercase letter, or an underscore.
  • Case Sensitivity: GLSL is strictly case-sensitive. The identifiers colorValue, ColorValue, and COLORVALUE represent three distinct identifiers.

Reserved Prefixes and Sequences

Even if an identifier uses the permitted characters, certain patterns are restricted by the compiler:

  • The gl_ Prefix: Any identifier beginning with gl_ is reserved for use by the OpenGL Shading Language itself (such as gl_Position or gl_FragCoord). User-defined variables cannot begin with gl_.
  • Consecutive Underscores: Identifiers containing two consecutive underscores (__) are reserved by the GLSL implementation and must not be used in user code.

Keywords and Reserved Words

An identifier composed of permitted characters is still illegal if it matches any GLSL reserved keyword (such as attribute, uniform, varying, vec2, float, or control-flow words like for and while) or words reserved for future language extensions.