-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path15_hello_triangle.diff
More file actions
199 lines (186 loc) · 5.46 KB
/
15_hello_triangle.diff
File metadata and controls
199 lines (186 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
diff --git a/../steps/14_command_buffers/main.go b/../steps/15_hello_triangle/main.go
index f3f8d35..172ae82 100644
--- a/../steps/14_command_buffers/main.go
+++ b/../steps/15_hello_triangle/main.go
@@ -2,6 +2,8 @@ package main
import (
"embed"
+ "log"
+
"github.com/pkg/errors"
"github.com/veandco/go-sdl2/sdl"
"github.com/vkngwrapper/core/v2"
@@ -13,12 +15,13 @@ import (
"github.com/vkngwrapper/extensions/v2/khr_surface"
"github.com/vkngwrapper/extensions/v2/khr_swapchain"
vkng_sdl2 "github.com/vkngwrapper/integrations/sdl2/v2"
- "log"
)
//go:embed shaders
var shaders embed.FS
+const MaxFramesInFlight = 2
+
var validationLayers = []string{"VK_LAYER_KHRONOS_validation"}
var deviceExtensions = []string{khr_swapchain.ExtensionName}
@@ -67,6 +70,12 @@ type HelloTriangleApplication struct {
commandPool core1_0.CommandPool
commandBuffers []core1_0.CommandBuffer
+
+ imageAvailableSemaphore []core1_0.Semaphore
+ renderFinishedSemaphore []core1_0.Semaphore
+ inFlightFence []core1_0.Fence
+ imagesInFlight []core1_0.Fence
+ currentFrame int
}
func (app *HelloTriangleApplication) Run() error {
@@ -154,24 +163,46 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
- return app.createCommandBuffers()
+ err = app.createCommandBuffers()
+ if err != nil {
+ return err
+ }
+
+ return app.createSyncObjects()
}
func (app *HelloTriangleApplication) mainLoop() error {
appLoop:
- for true {
+ for {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
break appLoop
}
}
+ err := app.drawFrame()
+ if err != nil {
+ return err
+ }
}
- return nil
+ _, err := app.device.WaitIdle()
+ return err
}
func (app *HelloTriangleApplication) cleanup() {
+ for _, fence := range app.inFlightFence {
+ fence.Destroy(nil)
+ }
+
+ for _, semaphore := range app.renderFinishedSemaphore {
+ semaphore.Destroy(nil)
+ }
+
+ for _, semaphore := range app.imageAvailableSemaphore {
+ semaphore.Destroy(nil)
+ }
+
if app.commandPool != nil {
app.commandPool.Destroy(nil)
}
@@ -439,6 +470,7 @@ func (app *HelloTriangleApplication) createSwapchain() error {
}
app.swapchainExtent = extent
app.swapchain = swapchain
+ app.swapchainImageFormat = surfaceFormat.Format
images, _, err := swapchain.SwapchainImages()
if err != nil {
@@ -473,7 +505,6 @@ func (app *HelloTriangleApplication) createSwapchain() error {
imageViews = append(imageViews, view)
}
app.swapchainImageViews = imageViews
- app.swapchainImageFormat = surfaceFormat.Format
return nil
}
@@ -755,6 +786,91 @@ func (app *HelloTriangleApplication) createCommandBuffers() error {
return nil
}
+func (app *HelloTriangleApplication) createSyncObjects() error {
+ for i := 0; i < len(app.swapchainImages); i++ {
+ semaphore, _, err := app.device.CreateSemaphore(nil, core1_0.SemaphoreCreateInfo{})
+ if err != nil {
+ return err
+ }
+
+ app.imageAvailableSemaphore = append(app.imageAvailableSemaphore, semaphore)
+
+ fence, _, err := app.device.CreateFence(nil, core1_0.FenceCreateInfo{
+ Flags: core1_0.FenceCreateSignaled,
+ })
+ if err != nil {
+ return err
+ }
+
+ app.inFlightFence = append(app.inFlightFence, fence)
+ }
+
+ for i := 0; i < len(app.swapchainImages); i++ {
+ semaphore, _, err := app.device.CreateSemaphore(nil, core1_0.SemaphoreCreateInfo{})
+ if err != nil {
+ return err
+ }
+
+ app.renderFinishedSemaphore = append(app.renderFinishedSemaphore, semaphore)
+
+ app.imagesInFlight = append(app.imagesInFlight, nil)
+ }
+
+ return nil
+}
+
+func (app *HelloTriangleApplication) drawFrame() error {
+ fences := []core1_0.Fence{app.inFlightFence[app.currentFrame]}
+
+ _, err := app.device.WaitForFences(true, common.NoTimeout, fences)
+ if err != nil {
+ return err
+ }
+
+ imageIndex, _, err := app.swapchain.AcquireNextImage(common.NoTimeout, app.imageAvailableSemaphore[app.currentFrame], nil)
+ if err != nil {
+ return err
+ }
+
+ if app.imagesInFlight[imageIndex] != nil {
+ _, err := app.device.WaitForFences(true, common.NoTimeout, []core1_0.Fence{app.imagesInFlight[imageIndex]})
+ if err != nil {
+ return err
+ }
+ }
+ app.imagesInFlight[imageIndex] = app.inFlightFence[app.currentFrame]
+
+ _, err = app.device.ResetFences(fences)
+ if err != nil {
+ return err
+ }
+
+ _, err = app.graphicsQueue.Submit(app.inFlightFence[app.currentFrame], []core1_0.SubmitInfo{
+ {
+ WaitSemaphores: []core1_0.Semaphore{app.imageAvailableSemaphore[app.currentFrame]},
+ WaitDstStageMask: []core1_0.PipelineStageFlags{core1_0.PipelineStageColorAttachmentOutput},
+ CommandBuffers: []core1_0.CommandBuffer{app.commandBuffers[imageIndex]},
+ SignalSemaphores: []core1_0.Semaphore{app.renderFinishedSemaphore[imageIndex]},
+ },
+ })
+ if err != nil {
+ return err
+ }
+
+ _, err = app.swapchainExtension.QueuePresent(app.presentQueue, khr_swapchain.PresentInfo{
+ WaitSemaphores: []core1_0.Semaphore{app.renderFinishedSemaphore[imageIndex]},
+ Swapchains: []khr_swapchain.Swapchain{app.swapchain},
+ ImageIndices: []int{imageIndex},
+ })
+ if err != nil {
+ return err
+ }
+
+ app.currentFrame = (app.currentFrame + 1) % MaxFramesInFlight
+
+ return nil
+}
+
func (app *HelloTriangleApplication) chooseSwapSurfaceFormat(availableFormats []khr_surface.SurfaceFormat) khr_surface.SurfaceFormat {
for _, format := range availableFormats {
if format.Format == core1_0.FormatB8G8R8A8SRGB && format.ColorSpace == khr_surface.ColorSpaceSRGBNonlinear {