project-aster/aster/physical_device.cpp

100 lines
3.1 KiB
C++

// =============================================
// Aster: physical_device.cpp
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#include "physical_device.h"
[[nodiscard]] std::vector<vk::SurfaceFormatKHR> get_surface_formats(const Window *_window, const vk::PhysicalDevice *_physical_device) {
try {
return _physical_device->getSurfaceFormatsKHR(_window->surface);
} catch (const std::exception &_err) {
ERROR("Could not get surface formats. Cause: {}", _err.what());
throw;
}
}
[[nodiscard]] std::vector<vk::PresentModeKHR> get_present_modes(const Window *_window, const vk::PhysicalDevice *_physical_device) {
try {
return _physical_device->getSurfacePresentModesKHR(_window->surface);
} catch (const std::exception &_err) {
ERROR("Could not get present modes. Cause: {}", _err.what());
throw;
}
}
[[nodiscard]] bool get_present_support(const u32 _family_index, const Window *_window, const vk::PhysicalDevice *_device) {
try {
return _device->getSurfaceSupportKHR(_family_index, _window->surface);
} catch (const std::exception &_err) {
ERROR("Could not get queue family surface support. Cause: {}", _err.what());
throw;
}
}
[[nodiscard]] std::vector<QueueFamilyInfo> get_queue_families(const Window *_window, const vk::PhysicalDevice *_device) {
auto queue_family_props = _device->getQueueFamilyProperties();
std::vector<QueueFamilyInfo> queue_family_infos;
queue_family_infos.reserve(queue_family_props.size());
u32 family_index = 0;
for (auto qfp : queue_family_props) {
QueueSupportFlags support = {};
if (qfp.queueFlags | vk::QueueFlagBits::eGraphics) {
support |= QueueSupportFlagBits::eGraphics;
}
if (qfp.queueFlags | vk::QueueFlagBits::eTransfer) {
support |= QueueSupportFlagBits::eTransfer;
}
if (qfp.queueFlags | vk::QueueFlagBits::eCompute) {
support |= QueueSupportFlagBits::eCompute;
}
if (get_present_support(family_index, _window, _device)) {
support |= QueueSupportFlagBits::ePresent;
}
queue_family_infos.push_back({
.index = family_index,
.count = qfp.queueCount,
.support = support,
});
family_index++;
}
return queue_family_infos;
}
PhysicalDevice::PhysicalDevice(const Window *_window, vk::PhysicalDevice _physical_device) {
_physical_device.getProperties(&properties);
_physical_device.getFeatures(&features);
surface_formats = get_surface_formats(_window, &_physical_device);
present_modes = get_present_modes(_window, &_physical_device);
queue_families = get_queue_families(_window, &_physical_device);
device = _physical_device;
}
std::vector<vk::PhysicalDevice> enumerate_physical_devices(const Context *_context) {
try {
return _context->instance.enumeratePhysicalDevices();
} catch (const std::exception &_err) {
ERROR("Could not fetch vulkan devices. Cause: {}", _err.what());
throw;
}
}
PhysicalDevices::PhysicalDevices(const Window *_window, const Context *_context) {
auto physical_devices = enumerate_physical_devices(_context);
this->reserve(physical_devices.size());
for (auto physical_device : physical_devices) {
this->emplace_back(_window, physical_device);
}
}