Skip to content

Commit

Permalink
qt,gtk/vulkan: Add VK_EXT_present_wait support again.
Browse files Browse the repository at this point in the history
  • Loading branch information
bearoso committed Jan 24, 2025
1 parent 191cdf4 commit 6637383
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
25 changes: 25 additions & 0 deletions common/video/vulkan/vulkan_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ bool Context::init_device()
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
};

std::vector<const char *> present_wait_extensions =
{
VK_KHR_PRESENT_ID_EXTENSION_NAME,
VK_KHR_PRESENT_WAIT_EXTENSION_NAME
};

auto device_list = instance->enumeratePhysicalDevices().value;
bool device_chosen = false;
physical_device = vk::PhysicalDevice();
Expand Down Expand Up @@ -263,6 +269,17 @@ bool Context::init_device()
if (!device_chosen)
return false;

if (check_extensions(present_wait_extensions, physical_device))
{
for (auto &ext : present_wait_extensions)
required_extensions.push_back(ext);
have_present_wait = true;
}
else
{
have_present_wait = false;
}

if (auto index = find_graphics_queue(physical_device))
graphics_queue_family_index = *index;
else
Expand All @@ -272,6 +289,14 @@ bool Context::init_device()
vk::DeviceQueueCreateInfo dqci({}, graphics_queue_family_index, priorities);
vk::DeviceCreateInfo dci({}, dqci, {}, required_extensions);

vk::PhysicalDevicePresentWaitFeaturesKHR physical_device_present_wait_feature(true);
vk::PhysicalDevicePresentIdFeaturesKHR physical_device_present_id_feature(true);
if (have_present_wait)
{
dci.setPNext(&physical_device_present_wait_feature);
physical_device_present_wait_feature.setPNext(&physical_device_present_id_feature);
}

device = physical_device.createDevice(dci).value;
queue = device.getQueue(graphics_queue_family_index, 0);

Expand Down
1 change: 1 addition & 0 deletions common/video/vulkan/vulkan_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Context
vk::PhysicalDeviceProperties physical_device_props;
vk::UniqueSurfaceKHR surface;
std::string platform_name;
bool have_present_wait;

private:
bool init_vma();
Expand Down
16 changes: 16 additions & 0 deletions common/video/vulkan/vulkan_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ bool Swapchain::swap()
.setSwapchains(swapchain_object.get())
.setImageIndices(current_swapchain_image);

vk::PresentIdKHR present_id;
if (context.have_present_wait)
{
presentation_id++;
present_id.setPresentIds(presentation_id);
present_info.setPNext(&present_id);
}

vk::Result result = queue.presentKHR(present_info);
if (result == vk::Result::eErrorOutOfDateKHR)
{
Expand Down Expand Up @@ -430,4 +438,12 @@ vk::RenderPass &Swapchain::get_render_pass()
return render_pass.get();
}

void Swapchain::present_wait()
{
if (context.have_present_wait && context.platform_name != "wayland")
{
device.waitForPresentKHR(swapchain_object.get(), presentation_id, 16666666);
}
}

} // namespace Vulkan
1 change: 1 addition & 0 deletions common/video/vulkan/vulkan_swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Swapchain
bool end_frame();
void end_frame_without_swap();
bool swap();
void present_wait();
void set_vsync(bool on);
void on_render_pass_end(std::function<void()> function);
int get_num_frames() { return num_swapchain_images; }
Expand Down
3 changes: 3 additions & 0 deletions gtk/src/gtk_display_driver_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ void S9xVulkanDisplayDriver::update(uint16_t *buffer, int width, int height, int
context->swapchain->swap();

if (gui_config->reduce_input_lag)
{
context->wait_idle();
context->swapchain->present_wait();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions qt/src/EmuCanvasVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ void EmuCanvasVulkan::draw()
if (config->reduce_input_lag)
{
context->wait_idle();
context->swapchain->present_wait();
}
}
}
Expand Down

0 comments on commit 6637383

Please sign in to comment.