kernel_subsurface_scatter.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2011-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. #if defined(__BRANCHED_PATH__) && defined(__SUBSURFACE__)
  18. ccl_device_inline void kernel_split_branched_path_subsurface_indirect_light_init(KernelGlobals *kg,
  19. int ray_index)
  20. {
  21. kernel_split_branched_path_indirect_loop_init(kg, ray_index);
  22. SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
  23. branched_state->ss_next_closure = 0;
  24. branched_state->ss_next_sample = 0;
  25. branched_state->num_hits = 0;
  26. branched_state->next_hit = 0;
  27. ADD_RAY_FLAG(kernel_split_state.ray_state, ray_index, RAY_BRANCHED_SUBSURFACE_INDIRECT);
  28. }
  29. ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_iter(
  30. KernelGlobals *kg, int ray_index)
  31. {
  32. SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
  33. ShaderData *sd = kernel_split_sd(branched_state_sd, ray_index);
  34. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  35. ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
  36. for (int i = branched_state->ss_next_closure; i < sd->num_closure; i++) {
  37. ShaderClosure *sc = &sd->closure[i];
  38. if (!CLOSURE_IS_BSSRDF(sc->type))
  39. continue;
  40. /* Closure memory will be overwritten, so read required variables now. */
  41. Bssrdf *bssrdf = (Bssrdf *)sc;
  42. ClosureType bssrdf_type = sc->type;
  43. float bssrdf_roughness = bssrdf->roughness;
  44. /* set up random number generator */
  45. if (branched_state->ss_next_sample == 0 && branched_state->next_hit == 0 &&
  46. branched_state->next_closure == 0 && branched_state->next_sample == 0) {
  47. branched_state->lcg_state = lcg_state_init_addrspace(&branched_state->path_state,
  48. 0x68bc21eb);
  49. }
  50. int num_samples = kernel_data.integrator.subsurface_samples * 3;
  51. float num_samples_inv = 1.0f / num_samples;
  52. uint bssrdf_rng_hash = cmj_hash(branched_state->path_state.rng_hash, i);
  53. /* do subsurface scatter step with copy of shader data, this will
  54. * replace the BSSRDF with a diffuse BSDF closure */
  55. for (int j = branched_state->ss_next_sample; j < num_samples; j++) {
  56. ccl_global PathState *hit_state = &kernel_split_state.path_state[ray_index];
  57. *hit_state = branched_state->path_state;
  58. hit_state->rng_hash = bssrdf_rng_hash;
  59. path_state_branch(hit_state, j, num_samples);
  60. ccl_global LocalIntersection *ss_isect = &branched_state->ss_isect;
  61. float bssrdf_u, bssrdf_v;
  62. path_branched_rng_2D(
  63. kg, bssrdf_rng_hash, hit_state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
  64. /* intersection is expensive so avoid doing multiple times for the same input */
  65. if (branched_state->next_hit == 0 && branched_state->next_closure == 0 &&
  66. branched_state->next_sample == 0) {
  67. uint lcg_state = branched_state->lcg_state;
  68. LocalIntersection ss_isect_private;
  69. branched_state->num_hits = subsurface_scatter_multi_intersect(
  70. kg, &ss_isect_private, sd, hit_state, sc, &lcg_state, bssrdf_u, bssrdf_v, true);
  71. branched_state->lcg_state = lcg_state;
  72. *ss_isect = ss_isect_private;
  73. }
  74. hit_state->rng_offset += PRNG_BOUNCE_NUM;
  75. # ifdef __VOLUME__
  76. Ray volume_ray = branched_state->ray;
  77. bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
  78. sd->object_flag & SD_OBJECT_INTERSECTS_VOLUME;
  79. # endif /* __VOLUME__ */
  80. /* compute lighting with the BSDF closure */
  81. for (int hit = branched_state->next_hit; hit < branched_state->num_hits; hit++) {
  82. ShaderData *bssrdf_sd = kernel_split_sd(sd, ray_index);
  83. *bssrdf_sd = *sd; /* note: copy happens each iteration of inner loop, this is
  84. * important as the indirect path will write into bssrdf_sd */
  85. LocalIntersection ss_isect_private = *ss_isect;
  86. subsurface_scatter_multi_setup(
  87. kg, &ss_isect_private, hit, bssrdf_sd, hit_state, bssrdf_type, bssrdf_roughness);
  88. *ss_isect = ss_isect_private;
  89. # ifdef __VOLUME__
  90. if (need_update_volume_stack) {
  91. /* Setup ray from previous surface point to the new one. */
  92. float3 P = ray_offset(bssrdf_sd->P, -bssrdf_sd->Ng);
  93. volume_ray.D = normalize_len(P - volume_ray.P, &volume_ray.t);
  94. for (int k = 0; k < VOLUME_STACK_SIZE; k++) {
  95. hit_state->volume_stack[k] = branched_state->path_state.volume_stack[k];
  96. }
  97. kernel_volume_stack_update_for_subsurface(
  98. kg, emission_sd, &volume_ray, hit_state->volume_stack);
  99. }
  100. # endif /* __VOLUME__ */
  101. # ifdef __EMISSION__
  102. if (branched_state->next_closure == 0 && branched_state->next_sample == 0) {
  103. /* direct light */
  104. if (kernel_data.integrator.use_direct_light) {
  105. int all = (kernel_data.integrator.sample_all_lights_direct) ||
  106. (hit_state->flag & PATH_RAY_SHADOW_CATCHER);
  107. kernel_branched_path_surface_connect_light(kg,
  108. bssrdf_sd,
  109. emission_sd,
  110. hit_state,
  111. branched_state->throughput,
  112. num_samples_inv,
  113. L,
  114. all);
  115. }
  116. }
  117. # endif /* __EMISSION__ */
  118. /* indirect light */
  119. if (kernel_split_branched_path_surface_indirect_light_iter(
  120. kg, ray_index, num_samples_inv, bssrdf_sd, false, false)) {
  121. branched_state->ss_next_closure = i;
  122. branched_state->ss_next_sample = j;
  123. branched_state->next_hit = hit;
  124. return true;
  125. }
  126. branched_state->next_closure = 0;
  127. }
  128. branched_state->next_hit = 0;
  129. }
  130. branched_state->ss_next_sample = 0;
  131. }
  132. branched_state->ss_next_closure = sd->num_closure;
  133. branched_state->waiting_on_shared_samples = (branched_state->shared_sample_count > 0);
  134. if (branched_state->waiting_on_shared_samples) {
  135. return true;
  136. }
  137. kernel_split_branched_path_indirect_loop_end(kg, ray_index);
  138. return false;
  139. }
  140. #endif /* __BRANCHED_PATH__ && __SUBSURFACE__ */
  141. ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
  142. {
  143. int thread_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  144. if (thread_index == 0) {
  145. /* We will empty both queues in this kernel. */
  146. kernel_split_params.queue_index[QUEUE_ACTIVE_AND_REGENERATED_RAYS] = 0;
  147. kernel_split_params.queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
  148. }
  149. int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  150. ray_index = get_ray_index(kg,
  151. ray_index,
  152. QUEUE_ACTIVE_AND_REGENERATED_RAYS,
  153. kernel_split_state.queue_data,
  154. kernel_split_params.queue_size,
  155. 1);
  156. get_ray_index(kg,
  157. thread_index,
  158. QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
  159. kernel_split_state.queue_data,
  160. kernel_split_params.queue_size,
  161. 1);
  162. #ifdef __SUBSURFACE__
  163. ccl_global char *ray_state = kernel_split_state.ray_state;
  164. if (IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
  165. ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
  166. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  167. ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
  168. ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
  169. ccl_global SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
  170. ShaderData *sd = kernel_split_sd(sd, ray_index);
  171. ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
  172. if (sd->flag & SD_BSSRDF) {
  173. # ifdef __BRANCHED_PATH__
  174. if (!kernel_data.integrator.branched ||
  175. IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
  176. # endif
  177. if (kernel_path_subsurface_scatter(
  178. kg, sd, emission_sd, L, state, ray, throughput, ss_indirect)) {
  179. kernel_split_path_end(kg, ray_index);
  180. }
  181. # ifdef __BRANCHED_PATH__
  182. }
  183. else {
  184. kernel_split_branched_path_subsurface_indirect_light_init(kg, ray_index);
  185. if (kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
  186. ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
  187. }
  188. }
  189. # endif
  190. }
  191. }
  192. # ifdef __BRANCHED_PATH__
  193. if (ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
  194. kernel_split_params.queue_index[QUEUE_SUBSURFACE_INDIRECT_ITER] = 0;
  195. }
  196. /* iter loop */
  197. ray_index = get_ray_index(kg,
  198. ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0),
  199. QUEUE_SUBSURFACE_INDIRECT_ITER,
  200. kernel_split_state.queue_data,
  201. kernel_split_params.queue_size,
  202. 1);
  203. if (IS_STATE(ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER)) {
  204. /* for render passes, sum and reset indirect light pass variables
  205. * for the next samples */
  206. path_radiance_sum_indirect(&kernel_split_state.path_radiance[ray_index]);
  207. path_radiance_reset_indirect(&kernel_split_state.path_radiance[ray_index]);
  208. if (kernel_split_branched_path_subsurface_indirect_light_iter(kg, ray_index)) {
  209. ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
  210. }
  211. }
  212. # endif /* __BRANCHED_PATH__ */
  213. #endif /* __SUBSURFACE__ */
  214. }
  215. CCL_NAMESPACE_END