annotate src/engine.nim @ 463:91544fc1afe5

did: hello world triangle, a bit of code organization
author Sam <sam@basx.dev>
date Mon, 19 Dec 2022 10:41:20 +0700
parents 0bc8643cfe25
children af9183acb173
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
1 import std/strformat
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/enumerate
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
3 import std/logging
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
4
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
5
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./vulkan
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
7 import ./vulkan_helpers
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
8 import ./xlib_helpers
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
9
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./glslang/glslang
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
11
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
12 const MAX_FRAMES_IN_FLIGHT = 2
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
13
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
14 var logger = newConsoleLogger()
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
15 addHandler(logger)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
16
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
17
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
18 var vertexShaderCode: string = """#version 450
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
19 layout(location = 0) out vec3 fragColor;
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
20 vec3 colors[3] = vec3[](
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
21 vec3(1.0, 0.0, 0.0),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
22 vec3(0.0, 1.0, 0.0),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
23 vec3(0.0, 0.0, 1.0)
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 vec2 positions[3] = vec2[](
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
26 vec2(0.0, -0.5),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
27 vec2(0.5, 0.5),
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
28 vec2(-0.5, 0.5)
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
29 );
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
30 void main() {
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
31 gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
32 fragColor = colors[gl_VertexIndex];
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
33 }"""
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
34
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
35 var fragmentShaderCode: string = """#version 450
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
36 layout(location = 0) out vec4 outColor;
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
37 layout(location = 0) in vec3 fragColor;
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
38 void main() {
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
39 outColor = vec4(fragColor, 1.0);
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
40 }"""
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
41
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
42 import
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
43 x11/xlib,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
44 x11/x
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
45
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
46 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
47
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
48 type
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
49 GraphicsPipeline = object
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
50 shaderStages*: seq[VkPipelineShaderStageCreateInfo]
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
51 layout*: VkPipelineLayout
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
52 renderPass*: VkRenderPass
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
53 pipeline*: VkPipeline
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
54 QueueFamily = object
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
55 properties*: VkQueueFamilyProperties
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
56 hasSurfaceSupport*: bool
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
57 PhysicalDevice = object
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
58 device*: VkPhysicalDevice
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
59 extensions*: seq[string]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
60 properties*: VkPhysicalDeviceProperties
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
61 features*: VkPhysicalDeviceFeatures
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
62 queueFamilies*: seq[QueueFamily]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
63 surfaceCapabilities*: VkSurfaceCapabilitiesKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
64 surfaceFormats: seq[VkSurfaceFormatKHR]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
65 presentModes: seq[VkPresentModeKHR]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
66 Vulkan* = object
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
67 debugMessenger: VkDebugUtilsMessengerEXT
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
68 instance*: VkInstance
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
69 deviceList*: seq[PhysicalDevice]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
70 activePhysicalDevice*: PhysicalDevice
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
71 graphicsQueueFamily*: uint32
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
72 graphicsQueue*: VkQueue
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
73 presentationQueueFamily*: uint32
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
74 presentationQueue*: VkQueue
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
75 device*: VkDevice
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
76 surface*: VkSurfaceKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
77 selectedSurfaceFormat: VkSurfaceFormatKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
78 selectedPresentationMode: VkPresentModeKHR
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
79 frameDimension: VkExtent2D
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
80 swapChain: VkSwapchainKHR
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
81 swapImages: seq[VkImage]
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
82 swapFramebuffers: seq[VkFramebuffer]
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
83 swapImageViews: seq[VkImageView]
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
84 pipeline*: GraphicsPipeline
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
85 commandPool*: VkCommandPool
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
86 commandBuffers*: array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
87 viewport*: VkViewport
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
88 scissor*: VkRect2D
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
89 imageAvailableSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
90 renderFinishedSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
91 inFlightFences*: array[MAX_FRAMES_IN_FLIGHT, VkFence]
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
92 Engine* = object
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
93 display*: PDisplay
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
94 window*: x.Window
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
95 vulkan*: Vulkan
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
96
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
97
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
98 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[PhysicalDevice] =
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
99 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance):
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
100 var device = PhysicalDevice(device: vulkanPhysicalDevice, extensions: getDeviceExtensions(vulkanPhysicalDevice))
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
101 vkGetPhysicalDeviceProperties(vulkanPhysicalDevice, addr(device.properties))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
102 vkGetPhysicalDeviceFeatures(vulkanPhysicalDevice, addr(device.features))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
103 checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanPhysicalDevice, surface, addr(device.surfaceCapabilities))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
104 device.surfaceFormats = getDeviceSurfaceFormats(vulkanPhysicalDevice, surface)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
105 device.presentModes = getDeviceSurfacePresentModes(vulkanPhysicalDevice, surface)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
106
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
107 debug(&"Physical device nr {int(vulkanPhysicalDevice)} {cleanString(device.properties.deviceName)}")
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
108 for i, queueFamilyProperty in enumerate(getQueueFamilies(vulkanPhysicalDevice)):
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
109 var hasSurfaceSupport: VkBool32 = VK_FALSE
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
110 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, uint32(i), surface, addr(hasSurfaceSupport))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
111 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty, hasSurfaceSupport: bool(hasSurfaceSupport)))
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
112 debug(&" Queue family {i} {queueFamilyProperty}")
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
113
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
114 result.add(device)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
115
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
116 proc filterForDevice(devices: seq[PhysicalDevice]): seq[(PhysicalDevice, uint32, uint32)] =
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
117 for device in devices:
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
118 if not (device.surfaceFormats.len > 0 and device.presentModes.len > 0 and "VK_KHR_swapchain" in device.extensions):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
119 continue
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
120 var graphicsQueueFamily = high(uint32)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
121 var presentationQueueFamily = high(uint32)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
122 for i, queueFamily in enumerate(device.queueFamilies):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
123 if queueFamily.hasSurfaceSupport:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
124 presentationQueueFamily = uint32(i)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
125 if bool(uint32(queueFamily.properties.queueFlags) and ord(VK_QUEUE_GRAPHICS_BIT)):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
126 graphicsQueueFamily = uint32(i)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
127 if graphicsQueueFamily != high(uint32) and presentationQueueFamily != high(uint32):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
128 result.add((device, graphicsQueueFamily, presentationQueueFamily))
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
129
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
130 for (device, graphicsQueueFamily, presentationQueueFamily) in result:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
131 debug(&"Viable device: {cleanString(device.properties.deviceName)} (graphics queue family {graphicsQueueFamily}, presentation queue family {presentationQueueFamily})")
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
132
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
133
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
134 proc getFrameDimension(display: PDisplay, window: Window, capabilities: VkSurfaceCapabilitiesKHR): VkExtent2D =
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
135 if capabilities.currentExtent.width != high(uint32):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
136 return capabilities.currentExtent
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
137 else:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
138 let (width, height) = xlibFramebufferSize(display, window)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
139 return VkExtent2D(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
140 width: min(max(uint32(width), capabilities.minImageExtent.width), capabilities.maxImageExtent.width),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
141 height: min(max(uint32(height), capabilities.minImageExtent.height), capabilities.maxImageExtent.height),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
142 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
143
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
144 proc createVulkanSurface(instance: VkInstance, display: PDisplay, window: Window): VkSurfaceKHR =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
145 var surfaceCreateInfo = VkXlibSurfaceCreateInfoKHR(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
146 sType: VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
147 dpy: display,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
148 window: window,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
149 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
150 checkVkResult vkCreateXlibSurfaceKHR(instance, addr(surfaceCreateInfo), nil, addr(result))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
151
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
152 proc setupVulkanDeviceAndQueues(instance: VkInstance, surface: VkSurfaceKHR): (PhysicalDevice, uint32, uint32, VkDevice, VkQueue, VkQueue) =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
153 let usableDevices = instance.getAllPhysicalDevices(surface).filterForDevice()
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
154 if len(usableDevices) == 0:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
155 raise newException(Exception, "No suitable graphics device found")
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
156 result[0] = usableDevices[0][0]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
157 result[1] = usableDevices[0][1]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
158 result[2] = usableDevices[0][2]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
159
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
160 debug(&"Chose device {cleanString(result[0].properties.deviceName)}")
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
161
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
162 (result[3], result[4], result[5]) = getVulcanDevice(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
163 result[0].device,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
164 result[0].features,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
165 result[1],
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
166 result[2],
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
167 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
168
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
169 proc igniteEngine*(): Engine =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
170
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
171 # init X11 window
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
172 (result.display, result.window) = xlibInit()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
173
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
174 # create vulkan instance
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
175 vkLoad1_0()
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
176 vkLoad1_1()
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
177 vkLoad1_2()
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
178 result.vulkan.instance = createVulkanInstance(VULKAN_VERSION)
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
179 when ENABLEVULKANVALIDATIONLAYERS:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
180 var createInfo = VkDebugUtilsMessengerCreateInfoEXT(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
181 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
182 messageSeverity: VkDebugUtilsMessageSeverityFlagsEXT(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
183 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) or
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
184 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) or
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
185 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
186 ),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
187 messageType: VkDebugUtilsMessageTypeFlagsEXT(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
188 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) or
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
189 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) or
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
190 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
191 ),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
192 pfnUserCallback: debugCallback,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
193 pUserData: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
194 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
195 checkVkResult vkCreateDebugUtilsMessengerEXT(result.vulkan.instance, addr(createInfo), nil, addr(result.vulkan.debugMessenger))
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
196
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
197 result.vulkan.surface = result.vulkan.instance.createVulkanSurface(result.display, result.window)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
198
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
199 (
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
200 result.vulkan.activePhysicalDevice,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
201 result.vulkan.graphicsQueueFamily,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
202 result.vulkan.presentationQueueFamily,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
203 result.vulkan.device,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
204 result.vulkan.graphicsQueue,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
205 result.vulkan.presentationQueue
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
206 ) = result.vulkan.instance.setupVulkanDeviceAndQueues(result.vulkan.surface)
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
207
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
208 # determine surface format for swapchain
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
209 let usableSurfaceFormats = filterForSurfaceFormat(result.vulkan.activePhysicalDevice.surfaceFormats)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
210 if len(usableSurfaceFormats) == 0:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
211 raise newException(Exception, "No suitable surface formats found")
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
212 result.vulkan.selectedSurfaceFormat = usableSurfaceFormats[0]
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
213 result.vulkan.selectedPresentationMode = getPresentMode(result.vulkan.activePhysicalDevice.presentModes)
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
214 result.vulkan.frameDimension = result.display.getFrameDimension(result.window, result.vulkan.activePhysicalDevice.surfaceCapabilities)
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
215
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
216 # setup swapchain
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
217 var swapchainCreateInfo = VkSwapchainCreateInfoKHR(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
218 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
219 surface: result.vulkan.surface,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
220 minImageCount: max(result.vulkan.activePhysicalDevice.surfaceCapabilities.minImageCount + 1, result.vulkan.activePhysicalDevice.surfaceCapabilities.maxImageCount),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
221 imageFormat: result.vulkan.selectedSurfaceFormat.format,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
222 imageColorSpace: result.vulkan.selectedSurfaceFormat.colorSpace,
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
223 imageExtent: result.vulkan.frameDimension,
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
224 imageArrayLayers: 1,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
225 imageUsage: VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
226 # 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
227 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
228 preTransform: result.vulkan.activePhysicalDevice.surfaceCapabilities.currentTransform,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
229 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
230 presentMode: result.vulkan.selectedPresentationMode,
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
231 clipped: VK_TRUE,
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
232 oldSwapchain: VkSwapchainKHR(0),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
233 )
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
234 checkVkResult result.vulkan.device.vkCreateSwapchainKHR(addr(swapchainCreateInfo), nil, addr(result.vulkan.swapChain))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
235 result.vulkan.swapImages = result.vulkan.device.getSwapChainImages(result.vulkan.swapChain)
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
236
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
237 # setup swapchian image views
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
238 result.vulkan.swapImageViews = newSeq[VkImageView](result.vulkan.swapImages.len)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
239 for i, image in enumerate(result.vulkan.swapImages):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
240 var imageViewCreateInfo = VkImageViewCreateInfo(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
241 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
242 image: image,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
243 viewType: VK_IMAGE_VIEW_TYPE_2D,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
244 format: result.vulkan.selectedSurfaceFormat.format,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
245 components: VkComponentMapping(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
246 r: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
247 g: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
248 b: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
249 a: VK_COMPONENT_SWIZZLE_IDENTITY,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
250 ),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
251 subresourceRange: VkImageSubresourceRange(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
252 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
253 baseMipLevel: 0,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
254 levelCount: 1,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
255 baseArrayLayer: 0,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
256 layerCount: 1,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
257 ),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
258 )
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
259 checkVkResult result.vulkan.device.vkCreateImageView(addr(imageViewCreateInfo), nil, addr(result.vulkan.swapImageViews[i]))
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
260
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
261 # init shader system
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
262 checkGlslangResult glslang_initialize_process()
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
263
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
264 # load shaders
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
265 result.vulkan.pipeline.shaderStages.add(result.vulkan.device.createShaderStage(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderCode))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
266 result.vulkan.pipeline.shaderStages.add(result.vulkan.device.createShaderStage(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderCode))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
267
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
268 # setup render passes
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
269 var
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
270 colorAttachment = VkAttachmentDescription(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
271 format: result.vulkan.selectedSurfaceFormat.format,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
272 samples: VK_SAMPLE_COUNT_1_BIT,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
273 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
274 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
275 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
276 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
277 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
278 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
279 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
280 colorAttachmentRef = VkAttachmentReference(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
281 attachment: 0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
282 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
283 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
284 subpass = VkSubpassDescription(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
285 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
286 colorAttachmentCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
287 pColorAttachments: addr(colorAttachmentRef)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
288 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
289 dependency = VkSubpassDependency(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
290 srcSubpass: VK_SUBPASS_EXTERNAL,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
291 dstSubpass: 0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
292 srcStageMask: VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
293 srcAccessMask: VkAccessFlags(0),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
294 dstStageMask: VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
295 dstAccessMask: VkAccessFlags(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
296 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
297 renderPassCreateInfo = VkRenderPassCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
298 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
299 attachmentCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
300 pAttachments: addr(colorAttachment),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
301 subpassCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
302 pSubpasses: addr(subpass),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
303 dependencyCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
304 pDependencies: addr(dependency),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
305 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
306 checkVkResult result.vulkan.device.vkCreateRenderPass(addr(renderPassCreateInfo), nil, addr(result.vulkan.pipeline.renderPass))
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
307
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
308 # create graphis pipeline
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
309
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
310 var
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
311 # define which parts can be dynamic (pipeline is fixed after setup)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
312 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
313 dynamicState = VkPipelineDynamicStateCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
314 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
315 dynamicStateCount: uint32(dynamicStates.len),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
316 pDynamicStates: addr(dynamicStates[0]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
317 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
318
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
319 # define input data format
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
320 vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
321 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
322 vertexBindingDescriptionCount: 0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
323 pVertexBindingDescriptions: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
324 vertexAttributeDescriptionCount: 0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
325 pVertexAttributeDescriptions: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
326 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
327 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
328 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
329 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
330 primitiveRestartEnable: VK_FALSE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
331 )
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
332
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
333 # setup viewport
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
334 result.vulkan.viewport = VkViewport(
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
335 x: 0.0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
336 y: 0.0,
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
337 width: (float) result.vulkan.frameDimension.width,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
338 height: (float) result.vulkan.frameDimension.height,
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
339 minDepth: 0.0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
340 maxDepth: 1.0,
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
341 )
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
342 result.vulkan.scissor = VkRect2D(
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
343 offset: VkOffset2D(x: 0, y: 0),
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
344 extent: result.vulkan.frameDimension
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
345 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
346 var viewportState = VkPipelineViewportStateCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
347 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
348 viewportCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
349 pViewports: addr(result.vulkan.viewport),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
350 scissorCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
351 pScissors: addr(result.vulkan.scissor),
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
352 )
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
353
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
354 # rasterizerization config
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
355 var
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
356 rasterizer = VkPipelineRasterizationStateCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
357 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
358 depthClampEnable: VK_FALSE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
359 rasterizerDiscardEnable: VK_FALSE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
360 polygonMode: VK_POLYGON_MODE_FILL,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
361 lineWidth: 1.0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
362 cullMode: VkCullModeFlags(VK_CULL_MODE_BACK_BIT),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
363 frontFace: VK_FRONT_FACE_CLOCKWISE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
364 depthBiasEnable: VK_FALSE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
365 depthBiasConstantFactor: 0.0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
366 depthBiasClamp: 0.0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
367 depthBiasSlopeFactor: 0.0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
368 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
369 multisampling = VkPipelineMultisampleStateCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
370 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
371 sampleShadingEnable: VK_FALSE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
372 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
373 minSampleShading: 1.0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
374 pSampleMask: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
375 alphaToCoverageEnable: VK_FALSE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
376 alphaToOneEnable: VK_FALSE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
377 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
378 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
379 colorWriteMask: VkColorComponentFlags(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
380 ord(VK_COLOR_COMPONENT_R_BIT) or
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
381 ord(VK_COLOR_COMPONENT_G_BIT) or
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
382 ord(VK_COLOR_COMPONENT_B_BIT) or
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
383 ord(VK_COLOR_COMPONENT_A_BIT)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
384 ),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
385 blendEnable: VK_TRUE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
386 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
387 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
388 colorBlendOp: VK_BLEND_OP_ADD,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
389 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
390 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
391 alphaBlendOp: VK_BLEND_OP_ADD,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
392 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
393 colorBlending = VkPipelineColorBlendStateCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
394 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
395 logicOpEnable: VK_TRUE,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
396 logicOp: VK_LOGIC_OP_COPY,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
397 attachmentCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
398 pAttachments: addr(colorBlendAttachment),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
399 blendConstants: [0.0'f, 0.0'f, 0.0'f, 0.0'f],
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
400 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
401
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
402 # create pipeline
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
403 pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
404 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
405 setLayoutCount: 0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
406 pSetLayouts: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
407 pushConstantRangeCount: 0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
408 pPushConstantRanges: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
409 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
410 checkVkResult result.vulkan.device.vkCreatePipelineLayout(addr(pipelineLayoutInfo), nil, addr(result.vulkan.pipeline.layout))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
411
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
412 var pipelineInfo = VkGraphicsPipelineCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
413 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
414 stageCount: 2,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
415 pStages: addr(result.vulkan.pipeline.shaderStages[0]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
416 pVertexInputState: addr(vertexInputInfo),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
417 pInputAssemblyState: addr(inputAssembly),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
418 pViewportState: addr(viewportState),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
419 pRasterizationState: addr(rasterizer),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
420 pMultisampleState: addr(multisampling),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
421 pDepthStencilState: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
422 pColorBlendState: addr(colorBlending),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
423 pDynamicState: addr(dynamicState),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
424 layout: result.vulkan.pipeline.layout,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
425 renderPass: result.vulkan.pipeline.renderPass,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
426 subpass: 0,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
427 basePipelineHandle: VkPipeline(0),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
428 basePipelineIndex: -1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
429 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
430 checkVkResult result.vulkan.device.vkCreateGraphicsPipelines(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
431 VkPipelineCache(0),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
432 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
433 addr(pipelineInfo),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
434 nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
435 addr(result.vulkan.pipeline.pipeline)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
436 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
437
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
438 # set up framebuffers
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
439 result.vulkan.swapFramebuffers = newSeq[VkFramebuffer](result.vulkan.swapImages.len)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
440
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
441 for i, imageview in enumerate(result.vulkan.swapImageViews):
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
442 var framebufferInfo = VkFramebufferCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
443 sType: VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
444 renderPass: result.vulkan.pipeline.renderPass,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
445 attachmentCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
446 pAttachments: addr(result.vulkan.swapImageViews[i]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
447 width: result.vulkan.frameDimension.width,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
448 height: result.vulkan.frameDimension.height,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
449 layers: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
450 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
451 checkVkResult result.vulkan.device.vkCreateFramebuffer(addr(framebufferInfo), nil, addr(result.vulkan.swapFramebuffers[i]))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
452
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
453 # set up command buffer
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
454 var poolInfo = VkCommandPoolCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
455 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
456 flags: VkCommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
457 queueFamilyIndex: result.vulkan.graphicsQueueFamily,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
458 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
459 checkVkResult result.vulkan.device.vkCreateCommandPool(addr(poolInfo), nil, addr(result.vulkan.commandPool))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
460
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
461 var allocInfo = VkCommandBufferAllocateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
462 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
463 commandPool: result.vulkan.commandPool,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
464 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
465 commandBufferCount: result.vulkan.commandBuffers.len.uint32,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
466 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
467 checkVkResult result.vulkan.device.vkAllocateCommandBuffers(addr(allocInfo), addr(result.vulkan.commandBuffers[0]))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
468
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
469 # create semaphores for syncing rendering
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
470 var semaphoreInfo = VkSemaphoreCreateInfo(sType: VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
471 var fenceInfo = VkFenceCreateInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
472 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
473 flags: VkFenceCreateFlags(VK_FENCE_CREATE_SIGNALED_BIT)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
474 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
475 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
476 checkVkResult result.vulkan.device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr(result.vulkan.imageAvailableSemaphores[i]))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
477 checkVkResult result.vulkan.device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr(result.vulkan.renderFinishedSemaphores[i]))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
478 checkVkResult result.vulkan.device.vkCreateFence(addr(fenceInfo), nil, addr(result.vulkan.inFlightFences[i]))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
479
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
480
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
481 proc recordCommandBuffer(vulkan: var Vulkan, commandBuffer: VkCommandBuffer, imageIndex: uint32) =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
482 var beginInfo = VkCommandBufferBeginInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
483 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
484 pInheritanceInfo: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
485 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
486 checkVkResult commandBuffer.vkBeginCommandBuffer(addr(beginInfo))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
487
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
488 var
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
489 clearColor = VkClearValue(color: VkClearColorValue(float32: [0.2'f, 0.2'f, 0.2'f, 1.0'f]))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
490 renderPassInfo = VkRenderPassBeginInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
491 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
492 renderPass: vulkan.pipeline.renderPass,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
493 framebuffer: vulkan.swapFramebuffers[imageIndex],
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
494 renderArea: VkRect2D(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
495 offset: VkOffset2D(x: 0, y: 0),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
496 extent: vulkan.frameDimension,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
497 ),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
498 clearValueCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
499 pClearValues: addr(clearColor),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
500 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
501 commandBuffer.vkCmdBeginRenderPass(addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
502 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, vulkan.pipeline.pipeline)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
503
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
504 commandBuffer.vkCmdSetViewport(firstViewport=0, viewportCount=1, addr(vulkan.viewport))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
505 commandBuffer.vkCmdSetScissor(firstScissor=0, scissorCount=1, addr(vulkan.scissor))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
506 commandBuffer.vkCmdDraw(vertexCount=3, instanceCount=1, firstVertex=0, firstInstance=0)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
507 commandBuffer.vkCmdEndRenderPass()
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
508 checkVkResult commandBuffer.vkEndCommandBuffer()
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
509
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
510 proc drawFrame(vulkan: var Vulkan, currentFrame: int) =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
511 checkVkResult vulkan.device.vkWaitForFences(1, addr(vulkan.inFlightFences[currentFrame]), VK_TRUE, high(uint64))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
512 checkVkResult vulkan.device.vkResetFences(1, addr(vulkan.inFlightFences[currentFrame]))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
513 var bufferImageIndex: uint32
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
514 checkVkResult vulkan.device.vkAcquireNextImageKHR(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
515 vulkan.swapChain,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
516 high(uint64),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
517 vulkan.imageAvailableSemaphores[currentFrame],
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
518 VkFence(0),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
519 addr(bufferImageIndex)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
520 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
521
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
522 checkVkResult vkResetCommandBuffer(vulkan.commandBuffers[currentFrame], VkCommandBufferResetFlags(0))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
523 recordCommandBuffer(vulkan, vulkan.commandBuffers[currentFrame], bufferImageIndex)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
524 var
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
525 waitSemaphores = [vulkan.imageAvailableSemaphores[currentFrame]]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
526 waitStages = [VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
527 signalSemaphores = [vulkan.renderFinishedSemaphores[currentFrame]]
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
528 submitInfo = VkSubmitInfo(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
529 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
530 waitSemaphoreCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
531 pWaitSemaphores: addr(waitSemaphores[0]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
532 pWaitDstStageMask: addr(waitStages[0]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
533 commandBufferCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
534 pCommandBuffers: addr(vulkan.commandBuffers[currentFrame]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
535 signalSemaphoreCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
536 pSignalSemaphores: addr(signalSemaphores[0]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
537 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
538 checkVkResult vkQueueSubmit(vulkan.graphicsQueue, 1, addr(submitInfo), vulkan.inFlightFences[currentFrame])
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
539
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
540 var presentInfo = VkPresentInfoKHR(
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
541 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
542 waitSemaphoreCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
543 pWaitSemaphores: addr(signalSemaphores[0]),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
544 swapchainCount: 1,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
545 pSwapchains: addr(vulkan.swapChain),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
546 pImageIndices: addr(bufferImageIndex),
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
547 pResults: nil,
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
548 )
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
549 checkVkResult vkQueuePresentKHR(vulkan.presentationQueue, addr(presentInfo))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
550
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
551
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
552 proc fullThrottle*(engine: var Engine) =
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
553 var
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
554 event: XEvent
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
555 killed = false
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
556 currentFrame = 0
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
557
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
558 while not killed:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
559 while engine.display.XPending() > 0 and not killed:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
560 discard engine.display.XNextEvent(addr(event))
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
561 case event.theType
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
562 of ClientMessage:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
563 if cast[Atom](event.xclient.data.l[0]) == deleteMessage:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
564 killed = true
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
565 of KeyPress:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
566 let key = XLookupKeysym(cast[PXKeyEvent](addr(event)), 0)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
567 if key == XK_Escape:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
568 killed = true
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
569 else:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
570 discard
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
571 drawFrame(engine.vulkan, currentFrame)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
572 currentFrame = (currentFrame + 1) mod MAX_FRAMES_IN_FLIGHT;
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
573 checkVkResult engine.vulkan.device.vkDeviceWaitIdle()
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
574
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
575
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
576 proc trash*(engine: Engine) =
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
577 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
578 engine.vulkan.device.vkDestroySemaphore(engine.vulkan.imageAvailableSemaphores[i], nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
579 engine.vulkan.device.vkDestroySemaphore(engine.vulkan.renderFinishedSemaphores[i], nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
580 engine.vulkan.device.vkDestroyFence(engine.vulkan.inFlightFences[i], nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
581
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
582 engine.vulkan.device.vkDestroyCommandPool(engine.vulkan.commandPool, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
583 for framebuffer in engine.vulkan.swapFramebuffers:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
584 engine.vulkan.device.vkDestroyFramebuffer(framebuffer, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
585
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
586 engine.vulkan.device.vkDestroyPipeline(engine.vulkan.pipeline.pipeline, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
587 engine.vulkan.device.vkDestroyPipelineLayout(engine.vulkan.pipeline.layout, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
588 engine.vulkan.device.vkDestroyRenderPass(engine.vulkan.pipeline.renderPass, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
589
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
590 for shaderStage in engine.vulkan.pipeline.shaderStages:
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
591 engine.vulkan.device.vkDestroyShaderModule(shaderStage.module, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
592
462
0bc8643cfe25 add: more steps in setup
Sam <sam@basx.dev>
parents: 461
diff changeset
593 glslang_finalize_process()
463
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
594 engine.vulkan.device.vkDestroySwapchainKHR(engine.vulkan.swapChain, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
595 engine.vulkan.instance.vkDestroySurfaceKHR(engine.vulkan.surface, nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
596 engine.vulkan.device.vkDestroyDevice(nil)
91544fc1afe5 did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 462
diff changeset
597 engine.vulkan.instance.vkDestroyInstance(nil)
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
598 checkXlibResult engine.display.XDestroyWindow(engine.window)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
599 discard engine.display.XCloseDisplay() # always returns 0