Skip to content

Commit

Permalink
merian: DescriptorSetUpdate: Keep buffer alive
Browse files Browse the repository at this point in the history
  • Loading branch information
LDAP committed Nov 20, 2024
1 parent fc1d000 commit 9431e64
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 5 additions & 4 deletions include/merian/vk/descriptors/descriptor_set_update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DescriptorSetUpdate {
const vk::DeviceSize range = VK_WHOLE_SIZE,
const uint32_t dst_array_element = 0,
const uint32_t descriptor_count = 1) {
resources.emplace_back(buffer);
return write_descriptor_buffer_type(binding, *buffer, set->get_type_for_binding(binding),
offset, range, dst_array_element, descriptor_count);
}
Expand Down Expand Up @@ -100,11 +101,11 @@ class DescriptorSetUpdate {
// the descriptor". If std::nullopt the current layout is used.
DescriptorSetUpdate&
write_descriptor_texture(const uint32_t binding,
const TextureHandle texture,
const TextureHandle& texture,
const uint32_t dst_array_element = 0,
const uint32_t descriptor_count = 1,
const std::optional<vk::ImageLayout> access_layout = std::nullopt) {
textures.emplace_back(texture);
resources.emplace_back(texture);
return write_descriptor_image_type(
binding, set->get_type_for_binding(binding), texture->get_view(),
access_layout.value_or(texture->get_current_layout()), *texture->get_sampler(),
Expand Down Expand Up @@ -171,14 +172,14 @@ class DescriptorSetUpdate {
write_buffer_infos.clear();
write_image_infos.clear();
write_acceleration_structures.clear();
textures.clear();
resources.clear();
}

private:
std::shared_ptr<DescriptorSet> set;

std::vector<vk::WriteDescriptorSet> writes;
std::vector<TextureHandle> textures;
std::vector<ResourceHandle> resources;

// vk::WriteDescriptorSet takes pointers. We must ensure that these stay valid until update() ->
// use unique ptrs
Expand Down
12 changes: 8 additions & 4 deletions include/merian/vk/memory/resource_allocations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

namespace merian {

class Resource {};
using ResourceHandle = std::shared_ptr<Resource>;

// Forward def
class MemoryAllocation;
using MemoryAllocationHandle = std::shared_ptr<MemoryAllocation>;
class Buffer;
using BufferHandle = std::shared_ptr<Buffer>;

class Buffer : public std::enable_shared_from_this<Buffer> {
class Buffer : public std::enable_shared_from_this<Buffer>, public Resource {

public:
constexpr static vk::BufferUsageFlags SCRATCH_BUFFER_USAGE =
Expand Down Expand Up @@ -116,7 +119,7 @@ using ImageHandle = std::shared_ptr<Image>;
*
* Use the barrier() function to perform layout transitions to keep the internal state valid.
*/
class Image : public std::enable_shared_from_this<Image> {
class Image : public std::enable_shared_from_this<Image>, public Resource {

public:
// Creates a Image objects that automatically destroys Image when destructed.
Expand Down Expand Up @@ -281,7 +284,7 @@ class Image : public std::enable_shared_from_this<Image> {
* Try to only use the barrier() function to perform layout transitions,
* to keep the internal state valid.
*/
class Texture : public std::enable_shared_from_this<Texture> {
class Texture : public std::enable_shared_from_this<Texture>, public Resource {
public:
Texture(const vk::ImageView& view, const ImageHandle& image, const SamplerHandle& sampler);

Expand Down Expand Up @@ -340,7 +343,8 @@ class Texture : public std::enable_shared_from_this<Texture> {

using TextureHandle = std::shared_ptr<Texture>;

class AccelerationStructure : public std::enable_shared_from_this<AccelerationStructure> {
class AccelerationStructure : public std::enable_shared_from_this<AccelerationStructure>,
public Resource {
public:
// Creats a AccelerationStructure objects that automatically destroys `as` when destructed.
// The memory is not freed explicitly to let it free itself.
Expand Down

0 comments on commit 9431e64

Please sign in to comment.