annotate src/semicongine/vulkan/swapchain.nim @ 590:1edf3e16144e

add: input handling, small refactoring for renderer
author Sam <sam@basx.dev>
date Tue, 18 Apr 2023 03:04:37 +0700
parents b434feaf2b67
children 11666d28e04d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
1 import std/options
574
bbeec60e25ca did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 573
diff changeset
2 import std/logging
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
3
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
4 import ./api
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
5 import ./utils
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./device
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
7 import ./physicaldevice
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
8 import ./image
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
9 import ./renderpass
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
10 import ./framebuffer
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
11 import ./commandbuffer
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
12 import ./syncing
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
13
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
14 import ../math
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
15
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
16 type
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
17 Swapchain* = object
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
18 device*: Device
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
19 vk*: VkSwapchainKHR
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
20 format*: VkFormat
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
21 dimension*: TVec2[uint32]
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
22 renderPass*: RenderPass
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
23 nImages*: uint32
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
24 imageviews*: seq[ImageView]
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
25 framebuffers*: seq[Framebuffer]
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
26 currentInFlight*: int
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
27 currentFramebufferIndex: uint32
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
28 framesRendered*: uint64
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
29 queueFinishedFence: seq[Fence]
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
30 imageAvailableSemaphore*: seq[Semaphore]
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
31 renderFinishedSemaphore*: seq[Semaphore]
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
32 commandBufferPool: CommandBufferPool
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
33 inFlightFrames: int
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
34
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
35
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
36 proc createSwapchain*(
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
37 device: Device,
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
38 renderPass: RenderPass,
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
39 surfaceFormat: VkSurfaceFormatKHR,
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
40 queueFamily: QueueFamily,
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
41 desiredNumberOfImages=3'u32,
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
42 presentationMode: VkPresentModeKHR=VK_PRESENT_MODE_MAILBOX_KHR,
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
43 inFlightFrames=2
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
44 ): (Swapchain, VkResult) =
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
45 assert device.vk.valid
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
46 assert device.physicalDevice.vk.valid
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
47 assert renderPass.vk.valid
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
48 assert inFlightFrames > 0
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
49
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
50 var capabilities = device.physicalDevice.getSurfaceCapabilities()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
51
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
52 var imageCount = desiredNumberOfImages
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
53 # following is according to vulkan specs
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
54 if presentationMode in [VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR, VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR]:
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
55 imageCount = 1
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
56 else:
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
57 imageCount = max(imageCount, capabilities.minImageCount)
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
58 if capabilities.maxImageCount != 0:
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
59 imageCount = min(imageCount, capabilities.maxImageCount)
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
60
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
61 var createInfo = VkSwapchainCreateInfoKHR(
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
62 sType: VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
63 surface: device.physicalDevice.surface,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
64 minImageCount: imageCount,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
65 imageFormat: surfaceFormat.format,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
66 imageColorSpace: surfaceFormat.colorSpace,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
67 imageExtent: capabilities.currentExtent,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
68 imageArrayLayers: 1,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
69 imageUsage: toBits [VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT],
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
70 # VK_SHARING_MODE_CONCURRENT no supported currently
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
71 imageSharingMode: VK_SHARING_MODE_EXCLUSIVE,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
72 preTransform: capabilities.currentTransform,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
73 compositeAlpha: VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, # only used for blending with other windows, can be opaque
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
74 presentMode: presentationMode,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
75 clipped: true,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
76 )
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
77 var
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
78 swapchain = Swapchain(
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
79 device: device,
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
80 format: surfaceFormat.format,
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
81 dimension: TVec2[uint32]([capabilities.currentExtent.width, capabilities.currentExtent.height]),
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
82 renderPass: renderPass,
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
83 inFlightFrames: inFlightFrames
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
84 )
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
85 createResult = device.vk.vkCreateSwapchainKHR(addr(createInfo), nil, addr(swapchain.vk))
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
86
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
87 if createResult == VK_SUCCESS:
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
88 var nImages: uint32
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
89 checkVkResult device.vk.vkGetSwapchainImagesKHR(swapChain.vk, addr(nImages), nil)
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
90 swapchain.nImages = nImages
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
91 var images = newSeq[VkImage](nImages)
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
92 checkVkResult device.vk.vkGetSwapchainImagesKHR(swapChain.vk, addr(nImages), images.toCPointer)
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
93 for vkimage in images:
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
94 let image = Image(vk: vkimage, format: surfaceFormat.format, device: device)
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
95 let imageview = image.createImageView()
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
96 swapChain.imageviews.add imageview
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
97 swapChain.framebuffers.add swapchain.device.createFramebuffer(renderPass.vk, [imageview], swapchain.dimension)
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
98 for i in 0 ..< swapchain.inFlightFrames:
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
99 swapchain.queueFinishedFence.add device.createFence()
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
100 swapchain.imageAvailableSemaphore.add device.createSemaphore()
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
101 swapchain.renderFinishedSemaphore.add device.createSemaphore()
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
102 swapchain.commandBufferPool = device.createCommandBufferPool(queueFamily, swapchain.inFlightFrames)
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
103
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
104 return (swapchain, createResult)
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
105
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
106 proc currentFramebuffer*(swapchain: Swapchain): Framebuffer =
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
107 swapchain.framebuffers[swapchain.currentFramebufferIndex]
572
9f1de117aaca did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents: 571
diff changeset
108
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
109 proc nextFrame*(swapchain: var Swapchain): VkCommandBuffer =
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
110 assert swapchain.device.vk.valid
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
111 assert swapchain.vk.valid
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
112 assert swapchain.device.firstGraphicsQueue().isSome
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
113 assert swapchain.device.firstPresentationQueue().isSome
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
114
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
115 swapchain.currentInFlight = (swapchain.currentInFlight + 1) mod swapchain.inFlightFrames
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
116 swapchain.queueFinishedFence[swapchain.currentInFlight].wait()
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
117
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
118 let nextImageResult = swapchain.device.vk.vkAcquireNextImageKHR(
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
119 swapchain.vk,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
120 high(uint64),
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
121 swapchain.imageAvailableSemaphore[swapchain.currentInFlight].vk,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
122 VkFence(0),
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
123 addr(swapchain.currentFramebufferIndex)
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
124 )
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 589
diff changeset
125 # TODO: resize swapchain on VK_SUBOPTIMAL_KHR
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 589
diff changeset
126 echo "HIIIIIIIIIIII next image result", nextImageResult
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
127 if not (nextImageResult in [VK_SUCCESS, VK_TIMEOUT, VK_NOT_READY, VK_SUBOPTIMAL_KHR]):
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
128 return
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
129
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
130 swapchain.queueFinishedFence[swapchain.currentInFlight].reset()
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
131
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
132 return swapchain.commandBufferPool.buffers[swapchain.currentInFlight]
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
133
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
134 proc swap*(swapchain: var Swapchain): bool =
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
135 var
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
136 commandBuffer = swapchain.commandBufferPool.buffers[swapchain.currentInFlight]
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
137 waitSemaphores = [swapchain.imageAvailableSemaphore[swapchain.currentInFlight].vk]
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
138 waitStages = [VkPipelineStageFlags(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)]
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
139 submitInfo = VkSubmitInfo(
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
140 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
141 waitSemaphoreCount: 1,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
142 pWaitSemaphores: addr(waitSemaphores[0]),
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
143 pWaitDstStageMask: addr(waitStages[0]),
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
144 commandBufferCount: 1,
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
145 pCommandBuffers: addr(commandBuffer),
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
146 signalSemaphoreCount: 1,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
147 pSignalSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentInFlight].vk),
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
148 )
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
149 checkVkResult vkQueueSubmit(
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
150 swapchain.device.firstGraphicsQueue().get.vk,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
151 1,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
152 addr(submitInfo),
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
153 swapchain.queueFinishedFence[swapchain.currentInFlight].vk
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
154 )
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
155
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
156 var presentInfo = VkPresentInfoKHR(
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
157 sType: VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
158 waitSemaphoreCount: 1,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
159 pWaitSemaphores: addr(swapchain.renderFinishedSemaphore[swapchain.currentInFlight].vk),
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
160 swapchainCount: 1,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
161 pSwapchains: addr(swapchain.vk),
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
162 pImageIndices: addr(swapchain.currentFramebufferIndex),
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
163 pResults: nil,
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
164 )
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
165 let presentResult = vkQueuePresentKHR(swapchain.device.firstPresentationQueue().get().vk, addr(presentInfo))
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 589
diff changeset
166 # TODO: resize swapchain on VK_SUBOPTIMAL_KHR
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 589
diff changeset
167 echo "HIIIIIIIIIIII Present Result:", presentResult
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
168 if not (presentResult in [VK_SUCCESS, VK_SUBOPTIMAL_KHR]):
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
169 return false
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
170
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
171 inc swapchain.framesRendered
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
172 return true
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
173
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
174
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
175 proc destroy*(swapchain: var Swapchain) =
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
176 assert swapchain.vk.valid
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
177 assert swapchain.commandBufferPool.vk.valid
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
178
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
179 for imageview in swapchain.imageviews.mitems:
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
180 assert imageview.vk.valid
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
181 imageview.destroy()
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
182 for framebuffer in swapchain.framebuffers.mitems:
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
183 assert framebuffer.vk.valid
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 559
diff changeset
184 framebuffer.destroy()
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
185 swapchain.commandBufferPool.destroy()
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 585
diff changeset
186 for i in 0 ..< swapchain.inFlightFrames:
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
187 assert swapchain.queueFinishedFence[i].vk.valid
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
188 assert swapchain.imageAvailableSemaphore[i].vk.valid
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
189 assert swapchain.renderFinishedSemaphore[i].vk.valid
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
190 swapchain.queueFinishedFence[i].destroy()
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
191 swapchain.imageAvailableSemaphore[i].destroy()
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
192 swapchain.renderFinishedSemaphore[i].destroy()
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 581
diff changeset
193
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents:
diff changeset
194 swapchain.device.vk.vkDestroySwapchainKHR(swapchain.vk, nil)
558
affa6571a2c9 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 557
diff changeset
195 swapchain.vk.reset()