GLSL Octal and Hexadecimal Integer Literals
The OpenGL Shading Language (GLSL) provides strict lexical rules for defining non-decimal integer literals. Octal and hexadecimal literals allow developers to specify bitmasks, color encodings, and raw numerical values directly in shader code. This guide outlines the lexical structure, prefix requirements, permitted character sets, and type suffix rules governing octal and hexadecimal integers across standard GLSL versions.
Hexadecimal Integer Literals
A hexadecimal integer literal in GLSL represents a base-16 numerical value.
- Prefix: It must begin with either
0xor0X. - Digit Set: The prefix must be followed by one or
more hexadecimal digits:
0through9, andathroughf(orAthroughF). The letter casing is not case-sensitive. - Examples:
0x1A(Decimal 26)0XFF(Decimal 255)0x7fffffff(Decimal 2147483647)
An empty hexadecimal sequence (e.g., 0x followed
immediately by a non-hexadecimal character or semicolon) constitutes a
compile-time lexical error.
Octal Integer Literals
An octal integer literal represents a base-8 numerical value.
- Prefix: It must begin with a leading zero
(
0). - Digit Set: The leading zero must be followed by one
or more octal digits:
0through7. - Disallowed Digits: The digits
8and9are not valid in an octal literal. Any token starting with0that contains an8or9without a precedingxorXwill produce a compilation error. - Single Zero: The literal
0on its own is lexically treated as an octal zero, though its numerical value is identical to decimal zero. - Examples:
010(Decimal 8)077(Decimal 63)052(Decimal 42)
Type Suffixes and Signedness
By default, an integer literal without a suffix is treated as a
signed 32-bit integer (int). GLSL allows explicitly
defining an unsigned 32-bit integer (uint) using a
suffix:
- Suffix: Append
uorUto the end of the literal. - Hexadecimal Unsigned:
0xFFuor0x7fffffffU - Octal Unsigned:
077uor0123U
Whitespace cannot appear between the numerical digits and the type suffix.
Range and Bit Width Constraints
GLSL integers are defined as 32-bit values. Lexical values must conform to this representation:
- Signed Integers (
int): Values range from-2147483648to2147483647. A hexadecimal or octal literal exceeding0x7FFFFFFF(without ausuffix) may produce an overflow warning or error depending on the target GLSL profile, as it cannot be held in a standard signed 32-bit integer without reinterpretation. - Unsigned Integers (
uint): Values range from0uto4294967295u(0xFFFFFFFFu). - Overflow: If a literal token defines a numeric value that exceeds the capacity of a 32-bit integer, the GLSL compiler generates a compile-time error.