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