trianglei.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "primitive.h"
  5. #include "../common/scene.h"
  6. namespace embree
  7. {
  8. /* Stores M triangles from an indexed face set */
  9. template <int M>
  10. struct TriangleMi
  11. {
  12. /* Virtual interface to query information about the triangle type */
  13. struct Type : public PrimitiveType
  14. {
  15. const char* name() const;
  16. size_t sizeActive(const char* This) const;
  17. size_t sizeTotal(const char* This) const;
  18. size_t getBytes(const char* This) const;
  19. };
  20. static Type type;
  21. public:
  22. /* primitive supports multiple time segments */
  23. static const bool singleTimeSegment = false;
  24. /* Returns maximum number of stored triangles */
  25. static __forceinline size_t max_size() { return M; }
  26. /* Returns required number of primitive blocks for N primitives */
  27. static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); }
  28. public:
  29. /* Default constructor */
  30. __forceinline TriangleMi() { }
  31. /* Construction from vertices and IDs */
  32. __forceinline TriangleMi(const vuint<M>& v0,
  33. const vuint<M>& v1,
  34. const vuint<M>& v2,
  35. const vuint<M>& geomIDs,
  36. const vuint<M>& primIDs)
  37. #if defined(EMBREE_COMPACT_POLYS)
  38. : geomIDs(geomIDs), primIDs(primIDs) {}
  39. #else
  40. : v0_(v0), v1_(v1), v2_(v2), geomIDs(geomIDs), primIDs(primIDs) {}
  41. #endif
  42. /* Returns a mask that tells which triangles are valid */
  43. __forceinline vbool<M> valid() const { return primIDs != vuint<M>(-1); }
  44. /* Returns if the specified triangle is valid */
  45. __forceinline bool valid(const size_t i) const { assert(i<M); return primIDs[i] != -1; }
  46. /* Returns the number of stored triangles */
  47. __forceinline size_t size() const { return bsf(~movemask(valid())); }
  48. /* Returns the geometry IDs */
  49. __forceinline vuint<M> geomID() const { return geomIDs; }
  50. __forceinline unsigned int geomID(const size_t i) const { assert(i<M); return geomIDs[i]; }
  51. /* Returns the primitive IDs */
  52. __forceinline vuint<M> primID() const { return primIDs; }
  53. __forceinline unsigned int primID(const size_t i) const { assert(i<M); return primIDs[i]; }
  54. /* Calculate the bounds of the triangles */
  55. __forceinline const BBox3fa bounds(const Scene *const scene, const size_t itime=0) const
  56. {
  57. BBox3fa bounds = empty;
  58. for (size_t i=0; i<M && valid(i); i++) {
  59. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(i));
  60. bounds.extend(mesh->bounds(primID(i),itime));
  61. }
  62. return bounds;
  63. }
  64. /* Calculate the linear bounds of the primitive */
  65. __forceinline LBBox3fa linearBounds(const Scene *const scene, size_t itime) {
  66. return LBBox3fa(bounds(scene,itime+0),bounds(scene,itime+1));
  67. }
  68. __forceinline LBBox3fa linearBounds(const Scene *const scene, size_t itime, size_t numTimeSteps)
  69. {
  70. LBBox3fa allBounds = empty;
  71. for (size_t i=0; i<M && valid(i); i++)
  72. {
  73. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(i));
  74. allBounds.extend(mesh->linearBounds(primID(i), itime, numTimeSteps));
  75. }
  76. return allBounds;
  77. }
  78. __forceinline LBBox3fa linearBounds(const Scene *const scene, const BBox1f time_range)
  79. {
  80. LBBox3fa allBounds = empty;
  81. for (size_t i=0; i<M && valid(i); i++)
  82. {
  83. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(i));
  84. allBounds.extend(mesh->linearBounds(primID(i), time_range));
  85. }
  86. return allBounds;
  87. }
  88. /* Non-temporal store */
  89. __forceinline static void store_nt(TriangleMi* dst, const TriangleMi& src)
  90. {
  91. #if !defined(EMBREE_COMPACT_POLYS)
  92. vuint<M>::store_nt(&dst->v0_,src.v0_);
  93. vuint<M>::store_nt(&dst->v1_,src.v1_);
  94. vuint<M>::store_nt(&dst->v2_,src.v2_);
  95. #endif
  96. vuint<M>::store_nt(&dst->geomIDs,src.geomIDs);
  97. vuint<M>::store_nt(&dst->primIDs,src.primIDs);
  98. }
  99. /* Fill triangle from triangle list */
  100. template<typename PrimRefT>
  101. __forceinline void fill(const PrimRefT* prims, size_t& begin, size_t end, Scene* scene)
  102. {
  103. vuint<M> v0 = zero, v1 = zero, v2 = zero;
  104. vuint<M> geomID = -1, primID = -1;
  105. const PrimRefT* prim = &prims[begin];
  106. for (size_t i=0; i<M; i++)
  107. {
  108. if (begin<end) {
  109. geomID[i] = prim->geomID();
  110. primID[i] = prim->primID();
  111. #if !defined(EMBREE_COMPACT_POLYS)
  112. const TriangleMesh* mesh = scene->get<TriangleMesh>(prim->geomID());
  113. const TriangleMesh::Triangle& tri = mesh->triangle(prim->primID());
  114. unsigned int int_stride = mesh->vertices0.getStride()/4;
  115. v0[i] = tri.v[0] * int_stride;
  116. v1[i] = tri.v[1] * int_stride;
  117. v2[i] = tri.v[2] * int_stride;
  118. #endif
  119. begin++;
  120. } else {
  121. assert(i);
  122. if (likely(i > 0)) {
  123. geomID[i] = geomID[0];
  124. primID[i] = -1;
  125. v0[i] = v0[0];
  126. v1[i] = v0[0];
  127. v2[i] = v0[0];
  128. }
  129. }
  130. if (begin<end) prim = &prims[begin];
  131. }
  132. new (this) TriangleMi(v0,v1,v2,geomID,primID); // FIXME: use non temporal store
  133. }
  134. __forceinline LBBox3fa fillMB(const PrimRef* prims, size_t& begin, size_t end, Scene* scene, size_t itime)
  135. {
  136. fill(prims, begin, end, scene);
  137. return linearBounds(scene, itime);
  138. }
  139. __forceinline LBBox3fa fillMB(const PrimRefMB* prims, size_t& begin, size_t end, Scene* scene, const BBox1f time_range)
  140. {
  141. fill(prims, begin, end, scene);
  142. return linearBounds(scene, time_range);
  143. }
  144. /* Updates the primitive */
  145. __forceinline BBox3fa update(TriangleMesh* mesh)
  146. {
  147. BBox3fa bounds = empty;
  148. for (size_t i=0; i<M; i++)
  149. {
  150. if (primID(i) == -1) break;
  151. const unsigned int primId = primID(i);
  152. const TriangleMesh::Triangle& tri = mesh->triangle(primId);
  153. const Vec3fa p0 = mesh->vertex(tri.v[0]);
  154. const Vec3fa p1 = mesh->vertex(tri.v[1]);
  155. const Vec3fa p2 = mesh->vertex(tri.v[2]);
  156. bounds.extend(merge(BBox3fa(p0),BBox3fa(p1),BBox3fa(p2)));
  157. }
  158. return bounds;
  159. }
  160. protected:
  161. #if !defined(EMBREE_COMPACT_POLYS)
  162. vuint<M> v0_; // 4 byte offset of 1st vertex
  163. vuint<M> v1_; // 4 byte offset of 2nd vertex
  164. vuint<M> v2_; // 4 byte offset of 3rd vertex
  165. #endif
  166. vuint<M> geomIDs; // geometry ID of mesh
  167. vuint<M> primIDs; // primitive ID of primitive inside mesh
  168. };
  169. namespace isa
  170. {
  171. template<int M>
  172. struct TriangleMi : public embree::TriangleMi<M>
  173. {
  174. #if !defined(EMBREE_COMPACT_POLYS)
  175. using embree::TriangleMi<M>::v0_;
  176. using embree::TriangleMi<M>::v1_;
  177. using embree::TriangleMi<M>::v2_;
  178. #endif
  179. using embree::TriangleMi<M>::geomIDs;
  180. using embree::TriangleMi<M>::primIDs;
  181. using embree::TriangleMi<M>::geomID;
  182. using embree::TriangleMi<M>::primID;
  183. using embree::TriangleMi<M>::valid;
  184. /* loads a single vertex */
  185. template<int vid>
  186. __forceinline Vec3f getVertex(const size_t index, const Scene *const scene) const
  187. {
  188. #if defined(EMBREE_COMPACT_POLYS)
  189. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  190. const TriangleMesh::Triangle& tri = mesh->triangle(primID(index));
  191. return (Vec3f) mesh->vertices[0][tri.v[vid]];
  192. #else
  193. const vuint<M>& v = getVertexOffset<vid>();
  194. const float* vertices = scene->vertices[geomID(index)];
  195. return (Vec3f&) vertices[v[index]];
  196. #endif
  197. }
  198. template<int vid, typename T>
  199. __forceinline Vec3<T> getVertex(const size_t index, const Scene *const scene, const size_t itime, const T& ftime) const
  200. {
  201. #if defined(EMBREE_COMPACT_POLYS)
  202. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  203. const TriangleMesh::Triangle& tri = mesh->triangle(primID(index));
  204. const Vec3fa v0 = mesh->vertices[itime+0][tri.v[vid]];
  205. const Vec3fa v1 = mesh->vertices[itime+1][tri.v[vid]];
  206. #else
  207. const vuint<M>& v = getVertexOffset<vid>();
  208. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  209. const float* vertices0 = (const float*) mesh->vertexPtr(0,itime+0);
  210. const float* vertices1 = (const float*) mesh->vertexPtr(0,itime+1);
  211. const Vec3fa v0 = Vec3fa::loadu(vertices0+v[index]);
  212. const Vec3fa v1 = Vec3fa::loadu(vertices1+v[index]);
  213. #endif
  214. const Vec3<T> p0(v0.x,v0.y,v0.z);
  215. const Vec3<T> p1(v1.x,v1.y,v1.z);
  216. return lerp(p0,p1,ftime);
  217. }
  218. template<int vid, int K, typename T>
  219. __forceinline Vec3<T> getVertex(const vbool<K>& valid, const size_t index, const Scene *const scene, const vint<K>& itime, const T& ftime) const
  220. {
  221. Vec3<T> p0, p1;
  222. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  223. for (size_t mask=movemask(valid), i=bsf(mask); mask; mask=btc(mask,i), i=bsf(mask))
  224. {
  225. #if defined(EMBREE_COMPACT_POLYS)
  226. const TriangleMesh::Triangle& tri = mesh->triangle(primID(index));
  227. const Vec3fa v0 = mesh->vertices[itime[i]+0][tri.v[vid]];
  228. const Vec3fa v1 = mesh->vertices[itime[i]+1][tri.v[vid]];
  229. #else
  230. const vuint<M>& v = getVertexOffset<vid>();
  231. const float* vertices0 = (const float*) mesh->vertexPtr(0,itime[i]+0);
  232. const float* vertices1 = (const float*) mesh->vertexPtr(0,itime[i]+1);
  233. const Vec3fa v0 = Vec3fa::loadu(vertices0+v[index]);
  234. const Vec3fa v1 = Vec3fa::loadu(vertices1+v[index]);
  235. #endif
  236. p0.x[i] = v0.x; p0.y[i] = v0.y; p0.z[i] = v0.z;
  237. p1.x[i] = v1.x; p1.y[i] = v1.y; p1.z[i] = v1.z;
  238. }
  239. return (T(one)-ftime)*p0 + ftime*p1;
  240. }
  241. struct Triangle {
  242. vfloat4 v0,v1,v2;
  243. };
  244. #if defined(EMBREE_COMPACT_POLYS)
  245. __forceinline Triangle loadTriangle(const int i, const Scene* const scene) const
  246. {
  247. const unsigned int geomID = geomIDs[i];
  248. const unsigned int primID = primIDs[i];
  249. if (unlikely(primID == -1)) return { zero, zero, zero };
  250. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID);
  251. const TriangleMesh::Triangle& tri = mesh->triangle(primID);
  252. const vfloat4 v0 = (vfloat4) mesh->vertices0[tri.v[0]];
  253. const vfloat4 v1 = (vfloat4) mesh->vertices0[tri.v[1]];
  254. const vfloat4 v2 = (vfloat4) mesh->vertices0[tri.v[2]];
  255. return { v0, v1, v2 };
  256. }
  257. __forceinline Triangle loadTriangle(const int i, const int itime, const TriangleMesh* const mesh) const
  258. {
  259. const unsigned int primID = primIDs[i];
  260. if (unlikely(primID == -1)) return { zero, zero, zero };
  261. const TriangleMesh::Triangle& tri = mesh->triangle(primID);
  262. const vfloat4 v0 = (vfloat4) mesh->vertices[itime][tri.v[0]];
  263. const vfloat4 v1 = (vfloat4) mesh->vertices[itime][tri.v[1]];
  264. const vfloat4 v2 = (vfloat4) mesh->vertices[itime][tri.v[2]];
  265. return { v0, v1, v2 };
  266. }
  267. #else
  268. __forceinline Triangle loadTriangle(const int i, const Scene* const scene) const
  269. {
  270. const float* vertices = scene->vertices[geomID(i)];
  271. const vfloat4 v0 = vfloat4::loadu(vertices + v0_[i]);
  272. const vfloat4 v1 = vfloat4::loadu(vertices + v1_[i]);
  273. const vfloat4 v2 = vfloat4::loadu(vertices + v2_[i]);
  274. return { v0, v1, v2 };
  275. }
  276. __forceinline Triangle loadTriangle(const int i, const int itime, const TriangleMesh* const mesh) const
  277. {
  278. const float* vertices = (const float*) mesh->vertexPtr(0,itime);
  279. const vfloat4 v0 = vfloat4::loadu(vertices + v0_[i]);
  280. const vfloat4 v1 = vfloat4::loadu(vertices + v1_[i]);
  281. const vfloat4 v2 = vfloat4::loadu(vertices + v2_[i]);
  282. return { v0, v1, v2 };
  283. }
  284. #endif
  285. /* Gather the triangles */
  286. __forceinline void gather(Vec3vf<M>& p0, Vec3vf<M>& p1, Vec3vf<M>& p2, const Scene* const scene) const;
  287. template<int K>
  288. #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER < 2000) // workaround for compiler bug in ICC 2019
  289. __noinline
  290. #else
  291. __forceinline
  292. #endif
  293. void gather(const vbool<K>& valid,
  294. Vec3vf<K>& p0,
  295. Vec3vf<K>& p1,
  296. Vec3vf<K>& p2,
  297. const size_t index,
  298. const Scene* const scene,
  299. const vfloat<K>& time) const
  300. {
  301. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  302. vfloat<K> ftime;
  303. const vint<K> itime = mesh->timeSegment<K>(time, ftime);
  304. const size_t first = bsf(movemask(valid));
  305. if (likely(all(valid,itime[first] == itime)))
  306. {
  307. p0 = getVertex<0>(index, scene, itime[first], ftime);
  308. p1 = getVertex<1>(index, scene, itime[first], ftime);
  309. p2 = getVertex<2>(index, scene, itime[first], ftime);
  310. } else {
  311. p0 = getVertex<0,K>(valid, index, scene, itime, ftime);
  312. p1 = getVertex<1,K>(valid, index, scene, itime, ftime);
  313. p2 = getVertex<2,K>(valid, index, scene, itime, ftime);
  314. }
  315. }
  316. __forceinline void gather(Vec3vf<M>& p0,
  317. Vec3vf<M>& p1,
  318. Vec3vf<M>& p2,
  319. const TriangleMesh* mesh,
  320. const Scene *const scene,
  321. const int itime) const;
  322. __forceinline void gather(Vec3vf<M>& p0,
  323. Vec3vf<M>& p1,
  324. Vec3vf<M>& p2,
  325. const Scene *const scene,
  326. const float time) const;
  327. #if !defined(EMBREE_COMPACT_POLYS)
  328. template<int N> const vuint<M>& getVertexOffset() const;
  329. #endif
  330. };
  331. #if !defined(EMBREE_COMPACT_POLYS)
  332. template<> template<> __forceinline const vuint<4>& TriangleMi<4>::getVertexOffset<0>() const { return v0_; }
  333. template<> template<> __forceinline const vuint<4>& TriangleMi<4>::getVertexOffset<1>() const { return v1_; }
  334. template<> template<> __forceinline const vuint<4>& TriangleMi<4>::getVertexOffset<2>() const { return v2_; }
  335. #endif
  336. template<>
  337. __forceinline void TriangleMi<4>::gather(Vec3vf4& p0,
  338. Vec3vf4& p1,
  339. Vec3vf4& p2,
  340. const Scene* const scene) const
  341. {
  342. const Triangle tri0 = loadTriangle(0,scene);
  343. const Triangle tri1 = loadTriangle(1,scene);
  344. const Triangle tri2 = loadTriangle(2,scene);
  345. const Triangle tri3 = loadTriangle(3,scene);
  346. transpose(tri0.v0,tri1.v0,tri2.v0,tri3.v0,p0.x,p0.y,p0.z);
  347. transpose(tri0.v1,tri1.v1,tri2.v1,tri3.v1,p1.x,p1.y,p1.z);
  348. transpose(tri0.v2,tri1.v2,tri2.v2,tri3.v2,p2.x,p2.y,p2.z);
  349. }
  350. template<>
  351. __forceinline void TriangleMi<4>::gather(Vec3vf4& p0,
  352. Vec3vf4& p1,
  353. Vec3vf4& p2,
  354. const TriangleMesh* mesh,
  355. const Scene *const scene,
  356. const int itime) const
  357. {
  358. const Triangle tri0 = loadTriangle(0,itime,mesh);
  359. const Triangle tri1 = loadTriangle(1,itime,mesh);
  360. const Triangle tri2 = loadTriangle(2,itime,mesh);
  361. const Triangle tri3 = loadTriangle(3,itime,mesh);
  362. transpose(tri0.v0,tri1.v0,tri2.v0,tri3.v0,p0.x,p0.y,p0.z);
  363. transpose(tri0.v1,tri1.v1,tri2.v1,tri3.v1,p1.x,p1.y,p1.z);
  364. transpose(tri0.v2,tri1.v2,tri2.v2,tri3.v2,p2.x,p2.y,p2.z);
  365. }
  366. template<>
  367. __forceinline void TriangleMi<4>::gather(Vec3vf4& p0,
  368. Vec3vf4& p1,
  369. Vec3vf4& p2,
  370. const Scene *const scene,
  371. const float time) const
  372. {
  373. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(0)); // in mblur mode all geometries are identical
  374. float ftime;
  375. const int itime = mesh->timeSegment(time, ftime);
  376. Vec3vf4 a0,a1,a2; gather(a0,a1,a2,mesh,scene,itime);
  377. Vec3vf4 b0,b1,b2; gather(b0,b1,b2,mesh,scene,itime+1);
  378. p0 = lerp(a0,b0,vfloat4(ftime));
  379. p1 = lerp(a1,b1,vfloat4(ftime));
  380. p2 = lerp(a2,b2,vfloat4(ftime));
  381. }
  382. }
  383. template<int M>
  384. typename TriangleMi<M>::Type TriangleMi<M>::type;
  385. typedef TriangleMi<4> Triangle4i;
  386. }