/
dxvk_state_cache.h
194 lines (148 loc) · 5.29 KB
/
dxvk_state_cache.h
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
#pragma once
#include <atomic>
#include <condition_variable>
#include <fstream>
#include <mutex>
#include <queue>
#include <unordered_map>
#include <vector>
#include "dxvk_state_cache_types.h"
namespace dxvk {
class DxvkDevice;
/**
* \brief State cache
*
* The shader state cache stores state vectors and
* render pass formats of all pipelines used in a
* game, which allows DXVK to compile them ahead
* of time instead of compiling them on the first
* draw.
*/
class DxvkStateCache : public RcObject {
public:
DxvkStateCache(
const DxvkDevice* device,
DxvkPipelineManager* pipeManager,
DxvkRenderPassPool* passManager);
~DxvkStateCache();
/**
* Adds a graphics pipeline to the cache
*
* If the pipeline is not already cached, this
* will write a new pipeline to the cache file.
* \param [in] shaders Shader keys
* \param [in] state Graphics pipeline state
* \param [in] format Render pass format
*/
void addGraphicsPipeline(
const DxvkStateCacheKey& shaders,
const DxvkGraphicsPipelineStateInfo& state,
const DxvkRenderPassFormat& format);
/**
* Adds a compute pipeline to the cache
*
* If the pipeline is not already cached, this
* will write a new pipeline to the cache file.
* \param [in] shaders Shader keys
* \param [in] state Compute pipeline state
*/
void addComputePipeline(
const DxvkStateCacheKey& shaders,
const DxvkComputePipelineStateInfo& state);
/**
* \brief Registers a newly compiled shader
*
* Makes the shader available to the pipeline
* compiler, and starts compiling all pipelines
* for which all shaders become available.
* \param [in] shader The shader to add
*/
void registerShader(
const Rc<DxvkShader>& shader);
/**
* \brief Checks whether compiler threads are busy
* \returns \c true if we're compiling shaders
*/
bool isCompilingShaders() {
return m_workerBusy.load() > 0;
}
private:
using WriterItem = DxvkStateCacheEntry;
struct WorkerItem {
DxvkGraphicsPipelineShaders gp;
DxvkComputePipelineShaders cp;
};
DxvkPipelineManager* m_pipeManager;
DxvkRenderPassPool* m_passManager;
std::vector<DxvkStateCacheEntry> m_entries;
std::atomic<bool> m_stopThreads = { false };
std::mutex m_entryLock;
std::unordered_multimap<
DxvkStateCacheKey, size_t,
DxvkHash, DxvkEq> m_entryMap;
std::unordered_multimap<
DxvkShaderKey, DxvkStateCacheKey,
DxvkHash, DxvkEq> m_pipelineMap;
std::unordered_map<
DxvkShaderKey, Rc<DxvkShader>,
DxvkHash, DxvkEq> m_shaderMap;
std::mutex m_workerLock;
std::condition_variable m_workerCond;
std::queue<WorkerItem> m_workerQueue;
std::atomic<uint32_t> m_workerBusy;
std::vector<dxvk::thread> m_workerThreads;
std::mutex m_writerLock;
std::condition_variable m_writerCond;
std::queue<WriterItem> m_writerQueue;
dxvk::thread m_writerThread;
DxvkShaderKey getShaderKey(
const Rc<DxvkShader>& shader) const;
bool getShaderByKey(
const DxvkShaderKey& key,
Rc<DxvkShader>& shader) const;
void mapPipelineToEntry(
const DxvkStateCacheKey& key,
size_t entryId);
void mapShaderToPipeline(
const DxvkShaderKey& shader,
const DxvkStateCacheKey& key);
void compilePipelines(
const WorkerItem& item);
bool readCacheFile();
bool readCacheHeader(
std::istream& stream,
DxvkStateCacheHeader& header) const;
bool readCacheEntryV7(
uint32_t version,
std::istream& stream,
DxvkStateCacheEntry& entry) const;
bool readCacheEntry(
uint32_t version,
std::istream& stream,
DxvkStateCacheEntry& entry) const;
void writeCacheEntry(
std::ostream& stream,
DxvkStateCacheEntry& entry) const;
bool convertEntryV2(
DxvkStateCacheEntryV4& entry) const;
bool convertEntryV4(
const DxvkStateCacheEntryV4& in,
DxvkStateCacheEntryV6& out) const;
bool convertEntryV5(
const DxvkStateCacheEntryV5& in,
DxvkStateCacheEntryV6& out) const;
bool convertEntryV6(
const DxvkStateCacheEntryV6& in,
DxvkStateCacheEntry& out) const;
void workerFunc();
void writerFunc();
std::wstring getCacheFileName() const;
std::string getCacheDir() const;
static uint8_t packImageLayout(
VkImageLayout layout);
static VkImageLayout unpackImageLayout(
uint8_t layout);
static bool validateRenderPassFormat(
const DxvkRenderPassFormat& format);
};
}