schwenk
schwenk

Reputation: 360

GLSL bitwise vec4 to float copy

(How) is it possible to do a bitwise copy of a vec4(32bit) into a float(32bit) bitwise ?

Already tried: Vec4ToFloat and Back Vec4ToFloat and Back

Upvotes: 0

Views: 965

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126203

A vec4 is usually 128 bits (4x32-bit float), so packing it into a single float is problematic.

Perhaps you mean packing an arbitrary 32-bit value into a float? You can use the builtin intBitsToFloat to copy 32 bits from an int to a float in a bitwise manner. However, the resulting float value might be a Nan of some type, in which case converting it back to an int with floatBitsToInt might not give you the same result.

edit

From your comment, you seem to confusing pixel formats in buffers with numeric formats in the shader. Whenever you read a pixel from a buffer or texture, it will be converted from the pixel format to a numeric format, depending on how you read the pixel. If you've done a normal texture lookup on a sampler of some kind bound to a RGBA texture, then each channel of the texure will have been unpacked into a 32-bit float in the range 0.0..1.0. You can easily pack it back into a 32-bit uint with packUnorm4x8, and then convert that 32-bit uint to a float with intBitsToFloat (which might have the above noted problem with Nan on the second step). Or you can just read it as a 32-bit int or float in the first place by using the appropriate texel type when you set up the sampler (which will affect interpolation, but you probably don't want interpolation at all, depending on what you are trying to do).

Upvotes: 2

Related Questions