pack4x8unorm
On this page

pack4x8unorm

Function

Normalized packing maps a bounded floating-point interval onto integer fields.

Related names in other languages

These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.

  • GLSL: packUnorm4x8

Targets

Compare examples

Compare the components, hexadecimal word, and recovered values. Notice which component occupies the low field.

Example 1pack4x8unorm(vec4(0, 1, 0, 1))
0101
Components in this example
Result
0xff00ff00
Word in binary: 4278255360
Example 2pack4x8unorm(vec4(0, 0, 0, 0))
0000
Components in this example
Result
0x00000000
Word in binary: 0
Example 3pack4x8unorm(vec4(0.25, 0.25, 0.25, 0.25))
0.250.250.250.25
Components in this example
Result
0x40404040
Word in binary: 1077952576

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use it to reduce storage or bandwidth when the reduced precision is acceptable.

Inputs and limits

Packing clamps to the normalized interval and quantizes values. Unpacking approximates the original value; the low field belongs to the first component.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

WGSL
pack4x8unorm(a)
GLSL ES 3.00
(uint(floor(0.5 + clamp(a.x, 0.0, 1.0) * 255.0)) | (uint(floor(0.5 + clamp(a.y, 0.0, 1.0) * 255.0)) << 8) | (uint(floor(0.5 + clamp(a.z, 0.0, 1.0) * 255.0)) << 16) | (uint(floor(0.5 + clamp(a.w, 0.0, 1.0) * 255.0)) << 24))

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Packs a vec4 of values in [0, 1] into the four bytes of a u32, component 0 in the low byte.

Syntax

declare function pack4x8unorm(a0: vec4): u32
declare function pack4x8unorm(e: vec4): u32

Parameters

Return value

u32

Description

Hand-inlined on GLSL ES 3.00, which has no packUnorm4x8 (that is ES 3.10).

Targets

WGSL
pack4x8unorm(a)
GLSL ES 3.00
(uint(floor(0.5 + clamp(a.x, 0.0, 1.0) * 255.0)) | (uint(floor(0.5 + clamp(a.y, 0.0, 1.0) * 255.0)) << 8) | (uint(floor(0.5 + clamp(a.z, 0.0, 1.0) * 255.0)) << 16) | (uint(floor(0.5 + clamp(a.w, 0.0, 1.0) * 255.0)) << 24))

Examples

See also

Edit this page Report a problem