comparison semiconginev2/old/vulkan/device.nim @ 1218:56781cc0fc7c compiletime-tests

did: renamge main package
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:01:37 +0700
parents semicongine/old/vulkan/device.nim@a3eb305bcac2
children
comparison
equal deleted inserted replaced
1217:f819a874058f 1218:56781cc0fc7c
1 import std/sequtils
2 import std/options
3 import std/tables
4
5 import ../core
6 import ./instance
7 import ./physicaldevice
8
9 type
10 Device* = object
11 physicalDevice*: PhysicalDevice
12 vk*: VkDevice
13 queues*: Table[QueueFamily, Queue]
14 enabledFeatures*: VkPhysicalDeviceFeatures
15 Queue* = object
16 vk*: VkQueue
17 family*: QueueFamily
18 presentation: bool
19 graphics: bool
20
21 proc `$`*(device: Device): string =
22 "Device: vk=" & $device.vk
23
24 proc CreateDevice*(
25 instance: Instance,
26 physicalDevice: PhysicalDevice,
27 enabledExtensions: seq[string],
28 queueFamilies: seq[QueueFamily],
29 ): Device =
30 assert instance.vk.Valid
31 assert physicalDevice.vk.Valid
32 assert queueFamilies.len > 0
33
34 result.physicalDevice = physicalDevice
35 # TODO: allowing support for physical devices without hasUniformBufferStandardLayout
36 # would require us to ship different shaders, so we don't support standard layout
37 # if that will be added, check the function vulkan/shaders.nim:glslUniforms and update accordingly
38 # let hasUniformBufferStandardLayout = "VK_KHR_uniform_buffer_standard_layout" in physicalDevice.getExtensions()
39 let hasUniformBufferStandardLayout = false
40
41 var allExtensions = enabledExtensions & @["VK_KHR_swapchain"]
42 if hasUniformBufferStandardLayout:
43 allExtensions.add "VK_KHR_uniform_buffer_standard_layout"
44 for extension in allExtensions:
45 instance.vk.loadExtension(extension)
46
47 var
48 enabledExtensionsC = allocCStringArray(allExtensions)
49 priority = 1'f32
50 var deviceQueues: Table[QueueFamily, VkDeviceQueueCreateInfo]
51 for family in queueFamilies:
52 deviceQueues[family] = VkDeviceQueueCreateInfo(
53 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
54 queueFamilyIndex: family.index,
55 queueCount: 1,
56 pQueuePriorities: addr(priority),
57 )
58 var queueList = deviceQueues.values.toSeq
59
60 var uniformBufferLayoutFeature = VkPhysicalDeviceUniformBufferStandardLayoutFeatures(
61 stype: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES,
62 uniformBufferStandardLayout: true,
63 )
64 var features2 = VkPhysicalDeviceFeatures2(
65 stype: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
66 features: result.enabledFeatures,
67 pnext: if hasUniformBufferStandardLayout: addr uniformBufferLayoutFeature else: nil,
68 )
69 var createInfo = VkDeviceCreateInfo(
70 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
71 queueCreateInfoCount: uint32(queueList.len),
72 pQueueCreateInfos: queueList.ToCPointer,
73 enabledLayerCount: 0,
74 ppEnabledLayerNames: nil,
75 enabledExtensionCount: uint32(allExtensions.len),
76 ppEnabledExtensionNames: enabledExtensionsC,
77 pEnabledFeatures: nil,
78 pnext: addr features2,
79 )
80
81 checkVkResult vkCreateDevice(
82 physicalDevice = physicalDevice.vk,
83 pCreateInfo = addr createInfo,
84 pAllocator = nil,
85 pDevice = addr result.vk
86 )
87 deallocCStringArray(enabledExtensionsC)
88 for family in deviceQueues.keys:
89 var queue: VkQueue
90 vkGetDeviceQueue(result.vk, family.index, 0, addr queue)
91 result.queues[family] = Queue(vk: queue, family: family, presentation: family.CanDoPresentation(physicalDevice.surface), graphics: family.CanDoGraphics())
92
93 func FirstGraphicsQueue*(device: Device): Option[Queue] =
94 assert device.vk.Valid
95 for family, queue in device.queues:
96 if queue.graphics:
97 return some(queue)
98
99 proc FirstPresentationQueue*(device: Device): Option[Queue] =
100 assert device.vk.Valid
101 for family, queue in device.queues:
102 if queue.presentation:
103 return some(queue)
104
105 proc Destroy*(device: var Device) =
106 assert device.vk.Valid
107 device.vk.vkDestroyDevice(nil)
108 device.vk.Reset()