kernel_shader.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * ShaderData, used in four steps:
  18. *
  19. * Setup from incoming ray, sampled position and background.
  20. * Execute for surface, volume or displacement.
  21. * Evaluate one or more closures.
  22. * Release.
  23. */
  24. #include "kernel/closure/alloc.h"
  25. #include "kernel/closure/bsdf_util.h"
  26. #include "kernel/closure/bsdf.h"
  27. #include "kernel/closure/emissive.h"
  28. #include "kernel/svm/svm.h"
  29. CCL_NAMESPACE_BEGIN
  30. /* ShaderData setup from incoming ray */
  31. #ifdef __OBJECT_MOTION__
  32. ccl_device void shader_setup_object_transforms(KernelGlobals *kg, ShaderData *sd, float time)
  33. {
  34. if (sd->object_flag & SD_OBJECT_MOTION) {
  35. sd->ob_tfm = object_fetch_transform_motion(kg, sd->object, time);
  36. sd->ob_itfm = transform_quick_inverse(sd->ob_tfm);
  37. }
  38. else {
  39. sd->ob_tfm = object_fetch_transform(kg, sd->object, OBJECT_TRANSFORM);
  40. sd->ob_itfm = object_fetch_transform(kg, sd->object, OBJECT_INVERSE_TRANSFORM);
  41. }
  42. }
  43. #endif
  44. ccl_device_noinline void shader_setup_from_ray(KernelGlobals *kg,
  45. ShaderData *sd,
  46. const Intersection *isect,
  47. const Ray *ray)
  48. {
  49. PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
  50. #ifdef __INSTANCING__
  51. sd->object = (isect->object == OBJECT_NONE) ? kernel_tex_fetch(__prim_object, isect->prim) :
  52. isect->object;
  53. #endif
  54. sd->lamp = LAMP_NONE;
  55. sd->type = isect->type;
  56. sd->flag = 0;
  57. sd->object_flag = kernel_tex_fetch(__object_flag, sd->object);
  58. /* matrices and time */
  59. #ifdef __OBJECT_MOTION__
  60. shader_setup_object_transforms(kg, sd, ray->time);
  61. #endif
  62. sd->time = ray->time;
  63. sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
  64. sd->ray_length = isect->t;
  65. #ifdef __UV__
  66. sd->u = isect->u;
  67. sd->v = isect->v;
  68. #endif
  69. #ifdef __HAIR__
  70. if (sd->type & PRIMITIVE_ALL_CURVE) {
  71. /* curve */
  72. float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
  73. sd->shader = __float_as_int(curvedata.z);
  74. sd->P = curve_refine(kg, sd, isect, ray);
  75. }
  76. else
  77. #endif
  78. if (sd->type & PRIMITIVE_TRIANGLE) {
  79. /* static triangle */
  80. float3 Ng = triangle_normal(kg, sd);
  81. sd->shader = kernel_tex_fetch(__tri_shader, sd->prim);
  82. /* vectors */
  83. sd->P = triangle_refine(kg, sd, isect, ray);
  84. sd->Ng = Ng;
  85. sd->N = Ng;
  86. /* smooth normal */
  87. if (sd->shader & SHADER_SMOOTH_NORMAL)
  88. sd->N = triangle_smooth_normal(kg, Ng, sd->prim, sd->u, sd->v);
  89. #ifdef __DPDU__
  90. /* dPdu/dPdv */
  91. triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
  92. #endif
  93. }
  94. else {
  95. /* motion triangle */
  96. motion_triangle_shader_setup(kg, sd, isect, ray, false);
  97. }
  98. sd->I = -ray->D;
  99. sd->flag |= kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
  100. #ifdef __INSTANCING__
  101. if (isect->object != OBJECT_NONE) {
  102. /* instance transform */
  103. object_normal_transform_auto(kg, sd, &sd->N);
  104. object_normal_transform_auto(kg, sd, &sd->Ng);
  105. # ifdef __DPDU__
  106. object_dir_transform_auto(kg, sd, &sd->dPdu);
  107. object_dir_transform_auto(kg, sd, &sd->dPdv);
  108. # endif
  109. }
  110. #endif
  111. /* backfacing test */
  112. bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
  113. if (backfacing) {
  114. sd->flag |= SD_BACKFACING;
  115. sd->Ng = -sd->Ng;
  116. sd->N = -sd->N;
  117. #ifdef __DPDU__
  118. sd->dPdu = -sd->dPdu;
  119. sd->dPdv = -sd->dPdv;
  120. #endif
  121. }
  122. #ifdef __RAY_DIFFERENTIALS__
  123. /* differentials */
  124. differential_transfer(&sd->dP, ray->dP, ray->D, ray->dD, sd->Ng, isect->t);
  125. differential_incoming(&sd->dI, ray->dD);
  126. differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
  127. #endif
  128. PROFILING_SHADER(sd->shader);
  129. PROFILING_OBJECT(sd->object);
  130. }
  131. /* ShaderData setup from BSSRDF scatter */
  132. #ifdef __SUBSURFACE__
  133. # ifndef __KERNEL_CUDA__
  134. ccl_device
  135. # else
  136. ccl_device_inline
  137. # endif
  138. void
  139. shader_setup_from_subsurface(KernelGlobals *kg,
  140. ShaderData *sd,
  141. const Intersection *isect,
  142. const Ray *ray)
  143. {
  144. PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
  145. const bool backfacing = sd->flag & SD_BACKFACING;
  146. /* object, matrices, time, ray_length stay the same */
  147. sd->flag = 0;
  148. sd->object_flag = kernel_tex_fetch(__object_flag, sd->object);
  149. sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
  150. sd->type = isect->type;
  151. # ifdef __UV__
  152. sd->u = isect->u;
  153. sd->v = isect->v;
  154. # endif
  155. /* fetch triangle data */
  156. if (sd->type == PRIMITIVE_TRIANGLE) {
  157. float3 Ng = triangle_normal(kg, sd);
  158. sd->shader = kernel_tex_fetch(__tri_shader, sd->prim);
  159. /* static triangle */
  160. sd->P = triangle_refine_local(kg, sd, isect, ray);
  161. sd->Ng = Ng;
  162. sd->N = Ng;
  163. if (sd->shader & SHADER_SMOOTH_NORMAL)
  164. sd->N = triangle_smooth_normal(kg, Ng, sd->prim, sd->u, sd->v);
  165. # ifdef __DPDU__
  166. /* dPdu/dPdv */
  167. triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
  168. # endif
  169. }
  170. else {
  171. /* motion triangle */
  172. motion_triangle_shader_setup(kg, sd, isect, ray, true);
  173. }
  174. sd->flag |= kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
  175. # ifdef __INSTANCING__
  176. if (isect->object != OBJECT_NONE) {
  177. /* instance transform */
  178. object_normal_transform_auto(kg, sd, &sd->N);
  179. object_normal_transform_auto(kg, sd, &sd->Ng);
  180. # ifdef __DPDU__
  181. object_dir_transform_auto(kg, sd, &sd->dPdu);
  182. object_dir_transform_auto(kg, sd, &sd->dPdv);
  183. # endif
  184. }
  185. # endif
  186. /* backfacing test */
  187. if (backfacing) {
  188. sd->flag |= SD_BACKFACING;
  189. sd->Ng = -sd->Ng;
  190. sd->N = -sd->N;
  191. # ifdef __DPDU__
  192. sd->dPdu = -sd->dPdu;
  193. sd->dPdv = -sd->dPdv;
  194. # endif
  195. }
  196. /* should not get used in principle as the shading will only use a diffuse
  197. * BSDF, but the shader might still access it */
  198. sd->I = sd->N;
  199. # ifdef __RAY_DIFFERENTIALS__
  200. /* differentials */
  201. differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
  202. /* don't modify dP and dI */
  203. # endif
  204. PROFILING_SHADER(sd->shader);
  205. }
  206. #endif
  207. /* ShaderData setup from position sampled on mesh */
  208. ccl_device_inline void shader_setup_from_sample(KernelGlobals *kg,
  209. ShaderData *sd,
  210. const float3 P,
  211. const float3 Ng,
  212. const float3 I,
  213. int shader,
  214. int object,
  215. int prim,
  216. float u,
  217. float v,
  218. float t,
  219. float time,
  220. bool object_space,
  221. int lamp)
  222. {
  223. PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
  224. /* vectors */
  225. sd->P = P;
  226. sd->N = Ng;
  227. sd->Ng = Ng;
  228. sd->I = I;
  229. sd->shader = shader;
  230. if (prim != PRIM_NONE)
  231. sd->type = PRIMITIVE_TRIANGLE;
  232. else if (lamp != LAMP_NONE)
  233. sd->type = PRIMITIVE_LAMP;
  234. else
  235. sd->type = PRIMITIVE_NONE;
  236. /* primitive */
  237. #ifdef __INSTANCING__
  238. sd->object = object;
  239. #endif
  240. sd->lamp = LAMP_NONE;
  241. /* currently no access to bvh prim index for strand sd->prim*/
  242. sd->prim = prim;
  243. #ifdef __UV__
  244. sd->u = u;
  245. sd->v = v;
  246. #endif
  247. sd->time = time;
  248. sd->ray_length = t;
  249. sd->flag = kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
  250. sd->object_flag = 0;
  251. if (sd->object != OBJECT_NONE) {
  252. sd->object_flag |= kernel_tex_fetch(__object_flag, sd->object);
  253. #ifdef __OBJECT_MOTION__
  254. shader_setup_object_transforms(kg, sd, time);
  255. }
  256. else if (lamp != LAMP_NONE) {
  257. sd->ob_tfm = lamp_fetch_transform(kg, lamp, false);
  258. sd->ob_itfm = lamp_fetch_transform(kg, lamp, true);
  259. sd->lamp = lamp;
  260. #else
  261. }
  262. else if (lamp != LAMP_NONE) {
  263. sd->lamp = lamp;
  264. #endif
  265. }
  266. /* transform into world space */
  267. if (object_space) {
  268. object_position_transform_auto(kg, sd, &sd->P);
  269. object_normal_transform_auto(kg, sd, &sd->Ng);
  270. sd->N = sd->Ng;
  271. object_dir_transform_auto(kg, sd, &sd->I);
  272. }
  273. if (sd->type & PRIMITIVE_TRIANGLE) {
  274. /* smooth normal */
  275. if (sd->shader & SHADER_SMOOTH_NORMAL) {
  276. sd->N = triangle_smooth_normal(kg, Ng, sd->prim, sd->u, sd->v);
  277. #ifdef __INSTANCING__
  278. if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
  279. object_normal_transform_auto(kg, sd, &sd->N);
  280. }
  281. #endif
  282. }
  283. /* dPdu/dPdv */
  284. #ifdef __DPDU__
  285. triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
  286. # ifdef __INSTANCING__
  287. if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
  288. object_dir_transform_auto(kg, sd, &sd->dPdu);
  289. object_dir_transform_auto(kg, sd, &sd->dPdv);
  290. }
  291. # endif
  292. #endif
  293. }
  294. else {
  295. #ifdef __DPDU__
  296. sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
  297. sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
  298. #endif
  299. }
  300. /* backfacing test */
  301. if (sd->prim != PRIM_NONE) {
  302. bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
  303. if (backfacing) {
  304. sd->flag |= SD_BACKFACING;
  305. sd->Ng = -sd->Ng;
  306. sd->N = -sd->N;
  307. #ifdef __DPDU__
  308. sd->dPdu = -sd->dPdu;
  309. sd->dPdv = -sd->dPdv;
  310. #endif
  311. }
  312. }
  313. #ifdef __RAY_DIFFERENTIALS__
  314. /* no ray differentials here yet */
  315. sd->dP = differential3_zero();
  316. sd->dI = differential3_zero();
  317. sd->du = differential_zero();
  318. sd->dv = differential_zero();
  319. #endif
  320. PROFILING_SHADER(sd->shader);
  321. PROFILING_OBJECT(sd->object);
  322. }
  323. /* ShaderData setup for displacement */
  324. ccl_device void shader_setup_from_displace(
  325. KernelGlobals *kg, ShaderData *sd, int object, int prim, float u, float v)
  326. {
  327. float3 P, Ng, I = make_float3(0.0f, 0.0f, 0.0f);
  328. int shader;
  329. triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
  330. /* force smooth shading for displacement */
  331. shader |= SHADER_SMOOTH_NORMAL;
  332. shader_setup_from_sample(
  333. kg,
  334. sd,
  335. P,
  336. Ng,
  337. I,
  338. shader,
  339. object,
  340. prim,
  341. u,
  342. v,
  343. 0.0f,
  344. 0.5f,
  345. !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
  346. LAMP_NONE);
  347. }
  348. /* ShaderData setup from ray into background */
  349. ccl_device_inline void shader_setup_from_background(KernelGlobals *kg,
  350. ShaderData *sd,
  351. const Ray *ray)
  352. {
  353. PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
  354. /* vectors */
  355. sd->P = ray->D;
  356. sd->N = -ray->D;
  357. sd->Ng = -ray->D;
  358. sd->I = -ray->D;
  359. sd->shader = kernel_data.background.surface_shader;
  360. sd->flag = kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
  361. sd->object_flag = 0;
  362. sd->time = ray->time;
  363. sd->ray_length = 0.0f;
  364. #ifdef __INSTANCING__
  365. sd->object = OBJECT_NONE;
  366. #endif
  367. sd->lamp = LAMP_NONE;
  368. sd->prim = PRIM_NONE;
  369. #ifdef __UV__
  370. sd->u = 0.0f;
  371. sd->v = 0.0f;
  372. #endif
  373. #ifdef __DPDU__
  374. /* dPdu/dPdv */
  375. sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
  376. sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
  377. #endif
  378. #ifdef __RAY_DIFFERENTIALS__
  379. /* differentials */
  380. sd->dP = ray->dD;
  381. differential_incoming(&sd->dI, sd->dP);
  382. sd->du = differential_zero();
  383. sd->dv = differential_zero();
  384. #endif
  385. /* for NDC coordinates */
  386. sd->ray_P = ray->P;
  387. PROFILING_SHADER(sd->shader);
  388. PROFILING_OBJECT(sd->object);
  389. }
  390. /* ShaderData setup from point inside volume */
  391. #ifdef __VOLUME__
  392. ccl_device_inline void shader_setup_from_volume(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
  393. {
  394. PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
  395. /* vectors */
  396. sd->P = ray->P;
  397. sd->N = -ray->D;
  398. sd->Ng = -ray->D;
  399. sd->I = -ray->D;
  400. sd->shader = SHADER_NONE;
  401. sd->flag = 0;
  402. sd->object_flag = 0;
  403. sd->time = ray->time;
  404. sd->ray_length = 0.0f; /* todo: can we set this to some useful value? */
  405. # ifdef __INSTANCING__
  406. sd->object = OBJECT_NONE; /* todo: fill this for texture coordinates */
  407. # endif
  408. sd->lamp = LAMP_NONE;
  409. sd->prim = PRIM_NONE;
  410. sd->type = PRIMITIVE_NONE;
  411. # ifdef __UV__
  412. sd->u = 0.0f;
  413. sd->v = 0.0f;
  414. # endif
  415. # ifdef __DPDU__
  416. /* dPdu/dPdv */
  417. sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
  418. sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
  419. # endif
  420. # ifdef __RAY_DIFFERENTIALS__
  421. /* differentials */
  422. sd->dP = ray->dD;
  423. differential_incoming(&sd->dI, sd->dP);
  424. sd->du = differential_zero();
  425. sd->dv = differential_zero();
  426. # endif
  427. /* for NDC coordinates */
  428. sd->ray_P = ray->P;
  429. sd->ray_dP = ray->dP;
  430. PROFILING_SHADER(sd->shader);
  431. PROFILING_OBJECT(sd->object);
  432. }
  433. #endif /* __VOLUME__ */
  434. /* Merging */
  435. #if defined(__BRANCHED_PATH__) || defined(__VOLUME__)
  436. ccl_device_inline void shader_merge_closures(ShaderData *sd)
  437. {
  438. /* merge identical closures, better when we sample a single closure at a time */
  439. for (int i = 0; i < sd->num_closure; i++) {
  440. ShaderClosure *sci = &sd->closure[i];
  441. for (int j = i + 1; j < sd->num_closure; j++) {
  442. ShaderClosure *scj = &sd->closure[j];
  443. if (sci->type != scj->type)
  444. continue;
  445. if (!bsdf_merge(sci, scj))
  446. continue;
  447. sci->weight += scj->weight;
  448. sci->sample_weight += scj->sample_weight;
  449. int size = sd->num_closure - (j + 1);
  450. if (size > 0) {
  451. for (int k = 0; k < size; k++) {
  452. scj[k] = scj[k + 1];
  453. }
  454. }
  455. sd->num_closure--;
  456. kernel_assert(sd->num_closure >= 0);
  457. j--;
  458. }
  459. }
  460. }
  461. #endif /* __BRANCHED_PATH__ || __VOLUME__ */
  462. /* Defensive sampling. */
  463. ccl_device_inline void shader_prepare_closures(ShaderData *sd, ccl_addr_space PathState *state)
  464. {
  465. /* We can likely also do defensive sampling at deeper bounces, particularly
  466. * for cases like a perfect mirror but possibly also others. This will need
  467. * a good heuristic. */
  468. if (state->bounce + state->transparent_bounce == 0 && sd->num_closure > 1) {
  469. float sum = 0.0f;
  470. for (int i = 0; i < sd->num_closure; i++) {
  471. ShaderClosure *sc = &sd->closure[i];
  472. if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
  473. sum += sc->sample_weight;
  474. }
  475. }
  476. for (int i = 0; i < sd->num_closure; i++) {
  477. ShaderClosure *sc = &sd->closure[i];
  478. if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
  479. sc->sample_weight = max(sc->sample_weight, 0.125f * sum);
  480. }
  481. }
  482. }
  483. }
  484. /* BSDF */
  485. ccl_device_inline void _shader_bsdf_multi_eval(KernelGlobals *kg,
  486. ShaderData *sd,
  487. const float3 omega_in,
  488. float *pdf,
  489. const ShaderClosure *skip_sc,
  490. BsdfEval *result_eval,
  491. float sum_pdf,
  492. float sum_sample_weight)
  493. {
  494. /* this is the veach one-sample model with balance heuristic, some pdf
  495. * factors drop out when using balance heuristic weighting */
  496. for (int i = 0; i < sd->num_closure; i++) {
  497. const ShaderClosure *sc = &sd->closure[i];
  498. if (sc != skip_sc && CLOSURE_IS_BSDF(sc->type)) {
  499. float bsdf_pdf = 0.0f;
  500. float3 eval = bsdf_eval(kg, sd, sc, omega_in, &bsdf_pdf);
  501. if (bsdf_pdf != 0.0f) {
  502. bsdf_eval_accum(result_eval, sc->type, eval * sc->weight, 1.0f);
  503. sum_pdf += bsdf_pdf * sc->sample_weight;
  504. }
  505. sum_sample_weight += sc->sample_weight;
  506. }
  507. }
  508. *pdf = (sum_sample_weight > 0.0f) ? sum_pdf / sum_sample_weight : 0.0f;
  509. }
  510. #ifdef __BRANCHED_PATH__
  511. ccl_device_inline void _shader_bsdf_multi_eval_branched(KernelGlobals *kg,
  512. ShaderData *sd,
  513. const float3 omega_in,
  514. BsdfEval *result_eval,
  515. float light_pdf,
  516. bool use_mis)
  517. {
  518. for (int i = 0; i < sd->num_closure; i++) {
  519. const ShaderClosure *sc = &sd->closure[i];
  520. if (CLOSURE_IS_BSDF(sc->type)) {
  521. float bsdf_pdf = 0.0f;
  522. float3 eval = bsdf_eval(kg, sd, sc, omega_in, &bsdf_pdf);
  523. if (bsdf_pdf != 0.0f) {
  524. float mis_weight = use_mis ? power_heuristic(light_pdf, bsdf_pdf) : 1.0f;
  525. bsdf_eval_accum(result_eval, sc->type, eval * sc->weight, mis_weight);
  526. }
  527. }
  528. }
  529. }
  530. #endif /* __BRANCHED_PATH__ */
  531. #ifndef __KERNEL_CUDA__
  532. ccl_device
  533. #else
  534. ccl_device_inline
  535. #endif
  536. void
  537. shader_bsdf_eval(KernelGlobals *kg,
  538. ShaderData *sd,
  539. const float3 omega_in,
  540. BsdfEval *eval,
  541. float light_pdf,
  542. bool use_mis)
  543. {
  544. PROFILING_INIT(kg, PROFILING_CLOSURE_EVAL);
  545. bsdf_eval_init(
  546. eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
  547. #ifdef __BRANCHED_PATH__
  548. if (kernel_data.integrator.branched)
  549. _shader_bsdf_multi_eval_branched(kg, sd, omega_in, eval, light_pdf, use_mis);
  550. else
  551. #endif
  552. {
  553. float pdf;
  554. _shader_bsdf_multi_eval(kg, sd, omega_in, &pdf, NULL, eval, 0.0f, 0.0f);
  555. if (use_mis) {
  556. float weight = power_heuristic(light_pdf, pdf);
  557. bsdf_eval_mis(eval, weight);
  558. }
  559. }
  560. }
  561. ccl_device_inline const ShaderClosure *shader_bsdf_pick(ShaderData *sd, float *randu)
  562. {
  563. /* Note the sampling here must match shader_bssrdf_pick,
  564. * since we reuse the same random number. */
  565. int sampled = 0;
  566. if (sd->num_closure > 1) {
  567. /* Pick a BSDF or based on sample weights. */
  568. float sum = 0.0f;
  569. for (int i = 0; i < sd->num_closure; i++) {
  570. const ShaderClosure *sc = &sd->closure[i];
  571. if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
  572. sum += sc->sample_weight;
  573. }
  574. }
  575. float r = (*randu) * sum;
  576. float partial_sum = 0.0f;
  577. for (int i = 0; i < sd->num_closure; i++) {
  578. const ShaderClosure *sc = &sd->closure[i];
  579. if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
  580. float next_sum = partial_sum + sc->sample_weight;
  581. if (r < next_sum) {
  582. sampled = i;
  583. /* Rescale to reuse for direction sample, to better
  584. * preserve stratification. */
  585. *randu = (r - partial_sum) / sc->sample_weight;
  586. break;
  587. }
  588. partial_sum = next_sum;
  589. }
  590. }
  591. }
  592. const ShaderClosure *sc = &sd->closure[sampled];
  593. return CLOSURE_IS_BSDF(sc->type) ? sc : NULL;
  594. }
  595. ccl_device_inline const ShaderClosure *shader_bssrdf_pick(ShaderData *sd,
  596. ccl_addr_space float3 *throughput,
  597. float *randu)
  598. {
  599. /* Note the sampling here must match shader_bsdf_pick,
  600. * since we reuse the same random number. */
  601. int sampled = 0;
  602. if (sd->num_closure > 1) {
  603. /* Pick a BSDF or BSSRDF or based on sample weights. */
  604. float sum_bsdf = 0.0f;
  605. float sum_bssrdf = 0.0f;
  606. for (int i = 0; i < sd->num_closure; i++) {
  607. const ShaderClosure *sc = &sd->closure[i];
  608. if (CLOSURE_IS_BSDF(sc->type)) {
  609. sum_bsdf += sc->sample_weight;
  610. }
  611. else if (CLOSURE_IS_BSSRDF(sc->type)) {
  612. sum_bssrdf += sc->sample_weight;
  613. }
  614. }
  615. float r = (*randu) * (sum_bsdf + sum_bssrdf);
  616. float partial_sum = 0.0f;
  617. for (int i = 0; i < sd->num_closure; i++) {
  618. const ShaderClosure *sc = &sd->closure[i];
  619. if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
  620. float next_sum = partial_sum + sc->sample_weight;
  621. if (r < next_sum) {
  622. if (CLOSURE_IS_BSDF(sc->type)) {
  623. *throughput *= (sum_bsdf + sum_bssrdf) / sum_bsdf;
  624. return NULL;
  625. }
  626. else {
  627. *throughput *= (sum_bsdf + sum_bssrdf) / sum_bssrdf;
  628. sampled = i;
  629. /* Rescale to reuse for direction sample, to better
  630. * preserve stratifaction. */
  631. *randu = (r - partial_sum) / sc->sample_weight;
  632. break;
  633. }
  634. }
  635. partial_sum = next_sum;
  636. }
  637. }
  638. }
  639. const ShaderClosure *sc = &sd->closure[sampled];
  640. return CLOSURE_IS_BSSRDF(sc->type) ? sc : NULL;
  641. }
  642. ccl_device_inline int shader_bsdf_sample(KernelGlobals *kg,
  643. ShaderData *sd,
  644. float randu,
  645. float randv,
  646. BsdfEval *bsdf_eval,
  647. float3 *omega_in,
  648. differential3 *domega_in,
  649. float *pdf)
  650. {
  651. PROFILING_INIT(kg, PROFILING_CLOSURE_SAMPLE);
  652. const ShaderClosure *sc = shader_bsdf_pick(sd, &randu);
  653. if (sc == NULL) {
  654. *pdf = 0.0f;
  655. return LABEL_NONE;
  656. }
  657. /* BSSRDF should already have been handled elsewhere. */
  658. kernel_assert(CLOSURE_IS_BSDF(sc->type));
  659. int label;
  660. float3 eval;
  661. *pdf = 0.0f;
  662. label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
  663. if (*pdf != 0.0f) {
  664. bsdf_eval_init(bsdf_eval, sc->type, eval * sc->weight, kernel_data.film.use_light_pass);
  665. if (sd->num_closure > 1) {
  666. float sweight = sc->sample_weight;
  667. _shader_bsdf_multi_eval(kg, sd, *omega_in, pdf, sc, bsdf_eval, *pdf * sweight, sweight);
  668. }
  669. }
  670. return label;
  671. }
  672. ccl_device int shader_bsdf_sample_closure(KernelGlobals *kg,
  673. ShaderData *sd,
  674. const ShaderClosure *sc,
  675. float randu,
  676. float randv,
  677. BsdfEval *bsdf_eval,
  678. float3 *omega_in,
  679. differential3 *domega_in,
  680. float *pdf)
  681. {
  682. PROFILING_INIT(kg, PROFILING_CLOSURE_SAMPLE);
  683. int label;
  684. float3 eval;
  685. *pdf = 0.0f;
  686. label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
  687. if (*pdf != 0.0f)
  688. bsdf_eval_init(bsdf_eval, sc->type, eval * sc->weight, kernel_data.film.use_light_pass);
  689. return label;
  690. }
  691. ccl_device float shader_bsdf_average_roughness(ShaderData *sd)
  692. {
  693. float roughness = 0.0f;
  694. float sum_weight = 0.0f;
  695. for (int i = 0; i < sd->num_closure; i++) {
  696. ShaderClosure *sc = &sd->closure[i];
  697. if (CLOSURE_IS_BSDF(sc->type)) {
  698. /* sqrt once to undo the squaring from multiplying roughness on the
  699. * two axes, and once for the squared roughness convention. */
  700. float weight = fabsf(average(sc->weight));
  701. roughness += weight * sqrtf(safe_sqrtf(bsdf_get_roughness_squared(sc)));
  702. sum_weight += weight;
  703. }
  704. }
  705. return (sum_weight > 0.0f) ? roughness / sum_weight : 0.0f;
  706. }
  707. ccl_device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
  708. {
  709. for (int i = 0; i < sd->num_closure; i++) {
  710. ShaderClosure *sc = &sd->closure[i];
  711. if (CLOSURE_IS_BSDF(sc->type))
  712. bsdf_blur(kg, sc, roughness);
  713. }
  714. }
  715. ccl_device float3 shader_bsdf_transparency(KernelGlobals *kg, const ShaderData *sd)
  716. {
  717. if (sd->flag & SD_HAS_ONLY_VOLUME) {
  718. return make_float3(1.0f, 1.0f, 1.0f);
  719. }
  720. else if (sd->flag & SD_TRANSPARENT) {
  721. return sd->closure_transparent_extinction;
  722. }
  723. else {
  724. return make_float3(0.0f, 0.0f, 0.0f);
  725. }
  726. }
  727. ccl_device void shader_bsdf_disable_transparency(KernelGlobals *kg, ShaderData *sd)
  728. {
  729. if (sd->flag & SD_TRANSPARENT) {
  730. for (int i = 0; i < sd->num_closure; i++) {
  731. ShaderClosure *sc = &sd->closure[i];
  732. if (sc->type == CLOSURE_BSDF_TRANSPARENT_ID) {
  733. sc->sample_weight = 0.0f;
  734. sc->weight = make_float3(0.0f, 0.0f, 0.0f);
  735. }
  736. }
  737. sd->flag &= ~SD_TRANSPARENT;
  738. }
  739. }
  740. ccl_device float3 shader_bsdf_alpha(KernelGlobals *kg, ShaderData *sd)
  741. {
  742. float3 alpha = make_float3(1.0f, 1.0f, 1.0f) - shader_bsdf_transparency(kg, sd);
  743. alpha = max(alpha, make_float3(0.0f, 0.0f, 0.0f));
  744. alpha = min(alpha, make_float3(1.0f, 1.0f, 1.0f));
  745. return alpha;
  746. }
  747. ccl_device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
  748. {
  749. float3 eval = make_float3(0.0f, 0.0f, 0.0f);
  750. for (int i = 0; i < sd->num_closure; i++) {
  751. ShaderClosure *sc = &sd->closure[i];
  752. if (CLOSURE_IS_BSDF_DIFFUSE(sc->type))
  753. eval += sc->weight;
  754. }
  755. return eval;
  756. }
  757. ccl_device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
  758. {
  759. float3 eval = make_float3(0.0f, 0.0f, 0.0f);
  760. for (int i = 0; i < sd->num_closure; i++) {
  761. ShaderClosure *sc = &sd->closure[i];
  762. if (CLOSURE_IS_BSDF_GLOSSY(sc->type))
  763. eval += sc->weight;
  764. }
  765. return eval;
  766. }
  767. ccl_device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
  768. {
  769. float3 eval = make_float3(0.0f, 0.0f, 0.0f);
  770. for (int i = 0; i < sd->num_closure; i++) {
  771. ShaderClosure *sc = &sd->closure[i];
  772. if (CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
  773. eval += sc->weight;
  774. }
  775. return eval;
  776. }
  777. ccl_device float3 shader_bsdf_subsurface(KernelGlobals *kg, ShaderData *sd)
  778. {
  779. float3 eval = make_float3(0.0f, 0.0f, 0.0f);
  780. for (int i = 0; i < sd->num_closure; i++) {
  781. ShaderClosure *sc = &sd->closure[i];
  782. if (CLOSURE_IS_BSSRDF(sc->type) || CLOSURE_IS_BSDF_BSSRDF(sc->type))
  783. eval += sc->weight;
  784. }
  785. return eval;
  786. }
  787. ccl_device float3 shader_bsdf_average_normal(KernelGlobals *kg, ShaderData *sd)
  788. {
  789. float3 N = make_float3(0.0f, 0.0f, 0.0f);
  790. for (int i = 0; i < sd->num_closure; i++) {
  791. ShaderClosure *sc = &sd->closure[i];
  792. if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type))
  793. N += sc->N * fabsf(average(sc->weight));
  794. }
  795. return (is_zero(N)) ? sd->N : normalize(N);
  796. }
  797. ccl_device float3 shader_bsdf_ao(KernelGlobals *kg, ShaderData *sd, float ao_factor, float3 *N_)
  798. {
  799. float3 eval = make_float3(0.0f, 0.0f, 0.0f);
  800. float3 N = make_float3(0.0f, 0.0f, 0.0f);
  801. for (int i = 0; i < sd->num_closure; i++) {
  802. ShaderClosure *sc = &sd->closure[i];
  803. if (CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
  804. const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
  805. eval += sc->weight * ao_factor;
  806. N += bsdf->N * fabsf(average(sc->weight));
  807. }
  808. }
  809. *N_ = (is_zero(N)) ? sd->N : normalize(N);
  810. return eval;
  811. }
  812. #ifdef __SUBSURFACE__
  813. ccl_device float3 shader_bssrdf_sum(ShaderData *sd, float3 *N_, float *texture_blur_)
  814. {
  815. float3 eval = make_float3(0.0f, 0.0f, 0.0f);
  816. float3 N = make_float3(0.0f, 0.0f, 0.0f);
  817. float texture_blur = 0.0f, weight_sum = 0.0f;
  818. for (int i = 0; i < sd->num_closure; i++) {
  819. ShaderClosure *sc = &sd->closure[i];
  820. if (CLOSURE_IS_BSSRDF(sc->type)) {
  821. const Bssrdf *bssrdf = (const Bssrdf *)sc;
  822. float avg_weight = fabsf(average(sc->weight));
  823. N += bssrdf->N * avg_weight;
  824. eval += sc->weight;
  825. texture_blur += bssrdf->texture_blur * avg_weight;
  826. weight_sum += avg_weight;
  827. }
  828. }
  829. if (N_)
  830. *N_ = (is_zero(N)) ? sd->N : normalize(N);
  831. if (texture_blur_)
  832. *texture_blur_ = safe_divide(texture_blur, weight_sum);
  833. return eval;
  834. }
  835. #endif /* __SUBSURFACE__ */
  836. /* Constant emission optimization */
  837. ccl_device bool shader_constant_emission_eval(KernelGlobals *kg, int shader, float3 *eval)
  838. {
  839. int shader_index = shader & SHADER_MASK;
  840. int shader_flag = kernel_tex_fetch(__shaders, shader_index).flags;
  841. if (shader_flag & SD_HAS_CONSTANT_EMISSION) {
  842. *eval = make_float3(kernel_tex_fetch(__shaders, shader_index).constant_emission[0],
  843. kernel_tex_fetch(__shaders, shader_index).constant_emission[1],
  844. kernel_tex_fetch(__shaders, shader_index).constant_emission[2]);
  845. return true;
  846. }
  847. return false;
  848. }
  849. /* Background */
  850. ccl_device float3 shader_background_eval(ShaderData *sd)
  851. {
  852. if (sd->flag & SD_EMISSION) {
  853. return sd->closure_emission_background;
  854. }
  855. else {
  856. return make_float3(0.0f, 0.0f, 0.0f);
  857. }
  858. }
  859. /* Emission */
  860. ccl_device float3 shader_emissive_eval(ShaderData *sd)
  861. {
  862. if (sd->flag & SD_EMISSION) {
  863. return emissive_simple_eval(sd->Ng, sd->I) * sd->closure_emission_background;
  864. }
  865. else {
  866. return make_float3(0.0f, 0.0f, 0.0f);
  867. }
  868. }
  869. /* Holdout */
  870. ccl_device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
  871. {
  872. float3 weight = make_float3(0.0f, 0.0f, 0.0f);
  873. for (int i = 0; i < sd->num_closure; i++) {
  874. ShaderClosure *sc = &sd->closure[i];
  875. if (CLOSURE_IS_HOLDOUT(sc->type))
  876. weight += sc->weight;
  877. }
  878. return weight;
  879. }
  880. /* Surface Evaluation */
  881. ccl_device void shader_eval_surface(KernelGlobals *kg,
  882. ShaderData *sd,
  883. ccl_addr_space PathState *state,
  884. int path_flag)
  885. {
  886. PROFILING_INIT(kg, PROFILING_SHADER_EVAL);
  887. /* If path is being terminated, we are tracing a shadow ray or evaluating
  888. * emission, then we don't need to store closures. The emission and shadow
  889. * shader data also do not have a closure array to save GPU memory. */
  890. int max_closures;
  891. if (path_flag & (PATH_RAY_TERMINATE | PATH_RAY_SHADOW | PATH_RAY_EMISSION)) {
  892. max_closures = 0;
  893. }
  894. else {
  895. max_closures = kernel_data.integrator.max_closures;
  896. }
  897. sd->num_closure = 0;
  898. sd->num_closure_left = max_closures;
  899. #ifdef __OSL__
  900. if (kg->osl) {
  901. if (sd->object == OBJECT_NONE && sd->lamp == LAMP_NONE) {
  902. OSLShader::eval_background(kg, sd, state, path_flag);
  903. }
  904. else {
  905. OSLShader::eval_surface(kg, sd, state, path_flag);
  906. }
  907. }
  908. else
  909. #endif
  910. {
  911. #ifdef __SVM__
  912. svm_eval_nodes(kg, sd, state, SHADER_TYPE_SURFACE, path_flag);
  913. #else
  914. if (sd->object == OBJECT_NONE) {
  915. sd->closure_emission_background = make_float3(0.8f, 0.8f, 0.8f);
  916. sd->flag |= SD_EMISSION;
  917. }
  918. else {
  919. DiffuseBsdf *bsdf = (DiffuseBsdf *)bsdf_alloc(
  920. sd, sizeof(DiffuseBsdf), make_float3(0.8f, 0.8f, 0.8f));
  921. if (bsdf != NULL) {
  922. bsdf->N = sd->N;
  923. sd->flag |= bsdf_diffuse_setup(bsdf);
  924. }
  925. }
  926. #endif
  927. }
  928. if (sd->flag & SD_BSDF_NEEDS_LCG) {
  929. sd->lcg_state = lcg_state_init_addrspace(state, 0xb4bc3953);
  930. }
  931. }
  932. /* Volume */
  933. #ifdef __VOLUME__
  934. ccl_device_inline void _shader_volume_phase_multi_eval(const ShaderData *sd,
  935. const float3 omega_in,
  936. float *pdf,
  937. int skip_phase,
  938. BsdfEval *result_eval,
  939. float sum_pdf,
  940. float sum_sample_weight)
  941. {
  942. for (int i = 0; i < sd->num_closure; i++) {
  943. if (i == skip_phase)
  944. continue;
  945. const ShaderClosure *sc = &sd->closure[i];
  946. if (CLOSURE_IS_PHASE(sc->type)) {
  947. float phase_pdf = 0.0f;
  948. float3 eval = volume_phase_eval(sd, sc, omega_in, &phase_pdf);
  949. if (phase_pdf != 0.0f) {
  950. bsdf_eval_accum(result_eval, sc->type, eval, 1.0f);
  951. sum_pdf += phase_pdf * sc->sample_weight;
  952. }
  953. sum_sample_weight += sc->sample_weight;
  954. }
  955. }
  956. *pdf = (sum_sample_weight > 0.0f) ? sum_pdf / sum_sample_weight : 0.0f;
  957. }
  958. ccl_device void shader_volume_phase_eval(
  959. KernelGlobals *kg, const ShaderData *sd, const float3 omega_in, BsdfEval *eval, float *pdf)
  960. {
  961. PROFILING_INIT(kg, PROFILING_CLOSURE_VOLUME_EVAL);
  962. bsdf_eval_init(
  963. eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
  964. _shader_volume_phase_multi_eval(sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
  965. }
  966. ccl_device int shader_volume_phase_sample(KernelGlobals *kg,
  967. const ShaderData *sd,
  968. float randu,
  969. float randv,
  970. BsdfEval *phase_eval,
  971. float3 *omega_in,
  972. differential3 *domega_in,
  973. float *pdf)
  974. {
  975. PROFILING_INIT(kg, PROFILING_CLOSURE_VOLUME_SAMPLE);
  976. int sampled = 0;
  977. if (sd->num_closure > 1) {
  978. /* pick a phase closure based on sample weights */
  979. float sum = 0.0f;
  980. for (sampled = 0; sampled < sd->num_closure; sampled++) {
  981. const ShaderClosure *sc = &sd->closure[sampled];
  982. if (CLOSURE_IS_PHASE(sc->type))
  983. sum += sc->sample_weight;
  984. }
  985. float r = randu * sum;
  986. float partial_sum = 0.0f;
  987. for (sampled = 0; sampled < sd->num_closure; sampled++) {
  988. const ShaderClosure *sc = &sd->closure[sampled];
  989. if (CLOSURE_IS_PHASE(sc->type)) {
  990. float next_sum = partial_sum + sc->sample_weight;
  991. if (r <= next_sum) {
  992. /* Rescale to reuse for BSDF direction sample. */
  993. randu = (r - partial_sum) / sc->sample_weight;
  994. break;
  995. }
  996. partial_sum = next_sum;
  997. }
  998. }
  999. if (sampled == sd->num_closure) {
  1000. *pdf = 0.0f;
  1001. return LABEL_NONE;
  1002. }
  1003. }
  1004. /* todo: this isn't quite correct, we don't weight anisotropy properly
  1005. * depending on color channels, even if this is perhaps not a common case */
  1006. const ShaderClosure *sc = &sd->closure[sampled];
  1007. int label;
  1008. float3 eval;
  1009. *pdf = 0.0f;
  1010. label = volume_phase_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
  1011. if (*pdf != 0.0f) {
  1012. bsdf_eval_init(phase_eval, sc->type, eval, kernel_data.film.use_light_pass);
  1013. }
  1014. return label;
  1015. }
  1016. ccl_device int shader_phase_sample_closure(KernelGlobals *kg,
  1017. const ShaderData *sd,
  1018. const ShaderClosure *sc,
  1019. float randu,
  1020. float randv,
  1021. BsdfEval *phase_eval,
  1022. float3 *omega_in,
  1023. differential3 *domega_in,
  1024. float *pdf)
  1025. {
  1026. PROFILING_INIT(kg, PROFILING_CLOSURE_VOLUME_SAMPLE);
  1027. int label;
  1028. float3 eval;
  1029. *pdf = 0.0f;
  1030. label = volume_phase_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
  1031. if (*pdf != 0.0f)
  1032. bsdf_eval_init(phase_eval, sc->type, eval, kernel_data.film.use_light_pass);
  1033. return label;
  1034. }
  1035. /* Volume Evaluation */
  1036. ccl_device_inline void shader_eval_volume(KernelGlobals *kg,
  1037. ShaderData *sd,
  1038. ccl_addr_space PathState *state,
  1039. ccl_addr_space VolumeStack *stack,
  1040. int path_flag)
  1041. {
  1042. /* If path is being terminated, we are tracing a shadow ray or evaluating
  1043. * emission, then we don't need to store closures. The emission and shadow
  1044. * shader data also do not have a closure array to save GPU memory. */
  1045. int max_closures;
  1046. if (path_flag & (PATH_RAY_TERMINATE | PATH_RAY_SHADOW | PATH_RAY_EMISSION)) {
  1047. max_closures = 0;
  1048. }
  1049. else {
  1050. max_closures = kernel_data.integrator.max_closures;
  1051. }
  1052. /* reset closures once at the start, we will be accumulating the closures
  1053. * for all volumes in the stack into a single array of closures */
  1054. sd->num_closure = 0;
  1055. sd->num_closure_left = max_closures;
  1056. sd->flag = 0;
  1057. sd->object_flag = 0;
  1058. for (int i = 0; stack[i].shader != SHADER_NONE; i++) {
  1059. /* setup shaderdata from stack. it's mostly setup already in
  1060. * shader_setup_from_volume, this switching should be quick */
  1061. sd->object = stack[i].object;
  1062. sd->lamp = LAMP_NONE;
  1063. sd->shader = stack[i].shader;
  1064. sd->flag &= ~SD_SHADER_FLAGS;
  1065. sd->flag |= kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
  1066. sd->object_flag &= ~SD_OBJECT_FLAGS;
  1067. if (sd->object != OBJECT_NONE) {
  1068. sd->object_flag |= kernel_tex_fetch(__object_flag, sd->object);
  1069. # ifdef __OBJECT_MOTION__
  1070. /* todo: this is inefficient for motion blur, we should be
  1071. * caching matrices instead of recomputing them each step */
  1072. shader_setup_object_transforms(kg, sd, sd->time);
  1073. # endif
  1074. }
  1075. /* evaluate shader */
  1076. # ifdef __SVM__
  1077. # ifdef __OSL__
  1078. if (kg->osl) {
  1079. OSLShader::eval_volume(kg, sd, state, path_flag);
  1080. }
  1081. else
  1082. # endif
  1083. {
  1084. svm_eval_nodes(kg, sd, state, SHADER_TYPE_VOLUME, path_flag);
  1085. }
  1086. # endif
  1087. /* merge closures to avoid exceeding number of closures limit */
  1088. if (i > 0)
  1089. shader_merge_closures(sd);
  1090. }
  1091. }
  1092. #endif /* __VOLUME__ */
  1093. /* Displacement Evaluation */
  1094. ccl_device void shader_eval_displacement(KernelGlobals *kg,
  1095. ShaderData *sd,
  1096. ccl_addr_space PathState *state)
  1097. {
  1098. sd->num_closure = 0;
  1099. sd->num_closure_left = 0;
  1100. /* this will modify sd->P */
  1101. #ifdef __SVM__
  1102. # ifdef __OSL__
  1103. if (kg->osl)
  1104. OSLShader::eval_displacement(kg, sd, state);
  1105. else
  1106. # endif
  1107. {
  1108. svm_eval_nodes(kg, sd, state, SHADER_TYPE_DISPLACEMENT, 0);
  1109. }
  1110. #endif
  1111. }
  1112. /* Transparent Shadows */
  1113. #ifdef __TRANSPARENT_SHADOWS__
  1114. ccl_device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
  1115. {
  1116. int prim = kernel_tex_fetch(__prim_index, isect->prim);
  1117. int shader = 0;
  1118. # ifdef __HAIR__
  1119. if (kernel_tex_fetch(__prim_type, isect->prim) & PRIMITIVE_ALL_TRIANGLE) {
  1120. # endif
  1121. shader = kernel_tex_fetch(__tri_shader, prim);
  1122. # ifdef __HAIR__
  1123. }
  1124. else {
  1125. float4 str = kernel_tex_fetch(__curves, prim);
  1126. shader = __float_as_int(str.z);
  1127. }
  1128. # endif
  1129. int flag = kernel_tex_fetch(__shaders, (shader & SHADER_MASK)).flags;
  1130. return (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
  1131. }
  1132. #endif /* __TRANSPARENT_SHADOWS__ */
  1133. ccl_device float shader_cryptomatte_id(KernelGlobals *kg, int shader)
  1134. {
  1135. return kernel_tex_fetch(__shaders, (shader & SHADER_MASK)).cryptomatte_id;
  1136. }
  1137. CCL_NAMESPACE_END