Mercurial > games > semicongine
annotate semiconginev2/rendering.nim @ 1220:59e683c4728f compiletime-tests
del: outdated examples
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:04:09 +0700 |
parents | 56781cc0fc7c |
children | a3fa15c25026 |
rev | line source |
---|---|
1192 | 1 # in this file: |
2 # - const defintions for rendering | |
3 # - custom pragma defintions for rendering | |
4 # - type defintions for rendering | |
5 # - some utils code that is used in mutiple rendering files | |
6 # - inclusion of all rendering files | |
7 | |
8 | |
9 # const definitions | |
1197 | 10 const INFLIGHTFRAMES* = 2'u32 |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
11 const BUFFER_ALIGNMENT = 64'u64 # align offsets inside buffers along this alignment |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
12 const MEMORY_BLOCK_ALLOCATION_SIZE = 100_000_000'u64 # ca. 100mb per block, seems reasonable |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
13 const BUFFER_ALLOCATION_SIZE = 9_000_000'u64 # ca. 9mb per block, seems reasonable, can put 10 buffers into one memory block |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
14 const MAX_DESCRIPTORSETS = 4 |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
15 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
16 # custom pragmas to classify shader attributes |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
17 template VertexAttribute* {.pragma.} |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
18 template InstanceAttribute* {.pragma.} |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
19 template Pass* {.pragma.} |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
20 template PassFlat* {.pragma.} |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
21 template ShaderOutput* {.pragma.} |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
22 template DescriptorSets* {.pragma.} |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
23 |
1193 | 24 # there is a big, bad global vulkan object |
25 # believe me, this makes everything much, much easier | |
26 | |
27 include ./platform/window # for NativeWindow | |
28 include ./platform/surface # For CreateNativeSurface | |
29 | |
30 type | |
31 VulkanGlobals* = object | |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
32 # populated through InitVulkan proc |
1193 | 33 instance*: VkInstance |
34 device*: VkDevice | |
35 physicalDevice*: VkPhysicalDevice | |
36 surface: VkSurfaceKHR | |
37 window: NativeWindow | |
38 graphicsQueueFamily*: uint32 | |
39 graphicsQueue*: VkQueue | |
1199 | 40 debugMessenger: VkDebugUtilsMessengerEXT |
1197 | 41 # unclear as of yet |
42 anisotropy*: float32 = 0 # needs to be enable during device creation | |
1203
6360c8d17ce0
did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
1202
diff
changeset
|
43 Swapchain* = object |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
44 # parameters to InitSwapchain, required for swapchain recreation |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
45 renderPass: VkRenderPass |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
46 vSync: bool |
1204 | 47 samples*: VkSampleCountFlagBits |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
48 # populated through InitSwapchain proc |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
49 vk: VkSwapchainKHR |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
50 width*: uint32 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
51 height*: uint32 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
52 msaaImage: VkImage |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
53 msaaMemory: VkDeviceMemory |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
54 msaaImageView: VkImageView |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
55 framebuffers: seq[VkFramebuffer] |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
56 framebufferViews: seq[VkImageView] |
1198 | 57 currentFramebufferIndex: uint32 |
58 commandBufferPool: VkCommandPool | |
59 # frame-in-flight handling | |
60 currentFiF: range[0 .. (INFLIGHTFRAMES - 1).int] | |
1197 | 61 queueFinishedFence*: array[INFLIGHTFRAMES.int, VkFence] |
62 imageAvailableSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore] | |
63 renderFinishedSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore] | |
1198 | 64 commandBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
65 oldSwapchain: ref Swapchain |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
66 oldSwapchainCounter: int # swaps until old swapchain will be destroyed |
1193 | 67 |
68 var vulkan*: VulkanGlobals | |
69 | |
1202 | 70 func currentFiF*(swapchain: Swapchain): int = swapchain.currentFiF |
71 | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
72 type |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
73 # type aliases |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
74 SupportedGPUType = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64] |
1212
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1210
diff
changeset
|
75 TextureType = TVec1[uint8] | TVec4[uint8] |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
76 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
77 # shader related types |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
78 DescriptorSet*[T: object] = object |
1192 | 79 data*: T |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
80 vk: array[INFLIGHTFRAMES.int, VkDescriptorSet] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
81 Pipeline*[TShader] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
82 vk: VkPipeline |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
83 vertexShaderModule: VkShaderModule |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
84 fragmentShaderModule: VkShaderModule |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
85 layout: VkPipelineLayout |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
86 descriptorSetLayouts*: array[MAX_DESCRIPTORSETS, VkDescriptorSetLayout] |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
87 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
88 # memory/buffer related types |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
89 MemoryBlock* = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
90 vk: VkDeviceMemory |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
91 size: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
92 rawPointer: pointer # if not nil, this is mapped memory |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
93 offsetNextFree: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
94 BufferType* = enum |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
95 VertexBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
96 VertexBufferMapped |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
97 IndexBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
98 IndexBufferMapped |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
99 UniformBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
100 UniformBufferMapped |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
101 Buffer* = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
102 vk: VkBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
103 size: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
104 rawPointer: pointer # if not nil, buffer is using mapped memory |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
105 offsetNextFree: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
106 Texture*[T: TextureType] = object |
1210 | 107 width*: uint32 |
108 height*: uint32 | |
109 interpolation*: VkFilter = VK_FILTER_LINEAR | |
110 data*: seq[T] | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
111 vk*: VkImage |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
112 imageview*: VkImageView |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
113 sampler*: VkSampler |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
114 isRenderTarget*: bool = false |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
115 GPUArray*[T: SupportedGPUType, TBuffer: static BufferType] = object |
1192 | 116 data*: seq[T] |
1202 | 117 buffer*: Buffer |
118 offset*: uint64 | |
1210 | 119 GPUValue*[T: object, TBuffer: static BufferType] = object |
1192 | 120 data*: T |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
121 buffer: Buffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
122 offset: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
123 GPUData = GPUArray | GPUValue |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
124 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
125 RenderData* = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
126 descriptorPool: VkDescriptorPool |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
127 memory: array[VK_MAX_MEMORY_TYPES.int, seq[MemoryBlock]] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
128 buffers: array[BufferType, seq[Buffer]] |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
129 images: seq[VkImage] |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
130 imageViews: seq[VkImageView] |
1201 | 131 samplers: seq[VkSampler] |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
132 |
1192 | 133 template ForDescriptorFields(shader: typed, fieldname, valuename, typename, countname, bindingNumber, body: untyped): untyped = |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
134 var `bindingNumber` {.inject.} = 0'u32 |
1192 | 135 for theFieldname, value in fieldPairs(shader): |
136 when typeof(value) is Texture: | |
137 block: | |
138 const `fieldname` {.inject.} = theFieldname | |
139 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER | |
140 const `countname` {.inject.} = 1'u32 | |
141 let `valuename` {.inject.} = value | |
142 body | |
143 `bindingNumber`.inc | |
1210 | 144 elif typeof(value) is GPUValue: |
1192 | 145 block: |
146 const `fieldname` {.inject.} = theFieldname | |
147 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER | |
148 const `countname` {.inject.} = 1'u32 | |
149 let `valuename` {.inject.} = value | |
150 body | |
151 `bindingNumber`.inc | |
152 elif typeof(value) is array: | |
153 when elementType(value) is Texture: | |
154 block: | |
155 const `fieldname` {.inject.} = theFieldname | |
156 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER | |
157 const `countname` {.inject.} = uint32(typeof(value).len) | |
158 let `valuename` {.inject.} = value | |
159 body | |
160 `bindingNumber`.inc | |
1210 | 161 elif elementType(value) is GPUValue: |
1192 | 162 block: |
163 const `fieldname` {.inject.} = theFieldname | |
164 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER | |
1210 | 165 const `countname` {.inject.} = len(value).uint32 |
1192 | 166 let `valuename` {.inject.} = value |
167 body | |
168 `bindingNumber`.inc | |
169 else: | |
170 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(value)).} | |
1210 | 171 else: |
172 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(value)).} | |
1192 | 173 |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
174 include ./rendering/vulkan_wrappers |
1199 | 175 include ./rendering/renderpasses |
1194 | 176 include ./rendering/swapchain |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
177 include ./rendering/shaders |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
178 include ./rendering/renderer |
1193 | 179 |
1199 | 180 proc debugCallback( |
181 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT, | |
182 messageTypes: VkDebugUtilsMessageTypeFlagsEXT, | |
183 pCallbackData: ptr VkDebugUtilsMessengerCallbackDataEXT, | |
184 userData: pointer | |
185 ): VkBool32 {.cdecl.} = | |
186 const LOG_LEVEL_MAPPING = { | |
187 VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: lvlDebug, | |
188 VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: lvlInfo, | |
189 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: lvlWarn, | |
190 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: lvlError, | |
191 }.toTable | |
192 log LOG_LEVEL_MAPPING[messageSeverity], &"{toEnums messageTypes}: {pCallbackData.pMessage}" | |
193 if messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: | |
194 stderr.writeLine "-----------------------------------" | |
195 stderr.write getStackTrace() | |
196 stderr.writeLine LOG_LEVEL_MAPPING[messageSeverity], &"{toEnums messageTypes}: {pCallbackData.pMessage}" | |
197 stderr.writeLine "-----------------------------------" | |
198 let errorMsg = getStackTrace() & &"\n{toEnums messageTypes}: {pCallbackData.pMessage}" | |
199 raise newException(Exception, errorMsg) | |
200 return false | |
1193 | 201 |
1204 | 202 proc InitVulkan*(appName: string = "semicongine app") = |
1193 | 203 |
204 include ./platform/vulkan_extensions # for REQUIRED_PLATFORM_EXTENSIONS | |
205 | |
206 # instance creation | |
1198 | 207 |
208 # enagle all kind of debug stuff | |
1193 | 209 when not defined(release): |
210 let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface", "VK_EXT_debug_utils"] | |
211 let layers: seq[string] = if hasValidationLayer(): @["VK_LAYER_KHRONOS_validation"] else: @[] | |
1198 | 212 putEnv("VK_LAYER_ENABLES", "VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_AMD,VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_NVIDIA,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXTVK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT") |
1193 | 213 else: |
214 let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface"] | |
215 let layers: seq[string] | |
216 | |
217 var | |
218 layersC = allocCStringArray(layers) | |
219 instanceExtensionsC = allocCStringArray(requiredExtensions) | |
220 defer: | |
221 deallocCStringArray(layersC) | |
222 deallocCStringArray(instanceExtensionsC) | |
223 | |
224 var | |
225 appinfo = VkApplicationInfo( | |
226 sType: VK_STRUCTURE_TYPE_APPLICATION_INFO, | |
227 pApplicationName: appName, | |
228 pEngineName: "semicongine", | |
229 apiVersion: VK_MAKE_API_VERSION(0, 1, 3, 0), | |
230 ) | |
231 createinfo = VkInstanceCreateInfo( | |
232 sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, | |
233 pApplicationInfo: addr(appinfo), | |
234 enabledLayerCount: layers.len.uint32, | |
235 ppEnabledLayerNames: layersC, | |
236 enabledExtensionCount: requiredExtensions.len.uint32, | |
237 ppEnabledExtensionNames: instanceExtensionsC | |
238 ) | |
1204 | 239 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(vulkan.instance)) |
240 loadVulkan(vulkan.instance) | |
1193 | 241 |
242 # load extensions | |
243 # | |
244 for extension in requiredExtensions: | |
1204 | 245 loadExtension(vulkan.instance, $extension) |
246 vulkan.window = CreateWindow(appName) | |
247 vulkan.surface = CreateNativeSurface(vulkan.instance, vulkan.window) | |
1193 | 248 |
249 # logical device creation | |
250 | |
251 # TODO: allowing support for physical devices without hasUniformBufferStandardLayout | |
252 # would require us to ship different shaders, so we don't support standard layout | |
253 # if that will be added, check the function vulkan/shaders.nim:glslUniforms and update accordingly | |
254 # let hasUniformBufferStandardLayout = "VK_KHR_uniform_buffer_standard_layout" in physicalDevice.getExtensions() | |
255 # var deviceExtensions = @["VK_KHR_swapchain", "VK_KHR_uniform_buffer_standard_layout"] | |
256 var deviceExtensions = @["VK_KHR_swapchain"] | |
257 for extension in deviceExtensions: | |
1204 | 258 loadExtension(vulkan.instance, extension) |
1193 | 259 |
1199 | 260 when not defined(release): |
261 var debugMessengerCreateInfo = VkDebugUtilsMessengerCreateInfoEXT( | |
262 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, | |
263 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT.items.toSeq.toBits, | |
264 messageType: VkDebugUtilsMessageTypeFlagBitsEXT.items.toSeq.toBits, | |
265 pfnUserCallback: debugCallback, | |
266 pUserData: nil, | |
267 ) | |
268 checkVkResult vkCreateDebugUtilsMessengerEXT( | |
1204 | 269 vulkan.instance, |
1199 | 270 addr(debugMessengerCreateInfo), |
271 nil, | |
1204 | 272 addr(vulkan.debugMessenger) |
1199 | 273 ) |
274 | |
1193 | 275 # get physical device and graphics queue family |
1204 | 276 vulkan.physicalDevice = GetBestPhysicalDevice(vulkan.instance) |
277 vulkan.graphicsQueueFamily = GetQueueFamily(vulkan.physicalDevice, VK_QUEUE_GRAPHICS_BIT) | |
1193 | 278 |
279 let | |
280 priority = cfloat(1) | |
281 queueInfo = VkDeviceQueueCreateInfo( | |
282 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, | |
1204 | 283 queueFamilyIndex: vulkan.graphicsQueueFamily, |
1193 | 284 queueCount: 1, |
285 pQueuePriorities: addr(priority), | |
286 ) | |
287 deviceExtensionsC = allocCStringArray(deviceExtensions) | |
288 defer: deallocCStringArray(deviceExtensionsC) | |
289 var createDeviceInfo = VkDeviceCreateInfo( | |
290 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, | |
291 queueCreateInfoCount: 1, | |
292 pQueueCreateInfos: addr(queueInfo), | |
293 enabledLayerCount: 0, | |
294 ppEnabledLayerNames: nil, | |
295 enabledExtensionCount: uint32(deviceExtensions.len), | |
296 ppEnabledExtensionNames: deviceExtensionsC, | |
297 pEnabledFeatures: nil, | |
298 ) | |
299 checkVkResult vkCreateDevice( | |
1204 | 300 physicalDevice = vulkan.physicalDevice, |
1193 | 301 pCreateInfo = addr createDeviceInfo, |
302 pAllocator = nil, | |
1204 | 303 pDevice = addr vulkan.device |
1193 | 304 ) |
1204 | 305 vulkan.graphicsQueue = svkGetDeviceQueue(vulkan.device, vulkan.graphicsQueueFamily, VK_QUEUE_GRAPHICS_BIT) |
1193 | 306 |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
307 proc DestroyVulkan*() = |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
308 vkDestroyDevice(vulkan.device, nil) |
1201 | 309 vkDestroySurfaceKHR(vulkan.instance, vulkan.surface, nil) |
310 vkDestroyDebugUtilsMessengerEXT(vulkan.instance, vulkan.debugMessenger, nil) | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
311 vkDestroyInstance(vulkan.instance, nil) |