Skip to content

Commit

Permalink
swapchain cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
martty committed Feb 11, 2024
1 parent 9085b4c commit 1ba94ff
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
3 changes: 2 additions & 1 deletion examples/example_runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace vuk {
if (width == 0 && height == 0) {
runner.suspend = true;
} else {
runner.swapchain = util::make_swapchain(*runner.superframe_allocator, runner.vkbdevice, runner.swapchain->surface, runner.swapchain);
runner.swapchain = util::make_swapchain(*runner.superframe_allocator, runner.vkbdevice, runner.swapchain->surface, std::move(runner.swapchain));
for (auto& iv : runner.swapchain->images) {
runner.context->set_name(iv.image_view.payload, "Swapchain ImageView");
}
Expand Down Expand Up @@ -137,6 +137,7 @@ namespace vuk {
render_complete.reset();
imgui_data.font_image.reset();
imgui_data.font_image_view.reset();
swapchain.reset();
superframe_resource.reset();
context.reset();
auto vkDestroySurfaceKHR = (PFN_vkDestroySurfaceKHR)vkbinstance.fp_vkGetInstanceProcAddr(vkbinstance.instance, "vkDestroySurfaceKHR");
Expand Down
2 changes: 1 addition & 1 deletion examples/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace util {

old_swapchain->swapchain = vkswapchain->swapchain;
old_swapchain->surface = surface;
return *old_swapchain;
return std::move(*old_swapchain);
}

struct ImGuiData {
Expand Down
3 changes: 1 addition & 2 deletions include/vuk/IR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,9 @@ namespace vuk {
}

Ref make_declare_swapchain(Swapchain& bundle) {
auto swp_ptr = new Swapchain(bundle);
auto args_ptr = new Ref[2];
auto mem_ty = new Type*(emplace_type(Type{ .kind = Type::MEMORY_TY }));
args_ptr[0] = first(emplace_op(Node{ .kind = Node::CONSTANT, .type = std::span{ mem_ty, 1 }, .constant = { .value = swp_ptr } }));
args_ptr[0] = first(emplace_op(Node{ .kind = Node::CONSTANT, .type = std::span{ mem_ty, 1 }, .constant = { .value = &bundle } }));
std::vector<Ref> imgs;
for (auto i = 0; i < bundle.images.size(); i++) {
imgs.push_back(make_declare_image(bundle.images[i]));
Expand Down
2 changes: 1 addition & 1 deletion include/vuk/RenderGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ private:
return { make_ext_ref(rg, ref), ref };
}

[[nodiscard]] inline Value<Swapchain> declare_swapchain(Swapchain bundle, std::source_location loc = std::source_location::current()) {
[[nodiscard]] inline Value<Swapchain> declare_swapchain(Swapchain& bundle, std::source_location loc = std::source_location::current()) {
std::shared_ptr<RG> rg = std::make_shared<RG>();
Ref ref = rg->make_declare_swapchain(bundle);
rg->set_source_location(ref.node, loc);
Expand Down
11 changes: 9 additions & 2 deletions include/vuk/Swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ namespace vuk {

struct Swapchain {
Swapchain(Allocator allocator, size_t image_count);
Swapchain(const Swapchain&) = delete;
Swapchain(Swapchain&&) noexcept;

Swapchain& operator=(const Swapchain&) = delete;
Swapchain& operator=(Swapchain&&) noexcept;

~Swapchain();

Allocator allocator;
VkSwapchainKHR swapchain;
VkSurfaceKHR surface;
VkSwapchainKHR swapchain = VK_NULL_HANDLE;
VkSurfaceKHR surface = VK_NULL_HANDLE;

std::vector<ImageAttachment> images;
uint32_t linear_index = 0;
Expand Down
36 changes: 35 additions & 1 deletion src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ namespace vuk {
allocator.allocate_semaphores(std::span(semaphores));
}

Swapchain::~Swapchain() {
if (swapchain != VK_NULL_HANDLE) {
allocator.deallocate(std::span{ &swapchain, 1 });
}
for (auto& i : images) {
allocator.deallocate(std::span{ &i.image_view, 1 });
}
allocator.deallocate(std::span(semaphores));
}

Swapchain::Swapchain(Swapchain&& o) noexcept :
swapchain(std::exchange(o.swapchain, VK_NULL_HANDLE)),
semaphores(std::move(o.semaphores)),
allocator(o.allocator) {
images = std::move(o.images);
surface = o.surface;
linear_index = o.linear_index;
image_index = o.image_index;
acquire_result = o.acquire_result;
}

Swapchain& Swapchain::operator=(Swapchain&& o) noexcept {
swapchain = std::exchange(o.swapchain, VK_NULL_HANDLE);
semaphores = std::move(o.semaphores);
allocator = o.allocator;
images = std::move(o.images);
surface = o.surface;
linear_index = o.linear_index;
image_index = o.image_index;
acquire_result = o.acquire_result;

return *this;
}

Result<void> Context::wait_for_domains(std::span<SyncPoint> queue_waits) {
std::array<uint32_t, 3> domain_to_sema_index = { ~0u, ~0u, ~0u };
std::array<VkSemaphore, 3> queue_timeline_semaphores;
Expand Down Expand Up @@ -114,7 +148,7 @@ namespace vuk {
} else if (acqrel->status == Signal::Status::eHostAvailable || acqrel->status == Signal::Status::eSynchronizable) {
return { expected_value }; // nothing to do
} else {
//acqrel->status = Signal::Status::eSynchronizable;
// acqrel->status = Signal::Status::eSynchronizable;
auto erg = compiler.link(std::span{ &node, 1 }, options);
if (!erg) {
return erg;
Expand Down

0 comments on commit 1ba94ff

Please sign in to comment.