Home glsl - datatype
Post
Cancel

glsl - datatype

wiki

Basic types in GLSL are the most fundamental types. Non-basic types are aggregates of these fundamental types.

Scalars

  • bool: conditional type, values may be either true or false
  • int: a signed, two’s complement, 32-bit integer
  • uint: an unsigned 32-bit integer
  • float: an IEEE-754 single-precision floating point number
  • double: an IEEE-754 double-precision floating-point number

Vectors

Each of the scalar types, including booleans, have 2, 3, and 4-component vector equivalents. The n digit below can be 2, 3, or 4:

  • bvecn: a vector of booleans
  • ivecn: a vector of signed integers
  • uvecn: a vector of unsigned integers
  • vecn: a vector of single-precision floating-point numbers
  • dvecn: a vector of double-precision floating-point numbers

Vector values can have the same math operators applied to them that scalar values do. These all perform the component-wise operations on each component. However, in order for these operators to work on vectors, the two vectors must have the same number of components.

Swizzling

You can access the components of vectors using the following syntax:

1
2
vec4 someVec;
someVec.x + someVec.y;

This is called swizzling. You can use x, y, z, or w, referring to the first, second, third, and fourth components, respectively.

The reason it has that name “swizzling” is because the following syntax is entirely valid:

1
2
3
vec2 someVec;
vec4 otherVec = someVec.xyxx;
vec3 thirdVec = otherVec.zyy;

You can use any combination of up to 4 of the letters to create a vector (of the same basic type) of that length. So otherVec.zyy is a vec3, which is how we can initialize a vec3 value with it. Any combination of up to 4 letters is acceptable, so long as the source vector actually has those components. Attempting to access the ‘w’ component of a vec3 for example is a compile-time error.

Swizzling also works on l-values:

1
2
3
vec4 someVec;
someVec.wzyx = vec4(1.0, 2.0, 3.0, 4.0); // Reverses the order.
someVec.zx = vec2(3.0, 5.0); // Sets the 3rd component of someVec to 3.0 and the 1st component to 5.0

However, when you use a swizzle as a way of setting component values, you cannot use the same swizzle component twice. So someVec.xx = vec2(4.0, 4.0); is not allowed. Additionally, there are 3 sets of swizzle masks.

You can use xyzw, rgba (for colors), or stpq (for texture coordinates). These three sets have no actual difference; they’re just syntactic sugar.

You cannot combine names from different sets in a single swizzle operation. So “.xrs” is not a valid swizzle mask.

In OpenGL 4.2 or ARB_shading_language_420pack, scalars can be swizzled as well. They obviously only have one source component, but it is legal to do this:

1
2
float aFloat;
vec4 someVec = aFloat.xxxx;

Matrices

In addition to vectors, there are also matrix types. All matrix types are floating-point, either single-precision or double-precision. Matrix types are as follows, where n and m can be the numbers 2, 3, or 4:

  • matnxm: A matrix with n columns and m rows. OpenGL uses column-major matrices, which is standard for mathematics users. Example: mat3x4.
  • matn: A matrix with n columns and n rows. Shorthand for matnxn

Double-precision matrices (GL 4.0 and above) can be declared with a dmat instead of mat

Swizzling does not work with matrices. You can instead access a matrix’s fields with array syntax:

1
2
3
mat3 theMatrix;
theMatrix[1] = vec3(3.0, 3.0, 3.0); // Sets the second column to all 3.0s
theMatrix[2][0] = 16.0; // Sets the first entry of the third column to 16.0.

However, the result of the first array accessor is a vector, so you can swizzle that:

1
2
mat3 theMatrix;
theMatrix[1].yzx = vec3(3.0, 1.0, 2.0);

Opaque types

Opaque types represent some external object which the shader references in some fashion. Opaque variables do not have “values” in the same way as regular types; they are markers that reference the real data. As such, they can only be used as parameters to functions. These functions return/modify the actual referenced data.

Variables of opaque types can only be declared in one of two ways. They can be declared at global scope, as a uniform variables. Such variables can be arrays of the opaque type. They can be declared as members of a struct, but if so, then the struct can only be used to declare a uniform variable (or to declare a member of a struct/array that itself a uniform variable). They cannot be part of a buffer-backed interface block or an input/output variable, either directly or indirectly.

Opaque type variables can also be declared as in-qualified function parameters. This allows you to pass opaque types to user-defined functions.

Opaque types cannot be l-values. Non-array opaque types can only be passed to a function that takes this type as a parameter; they cannot be used as any other part of an expression. Opaque types that are arrayed can use array-index and structure field selection (for .length()).

Samplers

Texture access is not as simple as reading a value from a memory address. Filtering and other processes are applied to textures, and how texture coordinates are interpreted can be part of the texture access operation. For these reason, texture access is somewhat complicated.

