quadi_mb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "primitive.h"
  18. #include "../common/scene.h"
  19. namespace embree
  20. {
  21. /* Stores M motion blur quads from an indexed face set */
  22. template <int M>
  23. struct QuadMiMB
  24. {
  25. typedef Vec3<vfloat<M>> Vec3vfM;
  26. /* Virtual interface to query information about the quad type */
  27. struct Type : public PrimitiveType
  28. {
  29. Type();
  30. size_t size(const char* This) const;
  31. };
  32. static Type type;
  33. public:
  34. /* Returns maximal number of stored quads */
  35. static __forceinline size_t max_size() { return M; }
  36. /* Returns required number of primitive blocks for N primitives */
  37. static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); }
  38. public:
  39. /* Default constructor */
  40. __forceinline QuadMiMB() { }
  41. /* Construction from vertices and IDs */
  42. __forceinline QuadMiMB(const vint<M>& v0,
  43. const vint<M>& v1,
  44. const vint<M>& v2,
  45. const vint<M>& v3,
  46. const vint<M>& geomIDs,
  47. const vint<M>& primIDs)
  48. : v0(v0),v1(v1), v2(v2), v3(v3), geomIDs(geomIDs), primIDs(primIDs)
  49. {
  50. }
  51. /* Returns a mask that tells which quads are valid */
  52. __forceinline vbool<M> valid() const { return primIDs != vint<M>(-1); }
  53. /* Returns if the specified quad is valid */
  54. __forceinline bool valid(const size_t i) const { assert(i<M); return primIDs[i] != -1; }
  55. /* Returns the number of stored quads */
  56. __forceinline size_t size() const { return __bsf(~movemask(valid())); }
  57. /* Returns the geometry IDs */
  58. __forceinline vint<M> geomID() const { return geomIDs; }
  59. __forceinline int geomID(const size_t i) const { assert(i<M); assert(geomIDs[i] != -1); return geomIDs[i]; }
  60. /* Returns the primitive IDs */
  61. __forceinline vint<M> primID() const { return primIDs; }
  62. __forceinline int primID(const size_t i) const { assert(i<M); return primIDs[i]; }
  63. __forceinline Vec3fa& getVertex(const vint<M> &v, const size_t index, const Scene *const scene) const
  64. {
  65. const QuadMesh* mesh = scene->get<QuadMesh>(geomID(index));
  66. return *(Vec3fa*)mesh->vertexPtr(v[index]);
  67. }
  68. template<typename T>
  69. __forceinline Vec3<T> getVertex(const vint<M> &v, const size_t index, const Scene *const scene, const size_t itime, const T& ftime) const
  70. {
  71. const QuadMesh* mesh = scene->get<QuadMesh>(geomID(index));
  72. const Vec3fa v0 = *(Vec3fa*)mesh->vertexPtr(v[index],itime+0);
  73. const Vec3fa v1 = *(Vec3fa*)mesh->vertexPtr(v[index],itime+1);
  74. const Vec3<T> p0(v0.x,v0.y,v0.z);
  75. const Vec3<T> p1(v1.x,v1.y,v1.z);
  76. return lerp(p0,p1,ftime);
  77. }
  78. /* gather the quads */
  79. template<int K>
  80. __forceinline void gather(const vbool<K>& valid,
  81. Vec3<vfloat<K>>& p0,
  82. Vec3<vfloat<K>>& p1,
  83. Vec3<vfloat<K>>& p2,
  84. Vec3<vfloat<K>>& p3,
  85. const size_t index,
  86. const Scene* const scene,
  87. const vfloat<K>& time) const
  88. {
  89. const QuadMesh* mesh = scene->get<QuadMesh>(geomID(index));
  90. vfloat<K> ftime;
  91. const vint<K> itime = getTimeSegment(time, vfloat<K>(mesh->fnumTimeSegments), ftime);
  92. const size_t first = __bsf(movemask(valid)); // assume itime is uniform
  93. p0 = getVertex(v0, index, scene, itime[first], ftime);
  94. p1 = getVertex(v1, index, scene, itime[first], ftime);
  95. p2 = getVertex(v2, index, scene, itime[first], ftime);
  96. p3 = getVertex(v3, index, scene, itime[first], ftime);
  97. }
  98. __forceinline void gather(Vec3<vfloat<M>>& p0,
  99. Vec3<vfloat<M>>& p1,
  100. Vec3<vfloat<M>>& p2,
  101. Vec3<vfloat<M>>& p3,
  102. const QuadMesh* mesh0,
  103. const QuadMesh* mesh1,
  104. const QuadMesh* mesh2,
  105. const QuadMesh* mesh3,
  106. const vint<M>& itime) const;
  107. __forceinline void gather(Vec3<vfloat<M>>& p0,
  108. Vec3<vfloat<M>>& p1,
  109. Vec3<vfloat<M>>& p2,
  110. Vec3<vfloat<M>>& p3,
  111. const Scene *const scene,
  112. const float time) const;
  113. /* Calculate the bounds of the quads */
  114. __forceinline const BBox3fa bounds(const Scene *const scene, const size_t itime=0) const
  115. {
  116. BBox3fa bounds = empty;
  117. for (size_t i=0; i<M && valid(i); i++)
  118. {
  119. const QuadMesh* mesh = scene->get<QuadMesh>(geomID(i));
  120. const Vec3fa &p0 = mesh->vertex(v0[i],itime);
  121. const Vec3fa &p1 = mesh->vertex(v1[i],itime);
  122. const Vec3fa &p2 = mesh->vertex(v2[i],itime);
  123. const Vec3fa &p3 = mesh->vertex(v3[i],itime);
  124. bounds.extend(p0);
  125. bounds.extend(p1);
  126. bounds.extend(p2);
  127. bounds.extend(p3);
  128. }
  129. return bounds;
  130. }
  131. /* Calculate the linear bounds of the primitive */
  132. __forceinline LBBox3fa linearBounds(const Scene* const scene, const size_t itime) {
  133. return LBBox3fa(bounds(scene,itime+0),bounds(scene,itime+1));
  134. }
  135. __forceinline LBBox3fa linearBounds(const Scene *const scene, size_t itime, size_t numTimeSteps) {
  136. LBBox3fa allBounds = empty;
  137. for (size_t i=0; i<M && valid(i); i++)
  138. {
  139. const QuadMesh* mesh = scene->get<QuadMesh>(geomID(i));
  140. allBounds.extend(mesh->linearBounds(primID(i), itime, numTimeSteps));
  141. }
  142. return allBounds;
  143. }
  144. /* Fill quad from quad list */
  145. __forceinline LBBox3fa fillMB(const PrimRef* prims, size_t& begin, size_t end, Scene* scene, size_t itime, size_t numTimeSteps)
  146. {
  147. vint<M> geomID = -1, primID = -1;
  148. vint<M> v0 = zero, v1 = zero, v2 = zero, v3 = zero;
  149. const PrimRef* prim = &prims[begin];
  150. for (size_t i=0; i<M; i++)
  151. {
  152. const QuadMesh* mesh = scene->get<QuadMesh>(prim->geomID());
  153. const QuadMesh::Quad& q = mesh->quad(prim->primID());
  154. if (begin<end) {
  155. geomID[i] = prim->geomID();
  156. primID[i] = prim->primID();
  157. v0[i] = q.v[0];
  158. v1[i] = q.v[1];
  159. v2[i] = q.v[2];
  160. v3[i] = q.v[3];
  161. begin++;
  162. } else {
  163. assert(i);
  164. geomID[i] = geomID[0]; // always valid geomIDs
  165. primID[i] = -1; // indicates invalid data
  166. v0[i] = 0;
  167. v1[i] = 0;
  168. v2[i] = 0;
  169. v3[i] = 0;
  170. }
  171. if (begin<end) prim = &prims[begin];
  172. }
  173. new (this) QuadMiMB(v0,v1,v2,v3,geomID,primID); // FIXME: use non temporal store
  174. return linearBounds(scene,itime,numTimeSteps);
  175. }
  176. friend std::ostream& operator<<(std::ostream& cout, const QuadMiMB& quad) {
  177. return cout << "QuadMiMB<" << M << ">( v0 = " << quad.v0 << ", v1 = " << quad.v1 << ", v2 = " << quad.v2 << ", v3 = " << quad.v3 << ", geomID = " << quad.geomIDs << ", primID = " << quad.primIDs << " )";
  178. }
  179. public:
  180. vint<M> v0; // index of 1st vertex
  181. vint<M> v1; // index of 2nd vertex
  182. vint<M> v2; // index of 3rd vertex
  183. vint<M> v3; // index of 4th vertex
  184. vint<M> geomIDs; // geometry ID of mesh
  185. vint<M> primIDs; // primitive ID of primitive inside mesh
  186. };
  187. template<>
  188. __forceinline void QuadMiMB<4>::gather(Vec3vf4& p0,
  189. Vec3vf4& p1,
  190. Vec3vf4& p2,
  191. Vec3vf4& p3,
  192. const QuadMesh* mesh0,
  193. const QuadMesh* mesh1,
  194. const QuadMesh* mesh2,
  195. const QuadMesh* mesh3,
  196. const vint4& itime) const
  197. {
  198. const vfloat4 a0 = vfloat4::loadu(mesh0->vertexPtr(v0[0],itime[0]));
  199. const vfloat4 a1 = vfloat4::loadu(mesh1->vertexPtr(v0[1],itime[1]));
  200. const vfloat4 a2 = vfloat4::loadu(mesh2->vertexPtr(v0[2],itime[2]));
  201. const vfloat4 a3 = vfloat4::loadu(mesh3->vertexPtr(v0[3],itime[3]));
  202. transpose(a0,a1,a2,a3,p0.x,p0.y,p0.z);
  203. const vfloat4 b0 = vfloat4::loadu(mesh0->vertexPtr(v1[0],itime[0]));
  204. const vfloat4 b1 = vfloat4::loadu(mesh1->vertexPtr(v1[1],itime[1]));
  205. const vfloat4 b2 = vfloat4::loadu(mesh2->vertexPtr(v1[2],itime[2]));
  206. const vfloat4 b3 = vfloat4::loadu(mesh3->vertexPtr(v1[3],itime[3]));
  207. transpose(b0,b1,b2,b3,p1.x,p1.y,p1.z);
  208. const vfloat4 c0 = vfloat4::loadu(mesh0->vertexPtr(v2[0],itime[0]));
  209. const vfloat4 c1 = vfloat4::loadu(mesh1->vertexPtr(v2[1],itime[1]));
  210. const vfloat4 c2 = vfloat4::loadu(mesh2->vertexPtr(v2[2],itime[2]));
  211. const vfloat4 c3 = vfloat4::loadu(mesh3->vertexPtr(v2[3],itime[3]));
  212. transpose(c0,c1,c2,c3,p2.x,p2.y,p2.z);
  213. const vfloat4 d0 = vfloat4::loadu(mesh0->vertexPtr(v3[0],itime[0]));
  214. const vfloat4 d1 = vfloat4::loadu(mesh1->vertexPtr(v3[1],itime[1]));
  215. const vfloat4 d2 = vfloat4::loadu(mesh2->vertexPtr(v3[2],itime[2]));
  216. const vfloat4 d3 = vfloat4::loadu(mesh3->vertexPtr(v3[3],itime[3]));
  217. transpose(d0,d1,d2,d3,p3.x,p3.y,p3.z);
  218. }
  219. template<>
  220. __forceinline void QuadMiMB<4>::gather(Vec3vf4& p0,
  221. Vec3vf4& p1,
  222. Vec3vf4& p2,
  223. Vec3vf4& p3,
  224. const Scene *const scene,
  225. const float time) const
  226. {
  227. const QuadMesh* mesh0 = scene->get<QuadMesh>(geomIDs[0]);
  228. const QuadMesh* mesh1 = scene->get<QuadMesh>(geomIDs[1]);
  229. const QuadMesh* mesh2 = scene->get<QuadMesh>(geomIDs[2]);
  230. const QuadMesh* mesh3 = scene->get<QuadMesh>(geomIDs[3]);
  231. const vfloat4 numTimeSegments(mesh0->fnumTimeSegments, mesh1->fnumTimeSegments, mesh2->fnumTimeSegments, mesh3->fnumTimeSegments);
  232. vfloat4 ftime;
  233. const vint4 itime = getTimeSegment(vfloat4(time), numTimeSegments, ftime);
  234. Vec3vf4 a0,a1,a2,a3;
  235. gather(a0,a1,a2,a3,mesh0,mesh1,mesh2,mesh3,itime);
  236. Vec3vf4 b0,b1,b2,b3;
  237. gather(b0,b1,b2,b3,mesh0,mesh1,mesh2,mesh3,itime+1);
  238. p0 = lerp(a0,b0,ftime);
  239. p1 = lerp(a1,b1,ftime);
  240. p2 = lerp(a2,b2,ftime);
  241. p3 = lerp(a3,b3,ftime);
  242. }
  243. template<int M>
  244. typename QuadMiMB<M>::Type QuadMiMB<M>::type;
  245. typedef QuadMiMB<4> Quad4iMB;
  246. }