kernel_path_subsurface.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2017 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 __SUBSURFACE__
  18. # ifndef __KERNEL_CUDA__
  19. ccl_device
  20. # else
  21. ccl_device_inline
  22. # endif
  23. bool
  24. kernel_path_subsurface_scatter(KernelGlobals *kg,
  25. ShaderData *sd,
  26. ShaderData *emission_sd,
  27. PathRadiance *L,
  28. ccl_addr_space PathState *state,
  29. ccl_addr_space Ray *ray,
  30. ccl_addr_space float3 *throughput,
  31. ccl_addr_space SubsurfaceIndirectRays *ss_indirect)
  32. {
  33. PROFILING_INIT(kg, PROFILING_SUBSURFACE);
  34. float bssrdf_u, bssrdf_v;
  35. path_state_rng_2D(kg, state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
  36. const ShaderClosure *sc = shader_bssrdf_pick(sd, throughput, &bssrdf_u);
  37. /* do bssrdf scatter step if we picked a bssrdf closure */
  38. if (sc) {
  39. /* We should never have two consecutive BSSRDF bounces,
  40. * the second one should be converted to a diffuse BSDF to
  41. * avoid this.
  42. */
  43. kernel_assert(!(state->flag & PATH_RAY_DIFFUSE_ANCESTOR));
  44. uint lcg_state = lcg_state_init_addrspace(state, 0x68bc21eb);
  45. LocalIntersection ss_isect;
  46. int num_hits = subsurface_scatter_multi_intersect(
  47. kg, &ss_isect, sd, state, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
  48. # ifdef __VOLUME__
  49. bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
  50. sd->object_flag & SD_OBJECT_INTERSECTS_VOLUME;
  51. # endif /* __VOLUME__ */
  52. /* Closure memory will be overwritten, so read required variables now. */
  53. Bssrdf *bssrdf = (Bssrdf *)sc;
  54. ClosureType bssrdf_type = sc->type;
  55. float bssrdf_roughness = bssrdf->roughness;
  56. /* compute lighting with the BSDF closure */
  57. for (int hit = 0; hit < num_hits; hit++) {
  58. /* NOTE: We reuse the existing ShaderData, we assume the path
  59. * integration loop stops when this function returns true.
  60. */
  61. subsurface_scatter_multi_setup(kg, &ss_isect, hit, sd, state, bssrdf_type, bssrdf_roughness);
  62. kernel_path_surface_connect_light(kg, sd, emission_sd, *throughput, state, L);
  63. ccl_addr_space PathState *hit_state = &ss_indirect->state[ss_indirect->num_rays];
  64. ccl_addr_space Ray *hit_ray = &ss_indirect->rays[ss_indirect->num_rays];
  65. ccl_addr_space float3 *hit_tp = &ss_indirect->throughputs[ss_indirect->num_rays];
  66. PathRadianceState *hit_L_state = &ss_indirect->L_state[ss_indirect->num_rays];
  67. *hit_state = *state;
  68. *hit_ray = *ray;
  69. *hit_tp = *throughput;
  70. *hit_L_state = L->state;
  71. hit_state->rng_offset += PRNG_BOUNCE_NUM;
  72. if (kernel_path_surface_bounce(kg, sd, hit_tp, hit_state, hit_L_state, hit_ray)) {
  73. # ifdef __LAMP_MIS__
  74. hit_state->ray_t = 0.0f;
  75. # endif /* __LAMP_MIS__ */
  76. # ifdef __VOLUME__
  77. if (need_update_volume_stack) {
  78. Ray volume_ray = *ray;
  79. /* Setup ray from previous surface point to the new one. */
  80. volume_ray.D = normalize_len(hit_ray->P - volume_ray.P, &volume_ray.t);
  81. kernel_volume_stack_update_for_subsurface(
  82. kg, emission_sd, &volume_ray, hit_state->volume_stack);
  83. }
  84. # endif /* __VOLUME__ */
  85. ss_indirect->num_rays++;
  86. }
  87. }
  88. return true;
  89. }
  90. return false;
  91. }
  92. ccl_device_inline void kernel_path_subsurface_init_indirect(
  93. ccl_addr_space SubsurfaceIndirectRays *ss_indirect)
  94. {
  95. ss_indirect->num_rays = 0;
  96. }
  97. ccl_device void kernel_path_subsurface_setup_indirect(
  98. KernelGlobals *kg,
  99. ccl_addr_space SubsurfaceIndirectRays *ss_indirect,
  100. ccl_addr_space PathState *state,
  101. ccl_addr_space Ray *ray,
  102. PathRadiance *L,
  103. ccl_addr_space float3 *throughput)
  104. {
  105. /* Setup state, ray and throughput for indirect SSS rays. */
  106. ss_indirect->num_rays--;
  107. path_radiance_sum_indirect(L);
  108. path_radiance_reset_indirect(L);
  109. *state = ss_indirect->state[ss_indirect->num_rays];
  110. *ray = ss_indirect->rays[ss_indirect->num_rays];
  111. L->state = ss_indirect->L_state[ss_indirect->num_rays];
  112. *throughput = ss_indirect->throughputs[ss_indirect->num_rays];
  113. state->rng_offset += ss_indirect->num_rays * PRNG_BOUNCE_NUM;
  114. }
  115. #endif /* __SUBSURFACE__ */
  116. CCL_NAMESPACE_END