Skip to content

Commit 9ba0b5b

Browse files
committed
Make physics interpolation compatible with separate-thread rendering
1 parent 42b054a commit 9ba0b5b

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
@@ -2352,6 +2352,8 @@ bool Main::iteration() {
23522352
double step = advance.idle_step;
23532353
double scaled_step = step * time_scale;
23542354

2355+
VisualServer::get_singleton()->sync_and_halt();
2356+
23552357
Engine::get_singleton()->_frame_step = step;
23562358
Engine::get_singleton()->_physics_interpolation_fraction = advance.interpolation_fraction;
23572359

@@ -2391,12 +2393,16 @@ bool Main::iteration() {
23912393
Physics2DServer::get_singleton()->sync();
23922394
Physics2DServer::get_singleton()->flush_queries();
23932395

2396+
VisualServer::get_singleton()->thaw();
2397+
23942398
if (OS::get_singleton()->get_main_loop()->iteration(frame_slice * time_scale)) {
23952399
exit = true;
23962400
Engine::get_singleton()->_in_physics = false;
23972401
break;
23982402
}
23992403

2404+
VisualServer::get_singleton()->sync_and_halt();
2405+
24002406
NavigationServer::get_singleton_mut()->process(frame_slice * time_scale);
24012407
message_queue->flush();
24022408

@@ -2415,6 +2421,8 @@ bool Main::iteration() {
24152421
Engine::get_singleton()->_in_physics = false;
24162422
}
24172423

2424+
VisualServer::get_singleton()->thaw();
2425+
24182426
if (InputDefault::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
24192427
InputDefault::get_singleton()->flush_buffered_events();
24202428
}

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
@@ -803,6 +803,8 @@ class VisualServerRaster : public VisualServer {
803803
virtual void pre_draw(bool p_will_draw);
804804
virtual void draw(bool p_swap_buffers, double frame_step);
805805
virtual void sync();
806+
virtual void sync_and_halt();
807+
virtual void thaw();
806808
virtual bool has_changed(ChangedPriority p_priority = CHANGED_PRIORITY_ANY) const;
807809
virtual void init();
808810
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;
@@ -709,6 +712,8 @@ class VisualServerWrapMT : public VisualServer {
709712
virtual void pre_draw(bool p_will_draw);
710713
virtual void draw(bool p_swap_buffers, double frame_step);
711714
virtual void sync();
715+
virtual void sync_and_halt();
716+
virtual void thaw();
712717
FUNC1RC(bool, has_changed, ChangedPriority)
713718
virtual void set_physics_interpolation_enabled(bool p_enabled);
714719

servers/visual_server.h

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

11901190
virtual void draw(bool p_swap_buffers = true, double frame_step = 0.0) = 0;
11911191
virtual void sync() = 0;
1192+
virtual void sync_and_halt() = 0;
1193+
virtual void thaw() = 0;
11921194
virtual bool has_changed(ChangedPriority p_priority = CHANGED_PRIORITY_ANY) const = 0;
11931195
virtual void init() = 0;
11941196
virtual void finish() = 0;

0 commit comments

Comments
 (0)