-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.py
177 lines (108 loc) · 4.26 KB
/
device.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from config import *
import logging
import queue_families
"""
Vulkan separates the concept of physical and logical devices.
A physical device usually represents a single complete implementation of Vulkan
(excluding instance-level functionality) available to the host,
of which there are a finite number.
A logical device represents an instance of that implementation
with its own state and resources independent of other logical devices.
"""
def choose_physical_device(instance, debug):
if debug:
print("Choosing physical device")
# vkEnumeratePhysicalDevices(instance) -> List(vkPhysicalDevice)
availableDevices = vkEnumeratePhysicalDevices(instance)
if debug:
print(f"{len(availableDevices)} physical devices available")
# check if a suitable device can be found
for device in availableDevices:
if debug:
log_device_properties(device)
if is_suitable(device, debug):
return device
return None
def log_device_properties(device):
properties = vkGetPhysicalDeviceProperties(device)
print(f"Device name: {properties.deviceName}")
print("Device type: ", end="")
if properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU:
print("CPU")
elif properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
print("Discrete GPU")
elif properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
print("Integrated GPU")
elif properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
print("Virtual GPU")
else:
print("Other")
def is_suitable(device, debug):
if debug:
print("Checking if device is suitable")
requestedExtensions = [
VK_KHR_SWAPCHAIN_EXTENSION_NAME
]
if debug:
print("Requesting device extensions:")
for v in requestedExtensions:
print(f"\t\"{v}\"")
if check_device_extension_support(device, requestedExtensions, debug):
if debug:
print("Device can support the extensions!")
return True
if debug:
print("Device doesn't support required extensions.")
return False
def check_device_extension_support(device, requestedExtensions, debug):
supportedExtensions = [v.extensionName for v in vkEnumerateDeviceExtensionProperties(device, None)]
if debug:
print("Device supports following extensions:")
for v in supportedExtensions:
print(f"\t\"{v}\"")
for v in requestedExtensions:
if v not in supportedExtensions:
return False
return True
def create_logical_device(physicalDevice, instance, surface, debug):
indices = queue_families.find_queue_families(physicalDevice, instance, surface, debug)
uniqueIndices = [indices.graphicsFamily]
if indices.graphicsFamily != indices.presentFamily:
uniqueIndices.append(indices.presentFamily)
queueCreateInfo = []
for queueFamilyIndex in uniqueIndices:
queueCreateInfo.append(VkDeviceQueueCreateInfo(
queueFamilyIndex=queueFamilyIndex,
queueCount=1,
pQueuePriorities=[1.0]
))
deviceFeatures = VkPhysicalDeviceFeatures()
enabledLayers = []
if debug:
enabledLayers.append("VK_LAYER_KHRONOS_validation")
deviceExtensions = [
VK_KHR_SWAPCHAIN_EXTENSION_NAME
]
createInfo = VkDeviceCreateInfo(
queueCreateInfoCount=len(queueCreateInfo),
pQueueCreateInfos=queueCreateInfo,
enabledExtensionCount=len(deviceExtensions),
ppEnabledExtensionNames=deviceExtensions,
pEnabledFeatures=[deviceFeatures],
enabledLayerCount=len(enabledLayers),
ppEnabledLayerNames=enabledLayers
)
return vkCreateDevice(physicalDevice=physicalDevice, pCreateInfo=[createInfo], pAllocator=None)
def get_queues(physicalDevice, device, instance, surface, debug):
indices = queue_families.find_queue_families(physicalDevice, instance, surface, debug)
return [
vkGetDeviceQueue(
device=device,
queueFamilyIndex=indices.graphicsFamily,
queueIndex=0
),
vkGetDeviceQueue(
device=device,
queueFamilyIndex=indices.presentFamily,
queueIndex=0
)]