kernel_path_state.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright 2011-2013 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. ccl_device_inline void path_state_init(KernelGlobals *kg,
  18. ShaderData *stack_sd,
  19. ccl_addr_space PathState *state,
  20. uint rng_hash,
  21. int sample,
  22. ccl_addr_space Ray *ray)
  23. {
  24. state->flag = PATH_RAY_CAMERA | PATH_RAY_MIS_SKIP | PATH_RAY_TRANSPARENT_BACKGROUND;
  25. state->rng_hash = rng_hash;
  26. state->rng_offset = PRNG_BASE_NUM;
  27. state->sample = sample;
  28. state->num_samples = kernel_data.integrator.aa_samples;
  29. state->branch_factor = 1.0f;
  30. state->bounce = 0;
  31. state->diffuse_bounce = 0;
  32. state->glossy_bounce = 0;
  33. state->transmission_bounce = 0;
  34. state->transparent_bounce = 0;
  35. #ifdef __DENOISING_FEATURES__
  36. if (kernel_data.film.pass_denoising_data) {
  37. state->flag |= PATH_RAY_STORE_SHADOW_INFO;
  38. state->denoising_feature_weight = 1.0f;
  39. }
  40. else {
  41. state->denoising_feature_weight = 0.0f;
  42. }
  43. #endif /* __DENOISING_FEATURES__ */
  44. state->min_ray_pdf = FLT_MAX;
  45. state->ray_pdf = 0.0f;
  46. #ifdef __LAMP_MIS__
  47. state->ray_t = 0.0f;
  48. #endif
  49. #ifdef __VOLUME__
  50. state->volume_bounce = 0;
  51. state->volume_bounds_bounce = 0;
  52. if (kernel_data.integrator.use_volumes) {
  53. /* Initialize volume stack with volume we are inside of. */
  54. kernel_volume_stack_init(kg, stack_sd, state, ray, state->volume_stack);
  55. }
  56. else {
  57. state->volume_stack[0].shader = SHADER_NONE;
  58. }
  59. #endif
  60. }
  61. ccl_device_inline void path_state_next(KernelGlobals *kg,
  62. ccl_addr_space PathState *state,
  63. int label)
  64. {
  65. /* ray through transparent keeps same flags from previous ray and is
  66. * not counted as a regular bounce, transparent has separate max */
  67. if (label & LABEL_TRANSPARENT) {
  68. state->flag |= PATH_RAY_TRANSPARENT;
  69. state->transparent_bounce++;
  70. if (state->transparent_bounce >= kernel_data.integrator.transparent_max_bounce) {
  71. state->flag |= PATH_RAY_TERMINATE_IMMEDIATE;
  72. }
  73. if (!kernel_data.integrator.transparent_shadows)
  74. state->flag |= PATH_RAY_MIS_SKIP;
  75. /* random number generator next bounce */
  76. state->rng_offset += PRNG_BOUNCE_NUM;
  77. return;
  78. }
  79. state->bounce++;
  80. if (state->bounce >= kernel_data.integrator.max_bounce) {
  81. state->flag |= PATH_RAY_TERMINATE_AFTER_TRANSPARENT;
  82. }
  83. state->flag &= ~(PATH_RAY_ALL_VISIBILITY | PATH_RAY_MIS_SKIP);
  84. #ifdef __VOLUME__
  85. if (label & LABEL_VOLUME_SCATTER) {
  86. /* volume scatter */
  87. state->flag |= PATH_RAY_VOLUME_SCATTER;
  88. state->flag &= ~PATH_RAY_TRANSPARENT_BACKGROUND;
  89. state->volume_bounce++;
  90. if (state->volume_bounce >= kernel_data.integrator.max_volume_bounce) {
  91. state->flag |= PATH_RAY_TERMINATE_AFTER_TRANSPARENT;
  92. }
  93. }
  94. else
  95. #endif
  96. {
  97. /* surface reflection/transmission */
  98. if (label & LABEL_REFLECT) {
  99. state->flag |= PATH_RAY_REFLECT;
  100. state->flag &= ~PATH_RAY_TRANSPARENT_BACKGROUND;
  101. if (label & LABEL_DIFFUSE) {
  102. state->diffuse_bounce++;
  103. if (state->diffuse_bounce >= kernel_data.integrator.max_diffuse_bounce) {
  104. state->flag |= PATH_RAY_TERMINATE_AFTER_TRANSPARENT;
  105. }
  106. }
  107. else {
  108. state->glossy_bounce++;
  109. if (state->glossy_bounce >= kernel_data.integrator.max_glossy_bounce) {
  110. state->flag |= PATH_RAY_TERMINATE_AFTER_TRANSPARENT;
  111. }
  112. }
  113. }
  114. else {
  115. kernel_assert(label & LABEL_TRANSMIT);
  116. state->flag |= PATH_RAY_TRANSMIT;
  117. if (!(label & LABEL_TRANSMIT_TRANSPARENT)) {
  118. state->flag &= ~PATH_RAY_TRANSPARENT_BACKGROUND;
  119. }
  120. state->transmission_bounce++;
  121. if (state->transmission_bounce >= kernel_data.integrator.max_transmission_bounce) {
  122. state->flag |= PATH_RAY_TERMINATE_AFTER_TRANSPARENT;
  123. }
  124. }
  125. /* diffuse/glossy/singular */
  126. if (label & LABEL_DIFFUSE) {
  127. state->flag |= PATH_RAY_DIFFUSE | PATH_RAY_DIFFUSE_ANCESTOR;
  128. }
  129. else if (label & LABEL_GLOSSY) {
  130. state->flag |= PATH_RAY_GLOSSY;
  131. }
  132. else {
  133. kernel_assert(label & LABEL_SINGULAR);
  134. state->flag |= PATH_RAY_GLOSSY | PATH_RAY_SINGULAR | PATH_RAY_MIS_SKIP;
  135. }
  136. }
  137. /* random number generator next bounce */
  138. state->rng_offset += PRNG_BOUNCE_NUM;
  139. #ifdef __DENOISING_FEATURES__
  140. if ((state->denoising_feature_weight == 0.0f) && !(state->flag & PATH_RAY_SHADOW_CATCHER)) {
  141. state->flag &= ~PATH_RAY_STORE_SHADOW_INFO;
  142. }
  143. #endif
  144. }
  145. #ifdef __VOLUME__
  146. ccl_device_inline bool path_state_volume_next(KernelGlobals *kg, ccl_addr_space PathState *state)
  147. {
  148. /* For volume bounding meshes we pass through without counting transparent
  149. * bounces, only sanity check in case self intersection gets us stuck. */
  150. state->volume_bounds_bounce++;
  151. if (state->volume_bounds_bounce > VOLUME_BOUNDS_MAX) {
  152. return false;
  153. }
  154. /* Random number generator next bounce. */
  155. if (state->volume_bounds_bounce > 1) {
  156. state->rng_offset += PRNG_BOUNCE_NUM;
  157. }
  158. return true;
  159. }
  160. #endif
  161. ccl_device_inline uint path_state_ray_visibility(KernelGlobals *kg,
  162. ccl_addr_space PathState *state)
  163. {
  164. uint flag = state->flag & PATH_RAY_ALL_VISIBILITY;
  165. /* for visibility, diffuse/glossy are for reflection only */
  166. if (flag & PATH_RAY_TRANSMIT)
  167. flag &= ~(PATH_RAY_DIFFUSE | PATH_RAY_GLOSSY);
  168. /* todo: this is not supported as its own ray visibility yet */
  169. if (state->flag & PATH_RAY_VOLUME_SCATTER)
  170. flag |= PATH_RAY_DIFFUSE;
  171. return flag;
  172. }
  173. ccl_device_inline float path_state_continuation_probability(KernelGlobals *kg,
  174. ccl_addr_space PathState *state,
  175. const float3 throughput)
  176. {
  177. if (state->flag & PATH_RAY_TERMINATE_IMMEDIATE) {
  178. /* Ray is to be terminated immediately. */
  179. return 0.0f;
  180. }
  181. else if (state->flag & PATH_RAY_TRANSPARENT) {
  182. /* Do at least specified number of bounces without RR. */
  183. if (state->transparent_bounce <= kernel_data.integrator.transparent_min_bounce) {
  184. return 1.0f;
  185. }
  186. #ifdef __SHADOW_TRICKS__
  187. /* Exception for shadow catcher not working correctly with RR. */
  188. else if ((state->flag & PATH_RAY_SHADOW_CATCHER) && (state->transparent_bounce <= 8)) {
  189. return 1.0f;
  190. }
  191. #endif
  192. }
  193. else {
  194. /* Do at least specified number of bounces without RR. */
  195. if (state->bounce <= kernel_data.integrator.min_bounce) {
  196. return 1.0f;
  197. }
  198. #ifdef __SHADOW_TRICKS__
  199. /* Exception for shadow catcher not working correctly with RR. */
  200. else if ((state->flag & PATH_RAY_SHADOW_CATCHER) && (state->bounce <= 3)) {
  201. return 1.0f;
  202. }
  203. #endif
  204. }
  205. /* Probabilistic termination: use sqrt() to roughly match typical view
  206. * transform and do path termination a bit later on average. */
  207. return min(sqrtf(max3(fabs(throughput)) * state->branch_factor), 1.0f);
  208. }
  209. /* TODO(DingTo): Find more meaningful name for this */
  210. ccl_device_inline void path_state_modify_bounce(ccl_addr_space PathState *state, bool increase)
  211. {
  212. /* Modify bounce temporarily for shader eval */
  213. if (increase)
  214. state->bounce += 1;
  215. else
  216. state->bounce -= 1;
  217. }
  218. ccl_device_inline bool path_state_ao_bounce(KernelGlobals *kg, ccl_addr_space PathState *state)
  219. {
  220. if (state->bounce <= kernel_data.integrator.ao_bounces) {
  221. return false;
  222. }
  223. int bounce = state->bounce - state->transmission_bounce - (state->glossy_bounce > 0);
  224. return (bounce > kernel_data.integrator.ao_bounces);
  225. }
  226. ccl_device_inline void path_state_branch(ccl_addr_space PathState *state,
  227. int branch,
  228. int num_branches)
  229. {
  230. if (num_branches > 1) {
  231. /* Path is splitting into a branch, adjust so that each branch
  232. * still gets a unique sample from the same sequence. */
  233. state->sample = state->sample * num_branches + branch;
  234. state->num_samples = state->num_samples * num_branches;
  235. state->branch_factor *= num_branches;
  236. }
  237. }
  238. CCL_NAMESPACE_END