bvh_traversal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Adapted from code Copyright 2009-2010 NVIDIA Corporation,
  3. * and code copyright 2009-2012 Intel Corporation
  4. *
  5. * Modifications Copyright 2011-2013, Blender Foundation.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifdef __QBVH__
  20. # include "kernel/bvh/qbvh_traversal.h"
  21. #endif
  22. #ifdef __KERNEL_AVX2__
  23. # include "kernel/bvh/obvh_traversal.h"
  24. #endif
  25. #if BVH_FEATURE(BVH_HAIR)
  26. # define NODE_INTERSECT bvh_node_intersect
  27. #else
  28. # define NODE_INTERSECT bvh_aligned_node_intersect
  29. #endif
  30. /* This is a template BVH traversal function, where various features can be
  31. * enabled/disabled. This way we can compile optimized versions for each case
  32. * without new features slowing things down.
  33. *
  34. * BVH_INSTANCING: object instancing
  35. * BVH_HAIR: hair curve rendering
  36. * BVH_MOTION: motion blur rendering
  37. */
  38. ccl_device_noinline bool BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
  39. const Ray *ray,
  40. Intersection *isect,
  41. const uint visibility)
  42. {
  43. /* todo:
  44. * - test if pushing distance on the stack helps (for non shadow rays)
  45. * - separate version for shadow rays
  46. * - likely and unlikely for if() statements
  47. * - test restrict attribute for pointers
  48. */
  49. /* traversal stack in CUDA thread-local memory */
  50. int traversal_stack[BVH_STACK_SIZE];
  51. traversal_stack[0] = ENTRYPOINT_SENTINEL;
  52. /* traversal variables in registers */
  53. int stack_ptr = 0;
  54. int node_addr = kernel_data.bvh.root;
  55. /* ray parameters in registers */
  56. float3 P = ray->P;
  57. float3 dir = bvh_clamp_direction(ray->D);
  58. float3 idir = bvh_inverse_direction(dir);
  59. int object = OBJECT_NONE;
  60. #if BVH_FEATURE(BVH_MOTION)
  61. Transform ob_itfm;
  62. #endif
  63. isect->t = ray->t;
  64. isect->u = 0.0f;
  65. isect->v = 0.0f;
  66. isect->prim = PRIM_NONE;
  67. isect->object = OBJECT_NONE;
  68. BVH_DEBUG_INIT();
  69. #if defined(__KERNEL_SSE2__)
  70. const shuffle_swap_t shuf_identity = shuffle_swap_identity();
  71. const shuffle_swap_t shuf_swap = shuffle_swap_swap();
  72. const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
  73. ssef Psplat[3], idirsplat[3];
  74. # if BVH_FEATURE(BVH_HAIR)
  75. ssef tnear(0.0f), tfar(isect->t);
  76. # endif
  77. shuffle_swap_t shufflexyz[3];
  78. Psplat[0] = ssef(P.x);
  79. Psplat[1] = ssef(P.y);
  80. Psplat[2] = ssef(P.z);
  81. ssef tsplat(0.0f, 0.0f, -isect->t, -isect->t);
  82. gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
  83. #endif
  84. /* traversal loop */
  85. do {
  86. do {
  87. /* traverse internal nodes */
  88. while (node_addr >= 0 && node_addr != ENTRYPOINT_SENTINEL) {
  89. int node_addr_child1, traverse_mask;
  90. float dist[2];
  91. float4 cnodes = kernel_tex_fetch(__bvh_nodes, node_addr + 0);
  92. #if !defined(__KERNEL_SSE2__)
  93. {
  94. traverse_mask = NODE_INTERSECT(kg,
  95. P,
  96. # if BVH_FEATURE(BVH_HAIR)
  97. dir,
  98. # endif
  99. idir,
  100. isect->t,
  101. node_addr,
  102. visibility,
  103. dist);
  104. }
  105. #else // __KERNEL_SSE2__
  106. {
  107. traverse_mask = NODE_INTERSECT(kg,
  108. P,
  109. dir,
  110. # if BVH_FEATURE(BVH_HAIR)
  111. tnear,
  112. tfar,
  113. # endif
  114. tsplat,
  115. Psplat,
  116. idirsplat,
  117. shufflexyz,
  118. node_addr,
  119. visibility,
  120. dist);
  121. }
  122. #endif // __KERNEL_SSE2__
  123. node_addr = __float_as_int(cnodes.z);
  124. node_addr_child1 = __float_as_int(cnodes.w);
  125. if (traverse_mask == 3) {
  126. /* Both children were intersected, push the farther one. */
  127. bool is_closest_child1 = (dist[1] < dist[0]);
  128. if (is_closest_child1) {
  129. int tmp = node_addr;
  130. node_addr = node_addr_child1;
  131. node_addr_child1 = tmp;
  132. }
  133. ++stack_ptr;
  134. kernel_assert(stack_ptr < BVH_STACK_SIZE);
  135. traversal_stack[stack_ptr] = node_addr_child1;
  136. }
  137. else {
  138. /* One child was intersected. */
  139. if (traverse_mask == 2) {
  140. node_addr = node_addr_child1;
  141. }
  142. else if (traverse_mask == 0) {
  143. /* Neither child was intersected. */
  144. node_addr = traversal_stack[stack_ptr];
  145. --stack_ptr;
  146. }
  147. }
  148. BVH_DEBUG_NEXT_NODE();
  149. }
  150. /* if node is leaf, fetch triangle list */
  151. if (node_addr < 0) {
  152. float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-node_addr - 1));
  153. int prim_addr = __float_as_int(leaf.x);
  154. #if BVH_FEATURE(BVH_INSTANCING)
  155. if (prim_addr >= 0) {
  156. #endif
  157. const int prim_addr2 = __float_as_int(leaf.y);
  158. const uint type = __float_as_int(leaf.w);
  159. /* pop */
  160. node_addr = traversal_stack[stack_ptr];
  161. --stack_ptr;
  162. /* primitive intersection */
  163. switch (type & PRIMITIVE_ALL) {
  164. case PRIMITIVE_TRIANGLE: {
  165. for (; prim_addr < prim_addr2; prim_addr++) {
  166. BVH_DEBUG_NEXT_INTERSECTION();
  167. kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == type);
  168. if (triangle_intersect(kg, isect, P, dir, visibility, object, prim_addr)) {
  169. /* shadow ray early termination */
  170. #if defined(__KERNEL_SSE2__)
  171. if (visibility & PATH_RAY_SHADOW_OPAQUE)
  172. return true;
  173. tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
  174. # if BVH_FEATURE(BVH_HAIR)
  175. tfar = ssef(isect->t);
  176. # endif
  177. #else
  178. if (visibility & PATH_RAY_SHADOW_OPAQUE)
  179. return true;
  180. #endif
  181. }
  182. }
  183. break;
  184. }
  185. #if BVH_FEATURE(BVH_MOTION)
  186. case PRIMITIVE_MOTION_TRIANGLE: {
  187. for (; prim_addr < prim_addr2; prim_addr++) {
  188. BVH_DEBUG_NEXT_INTERSECTION();
  189. kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == type);
  190. if (motion_triangle_intersect(
  191. kg, isect, P, dir, ray->time, visibility, object, prim_addr)) {
  192. /* shadow ray early termination */
  193. # if defined(__KERNEL_SSE2__)
  194. if (visibility & PATH_RAY_SHADOW_OPAQUE)
  195. return true;
  196. tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
  197. # if BVH_FEATURE(BVH_HAIR)
  198. tfar = ssef(isect->t);
  199. # endif
  200. # else
  201. if (visibility & PATH_RAY_SHADOW_OPAQUE)
  202. return true;
  203. # endif
  204. }
  205. }
  206. break;
  207. }
  208. #endif /* BVH_FEATURE(BVH_MOTION) */
  209. #if BVH_FEATURE(BVH_HAIR)
  210. case PRIMITIVE_CURVE:
  211. case PRIMITIVE_MOTION_CURVE: {
  212. for (; prim_addr < prim_addr2; prim_addr++) {
  213. BVH_DEBUG_NEXT_INTERSECTION();
  214. const uint curve_type = kernel_tex_fetch(__prim_type, prim_addr);
  215. kernel_assert((curve_type & PRIMITIVE_ALL) == (type & PRIMITIVE_ALL));
  216. bool hit;
  217. if (kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE) {
  218. hit = cardinal_curve_intersect(
  219. kg, isect, P, dir, visibility, object, prim_addr, ray->time, curve_type);
  220. }
  221. else {
  222. hit = curve_intersect(
  223. kg, isect, P, dir, visibility, object, prim_addr, ray->time, curve_type);
  224. }
  225. if (hit) {
  226. /* shadow ray early termination */
  227. # if defined(__KERNEL_SSE2__)
  228. if (visibility & PATH_RAY_SHADOW_OPAQUE)
  229. return true;
  230. tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
  231. # if BVH_FEATURE(BVH_HAIR)
  232. tfar = ssef(isect->t);
  233. # endif
  234. # else
  235. if (visibility & PATH_RAY_SHADOW_OPAQUE)
  236. return true;
  237. # endif
  238. }
  239. }
  240. break;
  241. }
  242. #endif /* BVH_FEATURE(BVH_HAIR) */
  243. }
  244. }
  245. #if BVH_FEATURE(BVH_INSTANCING)
  246. else {
  247. /* instance push */
  248. object = kernel_tex_fetch(__prim_object, -prim_addr - 1);
  249. # if BVH_FEATURE(BVH_MOTION)
  250. isect->t = bvh_instance_motion_push(
  251. kg, object, ray, &P, &dir, &idir, isect->t, &ob_itfm);
  252. # else
  253. isect->t = bvh_instance_push(kg, object, ray, &P, &dir, &idir, isect->t);
  254. # endif
  255. # if defined(__KERNEL_SSE2__)
  256. Psplat[0] = ssef(P.x);
  257. Psplat[1] = ssef(P.y);
  258. Psplat[2] = ssef(P.z);
  259. tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
  260. # if BVH_FEATURE(BVH_HAIR)
  261. tfar = ssef(isect->t);
  262. # endif
  263. gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
  264. # endif
  265. ++stack_ptr;
  266. kernel_assert(stack_ptr < BVH_STACK_SIZE);
  267. traversal_stack[stack_ptr] = ENTRYPOINT_SENTINEL;
  268. node_addr = kernel_tex_fetch(__object_node, object);
  269. BVH_DEBUG_NEXT_INSTANCE();
  270. }
  271. }
  272. #endif /* FEATURE(BVH_INSTANCING) */
  273. } while (node_addr != ENTRYPOINT_SENTINEL);
  274. #if BVH_FEATURE(BVH_INSTANCING)
  275. if (stack_ptr >= 0) {
  276. kernel_assert(object != OBJECT_NONE);
  277. /* instance pop */
  278. # if BVH_FEATURE(BVH_MOTION)
  279. isect->t = bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, isect->t, &ob_itfm);
  280. # else
  281. isect->t = bvh_instance_pop(kg, object, ray, &P, &dir, &idir, isect->t);
  282. # endif
  283. # if defined(__KERNEL_SSE2__)
  284. Psplat[0] = ssef(P.x);
  285. Psplat[1] = ssef(P.y);
  286. Psplat[2] = ssef(P.z);
  287. tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
  288. # if BVH_FEATURE(BVH_HAIR)
  289. tfar = ssef(isect->t);
  290. # endif
  291. gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
  292. # endif
  293. object = OBJECT_NONE;
  294. node_addr = traversal_stack[stack_ptr];
  295. --stack_ptr;
  296. }
  297. #endif /* FEATURE(BVH_INSTANCING) */
  298. } while (node_addr != ENTRYPOINT_SENTINEL);
  299. return (isect->prim != PRIM_NONE);
  300. }
  301. ccl_device_inline bool BVH_FUNCTION_NAME(KernelGlobals *kg,
  302. const Ray *ray,
  303. Intersection *isect,
  304. const uint visibility)
  305. {
  306. switch (kernel_data.bvh.bvh_layout) {
  307. #ifdef __KERNEL_AVX2__
  308. case BVH_LAYOUT_BVH8:
  309. return BVH_FUNCTION_FULL_NAME(OBVH)(kg, ray, isect, visibility);
  310. #endif
  311. #ifdef __QBVH__
  312. case BVH_LAYOUT_BVH4:
  313. return BVH_FUNCTION_FULL_NAME(QBVH)(kg, ray, isect, visibility);
  314. #endif /* __QBVH__ */
  315. case BVH_LAYOUT_BVH2:
  316. return BVH_FUNCTION_FULL_NAME(BVH)(kg, ray, isect, visibility);
  317. }
  318. kernel_assert(!"Should not happen");
  319. return false;
  320. }
  321. #undef BVH_FUNCTION_NAME
  322. #undef BVH_FUNCTION_FEATURES
  323. #undef NODE_INTERSECT