Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

[glsl-in] True and false branch of conditional expression operands are always evaluated #1854

Closed
DDoS opened this issue Apr 19, 2022 · 0 comments · Fixed by #1877
Closed
Labels
area: front-end Input formats for conversion kind: bug Something isn't working lang: GLSL OpenGL Shading Language

Comments

@DDoS
Copy link
Contributor

DDoS commented Apr 19, 2022

The expression:

localSums[left] = globalLeft < globalSumBuffer.count ? globalSumBuffer.sums[globalLeft] : 0;

compiles to:

let _e36 = globalLeft;
let _e38 = globalSumBuffer.count;
let _e40 = globalLeft;
let _e43 = globalSumBuffer.sums[_e40];
localSums[_e34] = select(u32(0), _e43, (_e36 < _e38));

which evaluates both the true and false branches before evaluation the condition. This is wrong according to the GLSL spec:

The ternary selection operator (?:). It operates on three expressions (exp1 ? exp2 : exp3). This
operator evaluates the first expression, which must result in a scalar Boolean. If the result is true, it
selects to evaluate the second expression, otherwise it selects to evaluate the third expression. Only
one of the second and third expressions is evaluated.
GLSL 4.4 Spec page 109

In the example above, if the condition is false, then the true branch is an out-of-bound access, which cannot be evaluated.

@DDoS DDoS changed the title [glsl-in] True and false branch conditional expression operands are always evaluated [glsl-in] True and false branch of conditional expression operands are always evaluated Apr 19, 2022
@JCapucho JCapucho added kind: bug Something isn't working area: front-end Input formats for conversion lang: GLSL OpenGL Shading Language labels Apr 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: front-end Input formats for conversion kind: bug Something isn't working lang: GLSL OpenGL Shading Language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants