object_intersector.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  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. #pragma once
  17. #include "object.h"
  18. #include "../common/ray.h"
  19. namespace embree
  20. {
  21. namespace isa
  22. {
  23. template<bool mblur>
  24. struct ObjectIntersector1
  25. {
  26. typedef Object Primitive;
  27. static const bool validIntersectorK = false;
  28. struct PrecalculationsBase {
  29. __forceinline PrecalculationsBase() {}
  30. __forceinline PrecalculationsBase (const Ray& ray, const void *ptr) {}
  31. };
  32. typedef typename std::conditional<mblur,
  33. Intersector1PrecalculationsMB<PrecalculationsBase>,
  34. Intersector1Precalculations<PrecalculationsBase>>::type Precalculations;
  35. static __forceinline void intersect(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& prim)
  36. {
  37. AVX_ZERO_UPPER();
  38. AccelSet* accel = (AccelSet*) context->scene->get(prim.geomID);
  39. /* perform ray mask test */
  40. #if defined(EMBREE_RAY_MASK)
  41. if ((ray.mask & accel->mask) == 0)
  42. return;
  43. #endif
  44. accel->intersect(ray,prim.primID,context);
  45. }
  46. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& prim)
  47. {
  48. AVX_ZERO_UPPER();
  49. AccelSet* accel = (AccelSet*) context->scene->get(prim.geomID);
  50. /* perform ray mask test */
  51. #if defined(EMBREE_RAY_MASK)
  52. if ((ray.mask & accel->mask) == 0)
  53. return false;
  54. #endif
  55. accel->occluded(ray,prim.primID,context);
  56. return ray.geomID == 0;
  57. }
  58. //template<typename Context>
  59. static __forceinline size_t intersect(Precalculations* pre, size_t valid_in, Ray** rays, IntersectContext* context, size_t ty, const Primitive* prims, size_t num /*, size_t& lazy_node */)
  60. {
  61. AVX_ZERO_UPPER();
  62. /* intersect all primitives */
  63. for (size_t i=0; i<num; i++)
  64. {
  65. const Primitive& prim = prims[i];
  66. AccelSet* accel = (AccelSet*) context->scene->get(prim.geomID);
  67. size_t N = 0, valid = valid_in;
  68. Ray* rays_filtered[64];
  69. while (unlikely(valid))
  70. {
  71. const size_t i = __bscf(valid);
  72. Ray* ray = rays[i];
  73. /* perform ray mask test */
  74. #if defined(EMBREE_RAY_MASK)
  75. if ((ray->mask & accel->mask) == 0)
  76. continue;
  77. #endif
  78. rays_filtered[N++] = ray;
  79. }
  80. if (unlikely(N == 0)) continue;
  81. /* call user stream intersection function */
  82. accel->intersect1M(rays_filtered,N,prim.primID,context);
  83. }
  84. /* /\* update all contexts *\/ */
  85. /* size_t valid = valid_in; */
  86. /* while (unlikely(valid)) { */
  87. /* const size_t i = __bscf(valid); */
  88. /* ctx[i].update(rays[i]); */
  89. /* } */
  90. return valid_in;
  91. }
  92. static __forceinline size_t occluded(Precalculations* pre, size_t valid_in, Ray** rays, IntersectContext* context, size_t ty, const Primitive* prims, size_t num /*, size_t& lazy_node */)
  93. {
  94. AVX_ZERO_UPPER();
  95. size_t hit = 0;
  96. /* intersect all primitives */
  97. for (size_t i=0; i<num; i++)
  98. {
  99. const Primitive& prim = prims[i];
  100. AccelSet* accel = (AccelSet*) context->scene->get(prim.geomID);
  101. size_t N = 0, valid = valid_in;
  102. Ray* rays_filtered[64];
  103. size_t index_filtered[64];
  104. while (unlikely(valid))
  105. {
  106. const size_t i = __bscf(valid);
  107. Ray* ray = rays[i];
  108. /* perform ray mask test */
  109. #if defined(EMBREE_RAY_MASK)
  110. if ((ray->mask & accel->mask) == 0)
  111. continue;
  112. #endif
  113. rays_filtered[N] = ray;
  114. index_filtered[N] = i;
  115. N++;
  116. }
  117. if (unlikely(N == 0)) continue;
  118. /* call user stream occluded function */
  119. accel->occluded1M(rays_filtered,N,prim.primID,context);
  120. /* mark occluded rays */
  121. for (size_t i=0; i<N; i++)
  122. {
  123. if (rays_filtered[i]->geomID == 0) {
  124. hit |= (size_t)1 << index_filtered[i];
  125. }
  126. }
  127. valid_in &= ~hit;
  128. }
  129. return hit;
  130. }
  131. template<int K>
  132. static __forceinline void intersectK(const vbool<K>& valid, /* PrecalculationsK& pre, */ RayK<K>& ray, IntersectContext* context, const Primitive* prim, size_t num, size_t& lazy_node)
  133. {
  134. }
  135. template<int K>
  136. static __forceinline vbool<K> occludedK(const vbool<K>& valid, /* PrecalculationsK& pre, */ RayK<K>& ray, IntersectContext* context, const Primitive* prim, size_t num, size_t& lazy_node)
  137. {
  138. return valid;
  139. }
  140. };
  141. template<int K, bool mblur>
  142. struct ObjectIntersectorK
  143. {
  144. typedef Object Primitive;
  145. struct PrecalculationsBase {
  146. __forceinline PrecalculationsBase (const vbool<K>& valid, const RayK<K>& ray) {}
  147. };
  148. typedef typename std::conditional<mblur,
  149. IntersectorKPrecalculationsMB<K,PrecalculationsBase>,
  150. IntersectorKPrecalculations<K,PrecalculationsBase>>::type Precalculations;
  151. static __forceinline void intersect(const vbool<K>& valid_i, const Precalculations& pre, RayK<K>& ray, IntersectContext* context, const Primitive& prim)
  152. {
  153. AVX_ZERO_UPPER();
  154. vbool<K> valid = valid_i;
  155. AccelSet* accel = (AccelSet*) context->scene->get(prim.geomID);
  156. /* perform ray mask test */
  157. #if defined(EMBREE_RAY_MASK)
  158. valid &= (ray.mask & accel->mask) != 0;
  159. if (none(valid)) return;
  160. #endif
  161. accel->intersect(valid,ray,prim.primID,context);
  162. }
  163. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, const Precalculations& pre, RayK<K>& ray, IntersectContext* context, const Primitive& prim)
  164. {
  165. vbool<K> valid = valid_i;
  166. AccelSet* accel = (AccelSet*) context->scene->get(prim.geomID);
  167. /* perform ray mask test */
  168. #if defined(EMBREE_RAY_MASK)
  169. valid &= (ray.mask & accel->mask) != 0;
  170. if (none(valid)) return false;
  171. #endif
  172. accel->occluded(valid,ray,prim.primID,context);
  173. return ray.geomID == 0;
  174. }
  175. static __forceinline void intersect(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const Primitive& prim) {
  176. intersect(vbool<K>(1<<int(k)),pre,ray,context,prim);
  177. }
  178. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const Primitive& prim) {
  179. occluded(vbool<K>(1<<int(k)),pre,ray,context,prim);
  180. return ray.geomID[k] == 0;
  181. }
  182. };
  183. typedef ObjectIntersectorK<4,false> ObjectIntersector4;
  184. typedef ObjectIntersectorK<8,false> ObjectIntersector8;
  185. typedef ObjectIntersectorK<16,false> ObjectIntersector16;
  186. typedef ObjectIntersectorK<4,true> ObjectIntersector4MB;
  187. typedef ObjectIntersectorK<8,true> ObjectIntersector8MB;
  188. typedef ObjectIntersectorK<16,true> ObjectIntersector16MB;
  189. }
  190. }