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