-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.cpp
More file actions
90 lines (70 loc) · 2.92 KB
/
device.cpp
File metadata and controls
90 lines (70 loc) · 2.92 KB
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
#include "device.h"
#include <iostream>
#include <stdexcept>
namespace vkInit {
bool QueueFamilyIndices::isComplete() const {
return graphicsFamily.has_value() && presentFamily.has_value();
}
QueueFamilyIndices findQueueFamilies(vk::PhysicalDevice device, bool debug) {
QueueFamilyIndices indices;
std::vector<vk::QueueFamilyProperties> queueFamilies = device.getQueueFamilyProperties();
if (debug) {
std::cout << "System supports " << queueFamilies.size() << " queue families." << std::endl;
}
for (uint32_t i = 0; i < queueFamilies.size(); i++) {
if (queueFamilies[i].queueFlags & vk::QueueFlagBits::eGraphics) {
indices.graphicsFamily = i;
}
// Present family'yi istersen burada kontrol edebilirsin.
}
return indices;
}
vk::PhysicalDevice choose_physical_device(vk::Instance& instance, bool debug) {
if (debug) {
std::cout << "Choosing physical device..." << std::endl;
}
std::vector<vk::PhysicalDevice> devices = instance.enumeratePhysicalDevices();
if (devices.empty()) {
throw std::runtime_error("No physical devices found.");
}
if (debug) {
std::cout << devices.size() << " physical device(s) found:" << std::endl;
for (const auto& dev : devices) {
auto props = dev.getProperties();
std::cout << " - " << props.deviceName << std::endl;
}
}
return devices[0]; // Basite ilk cihaz seiyoruz
}
vk::Device create_logical_device(vk::PhysicalDevice physicalDevice, bool debug) {
QueueFamilyIndices indices = findQueueFamilies(physicalDevice, debug);
if (!indices.graphicsFamily.has_value()) {
throw std::runtime_error("Failed to find graphics queue family!");
}
float priority = 1.0f;
vk::DeviceQueueCreateInfo queueCreateInfo{};
queueCreateInfo.queueFamilyIndex = indices.graphicsFamily.value();
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &priority;
std::vector<const char*> layers;
if (debug) {
layers.push_back("VK_LAYER_KHRONOS_validation");
}
vk::PhysicalDeviceFeatures features{};
vk::DeviceCreateInfo createInfo{};
createInfo.queueCreateInfoCount = 1;
createInfo.pQueueCreateInfos = &queueCreateInfo;
createInfo.enabledLayerCount = static_cast<uint32_t>(layers.size());
createInfo.ppEnabledLayerNames = layers.data();
createInfo.pEnabledFeatures = &features;
try {
return physicalDevice.createDevice(createInfo);
}
catch (const vk::SystemError& e) {
if (debug) {
std::cerr << "Device creation failed: " << e.what() << std::endl;
}
return vk::Device{};
}
}
}