Skip to content

Commit 9eaaaf8

Browse files
committed
Make physics interpolation compatible with separate-thread rendering
1 parent 3cd3caa commit 9eaaaf8

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

main/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,8 @@ bool Main::iteration() {
23402340
double step = advance.idle_step;
23412341
double scaled_step = step * time_scale;
23422342

2343+
VisualServer::get_singleton()->sync_and_halt();
2344+
23432345
Engine::get_singleton()->_frame_step = step;
23442346
Engine::get_singleton()->_physics_interpolation_fraction = advance.interpolation_fraction;
23452347

@@ -2379,12 +2381,16 @@ bool Main::iteration() {
23792381
Physics2DServer::get_singleton()->sync();
23802382
Physics2DServer::get_singleton()->flush_queries();
23812383

2384+
VisualServer::get_singleton()->thaw();
2385+
23822386
if (OS::get_singleton()->get_main_loop()->iteration(frame_slice * time_scale)) {
23832387
exit = true;
23842388
Engine::get_singleton()->_in_physics = false;
23852389
break;
23862390
}
23872391

2392+
VisualServer::get_singleton()->sync_and_halt();
2393+
23882394
NavigationServer::get_singleton_mut()->process(frame_slice * time_scale);
23892395
message_queue->flush();
23902396

@@ -2403,6 +2409,8 @@ bool Main::iteration() {
24032409
Engine::get_singleton()->_in_physics = false;
24042410
}
24052411

2412+
VisualServer::get_singleton()->thaw();
2413+
24062414
if (InputDefault::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
24072415
InputDefault::get_singleton()->flush_buffered_events();
24082416
}

servers/visual/visual_server_raster.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,16 @@ void VisualServerRaster::draw(bool p_swap_buffers, double frame_step) {
133133
}
134134
VS::get_singleton()->emit_signal("frame_post_draw");
135135
}
136+
136137
void VisualServerRaster::sync() {
137138
}
138139

140+
void VisualServerRaster::sync_and_halt() {
141+
}
142+
143+
void VisualServerRaster::thaw() {
144+
}
145+
139146
void VisualServerRaster::set_physics_interpolation_enabled(bool p_enabled) {
140147
VSG::scene->set_physics_interpolation_enabled(p_enabled);
141148
VSG::canvas->set_physics_interpolation_enabled(p_enabled);

servers/visual/visual_server_raster.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,8 @@ class VisualServerRaster : public VisualServer {
784784
virtual void pre_draw(bool p_will_draw);
785785
virtual void draw(bool p_swap_buffers, double frame_step);
786786
virtual void sync();
787+
virtual void sync_and_halt();
788+
virtual void thaw();
787789
virtual bool has_changed(ChangedPriority p_priority = CHANGED_PRIORITY_ANY) const;
788790
virtual void init();
789791
virtual void finish();

servers/visual/visual_server_wrap_mt.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ void VisualServerWrapMT::thread_draw(bool p_swap_buffers, double frame_step) {
4343
void VisualServerWrapMT::thread_flush() {
4444
}
4545

46+
void VisualServerWrapMT::thread_halt() {
47+
thread_halt_semaphore.wait();
48+
}
49+
4650
void VisualServerWrapMT::_thread_callback(void *_instance) {
4751
VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance);
4852

@@ -102,6 +106,19 @@ void VisualServerWrapMT::sync() {
102106
}
103107
}
104108

109+
void VisualServerWrapMT::sync_and_halt() {
110+
if (create_thread) {
111+
command_queue.push_and_sync(this, &VisualServerWrapMT::thread_flush);
112+
command_queue.push(this, &VisualServerWrapMT::thread_halt);
113+
}
114+
}
115+
116+
void VisualServerWrapMT::thaw() {
117+
if (create_thread) {
118+
thread_halt_semaphore.post();
119+
}
120+
}
121+
105122
void VisualServerWrapMT::draw(bool p_swap_buffers, double frame_step) {
106123
if (create_thread) {
107124
command_queue.push(this, &VisualServerWrapMT::thread_draw, p_swap_buffers, frame_step);

servers/visual/visual_server_wrap_mt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class VisualServerWrapMT : public VisualServer {
5454
void thread_draw(bool p_swap_buffers, double frame_step);
5555
void thread_flush();
5656

57+
Semaphore thread_halt_semaphore;
58+
void thread_halt();
59+
5760
void thread_exit();
5861

5962
Mutex alloc_mutex;
@@ -690,6 +693,8 @@ class VisualServerWrapMT : public VisualServer {
690693
virtual void pre_draw(bool p_will_draw);
691694
virtual void draw(bool p_swap_buffers, double frame_step);
692695
virtual void sync();
696+
virtual void sync_and_halt();
697+
virtual void thaw();
693698
FUNC1RC(bool, has_changed, ChangedPriority)
694699
virtual void set_physics_interpolation_enabled(bool p_enabled);
695700

servers/visual_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,8 @@ class VisualServer : public Object {
11631163

11641164
virtual void draw(bool p_swap_buffers = true, double frame_step = 0.0) = 0;
11651165
virtual void sync() = 0;
1166+
virtual void sync_and_halt() = 0;
1167+
virtual void thaw() = 0;
11661168
virtual bool has_changed(ChangedPriority p_priority = CHANGED_PRIORITY_ANY) const = 0;
11671169
virtual void init() = 0;
11681170
virtual void finish() = 0;

0 commit comments

Comments
 (0)