我们将在绘制、渲染的时候需要用到的Vulkan对象统一封装为Vulkan上下文:
namespace ida {
struct SwapChainSupportDetails {
vk::SurfaceCapabilitiesKHR capabilities;
std::vector<vk::SurfaceFormatKHR> formats;
std::vector<vk::PresentModeKHR> presentModes;
};
struct QueueFamilyIndices final {
std::optional<std::uint32_t> graphicsIndex;
std::optional<std::uint32_t> presentIndex;
operator bool() {
return graphicsIndex.has_value() && presentIndex.has_value();
}
};
class Context final {
public:
friend class IdaWindow;
static void Init(std::vector<const char*>& extensions, GetSurfaceCallback);
static void Quit();
static Context& GetInstance();
vk::SurfaceKHR GetSurface() { return surface_; }
SwapChainSupportDetails QuerySwapChainSupport();
QueueFamilyIndices QueryQueueFamily(vk::SurfaceKHR);
vk::Format QuerySupportedFormat(const std::vector<vk::Format>&, vk::ImageTiling, vk::FormatFeatureFlags);
void CreateImageWithInfo(const vk::ImageCreateInfo&, vk::MemoryPropertyFlags, vk::Image&, vk::DeviceMemory&);
void ExecuteCommandBuffer(vk::Queue queue, std::function<void(vk::CommandBuffer&)> func);
~Context();
vk::Instance instance;
vk::PhysicalDevice phyDevice;
vk::Device device;
vk::Queue graphicsQueue;
vk::Queue presentQueue;
std::unique_ptr<IdaSwapChain> swapChain;
vk::CommandPool commandPool;
private:
const std::vector<const char*> validationLayers = {"VK_LAYER_KHRONOS_validation"};
const std::vector<const char*> deviceExtensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
static Context* instance_;
vk::SurfaceKHR surface_ = nullptr;
GetSurfaceCallback getSurfaceCb_ = nullptr;
Context(std::vector<const char*>& extensions, GetSurfaceCallback);
uint32_t FindMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
vk::Instance CreateInstance(std::vector<const char*>& extensions);
vk::PhysicalDevice PickupPhysicalDevice();
vk::Device CreateDevice(vk::SurfaceKHR);
vk::CommandPool CreateCommandPool();
// cmd helper
std::vector<vk::CommandBuffer> CreateCommandBuffer(int count = 1);
};
} // namespace ida
我们抽象成以下的上下文初始化函数,进而初始化Vulkan:
Context::Context(std::vector<const char*>& extensions, GetSurfaceCallback cb) {
getSurfaceCb_ = cb;
instance = CreateInstance(extensions);
if (!instance) {
IO::ThrowError("Failed to create instance");
}
phyDevice = PickupPhysicalDevice();
if (!phyDevice) {
IO::ThrowError("Failed to pickup physical device");
}
IO::PrintLog(LOG_LEVEL::LOG_LEVEL_INFO, "Physical device name: {}", phyDevice.getProperties().deviceName.data());
surface_ = getSurfaceCb_(instance);
device = CreateDevice(surface_);
if (!device) {
IO::ThrowError("Failed to create device");
}
auto queueInfo = QueryQueueFamily(surface_);
graphicsQueue = device.getQueue(queueInfo.graphicsIndex.value(), 0);
presentQueue = device.getQueue(queueInfo.presentIndex.value(), 0);
commandPool = CreateCommandPool();
}
可以发现顺序依次是:创建instance→挑选PhysicalDevice→创建surface→创建device。
其中surface的创建由窗口的API决定,比如GLFW、SDL和WIN32.
vulkan的instance由appInfo和createInfo决定,里面包括了Vulkan API版本的指定:
vk::Instance Context::CreateInstance(std::vector<const char*>& extensions) {
auto appInfo = vk::ApplicationInfo()
.setApiVersion(VK_VERSION_1_3);
auto createInfo = vk::InstanceCreateInfo()
.setPApplicationInfo(&appInfo)
.setPEnabledExtensionNames(extensions)
.setPEnabledLayerNames(validationLayers);
return vk::createInstance(createInfo);
}
考虑到大部分都是单一显卡,所以直接选择第一个就好,如果有多个设备需要考虑赋分机制:
vk::PhysicalDevice Context::PickupPhysicalDevice() {
auto devices = instance.enumeratePhysicalDevices();
if (devices.empty()) {
IO::ThrowError("Failed to find physical devices suitable for vulkan! Make sure you have a compatible GPU installed.");
}
return devices[0];
}