Search code examples
openglglslshader

Is there a built-in function in GLSL for AND or is there some optimized method for doing component wise AND?


GLSL has component wise functions for lessThan, greaterThan, etc, which return a bvec.

There's also any() and all(), but there seems to be no and().

If I have two bvec3s and want a new bvec3, equivalent to:

bvec3 new = bvec3(two.x && one.x, two.y && one.y, two.z && one.z);

Is there a faster way or more optimized way to do this?

I'm trying to write highly optimized GLSL code.


Solution

  • Not sure at all if this would be more efficient, but I believe you could do the and of two bvec3 values by converting them to another vector type like uvec3 or vec3, use the more extensive operations on those types (like bitwise and, multiplication), and then convert back.

    With your bvec3 values one and two, these are a few options:

    bvec3(uvec3(one) & uvec3(two))
    bvec3(uvec3(one) * uvec3(two))
    bvec3(vec3(one) * vec3(two))
    

    You should definitely benchmark before using this. There's a good chance that the component-wise expression is faster.