pointi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Copyright 2009-2020 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "primitive.h"
  5. namespace embree
  6. {
  7. template<int M>
  8. struct PointMi
  9. {
  10. /* Virtual interface to query information about the line segment type */
  11. struct Type : public PrimitiveType
  12. {
  13. const char* name() const;
  14. size_t sizeActive(const char* This) const;
  15. size_t sizeTotal(const char* This) const;
  16. size_t getBytes(const char* This) const;
  17. };
  18. static Type type;
  19. public:
  20. /* primitive supports multiple time segments */
  21. static const bool singleTimeSegment = false;
  22. /* Returns maximum number of stored line segments */
  23. static __forceinline size_t max_size()
  24. {
  25. return M;
  26. }
  27. /* Returns required number of primitive blocks for N line segments */
  28. static __forceinline size_t blocks(size_t N)
  29. {
  30. return (N + max_size() - 1) / max_size();
  31. }
  32. /* Returns required number of bytes for N line segments */
  33. static __forceinline size_t bytes(size_t N)
  34. {
  35. return blocks(N) * sizeof(PointMi);
  36. }
  37. public:
  38. /* Default constructor */
  39. __forceinline PointMi() {}
  40. /* Construction from vertices and IDs */
  41. __forceinline PointMi(const vuint<M>& geomIDs, const vuint<M>& primIDs, Geometry::GType gtype, uint32_t numPrimitives)
  42. : gtype((unsigned char)gtype),
  43. numPrimitives(numPrimitives),
  44. sharedGeomID(geomIDs[0]),
  45. primIDs(primIDs)
  46. {
  47. assert(all(vuint<M>(geomID()) == geomIDs));
  48. }
  49. /* Returns a mask that tells which line segments are valid */
  50. __forceinline vbool<M> valid() const {
  51. return vint<M>(step) < vint<M>(numPrimitives);
  52. }
  53. /* Returns a mask that tells which line segments are valid */
  54. template<int Mx> __forceinline vbool<Mx> valid() const {
  55. return vint<Mx>(step) < vint<Mx>(numPrimitives);
  56. }
  57. /* Returns if the specified line segment is valid */
  58. __forceinline bool valid(const size_t i) const
  59. {
  60. assert(i < M);
  61. return i < numPrimitives;
  62. }
  63. /* Returns the number of stored line segments */
  64. __forceinline size_t size() const {
  65. return numPrimitives;
  66. }
  67. __forceinline unsigned int geomID(unsigned int i = 0) const {
  68. return sharedGeomID;
  69. }
  70. __forceinline vuint<M>& primID() {
  71. return primIDs;
  72. }
  73. __forceinline const vuint<M>& primID() const {
  74. return primIDs;
  75. }
  76. __forceinline unsigned int primID(const size_t i) const {
  77. assert(i < M);
  78. return primIDs[i];
  79. }
  80. /* gather the line segments */
  81. __forceinline void gather(Vec4vf<M>& p0, const Points* geom) const;
  82. __forceinline void gather(Vec4vf<M>& p0, Vec3vf<M>& n0, const Points* geom) const;
  83. __forceinline void gatheri(Vec4vf<M>& p0, const Points* geom, const int itime) const;
  84. __forceinline void gatheri(Vec4vf<M>& p0, Vec3vf<M>& n0, const Points* geom, const int itime) const;
  85. __forceinline void gather(Vec4vf<M>& p0, const Points* geom, float time) const;
  86. __forceinline void gather(Vec4vf<M>& p0, Vec3vf<M>& n0, const Points* geom, float time) const;
  87. /* Calculate the bounds of the line segments */
  88. __forceinline const BBox3fa bounds(const Scene* scene, size_t itime = 0) const
  89. {
  90. BBox3fa bounds = empty;
  91. for (size_t i = 0; i < M && valid(i); i++) {
  92. const Points* geom = scene->get<Points>(geomID(i));
  93. bounds.extend(geom->bounds(primID(i),itime));
  94. }
  95. return bounds;
  96. }
  97. /* Calculate the linear bounds of the primitive */
  98. __forceinline LBBox3fa linearBounds(const Scene* scene, size_t itime) {
  99. return LBBox3fa(bounds(scene, itime + 0), bounds(scene, itime + 1));
  100. }
  101. __forceinline LBBox3fa linearBounds(const Scene* const scene, size_t itime, size_t numTimeSteps)
  102. {
  103. LBBox3fa allBounds = empty;
  104. for (size_t i = 0; i < M && valid(i); i++) {
  105. const Points* geom = scene->get<Points>(geomID(i));
  106. allBounds.extend(geom->linearBounds(primID(i), itime, numTimeSteps));
  107. }
  108. return allBounds;
  109. }
  110. __forceinline LBBox3fa linearBounds(const Scene* const scene, const BBox1f time_range)
  111. {
  112. LBBox3fa allBounds = empty;
  113. for (size_t i = 0; i < M && valid(i); i++) {
  114. const Points* geom = scene->get<Points>(geomID((unsigned int)i));
  115. allBounds.extend(geom->linearBounds(primID(i), time_range));
  116. }
  117. return allBounds;
  118. }
  119. /* Fill line segment from line segment list */
  120. template<typename PrimRefT>
  121. __forceinline void fill(const PrimRefT* prims, size_t& begin, size_t end, Scene* scene)
  122. {
  123. Geometry::GType gty = scene->get(prims[begin].geomID())->getType();
  124. vuint<M> geomID, primID;
  125. vuint<M> v0;
  126. const PrimRefT* prim = &prims[begin];
  127. int numPrimitives = 0;
  128. for (size_t i = 0; i < M; i++) {
  129. if (begin < end) {
  130. geomID[i] = prim->geomID();
  131. primID[i] = prim->primID();
  132. begin++;
  133. numPrimitives++;
  134. } else {
  135. assert(i);
  136. if (i > 0) {
  137. geomID[i] = geomID[i - 1];
  138. primID[i] = primID[i - 1];
  139. }
  140. }
  141. if (begin < end)
  142. prim = &prims[begin]; // FIXME: remove this line
  143. }
  144. new (this) PointMi(geomID, primID, gty, numPrimitives); // FIXME: use non temporal store
  145. }
  146. template<typename BVH, typename Allocator>
  147. __forceinline static typename BVH::NodeRef createLeaf(BVH* bvh,
  148. const PrimRef* prims,
  149. const range<size_t>& set,
  150. const Allocator& alloc)
  151. {
  152. size_t start = set.begin();
  153. size_t items = PointMi::blocks(set.size());
  154. size_t numbytes = PointMi::bytes(set.size());
  155. PointMi* accel = (PointMi*)alloc.malloc1(numbytes, M * sizeof(float));
  156. for (size_t i = 0; i < items; i++) {
  157. accel[i].fill(prims, start, set.end(), bvh->scene);
  158. }
  159. return bvh->encodeLeaf((char*)accel, items);
  160. };
  161. __forceinline LBBox3fa fillMB(const PrimRef* prims, size_t& begin, size_t end, Scene* scene, size_t itime)
  162. {
  163. fill(prims, begin, end, scene);
  164. return linearBounds(scene, itime);
  165. }
  166. __forceinline LBBox3fa fillMB(
  167. const PrimRefMB* prims, size_t& begin, size_t end, Scene* scene, const BBox1f time_range)
  168. {
  169. fill(prims, begin, end, scene);
  170. return linearBounds(scene, time_range);
  171. }
  172. template<typename BVH, typename SetMB, typename Allocator>
  173. __forceinline static typename BVH::NodeRecordMB4D createLeafMB(BVH* bvh, const SetMB& prims, const Allocator& alloc)
  174. {
  175. size_t start = prims.object_range.begin();
  176. size_t end = prims.object_range.end();
  177. size_t items = PointMi::blocks(prims.object_range.size());
  178. size_t numbytes = PointMi::bytes(prims.object_range.size());
  179. PointMi* accel = (PointMi*)alloc.malloc1(numbytes, M * sizeof(float));
  180. const typename BVH::NodeRef node = bvh->encodeLeaf((char*)accel, items);
  181. LBBox3fa bounds = empty;
  182. for (size_t i = 0; i < items; i++)
  183. bounds.extend(accel[i].fillMB(prims.prims->data(), start, end, bvh->scene, prims.time_range));
  184. return typename BVH::NodeRecordMB4D(node, bounds, prims.time_range);
  185. };
  186. /*! output operator */
  187. friend __forceinline embree_ostream operator<<(embree_ostream cout, const PointMi& point)
  188. {
  189. return cout << "Point" << M << "i {" << point.geomID() << ", " << point.primID() << "}";
  190. }
  191. public:
  192. unsigned char gtype;
  193. unsigned char numPrimitives;
  194. unsigned int sharedGeomID;
  195. private:
  196. vuint<M> primIDs; // primitive ID
  197. };
  198. template<>
  199. __forceinline void PointMi<4>::gather(Vec4vf4& p0, const Points* geom) const
  200. {
  201. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0)));
  202. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1)));
  203. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2)));
  204. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3)));
  205. transpose(a0, a1, a2, a3, p0.x, p0.y, p0.z, p0.w);
  206. }
  207. template<>
  208. __forceinline void PointMi<4>::gather(Vec4vf4& p0, Vec3vf4& n0, const Points* geom) const
  209. {
  210. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0)));
  211. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1)));
  212. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2)));
  213. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3)));
  214. transpose(a0, a1, a2, a3, p0.x, p0.y, p0.z, p0.w);
  215. const vfloat4 b0 = vfloat4(geom->normal(primID(0)));
  216. const vfloat4 b1 = vfloat4(geom->normal(primID(1)));
  217. const vfloat4 b2 = vfloat4(geom->normal(primID(2)));
  218. const vfloat4 b3 = vfloat4(geom->normal(primID(3)));
  219. transpose(b0, b1, b2, b3, n0.x, n0.y, n0.z);
  220. }
  221. template<>
  222. __forceinline void PointMi<4>::gatheri(Vec4vf4& p0, const Points* geom, const int itime) const
  223. {
  224. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0), itime));
  225. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1), itime));
  226. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2), itime));
  227. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3), itime));
  228. transpose(a0, a1, a2, a3, p0.x, p0.y, p0.z, p0.w);
  229. }
  230. template<>
  231. __forceinline void PointMi<4>::gatheri(Vec4vf4& p0, Vec3vf4& n0, const Points* geom, const int itime) const
  232. {
  233. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0), itime));
  234. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1), itime));
  235. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2), itime));
  236. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3), itime));
  237. transpose(a0, a1, a2, a3, p0.x, p0.y, p0.z, p0.w);
  238. const vfloat4 b0 = vfloat4(geom->normal(primID(0), itime));
  239. const vfloat4 b1 = vfloat4(geom->normal(primID(1), itime));
  240. const vfloat4 b2 = vfloat4(geom->normal(primID(2), itime));
  241. const vfloat4 b3 = vfloat4(geom->normal(primID(3), itime));
  242. transpose(b0, b1, b2, b3, n0.x, n0.y, n0.z);
  243. }
  244. template<>
  245. __forceinline void PointMi<4>::gather(Vec4vf4& p0, const Points* geom, float time) const
  246. {
  247. float ftime;
  248. const int itime = geom->timeSegment(time, ftime);
  249. Vec4vf4 a0; gatheri(a0, geom, itime);
  250. Vec4vf4 b0; gatheri(b0, geom, itime + 1);
  251. p0 = lerp(a0, b0, vfloat4(ftime));
  252. }
  253. template<>
  254. __forceinline void PointMi<4>::gather(Vec4vf4& p0, Vec3vf4& n0, const Points* geom, float time) const
  255. {
  256. float ftime;
  257. const int itime = geom->timeSegment(time, ftime);
  258. Vec4vf4 a0, b0;
  259. Vec3vf4 norm0, norm1;
  260. gatheri(a0, norm0, geom, itime);
  261. gatheri(b0, norm1, geom, itime + 1);
  262. p0 = lerp(a0, b0, vfloat4(ftime));
  263. n0 = lerp(norm0, norm1, vfloat4(ftime));
  264. }
  265. #if defined(__AVX__)
  266. template<>
  267. __forceinline void PointMi<8>::gather(Vec4vf8& p0, const Points* geom) const
  268. {
  269. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0)));
  270. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1)));
  271. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2)));
  272. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3)));
  273. const vfloat4 a4 = vfloat4::loadu(geom->vertexPtr(primID(4)));
  274. const vfloat4 a5 = vfloat4::loadu(geom->vertexPtr(primID(5)));
  275. const vfloat4 a6 = vfloat4::loadu(geom->vertexPtr(primID(6)));
  276. const vfloat4 a7 = vfloat4::loadu(geom->vertexPtr(primID(7)));
  277. transpose(a0, a1, a2, a3, a4, a5, a6, a7, p0.x, p0.y, p0.z, p0.w);
  278. }
  279. template<>
  280. __forceinline void PointMi<8>::gather(Vec4vf8& p0, Vec3vf8& n0, const Points* geom) const
  281. {
  282. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0)));
  283. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1)));
  284. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2)));
  285. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3)));
  286. const vfloat4 a4 = vfloat4::loadu(geom->vertexPtr(primID(4)));
  287. const vfloat4 a5 = vfloat4::loadu(geom->vertexPtr(primID(5)));
  288. const vfloat4 a6 = vfloat4::loadu(geom->vertexPtr(primID(6)));
  289. const vfloat4 a7 = vfloat4::loadu(geom->vertexPtr(primID(7)));
  290. transpose(a0, a1, a2, a3, a4, a5, a6, a7, p0.x, p0.y, p0.z, p0.w);
  291. const vfloat4 b0 = vfloat4(geom->normal(primID(0)));
  292. const vfloat4 b1 = vfloat4(geom->normal(primID(1)));
  293. const vfloat4 b2 = vfloat4(geom->normal(primID(2)));
  294. const vfloat4 b3 = vfloat4(geom->normal(primID(3)));
  295. const vfloat4 b4 = vfloat4(geom->normal(primID(4)));
  296. const vfloat4 b5 = vfloat4(geom->normal(primID(5)));
  297. const vfloat4 b6 = vfloat4(geom->normal(primID(6)));
  298. const vfloat4 b7 = vfloat4(geom->normal(primID(7)));
  299. transpose(b0, b1, b2, b3, b4, b5, b6, b7, n0.x, n0.y, n0.z);
  300. }
  301. template<>
  302. __forceinline void PointMi<8>::gatheri(Vec4vf8& p0, const Points* geom, const int itime) const
  303. {
  304. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0), itime));
  305. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1), itime));
  306. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2), itime));
  307. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3), itime));
  308. const vfloat4 a4 = vfloat4::loadu(geom->vertexPtr(primID(4), itime));
  309. const vfloat4 a5 = vfloat4::loadu(geom->vertexPtr(primID(5), itime));
  310. const vfloat4 a6 = vfloat4::loadu(geom->vertexPtr(primID(6), itime));
  311. const vfloat4 a7 = vfloat4::loadu(geom->vertexPtr(primID(7), itime));
  312. transpose(a0, a1, a2, a3, a4, a5, a6, a7, p0.x, p0.y, p0.z, p0.w);
  313. }
  314. template<>
  315. __forceinline void PointMi<8>::gatheri(Vec4vf8& p0, Vec3vf8& n0, const Points* geom, const int itime) const
  316. {
  317. const vfloat4 a0 = vfloat4::loadu(geom->vertexPtr(primID(0), itime));
  318. const vfloat4 a1 = vfloat4::loadu(geom->vertexPtr(primID(1), itime));
  319. const vfloat4 a2 = vfloat4::loadu(geom->vertexPtr(primID(2), itime));
  320. const vfloat4 a3 = vfloat4::loadu(geom->vertexPtr(primID(3), itime));
  321. const vfloat4 a4 = vfloat4::loadu(geom->vertexPtr(primID(4), itime));
  322. const vfloat4 a5 = vfloat4::loadu(geom->vertexPtr(primID(5), itime));
  323. const vfloat4 a6 = vfloat4::loadu(geom->vertexPtr(primID(6), itime));
  324. const vfloat4 a7 = vfloat4::loadu(geom->vertexPtr(primID(7), itime));
  325. transpose(a0, a1, a2, a3, a4, a5, a6, a7, p0.x, p0.y, p0.z, p0.w);
  326. const vfloat4 b0 = vfloat4(geom->normal(primID(0), itime));
  327. const vfloat4 b1 = vfloat4(geom->normal(primID(1), itime));
  328. const vfloat4 b2 = vfloat4(geom->normal(primID(2), itime));
  329. const vfloat4 b3 = vfloat4(geom->normal(primID(3), itime));
  330. const vfloat4 b4 = vfloat4(geom->normal(primID(4), itime));
  331. const vfloat4 b5 = vfloat4(geom->normal(primID(5), itime));
  332. const vfloat4 b6 = vfloat4(geom->normal(primID(6), itime));
  333. const vfloat4 b7 = vfloat4(geom->normal(primID(7), itime));
  334. transpose(b0, b1, b2, b3, b4, b5, b6, b7, n0.x, n0.y, n0.z);
  335. }
  336. template<>
  337. __forceinline void PointMi<8>::gather(Vec4vf8& p0, const Points* geom, float time) const
  338. {
  339. float ftime;
  340. const int itime = geom->timeSegment(time, ftime);
  341. Vec4vf8 a0;
  342. gatheri(a0, geom, itime);
  343. Vec4vf8 b0;
  344. gatheri(b0, geom, itime + 1);
  345. p0 = lerp(a0, b0, vfloat8(ftime));
  346. }
  347. template<>
  348. __forceinline void PointMi<8>::gather(Vec4vf8& p0, Vec3vf8& n0, const Points* geom, float time) const
  349. {
  350. float ftime;
  351. const int itime = geom->timeSegment(time, ftime);
  352. Vec4vf8 a0, b0;
  353. Vec3vf8 norm0, norm1;
  354. gatheri(a0, norm0, geom, itime);
  355. gatheri(b0, norm1, geom, itime + 1);
  356. p0 = lerp(a0, b0, vfloat8(ftime));
  357. n0 = lerp(norm0, norm1, vfloat8(ftime));
  358. }
  359. #endif
  360. template<int M>
  361. typename PointMi<M>::Type PointMi<M>::type;
  362. typedef PointMi<4> Point4i;
  363. typedef PointMi<8> Point8i;
  364. } // namespace embree