GLSL float16_t Scalar Variable Declaration
This article provides a concise guide on declaring 16-bit
floating-point (float16_t) scalar variables in GLSL using
the GL_EXT_shader_explicit_arithmetic_types_float16
extension. You will learn the required preprocessor directives,
declaration syntax, and literal suffixes needed to work with explicit
half-precision arithmetic in your shaders.
Enabling the Extension
Before declaring float16_t variables, you must enable
the extension in your shader. Place the following directive at the top
of your GLSL code, immediately following the #version
declaration:
#version 450
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : requireUsing require ensures that shader compilation fails
immediately if the target GPU or driver does not support 16-bit
floating-point operations.
Variable Declaration Syntax
Under the
GL_EXT_shader_explicit_arithmetic_types_float16 extension,
the scalar type identifier is float16_t.
Basic Declaration
You declare an uninitialized or default scalar variable by placing
the float16_t type specifier before the variable name:
float16_t myValue;Initialized Declaration with Literals
To initialize a float16_t scalar variable, you can
assign a floating-point literal using the hf or
HF suffix, which specifies a 16-bit half-precision float
literal:
float16_t factor = 1.5hf;
float16_t offset = -0.25HF;Type Construction and Casting
You can also use the float16_t constructor to explicitly
convert standard 32-bit floats (float), integers, or other
types to a 16-bit float:
float standardFloat = 3.14159;
float16_t convertedValue = float16_t(standardFloat);
float16_t fromInt = float16_t(42);Complete Example
#version 450
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
void main()
{
// Uninitialized declaration
float16_t a;
// Declaration with literal initialization
float16_t b = 2.0hf;
// Declaration using constructor cast
float16_t c = float16_t(1.0);
// Arithmetic operation
a = b + c;
}