rtcore_ray.h 12 KB

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