kernel_path_volume.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #ifdef __VOLUME_SCATTER__
  18. ccl_device_inline void kernel_path_volume_connect_light(KernelGlobals *kg,
  19. ShaderData *sd,
  20. ShaderData *emission_sd,
  21. float3 throughput,
  22. ccl_addr_space PathState *state,
  23. PathRadiance *L)
  24. {
  25. # ifdef __EMISSION__
  26. if (!kernel_data.integrator.use_direct_light)
  27. return;
  28. /* sample illumination from lights to find path contribution */
  29. float light_u, light_v;
  30. path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
  31. Ray light_ray;
  32. BsdfEval L_light;
  33. LightSample ls;
  34. bool is_lamp;
  35. /* connect to light from given point where shader has been evaluated */
  36. light_ray.time = sd->time;
  37. if (light_sample(kg, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
  38. float terminate = path_state_rng_light_termination(kg, state);
  39. if (direct_emission(
  40. kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
  41. /* trace shadow ray */
  42. float3 shadow;
  43. if (!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &shadow)) {
  44. /* accumulate */
  45. path_radiance_accum_light(L, state, throughput, &L_light, shadow, 1.0f, is_lamp);
  46. }
  47. }
  48. }
  49. # endif /* __EMISSION__ */
  50. }
  51. # ifdef __KERNEL_GPU__
  52. ccl_device_noinline
  53. # else
  54. ccl_device
  55. # endif
  56. bool
  57. kernel_path_volume_bounce(KernelGlobals *kg,
  58. ShaderData *sd,
  59. ccl_addr_space float3 *throughput,
  60. ccl_addr_space PathState *state,
  61. PathRadianceState *L_state,
  62. ccl_addr_space Ray *ray)
  63. {
  64. /* sample phase function */
  65. float phase_pdf;
  66. BsdfEval phase_eval;
  67. float3 phase_omega_in;
  68. differential3 phase_domega_in;
  69. float phase_u, phase_v;
  70. path_state_rng_2D(kg, state, PRNG_BSDF_U, &phase_u, &phase_v);
  71. int label;
  72. label = shader_volume_phase_sample(
  73. kg, sd, phase_u, phase_v, &phase_eval, &phase_omega_in, &phase_domega_in, &phase_pdf);
  74. if (phase_pdf == 0.0f || bsdf_eval_is_zero(&phase_eval))
  75. return false;
  76. /* modify throughput */
  77. path_radiance_bsdf_bounce(kg, L_state, throughput, &phase_eval, phase_pdf, state->bounce, label);
  78. /* set labels */
  79. state->ray_pdf = phase_pdf;
  80. # ifdef __LAMP_MIS__
  81. state->ray_t = 0.0f;
  82. # endif
  83. state->min_ray_pdf = fminf(phase_pdf, state->min_ray_pdf);
  84. /* update path state */
  85. path_state_next(kg, state, label);
  86. /* Russian roulette termination of volume ray scattering. */
  87. float probability = path_state_continuation_probability(kg, state, *throughput);
  88. if (probability == 0.0f) {
  89. return false;
  90. }
  91. else if (probability != 1.0f) {
  92. /* Use dimension from the previous bounce, has not been used yet. */
  93. float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE - PRNG_BOUNCE_NUM);
  94. if (terminate >= probability) {
  95. return false;
  96. }
  97. *throughput /= probability;
  98. }
  99. /* setup ray */
  100. ray->P = sd->P;
  101. ray->D = phase_omega_in;
  102. ray->t = FLT_MAX;
  103. # ifdef __RAY_DIFFERENTIALS__
  104. ray->dP = sd->dP;
  105. ray->dD = phase_domega_in;
  106. # endif
  107. return true;
  108. }
  109. # ifndef __SPLIT_KERNEL__
  110. ccl_device void kernel_branched_path_volume_connect_light(KernelGlobals *kg,
  111. ShaderData *sd,
  112. ShaderData *emission_sd,
  113. float3 throughput,
  114. ccl_addr_space PathState *state,
  115. PathRadiance *L,
  116. bool sample_all_lights,
  117. Ray *ray,
  118. const VolumeSegment *segment)
  119. {
  120. # ifdef __EMISSION__
  121. if (!kernel_data.integrator.use_direct_light)
  122. return;
  123. Ray light_ray;
  124. BsdfEval L_light;
  125. bool is_lamp;
  126. light_ray.time = sd->time;
  127. if (sample_all_lights) {
  128. /* lamp sampling */
  129. for (int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
  130. if (UNLIKELY(light_select_reached_max_bounces(kg, i, state->bounce)))
  131. continue;
  132. int num_samples = light_select_num_samples(kg, i);
  133. float num_samples_inv = 1.0f / (num_samples * kernel_data.integrator.num_all_lights);
  134. uint lamp_rng_hash = cmj_hash(state->rng_hash, i);
  135. for (int j = 0; j < num_samples; j++) {
  136. /* sample random position on given light */
  137. float light_u, light_v;
  138. path_branched_rng_2D(
  139. kg, lamp_rng_hash, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
  140. LightSample ls;
  141. lamp_light_sample(kg, i, light_u, light_v, ray->P, &ls);
  142. float3 tp = throughput;
  143. /* sample position on volume segment */
  144. float rphase = path_branched_rng_1D(
  145. kg, state->rng_hash, state, j, num_samples, PRNG_PHASE_CHANNEL);
  146. float rscatter = path_branched_rng_1D(
  147. kg, state->rng_hash, state, j, num_samples, PRNG_SCATTER_DISTANCE);
  148. VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
  149. state,
  150. ray,
  151. sd,
  152. &tp,
  153. rphase,
  154. rscatter,
  155. segment,
  156. (ls.t != FLT_MAX) ? &ls.P :
  157. NULL,
  158. false);
  159. /* todo: split up light_sample so we don't have to call it again with new position */
  160. if (result == VOLUME_PATH_SCATTERED &&
  161. lamp_light_sample(kg, i, light_u, light_v, sd->P, &ls)) {
  162. if (kernel_data.integrator.pdf_triangles != 0.0f)
  163. ls.pdf *= 2.0f;
  164. float terminate = path_branched_rng_light_termination(
  165. kg, state->rng_hash, state, j, num_samples);
  166. if (direct_emission(
  167. kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
  168. /* trace shadow ray */
  169. float3 shadow;
  170. if (!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &shadow)) {
  171. /* accumulate */
  172. path_radiance_accum_light(
  173. L, state, tp * num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. /* mesh light sampling */
  180. if (kernel_data.integrator.pdf_triangles != 0.0f) {
  181. int num_samples = kernel_data.integrator.mesh_light_samples;
  182. float num_samples_inv = 1.0f / num_samples;
  183. for (int j = 0; j < num_samples; j++) {
  184. /* sample random position on random triangle */
  185. float light_u, light_v;
  186. path_branched_rng_2D(
  187. kg, state->rng_hash, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
  188. /* only sample triangle lights */
  189. if (kernel_data.integrator.num_all_lights)
  190. light_u = 0.5f * light_u;
  191. LightSample ls;
  192. light_sample(kg, light_u, light_v, sd->time, ray->P, state->bounce, &ls);
  193. float3 tp = throughput;
  194. /* sample position on volume segment */
  195. float rphase = path_branched_rng_1D(
  196. kg, state->rng_hash, state, j, num_samples, PRNG_PHASE_CHANNEL);
  197. float rscatter = path_branched_rng_1D(
  198. kg, state->rng_hash, state, j, num_samples, PRNG_SCATTER_DISTANCE);
  199. VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
  200. state,
  201. ray,
  202. sd,
  203. &tp,
  204. rphase,
  205. rscatter,
  206. segment,
  207. (ls.t != FLT_MAX) ? &ls.P :
  208. NULL,
  209. false);
  210. /* todo: split up light_sample so we don't have to call it again with new position */
  211. if (result == VOLUME_PATH_SCATTERED &&
  212. light_sample(kg, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
  213. if (kernel_data.integrator.num_all_lights)
  214. ls.pdf *= 2.0f;
  215. float terminate = path_branched_rng_light_termination(
  216. kg, state->rng_hash, state, j, num_samples);
  217. if (direct_emission(
  218. kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
  219. /* trace shadow ray */
  220. float3 shadow;
  221. if (!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &shadow)) {
  222. /* accumulate */
  223. path_radiance_accum_light(
  224. L, state, tp * num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. else {
  232. /* sample random position on random light */
  233. float light_u, light_v;
  234. path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
  235. LightSample ls;
  236. light_sample(kg, light_u, light_v, sd->time, ray->P, state->bounce, &ls);
  237. float3 tp = throughput;
  238. /* sample position on volume segment */
  239. float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
  240. float rscatter = path_state_rng_1D(kg, state, PRNG_SCATTER_DISTANCE);
  241. VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
  242. state,
  243. ray,
  244. sd,
  245. &tp,
  246. rphase,
  247. rscatter,
  248. segment,
  249. (ls.t != FLT_MAX) ? &ls.P :
  250. NULL,
  251. false);
  252. /* todo: split up light_sample so we don't have to call it again with new position */
  253. if (result == VOLUME_PATH_SCATTERED &&
  254. light_sample(kg, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
  255. /* sample random light */
  256. float terminate = path_state_rng_light_termination(kg, state);
  257. if (direct_emission(
  258. kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
  259. /* trace shadow ray */
  260. float3 shadow;
  261. if (!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &shadow)) {
  262. /* accumulate */
  263. path_radiance_accum_light(L, state, tp, &L_light, shadow, 1.0f, is_lamp);
  264. }
  265. }
  266. }
  267. }
  268. # endif /* __EMISSION__ */
  269. }
  270. # endif /* __SPLIT_KERNEL__ */
  271. #endif /* __VOLUME_SCATTER__ */
  272. CCL_NAMESPACE_END