aboutsummaryrefslogtreecommitdiff
path: root/src/queue_context.hh
blob: f8782ded93fffd3042702de6175d5fea0c859122 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef QUEUE_STATE_HH_
#define QUEUE_STATE_HH_

#include "context.hh"
#include "device_context.hh"
#include "timestamp_pool.hh"

#include <vulkan/utility/vk_dispatch_table.h>
#include <vulkan/vulkan.hpp>

#include <chrono>
#include <deque>
#include <memory>
#include <unordered_set>

namespace low_latency {

class QueueContext final : public Context {
  public:
    DeviceContext& device_context;

    const VkQueue queue;
    const std::uint32_t queue_family_index;

    VkCommandPool command_pool;

    std::unique_ptr<TimestampPool> timestamp_pool;

  private:
    static constexpr auto MAX_TRACKED_TIMINGS = 50;

    // Potentially in flight queue submissions that come from this queue.
    struct Submission {
        const std::unordered_set<VkSemaphore> signals;
        const std::unordered_set<VkSemaphore> waits;

        const std::shared_ptr<TimestampPool::Handle> start_handle;
        const std::shared_ptr<TimestampPool::Handle> end_handle;

        std::string debug;
    };
    using submission_ptr_t = std::shared_ptr<Submission>;
    std::deque<submission_ptr_t> submissions;

    // In flight frame submissions grouped together.
    // The first element in the deque refers to the first submission that
    // contributed to that frame. The last element is the last submission before
    // present was called.
    // std::size(submissions) >= 1 btw
    struct Frame {
        submission_ptr_t prev_frame_last_submit;
        std::deque<submission_ptr_t> submissions;
    };
    std::deque<Frame> in_flight_frames;

    // Completed frames.
    struct Timing {
        DeviceContext::Clock::time_point_t::duration gputime, not_gputime;

        Frame frame;
    };
    std::deque<std::unique_ptr<Timing>> timings;

  private:
    void process_frames();

  public:
    QueueContext(DeviceContext& device_context, const VkQueue& queue,
                 const std::uint32_t& queue_family_index);
    virtual ~QueueContext();

  public:
    void
    notify_submit(const VkSubmitInfo& info,
                  const std::shared_ptr<TimestampPool::Handle> head_handle,
                  const std::shared_ptr<TimestampPool::Handle> tail_handle);

    void
    notify_submit(const VkSubmitInfo2& info,
                  const std::shared_ptr<TimestampPool::Handle> head_handle,
                  const std::shared_ptr<TimestampPool::Handle> tail_handle);

    void notify_present(const VkPresentInfoKHR& info);

  public:
    void sleep_in_present();
};

}; // namespace low_latency

#endif