project-aster/triangle/aster.cpp

64 lines
2.2 KiB
C++

#include "aster/constants.h"
#include "aster/device.h"
#include "aster/glfw_context.h"
#include "aster/physical_device.h"
#include "aster/window.h"
constexpr QueueSupportFlags required_queue_support = QueueSupportFlags{} | QueueSupportFlagBits::eGraphics | QueueSupportFlagBits::eCompute | QueueSupportFlagBits::ePresent | QueueSupportFlagBits::eTransfer;
[[nodiscard]] bool is_suitable_device(const PhysicalDevice *_physical_device) {
const bool all_required_queues = std::ranges::any_of(_physical_device->queue_families, [](const auto &_qfp) {
return (_qfp.support & required_queue_support) == required_queue_support;
});
const bool device_type_check = _physical_device->properties.deviceType != vk::PhysicalDeviceType::eCpu;
const bool supported_present_mode = !_physical_device->present_modes.empty();
const bool supported_format = !_physical_device->surface_formats.empty();
return supported_format && supported_present_mode && device_type_check && all_required_queues;
}
PhysicalDevice find_suitable_device(const PhysicalDevices &_physical_devices) {
for (auto &_physical_device : _physical_devices) {
if (is_suitable_device(&_physical_device)) {
return _physical_device;
}
}
throw std::runtime_error("No suitable device found.");
}
QueueAllocation find_appropriate_queue_allocation(const PhysicalDevice* _physical_device) {
for (auto &_queue_info: _physical_device->queue_families) {
if ((_queue_info.support & required_queue_support) == required_queue_support) {
return {
.family = _queue_info.index,
.count = _queue_info.count,
};
}
}
throw std::runtime_error("No suitable queue family.");
}
int main(int, char **) {
GlfwContext glfw_context = {};
Context context = {"Aster", VERSION};
Window window = {"Aster1", &context, { 640, 480 }};
PhysicalDevices physical_devices = { &window, &context };
PhysicalDevice device_to_use = find_suitable_device(physical_devices);
INFO("Using {} as the primary device.", device_to_use.properties.deviceName.data());
vk::PhysicalDeviceFeatures features = {};
QueueAllocation queue_allocation = find_appropriate_queue_allocation(&device_to_use);
Device device = { &context, std::move(device_to_use), &features, {queue_allocation}, "Primary Device" };
while (window.poll()) {
}
return 0;
}