aboutsummaryrefslogtreecommitdiff
path: root/src/strategies/low_latency2/swapchain_monitor.hh
blob: 837f8e4e22d42cb1aa59a866fcf4f1293eaf1607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#ifndef SWAPCHAIN_MONITOR_HH_
#define SWAPCHAIN_MONITOR_HH_

#include "frame_span.hh"

#include <vulkan/vulkan.h>

#include <chrono>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <thread>

namespace low_latency {

class DeviceContext;

class SwapchainMonitor final {
  private:
    struct WakeupSemaphore {
        VkSemaphore timeline_semaphore{};
        std::uint64_t value{};

      public:
        void signal(const DeviceContext& device) const;
    };

    // An empty vector here represents our 'no work' state.
    std::vector<std::unique_ptr<FrameSpan>> pending_frame_spans{};

    // A pairing of semaphore -> submissions.
    // If the Submissions completes then signal the bundled semaphore.
    struct SemaphoreSpans {
        WakeupSemaphore wakeup_semaphore{};
        std::vector<std::unique_ptr<FrameSpan>> frame_spans{};
    };
    std::optional<SemaphoreSpans> semaphore_spans{};

  protected:
    const DeviceContext& device;

    std::mutex mutex{};
    std::chrono::microseconds present_delay{};
    bool was_low_latency_requested{};

    std::condition_variable_any cv{};
    std::jthread monitor_worker{};

    std::optional<std::chrono::steady_clock::time_point> last_signal_time;

    void do_monitor(const std::stop_token stoken);

  public:
    SwapchainMonitor(const DeviceContext& device);
    SwapchainMonitor(const SwapchainMonitor&) = delete;
    SwapchainMonitor(SwapchainMonitor&&) = delete;
    SwapchainMonitor operator=(const SwapchainMonitor&) = delete;
    SwapchainMonitor operator=(SwapchainMonitor&&) = delete;
    ~SwapchainMonitor();

  public:
    void update_params(const bool was_low_latency_requested,
                       const std::chrono::microseconds delay);

    void notify_semaphore(const VkSemaphore& timeline_semaphore,
                          const std::uint64_t& value);

    void attach_work(std::vector<std::unique_ptr<FrameSpan>> submissions);
};

} // namespace low_latency

#endif