Path: blob/master/thirdparty/embree/include/embree4/rtcore_ray.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "rtcore_common.h"67RTC_NAMESPACE_BEGIN89/* Ray structure for a single ray */10struct RTC_ALIGN(16) RTCRay11{12float org_x; // x coordinate of ray origin13float org_y; // y coordinate of ray origin14float org_z; // z coordinate of ray origin15float tnear; // start of ray segment1617float dir_x; // x coordinate of ray direction18float dir_y; // y coordinate of ray direction19float dir_z; // z coordinate of ray direction20float time; // time of this ray for motion blur2122float tfar; // end of ray segment (set to hit distance)23unsigned int mask; // ray mask24unsigned int id; // ray ID25unsigned int flags; // ray flags26};2728/* Hit structure for a single ray */29struct RTC_ALIGN(16) RTCHit30{31float Ng_x; // x coordinate of geometry normal32float Ng_y; // y coordinate of geometry normal33float Ng_z; // z coordinate of geometry normal3435float u; // barycentric u coordinate of hit36float v; // barycentric v coordinate of hit3738unsigned int primID; // primitive ID39unsigned int geomID; // geometry ID40unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID41#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)42unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID43#endif44};4546/* Combined ray/hit structure for a single ray */47struct RTCRayHit48{49struct RTCRay ray;50struct RTCHit hit;51};5253/* Ray structure for a packet of 4 rays */54struct RTC_ALIGN(16) RTCRay455{56float org_x[4];57float org_y[4];58float org_z[4];59float tnear[4];6061float dir_x[4];62float dir_y[4];63float dir_z[4];64float time[4];6566float tfar[4];67unsigned int mask[4];68unsigned int id[4];69unsigned int flags[4];70};7172/* Hit structure for a packet of 4 rays */73struct RTC_ALIGN(16) RTCHit474{75float Ng_x[4];76float Ng_y[4];77float Ng_z[4];7879float u[4];80float v[4];8182unsigned int primID[4];83unsigned int geomID[4];84unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][4];85#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)86unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][4];87#endif88};8990/* Combined ray/hit structure for a packet of 4 rays */91struct RTCRayHit492{93struct RTCRay4 ray;94struct RTCHit4 hit;95};9697/* Ray structure for a packet of 8 rays */98struct RTC_ALIGN(32) RTCRay899{100float org_x[8];101float org_y[8];102float org_z[8];103float tnear[8];104105float dir_x[8];106float dir_y[8];107float dir_z[8];108float time[8];109110float tfar[8];111unsigned int mask[8];112unsigned int id[8];113unsigned int flags[8];114};115116/* Hit structure for a packet of 8 rays */117struct RTC_ALIGN(32) RTCHit8118{119float Ng_x[8];120float Ng_y[8];121float Ng_z[8];122123float u[8];124float v[8];125126unsigned int primID[8];127unsigned int geomID[8];128unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][8];129#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)130unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][8];131#endif132};133134/* Combined ray/hit structure for a packet of 8 rays */135struct RTCRayHit8136{137struct RTCRay8 ray;138struct RTCHit8 hit;139};140141/* Ray structure for a packet of 16 rays */142struct RTC_ALIGN(64) RTCRay16143{144float org_x[16];145float org_y[16];146float org_z[16];147float tnear[16];148149float dir_x[16];150float dir_y[16];151float dir_z[16];152float time[16];153154float tfar[16];155unsigned int mask[16];156unsigned int id[16];157unsigned int flags[16];158};159160/* Hit structure for a packet of 16 rays */161struct RTC_ALIGN(64) RTCHit16162{163float Ng_x[16];164float Ng_y[16];165float Ng_z[16];166167float u[16];168float v[16];169170unsigned int primID[16];171unsigned int geomID[16];172unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][16];173#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)174unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][16];175#endif176};177178/* Combined ray/hit structure for a packet of 16 rays */179struct RTCRayHit16180{181struct RTCRay16 ray;182struct RTCHit16 hit;183};184185struct RTCRayN;186struct RTCHitN;187struct RTCRayHitN;188189#if defined(__cplusplus)190191/* Helper functions to access ray packets of runtime size N */192RTC_FORCEINLINE float& RTCRayN_org_x(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[0*N+i]; }193RTC_FORCEINLINE float& RTCRayN_org_y(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[1*N+i]; }194RTC_FORCEINLINE float& RTCRayN_org_z(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[2*N+i]; }195RTC_FORCEINLINE float& RTCRayN_tnear(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[3*N+i]; }196197RTC_FORCEINLINE float& RTCRayN_dir_x(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[4*N+i]; }198RTC_FORCEINLINE float& RTCRayN_dir_y(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[5*N+i]; }199RTC_FORCEINLINE float& RTCRayN_dir_z(RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[6*N+i]; }200RTC_FORCEINLINE float& RTCRayN_time (RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[7*N+i]; }201202RTC_FORCEINLINE float& RTCRayN_tfar (RTCRayN* ray, unsigned int N, unsigned int i) { return ((float*)ray)[8*N+i]; }203RTC_FORCEINLINE unsigned int& RTCRayN_mask (RTCRayN* ray, unsigned int N, unsigned int i) { return ((unsigned*)ray)[9*N+i]; }204RTC_FORCEINLINE unsigned int& RTCRayN_id (RTCRayN* ray, unsigned int N, unsigned int i) { return ((unsigned*)ray)[10*N+i]; }205RTC_FORCEINLINE unsigned int& RTCRayN_flags(RTCRayN* ray, unsigned int N, unsigned int i) { return ((unsigned*)ray)[11*N+i]; }206207/* Helper functions to access hit packets of runtime size N */208RTC_FORCEINLINE float& RTCHitN_Ng_x(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[0*N+i]; }209RTC_FORCEINLINE float& RTCHitN_Ng_y(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[1*N+i]; }210RTC_FORCEINLINE float& RTCHitN_Ng_z(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[2*N+i]; }211212RTC_FORCEINLINE float& RTCHitN_u(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[3*N+i]; }213RTC_FORCEINLINE float& RTCHitN_v(RTCHitN* hit, unsigned int N, unsigned int i) { return ((float*)hit)[4*N+i]; }214215RTC_FORCEINLINE unsigned int& RTCHitN_primID (RTCHitN* hit, unsigned int N, unsigned int i) { return ((unsigned*)hit)[5*N+i]; }216RTC_FORCEINLINE unsigned int& RTCHitN_geomID (RTCHitN* hit, unsigned int N, unsigned int i) { return ((unsigned*)hit)[6*N+i]; }217RTC_FORCEINLINE unsigned int& RTCHitN_instID (RTCHitN* hit, unsigned int N, unsigned int i, unsigned int l) { return ((unsigned*)hit)[7*N + N*l + i]; }218#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)219RTC_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]; }220#endif221222/* Helper functions to extract RTCRayN and RTCHitN from RTCRayHitN */223RTC_FORCEINLINE RTCRayN* RTCRayHitN_RayN(RTCRayHitN* rayhit, unsigned int N) { return (RTCRayN*)&((float*)rayhit)[0*N]; }224RTC_FORCEINLINE RTCHitN* RTCRayHitN_HitN(RTCRayHitN* rayhit, unsigned int N) { return (RTCHitN*)&((float*)rayhit)[12*N]; }225226/* Helper structure for a ray packet of compile-time size N */227template<unsigned int N>228struct RTC_ALIGN((N && !(N & (N - 1)) ? (N * 4 > 16 ? N * 4 : 16) : 16)) RTCRayNt229{230float org_x[N];231float org_y[N];232float org_z[N];233float tnear[N];234235float dir_x[N];236float dir_y[N];237float dir_z[N];238float time[N];239240float tfar[N];241unsigned int mask[N];242unsigned int id[N];243unsigned int flags[N];244};245246/* Helper structure for a hit packet of compile-time size N */247template<unsigned int N>248struct RTC_ALIGN((N && !(N & (N - 1)) ? (N * 4 > 16 ? N * 4 : 16) : 16)) RTCHitNt249{250float Ng_x[N];251float Ng_y[N];252float Ng_z[N];253254float u[N];255float v[N];256257unsigned int primID[N];258unsigned int geomID[N];259unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT][N];260#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)261unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT][N];262#endif263};264265/* Helper structure for a combined ray/hit packet of compile-time size N */266template<int N>267struct RTCRayHitNt268{269RTCRayNt<N> ray;270RTCHitNt<N> hit;271};272273RTC_FORCEINLINE RTCRay rtcGetRayFromRayN(RTCRayN* rayN, unsigned int N, unsigned int i)274{275RTCRay ray;276ray.org_x = RTCRayN_org_x(rayN,N,i);277ray.org_y = RTCRayN_org_y(rayN,N,i);278ray.org_z = RTCRayN_org_z(rayN,N,i);279ray.tnear = RTCRayN_tnear(rayN,N,i);280ray.dir_x = RTCRayN_dir_x(rayN,N,i);281ray.dir_y = RTCRayN_dir_y(rayN,N,i);282ray.dir_z = RTCRayN_dir_z(rayN,N,i);283ray.time = RTCRayN_time(rayN,N,i);284ray.tfar = RTCRayN_tfar(rayN,N,i);285ray.mask = RTCRayN_mask(rayN,N,i);286ray.id = RTCRayN_id(rayN,N,i);287ray.flags = RTCRayN_flags(rayN,N,i);288return ray;289}290291RTC_FORCEINLINE RTCHit rtcGetHitFromHitN(RTCHitN* hitN, unsigned int N, unsigned int i)292{293RTCHit hit;294hit.Ng_x = RTCHitN_Ng_x(hitN,N,i);295hit.Ng_y = RTCHitN_Ng_y(hitN,N,i);296hit.Ng_z = RTCHitN_Ng_z(hitN,N,i);297hit.u = RTCHitN_u(hitN,N,i);298hit.v = RTCHitN_v(hitN,N,i);299hit.primID = RTCHitN_primID(hitN,N,i);300hit.geomID = RTCHitN_geomID(hitN,N,i);301for (unsigned int l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; l++) {302hit.instID[l] = RTCHitN_instID(hitN,N,i,l);303#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)304hit.instPrimID[l] = RTCHitN_instPrimID(hitN,N,i,l);305#endif306}307return hit;308}309310RTC_FORCEINLINE void rtcCopyHitToHitN(RTCHitN* hitN, const RTCHit* hit, unsigned int N, unsigned int i)311{312RTCHitN_Ng_x(hitN,N,i) = hit->Ng_x;313RTCHitN_Ng_y(hitN,N,i) = hit->Ng_y;314RTCHitN_Ng_z(hitN,N,i) = hit->Ng_z;315RTCHitN_u(hitN,N,i) = hit->u;316RTCHitN_v(hitN,N,i) = hit->v;317RTCHitN_primID(hitN,N,i) = hit->primID;318RTCHitN_geomID(hitN,N,i) = hit->geomID;319for (unsigned int l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; l++) {320RTCHitN_instID(hitN,N,i,l) = hit->instID[l];321#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)322RTCHitN_instPrimID(hitN,N,i,l) = hit->instPrimID[l];323#endif324}325}326327RTC_FORCEINLINE RTCRayHit rtcGetRayHitFromRayHitN(RTCRayHitN* rayhitN, unsigned int N, unsigned int i)328{329RTCRayHit rh;330331RTCRayN* ray = RTCRayHitN_RayN(rayhitN,N);332rh.ray.org_x = RTCRayN_org_x(ray,N,i);333rh.ray.org_y = RTCRayN_org_y(ray,N,i);334rh.ray.org_z = RTCRayN_org_z(ray,N,i);335rh.ray.tnear = RTCRayN_tnear(ray,N,i);336rh.ray.dir_x = RTCRayN_dir_x(ray,N,i);337rh.ray.dir_y = RTCRayN_dir_y(ray,N,i);338rh.ray.dir_z = RTCRayN_dir_z(ray,N,i);339rh.ray.time = RTCRayN_time(ray,N,i);340rh.ray.tfar = RTCRayN_tfar(ray,N,i);341rh.ray.mask = RTCRayN_mask(ray,N,i);342rh.ray.id = RTCRayN_id(ray,N,i);343rh.ray.flags = RTCRayN_flags(ray,N,i);344345RTCHitN* hit = RTCRayHitN_HitN(rayhitN,N);346rh.hit.Ng_x = RTCHitN_Ng_x(hit,N,i);347rh.hit.Ng_y = RTCHitN_Ng_y(hit,N,i);348rh.hit.Ng_z = RTCHitN_Ng_z(hit,N,i);349rh.hit.u = RTCHitN_u(hit,N,i);350rh.hit.v = RTCHitN_v(hit,N,i);351rh.hit.primID = RTCHitN_primID(hit,N,i);352rh.hit.geomID = RTCHitN_geomID(hit,N,i);353for (unsigned int l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; l++) {354rh.hit.instID[l] = RTCHitN_instID(hit,N,i,l);355#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)356rh.hit.instPrimID[l] = RTCHitN_instPrimID(hit,N,i,l);357#endif358}359360return rh;361}362363#endif364365RTC_NAMESPACE_END366367368369