| 
									
										
										
										
											2021-05-20 12:49:33 +02:00
										 |  |  | // Copyright 2009-2021 Intel Corporation
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  | // SPDX-License-Identifier: Apache-2.0
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "default.h"
 | 
					
						
							|  |  |  | #include "ray.h"
 | 
					
						
							|  |  |  | #include "instance_stack.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace embree | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* Hit structure for K hits */ | 
					
						
							|  |  |  |   template<int K> | 
					
						
							|  |  |  |     struct HitK | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Default construction does nothing */ | 
					
						
							|  |  |  |     __forceinline HitK() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Constructs a hit */ | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     __forceinline HitK(const RTCRayQueryContext* context, const vuint<K>& geomID, const vuint<K>& primID, const vfloat<K>& u, const vfloat<K>& v, const Vec3vf<K>& Ng) | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |       : Ng(Ng), u(u), v(v), primID(primID), geomID(geomID)  | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |       for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) { | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |         instID[l] = RTC_INVALID_GEOMETRY_ID; | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |         instPrimID[l] = RTC_INVALID_GEOMETRY_ID; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |        | 
					
						
							| 
									
										
										
										
											2021-05-20 12:49:33 +02:00
										 |  |  |       instance_id_stack::copy_UV<K>(context->instID, instID); | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |       instance_id_stack::copy_UV<K>(context->instPrimID, instPrimID); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     /* Constructs a hit */ | 
					
						
							|  |  |  |     __forceinline HitK(const RTCRayQueryContext* context, const vuint<K>& geomID, const vuint<K>& primID, const Vec2vf<K>& uv, const Vec3vf<K>& Ng) | 
					
						
							|  |  |  |       : HitK(context,geomID,primID,uv.x,uv.y,Ng) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |     /* Returns the size of the hit */ | 
					
						
							|  |  |  |     static __forceinline size_t size() { return K; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     Vec3vf<K> Ng;  // geometry normal
 | 
					
						
							|  |  |  |     vfloat<K> u;         // barycentric u coordinate of hit
 | 
					
						
							|  |  |  |     vfloat<K> v;         // barycentric v coordinate of hit
 | 
					
						
							|  |  |  |     vuint<K> primID;      // primitive ID
 | 
					
						
							|  |  |  |     vuint<K> geomID;      // geometry ID
 | 
					
						
							|  |  |  |     vuint<K> instID[RTC_MAX_INSTANCE_LEVEL_COUNT];      // instance ID
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |     vuint<K> instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT];      // instance primitive ID
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Specialization for a single hit */ | 
					
						
							|  |  |  |   template<> | 
					
						
							|  |  |  |     struct __aligned(16) HitK<1> | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      /* Default construction does nothing */ | 
					
						
							|  |  |  |     __forceinline HitK() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Constructs a hit */ | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     __forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, float u, float v, const Vec3fa& Ng) | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |       : Ng(Ng.x,Ng.y,Ng.z), u(u), v(v), primID(primID), geomID(geomID) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |       instance_id_stack::copy_UU(context, context->instID, instID); | 
					
						
							|  |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |       instance_id_stack::copy_UU(context, context->instPrimID, instPrimID); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     /* Constructs a hit */ | 
					
						
							|  |  |  |     __forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, const Vec2f& uv, const Vec3fa& Ng) | 
					
						
							|  |  |  |       : HitK<1>(context,geomID,primID,uv.x,uv.y,Ng) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |     /* Returns the size of the hit */ | 
					
						
							|  |  |  |     static __forceinline size_t size() { return 1; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     Vec3<float> Ng;  // geometry normal
 | 
					
						
							|  |  |  |     float u;         // barycentric u coordinate of hit
 | 
					
						
							|  |  |  |     float v;         // barycentric v coordinate of hit
 | 
					
						
							|  |  |  |     unsigned int primID;      // primitive ID
 | 
					
						
							|  |  |  |     unsigned int geomID;      // geometry ID
 | 
					
						
							|  |  |  |     unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT];      // instance ID
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |     unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT];      // instance primitive ID
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Shortcuts */ | 
					
						
							|  |  |  |   typedef HitK<1>  Hit; | 
					
						
							|  |  |  |   typedef HitK<4>  Hit4; | 
					
						
							|  |  |  |   typedef HitK<8>  Hit8; | 
					
						
							|  |  |  |   typedef HitK<16> Hit16; | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |   typedef HitK<VSIZEX> Hitx; | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Outputs hit to stream */ | 
					
						
							|  |  |  |   template<int K> | 
					
						
							|  |  |  |   __forceinline embree_ostream operator<<(embree_ostream cout, const HitK<K>& ray) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     cout << "{ " << embree_endl | 
					
						
							|  |  |  |          << "  Ng = " << ray.Ng <<  embree_endl | 
					
						
							|  |  |  |          << "  u = " << ray.u <<  embree_endl | 
					
						
							|  |  |  |          << "  v = " << ray.v << embree_endl | 
					
						
							|  |  |  |          << "  primID = " << ray.primID <<  embree_endl | 
					
						
							|  |  |  |          << "  geomID = " << ray.geomID << embree_endl | 
					
						
							|  |  |  |          << "  instID ="; | 
					
						
							|  |  |  |     for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       cout << " " << ray.instID[l]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |     cout << "  instPrimID ="; | 
					
						
							|  |  |  |     for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       cout << " " << ray.instPrimID[l]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |     cout << embree_endl; | 
					
						
							|  |  |  |     return cout << "}"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template<typename Hit> | 
					
						
							|  |  |  |     __forceinline void copyHitToRay(RayHit& ray, const Hit& hit) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     ray.Ng   = hit.Ng; | 
					
						
							|  |  |  |     ray.u    = hit.u; | 
					
						
							|  |  |  |     ray.v    = hit.v; | 
					
						
							|  |  |  |     ray.primID = hit.primID; | 
					
						
							|  |  |  |     ray.geomID = hit.geomID; | 
					
						
							| 
									
										
										
										
											2021-05-20 12:49:33 +02:00
										 |  |  |     instance_id_stack::copy_UU(hit.instID, ray.instID); | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |     instance_id_stack::copy_UU(hit.instPrimID, ray.instPrimID); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template<int K> | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     __forceinline void copyHitToRay(const vbool<K>& mask, RayHitK<K>& ray, const HitK<K>& hit) | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     vfloat<K>::storeu(mask,&ray.Ng.x, hit.Ng.x); | 
					
						
							|  |  |  |     vfloat<K>::storeu(mask,&ray.Ng.y, hit.Ng.y); | 
					
						
							|  |  |  |     vfloat<K>::storeu(mask,&ray.Ng.z, hit.Ng.z); | 
					
						
							|  |  |  |     vfloat<K>::storeu(mask,&ray.u, hit.u); | 
					
						
							|  |  |  |     vfloat<K>::storeu(mask,&ray.v, hit.v); | 
					
						
							|  |  |  |     vuint<K>::storeu(mask,&ray.primID, hit.primID); | 
					
						
							|  |  |  |     vuint<K>::storeu(mask,&ray.geomID, hit.geomID); | 
					
						
							| 
									
										
										
										
											2021-05-20 12:49:33 +02:00
										 |  |  |     instance_id_stack::copy_VV<K>(hit.instID, ray.instID, mask); | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
 | 
					
						
							|  |  |  |     instance_id_stack::copy_VV<K>(hit.instPrimID, ray.instPrimID, mask); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:38:09 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } |