Mercurial > games > semicongine
comparison src/engine.nim @ 1:bb2a7d3a7003
add: more steps in setup
| author | Sam <sam@basx.dev> |
|---|---|
| date | Fri, 16 Dec 2022 00:05:41 +0700 |
| parents | 5daf3f236d87 |
| children | 213fdf8d31dd |
comparison
equal
deleted
inserted
replaced
| 0:5daf3f236d87 | 1:bb2a7d3a7003 |
|---|---|
| 3 import ./vulkan | 3 import ./vulkan |
| 4 import ./vulkan_helpers | 4 import ./vulkan_helpers |
| 5 import ./xlib_helpers | 5 import ./xlib_helpers |
| 6 | 6 |
| 7 import ./glslang/glslang | 7 import ./glslang/glslang |
| 8 import ./glslang/glslang_c_shader_types | 8 |
| 9 | 9 var vertexShaderCode: string = """#version 450 |
| 10 layout(location = 0) out vec3 fragColor; | |
| 11 vec3 colors[3] = vec3[]( | |
| 12 vec3(1.0, 0.0, 0.0), | |
| 13 vec3(0.0, 1.0, 0.0), | |
| 14 vec3(0.0, 0.0, 1.0) | |
| 15 ); | |
| 16 vec2 positions[3] = vec2[]( | |
| 17 vec2(0.0, -0.5), | |
| 18 vec2(0.5, 0.5), | |
| 19 vec2(-0.5, 0.5) | |
| 20 ); | |
| 21 void main() { | |
| 22 gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); | |
| 23 fragColor = colors[gl_VertexIndex]; | |
| 24 }""" | |
| 25 | |
| 26 var fragmentShaderCode: string = """#version 450 | |
| 27 layout(location = 0) out vec4 outColor; | |
| 28 layout(location = 0) in vec3 fragColor; | |
| 29 void main() { | |
| 30 outColor = vec4(fragColor, 1.0); | |
| 31 }""" | |
| 10 | 32 |
| 11 import | 33 import |
| 12 x11/xlib, | 34 x11/xlib, |
| 13 x11/x | 35 x11/x |
| 14 | 36 |
| 15 const VULKAN_VERSION = VK_MAKE_API_VERSION(0'u32, 1'u32, 2'u32, 0'u32) | 37 const VULKAN_VERSION = VK_MAKE_API_VERSION(0'u32, 1'u32, 2'u32, 0'u32) |
| 16 | 38 |
| 17 type | 39 type |
| 40 GraphicsPipeline = object | |
| 41 shaderStages*: seq[VkPipelineShaderStageCreateInfo] | |
| 18 QueueFamily = object | 42 QueueFamily = object |
| 19 properties*: VkQueueFamilyProperties | 43 properties*: VkQueueFamilyProperties |
| 20 hasSurfaceSupport*: bool | 44 hasSurfaceSupport*: bool |
| 21 PhyscialDevice = object | 45 PhyscialDevice = object |
| 22 device*: VkPhysicalDevice | 46 device*: VkPhysicalDevice |
| 43 swapImageViews: seq[VkImageView] | 67 swapImageViews: seq[VkImageView] |
| 44 Engine* = object | 68 Engine* = object |
| 45 display*: PDisplay | 69 display*: PDisplay |
| 46 window*: x.Window | 70 window*: x.Window |
| 47 vulkan*: Vulkan | 71 vulkan*: Vulkan |
| 72 pipeline*: GraphicsPipeline | |
| 48 | 73 |
| 49 | 74 |
| 50 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[PhyscialDevice] = | 75 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[PhyscialDevice] = |
| 51 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance): | 76 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance): |
| 52 var device = PhyscialDevice(device: vulkanPhysicalDevice, extensions: getDeviceExtensions(vulkanPhysicalDevice)) | 77 var device = PhyscialDevice(device: vulkanPhysicalDevice, extensions: getDeviceExtensions(vulkanPhysicalDevice)) |
| 55 checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanPhysicalDevice, surface, addr(device.surfaceCapabilities)) | 80 checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanPhysicalDevice, surface, addr(device.surfaceCapabilities)) |
| 56 device.surfaceFormats = getDeviceSurfaceFormats(vulkanPhysicalDevice, surface) | 81 device.surfaceFormats = getDeviceSurfaceFormats(vulkanPhysicalDevice, surface) |
| 57 device.presentModes = getDeviceSurfacePresentModes(vulkanPhysicalDevice, surface) | 82 device.presentModes = getDeviceSurfacePresentModes(vulkanPhysicalDevice, surface) |
| 58 | 83 |
| 59 for i, queueFamilyProperty in enumerate(getQueueFamilies(vulkanPhysicalDevice)): | 84 for i, queueFamilyProperty in enumerate(getQueueFamilies(vulkanPhysicalDevice)): |
| 60 var hasSurfaceSupport: VkBool32 = VkBool32(false) | 85 var hasSurfaceSupport: VkBool32 = VK_FALSE |
| 61 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, uint32(i), surface, addr(hasSurfaceSupport)) | 86 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, uint32(i), surface, addr(hasSurfaceSupport)) |
| 62 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty, hasSurfaceSupport: bool(hasSurfaceSupport))) | 87 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty, hasSurfaceSupport: bool(hasSurfaceSupport))) |
| 63 | 88 |
| 64 result.add(device) | 89 result.add(device) |
| 65 | 90 |
| 144 # VK_SHARING_MODE_CONCURRENT no supported (i.e cannot use different queue families for drawing to swap surface?) | 169 # VK_SHARING_MODE_CONCURRENT no supported (i.e cannot use different queue families for drawing to swap surface?) |
| 145 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE, | 170 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE, |
| 146 preTransform: result.vulkan.activePhysicalDevice.surfaceCapabilities.currentTransform, | 171 preTransform: result.vulkan.activePhysicalDevice.surfaceCapabilities.currentTransform, |
| 147 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, | 172 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, |
| 148 presentMode: result.vulkan.selectedPresentationMode, | 173 presentMode: result.vulkan.selectedPresentationMode, |
| 149 clipped: VkBool32(true), | 174 clipped: VK_TRUE, |
| 150 oldSwapchain: VkSwapchainKHR(0), | 175 oldSwapchain: VkSwapchainKHR(0), |
| 151 ) | 176 ) |
| 152 checkVkResult vkCreateSwapchainKHR(result.vulkan.device, addr(swapchainCreateInfo), nil, addr(result.vulkan.swapChain)) | 177 checkVkResult vkCreateSwapchainKHR(result.vulkan.device, addr(swapchainCreateInfo), nil, addr(result.vulkan.swapChain)) |
| 153 result.vulkan.swapImages = getSwapChainImages(result.vulkan.device, result.vulkan.swapChain) | 178 result.vulkan.swapImages = getSwapChainImages(result.vulkan.device, result.vulkan.swapChain) |
| 154 | 179 |
| 173 baseArrayLayer: 0, | 198 baseArrayLayer: 0, |
| 174 layerCount: 1, | 199 layerCount: 1, |
| 175 ), | 200 ), |
| 176 ) | 201 ) |
| 177 checkVkResult vkCreateImageView(result.vulkan.device, addr(imageViewCreateInfo), nil, addr(result.vulkan.swapImageViews[i])) | 202 checkVkResult vkCreateImageView(result.vulkan.device, addr(imageViewCreateInfo), nil, addr(result.vulkan.swapImageViews[i])) |
| 178 echo compileShaderToSPIRV_Vulkan(GLSLANG_STAGE_VERTEX, """#version 450 | 203 |
| 179 vec2 positions[3] = vec2[]( | 204 # init shader system |
| 180 vec2(0.0, -0.5), | 205 checkGlslangResult glslang_initialize_process() |
| 181 vec2(0.5, 0.5), | 206 |
| 182 vec2(-0.5, 0.5) | 207 # load shaders |
| 183 ); | 208 result.pipeline.shaderStages.add(createShaderStage(result.vulkan.device, VK_SHADER_STAGE_VERTEX_BIT, vertexShaderCode)) |
| 184 void main() { | 209 result.pipeline.shaderStages.add(createShaderStage(result.vulkan.device, VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderCode)) |
| 185 gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); | 210 |
| 186 }""", "<memory-shader>") | 211 # create graphis pipeline |
| 212 var dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
| 213 var dynamicState = VkPipelineDynamicStateCreateInfo( | |
| 214 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
| 215 dynamicStateCount: uint32(dynamicStates.len), | |
| 216 pDynamicStates: addr(dynamicStates[0]), | |
| 217 ) | |
| 218 var vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
| 219 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
| 220 vertexBindingDescriptionCount: 0, | |
| 221 pVertexBindingDescriptions: nil, | |
| 222 vertexAttributeDescriptionCount: 0, | |
| 223 pVertexAttributeDescriptions: nil, | |
| 224 ) | |
| 225 var inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
| 226 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
| 227 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, | |
| 228 primitiveRestartEnable: VK_FALSE, | |
| 229 ) | |
| 230 | |
| 231 # setup viewport | |
| 232 var viewport = VkViewport( | |
| 233 x: 0.0, | |
| 234 y: 0.0, | |
| 235 width: (float) result.vulkan.selectedExtent.width, | |
| 236 height: (float) result.vulkan.selectedExtent.height, | |
| 237 minDepth: 0.0, | |
| 238 maxDepth: 1.0, | |
| 239 ) | |
| 240 var scissor = VkRect2D( | |
| 241 offset: VkOffset2D(x: 0, y: 0), | |
| 242 extent: result.vulkan.selectedExtent | |
| 243 ) | |
| 187 | 244 |
| 188 | 245 |
| 189 proc fullThrottle*(engine: Engine) = | 246 proc fullThrottle*(engine: Engine) = |
| 190 var event: XEvent | 247 var event: XEvent |
| 191 while true: | 248 while true: |
| 205 event.xbutton.x, ",", event.xbutton.y | 262 event.xbutton.x, ",", event.xbutton.y |
| 206 else: | 263 else: |
| 207 discard | 264 discard |
| 208 | 265 |
| 209 proc trash*(engine: Engine) = | 266 proc trash*(engine: Engine) = |
| 267 for shaderStage in engine.pipeline.shaderStages: | |
| 268 vkDestroyShaderModule(engine.vulkan.device, shaderStage.module, nil); | |
| 269 glslang_finalize_process() | |
| 210 vkDestroySwapchainKHR(engine.vulkan.device, engine.vulkan.swapChain, nil); | 270 vkDestroySwapchainKHR(engine.vulkan.device, engine.vulkan.swapChain, nil); |
| 211 vkDestroySurfaceKHR(engine.vulkan.instance, engine.vulkan.surface, nil); | 271 vkDestroySurfaceKHR(engine.vulkan.instance, engine.vulkan.surface, nil); |
| 212 vkDestroyDevice(engine.vulkan.device, nil) | 272 vkDestroyDevice(engine.vulkan.device, nil) |
| 213 vkDestroyInstance(engine.vulkan.instance, nil) | 273 vkDestroyInstance(engine.vulkan.instance, nil) |
| 214 checkXlibResult engine.display.XDestroyWindow(engine.window) | 274 checkXlibResult engine.display.XDestroyWindow(engine.window) |
