trianglev_mb.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /* Stores the vertices of M triangles in struct of array layout */
  8. template<int M>
  9. struct TriangleMvMB
  10. {
  11. public:
  12. struct Type : public PrimitiveType
  13. {
  14. const char* name() const;
  15. size_t sizeActive(const char* This) const;
  16. size_t sizeTotal(const char* This) const;
  17. size_t getBytes(const char* This) const;
  18. };
  19. static Type type;
  20. public:
  21. /* primitive supports single time segments */
  22. static const bool singleTimeSegment = true;
  23. /* Returns maximum number of stored triangles */
  24. static __forceinline size_t max_size() { return M; }
  25. /* Returns required number of primitive blocks for N primitives */
  26. static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); }
  27. public:
  28. /* Default constructor */
  29. __forceinline TriangleMvMB() {}
  30. /* Construction from vertices and IDs */
  31. __forceinline TriangleMvMB(const Vec3vf<M>& a0, const Vec3vf<M>& a1,
  32. const Vec3vf<M>& b0, const Vec3vf<M>& b1,
  33. const Vec3vf<M>& c0, const Vec3vf<M>& c1,
  34. const vuint<M>& geomIDs, const vuint<M>& primIDs)
  35. : v0(a0), v1(b0), v2(c0), dv0(a1-a0), dv1(b1-b0), dv2(c1-c0), geomIDs(geomIDs), primIDs(primIDs) {}
  36. /* Returns a mask that tells which triangles are valid */
  37. __forceinline vbool<M> valid() const { return geomIDs != vuint<M>(-1); }
  38. /* Returns if the specified triangle is valid */
  39. __forceinline bool valid(const size_t i) const { assert(i<M); return geomIDs[i] != -1; }
  40. /* Returns the number of stored triangles */
  41. __forceinline size_t size() const { return bsf(~movemask(valid())); }
  42. /* Returns the geometry IDs */
  43. __forceinline vuint<M>& geomID() { return geomIDs; }
  44. __forceinline const vuint<M>& geomID() const { return geomIDs; }
  45. __forceinline unsigned int geomID(const size_t i) const { assert(i<M); return geomIDs[i]; }
  46. /* Returns the primitive IDs */
  47. __forceinline vuint<M>& primID() { return primIDs; }
  48. __forceinline const vuint<M>& primID() const { return primIDs; }
  49. __forceinline unsigned int primID(const size_t i) const { assert(i<M); return primIDs[i]; }
  50. /* Calculate the bounds of the triangles at t0 */
  51. __forceinline BBox3fa bounds0() const
  52. {
  53. Vec3vf<M> lower = min(v0,v1,v2);
  54. Vec3vf<M> upper = max(v0,v1,v2);
  55. const vbool<M> mask = valid();
  56. lower.x = select(mask,lower.x,vfloat<M>(pos_inf));
  57. lower.y = select(mask,lower.y,vfloat<M>(pos_inf));
  58. lower.z = select(mask,lower.z,vfloat<M>(pos_inf));
  59. upper.x = select(mask,upper.x,vfloat<M>(neg_inf));
  60. upper.y = select(mask,upper.y,vfloat<M>(neg_inf));
  61. upper.z = select(mask,upper.z,vfloat<M>(neg_inf));
  62. return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)),
  63. Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z)));
  64. }
  65. /* Calculate the bounds of the triangles at t1 */
  66. __forceinline BBox3fa bounds1() const
  67. {
  68. const Vec3vf<M> p0 = v0+dv0;
  69. const Vec3vf<M> p1 = v1+dv1;
  70. const Vec3vf<M> p2 = v2+dv2;
  71. Vec3vf<M> lower = min(p0,p1,p2);
  72. Vec3vf<M> upper = max(p0,p1,p2);
  73. const vbool<M> mask = valid();
  74. lower.x = select(mask,lower.x,vfloat<M>(pos_inf));
  75. lower.y = select(mask,lower.y,vfloat<M>(pos_inf));
  76. lower.z = select(mask,lower.z,vfloat<M>(pos_inf));
  77. upper.x = select(mask,upper.x,vfloat<M>(neg_inf));
  78. upper.y = select(mask,upper.y,vfloat<M>(neg_inf));
  79. upper.z = select(mask,upper.z,vfloat<M>(neg_inf));
  80. return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)),
  81. Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z)));
  82. }
  83. /* Calculate the linear bounds of the primitive */
  84. __forceinline LBBox3fa linearBounds() const {
  85. return LBBox3fa(bounds0(),bounds1());
  86. }
  87. /* Fill triangle from triangle list */
  88. __forceinline LBBox3fa fillMB(const PrimRef* prims, size_t& begin, size_t end, Scene* scene, size_t itime)
  89. {
  90. vuint<M> vgeomID = -1, vprimID = -1;
  91. Vec3vf<M> va0 = zero, vb0 = zero, vc0 = zero;
  92. Vec3vf<M> va1 = zero, vb1 = zero, vc1 = zero;
  93. BBox3fa bounds0 = empty;
  94. BBox3fa bounds1 = empty;
  95. for (size_t i=0; i<M && begin<end; i++, begin++)
  96. {
  97. const PrimRef& prim = prims[begin];
  98. const unsigned geomID = prim.geomID();
  99. const unsigned primID = prim.primID();
  100. const TriangleMesh* __restrict__ const mesh = scene->get<TriangleMesh>(geomID);
  101. const TriangleMesh::Triangle& tri = mesh->triangle(primID);
  102. const Vec3fa& a0 = mesh->vertex(tri.v[0],itime+0); bounds0.extend(a0);
  103. const Vec3fa& a1 = mesh->vertex(tri.v[0],itime+1); bounds1.extend(a1);
  104. const Vec3fa& b0 = mesh->vertex(tri.v[1],itime+0); bounds0.extend(b0);
  105. const Vec3fa& b1 = mesh->vertex(tri.v[1],itime+1); bounds1.extend(b1);
  106. const Vec3fa& c0 = mesh->vertex(tri.v[2],itime+0); bounds0.extend(c0);
  107. const Vec3fa& c1 = mesh->vertex(tri.v[2],itime+1); bounds1.extend(c1);
  108. vgeomID [i] = geomID;
  109. vprimID [i] = primID;
  110. va0.x[i] = a0.x; va0.y[i] = a0.y; va0.z[i] = a0.z;
  111. va1.x[i] = a1.x; va1.y[i] = a1.y; va1.z[i] = a1.z;
  112. vb0.x[i] = b0.x; vb0.y[i] = b0.y; vb0.z[i] = b0.z;
  113. vb1.x[i] = b1.x; vb1.y[i] = b1.y; vb1.z[i] = b1.z;
  114. vc0.x[i] = c0.x; vc0.y[i] = c0.y; vc0.z[i] = c0.z;
  115. vc1.x[i] = c1.x; vc1.y[i] = c1.y; vc1.z[i] = c1.z;
  116. }
  117. new (this) TriangleMvMB(va0,va1,vb0,vb1,vc0,vc1,vgeomID,vprimID);
  118. return LBBox3fa(bounds0,bounds1);
  119. }
  120. /* Fill triangle from triangle list */
  121. __forceinline LBBox3fa fillMB(const PrimRefMB* prims, size_t& begin, size_t end, Scene* scene, const BBox1f time_range)
  122. {
  123. vuint<M> vgeomID = -1, vprimID = -1;
  124. Vec3vf<M> va0 = zero, vb0 = zero, vc0 = zero;
  125. Vec3vf<M> va1 = zero, vb1 = zero, vc1 = zero;
  126. LBBox3fa allBounds = empty;
  127. for (size_t i=0; i<M && begin<end; i++, begin++)
  128. {
  129. const PrimRefMB& prim = prims[begin];
  130. const unsigned geomID = prim.geomID();
  131. const unsigned primID = prim.primID();
  132. const TriangleMesh* const mesh = scene->get<TriangleMesh>(geomID);
  133. const range<int> itime_range = mesh->timeSegmentRange(time_range);
  134. assert(itime_range.size() == 1);
  135. const int ilower = itime_range.begin();
  136. const TriangleMesh::Triangle& tri = mesh->triangle(primID);
  137. allBounds.extend(mesh->linearBounds(primID, time_range));
  138. const Vec3fa& a0 = mesh->vertex(tri.v[0],ilower+0);
  139. const Vec3fa& a1 = mesh->vertex(tri.v[0],ilower+1);
  140. const Vec3fa& b0 = mesh->vertex(tri.v[1],ilower+0);
  141. const Vec3fa& b1 = mesh->vertex(tri.v[1],ilower+1);
  142. const Vec3fa& c0 = mesh->vertex(tri.v[2],ilower+0);
  143. const Vec3fa& c1 = mesh->vertex(tri.v[2],ilower+1);
  144. const BBox1f time_range_v(mesh->timeStep(ilower+0),mesh->timeStep(ilower+1));
  145. auto a01 = globalLinear(std::make_pair(a0,a1),time_range_v);
  146. auto b01 = globalLinear(std::make_pair(b0,b1),time_range_v);
  147. auto c01 = globalLinear(std::make_pair(c0,c1),time_range_v);
  148. vgeomID [i] = geomID;
  149. vprimID [i] = primID;
  150. va0.x[i] = a01.first .x; va0.y[i] = a01.first .y; va0.z[i] = a01.first .z;
  151. va1.x[i] = a01.second.x; va1.y[i] = a01.second.y; va1.z[i] = a01.second.z;
  152. vb0.x[i] = b01.first .x; vb0.y[i] = b01.first .y; vb0.z[i] = b01.first .z;
  153. vb1.x[i] = b01.second.x; vb1.y[i] = b01.second.y; vb1.z[i] = b01.second.z;
  154. vc0.x[i] = c01.first .x; vc0.y[i] = c01.first .y; vc0.z[i] = c01.first .z;
  155. vc1.x[i] = c01.second.x; vc1.y[i] = c01.second.y; vc1.z[i] = c01.second.z;
  156. }
  157. new (this) TriangleMvMB(va0,va1,vb0,vb1,vc0,vc1,vgeomID,vprimID);
  158. return allBounds;
  159. }
  160. public:
  161. Vec3vf<M> v0; // 1st vertex of the triangles
  162. Vec3vf<M> v1; // 2nd vertex of the triangles
  163. Vec3vf<M> v2; // 3rd vertex of the triangles
  164. Vec3vf<M> dv0; // difference vector between time steps t0 and t1 for first vertex
  165. Vec3vf<M> dv1; // difference vector between time steps t0 and t1 for second vertex
  166. Vec3vf<M> dv2; // difference vector between time steps t0 and t1 for third vertex
  167. private:
  168. vuint<M> geomIDs; // geometry ID
  169. vuint<M> primIDs; // primitive ID
  170. };
  171. template<int M>
  172. typename TriangleMvMB<M>::Type TriangleMvMB<M>::type;
  173. typedef TriangleMvMB<4> Triangle4vMB;
  174. }