~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to intern/cycles/kernel/osl/nodes/stdosl.h

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/////////////////////////////////////////////////////////////////////////////
2
 
// Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.  All Rights Reserved.
3
 
//
4
 
// Redistribution and use in source and binary forms, with or without
5
 
// modification, are permitted provided that the following conditions are
6
 
// met:
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
 
/////////////////////////////////////////////////////////////////////////////
27
 
 
28
 
 
29
 
#ifndef CCL_STDOSL_H
30
 
#define CCL_STDOSL_H
31
 
 
32
 
 
33
 
#ifndef M_PI
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) */
46
 
#endif
47
 
 
48
 
 
49
 
 
50
 
// Declaration of built-in functions and closures
51
 
#define BUILTIN [[ int builtin = 1 ]]
52
 
#define BUILTIN_DERIV [[ int builtin = 1, int deriv = 1 ]]
53
 
 
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;
60
 
 
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;
67
 
 
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;
74
 
 
75
 
 
76
 
// Basic math
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); }
87
 
PERCOMP1 (cos)
88
 
PERCOMP1 (sin)
89
 
PERCOMP1 (tan)
90
 
PERCOMP1 (acos)
91
 
PERCOMP1 (asin)
92
 
PERCOMP1 (atan)
93
 
PERCOMP2 (atan2)
94
 
PERCOMP1 (cosh)
95
 
PERCOMP1 (sinh)
96
 
PERCOMP1 (tanh)
97
 
PERCOMP2F (pow)
98
 
PERCOMP1 (exp)
99
 
PERCOMP1 (exp2)
100
 
PERCOMP1 (expm1)
101
 
PERCOMP1 (log)
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); }
106
 
PERCOMP1 (log2)
107
 
PERCOMP1 (log10)
108
 
PERCOMP1 (logb)
109
 
PERCOMP1 (sqrt)
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); }
113
 
PERCOMP1 (abs)
114
 
int abs (int x) BUILTIN;
115
 
PERCOMP1 (fabs)
116
 
int fabs (int x) BUILTIN;
117
 
PERCOMP1 (sign)
118
 
PERCOMP1 (floor)
119
 
PERCOMP1 (ceil)
120
 
PERCOMP1 (round)
121
 
PERCOMP1 (trunc)
122
 
PERCOMP2 (fmod)
123
 
PERCOMP2F (fmod)
124
 
PERCOMP2 (mod)
125
 
PERCOMP2F (mod)
126
 
int    mod (int x, int y) BUILTIN;
127
 
PERCOMP2 (min)
128
 
PERCOMP2 (max)
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;
153
 
 
154
 
// Vector functions
155
 
 
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)));
170
 
}
171
 
void fresnel (vector I, normal N, float eta,
172
 
              output float Kr, output float Kt,
173
 
              output vector R, output vector T)
174
 
{
175
 
    float sqr(float x) { return x*x; }
176
 
    float c = dot(I, N);
177
 
    if (c < 0)
178
 
        c = -c;
179
 
    R = reflect(I, N);
180
 
    float g = 1.0 / sqr(eta) - 1.0 + c * c;
181
 
    if (g >= 0.0) {
182
 
        g = sqrt (g);
183
 
        float beta = g - 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));
187
 
        Kr = F;
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);
192
 
    } else {
193
 
        // total internal reflection
194
 
        Kr = 1.0;
195
 
        Kt = 0.0;
196
 
        T = vector (0,0,0);
197
 
    }
198
 
#undef sqr
199
 
}
200
 
 
201
 
void fresnel (vector I, normal N, float eta,
202
 
              output float Kr, output float Kt)
203
 
{
204
 
    vector R, T;
205
 
    fresnel(I, N, eta, Kr, Kt, R, T);
206
 
}
207
 
 
208
 
point rotate (point q, float angle, point a, point b) BUILTIN;
209
 
 
210
 
normal transform (matrix Mto, normal p) BUILTIN;
211
 
vector transform (matrix Mto, vector p) BUILTIN;
212
 
point transform (matrix Mto, point p) BUILTIN;
213
 
 
214
 
// Implementation of transform-with-named-space in terms of matrices:
215
 
 
216
 
point transform (string tospace, point x)
217
 
{
218
 
    return transform (matrix ("common", tospace), x);
219
 
}
220
 
 
221
 
point transform (string fromspace, string tospace, point x)
222
 
{
223
 
    return transform (matrix (fromspace, tospace), x);
224
 
}
225
 
 
226
 
 
227
 
vector transform (string tospace, vector x)
228
 
{
229
 
    return transform (matrix ("common", tospace), x);
230
 
}
231
 
 
232
 
vector transform (string fromspace, string tospace, vector x)
233
 
{
234
 
    return transform (matrix (fromspace, tospace), x);
235
 
}
236
 
 
237
 
 
238
 
