Sign in to follow this  
schupf

Little HLSL question

Recommended Posts

float rayleighDepth = ...	
float mieDepth      = ...
float3	wavelength4 = ...

float3 attenuation
attenuation[0] = exp(-rayleighDepth / wavelength4[0] - mieDepth);
attenuation[1] = exp(-rayleighDepth / wavelength4[1] - mieDepth);
attenuation[2] = exp(-rayleighDepth / wavelength4[2] - mieDepth);

float3 attenuation2 = exp(-rayleighDepth / wavelength4 - mieDepth);

Short question: Are attenuation and attenuation2 the same?

Share this post


Link to post
Share on other sites
The result should be equivalent, since division, subtraction, and exp are all operations that that are performed on the individual components of the vectors. The HLSL compiler should compile both to the same assembly (using vector operations), or at least when optimization is enabled.

Share this post


Link to post
Share on other sites
Yes, but rayleighDepth and mieDepth are no vectors. Thats why I dont know if it is the same.

Would it be better to write rayleighDepth.xyz and mieDepth.xyz?

Share this post


Link to post
Share on other sites
Quote:
Original post by schupf
Yes, but rayleighDepth and mieDepth are no vectors. Thats why I dont know if it is the same.

It doesn't matter that they are not vectors. It's the same as in algebra, multiplication of a scalar and a vector is a vector with components of the original vector multiplied by the scalar, each one separately.
So in this HLSL code:

float3 vector = float3(1, 2, 3);
float scalar = 0.5f;
float3 result = vector * scalar;

The "result" variable will be equal to (0.5, 1, 1.5)

Quote:

Would it be better to write rayleighDepth.xyz and mieDepth.xyz?

Nope. These variables are float (scalars), they don't have x y z components.

Share this post


Link to post
Share on other sites
Yes, your multiplication example sounds logical to me.

Without a doubt this would be the same:

wavelength4 /rayleighDepth


Every component of vector wavelength4 is divided by rayleighDepth.

but my code

rayleighDepth / wavelength4



divides a scalar by a vector. Are you 100% sure that this doesnt mean "divide the sclara rayleighDepth by the first element of wavelength4"?

Is there a website which lists the arithmetic operator rules for vectors and scalars in HLSL?

Share this post


Link to post
Share on other sites
Ok, I see!

So
rayleighDepth / wavelength4
is actually identical
to
float3(rayleighDepth,rayleighDepth,rayleighDepth) / wavelength4

Thanks for your help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this