1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#include "acceln.h"
5#include "ray.h"
6#include "../../include/embree3/rtcore_ray.h"
7#include "../../common/algorithms/parallel_for.h"
8
9namespace embree
10{
11 AccelN::AccelN()
12 : Accel(AccelData::TY_ACCELN), accels() {}
13
14 AccelN::~AccelN()
15 {
16 for (size_t i=0; i<accels.size(); i++)
17 delete accels[i];
18 }
19
20 void AccelN::accels_add(Accel* accel)
21 {
22 assert(accel);
23 accels.push_back(x: accel);
24 }
25
26 void AccelN::accels_init()
27 {
28 for (size_t i=0; i<accels.size(); i++)
29 delete accels[i];
30
31 accels.clear();
32 }
33
34 bool AccelN::pointQuery (Accel::Intersectors* This_in, PointQuery* query, PointQueryContext* context)
35 {
36 bool changed = false;
37 AccelN* This = (AccelN*)This_in->ptr;
38 for (size_t i=0; i<This->accels.size(); i++)
39 if (!This->accels[i]->isEmpty())
40 changed |= This->accels[i]->intersectors.pointQuery(query,context);
41 return changed;
42 }
43
44 void AccelN::intersect (Accel::Intersectors* This_in, RTCRayHit& ray, IntersectContext* context)
45 {
46 AccelN* This = (AccelN*)This_in->ptr;
47 for (size_t i=0; i<This->accels.size(); i++)
48 if (!This->accels[i]->isEmpty())
49 This->accels[i]->intersectors.intersect(ray,context);
50 }
51
52 void AccelN::intersect4 (const void* valid, Accel::Intersectors* This_in, RTCRayHit4& ray, IntersectContext* context)
53 {
54 AccelN* This = (AccelN*)This_in->ptr;
55 for (size_t i=0; i<This->accels.size(); i++)
56 if (!This->accels[i]->isEmpty())
57 This->accels[i]->intersectors.intersect4(valid,ray,context);
58 }
59
60 void AccelN::intersect8 (const void* valid, Accel::Intersectors* This_in, RTCRayHit8& ray, IntersectContext* context)
61 {
62 AccelN* This = (AccelN*)This_in->ptr;
63 for (size_t i=0; i<This->accels.size(); i++)
64 if (!This->accels[i]->isEmpty())
65 This->accels[i]->intersectors.intersect8(valid,ray,context);
66 }
67
68 void AccelN::intersect16 (const void* valid, Accel::Intersectors* This_in, RTCRayHit16& ray, IntersectContext* context)
69 {
70 AccelN* This = (AccelN*)This_in->ptr;
71 for (size_t i=0; i<This->accels.size(); i++)
72 if (!This->accels[i]->isEmpty())
73 This->accels[i]->intersectors.intersect16(valid,ray,context);
74 }
75
76 void AccelN::intersectN (Accel::Intersectors* This_in, RTCRayHitN** ray, const size_t N, IntersectContext* context)
77 {
78 AccelN* This = (AccelN*)This_in->ptr;
79 for (size_t i=0; i<This->accels.size(); i++)
80 if (!This->accels[i]->isEmpty())
81 This->accels[i]->intersectors.intersectN(rayN: ray,N,context);
82 }
83
84 void AccelN::occluded (Accel::Intersectors* This_in, RTCRay& ray, IntersectContext* context)
85 {
86 AccelN* This = (AccelN*)This_in->ptr;
87 for (size_t i=0; i<This->accels.size(); i++) {
88 if (This->accels[i]->isEmpty()) continue;
89 This->accels[i]->intersectors.occluded(ray,context);
90 if (ray.tfar < 0.0f) break;
91 }
92 }
93
94 void AccelN::occluded4 (const void* valid, Accel::Intersectors* This_in, RTCRay4& ray, IntersectContext* context)
95 {
96 AccelN* This = (AccelN*)This_in->ptr;
97 for (size_t i=0; i<This->accels.size(); i++) {
98 if (This->accels[i]->isEmpty()) continue;
99 This->accels[i]->intersectors.occluded4(valid,ray,context);
100#if defined(__SSE2__)
101 vbool4 valid0 = asBool(a: ((vint4*)valid)[0]);
102 vbool4 hit0 = ((vfloat4*)ray.tfar)[0] >= vfloat4(zero);
103 if (unlikely(none(valid0 & hit0))) break;
104#endif
105 }
106 }
107
108 void AccelN::occluded8 (const void* valid, Accel::Intersectors* This_in, RTCRay8& ray, IntersectContext* context)
109 {
110 AccelN* This = (AccelN*)This_in->ptr;
111 for (size_t i=0; i<This->accels.size(); i++) {
112 if (This->accels[i]->isEmpty()) continue;
113 This->accels[i]->intersectors.occluded8(valid,ray,context);
114#if defined(__SSE2__) // FIXME: use higher ISA
115 vbool4 valid0 = asBool(a: ((vint4*)valid)[0]);
116 vbool4 hit0 = ((vfloat4*)ray.tfar)[0] >= vfloat4(zero);
117 vbool4 valid1 = asBool(a: ((vint4*)valid)[1]);
118 vbool4 hit1 = ((vfloat4*)ray.tfar)[1] >= vfloat4(zero);
119 if (unlikely((none((valid0 & hit0) | (valid1 & hit1))))) break;
120#endif
121 }
122 }
123
124 void AccelN::occluded16 (const void* valid, Accel::Intersectors* This_in, RTCRay16& ray, IntersectContext* context)
125 {
126 AccelN* This = (AccelN*)This_in->ptr;
127 for (size_t i=0; i<This->accels.size(); i++) {
128 if (This->accels[i]->isEmpty()) continue;
129 This->accels[i]->intersectors.occluded16(valid,ray,context);
130#if defined(__SSE2__) // FIXME: use higher ISA
131 vbool4 valid0 = asBool(a: ((vint4*)valid)[0]);
132 vbool4 hit0 = ((vfloat4*)ray.tfar)[0] >= vfloat4(zero);
133 vbool4 valid1 = asBool(a: ((vint4*)valid)[1]);
134 vbool4 hit1 = ((vfloat4*)ray.tfar)[1] >= vfloat4(zero);
135 vbool4 valid2 = asBool(a: ((vint4*)valid)[2]);
136 vbool4 hit2 = ((vfloat4*)ray.tfar)[2] >= vfloat4(zero);
137 vbool4 valid3 = asBool(a: ((vint4*)valid)[3]);
138 vbool4 hit3 = ((vfloat4*)ray.tfar)[3] >= vfloat4(zero);
139 if (unlikely((none((valid0 & hit0) | (valid1 & hit1) | (valid2 & hit2) | (valid3 & hit3))))) break;
140#endif
141 }
142 }
143
144 void AccelN::occludedN (Accel::Intersectors* This_in, RTCRayN** ray, const size_t N, IntersectContext* context)
145 {
146 AccelN* This = (AccelN*)This_in->ptr;
147 size_t M = N;
148 for (size_t i=0; i<This->accels.size(); i++)
149 if (!This->accels[i]->isEmpty())
150 This->accels[i]->intersectors.occludedN(rayN: ray,N: M,context);
151 }
152
153 void AccelN::accels_print(size_t ident)
154 {
155 for (size_t i=0; i<accels.size(); i++)
156 {
157 for (size_t j=0; j<ident; j++) std::cout << " ";
158 std::cout << "accels[" << i << "]" << std::endl;
159 accels[i]->intersectors.print(ident: ident+2);
160 }
161 }
162
163 void AccelN::accels_immutable()
164 {
165 for (size_t i=0; i<accels.size(); i++)
166 accels[i]->immutable();
167 }
168
169 void AccelN::accels_build ()
170 {
171 /* reduce memory consumption */
172 accels.shrink_to_fit();
173
174 /* build all acceleration structures in parallel */
175 parallel_for (N: accels.size(), func: [&] (size_t i) {
176 accels[i]->build();
177 });
178
179 /* create list of non-empty acceleration structures */
180 bool valid1 = true;
181 bool valid4 = true;
182 bool valid8 = true;
183 bool valid16 = true;
184 for (size_t i=0; i<accels.size(); i++) {
185 valid1 &= (bool) accels[i]->intersectors.intersector1;
186 valid4 &= (bool) accels[i]->intersectors.intersector4;
187 valid8 &= (bool) accels[i]->intersectors.intersector8;
188 valid16 &= (bool) accels[i]->intersectors.intersector16;
189 }
190
191 if (accels.size() == 1) {
192 type = accels[0]->type; // FIXME: should just assign entire Accel
193 bounds = accels[0]->bounds;
194 intersectors = accels[0]->intersectors;
195 }
196 else
197 {
198 type = AccelData::TY_ACCELN;
199 intersectors.ptr = this;
200 intersectors.intersector1 = Intersector1(&intersect,&occluded,&pointQuery,valid1 ? "AccelN::intersector1": nullptr);
201 intersectors.intersector4 = Intersector4(&intersect4,&occluded4,valid4 ? "AccelN::intersector4" : nullptr);
202 intersectors.intersector8 = Intersector8(&intersect8,&occluded8,valid8 ? "AccelN::intersector8" : nullptr);
203 intersectors.intersector16 = Intersector16(&intersect16,&occluded16,valid16 ? "AccelN::intersector16": nullptr);
204 intersectors.intersectorN = IntersectorN(&intersectN,&occludedN,"AccelN::intersectorN");
205
206 /*! calculate bounds */
207 bounds = empty;
208 for (size_t i=0; i<accels.size(); i++)
209 bounds.extend(other: accels[i]->bounds);
210 }
211 }
212
213 void AccelN::accels_select(bool filter)
214 {
215 for (size_t i=0; i<accels.size(); i++)
216 accels[i]->intersectors.select(filter);
217 }
218
219 void AccelN::accels_deleteGeometry(size_t geomID)
220 {
221 for (size_t i=0; i<accels.size(); i++)
222 accels[i]->deleteGeometry(geomID);
223 }
224
225 void AccelN::accels_clear()
226 {
227 for (size_t i=0; i<accels.size(); i++) {
228 accels[i]->clear();
229 }
230 }
231}
232
233

source code of qtquick3d/src/3rdparty/embree/kernels/common/acceln.cpp