1
/////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
7
// * Redistributions of source code must retain the above copyright
8
// notice, this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above copyright
10
// notice, this list of conditions and the following disclaimer in the
11
// documentation and/or other materials provided with the distribution.
12
// * Neither the name of Sony Pictures Imageworks nor the names of its
13
// contributors may be used to endorse or promote products derived from
14
// this software without specific prior written permission.
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
/////////////////////////////////////////////////////////////////////////////
34
#define M_PI 3.1415926535897932 /* pi */
35
#define M_PI_2 1.5707963267948966 /* pi/2 */
36
#define M_PI_4 0.7853981633974483 /* pi/4 */
37
#define M_2_PI 0.6366197723675813 /* 2/pi */
38
#define M_2_SQRTPI 1.1283791670955126 /* 2/sqrt(pi) */
39
#define M_E 2.7182818284590452 /* e (Euler's number) */
40
#define M_LN2 0.6931471805599453 /* ln(2) */
41
#define M_LN10 2.3025850929940457 /* ln(10) */
42
#define M_LOG2E 1.4426950408889634 /* log_2(e) */
43
#define M_LOG10E 0.4342944819032518 /* log_10(e) */
44
#define M_SQRT2 1.4142135623730950 /* sqrt(2) */
45
#define M_SQRT1_2 0.7071067811865475 /* 1/sqrt(2) */
50
// Declaration of built-in functions and closures
51
#define BUILTIN [[ int builtin = 1 ]]
52
#define BUILTIN_DERIV [[ int builtin = 1, int deriv = 1 ]]
54
#define PERCOMP1(name) \
55
normal name (normal x) BUILTIN; \
56
vector name (vector x) BUILTIN; \
57
point name (point x) BUILTIN; \
58
color name (color x) BUILTIN; \
59
float name (float x) BUILTIN;
61
#define PERCOMP2(name) \
62
normal name (normal x, normal y) BUILTIN; \
63
vector name (vector x, vector y) BUILTIN; \
64
point name (point x, point y) BUILTIN; \
65
color name (color x, color y) BUILTIN; \
66
float name (float x, float y) BUILTIN;
68
#define PERCOMP2F(name) \
69
normal name (normal x, float y) BUILTIN; \
70
vector name (vector x, float y) BUILTIN; \
71
point name (point x, float y) BUILTIN; \
72
color name (color x, float y) BUILTIN; \
73
float name (float x, float y) BUILTIN;
77
normal degrees (normal x) { return x*(180.0/M_PI); }
78
vector degrees (vector x) { return x*(180.0/M_PI); }
79
point degrees (point x) { return x*(180.0/M_PI); }
80
color degrees (color x) { return x*(180.0/M_PI); }
81
float degrees (float x) { return x*(180.0/M_PI); }
82
normal radians (normal x) { return x*(M_PI/180.0); }
83
vector radians (vector x) { return x*(M_PI/180.0); }
84
point radians (point x) { return x*(M_PI/180.0); }
85
color radians (color x) { return x*(M_PI/180.0); }
86
float radians (float x) { return x*(M_PI/180.0); }
102
point log (point a, float b) { return log(a)/log(b); }
103
vector log (vector a, float b) { return log(a)/log(b); }
104
color log (color a, float b) { return log(a)/log(b); }
105
float log (float a, float b) { return log(a)/log(b); }
110
PERCOMP1 (inversesqrt)
111
float hypot (float a, float b) { return sqrt (a*a + b*b); }
112
float hypot (float a, float b, float c) { return sqrt (a*a + b*b + c*c); }
114
int abs (int x) BUILTIN;
116
int fabs (int x) BUILTIN;
126
int mod (int x, int y) BUILTIN;
129
normal clamp (normal x, normal minval, normal maxval) { return max(min(x,maxval),minval); }
130
vector clamp (vector x, vector minval, vector maxval) { return max(min(x,maxval),minval); }
131
point clamp (point x, point minval, point maxval) { return max(min(x,maxval),minval); }
132
color clamp (color x, color minval, color maxval) { return max(min(x,maxval),minval); }
133
float clamp (float x, float minval, float maxval) { return max(min(x,maxval),minval); }
134
//normal clamp (normal x, normal minval, normal maxval) BUILTIN;
135
//vector clamp (vector x, vector minval, vector maxval) BUILTIN;
136
//point clamp (point x, point minval, point maxval) BUILTIN;
137
//color clamp (color x, color minval, color maxval) BUILTIN;
138
//float clamp (float x, float minval, float maxval) BUILTIN;
139
normal mix (normal x, normal y, normal a) { return x*(1-a) + y*a; }
140
normal mix (normal x, normal y, float a) { return x*(1-a) + y*a; }
141
vector mix (vector x, vector y, vector a) { return x*(1-a) + y*a; }
142
vector mix (vector x, vector y, float a) { return x*(1-a) + y*a; }
143
point mix (point x, point y, point a) { return x*(1-a) + y*a; }
144
point mix (point x, point y, float a) { return x*(1-a) + y*a; }
145
color mix (color x, color y, color a) { return x*(1-a) + y*a; }
146
color mix (color x, color y, float a) { return x*(1-a) + y*a; }
147
float mix (float x, float y, float a) { return x*(1-a) + y*a; }
148
int isnan (float x) BUILTIN;
149
int isinf (float x) BUILTIN;
150
int isfinite (float x) BUILTIN;
151
float erf (float x) BUILTIN;
152
float erfc (float x) BUILTIN;
156
vector cross (vector a, vector b) BUILTIN;
157
float dot (vector a, vector b) BUILTIN;
158
float length (vector v) BUILTIN;
159
float distance (point a, point b) BUILTIN;
160
float distance (point a, point b, point q) BUILTIN;
161
normal normalize (normal v) BUILTIN;
162
vector normalize (vector v) BUILTIN;
163
vector faceforward (vector N, vector I, vector Nref) BUILTIN;
164
vector faceforward (vector N, vector I) BUILTIN;
165
vector reflect (vector I, vector N) { return I - 2*dot(N,I)*N; }
166
vector refract (vector I, vector N, float eta) {
167
float IdotN = dot (I, N);
168
float k = 1 - eta*eta * (1 - IdotN*IdotN);
169
return (k < 0) ? vector(0,0,0) : (eta*I - N * (eta*IdotN + sqrt(k)));
171
void fresnel (vector I, normal N, float eta,
172
output float Kr, output float Kt,
173
output vector R, output vector T)
175
float sqr(float x) { return x*x; }
180
float g = 1.0 / sqr(eta) - 1.0 + c * c;
184
float F = (c * (g+c) - 1.0) / (c * beta + 1.0);
185
F = 0.5 * (1.0 + sqr(F));
186
F *= sqr (beta / (g+c));
188
Kt = (1.0 - Kr) * eta*eta;
189
// OPT: the following recomputes some of the above values, but it
190
// gives us the same result as if the shader-writer called refract()
191
T = refract(I, N, eta);
193
// total internal reflection
201
void fresnel (vector I, normal N, float eta,
202
output float Kr, output float Kt)
205
fresnel(I, N, eta, Kr, Kt, R, T);
208
point rotate (point q, float angle, point a, point b) BUILTIN;
210
normal transform (matrix Mto, normal p) BUILTIN;
211
vector transform (matrix Mto, vector p) BUILTIN;
212
point transform (matrix Mto, point p) BUILTIN;
214
// Implementation of transform-with-named-space in terms of matrices:
216
point transform (string tospace, point x)
218
return transform (matrix ("common", tospace), x);
221
point transform (string fromspace, string tospace, point x)
223
return transform (matrix (fromspace, tospace), x);
227
vector transform (string tospace, vector x)
229
return transform (matrix ("common", tospace), x);
232
vector transform (string fromspace, string tospace, vector x)
234
return transform (matrix (fromspace, tospace), x);
238
normal transform (string tospace, normal x)
240
return transform (matrix ("common", tospace), x);
243
normal transform (string fromspace, string tospace, normal x)
245
return transform (matrix (fromspace, tospace), x);
248
float transformu (string tounits, float x) BUILTIN;
249
float transformu (string fromunits, string tounits, float x) BUILTIN;
254
float luminance (color c) {
255
return dot ((vector)c, vector(0.2126, 0.7152, 0.0722));
260
color transformc (string to, color x)
262
color rgb_to_hsv (color rgb) { // See Foley & van Dam
263
float r = rgb[0], g = rgb[1], b = rgb[2];
264
float mincomp = min (r, min (g, b));
265
float maxcomp = max (r, max (g, b));
266
float delta = maxcomp - mincomp; // chroma
275
if (r >= maxcomp) h = (g-b) / delta;
276
else if (g >= maxcomp) h = 2 + (b-r) / delta;
277
else h = 4 + (r-g) / delta;
282
return color (h, s, v);
285
color rgb_to_hsl (color rgb) { // See Foley & van Dam
286
// First convert rgb to hsv, then to hsl
287
float minval = min (rgb[0], min (rgb[1], rgb[2]));
288
color hsv = rgb_to_hsv (rgb);
289
float maxval = hsv[2]; // v == maxval
290
float h = hsv[0], s, l = (minval+maxval) / 2;
291
if (minval == maxval)
292
s = 0; // special 'achromatic' case, hue is 0
294
s = (maxval - minval) / (maxval + minval);
296
s = (maxval - minval) / (2 - maxval - minval);
297
return color (h, s, l);
301
if (to == "rgb" || to == "RGB")
303
else if (to == "hsv")
305
else if (to == "hsl")
307
else if (to == "YIQ")
308
r = color (dot (vector(0.299, 0.587, 0.114), (vector)x),
309
dot (vector(0.596, -0.275, -0.321), (vector)x),
310
dot (vector(0.212, -0.523, 0.311), (vector)x));
311
else if (to == "xyz")
312
r = color (dot (vector(0.412453, 0.357580, 0.180423), (vector)x),
313
dot (vector(0.212671, 0.715160, 0.072169), (vector)x),
314
dot (vector(0.019334, 0.119193, 0.950227), (vector)x));
316
error ("Unknown color space \"%s\"", to);
323
color transformc (string from, string to, color x)
325
color hsv_to_rgb (color c) { // Reference: Foley & van Dam
326
float h = c[0], s = c[1], v = c[2];
331
h = 6 * (h - floor(h)); // expand to [0..6)
335
float q = v * (1-s*f);
336
float t = v * (1-s*(1-f));
337
if (hi == 0) r = color (v, t, p);
338
else if (hi == 1) r = color (q, v, p);
339
else if (hi == 2) r = color (p, v, t);
340
else if (hi == 3) r = color (p, q, v);
341
else if (hi == 4) r = color (t, p, v);
342
else r = color (v, p, q);
347
color hsl_to_rgb (color c) {
348
float h = c[0], s = c[1], l = c[2];
349
// Easiest to convert hsl -> hsv, then hsv -> RGB (per Foley & van Dam)
350
float v = (l <= 0.5) ? (l * (1 + s)) : (l * (1 - s) + s);
355
float min = 2 * l - v;
357
r = hsv_to_rgb (color (h, s, v));
363
if (from == "rgb" || from == "RGB")
365
else if (from == "hsv")
367
else if (from == "hsl")
369
else if (from == "YIQ")
370
r = color (dot (vector(1, 0.9557, 0.6199), (vector)x),
371
dot (vector(1, -0.2716, -0.6469), (vector)x),
372
dot (vector(1, -1.1082, 1.7051), (vector)x));
373
else if (from == "xyz")
374
r = color (dot (vector( 3.240479, -1.537150, -0.498535), (vector)x),
375
dot (vector(-0.969256, 1.875991, 0.041556), (vector)x),
376
dot (vector( 0.055648, -0.204043, 1.057311), (vector)x));
378
error ("Unknown color space \"%s\"", to);
381
return transformc (to, r);
388
float determinant (matrix m) BUILTIN;
389
matrix transpose (matrix m) BUILTIN;
393
// Pattern generation
395
float step (float edge, float x) BUILTIN;
396
color step (color edge, color x) BUILTIN;
397
point step (point edge, point x) BUILTIN;
398
vector step (vector edge, vector x) BUILTIN;
399
normal step (normal edge, normal x) BUILTIN;
400
float smoothstep (float edge0, float edge1, float x) BUILTIN;
403
// Derivatives and area operators
406
// Displacement functions
411
int strlen (string s) BUILTIN;
412
int startswith (string s, string prefix) BUILTIN;
413
int endswith (string s, string suffix) BUILTIN;
414
string substr (string s, int start, int len) BUILTIN;
415
string substr (string s, int start) { return substr (s, start, strlen(s)); }
417
// Define concat in terms of shorter concat
418
string concat (string a, string b, string c) {
419
return concat(concat(a,b), c);
421
string concat (string a, string b, string c, string d) {
422
return concat(concat(a,b,c), d);
424
string concat (string a, string b, string c, string d, string e) {
425
return concat(concat(a,b,c,d), e);
427
string concat (string a, string b, string c, string d, string e, string f) {
428
return concat(concat(a,b,c,d,e), f);
437
closure color diffuse(normal N) BUILTIN;
438
closure color oren_nayar(normal N, float sigma) BUILTIN;
439
closure color translucent(normal N) BUILTIN;
440
closure color reflection(normal N, float eta) BUILTIN;
441
closure color reflection(normal N) { return reflection (N, 0.0); }
442
closure color refraction(normal N, float eta) BUILTIN;
443
closure color dielectric(normal N, float eta) BUILTIN;
444
closure color transparent() BUILTIN;
445
closure color microfacet_ggx(normal N, float ag) BUILTIN;
446
closure color microfacet_ggx_refraction(normal N, float ag, float eta) BUILTIN;
447
closure color microfacet_beckmann(normal N, float ab) BUILTIN;
448
closure color microfacet_beckmann_refraction(normal N, float ab, float eta) BUILTIN;
449
closure color ward(normal N, vector T,float ax, float ay) BUILTIN;
450
closure color ashikhmin_velvet(normal N, float sigma) BUILTIN;
451
closure color westin_backscatter(normal N, float roughness) BUILTIN;
452
closure color westin_sheen(normal N, float edginess) BUILTIN;
453
closure color bssrdf_cubic(color radius) BUILTIN;
454
closure color emission(float inner_angle, float outer_angle) BUILTIN;
455
closure color emission(float outer_angle) BUILTIN;
456
closure color emission() BUILTIN;
457
closure color debug(string tag) BUILTIN;
458
closure color background() BUILTIN;
459
closure color holdout() BUILTIN;
460
closure color subsurface(float eta, float g, float mfp, float albedo) BUILTIN;
463
int raytype (string typename) BUILTIN;
471
#endif /* CCL_STDOSL_H */