Skip to content

Commit

Permalink
[hdEmbree] ensure we respect PXR_WORK_THREAD_LIMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
pmolodo committed Jul 31, 2024
1 parent 6b04660 commit 75af09e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pxr/imaging/plugin/hdEmbree/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@
#include <embree3/rtcore_geometry.h>
#include <embree3/rtcore_scene.h>

#include <tbb/tbb_stddef.h>
#if TBB_INTERFACE_VERSION_MAJOR < 12
#include <tbb/task_scheduler_init.h>
#endif

#include <algorithm>
#include <chrono>
#include <limits>
#include <memory>
#include <stdint.h>
#include <thread>

Expand All @@ -51,6 +57,30 @@ constexpr float _minLuminanceCutoff = 1e-9f;

constexpr GfVec3f _invalidColor = GfVec3f(-std::numeric_limits<float>::infinity());

// -------------------------------------------------------------------------
// Old TBB workaround - can remove once OneTBB is mandatory
// -------------------------------------------------------------------------

#if TBB_INTERFACE_VERSION_MAJOR < 12
// Make the calling context respect PXR_WORK_THREAD_LIMIT, if run from a thread
// other than the main thread (ie, the renderThread)
class _ScopedThreadScheduler {
public:
_ScopedThreadScheduler() {
auto limit = WorkGetConcurrencyLimitEnvSetting();
if (limit != 0) {
_tbbTaskSchedInit =
std::make_unique<tbb::task_scheduler_init>(limit);
}
}

std::unique_ptr<tbb::task_scheduler_init> _tbbTaskSchedInit;
};
#else
class _ScopedThreadScheduler {
};
#endif

// -------------------------------------------------------------------------
// General Math Utilities
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -1074,6 +1104,7 @@ HdEmbreeRenderer::Render(HdRenderThread *renderThread)

// Render by scheduling square tiles of the sample buffer in a parallel
// for loop.
_ScopedThreadScheduler scheduler;
// Always pass the renderThread to _RenderTiles to allow the first frame
// to be interrupted.
WorkParallelForN(numTilesX*numTilesY,
Expand Down

0 comments on commit 75af09e

Please sign in to comment.