annotate src/engine.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/enumerate
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
2
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
3 import ./vulkan
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
4 import ./vulkan_helpers
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
5 import ./xlib_helpers
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
6
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
7 import ./glslang/glslang
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
8
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
9 var vertexShaderCode: string = """#version 450
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
10 layout(location = 0) out vec3 fragColor;
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
11 vec3 colors[3] = vec3[](
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
12 vec3(1.0, 0.0, 0.0),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
13 vec3(0.0, 1.0, 0.0),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
14 vec3(0.0, 0.0, 1.0)
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
15 );
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
16 vec2 positions[3] = vec2[](
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
17 vec2(0.0, -0.5),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
18 vec2(0.5, 0.5),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
19 vec2(-0.5, 0.5)
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
20 );
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
21 void main() {
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
22 gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
23 fragColor = colors[gl_VertexIndex];
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
24 }"""
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
25
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
26 var fragmentShaderCode: string = """#version 450
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
27 layout(location = 0) out vec4 outColor;
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
28 layout(location = 0) in vec3 fragColor;
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
29 void main() {
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
30 outColor = vec4(fragColor, 1.0);
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
31 }"""
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
33 import
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
34 x11/xlib,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
35 x11/x
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
36
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
37 const VULKAN_VERSION = VK_MAKE_API_VERSION(0'u32, 1'u32, 2'u32, 0'u32)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
38
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
39 type
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
40 GraphicsPipeline = object
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
41 shaderStages*: seq[VkPipelineShaderStageCreateInfo]
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
42 QueueFamily = object
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
43 properties*: VkQueueFamilyProperties
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
44 hasSurfaceSupport*: bool
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
45 PhyscialDevice = object
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
46 device*: VkPhysicalDevice
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
47 extensions*: seq[string]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
48 properties*: VkPhysicalDeviceProperties
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
49 features*: VkPhysicalDeviceFeatures
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
50 queueFamilies*: seq[QueueFamily]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
51 surfaceCapabilities*: VkSurfaceCapabilitiesKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
52 surfaceFormats: seq[VkSurfaceFormatKHR]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
53 presentModes: seq[VkPresentModeKHR]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
54 Vulkan* = object
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
55 instance*: VkInstance
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
56 deviceList*: seq[PhyscialDevice]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
57 activePhysicalDevice*: PhyscialDevice
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
58 activeQueueFamily*: uint32
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
59 device*: VkDevice
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
60 presentationQueue*: VkQueue
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
61 surface*: VkSurfaceKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
62 selectedSurfaceFormat: VkSurfaceFormatKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
63 selectedPresentationMode: VkPresentModeKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
64 selectedExtent: VkExtent2D
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
65 swapChain: VkSwapchainKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
66 swapImages: seq[VkImage]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
67 swapImageViews: seq[VkImageView]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
68 Engine* = object
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
69 display*: PDisplay
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
70 window*: x.Window
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
71 vulkan*: Vulkan
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
72 pipeline*: GraphicsPipeline
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
73
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
74
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
75 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[PhyscialDevice] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
76 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
77 var device = PhyscialDevice(device: vulkanPhysicalDevice, extensions: getDeviceExtensions(vulkanPhysicalDevice))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
78 vkGetPhysicalDeviceProperties(vulkanPhysicalDevice, addr(device.properties))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
79 vkGetPhysicalDeviceFeatures(vulkanPhysicalDevice, addr(device.features))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
80 checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanPhysicalDevice, surface, addr(device.surfaceCapabilities))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
81 device.surfaceFormats = getDeviceSurfaceFormats(vulkanPhysicalDevice, surface)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
82 device.presentModes = getDeviceSurfacePresentModes(vulkanPhysicalDevice, surface)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
83
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
84 for i, queueFamilyProperty in enumerate(getQueueFamilies(vulkanPhysicalDevice)):
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
85 var hasSurfaceSupport: VkBool32 = VK_FALSE
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
86 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, uint32(i), surface, addr(hasSurfaceSupport))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
87 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty, hasSurfaceSupport: bool(hasSurfaceSupport)))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
88
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
89 result.add(device)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
90
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
91 proc filterForDevice(devices: seq[PhyscialDevice]): seq[(PhyscialDevice, uint32)] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
92 for device in devices:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
93 if "VK_KHR_swapchain" in device.extensions:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
94 for i, queueFamily in enumerate(device.queueFamilies):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
95 let hasGraphics = bool(uint32(queueFamily.properties.queueFlags) and ord(VK_QUEUE_GRAPHICS_BIT))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
96 if (
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
97 queueFamily.hasSurfaceSupport and
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
98 hasGraphics and
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
99 device.surfaceFormats.len > 0 and
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
100 device.presentModes.len > 0
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
101 ):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
102 result.add((device, uint32(i)))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
103
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
104 proc filterForSurfaceFormat(formats: seq[VkSurfaceFormatKHR]): seq[VkSurfaceFormatKHR] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
105 for format in formats:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
106 if format.format == VK_FORMAT_B8G8R8A8_SRGB and format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
107 result.add(format)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
108
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
109 proc getSwapExtent(display: PDisplay, window: Window, capabilities: VkSurfaceCapabilitiesKHR): VkExtent2D =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
110 if capabilities.currentExtent.width != high(uint32):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
111 return capabilities.currentExtent
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
112 else:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
113 let (width, height) = xlibFramebufferSize(display, window)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
114 return VkExtent2D(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
115 width: min(max(uint32(width), capabilities.minImageExtent.width), capabilities.maxImageExtent.width),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
116 height: min(max(uint32(height), capabilities.minImageExtent.height), capabilities.maxImageExtent.height),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
117 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
118
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
119 proc igniteEngine*(): Engine =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
120 vkLoad1_0()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
121 vkLoad1_1()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
122 vkLoad1_2()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
123
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
124 # init X11 window
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
125 (result.display, result.window) = xlibInit()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
126
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
127 # create vulkan instance
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
128 result.vulkan.instance = createVulkanInstance(VULKAN_VERSION)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
129
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
130 # create vulkan-X11 surface
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
131 var surfaceCreateInfo = VkXlibSurfaceCreateInfoKHR(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
132 sType: VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
133 dpy: result.display,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
134 window: result.window,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
135 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
136 checkVkResult vkCreateXlibSurfaceKHR(result.vulkan.instance, addr(surfaceCreateInfo), nil, addr(result.vulkan.surface))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
137
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
138 # determine device and queue to use and instantiate
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
139 result.vulkan.deviceList = result.vulkan.instance.getAllPhysicalDevices(result.vulkan.surface)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
140 let usableDevices = result.vulkan.deviceList.filterForDevice()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
141 if len(usableDevices) == 0:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
142 raise newException(Exception, "No suitable graphics device found")
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
143 (result.vulkan.activePhysicalDevice, result.vulkan.activeQueueFamily) = usableDevices[0]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
144
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
145 (result.vulkan.device, result.vulkan.presentationQueue) = getVulcanDevice(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
146 result.vulkan.activePhysicalDevice.device,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
147 result.vulkan.activePhysicalDevice.features,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
148 result.vulkan.activeQueueFamily
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
149 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
150
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
151 # determine surface format for swapchain
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
152 let usableSurfaceFormats = filterForSurfaceFormat(result.vulkan.activePhysicalDevice.surfaceFormats)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
153 if len(usableSurfaceFormats) == 0:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
154 raise newException(Exception, "No suitable surface formats found")
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
155 result.vulkan.selectedSurfaceFormat = usableSurfaceFormats[0]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
156 result.vulkan.selectedPresentationMode = getPresentMode(result.vulkan.activePhysicalDevice.presentModes)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
157 result.vulkan.selectedExtent = getSwapExtent(result.display, result.window, result.vulkan.activePhysicalDevice.surfaceCapabilities)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
158
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
159 # setup swapchain
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
160 var swapchainCreateInfo = VkSwapchainCreateInfoKHR(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
161 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
162 surface: result.vulkan.surface,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
163 minImageCount: max(result.vulkan.activePhysicalDevice.surfaceCapabilities.minImageCount + 1, result.vulkan.activePhysicalDevice.surfaceCapabilities.maxImageCount),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
164 imageFormat: result.vulkan.selectedSurfaceFormat.format,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
165 imageColorSpace: result.vulkan.selectedSurfaceFormat.colorSpace,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
166 imageExtent: result.vulkan.selectedExtent,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
167 imageArrayLayers: 1,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
168 imageUsage: VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
169 # VK_SHARING_MODE_CONCURRENT no supported (i.e cannot use different queue families for drawing to swap surface?)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
170 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
171 preTransform: result.vulkan.activePhysicalDevice.surfaceCapabilities.currentTransform,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
172 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
173 presentMode: result.vulkan.selectedPresentationMode,
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
174 clipped: VK_TRUE,
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
175 oldSwapchain: VkSwapchainKHR(0),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
176 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
177 checkVkResult vkCreateSwapchainKHR(result.vulkan.device, addr(swapchainCreateInfo), nil, addr(result.vulkan.swapChain))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
178 result.vulkan.swapImages = getSwapChainImages(result.vulkan.device, result.vulkan.swapChain)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
179
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
180 # setup swapchian image views
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
181 result.vulkan.swapImageViews = newSeq[VkImageView](result.vulkan.swapImages.len)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
182 for i, image in enumerate(result.vulkan.swapImages):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
183 var imageViewCreateInfo = VkImageViewCreateInfo(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
184 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
185 image: image,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
186 viewType: VK_IMAGE_VIEW_TYPE_2D,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
187 format: result.vulkan.selectedSurfaceFormat.format,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
188 components: VkComponentMapping(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
189 r: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
190 g: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
191 b: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
192 a: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
193 ),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
194 subresourceRange: VkImageSubresourceRange(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
195 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
196 baseMipLevel: 0,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
197 levelCount: 1,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
198 baseArrayLayer: 0,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
199 layerCount: 1,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
200 ),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
201 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
202 checkVkResult vkCreateImageView(result.vulkan.device, addr(imageViewCreateInfo), nil, addr(result.vulkan.swapImageViews[i]))
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
203
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
204 # init shader system
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
205 checkGlslangResult glslang_initialize_process()
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
206
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
207 # load shaders
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
208 result.pipeline.shaderStages.add(createShaderStage(result.vulkan.device, VK_SHADER_STAGE_VERTEX_BIT, vertexShaderCode))
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
209 result.pipeline.shaderStages.add(createShaderStage(result.vulkan.device, VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderCode))
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
210
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
211 # create graphis pipeline
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
212 var dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
213 var dynamicState = VkPipelineDynamicStateCreateInfo(
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
214 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
215 dynamicStateCount: uint32(dynamicStates.len),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
216 pDynamicStates: addr(dynamicStates[0]),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
217 )
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
218 var vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
219 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
220 vertexBindingDescriptionCount: 0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
221 pVertexBindingDescriptions: nil,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
222 vertexAttributeDescriptionCount: 0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
223 pVertexAttributeDescriptions: nil,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
224 )
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
225 var inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
226 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
227 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
228 primitiveRestartEnable: VK_FALSE,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
229 )
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
230
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
231 # setup viewport
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
232 var viewport = VkViewport(
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
233 x: 0.0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
234 y: 0.0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
235 width: (float) result.vulkan.selectedExtent.width,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
236 height: (float) result.vulkan.selectedExtent.height,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
237 minDepth: 0.0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
238 maxDepth: 1.0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
239 )
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
240 var scissor = VkRect2D(
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
241 offset: VkOffset2D(x: 0, y: 0),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
242 extent: result.vulkan.selectedExtent
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
243 )
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
244
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
245
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
246 proc fullThrottle*(engine: Engine) =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
247 var event: XEvent
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
248 while true:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
249 discard XNextEvent(engine.display, addr(event))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
250 case event.theType
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
251 of Expose:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
252 discard
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
253 of ClientMessage:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
254 if cast[Atom](event.xclient.data.l[0]) == deleteMessage:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
255 break
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
256 of KeyPress:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
257 let key = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
258 if key != 0:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
259 echo "Key ", key, " pressed"
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
260 of ButtonPressMask:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
261 echo "Mouse button ", event.xbutton.button, " pressed at ",
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
262 event.xbutton.x, ",", event.xbutton.y
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
263 else:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
264 discard
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
265
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
266 proc trash*(engine: Engine) =
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
267 for shaderStage in engine.pipeline.shaderStages:
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
268 vkDestroyShaderModule(engine.vulkan.device, shaderStage.module, nil);
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
269 glslang_finalize_process()
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
270 vkDestroySwapchainKHR(engine.vulkan.device, engine.vulkan.swapChain, nil);
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
271 vkDestroySurfaceKHR(engine.vulkan.instance, engine.vulkan.surface, nil);
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
272 vkDestroyDevice(engine.vulkan.device, nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
273 vkDestroyInstance(engine.vulkan.instance, nil)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
274 checkXlibResult engine.display.XDestroyWindow(engine.window)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
275 discard engine.display.XCloseDisplay() # always returns 0