kernel_direct_lighting.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 direct lighting logic.
  18. * However, the "shadow ray cast" part of direct lighting is handled
  19. * in the next kernel.
  20. *
  21. * This kernels determines the rays for which a shadow_blocked() function
  22. * associated with direct lighting should be executed. Those rays for which
  23. * a shadow_blocked() function for direct-lighting must be executed, are
  24. * marked with flag RAY_SHADOW_RAY_CAST_DL and enqueued into the queue
  25. * QUEUE_SHADOW_RAY_CAST_DL_RAYS
  26. *
  27. * Note on Queues:
  28. * This kernel only reads from the QUEUE_ACTIVE_AND_REGENERATED_RAYS queue
  29. * and processes only the rays of state RAY_ACTIVE; If a ray needs to execute
  30. * the corresponding shadow_blocked part, after direct lighting, the ray is
  31. * marked with RAY_SHADOW_RAY_CAST_DL flag.
  32. *
  33. * State of queues when this kernel is called:
  34. * - State of queues QUEUE_ACTIVE_AND_REGENERATED_RAYS and
  35. * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be same before and after this
  36. * kernel call.
  37. * - QUEUE_SHADOW_RAY_CAST_DL_RAYS queue will be filled with rays for which a
  38. * shadow_blocked function must be executed, after this kernel call
  39. * Before this kernel call the QUEUE_SHADOW_RAY_CAST_DL_RAYS will be empty.
  40. */
  41. ccl_device void kernel_direct_lighting(KernelGlobals *kg,
  42. ccl_local_param unsigned int *local_queue_atomics)
  43. {
  44. if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
  45. *local_queue_atomics = 0;
  46. }
  47. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  48. char enqueue_flag = 0;
  49. int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  50. ray_index = get_ray_index(kg,
  51. ray_index,
  52. QUEUE_ACTIVE_AND_REGENERATED_RAYS,
  53. kernel_split_state.queue_data,
  54. kernel_split_params.queue_size,
  55. 0);
  56. if (IS_STATE(kernel_split_state.ray_state, ray_index, RAY_ACTIVE)) {
  57. ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
  58. ShaderData *sd = kernel_split_sd(sd, ray_index);
  59. /* direct lighting */
  60. #ifdef __EMISSION__
  61. bool flag = (kernel_data.integrator.use_direct_light && (sd->flag & SD_BSDF_HAS_EVAL));
  62. # ifdef __BRANCHED_PATH__
  63. if (flag && kernel_data.integrator.branched) {
  64. flag = false;
  65. enqueue_flag = 1;
  66. }
  67. # endif /* __BRANCHED_PATH__ */
  68. # ifdef __SHADOW_TRICKS__
  69. if (flag && state->flag & PATH_RAY_SHADOW_CATCHER) {
  70. flag = false;
  71. enqueue_flag = 1;
  72. }
  73. # endif /* __SHADOW_TRICKS__ */
  74. if (flag) {
  75. /* Sample illumination from lights to find path contribution. */
  76. float light_u, light_v;
  77. path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
  78. float terminate = path_state_rng_light_termination(kg, state);
  79. LightSample ls;
  80. if (light_sample(kg, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
  81. Ray light_ray;
  82. light_ray.time = sd->time;
  83. BsdfEval L_light;
  84. bool is_lamp;
  85. if (direct_emission(kg,
  86. sd,
  87. AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]),
  88. &ls,
  89. state,
  90. &light_ray,
  91. &L_light,
  92. &is_lamp,
  93. terminate)) {
  94. /* Write intermediate data to global memory to access from
  95. * the next kernel.
  96. */
  97. kernel_split_state.light_ray[ray_index] = light_ray;
  98. kernel_split_state.bsdf_eval[ray_index] = L_light;
  99. kernel_split_state.is_lamp[ray_index] = is_lamp;
  100. /* Mark ray state for next shadow kernel. */
  101. enqueue_flag = 1;
  102. }
  103. }
  104. }
  105. #endif /* __EMISSION__ */
  106. }
  107. #ifdef __EMISSION__
  108. /* Enqueue RAY_SHADOW_RAY_CAST_DL rays. */
  109. enqueue_ray_index_local(ray_index,
  110. QUEUE_SHADOW_RAY_CAST_DL_RAYS,
  111. enqueue_flag,
  112. kernel_split_params.queue_size,
  113. local_queue_atomics,
  114. kernel_split_state.queue_data,
  115. kernel_split_params.queue_index);
  116. #endif
  117. #ifdef __BRANCHED_PATH__
  118. /* Enqueue RAY_LIGHT_INDIRECT_NEXT_ITER rays
  119. * this is the last kernel before next_iteration_setup that uses local atomics so we do this here
  120. */
  121. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  122. if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
  123. *local_queue_atomics = 0;
  124. }
  125. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  126. ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  127. enqueue_ray_index_local(
  128. ray_index,
  129. QUEUE_LIGHT_INDIRECT_ITER,
  130. IS_STATE(kernel_split_state.ray_state, ray_index, RAY_LIGHT_INDIRECT_NEXT_ITER),
  131. kernel_split_params.queue_size,
  132. local_queue_atomics,
  133. kernel_split_state.queue_data,
  134. kernel_split_params.queue_index);
  135. #endif /* __BRANCHED_PATH__ */
  136. }
  137. CCL_NAMESPACE_END