-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path20_index_buffer.diff
More file actions
103 lines (94 loc) · 3.62 KB
/
Copy path20_index_buffer.diff
File metadata and controls
103 lines (94 loc) · 3.62 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
diff --git a/../steps/19_staging_buffer/main.go b/../steps/20_index_buffer/main.go
index ddb8bcf..9fbfac7 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 {
globalDriver core1_0.GlobalDriver
instanceDriver core1_0.CoreInstanceDriver
@@ -123,6 +126,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 {
@@ -220,6 +225,11 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.createIndexBuffer()
+ if err != nil {
+ return err
+ }
+
err = app.createCommandBuffers()
if err != nil {
return err
@@ -309,6 +319,14 @@ func (app *HelloTriangleApplication) cleanupSwapChain() {
func (app *HelloTriangleApplication) cleanup() {
app.cleanupSwapChain()
+ if app.indexBuffer.Initialized() {
+ app.deviceDriver.DestroyBuffer(app.indexBuffer, nil)
+ }
+
+ if app.indexBufferMemory.Initialized() {
+ app.deviceDriver.FreeMemory(app.indexBufferMemory, nil)
+ }
+
if app.vertexBuffer.Initialized() {
app.deviceDriver.DestroyBuffer(app.vertexBuffer, nil)
}
@@ -952,6 +970,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.Initialized() {
+ defer app.deviceDriver.DestroyBuffer(stagingBuffer, nil)
+ }
+ if stagingBufferMemory.Initialized() {
+ defer app.deviceDriver.FreeMemory(stagingBufferMemory, nil)
+ }
+
+ if err != nil {
+ return err
+ }
+
+ err = writeData(app.deviceDriver, 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.deviceDriver.CreateBuffer(nil, core1_0.BufferCreateInfo{
Size: size,
@@ -1076,7 +1122,8 @@ func (app *HelloTriangleApplication) createCommandBuffers() error {
app.deviceDriver.CmdBindPipeline(buffer, core1_0.PipelineBindPointGraphics, app.graphicsPipeline)
app.deviceDriver.CmdBindVertexBuffers(buffer, 0, []core1_0.Buffer{app.vertexBuffer}, []int{0})
- app.deviceDriver.CmdDraw(buffer, 3, 1, 0, 0)
+ app.deviceDriver.CmdBindIndexBuffer(buffer, app.indexBuffer, 0, core1_0.IndexTypeUInt16)
+ app.deviceDriver.CmdDrawIndexed(buffer, len(indices), 1, 0, 0, 0)
app.deviceDriver.CmdEndRenderPass(buffer)
_, err = app.deviceDriver.EndCommandBuffer(buffer)