Skip to content

Commit 017dff6

Browse files
committed
Embree shape support fix performance issue
1 parent a176efa commit 017dff6

File tree

7 files changed

+26
-43
lines changed

7 files changed

+26
-43
lines changed

include/mitsuba/render/kdtree.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Temporary scratch space that is used to cache intersection information
3131
* (# of floats)
3232
*/
33-
#define MTS_KD_INTERSECTION_CACHE_SIZE 7
33+
#define MTS_KD_INTERSECTION_CACHE_SIZE 6
3434

3535
NAMESPACE_BEGIN(mitsuba)
3636

@@ -2427,7 +2427,7 @@ class MTS_EXPORT_RENDER ShapeKDTree : public TShapeKDTree<BoundingBox<Point<scal
24272427
else if (ShadowRay)
24282428
hit = shape->ray_test(ray, active);
24292429
else
2430-
std::tie(hit, t) = shape->ray_intersect(ray, cache + 3, active);
2430+
std::tie(hit, t) = shape->ray_intersect(ray, cache + 2, active);
24312431

24322432
if (!ShadowRay && any(hit)) {
24332433
Float shape_index_v = reinterpret_array<Float>(UInt(shape_index));
@@ -2441,16 +2441,13 @@ class MTS_EXPORT_RENDER ShapeKDTree : public TShapeKDTree<BoundingBox<Point<scal
24412441
masked(cache[1], hit) = prim_index_v;
24422442
}
24432443

2444-
// Indicates that all lanes have a valid cache
2445-
cache[2] = 1.f;
2446-
24472444
if (is_mesh) {
24482445
if constexpr (!is_array_v<Float>) {
2449-
cache[3] = u;
2450-
cache[4] = v;
2446+
cache[2] = u;
2447+
cache[3] = v;
24512448
} else {
2452-
masked(cache[3], hit) = u;
2453-
masked(cache[4], hit) = v;
2449+
masked(cache[2], hit) = u;
2450+
masked(cache[3], hit) = v;
24542451
}
24552452
}
24562453
}

