SPIR-V Decoration for GLSL layout(std140)

In GLSL, the layout(std140) qualifier defines a standardized memory layout for uniform blocks. In SPIR-V, there is no single, direct "std140" decoration; instead, the front-end compiler translates the std140 alignment and sizing rules into concrete, explicit memory layout decorations: Offset, ArrayStride, and MatrixStride, placed upon a structure type decorated with the Block decoration.

The Role of the Block Decoration

In SPIR-V, high-level interface blocks targeted for uniform buffers are designated with the Block decoration:

  • OpDecorate %struct_type Block: Tells the graphics pipeline and driver that this structure acts as an interface block (such as a uniform buffer) rather than an ordinary internal shader structure.

While Block flags the structure as a uniform buffer type, it does not intrinsically calculate member offsets or padding. SPIR-V requires all buffer layouts to be explicitly defined ahead of time by the compiler.

Explicit Member Layout Decorations

When you compile GLSL containing layout(std140) to SPIR-V using tools such as glslang, the compiler calculates the precise byte layout required by the std140 specification and outputs the following decorations:

  • OpMemberDecorate %struct_type member_index Offset byte_offset: Specifies the exact starting byte position of each member within the block, applying std140 base alignment rules (such as aligning vec4, arrays, and nested structures to 16-byte boundaries).
  • OpDecorate %array_type ArrayStride stride_in_bytes: Sets the distance in bytes between consecutive array elements. Under std140, array elements are always aligned to a multiple of 16 bytes (the size of a vec4).
  • OpMemberDecorate %struct_type member_index MatrixStride stride_in_bytes: Sets the byte stride between rows or columns of a matrix.
  • OpMemberDecorate %struct_type member_index ColMajor / RowMajor: Dictates the majorness of matrices (GLSL defaults to column-major).

Summary

The GLSL layout(std140) qualifier is represented in SPIR-V by marking the structure type with the Block decoration and explicitly decorating its members with Offset, ArrayStride, and MatrixStride values calculated according to std140 alignment requirements.