-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path20_index_buffer.diff
More file actions
129 lines (115 loc) · 4.14 KB
/
20_index_buffer.diff
File metadata and controls
129 lines (115 loc) · 4.14 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
diff --git a/../steps/19_staging_buffer/main.go b/../steps/20_index_buffer/main.go
index 6c65f99..2cda176 100644
--- a/../steps/19_staging_buffer/main.go
+++ b/../steps/20_index_buffer/main.go
@@ -80,11 +80,14 @@ func getVertexAttributeDescriptions() []core1_0.VertexInputAttributeDescription
}
var vertices = []Vertex{
- {X: 0, 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: 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},
}
+var indices = []uint16{0, 1, 2, 2, 3, 0}
+
type HelloTriangleApplication struct {
window *sdl.Window
loader core.Loader
@@ -122,6 +125,8 @@ type HelloTriangleApplication struct {
vertexBuffer core1_0.Buffer
vertexBufferMemory core1_0.DeviceMemory
+ indexBuffer core1_0.Buffer
+ indexBufferMemory core1_0.DeviceMemory
}
func (app *HelloTriangleApplication) Run() error {
@@ -219,6 +224,11 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.createIndexBuffer()
+ if err != nil {
+ return err
+ }
+
err = app.createCommandBuffers()
if err != nil {
return err
@@ -305,6 +315,14 @@ func (app *HelloTriangleApplication) cleanupSwapChain() {
func (app *HelloTriangleApplication) cleanup() {
app.cleanupSwapChain()
+ if app.indexBuffer != nil {
+ app.indexBuffer.Destroy(nil)
+ }
+
+ if app.indexBufferMemory != nil {
+ app.indexBufferMemory.Free(nil)
+ }
+
if app.vertexBuffer != nil {
app.vertexBuffer.Destroy(nil)
}
@@ -515,7 +533,7 @@ func (app *HelloTriangleApplication) pickPhysicalDevice() error {
}
if app.physicalDevice == nil {
- return errors.New("failed to find a suitable GPU!")
+ return errors.Errorf("failed to find a suitable GPU!")
}
return nil
@@ -948,6 +966,34 @@ func (app *HelloTriangleApplication) createVertexBuffer() error {
return app.copyBuffer(stagingBuffer, app.vertexBuffer, bufferSize)
}
+func (app *HelloTriangleApplication) createIndexBuffer() error {
+ bufferSize := binary.Size(indices)
+
+ stagingBuffer, stagingBufferMemory, err := app.createBuffer(bufferSize, core1_0.BufferUsageTransferSrc, core1_0.MemoryPropertyHostVisible|core1_0.MemoryPropertyHostCoherent)
+ if stagingBuffer != nil {
+ defer stagingBuffer.Destroy(nil)
+ }
+ if stagingBufferMemory != nil {
+ defer stagingBufferMemory.Free(nil)
+ }
+
+ if err != nil {
+ return err
+ }
+
+ err = writeData(stagingBufferMemory, 0, indices)
+ if err != nil {
+ return err
+ }
+
+ app.indexBuffer, app.indexBufferMemory, err = app.createBuffer(bufferSize, core1_0.BufferUsageTransferDst|core1_0.BufferUsageIndexBuffer, core1_0.MemoryPropertyDeviceLocal)
+ if err != nil {
+ return err
+ }
+
+ return app.copyBuffer(stagingBuffer, app.indexBuffer, bufferSize)
+}
+
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,
@@ -959,7 +1005,6 @@ func (app *HelloTriangleApplication) createBuffer(size int, usage core1_0.Buffer
}
memRequirements := buffer.MemoryRequirements()
-
memoryTypeIndex, err := app.findMemoryType(memRequirements.MemoryTypeBits, properties)
if err != nil {
return buffer, nil, err
@@ -1032,7 +1077,7 @@ func (app *HelloTriangleApplication) findMemoryType(typeFilter uint32, propertie
}
}
- return 0, errors.New("failed to find any suitable memory type!")
+ return 0, errors.Errorf("failed to find any suitable memory type!")
}
func (app *HelloTriangleApplication) createCommandBuffers() error {
@@ -1071,7 +1116,8 @@ func (app *HelloTriangleApplication) createCommandBuffers() error {
buffer.CmdBindPipeline(core1_0.PipelineBindPointGraphics, app.graphicsPipeline)
buffer.CmdBindVertexBuffers(0, []core1_0.Buffer{app.vertexBuffer}, []int{0})
- buffer.CmdDraw(len(vertices), 1, 0, 0)
+ buffer.CmdBindIndexBuffer(app.indexBuffer, 0, core1_0.IndexTypeUInt16)
+ buffer.CmdDrawIndexed(len(indices), 1, 0, 0, 0)
buffer.CmdEndRenderPass()
_, err = buffer.End()