Debian Codegraph @

modules/jolt_physics/spaces/jolt_job_system.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* jolt_job_system.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "jolt_job_system.h"
32
33 #include "../jolt_project_settings.h"
34
35 #include "core/debugger/engine_debugger.h"
36 #include "core/object/worker_thread_pool.h"
37 #include "core/os/os.h"
38 #include "core/os/time.h"
39
40 #include "Jolt/Physics/PhysicsSettings.h"
41
42 void JoltJobSystem::Job::_execute(void *p_user_data) {
43 Job *job = static_cast<Job *>(p_user_data);
44
45 #ifdef DEBUG_ENABLED
46 const uint64_t time_start = Time::get_singleton()->get_ticks_usec();
47 #endif
48
49 job->Execute();
50
51 #ifdef DEBUG_ENABLED
52 const uint64_t time_end = Time::get_singleton()->get_ticks_usec();
53 const uint64_t time_elapsed = time_end - time_start;
54
55 timings_lock.lock();
56 timings_by_job[job->name] += time_elapsed;
57 timings_lock.unlock();
58 #endif
59
60 job->Release();
61 }
62
63 JoltJobSystem::Job::Job(const char *p_name, JPH::ColorArg p_color, JPH::JobSystem *p_job_system, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count) :
64 JPH::JobSystem::Job(p_name, p_color, p_job_system, p_job_function, p_dependency_count)
65 #ifdef DEBUG_ENABLED
66 ,
67 name(p_name)
68 #endif
69 {
70 }
71
72 JoltJobSystem::Job::~Job() {
73 if (task_id != -1) {
74 WorkerThreadPool::get_singleton()->wait_for_task_completion(task_id);
75 }
76 }
77
78 void JoltJobSystem::Job::push_completed(Job *p_job) {
79 Job *prev_head = nullptr;
80
81 do {
82 prev_head = completed_head.load(std::memory_order_relaxed);
83 p_job->completed_next = prev_head;
84 } while (!completed_head.compare_exchange_weak(prev_head, p_job, std::memory_order_release, std::memory_order_relaxed));
85 }
86
87 JoltJobSystem::Job *JoltJobSystem::Job::pop_completed() {
88 Job *prev_head = nullptr;
89
90 do {
91 prev_head = completed_head.load(std::memory_order_relaxed);
92 if (prev_head == nullptr) {
93 return nullptr;
94 }
95 } while (!completed_head.compare_exchange_weak(prev_head, prev_head->completed_next, std::memory_order_acquire, std::memory_order_relaxed));
96
97 return prev_head;
98 }
99
100 void JoltJobSystem::Job::queue() {
101 AddRef();
102
103 // Ideally we would use Jolt's actual job name here, but I'd rather not incur the overhead of a memory allocation or
104 // thread-safe lookup every time we create/queue a task. So instead we use the same cached description for all of them.
105 static const String task_name("Jolt Physics");
106
107 task_id = WorkerThreadPool::get_singleton()->add_native_task(&_execute, this, true, task_name);
108 }
109
110 int JoltJobSystem::GetMaxConcurrency() const {
111 return thread_count;
112 }
113
114 JPH::JobHandle JoltJobSystem::CreateJob(const char *p_name, JPH::ColorArg p_color, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count) {
115 Job *job = nullptr;
116
117 while (true) {
118 JPH::uint32 job_index = jobs.ConstructObject(p_name, p_color, this, p_job_function, p_dependency_count);
119
120 if (job_index != JPH::FixedSizeFreeList<Job>::cInvalidObjectIndex) {
121 job = &jobs.Get(job_index);
122 break;
123 }
124
125 WARN_PRINT_ONCE("Jolt Physics job system exceeded the maximum number of jobs. This should not happen. Please report this. Waiting for jobs to become available...");
126
127 OS::get_singleton()->delay_usec(100);
128
129 _reclaim_jobs();
130 }
131
132 // This will increment the job's reference count, so must happen before we queue the job
133 JPH::JobHandle job_handle(job);
134
135 if (p_dependency_count == 0) {
136 QueueJob(job);
137 }
138
139 return job_handle;
140 }
141
142 void JoltJobSystem::QueueJob(JPH::JobSystem::Job *p_job) {
143 static_cast<Job *>(p_job)->queue();
144 }
145
146 void JoltJobSystem::QueueJobs(JPH::JobSystem::Job **p_jobs, JPH::uint p_job_count) {
147 for (JPH::uint i = 0; i < p_job_count; ++i) {
148 QueueJob(p_jobs[i]);
149 }
150 }
151
152 void JoltJobSystem::FreeJob(JPH::JobSystem::Job *p_job) {
153 Job::push_completed(static_cast<Job *>(p_job));
154 }
155
156 void JoltJobSystem::_reclaim_jobs() {
157 while (Job *job = Job::pop_completed()) {
158 jobs.DestructObject(job);
159 }
160 }
161
162 JoltJobSystem::JoltJobSystem() :
163 JPH::JobSystemWithBarrier(JPH::cMaxPhysicsBarriers),
164 thread_count(MAX(1, WorkerThreadPool::get_singleton()->get_thread_count())) {
165 jobs.Init(JPH::cMaxPhysicsJobs, JPH::cMaxPhysicsJobs);
166 }
167
168 void JoltJobSystem::pre_step() {
169 // Nothing to do.
170 }
171
172 void JoltJobSystem::post_step() {
173 _reclaim_jobs();
174 }
175
176 #ifdef DEBUG_ENABLED
177
178 void JoltJobSystem::flush_timings() {
179 const StringName profiler_name = SNAME("servers");
180
181 EngineDebugger *engine_debugger = EngineDebugger::get_singleton();
182
183 if (engine_debugger->is_profiling(profiler_name)) {
184 Array timings;
185
186 for (const KeyValue<const void *, uint64_t> &E : timings_by_job) {
187 timings.push_back(static_cast<const char *>(E.key));
188 timings.push_back(USEC_TO_SEC(E.value));
189 }
190
191 timings.push_front("physics_3d");
192
193 engine_debugger->profiler_add_frame_data(profiler_name, timings);
194 }
195
196 for (KeyValue<const void *, uint64_t> &E : timings_by_job) {
197 E.value = 0;
198 }
199 }
200
201 #endif