-
Notifications
You must be signed in to change notification settings - Fork 1
/
vkr_physical_devices.h
95 lines (84 loc) · 3.19 KB
/
vkr_physical_devices.h
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <optional>
#include <vector>
#include "vkr_util.h"
namespace vkr {
class PhysicalDevices {
public:
VkPhysicalDeviceMemoryProperties mem_properties_{};
VkPhysicalDeviceProperties properties_{};
VkPhysicalDeviceLimits limits_{};
VkPhysicalDevice device_{};
VkInstance instance_{};
VkPhysicalDevice Device() const { return device_; }
// Visitor returns priorities.
template <typename Visitor>
bool Choose(const Visitor& score_visitor, bool verbose = true) {
auto devices = EnumeratePhysicalDevices(instance_);
VkPhysicalDevice best_device = {};
size_t device_idx = 0;
int best_score = -1;
if (verbose) {
std::cout << "Found " << devices.size() << " physical devices" << std::endl;
}
for (auto& dev : devices) {
VkPhysicalDeviceProperties properties;
VkPhysicalDeviceFeatures features;
vkGetPhysicalDeviceProperties(dev, &properties);
vkGetPhysicalDeviceFeatures(dev, &features);
const auto score = score_visitor(properties, features);
if (verbose) {
std::cout << "Device " << properties.deviceName << " type=" << properties.deviceType << " score=" << score << std::endl;
}
if (score > best_score) {
best_score = score;
device_ = dev;
}
}
if (best_score < 0) {
return false;
}
vkGetPhysicalDeviceMemoryProperties(device_, &mem_properties_);
vkGetPhysicalDeviceProperties(device_, &properties_);
if (verbose) {
for (size_t i = 0; i < mem_properties_.memoryHeapCount; ++i) {
std::cout
<< "Memory heap " << i
<< " size=" << mem_properties_.memoryHeaps[i].size
<< " flags=" << mem_properties_.memoryHeaps[i].flags << std::endl;
}
}
return true;
}
VkFormat FindDepthFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, bool stencil) {
VkFormatFeatureFlags features = stencil ? VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT : 0;
for (auto fmt : candidates) {
VkFormatProperties props;
vkGetPhysicalDeviceFormatProperties(device_, fmt, &props);
if (tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & features) == features) {
return fmt;
} else if (tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & features) == features) {
return fmt;
}
}
return VK_FORMAT_UNDEFINED;
}
std::optional<size_t> GetMemoryTypeIdx(size_t supported, size_t requirements) const {
for (size_t i = 0; i < mem_properties_.memoryTypeCount; ++i) {
if ((supported & (1u << i)) && (mem_properties_.memoryTypes[i].propertyFlags & requirements) == requirements) {
return i;
}
}
return {};
}
std::vector<VkQueueFamilyProperties> GetPhysicalDeviceQueueFamilyProperties() {
uint32_t count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device_, &count, nullptr);
std::vector<VkQueueFamilyProperties> ret(count);
vkGetPhysicalDeviceQueueFamilyProperties(device_, &count, ret.data());
return ret;
}
const VkPhysicalDeviceMemoryProperties& MemProperties() const { return mem_properties_; }
const VkPhysicalDeviceProperties& Properties() const { return properties_; }
};
}