Skip to content

Commit 4654a56

Browse files
Fix parallel implementation
1 parent 97ea033 commit 4654a56

File tree

2 files changed

+11
-91
lines changed

2 files changed

+11
-91
lines changed

AOEmbree.cpp

Lines changed: 10 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -99,79 +99,6 @@ RTCScene initializeScene(RTCDevice device)
9999
return scene;
100100
}
101101

102-
bool castRay(RTCScene scene,
103-
float ox, float oy, float oz,
104-
float dx, float dy, float dz, float maxDist)
105-
{
106-
/*
107-
* The intersect context can be used to set intersection
108-
* filters or flags, and it also contains the instance ID stack
109-
* used in multi-level instancing.
110-
*/
111-
struct RTCIntersectContext context;
112-
rtcInitIntersectContext(&context);
113-
114-
/*
115-
* The ray hit structure holds both the ray and the hit.
116-
* The user must initialize it properly -- see API documentation
117-
* for rtcIntersect1() for details.
118-
*/
119-
// struct RTCRayHit rayhit;
120-
// rayhit.ray.org_x = ox;
121-
// rayhit.ray.org_y = oy;
122-
// rayhit.ray.org_z = oz;
123-
// rayhit.ray.dir_x = dx;
124-
// rayhit.ray.dir_y = dy;
125-
// rayhit.ray.dir_z = dz;
126-
// rayhit.ray.tnear = 0;
127-
// rayhit.ray.tfar = maxDist;
128-
// rayhit.ray.mask = 0;
129-
// rayhit.ray.flags = 0;
130-
// rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
131-
// rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
132-
133-
134-
struct RTCRay rayhit;
135-
rayhit.org_x = ox;
136-
rayhit.org_y = oy;
137-
rayhit.org_z = oz;
138-
rayhit.dir_x = dx;
139-
rayhit.dir_y = dy;
140-
rayhit.dir_z = dz;
141-
rayhit.tnear = 0.01f;
142-
rayhit.tfar = maxDist;
143-
rayhit.mask = 0;
144-
rayhit.flags = 0;
145-
146-
/*
147-
* There are multiple variants of rtcIntersect. This one
148-
* intersects a single ray with the scene.
149-
*/
150-
rtcOccluded1(scene, &context, &rayhit);
151-
152-
// printf("%f, %f, %f: ", ox, oy, oz);
153-
// if (rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID)
154-
if (rayhit.tfar < 0.0f)
155-
{
156-
/* Note how geomID and primID identify the geometry we just hit.
157-
* We could use them here to interpolate geometry information,
158-
* compute shading, etc.
159-
* Since there is only a single triangle in this scene, we will
160-
* get geomID=0 / primID=0 for all hits.
161-
* There is also instID, used for instancing. See
162-
* the instancing tutorials for more information */
163-
// printf("Found intersection on geometry %d, primitive %d at tfar=%f\n",
164-
// rayhit.hit.geomID,
165-
// rayhit.hit.primID,
166-
// rayhit.ray.tfar);
167-
return true;
168-
}
169-
return false;
170-
// else
171-
// printf("Did not find any intersection.\n");
172-
}
173-
174-
175102
void rotate_vector_by_quaternion(const vec3& v, const quat& q, vec3& vprime)
176103
{
177104
// Extract the vector part of the quaternion
@@ -199,7 +126,6 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
199126

200127
RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
201128

202-
203129
float* vertices = (float*) rtcSetNewGeometryBuffer(geom,
204130
RTC_BUFFER_TYPE_VERTEX,
205131
0,
@@ -254,14 +180,12 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
254180
struct RTCIntersectContext context;
255181
rtcInitIntersectContext(&context);
256182

257-
// struct RTCRay rayhit;
258-
RTCRay *rays = new RTCRay[rayDir.size()];
183+
tbb::parallel_for( tbb::blocked_range<int>(0, vcount),
184+
[&](tbb::blocked_range<int> r)
185+
{
186+
RTCRay *rays = new RTCRay[rayDir.size()];
259187

260-
// tbb::parallel_for( tbb::blocked_range<int>(0, vcount),
261-
// [&](tbb::blocked_range<int> r)
262-
// {
263-
// for (int i = r.begin(); i < r.end(); ++i)
264-
for (int i = 0; i < vcount; ++i)
188+
for (int i = r.begin(); i < r.end(); ++i)
265189
{
266190
vec3 oriVec(0, 1, 0);
267191

@@ -286,15 +210,10 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
286210
rays[s].mask = 0;
287211
rays[s].flags = 0;
288212

289-
// bool inter = castRay(scene, vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2],
290-
// rotatedDir.x, rotatedDir.y, rotatedDir.z, maxDist);
291-
292-
// if (inter) {
293-
// totalAO += 1.0f;
294213
}
295214

296215
rtcOccluded1M(scene, &context, &rays[0], rayDir.size(), sizeof(RTCRay));
297-
// rtcOccluded1(scene, &context, &rayhit);
216+
298217
float totalAO = 0.0f;
299218

300219
for (int s = 0; s < rayDir.size(); s++) {
@@ -305,7 +224,7 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
305224

306225
result[i] = 1.0f - (totalAO / samplesAO);
307226
}
308-
// });
227+
});
309228

310229
/* Though not strictly necessary in this example, you should
311230
* always make sure to release resources allocated through Embree. */
@@ -314,15 +233,14 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
314233

315234
auto timerstop = high_resolution_clock::now();
316235
auto duration = duration_cast<milliseconds>(timerstop - timerstart).count();
317-
// fprintf(stderr, "%.6f ms\n", duration;
318236
std::cerr << duration << " ms" << std::endl;
319237
}
320238

321239

322240
int main(int argc, char **argv) {
323241

324242
int samplesAO = 128;
325-
float maxDist = 100.0f;
243+
float maxDist = 20.0f;
326244

327245
tinyobj::attrib_t attrib;
328246
std::vector<tinyobj::shape_t> shapes;
@@ -355,7 +273,6 @@ int main(int argc, char **argv) {
355273
for (size_t v = 0; v < fv; v++) {
356274
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
357275
ids.push_back(idx.vertex_index);
358-
//std::cout << idx.vertex_index << std::endl;
359276
}
360277
index_offset += fv;
361278
}
@@ -370,6 +287,8 @@ int main(int argc, char **argv) {
370287
for (int i = 0; i < verts.size() / 3; i++) {
371288
printf("v %.6f %.6f %.6f %.3f %.3f %.3f\n",
372289
verts[i * 3], verts[i * 3 + 1], verts[i * 3 + 2], result[i], result[i], result[i]);
290+
printf("vn %.6f %.6f %.6f\n", norms[i * 3], norms[i * 3 + 1], norms[i * 3 + 2]);
291+
373292
}
374293

375294
for (int i = 0; i < ids.size() / 3; i++) {

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ INCLUDE_DIRECTORIES(${EMBREE_INCLUDE_DIRS})
1717
add_executable(AOEmbree AOEmbree.cpp)
1818

1919
set(CMAKE_CXX_FLAGS_RELEASE "-Ofast")
20+
set(CMAKE_C_FLAGS_RELEASE "-Ofast")
2021

2122
target_compile_definitions(AOEmbree PUBLIC NOMINMAX)
2223
TARGET_LINK_LIBRARIES(AOEmbree ${EMBREE_LIBRARY} ${TBB_IMPORTED_TARGETS})

0 commit comments

Comments
 (0)