Sign in to follow this  
AmzBee

HLSL Unpack float into 4 bytes

Recommended Posts

AmzBee    485
Hey everyone, if I pack 4 bytes into a float, is there any way for me to pass that float into a HLSL shader and un-pack those bytes again. I'm looking into this to save wastage in an instancing routine, I'd rather not have to use a whole float to represent a single byte value. If it helps, the method I'm using for packing the bytes is BitConverter.ToSingle
 
I also found a good example ( http://theinstructionlimit.com/encoding-boolean-flags-into-a-float-in-hlsl ) on how to pack/un-pack boolean's which is helpful, but really I need the bytes.
 
Thanks for any help.
 
Aimee

Share this post


Link to post
Share on other sites
Hodgman    51339
In that case, you don't have to do anything fancy in the shader at all. You can use a float4 in your HLSL code, write your byte values to the vertex buffer, tell the vertex-declaration/input-layout that those values are actually bytes (not floats), and the hardware will do the byte4->float4 conversion automatically.

Which version of D3D are you using? And you mentioned BitConverter, so I guess you're using C#, so which C# wrapper are you using too?

Share this post


Link to post
Share on other sites
AmzBee    485

I'm using SM3 for the shader, and XNA 4 (so 4 bytes = 1 single/float). I just did a bit more digging through msdn and it looks like there is no support for a byte type equivalent. which probably means id have to do something mad like cast the float in HLSL as an integer then do some bit manipulation which I believe isn't available either. 

 

You mentioned just passing the bytes on their own unpacked which may lead to a solution, though if it does convert each byte to a float then it'd remove the advantage by taking 4 times the space of just 4 plain old bytes which takes me back to the original problem.

 

Looks like i'll just have to suck it up, use the boolean packing example I found, and make the best of it.

 

Aimee

Share this post


Link to post
Share on other sites
Hodgman    51339
When you create your VertexDeclaration, you give it a VertexElement for each field in the buffer.
For your 4 byte values, you use an element with the VertexElementFormat.Byte4 format. They will exist as 4 (packed) bytes within the buffer, and are only expanded to 4 floats (by dividing them by 255.0f) the moment when the vertex shader is run.
 
If you were planning on using a single element from a matrix to store these values, then you'll have to replace one of the matrix's VertexElementFormat.Vector4 elements with a VertexElementFormat.Vector3 element and this VertexElementFormat.Byte4 element. Edited by Hodgman

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this