scene_instance_array.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "geometry.h"
  5. #include "accel.h"
  6. namespace embree
  7. {
  8. struct MotionDerivativeCoefficients;
  9. /*! Instanced acceleration structure */
  10. struct InstanceArray : public Geometry
  11. {
  12. //ALIGNED_STRUCT_(16);
  13. static const Geometry::GTypeMask geom_type = Geometry::MTY_INSTANCE_ARRAY;
  14. public:
  15. InstanceArray (Device* device, unsigned int numTimeSteps = 1);
  16. ~InstanceArray();
  17. private:
  18. InstanceArray (const InstanceArray& other) DELETED; // do not implement
  19. InstanceArray& operator= (const InstanceArray& other) DELETED; // do not implement
  20. private:
  21. LBBox3fa nonlinearBounds(size_t i,
  22. const BBox1f& time_range_in,
  23. const BBox1f& geom_time_range,
  24. float geom_time_segments) const;
  25. BBox3fa boundSegment(size_t i, size_t itime,
  26. BBox3fa const& obbox0, BBox3fa const& obbox1,
  27. BBox3fa const& bbox0, BBox3fa const& bbox1,
  28. float t_min, float t_max) const;
  29. /* calculates the (correct) interpolated bounds */
  30. __forceinline BBox3fa bounds(size_t i, size_t itime0, size_t itime1, float f) const
  31. {
  32. if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
  33. return xfmBounds(slerp(l2w(i, itime0), l2w(i, itime1), f),
  34. lerp(getObjectBounds(i, itime0), getObjectBounds(i, itime1), f));
  35. return xfmBounds(lerp(l2w(i, itime0), l2w(i, itime1), f),
  36. lerp(getObjectBounds(i, itime0), getObjectBounds(i, itime1), f));
  37. }
  38. public:
  39. virtual void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num) override;
  40. virtual void* getBuffer(RTCBufferType type, unsigned int slot) override;
  41. virtual void updateBuffer(RTCBufferType type, unsigned int slot) override;
  42. virtual void setNumTimeSteps (unsigned int numTimeSteps) override;
  43. virtual void setInstancedScene(const Ref<Scene>& scene) override;
  44. virtual void setInstancedScenes(const RTCScene* scenes, size_t numScenes) override;
  45. virtual AffineSpace3fa getTransform(size_t, float time) override;
  46. virtual void setMask (unsigned mask) override;
  47. virtual void build() {}
  48. virtual void addElementsToCount (GeometryCounts & counts) const override;
  49. virtual void commit() override;
  50. public:
  51. /*! calculates the bounds of instance */
  52. __forceinline BBox3fa bounds(size_t i) const {
  53. if (!valid(i))
  54. return BBox3fa();
  55. if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
  56. return xfmBounds(quaternionDecompositionToAffineSpace(l2w(i, 0)),getObject(i)->bounds.bounds());
  57. return xfmBounds(l2w(i, 0),getObject(i)->bounds.bounds());
  58. }
  59. /*! gets the bounds of the instanced scene */
  60. __forceinline BBox3fa getObjectBounds(size_t i, size_t itime) const {
  61. if (!valid(i))
  62. return BBox3fa();
  63. return getObject(i)->getBounds(timeStep(itime));
  64. }
  65. /*! calculates the bounds of instance */
  66. __forceinline BBox3fa bounds(size_t i, size_t itime) const {
  67. if (!valid(i))
  68. return BBox3fa();
  69. if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
  70. return xfmBounds(quaternionDecompositionToAffineSpace(l2w(i, itime)),getObjectBounds(i, itime));
  71. return xfmBounds(l2w(i, itime),getObjectBounds(i, itime));
  72. }
  73. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  74. __forceinline LBBox3fa linearBounds(size_t i, const BBox1f& dt) const {
  75. if (!valid(i))
  76. return LBBox3fa();
  77. LBBox3fa lbbox = nonlinearBounds(i, dt, time_range, fnumTimeSegments);
  78. return lbbox;
  79. }
  80. /*! calculates the build bounds of the i'th item, if it's valid */
  81. __forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
  82. {
  83. if (!valid(i))
  84. return false;
  85. const BBox3fa b = bounds(i);
  86. if (bbox) *bbox = b;
  87. return isvalid(b);
  88. }
  89. /*! calculates the build bounds of the i'th item at the itime'th time segment, if it's valid */
  90. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  91. {
  92. if (!valid(i))
  93. return false;
  94. const LBBox3fa bounds = linearBounds(i,itime);
  95. bbox = bounds.bounds ();
  96. return isvalid(bounds);
  97. }
  98. /* gets version info of topology */
  99. unsigned int getTopologyVersion() const {
  100. return numPrimitives;
  101. }
  102. /* returns true if topology changed */
  103. bool topologyChanged(unsigned int otherVersion) const {
  104. return numPrimitives != otherVersion;
  105. }
  106. /*! check if the i'th primitive is valid between the specified time range */
  107. __forceinline bool valid(size_t i) const
  108. {
  109. if (object) return true;
  110. return (object_ids[i] != (unsigned int)(-1));
  111. }
  112. /*! check if the i'th primitive is valid between the specified time range */
  113. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  114. {
  115. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
  116. if (!isvalid(bounds(i,itime))) return false;
  117. return true;
  118. }
  119. __forceinline AffineSpace3fa getLocal2World(size_t i) const
  120. {
  121. if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
  122. return quaternionDecompositionToAffineSpace(l2w(i,0));
  123. return l2w(i, 0);
  124. }
  125. __forceinline AffineSpace3fa getLocal2World(size_t i, float t) const
  126. {
  127. if (numTimeSegments() > 0) {
  128. float ftime; const unsigned int itime = timeSegment(t, ftime);
  129. if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
  130. return slerp(l2w(i, itime+0),l2w(i, itime+1),ftime);
  131. return lerp(l2w(i, itime+0),l2w(i, itime+1),ftime);
  132. }
  133. return getLocal2World(i);
  134. }
  135. __forceinline AffineSpace3fa getWorld2Local(size_t i) const {
  136. return rcp(getLocal2World(i));
  137. }
  138. __forceinline AffineSpace3fa getWorld2Local(size_t i, float t) const {
  139. return rcp(getLocal2World(i, t));
  140. }
  141. template<int K>
  142. __forceinline AffineSpace3vf<K> getWorld2Local(size_t i, const vbool<K>& valid, const vfloat<K>& t) const
  143. {
  144. if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
  145. return getWorld2LocalSlerp<K>(i, valid, t);
  146. return getWorld2LocalLerp<K>(i, valid, t);
  147. }
  148. __forceinline float projectedPrimitiveArea(const size_t i) const {
  149. return area(bounds(i));
  150. }
  151. inline Accel* getObject(size_t i) const {
  152. if (object) {
  153. return object;
  154. }
  155. assert(objects);
  156. assert(i < numPrimitives);
  157. if (object_ids[i] == (unsigned int)(-1))
  158. return nullptr;
  159. assert(object_ids[i] < numObjects);
  160. return objects[object_ids[i]];
  161. }
  162. private:
  163. template<int K>
  164. __forceinline AffineSpace3vf<K> getWorld2LocalSlerp(size_t i, const vbool<K>& valid, const vfloat<K>& t) const
  165. {
  166. vfloat<K> ftime;
  167. const vint<K> itime_k = timeSegment<K>(t, ftime);
  168. assert(any(valid));
  169. const size_t index = bsf(movemask(valid));
  170. const int itime = itime_k[index];
  171. if (likely(all(valid, itime_k == vint<K>(itime)))) {
  172. return rcp(slerp(AffineSpace3vff<K>(l2w(i, itime+0)),
  173. AffineSpace3vff<K>(l2w(i, itime+1)),
  174. ftime));
  175. }
  176. else {
  177. AffineSpace3vff<K> space0,space1;
  178. vbool<K> valid1 = valid;
  179. while (any(valid1)) {
  180. vbool<K> valid2;
  181. const int itime = next_unique(valid1, itime_k, valid2);
  182. space0 = select(valid2, AffineSpace3vff<K>(l2w(i, itime+0)), space0);
  183. space1 = select(valid2, AffineSpace3vff<K>(l2w(i, itime+1)), space1);
  184. }
  185. return rcp(slerp(space0, space1, ftime));
  186. }
  187. }
  188. template<int K>
  189. __forceinline AffineSpace3vf<K> getWorld2LocalLerp(size_t i, const vbool<K>& valid, const vfloat<K>& t) const
  190. {
  191. vfloat<K> ftime;
  192. const vint<K> itime_k = timeSegment<K>(t, ftime);
  193. assert(any(valid));
  194. const size_t index = bsf(movemask(valid));
  195. const int itime = itime_k[index];
  196. if (likely(all(valid, itime_k == vint<K>(itime)))) {
  197. return rcp(lerp(AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+0)),
  198. AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+1)),
  199. ftime));
  200. } else {
  201. AffineSpace3vf<K> space0,space1;
  202. vbool<K> valid1 = valid;
  203. while (any(valid1)) {
  204. vbool<K> valid2;
  205. const int itime = next_unique(valid1, itime_k, valid2);
  206. space0 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+0)), space0);
  207. space1 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+1)), space1);
  208. }
  209. return rcp(lerp(space0, space1, ftime));
  210. }
  211. }
  212. private:
  213. __forceinline AffineSpace3ff l2w(size_t i, size_t itime) const {
  214. if (l2w_buf[itime].getFormat() == RTC_FORMAT_FLOAT4X4_COLUMN_MAJOR) {
  215. return *(AffineSpace3ff*)(l2w_buf[itime].getPtr(i));
  216. }
  217. else if(l2w_buf[itime].getFormat() == RTC_FORMAT_QUATERNION_DECOMPOSITION) {
  218. AffineSpace3ff transform;
  219. QuaternionDecomposition* qd = (QuaternionDecomposition*)l2w_buf[itime].getPtr(i);
  220. transform.l.vx.x = qd->scale_x;
  221. transform.l.vy.y = qd->scale_y;
  222. transform.l.vz.z = qd->scale_z;
  223. transform.l.vy.x = qd->skew_xy;
  224. transform.l.vz.x = qd->skew_xz;
  225. transform.l.vz.y = qd->skew_yz;
  226. transform.l.vx.y = qd->translation_x;
  227. transform.l.vx.z = qd->translation_y;
  228. transform.l.vy.z = qd->translation_z;
  229. transform.p.x = qd->shift_x;
  230. transform.p.y = qd->shift_y;
  231. transform.p.z = qd->shift_z;
  232. // normalize quaternion
  233. Quaternion3f q(qd->quaternion_r, qd->quaternion_i, qd->quaternion_j, qd->quaternion_k);
  234. q = normalize(q);
  235. transform.l.vx.w = q.i;
  236. transform.l.vy.w = q.j;
  237. transform.l.vz.w = q.k;
  238. transform.p.w = q.r;
  239. return transform;
  240. }
  241. else if (l2w_buf[itime].getFormat() == RTC_FORMAT_FLOAT3X4_COLUMN_MAJOR) {
  242. AffineSpace3f* l2w = reinterpret_cast<AffineSpace3f*>(l2w_buf[itime].getPtr(i));
  243. return AffineSpace3ff(*l2w);
  244. }
  245. else if (l2w_buf[itime].getFormat() == RTC_FORMAT_FLOAT3X4_ROW_MAJOR) {
  246. float* data = reinterpret_cast<float*>(l2w_buf[itime].getPtr(i));
  247. AffineSpace3f l2w;
  248. l2w.l.vx.x = data[0]; l2w.l.vy.x = data[1]; l2w.l.vz.x = data[2]; l2w.p.x = data[3];
  249. l2w.l.vx.y = data[4]; l2w.l.vy.y = data[5]; l2w.l.vz.y = data[6]; l2w.p.y = data[7];
  250. l2w.l.vx.z = data[8]; l2w.l.vy.z = data[9]; l2w.l.vz.z = data[10]; l2w.p.z = data[11];
  251. return l2w;
  252. }
  253. assert(false);
  254. return AffineSpace3ff();
  255. }
  256. inline AffineSpace3ff l2w(size_t i) const {
  257. return l2w(i, 0);
  258. }
  259. private:
  260. Accel* object; //!< fast path if only one scene is instanced
  261. Accel** objects;
  262. uint32_t numObjects;
  263. Device::vector<RawBufferView> l2w_buf = device; //!< transformation from local space to world space for each timestep (either normal matrix or quaternion decomposition)
  264. BufferView<uint32_t> object_ids; //!< array of scene ids per instance array primitive
  265. };
  266. namespace isa
  267. {
  268. struct InstanceArrayISA : public InstanceArray
  269. {
  270. InstanceArrayISA (Device* device)
  271. : InstanceArray(device) {}
  272. LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const {
  273. return linearBounds(primID,time_range);
  274. }
  275. PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const
  276. {
  277. PrimInfo pinfo(empty);
  278. for (size_t j = r.begin(); j < r.end(); j++) {
  279. BBox3fa bounds = empty;
  280. if (!buildBounds(j, &bounds) || !valid(j))
  281. continue;
  282. const PrimRef prim(bounds, geomID, unsigned(j));
  283. pinfo.add_center2(prim);
  284. prims[k++] = prim;
  285. }
  286. return pinfo;
  287. }
  288. PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
  289. {
  290. PrimInfo pinfo(empty);
  291. for (size_t j = r.begin(); j < r.end(); j++) {
  292. BBox3fa bounds = empty;
  293. if (!buildBounds(j, itime, bounds))
  294. continue;
  295. const PrimRef prim(bounds, geomID, unsigned(j));
  296. pinfo.add_center2(prim);
  297. prims[k++] = prim;
  298. }
  299. return pinfo;
  300. }
  301. PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const
  302. {
  303. PrimInfo pinfo(empty);
  304. const BBox1f t0t1 = BBox1f::intersect(getTimeRange(), time_range);
  305. if (t0t1.empty()) return pinfo;
  306. for (size_t j = r.begin(); j < r.end(); j++) {
  307. LBBox3fa lbounds = linearBounds(j, t0t1);
  308. if (!isvalid(lbounds.bounds()))
  309. continue;
  310. const PrimRef prim(lbounds.bounds(), geomID, unsigned(j));
  311. pinfo.add_center2(prim);
  312. prims[k++] = prim;
  313. }
  314. return pinfo;
  315. }
  316. PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
  317. {
  318. PrimInfoMB pinfo(empty);
  319. for (size_t j = r.begin(); j < r.end(); j++) {
  320. if (!valid(j, timeSegmentRange(t0t1)))
  321. continue;
  322. const PrimRefMB prim(linearBounds(j, t0t1), this->numTimeSegments(), this->time_range, this->numTimeSegments(), geomID, unsigned(j));
  323. pinfo.add_primref(prim);
  324. prims[k++] = prim;
  325. }
  326. return pinfo;
  327. }
  328. };
  329. }
  330. DECLARE_ISA_FUNCTION(InstanceArray*, createInstanceArray, Device*);
  331. }