scene_triangle_mesh.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /*! returns i'th vertex of for specified time */
  113. __forceinline Vec3fa vertex(size_t i, float time) const
  114. {
  115. float ftime;
  116. const size_t itime = timeSegment(time, ftime);
  117. const float t0 = 1.0f - ftime;
  118. const float t1 = ftime;
  119. Vec3fa v0 = vertex(i, itime+0);
  120. Vec3fa v1 = vertex(i, itime+1);
  121. return madd(Vec3fa(t0),v0,t1*v1);
  122. }
  123. /*! calculates the bounds of the i'th triangle */
  124. __forceinline BBox3fa bounds(size_t i) const
  125. {
  126. const Triangle& tri = triangle(i);
  127. const Vec3fa v0 = vertex(tri.v[0]);
  128. const Vec3fa v1 = vertex(tri.v[1]);
  129. const Vec3fa v2 = vertex(tri.v[2]);
  130. return BBox3fa(min(v0,v1,v2),max(v0,v1,v2));
  131. }
  132. /*! calculates the bounds of the i'th triangle at the itime'th timestep */
  133. __forceinline BBox3fa bounds(size_t i, size_t itime) const
  134. {
  135. const Triangle& tri = triangle(i);
  136. const Vec3fa v0 = vertex(tri.v[0],itime);
  137. const Vec3fa v1 = vertex(tri.v[1],itime);
  138. const Vec3fa v2 = vertex(tri.v[2],itime);
  139. return BBox3fa(min(v0,v1,v2),max(v0,v1,v2));
  140. }
  141. /*! check if the i'th primitive is valid at the itime'th timestep */
  142. __forceinline bool valid(size_t i, size_t itime) const {
  143. return valid(i, make_range(itime, itime));
  144. }
  145. /*! check if the i'th primitive is valid between the specified time range */
  146. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  147. {
  148. const Triangle& tri = triangle(i);
  149. if (unlikely(tri.v[0] >= numVertices())) return false;
  150. if (unlikely(tri.v[1] >= numVertices())) return false;
  151. if (unlikely(tri.v[2] >= numVertices())) return false;
  152. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
  153. {
  154. if (!isvalid(vertex(tri.v[0],itime))) return false;
  155. if (!isvalid(vertex(tri.v[1],itime))) return false;
  156. if (!isvalid(vertex(tri.v[2],itime))) return false;
  157. }
  158. return true;
  159. }
  160. /*! calculates the linear bounds of the i'th primitive at the itimeGlobal'th time segment */
  161. __forceinline LBBox3fa linearBounds(size_t i, size_t itime) const {
  162. return LBBox3fa(bounds(i,itime+0),bounds(i,itime+1));
  163. }
  164. /*! calculates the build bounds of the i'th primitive, if it's valid */
  165. __forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
  166. {
  167. const Triangle& tri = triangle(i);
  168. if (unlikely(tri.v[0] >= numVertices())) return false;
  169. if (unlikely(tri.v[1] >= numVertices())) return false;
  170. if (unlikely(tri.v[2] >= numVertices())) return false;
  171. for (size_t t=0; t<numTimeSteps; t++)
  172. {
  173. const Vec3fa v0 = vertex(tri.v[0],t);
  174. const Vec3fa v1 = vertex(tri.v[1],t);
  175. const Vec3fa v2 = vertex(tri.v[2],t);
  176. if (unlikely(!isvalid(v0) || !isvalid(v1) || !isvalid(v2)))
  177. return false;
  178. }
  179. if (likely(bbox))
  180. *bbox = bounds(i);
  181. return true;
  182. }
  183. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  184. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  185. {
  186. const Triangle& tri = triangle(i);
  187. if (unlikely(tri.v[0] >= numVertices())) return false;
  188. if (unlikely(tri.v[1] >= numVertices())) return false;
  189. if (unlikely(tri.v[2] >= numVertices())) return false;
  190. assert(itime+1 < numTimeSteps);
  191. const Vec3fa a0 = vertex(tri.v[0],itime+0); if (unlikely(!isvalid(a0))) return false;
  192. const Vec3fa a1 = vertex(tri.v[1],itime+0); if (unlikely(!isvalid(a1))) return false;
  193. const Vec3fa a2 = vertex(tri.v[2],itime+0); if (unlikely(!isvalid(a2))) return false;
  194. const Vec3fa b0 = vertex(tri.v[0],itime+1); if (unlikely(!isvalid(b0))) return false;
  195. const Vec3fa b1 = vertex(tri.v[1],itime+1); if (unlikely(!isvalid(b1))) return false;
  196. const Vec3fa b2 = vertex(tri.v[2],itime+1); if (unlikely(!isvalid(b2))) return false;
  197. /* use bounds of first time step in builder */
  198. bbox = BBox3fa(min(a0,a1,a2),max(a0,a1,a2));
  199. return true;
  200. }
  201. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  202. __forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
  203. return LBBox3fa([&] (size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
  204. }
  205. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  206. __forceinline bool linearBounds(size_t i, const BBox1f& dt, LBBox3fa& bbox) const {
  207. if (!valid(i, timeSegmentRange(dt))) return false;
  208. bbox = linearBounds(i, dt);
  209. return true;
  210. }
  211. /*! get fast access to first vertex buffer */
  212. __forceinline float * getCompactVertexArray () const {
  213. return (float*) vertices0.getPtr();
  214. }
  215. /* gets version info of topology */
  216. unsigned int getTopologyVersion() const {
  217. return triangles.modCounter;
  218. }
  219. /* returns true if topology changed */
  220. bool topologyChanged(unsigned int otherVersion) const {
  221. return triangles.isModified(otherVersion); // || numPrimitivesChanged;
  222. }
  223. /* returns the projected area */
  224. __forceinline float projectedPrimitiveArea(const size_t i) const {
  225. const Triangle& tri = triangle(i);
  226. const Vec3fa v0 = vertex(tri.v[0]);
  227. const Vec3fa v1 = vertex(tri.v[1]);
  228. const Vec3fa v2 = vertex(tri.v[2]);
  229. return areaProjectedTriangle(v0,v1,v2);
  230. }
  231. public:
  232. BufferView<Triangle> triangles; //!< array of triangles
  233. BufferView<Vec3fa> vertices0; //!< fast access to first vertex buffer
  234. Device::vector<BufferView<Vec3fa>> vertices = device; //!< vertex array for each timestep
  235. Device::vector<RawBufferView> vertexAttribs = device; //!< vertex attributes
  236. };
  237. namespace isa
  238. {
  239. struct TriangleMeshISA : public TriangleMesh
  240. {
  241. TriangleMeshISA (Device* device)
  242. : TriangleMesh(device) {}
  243. LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const {
  244. return linearBounds(primID,time_range);
  245. }
  246. PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const
  247. {
  248. PrimInfo pinfo(empty);
  249. for (size_t j=r.begin(); j<r.end(); j++)
  250. {
  251. BBox3fa bounds = empty;
  252. if (!buildBounds(j,&bounds)) continue;
  253. const PrimRef prim(bounds,geomID,unsigned(j));
  254. pinfo.add_center2(prim);
  255. prims[k++] = prim;
  256. }
  257. return pinfo;
  258. }
  259. PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
  260. {
  261. PrimInfo pinfo(empty);
  262. for (size_t j=r.begin(); j<r.end(); j++)
  263. {
  264. BBox3fa bounds = empty;
  265. if (!buildBounds(j,itime,bounds)) continue;
  266. const PrimRef prim(bounds,geomID,unsigned(j));
  267. pinfo.add_center2(prim);
  268. prims[k++] = prim;
  269. }
  270. return pinfo;
  271. }
  272. PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const
  273. {
  274. PrimInfo pinfo(empty);
  275. const BBox1f t0t1 = BBox1f::intersect(getTimeRange(), time_range);
  276. if (t0t1.empty()) return pinfo;
  277. for (size_t j = r.begin(); j < r.end(); j++) {
  278. LBBox3fa lbounds = empty;
  279. if (!linearBounds(j, t0t1, lbounds))
  280. continue;
  281. const PrimRef prim(lbounds.bounds(), geomID, unsigned(j));
  282. pinfo.add_center2(prim);
  283. prims[k++] = prim;
  284. }
  285. return pinfo;
  286. }
  287. PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
  288. {
  289. PrimInfoMB pinfo(empty);
  290. for (size_t j=r.begin(); j<r.end(); j++)
  291. {
  292. if (!valid(j, timeSegmentRange(t0t1))) continue;
  293. const PrimRefMB prim(linearBounds(j,t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(j));
  294. pinfo.add_primref(prim);
  295. prims[k++] = prim;
  296. }
  297. return pinfo;
  298. }
  299. };
  300. }
  301. DECLARE_ISA_FUNCTION(TriangleMesh*, createTriangleMesh, Device*);
  302. }