annotate src/engine.nim @ 0:5daf3f236d87

add: initial version
author Sam <sam@basx.dev>
date Wed, 14 Dec 2022 00:49:35 +0700
parents
children bb2a7d3a7003
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 import ./glslang/glslang_c_shader_types
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
9
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
10
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
11 import
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
12 x11/xlib,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
13 x11/x
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
14
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
15 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
16
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
17 type
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
18 QueueFamily = object
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
19 properties*: VkQueueFamilyProperties
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
20 hasSurfaceSupport*: bool
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
21 PhyscialDevice = object
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
22 device*: VkPhysicalDevice
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
23 extensions*: seq[string]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
24 properties*: VkPhysicalDeviceProperties
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
25 features*: VkPhysicalDeviceFeatures
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
26 queueFamilies*: seq[QueueFamily]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
27 surfaceCapabilities*: VkSurfaceCapabilitiesKHR
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
28 surfaceFormats: seq[VkSurfaceFormatKHR]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
29 presentModes: seq[VkPresentModeKHR]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
30 Vulkan* = object
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
31 instance*: VkInstance
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
32 deviceList*: seq[PhyscialDevice]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
33 activePhysicalDevice*: PhyscialDevice
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
34 activeQueueFamily*: uint32
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
35 device*: VkDevice
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
36 presentationQueue*: VkQueue
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
37 surface*: VkSurfaceKHR
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
38 selectedSurfaceFormat: VkSurfaceFormatKHR
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
39 selectedPresentationMode: VkPresentModeKHR
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
40 selectedExtent: VkExtent2D
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
41 swapChain: VkSwapchainKHR
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
42 swapImages: seq[VkImage]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
43 swapImageViews: seq[VkImageView]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
44 Engine* = object
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
45 display*: PDisplay
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
46 window*: x.Window
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
47 vulkan*: Vulkan
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
48
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
49
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
50 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[PhyscialDevice] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
51 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
52 var device = PhyscialDevice(device: vulkanPhysicalDevice, extensions: getDeviceExtensions(vulkanPhysicalDevice))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
53 vkGetPhysicalDeviceProperties(vulkanPhysicalDevice, addr(device.properties))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
54 vkGetPhysicalDeviceFeatures(vulkanPhysicalDevice, addr(device.features))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
55 checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanPhysicalDevice, surface, addr(device.surfaceCapabilities))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
56 device.surfaceFormats = getDeviceSurfaceFormats(vulkanPhysicalDevice, surface)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
57 device.presentModes = getDeviceSurfacePresentModes(vulkanPhysicalDevice, surface)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
58
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
59 for i, queueFamilyProperty in enumerate(getQueueFamilies(vulkanPhysicalDevice)):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
60 var hasSurfaceSupport: VkBool32 = VkBool32(false)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
61 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, uint32(i), surface, addr(hasSurfaceSupport))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
62 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty, hasSurfaceSupport: bool(hasSurfaceSupport)))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
63
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
64 result.add(device)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
65
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
66 proc filterForDevice(devices: seq[PhyscialDevice]): seq[(PhyscialDevice, uint32)] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
67 for device in devices:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
68 if "VK_KHR_swapchain" in device.extensions:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
69 for i, queueFamily in enumerate(device.queueFamilies):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
70 let hasGraphics = bool(uint32(queueFamily.properties.queueFlags) and ord(VK_QUEUE_GRAPHICS_BIT))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
71 if (
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
72 queueFamily.hasSurfaceSupport and
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
73 hasGraphics and
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
74 device.surfaceFormats.len > 0 and
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
75 device.presentModes.len > 0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
76 ):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
77 result.add((device, uint32(i)))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
78
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
79 proc filterForSurfaceFormat(formats: seq[VkSurfaceFormatKHR]): seq[VkSurfaceFormatKHR] =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
80 for format in formats:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
81 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
82 result.add(format)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
83
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
84 proc getSwapExtent(display: PDisplay, window: Window, capabilities: VkSurfaceCapabilitiesKHR): VkExtent2D =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
85 if capabilities.currentExtent.width != high(uint32):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
86 return capabilities.currentExtent
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
87 else:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
88 let (width, height) = xlibFramebufferSize(display, window)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
89 return VkExtent2D(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
90 width: min(max(uint32(width), capabilities.minImageExtent.width), capabilities.maxImageExtent.width),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
91 height: min(max(uint32(height), capabilities.minImageExtent.height), capabilities.maxImageExtent.height),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
92 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
93
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
94 proc igniteEngine*(): Engine =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
95 vkLoad1_0()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
96 vkLoad1_1()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
97 vkLoad1_2()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
98
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
99 # init X11 window
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
100 (result.display, result.window) = xlibInit()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
101
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
102 # create vulkan instance
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
103 result.vulkan.instance = createVulkanInstance(VULKAN_VERSION)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
104
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
105 # create vulkan-X11 surface
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
106 var surfaceCreateInfo = VkXlibSurfaceCreateInfoKHR(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
107 sType: VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
108 dpy: result.display,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
109 window: result.window,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
110 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
111 checkVkResult vkCreateXlibSurfaceKHR(result.vulkan.instance, addr(surfaceCreateInfo), nil, addr(result.vulkan.surface))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
112
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
113 # determine device and queue to use and instantiate
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
114 result.vulkan.deviceList = result.vulkan.instance.getAllPhysicalDevices(result.vulkan.surface)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
115 let usableDevices = result.vulkan.deviceList.filterForDevice()
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
116 if len(usableDevices) == 0:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
117 raise newException(Exception, "No suitable graphics device found")
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
118 (result.vulkan.activePhysicalDevice, result.vulkan.activeQueueFamily) = usableDevices[0]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
119
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
120 (result.vulkan.device, result.vulkan.presentationQueue) = getVulcanDevice(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
121 result.vulkan.activePhysicalDevice.device,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
122 result.vulkan.activePhysicalDevice.features,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
123 result.vulkan.activeQueueFamily
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
124 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
125
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
126 # determine surface format for swapchain
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
127 let usableSurfaceFormats = filterForSurfaceFormat(result.vulkan.activePhysicalDevice.surfaceFormats)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
128 if len(usableSurfaceFormats) == 0:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
129 raise newException(Exception, "No suitable surface formats found")
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
130 result.vulkan.selectedSurfaceFormat = usableSurfaceFormats[0]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
131 result.vulkan.selectedPresentationMode = getPresentMode(result.vulkan.activePhysicalDevice.presentModes)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
132 result.vulkan.selectedExtent = getSwapExtent(result.display, result.window, result.vulkan.activePhysicalDevice.surfaceCapabilities)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
133
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
134 # setup swapchain
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
135 var swapchainCreateInfo = VkSwapchainCreateInfoKHR(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
136 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
137 surface: result.vulkan.surface,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
138 minImageCount: max(result.vulkan.activePhysicalDevice.surfaceCapabilities.minImageCount + 1, result.vulkan.activePhysicalDevice.surfaceCapabilities.maxImageCount),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
139 imageFormat: result.vulkan.selectedSurfaceFormat.format,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
140 imageColorSpace: result.vulkan.selectedSurfaceFormat.colorSpace,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
141 imageExtent: result.vulkan.selectedExtent,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
142 imageArrayLayers: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
143 imageUsage: VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
144 # 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
145 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
146 preTransform: result.vulkan.activePhysicalDevice.surfaceCapabilities.currentTransform,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
147 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
148 presentMode: result.vulkan.selectedPresentationMode,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
149 clipped: VkBool32(true),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
150 oldSwapchain: VkSwapchainKHR(0),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
151 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
152 checkVkResult vkCreateSwapchainKHR(result.vulkan.device, addr(swapchainCreateInfo), nil, addr(result.vulkan.swapChain))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
153 result.vulkan.swapImages = getSwapChainImages(result.vulkan.device, result.vulkan.swapChain)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
154
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
155 # setup swapchian image views
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
156 result.vulkan.swapImageViews = newSeq[VkImageView](result.vulkan.swapImages.len)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
157 for i, image in enumerate(result.vulkan.swapImages):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
158 var imageViewCreateInfo = VkImageViewCreateInfo(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
159 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
160 image: image,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
161 viewType: VK_IMAGE_VIEW_TYPE_2D,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
162 format: result.vulkan.selectedSurfaceFormat.format,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
163 components: VkComponentMapping(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
164 r: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
165 g: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
166 b: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
167 a: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
168 ),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
169 subresourceRange: VkImageSubresourceRange(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
170 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
171 baseMipLevel: 0,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
172 levelCount: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
173 baseArrayLayer: 0,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
174 layerCount: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
175 ),
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 vkCreateImageView(result.vulkan.device, addr(imageViewCreateInfo), nil, addr(result.vulkan.swapImageViews[i]))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
178 echo compileShaderToSPIRV_Vulkan(GLSLANG_STAGE_VERTEX, """#version 450
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
179 vec2 positions[3] = vec2[](
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
180 vec2(0.0, -0.5),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
181 vec2(0.5, 0.5),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
182 vec2(-0.5, 0.5)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
183 );
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
184 void main() {
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
185 gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
186 }""", "<memory-shader>")
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
187
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
188
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
189 proc fullThrottle*(engine: Engine) =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
190 var event: XEvent
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
191 while true:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
192 discard XNextEvent(engine.display, addr(event))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
193 case event.theType
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
194 of Expose:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
195 discard
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
196 of ClientMessage:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
197 if cast[Atom](event.xclient.data.l[0]) == deleteMessage:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
198 break
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
199 of KeyPress:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
200 let key = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
201 if key != 0:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
202 echo "Key ", key, " pressed"
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
203 of ButtonPressMask:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
204 echo "Mouse button ", event.xbutton.button, " pressed at ",
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
205 event.xbutton.x, ",", event.xbutton.y
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
206 else:
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
207 discard
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
208
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
209 proc trash*(engine: Engine) =
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
210 vkDestroySwapchainKHR(engine.vulkan.device, engine.vulkan.swapChain, nil);
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
211 vkDestroySurfaceKHR(engine.vulkan.instance, engine.vulkan.surface, nil);
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
212 vkDestroyDevice(engine.vulkan.device, nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
213 vkDestroyInstance(engine.vulkan.instance, nil)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
214 checkXlibResult engine.display.XDestroyWindow(engine.window)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
215 discard engine.display.XCloseDisplay() # always returns 0