GLSL Double-Precision Vector Swizzling Restrictions

Double-precision vector swizzling in core OpenGL Shading Language (GLSL) allows developers to access and reorder components of 64-bit vectors (dvec2, dvec3, and dvec4). While core GLSL applies the same overarching syntactic rules to double-precision vectors as it does to standard 32-bit floating-point vectors, strict language and hardware-level constraints govern how these operations are written and executed. Understanding these limitations—ranging from component bounds and write masks to register limits—ensures valid and efficient shader code.

Component Domain and Dimension Limits

Double-precision vectors can only be swizzled using components that exist within the source vector’s declared dimension. Attempting to select a component beyond the vector size results in a compile-time error:

  • A dvec2 only provides access to components x, y (or r, g / s, t). Accessing z or w is illegal.
  • A dvec3 provides access to x, y, z. Accessing w is illegal.
  • A dvec4 provides access to all four components (x, y, z, w).

Furthermore, swizzling expressions cannot produce types larger than a 4-component vector. A swizzle operator cannot yield more than four components (for example, my_dvec2.xyxyx is invalid because there is no dvec5 type in GLSL).

Selector Namespace Consistency

GLSL defines three distinct selector groups for vector components:

  • Spatial: x, y, z, w
  • Color: r, g, b, a
  • Texture: s, t, p, q

When swizzling a double-precision vector, you cannot mix selectors from different families within the same swizzle operation. An expression such as my_dvec3.xg or my_dvec4.xyzr is invalid and will fail compilation.

L-Value (Write Mask) Restrictions

When a dvec swizzle is used as an l-value (the target on the left-hand side of an assignment), two major restrictions apply:

  1. No Duplicate Components: A component cannot appear more than once in an l-value swizzle. For instance, my_dvec3.xx = dvec2(1.0lf, 2.0lf); is forbidden because writing to the same memory location twice within a single assignment is undefined.
  2. Component Cardinality Match: The number of components selected in the l-value swizzle must match the component count of the r-value expression on the right-hand side.

Scalar Double Limitations

Core GLSL does not support swizzling on scalar types. While some shading languages allow pseudo-swizzling on scalars, GLSL prohibits swizzling a plain double. An expression like my_double.xxxx to construct a dvec4 will not compile; explicit constructor syntax such as dvec4(my_double) must be used instead.

Parity with Single Precision and Performance Impacts

Syntactically, core GLSL imposes no unique structural restrictions on dvec swizzles that do not also apply to vec swizzles. However, double-precision components are 64 bits wide rather than 32 bits. A dvec4 requires 256 bits of storage—double that of a vec4. Consequently, complex swizzling patterns on double-precision vectors often require more internal temporary registers and multi-instruction data shuffling on GPUs lacking native full-width 64-bit datapaths.