Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/common/hit.h
9905 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#pragma once
5
6
#include "default.h"
7
#include "ray.h"
8
#include "instance_stack.h"
9
10
namespace embree
11
{
12
/* Hit structure for K hits */
13
template<int K>
14
struct HitK
15
{
16
/* Default construction does nothing */
17
__forceinline HitK() {}
18
19
/* Constructs a hit */
20
__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)
21
: Ng(Ng), u(u), v(v), primID(primID), geomID(geomID)
22
{
23
for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
24
instID[l] = RTC_INVALID_GEOMETRY_ID;
25
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
26
instPrimID[l] = RTC_INVALID_GEOMETRY_ID;
27
#endif
28
}
29
30
instance_id_stack::copy_UV<K>(context->instID, instID);
31
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
32
instance_id_stack::copy_UV<K>(context->instPrimID, instPrimID);
33
#endif
34
}
35
36
/* Constructs a hit */
37
__forceinline HitK(const RTCRayQueryContext* context, const vuint<K>& geomID, const vuint<K>& primID, const Vec2vf<K>& uv, const Vec3vf<K>& Ng)
38
: HitK(context,geomID,primID,uv.x,uv.y,Ng) {}
39
40
/* Returns the size of the hit */
41
static __forceinline size_t size() { return K; }
42
43
public:
44
Vec3vf<K> Ng; // geometry normal
45
vfloat<K> u; // barycentric u coordinate of hit
46
vfloat<K> v; // barycentric v coordinate of hit
47
vuint<K> primID; // primitive ID
48
vuint<K> geomID; // geometry ID
49
vuint<K> instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID
50
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
51
vuint<K> instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID
52
#endif
53
};
54
55
/* Specialization for a single hit */
56
template<>
57
struct __aligned(16) HitK<1>
58
{
59
/* Default construction does nothing */
60
__forceinline HitK() {}
61
62
/* Constructs a hit */
63
__forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, float u, float v, const Vec3fa& Ng)
64
: Ng(Ng.x,Ng.y,Ng.z), u(u), v(v), primID(primID), geomID(geomID)
65
{
66
instance_id_stack::copy_UU(context, context->instID, instID);
67
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
68
instance_id_stack::copy_UU(context, context->instPrimID, instPrimID);
69
#endif
70
}
71
72
/* Constructs a hit */
73
__forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, const Vec2f& uv, const Vec3fa& Ng)
74
: HitK<1>(context,geomID,primID,uv.x,uv.y,Ng) {}
75
76
/* Returns the size of the hit */
77
static __forceinline size_t size() { return 1; }
78
79
public:
80
Vec3<float> Ng; // geometry normal
81
float u; // barycentric u coordinate of hit
82
float v; // barycentric v coordinate of hit
83
unsigned int primID; // primitive ID
84
unsigned int geomID; // geometry ID
85
unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID
86
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
87
unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID
88
#endif
89
};
90
91
/* Shortcuts */
92
typedef HitK<1> Hit;
93
typedef HitK<4> Hit4;
94
typedef HitK<8> Hit8;
95
typedef HitK<16> Hit16;
96
typedef HitK<VSIZEX> Hitx;
97
98
/* Outputs hit to stream */
99
template<int K>
100
__forceinline embree_ostream operator<<(embree_ostream cout, const HitK<K>& ray)
101
{
102
cout << "{ " << embree_endl
103
<< " Ng = " << ray.Ng << embree_endl
104
<< " u = " << ray.u << embree_endl
105
<< " v = " << ray.v << embree_endl
106
<< " primID = " << ray.primID << embree_endl
107
<< " geomID = " << ray.geomID << embree_endl
108
<< " instID =";
109
for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)
110
{
111
cout << " " << ray.instID[l];
112
}
113
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
114
cout << " instPrimID =";
115
for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)
116
{
117
cout << " " << ray.instPrimID[l];
118
}
119
#endif
120
cout << embree_endl;
121
return cout << "}";
122
}
123
124
template<typename Hit>
125
__forceinline void copyHitToRay(RayHit& ray, const Hit& hit)
126
{
127
ray.Ng = hit.Ng;
128
ray.u = hit.u;
129
ray.v = hit.v;
130
ray.primID = hit.primID;
131
ray.geomID = hit.geomID;
132
instance_id_stack::copy_UU(hit.instID, ray.instID);
133
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
134
instance_id_stack::copy_UU(hit.instPrimID, ray.instPrimID);
135
#endif
136
}
137
138
template<int K>
139
__forceinline void copyHitToRay(const vbool<K>& mask, RayHitK<K>& ray, const HitK<K>& hit)
140
{
141
vfloat<K>::storeu(mask,&ray.Ng.x, hit.Ng.x);
142
vfloat<K>::storeu(mask,&ray.Ng.y, hit.Ng.y);
143
vfloat<K>::storeu(mask,&ray.Ng.z, hit.Ng.z);
144
vfloat<K>::storeu(mask,&ray.u, hit.u);
145
vfloat<K>::storeu(mask,&ray.v, hit.v);
146
vuint<K>::storeu(mask,&ray.primID, hit.primID);
147
vuint<K>::storeu(mask,&ray.geomID, hit.geomID);
148
instance_id_stack::copy_VV<K>(hit.instID, ray.instID, mask);
149
#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
150
instance_id_stack::copy_VV<K>(hit.instPrimID, ray.instPrimID, mask);
151
#endif
152
}
153
}
154
155