# Little HLSL question

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
Quote:
 Original post by schupfYes, 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 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 on other sites
Multiplication and division of a vector by a scalar is distributive, that's a mathematical rule.

##### Share on other sites
Ok, I see!

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

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633678
• Total Posts
3013290
×