From 56706244924987852e5ec610941bce8258ae647b Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Mon, 23 Feb 2026 15:47:39 +1100 Subject: Implement AntiLag2 --- src/device_context.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) (limited to 'src/device_context.cc') diff --git a/src/device_context.cc b/src/device_context.cc index a8e0347..2214b71 100644 --- a/src/device_context.cc +++ b/src/device_context.cc @@ -1,7 +1,9 @@ #include "device_context.hh" +#include "queue_context.hh" #include #include +#include namespace low_latency { @@ -13,6 +15,7 @@ DeviceContext::DeviceContext(InstanceContext& parent_instance, device(device), vtable(std::move(vtable)), clock(*this) {} DeviceContext::~DeviceContext() { + this->present_queue.reset(); // We will let the destructor handle clearing here, but they should be // unique by now (ie, removed from the layer's context map). for (const auto& [queue, queue_context] : this->queues) { @@ -24,9 +27,11 @@ void DeviceContext::notify_acquire(const VkSwapchainKHR& swapchain, const std::uint32_t& image_index, const VkSemaphore& signal_semaphore) { + /* std::cerr << "notify acquire for swapchain: " << swapchain << " : " << image_index << '\n'; std::cerr << " signal semaphore: " << signal_semaphore << '\n'; + */ const auto it = this->swapchain_signals.try_emplace(swapchain).first; @@ -63,7 +68,7 @@ DeviceContext::Clock::time_point_t DeviceContext::Clock::ticks_to_time(const std::uint64_t& ticks) const { const auto& pd = device.physical_device.properties; const auto ns_tick = static_cast(pd->limits.timestampPeriod); - + const auto diff = [&]() -> auto { auto a = this->device_ticks; auto b = ticks; @@ -76,7 +81,7 @@ DeviceContext::Clock::ticks_to_time(const std::uint64_t& ticks) const { const auto signed_abs_diff = static_cast(abs_diff); return is_negative ? -signed_abs_diff : signed_abs_diff; }(); - + // This will have issues because std::chrono::steady_clock::now(), which // we use for cpu time, may not be on the same time domain what was returned // by GetCalibratedTimestamps. It would be more robust to use the posix @@ -87,4 +92,62 @@ DeviceContext::Clock::ticks_to_time(const std::uint64_t& ticks) const { return time_point_t{delta}; } +const auto debug_log_time2 = [](auto& stream, const auto& diff) { + using namespace std::chrono; + const auto ms = duration_cast(diff); + const auto us = duration_cast(diff - ms); + const auto ns = duration_cast(diff - ms - us); + stream << ms << " " << us << " " << ns << '\n'; +}; + +const auto debug_log_time = [](const auto& diff) { + debug_log_time2(std::cerr, diff); +}; + +void DeviceContext::sleep_in_input() { + // Present hasn't happened yet, we don't know what queue to attack. + if (!this->present_queue) { + return; + } + + const auto before = std::chrono::steady_clock::now(); + // If we're here, that means that there might be an outstanding frame that's + // sitting on our present_queue which hasn't yet completed, so we need to + // stall until it's finished. + const auto& frames = this->present_queue->in_flight_frames; + if (std::size(frames)) { + frames.back().submissions.back()->end_handle->get_time_spinlock(); + } + const auto after = std::chrono::steady_clock::now(); + //debug_log_time(after - before); + + // FIXME this should take into account 'cpu_time', which we currently do not... + // idk if it matters. +} + +void DeviceContext::notify_antilag_update(const VkAntiLagDataAMD& data) { + this->antilag_mode = data.mode; + this->antilag_fps = data.maxFPS; + + // This might not be provided (probably just to set some settings). + if (!data.pPresentationInfo) { + return; + } + + const auto& presentation_info = *data.pPresentationInfo; + // Only care about the input stage for now. + if (presentation_info.stage != VK_ANTI_LAG_STAGE_INPUT_AMD) { + return; + } + + if (this->antilag_mode == VK_ANTI_LAG_MODE_ON_AMD) { + this->sleep_in_input(); + } +} + +void DeviceContext::notify_queue_present(const QueueContext& queue) { + assert(this->queues.contains(queue.queue)); + this->present_queue = this->queues[queue.queue]; +} + } // namespace low_latency \ No newline at end of file -- cgit v1.2.3