-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathembreeaccel.cpp
More file actions
278 lines (215 loc) · 9.61 KB
/
Copy pathembreeaccel.cpp
File metadata and controls
278 lines (215 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***************************************************************************
* Copyright 1998-2020 by authors (see AUTHORS.txt) *
* *
* This file is part of LuxCoreRender. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
#include <string>
#include <limits>
#if defined (_MSC_VER) || defined (__INTEL_COMPILER)
#include <intrin.h>
#endif
#include <boost/foreach.hpp>
#include "luxrays/core/context.h"
#include "luxrays/accelerators/embreeaccel.h"
#include "luxrays/utils/strutils.h"
namespace luxrays {
EmbreeAccel::EmbreeAccel(const Context *context) : ctx(context),
uniqueRTCSceneByMesh(MeshPtrCompare), uniqueGeomByMesh(MeshPtrCompare),
uniqueInstMatrixByMesh(MeshPtrCompare) {
#if defined (_MSC_VER) || defined (__INTEL_COMPILER)
// Embree recommends settings these flags to improve performance
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
#endif
embreeDevice = rtcNewDevice(NULL);
embreeScene = NULL;
}
EmbreeAccel::~EmbreeAccel() {
if (embreeScene) {
rtcReleaseScene(embreeScene);
// I have to free all Embree scenes used for instances
std::pair<const Mesh *, RTCScene> elem;
BOOST_FOREACH(elem, uniqueRTCSceneByMesh)
rtcReleaseScene(elem.second);
}
rtcReleaseDevice(embreeDevice);
}
void EmbreeAccel::ExportTriangleMesh(const RTCScene embreeScene, const Mesh *mesh) const {
const RTCGeometry geom = rtcNewGeometry(embreeDevice, RTC_GEOMETRY_TYPE_TRIANGLE);
// Share with Embree the mesh vertices
Point *meshVerts = mesh->GetVertices();
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, meshVerts,
0, sizeof(Point), mesh->GetTotalVertexCount());
// Share with Embree the mesh triangles
Triangle *meshTris = mesh->GetTriangles();
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, meshTris,
0, sizeof(Triangle), mesh->GetTotalTriangleCount());
rtcCommitGeometry(geom);
rtcAttachGeometry(embreeScene, geom);
rtcReleaseGeometry(geom);
}
void EmbreeAccel::ExportMotionTriangleMesh(const RTCScene embreeScene, const MotionTriangleMesh *mtm) const {
const MotionSystem &ms = mtm->GetMotionSystem();
// Check if I would need more than the max. number of steps (i.e. 129) supported by Embree
if (ms.times.size() > RTC_MAX_TIME_STEP_COUNT)
throw std::runtime_error("Embree accelerator supports up to " + ToString(RTC_MAX_TIME_STEP_COUNT) +
" motion blur steps, unable to use " + ToString(ms.times.size()));
const RTCGeometry geom = rtcNewGeometry(embreeDevice, RTC_GEOMETRY_TYPE_TRIANGLE);
rtcSetGeometryTimeStepCount(geom, ms.times.size());
for (u_int step = 0; step < ms.times.size(); ++step) {
// Copy the mesh start position vertices
Point *vertices = (Point *)rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, step, RTC_FORMAT_FLOAT3,
sizeof(Point), mtm->GetTotalVertexCount());
Transform local2World;
mtm->GetLocal2World(ms.times[step], local2World);
for (u_int i = 0; i < mtm->GetTotalVertexCount(); ++i)
vertices[i] = mtm->GetVertex(local2World, i);
}
// Share the mesh triangles
Triangle *meshTris = mtm->GetTriangles();
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, (RTCBuffer)meshTris,
0, sizeof(Triangle), mtm->GetTotalTriangleCount());
rtcCommitGeometry(geom);
rtcAttachGeometry(embreeScene, geom);
rtcReleaseGeometry(geom);
}
void EmbreeAccel::Init(const std::deque<const Mesh *> &meshes,
const u_longlong totalVertexCount,
const u_longlong totalTriangleCount) {
const double t0 = WallClockTime();
//--------------------------------------------------------------------------
// Extract the meshes min. and max. time. To normalize between 0.f and 1.f.
//--------------------------------------------------------------------------
minTime = std::numeric_limits<float>::max();
maxTime = std::numeric_limits<float>::min();
BOOST_FOREACH(const Mesh *mesh, meshes) {
const MotionTriangleMesh *mtm = dynamic_cast<const MotionTriangleMesh *>(mesh);
if (mtm) {
minTime = Min(minTime, mtm->GetMotionSystem().StartTime());
maxTime = Max(maxTime, mtm->GetMotionSystem().EndTime());
}
}
if ((minTime == std::numeric_limits<float>::max()) ||
(maxTime == std::numeric_limits<float>::min())) {
minTime = 0.f;
maxTime = 1.f;
timeScale = 1.f;
} else
timeScale = 1.f / (maxTime - minTime);
//--------------------------------------------------------------------------
// Convert the meshes to an Embree Scene
//--------------------------------------------------------------------------
embreeScene = rtcNewScene(embreeDevice);
rtcSetSceneBuildQuality(embreeScene, RTC_BUILD_QUALITY_HIGH);
rtcSetSceneFlags(embreeScene, RTC_SCENE_FLAG_DYNAMIC);
BOOST_FOREACH(const Mesh *mesh, meshes) {
switch (mesh->GetType()) {
case TYPE_TRIANGLE:
case TYPE_EXT_TRIANGLE:
ExportTriangleMesh(embreeScene, mesh);
break;
case TYPE_TRIANGLE_INSTANCE:
case TYPE_EXT_TRIANGLE_INSTANCE: {
const InstanceTriangleMesh *itm = dynamic_cast<const InstanceTriangleMesh *>(mesh);
// Check if a RTCScene has already been created
std::map<const Mesh *, RTCScene, bool (*)(const Mesh *, const Mesh *)>::iterator it =
uniqueRTCSceneByMesh.find(itm->GetTriangleMesh());
RTCScene instScene;
if (it == uniqueRTCSceneByMesh.end()) {
TriangleMesh *instancedMesh = itm->GetTriangleMesh();
// Create a new RTCScene
instScene = rtcNewScene(embreeDevice);
ExportTriangleMesh(instScene, instancedMesh);
rtcCommitScene(instScene);
uniqueRTCSceneByMesh[instancedMesh] = instScene;
} else
instScene = it->second;
RTCGeometry geom = rtcNewGeometry(embreeDevice, RTC_GEOMETRY_TYPE_INSTANCE);
rtcSetGeometryInstancedScene(geom, instScene);
rtcSetGeometryTransform(geom, 0, RTC_FORMAT_FLOAT3X4_ROW_MAJOR, &(itm->GetTransformation().m.m[0][0]));
rtcCommitGeometry(geom);
rtcAttachGeometry(embreeScene, geom);
rtcReleaseGeometry(geom);
// Save the instance ID
uniqueGeomByMesh[mesh] = geom;
// Save the matrix
uniqueInstMatrixByMesh[mesh] = itm->GetTransformation().m;
break;
}
case TYPE_TRIANGLE_MOTION:
case TYPE_EXT_TRIANGLE_MOTION: {
const MotionTriangleMesh *mtm = dynamic_cast<const MotionTriangleMesh *>(mesh);
ExportMotionTriangleMesh(embreeScene, mtm);
break;
}
default:
throw std::runtime_error("Unknown Mesh type in EmbreeAccel::Init(): " + ToString(mesh->GetType()));
}
}
rtcCommitScene(embreeScene);
LR_LOG(ctx, "EmbreeAccel build time: " << int((WallClockTime() - t0) * 1000) << "ms");
}
void EmbreeAccel::Update() {
// Update all Embree scenes used for instances
bool updated = false;
std::pair<const Mesh *, RTCGeometry> elem;
BOOST_FOREACH(elem, uniqueGeomByMesh) {
const InstanceTriangleMesh *itm = dynamic_cast<const InstanceTriangleMesh *>(elem.first);
// Check if the transformation has changed
if (uniqueInstMatrixByMesh[elem.first] != itm->GetTransformation().m) {
rtcSetGeometryTransform(elem.second, 0, RTC_FORMAT_FLOAT3X4_ROW_MAJOR, &(itm->GetTransformation().m.m[0][0]));
rtcCommitGeometry(elem.second);
updated = true;
}
}
if (updated)
rtcCommitScene(embreeScene);
}
bool EmbreeAccel::MeshPtrCompare(const Mesh *p0, const Mesh *p1) {
return p0 < p1;
}
bool EmbreeAccel::Intersect(const Ray *ray, RayHit *hit) const {
RTCIntersectContext context;
rtcInitIntersectContext(&context);
RTCRayHit embreeRayHit;
embreeRayHit.ray.org_x = ray->o.x;
embreeRayHit.ray.org_y = ray->o.y;
embreeRayHit.ray.org_z = ray->o.z;
embreeRayHit.ray.dir_x = ray->d.x;
embreeRayHit.ray.dir_y = ray->d.y;
embreeRayHit.ray.dir_z = ray->d.z;
embreeRayHit.ray.tnear = ray->mint;
embreeRayHit.ray.tfar = ray->maxt;
embreeRayHit.ray.mask = 0xFFFFFFFF;
embreeRayHit.ray.time = (ray->time - minTime) * timeScale;
embreeRayHit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
embreeRayHit.hit.primID = RTC_INVALID_GEOMETRY_ID;
embreeRayHit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
rtcIntersect1(embreeScene, &context, &embreeRayHit);
if ((embreeRayHit.hit.geomID != RTC_INVALID_GEOMETRY_ID) &&
// A safety check in case of not enough numerical precision. Embree
// can return some intersection out of [mint, maxt] range for
// some extremely large floating point number.
(embreeRayHit.ray.tfar >= ray->mint) && (embreeRayHit.ray.tfar <= ray->maxt)) {
hit->meshIndex = (embreeRayHit.hit.instID[0] == RTC_INVALID_GEOMETRY_ID) ? embreeRayHit.hit.geomID : embreeRayHit.hit.instID[0];
hit->triangleIndex = embreeRayHit.hit.primID;
hit->t = embreeRayHit.ray.tfar;
hit->b1 = embreeRayHit.hit.u;
hit->b2 = embreeRayHit.hit.v;
return true;
} else
return false;
}
}