annotate src/zamikongine/engine.nim @ 489:54a1f8ee208e

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