2018-03-29 12:06:53 +02:00
|
|
|
#include <chrono>
|
2018-02-14 17:54:35 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
2017-10-13 03:19:23 +02:00
|
|
|
#include "dxvk_compute.h"
|
2018-01-16 15:00:19 +01:00
|
|
|
#include "dxvk_device.h"
|
2018-09-21 19:42:48 +02:00
|
|
|
#include "dxvk_pipemanager.h"
|
2018-05-26 14:54:29 +02:00
|
|
|
#include "dxvk_spec_const.h"
|
2018-09-23 08:32:56 +02:00
|
|
|
#include "dxvk_state_cache.h"
|
2017-10-13 03:19:23 +02:00
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
2018-02-14 17:54:35 +01:00
|
|
|
bool DxvkComputePipelineStateInfo::operator == (const DxvkComputePipelineStateInfo& other) const {
|
|
|
|
return std::memcmp(this, &other, sizeof(DxvkComputePipelineStateInfo)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DxvkComputePipelineStateInfo::operator != (const DxvkComputePipelineStateInfo& other) const {
|
|
|
|
return std::memcmp(this, &other, sizeof(DxvkComputePipelineStateInfo)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-13 03:19:23 +02:00
|
|
|
DxvkComputePipeline::DxvkComputePipeline(
|
2018-09-21 19:42:48 +02:00
|
|
|
DxvkPipelineManager* pipeMgr,
|
2018-01-13 22:18:32 +01:00
|
|
|
const Rc<DxvkShader>& cs)
|
2018-09-21 19:42:48 +02:00
|
|
|
: m_vkd(pipeMgr->m_device->vkd()),
|
2019-04-03 19:46:28 +02:00
|
|
|
m_pipeMgr(pipeMgr), m_cs(cs) {
|
|
|
|
cs->defineResourceSlots(m_slotMapping);
|
2018-06-22 00:33:47 +02:00
|
|
|
|
2019-04-03 19:46:28 +02:00
|
|
|
m_slotMapping.makeDescriptorsDynamic(
|
2018-09-21 19:42:48 +02:00
|
|
|
m_pipeMgr->m_device->options().maxNumDynamicUniformBuffers,
|
|
|
|
m_pipeMgr->m_device->options().maxNumDynamicStorageBuffers);
|
2017-12-07 09:38:31 +01:00
|
|
|
|
2018-01-23 17:40:36 +01:00
|
|
|
m_layout = new DxvkPipelineLayout(m_vkd,
|
2019-05-07 20:25:50 +02:00
|
|
|
m_slotMapping, VK_PIPELINE_BIND_POINT_COMPUTE);
|
2017-12-07 09:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkComputePipeline::~DxvkComputePipeline() {
|
2018-09-23 08:32:56 +02:00
|
|
|
for (const auto& instance : m_pipelines)
|
|
|
|
this->destroyPipeline(instance.pipeline);
|
2017-12-07 09:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-14 17:54:35 +01:00
|
|
|
VkPipeline DxvkComputePipeline::getPipelineHandle(
|
2018-09-21 22:28:06 +02:00
|
|
|
const DxvkComputePipelineStateInfo& state) {
|
2018-09-23 08:32:56 +02:00
|
|
|
VkPipeline newPipelineHandle = VK_NULL_HANDLE;
|
|
|
|
|
|
|
|
{ std::lock_guard<sync::Spinlock> lock(m_mutex);
|
2018-09-25 18:22:58 +02:00
|
|
|
|
|
|
|
if (this->findPipeline(state, newPipelineHandle))
|
|
|
|
return newPipelineHandle;
|
2018-03-16 01:24:03 +01:00
|
|
|
|
2018-09-25 18:22:58 +02:00
|
|
|
// If no pipeline instance exists with the given state
|
|
|
|
// vector, create a new one and add it to the list.
|
2019-07-14 13:58:00 +02:00
|
|
|
newPipelineHandle = this->compilePipeline(state);
|
2018-05-01 16:45:28 +02:00
|
|
|
|
|
|
|
// Add new pipeline to the set
|
2018-09-23 08:32:56 +02:00
|
|
|
m_pipelines.push_back({ state, newPipelineHandle });
|
2018-09-21 22:28:06 +02:00
|
|
|
m_pipeMgr->m_numComputePipelines += 1;
|
2018-05-01 16:45:28 +02:00
|
|
|
}
|
2018-09-23 08:32:56 +02:00
|
|
|
|
|
|
|
if (newPipelineHandle != VK_NULL_HANDLE)
|
|
|
|
this->writePipelineStateToCache(state);
|
|
|
|
|
|
|
|
return newPipelineHandle;
|
2018-05-01 16:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DxvkComputePipeline::findPipeline(
|
|
|
|
const DxvkComputePipelineStateInfo& state,
|
|
|
|
VkPipeline& pipeline) const {
|
|
|
|
for (const PipelineStruct& pair : m_pipelines) {
|
|
|
|
if (pair.stateVector == state) {
|
|
|
|
pipeline = pair.pipeline;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 15:52:39 +02:00
|
|
|
|
2018-05-01 16:45:28 +02:00
|
|
|
return false;
|
2018-02-14 17:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-16 01:24:03 +01:00
|
|
|
VkPipeline DxvkComputePipeline::compilePipeline(
|
2019-07-14 13:58:00 +02:00
|
|
|
const DxvkComputePipelineStateInfo& state) const {
|
2017-10-14 23:52:47 +02:00
|
|
|
std::vector<VkDescriptorSetLayoutBinding> bindings;
|
2018-03-02 02:33:06 -07:00
|
|
|
|
|
|
|
if (Logger::logLevel() <= LogLevel::Debug) {
|
|
|
|
Logger::debug("Compiling compute pipeline...");
|
2019-04-03 19:46:28 +02:00
|
|
|
Logger::debug(str::format(" cs : ", m_cs->debugName()));
|
2018-03-02 02:33:06 -07:00
|
|
|
}
|
2017-10-14 23:52:47 +02:00
|
|
|
|
2019-05-01 00:34:27 +02:00
|
|
|
DxvkSpecConstants specData;
|
|
|
|
for (uint32_t i = 0; i < m_layout->bindingCount(); i++)
|
2019-06-23 18:44:57 +02:00
|
|
|
specData.set(i, state.bsBindingMask.test(i), true);
|
2019-05-01 00:34:27 +02:00
|
|
|
|
|
|
|
VkSpecializationInfo specInfo = specData.getSpecInfo();
|
2019-04-03 19:46:28 +02:00
|
|
|
|
|
|
|
DxvkShaderModuleCreateInfo moduleInfo;
|
|
|
|
moduleInfo.fsDualSrcBlend = false;
|
|
|
|
|
|
|
|
auto csm = m_cs->createShaderModule(m_vkd, m_slotMapping, moduleInfo);
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
VkComputePipelineCreateInfo info;
|
|
|
|
info.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
|
|
|
|
info.pNext = nullptr;
|
2019-07-14 13:58:00 +02:00
|
|
|
info.flags = 0;
|
2019-04-03 20:47:58 +02:00
|
|
|
info.stage = csm.stageInfo(&specInfo);
|
2017-12-03 20:23:26 +01:00
|
|
|
info.layout = m_layout->pipelineLayout();
|
2019-07-14 13:58:00 +02:00
|
|
|
info.basePipelineHandle = VK_NULL_HANDLE;
|
2018-01-10 22:54:00 +01:00
|
|
|
info.basePipelineIndex = -1;
|
2017-10-14 23:52:47 +02:00
|
|
|
|
2018-03-29 12:06:53 +02:00
|
|
|
// Time pipeline compilation for debugging purposes
|
|
|
|
auto t0 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
2018-03-16 01:24:03 +01:00
|
|
|
VkPipeline pipeline = VK_NULL_HANDLE;
|
2017-10-14 23:52:47 +02:00
|
|
|
if (m_vkd->vkCreateComputePipelines(m_vkd->device(),
|
2018-09-21 19:42:48 +02:00
|
|
|
m_pipeMgr->m_cache->handle(), 1, &info, nullptr, &pipeline) != VK_SUCCESS) {
|
2018-01-31 00:48:39 +01:00
|
|
|
Logger::err("DxvkComputePipeline: Failed to compile pipeline");
|
2019-04-03 19:46:28 +02:00
|
|
|
Logger::err(str::format(" cs : ", m_cs->debugName()));
|
2018-03-16 01:24:03 +01:00
|
|
|
return VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
|
2018-03-29 12:06:53 +02:00
|
|
|
auto t1 = std::chrono::high_resolution_clock::now();
|
|
|
|
auto td = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0);
|
|
|
|
Logger::debug(str::format("DxvkComputePipeline: Finished in ", td.count(), " ms"));
|
2018-03-16 01:24:03 +01:00
|
|
|
return pipeline;
|
|
|
|
}
|
2018-09-23 08:32:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
void DxvkComputePipeline::destroyPipeline(VkPipeline pipeline) {
|
|
|
|
m_vkd->vkDestroyPipeline(m_vkd->device(), pipeline, nullptr);
|
|
|
|
}
|
2018-03-16 01:24:03 +01:00
|
|
|
|
|
|
|
|
2018-09-23 08:32:56 +02:00
|
|
|
void DxvkComputePipeline::writePipelineStateToCache(
|
|
|
|
const DxvkComputePipelineStateInfo& state) const {
|
|
|
|
if (m_pipeMgr->m_stateCache == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DxvkStateCacheKey key;
|
|
|
|
|
|
|
|
if (m_cs != nullptr)
|
|
|
|
key.cs = m_cs->getShaderKey();
|
|
|
|
|
|
|
|
m_pipeMgr->m_stateCache->addComputePipeline(key, state);
|
2017-10-13 03:19:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-01 13:51:08 -07:00
|
|
|
}
|