2

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
        ;
}

21k
  • 391
  • 5
  • 16

1 Answers1

2

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)));
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    elegant. i wanted to use lessThanEqual, but step is better. (glm::convertSRGBToLinear uses lessThanEqual.) – 21k May 06 '23 at 19:11
  • i guess use lessThanEqual in this case is correct. because it is <= and not <. – 21k May 06 '23 at 19:16
  • @21k No. I use `step(x, vec3(0.0031308f))` which is x <= 0.0031308f. The opposit woud be incorrect, `step(x0.0031308f, x)` would be 0.0031308f <= x. – Rabbid76 May 06 '23 at 19:17
  • but according this: https://registry.khronos.org/OpenGL-Refpages/gl4/html/step.xhtml, it states: `For element i of the return value, 0.0 is returned if x[i] < edge[i], and 1.0 is returned otherwise.` – 21k May 06 '23 at 19:20
  • @21k yes exactly **0** is returned if x[i] < edge[i] and *1* is returned if edge[i] <= x[i]. "edge" is the 1st argument. – Rabbid76 May 06 '23 at 19:21
  • you're right. (It took me 45 minutes to figure it out, use lessThanEqual is more straightforward to me.) – 21k May 06 '23 at 20:16
  • @21k You need to compare performance to determine which solution is better. – Rabbid76 May 06 '23 at 20:21