scene_grid_mesh.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /*! Grid Mesh */
  9. struct GridMesh : public Geometry
  10. {
  11. /*! type of this geometry */
  12. static const Geometry::GTypeMask geom_type = Geometry::MTY_GRID_MESH;
  13. /*! grid */
  14. struct Grid
  15. {
  16. unsigned int startVtxID;
  17. unsigned int lineVtxOffset;
  18. unsigned short resX,resY;
  19. /* border flags due to 3x3 vertex pattern */
  20. __forceinline unsigned int get3x3FlagsX(const unsigned int x) const
  21. {
  22. return (x + 2 >= (unsigned int)resX) ? (1<<15) : 0;
  23. }
  24. /* border flags due to 3x3 vertex pattern */
  25. __forceinline unsigned int get3x3FlagsY(const unsigned int y) const
  26. {
  27. return (y + 2 >= (unsigned int)resY) ? (1<<15) : 0;
  28. }
  29. /*! outputs grid structure */
  30. __forceinline friend embree_ostream operator<<(embree_ostream cout, const Grid& t) {
  31. return cout << "Grid { startVtxID " << t.startVtxID << ", lineVtxOffset " << t.lineVtxOffset << ", resX " << t.resX << ", resY " << t.resY << " }";
  32. }
  33. };
  34. public:
  35. /*! grid mesh construction */
  36. GridMesh (Device* device);
  37. /* geometry interface */
  38. public:
  39. void setMask(unsigned mask);
  40. void setNumTimeSteps (unsigned int numTimeSteps);
  41. void setVertexAttributeCount (unsigned int N);
  42. void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num);
  43. void* getBuffer(RTCBufferType type, unsigned int slot);
  44. void updateBuffer(RTCBufferType type, unsigned int slot);
  45. void commit();
  46. bool verify();
  47. void interpolate(const RTCInterpolateArguments* const args);
  48. template<int N>
  49. void interpolate_impl(const RTCInterpolateArguments* const args)
  50. {
  51. unsigned int primID = args->primID;
  52. float U = args->u;
  53. float V = args->v;
  54. /* clamp input u,v to [0;1] range */
  55. U = max(min(U,1.0f),0.0f);
  56. V = max(min(V,1.0f),0.0f);
  57. RTCBufferType bufferType = args->bufferType;
  58. unsigned int bufferSlot = args->bufferSlot;
  59. float* P = args->P;
  60. float* dPdu = args->dPdu;
  61. float* dPdv = args->dPdv;
  62. float* ddPdudu = args->ddPdudu;
  63. float* ddPdvdv = args->ddPdvdv;
  64. float* ddPdudv = args->ddPdudv;
  65. unsigned int valueCount = args->valueCount;
  66. /* calculate base pointer and stride */
  67. assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
  68. (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
  69. const char* src = nullptr;
  70. size_t stride = 0;
  71. if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
  72. src = vertexAttribs[bufferSlot].getPtr();
  73. stride = vertexAttribs[bufferSlot].getStride();
  74. } else {
  75. src = vertices[bufferSlot].getPtr();
  76. stride = vertices[bufferSlot].getStride();
  77. }
  78. const Grid& grid = grids[primID];
  79. const int grid_width = grid.resX-1;
  80. const int grid_height = grid.resY-1;
  81. const float rcp_grid_width = rcp(float(grid_width));
  82. const float rcp_grid_height = rcp(float(grid_height));
  83. const int iu = min((int)floor(U*grid_width ),grid_width);
  84. const int iv = min((int)floor(V*grid_height),grid_height);
  85. const float u = U*grid_width-float(iu);
  86. const float v = V*grid_height-float(iv);
  87. for (unsigned int i=0; i<valueCount; i+=N)
  88. {
  89. const size_t ofs = i*sizeof(float);
  90. const unsigned int idx0 = grid.startVtxID + (iv+0)*grid.lineVtxOffset + iu;
  91. const unsigned int idx1 = grid.startVtxID + (iv+1)*grid.lineVtxOffset + iu;
  92. const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>(int(valueCount));
  93. const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[(idx0+0)*stride+ofs]);
  94. const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[(idx0+1)*stride+ofs]);
  95. const vfloat<N> p2 = mem<vfloat<N>>::loadu(valid,(float*)&src[(idx1+1)*stride+ofs]);
  96. const vfloat<N> p3 = mem<vfloat<N>>::loadu(valid,(float*)&src[(idx1+0)*stride+ofs]);
  97. const vbool<N> left = u+v <= 1.0f;
  98. const vfloat<N> Q0 = select(left,p0,p2);
  99. const vfloat<N> Q1 = select(left,p1,p3);
  100. const vfloat<N> Q2 = select(left,p3,p1);
  101. const vfloat<N> U = select(left,u,vfloat<N>(1.0f)-u);
  102. const vfloat<N> V = select(left,v,vfloat<N>(1.0f)-v);
  103. const vfloat<N> W = 1.0f-U-V;
  104. if (P) {
  105. mem<vfloat<N>>::storeu(valid,P+i,madd(W,Q0,madd(U,Q1,V*Q2)));
  106. }
  107. if (dPdu) {
  108. assert(dPdu); mem<vfloat<N>>::storeu(valid,dPdu+i,select(left,Q1-Q0,Q0-Q1)*rcp_grid_width);
  109. assert(dPdv); mem<vfloat<N>>::storeu(valid,dPdv+i,select(left,Q2-Q0,Q0-Q2)*rcp_grid_height);
  110. }
  111. if (ddPdudu) {
  112. assert(ddPdudu); mem<vfloat<N>>::storeu(valid,ddPdudu+i,vfloat<N>(zero));
  113. assert(ddPdvdv); mem<vfloat<N>>::storeu(valid,ddPdvdv+i,vfloat<N>(zero));
  114. assert(ddPdudv); mem<vfloat<N>>::storeu(valid,ddPdudv+i,vfloat<N>(zero));
  115. }
  116. }
  117. }
  118. void addElementsToCount (GeometryCounts & counts) const;
  119. __forceinline unsigned int getNumSubGrids(const size_t gridID)
  120. {
  121. const Grid &g = grid(gridID);
  122. return max((unsigned int)1,((unsigned int)g.resX >> 1) * ((unsigned int)g.resY >> 1));
  123. }
  124. /*! get fast access to first vertex buffer */
  125. __forceinline float * getCompactVertexArray () const {
  126. return (float*) vertices0.getPtr();
  127. }
  128. public:
  129. /*! returns number of vertices */
  130. __forceinline size_t numVertices() const {
  131. return vertices[0].size();
  132. }
  133. /*! returns i'th grid*/
  134. __forceinline const Grid& grid(size_t i) const {
  135. return grids[i];
  136. }
  137. /*! returns i'th vertex of the first time step */
  138. __forceinline const Vec3fa vertex(size_t i) const { // FIXME: check if this does a unaligned load
  139. return vertices0[i];
  140. }
  141. /*! returns i'th vertex of the first time step */
  142. __forceinline const char* vertexPtr(size_t i) const {
  143. return vertices0.getPtr(i);
  144. }
  145. /*! returns i'th vertex of itime'th timestep */
  146. __forceinline const Vec3fa vertex(size_t i, size_t itime) const {
  147. return vertices[itime][i];
  148. }
  149. /*! returns i'th vertex of itime'th timestep */
  150. __forceinline const char* vertexPtr(size_t i, size_t itime) const {
  151. return vertices[itime].getPtr(i);
  152. }
  153. /*! returns i'th vertex of the first timestep */
  154. __forceinline size_t grid_vertex_index(const Grid& g, size_t x, size_t y) const {
  155. assert(x < (size_t)g.resX);
  156. assert(y < (size_t)g.resY);
  157. return g.startVtxID + x + y * g.lineVtxOffset;
  158. }
  159. /*! returns i'th vertex of the first timestep */
  160. __forceinline const Vec3fa grid_vertex(const Grid& g, size_t x, size_t y) const {
  161. const size_t index = grid_vertex_index(g,x,y);
  162. return vertex(index);
  163. }
  164. /*! returns i'th vertex of the itime'th timestep */
  165. __forceinline const Vec3fa grid_vertex(const Grid& g, size_t x, size_t y, size_t itime) const {
  166. const size_t index = grid_vertex_index(g,x,y);
  167. return vertex(index,itime);
  168. }
  169. /*! calculates the build bounds of the i'th primitive, if it's valid */
  170. __forceinline bool buildBounds(const Grid& g, size_t sx, size_t sy, BBox3fa& bbox) const
  171. {
  172. BBox3fa b(empty);
  173. for (size_t t=0; t<numTimeSteps; t++)
  174. {
  175. for (size_t y=sy;y<min(sy+3,(size_t)g.resY);y++)
  176. for (size_t x=sx;x<min(sx+3,(size_t)g.resX);x++)
  177. {
  178. const Vec3fa v = grid_vertex(g,x,y,t);
  179. if (unlikely(!isvalid(v))) return false;
  180. b.extend(v);
  181. }
  182. }
  183. bbox = b;
  184. return true;
  185. }
  186. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  187. __forceinline bool buildBounds(const Grid& g, size_t sx, size_t sy, size_t itime, BBox3fa& bbox) const
  188. {
  189. assert(itime < numTimeSteps);
  190. BBox3fa b0(empty);
  191. for (size_t y=sy;y<min(sy+3,(size_t)g.resY);y++)
  192. for (size_t x=sx;x<min(sx+3,(size_t)g.resX);x++)
  193. {
  194. const Vec3fa v = grid_vertex(g,x,y,itime);
  195. if (unlikely(!isvalid(v))) return false;
  196. b0.extend(v);
  197. }
  198. /* use bounds of first time step in builder */
  199. bbox = b0;
  200. return true;
  201. }
  202. __forceinline bool valid(size_t gridID, size_t itime=0) const {
  203. return valid(gridID, make_range(itime, itime));
  204. }
  205. /*! check if the i'th primitive is valid between the specified time range */
  206. __forceinline bool valid(size_t gridID, const range<size_t>& itime_range) const
  207. {
  208. if (unlikely(gridID >= grids.size())) return false;
  209. const Grid &g = grid(gridID);
  210. if (unlikely(g.startVtxID + 0 >= vertices0.size())) return false;
  211. if (unlikely(g.startVtxID + (g.resY-1)*g.lineVtxOffset + g.resX-1 >= vertices0.size())) return false;
  212. for (size_t y=0;y<g.resY;y++)
  213. for (size_t x=0;x<g.resX;x++)
  214. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
  215. if (!isvalid(grid_vertex(g,x,y,itime))) return false;
  216. return true;
  217. }
  218. __forceinline BBox3fa bounds(const Grid& g, size_t sx, size_t sy, size_t itime) const
  219. {
  220. BBox3fa box(empty);
  221. buildBounds(g,sx,sy,itime,box);
  222. return box;
  223. }
  224. __forceinline LBBox3fa linearBounds(const Grid& g, size_t sx, size_t sy, size_t itime) const {
  225. BBox3fa bounds0, bounds1;
  226. buildBounds(g,sx,sy,itime+0,bounds0);
  227. buildBounds(g,sx,sy,itime+1,bounds1);
  228. return LBBox3fa(bounds0,bounds1);
  229. }
  230. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  231. __forceinline LBBox3fa linearBounds(const Grid& g, size_t sx, size_t sy, const BBox1f& dt) const {
  232. return LBBox3fa([&] (size_t itime) { return bounds(g,sx,sy,itime); }, dt, time_range, fnumTimeSegments);
  233. }
  234. public:
  235. BufferView<Grid> grids; //!< array of triangles
  236. BufferView<Vec3fa> vertices0; //!< fast access to first vertex buffer
  237. vector<BufferView<Vec3fa>> vertices; //!< vertex array for each timestep
  238. vector<RawBufferView> vertexAttribs; //!< vertex attributes
  239. };
  240. namespace isa
  241. {
  242. struct GridMeshISA : public GridMesh
  243. {
  244. GridMeshISA (Device* device)
  245. : GridMesh(device) {}
  246. };
  247. }
  248. DECLARE_ISA_FUNCTION(GridMesh*, createGridMesh, Device*);
  249. }