scene_line_segments.h 24 KB

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