rtcore_ray.h 10 KB

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