#ifndef LAYER_CONTEXT_HH_ #define LAYER_CONTEXT_HH_ #include #include #include #include "context.hh" #include "device_context.hh" #include "instance_context.hh" #include "physical_device_context.hh" #include "queue_context.hh" // The purpose of this file is to provide a definition for the highest level // entry point struct of our vulkan state. namespace low_latency { // All these templates do is make it so we can go from some DispatchableType // to their respective context's with nice syntax. This lets us write something // like this for all DispatchableTypes: // // const auto device_context = get_context(some_vk_device); // ^ It was automatically deduced as DeviceContext, wow! template concept DispatchableType = std::same_as, VkInstance> || std::same_as, VkPhysicalDevice> || std::same_as, VkDevice> || std::same_as, VkQueue>; template struct context_for_t; template <> struct context_for_t { using context = InstanceContext; }; template <> struct context_for_t { using context = PhysicalDeviceContext; }; template <> struct context_for_t { using context = DeviceContext; }; template <> struct context_for_t { using context = QueueContext; }; template using dispatch_context_t = typename context_for_t::context; struct LayerContext final : public Context { private: // If this is not null and set to exactly "1", then we should sleep after // present. static constexpr auto SLEEP_AFTER_PRESENT_ENV = "LOW_LATENCY_LAYER_SLEEP_AFTER_PRESENT"; public: std::mutex mutex; std::unordered_map> contexts; bool is_antilag_1_enabled = false; public: LayerContext(); virtual ~LayerContext(); public: template static void* get_key(const DT& dt) { return reinterpret_cast(dt); } template std::shared_ptr> get_context(const DT& dt) const { const auto key = get_key(dt); const auto lock = std::scoped_lock(this->mutex); const auto it = this->contexts.find(key); assert(it != std::end(this->contexts)); using context_t = dispatch_context_t
; const auto ptr = std::dynamic_pointer_cast(it->second); assert(ptr); return ptr; } }; }; // namespace low_latency #endif