packSnorm2x16
Converts a 2-component floating-point vector in the range [-1, 1] (vec2) into a single unsigned integer (uint). Each component is encoded as a 16-bit signed normalized (snorm) integer and packed into a 32-bit value, allowing you to store directional or vector data in a compact format on the GPU.
Core Advantages
Implements the same encoding as the GPU's built-in packSnorm2x16, packing two float components into one uint. This roughly halves memory and bandwidth usage compared to storing two 32-bit floats, while keeping a well-defined, cross-backend mapping that can be safely decoded in render or compute passes. It is ideal for tightly packed G-buffer layouts, vertex attributes, or storage buffers.
Common Uses
Packing the X and Y components of a tangent-space normal into a single uint for normal maps or deferred-rendering G-buffers.
Compressing 2D velocity, offset, or direction data in the range [-1, 1] before writing it into a storage buffer or data texture to reduce memory usage.
Designing custom compact data layouts and bit-packed formats together with uint / uvec2, for advanced GPGPU algorithms and custom buffers.
How to adjust
The node itself has no tunable parameters; its behavior is entirely determined by the input vec2. In practice you should ensure both components are already normalized to [-1, 1]; values outside this range will be clamped, so it is common to call normalize or clamp before packing. Each component is quantized to a 16-bit signed integer, so details are discretized with a step of about 1/32767 and may show visible quantization near -1 and 1. If you need more dynamic range or precision, consider a different encoding (such as float16 or an unorm packing scheme), but make sure the decoding stage uses the matching format to avoid artifacts.
Code Examples
1// Pack the X,Y components of a view-space normal (range [-1, 1]) into a single uint
2const n = normalize( normalView );
3const packedNormalXY = packSnorm2x16( n.xy );
4
5// For example, write the packed value into a uint storage buffer
6normalBuffer.element( index ).assign( packedNormalXY );