Files
mirai/src/render/vulkan_context.h
2026-01-07 20:43:34 +08:00

61 lines
1.5 KiB
C++

#pragma once
#include "vulkan_device.h"
#include "vulkan_instance.h"
#include "core/object.h"
namespace mirai {
class vulkan_queue;
struct vulkan_context_init_config {
/// Vulkan 实例配置
vulkan_instance_config instance_config;
};
struct vulkan_context_config {
vk::SurfaceKHR surface;
};
class vulkan_context {
public:
static auto& get() noexcept {
static vulkan_context instance;
return instance;
}
void init(const vulkan_context_init_config& config);
void setup(const vulkan_context_config& config);
void shutdown();
std::shared_ptr<vulkan_device> create_device(const vulkan_device_config& info);
[[nodiscard]] auto get_vk_instance() const noexcept { return instance_->get_instance(); }
// 获取默认设备(第一个创建设备, 一定带有呈现队列)
[[nodiscard]] auto get_default_device() const noexcept -> std::shared_ptr<vulkan_device> {
if (devices_.empty()) {
return nullptr;
}
return devices_.front();
}
protected:
void create_default_device(const vk::SurfaceKHR& surface);
private:
vk_dynamic_loader dl;
std::shared_ptr<vulkan_instance> instance_;
std::vector<std::shared_ptr<vulkan_device>> devices_;
};
class vulkan_thread_context {
static thread_local vulkan_thread_context instance;
public:
static auto& get() noexcept {
return instance;
}
vk::CommandPool get_pool(const std::shared_ptr<vulkan_queue>& queue);
private:
std::unordered_map<u32, vk::CommandPool> pools_;
};
}