Mercurial > games > semicongine
annotate semiconginev2/old/vulkan/device.nim @ 1221:000befd9479f compiletime-tests
did: remove support for git, for now
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:05:47 +0700 |
parents | 56781cc0fc7c |
children |
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 |
1138 | 24 proc CreateDevice*( |
840
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 = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
30 assert instance.vk.Valid |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
31 assert physicalDevice.vk.Valid |
840
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), |
1137 | 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) |
1138 | 91 result.queues[family] = Queue(vk: queue, family: family, presentation: family.CanDoPresentation(physicalDevice.surface), graphics: family.CanDoGraphics()) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 |
1138 | 93 func FirstGraphicsQueue*(device: Device): Option[Queue] = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
94 assert device.vk.Valid |
840
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 |
1138 | 99 proc FirstPresentationQueue*(device: Device): Option[Queue] = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
100 assert device.vk.Valid |
840
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 |
1138 | 105 proc Destroy*(device: var Device) = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
106 assert device.vk.Valid |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 device.vk.vkDestroyDevice(nil) |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
108 device.vk.Reset() |