0

I have encoded floats into a texture (4 bytes of a float32 are stored as RBGA values). Now I need to decode them back into a single float.

Here's what I've tried so far and what didn't work for me:

    float rgbaToFloat(vec4 rgba) {
      uint r = uint(rgba.x*255.);
      uint g = uint(rgba.y*255.);
      uint b = uint(rgba.z*255.);
      uint a = uint(rgba.w*255.);

      return uintBitsToFloat((r << 24) | (g << 16) | (b << 8) | a);
    }

I pack floats into a texture image using the following snippet (python):

import numpy as np

data_flat = [...]  #floats

dim = int(np.sqrt(len(data_flat))) + 1
image = np.zeros((dim, dim, 4), dtype='uint8')
for i, d in enumerate(data_flat):
    image[i // dim, i % dim] = np.array([d], dtype=np.float32).view(np.uint8)

For a single floating-point value, it produces the following output:

d = 0.06797099858522415;
np.array([d], dtype=np.float32).view(np.uint8)
>>> array([ 97,  52, 139,  61], dtype=uint8)

Which seems to be correct, as binary representations match.

It seems to return overflowed values, hard to say, with the only debug way being comparing pictures, but it's certainly not what I expect it to output.

15
  • 3
    Which code do you use to pack the float into the image? Also, wouldn't it be way easier to use a floating point texture? Commented Jan 28, 2020 at 12:59
  • 2
    note a 32-bit float can't represent all 32-bit integer values, it has only 24 bits of mantissa Commented Jan 28, 2020 at 13:00
  • 3
    Why don't you just directly use floats in GLSL? Commented Jan 28, 2020 at 13:12
  • 2
    @Vladislav In shadertoy? I have no idea. Do you have to use shadertoy though? OpenGL itself supports single-channel float textures just fine. Commented Jan 28, 2020 at 14:18
  • 1
    Maybe this is what you need: stackoverflow.com/questions/18453302/… Commented Jan 29, 2020 at 13:09

1 Answer 1

0

Thanks to @LJᛃ for referring me to this answer, it works like charm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.