Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Packing multiple floats into a single float value

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2008
    Posts
    6

    Packing multiple floats into a single float value

    I'm trying to pack multiple float values(for normals, colors, etc) into a single float for use with a deferred shader. From what I read, you can use Cg functions in GLSL as long as there isn't a #version line in the shader. I'm trying to use floatToRawIntBits & intBitsToFloat, because they seem to be exactly what I need. The shader compiles without errors or warnings.

    The values I'm getting back aren't correct. The majority seems to be 0. Let me know if you can spot what I'm doing wrong, or have a better way to do this.

    Here is my code.

    Code :
    int ConvertToInt(float f)
    { 
         return int(255.0*f);
    }
     
    float PackVec4(vec4 v)
    {
         int intVal = (ConvertToInt(v.w) << 24) | (ConvertToInt(v.y) << 16) | (ConvertToInt(v.z) << 8) | ConvertToInt(v.x);
         return intBitsToFloat(intVal);
    }
     
    vec4 UnpackVec4(float val)
    {
         vec4 result;
         result.x = float((floatToRawIntBits(val) >> 0) & 0xFF) / 255.0;
         result.y = float((floatToRawIntBits(val) >> 8) & 0xFF) / 255.0;
         result.z = float((floatToRawIntBits(val) >> 16) & 0xFF) / 255.0;
         result.w = float((floatToRawIntBits(val) >> 24) & 0xFF) / 255.0;
         return result; 
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948

    Re: Packing multiple floats into a single float value

    From what I read, you can use Cg functions in GLSL as long as there isn't a #version line in the shader.
    And you're using an NVIDIA implementation. I would strongly advise against this.

  3. #3
    Senior Member OpenGL Pro sqrt[-1]'s Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    1,000

    Re: Packing multiple floats into a single float value

    Try:
    http://aras-p.info/blog/2009/07/30/e...gba-the-final/

    Code :
    inline float4 EncodeFloatRGBA( float v ) {
      float4 enc = float4(1.0, 255.0, 65025.0, 160581375.0) * v;
      enc = frac(enc);
      enc -= enc.yzww * float4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);
      return enc;
    }
    inline float DecodeFloatRGBA( float4 rgba ) {
      return dot( rgba, float4(1.0, 1/255.0, 1/65025.0, 1/160581375.0) );
    }

    or
    http://diaryofagraphicsprogrammer.bl...fp-render.html

    or
    http://www.opengl.org/discussion_boa...;Number=265070

  4. #4
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,580

    Re: Packing multiple floats into a single float value

    Beware, there seem to be a typo in the original post, 160581375.0 should be replaced by 16581375.0

  5. #5
    Intern Newbie
    Join Date
    Sep 2009
    Posts
    30

    Re: Packing multiple floats into a single float value

    would it be quicker and would you lose any precision if you precalculated the divisions as constants like

    Code :
    const float x = 1.0/255.0;
    const float y = 1.0/65025.0;
    const float z = 1.0/16581375.0;
     
     
    vec4 EncodeFloatRGBA( float v ) {
      vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * v;
      enc = fract(enc);
      enc -= enc.yzww * vec4(x,x,x,0.0);
      return enc;
    }
    float DecodeFloatRGBA( vec4 rgba ) {
      return dot( rgba, vec4(1.0, x, y, z) );
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •