Question

I seem to be unable to crack this nut myself and after some googling where I came up blank I thought it were time to turn to the community looking for answers.

I have some code in a project I am working with, I will provide a quite simplified version but it should help me show the concept.

//hlsl code
...
ps_main(...) {
    half mask = function_calculating_mask_and_returning_half();
    half3 color = condition ? a_previously_defined_half3 : mask.rrr;
}

The last line here is what's confusing me, especially the mask.rrr what does it really do? as far as I understand it would return a half3 (mask, mask, mask) but I seem to get a really high value from it, if I change it to say mask.r or simply mask it give me a result with much lower values, (as far as I can tell).

Could it be that it does something like half3(mask^3, mask^3, mask^3)? Because that is how it seem when looking at the result.

Anyhow I am thankful for suggestions or explanations as to how the code work and what it means to subscript a half with .rrr and any reference to what subscripts are possible would be a neat Christmas present. :P

Was it helpful?

Solution

In HLSL scalar types such as float and half are one-dimensional vectors which have the only .x (or the .r) component. This subscription is usually used for the type-casting:

float s = .5;
float3 v1 = s.xxx; // v is (0.5, 0.5, 0.5), or even
float3 v2 = 0.5.xxx; // :)

There's also an implicit conversions from vectors to scalars that takes the first component. Remember that components are stored like RGBA (or XYZW):

float3 v = {0.5, 0.6, 0.7}
float s1 = v; // s1 is 0.5 (implicit conversion)
float s2 = v.x // s2 is 0.5 (explicit conversion)
float s3 = v.xxx /* s3 is 0.5 because we've just created float3(v.x, v.x, v.x) 
                    and then implicitly taken it's X component, which is v.x */

This is how it works. Try to modify your code:

half3 color = condition ? a_previously_defined_half3 : half3(mask, mask, mask)

Nothing should change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top