-
Notifications
You must be signed in to change notification settings - Fork 2
/
21_descriptor_layout.diff
262 lines (243 loc) · 8.11 KB
/
21_descriptor_layout.diff
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
diff --git a/../steps/20_index_buffer/main.go b/../steps/21_descriptor_layout/main.go
index 071d37b..7bd6a0b 100644
--- a/../steps/20_index_buffer/main.go
+++ b/../steps/21_descriptor_layout/main.go
@@ -4,6 +4,7 @@ import (
"bytes"
"embed"
"encoding/binary"
+ "github.com/loov/hrtime"
"github.com/pkg/errors"
"github.com/veandco/go-sdl2/sdl"
"github.com/vkngwrapper/core/v2"
@@ -15,7 +16,9 @@ import (
"github.com/vkngwrapper/extensions/v2/khr_surface"
"github.com/vkngwrapper/extensions/v2/khr_swapchain"
vkng_sdl2 "github.com/vkngwrapper/integrations/sdl2/v2"
+ vkngmath "github.com/vkngwrapper/math"
"log"
+ "math"
"unsafe"
)
@@ -45,8 +48,14 @@ type SwapChainSupportDetails struct {
}
type Vertex struct {
- X, Y float32 // Could also be Position Vector2 - as long as Vector2 is a value, not a pointer
- R, G, B float32 // Could also be Color Vector3
+ Position vkngmath.Vec2[float32]
+ Color vkngmath.Vec3[float32]
+}
+
+type UniformBufferObject struct {
+ Model vkngmath.Mat4x4[float32]
+ View vkngmath.Mat4x4[float32]
+ Proj vkngmath.Mat4x4[float32]
}
func getVertexBindingDescription() []core1_0.VertexInputBindingDescription {
@@ -66,23 +75,23 @@ func getVertexAttributeDescriptions() []core1_0.VertexInputAttributeDescription
{
Binding: 0,
Location: 0,
- Format: core1_0.FormatR32G32SignedFloat,
- Offset: int(unsafe.Offsetof(v.X)),
+ Format: core1_0.FormatR32G32B32SignedFloat,
+ Offset: int(unsafe.Offsetof(v.Position)),
},
{
Binding: 0,
Location: 1,
Format: core1_0.FormatR32G32B32SignedFloat,
- Offset: int(unsafe.Offsetof(v.R)),
+ Offset: int(unsafe.Offsetof(v.Color)),
},
}
}
var vertices = []Vertex{
- {X: -0.5, Y: -0.5, R: 1, G: 0, B: 0},
- {X: 0.5, Y: -0.5, R: 0, G: 1, B: 0},
- {X: 0.5, Y: 0.5, R: 0, G: 0, B: 1},
- {X: -0.5, Y: 0.5, R: 1, G: 1, B: 1},
+ {Position: vkngmath.Vec2[float32]{X: -0.5, Y: -0.5}, Color: vkngmath.Vec3[float32]{X: 1, Y: 0, Z: 0}},
+ {Position: vkngmath.Vec2[float32]{X: 0.5, Y: -0.5}, Color: vkngmath.Vec3[float32]{X: 0, Y: 1, Z: 0}},
+ {Position: vkngmath.Vec2[float32]{X: 0.5, Y: 0.5}, Color: vkngmath.Vec3[float32]{X: 0, Y: 0, Z: 1}},
+ {Position: vkngmath.Vec2[float32]{X: -0.5, Y: 0.5}, Color: vkngmath.Vec3[float32]{X: 1, Y: 1, Z: 1}},
}
var indices = []uint16{0, 1, 2, 2, 3, 0}
@@ -109,9 +118,10 @@ type HelloTriangleApplication struct {
swapchainImageViews []core1_0.ImageView
swapchainFramebuffers []core1_0.Framebuffer
- renderPass core1_0.RenderPass
- pipelineLayout core1_0.PipelineLayout
- graphicsPipeline core1_0.Pipeline
+ renderPass core1_0.RenderPass
+ descriptorSetLayout core1_0.DescriptorSetLayout
+ pipelineLayout core1_0.PipelineLayout
+ graphicsPipeline core1_0.Pipeline
commandPool core1_0.CommandPool
commandBuffers []core1_0.CommandBuffer
@@ -121,11 +131,15 @@ type HelloTriangleApplication struct {
inFlightFence []core1_0.Fence
imagesInFlight []core1_0.Fence
currentFrame int
+ frameStart float64
vertexBuffer core1_0.Buffer
vertexBufferMemory core1_0.DeviceMemory
indexBuffer core1_0.Buffer
indexBufferMemory core1_0.DeviceMemory
+
+ uniformBuffers []core1_0.Buffer
+ uniformBuffersMemory []core1_0.DeviceMemory
}
func (app *HelloTriangleApplication) Run() error {
@@ -203,6 +217,11 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.createDescriptorSetLayout()
+ if err != nil {
+ return err
+ }
+
err = app.createGraphicsPipeline()
if err != nil {
return err
@@ -228,6 +247,11 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.createUniformBuffers()
+ if err != nil {
+ return err
+ }
+
err = app.createCommandBuffers()
if err != nil {
return err
@@ -309,11 +333,25 @@ func (app *HelloTriangleApplication) cleanupSwapChain() {
app.swapchain.Destroy(nil)
app.swapchain = nil
}
+
+ for i := 0; i < len(app.uniformBuffers); i++ {
+ app.uniformBuffers[i].Destroy(nil)
+ }
+ app.uniformBuffers = app.uniformBuffers[:0]
+
+ for i := 0; i < len(app.uniformBuffersMemory); i++ {
+ app.uniformBuffersMemory[i].Free(nil)
+ }
+ app.uniformBuffersMemory = app.uniformBuffersMemory[:0]
}
func (app *HelloTriangleApplication) cleanup() {
app.cleanupSwapChain()
+ if app.descriptorSetLayout != nil {
+ app.descriptorSetLayout.Destroy(nil)
+ }
+
if app.indexBuffer != nil {
app.indexBuffer.Destroy(nil)
}
@@ -409,6 +447,11 @@ func (app *HelloTriangleApplication) recreateSwapChain() error {
return err
}
+ err = app.createUniformBuffers()
+ if err != nil {
+ return err
+ }
+
err = app.createCommandBuffers()
if err != nil {
return err
@@ -729,6 +772,26 @@ func (app *HelloTriangleApplication) createRenderPass() error {
return nil
}
+func (app *HelloTriangleApplication) createDescriptorSetLayout() error {
+ var err error
+ app.descriptorSetLayout, _, err = app.device.CreateDescriptorSetLayout(nil, core1_0.DescriptorSetLayoutCreateInfo{
+ Bindings: []core1_0.DescriptorSetLayoutBinding{
+ {
+ Binding: 0,
+ DescriptorType: core1_0.DescriptorTypeUniformBuffer,
+ DescriptorCount: 1,
+
+ StageFlags: core1_0.StageVertex,
+ },
+ },
+ })
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
func bytesToBytecode(b []byte) []uint32 {
byteCode := make([]uint32, len(b)/4)
for i := 0; i < len(byteCode); i++ {
@@ -845,7 +908,11 @@ func (app *HelloTriangleApplication) createGraphicsPipeline() error {
},
}
- app.pipelineLayout, _, err = app.device.CreatePipelineLayout(nil, core1_0.PipelineLayoutCreateInfo{})
+ app.pipelineLayout, _, err = app.device.CreatePipelineLayout(nil, core1_0.PipelineLayoutCreateInfo{
+ SetLayouts: []core1_0.DescriptorSetLayout{
+ app.descriptorSetLayout,
+ },
+ })
if err != nil {
return err
}
@@ -993,6 +1060,22 @@ func (app *HelloTriangleApplication) createIndexBuffer() error {
return app.copyBuffer(stagingBuffer, app.indexBuffer, bufferSize)
}
+func (app *HelloTriangleApplication) createUniformBuffers() error {
+ bufferSize := int(unsafe.Sizeof(UniformBufferObject{}))
+
+ for i := 0; i < len(app.swapchainImages); i++ {
+ buffer, memory, err := app.createBuffer(bufferSize, core1_0.BufferUsageUniformBuffer, core1_0.MemoryPropertyHostVisible|core1_0.MemoryPropertyHostCoherent)
+ if err != nil {
+ return err
+ }
+
+ app.uniformBuffers = append(app.uniformBuffers, buffer)
+ app.uniformBuffersMemory = append(app.uniformBuffersMemory, memory)
+ }
+
+ return nil
+}
+
func (app *HelloTriangleApplication) createBuffer(size int, usage core1_0.BufferUsageFlags, properties core1_0.MemoryPropertyFlags) (core1_0.Buffer, core1_0.DeviceMemory, error) {
buffer, _, err := app.device.CreateBuffer(nil, core1_0.BufferCreateInfo{
Size: size,
@@ -1189,6 +1272,11 @@ func (app *HelloTriangleApplication) drawFrame() error {
return err
}
+ err = app.updateUniformBuffer(imageIndex)
+ if err != nil {
+ return err
+ }
+
_, err = app.graphicsQueue.Submit(app.inFlightFence[app.currentFrame], []core1_0.SubmitInfo{
{
WaitSemaphores: []core1_0.Semaphore{app.imageAvailableSemaphore[app.currentFrame]},
@@ -1217,6 +1305,24 @@ func (app *HelloTriangleApplication) drawFrame() error {
return nil
}
+func (app *HelloTriangleApplication) updateUniformBuffer(currentImage int) error {
+ currentTime := hrtime.Now().Seconds()
+ timePeriod := math.Mod(currentTime, 4.0)
+
+ ubo := UniformBufferObject{}
+ ubo.Model.SetRotationZ(timePeriod * math.Pi / 2.0)
+ ubo.View.SetLookAt(
+ &vkngmath.Vec3[float32]{X: 2, Y: 2, Z: 2},
+ &vkngmath.Vec3[float32]{X: 0, Y: 0, Z: 0},
+ &vkngmath.Vec3[float32]{X: 0, Y: 0, Z: 1},
+ )
+ aspectRatio := float32(app.swapchainExtent.Width) / float32(app.swapchainExtent.Height)
+ ubo.Proj.SetPerspective(math.Pi/4.0, aspectRatio, 0.1, 10)
+
+ err := writeData(app.uniformBuffersMemory[currentImage], 0, &ubo)
+ return err
+}
+
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 {