aboutsummaryrefslogtreecommitdiff
path: root/src/physical_device_context.cc
blob: 9c4ad8e46d6d0d54da9dcaef610fa4da96401966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "physical_device_context.hh"

#include <vulkan/vulkan_core.h>

#include <ranges>
#include <string_view>
#include <unordered_set>
#include <vector>

namespace low_latency {

PhysicalDeviceContext::PhysicalDeviceContext(
    InstanceContext& instance_context, const VkPhysicalDevice& physical_device)
    : instance(instance_context), physical_device(physical_device) {

    const auto& vtable = instance_context.vtable;

    this->properties = [&]() {
        auto props = VkPhysicalDeviceProperties{};
        vtable.GetPhysicalDeviceProperties(physical_device, &props);
        return std::make_unique<VkPhysicalDeviceProperties>(std::move(props));
    }();

    this->queue_properties = [&]() {
        auto count = std::uint32_t{};
        vtable.GetPhysicalDeviceQueueFamilyProperties2(physical_device, &count,
                                                       nullptr);

        using qp_t = PhysicalDeviceContext::queue_properties_t;
        auto result = qp_t(
            count, VkQueueFamilyProperties2{
                       .sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2});
        vtable.GetPhysicalDeviceQueueFamilyProperties2(physical_device, &count,
                                                       std::data(result));

        return std::make_unique<qp_t>(std::move(result));
    }();

    this->supports_required_extensions = [&]() {
        auto count = std::uint32_t{};
        THROW_NON_VKSUCCESS(vtable.EnumerateDeviceExtensionProperties(
            physical_device, nullptr, &count, nullptr));

        auto supported_extensions = std::vector<VkExtensionProperties>(count);
        THROW_NON_VKSUCCESS(vtable.EnumerateDeviceExtensionProperties(
            physical_device, nullptr, &count,
            std::data(supported_extensions)));

        const auto supported =
            supported_extensions |
            std::views::transform(
                [](const auto& supported) { return supported.extensionName; }) |
            std::ranges::to<std::unordered_set<std::string_view>>();

        return std::ranges::all_of(
            this->required_extensions, [&](const auto& required_extension) {
                return supported.contains(required_extension);
            });
    }();
}

PhysicalDeviceContext::~PhysicalDeviceContext() {}

} // namespace low_latency