Jump to content
  • Advertisement
Sign in to follow this  
AmzBee

HLSL Unpack float into 4 bytes

This topic is 2074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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

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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!