annotate src/engine.nim @ 18:90e117952f74

add: basic vertex buffer functionality
author Sam <sam@basx.dev>
date Thu, 05 Jan 2023 01:16:48 +0700
parents b40466fa446a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
1 import std/typetraits
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
2 import std/strformat
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/enumerate
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
4 import std/logging
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
5
0
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 ./vulkan
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
8 import ./vulkan_helpers
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
9 import ./window
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
10 import ./events
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
11 import ./math/vector
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
12 import ./shader
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
13 import ./vertex
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
14 import ./buffer
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
15
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
16 import ./glslang/glslang
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
17
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
18 const MAX_FRAMES_IN_FLIGHT = 2
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 8
diff changeset
19 const DEBUG_LOG = not defined(release)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
20
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
21 var logger = newConsoleLogger()
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
22 addHandler(logger)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
23
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
24
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
25 type
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
26 MyVertex = object
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
27 position: VertexAttribute[Vec2[float32]]
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
28 color: VertexAttribute[Vec3[float32]]
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
29
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
30 var vertices = (
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
31 [
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
32 Vec2([-0.5'f32, -0.5'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
33 Vec2([ 0.5'f32, 0.5'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
34 Vec2([-0.5'f32, 0.5'f32]),
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
35
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
36 Vec2([ 0.0'f32, -0.7'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
37 Vec2([ 0.6'f32, 0.1'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
38 Vec2([ 0.3'f32, 0.4'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
39 ],
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
40 [
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
41 Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
42 Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
43 Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
44
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
45 Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
46 Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
47 Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
48 ]
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
49 )
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
50
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
51 var vertexShaderCode = """
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
52 #version 450
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
53
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
54 layout(location = 0) in vec2 inPosition;
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
55 layout(location = 1) in vec3 inColor;
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
56
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
57 layout(location = 0) out vec3 fragColor;
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
58
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
59 void main() {
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
60 gl_Position = vec4(inPosition, 0.0, 1.0);
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
61 fragColor = inColor;
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
62 }
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
63 """
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
64
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
65 var fragmentShaderCode = """#version 450
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
66 layout(location = 0) out vec4 outColor;
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
67 layout(location = 0) in vec3 fragColor;
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
68 void main() {
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
69 outColor = vec4(fragColor, 1.0);
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
70 }"""
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
71
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
72 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
73
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
74 type
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
75 Device = object
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
76 device: VkDevice
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
77 physicalDevice: PhysicalDevice
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
78 graphicsQueueFamily: uint32
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
79 presentationQueueFamily: uint32
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
80 graphicsQueue: VkQueue
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
81 presentationQueue: VkQueue
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
82 Swapchain = object
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
83 swapchain: VkSwapchainKHR
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
84 images: seq[VkImage]
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
85 imageviews: seq[VkImageView]
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
86 RenderPipeline = object
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
87 shaders*: seq[ShaderProgram]
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
88 layout*: VkPipelineLayout
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
89 pipeline*: VkPipeline
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
90 QueueFamily = object
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
91 properties*: VkQueueFamilyProperties
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
92 hasSurfaceSupport*: bool
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
93 PhysicalDevice = object
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
94 device*: VkPhysicalDevice
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
95 extensions*: seq[string]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
96 properties*: VkPhysicalDeviceProperties
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
97 features*: VkPhysicalDeviceFeatures
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
98 queueFamilies*: seq[QueueFamily]
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
99 formats: seq[VkSurfaceFormatKHR]
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
100 presentModes: seq[VkPresentModeKHR]
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
101 Vulkan* = object
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
102 debugMessenger: VkDebugUtilsMessengerEXT
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
103 instance*: VkInstance
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
104 deviceList*: seq[PhysicalDevice]
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
105 device*: Device
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
106 surface*: VkSurfaceKHR
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
107 surfaceFormat: VkSurfaceFormatKHR
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
108 frameDimension: VkExtent2D
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
109 swapchain: Swapchain
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
110 framebuffers: seq[VkFramebuffer]
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
111 renderPass*: VkRenderPass
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
112 pipeline*: RenderPipeline
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
113 commandPool*: VkCommandPool
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
114 commandBuffers*: array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer]
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
115 imageAvailableSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore]
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
116 renderFinishedSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore]
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
117 inFlightFences*: array[MAX_FRAMES_IN_FLIGHT, VkFence]
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
118 bufferA*: Buffer
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
119 bufferB*: Buffer
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
120 Engine* = object
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
121 vulkan*: Vulkan
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
122 window: NativeWindow
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
123
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
124 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[PhysicalDevice] =
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
125 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance):
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
126 var device = PhysicalDevice(device: vulkanPhysicalDevice, extensions: getDeviceExtensions(vulkanPhysicalDevice))
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
127 vkGetPhysicalDeviceProperties(vulkanPhysicalDevice, addr(device.properties))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
128 vkGetPhysicalDeviceFeatures(vulkanPhysicalDevice, addr(device.features))
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
129 device.formats = vulkanPhysicalDevice.getDeviceSurfaceFormats(surface)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
130 device.presentModes = vulkanPhysicalDevice.getDeviceSurfacePresentModes(surface)
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
131
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
132 debug(&"Physical device nr {int(vulkanPhysicalDevice)} {cleanString(device.properties.deviceName)}")
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
133 for i, queueFamilyProperty in enumerate(getQueueFamilies(vulkanPhysicalDevice)):
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
134 var hasSurfaceSupport: VkBool32 = VK_FALSE
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
135 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice, uint32(i), surface, addr(hasSurfaceSupport))
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
136 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty, hasSurfaceSupport: bool(hasSurfaceSupport)))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
137 debug(&" Queue family {i} {queueFamilyProperty}")
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
138
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
139 result.add(device)
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
140
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
141 proc filterForDevice(devices: seq[PhysicalDevice]): seq[(PhysicalDevice, uint32, uint32)] =
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
142 for device in devices:
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
143 if not (device.formats.len > 0 and device.presentModes.len > 0 and "VK_KHR_swapchain" in device.extensions):
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
144 continue
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
145 var graphicsQueueFamily = high(uint32)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
146 var presentationQueueFamily = high(uint32)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
147 for i, queueFamily in enumerate(device.queueFamilies):
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
148 if queueFamily.hasSurfaceSupport:
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
149 presentationQueueFamily = uint32(i)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
150 if bool(uint32(queueFamily.properties.queueFlags) and ord(VK_QUEUE_GRAPHICS_BIT)):
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
151 graphicsQueueFamily = uint32(i)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
152 if graphicsQueueFamily != high(uint32) and presentationQueueFamily != high(uint32):
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
153 result.add((device, graphicsQueueFamily, presentationQueueFamily))
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
154
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
155 for (device, graphicsQueueFamily, presentationQueueFamily) in result:
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
156 debug(&"Viable device: {cleanString(device.properties.deviceName)} (graphics queue family {graphicsQueueFamily}, presentation queue family {presentationQueueFamily})")
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
157
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
158
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
159 proc getFrameDimension(window: NativeWindow, device: VkPhysicalDevice, surface: VkSurfaceKHR): VkExtent2D =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
160 let capabilities = device.getSurfaceCapabilities(surface)
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
161 if capabilities.currentExtent.width != high(uint32):
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
162 return capabilities.currentExtent
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
163 else:
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
164 let (width, height) = window.size()
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
165 return VkExtent2D(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
166 width: min(max(uint32(width), capabilities.minImageExtent.width), capabilities.maxImageExtent.width),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
167 height: min(max(uint32(height), capabilities.minImageExtent.height), capabilities.maxImageExtent.height),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
168 )
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
169
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 8
diff changeset
170 when DEBUG_LOG:
8
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
171 proc setupDebugLog(instance: VkInstance): VkDebugUtilsMessengerEXT =
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
172 var createInfo = VkDebugUtilsMessengerCreateInfoEXT(
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
173 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
174 messageSeverity: VkDebugUtilsMessageSeverityFlagsEXT(
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
175 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) or
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
176 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) or
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
177 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
178 ),
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
179 messageType: VkDebugUtilsMessageTypeFlagsEXT(
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
180 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) or
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
181 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) or
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
182 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT)
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
183 ),
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
184 pfnUserCallback: debugCallback,
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
185 pUserData: nil,
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
186 )
1134f41a49e9 add: a few missing changes
Sam <sam@basx.dev>
parents: 5
diff changeset
187 checkVkResult instance.vkCreateDebugUtilsMessengerEXT(addr(createInfo), nil, addr(result))
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
188
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
189 proc setupVulkanDeviceAndQueues(instance: VkInstance, surface: VkSurfaceKHR): Device =
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
190 let usableDevices = instance.getAllPhysicalDevices(surface).filterForDevice()
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
191 if len(usableDevices) == 0:
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
192 raise newException(Exception, "No suitable graphics device found")
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
193 result.physicalDevice = usableDevices[0][0]
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
194 result.graphicsQueueFamily = usableDevices[0][1]
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
195 result.presentationQueueFamily = usableDevices[0][2]
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
196
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
197 debug(&"Chose device {cleanString(result.physicalDevice.properties.deviceName)}")
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
198
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
199 (result.device, result.graphicsQueue, result.presentationQueue) = getVulcanDevice(
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
200 result.physicalDevice.device,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
201 result.physicalDevice.features,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
202 result.graphicsQueueFamily,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
203 result.presentationQueueFamily,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
204 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
205
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
206 proc setupSwapChain(device: VkDevice, physicalDevice: PhysicalDevice, surface: VkSurfaceKHR, dimension: VkExtent2D, surfaceFormat: VkSurfaceFormatKHR): Swapchain =
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
207
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
208 let capabilities = physicalDevice.device.getSurfaceCapabilities(surface)
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
209 var selectedPresentationMode = getPresentMode(physicalDevice.presentModes)
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
210 # setup swapchain
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
211 var swapchainCreateInfo = VkSwapchainCreateInfoKHR(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
212 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
213 surface: surface,
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
214 minImageCount: max(capabilities.minImageCount + 1, capabilities.maxImageCount),
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
215 imageFormat: surfaceFormat.format,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
216 imageColorSpace: surfaceFormat.colorSpace,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
217 imageExtent: dimension,
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
218 imageArrayLayers: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
219 imageUsage: VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
220 # 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
221 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE,
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
222 preTransform: capabilities.currentTransform,
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
223 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
224 presentMode: selectedPresentationMode,
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
225 clipped: VK_TRUE,
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
226 oldSwapchain: VkSwapchainKHR(0),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
227 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
228 checkVkResult device.vkCreateSwapchainKHR(addr(swapchainCreateInfo), nil, addr(result.swapchain))
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
229 result.images = device.getSwapChainImages(result.swapchain)
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
230
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
231 # setup swapchian image views
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
232
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
233 result.imageviews = newSeq[VkImageView](result.images.len)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
234 for i, image in enumerate(result.images):
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
235 var imageViewCreateInfo = VkImageViewCreateInfo(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
236 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
237 image: image,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
238 viewType: VK_IMAGE_VIEW_TYPE_2D,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
239 format: surfaceFormat.format,
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
240 components: VkComponentMapping(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
241 r: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
242 g: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
243 b: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
244 a: VK_COMPONENT_SWIZZLE_IDENTITY,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
245 ),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
246 subresourceRange: VkImageSubresourceRange(
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
247 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
248 baseMipLevel: 0,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
249 levelCount: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
250 baseArrayLayer: 0,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
251 layerCount: 1,
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
252 ),
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
253 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
254 checkVkResult device.vkCreateImageView(addr(imageViewCreateInfo), nil, addr(result.imageviews[i]))
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
255
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
256 proc setupRenderPass(device: VkDevice, format: VkFormat): VkRenderPass =
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
257 var
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
258 colorAttachment = VkAttachmentDescription(
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
259 format: format,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
260 samples: VK_SAMPLE_COUNT_1_BIT,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
261 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
262 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
263 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
264 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
265 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
266 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
267 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
268 colorAttachmentRef = VkAttachmentReference(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
269 attachment: 0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
270 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
271 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
272 subpass = VkSubpassDescription(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
273 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
274 colorAttachmentCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
275 pColorAttachments: addr(colorAttachmentRef)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
276 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
277 dependency = VkSubpassDependency(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
278 srcSubpass: VK_SUBPASS_EXTERNAL,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
279 dstSubpass: 0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
280 srcStageMask: VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
281 srcAccessMask: VkAccessFlags(0),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
282 dstStageMask: VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
283 dstAccessMask: VkAccessFlags(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
284 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
285 renderPassCreateInfo = VkRenderPassCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
286 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
287 attachmentCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
288 pAttachments: addr(colorAttachment),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
289 subpassCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
290 pSubpasses: addr(subpass),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
291 dependencyCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
292 pDependencies: addr(dependency),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
293 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
294 checkVkResult device.vkCreateRenderPass(addr(renderPassCreateInfo), nil, addr(result))
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
295
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
296 proc setupRenderPipeline(device: VkDevice, frameDimension: VkExtent2D, renderPass: VkRenderPass): RenderPipeline =
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
297 # load shaders
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
298 result.shaders.add(device.initShaderProgram(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderCode))
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
299 result.shaders.add(device.initShaderProgram(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderCode))
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
300
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
301 var
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
302 # define which parts can be dynamic (pipeline is fixed after setup)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
303 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
304 dynamicState = VkPipelineDynamicStateCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
305 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
306 dynamicStateCount: uint32(dynamicStates.len),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
307 pDynamicStates: addr(dynamicStates[0]),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
308 )
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
309 vertexbindings = generateInputVertexBinding[MyVertex]()
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
310 attributebindings = generateInputAttributeBinding[MyVertex]()
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
311
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
312 # define input data format
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
313 vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
314 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
315 vertexBindingDescriptionCount: uint32(vertexbindings.len),
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
316 pVertexBindingDescriptions: addr(vertexbindings[0]),
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
317 vertexAttributeDescriptionCount: uint32(attributebindings.len),
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
318 pVertexAttributeDescriptions: addr(attributebindings[0]),
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
319 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
320 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
321 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
322 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
323 primitiveRestartEnable: VK_FALSE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
324 )
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
325
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
326 # setup viewport
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
327 var viewportState = VkPipelineViewportStateCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
328 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
329 viewportCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
330 scissorCount: 1,
1
bb2a7d3a7003 add: more steps in setup
Sam <sam@basx.dev>
parents: 0
diff changeset
331 )
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
332
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
333 # rasterizerization config
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
334 var
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
335 rasterizer = VkPipelineRasterizationStateCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
336 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
337 depthClampEnable: VK_FALSE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
338 rasterizerDiscardEnable: VK_FALSE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
339 polygonMode: VK_POLYGON_MODE_FILL,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
340 lineWidth: 1.0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
341 cullMode: VkCullModeFlags(VK_CULL_MODE_BACK_BIT),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
342 frontFace: VK_FRONT_FACE_CLOCKWISE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
343 depthBiasEnable: VK_FALSE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
344 depthBiasConstantFactor: 0.0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
345 depthBiasClamp: 0.0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
346 depthBiasSlopeFactor: 0.0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
347 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
348 multisampling = VkPipelineMultisampleStateCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
349 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
350 sampleShadingEnable: VK_FALSE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
351 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
352 minSampleShading: 1.0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
353 pSampleMask: nil,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
354 alphaToCoverageEnable: VK_FALSE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
355 alphaToOneEnable: VK_FALSE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
356 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
357 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
358 colorWriteMask: VkColorComponentFlags(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
359 ord(VK_COLOR_COMPONENT_R_BIT) or
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
360 ord(VK_COLOR_COMPONENT_G_BIT) or
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
361 ord(VK_COLOR_COMPONENT_B_BIT) or
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
362 ord(VK_COLOR_COMPONENT_A_BIT)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
363 ),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
364 blendEnable: VK_TRUE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
365 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
366 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
367 colorBlendOp: VK_BLEND_OP_ADD,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
368 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
369 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
370 alphaBlendOp: VK_BLEND_OP_ADD,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
371 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
372 colorBlending = VkPipelineColorBlendStateCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
373 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
374 logicOpEnable: VK_TRUE,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
375 logicOp: VK_LOGIC_OP_COPY,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
376 attachmentCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
377 pAttachments: addr(colorBlendAttachment),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
378 blendConstants: [0.0'f, 0.0'f, 0.0'f, 0.0'f],
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
379 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
380
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
381 # create pipeline
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
382 pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
383 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
384 setLayoutCount: 0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
385 pSetLayouts: nil,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
386 pushConstantRangeCount: 0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
387 pPushConstantRanges: nil,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
388 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
389 checkVkResult device.vkCreatePipelineLayout(addr(pipelineLayoutInfo), nil, addr(result.layout))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
390
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
391 var stages: seq[VkPipelineShaderStageCreateInfo]
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
392 for shader in result.shaders:
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
393 stages.add(shader.shader)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
394 var pipelineInfo = VkGraphicsPipelineCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
395 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
396 stageCount: uint32(stages.len),
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
397 pStages: addr(stages[0]),
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
398 pVertexInputState: addr(vertexInputInfo),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
399 pInputAssemblyState: addr(inputAssembly),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
400 pViewportState: addr(viewportState),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
401 pRasterizationState: addr(rasterizer),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
402 pMultisampleState: addr(multisampling),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
403 pDepthStencilState: nil,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
404 pColorBlendState: addr(colorBlending),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
405 pDynamicState: addr(dynamicState),
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
406 layout: result.layout,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
407 renderPass: renderPass,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
408 subpass: 0,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
409 basePipelineHandle: VkPipeline(0),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
410 basePipelineIndex: -1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
411 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
412 checkVkResult device.vkCreateGraphicsPipelines(
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
413 VkPipelineCache(0),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
414 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
415 addr(pipelineInfo),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
416 nil,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
417 addr(result.pipeline)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
418 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
419
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
420 proc setupFramebuffers(device: VkDevice, swapchain: var Swapchain, renderPass: VkRenderPass, dimension: VkExtent2D): seq[VkFramebuffer] =
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
421 result = newSeq[VkFramebuffer](swapchain.images.len)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
422 for i, imageview in enumerate(swapchain.imageviews):
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
423 var framebufferInfo = VkFramebufferCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
424 sType: VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
425 renderPass: renderPass,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
426 attachmentCount: 1,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
427 pAttachments: addr(swapchain.imageviews[i]),
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
428 width: dimension.width,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
429 height: dimension.height,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
430 layers: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
431 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
432 checkVkResult device.vkCreateFramebuffer(addr(framebufferInfo), nil, addr(result[i]))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
433
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
434 proc trash(device: VkDevice, swapchain: Swapchain, framebuffers: seq[VkFramebuffer]) =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
435 for framebuffer in framebuffers:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
436 device.vkDestroyFramebuffer(framebuffer, nil)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
437 for imageview in swapchain.imageviews:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
438 device.vkDestroyImageView(imageview, nil)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
439 device.vkDestroySwapchainKHR(swapchain.swapchain, nil)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
440
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
441 proc recreateSwapchain(vulkan: Vulkan): (Swapchain, seq[VkFramebuffer]) =
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
442 debug(&"Recreate swapchain with dimension {vulkan.frameDimension}")
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
443 checkVkResult vulkan.device.device.vkDeviceWaitIdle()
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
444
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
445 vulkan.device.device.trash(vulkan.swapchain, vulkan.framebuffers)
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
446
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
447 result[0] = vulkan.device.device.setupSwapChain(
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
448 vulkan.device.physicalDevice,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
449 vulkan.surface,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
450 vulkan.frameDimension,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
451 vulkan.surfaceFormat
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
452 )
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
453 result[1] = vulkan.device.device.setupFramebuffers(
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
454 result[0],
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
455 vulkan.renderPass,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
456 vulkan.frameDimension
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
457 )
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
458
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
459
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
460 proc setupCommandBuffers(device: VkDevice, graphicsQueueFamily: uint32): (VkCommandPool, array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer]) =
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
461 # set up command buffer
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
462 var poolInfo = VkCommandPoolCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
463 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
464 flags: VkCommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT),
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
465 queueFamilyIndex: graphicsQueueFamily,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
466 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
467 checkVkResult device.vkCreateCommandPool(addr(poolInfo), nil, addr(result[0]))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
468
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
469 var allocInfo = VkCommandBufferAllocateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
470 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
471 commandPool: result[0],
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
472 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
473 commandBufferCount: result[1].len.uint32,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
474 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
475 checkVkResult device.vkAllocateCommandBuffers(addr(allocInfo), addr(result[1][0]))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
476
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
477 proc setupSyncPrimitives(device: VkDevice): (
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
478 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore],
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
479 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore],
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
480 array[MAX_FRAMES_IN_FLIGHT, VkFence],
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
481 ) =
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
482 var semaphoreInfo = VkSemaphoreCreateInfo(sType: VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
483 var fenceInfo = VkFenceCreateInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
484 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
485 flags: VkFenceCreateFlags(VK_FENCE_CREATE_SIGNALED_BIT)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
486 )
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
487 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
488 checkVkResult device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr(result[0][i]))
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
489 checkVkResult device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr(result[1][i]))
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
490 checkVkResult device.vkCreateFence(addr(fenceInfo), nil, addr(result[2][i]))
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
491
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
492 proc igniteEngine*(): Engine =
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
493
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
494 result.window = createWindow("Hello triangle")
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
495
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
496 # setup vulkan functions
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
497 vkLoad1_0()
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
498 vkLoad1_1()
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
499 vkLoad1_2()
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 8
diff changeset
500
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
501 checkGlslangResult glslang_initialize_process()
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
502
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
503 # create vulkan instance
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
504 result.vulkan.instance = createVulkanInstance(VULKAN_VERSION)
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 8
diff changeset
505 when DEBUG_LOG:
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
506 result.vulkan.debugMessenger = result.vulkan.instance.setupDebugLog()
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
507 result.vulkan.surface = result.vulkan.instance.createVulkanSurface(result.window)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
508 result.vulkan.device = result.vulkan.instance.setupVulkanDeviceAndQueues(result.vulkan.surface)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
509
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
510 # get basic frame information
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
511 result.vulkan.surfaceFormat = result.vulkan.device.physicalDevice.formats.getSuitableSurfaceFormat()
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
512 result.vulkan.frameDimension = result.window.getFrameDimension(result.vulkan.device.physicalDevice.device, result.vulkan.surface)
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
513
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
514 # setup swapchain and render pipeline
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
515 result.vulkan.swapchain = result.vulkan.device.device.setupSwapChain(
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
516 result.vulkan.device.physicalDevice,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
517 result.vulkan.surface,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
518 result.vulkan.frameDimension,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
519 result.vulkan.surfaceFormat
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
520 )
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
521 result.vulkan.renderPass = result.vulkan.device.device.setupRenderPass(result.vulkan.surfaceFormat.format)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
522 result.vulkan.pipeline = result.vulkan.device.device.setupRenderPipeline(result.vulkan.frameDimension, result.vulkan.renderPass)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
523 result.vulkan.framebuffers = result.vulkan.device.device.setupFramebuffers(
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
524 result.vulkan.swapchain,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
525 result.vulkan.renderPass,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
526 result.vulkan.frameDimension
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
527 )
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
528
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
529 (
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
530 result.vulkan.commandPool,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
531 result.vulkan.commandBuffers,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
532 ) = result.vulkan.device.device.setupCommandBuffers(result.vulkan.device.graphicsQueueFamily)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
533
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
534 (
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
535 result.vulkan.imageAvailableSemaphores,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
536 result.vulkan.renderFinishedSemaphores,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
537 result.vulkan.inFlightFences,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
538 ) = result.vulkan.device.device.setupSyncPrimitives()
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
539 result.vulkan.bufferA = result.vulkan.device.device.InitBuffer(result.vulkan.device.physicalDevice.device, uint64(sizeof(vertices[0])), VertexBuffer)
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
540 result.vulkan.bufferB = result.vulkan.device.device.InitBuffer(result.vulkan.device.physicalDevice.device, uint64(sizeof(vertices[1])), VertexBuffer)
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
541 var d: pointer
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
542 result.vulkan.bufferA.withMapping(d):
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
543 copyMem(d, addr(vertices[0]), sizeof(vertices[0]))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
544 result.vulkan.bufferB.withMapping(d):
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
545 copyMem(d, addr(vertices[1]), sizeof(vertices[1]))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
546
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
547
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
548 proc recordCommandBuffer(renderPass: VkRenderPass, pipeline: VkPipeline, commandBuffer: VkCommandBuffer, framebuffer: VkFramebuffer, frameDimension: VkExtent2D, engine: var Engine) =
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
549 var
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
550 beginInfo = VkCommandBufferBeginInfo(
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
551 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
552 pInheritanceInfo: nil,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
553 )
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
554 clearColor = VkClearValue(color: VkClearColorValue(float32: [0.2'f, 0.2'f, 0.2'f, 1.0'f]))
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
555 renderPassInfo = VkRenderPassBeginInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
556 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
557 renderPass: renderPass,
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
558 framebuffer: framebuffer,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
559 renderArea: VkRect2D(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
560 offset: VkOffset2D(x: 0, y: 0),
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
561 extent: frameDimension,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
562 ),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
563 clearValueCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
564 pClearValues: addr(clearColor),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
565 )
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
566 viewport = VkViewport(
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
567 x: 0.0,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
568 y: 0.0,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
569 width: (float) frameDimension.width,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
570 height: (float) frameDimension.height,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
571 minDepth: 0.0,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
572 maxDepth: 1.0,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
573 )
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
574 scissor = VkRect2D(
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
575 offset: VkOffset2D(x: 0, y: 0),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
576 extent: frameDimension
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
577 )
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
578 checkVkResult commandBuffer.vkBeginCommandBuffer(addr(beginInfo))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
579 commandBuffer.vkCmdBeginRenderPass(addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE)
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
580 commandBuffer.vkCmdSetViewport(firstViewport=0, viewportCount=1, addr(viewport))
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
581 commandBuffer.vkCmdSetScissor(firstScissor=0, scissorCount=1, addr(scissor))
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
582 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline)
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
583
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
584 var
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
585 vertexBuffers = [engine.vulkan.bufferA.vkBuffer, engine.vulkan.bufferB.vkBuffer]
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
586 offsets = [VkDeviceSize(0), VkDeviceSize(0)]
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
587 commandBuffer.vkCmdBindVertexBuffers(firstBinding=0'u32, bindingCount=2'u32, pBuffers=addr(vertexBuffers[0]), pOffsets=addr(offsets[0]))
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
588 commandBuffer.vkCmdDraw(vertexCount=uint32(vertices[0].len), instanceCount=1'u32, firstVertex=0'u32, firstInstance=0'u32)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
589 commandBuffer.vkCmdEndRenderPass()
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
590 checkVkResult commandBuffer.vkEndCommandBuffer()
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
591
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
592 proc drawFrame(window: NativeWindow, vulkan: var Vulkan, currentFrame: int, resized: bool, engine: var Engine) =
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
593 checkVkResult vulkan.device.device.vkWaitForFences(1, addr(vulkan.inFlightFences[currentFrame]), VK_TRUE, high(uint64))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
594 var bufferImageIndex: uint32
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
595 let nextImageResult = vulkan.device.device.vkAcquireNextImageKHR(
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
596 vulkan.swapchain.swapchain,
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
597 high(uint64),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
598 vulkan.imageAvailableSemaphores[currentFrame],
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
599 VkFence(0),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
600 addr(bufferImageIndex)
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
601 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
602 if nextImageResult == VK_ERROR_OUT_OF_DATE_KHR:
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
603 vulkan.frameDimension = window.getFrameDimension(vulkan.device.physicalDevice.device, vulkan.surface)
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
604 (vulkan.swapchain, vulkan.framebuffers) = vulkan.recreateSwapchain()
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
605 elif not (nextImageResult in [VK_SUCCESS, VK_SUBOPTIMAL_KHR]):
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
606 raise newException(Exception, "Vulkan error: vkAcquireNextImageKHR returned " & $nextImageResult)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
607 checkVkResult vulkan.device.device.vkResetFences(1, addr(vulkan.inFlightFences[currentFrame]))
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
608
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
609 checkVkResult vulkan.commandBuffers[currentFrame].vkResetCommandBuffer(VkCommandBufferResetFlags(0))
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
610 vulkan.renderPass.recordCommandBuffer(vulkan.pipeline.pipeline, vulkan.commandBuffers[currentFrame], vulkan.framebuffers[bufferImageIndex], vulkan.frameDimension, engine)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
611 var
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
612 waitSemaphores = [vulkan.imageAvailableSemaphores[currentFrame]]
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
613 waitStages = [VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)]
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
614 signalSemaphores = [vulkan.renderFinishedSemaphores[currentFrame]]
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
615 submitInfo = VkSubmitInfo(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
616 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
617 waitSemaphoreCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
618 pWaitSemaphores: addr(waitSemaphores[0]),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
619 pWaitDstStageMask: addr(waitStages[0]),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
620 commandBufferCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
621 pCommandBuffers: addr(vulkan.commandBuffers[currentFrame]),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
622 signalSemaphoreCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
623 pSignalSemaphores: addr(signalSemaphores[0]),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
624 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
625 checkVkResult vkQueueSubmit(vulkan.device.graphicsQueue, 1, addr(submitInfo), vulkan.inFlightFences[currentFrame])
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
626
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
627 var presentInfo = VkPresentInfoKHR(
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
628 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
629 waitSemaphoreCount: 1,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
630 pWaitSemaphores: addr(signalSemaphores[0]),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
631 swapchainCount: 1,
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
632 pSwapchains: addr(vulkan.swapchain.swapchain),
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
633 pImageIndices: addr(bufferImageIndex),
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
634 pResults: nil,
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
635 )
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
636 let presentResult = vkQueuePresentKHR(vulkan.device.presentationQueue, addr(presentInfo))
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
637
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
638 if presentResult == VK_ERROR_OUT_OF_DATE_KHR or presentResult == VK_SUBOPTIMAL_KHR or resized:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
639 vulkan.frameDimension = window.getFrameDimension(vulkan.device.physicalDevice.device, vulkan.surface)
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
640 (vulkan.swapchain, vulkan.framebuffers) = vulkan.recreateSwapchain()
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
641
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
642
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
643 proc fullThrottle*(engine: var Engine) =
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
644 var
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
645 killed = false
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
646 currentFrame = 0
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
647 resized = false
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
648
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
649 while not killed:
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
650 for event in engine.window.pendingEvents():
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
651 case event.eventType:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
652 of Quit:
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
653 killed = true
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
654 of ResizedWindow:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
655 resized = true
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
656 of KeyDown:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
657 echo event
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
658 if event.key == Escape:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
659 killed = true
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
660 else:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
661 discard
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
662 engine.window.drawFrame(engine.vulkan, currentFrame, resized, engine)
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
663 resized = false
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
664 currentFrame = (currentFrame + 1) mod MAX_FRAMES_IN_FLIGHT;
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
665 checkVkResult engine.vulkan.device.device.vkDeviceWaitIdle()
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
666
18
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
667 proc trash*(engine: var Engine) =
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
668 `=destroy` engine.vulkan.bufferA
90e117952f74 add: basic vertex buffer functionality
Sam <sam@basx.dev>
parents: 17
diff changeset
669 `=destroy` engine.vulkan.bufferB
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
670 engine.vulkan.device.device.trash(engine.vulkan.swapchain, engine.vulkan.framebuffers)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents: 4
diff changeset
671 checkVkResult engine.vulkan.device.device.vkDeviceWaitIdle()
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
672
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
673 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
674 engine.vulkan.device.device.vkDestroySemaphore(engine.vulkan.imageAvailableSemaphores[i], nil)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
675 engine.vulkan.device.device.vkDestroySemaphore(engine.vulkan.renderFinishedSemaphores[i], nil)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
676 engine.vulkan.device.device.vkDestroyFence(engine.vulkan.inFlightFences[i], nil)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
677
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
678 engine.vulkan.device.device.vkDestroyCommandPool(engine.vulkan.commandPool, nil)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
679 engine.vulkan.device.device.vkDestroyPipeline(engine.vulkan.pipeline.pipeline, nil)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
680 engine.vulkan.device.device.vkDestroyPipelineLayout(engine.vulkan.pipeline.layout, nil)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
681 engine.vulkan.device.device.vkDestroyRenderPass(engine.vulkan.renderPass, nil)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
682
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
683 for shader in engine.vulkan.pipeline.shaders:
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 11
diff changeset
684 engine.vulkan.device.device.vkDestroyShaderModule(shader.shader.module, nil)
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
685
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
686 engine.vulkan.instance.vkDestroySurfaceKHR(engine.vulkan.surface, nil)
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
687 engine.vulkan.device.device.vkDestroyDevice(nil)
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 8
diff changeset
688 when DEBUG_LOG:
4
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
689 engine.vulkan.instance.vkDestroyDebugUtilsMessengerEXT(engine.vulkan.debugMessenger, nil)
af9183acb173 did: refactor, add resizing, proper cleanup
Sam <sam@basx.dev>
parents: 2
diff changeset
690 glslang_finalize_process()
9
227e53d1a853 fix: segfault on shutdown
Sam <sam@basx.dev>
parents: 8
diff changeset
691 engine.window.trash()
2
213fdf8d31dd did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents: 1
diff changeset
692 engine.vulkan.instance.vkDestroyInstance(nil)