-
Notifications
You must be signed in to change notification settings - Fork 37
/
dxvk-async-ab1d629.patch
572 lines (518 loc) · 18.3 KB
/
dxvk-async-ab1d629.patch
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
diff --git a/meson.build b/meson.build
index b6134233..652d1845 100644
--- a/meson.build
+++ b/meson.build
@@ -108,7 +108,7 @@ else
endif
dxvk_version = vcs_tag(
- command: ['git', 'describe', '--dirty=+'],
+ command: ['git', 'describe', '--dirty=-async'],
input: 'version.h.in',
output: 'version.h',
)
diff --git a/src/dxvk/dxvk_context.cpp b/src/dxvk/dxvk_context.cpp
index b1716c32..b1ffc3b1 100644
--- a/src/dxvk/dxvk_context.cpp
+++ b/src/dxvk/dxvk_context.cpp
@@ -4592,7 +4592,8 @@ namespace dxvk {
: DxvkContextFlag::GpDirtyRasterizerState);
// Retrieve and bind actual Vulkan pipeline handle
- auto pipelineInfo = m_state.gp.pipeline->getPipelineHandle(m_state.gp.state);
+ auto pipelineInfo = m_state.gp.pipeline->getPipelineHandle(
+ m_state.gp.state, this->checkAsyncCompilationCompat());
if (unlikely(!pipelineInfo.first))
return false;
@@ -4900,7 +4901,7 @@ namespace dxvk {
}
- void DxvkContext::updateFramebuffer() {
+ void DxvkContext::updateFramebuffer(bool isDraw) {
if (m_flags.test(DxvkContextFlag::GpDirtyFramebuffer)) {
m_flags.clr(DxvkContextFlag::GpDirtyFramebuffer);
@@ -4924,6 +4925,11 @@ namespace dxvk {
m_state.gp.state.omSwizzle[i] = DxvkOmAttachmentSwizzle(mapping);
}
+ if (isDraw) {
+ for (uint32_t i = 0; i < fbInfo.numAttachments(); i++)
+ fbInfo.getAttachment(i).view->setRtBindingFrameId(m_device->getCurrentFrameId());
+ }
+
m_flags.set(DxvkContextFlag::GpDirtyPipelineState);
}
}
@@ -5371,7 +5377,7 @@ namespace dxvk {
}
if (m_flags.test(DxvkContextFlag::GpDirtyFramebuffer))
- this->updateFramebuffer();
+ this->updateFramebuffer(true);
if (!m_flags.test(DxvkContextFlag::GpRenderPassBound))
this->startRenderPass();
@@ -5811,6 +5817,14 @@ namespace dxvk {
return true;
}
+ bool DxvkContext::checkAsyncCompilationCompat() {
+ bool fbCompat = true;
+ for (uint32_t i = 0; fbCompat && i < m_state.om.framebufferInfo.numAttachments(); i++) {
+ const auto& attachment = m_state.om.framebufferInfo.getAttachment(i);
+ fbCompat &= attachment.view->getRtBindingAsyncCompilationCompat();
+ }
+ return fbCompat;
+ }
DxvkGraphicsPipeline* DxvkContext::lookupGraphicsPipeline(
const DxvkGraphicsPipelineShaders& shaders) {
diff --git a/src/dxvk/dxvk_context.h b/src/dxvk/dxvk_context.h
index 3e9eff4e..8079ddf4 100644
--- a/src/dxvk/dxvk_context.h
+++ b/src/dxvk/dxvk_context.h
@@ -1359,7 +1359,7 @@ namespace dxvk {
DxvkFramebufferInfo makeFramebufferInfo(
const DxvkRenderTargets& renderTargets);
- void updateFramebuffer();
+ void updateFramebuffer(bool isDraw = false);
void applyRenderTargetLoadLayouts();
@@ -1431,6 +1431,8 @@ namespace dxvk {
const Rc<DxvkBuffer>& buffer,
VkDeviceSize copySize);
+ bool checkAsyncCompilationCompat();
+
DxvkGraphicsPipeline* lookupGraphicsPipeline(
const DxvkGraphicsPipelineShaders& shaders);
diff --git a/src/dxvk/dxvk_graphics.cpp b/src/dxvk/dxvk_graphics.cpp
index bffb9bce..f86833f4 100644
--- a/src/dxvk/dxvk_graphics.cpp
+++ b/src/dxvk/dxvk_graphics.cpp
@@ -558,7 +558,8 @@ namespace dxvk {
std::pair<VkPipeline, DxvkGraphicsPipelineType> DxvkGraphicsPipeline::getPipelineHandle(
- const DxvkGraphicsPipelineStateInfo& state) {
+ const DxvkGraphicsPipelineStateInfo& state,
+ bool async) {
DxvkGraphicsPipelineInstance* instance = this->findInstance(state);
if (unlikely(!instance)) {
@@ -567,26 +568,33 @@ namespace dxvk {
return std::make_pair(VK_NULL_HANDLE, DxvkGraphicsPipelineType::FastPipeline);
// Prevent other threads from adding new instances and check again
- std::lock_guard<dxvk::mutex> lock(m_mutex);
+ //std::lock_guard<dxvk::mutex> lock(m_mutex);
instance = this->findInstance(state);
if (!instance) {
- // Keep pipeline object locked, at worst we're going to stall
- // a state cache worker and the current thread needs priority.
- bool canCreateBasePipeline = this->canCreateBasePipeline(state);
- instance = this->createInstance(state, canCreateBasePipeline);
-
- // If necessary, compile an optimized pipeline variant
- if (!instance->fastHandle.load())
- m_workers->compileGraphicsPipeline(this, state);
-
- // Only store pipelines in the state cache that cannot benefit
- // from pipeline libraries, or if that feature is disabled.
- if (!canCreateBasePipeline)
- this->writePipelineStateToCache(state);
+ if (async && m_manager->m_compiler != nullptr)
+ m_manager->m_compiler->queueCompilation(this, state);
+ else {
+ // Keep pipeline object locked, at worst we're going to stall
+ // a state cache worker and the current thread needs priority.
+ bool canCreateBasePipeline = this->canCreateBasePipeline(state);
+ instance = this->createInstance(state, canCreateBasePipeline);
+
+ // If necessary, compile an optimized pipeline variant
+ if (!instance->fastHandle.load())
+ m_workers->compileGraphicsPipeline(this, state);
+
+ // Only store pipelines in the state cache that cannot benefit
+ // from pipeline libraries, or if that feature is disabled.
+ if (!canCreateBasePipeline)
+ this->writePipelineStateToCache(state);
+ }
}
}
+ if (!instance)
+ return std::make_pair(VK_NULL_HANDLE, DxvkGraphicsPipelineType::FastPipeline);
+
// Find a pipeline handle to use. If no optimized pipeline has
// been compiled yet, use the slower base pipeline instead.
VkPipeline fastHandle = instance->fastHandle.load();
@@ -598,10 +606,10 @@ namespace dxvk {
}
- void DxvkGraphicsPipeline::compilePipeline(
+ bool DxvkGraphicsPipeline::compilePipeline(
const DxvkGraphicsPipelineStateInfo& state) {
if (m_device->config().enableGraphicsPipelineLibrary == Tristate::True)
- return;
+ return false;
// Try to find an existing instance that contains a base pipeline
DxvkGraphicsPipelineInstance* instance = this->findInstance(state);
@@ -609,12 +617,12 @@ namespace dxvk {
if (!instance) {
// Exit early if the state vector is invalid
if (!this->validatePipelineState(state, false))
- return;
+ return false;
// Do not compile if this pipeline can be fast linked. This essentially
// disables the state cache for pipelines that do not benefit from it.
if (this->canCreateBasePipeline(state))
- return;
+ return false;
// Prevent other threads from adding new instances and check again
std::lock_guard<dxvk::mutex> lock(m_mutex);
@@ -628,7 +636,7 @@ namespace dxvk {
// an optimized version of this pipeline
if (instance->isCompiling.load()
|| instance->isCompiling.exchange(VK_TRUE, std::memory_order_acquire))
- return;
+ return false;
VkPipeline pipeline = this->createOptimizedPipeline(state, 0);
instance->fastHandle.store(pipeline, std::memory_order_release);
@@ -636,6 +644,7 @@ namespace dxvk {
// Log pipeline state on error
if (!pipeline)
this->logPipelineState(LogLevel::Error, state);
+ return true;
}
@@ -675,6 +684,7 @@ namespace dxvk {
if (!fastHandle && !baseHandle)
this->logPipelineState(LogLevel::Error, state);
+ std::lock_guard<dxvk::mutex> lock(m_mutex2);
m_stats->numGraphicsPipelines += 1;
return &(*m_pipelines.emplace(state, baseHandle, fastHandle));
}
@@ -682,6 +692,7 @@ namespace dxvk {
DxvkGraphicsPipelineInstance* DxvkGraphicsPipeline::findInstance(
const DxvkGraphicsPipelineStateInfo& state) {
+ std::lock_guard<dxvk::mutex> lock(m_mutex2);
for (auto& instance : m_pipelines) {
if (instance.state == state)
return &instance;
diff --git a/src/dxvk/dxvk_graphics.h b/src/dxvk/dxvk_graphics.h
index 8ab88b74..9b005c61 100644
--- a/src/dxvk/dxvk_graphics.h
+++ b/src/dxvk/dxvk_graphics.h
@@ -362,10 +362,12 @@ namespace dxvk {
* Retrieves a pipeline handle for the given pipeline
* state. If necessary, a new pipeline will be created.
* \param [in] state Pipeline state vector
+ * \param [in] async Compile asynchronously
* \returns Pipeline handle and handle type
*/
std::pair<VkPipeline, DxvkGraphicsPipelineType> getPipelineHandle(
- const DxvkGraphicsPipelineStateInfo& state);
+ const DxvkGraphicsPipelineStateInfo& state,
+ bool async);
/**
* \brief Compiles a pipeline
@@ -374,8 +376,11 @@ namespace dxvk {
* and stores the result for future use.
* \param [in] state Pipeline state vector
*/
- void compilePipeline(
+ bool compilePipeline(
const DxvkGraphicsPipelineStateInfo& state);
+
+ void writePipelineStateToCache(
+ const DxvkGraphicsPipelineStateInfo& state) const;
private:
@@ -399,6 +404,8 @@ namespace dxvk {
// List of pipeline instances, shared between threads
alignas(CACHE_LINE_SIZE)
dxvk::mutex m_mutex;
+ alignas(CACHE_LINE_SIZE)
+ dxvk::mutex m_mutex2;
sync::List<DxvkGraphicsPipelineInstance> m_pipelines;
sync::List<DxvkGraphicsPipelineBaseInstance> m_basePipelines;
@@ -440,9 +447,6 @@ namespace dxvk {
const DxvkGraphicsPipelineStateInfo& state,
bool trusted) const;
- void writePipelineStateToCache(
- const DxvkGraphicsPipelineStateInfo& state) const;
-
void logPipelineState(
LogLevel level,
const DxvkGraphicsPipelineStateInfo& state) const;
diff --git a/src/dxvk/dxvk_image.h b/src/dxvk/dxvk_image.h
index 31458413..900de6fc 100644
--- a/src/dxvk/dxvk_image.h
+++ b/src/dxvk/dxvk_image.h
@@ -552,6 +552,37 @@ namespace dxvk {
view->imageSubresources());
}
+ /**
+ * \brief Sets render target usage frame number
+ *
+ * The image view will track internally when
+ * it was last used as a render target. This
+ * info is used for async shader compilation.
+ * \param [in] frameId Frame number
+ */
+ void setRtBindingFrameId(uint32_t frameId) {
+ if (frameId != m_rtBindingFrameId) {
+ if (frameId == m_rtBindingFrameId + 1)
+ m_rtBindingFrameCount += 1;
+ else
+ m_rtBindingFrameCount = 0;
+
+ m_rtBindingFrameId = frameId;
+ }
+ }
+
+ /**
+ * \brief Checks for async pipeline compatibility
+ *
+ * Asynchronous pipeline compilation may be enabled if the
+ * render target has been drawn to in the previous frames.
+ * \param [in] frameId Current frame ID
+ * \returns \c true if async compilation is supported
+ */
+ bool getRtBindingAsyncCompilationCompat() const {
+ return m_rtBindingFrameCount >= 5;
+ }
+
private:
Rc<vk::DeviceFn> m_vkd;
@@ -564,6 +595,9 @@ namespace dxvk {
static std::atomic<uint64_t> s_cookie;
+ uint32_t m_rtBindingFrameId = 0;
+ uint32_t m_rtBindingFrameCount = 0;
+
void createView(VkImageViewType type, uint32_t numLayers);
};
diff --git a/src/dxvk/dxvk_options.cpp b/src/dxvk/dxvk_options.cpp
index d251cbcf..52e5e29d 100644
--- a/src/dxvk/dxvk_options.cpp
+++ b/src/dxvk/dxvk_options.cpp
@@ -10,6 +10,15 @@ namespace dxvk {
useRawSsbo = config.getOption<Tristate>("dxvk.useRawSsbo", Tristate::Auto);
shrinkNvidiaHvvHeap = config.getOption<bool> ("dxvk.shrinkNvidiaHvvHeap", false);
hud = config.getOption<std::string>("dxvk.hud", "");
+ numAsyncThreads = config.getOption<int32_t> ("dxvk.numAsyncThreads", 0);
+
+ if (env::getEnvVar("DXVK_ASYNC") == "1")
+ enableAsync = true;
+ else
+ enableAsync = config.getOption<bool>("dxvk.enableAsync", false);
+
+ if (enableAsync)
+ enableGraphicsPipelineLibrary = Tristate::False;
}
}
diff --git a/src/dxvk/dxvk_options.h b/src/dxvk/dxvk_options.h
index c2f7f11b..6eea8e29 100644
--- a/src/dxvk/dxvk_options.h
+++ b/src/dxvk/dxvk_options.h
@@ -2,6 +2,8 @@
#include "../util/config/config.h"
+#include "dxvk_include.h"
+
namespace dxvk {
struct DxvkOptions {
@@ -21,6 +23,13 @@ namespace dxvk {
/// Enable graphics pipeline library
Tristate enableGraphicsPipelineLibrary;
+ // Enable async pipelines
+ bool enableAsync;
+
+ /// Number of compiler threads
+ /// when using async pipelines
+ int32_t numAsyncThreads;
+
/// Shader-related options
Tristate useRawSsbo;
diff --git a/src/dxvk/dxvk_pipecompiler.cpp b/src/dxvk/dxvk_pipecompiler.cpp
new file mode 100644
index 00000000..1d5e35d5
--- /dev/null
+++ b/src/dxvk/dxvk_pipecompiler.cpp
@@ -0,0 +1,76 @@
+#include "dxvk_device.h"
+#include "dxvk_graphics.h"
+#include "dxvk_pipecompiler.h"
+
+namespace dxvk {
+
+ DxvkPipelineCompiler::DxvkPipelineCompiler(const DxvkDevice* device) {
+ uint32_t numCpuCores = dxvk::thread::hardware_concurrency();
+ uint32_t numWorkers = ((std::max(1u, numCpuCores) - 1) * 5) / 7;
+
+ if (numWorkers < 1) numWorkers = 1;
+ if (numWorkers > 32) numWorkers = 32;
+
+ if (device->config().numAsyncThreads > 0)
+ numWorkers = device->config().numAsyncThreads;
+
+ Logger::info(str::format("DXVK: Using ", numWorkers, " async compiler threads"));
+
+ // Start the compiler threads
+ m_compilerThreads.resize(numWorkers);
+
+ for (uint32_t i = 0; i < numWorkers; i++) {
+ m_compilerThreads.at(i) = dxvk::thread(
+ [this] { this->runCompilerThread(); });
+ }
+ }
+
+
+ DxvkPipelineCompiler::~DxvkPipelineCompiler() {
+ }
+
+ void DxvkPipelineCompiler::stopWorkerThreads() {
+ { std::lock_guard<std::mutex> lock(m_compilerLock);
+ m_compilerStop.store(true);
+ }
+
+ m_compilerCond.notify_all();
+ for (auto& thread : m_compilerThreads)
+ thread.join();
+ }
+
+ void DxvkPipelineCompiler::queueCompilation(
+ DxvkGraphicsPipeline* pipeline,
+ const DxvkGraphicsPipelineStateInfo& state) {
+ std::lock_guard<std::mutex> lock(m_compilerLock);
+ m_compilerQueue.push({ pipeline, state });
+ m_compilerCond.notify_one();
+ }
+
+
+ void DxvkPipelineCompiler::runCompilerThread() {
+ env::setThreadName("dxvk-pcompiler");
+
+ while (!m_compilerStop.load()) {
+ PipelineEntry entry;
+
+ { std::unique_lock<std::mutex> lock(m_compilerLock);
+
+ m_compilerCond.wait(lock, [this] {
+ return m_compilerStop.load()
+ || m_compilerQueue.size() != 0;
+ });
+
+ if (m_compilerQueue.size() != 0) {
+ entry = std::move(m_compilerQueue.front());
+ m_compilerQueue.pop();
+ }
+ }
+
+ if (entry.pipeline != nullptr && entry.pipeline->compilePipeline(entry.state)) {
+ entry.pipeline->writePipelineStateToCache(entry.state);
+ }
+ }
+ }
+
+}
diff --git a/src/dxvk/dxvk_pipecompiler.h b/src/dxvk/dxvk_pipecompiler.h
new file mode 100644
index 00000000..2f029956
--- /dev/null
+++ b/src/dxvk/dxvk_pipecompiler.h
@@ -0,0 +1,63 @@
+#pragma once
+
+#include <atomic>
+#include <condition_variable>
+#include <mutex>
+#include <queue>
+
+#include "../util/thread.h"
+#include "dxvk_include.h"
+
+namespace dxvk {
+
+ class DxvkDevice;
+ class DxvkGraphicsPipeline;
+ class DxvkGraphicsPipelineStateInfo;
+
+ /**
+ * \brief Pipeline compiler
+ *
+ * Asynchronous pipeline compiler
+ */
+ class DxvkPipelineCompiler : public RcObject {
+
+ public:
+
+ DxvkPipelineCompiler(const DxvkDevice* device);
+ ~DxvkPipelineCompiler();
+
+ /**
+ * \brief Compiles a pipeline asynchronously
+ *
+ * This should be used to compile graphics
+ * pipeline instances asynchronously.
+ * \param [in] pipeline The pipeline object
+ * \param [in] state The pipeline state info object
+ */
+ void queueCompilation(
+ DxvkGraphicsPipeline* pipeline,
+ const DxvkGraphicsPipelineStateInfo& state);
+
+ /**
+ * \brief Stops async compiler threads
+ */
+ void stopWorkerThreads();
+
+ private:
+
+ struct PipelineEntry {
+ DxvkGraphicsPipeline* pipeline = nullptr;
+ DxvkGraphicsPipelineStateInfo state;
+ };
+
+ std::atomic<bool> m_compilerStop = { false };
+ std::mutex m_compilerLock;
+ std::condition_variable m_compilerCond;
+ std::queue<PipelineEntry> m_compilerQueue;
+ std::vector<dxvk::thread> m_compilerThreads;
+
+ void runCompilerThread();
+
+ };
+
+}
diff --git a/src/dxvk/dxvk_pipemanager.cpp b/src/dxvk/dxvk_pipemanager.cpp
index 589f095f..b78f3b6b 100644
--- a/src/dxvk/dxvk_pipemanager.cpp
+++ b/src/dxvk/dxvk_pipemanager.cpp
@@ -164,6 +164,9 @@ namespace dxvk {
: m_device (device),
m_workers (device),
m_stateCache(device, this, &m_workers) {
+ if (device->config().enableAsync)
+ m_compiler = new DxvkPipelineCompiler(device);
+
Logger::info(str::format("DXVK: Graphics pipeline libraries ",
(m_device->canUseGraphicsPipelineLibrary() ? "supported" : "not supported")));
@@ -299,6 +302,8 @@ namespace dxvk {
void DxvkPipelineManager::stopWorkerThreads() {
+ if (m_compiler != nullptr)
+ m_compiler->stopWorkerThreads();
m_workers.stopWorkers();
m_stateCache.stopWorkers();
}
diff --git a/src/dxvk/dxvk_pipemanager.h b/src/dxvk/dxvk_pipemanager.h
index 9e6ed35e..74aa7535 100644
--- a/src/dxvk/dxvk_pipemanager.h
+++ b/src/dxvk/dxvk_pipemanager.h
@@ -8,6 +8,7 @@
#include "dxvk_compute.h"
#include "dxvk_graphics.h"
#include "dxvk_state_cache.h"
+#include "dxvk_pipecompiler.h"
namespace dxvk {
@@ -223,6 +224,7 @@ namespace dxvk {
DxvkDevice* m_device;
DxvkPipelineWorkers m_workers;
DxvkStateCache m_stateCache;
+ Rc<DxvkPipelineCompiler> m_compiler;
DxvkPipelineStats m_stats;
dxvk::mutex m_mutex;
diff --git a/src/dxvk/meson.build b/src/dxvk/meson.build
index 99100b32..ffd2e673 100644
--- a/src/dxvk/meson.build
+++ b/src/dxvk/meson.build
@@ -92,6 +92,7 @@ dxvk_src = files([
'dxvk_openvr.cpp',
'dxvk_openxr.cpp',
'dxvk_options.cpp',
+ 'dxvk_pipecompiler.cpp',
'dxvk_pipelayout.cpp',
'dxvk_pipemanager.cpp',
'dxvk_queue.cpp',