Debian Codegraph @

Source/Core/VideoCommon/AsyncShaderCompiler.h

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <atomic>
7 #include <condition_variable>
8 #include <deque>
9 #include <functional>
10 #include <map>
11 #include <memory>
12 #include <mutex>
13 #include <thread>
14 #include <utility>
15 #include <vector>
16
17 #include "Common/CommonTypes.h"
18 #include "Common/Event.h"
19 #include "Common/Flag.h"
20
21 namespace VideoCommon
22 {
23 class AsyncShaderCompiler
24 {
25 public:
26 class WorkItem
27 {
28 public:
29 virtual ~WorkItem() = default;
30 virtual bool Compile() = 0;
31 virtual void Retrieve() = 0;
32 };
33
34 using WorkItemPtr = std::unique_ptr<WorkItem>;
35
36 AsyncShaderCompiler();
37 virtual ~AsyncShaderCompiler();
38
39 template <typename T, typename... Params>
40 static WorkItemPtr CreateWorkItem(Params&&... params)
41 {
42 return std::make_unique<T>(std::forward<Params>(params)...);
43 }
44
45 // Queues a new work item to the compiler threads. The lower the priority, the sooner
46 // this work item will be compiled, relative to the other work items.
47 void QueueWorkItem(WorkItemPtr item, u32 priority);
48 void RetrieveWorkItems();
49 bool HasPendingWork();
50 bool HasCompletedWork();
51
52 // Clears both pending and completed work
53 void ClearAllWork();
54
55 // Calls progress_callback periodically, with completed_items, and total_items.
56 // Returns false if interrupted.
57 bool WaitUntilCompletion(const std::function<void(size_t, size_t)>& progress_callback);
58
59 // Needed because of calling virtual methods in shutdown procedure.
60 bool StartWorkerThreads(u32 num_worker_threads);
61 bool ResizeWorkerThreads(u32 num_worker_threads);
62 bool HasWorkerThreads() const;
63 void StopWorkerThreads();
64
65 protected:
66 virtual bool WorkerThreadInitMainThread(void** param);
67 virtual bool WorkerThreadInitWorkerThread(void* param);
68 virtual void WorkerThreadExit(void* param);
69
70 private:
71 void WorkerThreadEntryPoint(void* param);
72 void WorkerThreadRun();
73
74 Common::Flag m_exit_flag;
75 Common::Event m_init_event;
76
77 std::vector<std::thread> m_worker_threads;
78 std::atomic_bool m_worker_thread_start_result{false};
79
80 // A multimap is used to store the work items. We can't use a priority_queue here, because
81 // there's no way to obtain a non-const reference, which we need for the unique_ptr.
82 std::multimap<u32, WorkItemPtr> m_pending_work;
83 std::mutex m_pending_work_lock;
84 std::condition_variable m_worker_thread_wake;
85 std::atomic_size_t m_busy_workers{0};
86
87 std::deque<WorkItemPtr> m_completed_work;
88 std::mutex m_completed_work_lock;
89 };
90
91 } // namespace VideoCommon