include/mitsuba/render/shape.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ class MTS_EXPORT_RENDER Shape : public Object {
162162
* uninitialized.
163163
*
164164
* \param cache
165-
* Cached information about the previously computed intersection. The
166-
* first entry of the cache indicates which lanes in the entries are
167-
* valid. For invalid lanes, the information needs to be recomputed.
165+
* Cached information about the previously computed intersection.
168166
*/
169167
virtual void fill_surface_interaction(const Ray3f &ray, const Float *cache,
170168
SurfaceInteraction3f &si, Mask active = true) const;
@@ -348,7 +346,6 @@ NAMESPACE_END(mitsuba)
348346
ENOKI_CALL_SUPPORT_TEMPLATE_BEGIN(mitsuba::Shape)
349347
ENOKI_CALL_SUPPORT_METHOD(normal_derivative)
350348
ENOKI_CALL_SUPPORT_METHOD(fill_surface_interaction)
351-
ENOKI_CALL_SUPPORT_METHOD(is_mesh)
352349
ENOKI_CALL_SUPPORT_GETTER_TYPE(emitter, m_emitter, const typename Class::Emitter *)
353350
ENOKI_CALL_SUPPORT_GETTER_TYPE(sensor, m_sensor, const typename Class::Sensor *)
354351
ENOKI_CALL_SUPPORT_GETTER_TYPE(bsdf, m_bsdf, const typename Class::BSDF *)

src/librender/mesh.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,6 @@ MTS_VARIANT void Mesh<Float, Spectrum>::fill_surface_interaction(const Ray3f & /
265265
Mask active) const {
266266
MTS_MASK_ARGUMENT(active);
267267

268-
// Only fill surface interaction for lanes with valid cache
269-
Mask invalid_cache = neq(*cache++, 1.f);
270-
active &= !invalid_cache;
271-
272268
// Barycentric coordinates within triangle
273269
Float b1 = cache[0],
274270
b2 = cache[1];

src/librender/scene_embree.inl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Scene<Float, Spectrum>::ray_intersect_cpu(const Ray3f &ray, Mask active) const {
7575
si.prim_index = prim_index;
7676

7777
// Create the cache for the Mesh shape
78-
Float cache[3] = { (si.shape->is_mesh() ? 1.f : 0.f), rh.hit.u, rh.hit.v };
78+
Float cache[2] = { rh.hit.u, rh.hit.v };
7979

8080
// Ask shape to fill in the rest
8181
si.shape->fill_surface_interaction(ray, cache, si);
@@ -130,10 +130,7 @@ Scene<Float, Spectrum>::ray_intersect_cpu(const Ray3f &ray, Mask active) const {
130130
si.prim_index = prim_index;
131131

132132
// Create the cache for the Mesh shapes
133-
Float cache[3] = {
134-
select(si.shape->is_mesh(), Float(1.f), Float(0.f)),
135-
load<Float>(rh.hit.u), load<Float>(rh.hit.v)
136-
};
133+
Float cache[2] = { load<Float>(rh.hit.u), load<Float>(rh.hit.v) };
137134

138135
// Ask shape(s) to fill in the rest
139136
si.shape->fill_surface_interaction(ray, cache, si, active);

src/librender/shape.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ Shape<Float, Spectrum>::ray_intersect(const Ray3f &ray, Mask active) const {
276276

277277
SurfaceInteraction3f si = zero<SurfaceInteraction3f>();
278278
Float cache[MTS_KD_INTERSECTION_CACHE_SIZE];
279-
cache[0] = Float(1.f); // Indicates that all lanes have a valid cache
280-
281-
auto [success, t] = ray_intersect(ray, cache + 1, active);
279+
auto [success, t] = ray_intersect(ray, cache, active);
282280
active &= success;
283281
si.t = select(active, t, math::Infinity<Float>);
284282

src/shapes/disk.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,16 @@ class Disk final : public Shape<Float, Spectrum> {
182182
SurfaceInteraction3f &si_out, Mask active) const override {
183183
MTS_MASK_ARGUMENT(active);
184184

185-
// Load and/or recompute cache if necessary
186-
Mask invalid_cache = neq(*cache++, 1.f);
185+
#if !defined(MTS_ENABLE_EMBREE)
187186
Float local_x = cache[0];
188187
Float local_y = cache[1];
189-
if (any(invalid_cache)) {
190-
Ray3f ray_ = m_world_to_object.transform_affine(ray);
191-
Float t = -ray_.o.z() / ray_.d.z();
192-
Point3f local = ray_(t);
193-
masked(local_x, invalid_cache) = local.x();
194-
masked(local_y, invalid_cache) = local.y();
195-
}
188+
#else
189+
Ray3f ray_ = m_world_to_object.transform_affine(ray);
190+
Float t = -ray_.o.z() / ray_.d.z();
191+
Point3f local = ray_(t);
192+
Float local_x = local.x();
193+
Float local_y = local.y();
194+
#endif
196195

197196
SurfaceInteraction3f si(si_out);
198197

src/shapes/rectangle.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,16 @@ class Rectangle final : public Shape<Float, Spectrum> {
212212
SurfaceInteraction3f &si_out, Mask active) const override {
213213
MTS_MASK_ARGUMENT(active);
214214

215-
// Load and/or recompute cache if necessary
216-
Mask invalid_cache = neq(*cache++, 1.f);
215+
#if !defined(MTS_ENABLE_EMBREE)
217216
Float local_x = cache[0];
218217
Float local_y = cache[1];
219-
if (any(invalid_cache)) {
220-
Ray3f ray = m_world_to_object.transform_affine(ray_);
221-
Float t = -ray.o.z() * ray.d_rcp.z();
222-
Point3f local = ray(t);
223-
masked(local_x, invalid_cache) = local.x();
224-
masked(local_y, invalid_cache) = local.y();
225-
}
218+
#else
219+
Ray3f ray = m_world_to_object.transform_affine(ray_);
220+
Float t = -ray.o.z() * ray.d_rcp.z();
221+
Point3f local = ray(t);
222+
Float local_x = local.x();
223+
Float local_y = local.y();
224+
#endif
226225

227226
SurfaceInteraction3f si(si_out);
228227

0 commit comments

Comments
 (0)