scene_triangle_mesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "geometry.h"
  5. #include "buffer.h"
  6. namespace embree
  7. {
  8. /*! Triangle Mesh */
  9. struct TriangleMesh : public Geometry
  10. {
  11. /*! type of this geometry */
  12. static const Geometry::GTypeMask geom_type = Geometry::MTY_TRIANGLE_MESH;
  13. /*! triangle indices */
  14. struct Triangle
  15. {
  16. uint32_t v[3];
  17. /*! outputs triangle indices */
  18. __forceinline friend embree_ostream operator<<(embree_ostream cout, const Triangle& t) {
  19. return cout << "Triangle { " << t.v[0] << ", " << t.v[1] << ", " << t.v[2] << " }";
  20. }
  21. };
  22. public:
  23. /*! triangle mesh construction */
  24. TriangleMesh (Device* device);
  25. /* geometry interface */
  26. public:
  27. void setMask(unsigned mask);
  28. void setNumTimeSteps (unsigned int numTimeSteps);
  29. void setVertexAttributeCount (unsigned int N);
  30. void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num);
  31. void* getBuffer(RTCBufferType type, unsigned int slot);
  32. void updateBuffer(RTCBufferType type, unsigned int slot);
  33. void commit();
  34. bool verify();
  35. void interpolate(const RTCInterpolateArguments* const args);
  36. void addElementsToCount (GeometryCounts & counts) const;
  37. template<int N>
  38. void interpolate_impl(const RTCInterpolateArguments* const args)
  39. {
  40. unsigned int primID = args->primID;
  41. float u = args->u;
  42. float v = args->v;
  43. RTCBufferType bufferType = args->bufferType;
  44. unsigned int bufferSlot = args->bufferSlot;
  45. float* P = args->P;
  46. float* dPdu = args->dPdu;
  47. float* dPdv = args->dPdv;
  48. float* ddPdudu = args->ddPdudu;
  49. float* ddPdvdv = args->ddPdvdv;
  50. float* ddPdudv = args->ddPdudv;
  51. unsigned int valueCount = args->valueCount;
  52. /* calculate base pointer and stride */
  53. assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
  54. (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
  55. const char* src = nullptr;
  56. size_t stride = 0;
  57. if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
  58. src = vertexAttribs[bufferSlot].getPtr();
  59. stride = vertexAttribs[bufferSlot].getStride();
  60. } else {
  61. src = vertices[bufferSlot].getPtr();
  62. stride = vertices[bufferSlot].getStride();
  63. }
  64. for (unsigned int i=0; i<valueCount; i+=N)
  65. {
  66. size_t ofs = i*sizeof(float);
  67. const float w = 1.0f-u-v;
  68. const Triangle& tri = triangle(primID);
  69. const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>(int(valueCount));
  70. const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[0]*stride+ofs]);
  71. const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[1]*stride+ofs]);
  72. const vfloat<N> p2 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[2]*stride+ofs]);
  73. if (P) {
  74. mem<vfloat<N>>::storeu(valid,P+i,madd(w,p0,madd(u,p1,v*p2)));
  75. }
  76. if (dPdu) {
  77. assert(dPdu); mem<vfloat<N>>::storeu(valid,dPdu+i,p1-p0);
  78. assert(dPdv); mem<vfloat<N>>::storeu(valid,dPdv+i,p2-p0);
  79. }
  80. if (ddPdudu) {
  81. assert(ddPdudu); mem<vfloat<N>>::storeu(valid,ddPdudu+i,vfloat<N>(zero));
  82. assert(ddPdvdv); mem<vfloat<N>>::storeu(valid,ddPdvdv+i,vfloat<N>(zero));
  83. assert(ddPdudv); mem<vfloat<N>>::storeu(valid,ddPdudv+i,vfloat<N>(zero));
  84. }
  85. }
  86. }
  87. public:
  88. /*! returns number of vertices */
  89. __forceinline size_t numVertices() const {
  90. return vertices[0].size();
  91. }
  92. /*! returns i'th triangle*/
  93. __forceinline const Triangle& triangle(size_t i) const {
  94. return triangles[i];
  95. }
  96. /*! returns i'th vertex of the first time step */
  97. __forceinline const Vec3fa vertex(size_t i) const {
  98. return vertices0[i];
  99. }
  100. /*! returns i'th vertex of the first time step */
  101. __forceinline const char* vertexPtr(size_t i) const {
  102. return vertices0.getPtr(i);
  103. }
  104. /*! returns i'th vertex of itime'th timestep */
  105. __forceinline const Vec3fa vertex(size_t i, size_t itime) const {
  106. return vertices[itime][i];
  107. }
  108. /*! returns i'th vertex of itime'th timestep */
  109. __forceinline const char* vertexPtr(size_t i, size_t itime) const {
  110. return vertices[itime].getPtr(i);
  111. }
  112. /*! calculates the bounds of the i'th triangle */
  113. __forceinline BBox3fa bounds(size_t i) const
  114. {
  115. const Triangle& tri = triangle(i);
  116. const Vec3fa v0 = vertex(tri.v[0]);
  117. const Vec3fa v1 = vertex(tri.v[1]);
  118. const Vec3fa v2 = vertex(tri.v[2]);
  119. return BBox3fa(min(v0,v1,v2),max(v0,v1,v2));
  120. }
  121. /*! calculates the bounds of the i'th triangle at the itime'th timestep */
  122. __forceinline BBox3fa bounds(size_t i, size_t itime) const
  123. {
  124. const Triangle& tri = triangle(i);
  125. const Vec3fa v0 = vertex(tri.v[0],itime);
  126. const Vec3fa v1 = vertex(tri.v[1],itime);
  127. const Vec3fa v2 = vertex(tri.v[2],itime);
  128. return BBox3fa(min(v0,v1,v2),max(v0,v1,v2));
  129. }
  130. /*! check if the i'th primitive is valid at the itime'th timestep */
  131. __forceinline bool valid(size_t i, size_t itime) const {
  132. return valid(i, make_range(itime, itime));
  133. }
  134. /*! check if the i'th primitive is valid between the specified time range */
  135. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  136. {
  137. const Triangle& tri = triangle(i);
  138. if (unlikely(tri.v[0] >= numVertices())) return false;
  139. if (unlikely(tri.v[1] >= numVertices())) return false;
  140. if (unlikely(tri.v[2] >= numVertices())) return false;
  141. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
  142. {
  143. if (!isvalid(vertex(tri.v[0],itime))) return false;
  144. if (!isvalid(vertex(tri.v[1],itime))) return false;
  145. if (!isvalid(vertex(tri.v[2],itime))) return false;
  146. }
  147. return true;
  148. }
  149. /*! calculates the linear bounds of the i'th primitive at the itimeGlobal'th time segment */
  150. __forceinline LBBox3fa linearBounds(size_t i, size_t itime) const {
  151. return LBBox3fa(bounds(i,itime+0),bounds(i,itime+1));
  152. }
  153. /*! calculates the build bounds of the i'th primitive, if it's valid */
  154. __forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
  155. {
  156. const Triangle& tri = triangle(i);
  157. if (unlikely(tri.v[0] >= numVertices())) return false;
  158. if (unlikely(tri.v[1] >= numVertices())) return false;
  159. if (unlikely(tri.v[2] >= numVertices())) return false;
  160. for (size_t t=0; t<numTimeSteps; t++)
  161. {
  162. const Vec3fa v0 = vertex(tri.v[0],t);
  163. const Vec3fa v1 = vertex(tri.v[1],t);
  164. const Vec3fa v2 = vertex(tri.v[2],t);
  165. if (unlikely(!isvalid(v0) || !isvalid(v1) || !isvalid(v2)))
  166. return false;
  167. }
  168. if (likely(bbox))
  169. *bbox = bounds(i);
  170. return true;
  171. }
  172. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  173. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  174. {
  175. const Triangle& tri = triangle(i);
  176. if (unlikely(tri.v[0] >= numVertices())) return false;
  177. if (unlikely(tri.v[1] >= numVertices())) return false;
  178. if (unlikely(tri.v[2] >= numVertices())) return false;
  179. assert(itime+1 < numTimeSteps);
  180. const Vec3fa a0 = vertex(tri.v[0],itime+0); if (unlikely(!isvalid(a0))) return false;
  181. const Vec3fa a1 = vertex(tri.v[1],itime+0); if (unlikely(!isvalid(a1))) return false;
  182. const Vec3fa a2 = vertex(tri.v[2],itime+0); if (unlikely(!isvalid(a2))) return false;
  183. const Vec3fa b0 = vertex(tri.v[0],itime+1); if (unlikely(!isvalid(b0))) return false;
  184. const Vec3fa b1 = vertex(tri.v[1],itime+1); if (unlikely(!isvalid(b1))) return false;
  185. const Vec3fa b2 = vertex(tri.v[2],itime+1); if (unlikely(!isvalid(b2))) return false;
  186. /* use bounds of first time step in builder */
  187. bbox = BBox3fa(min(a0,a1,a2),max(a0,a1,a2));
  188. return true;
  189. }
  190. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  191. __forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
  192. return LBBox3fa([&] (size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
  193. }
  194. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  195. __forceinline bool linearBounds(size_t i, const BBox1f& dt, LBBox3fa& bbox) const {
  196. if (!valid(i, timeSegmentRange(dt))) return false;
  197. bbox = linearBounds(i, dt);
  198. return true;
  199. }
  200. /*! get fast access to first vertex buffer */
  201. __forceinline float * getCompactVertexArray () const {
  202. return (float*) vertices0.getPtr();
  203. }
  204. /* gets version info of topology */
  205. unsigned int getTopologyVersion() const {
  206. return triangles.modCounter;
  207. }
  208. /* returns true if topology changed */
  209. bool topologyChanged(unsigned int otherVersion) const {
  210. return triangles.isModified(otherVersion); // || numPrimitivesChanged;
  211. }
  212. /* returns the projected area */
  213. __forceinline float projectedPrimitiveArea(const size_t i) const {
  214. const Triangle& tri = triangle(i);
  215. const Vec3fa v0 = vertex(tri.v[0]);
  216. const Vec3fa v1 = vertex(tri.v[1]);
  217. const Vec3fa v2 = vertex(tri.v[2]);
  218. return areaProjectedTriangle(v0,v1,v2);
  219. }
  220. public:
  221. BufferView<Triangle> triangles; //!< array of triangles
  222. BufferView<Vec3fa> vertices0; //!< fast access to first vertex buffer
  223. vector<BufferView<Vec3fa>> vertices; //!< vertex array for each timestep
  224. vector<RawBufferView> vertexAttribs; //!< vertex attributes
  225. };
  226. namespace isa
  227. {
  228. struct TriangleMeshISA : public TriangleMesh
  229. {
  230. TriangleMeshISA (Device* device)
  231. : TriangleMesh(device) {}
  232. PrimInfo createPrimRefArray(mvector<PrimRef>& prims, const range<size_t>& r, size_t k, unsigned int geomID) const
  233. {
  234. PrimInfo pinfo(empty);
  235. for (size_t j=r.begin(); j<r.end(); j++)
  236. {
  237. BBox3fa bounds = empty;
  238. if (!buildBounds(j,&bounds)) continue;
  239. const PrimRef prim(bounds,geomID,unsigned(j));
  240. pinfo.add_center2(prim);
  241. prims[k++] = prim;
  242. }
  243. return pinfo;
  244. }
  245. PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
  246. {
  247. PrimInfo pinfo(empty);
  248. for (size_t j=r.begin(); j<r.end(); j++)
  249. {
  250. BBox3fa bounds = empty;
  251. if (!buildBounds(j,itime,bounds)) continue;
  252. const PrimRef prim(bounds,geomID,unsigned(j));
  253. pinfo.add_center2(prim);
  254. prims[k++] = prim;
  255. }
  256. return pinfo;
  257. }
  258. PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
  259. {
  260. PrimInfoMB pinfo(empty);
  261. for (size_t j=r.begin(); j<r.end(); j++)
  262. {
  263. if (!valid(j, timeSegmentRange(t0t1))) continue;
  264. const PrimRefMB prim(linearBounds(j,t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(j));
  265. pinfo.add_primref(prim);
  266. prims[k++] = prim;
  267. }
  268. return pinfo;
  269. }
  270. };
  271. }
  272. DECLARE_ISA_FUNCTION(TriangleMesh*, createTriangleMesh, Device*);
  273. }