Debian Codegraph @

Source/Core/VideoCommon/AsyncShaderCompiler.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/AsyncShaderCompiler.h"
5
6 #include <thread>
7
8 #include "Common/Assert.h"
9 #include "Common/Logging/Log.h"
10 #include "Common/Thread.h"
11
12 #include "Core/Core.h"
13 #include "Core/System.h"
14
15 namespace VideoCommon
16 {
17 AsyncShaderCompiler::AsyncShaderCompiler()
18 {
19 }
20
21 AsyncShaderCompiler::~AsyncShaderCompiler()
22 {
23 // Pending work can be left at shutdown.
24 // The work item classes are expected to clean up after themselves.
25 ASSERT(!HasWorkerThreads());
26 }
27
28 void AsyncShaderCompiler::QueueWorkItem(WorkItemPtr item, u32 priority)
29 {
30 // If no worker threads are available, compile synchronously.
31 if (!HasWorkerThreads())
32 {
33 item->Compile();
34 m_completed_work.push_back(std::move(item));
35 }
36 else
37 {
38 std::lock_guard guard(m_pending_work_lock);
39 m_pending_work.emplace(priority, std::move(item));
40 m_worker_thread_wake.notify_one();
41 }
42 }
43
44 void AsyncShaderCompiler::RetrieveWorkItems()
45 {
46 std::deque<WorkItemPtr> completed_work;
47 {
48 std::lock_guard guard(m_completed_work_lock);
49 m_completed_work.swap(completed_work);
50 }
51
52 while (!completed_work.empty())
53 {
54 completed_work.front()->Retrieve();
55 completed_work.pop_front();
56 }
57 }
58
59 bool AsyncShaderCompiler::HasPendingWork()
60 {
61 std::lock_guard guard(m_pending_work_lock);
62 return !m_pending_work.empty() || m_busy_workers.load() != 0;
63 }
64
65 bool AsyncShaderCompiler::HasCompletedWork()
66 {
67 std::lock_guard guard(m_completed_work_lock);
68 return !m_completed_work.empty();
69 }
70
71 void AsyncShaderCompiler::ClearAllWork()
72 {
73 {
74 std::lock_guard guard(m_pending_work_lock);
75 m_pending_work.clear();
76 }
77
78 {
79 std::lock_guard guard(m_completed_work_lock);
80 m_completed_work.clear();
81 }
82 }
83
84 bool AsyncShaderCompiler::WaitUntilCompletion(
85 const std::function<void(size_t, size_t)>& progress_callback)
86 {
87 if (!HasPendingWork())
88 return true;
89
90 // Wait a second before opening a progress dialog.
91 // This way, if the operation completes quickly, we don't annoy the user.
92 constexpr u32 CHECK_INTERVAL_MS = 1000 / 30;
93 constexpr auto CHECK_INTERVAL = std::chrono::milliseconds(CHECK_INTERVAL_MS);
94 for (u32 i = 0; i < (1000 / CHECK_INTERVAL_MS); i++)
95 {
96 std::this_thread::sleep_for(std::chrono::milliseconds(CHECK_INTERVAL));
97 if (!HasPendingWork())
98 return true;
99 }
100
101 // Grab the number of pending items. We use this to work out how many are left.
102 size_t total_items;
103 {
104 // Safe to hold both locks here, since nowhere else does.
105 std::lock_guard pending_guard(m_pending_work_lock);
106 std::lock_guard completed_guard(m_completed_work_lock);
107 total_items = m_completed_work.size() + m_pending_work.size() + m_busy_workers.load() + 1;
108 }
109
110 // Update progress while the compiles complete.
111 while (Core::GetState(Core::System::GetInstance()) != Core::State::Stopping)
112 {
113 size_t remaining_items;
114 {
115 std::lock_guard pending_guard(m_pending_work_lock);
116 if (m_pending_work.empty() && !m_busy_workers.load())
117 return true;
118 remaining_items = m_pending_work.size();
119 }
120
121 progress_callback(total_items - remaining_items, total_items);
122 std::this_thread::sleep_for(CHECK_INTERVAL);
123 }
124 return false;
125 }
126
127 bool AsyncShaderCompiler::StartWorkerThreads(u32 num_worker_threads)
128 {
129 if (num_worker_threads == 0)
130 return true;
131
132 for (u32 i = 0; i < num_worker_threads; i++)
133 {
134 void* thread_param = nullptr;
135 if (!WorkerThreadInitMainThread(&thread_param))
136 {
137 WARN_LOG_FMT(VIDEO, "Failed to initialize shader compiler worker thread.");
138 break;
139 }
140
141 m_worker_thread_start_result.store(false);
142
143 std::thread thr(&AsyncShaderCompiler::WorkerThreadEntryPoint, this, thread_param);
144 m_init_event.Wait();
145
146 if (!m_worker_thread_start_result.load())
147 {
148 WARN_LOG_FMT(VIDEO, "Failed to start shader compiler worker thread.");
149 thr.join();
150 break;
151 }
152
153 m_worker_threads.push_back(std::move(thr));
154 }
155
156 return HasWorkerThreads();
157 }
158
159 bool AsyncShaderCompiler::ResizeWorkerThreads(u32 num_worker_threads)
160 {
161 if (m_worker_threads.size() == num_worker_threads)
162 return true;
163
164 StopWorkerThreads();
165 return StartWorkerThreads(num_worker_threads);
166 }
167
168 bool AsyncShaderCompiler::HasWorkerThreads() const
169 {
170 return !m_worker_threads.empty();
171 }
172
173 void AsyncShaderCompiler::StopWorkerThreads()
174 {
175 if (!HasWorkerThreads())
176 return;
177
178 // Signal worker threads to stop, and wake all of them.
179 {
180 std::lock_guard guard(m_pending_work_lock);
181 m_exit_flag.Set();
182 m_worker_thread_wake.notify_all();
183 }
184
185 // Wait for worker threads to exit.
186 for (std::thread& thr : m_worker_threads)
187 thr.join();
188 m_worker_threads.clear();
189 m_exit_flag.Clear();
190 }
191
192 bool AsyncShaderCompiler::WorkerThreadInitMainThread(void** param)
193 {
194 return true;
195 }
196
197 bool AsyncShaderCompiler::WorkerThreadInitWorkerThread(void* param)
198 {
199 return true;
200 }
201
202 void AsyncShaderCompiler::WorkerThreadExit(void* param)
203 {
204 }
205
206 void AsyncShaderCompiler::WorkerThreadEntryPoint(void* param)
207 {
208 Common::SetCurrentThreadName("AsyncShaderCompiler Worker");
209
210 // Initialize worker thread with backend-specific method.
211 if (!WorkerThreadInitWorkerThread(param))
212 {
213 WARN_LOG_FMT(VIDEO, "Failed to initialize shader compiler worker.");
214 m_worker_thread_start_result.store(false);
215 m_init_event.Set();
216 return;
217 }
218
219 m_worker_thread_start_result.store(true);
220 m_init_event.Set();
221
222 WorkerThreadRun();
223
224 WorkerThreadExit(param);
225 }
226
227 void AsyncShaderCompiler::WorkerThreadRun()
228 {
229 std::unique_lock pending_lock(m_pending_work_lock);
230 while (!m_exit_flag.IsSet())
231 {
232 m_worker_thread_wake.wait(pending_lock);
233
234 while (!m_pending_work.empty() && !m_exit_flag.IsSet())
235 {
236 m_busy_workers++;
237 auto iter = m_pending_work.begin();
238 WorkItemPtr item(std::move(iter->second));
239 m_pending_work.erase(iter);
240 pending_lock.unlock();
241
242 if (item->Compile())
243 {
244 std::lock_guard completed_guard(m_completed_work_lock);
245 m_completed_work.push_back(std::move(item));
246 }
247
248 pending_lock.lock();
249 m_busy_workers--;
250 }
251 }
252 }
253
254 } // namespace VideoCommon