Jake Freelander
Jake Freelander

Reputation: 1471

how to convert float to int preserving bit value

I have a float4 coming into a compute shader, 3 of these floats are really floats but the fourth is 2 uints shifted together, how would i convert the float to uint by preserving the bit sequence instead of the numeric value?

on the c++ side i solved it by creating a uint pointer, filling it with the desired number and passing on the pointer as a float pointer instead. However in hlsl as similar as it is to c/c++ there are no pointers so im stuck here :|

Upvotes: 5

Views: 10951

Answers (2)

Goz
Goz

Reputation: 62323

In HLSL you should be able to do the following (assuming the value you are after is in f4.w)

uint ui = asuint( f4.w );
uint ui1 = ui & 0xffff;
uint ui2 = ui >> 16;

Basically it looks like the asuint intrinsic is your friend :)

Upvotes: 9

Anon Mail
Anon Mail

Reputation: 4770

You could use a union.

float f;  // you float value is here

union X  
{  
    float f;  
    short int a[2];  
} x;  
x.f = f;

int i1 = x.a[0];  // these are your ints  
int i2 = x.a[1];

Upvotes: 2

Related Questions