stdosl.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. #ifndef CCL_STDOSL_H
  28. #define CCL_STDOSL_H
  29. #ifndef M_PI
  30. # define M_PI 3.1415926535897932 /* pi */
  31. # define M_PI_2 1.5707963267948966 /* pi/2 */
  32. # define M_PI_4 0.7853981633974483 /* pi/4 */
  33. # define M_2_PI 0.6366197723675813 /* 2/pi */
  34. # define M_2PI 6.2831853071795865 /* 2*pi */
  35. # define M_4PI 12.566370614359173 /* 4*pi */
  36. # define M_2_SQRTPI 1.1283791670955126 /* 2/sqrt(pi) */
  37. # define M_E 2.7182818284590452 /* e (Euler's number) */
  38. # define M_LN2 0.6931471805599453 /* ln(2) */
  39. # define M_LN10 2.3025850929940457 /* ln(10) */
  40. # define M_LOG2E 1.4426950408889634 /* log_2(e) */
  41. # define M_LOG10E 0.4342944819032518 /* log_10(e) */
  42. # define M_SQRT2 1.4142135623730950 /* sqrt(2) */
  43. # define M_SQRT1_2 0.7071067811865475 /* 1/sqrt(2) */
  44. #endif
  45. // Declaration of built-in functions and closures
  46. #define BUILTIN [[int builtin = 1]]
  47. #define BUILTIN_DERIV [[ int builtin = 1, int deriv = 1 ]]
  48. #define PERCOMP1(name) \
  49. normal name(normal x) BUILTIN; \
  50. vector name(vector x) BUILTIN; \
  51. point name(point x) BUILTIN; \
  52. color name(color x) BUILTIN; \
  53. float name(float x) BUILTIN;
  54. #define PERCOMP2(name) \
  55. normal name(normal x, normal y) BUILTIN; \
  56. vector name(vector x, vector y) BUILTIN; \
  57. point name(point x, point y) BUILTIN; \
  58. color name(color x, color y) BUILTIN; \
  59. float name(float x, float y) BUILTIN;
  60. #define PERCOMP2F(name) \
  61. normal name(normal x, float y) BUILTIN; \
  62. vector name(vector x, float y) BUILTIN; \
  63. point name(point x, float y) BUILTIN; \
  64. color name(color x, float y) BUILTIN; \
  65. float name(float x, float y) BUILTIN;
  66. // Basic math
  67. normal degrees(normal x)
  68. {
  69. return x * (180.0 / M_PI);
  70. }
  71. vector degrees(vector x)
  72. {
  73. return x * (180.0 / M_PI);
  74. }
  75. point degrees(point x)
  76. {
  77. return x * (180.0 / M_PI);
  78. }
  79. color degrees(color x)
  80. {
  81. return x * (180.0 / M_PI);
  82. }
  83. float degrees(float x)
  84. {
  85. return x * (180.0 / M_PI);
  86. }
  87. normal radians(normal x)
  88. {
  89. return x * (M_PI / 180.0);
  90. }
  91. vector radians(vector x)
  92. {
  93. return x * (M_PI / 180.0);
  94. }
  95. point radians(point x)
  96. {
  97. return x * (M_PI / 180.0);
  98. }
  99. color radians(color x)
  100. {
  101. return x * (M_PI / 180.0);
  102. }
  103. float radians(float x)
  104. {
  105. return x * (M_PI / 180.0);
  106. }
  107. PERCOMP1(cos)
  108. PERCOMP1(sin)
  109. PERCOMP1(tan)
  110. PERCOMP1(acos)
  111. PERCOMP1(asin)
  112. PERCOMP1(atan)
  113. PERCOMP2(atan2)
  114. PERCOMP1(cosh)
  115. PERCOMP1(sinh)
  116. PERCOMP1(tanh)
  117. PERCOMP2F(pow)
  118. PERCOMP1(exp)
  119. PERCOMP1(exp2)
  120. PERCOMP1(expm1)
  121. PERCOMP1(log)
  122. point log(point a, float b)
  123. {
  124. return log(a) / log(b);
  125. }
  126. vector log(vector a, float b)
  127. {
  128. return log(a) / log(b);
  129. }
  130. color log(color a, float b)
  131. {
  132. return log(a) / log(b);
  133. }
  134. float log(float a, float b)
  135. {
  136. return log(a) / log(b);
  137. }
  138. PERCOMP1(log2)
  139. PERCOMP1(log10)
  140. PERCOMP1(logb)
  141. PERCOMP1(sqrt)
  142. PERCOMP1(inversesqrt)
  143. float hypot(float a, float b)
  144. {
  145. return sqrt(a * a + b * b);
  146. }
  147. float hypot(float a, float b, float c)
  148. {
  149. return sqrt(a * a + b * b + c * c);
  150. }
  151. PERCOMP1(abs)
  152. int abs(int x) BUILTIN;
  153. PERCOMP1(fabs)
  154. int fabs(int x) BUILTIN;
  155. PERCOMP1(sign)
  156. PERCOMP1(floor)
  157. PERCOMP1(ceil)
  158. PERCOMP1(round)
  159. PERCOMP1(trunc)
  160. PERCOMP2(fmod)
  161. PERCOMP2F(fmod)
  162. int mod(int a, int b)
  163. {
  164. return a - b * (int)floor(a / b);
  165. }
  166. point mod(point a, point b)
  167. {
  168. return a - b * floor(a / b);
  169. }
  170. vector mod(vector a, vector b)
  171. {
  172. return a - b * floor(a / b);
  173. }
  174. normal mod(normal a, normal b)
  175. {
  176. return a - b * floor(a / b);
  177. }
  178. color mod(color a, color b)
  179. {
  180. return a - b * floor(a / b);
  181. }
  182. point mod(point a, float b)
  183. {
  184. return a - b * floor(a / b);
  185. }
  186. vector mod(vector a, float b)
  187. {
  188. return a - b * floor(a / b);
  189. }
  190. normal mod(normal a, float b)
  191. {
  192. return a - b * floor(a / b);
  193. }
  194. color mod(color a, float b)
  195. {
  196. return a - b * floor(a / b);
  197. }
  198. float mod(float a, float b)
  199. {
  200. return a - b * floor(a / b);
  201. }
  202. PERCOMP2(min)
  203. int min(int a, int b) BUILTIN;
  204. PERCOMP2(max)
  205. int max(int a, int b) BUILTIN;
  206. normal clamp(normal x, normal minval, normal maxval)
  207. {
  208. return max(min(x, maxval), minval);
  209. }
  210. vector clamp(vector x, vector minval, vector maxval)
  211. {
  212. return max(min(x, maxval), minval);
  213. }
  214. point clamp(point x, point minval, point maxval)
  215. {
  216. return max(min(x, maxval), minval);
  217. }
  218. color clamp(color x, color minval, color maxval)
  219. {
  220. return max(min(x, maxval), minval);
  221. }
  222. float clamp(float x, float minval, float maxval)
  223. {
  224. return max(min(x, maxval), minval);
  225. }
  226. int clamp(int x, int minval, int maxval)
  227. {
  228. return max(min(x, maxval), minval);
  229. }
  230. #if 0
  231. normal mix(normal x, normal y, normal a)
  232. {
  233. return x * (1 - a) + y * a;
  234. }
  235. normal mix(normal x, normal y, float a)
  236. {
  237. return x * (1 - a) + y * a;
  238. }
  239. vector mix(vector x, vector y, vector a)
  240. {
  241. return x * (1 - a) + y * a;
  242. }
  243. vector mix(vector x, vector y, float a)
  244. {
  245. return x * (1 - a) + y * a;
  246. }
  247. point mix(point x, point y, point a)
  248. {
  249. return x * (1 - a) + y * a;
  250. }
  251. point mix(point x, point y, float a)
  252. {
  253. return x * (1 - a) + y * a;
  254. }
  255. color mix(color x, color y, color a)
  256. {
  257. return x * (1 - a) + y * a;
  258. }
  259. color mix(color x, color y, float a)
  260. {
  261. return x * (1 - a) + y * a;
  262. }
  263. float mix(float x, float y, float a)
  264. {
  265. return x * (1 - a) + y * a;
  266. }
  267. #else
  268. normal mix(normal x, normal y, normal a) BUILTIN;
  269. normal mix(normal x, normal y, float a) BUILTIN;
  270. vector mix(vector x, vector y, vector a) BUILTIN;
  271. vector mix(vector x, vector y, float a) BUILTIN;
  272. point mix(point x, point y, point a) BUILTIN;
  273. point mix(point x, point y, float a) BUILTIN;
  274. color mix(color x, color y, color a) BUILTIN;
  275. color mix(color x, color y, float a) BUILTIN;
  276. float mix(float x, float y, float a) BUILTIN;
  277. #endif
  278. int isnan(float x) BUILTIN;
  279. int isinf(float x) BUILTIN;
  280. int isfinite(float x) BUILTIN;
  281. float erf(float x) BUILTIN;
  282. float erfc(float x) BUILTIN;
  283. // Vector functions
  284. vector cross(vector a, vector b) BUILTIN;
  285. float dot(vector a, vector b) BUILTIN;
  286. float length(vector v) BUILTIN;
  287. float distance(point a, point b) BUILTIN;
  288. float distance(point a, point b, point q)
  289. {
  290. vector d = b - a;
  291. float dd = dot(d, d);
  292. if (dd == 0.0)
  293. return distance(q, a);
  294. float t = dot(q - a, d) / dd;
  295. return distance(q, a + clamp(t, 0.0, 1.0) * d);
  296. }
  297. normal normalize(normal v) BUILTIN;
  298. vector normalize(vector v) BUILTIN;
  299. vector faceforward(vector N, vector I, vector Nref) BUILTIN;
  300. vector faceforward(vector N, vector I) BUILTIN;
  301. vector reflect(vector I, vector N)
  302. {
  303. return I - 2 * dot(N, I) * N;
  304. }
  305. vector refract(vector I, vector N, float eta)
  306. {
  307. float IdotN = dot(I, N);
  308. float k = 1 - eta * eta * (1 - IdotN * IdotN);
  309. return (k < 0) ? vector(0, 0, 0) : (eta * I - N * (eta * IdotN + sqrt(k)));
  310. }
  311. void fresnel(vector I,
  312. normal N,
  313. float eta,
  314. output float Kr,
  315. output float Kt,
  316. output vector R,
  317. output vector T)
  318. {
  319. float sqr(float x)
  320. {
  321. return x * x;
  322. }
  323. float c = dot(I, N);
  324. if (c < 0)
  325. c = -c;
  326. R = reflect(I, N);
  327. float g = 1.0 / sqr(eta) - 1.0 + c * c;
  328. if (g >= 0.0) {
  329. g = sqrt(g);
  330. float beta = g - c;
  331. float F = (c * (g + c) - 1.0) / (c * beta + 1.0);
  332. F = 0.5 * (1.0 + sqr(F));
  333. F *= sqr(beta / (g + c));
  334. Kr = F;
  335. Kt = (1.0 - Kr) * eta * eta;
  336. // OPT: the following recomputes some of the above values, but it
  337. // gives us the same result as if the shader-writer called refract()
  338. T = refract(I, N, eta);
  339. }
  340. else {
  341. // total internal reflection
  342. Kr = 1.0;
  343. Kt = 0.0;
  344. T = vector(0, 0, 0);
  345. }
  346. }
  347. void fresnel(vector I, normal N, float eta, output float Kr, output float Kt)
  348. {
  349. vector R, T;
  350. fresnel(I, N, eta, Kr, Kt, R, T);
  351. }
  352. normal transform(matrix Mto, normal p) BUILTIN;
  353. vector transform(matrix Mto, vector p) BUILTIN;
  354. point transform(matrix Mto, point p) BUILTIN;
  355. normal transform(string from, string to, normal p) BUILTIN;
  356. vector transform(string from, string to, vector p) BUILTIN;
  357. point transform(string from, string to, point p) BUILTIN;
  358. normal transform(string to, normal p)
  359. {
  360. return transform("common", to, p);
  361. }
  362. vector transform(string to, vector p)
  363. {
  364. return transform("common", to, p);
  365. }
  366. point transform(string to, point p)
  367. {
  368. return transform("common", to, p);
  369. }
  370. float transformu(string tounits, float x) BUILTIN;
  371. float transformu(string fromunits, string tounits, float x) BUILTIN;
  372. point rotate(point p, float angle, point a, point b)
  373. {
  374. vector axis = normalize(b - a);
  375. float cosang, sinang;
  376. /* Older OSX has major issues with sincos() function,
  377. * it's likely a big in OSL or LLVM. For until we've
  378. * updated to new versions of this libraries we'll
  379. * use a workaround to prevent possible crashes on all
  380. * the platforms.
  381. *
  382. * Shouldn't be that bad because it's mainly used for
  383. * anisotropic shader where angle is usually constant.
  384. */
  385. #if 0
  386. sincos(angle, sinang, cosang);
  387. #else
  388. sinang = sin(angle);
  389. cosang = cos(angle);
  390. #endif
  391. float cosang1 = 1.0 - cosang;
  392. float x = axis[0], y = axis[1], z = axis[2];
  393. matrix M = matrix(x * x + (1.0 - x * x) * cosang,
  394. x * y * cosang1 + z * sinang,
  395. x * z * cosang1 - y * sinang,
  396. 0.0,
  397. x * y * cosang1 - z * sinang,
  398. y * y + (1.0 - y * y) * cosang,
  399. y * z * cosang1 + x * sinang,
  400. 0.0,
  401. x * z * cosang1 + y * sinang,
  402. y * z * cosang1 - x * sinang,
  403. z * z + (1.0 - z * z) * cosang,
  404. 0.0,
  405. 0.0,
  406. 0.0,
  407. 0.0,
  408. 1.0);
  409. return transform(M, p - a) + a;
  410. }
  411. normal ensure_valid_reflection(normal Ng, vector I, normal N)
  412. {
  413. /* The implementation here mirrors the one in kernel_montecarlo.h,
  414. * check there for an explanation of the algorithm. */
  415. float sqr(float x)
  416. {
  417. return x * x;
  418. }
  419. vector R = 2 * dot(N, I) * N - I;
  420. float threshold = min(0.9 * dot(Ng, I), 0.01);
  421. if (dot(Ng, R) >= threshold) {
  422. return N;
  423. }
  424. float NdotNg = dot(N, Ng);
  425. vector X = normalize(N - NdotNg * Ng);
  426. float Ix = dot(I, X), Iz = dot(I, Ng);
  427. float Ix2 = sqr(Ix), Iz2 = sqr(Iz);
  428. float a = Ix2 + Iz2;
  429. float b = sqrt(Ix2 * (a - sqr(threshold)));
  430. float c = Iz * threshold + a;
  431. float fac = 0.5 / a;
  432. float N1_z2 = fac * (b + c), N2_z2 = fac * (-b + c);
  433. int valid1 = (N1_z2 > 1e-5) && (N1_z2 <= (1.0 + 1e-5));
  434. int valid2 = (N2_z2 > 1e-5) && (N2_z2 <= (1.0 + 1e-5));
  435. float N_new_x, N_new_z;
  436. if (valid1 && valid2) {
  437. float N1_x = sqrt(1.0 - N1_z2), N1_z = sqrt(N1_z2);
  438. float N2_x = sqrt(1.0 - N2_z2), N2_z = sqrt(N2_z2);
  439. float R1 = 2 * (N1_x * Ix + N1_z * Iz) * N1_z - Iz;
  440. float R2 = 2 * (N2_x * Ix + N2_z * Iz) * N2_z - Iz;
  441. valid1 = (R1 >= 1e-5);
  442. valid2 = (R2 >= 1e-5);
  443. if (valid1 && valid2) {
  444. N_new_x = (R1 < R2) ? N1_x : N2_x;
  445. N_new_z = (R1 < R2) ? N1_z : N2_z;
  446. }
  447. else {
  448. N_new_x = (R1 > R2) ? N1_x : N2_x;
  449. N_new_z = (R1 > R2) ? N1_z : N2_z;
  450. }
  451. }
  452. else if (valid1 || valid2) {
  453. float Nz2 = valid1 ? N1_z2 : N2_z2;
  454. N_new_x = sqrt(1.0 - Nz2);
  455. N_new_z = sqrt(Nz2);
  456. }
  457. else {
  458. return Ng;
  459. }
  460. return N_new_x * X + N_new_z * Ng;
  461. }
  462. // Color functions
  463. float luminance(color c) BUILTIN;
  464. color blackbody(float temperatureK) BUILTIN;
  465. color wavelength_color(float wavelength_nm) BUILTIN;
  466. color transformc(string to, color x)
  467. {
  468. color rgb_to_hsv(color rgb)
  469. { // See Foley & van Dam
  470. float r = rgb[0], g = rgb[1], b = rgb[2];
  471. float mincomp = min(r, min(g, b));
  472. float maxcomp = max(r, max(g, b));
  473. float delta = maxcomp - mincomp; // chroma
  474. float h, s, v;
  475. v = maxcomp;
  476. if (maxcomp > 0)
  477. s = delta / maxcomp;
  478. else
  479. s = 0;
  480. if (s <= 0)
  481. h = 0;
  482. else {
  483. if (r >= maxcomp)
  484. h = (g - b) / delta;
  485. else if (g >= maxcomp)
  486. h = 2 + (b - r) / delta;
  487. else
  488. h = 4 + (r - g) / delta;
  489. h /= 6;
  490. if (h < 0)
  491. h += 1;
  492. }
  493. return color(h, s, v);
  494. }
  495. color rgb_to_hsl(color rgb)
  496. { // See Foley & van Dam
  497. // First convert rgb to hsv, then to hsl
  498. float minval = min(rgb[0], min(rgb[1], rgb[2]));
  499. color hsv = rgb_to_hsv(rgb);
  500. float maxval = hsv[2]; // v == maxval
  501. float h = hsv[0], s, l = (minval + maxval) / 2;
  502. if (minval == maxval)
  503. s = 0; // special 'achromatic' case, hue is 0
  504. else if (l <= 0.5)
  505. s = (maxval - minval) / (maxval + minval);
  506. else
  507. s = (maxval - minval) / (2 - maxval - minval);
  508. return color(h, s, l);
  509. }
  510. color r;
  511. if (to == "rgb" || to == "RGB")
  512. r = x;
  513. else if (to == "hsv")
  514. r = rgb_to_hsv(x);
  515. else if (to == "hsl")
  516. r = rgb_to_hsl(x);
  517. else if (to == "YIQ")
  518. r = color(dot(vector(0.299, 0.587, 0.114), (vector)x),
  519. dot(vector(0.596, -0.275, -0.321), (vector)x),
  520. dot(vector(0.212, -0.523, 0.311), (vector)x));
  521. else if (to == "XYZ")
  522. r = color(dot(vector(0.412453, 0.357580, 0.180423), (vector)x),
  523. dot(vector(0.212671, 0.715160, 0.072169), (vector)x),
  524. dot(vector(0.019334, 0.119193, 0.950227), (vector)x));
  525. else {
  526. error("Unknown color space \"%s\"", to);
  527. r = x;
  528. }
  529. return r;
  530. }
  531. color transformc(string from, string to, color x)
  532. {
  533. color hsv_to_rgb(color c)
  534. { // Reference: Foley & van Dam
  535. float h = c[0], s = c[1], v = c[2];
  536. color r;
  537. if (s < 0.0001) {
  538. r = v;
  539. }
  540. else {
  541. h = 6 * (h - floor(h)); // expand to [0..6)
  542. int hi = (int)h;
  543. float f = h - hi;
  544. float p = v * (1 - s);
  545. float q = v * (1 - s * f);
  546. float t = v * (1 - s * (1 - f));
  547. if (hi == 0)
  548. r = color(v, t, p);
  549. else if (hi == 1)
  550. r = color(q, v, p);
  551. else if (hi == 2)
  552. r = color(p, v, t);
  553. else if (hi == 3)
  554. r = color(p, q, v);
  555. else if (hi == 4)
  556. r = color(t, p, v);
  557. else
  558. r = color(v, p, q);
  559. }
  560. return r;
  561. }
  562. color hsl_to_rgb(color c)
  563. {
  564. float h = c[0], s = c[1], l = c[2];
  565. // Easiest to convert hsl -> hsv, then hsv -> RGB (per Foley & van Dam)
  566. float v = (l <= 0.5) ? (l * (1 + s)) : (l * (1 - s) + s);
  567. color r;
  568. if (v <= 0) {
  569. r = 0;
  570. }
  571. else {
  572. float min = 2 * l - v;
  573. s = (v - min) / v;
  574. r = hsv_to_rgb(color(h, s, v));
  575. }
  576. return r;
  577. }
  578. color r;
  579. if (from == "rgb" || from == "RGB")
  580. r = x;
  581. else if (from == "hsv")
  582. r = hsv_to_rgb(x);
  583. else if (from == "hsl")
  584. r = hsl_to_rgb(x);
  585. else if (from == "YIQ")
  586. r = color(dot(vector(1, 0.9557, 0.6199), (vector)x),
  587. dot(vector(1, -0.2716, -0.6469), (vector)x),
  588. dot(vector(1, -1.1082, 1.7051), (vector)x));
  589. else if (from == "XYZ")
  590. r = color(dot(vector(3.240479, -1.537150, -0.498535), (vector)x),
  591. dot(vector(-0.969256, 1.875991, 0.041556), (vector)x),
  592. dot(vector(0.055648, -0.204043, 1.057311), (vector)x));
  593. else {
  594. error("Unknown color space \"%s\"", to);
  595. r = x;
  596. }
  597. return transformc(to, r);
  598. }
  599. // Matrix functions
  600. float determinant(matrix m) BUILTIN;
  601. matrix transpose(matrix m) BUILTIN;
  602. // Pattern generation
  603. color step(color edge, color x) BUILTIN;
  604. point step(point edge, point x) BUILTIN;
  605. vector step(vector edge, vector x) BUILTIN;
  606. normal step(normal edge, normal x) BUILTIN;
  607. float step(float edge, float x) BUILTIN;
  608. float smoothstep(float edge0, float edge1, float x) BUILTIN;
  609. float linearstep(float edge0, float edge1, float x)
  610. {
  611. float result;
  612. if (edge0 != edge1) {
  613. float xclamped = clamp(x, edge0, edge1);
  614. result = (xclamped - edge0) / (edge1 - edge0);
  615. }
  616. else { // special case: edges coincide
  617. result = step(edge0, x);
  618. }
  619. return result;
  620. }
  621. float smooth_linearstep(float edge0, float edge1, float x_, float eps_)
  622. {
  623. float result;
  624. if (edge0 != edge1) {
  625. float rampup(float x, float r)
  626. {
  627. return 0.5 / r * x * x;
  628. }
  629. float width_inv = 1.0 / (edge1 - edge0);
  630. float eps = eps_ * width_inv;
  631. float x = (x_ - edge0) * width_inv;
  632. if (x <= -eps)
  633. result = 0;
  634. else if (x >= eps && x <= 1.0 - eps)
  635. result = x;
  636. else if (x >= 1.0 + eps)
  637. result = 1;
  638. else if (x < eps)
  639. result = rampup(x + eps, 2.0 * eps);
  640. else /* if (x < 1.0+eps) */
  641. result = 1.0 - rampup(1.0 + eps - x, 2.0 * eps);
  642. }
  643. else {
  644. result = step(edge0, x_);
  645. }
  646. return result;
  647. }
  648. float aastep(float edge, float s, float dedge, float ds)
  649. {
  650. // Box filtered AA step
  651. float width = fabs(dedge) + fabs(ds);
  652. float halfwidth = 0.5 * width;
  653. float e1 = edge - halfwidth;
  654. return (s <= e1) ? 0.0 : ((s >= (edge + halfwidth)) ? 1.0 : (s - e1) / width);
  655. }
  656. float aastep(float edge, float s, float ds)
  657. {
  658. return aastep(edge, s, filterwidth(edge), ds);
  659. }
  660. float aastep(float edge, float s)
  661. {
  662. return aastep(edge, s, filterwidth(edge), filterwidth(s));
  663. }
  664. // Derivatives and area operators
  665. // Displacement functions
  666. // String functions
  667. int strlen(string s) BUILTIN;
  668. int hash(string s) BUILTIN;
  669. int getchar(string s, int index) BUILTIN;
  670. int startswith(string s, string prefix) BUILTIN;
  671. int endswith(string s, string suffix) BUILTIN;
  672. string substr(string s, int start, int len) BUILTIN;
  673. string substr(string s, int start)
  674. {
  675. return substr(s, start, strlen(s));
  676. }
  677. float stof(string str) BUILTIN;
  678. int stoi(string str) BUILTIN;
  679. // Define concat in terms of shorter concat
  680. string concat(string a, string b, string c)
  681. {
  682. return concat(concat(a, b), c);
  683. }
  684. string concat(string a, string b, string c, string d)
  685. {
  686. return concat(concat(a, b, c), d);
  687. }
  688. string concat(string a, string b, string c, string d, string e)
  689. {
  690. return concat(concat(a, b, c, d), e);
  691. }
  692. string concat(string a, string b, string c, string d, string e, string f)
  693. {
  694. return concat(concat(a, b, c, d, e), f);
  695. }
  696. // Texture
  697. // Closures
  698. closure color diffuse(normal N) BUILTIN;
  699. closure color oren_nayar(normal N, float sigma) BUILTIN;
  700. closure color diffuse_ramp(normal N, color colors[8]) BUILTIN;
  701. closure color phong_ramp(normal N, float exponent, color colors[8]) BUILTIN;
  702. closure color diffuse_toon(normal N, float size, float smooth) BUILTIN;
  703. closure color glossy_toon(normal N, float size, float smooth) BUILTIN;
  704. closure color translucent(normal N) BUILTIN;
  705. closure color reflection(normal N) BUILTIN;
  706. closure color refraction(normal N, float eta) BUILTIN;
  707. closure color transparent() BUILTIN;
  708. closure color microfacet_ggx(normal N, float ag) BUILTIN;
  709. closure color microfacet_ggx_aniso(normal N, vector T, float ax, float ay) BUILTIN;
  710. closure color microfacet_ggx_refraction(normal N, float ag, float eta) BUILTIN;
  711. closure color microfacet_multi_ggx(normal N, float ag, color C) BUILTIN;
  712. closure color microfacet_multi_ggx_aniso(normal N, vector T, float ax, float ay, color C) BUILTIN;
  713. closure color microfacet_multi_ggx_glass(normal N, float ag, float eta, color C) BUILTIN;
  714. closure color microfacet_ggx_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN;
  715. closure color microfacet_ggx_aniso_fresnel(
  716. normal N, vector T, float ax, float ay, float eta, color C, color Cspec0) BUILTIN;
  717. closure color
  718. microfacet_multi_ggx_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN;
  719. closure color microfacet_multi_ggx_aniso_fresnel(
  720. normal N, vector T, float ax, float ay, float eta, color C, color Cspec0) BUILTIN;
  721. closure color
  722. microfacet_multi_ggx_glass_fresnel(normal N, float ag, float eta, color C, color Cspec0) BUILTIN;
  723. closure color microfacet_beckmann(normal N, float ab) BUILTIN;
  724. closure color microfacet_beckmann_aniso(normal N, vector T, float ax, float ay) BUILTIN;
  725. closure color microfacet_beckmann_refraction(normal N, float ab, float eta) BUILTIN;
  726. closure color ashikhmin_shirley(normal N, vector T, float ax, float ay) BUILTIN;
  727. closure color ashikhmin_velvet(normal N, float sigma) BUILTIN;
  728. closure color emission() BUILTIN;
  729. closure color background() BUILTIN;
  730. closure color holdout() BUILTIN;
  731. closure color ambient_occlusion() BUILTIN;
  732. closure color principled_diffuse(normal N, float roughness) BUILTIN;
  733. closure color principled_sheen(normal N) BUILTIN;
  734. closure color principled_clearcoat(normal N, float clearcoat, float clearcoat_roughness) BUILTIN;
  735. // BSSRDF
  736. closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN;
  737. // Hair
  738. closure color
  739. hair_reflection(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN;
  740. closure color
  741. hair_transmission(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN;
  742. closure color principled_hair(normal N,
  743. color sigma,
  744. float roughnessu,
  745. float roughnessv,
  746. float coat,
  747. float alpha,
  748. float eta) BUILTIN;
  749. // Volume
  750. closure color henyey_greenstein(float g) BUILTIN;
  751. closure color absorption() BUILTIN;
  752. // OSL 1.5 Microfacet functions
  753. closure color microfacet(
  754. string distribution, normal N, vector U, float xalpha, float yalpha, float eta, int refract)
  755. {
  756. /* GGX */
  757. if (distribution == "ggx" || distribution == "default") {
  758. if (!refract) {
  759. if (xalpha == yalpha) {
  760. /* Isotropic */
  761. return microfacet_ggx(N, xalpha);
  762. }
  763. else {
  764. /* Anisotropic */
  765. return microfacet_ggx_aniso(N, U, xalpha, yalpha);
  766. }
  767. }
  768. else {
  769. return microfacet_ggx_refraction(N, xalpha, eta);
  770. }
  771. }
  772. /* Beckmann */
  773. else {
  774. if (!refract) {
  775. if (xalpha == yalpha) {
  776. /* Isotropic */
  777. return microfacet_beckmann(N, xalpha);
  778. }
  779. else {
  780. /* Anisotropic */
  781. return microfacet_beckmann_aniso(N, U, xalpha, yalpha);
  782. }
  783. }
  784. else {
  785. return microfacet_beckmann_refraction(N, xalpha, eta);
  786. }
  787. }
  788. }
  789. closure color microfacet(string distribution, normal N, float alpha, float eta, int refract)
  790. {
  791. return microfacet(distribution, N, vector(0), alpha, alpha, eta, refract);
  792. }
  793. // Renderer state
  794. int backfacing() BUILTIN;
  795. int raytype(string typename) BUILTIN;
  796. // the individual 'isFOOray' functions are deprecated
  797. int iscameraray()
  798. {
  799. return raytype("camera");
  800. }
  801. int isdiffuseray()
  802. {
  803. return raytype("diffuse");
  804. }
  805. int isglossyray()
  806. {
  807. return raytype("glossy");
  808. }
  809. int isshadowray()
  810. {
  811. return raytype("shadow");
  812. }
  813. int getmatrix(string fromspace, string tospace, output matrix M) BUILTIN;
  814. int getmatrix(string fromspace, output matrix M)
  815. {
  816. return getmatrix(fromspace, "common", M);
  817. }
  818. // Miscellaneous
  819. #undef BUILTIN
  820. #undef BUILTIN_DERIV
  821. #undef PERCOMP1
  822. #undef PERCOMP2
  823. #undef PERCOMP2F
  824. #endif /* CCL_STDOSL_H */