scene_line_segments.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "default.h"
  5. #include "geometry.h"
  6. #include "buffer.h"
  7. namespace embree
  8. {
  9. /*! represents an array of line segments */
  10. struct LineSegments : public Geometry
  11. {
  12. /*! type of this geometry */
  13. static const Geometry::GTypeMask geom_type = Geometry::MTY_CURVE2;
  14. public:
  15. /*! line segments construction */
  16. LineSegments (Device* device, Geometry::GType gtype);
  17. public:
  18. void setMask (unsigned mask);
  19. void setNumTimeSteps (unsigned int numTimeSteps);
  20. void setVertexAttributeCount (unsigned int N);
  21. void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num);
  22. void* getBuffer(RTCBufferType type, unsigned int slot);
  23. void updateBuffer(RTCBufferType type, unsigned int slot);
  24. void commit();
  25. bool verify ();
  26. void interpolate(const RTCInterpolateArguments* const args);
  27. void setTessellationRate(float N);
  28. void setMaxRadiusScale(float s);
  29. void addElementsToCount (GeometryCounts & counts) const;
  30. template<int N>
  31. void interpolate_impl(const RTCInterpolateArguments* const args)
  32. {
  33. unsigned int primID = args->primID;
  34. float u = args->u;
  35. RTCBufferType bufferType = args->bufferType;
  36. unsigned int bufferSlot = args->bufferSlot;
  37. float* P = args->P;
  38. float* dPdu = args->dPdu;
  39. float* ddPdudu = args->ddPdudu;
  40. unsigned int valueCount = args->valueCount;
  41. /* calculate base pointer and stride */
  42. assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
  43. (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
  44. const char* src = nullptr;
  45. size_t stride = 0;
  46. if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
  47. src = vertexAttribs[bufferSlot].getPtr();
  48. stride = vertexAttribs[bufferSlot].getStride();
  49. } else {
  50. src = vertices[bufferSlot].getPtr();
  51. stride = vertices[bufferSlot].getStride();
  52. }
  53. for (unsigned int i=0; i<valueCount; i+=N)
  54. {
  55. const size_t ofs = i*sizeof(float);
  56. const size_t segment = segments[primID];
  57. const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>(int(valueCount));
  58. const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[(segment+0)*stride+ofs]);
  59. const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[(segment+1)*stride+ofs]);
  60. if (P ) mem<vfloat<N>>::storeu(valid,P+i,lerp(p0,p1,u));
  61. if (dPdu ) mem<vfloat<N>>::storeu(valid,dPdu+i,p1-p0);
  62. if (ddPdudu) mem<vfloat<N>>::storeu(valid,dPdu+i,vfloat<N>(zero));
  63. }
  64. }
  65. public:
  66. /*! returns the number of vertices */
  67. __forceinline size_t numVertices() const {
  68. return vertices[0].size();
  69. }
  70. /*! returns the i'th segment */
  71. __forceinline const unsigned int& segment(size_t i) const {
  72. return segments[i];
  73. }
  74. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  75. /*! returns the i'th segment */
  76. template<int M>
  77. __forceinline const vuint<M> vsegment(const vuint<M>& i) const {
  78. return segments[i.v];
  79. }
  80. #endif
  81. /*! returns the segment to the left of the i'th segment */
  82. __forceinline bool segmentLeftExists(size_t i) const {
  83. assert (flags);
  84. return (flags[i] & RTC_CURVE_FLAG_NEIGHBOR_LEFT) != 0;
  85. }
  86. /*! returns the segment to the right of the i'th segment */
  87. __forceinline bool segmentRightExists(size_t i) const {
  88. assert (flags);
  89. return (flags[i] & RTC_CURVE_FLAG_NEIGHBOR_RIGHT) != 0;
  90. }
  91. /*! returns i'th vertex of the first time step */
  92. __forceinline Vec3ff vertex(size_t i) const {
  93. return vertices0[i];
  94. }
  95. /*! returns i'th vertex of the first time step */
  96. __forceinline const char* vertexPtr(size_t i) const {
  97. return vertices0.getPtr(i);
  98. }
  99. /*! returns i'th normal of the first time step */
  100. __forceinline Vec3fa normal(size_t i) const {
  101. return normals0[i];
  102. }
  103. /*! returns i'th radius of the first time step */
  104. __forceinline float radius(size_t i) const {
  105. return vertices0[i].w;
  106. }
  107. /*! returns i'th vertex of itime'th timestep */
  108. __forceinline Vec3ff vertex(size_t i, size_t itime) const {
  109. return vertices[itime][i];
  110. }
  111. /*! returns i'th vertex of itime'th timestep */
  112. __forceinline const char* vertexPtr(size_t i, size_t itime) const {
  113. return vertices[itime].getPtr(i);
  114. }
  115. /*! returns i'th normal of itime'th timestep */
  116. __forceinline Vec3fa normal(size_t i, size_t itime) const {
  117. return normals[itime][i];
  118. }
  119. /*! returns i'th radius of itime'th timestep */
  120. __forceinline float radius(size_t i, size_t itime) const {
  121. return vertices[itime][i].w;
  122. }
  123. /*! gathers the curve starting with i'th vertex */
  124. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, unsigned int vid) const
  125. {
  126. p0 = vertex(vid+0);
  127. p1 = vertex(vid+1);
  128. }
  129. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  130. template<int M>
  131. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, const vuint<M>& vid) const
  132. {
  133. p0 = vertex(vid.v+0);
  134. p1 = vertex(vid.v+1);
  135. }
  136. #endif
  137. /*! gathers the curve starting with i'th vertex of itime'th timestep */
  138. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, unsigned int vid, size_t itime) const
  139. {
  140. p0 = vertex(vid+0,itime);
  141. p1 = vertex(vid+1,itime);
  142. }
  143. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  144. template<int M>
  145. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, const vuint<M>& vid, const vint<M>& itime) const
  146. {
  147. p0 = vertex(vid.v+0,itime.v);
  148. p1 = vertex(vid.v+1,itime.v);
  149. }
  150. #endif
  151. /*! loads curve vertices for specified time */
  152. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, unsigned int vid, float time) const
  153. {
  154. float ftime;
  155. const size_t itime = timeSegment(time, ftime);
  156. const float t0 = 1.0f - ftime;
  157. const float t1 = ftime;
  158. Vec3ff a0,a1; gather(a0,a1,vid,itime);
  159. Vec3ff b0,b1; gather(b0,b1,vid,itime+1);
  160. p0 = madd(Vec3ff(t0),a0,t1*b0);
  161. p1 = madd(Vec3ff(t0),a1,t1*b1);
  162. }
  163. /*! loads curve vertices for specified time for mblur and non-mblur case */
  164. __forceinline void gather_safe(Vec3ff& p0, Vec3ff& p1, unsigned int vid, float time) const
  165. {
  166. if (hasMotionBlur()) gather(p0,p1,vid,time);
  167. else gather(p0,p1,vid);
  168. }
  169. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  170. template<int M>
  171. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, const vuint<M>& vid, const vfloat<M>& time) const
  172. {
  173. vfloat<M> ftime;
  174. const vint<M> itime = timeSegment<M>(time, ftime);
  175. const vfloat<M> t0 = 1.0f - ftime;
  176. const vfloat<M> t1 = ftime;
  177. Vec4vf<M> a0,a1; vgather<M>(a0,a1,vid,itime);
  178. Vec4vf<M> b0,b1; vgather<M>(b0,b1,vid,itime+1);
  179. p0 = madd(Vec4vf<M>(t0),a0,t1*b0);
  180. p1 = madd(Vec4vf<M>(t0),a1,t1*b1);
  181. }
  182. #endif
  183. /*! gathers the cone curve starting with i'th vertex */
  184. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, bool& cL, bool& cR, unsigned int primID, unsigned int vid) const
  185. {
  186. gather(p0,p1,vid);
  187. cL = !segmentLeftExists (primID);
  188. cR = !segmentRightExists(primID);
  189. }
  190. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  191. template<int M>
  192. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, vbool<M>& cL, vbool<M>& cR, const vuint<M>& primID, const vuint<M>& vid) const
  193. {
  194. vgather<M>(p0,p1,vid);
  195. cL = !segmentLeftExists (primID.v);
  196. cR = !segmentRightExists(primID.v);
  197. }
  198. #endif
  199. /*! gathers the cone curve starting with i'th vertex of itime'th timestep */
  200. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, bool& cL, bool& cR, unsigned int primID, size_t vid, size_t itime) const
  201. {
  202. gather(p0,p1,vid,itime);
  203. cL = !segmentLeftExists (primID);
  204. cR = !segmentRightExists(primID);
  205. }
  206. /*! loads cone curve vertices for specified time */
  207. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, bool& cL, bool& cR, unsigned int primID, size_t vid, float time) const
  208. {
  209. gather(p0,p1,vid,time);
  210. cL = !segmentLeftExists (primID);
  211. cR = !segmentRightExists(primID);
  212. }
  213. /*! loads cone curve vertices for specified time for mblur and non-mblur geometry */
  214. __forceinline void gather_safe(Vec3ff& p0, Vec3ff& p1, bool& cL, bool& cR, unsigned int primID, size_t vid, float time) const
  215. {
  216. if (hasMotionBlur()) gather(p0,p1,cL,cR,primID,vid,time);
  217. else gather(p0,p1,cL,cR,primID,vid);
  218. }
  219. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  220. template<int M>
  221. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, vbool<M>& cL, vbool<M>& cR, const vuint<M>& primID, const vuint<M>& vid, const vfloat<M>& time) const
  222. {
  223. vgather<M>(p0,p1,vid,time);
  224. cL = !segmentLeftExists (primID.v);
  225. cR = !segmentRightExists(primID.v);
  226. }
  227. #endif
  228. /*! gathers the curve starting with i'th vertex */
  229. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, unsigned int primID, size_t vid) const
  230. {
  231. p0 = vertex(vid+0);
  232. p1 = vertex(vid+1);
  233. p2 = segmentLeftExists (primID) ? vertex(vid-1) : Vec3ff(inf);
  234. p3 = segmentRightExists(primID) ? vertex(vid+2) : Vec3ff(inf);
  235. }
  236. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  237. template<int M>
  238. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, Vec4vf<M>& p2, Vec4vf<M>& p3, const vuint<M>& primID, const vuint<M>& vid) const
  239. {
  240. p0 = vertex(vid.v+0);
  241. p1 = vertex(vid.v+1);
  242. vbool<M> left = segmentLeftExists (primID.v);
  243. vbool<M> right = segmentRightExists(primID.v);
  244. vuint<M> i2 = select(left, vid-1,vid+0);
  245. vuint<M> i3 = select(right,vid+2,vid+1);
  246. p2 = vertex(i2.v);
  247. p3 = vertex(i3.v);
  248. p2 = select(left, p2,Vec4vf<M>(inf));
  249. p3 = select(right,p3,Vec4vf<M>(inf));
  250. }
  251. #endif
  252. /*! gathers the curve starting with i'th vertex of itime'th timestep */
  253. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, unsigned int primID, size_t vid, size_t itime) const
  254. {
  255. p0 = vertex(vid+0,itime);
  256. p1 = vertex(vid+1,itime);
  257. p2 = segmentLeftExists (primID) ? vertex(vid-1,itime) : Vec3ff(inf);
  258. p3 = segmentRightExists(primID) ? vertex(vid+2,itime) : Vec3ff(inf);
  259. }
  260. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  261. template<int M>
  262. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, Vec4vf<M>& p2, Vec4vf<M>& p3, const vuint<M>& primID, const vuint<M>& vid, const vint<M>& itime) const
  263. {
  264. p0 = vertex(vid.v+0, itime.v);
  265. p1 = vertex(vid.v+1, itime.v);
  266. vbool<M> left = segmentLeftExists (primID.v);
  267. vbool<M> right = segmentRightExists(primID.v);
  268. vuint<M> i2 = select(left, vid-1,vid+0);
  269. vuint<M> i3 = select(right,vid+2,vid+1);
  270. p2 = vertex(i2.v, itime.v);
  271. p3 = vertex(i3.v, itime.v);
  272. p2 = select(left, p2,Vec4vf<M>(inf));
  273. p3 = select(right,p3,Vec4vf<M>(inf));
  274. }
  275. #endif
  276. /*! loads curve vertices for specified time */
  277. __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, unsigned int primID, size_t vid, float time) const
  278. {
  279. float ftime;
  280. const size_t itime = timeSegment(time, ftime);
  281. const float t0 = 1.0f - ftime;
  282. const float t1 = ftime;
  283. Vec3ff a0,a1,a2,a3; gather(a0,a1,a2,a3,primID,vid,itime);
  284. Vec3ff b0,b1,b2,b3; gather(b0,b1,b2,b3,primID,vid,itime+1);
  285. p0 = madd(Vec3ff(t0),a0,t1*b0);
  286. p1 = madd(Vec3ff(t0),a1,t1*b1);
  287. p2 = madd(Vec3ff(t0),a2,t1*b2);
  288. p3 = madd(Vec3ff(t0),a3,t1*b3);
  289. }
  290. /*! loads curve vertices for specified time for mblur and non-mblur geometry */
  291. __forceinline void gather_safe(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, unsigned int primID, size_t vid, float time) const
  292. {
  293. if (hasMotionBlur()) gather(p0,p1,p2,p3,primID,vid,time);
  294. else gather(p0,p1,p2,p3,primID,vid);
  295. }
  296. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  297. template<int M>
  298. __forceinline void vgather(Vec4vf<M>& p0, Vec4vf<M>& p1, Vec4vf<M>& p2, Vec4vf<M>& p3, const vuint<M>& primID, const vuint<M>& vid, const vfloat<M>& time) const
  299. {
  300. vfloat<M> ftime;
  301. const vint<M> itime = timeSegment<M>(time, ftime);
  302. const vfloat<M> t0 = 1.0f - ftime;
  303. const vfloat<M> t1 = ftime;
  304. Vec4vf<M> a0,a1,a2,a3; vgather<M>(a0,a1,a2,a3,primID,vid,itime);
  305. Vec4vf<M> b0,b1,b2,b3; vgather<M>(b0,b1,b2,b3,primID,vid,itime+1);
  306. p0 = madd(Vec4vf<M>(t0),a0,t1*b0);
  307. p1 = madd(Vec4vf<M>(t0),a1,t1*b1);
  308. p2 = madd(Vec4vf<M>(t0),a2,t1*b2);
  309. p3 = madd(Vec4vf<M>(t0),a3,t1*b3);
  310. }
  311. #endif
  312. /*! calculates bounding box of i'th line segment */
  313. __forceinline BBox3fa bounds(const Vec3ff& v0, const Vec3ff& v1) const
  314. {
  315. const BBox3ff b = merge(BBox3ff(v0),BBox3ff(v1));
  316. return enlarge((BBox3fa)b,maxRadiusScale*Vec3fa(max(v0.w,v1.w)));
  317. }
  318. /*! calculates bounding box of i'th line segment */
  319. __forceinline BBox3fa bounds(size_t i) const
  320. {
  321. const unsigned int index = segment(i);
  322. const Vec3ff v0 = vertex(index+0);
  323. const Vec3ff v1 = vertex(index+1);
  324. return bounds(v0,v1);
  325. }
  326. /*! calculates bounding box of i'th line segment for the itime'th time step */
  327. __forceinline BBox3fa bounds(size_t i, size_t itime) const
  328. {
  329. const unsigned int index = segment(i);
  330. const Vec3ff v0 = vertex(index+0,itime);
  331. const Vec3ff v1 = vertex(index+1,itime);
  332. return bounds(v0,v1);
  333. }
  334. /*! calculates bounding box of i'th line segment */
  335. __forceinline BBox3fa bounds(const LinearSpace3fa& space, size_t i) const
  336. {
  337. const unsigned int index = segment(i);
  338. const Vec3ff v0 = vertex(index+0);
  339. const Vec3ff v1 = vertex(index+1);
  340. const Vec3ff w0(xfmVector(space,(Vec3fa)v0),v0.w);
  341. const Vec3ff w1(xfmVector(space,(Vec3fa)v1),v1.w);
  342. return bounds(w0,w1);
  343. }
  344. /*! calculates bounding box of i'th line segment for the itime'th time step */
  345. __forceinline BBox3fa bounds(const LinearSpace3fa& space, size_t i, size_t itime) const
  346. {
  347. const unsigned int index = segment(i);
  348. const Vec3ff v0 = vertex(index+0,itime);
  349. const Vec3ff v1 = vertex(index+1,itime);
  350. const Vec3ff w0(xfmVector(space,(Vec3fa)v0),v0.w);
  351. const Vec3ff w1(xfmVector(space,(Vec3fa)v1),v1.w);
  352. return bounds(w0,w1);
  353. }
  354. /*! calculates bounding box of i'th segment */
  355. __forceinline BBox3fa bounds(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t i, size_t itime = 0) const
  356. {
  357. const float r_scale = r_scale0*scale;
  358. const unsigned int index = segment(i);
  359. const Vec3ff v0 = vertex(index+0,itime);
  360. const Vec3ff v1 = vertex(index+1,itime);
  361. const Vec3ff w0(xfmVector(space,(v0-ofs)*Vec3fa(scale)),maxRadiusScale*v0.w*r_scale);
  362. const Vec3ff w1(xfmVector(space,(v1-ofs)*Vec3fa(scale)),maxRadiusScale*v1.w*r_scale);
  363. return bounds(w0,w1);
  364. }
  365. /*! check if the i'th primitive is valid at the itime'th timestep */
  366. __forceinline bool valid(size_t i, size_t itime) const {
  367. return valid(i, make_range(itime, itime));
  368. }
  369. /*! check if the i'th primitive is valid between the specified time range */
  370. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  371. {
  372. const unsigned int index = segment(i);
  373. if (index+1 >= numVertices()) return false;
  374. #if !defined(__SYCL_DEVICE_ONLY__)
  375. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
  376. {
  377. const Vec3ff v0 = vertex(index+0,itime); if (unlikely(!isvalid4(v0))) return false;
  378. const Vec3ff v1 = vertex(index+1,itime); if (unlikely(!isvalid4(v1))) return false;
  379. if (min(v0.w,v1.w) < 0.0f) return false;
  380. }
  381. #endif
  382. return true;
  383. }
  384. /*! calculates the linear bounds of the i'th primitive at the itimeGlobal'th time segment */
  385. __forceinline LBBox3fa linearBounds(size_t i, size_t itime) const {
  386. return LBBox3fa(bounds(i,itime+0),bounds(i,itime+1));
  387. }
  388. /*! calculates the build bounds of the i'th primitive, if it's valid */
  389. __forceinline bool buildBounds(size_t i, BBox3fa* bbox) const
  390. {
  391. if (!valid(i,0)) return false;
  392. *bbox = bounds(i);
  393. return true;
  394. }
  395. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  396. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  397. {
  398. if (!valid(i,itime+0) || !valid(i,itime+1)) return false;
  399. bbox = bounds(i,itime); // use bounds of first time step in builder
  400. return true;
  401. }
  402. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  403. __forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
  404. return LBBox3fa([&] (size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
  405. }
  406. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  407. __forceinline LBBox3fa linearBounds(const LinearSpace3fa& space, size_t primID, const BBox1f& dt) const {
  408. return LBBox3fa([&] (size_t itime) { return bounds(space, primID, itime); }, dt, time_range, fnumTimeSegments);
  409. }
  410. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  411. __forceinline LBBox3fa linearBounds(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t primID, const BBox1f& dt) const {
  412. return LBBox3fa([&] (size_t itime) { return bounds(ofs, scale, r_scale0, space, primID, itime); }, dt, this->time_range, fnumTimeSegments);
  413. }
  414. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  415. __forceinline bool linearBounds(size_t i, const BBox1f& time_range, LBBox3fa& bbox) const
  416. {
  417. if (!valid(i, timeSegmentRange(time_range))) return false;
  418. bbox = linearBounds(i, time_range);
  419. return true;
  420. }
  421. /*! get fast access to first vertex buffer */
  422. __forceinline float * getCompactVertexArray () const {
  423. return (float*) vertices0.getPtr();
  424. }
  425. public:
  426. BufferView<unsigned int> segments; //!< array of line segment indices
  427. BufferView<Vec3ff> vertices0; //!< fast access to first vertex buffer
  428. BufferView<Vec3fa> normals0; //!< fast access to first normal buffer
  429. BufferView<char> flags; //!< start, end flag per segment
  430. Device::vector<BufferView<Vec3ff>> vertices = device; //!< vertex array for each timestep
  431. Device::vector<BufferView<Vec3fa>> normals = device; //!< normal array for each timestep
  432. Device::vector<BufferView<char>> vertexAttribs = device; //!< user buffers
  433. int tessellationRate; //!< tessellation rate for bezier curve
  434. float maxRadiusScale = 1.0; //!< maximal min-width scaling of curve radii
  435. };
  436. namespace isa
  437. {
  438. struct LineSegmentsISA : public LineSegments
  439. {
  440. LineSegmentsISA (Device* device, Geometry::GType gtype)
  441. : LineSegments(device,gtype) {}
  442. LinearSpace3fa computeAlignedSpace(const size_t primID) const
  443. {
  444. const Vec3fa dir = normalize(computeDirection(primID));
  445. if (is_finite(dir)) return frame(dir);
  446. else return LinearSpace3fa(one);
  447. }
  448. LinearSpace3fa computeAlignedSpaceMB(const size_t primID, const BBox1f time_range) const
  449. {
  450. Vec3fa axisz(0,0,1);
  451. Vec3fa axisy(0,1,0);
  452. const range<int> tbounds = this->timeSegmentRange(time_range);
  453. if (tbounds.size() == 0) return frame(axisz);
  454. const size_t itime = (tbounds.begin()+tbounds.end())/2;
  455. const Vec3fa dir = normalize(computeDirection(primID,itime));
  456. if (is_finite(dir)) return frame(dir);
  457. else return LinearSpace3fa(one);
  458. }
  459. Vec3fa computeDirection(unsigned int primID) const
  460. {
  461. const unsigned vtxID = segment(primID);
  462. const Vec3fa v0 = vertex(vtxID+0);
  463. const Vec3fa v1 = vertex(vtxID+1);
  464. return v1-v0;
  465. }
  466. Vec3fa computeDirection(unsigned int primID, size_t time) const
  467. {
  468. const unsigned vtxID = segment(primID);
  469. const Vec3fa v0 = vertex(vtxID+0,time);
  470. const Vec3fa v1 = vertex(vtxID+1,time);
  471. return v1-v0;
  472. }
  473. PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const
  474. {
  475. PrimInfo pinfo(empty);
  476. for (size_t j=r.begin(); j<r.end(); j++)
  477. {
  478. BBox3fa bounds = empty;
  479. if (!buildBounds(j,&bounds)) continue;
  480. const PrimRef prim(bounds,geomID,unsigned(j));
  481. pinfo.add_center2(prim);
  482. prims[k++] = prim;
  483. }
  484. return pinfo;
  485. }
  486. PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
  487. {
  488. PrimInfo pinfo(empty);
  489. for (size_t j=r.begin(); j<r.end(); j++)
  490. {
  491. BBox3fa bounds = empty;
  492. if (!buildBounds(j,itime,bounds)) continue;
  493. const PrimRef prim(bounds,geomID,unsigned(j));
  494. pinfo.add_center2(prim);
  495. prims[k++] = prim;
  496. }
  497. return pinfo;
  498. }
  499. PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const
  500. {
  501. PrimInfo pinfo(empty);
  502. const BBox1f t0t1 = BBox1f::intersect(getTimeRange(), time_range);
  503. if (t0t1.empty()) return pinfo;
  504. for (size_t j = r.begin(); j < r.end(); j++) {
  505. LBBox3fa lbounds = empty;
  506. if (!linearBounds(j, t0t1, lbounds))
  507. continue;
  508. const PrimRef prim(lbounds.bounds(), geomID, unsigned(j));
  509. pinfo.add_center2(prim);
  510. prims[k++] = prim;
  511. }
  512. return pinfo;
  513. }
  514. PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
  515. {
  516. PrimInfoMB pinfo(empty);
  517. for (size_t j=r.begin(); j<r.end(); j++)
  518. {
  519. if (!valid(j, timeSegmentRange(t0t1))) continue;
  520. const PrimRefMB prim(linearBounds(j,t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(j));
  521. pinfo.add_primref(prim);
  522. prims[k++] = prim;
  523. }
  524. return pinfo;
  525. }
  526. BBox3fa vbounds(size_t i) const {
  527. return bounds(i);
  528. }
  529. BBox3fa vbounds(const LinearSpace3fa& space, size_t i) const {
  530. return bounds(space,i);
  531. }
  532. BBox3fa vbounds(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t i, size_t itime = 0) const {
  533. return bounds(ofs,scale,r_scale0,space,i,itime);
  534. }
  535. LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const {
  536. return linearBounds(primID,time_range);
  537. }
  538. LBBox3fa vlinearBounds(const LinearSpace3fa& space, size_t primID, const BBox1f& time_range) const {
  539. return linearBounds(space,primID,time_range);
  540. }
  541. LBBox3fa vlinearBounds(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t primID, const BBox1f& time_range) const {
  542. return linearBounds(ofs,scale,r_scale0,space,primID,time_range);
  543. }
  544. };
  545. }
  546. DECLARE_ISA_FUNCTION(LineSegments*, createLineSegments, Device* COMMA Geometry::GType);
  547. }