rtcore_ray.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "rtcore_common.h"
  5. RTC_NAMESPACE_BEGIN
  6. /* Ray structure for a single ray */
  7. struct RTC_ALIGN(16) RTCRay
  8. {
  9. float org_x; // x coordinate of ray origin
  10. float org_y; // y coordinate of ray origin
  11. float org_z; // z coordinate of ray origin
  12. float tnear; // start of ray segment
  13. float dir_x; // x coordinate of ray direction
  14. float dir_y; // y coordinate of ray direction
  15. float dir_z; // z coordinate of ray direction
  16. float time; // time of this ray for motion blur
  17. float tfar; // end of ray segment (set to hit distance)
  18. unsigned int mask; // ray mask
  19. unsigned int id; // ray ID
  20. unsigned int flags; // ray flags
  21. };
  22. /* Hit structure for a single ray */
  23. struct RTC_ALIGN(16) RTCHit
  24. {
  25. float Ng_x; // x coordinate of geometry normal
  26. float Ng_y; // y coordinate of geometry normal
  27. float Ng_z; // z coordinate of geometry normal
  28. float u; // barycentric u coordinate of hit
  29. float v; // barycentric v coordinate of hit
  30. unsigned int primID; // primitive ID
  31. unsigned int geomID; // geometry ID
  32. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID
  33. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  34. unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID
  35. #endif
  36. };
  37. /* Combined ray/hit structure for a single ray */
  38. struct RTCRayHit
  39. {
  40. struct RTCRay ray;
  41. struct RTCHit hit;
  42. };
  43. /* Ray structure for a packet of 4 rays */
  44. struct RTC_ALIGN(16) RTCRay4
  45. {
  46. float org_x[4];
  47. float org_y[4];
  48. float org_z[4];
  49. float tnear[4];
  50. float dir_x[4];
  51. float dir_y[4];
  52. float dir_z[4];
  53. float time[4];
  54. float tfar[4];
  55. unsigned int mask[4];
  56. unsigned int id[4];
  57. unsigned int flags[4];
  58. };
  59. /* Hit structure for a packet of 4 rays */
  60. struct RTC_ALIGN(16) RTCHit4
  61. {
  62. float Ng_x[4];
  63. float Ng_y[4];
  64. float Ng_z[4];
  65. float u[4];
  66. float v[4];
  67. unsigned int primID[4];
  68. unsigned int geomID[4];
  69. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][4];
  70. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  71. unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][4];
  72. #endif
  73. };
  74. /* Combined ray/hit structure for a packet of 4 rays */
  75. struct RTCRayHit4
  76. {
  77. struct RTCRay4 ray;
  78. struct RTCHit4 hit;
  79. };
  80. /* Ray structure for a packet of 8 rays */
  81. struct RTC_ALIGN(32) RTCRay8
  82. {
  83. float org_x[8];
  84. float org_y[8];
  85. float org_z[8];
  86. float tnear[8];
  87. float dir_x[8];
  88. float dir_y[8];
  89. float dir_z[8];
  90. float time[8];
  91. float tfar[8];
  92. unsigned int mask[8];
  93. unsigned int id[8];
  94. unsigned int flags[8];
  95. };
  96. /* Hit structure for a packet of 8 rays */
  97. struct RTC_ALIGN(32) RTCHit8
  98. {
  99. float Ng_x[8];
  100. float Ng_y[8];
  101. float Ng_z[8];
  102. float u[8];
  103. float v[8];
  104. unsigned int primID[8];
  105. unsigned int geomID[8];
  106. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][8];
  107. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  108. unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][8];
  109. #endif
  110. };
  111. /* Combined ray/hit structure for a packet of 8 rays */
  112. struct RTCRayHit8
  113. {
  114. struct RTCRay8 ray;
  115. struct RTCHit8 hit;
  116. };
  117. /* Ray structure for a packet of 16 rays */
  118. struct RTC_ALIGN(64) RTCRay16
  119. {
  120. float org_x[16];
  121. float org_y[16];
  122. float org_z[16];
  123. float tnear[16];
  124. float dir_x[16];
  125. float dir_y[16];
  126. float dir_z[16];
  127. float time[16];
  128. float tfar[16];
  129. unsigned int mask[16];
  130. unsigned int id[16];
  131. unsigned int flags[16];
  132. };
  133. /* Hit structure for a packet of 16 rays */
  134. struct RTC_ALIGN(64) RTCHit16
  135. {
  136. float Ng_x[16];
  137. float Ng_y[16];
  138. float Ng_z[16];
  139. float u[16];
  140. float v[16];
  141. unsigned int primID[16];
  142. unsigned int geomID[16];
  143. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][16];
  144. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  145. unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][16];
  146. #endif
  147. };
  148. /* Combined ray/hit structure for a packet of 16 rays */
  149. struct RTCRayHit16
  150. {
  151. struct RTCRay16 ray;
  152. struct RTCHit16 hit;
  153. };
  154. struct RTCRayN;
  155. struct RTCHitN;
  156. struct RTCRayHitN;
  157. #if defined(__cplusplus)
  158. /* Helper functions to access ray packets of runtime size N */
  159. RTC_FORCEINLINE float& RTCRayN_org_x(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[0*N+i]; }
  160. RTC_FORCEINLINE float& RTCRayN_org_y(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[1*N+i]; }
  161. RTC_FORCEINLINE float& RTCRayN_org_z(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[2*N+i]; }
  162. RTC_FORCEINLINE float& RTCRayN_tnear(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[3*N+i]; }
  163. RTC_FORCEINLINE float& RTCRayN_dir_x(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[4*N+i]; }
  164. RTC_FORCEINLINE float& RTCRayN_dir_y(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[5*N+i]; }
  165. RTC_FORCEINLINE float& RTCRayN_dir_z(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[6*N+i]; }
  166. RTC_FORCEINLINE float& RTCRayN_time (RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[7*N+i]; }
  167. RTC_FORCEINLINE float& RTCRayN_tfar (RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[8*N+i]; }
  168. RTC_FORCEINLINE unsigned int& RTCRayN_mask (RTCRayN* ray, unsigned int N, unsigned int i) { return ((unsigned*)ray)[9*N+i]; }
  169. RTC_FORCEINLINE unsigned int& RTCRayN_id (RTCRayN* ray, unsigned int N, unsigned int i) { return ((unsigned*)ray)[10*N+i]; }
  170. RTC_FORCEINLINE unsigned int& RTCRayN_flags(RTCRayN* ray, unsigned int N, unsigned int i) { return ((unsigned*)ray)[11*N+i]; }
  171. /* Helper functions to access hit packets of runtime size N */
  172. RTC_FORCEINLINE float& RTCHitN_Ng_x(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[0*N+i]; }
  173. RTC_FORCEINLINE float& RTCHitN_Ng_y(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[1*N+i]; }
  174. RTC_FORCEINLINE float& RTCHitN_Ng_z(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[2*N+i]; }
  175. RTC_FORCEINLINE float& RTCHitN_u(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[3*N+i]; }
  176. RTC_FORCEINLINE float& RTCHitN_v(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[4*N+i]; }
  177. RTC_FORCEINLINE unsigned int& RTCHitN_primID (RTCHitN* hit, unsigned int N, unsigned int i) { return ((unsigned*)hit)[5*N+i]; }
  178. RTC_FORCEINLINE unsigned int& RTCHitN_geomID (RTCHitN* hit, unsigned int N, unsigned int i) { return ((unsigned*)hit)[6*N+i]; }
  179. RTC_FORCEINLINE unsigned int& RTCHitN_instID (RTCHitN* hit, unsigned int N, unsigned int i, unsigned int l) { return ((unsigned*)hit)[7*N + N*l + i]; }
  180. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  181. RTC_FORCEINLINE unsigned int& RTCHitN_instPrimID(RTCHitN* hit, unsigned int N, unsigned int i, unsigned int l) { return ((unsigned*)hit)[7*N + N*RTC_MAX_INSTANCE_LEVEL_COUNT + N*l + i]; }
  182. #endif
  183. /* Helper functions to extract RTCRayN and RTCHitN from RTCRayHitN */
  184. RTC_FORCEINLINE RTCRayN* RTCRayHitN_RayN(RTCRayHitN* rayhit, unsigned int N) { return (RTCRayN*)&((float*)rayhit)[0*N]; }
  185. RTC_FORCEINLINE RTCHitN* RTCRayHitN_HitN(RTCRayHitN* rayhit, unsigned int N) { return (RTCHitN*)&((float*)rayhit)[12*N]; }
  186. /* Helper structure for a ray packet of compile-time size N */
  187. template<int N>
  188. struct RTCRayNt
  189. {
  190. float org_x[N];
  191. float org_y[N];
  192. float org_z[N];
  193. float tnear[N];
  194. float dir_x[N];
  195. float dir_y[N];
  196. float dir_z[N];
  197. float time[N];
  198. float tfar[N];
  199. unsigned int mask[N];
  200. unsigned int id[N];
  201. unsigned int flags[N];
  202. };
  203. /* Helper structure for a hit packet of compile-time size N */
  204. template<int N>
  205. struct RTCHitNt
  206. {
  207. float Ng_x[N];
  208. float Ng_y[N];
  209. float Ng_z[N];
  210. float u[N];
  211. float v[N];
  212. unsigned int primID[N];
  213. unsigned int geomID[N];
  214. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][N];
  215. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  216. unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][N];
  217. #endif
  218. };
  219. /* Helper structure for a combined ray/hit packet of compile-time size N */
  220. template<int N>
  221. struct RTCRayHitNt
  222. {
  223. RTCRayNt<N> ray;
  224. RTCHitNt<N> hit;
  225. };
  226. RTC_FORCEINLINE RTCRay rtcGetRayFromRayN(RTCRayN* rayN, unsigned int N, unsigned int i)
  227. {
  228. RTCRay ray;
  229. ray.org_x = RTCRayN_org_x(rayN,N,i);
  230. ray.org_y = RTCRayN_org_y(rayN,N,i);
  231. ray.org_z = RTCRayN_org_z(rayN,N,i);
  232. ray.tnear = RTCRayN_tnear(rayN,N,i);
  233. ray.dir_x = RTCRayN_dir_x(rayN,N,i);
  234. ray.dir_y = RTCRayN_dir_y(rayN,N,i);
  235. ray.dir_z = RTCRayN_dir_z(rayN,N,i);
  236. ray.time = RTCRayN_time(rayN,N,i);
  237. ray.tfar = RTCRayN_tfar(rayN,N,i);
  238. ray.mask = RTCRayN_mask(rayN,N,i);
  239. ray.id = RTCRayN_id(rayN,N,i);
  240. ray.flags = RTCRayN_flags(rayN,N,i);
  241. return ray;
  242. }
  243. RTC_FORCEINLINE RTCHit rtcGetHitFromHitN(RTCHitN* hitN, unsigned int N, unsigned int i)
  244. {
  245. RTCHit hit;
  246. hit.Ng_x = RTCHitN_Ng_x(hitN,N,i);
  247. hit.Ng_y = RTCHitN_Ng_y(hitN,N,i);
  248. hit.Ng_z = RTCHitN_Ng_z(hitN,N,i);
  249. hit.u = RTCHitN_u(hitN,N,i);
  250. hit.v = RTCHitN_v(hitN,N,i);
  251. hit.primID = RTCHitN_primID(hitN,N,i);
  252. hit.geomID = RTCHitN_geomID(hitN,N,i);
  253. for (unsigned int l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; l++) {
  254. hit.instID[l] = RTCHitN_instID(hitN,N,i,l);
  255. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  256. hit.instPrimID[l] = RTCHitN_instPrimID(hitN,N,i,l);
  257. #endif
  258. }
  259. return hit;
  260. }
  261. RTC_FORCEINLINE void rtcCopyHitToHitN(RTCHitN* hitN, const RTCHit* hit, unsigned int N, unsigned int i)
  262. {
  263. RTCHitN_Ng_x(hitN,N,i) = hit->Ng_x;
  264. RTCHitN_Ng_y(hitN,N,i) = hit->Ng_y;
  265. RTCHitN_Ng_z(hitN,N,i) = hit->Ng_z;
  266. RTCHitN_u(hitN,N,i) = hit->u;
  267. RTCHitN_v(hitN,N,i) = hit->v;
  268. RTCHitN_primID(hitN,N,i) = hit->primID;
  269. RTCHitN_geomID(hitN,N,i) = hit->geomID;
  270. for (unsigned int l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; l++) {
  271. RTCHitN_instID(hitN,N,i,l) = hit->instID[l];
  272. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  273. RTCHitN_instPrimID(hitN,N,i,l) = hit->instPrimID[l];
  274. #endif
  275. }
  276. }
  277. RTC_FORCEINLINE RTCRayHit rtcGetRayHitFromRayHitN(RTCRayHitN* rayhitN, unsigned int N, unsigned int i)
  278. {
  279. RTCRayHit rh;
  280. RTCRayN* ray = RTCRayHitN_RayN(rayhitN,N);
  281. rh.ray.org_x = RTCRayN_org_x(ray,N,i);
  282. rh.ray.org_y = RTCRayN_org_y(ray,N,i);
  283. rh.ray.org_z = RTCRayN_org_z(ray,N,i);
  284. rh.ray.tnear = RTCRayN_tnear(ray,N,i);
  285. rh.ray.dir_x = RTCRayN_dir_x(ray,N,i);
  286. rh.ray.dir_y = RTCRayN_dir_y(ray,N,i);
  287. rh.ray.dir_z = RTCRayN_dir_z(ray,N,i);
  288. rh.ray.time = RTCRayN_time(ray,N,i);
  289. rh.ray.tfar = RTCRayN_tfar(ray,N,i);
  290. rh.ray.mask = RTCRayN_mask(ray,N,i);
  291. rh.ray.id = RTCRayN_id(ray,N,i);
  292. rh.ray.flags = RTCRayN_flags(ray,N,i);
  293. RTCHitN* hit = RTCRayHitN_HitN(rayhitN,N);
  294. rh.hit.Ng_x = RTCHitN_Ng_x(hit,N,i);
  295. rh.hit.Ng_y = RTCHitN_Ng_y(hit,N,i);
  296. rh.hit.Ng_z = RTCHitN_Ng_z(hit,N,i);
  297. rh.hit.u = RTCHitN_u(hit,N,i);
  298. rh.hit.v = RTCHitN_v(hit,N,i);
  299. rh.hit.primID = RTCHitN_primID(hit,N,i);
  300. rh.hit.geomID = RTCHitN_geomID(hit,N,i);
  301. for (unsigned int l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; l++) {
  302. rh.hit.instID[l] = RTCHitN_instID(hit,N,i,l);
  303. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  304. rh.hit.instPrimID[l] = RTCHitN_instPrimID(hit,N,i,l);
  305. #endif
  306. }
  307. return rh;
  308. }
  309. #endif
  310. RTC_NAMESPACE_END