Skip to content

Commit bc0c58b

Browse files
committed
Extend linearstep and smooth_linearstep to color/point/vector/normal (#994)
1 parent bb92d7b commit bc0c58b

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

src/doc/languagespec.tex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
\emph{lg@imageworks.com}
6262
}
6363
\date{{\large Date: 31 Mar 2018 \\
64-
(with corrections, 1 Nov 2018)
64+
(with corrections, 12 Apr 2019)
6565
}
6666
\bigskip
6767
\bigskip
@@ -3448,11 +3448,15 @@ \section{Pattern generation}
34483448
performed component-by-component (separately for $x$, $y$, and $z$).
34493449
\apiend
34503450

3451-
\apiitem{float {\ce linearstep} (float edge0, float edge1, float x)}
3451+
\apiitem{float {\ce linearstep} (float edge0, float edge1, float x) \\
3452+
\emph{type} {\ce linearstep} (\emph{type} edge0, \emph{type} edge1, \emph{type} x)}
34523453
\indexapi{linearstep()}
34533454
Returns 0 if $x \le {\mathit edge0}$, and 1 if $x \ge {\mathit edge1}$,
34543455
and performs a linear
34553456
interpolation between 0 and 1 when ${\mathit edge0} < x < {\mathit edge1}$.
3457+
This is equivalent to {\cf step(edge0, x)} when {\cf edge0 == edge1}.
3458+
For \color and \point-like types, the computations are
3459+
performed component-by-component (separately for $x$, $y$, and $z$).
34563460
\apiend
34573461

34583462
\apiitem{float {\ce smoothstep} (float edge0, float edge1, float x) \\
@@ -3466,17 +3470,20 @@ \section{Pattern generation}
34663470

34673471
The \emph{type} may be any of of \float, \color, \point, \vector, or
34683472
\normal. For \color and \point-like types, the computations are
3469-
performed component-by-component (separately for $x$, $y$, and $z$).
3473+
performed component-by-component.
34703474
\apiend
34713475

3472-
\apiitem{float {\ce smooth_linearstep} (float edge0, float edge1, float x, float eps)}
3476+
\apiitem{float {\ce smooth_linearstep} (float edge0, float edge1, float x, float eps) \\
3477+
\emph{type} {\ce smooth_linearstep} (\emph{type} edge0, \emph{type} edge1, \emph{type} x, \emph{type} eps)}
34733478
\indexapi{smooth_linearstep()}
34743479
This function is strictly linear between ${\mathit edge0}+{\mathit eps}$ and ${\mathit edge1}-{\mathit eps}$
34753480
but smoothly ramps to 0 between ${\mathit edge0}-{\mathit eps}$ and ${\mathit edge0}+{\mathit eps}$
34763481
and smoothly ramps to 1 between ${\mathit edge1}-{\mathit eps}$ and ${\mathit edge1}+{\mathit eps}$.
34773482
It is 0 when $x \le {\mathit edge0}-{\mathit eps}$, and 1 if $x \ge {\mathit edge1}+{\mathit eps}$,
34783483
and performs a linear
34793484
interpolation between 0 and 1 when ${\mathit edge0} < x < {\mathit edge1}$.
3485+
For \color and \point-like types, the computations are
3486+
performed component-by-component.
34803487
\apiend
34813488

34823489

src/shaders/stdosl.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,17 @@ normal step (normal edge, normal x) BUILTIN;
343343
float step (float edge, float x) BUILTIN;
344344
float smoothstep (float edge0, float edge1, float x) BUILTIN;
345345

346-
color smoothstep (color edge0, color edge1, color in)
346+
color smoothstep (color edge0, color edge1, color x)
347347
{
348-
return color (smoothstep(edge0[0], edge1[0], in[0]),
349-
smoothstep(edge0[1], edge1[1], in[1]),
350-
smoothstep(edge0[2], edge1[2], in[2]));
348+
return color (smoothstep(edge0[0], edge1[0], x[0]),
349+
smoothstep(edge0[1], edge1[1], x[1]),
350+
smoothstep(edge0[2], edge1[2], x[2]));
351351
}
352-
vector smoothstep (vector edge0, vector edge1, vector in)
352+
vector smoothstep (vector edge0, vector edge1, vector x)
353353
{
354-
return vector (smoothstep(edge0[0], edge1[0], in[0]),
355-
smoothstep(edge0[1], edge1[1], in[1]),
356-
smoothstep(edge0[2], edge1[2], in[2]));
354+
return vector (smoothstep(edge0[0], edge1[0], x[0]),
355+
smoothstep(edge0[1], edge1[1], x[1]),
356+
smoothstep(edge0[2], edge1[2], x[2]));
357357
}
358358

359359
float linearstep (float edge0, float edge1, float x) {
@@ -366,6 +366,18 @@ float linearstep (float edge0, float edge1, float x) {
366366
}
367367
return result;
368368
}
369+
color linearstep (color edge0, color edge1, color x)
370+
{
371+
return color (linearstep(edge0[0], edge1[0], x[0]),
372+
linearstep(edge0[1], edge1[1], x[1]),
373+
linearstep(edge0[2], edge1[2], x[2]));
374+
}
375+
vector linearstep (vector edge0, vector edge1, vector x)
376+
{
377+
return vector (linearstep(edge0[0], edge1[0], x[0]),
378+
linearstep(edge0[1], edge1[1], x[1]),
379+
linearstep(edge0[2], edge1[2], x[2]));
380+
}
369381

370382
float smooth_linearstep (float edge0, float edge1, float x_, float eps_) {
371383
float result;
@@ -385,6 +397,19 @@ float smooth_linearstep (float edge0, float edge1, float x_, float eps_) {
385397
return result;
386398
}
387399

400+
color smooth_linearstep (color edge0, color edge1, color x, color eps)
401+
{
402+
return color (smooth_linearstep(edge0[0], edge1[0], x[0], eps[0]),
403+
smooth_linearstep(edge0[1], edge1[1], x[1], eps[1]),
404+
smooth_linearstep(edge0[2], edge1[2], x[2], eps[2]));
405+
}
406+
vector smooth_linearstep (vector edge0, vector edge1, vector x, vector eps)
407+
{
408+
return vector (smooth_linearstep(edge0[0], edge1[0], x[0], eps[0]),
409+
smooth_linearstep(edge0[1], edge1[1], x[1], eps[1]),
410+
smooth_linearstep(edge0[2], edge1[2], x[2], eps[2]));
411+
}
412+
388413
float aastep (float edge, float s, float dedge, float ds) {
389414
// Box filtered AA step
390415
float width = fabs(dedge) + fabs(ds);

testsuite/linearstep/ref/out.tif

-369 Bytes
Binary file not shown.

testsuite/linearstep/test.osl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@ shader
22
test (output color Cout = 0)
33
{
44
Cout = 0;
5-
if (v < 0.2) // degenerate linearstep with coincident edges
5+
if (v < 0.1) // degenerate linearstep with coincident edges
66
Cout = linearstep (0.5, 0.5, u);
7-
else if (v < 0.4) // ordinary linearstep
7+
else if (v < 0.2) // ordinary linearstep
88
Cout = linearstep (0.25, 0.75, u);
9-
else if (v < 0.6) // deginerate sls with eps==0
9+
else if (v < 0.3) // degenerate sls with eps==0
1010
Cout = smooth_linearstep (0.25, 0.75, u, 0.0);
11-
else if (v < 0.8) // regular sls
11+
else if (v < 0.4) // regular sls
1212
Cout = smooth_linearstep (0.25, 0.75, u, 0.05);
13-
else if (v < 1.0) // degenerate sls with coincident edges
13+
else if (v < 0.5) // degenerate sls with coincident edges
1414
Cout = smooth_linearstep (0.5, 0.5, u, 0.0);
15+
16+
else if (v < 0.6) // degenerate linearstep with coincident edges
17+
Cout = linearstep (color(0.5), color(0.5), u);
18+
else if (v < 0.7) // ordinary linearstep
19+
Cout = linearstep (color(0.25), color(0.75), u);
20+
else if (v < 0.8) // degenerate sls with eps==0
21+
Cout = smooth_linearstep (color(0.25), color(0.75), u, color(0.0));
22+
else if (v < 0.9) // regular sls
23+
Cout = smooth_linearstep (color(0.25), color(0.75), u, color(0.05));
24+
else if (v <= 1.0) // degenerate sls with coincident edges
25+
Cout = smooth_linearstep (color(0.5), color(0.5), u, color(0.0));
26+
1527
Cout = 0.1 + Cout * 0.8;
1628
}

0 commit comments

Comments
 (0)