Compression in HLSL

Started by
3 comments, last by smallGame 10 years, 10 months ago

Hi,

I want to put 4 values of 256 bits in a float and read these 4 values in HLSL. Do you know if it is possible, in c++ I see several way to do that by using union or reinterpret_cast. But both are not avaible in HLSL:

To be clear I wrote a simple example :


// 4 values in c++
char v1 = 1, v2 = 2, v3 = 3, v4 = 4;
// Store them in C++
int store = (v1 << 0) | (v2 << 8) | (v3 << 16) | (v4 << 24);
// Put them in a float
float valueStored = *reinterpret_cast<float*>(&store);

// Now I want to use these values
// Oh no I cannot use reinterpret_cast!! :( in HLSL
int uncompress = *reinterpret_cast<int*>(&valueStored);

// Read the value
char o1, o2, o3, o4;
o1 = (char) uncompress & 0xFF;
o2 = (char)(uncompress >> 8) & 0xFF;
o3 = (char)(uncompress >> 16) & 0xFF;
o4 = (char)(uncompress >> 24) & 0xFF;

I want to use a float and not an int because I want to send a float4 to the GPU, with 3 floats and 1 float with 4 values.

Thanks for your help,

cheers,

Advertisement
Which version of HLSL (shader model 2/3/4/5) are you targeting?

Hi Hodgman,

I am using shader model 4 and 5, I don't have any restriction...

I saw you are helping a lot of people on this forum, so I guess I ll have a solution quite soon :) . I am really wondering why talented people like you spend so much time to help people stuck like me :).

Thanks,

SM4/5 has asint/asuint, which is the same as a reinterpret_cast to int/uint. From there, you can shift and mask as usual.

I am really wondering why talented people like you spend so much time to help people stuck like me

When I see a question that I can't answer, I wait for someone else to answer it and then I've learned something. That way I don't just learn what I'm looking for right now, but also what a dozen other people are looking for.
When I see a question that I can answer, by putting the answer into words myself, it forces me to make sure that I really do understand the question and reinforces my own learning.
I have become a much better programmer by reading these forums for years biggrin.png

Hi,

Thanks for your help ! Everything is working perfectly ! So it was an easy question I miss-read the doc ;)

see you

This topic is closed to new replies.

Advertisement