Little HLSL question

Started by
5 comments, last by schupf 15 years, 11 months ago

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?
Advertisement
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.
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?
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.
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?
Multiplication and division of a vector by a scalar is distributive, that's a mathematical rule.
Ok, I see!

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

Thanks for your help!

This topic is closed to new replies.

Advertisement