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