curveNi_mb.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "primitive.h"
  5. #include "curve_intersector_precalculations.h"
  6. namespace embree
  7. {
  8. template<int M>
  9. struct CurveNiMB
  10. {
  11. struct Type : public PrimitiveType {
  12. const char* name() const;
  13. size_t sizeActive(const char* This) const;
  14. size_t sizeTotal(const char* This) const;
  15. size_t getBytes(const char* This) const;
  16. };
  17. static Type type;
  18. public:
  19. /* Returns maximum number of stored primitives */
  20. static __forceinline size_t max_size() { return M; }
  21. /* Returns required number of primitive blocks for N primitives */
  22. static __forceinline size_t blocks(size_t N) { return (N+M-1)/M; }
  23. static __forceinline size_t bytes(size_t N)
  24. {
  25. const size_t f = N/M, r = N%M;
  26. static_assert(sizeof(CurveNiMB) == 6+37*M+24, "internal data layout issue");
  27. return f*sizeof(CurveNiMB) + (r!=0)*(6+37*r+24);
  28. }
  29. public:
  30. /*! Default constructor. */
  31. __forceinline CurveNiMB () {}
  32. /*! fill curve from curve list */
  33. __forceinline LBBox3fa fillMB(const PrimRefMB* prims, size_t& begin, size_t _end, Scene* scene, const BBox1f time_range)
  34. {
  35. size_t end = min(begin+M,_end);
  36. N = (unsigned char)(end-begin);
  37. const unsigned int geomID0 = prims[begin].geomID();
  38. this->geomID(N) = geomID0;
  39. ty = (unsigned char) scene->get(geomID0)->getType();
  40. /* encode all primitives */
  41. LBBox3fa lbounds = empty;
  42. for (size_t i=0; i<N; i++)
  43. {
  44. const PrimRefMB& prim = prims[begin+i];
  45. const unsigned int geomID = prim.geomID(); assert(geomID == geomID0);
  46. const unsigned int primID = prim.primID();
  47. lbounds.extend(scene->get(geomID)->vlinearBounds(primID,time_range));
  48. }
  49. BBox3fa bounds = lbounds.bounds();
  50. /* calculate offset and scale */
  51. Vec3fa loffset = bounds.lower;
  52. float lscale = reduce_min(256.0f/(bounds.size()*sqrt(3.0f)));
  53. if (bounds.size() == Vec3fa(zero)) lscale = 0.0f;
  54. *this->offset(N) = loffset;
  55. *this->scale(N) = lscale;
  56. this->time_offset(N) = time_range.lower;
  57. this->time_scale(N) = 1.0f/time_range.size();
  58. /* encode all primitives */
  59. for (size_t i=0; i<M && begin<end; i++, begin++)
  60. {
  61. const PrimRefMB& prim = prims[begin];
  62. const unsigned int geomID = prim.geomID();
  63. const unsigned int primID = prim.primID();
  64. const LinearSpace3fa space2 = scene->get(geomID)->computeAlignedSpaceMB(primID,time_range);
  65. const LinearSpace3fa space3(trunc(126.0f*space2.vx),trunc(126.0f*space2.vy),trunc(126.0f*space2.vz));
  66. const LBBox3fa bounds = scene->get(geomID)->vlinearBounds(loffset,lscale,max(length(space3.vx),length(space3.vy),length(space3.vz)),space3.transposed(),primID,time_range);
  67. // NOTE: this weird (char) (short) cast works around VS2015 Win32 compiler bug
  68. bounds_vx_x(N)[i] = (char) (short) space3.vx.x;
  69. bounds_vx_y(N)[i] = (char) (short) space3.vx.y;
  70. bounds_vx_z(N)[i] = (char) (short) space3.vx.z;
  71. bounds_vx_lower0(N)[i] = (short) clamp(floor(bounds.bounds0.lower.x),-32767.0f,32767.0f);
  72. bounds_vx_upper0(N)[i] = (short) clamp(ceil (bounds.bounds0.upper.x),-32767.0f,32767.0f);
  73. bounds_vx_lower1(N)[i] = (short) clamp(floor(bounds.bounds1.lower.x),-32767.0f,32767.0f);
  74. bounds_vx_upper1(N)[i] = (short) clamp(ceil (bounds.bounds1.upper.x),-32767.0f,32767.0f);
  75. assert(-32767.0f <= floor(bounds.bounds0.lower.x) && floor(bounds.bounds0.lower.x) <= 32767.0f);
  76. assert(-32767.0f <= ceil (bounds.bounds0.upper.x) && ceil (bounds.bounds0.upper.x) <= 32767.0f);
  77. assert(-32767.0f <= floor(bounds.bounds1.lower.x) && floor(bounds.bounds1.lower.x) <= 32767.0f);
  78. assert(-32767.0f <= ceil (bounds.bounds1.upper.x) && ceil (bounds.bounds1.upper.x) <= 32767.0f);
  79. bounds_vy_x(N)[i] = (char) (short) space3.vy.x;
  80. bounds_vy_y(N)[i] = (char) (short) space3.vy.y;
  81. bounds_vy_z(N)[i] = (char) (short) space3.vy.z;
  82. bounds_vy_lower0(N)[i] = (short) clamp(floor(bounds.bounds0.lower.y),-32767.0f,32767.0f);
  83. bounds_vy_upper0(N)[i] = (short) clamp(ceil (bounds.bounds0.upper.y),-32767.0f,32767.0f);
  84. bounds_vy_lower1(N)[i] = (short) clamp(floor(bounds.bounds1.lower.y),-32767.0f,32767.0f);
  85. bounds_vy_upper1(N)[i] = (short) clamp(ceil (bounds.bounds1.upper.y),-32767.0f,32767.0f);
  86. assert(-32767.0f <= floor(bounds.bounds0.lower.y) && floor(bounds.bounds0.lower.y) <= 32767.0f);
  87. assert(-32767.0f <= ceil (bounds.bounds0.upper.y) && ceil (bounds.bounds0.upper.y) <= 32767.0f);
  88. assert(-32767.0f <= floor(bounds.bounds1.lower.y) && floor(bounds.bounds1.lower.y) <= 32767.0f);
  89. assert(-32767.0f <= ceil (bounds.bounds1.upper.y) && ceil (bounds.bounds1.upper.y) <= 32767.0f);
  90. bounds_vz_x(N)[i] = (char) (short) space3.vz.x;
  91. bounds_vz_y(N)[i] = (char) (short) space3.vz.y;
  92. bounds_vz_z(N)[i] = (char) (short) space3.vz.z;
  93. bounds_vz_lower0(N)[i] = (short) clamp(floor(bounds.bounds0.lower.z),-32767.0f,32767.0f);
  94. bounds_vz_upper0(N)[i] = (short) clamp(ceil (bounds.bounds0.upper.z),-32767.0f,32767.0f);
  95. bounds_vz_lower1(N)[i] = (short) clamp(floor(bounds.bounds1.lower.z),-32767.0f,32767.0f);
  96. bounds_vz_upper1(N)[i] = (short) clamp(ceil (bounds.bounds1.upper.z),-32767.0f,32767.0f);
  97. assert(-32767.0f <= floor(bounds.bounds0.lower.z) && floor(bounds.bounds0.lower.z) <= 32767.0f);
  98. assert(-32767.0f <= ceil (bounds.bounds0.upper.z) && ceil (bounds.bounds0.upper.z) <= 32767.0f);
  99. assert(-32767.0f <= floor(bounds.bounds1.lower.z) && floor(bounds.bounds1.lower.z) <= 32767.0f);
  100. assert(-32767.0f <= ceil (bounds.bounds1.upper.z) && ceil (bounds.bounds1.upper.z) <= 32767.0f);
  101. this->primID(N)[i] = primID;
  102. }
  103. return lbounds;
  104. }
  105. template<typename BVH, typename SetMB, typename Allocator>
  106. __forceinline static typename BVH::NodeRecordMB4D createLeafMB(BVH* bvh, const SetMB& prims, const Allocator& alloc)
  107. {
  108. size_t start = prims.begin();
  109. size_t end = prims.end();
  110. size_t items = CurveNiMB::blocks(prims.size());
  111. size_t numbytes = CurveNiMB::bytes(prims.size());
  112. CurveNiMB* accel = (CurveNiMB*) alloc.malloc1(numbytes,BVH::byteAlignment);
  113. const typename BVH::NodeRef node = bvh->encodeLeaf((char*)accel,items);
  114. LBBox3fa bounds = empty;
  115. for (size_t i=0; i<items; i++)
  116. bounds.extend(accel[i].fillMB(prims.prims->data(),start,end,bvh->scene,prims.time_range));
  117. return typename BVH::NodeRecordMB4D(node,bounds,prims.time_range);
  118. };
  119. public:
  120. // 27.6 - 46 bytes per primitive
  121. unsigned char ty;
  122. unsigned char N;
  123. unsigned char data[4+37*M+24];
  124. /*
  125. struct Layout
  126. {
  127. unsigned int geomID;
  128. unsigned int primID[N];
  129. char bounds_vx_x[N];
  130. char bounds_vx_y[N];
  131. char bounds_vx_z[N];
  132. short bounds_vx_lower0[N];
  133. short bounds_vx_upper0[N];
  134. short bounds_vx_lower1[N];
  135. short bounds_vx_upper1[N];
  136. char bounds_vy_x[N];
  137. char bounds_vy_y[N];
  138. char bounds_vy_z[N];
  139. short bounds_vy_lower0[N];
  140. short bounds_vy_upper0[N];
  141. short bounds_vy_lower1[N];
  142. short bounds_vy_upper1[N];
  143. char bounds_vz_x[N];
  144. char bounds_vz_y[N];
  145. char bounds_vz_z[N];
  146. short bounds_vz_lower0[N];
  147. short bounds_vz_upper0[N];
  148. short bounds_vz_lower1[N];
  149. short bounds_vz_upper1[N];
  150. Vec3f offset;
  151. float scale;
  152. float time_offset;
  153. float time_scale;
  154. };
  155. */
  156. __forceinline unsigned int& geomID(size_t N) { return *(unsigned int*)((char*)this+2); }
  157. __forceinline const unsigned int& geomID(size_t N) const { return *(unsigned int*)((char*)this+2); }
  158. __forceinline unsigned int* primID(size_t N) { return (unsigned int*)((char*)this+6); }
  159. __forceinline const unsigned int* primID(size_t N) const { return (unsigned int*)((char*)this+6); }
  160. __forceinline char* bounds_vx_x(size_t N) { return (char*)((char*)this+6+4*N); }
  161. __forceinline const char* bounds_vx_x(size_t N) const { return (char*)((char*)this+6+4*N); }
  162. __forceinline char* bounds_vx_y(size_t N) { return (char*)((char*)this+6+5*N); }
  163. __forceinline const char* bounds_vx_y(size_t N) const { return (char*)((char*)this+6+5*N); }
  164. __forceinline char* bounds_vx_z(size_t N) { return (char*)((char*)this+6+6*N); }
  165. __forceinline const char* bounds_vx_z(size_t N) const { return (char*)((char*)this+6+6*N); }
  166. __forceinline short* bounds_vx_lower0(size_t N) { return (short*)((char*)this+6+7*N); }
  167. __forceinline const short* bounds_vx_lower0(size_t N) const { return (short*)((char*)this+6+7*N); }
  168. __forceinline short* bounds_vx_upper0(size_t N) { return (short*)((char*)this+6+9*N); }
  169. __forceinline const short* bounds_vx_upper0(size_t N) const { return (short*)((char*)this+6+9*N); }
  170. __forceinline short* bounds_vx_lower1(size_t N) { return (short*)((char*)this+6+11*N); }
  171. __forceinline const short* bounds_vx_lower1(size_t N) const { return (short*)((char*)this+6+11*N); }
  172. __forceinline short* bounds_vx_upper1(size_t N) { return (short*)((char*)this+6+13*N); }
  173. __forceinline const short* bounds_vx_upper1(size_t N) const { return (short*)((char*)this+6+13*N); }
  174. __forceinline char* bounds_vy_x(size_t N) { return (char*)((char*)this+6+15*N); }
  175. __forceinline const char* bounds_vy_x(size_t N) const { return (char*)((char*)this+6+15*N); }
  176. __forceinline char* bounds_vy_y(size_t N) { return (char*)((char*)this+6+16*N); }
  177. __forceinline const char* bounds_vy_y(size_t N) const { return (char*)((char*)this+6+16*N); }
  178. __forceinline char* bounds_vy_z(size_t N) { return (char*)((char*)this+6+17*N); }
  179. __forceinline const char* bounds_vy_z(size_t N) const { return (char*)((char*)this+6+17*N); }
  180. __forceinline short* bounds_vy_lower0(size_t N) { return (short*)((char*)this+6+18*N); }
  181. __forceinline const short* bounds_vy_lower0(size_t N) const { return (short*)((char*)this+6+18*N); }
  182. __forceinline short* bounds_vy_upper0(size_t N) { return (short*)((char*)this+6+20*N); }
  183. __forceinline const short* bounds_vy_upper0(size_t N) const { return (short*)((char*)this+6+20*N); }
  184. __forceinline short* bounds_vy_lower1(size_t N) { return (short*)((char*)this+6+22*N); }
  185. __forceinline const short* bounds_vy_lower1(size_t N) const { return (short*)((char*)this+6+22*N); }
  186. __forceinline short* bounds_vy_upper1(size_t N) { return (short*)((char*)this+6+24*N); }
  187. __forceinline const short* bounds_vy_upper1(size_t N) const { return (short*)((char*)this+6+24*N); }
  188. __forceinline char* bounds_vz_x(size_t N) { return (char*)((char*)this+6+26*N); }
  189. __forceinline const char* bounds_vz_x(size_t N) const { return (char*)((char*)this+6+26*N); }
  190. __forceinline char* bounds_vz_y(size_t N) { return (char*)((char*)this+6+27*N); }
  191. __forceinline const char* bounds_vz_y(size_t N) const { return (char*)((char*)this+6+27*N); }
  192. __forceinline char* bounds_vz_z(size_t N) { return (char*)((char*)this+6+28*N); }
  193. __forceinline const char* bounds_vz_z(size_t N) const { return (char*)((char*)this+6+28*N); }
  194. __forceinline short* bounds_vz_lower0(size_t N) { return (short*)((char*)this+6+29*N); }
  195. __forceinline const short* bounds_vz_lower0(size_t N) const { return (short*)((char*)this+6+29*N); }
  196. __forceinline short* bounds_vz_upper0(size_t N) { return (short*)((char*)this+6+31*N); }
  197. __forceinline const short* bounds_vz_upper0(size_t N) const { return (short*)((char*)this+6+31*N); }
  198. __forceinline short* bounds_vz_lower1(size_t N) { return (short*)((char*)this+6+33*N); }
  199. __forceinline const short* bounds_vz_lower1(size_t N) const { return (short*)((char*)this+6+33*N); }
  200. __forceinline short* bounds_vz_upper1(size_t N) { return (short*)((char*)this+6+35*N); }
  201. __forceinline const short* bounds_vz_upper1(size_t N) const { return (short*)((char*)this+6+35*N); }
  202. __forceinline Vec3f* offset(size_t N) { return (Vec3f*)((char*)this+6+37*N); }
  203. __forceinline const Vec3f* offset(size_t N) const { return (Vec3f*)((char*)this+6+37*N); }
  204. __forceinline float* scale(size_t N) { return (float*)((char*)this+6+37*N+12); }
  205. __forceinline const float* scale(size_t N) const { return (float*)((char*)this+6+37*N+12); }
  206. __forceinline float& time_offset(size_t N) { return *(float*)((char*)this+6+37*N+16); }
  207. __forceinline const float& time_offset(size_t N) const { return *(float*)((char*)this+6+37*N+16); }
  208. __forceinline float& time_scale(size_t N) { return *(float*)((char*)this+6+37*N+20); }
  209. __forceinline const float& time_scale(size_t N) const { return *(float*)((char*)this+6+37*N+20); }
  210. __forceinline char* end(size_t N) { return (char*)this+6+37*N+24; }
  211. __forceinline const char* end(size_t N) const { return (char*)this+6+37*N+24; }
  212. };
  213. template<int M>
  214. typename CurveNiMB<M>::Type CurveNiMB<M>::type;
  215. typedef CurveNiMB<4> Curve4iMB;
  216. typedef CurveNiMB<8> Curve8iMB;
  217. }