HLSL Unpack float into 4 bytes

Started by
5 comments, last by AmzBee 11 years, 2 months ago
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

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

Advertisement
Are you trying to do this for a shader uniform variable, or an element in a vertex stream?

I'm using instancing, so the float is passed as a component of a matrix in a 2nd vertex buffer.

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

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?

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

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

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.

That definately puts me in the right direction, thanks for your help :)

Aimee

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

This topic is closed to new replies.

Advertisement