scene_quad_mesh.h 14 KB

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