Handle command buffers and fences correcctly for imgui
This commit is contained in:
@@ -170,12 +170,6 @@ imgui_context::imgui_frame& imgui_context::get_frame(vk::Image destination)
|
|||||||
.layers = 1,
|
.layers = 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
frame.command_buffer = std::move(device.allocateCommandBuffers({
|
|
||||||
.commandPool = *command_pool,
|
|
||||||
.commandBufferCount = 1,
|
|
||||||
})[0]);
|
|
||||||
|
|
||||||
frame.fence = device.createFence(vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
|
|
||||||
|
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
@@ -188,7 +182,7 @@ static const std::array pool_sizes =
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers_, vk::Extent2D size, float resolution, vk::Format format, int image_count) :
|
imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers_, vk::Extent2D size, float resolution, vk::Format format, int frames_in_flight) :
|
||||||
device(device),
|
device(device),
|
||||||
queue_family_index(queue_family_index),
|
queue_family_index(queue_family_index),
|
||||||
queue(queue),
|
queue(queue),
|
||||||
@@ -204,6 +198,7 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
|||||||
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
|
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
|
||||||
.queueFamilyIndex = queue_family_index,
|
.queueFamilyIndex = queue_family_index,
|
||||||
}),
|
}),
|
||||||
|
command_buffers(frames_in_flight),
|
||||||
size(size),
|
size(size),
|
||||||
format(format),
|
format(format),
|
||||||
scale(size.width / resolution, size.height / resolution),
|
scale(size.width / resolution, size.height / resolution),
|
||||||
@@ -217,6 +212,16 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
|||||||
|
|
||||||
io.IniFilename = nullptr;
|
io.IniFilename = nullptr;
|
||||||
|
|
||||||
|
for(command_buffer& cb: command_buffers)
|
||||||
|
{
|
||||||
|
cb.command_buffer = std::move(device.allocateCommandBuffers({
|
||||||
|
.commandPool = *command_pool,
|
||||||
|
.commandBufferCount = 1,
|
||||||
|
})[0]);
|
||||||
|
|
||||||
|
cb.fence = device.createFence(vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||||
|
}
|
||||||
|
|
||||||
ImGui_ImplVulkan_InitInfo init_info = {
|
ImGui_ImplVulkan_InitInfo init_info = {
|
||||||
.Instance = *application::get_vulkan_instance(),
|
.Instance = *application::get_vulkan_instance(),
|
||||||
.PhysicalDevice = *application::get_physical_device(),
|
.PhysicalDevice = *application::get_physical_device(),
|
||||||
@@ -227,7 +232,7 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
|||||||
.DescriptorPool = *descriptor_pool,
|
.DescriptorPool = *descriptor_pool,
|
||||||
.Subpass = 0,
|
.Subpass = 0,
|
||||||
.MinImageCount = 2,
|
.MinImageCount = 2,
|
||||||
.ImageCount = (uint32_t)image_count, // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData
|
.ImageCount = (uint32_t)frames_in_flight, // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData
|
||||||
.MSAASamples = VK_SAMPLE_COUNT_1_BIT,
|
.MSAASamples = VK_SAMPLE_COUNT_1_BIT,
|
||||||
.Allocator = nullptr,
|
.Allocator = nullptr,
|
||||||
.CheckVkResultFn = check_vk_result,
|
.CheckVkResultFn = check_vk_result,
|
||||||
@@ -421,11 +426,14 @@ void imgui_context::render(vk::Image destination)
|
|||||||
if (is_minimized)
|
if (is_minimized)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto& f = get_frame(destination);
|
current_command_buffer = (current_command_buffer + 1) % command_buffers.size();
|
||||||
auto& cb = f.command_buffer;
|
|
||||||
|
|
||||||
device.waitForFences(*f.fence, true, 1'000'000'000); // TODO check timeout
|
auto& f = get_frame(destination);
|
||||||
device.resetFences(*f.fence);
|
auto& cb = get_command_buffer().command_buffer;
|
||||||
|
auto& fence = get_command_buffer().fence;
|
||||||
|
|
||||||
|
device.waitForFences(*fence, true, 1'000'000'000); // TODO check timeout
|
||||||
|
device.resetFences(*fence);
|
||||||
|
|
||||||
cb.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
cb.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
||||||
|
|
||||||
@@ -450,7 +458,7 @@ void imgui_context::render(vk::Image destination)
|
|||||||
queue.submit(vk::SubmitInfo{
|
queue.submit(vk::SubmitInfo{
|
||||||
.commandBufferCount = 1,
|
.commandBufferCount = 1,
|
||||||
.pCommandBuffers = &*cb,
|
.pCommandBuffers = &*cb,
|
||||||
}, *f.fence);
|
}, *fence);
|
||||||
|
|
||||||
// Use the aliasing constructor
|
// Use the aliasing constructor
|
||||||
// return std::shared_ptr<vk::raii::ImageView>{viewport, &vp.frames[vp.frameindex].image_view_texture};
|
// return std::shared_ptr<vk::raii::ImageView>{viewport, &vp.frames[vp.frameindex].image_view_texture};
|
||||||
@@ -458,18 +466,18 @@ void imgui_context::render(vk::Image destination)
|
|||||||
|
|
||||||
imgui_context::~imgui_context()
|
imgui_context::~imgui_context()
|
||||||
{
|
{
|
||||||
// Release the command buffers, they will be destroyed with the command pool
|
std::vector<vk::Fence> fences;
|
||||||
for(auto& f: frames)
|
|
||||||
|
// Release the command buffers without freing them, they will be destroyed with the command pool
|
||||||
|
for(auto& f: command_buffers)
|
||||||
|
{
|
||||||
f.command_buffer.release();
|
f.command_buffer.release();
|
||||||
|
fences.push_back(*f.fence);
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for fences before ImGui_ImplVulkan_Shutdown is called
|
// Wait for fences before ImGui_ImplVulkan_Shutdown is called
|
||||||
std::vector<vk::Fence> fences;
|
|
||||||
for(auto& f: frames)
|
|
||||||
fences.push_back(*f.fence);
|
|
||||||
|
|
||||||
device.waitForFences(fences, true, 1'000'000'000);
|
device.waitForFences(fences, true, 1'000'000'000);
|
||||||
|
|
||||||
|
|
||||||
ImGui_ImplVulkan_Shutdown();
|
ImGui_ImplVulkan_Shutdown();
|
||||||
ImGui::DestroyContext(context);
|
ImGui::DestroyContext(context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,16 +29,21 @@
|
|||||||
|
|
||||||
class imgui_context
|
class imgui_context
|
||||||
{
|
{
|
||||||
|
struct command_buffer
|
||||||
|
{
|
||||||
|
vk::raii::CommandBuffer command_buffer = nullptr;
|
||||||
|
vk::raii::Fence fence = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
struct imgui_frame
|
struct imgui_frame
|
||||||
{
|
{
|
||||||
vk::Image destination;
|
vk::Image destination;
|
||||||
vk::raii::ImageView image_view_framebuffer = nullptr;
|
vk::raii::ImageView image_view_framebuffer = nullptr;
|
||||||
vk::raii::Framebuffer framebuffer = nullptr;
|
vk::raii::Framebuffer framebuffer = nullptr;
|
||||||
vk::raii::CommandBuffer command_buffer = nullptr;
|
|
||||||
vk::raii::Fence fence = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct controller
|
struct controller
|
||||||
{
|
{
|
||||||
XrSpace aim;
|
XrSpace aim;
|
||||||
@@ -77,6 +82,13 @@ private:
|
|||||||
std::vector<imgui_frame> frames;
|
std::vector<imgui_frame> frames;
|
||||||
imgui_frame& get_frame(vk::Image destination);
|
imgui_frame& get_frame(vk::Image destination);
|
||||||
|
|
||||||
|
std::vector<command_buffer> command_buffers;
|
||||||
|
size_t current_command_buffer = 0;
|
||||||
|
command_buffer& get_command_buffer()
|
||||||
|
{
|
||||||
|
return command_buffers[current_command_buffer];
|
||||||
|
}
|
||||||
|
|
||||||
vk::Extent2D size;
|
vk::Extent2D size;
|
||||||
vk::Format format;
|
vk::Format format;
|
||||||
vk::ClearValue clear_value = {vk::ClearColorValue{0,0,0,0}};
|
vk::ClearValue clear_value = {vk::ClearColorValue{0,0,0,0}};
|
||||||
@@ -98,7 +110,7 @@ private:
|
|||||||
std::optional<ImVec2> ray_plane_intersection(const imgui_context::controller_state& in);
|
std::optional<ImVec2> ray_plane_intersection(const imgui_context::controller_state& in);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers, vk::Extent2D size, float resolution, vk::Format format, int image_count);
|
imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers, vk::Extent2D size, float resolution, vk::Format format, int frames_in_flight = 3);
|
||||||
~imgui_context();
|
~imgui_context();
|
||||||
|
|
||||||
void set_position(glm::vec3 position, glm::quat orientation)
|
void set_position(glm::vec3 position, glm::quat orientation)
|
||||||
|
|||||||
Reference in New Issue
Block a user