scene_quad_mesh.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. uint32_t v[4];
  17. /*! outputs triangle indices */
  18. __forceinline friend embree_ostream operator<<(embree_ostream cout, const Quad& q) {
  19. return cout << "Quad {" << q.v[0] << ", " << q.v[1] << ", " << q.v[2] << ", " << q.v[3] << " }";
  20. }
  21. };
  22. public:
  23. /*! quad mesh construction */
  24. QuadMesh (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. const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>(int(valueCount));
  67. const size_t ofs = i*sizeof(float);
  68. const Quad& tri = quad(primID);
  69. const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[0]*stride+ofs]);
  70. const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[1]*stride+ofs]);
  71. const vfloat<N> p2 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[2]*stride+ofs]);
  72. const vfloat<N> p3 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[3]*stride+ofs]);
  73. const vbool<N> left = u+v <= 1.0f;
  74. const vfloat<N> Q0 = select(left,p0,p2);
  75. const vfloat<N> Q1 = select(left,p1,p3);
  76. const vfloat<N> Q2 = select(left,p3,p1);
  77. const vfloat<N> U = select(left,u,vfloat<N>(1.0f)-u);
  78. const vfloat<N> V = select(left,v,vfloat<N>(1.0f)-v);
  79. const vfloat<N> W = 1.0f-U-V;
  80. if (P) {
  81. mem<vfloat<N>>::storeu(valid,P+i,madd(W,Q0,madd(U,Q1,V*Q2)));
  82. }
  83. if (dPdu) {
  84. assert(dPdu); mem<vfloat<N>>::storeu(valid,dPdu+i,select(left,Q1-Q0,Q0-Q1));
  85. assert(dPdv); mem<vfloat<N>>::storeu(valid,dPdv+i,select(left,Q2-Q0,Q0-Q2));
  86. }
  87. if (ddPdudu) {
  88. assert(ddPdudu); mem<vfloat<N>>::storeu(valid,ddPdudu+i,vfloat<N>(zero));
  89. assert(ddPdvdv); mem<vfloat<N>>::storeu(valid,ddPdvdv+i,vfloat<N>(zero));
  90. assert(ddPdudv); mem<vfloat<N>>::storeu(valid,ddPdudv+i,vfloat<N>(zero));
  91. }
  92. }
  93. }
  94. public:
  95. /*! returns number of vertices */
  96. __forceinline size_t numVertices() const {
  97. return vertices[0].size();
  98. }
  99. /*! returns i'th quad */
  100. __forceinline const Quad& quad(size_t i) const {
  101. return quads[i];
  102. }
  103. /*! returns i'th vertex of itime'th timestep */
  104. __forceinline const Vec3fa vertex(size_t i) const {
  105. return vertices0[i];
  106. }
  107. /*! returns i'th vertex of itime'th timestep */
  108. __forceinline const char* vertexPtr(size_t i) const {
  109. return vertices0.getPtr(i);
  110. }
  111. /*! returns i'th vertex of itime'th timestep */
  112. __forceinline const Vec3fa vertex(size_t i, size_t itime) const {
  113. return vertices[itime][i];
  114. }
  115. /*! returns i'th vertex of itime'th timestep */
  116. __forceinline const char* vertexPtr(size_t i, size_t itime) const {
  117. return vertices[itime].getPtr(i);
  118. }
  119. /*! calculates the bounds of the i'th quad */
  120. __forceinline BBox3fa bounds(size_t i) const
  121. {
  122. const Quad& q = quad(i);
  123. const Vec3fa v0 = vertex(q.v[0]);
  124. const Vec3fa v1 = vertex(q.v[1]);
  125. const Vec3fa v2 = vertex(q.v[2]);
  126. const Vec3fa v3 = vertex(q.v[3]);
  127. return BBox3fa(min(v0,v1,v2,v3),max(v0,v1,v2,v3));
  128. }
  129. /*! calculates the bounds of the i'th quad at the itime'th timestep */
  130. __forceinline BBox3fa bounds(size_t i, size_t itime) const
  131. {
  132. const Quad& q = quad(i);
  133. const Vec3fa v0 = vertex(q.v[0],itime);
  134. const Vec3fa v1 = vertex(q.v[1],itime);
  135. const Vec3fa v2 = vertex(q.v[2],itime);
  136. const Vec3fa v3 = vertex(q.v[3],itime);
  137. return BBox3fa(min(v0,v1,v2,v3),max(v0,v1,v2,v3));
  138. }
  139. /*! check if the i'th primitive is valid at the itime'th timestep */
  140. __forceinline bool valid(size_t i, size_t itime) const {
  141. return valid(i, make_range(itime, itime));
  142. }
  143. /*! check if the i'th primitive is valid between the specified time range */
  144. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  145. {
  146. const Quad& q = quad(i);
  147. if (unlikely(q.v[0] >= numVertices())) return false;
  148. if (unlikely(q.v[1] >= numVertices())) return false;
  149. if (unlikely(q.v[2] >= numVertices())) return false;
  150. if (unlikely(q.v[3] >= numVertices())) return false;
  151. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
  152. {
  153. if (!isvalid(vertex(q.v[0],itime))) return false;
  154. if (!isvalid(vertex(q.v[1],itime))) return false;
  155. if (!isvalid(vertex(q.v[2],itime))) return false;
  156. if (!isvalid(vertex(q.v[3],itime))) return false;
  157. }
  158. return true;
  159. }
  160. /*! calculates the linear bounds of the i'th quad 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 Quad& q = quad(i);
  168. if (q.v[0] >= numVertices()) return false;
  169. if (q.v[1] >= numVertices()) return false;
  170. if (q.v[2] >= numVertices()) return false;
  171. if (q.v[3] >= numVertices()) return false;
  172. for (unsigned int t=0; t<numTimeSteps; t++)
  173. {
  174. const Vec3fa v0 = vertex(q.v[0],t);
  175. const Vec3fa v1 = vertex(q.v[1],t);
  176. const Vec3fa v2 = vertex(q.v[2],t);
  177. const Vec3fa v3 = vertex(q.v[3],t);
  178. if (unlikely(!isvalid(v0) || !isvalid(v1) || !isvalid(v2) || !isvalid(v3)))
  179. return false;
  180. }
  181. if (bbox)
  182. *bbox = bounds(i);
  183. return true;
  184. }
  185. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  186. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  187. {
  188. const Quad& q = quad(i);
  189. if (unlikely(q.v[0] >= numVertices())) return false;
  190. if (unlikely(q.v[1] >= numVertices())) return false;
  191. if (unlikely(q.v[2] >= numVertices())) return false;
  192. if (unlikely(q.v[3] >= numVertices())) return false;
  193. assert(itime+1 < numTimeSteps);
  194. const Vec3fa a0 = vertex(q.v[0],itime+0); if (unlikely(!isvalid(a0))) return false;
  195. const Vec3fa a1 = vertex(q.v[1],itime+0); if (unlikely(!isvalid(a1))) return false;
  196. const Vec3fa a2 = vertex(q.v[2],itime+0); if (unlikely(!isvalid(a2))) return false;
  197. const Vec3fa a3 = vertex(q.v[3],itime+0); if (unlikely(!isvalid(a3))) return false;
  198. const Vec3fa b0 = vertex(q.v[0],itime+1); if (unlikely(!isvalid(b0))) return false;
  199. const Vec3fa b1 = vertex(q.v[1],itime+1); if (unlikely(!isvalid(b1))) return false;
  200. const Vec3fa b2 = vertex(q.v[2],itime+1); if (unlikely(!isvalid(b2))) return false;
  201. const Vec3fa b3 = vertex(q.v[3],itime+1); if (unlikely(!isvalid(b3))) return false;
  202. /* use bounds of first time step in builder */
  203. bbox = BBox3fa(min(a0,a1,a2,a3),max(a0,a1,a2,a3));
  204. return true;
  205. }
  206. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  207. __forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
  208. return LBBox3fa([&] (size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
  209. }
  210. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  211. __forceinline bool linearBounds(size_t i, const BBox1f& dt, LBBox3fa& bbox) const
  212. {
  213. if (!valid(i, timeSegmentRange(dt))) return false;
  214. bbox = linearBounds(i, dt);
  215. return true;
  216. }
  217. /*! get fast access to first vertex buffer */
  218. __forceinline float * getCompactVertexArray () const {
  219. return (float*) vertices0.getPtr();
  220. }
  221. /* gets version info of topology */
  222. unsigned int getTopologyVersion() const {
  223. return quads.modCounter;
  224. }
  225. /* returns true if topology changed */
  226. bool topologyChanged(unsigned int otherVersion) const {
  227. return quads.isModified(otherVersion); // || numPrimitivesChanged;
  228. }
  229. /* returns the projected area */
  230. __forceinline float projectedPrimitiveArea(const size_t i) const {
  231. const Quad& q = quad(i);
  232. const Vec3fa v0 = vertex(q.v[0]);
  233. const Vec3fa v1 = vertex(q.v[1]);
  234. const Vec3fa v2 = vertex(q.v[2]);
  235. const Vec3fa v3 = vertex(q.v[3]);
  236. return areaProjectedTriangle(v0,v1,v3) +
  237. areaProjectedTriangle(v1,v2,v3);
  238. }
  239. public:
  240. BufferView<Quad> quads; //!< array of quads
  241. BufferView<Vec3fa> vertices0; //!< fast access to first vertex buffer
  242. vector<BufferView<Vec3fa>> vertices; //!< vertex array for each timestep
  243. vector<BufferView<char>> vertexAttribs; //!< vertex attribute buffers
  244. };
  245. namespace isa
  246. {
  247. struct QuadMeshISA : public QuadMesh
  248. {
  249. QuadMeshISA (Device* device)
  250. : QuadMesh(device) {}
  251. PrimInfo createPrimRefArray(mvector<PrimRef>& prims, const range<size_t>& r, size_t k, unsigned int geomID) const
  252. {
  253. PrimInfo pinfo(empty);
  254. for (size_t j=r.begin(); j<r.end(); j++)
  255. {
  256. BBox3fa bounds = empty;
  257. if (!buildBounds(j,&bounds)) continue;
  258. const PrimRef prim(bounds,geomID,unsigned(j));
  259. pinfo.add_center2(prim);
  260. prims[k++] = prim;
  261. }
  262. return pinfo;
  263. }
  264. PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
  265. {
  266. PrimInfo pinfo(empty);
  267. for (size_t j=r.begin(); j<r.end(); j++)
  268. {
  269. BBox3fa bounds = empty;
  270. if (!buildBounds(j,itime,bounds)) continue;
  271. const PrimRef prim(bounds,geomID,unsigned(j));
  272. pinfo.add_center2(prim);
  273. prims[k++] = prim;
  274. }
  275. return pinfo;
  276. }
  277. PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
  278. {
  279. PrimInfoMB pinfo(empty);
  280. for (size_t j=r.begin(); j<r.end(); j++)
  281. {
  282. if (!valid(j, timeSegmentRange(t0t1))) continue;
  283. const PrimRefMB prim(linearBounds(j,t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(j));
  284. pinfo.add_primref(prim);
  285. prims[k++] = prim;
  286. }
  287. return pinfo;
  288. }
  289. };
  290. }
  291. DECLARE_ISA_FUNCTION(QuadMesh*, createQuadMesh, Device*);
  292. }