|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#ifndef BRANCHING_GLSL_ |
| 6 | +#define BRANCHING_GLSL_ |
| 7 | + |
| 8 | +#include <impeller/constants.glsl> |
| 9 | +#include <impeller/types.glsl> |
| 10 | + |
| 11 | +/// Perform an equality check for each vec3 component. |
| 12 | +/// |
| 13 | +/// Returns 1.0 if x == y, otherwise 0.0. |
| 14 | +BoolV3 IPVec3IsEqual(vec3 x, float y) { |
| 15 | + vec3 diff = abs(x - y); |
| 16 | + return vec3(diff.r < kEhCloseEnough, // |
| 17 | + diff.g < kEhCloseEnough, // |
| 18 | + diff.b < kEhCloseEnough); |
| 19 | +} |
| 20 | + |
| 21 | +/// Perform a branchless greater than check. |
| 22 | +/// |
| 23 | +/// Returns 1.0 if x > y, otherwise 0.0. |
| 24 | +BoolF IPFloatIsGreaterThan(float x, float y) { |
| 25 | + return max(sign(x - y), 0); |
| 26 | +} |
| 27 | + |
| 28 | +/// Perform a branchless greater than check for each vec3 component. |
| 29 | +/// |
| 30 | +/// Returns 1.0 if x > y, otherwise 0.0. |
| 31 | +BoolV3 IPVec3IsGreaterThan(vec3 x, vec3 y) { |
| 32 | + return max(sign(x - y), 0); |
| 33 | +} |
| 34 | + |
| 35 | +/// Perform a branchless less than check. |
| 36 | +/// |
| 37 | +/// Returns 1.0 if x < y, otherwise 0.0. |
| 38 | +BoolF IPFloatIsLessThan(float x, float y) { |
| 39 | + return max(sign(y - x), 0); |
| 40 | +} |
| 41 | + |
| 42 | +/// For each vec3 component, if value > cutoff, return b, otherwise return a. |
| 43 | +vec3 IPVec3ChooseCutoff(vec3 a, vec3 b, vec3 value, float cutoff) { |
| 44 | + return mix(a, b, IPVec3IsGreaterThan(value, vec3(cutoff))); |
| 45 | +} |
| 46 | + |
| 47 | +/// For each vec3 component, if value > 0.5, return b, otherwise return a. |
| 48 | +vec3 IPVec3Choose(vec3 a, vec3 b, vec3 value) { |
| 49 | + return IPVec3ChooseCutoff(a, b, value, 0.5); |
| 50 | +} |
| 51 | + |
| 52 | +#endif |
0 commit comments