Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packing ints into a vec4 in a shader does not currently work #2

Open
gkjohnson opened this issue Sep 27, 2017 · 4 comments
Open

Packing ints into a vec4 in a shader does not currently work #2

gkjohnson opened this issue Sep 27, 2017 · 4 comments

Comments

@gkjohnson
Copy link
Owner

Apparently bitwise operations are not available in the current version of webgl (??!!!??), meaning ints and uints can't easily be packed into the vec4 color output and unpacked in javascript again.

It may be possible to use these functions to emulate bitwise operations.

@gkjohnson
Copy link
Owner Author

WebGL2 will come with bit-shift operators and higher-precision textures, both of which can fix this issue

@gkjohnson
Copy link
Owner Author

ThreeJS WebGL2Renderer Issue: mrdoob/three.js#9965

@DanielJoyce
Copy link

DanielJoyce commented Dec 11, 2017

You can do it webgl you just need to do the shifting youtself via multiplies

/**
* Properly encoding 32 bit float in rgba from here:
* http://www.gamedev.net/topic/486847-encoding-16-and-32-bit-floating-point-value-into-rgba-byte-texture/
*/
vec4 pack( const in float depth ) {
    const float toFixed = 255.0/256.0;
    vec4 result = vec4(0);
    result.r = fract(depth*toFixed*1.0);
    result.g = fract(depth*toFixed*255.0);
    result.b = fract(depth*toFixed*255.0*255.0);
    result.a = fract(depth*toFixed*255.0*255.0*255.0);
    return result;
}

void main() {
  gl_FragColor = pack(gl_FragCoord.z);
}

@gkjohnson
Copy link
Owner Author

Thanks @DanielJoyce!

I'd considered packing the values manually -- I'll probably get around to it at some point!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants