pointi.h 16 KB

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