kernel_accumulate.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. CCL_NAMESPACE_BEGIN
  17. /* BSDF Eval
  18. *
  19. * BSDF evaluation result, split per BSDF type. This is used to accumulate
  20. * render passes separately. */
  21. ccl_device float3 shader_bsdf_transparency(KernelGlobals *kg, const ShaderData *sd);
  22. ccl_device_inline void bsdf_eval_init(BsdfEval *eval,
  23. ClosureType type,
  24. float3 value,
  25. int use_light_pass)
  26. {
  27. #ifdef __PASSES__
  28. eval->use_light_pass = use_light_pass;
  29. if (eval->use_light_pass) {
  30. eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
  31. eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
  32. eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
  33. eval->transparent = make_float3(0.0f, 0.0f, 0.0f);
  34. eval->subsurface = make_float3(0.0f, 0.0f, 0.0f);
  35. eval->scatter = make_float3(0.0f, 0.0f, 0.0f);
  36. if (type == CLOSURE_BSDF_TRANSPARENT_ID)
  37. eval->transparent = value;
  38. else if (CLOSURE_IS_BSDF_DIFFUSE(type))
  39. eval->diffuse = value;
  40. else if (CLOSURE_IS_BSDF_GLOSSY(type))
  41. eval->glossy = value;
  42. else if (CLOSURE_IS_BSDF_TRANSMISSION(type))
  43. eval->transmission = value;
  44. else if (CLOSURE_IS_BSDF_BSSRDF(type))
  45. eval->subsurface = value;
  46. else if (CLOSURE_IS_PHASE(type))
  47. eval->scatter = value;
  48. }
  49. else
  50. #endif
  51. {
  52. eval->diffuse = value;
  53. }
  54. #ifdef __SHADOW_TRICKS__
  55. eval->sum_no_mis = make_float3(0.0f, 0.0f, 0.0f);
  56. #endif
  57. }
  58. ccl_device_inline void bsdf_eval_accum(BsdfEval *eval,
  59. ClosureType type,
  60. float3 value,
  61. float mis_weight)
  62. {
  63. #ifdef __SHADOW_TRICKS__
  64. eval->sum_no_mis += value;
  65. #endif
  66. value *= mis_weight;
  67. #ifdef __PASSES__
  68. if (eval->use_light_pass) {
  69. if (CLOSURE_IS_BSDF_DIFFUSE(type))
  70. eval->diffuse += value;
  71. else if (CLOSURE_IS_BSDF_GLOSSY(type))
  72. eval->glossy += value;
  73. else if (CLOSURE_IS_BSDF_TRANSMISSION(type))
  74. eval->transmission += value;
  75. else if (CLOSURE_IS_BSDF_BSSRDF(type))
  76. eval->subsurface += value;
  77. else if (CLOSURE_IS_PHASE(type))
  78. eval->scatter += value;
  79. /* skipping transparent, this function is used by for eval(), will be zero then */
  80. }
  81. else
  82. #endif
  83. {
  84. eval->diffuse += value;
  85. }
  86. }
  87. ccl_device_inline bool bsdf_eval_is_zero(BsdfEval *eval)
  88. {
  89. #ifdef __PASSES__
  90. if (eval->use_light_pass) {
  91. return is_zero(eval->diffuse) && is_zero(eval->glossy) && is_zero(eval->transmission) &&
  92. is_zero(eval->transparent) && is_zero(eval->subsurface) && is_zero(eval->scatter);
  93. }
  94. else
  95. #endif
  96. {
  97. return is_zero(eval->diffuse);
  98. }
  99. }
  100. ccl_device_inline void bsdf_eval_mis(BsdfEval *eval, float value)
  101. {
  102. #ifdef __PASSES__
  103. if (eval->use_light_pass) {
  104. eval->diffuse *= value;
  105. eval->glossy *= value;
  106. eval->transmission *= value;
  107. eval->subsurface *= value;
  108. eval->scatter *= value;
  109. /* skipping transparent, this function is used by for eval(), will be zero then */
  110. }
  111. else
  112. #endif
  113. {
  114. eval->diffuse *= value;
  115. }
  116. }
  117. ccl_device_inline void bsdf_eval_mul(BsdfEval *eval, float value)
  118. {
  119. #ifdef __SHADOW_TRICKS__
  120. eval->sum_no_mis *= value;
  121. #endif
  122. bsdf_eval_mis(eval, value);
  123. }
  124. ccl_device_inline void bsdf_eval_mul3(BsdfEval *eval, float3 value)
  125. {
  126. #ifdef __SHADOW_TRICKS__
  127. eval->sum_no_mis *= value;
  128. #endif
  129. #ifdef __PASSES__
  130. if (eval->use_light_pass) {
  131. eval->diffuse *= value;
  132. eval->glossy *= value;
  133. eval->transmission *= value;
  134. eval->subsurface *= value;
  135. eval->scatter *= value;
  136. /* skipping transparent, this function is used by for eval(), will be zero then */
  137. }
  138. else
  139. eval->diffuse *= value;
  140. #else
  141. eval->diffuse *= value;
  142. #endif
  143. }
  144. ccl_device_inline float3 bsdf_eval_sum(const BsdfEval *eval)
  145. {
  146. #ifdef __PASSES__
  147. if (eval->use_light_pass) {
  148. return eval->diffuse + eval->glossy + eval->transmission + eval->subsurface + eval->scatter;
  149. }
  150. else
  151. #endif
  152. return eval->diffuse;
  153. }
  154. /* Path Radiance
  155. *
  156. * We accumulate different render passes separately. After summing at the end
  157. * to get the combined result, it should be identical. We definite directly
  158. * visible as the first non-transparent hit, while indirectly visible are the
  159. * bounces after that. */
  160. ccl_device_inline void path_radiance_init(PathRadiance *L, int use_light_pass)
  161. {
  162. /* clear all */
  163. #ifdef __PASSES__
  164. L->use_light_pass = use_light_pass;
  165. if (use_light_pass) {
  166. L->indirect = make_float3(0.0f, 0.0f, 0.0f);
  167. L->direct_emission = make_float3(0.0f, 0.0f, 0.0f);
  168. L->color_diffuse = make_float3(0.0f, 0.0f, 0.0f);
  169. L->color_glossy = make_float3(0.0f, 0.0f, 0.0f);
  170. L->color_transmission = make_float3(0.0f, 0.0f, 0.0f);
  171. L->color_subsurface = make_float3(0.0f, 0.0f, 0.0f);
  172. L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
  173. L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
  174. L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
  175. L->direct_subsurface = make_float3(0.0f, 0.0f, 0.0f);
  176. L->direct_scatter = make_float3(0.0f, 0.0f, 0.0f);
  177. L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
  178. L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
  179. L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
  180. L->indirect_subsurface = make_float3(0.0f, 0.0f, 0.0f);
  181. L->indirect_scatter = make_float3(0.0f, 0.0f, 0.0f);
  182. L->transparent = 0.0f;
  183. L->emission = make_float3(0.0f, 0.0f, 0.0f);
  184. L->background = make_float3(0.0f, 0.0f, 0.0f);
  185. L->ao = make_float3(0.0f, 0.0f, 0.0f);
  186. L->shadow = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
  187. L->mist = 0.0f;
  188. L->state.diffuse = make_float3(0.0f, 0.0f, 0.0f);
  189. L->state.glossy = make_float3(0.0f, 0.0f, 0.0f);
  190. L->state.transmission = make_float3(0.0f, 0.0f, 0.0f);
  191. L->state.subsurface = make_float3(0.0f, 0.0f, 0.0f);
  192. L->state.scatter = make_float3(0.0f, 0.0f, 0.0f);
  193. L->state.direct = make_float3(0.0f, 0.0f, 0.0f);
  194. }
  195. else
  196. #endif
  197. {
  198. L->transparent = 0.0f;
  199. L->emission = make_float3(0.0f, 0.0f, 0.0f);
  200. }
  201. #ifdef __SHADOW_TRICKS__
  202. L->path_total = make_float3(0.0f, 0.0f, 0.0f);
  203. L->path_total_shaded = make_float3(0.0f, 0.0f, 0.0f);
  204. L->shadow_background_color = make_float3(0.0f, 0.0f, 0.0f);
  205. L->shadow_throughput = 0.0f;
  206. L->shadow_transparency = 1.0f;
  207. L->has_shadow_catcher = 0;
  208. #endif
  209. #ifdef __DENOISING_FEATURES__
  210. L->denoising_normal = make_float3(0.0f, 0.0f, 0.0f);
  211. L->denoising_albedo = make_float3(0.0f, 0.0f, 0.0f);
  212. L->denoising_depth = 0.0f;
  213. #endif
  214. #ifdef __KERNEL_DEBUG__
  215. L->debug_data.num_bvh_traversed_nodes = 0;
  216. L->debug_data.num_bvh_traversed_instances = 0;
  217. L->debug_data.num_bvh_intersections = 0;
  218. L->debug_data.num_ray_bounces = 0;
  219. #endif
  220. }
  221. ccl_device_inline void path_radiance_bsdf_bounce(KernelGlobals *kg,
  222. PathRadianceState *L_state,
  223. ccl_addr_space float3 *throughput,
  224. BsdfEval *bsdf_eval,
  225. float bsdf_pdf,
  226. int bounce,
  227. int bsdf_label)
  228. {
  229. float inverse_pdf = 1.0f / bsdf_pdf;
  230. #ifdef __PASSES__
  231. if (kernel_data.film.use_light_pass) {
  232. if (bounce == 0 && !(bsdf_label & LABEL_TRANSPARENT)) {
  233. /* first on directly visible surface */
  234. float3 value = *throughput * inverse_pdf;
  235. L_state->diffuse = bsdf_eval->diffuse * value;
  236. L_state->glossy = bsdf_eval->glossy * value;
  237. L_state->transmission = bsdf_eval->transmission * value;
  238. L_state->subsurface = bsdf_eval->subsurface * value;
  239. L_state->scatter = bsdf_eval->scatter * value;
  240. *throughput = L_state->diffuse + L_state->glossy + L_state->transmission +
  241. L_state->subsurface + L_state->scatter;
  242. L_state->direct = *throughput;
  243. }
  244. else {
  245. /* transparent bounce before first hit, or indirectly visible through BSDF */
  246. float3 sum = (bsdf_eval_sum(bsdf_eval) + bsdf_eval->transparent) * inverse_pdf;
  247. *throughput *= sum;
  248. }
  249. }
  250. else
  251. #endif
  252. {
  253. *throughput *= bsdf_eval->diffuse * inverse_pdf;
  254. }
  255. }
  256. ccl_device_inline void path_radiance_accum_emission(PathRadiance *L,
  257. ccl_addr_space PathState *state,
  258. float3 throughput,
  259. float3 value)
  260. {
  261. #ifdef __SHADOW_TRICKS__
  262. if (state->flag & PATH_RAY_SHADOW_CATCHER) {
  263. return;
  264. }
  265. #endif
  266. #ifdef __PASSES__
  267. if (L->use_light_pass) {
  268. if (state->bounce == 0)
  269. L->emission += throughput * value;
  270. else if (state->bounce == 1)
  271. L->direct_emission += throughput * value;
  272. else
  273. L->indirect += throughput * value;
  274. }
  275. else
  276. #endif
  277. {
  278. L->emission += throughput * value;
  279. }
  280. }
  281. ccl_device_inline void path_radiance_accum_ao(PathRadiance *L,
  282. ccl_addr_space PathState *state,
  283. float3 throughput,
  284. float3 alpha,
  285. float3 bsdf,
  286. float3 ao)
  287. {
  288. /* Store AO pass. */
  289. if (L->use_light_pass && state->bounce == 0) {
  290. L->ao += alpha * throughput * ao;
  291. }
  292. #ifdef __SHADOW_TRICKS__
  293. /* For shadow catcher, accumulate ratio. */
  294. if (state->flag & PATH_RAY_STORE_SHADOW_INFO) {
  295. float3 light = throughput * bsdf;
  296. L->path_total += light;
  297. L->path_total_shaded += ao * light;
  298. if (state->flag & PATH_RAY_SHADOW_CATCHER) {
  299. return;
  300. }
  301. }
  302. #endif
  303. #ifdef __PASSES__
  304. if (L->use_light_pass) {
  305. if (state->bounce == 0) {
  306. /* Directly visible lighting. */
  307. L->direct_diffuse += throughput * bsdf * ao;
  308. }
  309. else {
  310. /* Indirectly visible lighting after BSDF bounce. */
  311. L->indirect += throughput * bsdf * ao;
  312. }
  313. }
  314. else
  315. #endif
  316. {
  317. L->emission += throughput * bsdf * ao;
  318. }
  319. }
  320. ccl_device_inline void path_radiance_accum_total_ao(PathRadiance *L,
  321. ccl_addr_space PathState *state,
  322. float3 throughput,
  323. float3 bsdf)
  324. {
  325. #ifdef __SHADOW_TRICKS__
  326. if (state->flag & PATH_RAY_STORE_SHADOW_INFO) {
  327. L->path_total += throughput * bsdf;
  328. }
  329. #else
  330. (void)L;
  331. (void)state;
  332. (void)throughput;
  333. (void)bsdf;
  334. #endif
  335. }
  336. ccl_device_inline void path_radiance_accum_light(PathRadiance *L,
  337. ccl_addr_space PathState *state,
  338. float3 throughput,
  339. BsdfEval *bsdf_eval,
  340. float3 shadow,
  341. float shadow_fac,
  342. bool is_lamp)
  343. {
  344. #ifdef __SHADOW_TRICKS__
  345. if (state->flag & PATH_RAY_STORE_SHADOW_INFO) {
  346. float3 light = throughput * bsdf_eval->sum_no_mis;
  347. L->path_total += light;
  348. L->path_total_shaded += shadow * light;
  349. if (state->flag & PATH_RAY_SHADOW_CATCHER) {
  350. return;
  351. }
  352. }
  353. #endif
  354. #ifdef __PASSES__
  355. if (L->use_light_pass) {
  356. if (state->bounce == 0) {
  357. /* directly visible lighting */
  358. L->direct_diffuse += throughput * bsdf_eval->diffuse * shadow;
  359. L->direct_glossy += throughput * bsdf_eval->glossy * shadow;
  360. L->direct_transmission += throughput * bsdf_eval->transmission * shadow;
  361. L->direct_subsurface += throughput * bsdf_eval->subsurface * shadow;
  362. L->direct_scatter += throughput * bsdf_eval->scatter * shadow;
  363. if (is_lamp) {
  364. L->shadow.x += shadow.x * shadow_fac;
  365. L->shadow.y += shadow.y * shadow_fac;
  366. L->shadow.z += shadow.z * shadow_fac;
  367. }
  368. }
  369. else {
  370. /* indirectly visible lighting after BSDF bounce */
  371. L->indirect += throughput * bsdf_eval_sum(bsdf_eval) * shadow;
  372. }
  373. }
  374. else
  375. #endif
  376. {
  377. L->emission += throughput * bsdf_eval->diffuse * shadow;
  378. }
  379. }
  380. ccl_device_inline void path_radiance_accum_total_light(PathRadiance *L,
  381. ccl_addr_space PathState *state,
  382. float3 throughput,
  383. const BsdfEval *bsdf_eval)
  384. {
  385. #ifdef __SHADOW_TRICKS__
  386. if (state->flag & PATH_RAY_STORE_SHADOW_INFO) {
  387. L->path_total += throughput * bsdf_eval->sum_no_mis;
  388. }
  389. #else
  390. (void)L;
  391. (void)state;
  392. (void)throughput;
  393. (void)bsdf_eval;
  394. #endif
  395. }
  396. ccl_device_inline void path_radiance_accum_background(PathRadiance *L,
  397. ccl_addr_space PathState *state,
  398. float3 throughput,
  399. float3 value)
  400. {
  401. #ifdef __SHADOW_TRICKS__
  402. if (state->flag & PATH_RAY_STORE_SHADOW_INFO) {
  403. L->path_total += throughput * value;
  404. L->path_total_shaded += throughput * value * L->shadow_transparency;
  405. if (state->flag & PATH_RAY_SHADOW_CATCHER) {
  406. return;
  407. }
  408. }
  409. #endif
  410. #ifdef __PASSES__
  411. if (L->use_light_pass) {
  412. if (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND)
  413. L->background += throughput * value;
  414. else if (state->bounce == 1)
  415. L->direct_emission += throughput * value;
  416. else
  417. L->indirect += throughput * value;
  418. }
  419. else
  420. #endif
  421. {
  422. L->emission += throughput * value;
  423. }
  424. #ifdef __DENOISING_FEATURES__
  425. L->denoising_albedo += state->denoising_feature_weight * value;
  426. #endif /* __DENOISING_FEATURES__ */
  427. }
  428. ccl_device_inline void path_radiance_accum_transparent(PathRadiance *L,
  429. ccl_addr_space PathState *state,
  430. float3 throughput)
  431. {
  432. L->transparent += average(throughput);
  433. }
  434. #ifdef __SHADOW_TRICKS__
  435. ccl_device_inline void path_radiance_accum_shadowcatcher(PathRadiance *L,
  436. float3 throughput,
  437. float3 background)
  438. {
  439. L->shadow_throughput += average(throughput);
  440. L->shadow_background_color += throughput * background;
  441. L->has_shadow_catcher = 1;
  442. }
  443. #endif
  444. ccl_device_inline void path_radiance_sum_indirect(PathRadiance *L)
  445. {
  446. #ifdef __PASSES__
  447. /* this division is a bit ugly, but means we only have to keep track of
  448. * only a single throughput further along the path, here we recover just
  449. * the indirect path that is not influenced by any particular BSDF type */
  450. if (L->use_light_pass) {
  451. L->direct_emission = safe_divide_color(L->direct_emission, L->state.direct);
  452. L->direct_diffuse += L->state.diffuse * L->direct_emission;
  453. L->direct_glossy += L->state.glossy * L->direct_emission;
  454. L->direct_transmission += L->state.transmission * L->direct_emission;
  455. L->direct_subsurface += L->state.subsurface * L->direct_emission;
  456. L->direct_scatter += L->state.scatter * L->direct_emission;
  457. L->indirect = safe_divide_color(L->indirect, L->state.direct);
  458. L->indirect_diffuse += L->state.diffuse * L->indirect;
  459. L->indirect_glossy += L->state.glossy * L->indirect;
  460. L->indirect_transmission += L->state.transmission * L->indirect;
  461. L->indirect_subsurface += L->state.subsurface * L->indirect;
  462. L->indirect_scatter += L->state.scatter * L->indirect;
  463. }
  464. #endif
  465. }
  466. ccl_device_inline void path_radiance_reset_indirect(PathRadiance *L)
  467. {
  468. #ifdef __PASSES__
  469. if (L->use_light_pass) {
  470. L->state.diffuse = make_float3(0.0f, 0.0f, 0.0f);
  471. L->state.glossy = make_float3(0.0f, 0.0f, 0.0f);
  472. L->state.transmission = make_float3(0.0f, 0.0f, 0.0f);
  473. L->state.subsurface = make_float3(0.0f, 0.0f, 0.0f);
  474. L->state.scatter = make_float3(0.0f, 0.0f, 0.0f);
  475. L->direct_emission = make_float3(0.0f, 0.0f, 0.0f);
  476. L->indirect = make_float3(0.0f, 0.0f, 0.0f);
  477. }
  478. #endif
  479. }
  480. ccl_device_inline void path_radiance_copy_indirect(PathRadiance *L, const PathRadiance *L_src)
  481. {
  482. #ifdef __PASSES__
  483. if (L->use_light_pass) {
  484. L->state = L_src->state;
  485. L->direct_emission = L_src->direct_emission;
  486. L->indirect = L_src->indirect;
  487. }
  488. #endif
  489. }
  490. #ifdef __SHADOW_TRICKS__
  491. ccl_device_inline void path_radiance_sum_shadowcatcher(KernelGlobals *kg,
  492. PathRadiance *L,
  493. float3 *L_sum,
  494. float *alpha)
  495. {
  496. /* Calculate current shadow of the path. */
  497. float path_total = average(L->path_total);
  498. float shadow;
  499. if (UNLIKELY(!isfinite_safe(path_total))) {
  500. kernel_assert(!"Non-finite total radiance along the path");
  501. shadow = 0.0f;
  502. }
  503. else if (path_total == 0.0f) {
  504. shadow = L->shadow_transparency;
  505. }
  506. else {
  507. float path_total_shaded = average(L->path_total_shaded);
  508. shadow = path_total_shaded / path_total;
  509. }
  510. /* Calculate final light sum and transparency for shadow catcher object. */
  511. if (kernel_data.background.transparent) {
  512. *alpha -= L->shadow_throughput * shadow;
  513. }
  514. else {
  515. L->shadow_background_color *= shadow;
  516. *L_sum += L->shadow_background_color;
  517. }
  518. }
  519. #endif
  520. ccl_device_inline float3 path_radiance_clamp_and_sum(KernelGlobals *kg,
  521. PathRadiance *L,
  522. float *alpha)
  523. {
  524. float3 L_sum;
  525. /* Light Passes are used */
  526. #ifdef __PASSES__
  527. float3 L_direct, L_indirect;
  528. float clamp_direct = kernel_data.integrator.sample_clamp_direct;
  529. float clamp_indirect = kernel_data.integrator.sample_clamp_indirect;
  530. if (L->use_light_pass) {
  531. path_radiance_sum_indirect(L);
  532. L_direct = L->direct_diffuse + L->direct_glossy + L->direct_transmission +
  533. L->direct_subsurface + L->direct_scatter + L->emission;
  534. L_indirect = L->indirect_diffuse + L->indirect_glossy + L->indirect_transmission +
  535. L->indirect_subsurface + L->indirect_scatter;
  536. if (!kernel_data.background.transparent)
  537. L_direct += L->background;
  538. L_sum = L_direct + L_indirect;
  539. float sum = fabsf((L_sum).x) + fabsf((L_sum).y) + fabsf((L_sum).z);
  540. /* Reject invalid value */
  541. if (!isfinite_safe(sum)) {
  542. kernel_assert(!"Non-finite sum in path_radiance_clamp_and_sum!");
  543. L_sum = make_float3(0.0f, 0.0f, 0.0f);
  544. L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
  545. L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
  546. L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
  547. L->direct_subsurface = make_float3(0.0f, 0.0f, 0.0f);
  548. L->direct_scatter = make_float3(0.0f, 0.0f, 0.0f);
  549. L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
  550. L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
  551. L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
  552. L->indirect_subsurface = make_float3(0.0f, 0.0f, 0.0f);
  553. L->indirect_scatter = make_float3(0.0f, 0.0f, 0.0f);
  554. L->emission = make_float3(0.0f, 0.0f, 0.0f);
  555. }
  556. /* Clamp direct and indirect samples */
  557. # ifdef __CLAMP_SAMPLE__
  558. else if (sum > clamp_direct || sum > clamp_indirect) {
  559. float scale;
  560. /* Direct */
  561. float sum_direct = fabsf(L_direct.x) + fabsf(L_direct.y) + fabsf(L_direct.z);
  562. if (sum_direct > clamp_direct) {
  563. scale = clamp_direct / sum_direct;
  564. L_direct *= scale;
  565. L->direct_diffuse *= scale;
  566. L->direct_glossy *= scale;
  567. L->direct_transmission *= scale;
  568. L->direct_subsurface *= scale;
  569. L->direct_scatter *= scale;
  570. L->emission *= scale;
  571. L->background *= scale;
  572. }
  573. /* Indirect */
  574. float sum_indirect = fabsf(L_indirect.x) + fabsf(L_indirect.y) + fabsf(L_indirect.z);
  575. if (sum_indirect > clamp_indirect) {
  576. scale = clamp_indirect / sum_indirect;
  577. L_indirect *= scale;
  578. L->indirect_diffuse *= scale;
  579. L->indirect_glossy *= scale;
  580. L->indirect_transmission *= scale;
  581. L->indirect_subsurface *= scale;
  582. L->indirect_scatter *= scale;
  583. }
  584. /* Sum again, after clamping */
  585. L_sum = L_direct + L_indirect;
  586. }
  587. # endif
  588. }
  589. /* No Light Passes */
  590. else
  591. #endif
  592. {
  593. L_sum = L->emission;
  594. /* Reject invalid value */
  595. float sum = fabsf((L_sum).x) + fabsf((L_sum).y) + fabsf((L_sum).z);
  596. if (!isfinite_safe(sum)) {
  597. kernel_assert(!"Non-finite final sum in path_radiance_clamp_and_sum!");
  598. L_sum = make_float3(0.0f, 0.0f, 0.0f);
  599. }
  600. }
  601. /* Compute alpha. */
  602. *alpha = 1.0f - L->transparent;
  603. /* Add shadow catcher contributions. */
  604. #ifdef __SHADOW_TRICKS__
  605. if (L->has_shadow_catcher) {
  606. path_radiance_sum_shadowcatcher(kg, L, &L_sum, alpha);
  607. }
  608. #endif /* __SHADOW_TRICKS__ */
  609. return L_sum;
  610. }
  611. ccl_device_inline void path_radiance_split_denoising(KernelGlobals *kg,
  612. PathRadiance *L,
  613. float3 *noisy,
  614. float3 *clean)
  615. {
  616. #ifdef __PASSES__
  617. kernel_assert(L->use_light_pass);
  618. *clean = L->emission + L->background;
  619. *noisy = L->direct_scatter + L->indirect_scatter;
  620. # define ADD_COMPONENT(flag, component) \
  621. if (kernel_data.film.denoising_flags & flag) \
  622. *clean += component; \
  623. else \
  624. *noisy += component;
  625. ADD_COMPONENT(DENOISING_CLEAN_DIFFUSE_DIR, L->direct_diffuse);
  626. ADD_COMPONENT(DENOISING_CLEAN_DIFFUSE_IND, L->indirect_diffuse);
  627. ADD_COMPONENT(DENOISING_CLEAN_GLOSSY_DIR, L->direct_glossy);
  628. ADD_COMPONENT(DENOISING_CLEAN_GLOSSY_IND, L->indirect_glossy);
  629. ADD_COMPONENT(DENOISING_CLEAN_TRANSMISSION_DIR, L->direct_transmission);
  630. ADD_COMPONENT(DENOISING_CLEAN_TRANSMISSION_IND, L->indirect_transmission);
  631. ADD_COMPONENT(DENOISING_CLEAN_SUBSURFACE_DIR, L->direct_subsurface);
  632. ADD_COMPONENT(DENOISING_CLEAN_SUBSURFACE_IND, L->indirect_subsurface);
  633. # undef ADD_COMPONENT
  634. #else
  635. *noisy = L->emission;
  636. *clean = make_float3(0.0f, 0.0f, 0.0f);
  637. #endif
  638. #ifdef __SHADOW_TRICKS__
  639. if (L->has_shadow_catcher) {
  640. *noisy += L->shadow_background_color;
  641. }
  642. #endif
  643. *noisy = ensure_finite3(*noisy);
  644. *clean = ensure_finite3(*clean);
  645. }
  646. ccl_device_inline void path_radiance_accum_sample(PathRadiance *L, PathRadiance *L_sample)
  647. {
  648. #ifdef __SPLIT_KERNEL__
  649. # define safe_float3_add(f, v) \
  650. do { \
  651. ccl_global float *p = (ccl_global float *)(&(f)); \
  652. atomic_add_and_fetch_float(p + 0, (v).x); \
  653. atomic_add_and_fetch_float(p + 1, (v).y); \
  654. atomic_add_and_fetch_float(p + 2, (v).z); \
  655. } while (0)
  656. # define safe_float_add(f, v) atomic_add_and_fetch_float(&(f), (v))
  657. #else
  658. # define safe_float3_add(f, v) (f) += (v)
  659. # define safe_float_add(f, v) (f) += (v)
  660. #endif /* __SPLIT_KERNEL__ */
  661. #ifdef __PASSES__
  662. safe_float3_add(L->direct_diffuse, L_sample->direct_diffuse);
  663. safe_float3_add(L->direct_glossy, L_sample->direct_glossy);
  664. safe_float3_add(L->direct_transmission, L_sample->direct_transmission);
  665. safe_float3_add(L->direct_subsurface, L_sample->direct_subsurface);
  666. safe_float3_add(L->direct_scatter, L_sample->direct_scatter);
  667. safe_float3_add(L->indirect_diffuse, L_sample->indirect_diffuse);
  668. safe_float3_add(L->indirect_glossy, L_sample->indirect_glossy);
  669. safe_float3_add(L->indirect_transmission, L_sample->indirect_transmission);
  670. safe_float3_add(L->indirect_subsurface, L_sample->indirect_subsurface);
  671. safe_float3_add(L->indirect_scatter, L_sample->indirect_scatter);
  672. safe_float3_add(L->background, L_sample->background);
  673. safe_float3_add(L->ao, L_sample->ao);
  674. safe_float3_add(L->shadow, L_sample->shadow);
  675. safe_float_add(L->mist, L_sample->mist);
  676. #endif /* __PASSES__ */
  677. safe_float3_add(L->emission, L_sample->emission);
  678. #undef safe_float_add
  679. #undef safe_float3_add
  680. }
  681. CCL_NAMESPACE_END