Mercurial > games > semicongine
annotate semicongine/vulkan/device.nim @ 982:a40bb0a48e07
do: try to increase compatability with older hardware...
author | sam <sam@basx.dev> |
---|---|
date | Sat, 06 Apr 2024 17:29:48 +0700 |
parents | c81541f55d9c |
children | a4aa9f374d44 |
rev | line source |
---|---|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/sequtils |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/options |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/tables |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import ../core |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 import ./instance |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 import ./physicaldevice |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 Device* = object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 physicalDevice*: PhysicalDevice |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 vk*: VkDevice |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 queues*: Table[QueueFamily, Queue] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 enabledFeatures*: VkPhysicalDeviceFeatures |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 Queue* = object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 vk*: VkQueue |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 family*: QueueFamily |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 presentation: bool |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 graphics: bool |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 proc `$`*(device: Device): string = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 "Device: vk=" & $device.vk |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 proc createDevice*( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 instance: Instance, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 physicalDevice: PhysicalDevice, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 enabledExtensions: seq[string], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 queueFamilies: seq[QueueFamily], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 ): Device = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 assert instance.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 assert physicalDevice.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 assert queueFamilies.len > 0 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 result.physicalDevice = physicalDevice |
982
a40bb0a48e07
do: try to increase compatability with older hardware...
sam <sam@basx.dev>
parents:
981
diff
changeset
|
35 # TODO: allowing support for physical devices without hasUniformBufferStandardLayout |
a40bb0a48e07
do: try to increase compatability with older hardware...
sam <sam@basx.dev>
parents:
981
diff
changeset
|
36 # would require us to ship different shaders, so we don't support standard layout |
a40bb0a48e07
do: try to increase compatability with older hardware...
sam <sam@basx.dev>
parents:
981
diff
changeset
|
37 # if that will be added, check the function vulkan/shaders.nim:glslUniforms and update accordingly |
a40bb0a48e07
do: try to increase compatability with older hardware...
sam <sam@basx.dev>
parents:
981
diff
changeset
|
38 # let hasUniformBufferStandardLayout = "VK_KHR_uniform_buffer_standard_layout" in physicalDevice.getExtensions() |
a40bb0a48e07
do: try to increase compatability with older hardware...
sam <sam@basx.dev>
parents:
981
diff
changeset
|
39 let hasUniformBufferStandardLayout = false |
981
c81541f55d9c
add: check for uniform buffer layout, seem not be available on some older devices
sam <sam@basx.dev>
parents:
979
diff
changeset
|
40 |
c81541f55d9c
add: check for uniform buffer layout, seem not be available on some older devices
sam <sam@basx.dev>
parents:
979
diff
changeset
|
41 var allExtensions = enabledExtensions & @["VK_KHR_swapchain"] |
c81541f55d9c
add: check for uniform buffer layout, seem not be available on some older devices
sam <sam@basx.dev>
parents:
979
diff
changeset
|
42 if hasUniformBufferStandardLayout: |
c81541f55d9c
add: check for uniform buffer layout, seem not be available on some older devices
sam <sam@basx.dev>
parents:
979
diff
changeset
|
43 allExtensions.add "VK_KHR_uniform_buffer_standard_layout" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 for extension in allExtensions: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 instance.vk.loadExtension(extension) |
981
c81541f55d9c
add: check for uniform buffer layout, seem not be available on some older devices
sam <sam@basx.dev>
parents:
979
diff
changeset
|
46 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 var |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 enabledExtensionsC = allocCStringArray(allExtensions) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 priority = 1'f32 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 var deviceQueues: Table[QueueFamily, VkDeviceQueueCreateInfo] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 for family in queueFamilies: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 deviceQueues[family] = VkDeviceQueueCreateInfo( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 queueFamilyIndex: family.index, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 queueCount: 1, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 pQueuePriorities: addr(priority), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 var queueList = deviceQueues.values.toSeq |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 |
979 | 60 var uniformBufferLayoutFeature = VkPhysicalDeviceUniformBufferStandardLayoutFeatures( |
61 stype: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES, | |
62 uniformBufferStandardLayout: true, | |
63 ) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 var features2 = VkPhysicalDeviceFeatures2( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 stype: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 features: result.enabledFeatures, |
981
c81541f55d9c
add: check for uniform buffer layout, seem not be available on some older devices
sam <sam@basx.dev>
parents:
979
diff
changeset
|
67 pnext: if hasUniformBufferStandardLayout: addr uniformBufferLayoutFeature else: nil, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 var createInfo = VkDeviceCreateInfo( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 queueCreateInfoCount: uint32(queueList.len), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 pQueueCreateInfos: queueList.toCPointer, |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
945
diff
changeset
|
73 enabledLayerCount: 0, |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
945
diff
changeset
|
74 ppEnabledLayerNames: nil, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 enabledExtensionCount: uint32(allExtensions.len), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 ppEnabledExtensionNames: enabledExtensionsC, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 pEnabledFeatures: nil, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 pnext: addr features2, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
81 checkVkResult vkCreateDevice( |
902 | 82 physicalDevice = physicalDevice.vk, |
83 pCreateInfo = addr createInfo, | |
84 pAllocator = nil, | |
85 pDevice = addr result.vk | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
86 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
87 deallocCStringArray(enabledExtensionsC) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
88 for family in deviceQueues.keys: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
89 var queue: VkQueue |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
90 vkGetDeviceQueue(result.vk, family.index, 0, addr queue) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 result.queues[family] = Queue(vk: queue, family: family, presentation: family.canDoPresentation(physicalDevice.surface), graphics: family.canDoGraphics()) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 func firstGraphicsQueue*(device: Device): Option[Queue] = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 assert device.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 for family, queue in device.queues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 if queue.graphics: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 return some(queue) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 proc firstPresentationQueue*(device: Device): Option[Queue] = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 assert device.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 for family, queue in device.queues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 if queue.presentation: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 return some(queue) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
105 proc destroy*(device: var Device) = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 assert device.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 device.vk.vkDestroyDevice(nil) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 device.vk.reset() |