curveNi.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 CurveNi
  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(CurveNi) == 22+25*M, "internal data layout issue");
  27. return f*sizeof(CurveNi) + (r!=0)*(22 + 25*r);
  28. }
  29. public:
  30. /*! Default constructor. */
  31. __forceinline CurveNi () {}
  32. /*! fill curve from curve list */
  33. __forceinline void fill(const PrimRef* prims, size_t& begin, size_t _end, Scene* scene)
  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. BBox3fa bounds = empty;
  42. for (size_t i=0; i<N; i++)
  43. {
  44. const PrimRef& prim = prims[begin+i];
  45. const unsigned int geomID = prim.geomID(); assert(geomID == geomID0);
  46. const unsigned int primID = prim.primID();
  47. bounds.extend(scene->get(geomID)->vbounds(primID));
  48. }
  49. /* calculate offset and scale */
  50. Vec3fa loffset = bounds.lower;
  51. float lscale = reduce_min(256.0f/(bounds.size()*sqrt(3.0f)));
  52. if (bounds.size() == Vec3fa(zero)) lscale = 0.0f;
  53. *this->offset(N) = loffset;
  54. *this->scale(N) = lscale;
  55. /* encode all primitives */
  56. for (size_t i=0; i<M && begin<end; i++, begin++)
  57. {
  58. const PrimRef& prim = prims[begin];
  59. const unsigned int geomID = prim.geomID();
  60. const unsigned int primID = prim.primID();
  61. const LinearSpace3fa space2 = scene->get(geomID)->computeAlignedSpace(primID);
  62. const LinearSpace3fa space3(trunc(126.0f*space2.vx),trunc(126.0f*space2.vy),trunc(126.0f*space2.vz));
  63. const BBox3fa bounds = scene->get(geomID)->vbounds(loffset,lscale,max(length(space3.vx),length(space3.vy),length(space3.vz)),space3.transposed(),primID);
  64. bounds_vx_x(N)[i] = (char) space3.vx.x;
  65. bounds_vx_y(N)[i] = (char) space3.vx.y;
  66. bounds_vx_z(N)[i] = (char) space3.vx.z;
  67. bounds_vx_lower(N)[i] = (short) clamp(floor(bounds.lower.x),-32767.0f,32767.0f);
  68. bounds_vx_upper(N)[i] = (short) clamp(ceil (bounds.upper.x),-32767.0f,32767.0f);
  69. assert(-32767.0f <= floor(bounds.lower.x) && floor(bounds.lower.x) <= 32767.0f);
  70. assert(-32767.0f <= ceil (bounds.upper.x) && ceil (bounds.upper.x) <= 32767.0f);
  71. bounds_vy_x(N)[i] = (char) space3.vy.x;
  72. bounds_vy_y(N)[i] = (char) space3.vy.y;
  73. bounds_vy_z(N)[i] = (char) space3.vy.z;
  74. bounds_vy_lower(N)[i] = (short) clamp(floor(bounds.lower.y),-32767.0f,32767.0f);
  75. bounds_vy_upper(N)[i] = (short) clamp(ceil (bounds.upper.y),-32767.0f,32767.0f);
  76. assert(-32767.0f <= floor(bounds.lower.y) && floor(bounds.lower.y) <= 32767.0f);
  77. assert(-32767.0f <= ceil (bounds.upper.y) && ceil (bounds.upper.y) <= 32767.0f);
  78. bounds_vz_x(N)[i] = (char) space3.vz.x;
  79. bounds_vz_y(N)[i] = (char) space3.vz.y;
  80. bounds_vz_z(N)[i] = (char) space3.vz.z;
  81. bounds_vz_lower(N)[i] = (short) clamp(floor(bounds.lower.z),-32767.0f,32767.0f);
  82. bounds_vz_upper(N)[i] = (short) clamp(ceil (bounds.upper.z),-32767.0f,32767.0f);
  83. assert(-32767.0f <= floor(bounds.lower.z) && floor(bounds.lower.z) <= 32767.0f);
  84. assert(-32767.0f <= ceil (bounds.upper.z) && ceil (bounds.upper.z) <= 32767.0f);
  85. this->primID(N)[i] = primID;
  86. }
  87. }
  88. template<typename BVH, typename Allocator>
  89. __forceinline static typename BVH::NodeRef createLeaf (BVH* bvh, const PrimRef* prims, const range<size_t>& set, const Allocator& alloc)
  90. {
  91. size_t start = set.begin();
  92. size_t items = CurveNi::blocks(set.size());
  93. size_t numbytes = CurveNi::bytes(set.size());
  94. CurveNi* accel = (CurveNi*) alloc.malloc1(numbytes,BVH::byteAlignment);
  95. for (size_t i=0; i<items; i++) {
  96. accel[i].fill(prims,start,set.end(),bvh->scene);
  97. }
  98. return bvh->encodeLeaf((char*)accel,items);
  99. };
  100. public:
  101. // 27.6 - 46 bytes per primitive
  102. unsigned char ty;
  103. unsigned char N;
  104. unsigned char data[4+25*M+16];
  105. /*
  106. struct Layout
  107. {
  108. unsigned int geomID;
  109. unsigned int primID[N];
  110. char bounds_vx_x[N];
  111. char bounds_vx_y[N];
  112. char bounds_vx_z[N];
  113. short bounds_vx_lower[N];
  114. short bounds_vx_upper[N];
  115. char bounds_vy_x[N];
  116. char bounds_vy_y[N];
  117. char bounds_vy_z[N];
  118. short bounds_vy_lower[N];
  119. short bounds_vy_upper[N];
  120. char bounds_vz_x[N];
  121. char bounds_vz_y[N];
  122. char bounds_vz_z[N];
  123. short bounds_vz_lower[N];
  124. short bounds_vz_upper[N];
  125. Vec3f offset;
  126. float scale;
  127. };
  128. */
  129. __forceinline unsigned int& geomID(size_t N) { return *(unsigned int*)((char*)this+2); }
  130. __forceinline const unsigned int& geomID(size_t N) const { return *(unsigned int*)((char*)this+2); }
  131. __forceinline unsigned int* primID(size_t N) { return (unsigned int*)((char*)this+6); }
  132. __forceinline const unsigned int* primID(size_t N) const { return (unsigned int*)((char*)this+6); }
  133. __forceinline char* bounds_vx_x(size_t N) { return (char*)((char*)this+6+4*N); }
  134. __forceinline const char* bounds_vx_x(size_t N) const { return (char*)((char*)this+6+4*N); }
  135. __forceinline char* bounds_vx_y(size_t N) { return (char*)((char*)this+6+5*N); }
  136. __forceinline const char* bounds_vx_y(size_t N) const { return (char*)((char*)this+6+5*N); }
  137. __forceinline char* bounds_vx_z(size_t N) { return (char*)((char*)this+6+6*N); }
  138. __forceinline const char* bounds_vx_z(size_t N) const { return (char*)((char*)this+6+6*N); }
  139. __forceinline short* bounds_vx_lower(size_t N) { return (short*)((char*)this+6+7*N); }
  140. __forceinline const short* bounds_vx_lower(size_t N) const { return (short*)((char*)this+6+7*N); }
  141. __forceinline short* bounds_vx_upper(size_t N) { return (short*)((char*)this+6+9*N); }
  142. __forceinline const short* bounds_vx_upper(size_t N) const { return (short*)((char*)this+6+9*N); }
  143. __forceinline char* bounds_vy_x(size_t N) { return (char*)((char*)this+6+11*N); }
  144. __forceinline const char* bounds_vy_x(size_t N) const { return (char*)((char*)this+6+11*N); }
  145. __forceinline char* bounds_vy_y(size_t N) { return (char*)((char*)this+6+12*N); }
  146. __forceinline const char* bounds_vy_y(size_t N) const { return (char*)((char*)this+6+12*N); }
  147. __forceinline char* bounds_vy_z(size_t N) { return (char*)((char*)this+6+13*N); }
  148. __forceinline const char* bounds_vy_z(size_t N) const { return (char*)((char*)this+6+13*N); }
  149. __forceinline short* bounds_vy_lower(size_t N) { return (short*)((char*)this+6+14*N); }
  150. __forceinline const short* bounds_vy_lower(size_t N) const { return (short*)((char*)this+6+14*N); }
  151. __forceinline short* bounds_vy_upper(size_t N) { return (short*)((char*)this+6+16*N); }
  152. __forceinline const short* bounds_vy_upper(size_t N) const { return (short*)((char*)this+6+16*N); }
  153. __forceinline char* bounds_vz_x(size_t N) { return (char*)((char*)this+6+18*N); }
  154. __forceinline const char* bounds_vz_x(size_t N) const { return (char*)((char*)this+6+18*N); }
  155. __forceinline char* bounds_vz_y(size_t N) { return (char*)((char*)this+6+19*N); }
  156. __forceinline const char* bounds_vz_y(size_t N) const { return (char*)((char*)this+6+19*N); }
  157. __forceinline char* bounds_vz_z(size_t N) { return (char*)((char*)this+6+20*N); }
  158. __forceinline const char* bounds_vz_z(size_t N) const { return (char*)((char*)this+6+20*N); }
  159. __forceinline short* bounds_vz_lower(size_t N) { return (short*)((char*)this+6+21*N); }
  160. __forceinline const short* bounds_vz_lower(size_t N) const { return (short*)((char*)this+6+21*N); }
  161. __forceinline short* bounds_vz_upper(size_t N) { return (short*)((char*)this+6+23*N); }
  162. __forceinline const short* bounds_vz_upper(size_t N) const { return (short*)((char*)this+6+23*N); }
  163. __forceinline Vec3f* offset(size_t N) { return (Vec3f*)((char*)this+6+25*N); }
  164. __forceinline const Vec3f* offset(size_t N) const { return (Vec3f*)((char*)this+6+25*N); }
  165. __forceinline float* scale(size_t N) { return (float*)((char*)this+6+25*N+12); }
  166. __forceinline const float* scale(size_t N) const { return (float*)((char*)this+6+25*N+12); }
  167. __forceinline char* end(size_t N) { return (char*)this+6+25*N+16; }
  168. __forceinline const char* end(size_t N) const { return (char*)this+6+25*N+16; }
  169. };
  170. template<int M>
  171. typename CurveNi<M>::Type CurveNi<M>::type;
  172. typedef CurveNi<4> Curve4i;
  173. typedef CurveNi<8> Curve8i;
  174. }