Skip to content

Commit 37f03e9

Browse files
committed
lock imgui submit
1 parent d44a9c4 commit 37f03e9

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

rvk/device.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,19 @@ u32 Device::queue_index(Queue_Family family) {
606606
}
607607
}
608608

609+
void Device::lock_queues() {
610+
mutex.lock();
611+
}
612+
613+
void Device::unlock_queues() {
614+
mutex.unlock();
615+
}
616+
617+
VkResult Device::present(const VkPresentInfoKHR& info) {
618+
Thread::Lock lock(mutex);
619+
return vkQueuePresentKHR(present_q, &info);
620+
}
621+
609622
void Device::submit(Commands& cmds, u32 index) {
610623

611624
VkCommandBufferSubmitInfo cmd_info = {};

rvk/device.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ struct Device {
9393
Device& operator=(Device&&) = delete;
9494

9595
void imgui();
96+
VkResult present(const VkPresentInfoKHR& info);
9697

9798
u32 heap_index(Heap heap);
9899
u64 heap_size(Heap heap);
@@ -103,7 +104,6 @@ struct Device {
103104

104105
u32 queue_index(Queue_Family family);
105106
u64 queue_count(Queue_Family family);
106-
VkQueue queue(Queue_Family family, u32 index = 0);
107107

108108
void submit(Commands& cmds, u32 index);
109109
void submit(Commands& cmds, u32 index, Fence& fence);
@@ -122,6 +122,13 @@ struct Device {
122122
explicit Device(Arc<Physical_Device, Alloc> physical_device, VkSurfaceKHR surface,
123123
bool ray_tracing);
124124
friend struct Arc<Device, Alloc>;
125+
friend struct Vk;
126+
friend struct Compositor;
127+
128+
// TODO(max): lock per queue
129+
void lock_queues();
130+
void unlock_queues();
131+
VkQueue queue(Queue_Family family, u32 index = 0);
125132

126133
Arc<Physical_Device, Alloc> physical_device;
127134
Vec<String<Alloc>, Alloc> enabled_extensions;

rvk/rvk.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Vk::Vk(Config config) {
198198
instance->surface(), config.frames_in_flight);
199199
});
200200

201-
compositor = Arc<Compositor, Alloc>::make(device, descriptor_pool, swapchain.dup());
201+
compositor = Arc<Compositor, Alloc>::make(device.dup(), swapchain.dup(), descriptor_pool);
202202

203203
create_imgui();
204204
}
@@ -383,7 +383,7 @@ void Vk::end_frame(Image_View& output) {
383383
present_info.pImageIndices = &state.swapchain_index;
384384

385385
// Submit presentation command
386-
VkResult result = vkQueuePresentKHR(device->queue(Queue_Family::present), &present_info);
386+
VkResult result = device->present(present_info);
387387
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) {
388388
recreate_swapchain();
389389
} else if(result != VK_SUCCESS) {
@@ -414,7 +414,7 @@ void Vk::recreate_swapchain() {
414414
swapchain = Arc<Swapchain, Alloc>::make(cmds, physical_device, device.dup(),
415415
instance->surface(), state.frames_in_flight);
416416
});
417-
compositor = Arc<Compositor, Alloc>::make(device, descriptor_pool, swapchain.dup());
417+
compositor = Arc<Compositor, Alloc>::make(device.dup(), swapchain.dup(), descriptor_pool);
418418

419419
create_imgui();
420420

rvk/swapchain.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ VkExtent2D Swapchain::choose_extent(VkSurfaceCapabilitiesKHR capabilities) {
190190
return ext;
191191
}
192192

193-
Compositor::Compositor(Arc<Device, Alloc>& device, Arc<Descriptor_Pool, Alloc>& pool,
194-
Arc<Swapchain, Alloc> S)
195-
: swapchain(move(S)), v(compositor_v(device.dup())), f(compositor_f(device.dup())),
196-
ds_layout(device.dup(), compositor_ds_layout()),
193+
Compositor::Compositor(Arc<Device, Alloc> D, Arc<Swapchain, Alloc> S,
194+
Arc<Descriptor_Pool, Alloc>& pool)
195+
: swapchain(move(S)), device(move(D)), v(compositor_v(device.dup())),
196+
f(compositor_f(device.dup())), ds_layout(device.dup(), compositor_ds_layout()),
197197
ds(pool->make(ds_layout, swapchain->frame_count())),
198198
sampler(device.dup(), VK_FILTER_NEAREST, VK_FILTER_NEAREST),
199199
pipeline(Pipeline{device.dup(), compositor_pipeline_info(swapchain, ds_layout, v, f)}) {
@@ -243,7 +243,11 @@ void Compositor::render(Commands& cmds, u64 frame_index, u64 slot_index, bool ha
243243
vkCmdDraw(cmds, 4, 1, 0, 0);
244244
if(has_imgui) {
245245
ImGui::Render();
246-
if(auto draw = ImGui::GetDrawData()) ImGui_ImplVulkan_RenderDrawData(draw, cmds);
246+
if(auto draw = ImGui::GetDrawData()) {
247+
device->lock_queues();
248+
ImGui_ImplVulkan_RenderDrawData(draw, cmds);
249+
device->unlock_queues();
250+
}
247251
}
248252
vkCmdEndRendering(cmds);
249253
}

rvk/swapchain.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ using namespace rpp;
1616

1717
struct Compositor {
1818

19-
explicit Compositor(Arc<Device, Alloc>& device, Arc<Descriptor_Pool, Alloc>& pool,
20-
Arc<Swapchain, Alloc> swapchain);
19+
explicit Compositor(Arc<Device, Alloc> device, Arc<Swapchain, Alloc> swapchain,
20+
Arc<Descriptor_Pool, Alloc>& pool);
2121
~Compositor();
2222

2323
Compositor(const Compositor&) = delete;
@@ -28,6 +28,7 @@ struct Compositor {
2828
void render(Commands& cmds, u64 frame_index, u64 slot_index, bool has_imgui, Image_View& input);
2929

3030
private:
31+
Arc<Device, Alloc> device;
3132
Arc<Swapchain, Alloc> swapchain;
3233

3334
Shader v, f;

0 commit comments

Comments
 (0)