scene_instance_array.h 14 KB

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