rhi: Add rhi::recreate_buffer_to_size

Simple utility to retain or recreate a buffer to a desired size.
This commit is contained in:
Eidolon 2023-04-15 16:29:18 -05:00
parent ae1d0e680a
commit 5f78620bd9
2 changed files with 36 additions and 0 deletions

View file

@ -83,3 +83,28 @@ const ProgramRequirements& rhi::program_requirements_for_program(PipelineProgram
std::terminate();
}
}
bool rhi::recreate_buffer_to_size(Rhi& rhi, Handle<Buffer>& buffer, const BufferDesc& desc)
{
bool recreate = false;
if (buffer == kNullHandle)
{
recreate = true;
}
else
{
std::size_t existing_size = rhi.get_buffer_size(buffer);
if (existing_size < desc.size)
{
rhi.destroy_buffer(buffer);
recreate = true;
}
}
if (recreate)
{
buffer = rhi.create_buffer(desc);
}
return recreate;
}

View file

@ -654,6 +654,17 @@ struct Rhi
virtual void finish() = 0;
};
// Utility functions
/// @brief If the buffer for the given handle is too small or does not exist, creates a new buffer with the given
/// parameters.
/// @param buffer the existing valid buffer handle or kNullHandle, replaced if recreated
/// @param type
/// @param usage
/// @param size the target size of the new buffer
/// @return true if the buffer was recreated, false otherwise
bool recreate_buffer_to_size(Rhi& rhi, Handle<Buffer>& buffer, const BufferDesc& desc);
} // namespace srb2::rhi
#endif // __SRB2_RHI_RHI_HPP__