node_intersector_frustum.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "node_intersector.h"
  5. namespace embree
  6. {
  7. namespace isa
  8. {
  9. //////////////////////////////////////////////////////////////////////////////////////
  10. // Frustum structure used in hybrid and stream traversal
  11. //////////////////////////////////////////////////////////////////////////////////////
  12. /*
  13. Optimized frustum test. We calculate t=(p-org)/dir in ray/box
  14. intersection. We assume the rays are split by octant, thus
  15. dir intervals are either positive or negative in each
  16. dimension.
  17. Case 1: dir.min >= 0 && dir.max >= 0:
  18. t_min = (p_min - org_max) / dir_max = (p_min - org_max)*rdir_min = p_min*rdir_min - org_max*rdir_min
  19. t_max = (p_max - org_min) / dir_min = (p_max - org_min)*rdir_max = p_max*rdir_max - org_min*rdir_max
  20. Case 2: dir.min < 0 && dir.max < 0:
  21. t_min = (p_max - org_min) / dir_min = (p_max - org_min)*rdir_max = p_max*rdir_max - org_min*rdir_max
  22. t_max = (p_min - org_max) / dir_max = (p_min - org_max)*rdir_min = p_min*rdir_min - org_max*rdir_min
  23. */
  24. template<bool robust>
  25. struct Frustum;
  26. /* Fast variant */
  27. template<>
  28. struct Frustum<false>
  29. {
  30. __forceinline Frustum() {}
  31. template<int K>
  32. __forceinline void init(const vbool<K>& valid, const Vec3vf<K>& org, const Vec3vf<K>& rdir, const vfloat<K>& ray_tnear, const vfloat<K>& ray_tfar, int N)
  33. {
  34. const Vec3fa reduced_min_org(reduce_min(select(valid, org.x, pos_inf)),
  35. reduce_min(select(valid, org.y, pos_inf)),
  36. reduce_min(select(valid, org.z, pos_inf)));
  37. const Vec3fa reduced_max_org(reduce_max(select(valid, org.x, neg_inf)),
  38. reduce_max(select(valid, org.y, neg_inf)),
  39. reduce_max(select(valid, org.z, neg_inf)));
  40. const Vec3fa reduced_min_rdir(reduce_min(select(valid, rdir.x, pos_inf)),
  41. reduce_min(select(valid, rdir.y, pos_inf)),
  42. reduce_min(select(valid, rdir.z, pos_inf)));
  43. const Vec3fa reduced_max_rdir(reduce_max(select(valid, rdir.x, neg_inf)),
  44. reduce_max(select(valid, rdir.y, neg_inf)),
  45. reduce_max(select(valid, rdir.z, neg_inf)));
  46. const float reduced_min_dist = reduce_min(select(valid, ray_tnear, vfloat<K>(pos_inf)));
  47. const float reduced_max_dist = reduce_max(select(valid, ray_tfar , vfloat<K>(neg_inf)));
  48. init(reduced_min_org, reduced_max_org, reduced_min_rdir, reduced_max_rdir, reduced_min_dist, reduced_max_dist, N);
  49. }
  50. __forceinline void init(const Vec3fa& reduced_min_org,
  51. const Vec3fa& reduced_max_org,
  52. const Vec3fa& reduced_min_rdir,
  53. const Vec3fa& reduced_max_rdir,
  54. float reduced_min_dist,
  55. float reduced_max_dist,
  56. int N)
  57. {
  58. const Vec3ba pos_rdir = ge_mask(reduced_min_rdir, Vec3fa(zero));
  59. min_rdir = select(pos_rdir, reduced_min_rdir, reduced_max_rdir);
  60. max_rdir = select(pos_rdir, reduced_max_rdir, reduced_min_rdir);
  61. min_org_rdir = min_rdir * select(pos_rdir, reduced_max_org, reduced_min_org);
  62. max_org_rdir = max_rdir * select(pos_rdir, reduced_min_org, reduced_max_org);
  63. min_dist = reduced_min_dist;
  64. max_dist = reduced_max_dist;
  65. nf = NearFarPrecalculations(min_rdir, N);
  66. }
  67. template<int K>
  68. __forceinline void updateMaxDist(const vfloat<K>& ray_tfar)
  69. {
  70. max_dist = reduce_max(ray_tfar);
  71. }
  72. NearFarPrecalculations nf;
  73. Vec3fa min_rdir;
  74. Vec3fa max_rdir;
  75. Vec3fa min_org_rdir;
  76. Vec3fa max_org_rdir;
  77. float min_dist;
  78. float max_dist;
  79. };
  80. typedef Frustum<false> FrustumFast;
  81. /* Robust variant */
  82. template<>
  83. struct Frustum<true>
  84. {
  85. __forceinline Frustum() {}
  86. template<int K>
  87. __forceinline void init(const vbool<K>& valid, const Vec3vf<K>& org, const Vec3vf<K>& rdir, const vfloat<K>& ray_tnear, const vfloat<K>& ray_tfar, int N)
  88. {
  89. const Vec3fa reduced_min_org(reduce_min(select(valid, org.x, pos_inf)),
  90. reduce_min(select(valid, org.y, pos_inf)),
  91. reduce_min(select(valid, org.z, pos_inf)));
  92. const Vec3fa reduced_max_org(reduce_max(select(valid, org.x, neg_inf)),
  93. reduce_max(select(valid, org.y, neg_inf)),
  94. reduce_max(select(valid, org.z, neg_inf)));
  95. const Vec3fa reduced_min_rdir(reduce_min(select(valid, rdir.x, pos_inf)),
  96. reduce_min(select(valid, rdir.y, pos_inf)),
  97. reduce_min(select(valid, rdir.z, pos_inf)));
  98. const Vec3fa reduced_max_rdir(reduce_max(select(valid, rdir.x, neg_inf)),
  99. reduce_max(select(valid, rdir.y, neg_inf)),
  100. reduce_max(select(valid, rdir.z, neg_inf)));
  101. const float reduced_min_dist = reduce_min(select(valid, ray_tnear, vfloat<K>(pos_inf)));
  102. const float reduced_max_dist = reduce_max(select(valid, ray_tfar , vfloat<K>(neg_inf)));
  103. init(reduced_min_org, reduced_max_org, reduced_min_rdir, reduced_max_rdir, reduced_min_dist, reduced_max_dist, N);
  104. }
  105. __forceinline void init(const Vec3fa& reduced_min_org,
  106. const Vec3fa& reduced_max_org,
  107. const Vec3fa& reduced_min_rdir,
  108. const Vec3fa& reduced_max_rdir,
  109. float reduced_min_dist,
  110. float reduced_max_dist,
  111. int N)
  112. {
  113. const Vec3ba pos_rdir = ge_mask(reduced_min_rdir, Vec3fa(zero));
  114. min_rdir = select(pos_rdir, reduced_min_rdir, reduced_max_rdir);
  115. max_rdir = select(pos_rdir, reduced_max_rdir, reduced_min_rdir);
  116. min_org = select(pos_rdir, reduced_max_org, reduced_min_org);
  117. max_org = select(pos_rdir, reduced_min_org, reduced_max_org);
  118. min_dist = reduced_min_dist;
  119. max_dist = reduced_max_dist;
  120. nf = NearFarPrecalculations(min_rdir, N);
  121. }
  122. template<int K>
  123. __forceinline void updateMaxDist(const vfloat<K>& ray_tfar)
  124. {
  125. max_dist = reduce_max(ray_tfar);
  126. }
  127. NearFarPrecalculations nf;
  128. Vec3fa min_rdir;
  129. Vec3fa max_rdir;
  130. Vec3fa min_org;
  131. Vec3fa max_org;
  132. float min_dist;
  133. float max_dist;
  134. };
  135. typedef Frustum<true> FrustumRobust;
  136. //////////////////////////////////////////////////////////////////////////////////////
  137. // Fast AABBNode intersection
  138. //////////////////////////////////////////////////////////////////////////////////////
  139. template<int N>
  140. __forceinline size_t intersectNodeFrustum(const typename BVHN<N>::AABBNode* __restrict__ node,
  141. const FrustumFast& frustum, vfloat<N>& dist)
  142. {
  143. const vfloat<N> bminX = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.nearX);
  144. const vfloat<N> bminY = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.nearY);
  145. const vfloat<N> bminZ = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.nearZ);
  146. const vfloat<N> bmaxX = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.farX);
  147. const vfloat<N> bmaxY = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.farY);
  148. const vfloat<N> bmaxZ = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.farZ);
  149. const vfloat<N> fminX = msub(bminX, vfloat<N>(frustum.min_rdir.x), vfloat<N>(frustum.min_org_rdir.x));
  150. const vfloat<N> fminY = msub(bminY, vfloat<N>(frustum.min_rdir.y), vfloat<N>(frustum.min_org_rdir.y));
  151. const vfloat<N> fminZ = msub(bminZ, vfloat<N>(frustum.min_rdir.z), vfloat<N>(frustum.min_org_rdir.z));
  152. const vfloat<N> fmaxX = msub(bmaxX, vfloat<N>(frustum.max_rdir.x), vfloat<N>(frustum.max_org_rdir.x));
  153. const vfloat<N> fmaxY = msub(bmaxY, vfloat<N>(frustum.max_rdir.y), vfloat<N>(frustum.max_org_rdir.y));
  154. const vfloat<N> fmaxZ = msub(bmaxZ, vfloat<N>(frustum.max_rdir.z), vfloat<N>(frustum.max_org_rdir.z));
  155. const vfloat<N> fmin = maxi(fminX, fminY, fminZ, vfloat<N>(frustum.min_dist));
  156. dist = fmin;
  157. const vfloat<N> fmax = mini(fmaxX, fmaxY, fmaxZ, vfloat<N>(frustum.max_dist));
  158. const vbool<N> vmask_node_hit = fmin <= fmax;
  159. size_t m_node = movemask(vmask_node_hit) & (((size_t)1 << N)-1);
  160. return m_node;
  161. }
  162. //////////////////////////////////////////////////////////////////////////////////////
  163. // Robust AABBNode intersection
  164. //////////////////////////////////////////////////////////////////////////////////////
  165. template<int N>
  166. __forceinline size_t intersectNodeFrustum(const typename BVHN<N>::AABBNode* __restrict__ node,
  167. const FrustumRobust& frustum, vfloat<N>& dist)
  168. {
  169. const vfloat<N> bminX = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.nearX);
  170. const vfloat<N> bminY = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.nearY);
  171. const vfloat<N> bminZ = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.nearZ);
  172. const vfloat<N> bmaxX = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.farX);
  173. const vfloat<N> bmaxY = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.farY);
  174. const vfloat<N> bmaxZ = *(const vfloat<N>*)((const char*)&node->lower_x + frustum.nf.farZ);
  175. const vfloat<N> fminX = (bminX - vfloat<N>(frustum.min_org.x)) * vfloat<N>(frustum.min_rdir.x);
  176. const vfloat<N> fminY = (bminY - vfloat<N>(frustum.min_org.y)) * vfloat<N>(frustum.min_rdir.y);
  177. const vfloat<N> fminZ = (bminZ - vfloat<N>(frustum.min_org.z)) * vfloat<N>(frustum.min_rdir.z);
  178. const vfloat<N> fmaxX = (bmaxX - vfloat<N>(frustum.max_org.x)) * vfloat<N>(frustum.max_rdir.x);
  179. const vfloat<N> fmaxY = (bmaxY - vfloat<N>(frustum.max_org.y)) * vfloat<N>(frustum.max_rdir.y);
  180. const vfloat<N> fmaxZ = (bmaxZ - vfloat<N>(frustum.max_org.z)) * vfloat<N>(frustum.max_rdir.z);
  181. const float round_down = 1.0f-2.0f*float(ulp); // FIXME: use per instruction rounding for AVX512
  182. const float round_up = 1.0f+2.0f*float(ulp);
  183. const vfloat<N> fmin = max(fminX, fminY, fminZ, vfloat<N>(frustum.min_dist));
  184. dist = fmin;
  185. const vfloat<N> fmax = min(fmaxX, fmaxY, fmaxZ, vfloat<N>(frustum.max_dist));
  186. const vbool<N> vmask_node_hit = (round_down*fmin <= round_up*fmax);
  187. size_t m_node = movemask(vmask_node_hit) & (((size_t)1 << N)-1);
  188. return m_node;
  189. }
  190. }
  191. }