annotate src/semicongine/engine.nim @ 522:f2c97bdbb0b3

did: rename and update older demos to work with new APIs
author Sam <sam@basx.dev>
date Tue, 24 Jan 2023 10:22:38 +0700
parents a25325bec7f2
children 311ee4e58032
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
1 import std/options
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/times
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/typetraits
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/strformat
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/enumerate
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
6 import std/logging
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
7
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
8
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
9 import ./math/vector
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./vulkan
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
11 import ./vulkan_helpers
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
12 import ./window
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
13 import ./events
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
14 import ./shader
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
15 import ./vertex
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
16 import ./buffer
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
17 import ./thing
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
18 import ./mesh
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
19 import ./descriptor
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
20
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
21 const MAX_FRAMES_IN_FLIGHT = 2
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
22 const DEBUG_LOG = not defined(release)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
23
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
24 var logger = newConsoleLogger()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
25 addHandler(logger)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
26
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
27
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
28 const VULKAN_VERSION = VK_MAKE_API_VERSION(0'u32, 1'u32, 2'u32, 0'u32)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
29 const ENGINE_NAME = "zamkongine"
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
30 const ENGINE_VERSION = "0.1"
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
31 const BUILD_VERSION = ENGINE_VERSION & '-' & gorge("git log -1 --format=format:'%H'")
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
32 echo "Engine: " & ENGINE_NAME & " " & BUILD_VERSION
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
33
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
34 type
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
35 Device = object
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
36 device*: VkDevice
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
37 physicalDevice*: PhysicalDevice
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
38 graphicsQueueFamily*: uint32
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
39 presentationQueueFamily*: uint32
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
40 graphicsQueue*: VkQueue
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
41 presentationQueue*: VkQueue
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
42 commandPool*: VkCommandPool
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
43 commandBuffers*: array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer]
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
44 Swapchain = object
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
45 swapchain: VkSwapchainKHR
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
46 images: seq[VkImage]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
47 imageviews: seq[VkImageView]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
48 RenderPipeline*[VertexType, Uniforms] = object
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
49 device*: VkDevice
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
50 shaders*: seq[ShaderProgram[VertexType, Uniforms]]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
51 layout*: VkPipelineLayout
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
52 pipeline*: VkPipeline
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
53 vertexBuffers*: seq[(seq[Buffer], uint32)]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
54 indexedVertexBuffers*: seq[(seq[Buffer], Buffer, uint32, VkIndexType)]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
55 descriptorSetLayout*: VkDescriptorSetLayout
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
56 uniformBuffers*: array[MAX_FRAMES_IN_FLIGHT, Buffer]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
57 descriptorPool*: VkDescriptorPool
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
58 descriptors: array[MAX_FRAMES_IN_FLIGHT, VkDescriptorSet]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
59 QueueFamily = object
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
60 properties*: VkQueueFamilyProperties
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
61 hasSurfaceSupport*: bool
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
62 PhysicalDevice = object
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
63 device*: VkPhysicalDevice
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
64 extensions*: seq[string]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
65 properties*: VkPhysicalDeviceProperties
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
66 features*: VkPhysicalDeviceFeatures
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
67 queueFamilies*: seq[QueueFamily]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
68 formats: seq[VkSurfaceFormatKHR]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
69 presentModes: seq[VkPresentModeKHR]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
70 Vulkan* = object
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
71 debugMessenger*: VkDebugUtilsMessengerEXT
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
72 instance*: VkInstance
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
73 deviceList*: seq[PhysicalDevice]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
74 device*: Device
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
75 surface*: VkSurfaceKHR
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
76 surfaceFormat*: VkSurfaceFormatKHR
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
77 frameSize*: TVec2[uint32]
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
78 swapchain*: Swapchain
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
79 framebuffers*: seq[VkFramebuffer]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
80 renderPass*: VkRenderPass
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
81 imageAvailableSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
82 renderFinishedSemaphores*: array[MAX_FRAMES_IN_FLIGHT, VkSemaphore]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
83 inFlightFences*: array[MAX_FRAMES_IN_FLIGHT, VkFence]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
84 Input* = object
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
85 keysDown*: set[Key]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
86 keysPressed*: set[Key]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
87 keysReleased*: set[Key]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
88 mouseDown*: set[MouseButton]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
89 mousePressed*: set[MouseButton]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
90 mouseReleased*: set[MouseButton]
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
91 mousePos*: Vec2
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
92 Engine* = object
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
93 vulkan*: Vulkan
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
94 window*: NativeWindow
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
95 currentscenedata*: Thing
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
96 input*: Input
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
97
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
98 proc getAllPhysicalDevices(instance: VkInstance, surface: VkSurfaceKHR): seq[
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
99 PhysicalDevice] =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
100 for vulkanPhysicalDevice in getVulkanPhysicalDevices(instance):
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
101 var device = PhysicalDevice(device: vulkanPhysicalDevice,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
102 extensions: getDeviceExtensions(vulkanPhysicalDevice))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
103 vkGetPhysicalDeviceProperties(vulkanPhysicalDevice, addr(device.properties))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
104 vkGetPhysicalDeviceFeatures(vulkanPhysicalDevice, addr(device.features))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
105 device.formats = vulkanPhysicalDevice.getDeviceSurfaceFormats(surface)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
106 device.presentModes = vulkanPhysicalDevice.getDeviceSurfacePresentModes(surface)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
107
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
108 debug(&"Physical device nr {int(vulkanPhysicalDevice)} {cleanString(device.properties.deviceName)}")
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
109 for i, queueFamilyProperty in enumerate(getQueueFamilies(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
110 vulkanPhysicalDevice)):
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
111 var hasSurfaceSupport: VkBool32 = VK_FALSE
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
112 checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkanPhysicalDevice,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
113 uint32(i), surface, addr(hasSurfaceSupport))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
114 device.queueFamilies.add(QueueFamily(properties: queueFamilyProperty,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
115 hasSurfaceSupport: bool(hasSurfaceSupport)))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
116 debug(&" Queue family {i} {queueFamilyProperty}")
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
117
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
118 result.add(device)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
119
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
120 proc filterForDevice(devices: seq[PhysicalDevice]): seq[(PhysicalDevice, uint32, uint32)] =
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
121 for device in devices:
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
122 if not (device.formats.len > 0 and device.presentModes.len > 0 and
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
123 "VK_KHR_swapchain" in device.extensions):
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
124 continue
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
125 var graphicsQueueFamily = high(uint32)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
126 var presentationQueueFamily = high(uint32)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
127 for i, queueFamily in enumerate(device.queueFamilies):
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
128 if queueFamily.hasSurfaceSupport:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
129 presentationQueueFamily = uint32(i)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
130 if bool(uint32(queueFamily.properties.queueFlags) and ord(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
131 VK_QUEUE_GRAPHICS_BIT)):
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
132 graphicsQueueFamily = uint32(i)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
133 if graphicsQueueFamily != high(uint32) and presentationQueueFamily != high(uint32):
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
134 result.add((device, graphicsQueueFamily, presentationQueueFamily))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
135
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
136 for (device, graphicsQueueFamily, presentationQueueFamily) in result:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
137 debug(&"Viable device: {cleanString(device.properties.deviceName)} (graphics queue family {graphicsQueueFamily}, presentation queue family {presentationQueueFamily})")
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
138
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
139
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
140 proc getFrameDimension(window: NativeWindow, device: VkPhysicalDevice,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
141 surface: VkSurfaceKHR): TVec2[uint32] =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
142 let capabilities = device.getSurfaceCapabilities(surface)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
143 if capabilities.currentExtent.width != high(uint32):
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
144 return TVec2[uint32]([capabilities.currentExtent.width,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
145 capabilities.currentExtent.height])
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
146 else:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
147 let (width, height) = window.size()
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
148 return TVec2[uint32]([
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
149 min(max(uint32(width), capabilities.minImageExtent.width),
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
150 capabilities.maxImageExtent.width),
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
151 min(max(uint32(height), capabilities.minImageExtent.height),
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
152 capabilities.maxImageExtent.height),
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
153 ])
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
154
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
155 when DEBUG_LOG:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
156 proc setupDebugLog(instance: VkInstance): VkDebugUtilsMessengerEXT =
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
157 var createInfo = VkDebugUtilsMessengerCreateInfoEXT(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
158 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
159 messageSeverity: VkDebugUtilsMessageSeverityFlagsEXT(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
160 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) or
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
161 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) or
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
162 ord(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
163 ),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
164 messageType: VkDebugUtilsMessageTypeFlagsEXT(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
165 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) or
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
166 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) or
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
167 ord(VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
168 ),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
169 pfnUserCallback: debugCallback,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
170 pUserData: nil,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
171 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
172 checkVkResult instance.vkCreateDebugUtilsMessengerEXT(addr(createInfo), nil,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
173 addr(result))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
174
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
175 proc setupVulkanDeviceAndQueues(instance: VkInstance,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
176 surface: VkSurfaceKHR): Device =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
177 let usableDevices = instance.getAllPhysicalDevices(surface).filterForDevice()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
178 if len(usableDevices) == 0:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
179 raise newException(Exception, "No suitable graphics device found")
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
180 result.physicalDevice = usableDevices[0][0]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
181 result.graphicsQueueFamily = usableDevices[0][1]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
182 result.presentationQueueFamily = usableDevices[0][2]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
183
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
184 debug(&"Chose device {cleanString(result.physicalDevice.properties.deviceName)}")
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
185
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
186 (result.device, result.graphicsQueue, result.presentationQueue) = getVulcanDevice(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
187 result.physicalDevice.device,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
188 result.physicalDevice.features,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
189 result.graphicsQueueFamily,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
190 result.presentationQueueFamily,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
191 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
192
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
193 proc setupSwapChain(device: VkDevice, physicalDevice: PhysicalDevice,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
194 surface: VkSurfaceKHR, dimension: TVec2[uint32],
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
195 surfaceFormat: VkSurfaceFormatKHR): Swapchain =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
196
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
197 let capabilities = physicalDevice.device.getSurfaceCapabilities(surface)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
198 var selectedPresentationMode = getPresentMode(physicalDevice.presentModes)
513
5c8ce3d5a519 fix: wrong number of swapchain images
Sam <sam@basx.dev>
parents: 512
diff changeset
199 var imageCount = capabilities.minImageCount + 1
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
200 if capabilities.maxImageCount > 0:
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
201 imageCount = min(capabilities.maxImageCount, imageCount)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
202 # setup swapchain
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
203 var swapchainCreateInfo = VkSwapchainCreateInfoKHR(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
204 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
205 surface: surface,
513
5c8ce3d5a519 fix: wrong number of swapchain images
Sam <sam@basx.dev>
parents: 512
diff changeset
206 minImageCount: imageCount,
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
207 imageFormat: surfaceFormat.format,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
208 imageColorSpace: surfaceFormat.colorSpace,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
209 imageExtent: VkExtent2D(width: dimension[0], height: dimension[1]),
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
210 imageArrayLayers: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
211 imageUsage: VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
212 # VK_SHARING_MODE_CONCURRENT no supported (i.e cannot use different queue families for drawing to swap surface?)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
213 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
214 preTransform: capabilities.currentTransform,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
215 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
216 presentMode: selectedPresentationMode,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
217 clipped: VK_TRUE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
218 oldSwapchain: VkSwapchainKHR(0),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
219 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
220 checkVkResult device.vkCreateSwapchainKHR(addr(swapchainCreateInfo), nil,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
221 addr(result.swapchain))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
222 result.images = device.getSwapChainImages(result.swapchain)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
223
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
224 # setup swapchian image views
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
225
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
226 result.imageviews = newSeq[VkImageView](result.images.len)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
227 for i, image in enumerate(result.images):
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
228 var imageViewCreateInfo = VkImageViewCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
229 sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
230 image: image,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
231 viewType: VK_IMAGE_VIEW_TYPE_2D,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
232 format: surfaceFormat.format,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
233 components: VkComponentMapping(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
234 r: VK_COMPONENT_SWIZZLE_IDENTITY,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
235 g: VK_COMPONENT_SWIZZLE_IDENTITY,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
236 b: VK_COMPONENT_SWIZZLE_IDENTITY,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
237 a: VK_COMPONENT_SWIZZLE_IDENTITY,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
238 ),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
239 subresourceRange: VkImageSubresourceRange(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
240 aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
241 baseMipLevel: 0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
242 levelCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
243 baseArrayLayer: 0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
244 layerCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
245 ),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
246 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
247 checkVkResult device.vkCreateImageView(addr(imageViewCreateInfo), nil, addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
248 result.imageviews[i]))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
249
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
250 proc setupRenderPass(device: VkDevice, format: VkFormat): VkRenderPass =
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
251 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
252 colorAttachment = VkAttachmentDescription(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
253 format: format,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
254 samples: VK_SAMPLE_COUNT_1_BIT,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
255 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
256 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
257 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
258 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
259 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
260 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
261 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
262 colorAttachmentRef = VkAttachmentReference(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
263 attachment: 0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
264 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
265 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
266 subpass = VkSubpassDescription(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
267 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
268 colorAttachmentCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
269 pColorAttachments: addr(colorAttachmentRef)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
270 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
271 dependency = VkSubpassDependency(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
272 srcSubpass: VK_SUBPASS_EXTERNAL,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
273 dstSubpass: 0,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
274 srcStageMask: VkPipelineStageFlags(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
275 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
276 srcAccessMask: VkAccessFlags(0),
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
277 dstStageMask: VkPipelineStageFlags(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
278 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
279 dstAccessMask: VkAccessFlags(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
280 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
281 renderPassCreateInfo = VkRenderPassCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
282 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
283 attachmentCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
284 pAttachments: addr(colorAttachment),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
285 subpassCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
286 pSubpasses: addr(subpass),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
287 dependencyCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
288 pDependencies: addr(dependency),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
289 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
290 checkVkResult device.vkCreateRenderPass(addr(renderPassCreateInfo), nil, addr(result))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
291
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
292 proc initRenderPipeline[VertexType, Uniforms](device: VkDevice,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
293 frameSize: TVec2[uint32], renderPass: VkRenderPass, vertexShader,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
294 fragmentShader: static string): RenderPipeline[VertexType, Uniforms] =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
295 # load shaders
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
296 result.device = device
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
297 result.shaders.add(initShaderProgram[VertexType, Uniforms](device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
298 VK_SHADER_STAGE_VERTEX_BIT, vertexShader))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
299 result.shaders.add(initShaderProgram[VertexType, Uniforms](device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
300 VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShader))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
301
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
302 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
303 # define which parts can be dynamic (pipeline is fixed after setup)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
304 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
305 dynamicState = VkPipelineDynamicStateCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
306 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
307 dynamicStateCount: uint32(dynamicStates.len),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
308 pDynamicStates: addr(dynamicStates[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
309 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
310 vertexbindings = generateInputVertexBinding[VertexType]()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
311 attributebindings = generateInputAttributeBinding[VertexType]()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
312
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
313 # define input data format
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
314 vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
315 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
316 vertexBindingDescriptionCount: uint32(vertexbindings.len),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
317 pVertexBindingDescriptions: addr(vertexbindings[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
318 vertexAttributeDescriptionCount: uint32(attributebindings.len),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
319 pVertexAttributeDescriptions: addr(attributebindings[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
320 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
321 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
322 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
323 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
324 primitiveRestartEnable: VK_FALSE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
325 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
326
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
327 # setup viewport
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
328 var viewportState = VkPipelineViewportStateCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
329 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
330 viewportCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
331 scissorCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
332 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
333
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
334 # rasterizerization config
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
335 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
336 rasterizer = VkPipelineRasterizationStateCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
337 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
338 depthClampEnable: VK_FALSE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
339 rasterizerDiscardEnable: VK_FALSE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
340 polygonMode: VK_POLYGON_MODE_FILL,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
341 lineWidth: 1.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
342 cullMode: VkCullModeFlags(VK_CULL_MODE_BACK_BIT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
343 frontFace: VK_FRONT_FACE_CLOCKWISE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
344 depthBiasEnable: VK_FALSE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
345 depthBiasConstantFactor: 0.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
346 depthBiasClamp: 0.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
347 depthBiasSlopeFactor: 0.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
348 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
349 multisampling = VkPipelineMultisampleStateCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
350 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
351 sampleShadingEnable: VK_FALSE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
352 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
353 minSampleShading: 1.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
354 pSampleMask: nil,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
355 alphaToCoverageEnable: VK_FALSE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
356 alphaToOneEnable: VK_FALSE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
357 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
358 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
359 colorWriteMask: VkColorComponentFlags(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
360 ord(VK_COLOR_COMPONENT_R_BIT) or
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
361 ord(VK_COLOR_COMPONENT_G_BIT) or
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
362 ord(VK_COLOR_COMPONENT_B_BIT) or
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
363 ord(VK_COLOR_COMPONENT_A_BIT)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
364 ),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
365 blendEnable: VK_TRUE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
366 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
367 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
368 colorBlendOp: VK_BLEND_OP_ADD,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
369 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
370 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
371 alphaBlendOp: VK_BLEND_OP_ADD,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
372 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
373 colorBlending = VkPipelineColorBlendStateCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
374 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
375 logicOpEnable: VK_TRUE,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
376 logicOp: VK_LOGIC_OP_COPY,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
377 attachmentCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
378 pAttachments: addr(colorBlendAttachment),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
379 blendConstants: [0.0'f, 0.0'f, 0.0'f, 0.0'f],
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
380 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
381
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
382 result.descriptorSetLayout = device.createUniformDescriptorLayout(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
383 VkShaderStageFlags(VK_SHADER_STAGE_VERTEX_BIT), 0)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
384 var
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
385 # "globals" that go into the shader, uniforms etc.
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
386 pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
387 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
388 setLayoutCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
389 pSetLayouts: addr(result.descriptorSetLayout),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
390 pushConstantRangeCount: 0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
391 pPushConstantRanges: nil,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
392 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
393 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
394 addr(result.layout))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
395
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
396 var stages: seq[VkPipelineShaderStageCreateInfo]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
397 for shader in result.shaders:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
398 stages.add(shader.shader)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
399 var pipelineInfo = VkGraphicsPipelineCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
400 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
401 stageCount: uint32(stages.len),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
402 pStages: addr(stages[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
403 pVertexInputState: addr(vertexInputInfo),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
404 pInputAssemblyState: addr(inputAssembly),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
405 pViewportState: addr(viewportState),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
406 pRasterizationState: addr(rasterizer),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
407 pMultisampleState: addr(multisampling),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
408 pDepthStencilState: nil,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
409 pColorBlendState: addr(colorBlending),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
410 pDynamicState: addr(dynamicState),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
411 layout: result.layout,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
412 renderPass: renderPass,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
413 subpass: 0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
414 basePipelineHandle: VkPipeline(0),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
415 basePipelineIndex: -1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
416 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
417 checkVkResult vkCreateGraphicsPipelines(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
418 device,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
419 VkPipelineCache(0),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
420 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
421 addr(pipelineInfo),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
422 nil,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
423 addr(result.pipeline)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
424 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
425
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
426 proc setupFramebuffers(device: VkDevice, swapchain: var Swapchain,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
427 renderPass: VkRenderPass, dimension: TVec2[uint32]): seq[VkFramebuffer] =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
428 result = newSeq[VkFramebuffer](swapchain.images.len)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
429 for i, imageview in enumerate(swapchain.imageviews):
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
430 var framebufferInfo = VkFramebufferCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
431 sType: VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
432 renderPass: renderPass,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
433 attachmentCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
434 pAttachments: addr(swapchain.imageviews[i]),
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
435 width: dimension[0],
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
436 height: dimension[1],
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
437 layers: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
438 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
439 checkVkResult device.vkCreateFramebuffer(addr(framebufferInfo), nil, addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
440 result[i]))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
441
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
442 proc trash(device: VkDevice, swapchain: Swapchain, framebuffers: seq[
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
443 VkFramebuffer]) =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
444 for framebuffer in framebuffers:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
445 device.vkDestroyFramebuffer(framebuffer, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
446 for imageview in swapchain.imageviews:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
447 device.vkDestroyImageView(imageview, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
448 device.vkDestroySwapchainKHR(swapchain.swapchain, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
449
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
450 proc recreateSwapchain(vulkan: Vulkan): (Swapchain, seq[VkFramebuffer]) =
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
451 if vulkan.frameSize.x == 0 or vulkan.frameSize.y == 0:
507
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
452 return (vulkan.swapchain, vulkan.framebuffers)
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
453 debug(&"Recreate swapchain with dimension {vulkan.frameSize}")
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
454 checkVkResult vulkan.device.device.vkDeviceWaitIdle()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
455
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
456 vulkan.device.device.trash(vulkan.swapchain, vulkan.framebuffers)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
457
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
458 result[0] = vulkan.device.device.setupSwapChain(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
459 vulkan.device.physicalDevice,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
460 vulkan.surface,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
461 vulkan.frameSize,
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
462 vulkan.surfaceFormat
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
463 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
464 result[1] = vulkan.device.device.setupFramebuffers(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
465 result[0],
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
466 vulkan.renderPass,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
467 vulkan.frameSize
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
468 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
469
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
470
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
471 proc setupCommandBuffers(device: VkDevice, graphicsQueueFamily: uint32): (
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
472 VkCommandPool, array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer]) =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
473 # set up command buffer
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
474 var poolInfo = VkCommandPoolCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
475 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
476 flags: VkCommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
477 queueFamilyIndex: graphicsQueueFamily,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
478 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
479 checkVkResult device.vkCreateCommandPool(addr(poolInfo), nil, addr(result[0]))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
480
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
481 var allocInfo = VkCommandBufferAllocateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
482 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
483 commandPool: result[0],
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
484 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
485 commandBufferCount: result[1].len.uint32,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
486 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
487 checkVkResult device.vkAllocateCommandBuffers(addr(allocInfo), addr(result[1][0]))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
488
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
489 proc setupSyncPrimitives(device: VkDevice): (
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
490 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore],
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
491 array[MAX_FRAMES_IN_FLIGHT, VkSemaphore],
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
492 array[MAX_FRAMES_IN_FLIGHT, VkFence],
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
493 ) =
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
494 var semaphoreInfo = VkSemaphoreCreateInfo(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
495 sType: VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
496 var fenceInfo = VkFenceCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
497 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
498 flags: VkFenceCreateFlags(VK_FENCE_CREATE_SIGNALED_BIT)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
499 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
500 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
501 checkVkResult device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
502 result[0][i]))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
503 checkVkResult device.vkCreateSemaphore(addr(semaphoreInfo), nil, addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
504 result[1][i]))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
505 checkVkResult device.vkCreateFence(addr(fenceInfo), nil, addr(result[2][i]))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
506
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
507 proc igniteEngine*(windowTitle: string): Engine =
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
508
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
509 result.window = createWindow(windowTitle)
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
510 let mousepos = result.window.getMousePosition()
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
511 if mousepos.isSome():
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
512 result.input.mousePos = mousePos.get()
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
513
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
514 # setup vulkan functions
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
515 vkLoad1_0()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
516 vkLoad1_1()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
517 vkLoad1_2()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
518
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
519 # create vulkan instance
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
520 result.vulkan.instance = createVulkanInstance(VULKAN_VERSION)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
521 when DEBUG_LOG:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
522 result.vulkan.debugMessenger = result.vulkan.instance.setupDebugLog()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
523 result.vulkan.surface = result.vulkan.instance.createVulkanSurface(result.window)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
524 result.vulkan.device = result.vulkan.instance.setupVulkanDeviceAndQueues(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
525 result.vulkan.surface)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
526
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
527 # get basic frame information
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
528 result.vulkan.surfaceFormat = result.vulkan.device.physicalDevice.formats.getSuitableSurfaceFormat()
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
529 result.vulkan.frameSize = result.window.getFrameDimension(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
530 result.vulkan.device.physicalDevice.device, result.vulkan.surface)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
531
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
532 # setup swapchain and render pipeline
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
533 result.vulkan.swapchain = result.vulkan.device.device.setupSwapChain(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
534 result.vulkan.device.physicalDevice,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
535 result.vulkan.surface,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
536 result.vulkan.frameSize,
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
537 result.vulkan.surfaceFormat
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
538 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
539 result.vulkan.renderPass = result.vulkan.device.device.setupRenderPass(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
540 result.vulkan.surfaceFormat.format)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
541 result.vulkan.framebuffers = result.vulkan.device.device.setupFramebuffers(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
542 result.vulkan.swapchain,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
543 result.vulkan.renderPass,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
544 result.vulkan.frameSize
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
545 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
546 (
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
547 result.vulkan.device.commandPool,
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
548 result.vulkan.device.commandBuffers,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
549 ) = result.vulkan.device.device.setupCommandBuffers(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
550 result.vulkan.device.graphicsQueueFamily)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
551
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
552 (
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
553 result.vulkan.imageAvailableSemaphores,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
554 result.vulkan.renderFinishedSemaphores,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
555 result.vulkan.inFlightFences,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
556 ) = result.vulkan.device.device.setupSyncPrimitives()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
557
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
558
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
559 proc setupPipeline*[VertexType; UniformType; IndexType: uint16|uint32](
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
560 engine: var Engine, scenedata: Thing, vertexShader,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
561 fragmentShader: static string): RenderPipeline[VertexType, UniformType] =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
562 engine.currentscenedata = scenedata
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
563 result = initRenderPipeline[VertexType, UniformType](
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
564 engine.vulkan.device.device,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
565 engine.vulkan.frameSize,
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
566 engine.vulkan.renderPass,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
567 vertexShader,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
568 fragmentShader,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
569 )
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
570
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
571 # vertex buffers
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
572 for mesh in allPartsOfType[Mesh[VertexType]](engine.currentscenedata):
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
573 result.vertexBuffers.add createVertexBuffers(mesh, result.device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
574 engine.vulkan.device.physicalDevice.device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
575 engine.vulkan.device.commandPool, engine.vulkan.device.graphicsQueue)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
576
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
577 # vertex buffers with indexes
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
578 when not (IndexType is void):
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
579 for mesh in allPartsOfType[IndexedMesh[VertexType, IndexType]](
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
580 engine.currentscenedata):
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
581 result.indexedVertexBuffers.add createIndexedVertexBuffers(mesh,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
582 result.device, engine.vulkan.device.physicalDevice.device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
583 engine.vulkan.device.commandPool, engine.vulkan.device.graphicsQueue)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
584
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
585 # uniform buffers
507
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
586 when not (UniformType is void):
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
587 result.uniformBuffers = createUniformBuffers[MAX_FRAMES_IN_FLIGHT,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
588 UniformType](
507
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
589 result.device,
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
590 engine.vulkan.device.physicalDevice.device
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
591 )
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
592
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
593 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
594 poolSize = VkDescriptorPoolSize(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
595 `type`: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
596 descriptorCount: uint32(MAX_FRAMES_IN_FLIGHT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
597 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
598 poolInfo = VkDescriptorPoolCreateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
599 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
600 poolSizeCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
601 pPoolSizes: addr(poolSize),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
602 maxSets: uint32(MAX_FRAMES_IN_FLIGHT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
603 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
604 checkVkResult vkCreateDescriptorPool(result.device, addr(poolInfo), nil, addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
605 result.descriptorPool))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
606
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
607 var layouts: array[MAX_FRAMES_IN_FLIGHT, VkDescriptorSetLayout]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
608 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
609 layouts[i] = result.descriptorSetLayout
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
610 var allocInfo = VkDescriptorSetAllocateInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
611 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
612 descriptorPool: result.descriptorPool,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
613 descriptorSetCount: uint32(MAX_FRAMES_IN_FLIGHT),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
614 pSetLayouts: addr(layouts[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
615 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
616
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
617 checkVkResult vkAllocateDescriptorSets(result.device, addr(allocInfo), addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
618 result.descriptors[0]))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
619
507
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
620 when not (UniformType is void):
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
621 var bufferInfos: array[MAX_FRAMES_IN_FLIGHT, array[1,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
622 VkDescriptorBufferInfo]] # because we use only one Uniform atm
507
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
623 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
624 bufferInfos[i][0] = VkDescriptorBufferInfo(
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
625 buffer: result.uniformBuffers[i].vkBuffer,
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
626 offset: VkDeviceSize(0),
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
627 range: VkDeviceSize(sizeof(UniformType)),
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
628 )
507
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
629 var descriptorWrite = VkWriteDescriptorSet(
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
630 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
631 dstSet: result.descriptors[i],
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
632 dstBinding: 0,
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
633 dstArrayElement: 0,
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
634 descriptorType: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
635 descriptorCount: 1,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
636 pBufferInfo: cast[ptr ptr VkDescriptorBufferInfo](addr(bufferInfos[i][
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
637 0])),
507
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
638 )
15e78601b390 add: some changes to build on windows host
Sam <sam@basx.dev>
parents: 501
diff changeset
639 vkUpdateDescriptorSets(result.device, 1, addr(descriptorWrite), 0, nil)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
640
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
641 proc updateBufferData*[T](device: Device, buffer: Buffer, data: var T) =
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
642 when stripGenericParams(T) is seq: # seq needs special treatment for automated data uploading
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
643 assert data.len > 0
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
644 let size = data.len * sizeof(get(genericParams(typeof(data)), 0))
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
645 let dataptr = addr(data[0])
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
646 else:
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
647 let size = sizeof(data)
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
648 let dataptr = addr(data)
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
649 if not (HostVisible in buffer.memoryProperties):
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
650 if not (TransferDst in buffer.bufferTypes):
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
651 raise newException(Exception, "Buffer cannot be updated")
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
652 var stagingBuffer = device.device.InitBuffer(device.physicalDevice.device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
653 uint64(size), {TransferSrc}, {HostVisible, HostCoherent})
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
654 copyMem(stagingBuffer.data, dataptr, size)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
655 transferBuffer(device.commandPool, device.graphicsQueue, stagingBuffer,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
656 buffer, uint64(size))
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
657 stagingBuffer.trash()
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
658 else:
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
659 copyMem(buffer.data, dataptr, size)
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
660
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
661 proc updateVertexData*[T: VertexAttribute](device: Device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
662 vertexAttribute: var T) =
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
663 device.updateBufferData(vertexAttribute.buffer, vertexAttribute.data)
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
664
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
665 proc updateUniformData*[VertexType, Uniforms](device: Device,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
666 pipeline: RenderPipeline[VertexType, Uniforms], data: var Uniforms) =
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
667 for buffer in pipeline.uniformBuffers:
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
668 device.updateBufferData(buffer, data)
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
669
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
670
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
671 proc runPipeline[VertexType; Uniforms](commandBuffer: VkCommandBuffer,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
672 pipeline: var RenderPipeline[VertexType, Uniforms], currentFrame: int) =
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
673 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
674 pipeline.pipeline)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
675
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
676 vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
677 pipeline.layout, 0, 1, addr(pipeline.descriptors[currentFrame]), 0, nil)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
678 for (vertexBufferSet, vertexCount) in pipeline.vertexBuffers:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
679 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
680 vertexBuffers: seq[VkBuffer]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
681 offsets: seq[VkDeviceSize]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
682 for buffer in vertexBufferSet:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
683 vertexBuffers.add buffer.vkBuffer
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
684 offsets.add VkDeviceSize(0)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
685
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
686 vkCmdBindVertexBuffers(commandBuffer, firstBinding = 0'u32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
687 bindingCount = uint32(vertexBuffers.len), pBuffers = addr(vertexBuffers[
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
688 0]), pOffsets = addr(offsets[0]))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
689 vkCmdDraw(commandBuffer, vertexCount = vertexCount, instanceCount = 1'u32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
690 firstVertex = 0'u32, firstInstance = 0'u32)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
691
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
692 for (vertexBufferSet, indexBuffer, indicesCount, indexType) in
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
693 pipeline.indexedVertexBuffers:
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
694 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
695 vertexBuffers: seq[VkBuffer]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
696 offsets: seq[VkDeviceSize]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
697 for buffer in vertexBufferSet:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
698 vertexBuffers.add buffer.vkBuffer
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
699 offsets.add VkDeviceSize(0)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
700
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
701 vkCmdBindVertexBuffers(commandBuffer, firstBinding = 0'u32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
702 bindingCount = uint32(vertexBuffers.len), pBuffers = addr(vertexBuffers[
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
703 0]), pOffsets = addr(offsets[0]))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
704 vkCmdBindIndexBuffer(commandBuffer, indexBuffer.vkBuffer, VkDeviceSize(0), indexType)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
705 vkCmdDrawIndexed(commandBuffer, indicesCount, 1, 0, 0, 0)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
706
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
707 proc recordCommandBuffer(renderPass: VkRenderPass, pipeline: var RenderPipeline,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
708 commandBuffer: VkCommandBuffer, framebuffer: VkFramebuffer,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
709 frameSize: TVec2[uint32], currentFrame: int) =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
710 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
711 beginInfo = VkCommandBufferBeginInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
712 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
713 pInheritanceInfo: nil,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
714 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
715 clearColor = VkClearValue(color: VkClearColorValue(float32: [0.2'f, 0.2'f,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
716 0.2'f, 1.0'f]))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
717 renderPassInfo = VkRenderPassBeginInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
718 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
719 renderPass: renderPass,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
720 framebuffer: framebuffer,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
721 renderArea: VkRect2D(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
722 offset: VkOffset2D(x: 0, y: 0),
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
723 extent: VkExtent2D(width: frameSize.x, height: frameSize.y),
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
724 ),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
725 clearValueCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
726 pClearValues: addr(clearColor),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
727 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
728 viewport = VkViewport(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
729 x: 0.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
730 y: 0.0,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
731 width: (float)frameSize.x,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
732 height: (float)frameSize.y,
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
733 minDepth: 0.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
734 maxDepth: 1.0,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
735 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
736 scissor = VkRect2D(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
737 offset: VkOffset2D(x: 0, y: 0),
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
738 extent: VkExtent2D(width: frameSize.x, height: frameSize.y)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
739 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
740 checkVkResult vkBeginCommandBuffer(commandBuffer, addr(beginInfo))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
741 block:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
742 vkCmdBeginRenderPass(commandBuffer, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
743 vkCmdSetViewport(commandBuffer, firstViewport = 0, viewportCount = 1, addr(viewport))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
744 vkCmdSetScissor(commandBuffer, firstScissor = 0, scissorCount = 1, addr(scissor))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
745 runPipeline(commandBuffer, pipeline, currentFrame)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
746 vkCmdEndRenderPass(commandBuffer)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
747 checkVkResult vkEndCommandBuffer(commandBuffer)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
748
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
749 proc drawFrame(window: NativeWindow, vulkan: var Vulkan, currentFrame: int,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
750 resized: bool, pipeline: var RenderPipeline) =
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
751 checkVkResult vkWaitForFences(vulkan.device.device, 1, addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
752 vulkan.inFlightFences[currentFrame]), VK_TRUE, high(uint64))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
753 var bufferImageIndex: uint32
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
754 let nextImageResult = vkAcquireNextImageKHR(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
755 vulkan.device.device,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
756 vulkan.swapchain.swapchain,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
757 high(uint64),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
758 vulkan.imageAvailableSemaphores[currentFrame],
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
759 VkFence(0),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
760 addr(bufferImageIndex)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
761 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
762 if nextImageResult == VK_ERROR_OUT_OF_DATE_KHR:
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
763 vulkan.frameSize = window.getFrameDimension(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
764 vulkan.device.physicalDevice.device, vulkan.surface)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
765 (vulkan.swapchain, vulkan.framebuffers) = vulkan.recreateSwapchain()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
766 elif not (nextImageResult in [VK_SUCCESS, VK_SUBOPTIMAL_KHR]):
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
767 raise newException(Exception, "Vulkan error: vkAcquireNextImageKHR returned " &
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
768 $nextImageResult)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
769 checkVkResult vkResetFences(vulkan.device.device, 1, addr(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
770 vulkan.inFlightFences[currentFrame]))
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
771
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
772 checkVkResult vkResetCommandBuffer(vulkan.device.commandBuffers[currentFrame],
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
773 VkCommandBufferResetFlags(0))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
774 vulkan.renderPass.recordCommandBuffer(pipeline, vulkan.device.commandBuffers[
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
775 currentFrame], vulkan.framebuffers[bufferImageIndex], vulkan.frameSize, currentFrame)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
776 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
777 waitSemaphores = [vulkan.imageAvailableSemaphores[currentFrame]]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
778 waitStages = [VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
779 signalSemaphores = [vulkan.renderFinishedSemaphores[currentFrame]]
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
780 submitInfo = VkSubmitInfo(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
781 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
782 waitSemaphoreCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
783 pWaitSemaphores: addr(waitSemaphores[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
784 pWaitDstStageMask: addr(waitStages[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
785 commandBufferCount: 1,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
786 pCommandBuffers: addr(vulkan.device.commandBuffers[currentFrame]),
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
787 signalSemaphoreCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
788 pSignalSemaphores: addr(signalSemaphores[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
789 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
790 checkVkResult vkQueueSubmit(vulkan.device.graphicsQueue, 1, addr(submitInfo),
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
791 vulkan.inFlightFences[currentFrame])
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
792
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
793 var presentInfo = VkPresentInfoKHR(
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
794 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
795 waitSemaphoreCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
796 pWaitSemaphores: addr(signalSemaphores[0]),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
797 swapchainCount: 1,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
798 pSwapchains: addr(vulkan.swapchain.swapchain),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
799 pImageIndices: addr(bufferImageIndex),
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
800 pResults: nil,
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
801 )
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
802 let presentResult = vkQueuePresentKHR(vulkan.device.presentationQueue, addr(presentInfo))
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
803
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
804 if presentResult == VK_ERROR_OUT_OF_DATE_KHR or presentResult ==
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
805 VK_SUBOPTIMAL_KHR or resized:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
806 vulkan.frameSize = window.getFrameDimension(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
807 vulkan.device.physicalDevice.device, vulkan.surface)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
808 (vulkan.swapchain, vulkan.framebuffers) = vulkan.recreateSwapchain()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
809
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
810
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
811 proc run*(engine: var Engine, pipeline: var RenderPipeline, globalUpdate: proc(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
812 engine: var Engine, dt: float32)) =
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
813 var
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
814 currentFrame = 0
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
815 resized = false
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
816 lastUpdate = getTime()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
817
512
9fd6a78c94a2 fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents: 507
diff changeset
818 while true:
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
819 # process input
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
820 engine.input.keysPressed = {}
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
821 engine.input.keysReleased = {}
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
822 engine.input.mousePressed = {}
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
823 engine.input.mouseReleased = {}
512
9fd6a78c94a2 fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents: 507
diff changeset
824 var killed = false
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
825 for event in engine.window.pendingEvents():
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
826 case event.eventType:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
827 of Quit:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
828 killed = true
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
829 of ResizedWindow:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
830 resized = true
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
831 of KeyPressed:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
832 engine.input.keysPressed.incl event.key
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
833 engine.input.keysDown.incl event.key
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
834 of KeyReleased:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
835 engine.input.keysReleased.incl event.key
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
836 engine.input.keysDown.excl event.key
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
837 of MousePressed:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
838 engine.input.mousePressed.incl event.button
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
839 engine.input.mouseDown.incl event.button
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
840 of MouseReleased:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
841 engine.input.mouseReleased.incl event.button
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
842 engine.input.mouseDown.excl event.button
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
843 of MouseMoved:
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
844 engine.input.mousePos = Vec2([float32(event.x), float32(event.y)])
512
9fd6a78c94a2 fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents: 507
diff changeset
845 if killed: # at least on windows we should return immediately as swapchain recreation will fail after kill
9fd6a78c94a2 fix: quit early to prevent swapchain recreation after program termination
Sam <sam@basx.dev>
parents: 507
diff changeset
846 break
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
847
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
848 # game logic update
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
849 let
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
850 now = getTime()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
851 dt = float32(float64((now - lastUpdate).inNanoseconds) / 1_000_000_000'f64)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
852 lastUpdate = now
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
853 engine.globalUpdate(dt)
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
854 for thing in allThings(engine.currentscenedata):
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
855 for part in thing.parts:
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
856 part.update(dt)
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 513
diff changeset
857 thing.update(dt)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
858
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
859 # submit frame for drawing
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
860 engine.window.drawFrame(engine.vulkan, currentFrame, resized, pipeline)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
861 resized = false
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
862 currentFrame = (currentFrame + 1) mod MAX_FRAMES_IN_FLIGHT
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
863 checkVkResult vkDeviceWaitIdle(engine.vulkan.device.device)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
864
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
865 proc trash*(pipeline: var RenderPipeline) =
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
866 vkDestroyDescriptorPool(pipeline.device, pipeline.descriptorPool, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
867 vkDestroyDescriptorSetLayout(pipeline.device, pipeline.descriptorSetLayout, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
868 vkDestroyPipeline(pipeline.device, pipeline.pipeline, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
869 vkDestroyPipelineLayout(pipeline.device, pipeline.layout, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
870 for shader in pipeline.shaders:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
871 vkDestroyShaderModule(pipeline.device, shader.shader.module, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
872
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
873 for (bufferset, cnt) in pipeline.vertexBuffers.mitems:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
874 for buffer in bufferset.mitems:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
875 buffer.trash()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
876 for (bufferset, indexbuffer, cnt, t) in pipeline.indexedVertexBuffers.mitems:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
877 indexbuffer.trash()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
878 for buffer in bufferset.mitems:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
879 buffer.trash()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
880 for buffer in pipeline.uniformBuffers.mitems:
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
881 buffer.trash()
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
882
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
883 proc trash*(engine: var Engine) =
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
884 checkVkResult vkDeviceWaitIdle(engine.vulkan.device.device)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
885 engine.vulkan.device.device.trash(engine.vulkan.swapchain,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
886 engine.vulkan.framebuffers)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
887
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
888 for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
889 engine.vulkan.device.device.vkDestroySemaphore(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
890 engine.vulkan.imageAvailableSemaphores[i], nil)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
891 engine.vulkan.device.device.vkDestroySemaphore(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
892 engine.vulkan.renderFinishedSemaphores[i], nil)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
893 engine.vulkan.device.device.vkDestroyFence(engine.vulkan.inFlightFences[i], nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
894
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
895 engine.vulkan.device.device.vkDestroyRenderPass(engine.vulkan.renderPass, nil)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
896 engine.vulkan.device.device.vkDestroyCommandPool(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
897 engine.vulkan.device.commandPool, nil)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
898
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
899 engine.vulkan.instance.vkDestroySurfaceKHR(engine.vulkan.surface, nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
900 engine.vulkan.device.device.vkDestroyDevice(nil)
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
901 when DEBUG_LOG:
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
902 engine.vulkan.instance.vkDestroyDebugUtilsMessengerEXT(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
903 engine.vulkan.debugMessenger, nil)
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents:
diff changeset
904 engine.window.trash()
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
905 engine.vulkan.instance.vkDestroyInstance(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 521
diff changeset
906 nil) # needs to happen after window is trashed as the driver might have a hook registered for the window destruction