2017-12-16 18:10:55 +01:00
|
|
|
#include "dxvk_device.h"
|
|
|
|
#include "dxvk_queue.h"
|
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
|
|
|
DxvkSubmissionQueue::DxvkSubmissionQueue(DxvkDevice* device)
|
2018-09-20 19:58:00 +02:00
|
|
|
: m_device(device),
|
2019-05-05 14:08:07 +02:00
|
|
|
m_submitThread([this] () { submitCmdLists(); }),
|
|
|
|
m_finishThread([this] () { finishCmdLists(); }) {
|
2019-08-04 16:28:32 +02:00
|
|
|
|
2017-12-16 18:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkSubmissionQueue::~DxvkSubmissionQueue() {
|
2018-09-20 19:58:00 +02:00
|
|
|
{ std::unique_lock<std::mutex> lock(m_mutex);
|
2017-12-25 16:05:11 +01:00
|
|
|
m_stopped.store(true);
|
|
|
|
}
|
|
|
|
|
2019-05-05 14:08:07 +02:00
|
|
|
m_appendCond.notify_all();
|
|
|
|
m_submitCond.notify_all();
|
|
|
|
|
|
|
|
m_submitThread.join();
|
|
|
|
m_finishThread.join();
|
2017-12-16 18:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-05 14:08:07 +02:00
|
|
|
void DxvkSubmissionQueue::submit(DxvkSubmitInfo submitInfo) {
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
m_finishCond.wait(lock, [this] {
|
|
|
|
return m_submitQueue.size() + m_finishQueue.size() <= MaxNumQueuedCommandBuffers;
|
|
|
|
});
|
|
|
|
|
2019-07-05 14:27:45 +02:00
|
|
|
DxvkSubmitEntry entry = { };
|
|
|
|
entry.submit = std::move(submitInfo);
|
|
|
|
|
2019-05-05 14:08:07 +02:00
|
|
|
m_pending += 1;
|
2019-07-05 14:27:45 +02:00
|
|
|
m_submitQueue.push(std::move(entry));
|
2019-05-05 14:08:07 +02:00
|
|
|
m_appendCond.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-05 14:27:45 +02:00
|
|
|
void DxvkSubmissionQueue::present(DxvkPresentInfo presentInfo, DxvkSubmitStatus* status) {
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
2019-11-19 23:19:40 +01:00
|
|
|
DxvkSubmitEntry entry = { };
|
|
|
|
entry.status = status;
|
|
|
|
entry.present = std::move(presentInfo);
|
2019-08-04 16:28:32 +02:00
|
|
|
|
2019-11-19 23:19:40 +01:00
|
|
|
m_submitQueue.push(std::move(entry));
|
|
|
|
m_appendCond.notify_all();
|
2019-07-05 14:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkSubmissionQueue::synchronizeSubmission(
|
|
|
|
DxvkSubmitStatus* status) {
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
m_submitCond.wait(lock, [status] {
|
|
|
|
return status->result.load() != VK_NOT_READY;
|
|
|
|
});
|
2019-05-05 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkSubmissionQueue::synchronize() {
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
m_submitCond.wait(lock, [this] {
|
|
|
|
return m_submitQueue.empty();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkSubmissionQueue::lockDeviceQueue() {
|
|
|
|
m_mutexQueue.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkSubmissionQueue::unlockDeviceQueue() {
|
|
|
|
m_mutexQueue.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkSubmissionQueue::submitCmdLists() {
|
|
|
|
env::setThreadName("dxvk-submit");
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
while (!m_stopped.load()) {
|
|
|
|
m_appendCond.wait(lock, [this] {
|
|
|
|
return m_stopped.load() || !m_submitQueue.empty();
|
2018-09-20 19:58:00 +02:00
|
|
|
});
|
|
|
|
|
2019-05-05 14:08:07 +02:00
|
|
|
if (m_stopped.load())
|
|
|
|
return;
|
|
|
|
|
2019-07-05 14:27:45 +02:00
|
|
|
DxvkSubmitEntry entry = std::move(m_submitQueue.front());
|
2019-05-05 14:08:07 +02:00
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
// Submit command buffer to device
|
2019-07-05 14:27:45 +02:00
|
|
|
VkResult status = VK_NOT_READY;
|
2019-05-05 14:08:07 +02:00
|
|
|
|
2019-09-22 18:51:11 +02:00
|
|
|
if (m_lastError != VK_ERROR_DEVICE_LOST) {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutexQueue);
|
2019-05-05 14:08:07 +02:00
|
|
|
|
2019-07-05 14:27:45 +02:00
|
|
|
if (entry.submit.cmdList != nullptr) {
|
|
|
|
status = entry.submit.cmdList->submit(
|
|
|
|
entry.submit.waitSync,
|
|
|
|
entry.submit.wakeSync);
|
|
|
|
} else if (entry.present.presenter != nullptr) {
|
|
|
|
status = entry.present.presenter->presentImage(
|
|
|
|
entry.present.waitSync);
|
|
|
|
}
|
2019-09-22 18:51:11 +02:00
|
|
|
} else {
|
|
|
|
// Don't submit anything after device loss
|
|
|
|
// so that drivers get a chance to recover
|
|
|
|
status = VK_ERROR_DEVICE_LOST;
|
2019-05-05 14:08:07 +02:00
|
|
|
}
|
2019-07-05 14:27:45 +02:00
|
|
|
|
|
|
|
if (entry.status)
|
|
|
|
entry.status->result = status;
|
2019-05-05 14:08:07 +02:00
|
|
|
|
|
|
|
// On success, pass it on to the queue thread
|
|
|
|
lock = std::unique_lock<std::mutex>(m_mutex);
|
|
|
|
|
|
|
|
if (status == VK_SUCCESS) {
|
2019-07-05 14:27:45 +02:00
|
|
|
if (entry.submit.cmdList != nullptr)
|
|
|
|
m_finishQueue.push(std::move(entry));
|
2019-09-22 18:51:11 +02:00
|
|
|
} else if (status == VK_ERROR_DEVICE_LOST || entry.submit.cmdList != nullptr) {
|
2019-07-05 14:27:45 +02:00
|
|
|
Logger::err(str::format("DxvkSubmissionQueue: Command submission failed: ", status));
|
2019-09-22 18:51:11 +02:00
|
|
|
m_lastError = status;
|
|
|
|
m_device->waitForIdle();
|
2019-05-05 14:08:07 +02:00
|
|
|
}
|
2019-07-05 14:27:45 +02:00
|
|
|
|
|
|
|
m_submitQueue.pop();
|
|
|
|
m_submitCond.notify_all();
|
2018-09-20 19:58:00 +02:00
|
|
|
}
|
2017-12-16 18:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-05 14:08:07 +02:00
|
|
|
void DxvkSubmissionQueue::finishCmdLists() {
|
2019-01-11 13:43:15 +01:00
|
|
|
env::setThreadName("dxvk-queue");
|
2018-06-21 15:12:04 +02:00
|
|
|
|
2019-05-05 14:08:07 +02:00
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
2017-12-16 18:10:55 +01:00
|
|
|
while (!m_stopped.load()) {
|
2019-07-18 22:52:20 +02:00
|
|
|
if (m_finishQueue.empty()) {
|
2019-11-26 00:46:24 +00:00
|
|
|
auto t0 = dxvk::high_resolution_clock::now();
|
2019-07-18 22:52:20 +02:00
|
|
|
|
|
|
|
m_submitCond.wait(lock, [this] {
|
|
|
|
return m_stopped.load() || !m_finishQueue.empty();
|
|
|
|
});
|
|
|
|
|
2019-11-26 00:46:24 +00:00
|
|
|
auto t1 = dxvk::high_resolution_clock::now();
|
2019-07-18 22:52:20 +02:00
|
|
|
m_gpuIdle += std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count();
|
|
|
|
}
|
2019-05-05 14:08:07 +02:00
|
|
|
|
|
|
|
if (m_stopped.load())
|
|
|
|
return;
|
2017-12-16 18:10:55 +01:00
|
|
|
|
2019-07-05 14:27:45 +02:00
|
|
|
DxvkSubmitEntry entry = std::move(m_finishQueue.front());
|
2019-05-05 14:08:07 +02:00
|
|
|
lock.unlock();
|
2017-12-16 18:10:55 +01:00
|
|
|
|
2019-09-22 18:51:11 +02:00
|
|
|
VkResult status = m_lastError.load();
|
2019-05-05 14:08:07 +02:00
|
|
|
|
2019-09-22 18:51:11 +02:00
|
|
|
if (status != VK_ERROR_DEVICE_LOST)
|
|
|
|
status = entry.submit.cmdList->synchronize();
|
|
|
|
|
|
|
|
if (status != VK_SUCCESS) {
|
|
|
|
Logger::err(str::format("DxvkSubmissionQueue: Failed to sync fence: ", status));
|
|
|
|
m_lastError = status;
|
|
|
|
m_device->waitForIdle();
|
2017-12-16 18:10:55 +01:00
|
|
|
}
|
2019-05-05 14:08:07 +02:00
|
|
|
|
2019-09-22 18:51:11 +02:00
|
|
|
entry.submit.cmdList->notifySignals();
|
|
|
|
entry.submit.cmdList->reset();
|
|
|
|
|
|
|
|
m_device->recycleCommandList(entry.submit.cmdList);
|
|
|
|
|
2019-05-05 14:08:07 +02:00
|
|
|
lock = std::unique_lock<std::mutex>(m_mutex);
|
|
|
|
m_pending -= 1;
|
|
|
|
|
|
|
|
m_finishQueue.pop();
|
|
|
|
m_finishCond.notify_all();
|
2017-12-16 18:10:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|