Skip to content

Commit

Permalink
Retain root constant buffers in program bindings when applied, so tha…
Browse files Browse the repository at this point in the history
…t the buffers are not deleted during commands execution
  • Loading branch information
egorodet committed Sep 30, 2024
1 parent 7d92a98 commit ab1c4ce
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Program
using ShadersByType = std::array<Ptr<Rhi::IShader>, magic_enum::enum_count<Rhi::ShaderType>() - 1>;
static ShadersByType CreateShadersByType(const Ptrs<Rhi::IShader>& shaders);

Ptrs<Rhi::IBuffer> GetRootConstantBufferPtrs(Data::Index frame_index) const;
Data::Size GetBindingsCountAndIncrement() noexcept { return m_bindings_count++; }

template<typename ShaderFuncType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ProgramBindings
void VerifyAllArgumentsAreBoundToResources() const;
const ArgumentBindings& GetArgumentBindings() const { return m_binding_by_argument; }
const Refs<Rhi::IResource>& GetResourceRefsByAccess(Rhi::ProgramArgumentAccessType access_type) const;
void RetainRootConstantBuffers() const;

void ClearTransitionResourceStates();
void RemoveTransitionResourceStates(const Rhi::IProgramArgumentBinding& argument_binding, const Rhi::IResource& resource);
Expand Down Expand Up @@ -128,6 +129,7 @@ class ProgramBindings
ResourceStatesByAccess m_transition_resource_states_by_access;
ResourceRefsByAccess m_resource_refs_by_access;
mutable Ptr<Rhi::IResourceBarriers> m_resource_state_transition_barriers_ptr;
mutable Ptrs<Rhi::IBuffer> m_applied_root_constant_buffer_ptr;
Data::Index m_bindings_index = 0u; // index of this program bindings object between all program bindings of the program
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class RootConstantBuffer

Data::Bytes& GetData();
Rhi::IBuffer& GetBuffer();
const Ptr<Rhi::IBuffer>& GetBufferPtr() const { return m_buffer_ptr; }

void SetBufferName(std::string_view buffer_name);
std::string_view GetBufferName() { return m_buffer_name; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ Program::ShadersByType Program::CreateShadersByType(const Ptrs<Rhi::IShader>& sh
return shaders_by_type;
}

Ptrs<Rhi::IBuffer> Program::GetRootConstantBufferPtrs(Data::Index frame_index) const
{
META_FUNCTION_TASK();
Ptrs<Rhi::IBuffer> root_constant_buffer_ptrs;
root_constant_buffer_ptrs.reserve(3);

if (const Ptr<Rhi::IBuffer>& buffer_ptr = m_root_constant_buffer.GetBufferPtr())
{
root_constant_buffer_ptrs.push_back(buffer_ptr);
}
if (frame_index < m_root_frame_constant_buffers.size())
{
if (const Ptr<Rhi::IBuffer>& buffer_ptr = m_root_frame_constant_buffers.at(frame_index)->GetBufferPtr())
root_constant_buffer_ptrs.push_back(buffer_ptr);
}
if (const Ptr<Rhi::IBuffer>& buffer_ptr = m_root_mutable_buffer.GetBufferPtr())
{
root_constant_buffer_ptrs.push_back(buffer_ptr);
}

return root_constant_buffer_ptrs;
}

static Rhi::ShaderTypes CreateShaderTypes(const Ptrs<Rhi::IShader>& shaders)
{
META_FUNCTION_TASK();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,14 @@ const Refs<Rhi::IResource>& ProgramBindings::GetResourceRefsByAccess(Rhi::Progra
return m_resource_refs_by_access[magic_enum::enum_index(access_type).value()];
}

void ProgramBindings::RetainRootConstantBuffers() const
{
META_FUNCTION_TASK();
// NOTE: We have to retain root constant buffers applied to command list and used during execution,
// because these buffers could be recreated in order to be resized;
// and will be automatically updated in all retained program binding objects.
auto& program = static_cast<Program&>(GetProgram());
m_applied_root_constant_buffer_ptr = program.GetRootConstantBufferPtrs(m_frame_index);
}

} // namespace Methane::Graphics::Base
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,15 @@ Ptr<Rhi::IProgramBindings> ProgramBindings::CreateCopy(const BindingValueByArgum

void ProgramBindings::Apply(Base::CommandList& command_list, ApplyBehaviorMask apply_behavior) const
{
META_FUNCTION_TASK();
Apply(dynamic_cast<ICommandList&>(command_list), command_list.GetProgramBindingsPtr(), apply_behavior);
}

void ProgramBindings::Apply(ICommandList& command_list, const Base::ProgramBindings* applied_program_bindings_ptr, ApplyBehaviorMask apply_behavior) const
{
META_FUNCTION_TASK();
RetainRootConstantBuffers();

Rhi::ProgramArgumentAccessMask apply_access_mask;
apply_access_mask.SetBitOn(Rhi::ProgramArgumentAccessType::Mutable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,9 @@ static void WriteArgumentBindingResourceIds(const ProgramArgumentBinding& arg_bi
void ProgramBindings::Apply(Base::CommandList& command_list, ApplyBehaviorMask apply_behavior) const
{
META_FUNCTION_TASK();
const Rhi::CommandListType command_list_type = command_list.GetType();
switch (command_list_type)
RetainRootConstantBuffers();
switch (const Rhi::CommandListType command_list_type = command_list.GetType();
command_list_type)
{
case Rhi::CommandListType::Render:
Apply<CommandType::Render>(static_cast<RenderCommandList&>(command_list), apply_behavior);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ void ProgramBindings::Apply(ICommandList& command_list_vk, const Rhi::ICommandQu
{
META_FUNCTION_TASK();
META_CHECK_NOT_EMPTY(m_descriptor_sets);
RetainRootConstantBuffers();

Rhi::ProgramArgumentAccessMask apply_access;
apply_access.SetBitOn(Rhi::ProgramArgumentAccessType::Mutable);
Expand Down

0 comments on commit ab1c4ce

Please sign in to comment.