kernel_branched.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifdef __BRANCHED_PATH__
  18. /* sets up the various state needed to do an indirect loop */
  19. ccl_device_inline void kernel_split_branched_path_indirect_loop_init(KernelGlobals *kg,
  20. int ray_index)
  21. {
  22. SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
  23. /* save a copy of the state to restore later */
  24. # define BRANCHED_STORE(name) branched_state->name = kernel_split_state.name[ray_index];
  25. BRANCHED_STORE(path_state);
  26. BRANCHED_STORE(throughput);
  27. BRANCHED_STORE(ray);
  28. BRANCHED_STORE(isect);
  29. BRANCHED_STORE(ray_state);
  30. *kernel_split_sd(branched_state_sd, ray_index) = *kernel_split_sd(sd, ray_index);
  31. for (int i = 0; i < kernel_split_sd(branched_state_sd, ray_index)->num_closure; i++) {
  32. kernel_split_sd(branched_state_sd, ray_index)->closure[i] =
  33. kernel_split_sd(sd, ray_index)->closure[i];
  34. }
  35. # undef BRANCHED_STORE
  36. /* set loop counters to intial position */
  37. branched_state->next_closure = 0;
  38. branched_state->next_sample = 0;
  39. }
  40. /* ends an indirect loop and restores the previous state */
  41. ccl_device_inline void kernel_split_branched_path_indirect_loop_end(KernelGlobals *kg,
  42. int ray_index)
  43. {
  44. SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
  45. /* restore state */
  46. # define BRANCHED_RESTORE(name) kernel_split_state.name[ray_index] = branched_state->name;
  47. BRANCHED_RESTORE(path_state);
  48. BRANCHED_RESTORE(throughput);
  49. BRANCHED_RESTORE(ray);
  50. BRANCHED_RESTORE(isect);
  51. BRANCHED_RESTORE(ray_state);
  52. *kernel_split_sd(sd, ray_index) = *kernel_split_sd(branched_state_sd, ray_index);
  53. for (int i = 0; i < kernel_split_sd(branched_state_sd, ray_index)->num_closure; i++) {
  54. kernel_split_sd(sd, ray_index)->closure[i] =
  55. kernel_split_sd(branched_state_sd, ray_index)->closure[i];
  56. }
  57. # undef BRANCHED_RESTORE
  58. /* leave indirect loop */
  59. REMOVE_RAY_FLAG(kernel_split_state.ray_state, ray_index, RAY_BRANCHED_INDIRECT);
  60. }
  61. ccl_device_inline bool kernel_split_branched_indirect_start_shared(KernelGlobals *kg,
  62. int ray_index)
  63. {
  64. ccl_global char *ray_state = kernel_split_state.ray_state;
  65. int inactive_ray = dequeue_ray_index(QUEUE_INACTIVE_RAYS,
  66. kernel_split_state.queue_data,
  67. kernel_split_params.queue_size,
  68. kernel_split_params.queue_index);
  69. if (!IS_STATE(ray_state, inactive_ray, RAY_INACTIVE)) {
  70. return false;
  71. }
  72. # define SPLIT_DATA_ENTRY(type, name, num) \
  73. if (num) { \
  74. kernel_split_state.name[inactive_ray] = kernel_split_state.name[ray_index]; \
  75. }
  76. SPLIT_DATA_ENTRIES_BRANCHED_SHARED
  77. # undef SPLIT_DATA_ENTRY
  78. *kernel_split_sd(sd, inactive_ray) = *kernel_split_sd(sd, ray_index);
  79. for (int i = 0; i < kernel_split_sd(sd, ray_index)->num_closure; i++) {
  80. kernel_split_sd(sd, inactive_ray)->closure[i] = kernel_split_sd(sd, ray_index)->closure[i];
  81. }
  82. kernel_split_state.branched_state[inactive_ray].shared_sample_count = 0;
  83. kernel_split_state.branched_state[inactive_ray].original_ray = ray_index;
  84. kernel_split_state.branched_state[inactive_ray].waiting_on_shared_samples = false;
  85. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  86. PathRadiance *inactive_L = &kernel_split_state.path_radiance[inactive_ray];
  87. path_radiance_init(inactive_L, kernel_data.film.use_light_pass);
  88. path_radiance_copy_indirect(inactive_L, L);
  89. ray_state[inactive_ray] = RAY_REGENERATED;
  90. ADD_RAY_FLAG(ray_state, inactive_ray, RAY_BRANCHED_INDIRECT_SHARED);
  91. ADD_RAY_FLAG(ray_state, inactive_ray, IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT));
  92. atomic_fetch_and_inc_uint32(
  93. (ccl_global uint *)&kernel_split_state.branched_state[ray_index].shared_sample_count);
  94. return true;
  95. }
  96. /* bounce off surface and integrate indirect light */
  97. ccl_device_noinline bool kernel_split_branched_path_surface_indirect_light_iter(
  98. KernelGlobals *kg,
  99. int ray_index,
  100. float num_samples_adjust,
  101. ShaderData *saved_sd,
  102. bool reset_path_state,
  103. bool wait_for_shared)
  104. {
  105. SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
  106. ShaderData *sd = saved_sd;
  107. PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
  108. float3 throughput = branched_state->throughput;
  109. ccl_global PathState *ps = &kernel_split_state.path_state[ray_index];
  110. float sum_sample_weight = 0.0f;
  111. # ifdef __DENOISING_FEATURES__
  112. if (ps->denoising_feature_weight > 0.0f) {
  113. for (int i = 0; i < sd->num_closure; i++) {
  114. const ShaderClosure *sc = &sd->closure[i];
  115. /* transparency is not handled here, but in outer loop */
  116. if (!CLOSURE_IS_BSDF(sc->type) || CLOSURE_IS_BSDF_TRANSPARENT(sc->type)) {
  117. continue;
  118. }
  119. sum_sample_weight += sc->sample_weight;
  120. }
  121. }
  122. else {
  123. sum_sample_weight = 1.0f;
  124. }
  125. # endif /* __DENOISING_FEATURES__ */
  126. for (int i = branched_state->next_closure; i < sd->num_closure; i++) {
  127. const ShaderClosure *sc = &sd->closure[i];
  128. if (!CLOSURE_IS_BSDF(sc->type))
  129. continue;
  130. /* transparency is not handled here, but in outer loop */
  131. if (sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
  132. continue;
  133. int num_samples;
  134. if (CLOSURE_IS_BSDF_DIFFUSE(sc->type))
  135. num_samples = kernel_data.integrator.diffuse_samples;
  136. else if (CLOSURE_IS_BSDF_BSSRDF(sc->type))
  137. num_samples = 1;
  138. else if (CLOSURE_IS_BSDF_GLOSSY(sc->type))
  139. num_samples = kernel_data.integrator.glossy_samples;
  140. else
  141. num_samples = kernel_data.integrator.transmission_samples;
  142. num_samples = ceil_to_int(num_samples_adjust * num_samples);
  143. float num_samples_inv = num_samples_adjust / num_samples;
  144. for (int j = branched_state->next_sample; j < num_samples; j++) {
  145. if (reset_path_state) {
  146. *ps = branched_state->path_state;
  147. }
  148. ps->rng_hash = cmj_hash(branched_state->path_state.rng_hash, i);
  149. ccl_global float3 *tp = &kernel_split_state.throughput[ray_index];
  150. *tp = throughput;
  151. ccl_global Ray *bsdf_ray = &kernel_split_state.ray[ray_index];
  152. if (!kernel_branched_path_surface_bounce(
  153. kg, sd, sc, j, num_samples, tp, ps, &L->state, bsdf_ray, sum_sample_weight)) {
  154. continue;
  155. }
  156. ps->rng_hash = branched_state->path_state.rng_hash;
  157. /* update state for next iteration */
  158. branched_state->next_closure = i;
  159. branched_state->next_sample = j + 1;
  160. /* start the indirect path */
  161. *tp *= num_samples_inv;
  162. if (kernel_split_branched_indirect_start_shared(kg, ray_index)) {
  163. continue;
  164. }
  165. return true;
  166. }
  167. branched_state->next_sample = 0;
  168. }
  169. branched_state->next_closure = sd->num_closure;
  170. if (wait_for_shared) {
  171. branched_state->waiting_on_shared_samples = (branched_state->shared_sample_count > 0);
  172. if (branched_state->waiting_on_shared_samples) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. #endif /* __BRANCHED_PATH__ */
  179. CCL_NAMESPACE_END