6161Editor: Larry Gritz \\
6262\emph {lg@imageworks.com }
6363}
64- \date {{\large Date: 6 Feb 2017 \\
65- (with corrections, 12 May 2017)
64+ \date {{\large Date: 16 May 2017 \\
65+ % (with corrections, 12 May 2017)
6666}
6767\bigskip
6868\bigskip
@@ -886,7 +886,7 @@ \section{Preprocessor}
886886\end {tabular }
887887
888888
889- \chapter {Gross syntax, shader types, parameters, functions }
889+ \chapter {Gross syntax, shader types, parameters }
890890\label {chap:grosssyntax }
891891
892892The overall structure of a shader is as follows:
@@ -1336,48 +1336,6 @@ \section{Shader metadata}
13361336
13371337
13381338\newpage
1339- \section {Functions }
1340- \label {sec:functions }
1341- \index {functions!declarations}
1342-
1343- You may define functions much like in C or C++.
1344-
1345- \begin {quote }
1346- \em
1347- return-type ~ function-name ~ {\rm\cf (} optional-parameters {\rm\cf )} \\
1348- \rm
1349- {\cf \{ } \\
1350- \em
1351- \spc statements
1352-
1353- {\cf \} }
1354- \end {quote }
1355-
1356- Parameters to functions are similar to shader parameters, except that
1357- they do not permit initializers. A function call must pass values for
1358- all formal parameters. Function parameters in \langname are all
1359- \emph {passed by reference }, and are read-only within the body of the
1360- function unless they are also designated as {\cf output} (in the same
1361- manner as output shader parameters).
1362-
1363- Like for shaders, statements inside functions may be actual executions
1364- (assignments, function call, etc.), local variable declarations (visible
1365- only from within the body of the function), or local function
1366- declarations (callable only from within the body of the function).
1367-
1368- The return type may be any simple data type, a {\cf struct}, or a {\cf
1369- closure}. Functions may not return arrays. The return type may be
1370- {\cf void}, indicating that the function does not return a value (and
1371- should not contain a {\cf return} statement). A {\cf return} statement
1372- inside the body of the function will halt execution of the function at
1373- that point, and designates the value that will be returned (if not a
1374- {\cf void} function).
1375-
1376- Functions may be \emph {overloaded }. That is, multiple functions may be
1377- defined to have the same name, as long as they have differently-typed
1378- parameters, so that when the function is called the list of arguments
1379- can disambiguate which version of the function is desired.
1380-
13811339\section {Public methods }
13821340\label {sec:publicmethods }
13831341\indexapi {public}
@@ -2632,7 +2590,10 @@ \section{Control flow: {\cf if, while, do, for}}
26322590and proceeds to the next iteration of the loop.
26332591
26342592\section {Functions }
2635- \label {sec:syntax:functions }
2593+ \label {sec:functions }
2594+
2595+ \subsection {Function calls }
2596+ \label {sec:syntax:functioncalls }
26362597\index {function calls}
26372598
26382599Function calls are very similar to C and related programming languages:
@@ -2651,8 +2612,111 @@ \section{Functions}
26512612semantics, except if you pass the same variable as two separate
26522613arguments to a function that modifies an argument's value.
26532614
2654- Function definitions are described in detail in Section~\ref {sec:functions }.
26552615
2616+ \subsection {Function definitions }
2617+ \label {sec:syntax:functiondefinitions }
2618+ \index {functions!definitions}
2619+
2620+ You may define functions much like in C or C++.
2621+
2622+ \begin {quote }
2623+ \em
2624+ return-type ~ function-name ~ {\rm\cf (} optional-parameters {\rm\cf )} \\
2625+ \rm
2626+ {\cf \{ } \\
2627+ \em
2628+ \spc statements
2629+
2630+ {\cf \} }
2631+ \end {quote }
2632+
2633+ Parameters to functions are similar to shader parameters, except that
2634+ they do not permit initializers. A function call must pass values for
2635+ all formal parameters. Function parameters in \langname are all
2636+ \emph {passed by reference }, and are read-only within the body of the
2637+ function unless they are also designated as {\cf output} (in the same
2638+ manner as output shader parameters).
2639+
2640+ Like for shaders, statements inside functions may be actual executions
2641+ (assignments, function call, etc.), local variable declarations (visible
2642+ only from within the body of the function), or local function
2643+ declarations (callable only from within the body of the function).
2644+
2645+ The return type may be any simple data type, a {\cf struct}, or a {\cf
2646+ closure}. Functions may not return arrays. The return type may be
2647+ {\cf void}, indicating that the function does not return a value (and
2648+ should not contain a {\cf return} statement). A {\cf return} statement
2649+ inside the body of the function will halt execution of the function at
2650+ that point, and designates the value that will be returned (if not a
2651+ {\cf void} function).
2652+
2653+ Functions may be \emph {overloaded }. That is, multiple functions may be
2654+ defined to have the same name, as long as they have differently-typed
2655+ parameters, so that when the function is called the list of arguments
2656+ can disambiguate which version of the function is desired.
2657+
2658+ \subsection {Operator overloading }
2659+ \label {sec:syntax:operatoroverloading }
2660+ \index {operator overloading}
2661+
2662+ \NEW % 1.9
2663+ OSL permits \emph {operator overloading }, which is the practice of providing
2664+ a function that will be called when you use an operator like {\cf +} or
2665+ {\cf *}. This is especially handy when you use {\cf struct} to define
2666+ mathematical types and wish for the usual math operators to work with them.
2667+ Here is a typical example, which also shows the special naming convention
2668+ that allows operator overloading:
2669+
2670+ \begin {code }
2671+ struct vector4 {
2672+ float x, y, z, w;
2673+ };
2674+
2675+ vector4 __operator__add__ (vector4 a, vector4 b) {
2676+ return vector4 (a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w);
2677+ }
2678+
2679+ shader test ()
2680+ {
2681+ vector4 a = vector4 (.2, .3, .4, .5);
2682+ vector4 b = vector4 (1, 2, 3, 4);
2683+
2684+ vector4 c = a + b; // Will call __operator__add__(vector4,vector4)
2685+ printf ("a+b = % g %g %g %g\n", c.x, c.y, c.z, c.w);
2686+ }
2687+ \end {code }
2688+
2689+ \noindent The full list of these special function names is as follows (in
2690+ order of decreasing operator precedence):
2691+
2692+ \smallskip
2693+
2694+ \begin {tabular }{ p{0.5in} p{2in} p{1.75in}}
2695+ % op & overload function name & ~ \\
2696+ % \hline
2697+ {\cf \bfseries -} & {\cf __operator__neg__} & unary negation \\
2698+ {\cf \bfseries \textasciitilde } & {\cf __operator__compl__} & unary bitwise complement \\
2699+ {\cf \bfseries !} & {\cf __operator__not__} & unary boolean `not' \\[1.5ex]
2700+ {\cf \bfseries *} & {\cf __operator__mul__} & \\
2701+ {\cf \bfseries /} & {\cf __operator__div__} & \\
2702+ {\cf \bfseries \% } & {\cf __operator__mod__} & \\
2703+ {\cf \bfseries +} & {\cf __operator__add__} & \\
2704+ {\cf \bfseries -} & {\cf __operator__sub__} & \\[1.5ex]
2705+ {\cf \bfseries <<} & {\cf __operator__shl__} & \\
2706+ {\cf \bfseries >>} & {\cf __operator__shr__} & \\[1.5ex]
2707+ {\cf \bfseries <} & {\cf __operator__lt__} & \\
2708+ {\cf \bfseries <=} & {\cf __operator__le__} & \\
2709+ {\cf \bfseries >} & {\cf __operator__gt__} & \\
2710+ {\cf \bfseries >=} & {\cf __operator__ge__} & \\
2711+ {\cf \bfseries ==} & {\cf __operator__eq__} & \\
2712+ {\cf \bfseries !=} & {\cf __operator__ne__} & \\[1.5ex]
2713+ {\cf \bfseries \& } & {\cf __operator__bitand__} & \\
2714+ {\cf \bfseries \textasciicircum } & {\cf __operator__xor__} & \\
2715+ {\cf \bfseries |} & {\cf __operator__bitor__} & \\
2716+ % {\cf \bfseries \&\&} & {\cf __operator__and__} & boolean and \\
2717+ % {\cf \bfseries ||} & {\cf __operator__or__} & boolean or \\
2718+ % \hline
2719+ \end {tabular }
26562720
26572721
26582722\section {Global variables }
0 commit comments