bvh_volume.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Adapted from code Copyright 2009-2010 NVIDIA Corporation,
  3. * and code copyright 2009-2012 Intel Corporation
  4. *
  5. * Modifications Copyright 2011-2014, 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_volume.h"
  21. # ifdef __KERNEL_AVX2__
  22. # include "kernel/bvh/obvh_volume.h"
  23. # endif
  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 for volumes, where
  31. * various features can be enabled/disabled. This way we can compile optimized
  32. * versions for each case without new features slowing things down.
  33. *
  34. * BVH_INSTANCING: object instancing
  35. * BVH_MOTION: motion blur rendering
  36. */
  37. #ifndef __KERNEL_GPU__
  38. ccl_device
  39. #else
  40. ccl_device_inline
  41. #endif
  42. bool BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
  43. const Ray *ray,
  44. Intersection *isect,
  45. const uint visibility)
  46. {
  47. /* todo:
  48. * - test if pushing distance on the stack helps (for non shadow rays)
  49. * - separate version for shadow rays
  50. * - likely and unlikely for if() statements
  51. * - test restrict attribute for pointers
  52. */
  53. /* traversal stack in CUDA thread-local memory */
  54. int traversal_stack[BVH_STACK_SIZE];
  55. traversal_stack[0] = ENTRYPOINT_SENTINEL;
  56. /* traversal variables in registers */
  57. int stack_ptr = 0;
  58. int node_addr = kernel_data.bvh.root;
  59. /* ray parameters in registers */
  60. float3 P = ray->P;
  61. float3 dir = bvh_clamp_direction(ray->D);
  62. float3 idir = bvh_inverse_direction(dir);
  63. int object = OBJECT_NONE;
  64. #if BVH_FEATURE(BVH_MOTION)
  65. Transform ob_itfm;
  66. #endif
  67. isect->t = ray->t;
  68. isect->u = 0.0f;
  69. isect->v = 0.0f;
  70. isect->prim = PRIM_NONE;
  71. isect->object = OBJECT_NONE;
  72. #if defined(__KERNEL_SSE2__)
  73. const shuffle_swap_t shuf_identity = shuffle_swap_identity();
  74. const shuffle_swap_t shuf_swap = shuffle_swap_swap();
  75. const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
  76. ssef Psplat[3], idirsplat[3];
  77. # if BVH_FEATURE(BVH_HAIR)
  78. ssef tnear(0.0f), tfar(isect->t);
  79. # endif
  80. shuffle_swap_t shufflexyz[3];
  81. Psplat[0] = ssef(P.x);
  82. Psplat[1] = ssef(P.y);
  83. Psplat[2] = ssef(P.z);
  84. ssef tsplat(0.0f, 0.0f, -isect->t, -isect->t);
  85. gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
  86. #endif
  87. /* traversal loop */
  88. do {
  89. do {
  90. /* traverse internal nodes */
  91. while (node_addr >= 0 && node_addr != ENTRYPOINT_SENTINEL) {
  92. int node_addr_child1, traverse_mask;
  93. float dist[2];
  94. float4 cnodes = kernel_tex_fetch(__bvh_nodes, node_addr + 0);
  95. #if !defined(__KERNEL_SSE2__)
  96. traverse_mask = NODE_INTERSECT(kg,
  97. P,
  98. # if BVH_FEATURE(BVH_HAIR)
  99. dir,
  100. # endif
  101. idir,
  102. isect->t,
  103. node_addr,
  104. visibility,
  105. dist);
  106. #else // __KERNEL_SSE2__
  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. #endif // __KERNEL_SSE2__
  122. node_addr = __float_as_int(cnodes.z);
  123. node_addr_child1 = __float_as_int(cnodes.w);
  124. if (traverse_mask == 3) {
  125. /* Both children were intersected, push the farther one. */
  126. bool is_closest_child1 = (dist[1] < dist[0]);
  127. if (is_closest_child1) {
  128. int tmp = node_addr;
  129. node_addr = node_addr_child1;
  130. node_addr_child1 = tmp;
  131. }
  132. ++stack_ptr;
  133. kernel_assert(stack_ptr < BVH_STACK_SIZE);
  134. traversal_stack[stack_ptr] = node_addr_child1;
  135. }
  136. else {
  137. /* One child was intersected. */
  138. if (traverse_mask == 2) {
  139. node_addr = node_addr_child1;
  140. }
  141. else if (traverse_mask == 0) {
  142. /* Neither child was intersected. */
  143. node_addr = traversal_stack[stack_ptr];
  144. --stack_ptr;
  145. }
  146. }
  147. }
  148. /* if node is leaf, fetch triangle list */
  149. if (node_addr < 0) {
  150. float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-node_addr - 1));
  151. int prim_addr = __float_as_int(leaf.x);
  152. #if BVH_FEATURE(BVH_INSTANCING)
  153. if (prim_addr >= 0) {
  154. #endif
  155. const int prim_addr2 = __float_as_int(leaf.y);
  156. const uint type = __float_as_int(leaf.w);
  157. /* pop */
  158. node_addr = traversal_stack[stack_ptr];
  159. --stack_ptr;
  160. /* primitive intersection */
  161. switch (type & PRIMITIVE_ALL) {
  162. case PRIMITIVE_TRIANGLE: {
  163. /* intersect ray against primitive */
  164. for (; prim_addr < prim_addr2; prim_addr++) {
  165. kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == type);
  166. /* only primitives from volume object */
  167. uint tri_object = (object == OBJECT_NONE) ?
  168. kernel_tex_fetch(__prim_object, prim_addr) :
  169. object;
  170. int object_flag = kernel_tex_fetch(__object_flag, tri_object);
  171. if ((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
  172. continue;
  173. }
  174. triangle_intersect(kg, isect, P, dir, visibility, object, prim_addr);
  175. }
  176. break;
  177. }
  178. #if BVH_FEATURE(BVH_MOTION)
  179. case PRIMITIVE_MOTION_TRIANGLE: {
  180. /* intersect ray against primitive */
  181. for (; prim_addr < prim_addr2; prim_addr++) {
  182. kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == type);
  183. /* only primitives from volume object */
  184. uint tri_object = (object == OBJECT_NONE) ?
  185. kernel_tex_fetch(__prim_object, prim_addr) :
  186. object;
  187. int object_flag = kernel_tex_fetch(__object_flag, tri_object);
  188. if ((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
  189. continue;
  190. }
  191. motion_triangle_intersect(
  192. kg, isect, P, dir, ray->time, visibility, object, prim_addr);
  193. }
  194. break;
  195. }
  196. #endif
  197. default: {
  198. break;
  199. }
  200. }
  201. }
  202. #if BVH_FEATURE(BVH_INSTANCING)
  203. else {
  204. /* instance push */
  205. object = kernel_tex_fetch(__prim_object, -prim_addr - 1);
  206. int object_flag = kernel_tex_fetch(__object_flag, object);
  207. if (object_flag & SD_OBJECT_HAS_VOLUME) {
  208. # if BVH_FEATURE(BVH_MOTION)
  209. isect->t = bvh_instance_motion_push(
  210. kg, object, ray, &P, &dir, &idir, isect->t, &ob_itfm);
  211. # else
  212. isect->t = bvh_instance_push(kg, object, ray, &P, &dir, &idir, isect->t);
  213. # endif
  214. # if defined(__KERNEL_SSE2__)
  215. Psplat[0] = ssef(P.x);
  216. Psplat[1] = ssef(P.y);
  217. Psplat[2] = ssef(P.z);
  218. tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
  219. # if BVH_FEATURE(BVH_HAIR)
  220. tfar = ssef(isect->t);
  221. # endif
  222. gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
  223. # endif
  224. ++stack_ptr;
  225. kernel_assert(stack_ptr < BVH_STACK_SIZE);
  226. traversal_stack[stack_ptr] = ENTRYPOINT_SENTINEL;
  227. node_addr = kernel_tex_fetch(__object_node, object);
  228. }
  229. else {
  230. /* pop */
  231. object = OBJECT_NONE;
  232. node_addr = traversal_stack[stack_ptr];
  233. --stack_ptr;
  234. }
  235. }
  236. }
  237. #endif /* FEATURE(BVH_INSTANCING) */
  238. } while (node_addr != ENTRYPOINT_SENTINEL);
  239. #if BVH_FEATURE(BVH_INSTANCING)
  240. if (stack_ptr >= 0) {
  241. kernel_assert(object != OBJECT_NONE);
  242. /* instance pop */
  243. # if BVH_FEATURE(BVH_MOTION)
  244. isect->t = bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, isect->t, &ob_itfm);
  245. # else
  246. isect->t = bvh_instance_pop(kg, object, ray, &P, &dir, &idir, isect->t);
  247. # endif
  248. # if defined(__KERNEL_SSE2__)
  249. Psplat[0] = ssef(P.x);
  250. Psplat[1] = ssef(P.y);
  251. Psplat[2] = ssef(P.z);
  252. tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
  253. # if BVH_FEATURE(BVH_HAIR)
  254. tfar = ssef(isect->t);
  255. # endif
  256. gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
  257. # endif
  258. object = OBJECT_NONE;
  259. node_addr = traversal_stack[stack_ptr];
  260. --stack_ptr;
  261. }
  262. #endif /* FEATURE(BVH_MOTION) */
  263. } while (node_addr != ENTRYPOINT_SENTINEL);
  264. return (isect->prim != PRIM_NONE);
  265. }
  266. ccl_device_inline bool BVH_FUNCTION_NAME(KernelGlobals *kg,
  267. const Ray *ray,
  268. Intersection *isect,
  269. const uint visibility)
  270. {
  271. switch (kernel_data.bvh.bvh_layout) {
  272. #ifdef __KERNEL_AVX2__
  273. case BVH_LAYOUT_BVH8:
  274. return BVH_FUNCTION_FULL_NAME(OBVH)(kg, ray, isect, visibility);
  275. #endif
  276. #ifdef __QBVH__
  277. case BVH_LAYOUT_BVH4:
  278. return BVH_FUNCTION_FULL_NAME(QBVH)(kg, ray, isect, visibility);
  279. #endif
  280. case BVH_LAYOUT_BVH2:
  281. return BVH_FUNCTION_FULL_NAME(BVH)(kg, ray, isect, visibility);
  282. }
  283. kernel_assert(!"Should not happen");
  284. return false;
  285. }
  286. #undef BVH_FUNCTION_NAME
  287. #undef BVH_FUNCTION_FEATURES
  288. #undef NODE_INTERSECT