kernel_volume.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. /* Events for probalistic scattering */
  18. typedef enum VolumeIntegrateResult {
  19. VOLUME_PATH_SCATTERED = 0,
  20. VOLUME_PATH_ATTENUATED = 1,
  21. VOLUME_PATH_MISSED = 2
  22. } VolumeIntegrateResult;
  23. /* Volume shader properties
  24. *
  25. * extinction coefficient = absorption coefficient + scattering coefficient
  26. * sigma_t = sigma_a + sigma_s */
  27. typedef struct VolumeShaderCoefficients {
  28. float3 sigma_t;
  29. float3 sigma_s;
  30. float3 emission;
  31. } VolumeShaderCoefficients;
  32. #ifdef __VOLUME__
  33. /* evaluate shader to get extinction coefficient at P */
  34. ccl_device_inline bool volume_shader_extinction_sample(KernelGlobals *kg,
  35. ShaderData *sd,
  36. ccl_addr_space PathState *state,
  37. float3 P,
  38. float3 *extinction)
  39. {
  40. sd->P = P;
  41. shader_eval_volume(kg, sd, state, state->volume_stack, PATH_RAY_SHADOW);
  42. if (sd->flag & SD_EXTINCTION) {
  43. *extinction = sd->closure_transparent_extinction;
  44. return true;
  45. }
  46. else {
  47. return false;
  48. }
  49. }
  50. /* evaluate shader to get absorption, scattering and emission at P */
  51. ccl_device_inline bool volume_shader_sample(KernelGlobals *kg,
  52. ShaderData *sd,
  53. ccl_addr_space PathState *state,
  54. float3 P,
  55. VolumeShaderCoefficients *coeff)
  56. {
  57. sd->P = P;
  58. shader_eval_volume(kg, sd, state, state->volume_stack, state->flag);
  59. if (!(sd->flag & (SD_EXTINCTION | SD_SCATTER | SD_EMISSION)))
  60. return false;
  61. coeff->sigma_s = make_float3(0.0f, 0.0f, 0.0f);
  62. coeff->sigma_t = (sd->flag & SD_EXTINCTION) ? sd->closure_transparent_extinction :
  63. make_float3(0.0f, 0.0f, 0.0f);
  64. coeff->emission = (sd->flag & SD_EMISSION) ? sd->closure_emission_background :
  65. make_float3(0.0f, 0.0f, 0.0f);
  66. if (sd->flag & SD_SCATTER) {
  67. for (int i = 0; i < sd->num_closure; i++) {
  68. const ShaderClosure *sc = &sd->closure[i];
  69. if (CLOSURE_IS_VOLUME(sc->type))
  70. coeff->sigma_s += sc->weight;
  71. }
  72. }
  73. return true;
  74. }
  75. #endif /* __VOLUME__ */
  76. ccl_device float3 volume_color_transmittance(float3 sigma, float t)
  77. {
  78. return exp3(-sigma * t);
  79. }
  80. ccl_device float kernel_volume_channel_get(float3 value, int channel)
  81. {
  82. return (channel == 0) ? value.x : ((channel == 1) ? value.y : value.z);
  83. }
  84. #ifdef __VOLUME__
  85. ccl_device bool volume_stack_is_heterogeneous(KernelGlobals *kg, ccl_addr_space VolumeStack *stack)
  86. {
  87. for (int i = 0; stack[i].shader != SHADER_NONE; i++) {
  88. int shader_flag = kernel_tex_fetch(__shaders, (stack[i].shader & SHADER_MASK)).flags;
  89. if (shader_flag & SD_HETEROGENEOUS_VOLUME) {
  90. return true;
  91. }
  92. else if (shader_flag & SD_NEED_ATTRIBUTES) {
  93. /* We want to render world or objects without any volume grids
  94. * as homogeneous, but can only verify this at run-time since other
  95. * heterogeneous volume objects may be using the same shader. */
  96. int object = stack[i].object;
  97. if (object != OBJECT_NONE) {
  98. int object_flag = kernel_tex_fetch(__object_flag, object);
  99. if (object_flag & SD_OBJECT_HAS_VOLUME_ATTRIBUTES) {
  100. return true;
  101. }
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. ccl_device int volume_stack_sampling_method(KernelGlobals *kg, VolumeStack *stack)
  108. {
  109. if (kernel_data.integrator.num_all_lights == 0)
  110. return 0;
  111. int method = -1;
  112. for (int i = 0; stack[i].shader != SHADER_NONE; i++) {
  113. int shader_flag = kernel_tex_fetch(__shaders, (stack[i].shader & SHADER_MASK)).flags;
  114. if (shader_flag & SD_VOLUME_MIS) {
  115. return SD_VOLUME_MIS;
  116. }
  117. else if (shader_flag & SD_VOLUME_EQUIANGULAR) {
  118. if (method == 0)
  119. return SD_VOLUME_MIS;
  120. method = SD_VOLUME_EQUIANGULAR;
  121. }
  122. else {
  123. if (method == SD_VOLUME_EQUIANGULAR)
  124. return SD_VOLUME_MIS;
  125. method = 0;
  126. }
  127. }
  128. return method;
  129. }
  130. ccl_device_inline void kernel_volume_step_init(KernelGlobals *kg,
  131. ccl_addr_space PathState *state,
  132. float t,
  133. float *step_size,
  134. float *step_offset)
  135. {
  136. const int max_steps = kernel_data.integrator.volume_max_steps;
  137. float step = min(kernel_data.integrator.volume_step_size, t);
  138. /* compute exact steps in advance for malloc */
  139. if (t > max_steps * step) {
  140. step = t / (float)max_steps;
  141. }
  142. *step_size = step;
  143. *step_offset = path_state_rng_1D_hash(kg, state, 0x1e31d8a4) * step;
  144. }
  145. /* Volume Shadows
  146. *
  147. * These functions are used to attenuate shadow rays to lights. Both absorption
  148. * and scattering will block light, represented by the extinction coefficient. */
  149. /* homogeneous volume: assume shader evaluation at the starts gives
  150. * the extinction coefficient for the entire line segment */
  151. ccl_device void kernel_volume_shadow_homogeneous(KernelGlobals *kg,
  152. ccl_addr_space PathState *state,
  153. Ray *ray,
  154. ShaderData *sd,
  155. float3 *throughput)
  156. {
  157. float3 sigma_t;
  158. if (volume_shader_extinction_sample(kg, sd, state, ray->P, &sigma_t))
  159. *throughput *= volume_color_transmittance(sigma_t, ray->t);
  160. }
  161. /* heterogeneous volume: integrate stepping through the volume until we
  162. * reach the end, get absorbed entirely, or run out of iterations */
  163. ccl_device void kernel_volume_shadow_heterogeneous(KernelGlobals *kg,
  164. ccl_addr_space PathState *state,
  165. Ray *ray,
  166. ShaderData *sd,
  167. float3 *throughput)
  168. {
  169. float3 tp = *throughput;
  170. const float tp_eps = 1e-6f; /* todo: this is likely not the right value */
  171. /* prepare for stepping */
  172. int max_steps = kernel_data.integrator.volume_max_steps;
  173. float step_offset, step_size;
  174. kernel_volume_step_init(kg, state, ray->t, &step_size, &step_offset);
  175. /* compute extinction at the start */
  176. float t = 0.0f;
  177. float3 sum = make_float3(0.0f, 0.0f, 0.0f);
  178. for (int i = 0; i < max_steps; i++) {
  179. /* advance to new position */
  180. float new_t = min(ray->t, (i + 1) * step_size);
  181. /* use random position inside this segment to sample shader, adjust
  182. * for last step that is shorter than other steps. */
  183. if (new_t == ray->t) {
  184. step_offset *= (new_t - t) / step_size;
  185. }
  186. float3 new_P = ray->P + ray->D * (t + step_offset);
  187. float3 sigma_t;
  188. /* compute attenuation over segment */
  189. if (volume_shader_extinction_sample(kg, sd, state, new_P, &sigma_t)) {
  190. /* Compute expf() only for every Nth step, to save some calculations
  191. * because exp(a)*exp(b) = exp(a+b), also do a quick tp_eps check then. */
  192. sum += (-sigma_t * (new_t - t));
  193. if ((i & 0x07) == 0) { /* ToDo: Other interval? */
  194. tp = *throughput * exp3(sum);
  195. /* stop if nearly all light is blocked */
  196. if (tp.x < tp_eps && tp.y < tp_eps && tp.z < tp_eps)
  197. break;
  198. }
  199. }
  200. /* stop if at the end of the volume */
  201. t = new_t;
  202. if (t == ray->t) {
  203. /* Update throughput in case we haven't done it above */
  204. tp = *throughput * exp3(sum);
  205. break;
  206. }
  207. }
  208. *throughput = tp;
  209. }
  210. /* get the volume attenuation over line segment defined by ray, with the
  211. * assumption that there are no surfaces blocking light between the endpoints */
  212. ccl_device_noinline void kernel_volume_shadow(KernelGlobals *kg,
  213. ShaderData *shadow_sd,
  214. ccl_addr_space PathState *state,
  215. Ray *ray,
  216. float3 *throughput)
  217. {
  218. shader_setup_from_volume(kg, shadow_sd, ray);
  219. if (volume_stack_is_heterogeneous(kg, state->volume_stack))
  220. kernel_volume_shadow_heterogeneous(kg, state, ray, shadow_sd, throughput);
  221. else
  222. kernel_volume_shadow_homogeneous(kg, state, ray, shadow_sd, throughput);
  223. }
  224. #endif /* __VOLUME__ */
  225. /* Equi-angular sampling as in:
  226. * "Importance Sampling Techniques for Path Tracing in Participating Media" */
  227. ccl_device float kernel_volume_equiangular_sample(Ray *ray, float3 light_P, float xi, float *pdf)
  228. {
  229. float t = ray->t;
  230. float delta = dot((light_P - ray->P), ray->D);
  231. float D = safe_sqrtf(len_squared(light_P - ray->P) - delta * delta);
  232. if (UNLIKELY(D == 0.0f)) {
  233. *pdf = 0.0f;
  234. return 0.0f;
  235. }
  236. float theta_a = -atan2f(delta, D);
  237. float theta_b = atan2f(t - delta, D);
  238. float t_ = D * tanf((xi * theta_b) + (1 - xi) * theta_a);
  239. if (UNLIKELY(theta_b == theta_a)) {
  240. *pdf = 0.0f;
  241. return 0.0f;
  242. }
  243. *pdf = D / ((theta_b - theta_a) * (D * D + t_ * t_));
  244. return min(t, delta + t_); /* min is only for float precision errors */
  245. }
  246. ccl_device float kernel_volume_equiangular_pdf(Ray *ray, float3 light_P, float sample_t)
  247. {
  248. float delta = dot((light_P - ray->P), ray->D);
  249. float D = safe_sqrtf(len_squared(light_P - ray->P) - delta * delta);
  250. if (UNLIKELY(D == 0.0f)) {
  251. return 0.0f;
  252. }
  253. float t = ray->t;
  254. float t_ = sample_t - delta;
  255. float theta_a = -atan2f(delta, D);
  256. float theta_b = atan2f(t - delta, D);
  257. if (UNLIKELY(theta_b == theta_a)) {
  258. return 0.0f;
  259. }
  260. float pdf = D / ((theta_b - theta_a) * (D * D + t_ * t_));
  261. return pdf;
  262. }
  263. /* Distance sampling */
  264. ccl_device float kernel_volume_distance_sample(
  265. float max_t, float3 sigma_t, int channel, float xi, float3 *transmittance, float3 *pdf)
  266. {
  267. /* xi is [0, 1[ so log(0) should never happen, division by zero is
  268. * avoided because sample_sigma_t > 0 when SD_SCATTER is set */
  269. float sample_sigma_t = kernel_volume_channel_get(sigma_t, channel);
  270. float3 full_transmittance = volume_color_transmittance(sigma_t, max_t);
  271. float sample_transmittance = kernel_volume_channel_get(full_transmittance, channel);
  272. float sample_t = min(max_t, -logf(1.0f - xi * (1.0f - sample_transmittance)) / sample_sigma_t);
  273. *transmittance = volume_color_transmittance(sigma_t, sample_t);
  274. *pdf = safe_divide_color(sigma_t * *transmittance,
  275. make_float3(1.0f, 1.0f, 1.0f) - full_transmittance);
  276. /* todo: optimization: when taken together with hit/miss decision,
  277. * the full_transmittance cancels out drops out and xi does not
  278. * need to be remapped */
  279. return sample_t;
  280. }
  281. ccl_device float3 kernel_volume_distance_pdf(float max_t, float3 sigma_t, float sample_t)
  282. {
  283. float3 full_transmittance = volume_color_transmittance(sigma_t, max_t);
  284. float3 transmittance = volume_color_transmittance(sigma_t, sample_t);
  285. return safe_divide_color(sigma_t * transmittance,
  286. make_float3(1.0f, 1.0f, 1.0f) - full_transmittance);
  287. }
  288. /* Emission */
  289. ccl_device float3 kernel_volume_emission_integrate(VolumeShaderCoefficients *coeff,
  290. int closure_flag,
  291. float3 transmittance,
  292. float t)
  293. {
  294. /* integral E * exp(-sigma_t * t) from 0 to t = E * (1 - exp(-sigma_t * t))/sigma_t
  295. * this goes to E * t as sigma_t goes to zero
  296. *
  297. * todo: we should use an epsilon to avoid precision issues near zero sigma_t */
  298. float3 emission = coeff->emission;
  299. if (closure_flag & SD_EXTINCTION) {
  300. float3 sigma_t = coeff->sigma_t;
  301. emission.x *= (sigma_t.x > 0.0f) ? (1.0f - transmittance.x) / sigma_t.x : t;
  302. emission.y *= (sigma_t.y > 0.0f) ? (1.0f - transmittance.y) / sigma_t.y : t;
  303. emission.z *= (sigma_t.z > 0.0f) ? (1.0f - transmittance.z) / sigma_t.z : t;
  304. }
  305. else
  306. emission *= t;
  307. return emission;
  308. }
  309. /* Volume Path */
  310. ccl_device int kernel_volume_sample_channel(float3 albedo,
  311. float3 throughput,
  312. float rand,
  313. float3 *pdf)
  314. {
  315. /* Sample color channel proportional to throughput and single scattering
  316. * albedo, to significantly reduce noise with many bounce, following:
  317. *
  318. * "Practical and Controllable Subsurface Scattering for Production Path
  319. * Tracing". Matt Jen-Yuan Chiang, Peter Kutz, Brent Burley. SIGGRAPH 2016. */
  320. float3 weights = fabs(throughput * albedo);
  321. float sum_weights = weights.x + weights.y + weights.z;
  322. float3 weights_pdf;
  323. if (sum_weights > 0.0f) {
  324. weights_pdf = weights / sum_weights;
  325. }
  326. else {
  327. weights_pdf = make_float3(1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f);
  328. }
  329. *pdf = weights_pdf;
  330. /* OpenCL does not support -> on float3, so don't use pdf->x. */
  331. if (rand < weights_pdf.x) {
  332. return 0;
  333. }
  334. else if (rand < weights_pdf.x + weights_pdf.y) {
  335. return 1;
  336. }
  337. else {
  338. return 2;
  339. }
  340. }
  341. #ifdef __VOLUME__
  342. /* homogeneous volume: assume shader evaluation at the start gives
  343. * the volume shading coefficient for the entire line segment */
  344. ccl_device VolumeIntegrateResult
  345. kernel_volume_integrate_homogeneous(KernelGlobals *kg,
  346. ccl_addr_space PathState *state,
  347. Ray *ray,
  348. ShaderData *sd,
  349. PathRadiance *L,
  350. ccl_addr_space float3 *throughput,
  351. bool probalistic_scatter)
  352. {
  353. VolumeShaderCoefficients coeff;
  354. if (!volume_shader_sample(kg, sd, state, ray->P, &coeff))
  355. return VOLUME_PATH_MISSED;
  356. int closure_flag = sd->flag;
  357. float t = ray->t;
  358. float3 new_tp;
  359. # ifdef __VOLUME_SCATTER__
  360. /* randomly scatter, and if we do t is shortened */
  361. if (closure_flag & SD_SCATTER) {
  362. /* Sample channel, use MIS with balance heuristic. */
  363. float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
  364. float3 albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t);
  365. float3 channel_pdf;
  366. int channel = kernel_volume_sample_channel(albedo, *throughput, rphase, &channel_pdf);
  367. /* decide if we will hit or miss */
  368. bool scatter = true;
  369. float xi = path_state_rng_1D(kg, state, PRNG_SCATTER_DISTANCE);
  370. if (probalistic_scatter) {
  371. float sample_sigma_t = kernel_volume_channel_get(coeff.sigma_t, channel);
  372. float sample_transmittance = expf(-sample_sigma_t * t);
  373. if (1.0f - xi >= sample_transmittance) {
  374. scatter = true;
  375. /* rescale random number so we can reuse it */
  376. xi = 1.0f - (1.0f - xi - sample_transmittance) / (1.0f - sample_transmittance);
  377. }
  378. else
  379. scatter = false;
  380. }
  381. if (scatter) {
  382. /* scattering */
  383. float3 pdf;
  384. float3 transmittance;
  385. float sample_t;
  386. /* distance sampling */
  387. sample_t = kernel_volume_distance_sample(
  388. ray->t, coeff.sigma_t, channel, xi, &transmittance, &pdf);
  389. /* modify pdf for hit/miss decision */
  390. if (probalistic_scatter)
  391. pdf *= make_float3(1.0f, 1.0f, 1.0f) - volume_color_transmittance(coeff.sigma_t, t);
  392. new_tp = *throughput * coeff.sigma_s * transmittance / dot(channel_pdf, pdf);
  393. t = sample_t;
  394. }
  395. else {
  396. /* no scattering */
  397. float3 transmittance = volume_color_transmittance(coeff.sigma_t, t);
  398. float pdf = dot(channel_pdf, transmittance);
  399. new_tp = *throughput * transmittance / pdf;
  400. }
  401. }
  402. else
  403. # endif
  404. if (closure_flag & SD_EXTINCTION) {
  405. /* absorption only, no sampling needed */
  406. float3 transmittance = volume_color_transmittance(coeff.sigma_t, t);
  407. new_tp = *throughput * transmittance;
  408. }
  409. else {
  410. new_tp = *throughput;
  411. }
  412. /* integrate emission attenuated by extinction */
  413. if (L && (closure_flag & SD_EMISSION)) {
  414. float3 transmittance = volume_color_transmittance(coeff.sigma_t, ray->t);
  415. float3 emission = kernel_volume_emission_integrate(
  416. &coeff, closure_flag, transmittance, ray->t);
  417. path_radiance_accum_emission(L, state, *throughput, emission);
  418. }
  419. /* modify throughput */
  420. if (closure_flag & SD_EXTINCTION) {
  421. *throughput = new_tp;
  422. /* prepare to scatter to new direction */
  423. if (t < ray->t) {
  424. /* adjust throughput and move to new location */
  425. sd->P = ray->P + t * ray->D;
  426. return VOLUME_PATH_SCATTERED;
  427. }
  428. }
  429. return VOLUME_PATH_ATTENUATED;
  430. }
  431. /* heterogeneous volume distance sampling: integrate stepping through the
  432. * volume until we reach the end, get absorbed entirely, or run out of
  433. * iterations. this does probabilistically scatter or get transmitted through
  434. * for path tracing where we don't want to branch. */
  435. ccl_device VolumeIntegrateResult
  436. kernel_volume_integrate_heterogeneous_distance(KernelGlobals *kg,
  437. ccl_addr_space PathState *state,
  438. Ray *ray,
  439. ShaderData *sd,
  440. PathRadiance *L,
  441. ccl_addr_space float3 *throughput)
  442. {
  443. float3 tp = *throughput;
  444. const float tp_eps = 1e-6f; /* todo: this is likely not the right value */
  445. /* prepare for stepping */
  446. int max_steps = kernel_data.integrator.volume_max_steps;
  447. float step_offset, step_size;
  448. kernel_volume_step_init(kg, state, ray->t, &step_size, &step_offset);
  449. /* compute coefficients at the start */
  450. float t = 0.0f;
  451. float3 accum_transmittance = make_float3(1.0f, 1.0f, 1.0f);
  452. /* pick random color channel, we use the Veach one-sample
  453. * model with balance heuristic for the channels */
  454. float xi = path_state_rng_1D(kg, state, PRNG_SCATTER_DISTANCE);
  455. float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
  456. bool has_scatter = false;
  457. for (int i = 0; i < max_steps; i++) {
  458. /* advance to new position */
  459. float new_t = min(ray->t, (i + 1) * step_size);
  460. float dt = new_t - t;
  461. /* use random position inside this segment to sample shader,
  462. * for last shorter step we remap it to fit within the segment. */
  463. if (new_t == ray->t) {
  464. step_offset *= (new_t - t) / step_size;
  465. }
  466. float3 new_P = ray->P + ray->D * (t + step_offset);
  467. VolumeShaderCoefficients coeff;
  468. /* compute segment */
  469. if (volume_shader_sample(kg, sd, state, new_P, &coeff)) {
  470. int closure_flag = sd->flag;
  471. float3 new_tp;
  472. float3 transmittance;
  473. bool scatter = false;
  474. /* distance sampling */
  475. # ifdef __VOLUME_SCATTER__
  476. if ((closure_flag & SD_SCATTER) || (has_scatter && (closure_flag & SD_EXTINCTION))) {
  477. has_scatter = true;
  478. /* Sample channel, use MIS with balance heuristic. */
  479. float3 albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t);
  480. float3 channel_pdf;
  481. int channel = kernel_volume_sample_channel(albedo, tp, rphase, &channel_pdf);
  482. /* compute transmittance over full step */
  483. transmittance = volume_color_transmittance(coeff.sigma_t, dt);
  484. /* decide if we will scatter or continue */
  485. float sample_transmittance = kernel_volume_channel_get(transmittance, channel);
  486. if (1.0f - xi >= sample_transmittance) {
  487. /* compute sampling distance */
  488. float sample_sigma_t = kernel_volume_channel_get(coeff.sigma_t, channel);
  489. float new_dt = -logf(1.0f - xi) / sample_sigma_t;
  490. new_t = t + new_dt;
  491. /* transmittance and pdf */
  492. float3 new_transmittance = volume_color_transmittance(coeff.sigma_t, new_dt);
  493. float3 pdf = coeff.sigma_t * new_transmittance;
  494. /* throughput */
  495. new_tp = tp * coeff.sigma_s * new_transmittance / dot(channel_pdf, pdf);
  496. scatter = true;
  497. }
  498. else {
  499. /* throughput */
  500. float pdf = dot(channel_pdf, transmittance);
  501. new_tp = tp * transmittance / pdf;
  502. /* remap xi so we can reuse it and keep thing stratified */
  503. xi = 1.0f - (1.0f - xi) / sample_transmittance;
  504. }
  505. }
  506. else
  507. # endif
  508. if (closure_flag & SD_EXTINCTION) {
  509. /* absorption only, no sampling needed */
  510. transmittance = volume_color_transmittance(coeff.sigma_t, dt);
  511. new_tp = tp * transmittance;
  512. }
  513. else {
  514. new_tp = tp;
  515. }
  516. /* integrate emission attenuated by absorption */
  517. if (L && (closure_flag & SD_EMISSION)) {
  518. float3 emission = kernel_volume_emission_integrate(
  519. &coeff, closure_flag, transmittance, dt);
  520. path_radiance_accum_emission(L, state, tp, emission);
  521. }
  522. /* modify throughput */
  523. if (closure_flag & SD_EXTINCTION) {
  524. tp = new_tp;
  525. /* stop if nearly all light blocked */
  526. if (tp.x < tp_eps && tp.y < tp_eps && tp.z < tp_eps) {
  527. tp = make_float3(0.0f, 0.0f, 0.0f);
  528. break;
  529. }
  530. }
  531. /* prepare to scatter to new direction */
  532. if (scatter) {
  533. /* adjust throughput and move to new location */
  534. sd->P = ray->P + new_t * ray->D;
  535. *throughput = tp;
  536. return VOLUME_PATH_SCATTERED;
  537. }
  538. else {
  539. /* accumulate transmittance */
  540. accum_transmittance *= transmittance;
  541. }
  542. }
  543. /* stop if at the end of the volume */
  544. t = new_t;
  545. if (t == ray->t)
  546. break;
  547. }
  548. *throughput = tp;
  549. return VOLUME_PATH_ATTENUATED;
  550. }
  551. /* get the volume attenuation and emission over line segment defined by
  552. * ray, with the assumption that there are no surfaces blocking light
  553. * between the endpoints. distance sampling is used to decide if we will
  554. * scatter or not. */
  555. ccl_device_noinline VolumeIntegrateResult
  556. kernel_volume_integrate(KernelGlobals *kg,
  557. ccl_addr_space PathState *state,
  558. ShaderData *sd,
  559. Ray *ray,
  560. PathRadiance *L,
  561. ccl_addr_space float3 *throughput,
  562. bool heterogeneous)
  563. {
  564. shader_setup_from_volume(kg, sd, ray);
  565. if (heterogeneous)
  566. return kernel_volume_integrate_heterogeneous_distance(kg, state, ray, sd, L, throughput);
  567. else
  568. return kernel_volume_integrate_homogeneous(kg, state, ray, sd, L, throughput, true);
  569. }
  570. # ifndef __SPLIT_KERNEL__
  571. /* Decoupled Volume Sampling
  572. *
  573. * VolumeSegment is list of coefficients and transmittance stored at all steps
  574. * through a volume. This can then later be used for decoupled sampling as in:
  575. * "Importance Sampling Techniques for Path Tracing in Participating Media"
  576. *
  577. * On the GPU this is only supported (but currently not enabled)
  578. * for homogeneous volumes (1 step), due to
  579. * no support for malloc/free and too much stack usage with a fix size array. */
  580. typedef struct VolumeStep {
  581. float3 sigma_s; /* scatter coefficient */
  582. float3 sigma_t; /* extinction coefficient */
  583. float3 accum_transmittance; /* accumulated transmittance including this step */
  584. float3 cdf_distance; /* cumulative density function for distance sampling */
  585. float t; /* distance at end of this step */
  586. float shade_t; /* jittered distance where shading was done in step */
  587. int closure_flag; /* shader evaluation closure flags */
  588. } VolumeStep;
  589. typedef struct VolumeSegment {
  590. VolumeStep stack_step; /* stack storage for homogeneous step, to avoid malloc */
  591. VolumeStep *steps; /* recorded steps */
  592. int numsteps; /* number of steps */
  593. int closure_flag; /* accumulated closure flags from all steps */
  594. float3 accum_emission; /* accumulated emission at end of segment */
  595. float3 accum_transmittance; /* accumulated transmittance at end of segment */
  596. float3 accum_albedo; /* accumulated average albedo over segment */
  597. int sampling_method; /* volume sampling method */
  598. } VolumeSegment;
  599. /* record volume steps to the end of the volume.
  600. *
  601. * it would be nice if we could only record up to the point that we need to scatter,
  602. * but the entire segment is needed to do always scattering, rather than probabilistically
  603. * hitting or missing the volume. if we don't know the transmittance at the end of the
  604. * volume we can't generate stratified distance samples up to that transmittance */
  605. # ifdef __VOLUME_DECOUPLED__
  606. ccl_device void kernel_volume_decoupled_record(KernelGlobals *kg,
  607. PathState *state,
  608. Ray *ray,
  609. ShaderData *sd,
  610. VolumeSegment *segment,
  611. bool heterogeneous)
  612. {
  613. const float tp_eps = 1e-6f; /* todo: this is likely not the right value */
  614. /* prepare for volume stepping */
  615. int max_steps;
  616. float step_size, step_offset;
  617. if (heterogeneous) {
  618. max_steps = kernel_data.integrator.volume_max_steps;
  619. kernel_volume_step_init(kg, state, ray->t, &step_size, &step_offset);
  620. # ifdef __KERNEL_CPU__
  621. /* NOTE: For the branched path tracing it's possible to have direct
  622. * and indirect light integration both having volume segments allocated.
  623. * We detect this using index in the pre-allocated memory. Currently we
  624. * only support two segments allocated at a time, if more needed some
  625. * modifications to the KernelGlobals will be needed.
  626. *
  627. * This gives us restrictions that decoupled record should only happen
  628. * in the stack manner, meaning if there's subsequent call of decoupled
  629. * record it'll need to free memory before it's caller frees memory.
  630. */
  631. const int index = kg->decoupled_volume_steps_index;
  632. assert(index < sizeof(kg->decoupled_volume_steps) / sizeof(*kg->decoupled_volume_steps));
  633. if (kg->decoupled_volume_steps[index] == NULL) {
  634. kg->decoupled_volume_steps[index] = (VolumeStep *)malloc(sizeof(VolumeStep) * max_steps);
  635. }
  636. segment->steps = kg->decoupled_volume_steps[index];
  637. ++kg->decoupled_volume_steps_index;
  638. # else
  639. segment->steps = (VolumeStep *)malloc(sizeof(VolumeStep) * max_steps);
  640. # endif
  641. }
  642. else {
  643. max_steps = 1;
  644. step_size = ray->t;
  645. step_offset = 0.0f;
  646. segment->steps = &segment->stack_step;
  647. }
  648. /* init accumulation variables */
  649. float3 accum_emission = make_float3(0.0f, 0.0f, 0.0f);
  650. float3 accum_transmittance = make_float3(1.0f, 1.0f, 1.0f);
  651. float3 accum_albedo = make_float3(0.0f, 0.0f, 0.0f);
  652. float3 cdf_distance = make_float3(0.0f, 0.0f, 0.0f);
  653. float t = 0.0f;
  654. segment->numsteps = 0;
  655. segment->closure_flag = 0;
  656. bool is_last_step_empty = false;
  657. VolumeStep *step = segment->steps;
  658. for (int i = 0; i < max_steps; i++, step++) {
  659. /* advance to new position */
  660. float new_t = min(ray->t, (i + 1) * step_size);
  661. float dt = new_t - t;
  662. /* use random position inside this segment to sample shader,
  663. * for last shorter step we remap it to fit within the segment. */
  664. if (new_t == ray->t) {
  665. step_offset *= (new_t - t) / step_size;
  666. }
  667. float3 new_P = ray->P + ray->D * (t + step_offset);
  668. VolumeShaderCoefficients coeff;
  669. /* compute segment */
  670. if (volume_shader_sample(kg, sd, state, new_P, &coeff)) {
  671. int closure_flag = sd->flag;
  672. float3 sigma_t = coeff.sigma_t;
  673. /* compute average albedo for channel sampling */
  674. if (closure_flag & SD_SCATTER) {
  675. accum_albedo += dt * safe_divide_color(coeff.sigma_s, sigma_t);
  676. }
  677. /* compute accumulated transmittance */
  678. float3 transmittance = volume_color_transmittance(sigma_t, dt);
  679. /* compute emission attenuated by absorption */
  680. if (closure_flag & SD_EMISSION) {
  681. float3 emission = kernel_volume_emission_integrate(
  682. &coeff, closure_flag, transmittance, dt);
  683. accum_emission += accum_transmittance * emission;
  684. }
  685. accum_transmittance *= transmittance;
  686. /* compute pdf for distance sampling */
  687. float3 pdf_distance = dt * accum_transmittance * coeff.sigma_s;
  688. cdf_distance = cdf_distance + pdf_distance;
  689. /* write step data */
  690. step->sigma_t = sigma_t;
  691. step->sigma_s = coeff.sigma_s;
  692. step->closure_flag = closure_flag;
  693. segment->closure_flag |= closure_flag;
  694. is_last_step_empty = false;
  695. segment->numsteps++;
  696. }
  697. else {
  698. if (is_last_step_empty) {
  699. /* consecutive empty step, merge */
  700. step--;
  701. }
  702. else {
  703. /* store empty step */
  704. step->sigma_t = make_float3(0.0f, 0.0f, 0.0f);
  705. step->sigma_s = make_float3(0.0f, 0.0f, 0.0f);
  706. step->closure_flag = 0;
  707. segment->numsteps++;
  708. is_last_step_empty = true;
  709. }
  710. }
  711. step->accum_transmittance = accum_transmittance;
  712. step->cdf_distance = cdf_distance;
  713. step->t = new_t;
  714. step->shade_t = t + step_offset;
  715. /* stop if at the end of the volume */
  716. t = new_t;
  717. if (t == ray->t)
  718. break;
  719. /* stop if nearly all light blocked */
  720. if (accum_transmittance.x < tp_eps && accum_transmittance.y < tp_eps &&
  721. accum_transmittance.z < tp_eps)
  722. break;
  723. }
  724. /* store total emission and transmittance */
  725. segment->accum_emission = accum_emission;
  726. segment->accum_transmittance = accum_transmittance;
  727. segment->accum_albedo = accum_albedo;
  728. /* normalize cumulative density function for distance sampling */
  729. VolumeStep *last_step = segment->steps + segment->numsteps - 1;
  730. if (!is_zero(last_step->cdf_distance)) {
  731. VolumeStep *step = &segment->steps[0];
  732. int numsteps = segment->numsteps;
  733. float3 inv_cdf_distance_sum = safe_invert_color(last_step->cdf_distance);
  734. for (int i = 0; i < numsteps; i++, step++)
  735. step->cdf_distance *= inv_cdf_distance_sum;
  736. }
  737. }
  738. ccl_device void kernel_volume_decoupled_free(KernelGlobals *kg, VolumeSegment *segment)
  739. {
  740. if (segment->steps != &segment->stack_step) {
  741. # ifdef __KERNEL_CPU__
  742. /* NOTE: We only allow free last allocated segment.
  743. * No random order of alloc/free is supported.
  744. */
  745. assert(kg->decoupled_volume_steps_index > 0);
  746. assert(segment->steps == kg->decoupled_volume_steps[kg->decoupled_volume_steps_index - 1]);
  747. --kg->decoupled_volume_steps_index;
  748. # else
  749. free(segment->steps);
  750. # endif
  751. }
  752. }
  753. # endif /* __VOLUME_DECOUPLED__ */
  754. /* scattering for homogeneous and heterogeneous volumes, using decoupled ray
  755. * marching.
  756. *
  757. * function is expected to return VOLUME_PATH_SCATTERED when probalistic_scatter is false */
  758. ccl_device VolumeIntegrateResult kernel_volume_decoupled_scatter(KernelGlobals *kg,
  759. PathState *state,
  760. Ray *ray,
  761. ShaderData *sd,
  762. float3 *throughput,
  763. float rphase,
  764. float rscatter,
  765. const VolumeSegment *segment,
  766. const float3 *light_P,
  767. bool probalistic_scatter)
  768. {
  769. kernel_assert(segment->closure_flag & SD_SCATTER);
  770. /* Sample color channel, use MIS with balance heuristic. */
  771. float3 channel_pdf;
  772. int channel = kernel_volume_sample_channel(
  773. segment->accum_albedo, *throughput, rphase, &channel_pdf);
  774. float xi = rscatter;
  775. /* probabilistic scattering decision based on transmittance */
  776. if (probalistic_scatter) {
  777. float sample_transmittance = kernel_volume_channel_get(segment->accum_transmittance, channel);
  778. if (1.0f - xi >= sample_transmittance) {
  779. /* rescale random number so we can reuse it */
  780. xi = 1.0f - (1.0f - xi - sample_transmittance) / (1.0f - sample_transmittance);
  781. }
  782. else {
  783. *throughput /= sample_transmittance;
  784. return VOLUME_PATH_MISSED;
  785. }
  786. }
  787. VolumeStep *step;
  788. float3 transmittance;
  789. float pdf, sample_t;
  790. float mis_weight = 1.0f;
  791. bool distance_sample = true;
  792. bool use_mis = false;
  793. if (segment->sampling_method && light_P) {
  794. if (segment->sampling_method == SD_VOLUME_MIS) {
  795. /* multiple importance sample: randomly pick between
  796. * equiangular and distance sampling strategy */
  797. if (xi < 0.5f) {
  798. xi *= 2.0f;
  799. }
  800. else {
  801. xi = (xi - 0.5f) * 2.0f;
  802. distance_sample = false;
  803. }
  804. use_mis = true;
  805. }
  806. else {
  807. /* only equiangular sampling */
  808. distance_sample = false;
  809. }
  810. }
  811. /* distance sampling */
  812. if (distance_sample) {
  813. /* find step in cdf */
  814. step = segment->steps;
  815. float prev_t = 0.0f;
  816. float3 step_pdf_distance = make_float3(1.0f, 1.0f, 1.0f);
  817. if (segment->numsteps > 1) {
  818. float prev_cdf = 0.0f;
  819. float step_cdf = 1.0f;
  820. float3 prev_cdf_distance = make_float3(0.0f, 0.0f, 0.0f);
  821. for (int i = 0;; i++, step++) {
  822. /* todo: optimize using binary search */
  823. step_cdf = kernel_volume_channel_get(step->cdf_distance, channel);
  824. if (xi < step_cdf || i == segment->numsteps - 1)
  825. break;
  826. prev_cdf = step_cdf;
  827. prev_t = step->t;
  828. prev_cdf_distance = step->cdf_distance;
  829. }
  830. /* remap xi so we can reuse it */
  831. xi = (xi - prev_cdf) / (step_cdf - prev_cdf);
  832. /* pdf for picking step */
  833. step_pdf_distance = step->cdf_distance - prev_cdf_distance;
  834. }
  835. /* determine range in which we will sample */
  836. float step_t = step->t - prev_t;
  837. /* sample distance and compute transmittance */
  838. float3 distance_pdf;
  839. sample_t = prev_t + kernel_volume_distance_sample(
  840. step_t, step->sigma_t, channel, xi, &transmittance, &distance_pdf);
  841. /* modify pdf for hit/miss decision */
  842. if (probalistic_scatter)
  843. distance_pdf *= make_float3(1.0f, 1.0f, 1.0f) - segment->accum_transmittance;
  844. pdf = dot(channel_pdf, distance_pdf * step_pdf_distance);
  845. /* multiple importance sampling */
  846. if (use_mis) {
  847. float equi_pdf = kernel_volume_equiangular_pdf(ray, *light_P, sample_t);
  848. mis_weight = 2.0f * power_heuristic(pdf, equi_pdf);
  849. }
  850. }
  851. /* equi-angular sampling */
  852. else {
  853. /* sample distance */
  854. sample_t = kernel_volume_equiangular_sample(ray, *light_P, xi, &pdf);
  855. /* find step in which sampled distance is located */
  856. step = segment->steps;
  857. float prev_t = 0.0f;
  858. float3 step_pdf_distance = make_float3(1.0f, 1.0f, 1.0f);
  859. if (segment->numsteps > 1) {
  860. float3 prev_cdf_distance = make_float3(0.0f, 0.0f, 0.0f);
  861. int numsteps = segment->numsteps;
  862. int high = numsteps - 1;
  863. int low = 0;
  864. int mid;
  865. while (low < high) {
  866. mid = (low + high) >> 1;
  867. if (sample_t < step[mid].t)
  868. high = mid;
  869. else if (sample_t >= step[mid + 1].t)
  870. low = mid + 1;
  871. else {
  872. /* found our interval in step[mid] .. step[mid+1] */
  873. prev_t = step[mid].t;
  874. prev_cdf_distance = step[mid].cdf_distance;
  875. step += mid + 1;
  876. break;
  877. }
  878. }
  879. if (low >= numsteps - 1) {
  880. prev_t = step[numsteps - 1].t;
  881. prev_cdf_distance = step[numsteps - 1].cdf_distance;
  882. step += numsteps - 1;
  883. }
  884. /* pdf for picking step with distance sampling */
  885. step_pdf_distance = step->cdf_distance - prev_cdf_distance;
  886. }
  887. /* determine range in which we will sample */
  888. float step_t = step->t - prev_t;
  889. float step_sample_t = sample_t - prev_t;
  890. /* compute transmittance */
  891. transmittance = volume_color_transmittance(step->sigma_t, step_sample_t);
  892. /* multiple importance sampling */
  893. if (use_mis) {
  894. float3 distance_pdf3 = kernel_volume_distance_pdf(step_t, step->sigma_t, step_sample_t);
  895. float distance_pdf = dot(channel_pdf, distance_pdf3 * step_pdf_distance);
  896. mis_weight = 2.0f * power_heuristic(pdf, distance_pdf);
  897. }
  898. }
  899. if (sample_t < 0.0f || pdf == 0.0f) {
  900. return VOLUME_PATH_MISSED;
  901. }
  902. /* compute transmittance up to this step */
  903. if (step != segment->steps)
  904. transmittance *= (step - 1)->accum_transmittance;
  905. /* modify throughput */
  906. *throughput *= step->sigma_s * transmittance * (mis_weight / pdf);
  907. /* evaluate shader to create closures at shading point */
  908. if (segment->numsteps > 1) {
  909. sd->P = ray->P + step->shade_t * ray->D;
  910. VolumeShaderCoefficients coeff;
  911. volume_shader_sample(kg, sd, state, sd->P, &coeff);
  912. }
  913. /* move to new position */
  914. sd->P = ray->P + sample_t * ray->D;
  915. return VOLUME_PATH_SCATTERED;
  916. }
  917. # endif /* __SPLIT_KERNEL */
  918. /* decide if we need to use decoupled or not */
  919. ccl_device bool kernel_volume_use_decoupled(KernelGlobals *kg,
  920. bool heterogeneous,
  921. bool direct,
  922. int sampling_method)
  923. {
  924. /* decoupled ray marching for heterogeneous volumes not supported on the GPU,
  925. * which also means equiangular and multiple importance sampling is not
  926. * support for that case */
  927. if (!kernel_data.integrator.volume_decoupled)
  928. return false;
  929. # ifdef __KERNEL_GPU__
  930. if (heterogeneous)
  931. return false;
  932. # endif
  933. /* equiangular and multiple importance sampling only implemented for decoupled */
  934. if (sampling_method != 0)
  935. return true;
  936. /* for all light sampling use decoupled, reusing shader evaluations is
  937. * typically faster in that case */
  938. if (direct)
  939. return kernel_data.integrator.sample_all_lights_direct;
  940. else
  941. return kernel_data.integrator.sample_all_lights_indirect;
  942. }
  943. /* Volume Stack
  944. *
  945. * This is an array of object/shared ID's that the current segment of the path
  946. * is inside of. */
  947. ccl_device void kernel_volume_stack_init(KernelGlobals *kg,
  948. ShaderData *stack_sd,
  949. ccl_addr_space const PathState *state,
  950. ccl_addr_space const Ray *ray,
  951. ccl_addr_space VolumeStack *stack)
  952. {
  953. /* NULL ray happens in the baker, does it need proper initialization of
  954. * camera in volume?
  955. */
  956. if (!kernel_data.cam.is_inside_volume || ray == NULL) {
  957. /* Camera is guaranteed to be in the air, only take background volume
  958. * into account in this case.
  959. */
  960. if (kernel_data.background.volume_shader != SHADER_NONE) {
  961. stack[0].shader = kernel_data.background.volume_shader;
  962. stack[0].object = PRIM_NONE;
  963. stack[1].shader = SHADER_NONE;
  964. }
  965. else {
  966. stack[0].shader = SHADER_NONE;
  967. }
  968. return;
  969. }
  970. kernel_assert(state->flag & PATH_RAY_CAMERA);
  971. Ray volume_ray = *ray;
  972. volume_ray.t = FLT_MAX;
  973. const uint visibility = (state->flag & PATH_RAY_ALL_VISIBILITY);
  974. int stack_index = 0, enclosed_index = 0;
  975. # ifdef __VOLUME_RECORD_ALL__
  976. Intersection hits[2 * VOLUME_STACK_SIZE + 1];
  977. uint num_hits = scene_intersect_volume_all(
  978. kg, &volume_ray, hits, 2 * VOLUME_STACK_SIZE, visibility);
  979. if (num_hits > 0) {
  980. int enclosed_volumes[VOLUME_STACK_SIZE];
  981. Intersection *isect = hits;
  982. qsort(hits, num_hits, sizeof(Intersection), intersections_compare);
  983. for (uint hit = 0; hit < num_hits; ++hit, ++isect) {
  984. shader_setup_from_ray(kg, stack_sd, isect, &volume_ray);
  985. if (stack_sd->flag & SD_BACKFACING) {
  986. bool need_add = true;
  987. for (int i = 0; i < enclosed_index && need_add; ++i) {
  988. /* If ray exited the volume and never entered to that volume
  989. * it means that camera is inside such a volume.
  990. */
  991. if (enclosed_volumes[i] == stack_sd->object) {
  992. need_add = false;
  993. }
  994. }
  995. for (int i = 0; i < stack_index && need_add; ++i) {
  996. /* Don't add intersections twice. */
  997. if (stack[i].object == stack_sd->object) {
  998. need_add = false;
  999. break;
  1000. }
  1001. }
  1002. if (need_add && stack_index < VOLUME_STACK_SIZE - 1) {
  1003. stack[stack_index].object = stack_sd->object;
  1004. stack[stack_index].shader = stack_sd->shader;
  1005. ++stack_index;
  1006. }
  1007. }
  1008. else {
  1009. /* If ray from camera enters the volume, this volume shouldn't
  1010. * be added to the stack on exit.
  1011. */
  1012. enclosed_volumes[enclosed_index++] = stack_sd->object;
  1013. }
  1014. }
  1015. }
  1016. # else
  1017. int enclosed_volumes[VOLUME_STACK_SIZE];
  1018. int step = 0;
  1019. while (stack_index < VOLUME_STACK_SIZE - 1 && enclosed_index < VOLUME_STACK_SIZE - 1 &&
  1020. step < 2 * VOLUME_STACK_SIZE) {
  1021. Intersection isect;
  1022. if (!scene_intersect_volume(kg, &volume_ray, &isect, visibility)) {
  1023. break;
  1024. }
  1025. shader_setup_from_ray(kg, stack_sd, &isect, &volume_ray);
  1026. if (stack_sd->flag & SD_BACKFACING) {
  1027. /* If ray exited the volume and never entered to that volume
  1028. * it means that camera is inside such a volume.
  1029. */
  1030. bool need_add = true;
  1031. for (int i = 0; i < enclosed_index && need_add; ++i) {
  1032. /* If ray exited the volume and never entered to that volume
  1033. * it means that camera is inside such a volume.
  1034. */
  1035. if (enclosed_volumes[i] == stack_sd->object) {
  1036. need_add = false;
  1037. }
  1038. }
  1039. for (int i = 0; i < stack_index && need_add; ++i) {
  1040. /* Don't add intersections twice. */
  1041. if (stack[i].object == stack_sd->object) {
  1042. need_add = false;
  1043. break;
  1044. }
  1045. }
  1046. if (need_add) {
  1047. stack[stack_index].object = stack_sd->object;
  1048. stack[stack_index].shader = stack_sd->shader;
  1049. ++stack_index;
  1050. }
  1051. }
  1052. else {
  1053. /* If ray from camera enters the volume, this volume shouldn't
  1054. * be added to the stack on exit.
  1055. */
  1056. enclosed_volumes[enclosed_index++] = stack_sd->object;
  1057. }
  1058. /* Move ray forward. */
  1059. volume_ray.P = ray_offset(stack_sd->P, -stack_sd->Ng);
  1060. ++step;
  1061. }
  1062. # endif
  1063. /* stack_index of 0 means quick checks outside of the kernel gave false
  1064. * positive, nothing to worry about, just we've wasted quite a few of
  1065. * ticks just to come into conclusion that camera is in the air.
  1066. *
  1067. * In this case we're doing the same above -- check whether background has
  1068. * volume.
  1069. */
  1070. if (stack_index == 0 && kernel_data.background.volume_shader == SHADER_NONE) {
  1071. stack[0].shader = kernel_data.background.volume_shader;
  1072. stack[0].object = PRIM_NONE;
  1073. stack[1].shader = SHADER_NONE;
  1074. }
  1075. else {
  1076. stack[stack_index].shader = SHADER_NONE;
  1077. }
  1078. }
  1079. ccl_device void kernel_volume_stack_enter_exit(KernelGlobals *kg,
  1080. ShaderData *sd,
  1081. ccl_addr_space VolumeStack *stack)
  1082. {
  1083. /* todo: we should have some way for objects to indicate if they want the
  1084. * world shader to work inside them. excluding it by default is problematic
  1085. * because non-volume objects can't be assumed to be closed manifolds */
  1086. if (!(sd->flag & SD_HAS_VOLUME))
  1087. return;
  1088. if (sd->flag & SD_BACKFACING) {
  1089. /* exit volume object: remove from stack */
  1090. for (int i = 0; stack[i].shader != SHADER_NONE; i++) {
  1091. if (stack[i].object == sd->object) {
  1092. /* shift back next stack entries */
  1093. do {
  1094. stack[i] = stack[i + 1];
  1095. i++;
  1096. } while (stack[i].shader != SHADER_NONE);
  1097. return;
  1098. }
  1099. }
  1100. }
  1101. else {
  1102. /* enter volume object: add to stack */
  1103. int i;
  1104. for (i = 0; stack[i].shader != SHADER_NONE; i++) {
  1105. /* already in the stack? then we have nothing to do */
  1106. if (stack[i].object == sd->object)
  1107. return;
  1108. }
  1109. /* if we exceed the stack limit, ignore */
  1110. if (i >= VOLUME_STACK_SIZE - 1)
  1111. return;
  1112. /* add to the end of the stack */
  1113. stack[i].shader = sd->shader;
  1114. stack[i].object = sd->object;
  1115. stack[i + 1].shader = SHADER_NONE;
  1116. }
  1117. }
  1118. # ifdef __SUBSURFACE__
  1119. ccl_device void kernel_volume_stack_update_for_subsurface(KernelGlobals *kg,
  1120. ShaderData *stack_sd,
  1121. Ray *ray,
  1122. ccl_addr_space VolumeStack *stack)
  1123. {
  1124. kernel_assert(kernel_data.integrator.use_volumes);
  1125. Ray volume_ray = *ray;
  1126. # ifdef __VOLUME_RECORD_ALL__
  1127. Intersection hits[2 * VOLUME_STACK_SIZE + 1];
  1128. uint num_hits = scene_intersect_volume_all(
  1129. kg, &volume_ray, hits, 2 * VOLUME_STACK_SIZE, PATH_RAY_ALL_VISIBILITY);
  1130. if (num_hits > 0) {
  1131. Intersection *isect = hits;
  1132. qsort(hits, num_hits, sizeof(Intersection), intersections_compare);
  1133. for (uint hit = 0; hit < num_hits; ++hit, ++isect) {
  1134. shader_setup_from_ray(kg, stack_sd, isect, &volume_ray);
  1135. kernel_volume_stack_enter_exit(kg, stack_sd, stack);
  1136. }
  1137. }
  1138. # else
  1139. Intersection isect;
  1140. int step = 0;
  1141. float3 Pend = ray->P + ray->D * ray->t;
  1142. while (step < 2 * VOLUME_STACK_SIZE &&
  1143. scene_intersect_volume(kg, &volume_ray, &isect, PATH_RAY_ALL_VISIBILITY)) {
  1144. shader_setup_from_ray(kg, stack_sd, &isect, &volume_ray);
  1145. kernel_volume_stack_enter_exit(kg, stack_sd, stack);
  1146. /* Move ray forward. */
  1147. volume_ray.P = ray_offset(stack_sd->P, -stack_sd->Ng);
  1148. if (volume_ray.t != FLT_MAX) {
  1149. volume_ray.D = normalize_len(Pend - volume_ray.P, &volume_ray.t);
  1150. }
  1151. ++step;
  1152. }
  1153. # endif
  1154. }
  1155. # endif
  1156. /* Clean stack after the last bounce.
  1157. *
  1158. * It is expected that all volumes are closed manifolds, so at the time when ray
  1159. * hits nothing (for example, it is a last bounce which goes to environment) the
  1160. * only expected volume in the stack is the world's one. All the rest volume
  1161. * entries should have been exited already.
  1162. *
  1163. * This isn't always true because of ray intersection precision issues, which
  1164. * could lead us to an infinite non-world volume in the stack, causing render
  1165. * artifacts.
  1166. *
  1167. * Use this function after the last bounce to get rid of all volumes apart from
  1168. * the world's one after the last bounce to avoid render artifacts.
  1169. */
  1170. ccl_device_inline void kernel_volume_clean_stack(KernelGlobals *kg,
  1171. ccl_addr_space VolumeStack *volume_stack)
  1172. {
  1173. if (kernel_data.background.volume_shader != SHADER_NONE) {
  1174. /* Keep the world's volume in stack. */
  1175. volume_stack[1].shader = SHADER_NONE;
  1176. }
  1177. else {
  1178. volume_stack[0].shader = SHADER_NONE;
  1179. }
  1180. }
  1181. #endif /* __VOLUME__ */
  1182. CCL_NAMESPACE_END