Skip to content

Commit

Permalink
ComputePipeline: lazy initialization first time pipeline is bound. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
azonenberg committed Sep 6, 2022
1 parent 27e105f commit 531a9ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
42 changes: 24 additions & 18 deletions scopehal/ComputePipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,35 @@ using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction / destruction

ComputePipeline::ComputePipeline(const std::string& shaderPath, size_t numSSBOs, size_t pushConstantSize)
ComputePipeline::ComputePipeline(const string& shaderPath, size_t numSSBOs, size_t pushConstantSize)
: m_shaderPath(shaderPath)
, m_numSSBOs(numSSBOs)
, m_pushConstantSize(pushConstantSize)
{
m_writeDescriptors.resize(numSSBOs);
m_bufferInfo.resize(numSSBOs);
}

ComputePipeline::~ComputePipeline()
{
//Make sure we destroy some objects in a particular order
//TODO: how much of this really is important?
m_computePipeline = nullptr;
m_descriptorSetLayout = nullptr;
m_pipelineLayout = nullptr;
m_shaderModule = nullptr;
}

void ComputePipeline::DeferredInit()
{
//Load the shader module
auto srcvec = ReadDataFileUint32(shaderPath);
auto srcvec = ReadDataFileUint32(m_shaderPath);
vk::ShaderModuleCreateInfo info({}, srcvec);
m_shaderModule = make_unique<vk::raii::ShaderModule>(*g_vkComputeDevice, info);

//Configure shader input bindings
vector<vk::DescriptorSetLayoutBinding> bindings;
for(size_t i=0; i<numSSBOs; i++)
for(size_t i=0; i<m_numSSBOs; i++)
{
bindings.push_back(vk::DescriptorSetLayoutBinding(
i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute));
Expand All @@ -52,7 +71,7 @@ ComputePipeline::ComputePipeline(const std::string& shaderPath, size_t numSSBOs,
m_descriptorSetLayout = make_unique<vk::raii::DescriptorSetLayout>(*g_vkComputeDevice, dinfo);

//Configure push constants
vk::PushConstantRange range(vk::ShaderStageFlagBits::eCompute, 0, pushConstantSize);
vk::PushConstantRange range(vk::ShaderStageFlagBits::eCompute, 0, m_pushConstantSize);

//Make the pipeline layout
vk::PipelineLayoutCreateInfo linfo(
Expand All @@ -68,7 +87,7 @@ ComputePipeline::ComputePipeline(const std::string& shaderPath, size_t numSSBOs,
std::move(g_vkComputeDevice->createComputePipelines(nullptr, pinfo).front())); //TODO: pipeline cache

//Descriptor pool for our shader parameters
vk::DescriptorPoolSize poolSize(vk::DescriptorType::eStorageBuffer, numSSBOs);
vk::DescriptorPoolSize poolSize(vk::DescriptorType::eStorageBuffer, m_numSSBOs);
vk::DescriptorPoolCreateInfo poolInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet |
vk::DescriptorPoolCreateFlagBits::eUpdateAfterBind,
Expand All @@ -80,17 +99,4 @@ ComputePipeline::ComputePipeline(const std::string& shaderPath, size_t numSSBOs,
vk::DescriptorSetAllocateInfo dsinfo(**m_descriptorPool, **m_descriptorSetLayout);
m_descriptorSet = make_unique<vk::raii::DescriptorSet>(
std::move(vk::raii::DescriptorSets(*g_vkComputeDevice, dsinfo).front()));

m_writeDescriptors.resize(numSSBOs);
m_bufferInfo.resize(numSSBOs);
}

ComputePipeline::~ComputePipeline()
{
//Make sure we destroy some objects in a particular order
//TODO: how much of this really is important?
m_computePipeline = nullptr;
m_descriptorSetLayout = nullptr;
m_pipelineLayout = nullptr;
m_shaderModule = nullptr;
}
15 changes: 15 additions & 0 deletions scopehal/ComputePipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ComputePipeline
template<class T>
void BindBuffer(size_t i, AcceleratorBuffer<T>& buf, bool outputOnly = false)
{
if(m_computePipeline == nullptr)
DeferredInit();

buf.PrepareForGpuAccess(outputOnly);

m_bufferInfo[i] = buf.GetBufferInfo();
Expand All @@ -65,6 +68,9 @@ class ComputePipeline
template<class T>
void BindBufferNonblocking(size_t i, AcceleratorBuffer<T>& buf, vk::raii::CommandBuffer& cmdBuf, bool outputOnly = false)
{
if(m_computePipeline == nullptr)
DeferredInit();

buf.PrepareForGpuAccessNonblocking(outputOnly, cmdBuf);

m_bufferInfo[i] = buf.GetBufferInfo();
Expand Down Expand Up @@ -92,6 +98,9 @@ class ComputePipeline
template<class T>
void Dispatch(vk::raii::CommandBuffer& cmdBuf, T pushConstants, uint32_t x, uint32_t y=1, uint32_t z=1)
{
if(m_computePipeline == nullptr)
DeferredInit();

g_vkComputeDevice->updateDescriptorSets(m_writeDescriptors, nullptr);
cmdBuf.bindPipeline(vk::PipelineBindPoint::eCompute, **m_computePipeline);
cmdBuf.pushConstants<T>(
Expand All @@ -109,6 +118,12 @@ class ComputePipeline
}

protected:
void DeferredInit();

std::string m_shaderPath;
size_t m_numSSBOs;
size_t m_pushConstantSize;

std::unique_ptr<vk::raii::ShaderModule> m_shaderModule;
std::unique_ptr<vk::raii::Pipeline> m_computePipeline;
std::unique_ptr<vk::raii::PipelineLayout> m_pipelineLayout;
Expand Down

0 comments on commit 531a9ac

Please sign in to comment.