Skip to content

Commit 21f8930

Browse files
roelandschoukenslgritz
authored andcommitted
fix: Make linearstep definitely return 1 for x > edge1 (#2064)
The division (xclamped - edge0) / (edge1 - edge0) is not guaranteed to be exactly 1 for xclamped == edge1 if the backend floating point math has higher than expected error bounds. Eg. it is common on GPU backends to use faster but less precise instructions for division. The modified code is mathematically equivalent and it logically guarantees the returned value is exactly 1.0 for x > edge1 > edge0. Signed-off-by: Roeland Schoukens <roelandschoukens@hotmail.com>
1 parent 86d9f30 commit 21f8930

1 file changed

Lines changed: 1 addition & 2 deletions

File tree

src/shaders/stdosl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ vector smoothstep (vector edge0, vector edge1, vector x)
337337
float linearstep (float edge0, float edge1, float x) {
338338
float result;
339339
if (edge0 != edge1) {
340-
float xclamped = clamp (x, edge0, edge1);
341-
result = (xclamped - edge0) / (edge1 - edge0);
340+
result = clamp((x - edge0) / (edge1 - edge0), 0, 1);
342341
} else { // special case: edges coincide
343342
result = step (edge0, x);
344343
}

0 commit comments

Comments
 (0)