instance_stack.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "rtcore.h"
  5. namespace embree {
  6. namespace instance_id_stack {
  7. static_assert(RTC_MAX_INSTANCE_LEVEL_COUNT > 0,
  8. "RTC_MAX_INSTANCE_LEVEL_COUNT must be greater than 0.");
  9. /*******************************************************************************
  10. * Instance ID stack manipulation.
  11. * This is used from the instance intersector.
  12. ******************************************************************************/
  13. /*
  14. * Push an instance to the stack.
  15. */
  16. template<typename Context>
  17. RTC_FORCEINLINE bool push(Context context,
  18. unsigned instanceId,
  19. unsigned instancePrimId)
  20. {
  21. #if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
  22. const bool spaceAvailable = context->instStackSize < RTC_MAX_INSTANCE_LEVEL_COUNT;
  23. /* We assert here because instances are silently dropped when the stack is full.
  24. This might be quite hard to find in production. */
  25. assert(spaceAvailable);
  26. if (likely(spaceAvailable)) {
  27. context->instID[context->instStackSize] = instanceId;
  28. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  29. context->instPrimID[context->instStackSize] = instancePrimId;
  30. #endif
  31. context->instStackSize++;
  32. }
  33. return spaceAvailable;
  34. #else
  35. const bool spaceAvailable = (context->instID[0] == RTC_INVALID_GEOMETRY_ID);
  36. assert(spaceAvailable);
  37. if (likely(spaceAvailable)) {
  38. context->instID[0] = instanceId;
  39. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  40. context->instPrimID[0] = instancePrimId;
  41. #endif
  42. }
  43. return spaceAvailable;
  44. #endif
  45. }
  46. /*
  47. * Pop the last instance pushed to the stack.
  48. * Do not call on an empty stack.
  49. */
  50. template<typename Context>
  51. RTC_FORCEINLINE void pop(Context context)
  52. {
  53. assert(context);
  54. #if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
  55. assert(context->instStackSize > 0);
  56. --context->instStackSize;
  57. context->instID[context->instStackSize] = RTC_INVALID_GEOMETRY_ID;
  58. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  59. context->instPrimID[context->instStackSize] = RTC_INVALID_GEOMETRY_ID;
  60. #endif
  61. #else
  62. assert(context->instID[0] != RTC_INVALID_GEOMETRY_ID);
  63. context->instID[0] = RTC_INVALID_GEOMETRY_ID;
  64. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  65. context->instPrimID[0] = RTC_INVALID_GEOMETRY_ID;
  66. #endif
  67. #endif
  68. }
  69. /* Push an instance to the stack. Used for point queries*/
  70. RTC_FORCEINLINE bool push(RTCPointQueryContext* context,
  71. unsigned int instanceId,
  72. unsigned int instancePrimId,
  73. AffineSpace3fa const& w2i,
  74. AffineSpace3fa const& i2w)
  75. {
  76. assert(context);
  77. const size_t stackSize = context->instStackSize;
  78. assert(stackSize < RTC_MAX_INSTANCE_LEVEL_COUNT);
  79. context->instID[stackSize] = instanceId;
  80. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  81. context->instPrimID[stackSize] = instancePrimId;
  82. #endif
  83. AffineSpace3fa_store_unaligned(w2i,(AffineSpace3fa*)context->world2inst[stackSize]);
  84. AffineSpace3fa_store_unaligned(i2w,(AffineSpace3fa*)context->inst2world[stackSize]);
  85. #if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
  86. if (unlikely(stackSize > 0))
  87. {
  88. const AffineSpace3fa world2inst = AffineSpace3fa_load_unaligned((AffineSpace3fa*)context->world2inst[stackSize ])
  89. * AffineSpace3fa_load_unaligned((AffineSpace3fa*)context->world2inst[stackSize-1]);
  90. const AffineSpace3fa inst2world = AffineSpace3fa_load_unaligned((AffineSpace3fa*)context->inst2world[stackSize-1])
  91. * AffineSpace3fa_load_unaligned((AffineSpace3fa*)context->inst2world[stackSize ]);
  92. AffineSpace3fa_store_unaligned(world2inst,(AffineSpace3fa*)context->world2inst[stackSize]);
  93. AffineSpace3fa_store_unaligned(inst2world,(AffineSpace3fa*)context->inst2world[stackSize]);
  94. }
  95. #endif
  96. context->instStackSize++;
  97. return true;
  98. }
  99. template<>
  100. RTC_FORCEINLINE void pop(RTCPointQueryContext* context)
  101. {
  102. assert(context);
  103. #if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
  104. assert(context->instStackSize > 0);
  105. #else
  106. assert(context->instID[0] != RTC_INVALID_GEOMETRY_ID);
  107. #endif
  108. --context->instStackSize;
  109. context->instID[context->instStackSize] = RTC_INVALID_GEOMETRY_ID;
  110. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  111. context->instPrimID[context->instStackSize] = RTC_INVALID_GEOMETRY_ID;
  112. #endif
  113. }
  114. /*
  115. * Optimized instance id stack copy.
  116. * The copy() functions will either copy full
  117. * stacks or copy only until the last valid element has been copied, depending
  118. * on RTC_MAX_INSTANCE_LEVEL_COUNT.
  119. */
  120. RTC_FORCEINLINE void copy_UU(const unsigned* src, unsigned* tgt)
  121. {
  122. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  123. tgt[0] = src[0];
  124. #else
  125. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  126. tgt[l] = src[l];
  127. if (RTC_MAX_INSTANCE_LEVEL_COUNT > 4)
  128. if (src[l] == RTC_INVALID_GEOMETRY_ID)
  129. break;
  130. }
  131. #endif
  132. }
  133. RTC_FORCEINLINE void copy_UU(const RTCRayQueryContext* context, const unsigned* src, unsigned* tgt)
  134. {
  135. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  136. tgt[0] = src[0];
  137. #else
  138. unsigned int depth = context->instStackSize;
  139. for (unsigned l = 0; l < depth; ++l)
  140. tgt[l] = src[l];
  141. for (unsigned l = depth; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)
  142. tgt[l] = RTC_INVALID_GEOMETRY_ID;
  143. #endif
  144. }
  145. template <int K>
  146. RTC_FORCEINLINE void copy_UV(const unsigned* src, vuint<K>* tgt)
  147. {
  148. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  149. tgt[0] = src[0];
  150. #else
  151. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  152. tgt[l] = src[l];
  153. if (RTC_MAX_INSTANCE_LEVEL_COUNT > 4)
  154. if (src[l] == RTC_INVALID_GEOMETRY_ID)
  155. break;
  156. }
  157. #endif
  158. }
  159. template <int K>
  160. RTC_FORCEINLINE void copy_UV(const unsigned* src, vuint<K>* tgt, size_t j)
  161. {
  162. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  163. tgt[0][j] = src[0];
  164. #else
  165. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  166. tgt[l][j] = src[l];
  167. if (RTC_MAX_INSTANCE_LEVEL_COUNT > 4)
  168. if (src[l] == RTC_INVALID_GEOMETRY_ID)
  169. break;
  170. }
  171. #endif
  172. }
  173. template <int K>
  174. RTC_FORCEINLINE void copy_UV(const unsigned* src, vuint<K>* tgt, const vbool<K>& mask)
  175. {
  176. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  177. vuint<K>::store(mask, tgt, src[0]);
  178. #else
  179. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  180. vuint<K>::store(mask, tgt + l, src[l]);
  181. if (RTC_MAX_INSTANCE_LEVEL_COUNT > 4)
  182. if (src[l] == RTC_INVALID_GEOMETRY_ID)
  183. break;
  184. }
  185. #endif
  186. }
  187. template <int K>
  188. RTC_FORCEINLINE void copy_VU(const vuint<K>* src, unsigned* tgt, size_t i)
  189. {
  190. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  191. tgt[0] = src[0][i];
  192. #else
  193. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  194. tgt[l] = src[l][i];
  195. if (RTC_MAX_INSTANCE_LEVEL_COUNT > 4)
  196. if (src[l][i] == RTC_INVALID_GEOMETRY_ID)
  197. break;
  198. }
  199. #endif
  200. }
  201. template <int K>
  202. RTC_FORCEINLINE void copy_VV(const vuint<K>* src, vuint<K>* tgt, size_t i, size_t j)
  203. {
  204. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  205. tgt[0][j] = src[0][i];
  206. #else
  207. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  208. tgt[l][j] = src[l][i];
  209. if (RTC_MAX_INSTANCE_LEVEL_COUNT > 4)
  210. if (src[l][i] == RTC_INVALID_GEOMETRY_ID)
  211. break;
  212. }
  213. #endif
  214. }
  215. template <int K>
  216. RTC_FORCEINLINE void copy_VV(const vuint<K>* src, vuint<K>* tgt, const vbool<K>& mask)
  217. {
  218. #if (RTC_MAX_INSTANCE_LEVEL_COUNT == 1)
  219. vuint<K>::store(mask, tgt, src[0]);
  220. #else
  221. vbool<K> done = !mask;
  222. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  223. vuint<K>::store(mask, tgt + l, src[l]);
  224. if (RTC_MAX_INSTANCE_LEVEL_COUNT > 4) {
  225. done |= src[l] == RTC_INVALID_GEOMETRY_ID;
  226. if (all(done)) break;
  227. }
  228. }
  229. #endif
  230. }
  231. } // namespace instance_id_stack
  232. } // namespace embree