The sampler type is an opaque GLSL type that represents a texture bound to the OpenGL context. There are many sampler types, one for each type of texture (2D, 2D_ARRAY, etc). Samplers can only access textures of the proper type.

Images

Image variables refer to an image, of a particular type, stored within a texture. These are used for arbitrary loading/storing of values within shaders.

Atomic counters

Atomic variables represent a memory location within a Buffer Object upon which atomic operations can take place.

Implicit conversion

Certain values can be implicitly converted to certain types. This means that an explicit construction operation is not necessary.

Signed integers can be implicitly converted to unsigned integers, but the reverse is not true. Either integer type can be converted into floats, and integers and floats can be converted into doubles.

Vector and matrix values are implicitly converted if the basic type they contain is implicitly convertible.

Arrays

Basic types can be grouped into sequences of those elements, called arrays. This generally works like in C/C++, but there are some limitations. First and foremost is that arrays cannot be multidimensional (unless OpenGL 4.3 or ARB_arrays_of_arrays is available, as shown below).

Arrays usually must be declared with a size which must be initialized with a Constant Expression. Input arrays to Geometry, Tessellation Control, and Tessellation Evaluation Shaders do not need a size, nor do output arrays for non-patch outputs for Tessellation Control Shaders. Global variables can be declared without a size, but only if they are later redeclared with a size. An array in a shader storage interface block may be declared without a size, but only if it is the last variable in the block.

In all cases, the array size must be a compile-time integral Constant Expression.

With a few exceptions, arrays can be used for variables of all Storage Qualifier types: inputs, outputs, uniforms, constants, etc. Outputs from the Fragment Shader cannot be arrayed at all. The length of an array variable can be computed with the .length() function. For example:

1
2
3
4
5
uniform float myValues[12];

...

myValues.length();    // Returns 12.

Invoking the length() function results in a Constant Expression, except when the variable is an unsized array that is the last member of a storage block.

Arrays can be accessed with arbitrary numeric expressions. They do not have to be compile-time constants (though there are a few exceptions to this rule; for example, the very next section).

Opaque arrays

When an array indexing expression, including struct field member accesses, results in an opaque types, the standard has special requirements on those array indices. Under GLSL version 3.30, Sampler arrays (the only opaque type 3.30 provides) can be declared, but they can only be accessed by compile-time integral Constant Expressions.So you cannot loop over an array of samplers, no matter what the array initializer, offset and comparison expressions are.

Under GLSL 4.00 and above, array indices leading to an opaque value can be accessed by non-compile-time constants, but these index values must be dynamically uniform. The value of those indices must be the same value, in the same execution order, regardless of any non-uniform parameter values, for all shader invocations in the invocation group.

For example, in 4.00, it is legal to loop over an array of samplers, so long as the loop index is based on constants and uniforms. So this is legal:

1
2
3
4
5
6
7
8
9
10
11
uniform sampler images[10];
uniform int imageCount;

void main()
{
  vec4 accum = vec4(0.0);
  for (int i = 0; i < imageCount; i++)
  {
    accum += texture(images[i], ...);
  }
}

This would add up all of the values in the textures, up to imageCount in size. Note that this is not legal in GLSL 3.30.

Arrays of arrays

Given the presence of this feature, arrays can be declared multidimensionally in GLSL:

1
uniform vec3 multidim[5][2];

multidim is an array of 5 elements, where each element is an array of 2 vec3 elements. So multidim.length() is 5, while multidim[0].length() is 2.

Arrays can also be declared like this:

1
uniform vec3[5][2] multidim;

Structs

Structs are defined much like C++ (note: the C-style typedef struct {} syntax is *not supported). GLSL does not support anonymous structures (ie: structs without a type name), and structs must have at least one member declaration. Structs cannot be defined within another struct, but one struct can use another previously defined struct as a member.

1
2
3
4
5
6
7
struct Light
{
  vec3 eyePosOrDir;
  bool isDirectional;
  vec3 intensity;
  float attenuation;
} variableName;

The above definition not only defines a struct type Light, it also creates a variable of that type called variableName. As in C/C++, the variable name can be omitted.

Structs can contain opaque types, but such structs can only be used in the ways that opaque types can be used (declared as uniforms or function input variables).

Furthermore, if a struct contains an opaque type, arrays of such structs, when used to fetch a member of an opaque type, have to use array indices which conform to the rules on Opaque Arrays.

There are limits on the storage type qualifiers that variables of struct types can be defined with. Specifically, structs cannot be used as input/output variables. They can be used in all other contexts, including global variables, local variables, uniforms, interface block definitions (as long as they aren’t input/output blocks), function parameters and return values, and so forth.

This post is licensed under CC BY 4.0 by the author.