Skip to content

Commit

Permalink
finish demo
Browse files Browse the repository at this point in the history
  • Loading branch information
YukinoHayakawa committed Sep 3, 2021
1 parent c5ef8f8 commit f5c8981
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
55 changes: 34 additions & 21 deletions DemoHelloSwapchain/SystemClearSwapchainImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@ struct SystemClearSwapchainImage
// todo: how to know when the window is destroyed so the resource can be released.
// todo: how to specify the swapchain spec
auto &swapchain = gfx.swapchain(wnd);
// this will return a semaphore that can be waited on GPU. see later
// submission commands for synchronization details.
std::array image_sem { swapchain.acquire_next_image() };

// prepare synchronization primitives
auto sem_image_avail = gfx.allocate_semaphore();
auto sem_render_finished = gfx.allocate_semaphore();
std::array present_wait { sem_render_finished.get() };

const auto image_info = swapchain.acquire_next_image(
sem_image_avail.get());

// The graphics service internally manages a command list allocator
// for each thread.
// todo: how to know when the thread is destroyed so we can release the resource (perhaps needs some common mechanism as the window)
std::array cmd_lists { gfx.thread_allocate_graphics_command_list() };
auto &cmd_list = cmd_lists.front();
auto cmd_list = gfx.allocate_graphics_command_list(0);

cmd_list.begin_recording();
cmd_list.image_transition(
swapchain.current_image(),
image_info.image,
// end of pipeline of the last usage of the swapchain image
GpuPipelineStage::AFTER_ALL_COMMANDS,
GpuAccessMask::NONE,
Expand All @@ -65,12 +69,12 @@ struct SystemClearSwapchainImage
GpuImageLayout::TRANSFER_DST
);
cmd_list.clear_color_image(
swapchain.current_image(),
image_info.image,
GpuImageLayout::TRANSFER_DST,
fill_color
);
cmd_list.image_transition(
swapchain.current_image(),
image_info.image,
GpuPipelineStage::TRANSFER_CLEAR,
GpuAccessMask::TRANSFER_WRITE,
GpuImageLayout::TRANSFER_DST,
Expand All @@ -82,23 +86,32 @@ struct SystemClearSwapchainImage
);
cmd_list.end_recording();

// todo semaphore pool
std::array rendering_finished_sem {
swapchain.semaphore_rendering_finished()
};
auto cmd_submission_list = gfx.create_command_buffer_list();
cmd_submission_list.add(std::move(cmd_list));

auto wait_sem = gfx.create_semaphore_info();
wait_sem.add(
std::move(sem_image_avail),
GpuPipelineStage::COLOR_ATTACHMENT_OUTPUT
);
auto signal_sem = gfx.create_semaphore_info();
signal_sem.add(
std::move(sem_render_finished),
GpuPipelineStage::AFTER_ALL_COMMANDS
);

std::array wait_stages { GpuPipelineStage::COLOR_ATTACHMENT_OUTPUT };
std::array signal_stages { GpuPipelineStage::AFTER_ALL_COMMANDS };
// todo new submission command
// https://github.com/KhronosGroup/Vulkan-Guide/blob/master/chapters/extensions/VK_KHR_synchronization2.md
gfx.submit_graphics_jobs(
cmd_lists,
image_sem,
wait_stages,
rendering_finished_sem,
signal_stages
std::move(cmd_submission_list),
std::move(wait_sem),
// render finished sem signaled here
std::move(signal_sem)
);
swapchain.present(rendering_finished_sem);

// wait on render finished sem
swapchain.present(image_info, present_wait);

gfx.reclaim_resources();
}
};
}
7 changes: 6 additions & 1 deletion DemoHelloSwapchain/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ using App = AppHost<Services, TaskList>;

int main(int argc, char *argv[])
{
std::filesystem::remove_all("demo_window_swapchain");

App app { "demo_window_swapchain" };
auto &db = app.database_world();
InputEventQueue input_event_queue;
Expand All @@ -67,7 +69,7 @@ int main(int argc, char *argv[])
.view<ComponentNativeWindow>()) == 0)
{
auto &c_wnd = gArchetypeWindow.component<ComponentNativeWindow>();
c_wnd.identifier = "usagi";
c_wnd.identifier = "main";
c_wnd.on_close = ComponentNativeWindow::NOTIFY_EXIT;
auto &c_region = gArchetypeWindow.component<ComponentRegion2D>();
c_region.size = { 1280, 720 };
Expand All @@ -78,6 +80,9 @@ int main(int argc, char *argv[])
// auto &wnd_mgr = USAGI_SERVICE(gServices, ServiceNativeWindowManager);

auto &tg = USAGI_SERVICE(app.services(), ServiceStateTransitionGraph);
auto &gfx = USAGI_SERVICE(app.services(), ServiceHardwareGraphics);
gfx.set_thread_resource_pool_size(1);

while(!tg.should_exit)
{
auto sys_access = input_event_queue.create_access<
Expand Down

0 comments on commit f5c8981

Please sign in to comment.