quadv.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. namespace embree
  19. {
  20. /* Stores the vertices of M quads in struct of array layout */
  21. template <int M>
  22. struct QuadMv
  23. {
  24. typedef Vec3<vfloat<M>> Vec3vfM;
  25. public:
  26. struct Type : public PrimitiveType
  27. {
  28. Type();
  29. size_t size(const char* This) const;
  30. };
  31. static Type type;
  32. public:
  33. /* Returns maximal number of stored quads */
  34. static __forceinline size_t max_size() { return M; }
  35. /* Returns required number of primitive blocks for N primitives */
  36. static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); }
  37. public:
  38. /* Default constructor */
  39. __forceinline QuadMv() {}
  40. /* Construction from vertices and IDs */
  41. __forceinline QuadMv(const Vec3vfM& v0, const Vec3vfM& v1, const Vec3vfM& v2, const Vec3vfM& v3, const vint<M>& geomIDs, const vint<M>& primIDs)
  42. : v0(v0), v1(v1), v2(v2), v3(v3), geomIDs(geomIDs), primIDs(primIDs) {}
  43. /* Returns a mask that tells which quads are valid */
  44. __forceinline vbool<M> valid() const { return geomIDs != vint<M>(-1); }
  45. /* Returns true if the specified quad is valid */
  46. __forceinline bool valid(const size_t i) const { assert(i<M); return geomIDs[i] != -1; }
  47. /* Returns the number of stored quads */
  48. __forceinline size_t size() const { return __bsf(~movemask(valid())); }
  49. /* Returns the geometry IDs */
  50. __forceinline vint<M> geomID() const { return geomIDs; }
  51. __forceinline int geomID(const size_t i) const { assert(i<M); return geomIDs[i]; }
  52. /* Returns the primitive IDs */
  53. __forceinline vint<M> primID() const { return primIDs; }
  54. __forceinline int primID(const size_t i) const { assert(i<M); return primIDs[i]; }
  55. /* Calculate the bounds of the quads */
  56. __forceinline BBox3fa bounds() const
  57. {
  58. Vec3vfM lower = min(v0,v1,v2,v3);
  59. Vec3vfM upper = max(v0,v1,v2,v3);
  60. vbool<M> mask = valid();
  61. lower.x = select(mask,lower.x,vfloat<M>(pos_inf));
  62. lower.y = select(mask,lower.y,vfloat<M>(pos_inf));
  63. lower.z = select(mask,lower.z,vfloat<M>(pos_inf));
  64. upper.x = select(mask,upper.x,vfloat<M>(neg_inf));
  65. upper.y = select(mask,upper.y,vfloat<M>(neg_inf));
  66. upper.z = select(mask,upper.z,vfloat<M>(neg_inf));
  67. return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)),
  68. Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z)));
  69. }
  70. /* Non temporal store */
  71. __forceinline static void store_nt(QuadMv* dst, const QuadMv& src)
  72. {
  73. vfloat<M>::store_nt(&dst->v0.x,src.v0.x);
  74. vfloat<M>::store_nt(&dst->v0.y,src.v0.y);
  75. vfloat<M>::store_nt(&dst->v0.z,src.v0.z);
  76. vfloat<M>::store_nt(&dst->v1.x,src.v1.x);
  77. vfloat<M>::store_nt(&dst->v1.y,src.v1.y);
  78. vfloat<M>::store_nt(&dst->v1.z,src.v1.z);
  79. vfloat<M>::store_nt(&dst->v2.x,src.v2.x);
  80. vfloat<M>::store_nt(&dst->v2.y,src.v2.y);
  81. vfloat<M>::store_nt(&dst->v2.z,src.v2.z);
  82. vfloat<M>::store_nt(&dst->v3.x,src.v3.x);
  83. vfloat<M>::store_nt(&dst->v3.y,src.v3.y);
  84. vfloat<M>::store_nt(&dst->v3.z,src.v3.z);
  85. vint<M>::store_nt(&dst->geomIDs,src.geomIDs);
  86. vint<M>::store_nt(&dst->primIDs,src.primIDs);
  87. }
  88. /* Fill quad from quad list */
  89. __forceinline void fill(const PrimRef* prims, size_t& begin, size_t end, Scene* scene)
  90. {
  91. vint<M> vgeomID = -1, vprimID = -1;
  92. Vec3vfM v0 = zero, v1 = zero, v2 = zero, v3 = zero;
  93. for (size_t i=0; i<M && begin<end; i++, begin++)
  94. {
  95. const PrimRef& prim = prims[begin];
  96. const unsigned geomID = prim.geomID();
  97. const unsigned primID = prim.primID();
  98. const QuadMesh* __restrict__ const mesh = scene->get<QuadMesh>(geomID);
  99. const QuadMesh::Quad& quad = mesh->quad(primID);
  100. const Vec3fa& p0 = mesh->vertex(quad.v[0]);
  101. const Vec3fa& p1 = mesh->vertex(quad.v[1]);
  102. const Vec3fa& p2 = mesh->vertex(quad.v[2]);
  103. const Vec3fa& p3 = mesh->vertex(quad.v[3]);
  104. vgeomID [i] = geomID;
  105. vprimID [i] = primID;
  106. v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;
  107. v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;
  108. v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;
  109. v3.x[i] = p3.x; v3.y[i] = p3.y; v3.z[i] = p3.z;
  110. }
  111. QuadMv::store_nt(this,QuadMv(v0,v1,v2,v3,vgeomID,vprimID));
  112. }
  113. /* Updates the primitive */
  114. __forceinline BBox3fa update(QuadMesh* mesh)
  115. {
  116. BBox3fa bounds = empty;
  117. vint<M> vgeomID = -1, vprimID = -1;
  118. Vec3vfM v0 = zero, v1 = zero, v2 = zero;
  119. for (size_t i=0; i<M; i++)
  120. {
  121. if (primID(i) == -1) break;
  122. const unsigned geomId = geomID(i);
  123. const unsigned primId = primID(i);
  124. const QuadMesh::Quad& quad = mesh->quad(primId);
  125. const Vec3fa p0 = mesh->vertex(quad.v[0]);
  126. const Vec3fa p1 = mesh->vertex(quad.v[1]);
  127. const Vec3fa p2 = mesh->vertex(quad.v[2]);
  128. const Vec3fa p3 = mesh->vertex(quad.v[3]);
  129. bounds.extend(merge(BBox3fa(p0),BBox3fa(p1),BBox3fa(p2),BBox3fa(p3)));
  130. vgeomID [i] = geomId;
  131. vprimID [i] = primId;
  132. v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;
  133. v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;
  134. v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;
  135. v3.x[i] = p3.x; v3.y[i] = p3.y; v3.z[i] = p3.z;
  136. }
  137. new (this) QuadMv(v0,v1,v2,v3,vgeomID,vprimID);
  138. return bounds;
  139. }
  140. public:
  141. Vec3vfM v0; // 1st vertex of the quads
  142. Vec3vfM v1; // 2nd vertex of the quads
  143. Vec3vfM v2; // 3rd vertex of the quads
  144. Vec3vfM v3; // 4rd vertex of the quads
  145. vint<M> geomIDs; // geometry ID
  146. vint<M> primIDs; // primitive ID
  147. };
  148. template<int M>
  149. typename QuadMv<M>::Type QuadMv<M>::type;
  150. typedef QuadMv<4> Quad4v;
  151. }