Skip to content

Commit 6104bed

Browse files
committed
RT CPU: flush scene objects pending deletion when rendering is paused
1 parent 0e7c86b commit 6104bed

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

include/slg/engines/rtpathcpu/rtpathcpu.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class RTPathCPUSampler;
5858
class RTPathCPURenderEngine : public PathCPURenderEngine {
5959
public:
6060
RTPathCPURenderEngine(RenderConfigRef cfg);
61-
~RTPathCPURenderEngine();
61+
virtual ~RTPathCPURenderEngine() = default;
6262

6363
virtual RenderEngineType GetType() const { return GetObjectType(); }
6464
virtual std::string GetTag() const { return GetObjectTag(); }
@@ -80,15 +80,11 @@ class RTPathCPURenderEngine : public PathCPURenderEngine {
8080
friend class RTPathCPURenderThread;
8181
friend class RTPathCPUSampler;
8282

83-
struct completion_t {
84-
void operator()() noexcept { }
85-
};
86-
8783
protected:
8884
static luxrays::PropertiesUPtr GetDefaultProps();
8985

9086
virtual bool IsRTMode() const { return true; }
91-
87+
9288
CPURenderThreadUPtr NewRenderThread(const u_int index,
9389
luxrays::IntersectionDevice *device) {
9490
return std::make_unique<RTPathCPURenderThread>(this, index, device);
@@ -113,10 +109,12 @@ class RTPathCPURenderEngine : public PathCPURenderEngine {
113109

114110
std::mutex firstFrameMutex;
115111
std::condition_variable firstFrameCondition;
116-
u_int firstFrameThreadDoneCount;
112+
std::atomic<u_int> firstFrameThreadDoneCount;
117113
bool firstFrameDone;
118114

119-
std::barrier<completion_t> *threadsSyncBarrier;
115+
using CompletionFunction = std::function<void()>;
116+
std::unique_ptr<std::barrier<CompletionFunction>> threadsSyncBarrier; // General sync
117+
std::unique_ptr<std::barrier<CompletionFunction>> threadsPauseBarrier; // Dedicated to pause event
120118
std::atomic<bool> threadsPauseMode;
121119
};
122120

include/slg/scene/scene.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class Scene {
262262
void SetEnableParsePrint(bool status) { enableParsePrint = status; }
263263

264264
void moveToTrash(luxrays::NamedObjectUPtr&&);
265-
void emptyTrash();
265+
void EmptyTrash();
266266

267267
// Serialization
268268
static SceneUPtr LoadSerialized(const std::string &fileName);

src/slg/engines/rtpathcpu/rtpathcpu.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "slg/slg.h"
2020
#include "slg/samplers/rtpathcpusampler.h"
2121
#include <memory>
22+
#include <barrier>
23+
#include <csignal>
2224
#include "slg/engines/rtpathcpu/rtpathcpu.h"
2325

2426
using namespace std;
@@ -29,15 +31,26 @@ using namespace slg;
2931
// RTPathCPURenderEngine
3032
//------------------------------------------------------------------------------
3133

34+
3235
RTPathCPURenderEngine::RTPathCPURenderEngine(RenderConfigRef rcfg) :
3336
PathCPURenderEngine(rcfg) {
34-
threadsSyncBarrier = new std::barrier(renderThreads.size() + 1, completion_t());
35-
}
3637

37-
RTPathCPURenderEngine::~RTPathCPURenderEngine() {
38-
delete threadsSyncBarrier;
38+
auto nullHandler = [&]() noexcept {
39+
};
40+
41+
auto onPause = [&]() noexcept {
42+
renderConfig.GetScene().EmptyTrash();
43+
};
44+
45+
threadsSyncBarrier = std::make_unique<std::barrier<CompletionFunction>>(
46+
renderThreads.size() + 1, nullHandler
47+
);
48+
threadsPauseBarrier = std::make_unique<std::barrier<CompletionFunction>>(
49+
renderThreads.size() + 1, onPause
50+
);
3951
}
4052

53+
4154
void RTPathCPURenderEngine::StartLockLess() {
4255
auto& cfg = renderConfig.GetConfig();
4356
zoomFactor = (u_int)Max(1, cfg.Get(GetDefaultProps()->Get("rtpathcpu.zoomphase.size")).Get<int>());
@@ -70,7 +83,7 @@ void RTPathCPURenderEngine::PauseThreads() {
7083
threadsPauseMode = true;
7184

7285
// Wait for the threads
73-
threadsSyncBarrier->arrive_and_wait();
86+
threadsPauseBarrier->arrive_and_wait();
7487
}
7588

7689
void RTPathCPURenderEngine::ResumeThreads() {

src/slg/engines/rtpathcpu/rtpathcputhread.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ RTPathCPURenderThread::RTPathCPURenderThread(RTPathCPURenderEngine *engine, cons
3636
PathCPURenderThread(engine, index, device) {
3737
}
3838

39-
RTPathCPURenderThread::~RTPathCPURenderThread() {
40-
}
39+
RTPathCPURenderThread::~RTPathCPURenderThread() = default;
4140

4241
void RTPathCPURenderThread::StartRenderThread() {
4342
// Avoid to allocate the film thread because I'm going to use the global one
@@ -83,7 +82,7 @@ void RTPathCPURenderThread::RTRenderFunc(std::stop_token stop_token) {
8382
// Check if we are in pause or edit mode
8483
if (engine->threadsPauseMode) {
8584
// Synchronize all threads -> This waits for RTPathCPURenderEngine::PauseThreads()
86-
engine->threadsSyncBarrier->arrive_and_wait();
85+
engine->threadsPauseBarrier->arrive_and_wait();
8786

8887
while (!stop_token.stop_requested() && engine->threadsPauseMode)
8988
std::this_thread::sleep_for(100ms);

src/slg/scene/scene.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ void Scene::moveToTrash(NamedObjectUPtr&& oldObj) {
462462
}
463463

464464

465-
void Scene::emptyTrash() {
465+
void Scene::EmptyTrash() {
466466
std::lock_guard lk(trashMtx);
467467
trashBin.clear();
468468
}

0 commit comments

Comments
 (0)