kernel_path.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. #ifdef __OSL__
  17. # include "kernel/osl/osl_shader.h"
  18. #endif
  19. #include "kernel/kernel_random.h"
  20. #include "kernel/kernel_projection.h"
  21. #include "kernel/kernel_montecarlo.h"
  22. #include "kernel/kernel_differential.h"
  23. #include "kernel/kernel_camera.h"
  24. #include "kernel/geom/geom.h"
  25. #include "kernel/bvh/bvh.h"
  26. #include "kernel/kernel_accumulate.h"
  27. #include "kernel/kernel_shader.h"
  28. #include "kernel/kernel_light.h"
  29. #include "kernel/kernel_passes.h"
  30. #if defined(__VOLUME__) || defined(__SUBSURFACE__)
  31. # include "kernel/kernel_volume.h"
  32. #endif
  33. #ifdef __SUBSURFACE__
  34. # include "kernel/kernel_subsurface.h"
  35. #endif
  36. #include "kernel/kernel_path_state.h"
  37. #include "kernel/kernel_shadow.h"
  38. #include "kernel/kernel_emission.h"
  39. #include "kernel/kernel_path_common.h"
  40. #include "kernel/kernel_path_surface.h"
  41. #include "kernel/kernel_path_volume.h"
  42. #include "kernel/kernel_path_subsurface.h"
  43. CCL_NAMESPACE_BEGIN
  44. ccl_device_forceinline bool kernel_path_scene_intersect(KernelGlobals *kg,
  45. ccl_addr_space PathState *state,
  46. Ray *ray,
  47. Intersection *isect,
  48. PathRadiance *L)
  49. {
  50. PROFILING_INIT(kg, PROFILING_SCENE_INTERSECT);
  51. uint visibility = path_state_ray_visibility(kg, state);
  52. if (path_state_ao_bounce(kg, state)) {
  53. visibility = PATH_RAY_SHADOW;
  54. ray->t = kernel_data.background.ao_distance;
  55. }
  56. bool hit = scene_intersect(kg, *ray, visibility, isect);
  57. #ifdef __KERNEL_DEBUG__
  58. if (state->flag & PATH_RAY_CAMERA) {
  59. L->debug_data.num_bvh_traversed_nodes += isect->num_traversed_nodes;
  60. L->debug_data.num_bvh_traversed_instances += isect->num_traversed_instances;
  61. L->debug_data.num_bvh_intersections += isect->num_intersections;
  62. }
  63. L->debug_data.num_ray_bounces++;
  64. #endif /* __KERNEL_DEBUG__ */
  65. return hit;
  66. }
  67. ccl_device_forceinline void kernel_path_lamp_emission(KernelGlobals *kg,
  68. ccl_addr_space PathState *state,
  69. Ray *ray,
  70. float3 throughput,
  71. ccl_addr_space Intersection *isect,
  72. ShaderData *emission_sd,
  73. PathRadiance *L)
  74. {
  75. PROFILING_INIT(kg, PROFILING_INDIRECT_EMISSION);
  76. #ifdef __LAMP_MIS__
  77. if (kernel_data.integrator.use_lamp_mis && !(state->flag & PATH_RAY_CAMERA)) {
  78. /* ray starting from previous non-transparent bounce */
  79. Ray light_ray;
  80. light_ray.P = ray->P - state->ray_t * ray->D;
  81. state->ray_t += isect->t;
  82. light_ray.D = ray->D;
  83. light_ray.t = state->ray_t;
  84. light_ray.time = ray->time;
  85. light_ray.dD = ray->dD;
  86. light_ray.dP = ray->dP;
  87. /* intersect with lamp */
  88. float3 emission;
  89. if (indirect_lamp_emission(kg, emission_sd, state, &light_ray, &emission))
  90. path_radiance_accum_emission(L, state, throughput, emission);
  91. }
  92. #endif /* __LAMP_MIS__ */
  93. }
  94. ccl_device_forceinline void kernel_path_background(KernelGlobals *kg,
  95. ccl_addr_space PathState *state,
  96. ccl_addr_space Ray *ray,
  97. float3 throughput,
  98. ShaderData *sd,
  99. PathRadiance *L)
  100. {
  101. /* eval background shader if nothing hit */
  102. if (kernel_data.background.transparent && (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
  103. L->transparent += average(throughput);
  104. #ifdef __PASSES__
  105. if (!(kernel_data.film.light_pass_flag & PASSMASK(BACKGROUND)))
  106. #endif /* __PASSES__ */
  107. return;
  108. }
  109. /* When using the ao bounces approximation, adjust background
  110. * shader intensity with ao factor. */
  111. if (path_state_ao_bounce(kg, state)) {
  112. throughput *= kernel_data.background.ao_bounces_factor;
  113. }
  114. #ifdef __BACKGROUND__
  115. /* sample background shader */
  116. float3 L_background = indirect_background(kg, sd, state, ray);
  117. path_radiance_accum_background(L, state, throughput, L_background);
  118. #endif /* __BACKGROUND__ */
  119. }
  120. #ifndef __SPLIT_KERNEL__
  121. # ifdef __VOLUME__
  122. ccl_device_forceinline VolumeIntegrateResult kernel_path_volume(KernelGlobals *kg,
  123. ShaderData *sd,
  124. PathState *state,
  125. Ray *ray,
  126. float3 *throughput,
  127. ccl_addr_space Intersection *isect,
  128. bool hit,
  129. ShaderData *emission_sd,
  130. PathRadiance *L)
  131. {
  132. PROFILING_INIT(kg, PROFILING_VOLUME);
  133. /* Sanitize volume stack. */
  134. if (!hit) {
  135. kernel_volume_clean_stack(kg, state->volume_stack);
  136. }
  137. if (state->volume_stack[0].shader == SHADER_NONE) {
  138. return VOLUME_PATH_ATTENUATED;
  139. }
  140. /* volume attenuation, emission, scatter */
  141. Ray volume_ray = *ray;
  142. volume_ray.t = (hit) ? isect->t : FLT_MAX;
  143. bool heterogeneous = volume_stack_is_heterogeneous(kg, state->volume_stack);
  144. # ifdef __VOLUME_DECOUPLED__
  145. int sampling_method = volume_stack_sampling_method(kg, state->volume_stack);
  146. bool direct = (state->flag & PATH_RAY_CAMERA) != 0;
  147. bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, direct, sampling_method);
  148. if (decoupled) {
  149. /* cache steps along volume for repeated sampling */
  150. VolumeSegment volume_segment;
  151. shader_setup_from_volume(kg, sd, &volume_ray);
  152. kernel_volume_decoupled_record(kg, state, &volume_ray, sd, &volume_segment, heterogeneous);
  153. volume_segment.sampling_method = sampling_method;
  154. /* emission */
  155. if (volume_segment.closure_flag & SD_EMISSION)
  156. path_radiance_accum_emission(L, state, *throughput, volume_segment.accum_emission);
  157. /* scattering */
  158. VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
  159. if (volume_segment.closure_flag & SD_SCATTER) {
  160. int all = kernel_data.integrator.sample_all_lights_indirect;
  161. /* direct light sampling */
  162. kernel_branched_path_volume_connect_light(
  163. kg, sd, emission_sd, *throughput, state, L, all, &volume_ray, &volume_segment);
  164. /* indirect sample. if we use distance sampling and take just
  165. * one sample for direct and indirect light, we could share
  166. * this computation, but makes code a bit complex */
  167. float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
  168. float rscatter = path_state_rng_1D(kg, state, PRNG_SCATTER_DISTANCE);
  169. result = kernel_volume_decoupled_scatter(
  170. kg, state, &volume_ray, sd, throughput, rphase, rscatter, &volume_segment, NULL, true);
  171. }
  172. /* free cached steps */
  173. kernel_volume_decoupled_free(kg, &volume_segment);
  174. if (result == VOLUME_PATH_SCATTERED) {
  175. if (kernel_path_volume_bounce(kg, sd, throughput, state, &L->state, ray))
  176. return VOLUME_PATH_SCATTERED;
  177. else
  178. return VOLUME_PATH_MISSED;
  179. }
  180. else {
  181. *throughput *= volume_segment.accum_transmittance;
  182. }
  183. }
  184. else
  185. # endif /* __VOLUME_DECOUPLED__ */
  186. {
  187. /* integrate along volume segment with distance sampling */
  188. VolumeIntegrateResult result = kernel_volume_integrate(
  189. kg, state, sd, &volume_ray, L, throughput, heterogeneous);
  190. # ifdef __VOLUME_SCATTER__
  191. if (result == VOLUME_PATH_SCATTERED) {
  192. /* direct lighting */
  193. kernel_path_volume_connect_light(kg, sd, emission_sd, *throughput, state, L);
  194. /* indirect light bounce */
  195. if (kernel_path_volume_bounce(kg, sd, throughput, state, &L->state, ray))
  196. return VOLUME_PATH_SCATTERED;
  197. else
  198. return VOLUME_PATH_MISSED;
  199. }
  200. # endif /* __VOLUME_SCATTER__ */
  201. }
  202. return VOLUME_PATH_ATTENUATED;
  203. }
  204. # endif /* __VOLUME__ */
  205. #endif /* __SPLIT_KERNEL__ */
  206. ccl_device_forceinline bool kernel_path_shader_apply(KernelGlobals *kg,
  207. ShaderData *sd,
  208. ccl_addr_space PathState *state,
  209. ccl_addr_space Ray *ray,
  210. float3 throughput,
  211. ShaderData *emission_sd,
  212. PathRadiance *L,
  213. ccl_global float *buffer)
  214. {
  215. PROFILING_INIT(kg, PROFILING_SHADER_APPLY);
  216. #ifdef __SHADOW_TRICKS__
  217. if ((sd->object_flag & SD_OBJECT_SHADOW_CATCHER)) {
  218. if (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND) {
  219. state->flag |= (PATH_RAY_SHADOW_CATCHER | PATH_RAY_STORE_SHADOW_INFO);
  220. float3 bg = make_float3(0.0f, 0.0f, 0.0f);
  221. if (!kernel_data.background.transparent) {
  222. bg = indirect_background(kg, emission_sd, state, ray);
  223. }
  224. path_radiance_accum_shadowcatcher(L, throughput, bg);
  225. }
  226. }
  227. else if (state->flag & PATH_RAY_SHADOW_CATCHER) {
  228. /* Only update transparency after shadow catcher bounce. */
  229. L->shadow_transparency *= average(shader_bsdf_transparency(kg, sd));
  230. }
  231. #endif /* __SHADOW_TRICKS__ */
  232. /* holdout */
  233. #ifdef __HOLDOUT__
  234. if (((sd->flag & SD_HOLDOUT) || (sd->object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
  235. (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
  236. if (kernel_data.background.transparent) {
  237. float3 holdout_weight;
  238. if (sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
  239. holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
  240. }
  241. else {
  242. holdout_weight = shader_holdout_eval(kg, sd);
  243. }
  244. /* any throughput is ok, should all be identical here */
  245. L->transparent += average(holdout_weight * throughput);
  246. }
  247. if (sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
  248. return false;
  249. }
  250. }
  251. #endif /* __HOLDOUT__ */
  252. /* holdout mask objects do not write data passes */
  253. kernel_write_data_passes(kg, buffer, L, sd, state, throughput);
  254. /* blurring of bsdf after bounces, for rays that have a small likelihood
  255. * of following this particular path (diffuse, rough glossy) */
  256. if (kernel_data.integrator.filter_glossy != FLT_MAX) {
  257. float blur_pdf = kernel_data.integrator.filter_glossy * state->min_ray_pdf;
  258. if (blur_pdf < 1.0f) {
  259. float blur_roughness = sqrtf(1.0f - blur_pdf) * 0.5f;
  260. shader_bsdf_blur(kg, sd, blur_roughness);
  261. }
  262. }
  263. #ifdef __EMISSION__
  264. /* emission */
  265. if (sd->flag & SD_EMISSION) {
  266. float3 emission = indirect_primitive_emission(
  267. kg, sd, sd->ray_length, state->flag, state->ray_pdf);
  268. path_radiance_accum_emission(L, state, throughput, emission);
  269. }
  270. #endif /* __EMISSION__ */
  271. return true;
  272. }
  273. ccl_device_noinline void kernel_path_ao(KernelGlobals *kg,
  274. ShaderData *sd,
  275. ShaderData *emission_sd,
  276. PathRadiance *L,
  277. ccl_addr_space PathState *state,
  278. float3 throughput,
  279. float3 ao_alpha)
  280. {
  281. PROFILING_INIT(kg, PROFILING_AO);
  282. /* todo: solve correlation */
  283. float bsdf_u, bsdf_v;
  284. path_state_rng_2D(kg, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
  285. float ao_factor = kernel_data.background.ao_factor;
  286. float3 ao_N;
  287. float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
  288. float3 ao_D;
  289. float ao_pdf;
  290. sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
  291. if (dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
  292. Ray light_ray;
  293. float3 ao_shadow;
  294. light_ray.P = ray_offset(sd->P, sd->Ng);
  295. light_ray.D = ao_D;
  296. light_ray.t = kernel_data.background.ao_distance;
  297. light_ray.time = sd->time;
  298. light_ray.dP = sd->dP;
  299. light_ray.dD = differential3_zero();
  300. if (!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &ao_shadow)) {
  301. path_radiance_accum_ao(L, state, throughput, ao_alpha, ao_bsdf, ao_shadow);
  302. }
  303. else {
  304. path_radiance_accum_total_ao(L, state, throughput, ao_bsdf);
  305. }
  306. }
  307. }
  308. #ifndef __SPLIT_KERNEL__
  309. # if defined(__BRANCHED_PATH__) || defined(__BAKING__)
  310. ccl_device void kernel_path_indirect(KernelGlobals *kg,
  311. ShaderData *sd,
  312. ShaderData *emission_sd,
  313. Ray *ray,
  314. float3 throughput,
  315. PathState *state,
  316. PathRadiance *L)
  317. {
  318. # ifdef __SUBSURFACE__
  319. SubsurfaceIndirectRays ss_indirect;
  320. kernel_path_subsurface_init_indirect(&ss_indirect);
  321. for (;;) {
  322. # endif /* __SUBSURFACE__ */
  323. /* path iteration */
  324. for (;;) {
  325. /* Find intersection with objects in scene. */
  326. Intersection isect;
  327. bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
  328. /* Find intersection with lamps and compute emission for MIS. */
  329. kernel_path_lamp_emission(kg, state, ray, throughput, &isect, sd, L);
  330. # ifdef __VOLUME__
  331. /* Volume integration. */
  332. VolumeIntegrateResult result = kernel_path_volume(
  333. kg, sd, state, ray, &throughput, &isect, hit, emission_sd, L);
  334. if (result == VOLUME_PATH_SCATTERED) {
  335. continue;
  336. }
  337. else if (result == VOLUME_PATH_MISSED) {
  338. break;
  339. }
  340. # endif /* __VOLUME__*/
  341. /* Shade background. */
  342. if (!hit) {
  343. kernel_path_background(kg, state, ray, throughput, sd, L);
  344. break;
  345. }
  346. else if (path_state_ao_bounce(kg, state)) {
  347. break;
  348. }
  349. /* Setup shader data. */
  350. shader_setup_from_ray(kg, sd, &isect, ray);
  351. /* Skip most work for volume bounding surface. */
  352. # ifdef __VOLUME__
  353. if (!(sd->flag & SD_HAS_ONLY_VOLUME)) {
  354. # endif
  355. /* Evaluate shader. */
  356. shader_eval_surface(kg, sd, state, state->flag);
  357. shader_prepare_closures(sd, state);
  358. /* Apply shadow catcher, holdout, emission. */
  359. if (!kernel_path_shader_apply(kg, sd, state, ray, throughput, emission_sd, L, NULL)) {
  360. break;
  361. }
  362. /* path termination. this is a strange place to put the termination, it's
  363. * mainly due to the mixed in MIS that we use. gives too many unneeded
  364. * shader evaluations, only need emission if we are going to terminate */
  365. float probability = path_state_continuation_probability(kg, state, throughput);
  366. if (probability == 0.0f) {
  367. break;
  368. }
  369. else if (probability != 1.0f) {
  370. float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
  371. if (terminate >= probability)
  372. break;
  373. throughput /= probability;
  374. }
  375. kernel_update_denoising_features(kg, sd, state, L);
  376. # ifdef __AO__
  377. /* ambient occlusion */
  378. if (kernel_data.integrator.use_ambient_occlusion) {
  379. kernel_path_ao(kg, sd, emission_sd, L, state, throughput, make_float3(0.0f, 0.0f, 0.0f));
  380. }
  381. # endif /* __AO__ */
  382. # ifdef __SUBSURFACE__
  383. /* bssrdf scatter to a different location on the same object, replacing
  384. * the closures with a diffuse BSDF */
  385. if (sd->flag & SD_BSSRDF) {
  386. if (kernel_path_subsurface_scatter(
  387. kg, sd, emission_sd, L, state, ray, &throughput, &ss_indirect)) {
  388. break;
  389. }
  390. }
  391. # endif /* __SUBSURFACE__ */
  392. # if defined(__EMISSION__)
  393. if (kernel_data.integrator.use_direct_light) {
  394. int all = (kernel_data.integrator.sample_all_lights_indirect) ||
  395. (state->flag & PATH_RAY_SHADOW_CATCHER);
  396. kernel_branched_path_surface_connect_light(
  397. kg, sd, emission_sd, state, throughput, 1.0f, L, all);
  398. }
  399. # endif /* defined(__EMISSION__) */
  400. # ifdef __VOLUME__
  401. }
  402. # endif
  403. if (!kernel_path_surface_bounce(kg, sd, &throughput, state, &L->state, ray))
  404. break;
  405. }
  406. # ifdef __SUBSURFACE__
  407. /* Trace indirect subsurface rays by restarting the loop. this uses less
  408. * stack memory than invoking kernel_path_indirect.
  409. */
  410. if (ss_indirect.num_rays) {
  411. kernel_path_subsurface_setup_indirect(kg, &ss_indirect, state, ray, L, &throughput);
  412. }
  413. else {
  414. break;
  415. }
  416. }
  417. # endif /* __SUBSURFACE__ */
  418. }
  419. # endif /* defined(__BRANCHED_PATH__) || defined(__BAKING__) */
  420. ccl_device_forceinline void kernel_path_integrate(KernelGlobals *kg,
  421. PathState *state,
  422. float3 throughput,
  423. Ray *ray,
  424. PathRadiance *L,
  425. ccl_global float *buffer,
  426. ShaderData *emission_sd)
  427. {
  428. PROFILING_INIT(kg, PROFILING_PATH_INTEGRATE);
  429. /* Shader data memory used for both volumes and surfaces, saves stack space. */
  430. ShaderData sd;
  431. # ifdef __SUBSURFACE__
  432. SubsurfaceIndirectRays ss_indirect;
  433. kernel_path_subsurface_init_indirect(&ss_indirect);
  434. for (;;) {
  435. # endif /* __SUBSURFACE__ */
  436. /* path iteration */
  437. for (;;) {
  438. /* Find intersection with objects in scene. */
  439. Intersection isect;
  440. bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
  441. /* Find intersection with lamps and compute emission for MIS. */
  442. kernel_path_lamp_emission(kg, state, ray, throughput, &isect, &sd, L);
  443. # ifdef __VOLUME__
  444. /* Volume integration. */
  445. VolumeIntegrateResult result = kernel_path_volume(
  446. kg, &sd, state, ray, &throughput, &isect, hit, emission_sd, L);
  447. if (result == VOLUME_PATH_SCATTERED) {
  448. continue;
  449. }
  450. else if (result == VOLUME_PATH_MISSED) {
  451. break;
  452. }
  453. # endif /* __VOLUME__*/
  454. /* Shade background. */
  455. if (!hit) {
  456. kernel_path_background(kg, state, ray, throughput, &sd, L);
  457. break;
  458. }
  459. else if (path_state_ao_bounce(kg, state)) {
  460. break;
  461. }
  462. /* Setup shader data. */
  463. shader_setup_from_ray(kg, &sd, &isect, ray);
  464. /* Skip most work for volume bounding surface. */
  465. # ifdef __VOLUME__
  466. if (!(sd.flag & SD_HAS_ONLY_VOLUME)) {
  467. # endif
  468. /* Evaluate shader. */
  469. shader_eval_surface(kg, &sd, state, state->flag);
  470. shader_prepare_closures(&sd, state);
  471. /* Apply shadow catcher, holdout, emission. */
  472. if (!kernel_path_shader_apply(kg, &sd, state, ray, throughput, emission_sd, L, buffer)) {
  473. break;
  474. }
  475. /* path termination. this is a strange place to put the termination, it's
  476. * mainly due to the mixed in MIS that we use. gives too many unneeded
  477. * shader evaluations, only need emission if we are going to terminate */
  478. float probability = path_state_continuation_probability(kg, state, throughput);
  479. if (probability == 0.0f) {
  480. break;
  481. }
  482. else if (probability != 1.0f) {
  483. float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
  484. if (terminate >= probability)
  485. break;
  486. throughput /= probability;
  487. }
  488. kernel_update_denoising_features(kg, &sd, state, L);
  489. # ifdef __AO__
  490. /* ambient occlusion */
  491. if (kernel_data.integrator.use_ambient_occlusion) {
  492. kernel_path_ao(kg, &sd, emission_sd, L, state, throughput, shader_bsdf_alpha(kg, &sd));
  493. }
  494. # endif /* __AO__ */
  495. # ifdef __SUBSURFACE__
  496. /* bssrdf scatter to a different location on the same object, replacing
  497. * the closures with a diffuse BSDF */
  498. if (sd.flag & SD_BSSRDF) {
  499. if (kernel_path_subsurface_scatter(
  500. kg, &sd, emission_sd, L, state, ray, &throughput, &ss_indirect)) {
  501. break;
  502. }
  503. }
  504. # endif /* __SUBSURFACE__ */
  505. /* direct lighting */
  506. kernel_path_surface_connect_light(kg, &sd, emission_sd, throughput, state, L);
  507. # ifdef __VOLUME__
  508. }
  509. # endif
  510. /* compute direct lighting and next bounce */
  511. if (!kernel_path_surface_bounce(kg, &sd, &throughput, state, &L->state, ray))
  512. break;
  513. }
  514. # ifdef __SUBSURFACE__
  515. /* Trace indirect subsurface rays by restarting the loop. this uses less
  516. * stack memory than invoking kernel_path_indirect.
  517. */
  518. if (ss_indirect.num_rays) {
  519. kernel_path_subsurface_setup_indirect(kg, &ss_indirect, state, ray, L, &throughput);
  520. }
  521. else {
  522. break;
  523. }
  524. }
  525. # endif /* __SUBSURFACE__ */
  526. }
  527. ccl_device void kernel_path_trace(
  528. KernelGlobals *kg, ccl_global float *buffer, int sample, int x, int y, int offset, int stride)
  529. {
  530. PROFILING_INIT(kg, PROFILING_RAY_SETUP);
  531. /* buffer offset */
  532. int index = offset + x + y * stride;
  533. int pass_stride = kernel_data.film.pass_stride;
  534. buffer += index * pass_stride;
  535. /* Initialize random numbers and sample ray. */
  536. uint rng_hash;
  537. Ray ray;
  538. kernel_path_trace_setup(kg, sample, x, y, &rng_hash, &ray);
  539. if (ray.t == 0.0f) {
  540. return;
  541. }
  542. /* Initialize state. */
  543. float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
  544. PathRadiance L;
  545. path_radiance_init(&L, kernel_data.film.use_light_pass);
  546. ShaderDataTinyStorage emission_sd_storage;
  547. ShaderData *emission_sd = AS_SHADER_DATA(&emission_sd_storage);
  548. PathState state;
  549. path_state_init(kg, emission_sd, &state, rng_hash, sample, &ray);
  550. /* Integrate. */
  551. kernel_path_integrate(kg, &state, throughput, &ray, &L, buffer, emission_sd);
  552. kernel_write_result(kg, buffer, sample, &L);
  553. }
  554. #endif /* __SPLIT_KERNEL__ */
  555. CCL_NAMESPACE_END