Jump to content

  • Log In with Google      Sign In   
  • Create Account


HLSL Unpack float into 4 bytes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 18 February 2013 - 04:30 AM

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


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 27590

Like
0Likes
Like

Posted 18 February 2013 - 04:40 AM

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

#3 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 18 February 2013 - 04:45 AM

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


#4 Hodgman   Moderators   -  Reputation: 27590

Like
0Likes
Like

Posted 18 February 2013 - 04:50 AM

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?

#5 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 18 February 2013 - 04:59 AM

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


#6 Hodgman   Moderators   -  Reputation: 27590

Like
3Likes
Like

Posted 18 February 2013 - 05:11 AM

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, 18 February 2013 - 05:14 AM.


#7 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 18 February 2013 - 05:20 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS