kernel_next_iteration_setup.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2011-2015 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. /*This kernel takes care of setting up ray for the next iteration of
  18. * path-iteration and accumulating radiance corresponding to AO and
  19. * direct-lighting
  20. *
  21. * Ray state of rays that are terminated in this kernel are changed
  22. * to RAY_UPDATE_BUFFER.
  23. *
  24. * Note on queues:
  25. * This kernel fetches rays from the queue QUEUE_ACTIVE_AND_REGENERATED_RAYS
  26. * and processes only the rays of state RAY_ACTIVE.
  27. * There are different points in this kernel where a ray may terminate and
  28. * reach RAY_UPDATE_BUFF state. These rays are enqueued into
  29. * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue. These rays will still be present
  30. * in QUEUE_ACTIVE_AND_REGENERATED_RAYS queue, but since their ray-state has
  31. * been changed to RAY_UPDATE_BUFF, there is no problem.
  32. *
  33. * State of queues when this kernel is called:
  34. * At entry,
  35. * - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE,
  36. * RAY_REGENERATED, RAY_UPDATE_BUFFER rays.
  37. * - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
  38. * RAY_TO_REGENERATE and RAY_UPDATE_BUFFER rays.
  39. * At exit,
  40. * - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE,
  41. * RAY_REGENERATED and more RAY_UPDATE_BUFFER rays.
  42. * - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
  43. * RAY_TO_REGENERATE and more RAY_UPDATE_BUFFER rays.
  44. */
  45. #ifdef __BRANCHED_PATH__
  46. ccl_device_inline void kernel_split_branched_indirect_light_init(KernelGlobals *kg, int ray_index)
  47. {
  48. kernel_split_branched_path_indirect_loop_init(kg, ray_index);
  49. ADD_RAY_FLAG(kernel_split_state.ray_state, ray_index, RAY_BRANCHED_LIGHT_INDIRECT);
  50. }
  51. ccl_device void kernel_split_branched_transparent_bounce(KernelGlobals *kg, int ray_index)
  52. {
  53. ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
  54. ShaderData *sd = kernel_split_sd(sd, ray_index);
  55. ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
  56. ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
  57. # ifdef __VOLUME__
  58. if (!(sd->flag & SD_HAS_ONLY_VOLUME)) {
  59. # endif
  60. /* continue in case of transparency */
  61. *throughput *= shader_bsdf_transparency(kg, sd);
  62. if (is_zero(*throughput)) {
  63. kernel_split_path_end(kg, ray_index);
  64. return;
  65. }
  66. /* Update Path State */
  67. path_state_next(kg, state, LABEL_TRANSPARENT);
  68. # ifdef __VOLUME__
  69. }
  70. else {
  71. if (!path_state_volume_next(kg, state)) {
  72. kernel_split_path_end(kg, ray_index);
  73. return;
  74. }
  75. }
  76. # endif
  77. ray->P = ray_offset(sd->P, -sd->Ng);
  78. ray->t -= sd->ray_length; /* clipping works through transparent */
  79. # ifdef __RAY_DIFFERENTIALS__
  80. ray->dP = sd->dP;
  81. ray->dD.dx = -sd->dI.dx;
  82. ray->dD.dy = -sd->dI.dy;
  83. # endif /* __RAY_DIFFERENTIALS__ */
  84. # ifdef __VOLUME__
  85. /* enter/exit volume */
  86. kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
  87. # endif /* __VOLUME__ */
  88. }
  89. #endif /* __BRANCHED_PATH__ */
  90. ccl_device void kernel_next_iteration_setup(KernelGlobals *kg,
  91. ccl_local_param unsigned int *local_queue_atomics)
  92. {
  93. if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
  94. *local_queue_atomics = 0;
  95. }
  96. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  97. if (ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
  98. /* If we are here, then it means that scene-intersect kernel
  99. * has already been executed at least once. From the next time,
  100. * scene-intersect kernel may operate on queues to fetch ray index
  101. */
  102. *kernel_split_params.use_queues_flag = 1;
  103. /* Mark queue indices of QUEUE_SHADOW_RAY_CAST_AO_RAYS and
  104. * QUEUE_SHADOW_RAY_CAST_DL_RAYS queues that were made empty during the
  105. * previous kernel.
  106. */
  107. kernel_split_params.queue_index[QUEUE_SHADOW_RAY_CAST_AO_RAYS] = 0;
  108. kernel_split_params.queue_index[QUEUE_SHADOW_RAY_CAST_DL_RAYS] = 0;
  109. }
  110. int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  111. ray_index = get_ray_index(kg,
  112. ray_index,
  113. QUEUE_ACTIVE_AND_REGENERATED_RAYS,
  114. kernel_split_state.queue_data,
  115. kernel_split_params.queue_size,
  116. 0);
  117. ccl_global char *ray_state = kernel_split_state.ray_state;
  118. #ifdef __VOLUME__
  119. /* Reactivate only volume rays here, most surface work was skipped. */
  120. if (IS_STATE(ray_state, ray_index, RAY_HAS_ONLY_VOLUME)) {
  121. ASSIGN_RAY_STATE(ray_state, ray_index, RAY_ACTIVE);
  122. }
  123. #endif
  124. bool active = IS_STATE(ray_state, ray_index, RAY_ACTIVE);
  125. if (active) {
  126. ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
  127. ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
  128. ShaderData *sd = kernel_split_sd(sd, ray_index);
  129. ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
  130. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  131. #ifdef __BRANCHED_PATH__
  132. if (!kernel_data.integrator.branched || IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
  133. #endif
  134. /* Compute direct lighting and next bounce. */
  135. if (!kernel_path_surface_bounce(kg, sd, throughput, state, &L->state, ray)) {
  136. kernel_split_path_end(kg, ray_index);
  137. }
  138. #ifdef __BRANCHED_PATH__
  139. }
  140. else if (sd->flag & SD_HAS_ONLY_VOLUME) {
  141. kernel_split_branched_transparent_bounce(kg, ray_index);
  142. }
  143. else {
  144. kernel_split_branched_indirect_light_init(kg, ray_index);
  145. if (kernel_split_branched_path_surface_indirect_light_iter(
  146. kg, ray_index, 1.0f, kernel_split_sd(branched_state_sd, ray_index), true, true)) {
  147. ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
  148. }
  149. else {
  150. kernel_split_branched_path_indirect_loop_end(kg, ray_index);
  151. kernel_split_branched_transparent_bounce(kg, ray_index);
  152. }
  153. }
  154. #endif /* __BRANCHED_PATH__ */
  155. }
  156. /* Enqueue RAY_UPDATE_BUFFER rays. */
  157. enqueue_ray_index_local(ray_index,
  158. QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
  159. IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER) && active,
  160. kernel_split_params.queue_size,
  161. local_queue_atomics,
  162. kernel_split_state.queue_data,
  163. kernel_split_params.queue_index);
  164. #ifdef __BRANCHED_PATH__
  165. /* iter loop */
  166. if (ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
  167. kernel_split_params.queue_index[QUEUE_LIGHT_INDIRECT_ITER] = 0;
  168. }
  169. ray_index = get_ray_index(kg,
  170. ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0),
  171. QUEUE_LIGHT_INDIRECT_ITER,
  172. kernel_split_state.queue_data,
  173. kernel_split_params.queue_size,
  174. 1);
  175. if (IS_STATE(ray_state, ray_index, RAY_LIGHT_INDIRECT_NEXT_ITER)) {
  176. /* for render passes, sum and reset indirect light pass variables
  177. * for the next samples */
  178. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  179. path_radiance_sum_indirect(L);
  180. path_radiance_reset_indirect(L);
  181. if (kernel_split_branched_path_surface_indirect_light_iter(
  182. kg, ray_index, 1.0f, kernel_split_sd(branched_state_sd, ray_index), true, true)) {
  183. ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
  184. }
  185. else {
  186. kernel_split_branched_path_indirect_loop_end(kg, ray_index);
  187. kernel_split_branched_transparent_bounce(kg, ray_index);
  188. }
  189. }
  190. # ifdef __VOLUME__
  191. /* Enqueue RAY_VOLUME_INDIRECT_NEXT_ITER rays */
  192. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  193. if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
  194. *local_queue_atomics = 0;
  195. }
  196. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  197. ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  198. enqueue_ray_index_local(
  199. ray_index,
  200. QUEUE_VOLUME_INDIRECT_ITER,
  201. IS_STATE(kernel_split_state.ray_state, ray_index, RAY_VOLUME_INDIRECT_NEXT_ITER),
  202. kernel_split_params.queue_size,
  203. local_queue_atomics,
  204. kernel_split_state.queue_data,
  205. kernel_split_params.queue_index);
  206. # endif /* __VOLUME__ */
  207. # ifdef __SUBSURFACE__
  208. /* Enqueue RAY_SUBSURFACE_INDIRECT_NEXT_ITER rays */
  209. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  210. if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
  211. *local_queue_atomics = 0;
  212. }
  213. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  214. ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  215. enqueue_ray_index_local(
  216. ray_index,
  217. QUEUE_SUBSURFACE_INDIRECT_ITER,
  218. IS_STATE(kernel_split_state.ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER),
  219. kernel_split_params.queue_size,
  220. local_queue_atomics,
  221. kernel_split_state.queue_data,
  222. kernel_split_params.queue_index);
  223. # endif /* __SUBSURFACE__ */
  224. #endif /* __BRANCHED_PATH__ */
  225. }
  226. CCL_NAMESPACE_END