Discussion:
HLSL, casting
(too old to reply)
Hanna-Barbera
2005-01-07 20:54:43 UTC
Permalink
Hi,

I'm wondering what is the proper way to cast variables in HLSL.
Say I want a float4 to become a float3

objLightPos0= (float3)mul(float4(LightPosition0, 1.0), matViewInverse);

When I do this
objLightPos0= float3(mul(float4(LightPosition0, 1.0), matViewInverse));

I get a compile error
legalize+ (Rich [Microsoft Direct3D MVP])
2005-01-07 22:24:56 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by Hanna-Barbera
I'm wondering what is the proper way to cast variables in HLSL.
C-style, not C++ style.
Post by Hanna-Barbera
Say I want a float4 to become a float3
objLightPos0= (float3)mul(float4(LightPosition0, 1.0), matViewInverse);
This is C style.
Post by Hanna-Barbera
When I do this
objLightPos0= float3(mul(float4(LightPosition0, 1.0), matViewInverse));
I get a compile error
This is C++ style (using the cast as a "constructor" syntax.)
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>
Hanna-Barbera
2005-01-08 23:29:34 UTC
Permalink
OK, another 2 questions

if I want to cast a "higher order" variable to a "lower order", can I do

float value;
float4 value2; //contains something

value = value2;

Also, for constructing a higher order variable
I guess we have to do something like this

float value; //contains something
float4 value2;

value2 = float4(value, value, value, value);
legalize+ (Rich [Microsoft Direct3D MVP])
2005-01-10 17:53:49 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by Hanna-Barbera
if I want to cast a "higher order" variable to a "lower order", can I do
float value;
float4 value2; //contains something
value = value2;
It doesn't make any sense to do this because the assignment is
ambiguous. What does it mean to assign a 4D vector to a scalar? Even
in mathematics this is nonsensical. Use value = value2.x instead.
Post by Hanna-Barbera
Also, for constructing a higher order variable
I guess we have to do something like this
float value; //contains something
float4 value2;
value2 = float4(value, value, value, value);
Promoting a scalar to a 4D vector *is* well defined. Whether you want
replication of (x,x,x,x) or promotion of (x,0,0,0) is up to you in
terms of what you write. I don't recall what the compiler does off
the top of my head when you promote a scalar to a vector.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>
Loading...