forked from vkngwrapper/vulkan-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18_vertex_buffer.diff
131 lines (123 loc) · 3.64 KB
/
18_vertex_buffer.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
diff --git a/../steps/17_vertex_input/main.go b/../steps/18_vertex_buffer/main.go
index 249e800..24487c7 100644
--- a/../steps/17_vertex_input/main.go
+++ b/../steps/18_vertex_buffer/main.go
@@ -1,7 +1,9 @@
package main
import (
+ "bytes"
"embed"
+ "encoding/binary"
"github.com/cockroachdb/errors"
"github.com/veandco/go-sdl2/sdl"
"github.com/vkngwrapper/core/v2"
@@ -116,6 +118,9 @@ type HelloTriangleApplication struct {
inFlightFence []core1_0.Fence
imagesInFlight []core1_0.Fence
currentFrame int
+
+ vertexBuffer core1_0.Buffer
+ vertexBufferMemory core1_0.DeviceMemory
}
func (app *HelloTriangleApplication) Run() error {
@@ -208,6 +213,11 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.createVertexBuffer()
+ if err != nil {
+ return err
+ }
+
err = app.createCommandBuffers()
if err != nil {
return err
@@ -294,6 +304,14 @@ func (app *HelloTriangleApplication) cleanupSwapChain() {
func (app *HelloTriangleApplication) cleanup() {
app.cleanupSwapChain()
+ if app.vertexBuffer != nil {
+ app.vertexBuffer.Destroy(nil)
+ }
+
+ if app.vertexBufferMemory != nil {
+ app.vertexBufferMemory.Free(nil)
+ }
+
for _, fence := range app.inFlightFence {
fence.Destroy(nil)
}
@@ -879,6 +897,69 @@ func (app *HelloTriangleApplication) createCommandPool() error {
return nil
}
+func (app *HelloTriangleApplication) createVertexBuffer() error {
+ var err error
+ bufferSize := binary.Size(vertices)
+ app.vertexBuffer, _, err = app.device.CreateBuffer(nil, core1_0.BufferCreateInfo{
+ Size: bufferSize,
+ Usage: core1_0.BufferUsageVertexBuffer,
+ SharingMode: core1_0.SharingModeExclusive,
+ })
+ if err != nil {
+ return err
+ }
+
+ memRequirements := app.vertexBuffer.MemoryRequirements()
+ memoryTypeIndex, err := app.findMemoryType(memRequirements.MemoryTypeBits, core1_0.MemoryPropertyHostVisible|core1_0.MemoryPropertyHostCoherent)
+ if err != nil {
+ return err
+ }
+
+ app.vertexBufferMemory, _, err = app.device.AllocateMemory(nil, core1_0.MemoryAllocateInfo{
+ AllocationSize: memRequirements.Size,
+ MemoryTypeIndex: memoryTypeIndex,
+ })
+ if err != nil {
+ return err
+ }
+
+ _, err = app.vertexBuffer.BindBufferMemory(app.vertexBufferMemory, 0)
+ if err != nil {
+ return err
+ }
+
+ memory, _, err := app.vertexBufferMemory.Map(0, bufferSize, 0)
+ if err != nil {
+ return err
+ }
+ defer app.vertexBufferMemory.Unmap()
+
+ dataBuffer := unsafe.Slice((*byte)(memory), bufferSize)
+
+ buf := &bytes.Buffer{}
+ err = binary.Write(buf, common.ByteOrder, vertices)
+ if err != nil {
+ return err
+ }
+
+ copy(dataBuffer, buf.Bytes())
+
+ return nil
+}
+
+func (app *HelloTriangleApplication) findMemoryType(typeFilter uint32, properties core1_0.MemoryPropertyFlags) (int, error) {
+ memProperties := app.physicalDevice.MemoryProperties()
+ for i, memoryType := range memProperties.MemoryTypes {
+ typeBit := uint32(1 << i)
+
+ if (typeFilter&typeBit) != 0 && (memoryType.PropertyFlags&properties) == properties {
+ return i, nil
+ }
+ }
+
+ return 0, errors.New("failed to find any suitable memory type!")
+}
+
func (app *HelloTriangleApplication) createCommandBuffers() error {
buffers, _, err := app.device.AllocateCommandBuffers(core1_0.CommandBufferAllocateInfo{
@@ -914,7 +995,8 @@ func (app *HelloTriangleApplication) createCommandBuffers() error {
}
buffer.CmdBindPipeline(core1_0.PipelineBindPointGraphics, app.graphicsPipeline)
- buffer.CmdDraw(3, 1, 0, 0)
+ buffer.CmdBindVertexBuffers(0, []core1_0.Buffer{app.vertexBuffer}, []int{0})
+ buffer.CmdDraw(len(vertices), 1, 0, 0)
buffer.CmdEndRenderPass()
_, err = buffer.End()