quadv.h 6.1 KB

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