Search code examples
vectorcompareglslhlsl

glsl member-wise ?: operator


does glsl support ?: on vectors, like this:

// hlsl
half3 linear_to_sRGB(half3 x)
{
    return (x <= 0.0031308 ? (x * 12.9232102) : 1.055 * pow(x, 1.0 / 2.4) - 0.055);
}

I tried like this (via glm), not working as expected (i don't think they are the same thing):

//c++, glm
vec3 linear_to_sRGB(vec3 x)
{
    return lessThan(v, vec3(0.0031308f)) == bvec3(true)
        ? (v * 12.9232102f)
        : 1.055f * glm::pow(v, vec3(1.0f / 2.4f)) - 0.055f
        ;
}


Solution

  • You can achieve something like this with the built-in step and mix functions:

    return mix(
        1.055 * pow(x, 1.0 / 2.4) - 0.055,
        x * 12.9232102,
        step(x, vec3(0.0031308f)));