kernel_holdout_emission_blurring_pathtermination_ao.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 the logic to process "material of type holdout",
  18. * indirect primitive emission, bsdf blurring, probabilistic path termination
  19. * and AO.
  20. *
  21. * This kernels determines the rays for which a shadow_blocked() function
  22. * associated with AO should be executed. Those rays for which a
  23. * shadow_blocked() function for AO must be executed are marked with flag
  24. * RAY_SHADOW_RAY_CAST_ao and enqueued into the queue
  25. * QUEUE_SHADOW_RAY_CAST_AO_RAYS
  26. *
  27. * Ray state of rays that are terminated in this kernel are changed to RAY_UPDATE_BUFFER
  28. *
  29. * Note on Queues:
  30. * This kernel fetches rays from the queue QUEUE_ACTIVE_AND_REGENERATED_RAYS
  31. * and processes only the rays of state RAY_ACTIVE.
  32. * There are different points in this kernel where a ray may terminate and
  33. * reach RAY_UPDATE_BUFFER state. These rays are enqueued into
  34. * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue. These rays will still be present
  35. * in QUEUE_ACTIVE_AND_REGENERATED_RAYS queue, but since their ray-state has
  36. * been changed to RAY_UPDATE_BUFFER, there is no problem.
  37. *
  38. * State of queues when this kernel is called:
  39. * At entry,
  40. * - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE and
  41. * RAY_REGENERATED rays
  42. * - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
  43. * RAY_TO_REGENERATE rays.
  44. * - QUEUE_SHADOW_RAY_CAST_AO_RAYS will be empty.
  45. * At exit,
  46. * - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE,
  47. * RAY_REGENERATED and RAY_UPDATE_BUFFER rays.
  48. * - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
  49. * RAY_TO_REGENERATE and RAY_UPDATE_BUFFER rays.
  50. * - QUEUE_SHADOW_RAY_CAST_AO_RAYS will be filled with rays marked with
  51. * flag RAY_SHADOW_RAY_CAST_AO
  52. */
  53. ccl_device void kernel_holdout_emission_blurring_pathtermination_ao(
  54. KernelGlobals *kg, ccl_local_param BackgroundAOLocals *locals)
  55. {
  56. if (ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
  57. locals->queue_atomics_bg = 0;
  58. locals->queue_atomics_ao = 0;
  59. }
  60. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  61. #ifdef __AO__
  62. char enqueue_flag = 0;
  63. #endif
  64. int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
  65. ray_index = get_ray_index(kg,
  66. ray_index,
  67. QUEUE_ACTIVE_AND_REGENERATED_RAYS,
  68. kernel_split_state.queue_data,
  69. kernel_split_params.queue_size,
  70. 0);
  71. #ifdef __COMPUTE_DEVICE_GPU__
  72. /* If we are executing on a GPU device, we exit all threads that are not
  73. * required.
  74. *
  75. * If we are executing on a CPU device, then we need to keep all threads
  76. * active since we have barrier() calls later in the kernel. CPU devices,
  77. * expect all threads to execute barrier statement.
  78. */
  79. if (ray_index == QUEUE_EMPTY_SLOT) {
  80. return;
  81. }
  82. #endif /* __COMPUTE_DEVICE_GPU__ */
  83. #ifndef __COMPUTE_DEVICE_GPU__
  84. if (ray_index != QUEUE_EMPTY_SLOT) {
  85. #endif
  86. ccl_global PathState *state = 0x0;
  87. float3 throughput;
  88. ccl_global char *ray_state = kernel_split_state.ray_state;
  89. ShaderData *sd = kernel_split_sd(sd, ray_index);
  90. if (IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
  91. uint buffer_offset = kernel_split_state.buffer_offset[ray_index];
  92. ccl_global float *buffer = kernel_split_params.tile.buffer + buffer_offset;
  93. ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
  94. ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
  95. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  96. throughput = kernel_split_state.throughput[ray_index];
  97. state = &kernel_split_state.path_state[ray_index];
  98. if (!kernel_path_shader_apply(kg, sd, state, ray, throughput, emission_sd, L, buffer)) {
  99. kernel_split_path_end(kg, ray_index);
  100. }
  101. }
  102. if (IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
  103. /* Path termination. this is a strange place to put the termination, it's
  104. * mainly due to the mixed in MIS that we use. gives too many unneeded
  105. * shader evaluations, only need emission if we are going to terminate.
  106. */
  107. float probability = path_state_continuation_probability(kg, state, throughput);
  108. if (probability == 0.0f) {
  109. kernel_split_path_end(kg, ray_index);
  110. }
  111. else if (probability < 1.0f) {
  112. float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
  113. if (terminate >= probability) {
  114. kernel_split_path_end(kg, ray_index);
  115. }
  116. else {
  117. kernel_split_state.throughput[ray_index] = throughput / probability;
  118. }
  119. }
  120. if (IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
  121. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  122. kernel_update_denoising_features(kg, sd, state, L);
  123. }
  124. }
  125. #ifdef __AO__
  126. if (IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
  127. /* ambient occlusion */
  128. if (kernel_data.integrator.use_ambient_occlusion) {
  129. enqueue_flag = 1;
  130. }
  131. }
  132. #endif /* __AO__ */
  133. #ifndef __COMPUTE_DEVICE_GPU__
  134. }
  135. #endif
  136. #ifdef __AO__
  137. /* Enqueue to-shadow-ray-cast rays. */
  138. enqueue_ray_index_local(ray_index,
  139. QUEUE_SHADOW_RAY_CAST_AO_RAYS,
  140. enqueue_flag,
  141. kernel_split_params.queue_size,
  142. &locals->queue_atomics_ao,
  143. kernel_split_state.queue_data,
  144. kernel_split_params.queue_index);
  145. #endif
  146. }
  147. CCL_NAMESPACE_END