| 1 |
|
| 2 |
|
| 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 |
|
| 46 |
|
| 47 |
void QueueWorkItem(WorkItemPtr item, u32 priority); |
| 48 |
void RetrieveWorkItems(); |
| 49 |
bool HasPendingWork(); |
| 50 |
bool HasCompletedWork(); |
| 51 |
|
| 52 |
|
| 53 |
void ClearAllWork(); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
bool WaitUntilCompletion(const std::function<void(size_t, size_t)>& progress_callback); |
| 58 |
|
| 59 |
|
| 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 |
|
| 81 |
|
| 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 |
} |