forked from vkngwrapper/vulkan-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_command_buffers.diff
116 lines (111 loc) · 3.03 KB
/
14_command_buffers.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
diff --git a/../steps/13_framebuffers/main.go b/../steps/14_command_buffers/main.go
index 34d50ec..c6fa94d 100644
--- a/../steps/13_framebuffers/main.go
+++ b/../steps/14_command_buffers/main.go
@@ -64,6 +64,9 @@ type HelloTriangleApplication struct {
renderPass core1_0.RenderPass
pipelineLayout core1_0.PipelineLayout
graphicsPipeline core1_0.Pipeline
+
+ commandPool core1_0.CommandPool
+ commandBuffers []core1_0.CommandBuffer
}
func (app *HelloTriangleApplication) Run() error {
@@ -141,7 +144,17 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
- return app.createFramebuffers()
+ err = app.createFramebuffers()
+ if err != nil {
+ return err
+ }
+
+ err = app.createCommandPool()
+ if err != nil {
+ return err
+ }
+
+ return app.createCommandBuffers()
}
func (app *HelloTriangleApplication) mainLoop() error {
@@ -159,6 +172,10 @@ appLoop:
}
func (app *HelloTriangleApplication) cleanup() {
+ if app.commandPool != nil {
+ app.commandPool.Destroy(nil)
+ }
+
for _, framebuffer := range app.swapchainFramebuffers {
framebuffer.Destroy(nil)
}
@@ -673,6 +690,71 @@ func (app *HelloTriangleApplication) createFramebuffers() error {
return nil
}
+func (app *HelloTriangleApplication) createCommandPool() error {
+ indices, err := app.findQueueFamilies(app.physicalDevice)
+ if err != nil {
+ return err
+ }
+
+ pool, _, err := app.device.CreateCommandPool(nil, core1_0.CommandPoolCreateInfo{
+ QueueFamilyIndex: *indices.GraphicsFamily,
+ })
+
+ if err != nil {
+ return err
+ }
+ app.commandPool = pool
+
+ return nil
+}
+
+func (app *HelloTriangleApplication) createCommandBuffers() error {
+
+ buffers, _, err := app.device.AllocateCommandBuffers(core1_0.CommandBufferAllocateInfo{
+ CommandPool: app.commandPool,
+ Level: core1_0.CommandBufferLevelPrimary,
+ CommandBufferCount: len(app.swapchainImages),
+ })
+ if err != nil {
+ return err
+ }
+ app.commandBuffers = buffers
+
+ for bufferIdx, buffer := range buffers {
+ _, err = buffer.Begin(core1_0.CommandBufferBeginInfo{})
+ if err != nil {
+ return err
+ }
+
+ err = buffer.CmdBeginRenderPass(core1_0.SubpassContentsInline,
+ core1_0.RenderPassBeginInfo{
+ RenderPass: app.renderPass,
+ Framebuffer: app.swapchainFramebuffers[bufferIdx],
+ RenderArea: core1_0.Rect2D{
+ Offset: core1_0.Offset2D{X: 0, Y: 0},
+ Extent: app.swapchainExtent,
+ },
+ ClearValues: []core1_0.ClearValue{
+ core1_0.ClearValueFloat{0, 0, 0, 1},
+ },
+ })
+ if err != nil {
+ return err
+ }
+
+ buffer.CmdBindPipeline(core1_0.PipelineBindPointGraphics, app.graphicsPipeline)
+ buffer.CmdDraw(3, 1, 0, 0)
+ buffer.CmdEndRenderPass()
+
+ _, err = buffer.End()
+ if err != nil {
+ return err
+ }
+ }
+
+ 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 {