normal transform (string tospace, normal x)
239
 
{
240
 
    return transform (matrix ("common", tospace), x);
241
 
}
242
 
 
243
 
normal transform (string fromspace, string tospace, normal x)
244
 
{
245
 
    return transform (matrix (fromspace, tospace), x);
246
 
}
247
 
 
248
 
float transformu (string tounits, float x) BUILTIN;
249
 
float transformu (string fromunits, string tounits, float x) BUILTIN;
250
 
 
251
 
 
252
 
// Color functions
253
 
 
254
 
float luminance (color c) {
255
 
    return dot ((vector)c, vector(0.2126, 0.7152, 0.0722));
256
 
}
257
 
 
258
 
 
259
 
 
260
 
color transformc (string to, color x)
261
 
{
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
267
 
        float h, s, v;
268
 
        v = maxcomp;
269
 
        if (maxcomp > 0)
270
 
            s = delta / maxcomp;
271
 
        else s = 0;
272
 
        if (s <= 0)
273
 
            h = 0;
274
 
        else {
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;
278
 
            h /= 6;
279
 
            if (h < 0)
280
 
                h += 1;
281
 
        }
282
 
        return color (h, s, v);
283
 
    }
284
 
 
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
293
 
        else if (l <= 0.5)
294
 
            s = (maxval - minval) / (maxval + minval);
295
 
        else
296
 
            s = (maxval - minval) / (2 - maxval - minval);
297
 
        return color (h, s, l);
298
 
    }
299
 
 
300
 
    color r;
301
 
    if (to == "rgb" || to == "RGB")
302
 
        r = x;
303
 
    else if (to == "hsv")
304
 
        r = rgb_to_hsv (x);
305
 
    else if (to == "hsl")
306
 
        r = rgb_to_hsl (x);
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));
315
 
    else {
316
 
        error ("Unknown color space \"%s\"", to);
317
 
        r = x;
318
 
    }
319
 
    return r;
320
 
}
321
 
 
322
 
 
323
 
color transformc (string from, string to, color x)
324
 
{
325
 
    color hsv_to_rgb (color c) { // Reference: Foley & van Dam
326
 
        float h = c[0], s = c[1], v = c[2];
327
 
        color r;
328
 
        if (s < 0.0001) {
329
 
            r = v;
330
 
        } else {
331
 
            h = 6 * (h - floor(h));  // expand to [0..6)
332
 
            int hi = (int)h;
333
 
            float f = h - hi;
334
 
            float p = v * (1-s);
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);
343
 
        }
344
 
        return r;
345
 
    }
346
 
 
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);
351
 
        color r;
352
 
        if (v <= 0) {
353
 
            r = 0;
354
 
        } else {
355
 
            float min = 2 * l - v;
356
 
            s = (v - min) / v;
357
 
            r = hsv_to_rgb (color (h, s, v));
358
 
        }
359
 
        return r;
360
 
    }
361
 
 
362
 
    color r;
363
 
    if (from == "rgb" || from == "RGB")
364
 
        r = x;
365
 
    else if (from == "hsv")
366
 
        r = hsv_to_rgb (x);
367
 
    else if (from == "hsl")
368
 
        r = hsl_to_rgb (x);
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));
377
 
    else {
378
 
        error ("Unknown color space \"%s\"", to);
379
 
        r = x;
380
 
    }
381
 
    return transformc (to, r);
382
 
}
383
 
 
384
 
 
385
 
 
386
 
// Matrix functions
387
 
 
388
 
float determinant (matrix m) BUILTIN;
389
 
matrix transpose (matrix m) BUILTIN;
390
 
 
391
 
 
392
 
 
393
 
// Pattern generation
394
 
 
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;
401
 
 
402
 
 
403
 
// Derivatives and area operators
404
 
 
405
 
 
406
 
// Displacement functions
407
 
 
408
 
 
409
 
// String functions
410
 
 
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)); }
416
 
 
417
 
// Define concat in terms of shorter concat
418
 
string concat (string a, string b, string c) {
419
 
    return concat(concat(a,b), c);
420
 
}
421
 
string concat (string a, string b, string c, string d) {
422
 
    return concat(concat(a,b,c), d);
423
 
}
424
 
string concat (string a, string b, string c, string d, string e) {
425
 
    return concat(concat(a,b,c,d), e);
426
 
}
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);
429
 
}
430
 
 
431
 
 
432
 
// Texture
433
 
 
434
 
 
435
 
// Closures
436
 
 
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;
461
 
 
462
 
// Renderer state
463
 
int raytype (string typename) BUILTIN;
464
 
 
465
 
#undef BUILTIN
466
 
#undef BUILTIN_DERIV
467
 
#undef PERCOMP1
468
 
#undef PERCOMP2
469
 
#undef PERCOMP2F
470
 
 
471
 
#endif /* CCL_STDOSL_H */
472