trianglei_mb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 triangles from an indexed face set */
  22. template <int M>
  23. struct TriangleMiMB
  24. {
  25. typedef Vec3<vfloat<M>> Vec3vfM;
  26. /* Virtual interface to query information about the triangle 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 triangles */
  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 TriangleMiMB() { }
  41. /* Construction from vertices and IDs */
  42. __forceinline TriangleMiMB(const vint<M>& v0,
  43. const vint<M>& v1,
  44. const vint<M>& v2,
  45. const vint<M>& geomIDs,
  46. const vint<M>& primIDs)
  47. : v0(v0), v1(v1), v2(v2), geomIDs(geomIDs), primIDs(primIDs)
  48. {
  49. }
  50. /* Returns a mask that tells which triangles are valid */
  51. __forceinline vbool<M> valid() const { return primIDs != vint<M>(-1); }
  52. /* Returns if the specified triangle is valid */
  53. __forceinline bool valid(const size_t i) const { assert(i<M); return primIDs[i] != -1; }
  54. /* Returns the number of stored triangles */
  55. __forceinline size_t size() const { return __bsf(~movemask(valid())); }
  56. /* Returns the geometry IDs */
  57. __forceinline vint<M> geomID() const { return geomIDs; }
  58. __forceinline int geomID(const size_t i) const { assert(i<M); assert(geomIDs[i] != -1); return geomIDs[i]; }
  59. /* Returns the primitive IDs */
  60. __forceinline vint<M> primID() const { return primIDs; }
  61. __forceinline int primID(const size_t i) const { assert(i<M); return primIDs[i]; }
  62. __forceinline Vec3fa& getVertex(const vint<M> &v, const size_t index, const Scene *const scene) const
  63. {
  64. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  65. return *(Vec3fa*)mesh->vertexPtr(v[index]);
  66. }
  67. template<typename T>
  68. __forceinline Vec3<T> getVertex(const vint<M> &v, const size_t index, const Scene *const scene, const size_t itime, const T& ftime) const
  69. {
  70. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  71. const Vec3fa v0 = *(Vec3fa*)mesh->vertexPtr(v[index],itime+0);
  72. const Vec3fa v1 = *(Vec3fa*)mesh->vertexPtr(v[index],itime+1);
  73. const Vec3<T> p0(v0.x,v0.y,v0.z);
  74. const Vec3<T> p1(v1.x,v1.y,v1.z);
  75. return lerp(p0,p1,ftime);
  76. }
  77. /* gather the triangles */
  78. template<int K>
  79. __forceinline void gather(const vbool<K>& valid,
  80. Vec3<vfloat<K>>& p0,
  81. Vec3<vfloat<K>>& p1,
  82. Vec3<vfloat<K>>& p2,
  83. const size_t index,
  84. const Scene* const scene,
  85. const vfloat<K>& time) const
  86. {
  87. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(index));
  88. vfloat<K> ftime;
  89. const vint<K> itime = getTimeSegment(time, vfloat<K>(mesh->fnumTimeSegments), ftime);
  90. const size_t first = __bsf(movemask(valid)); // assume itime is uniform
  91. p0 = getVertex(v0, index, scene, itime[first], ftime);
  92. p1 = getVertex(v1, index, scene, itime[first], ftime);
  93. p2 = getVertex(v2, index, scene, itime[first], ftime);
  94. }
  95. __forceinline void gather(Vec3<vfloat<M>>& p0,
  96. Vec3<vfloat<M>>& p1,
  97. Vec3<vfloat<M>>& p2,
  98. const TriangleMesh* mesh0,
  99. const TriangleMesh* mesh1,
  100. const TriangleMesh* mesh2,
  101. const TriangleMesh* mesh3,
  102. const vint<M>& itime) const;
  103. __forceinline void gather(Vec3<vfloat<M>>& p0,
  104. Vec3<vfloat<M>>& p1,
  105. Vec3<vfloat<M>>& p2,
  106. const Scene *const scene,
  107. const float time) const;
  108. /* Calculate the bounds of the triangles */
  109. __forceinline const BBox3fa bounds(const Scene *const scene, const size_t itime=0) const
  110. {
  111. BBox3fa bounds = empty;
  112. for (size_t i=0; i<M && valid(i); i++)
  113. {
  114. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(i));
  115. const Vec3fa &p0 = mesh->vertex(v0[i],itime);
  116. const Vec3fa &p1 = mesh->vertex(v1[i],itime);
  117. const Vec3fa &p2 = mesh->vertex(v2[i],itime);
  118. bounds.extend(p0);
  119. bounds.extend(p1);
  120. bounds.extend(p2);
  121. }
  122. return bounds;
  123. }
  124. /* Calculate the linear bounds of the primitive */
  125. __forceinline LBBox3fa linearBounds(const Scene *const scene, size_t itime) {
  126. return LBBox3fa(bounds(scene,itime+0),bounds(scene,itime+1));
  127. }
  128. __forceinline LBBox3fa linearBounds(const Scene *const scene, size_t itime, size_t numTimeSteps) {
  129. LBBox3fa allBounds = empty;
  130. for (size_t i=0; i<M && valid(i); i++)
  131. {
  132. const TriangleMesh* mesh = scene->get<TriangleMesh>(geomID(i));
  133. allBounds.extend(mesh->linearBounds(primID(i), itime, numTimeSteps));
  134. }
  135. return allBounds;
  136. }
  137. /* Fill triangle from triangle list */
  138. __forceinline LBBox3fa fillMB(const PrimRef* prims, size_t& begin, size_t end, Scene* scene, size_t itime, size_t numTimeSteps)
  139. {
  140. vint<M> geomID = -1, primID = -1;
  141. vint<M> v0 = zero, v1 = zero, v2 = zero;
  142. const PrimRef* prim = &prims[begin];
  143. for (size_t i=0; i<M; i++)
  144. {
  145. const TriangleMesh* mesh = scene->get<TriangleMesh>(prim->geomID());
  146. const TriangleMesh::Triangle& tri = mesh->triangle(prim->primID());
  147. if (begin<end) {
  148. geomID[i] = prim->geomID();
  149. primID[i] = prim->primID();
  150. v0[i] = tri.v[0];
  151. v1[i] = tri.v[1];
  152. v2[i] = tri.v[2];
  153. begin++;
  154. } else {
  155. assert(i);
  156. geomID[i] = geomID[0]; // always valid geomIDs
  157. primID[i] = -1; // indicates invalid data
  158. v0[i] = 0;
  159. v1[i] = 0;
  160. v2[i] = 0;
  161. }
  162. if (begin<end) prim = &prims[begin];
  163. }
  164. new (this) TriangleMiMB(v0,v1,v2,geomID,primID); // FIXME: use non temporal store
  165. return linearBounds(scene,itime,numTimeSteps);
  166. }
  167. /* Updates the primitive */
  168. __forceinline BBox3fa update(TriangleMesh* mesh)
  169. {
  170. BBox3fa bounds = empty;
  171. for (size_t i=0; i<M; i++)
  172. {
  173. if (!valid(i)) break;
  174. const unsigned primId = primID(i);
  175. const TriangleMesh::Triangle& tri = mesh->triangle(primId);
  176. const Vec3fa p0 = mesh->vertex(tri.v[0]);
  177. const Vec3fa p1 = mesh->vertex(tri.v[1]);
  178. const Vec3fa p2 = mesh->vertex(tri.v[2]);
  179. bounds.extend(merge(BBox3fa(p0),BBox3fa(p1),BBox3fa(p2)));
  180. }
  181. return bounds;
  182. }
  183. public:
  184. vint<M> v0; // index of 1st vertex
  185. vint<M> v1; // index of 2nd vertex
  186. vint<M> v2; // index of 3rd vertex
  187. vint<M> geomIDs; // geometry ID of mesh
  188. vint<M> primIDs; // primitive ID of primitive inside mesh
  189. };
  190. template<>
  191. __forceinline void TriangleMiMB<4>::gather(Vec3vf4& p0,
  192. Vec3vf4& p1,
  193. Vec3vf4& p2,
  194. const TriangleMesh* mesh0,
  195. const TriangleMesh* mesh1,
  196. const TriangleMesh* mesh2,
  197. const TriangleMesh* mesh3,
  198. const vint4& itime) const
  199. {
  200. const vfloat4 a0 = vfloat4::loadu(mesh0->vertexPtr(v0[0],itime[0]));
  201. const vfloat4 a1 = vfloat4::loadu(mesh1->vertexPtr(v0[1],itime[1]));
  202. const vfloat4 a2 = vfloat4::loadu(mesh2->vertexPtr(v0[2],itime[2]));
  203. const vfloat4 a3 = vfloat4::loadu(mesh3->vertexPtr(v0[3],itime[3]));
  204. transpose(a0,a1,a2,a3,p0.x,p0.y,p0.z);
  205. const vfloat4 b0 = vfloat4::loadu(mesh0->vertexPtr(v1[0],itime[0]));
  206. const vfloat4 b1 = vfloat4::loadu(mesh1->vertexPtr(v1[1],itime[1]));
  207. const vfloat4 b2 = vfloat4::loadu(mesh2->vertexPtr(v1[2],itime[2]));
  208. const vfloat4 b3 = vfloat4::loadu(mesh3->vertexPtr(v1[3],itime[3]));
  209. transpose(b0,b1,b2,b3,p1.x,p1.y,p1.z);
  210. const vfloat4 c0 = vfloat4::loadu(mesh0->vertexPtr(v2[0],itime[0]));
  211. const vfloat4 c1 = vfloat4::loadu(mesh1->vertexPtr(v2[1],itime[1]));
  212. const vfloat4 c2 = vfloat4::loadu(mesh2->vertexPtr(v2[2],itime[2]));
  213. const vfloat4 c3 = vfloat4::loadu(mesh3->vertexPtr(v2[3],itime[3]));
  214. transpose(c0,c1,c2,c3,p2.x,p2.y,p2.z);
  215. }
  216. template<>
  217. __forceinline void TriangleMiMB<4>::gather(Vec3vf4& p0,
  218. Vec3vf4& p1,
  219. Vec3vf4& p2,
  220. const Scene *const scene,
  221. const float time) const
  222. {
  223. const TriangleMesh* mesh0 = scene->get<TriangleMesh>(geomIDs[0]);
  224. const TriangleMesh* mesh1 = scene->get<TriangleMesh>(geomIDs[1]);
  225. const TriangleMesh* mesh2 = scene->get<TriangleMesh>(geomIDs[2]);
  226. const TriangleMesh* mesh3 = scene->get<TriangleMesh>(geomIDs[3]);
  227. const vfloat4 numTimeSegments(mesh0->fnumTimeSegments, mesh1->fnumTimeSegments, mesh2->fnumTimeSegments, mesh3->fnumTimeSegments);
  228. vfloat4 ftime;
  229. const vint4 itime = getTimeSegment(vfloat4(time), numTimeSegments, ftime);
  230. Vec3vf4 a0,a1,a2;
  231. gather(a0,a1,a2,mesh0,mesh1,mesh2,mesh3,itime);
  232. Vec3vf4 b0,b1,b2;
  233. gather(b0,b1,b2,mesh0,mesh1,mesh2,mesh3,itime+1);
  234. p0 = lerp(a0,b0,ftime);
  235. p1 = lerp(a1,b1,ftime);
  236. p2 = lerp(a2,b2,ftime);
  237. }
  238. template<int M>
  239. typename TriangleMiMB<M>::Type TriangleMiMB<M>::type;
  240. typedef TriangleMiMB<4> Triangle4iMB;
  241. }