scene_quad_mesh.h 14 KB

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