comparison src/vulkan_helpers.nim @ 462:0bc8643cfe25

add: more steps in setup
author Sam <sam@basx.dev>
date Fri, 16 Dec 2022 00:05:41 +0700
parents 59d861a6a5c4
children 213fdf8d31dd
comparison
equal deleted inserted replaced
461:59d861a6a5c4 462:0bc8643cfe25
1 import std/tables
1 import std/strutils 2 import std/strutils
2 import std/enumerate 3
3 import vulkan 4 import ./glslang/glslang
5 import ./vulkan
6
4 7
5 when defined(release): 8 when defined(release):
6 const ENABLEVULKANVALIDATIONLAYERS = false 9 const ENABLEVULKANVALIDATIONLAYERS = false
7 else: 10 else:
8 const ENABLEVULKANVALIDATIONLAYERS = true 11 const ENABLEVULKANVALIDATIONLAYERS = true
14 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value) 17 raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value)
15 18
16 19
17 proc VK_MAKE_API_VERSION*(variant: uint32, major: uint32, minor: uint32, patch: uint32): uint32 {.compileTime.} = 20 proc VK_MAKE_API_VERSION*(variant: uint32, major: uint32, minor: uint32, patch: uint32): uint32 {.compileTime.} =
18 (variant shl 29) or (major shl 22) or (minor shl 12) or patch 21 (variant shl 29) or (major shl 22) or (minor shl 12) or patch
22
19 23
20 proc getInstanceExtensions*(): seq[string] = 24 proc getInstanceExtensions*(): seq[string] =
21 var extensionCount: uint32 25 var extensionCount: uint32
22 checkVkResult vkEnumerateInstanceExtensionProperties(nil, addr(extensionCount), nil) 26 checkVkResult vkEnumerateInstanceExtensionProperties(nil, addr(extensionCount), nil)
23 var extensions = newSeq[VkExtensionProperties](extensionCount) 27 var extensions = newSeq[VkExtensionProperties](extensionCount)
165 enabledExtensionCount: requiredExtensions.len.uint32, 169 enabledExtensionCount: requiredExtensions.len.uint32,
166 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions)) 170 ppEnabledExtensionNames: cast[ptr UncheckedArray[cstring]](addr(requiredExtensions))
167 ) 171 )
168 checkVkResult vkCreateDevice(physicalDevice, addr(deviceCreateInfo), nil, addr(result[0])) 172 checkVkResult vkCreateDevice(physicalDevice, addr(deviceCreateInfo), nil, addr(result[0]))
169 vkGetDeviceQueue(result[0], selectedQueueFamily, 0'u32, addr(result[1])); 173 vkGetDeviceQueue(result[0], selectedQueueFamily, 0'u32, addr(result[1]));
174
175 proc createShaderStage*(device: VkDevice, stage: VkShaderStageFlagBits, shader: string): VkPipelineShaderStageCreateInfo =
176 const VK_GLSL_MAP = {
177 VK_SHADER_STAGE_VERTEX_BIT: GLSLANG_STAGE_VERTEX,
178 VK_SHADER_STAGE_FRAGMENT_BIT: GLSLANG_STAGE_FRAGMENT,
179 }.toTable()
180 var code = compileGLSLToSPIRV(VK_GLSL_MAP[stage], shader, "<memory-shader>")
181 var createInfo = VkShaderModuleCreateInfo(
182 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
183 codeSize: code.len.uint,
184 pCode: addr(code[0]),
185 )
186 var shaderModule: VkShaderModule
187 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule))
188
189 var vertShaderStageInfo = VkPipelineShaderStageCreateInfo(
190 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
191 stage: stage,
192 module: shaderModule,
193 pName: "main", # entry point for shader
194 )