-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path22_descriptor_sets.diff
More file actions
160 lines (148 loc) · 4.74 KB
/
22_descriptor_sets.diff
File metadata and controls
160 lines (148 loc) · 4.74 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
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
diff --git a/../steps/21_descriptor_layout/main.go b/../steps/22_descriptor_sets/main.go
index ec5b67b..40dd122 100644
--- a/../steps/21_descriptor_layout/main.go
+++ b/../steps/22_descriptor_sets/main.go
@@ -54,9 +54,9 @@ type Vertex struct {
}
type UniformBufferObject struct {
- Model vkngmath.Mat4x4[float32] `json:"model"`
- View vkngmath.Mat4x4[float32] `json:"view"`
- Proj vkngmath.Mat4x4[float32] `json:"proj"`
+ Model vkngmath.Mat4x4[float32]
+ View vkngmath.Mat4x4[float32]
+ Proj vkngmath.Mat4x4[float32]
}
func getVertexBindingDescription() []core1_0.VertexInputBindingDescription {
@@ -120,6 +120,8 @@ type HelloTriangleApplication struct {
swapchainFramebuffers []core1_0.Framebuffer
renderPass core1_0.RenderPass
+ descriptorPool core1_0.DescriptorPool
+ descriptorSets []core1_0.DescriptorSet
descriptorSetLayout core1_0.DescriptorSetLayout
pipelineLayout core1_0.PipelineLayout
graphicsPipeline core1_0.Pipeline
@@ -253,6 +255,16 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.createDescriptorPool()
+ if err != nil {
+ return err
+ }
+
+ err = app.createDescriptorSets()
+ if err != nil {
+ return err
+ }
+
err = app.createCommandBuffers()
if err != nil {
return err
@@ -344,6 +356,8 @@ func (app *HelloTriangleApplication) cleanupSwapChain() {
app.uniformBuffersMemory[i].Free(nil)
}
app.uniformBuffersMemory = app.uniformBuffersMemory[:0]
+
+ app.descriptorPool.Destroy(nil)
}
func (app *HelloTriangleApplication) cleanup() {
@@ -453,6 +467,16 @@ func (app *HelloTriangleApplication) recreateSwapChain() error {
return err
}
+ err = app.createDescriptorPool()
+ if err != nil {
+ return err
+ }
+
+ err = app.createDescriptorSets()
+ if err != nil {
+ return err
+ }
+
err = app.createCommandBuffers()
if err != nil {
return err
@@ -883,7 +907,7 @@ func (app *HelloTriangleApplication) createGraphicsPipeline() error {
PolygonMode: core1_0.PolygonModeFill,
CullMode: core1_0.CullModeBack,
- FrontFace: core1_0.FrontFaceClockwise,
+ FrontFace: core1_0.FrontFaceCounterClockwise,
DepthBiasEnable: false,
@@ -914,9 +938,6 @@ func (app *HelloTriangleApplication) createGraphicsPipeline() error {
app.descriptorSetLayout,
},
})
- if err != nil {
- return err
- }
pipelines, _, err := app.device.CreateGraphicsPipelines(nil, nil, []core1_0.GraphicsPipelineCreateInfo{
{
@@ -1077,6 +1098,61 @@ func (app *HelloTriangleApplication) createUniformBuffers() error {
return nil
}
+func (app *HelloTriangleApplication) createDescriptorPool() error {
+ var err error
+ app.descriptorPool, _, err = app.device.CreateDescriptorPool(nil, core1_0.DescriptorPoolCreateInfo{
+ MaxSets: len(app.swapchainImages),
+ PoolSizes: []core1_0.DescriptorPoolSize{
+ {
+ Type: core1_0.DescriptorTypeUniformBuffer,
+ DescriptorCount: len(app.swapchainImages),
+ },
+ },
+ })
+ return err
+}
+
+func (app *HelloTriangleApplication) createDescriptorSets() error {
+ var allocLayouts []core1_0.DescriptorSetLayout
+ for i := 0; i < len(app.swapchainImages); i++ {
+ allocLayouts = append(allocLayouts, app.descriptorSetLayout)
+ }
+
+ var err error
+ app.descriptorSets, _, err = app.device.AllocateDescriptorSets(core1_0.DescriptorSetAllocateInfo{
+ DescriptorPool: app.descriptorPool,
+ SetLayouts: allocLayouts,
+ })
+ if err != nil {
+ return err
+ }
+
+ for i := 0; i < len(app.swapchainImages); i++ {
+ err = app.device.UpdateDescriptorSets([]core1_0.WriteDescriptorSet{
+ {
+ DstSet: app.descriptorSets[i],
+ DstBinding: 0,
+ DstArrayElement: 0,
+
+ DescriptorType: core1_0.DescriptorTypeUniformBuffer,
+
+ BufferInfo: []core1_0.DescriptorBufferInfo{
+ {
+ Buffer: app.uniformBuffers[i],
+ Offset: 0,
+ Range: int(unsafe.Sizeof(UniformBufferObject{})),
+ },
+ },
+ },
+ }, nil)
+ if err != nil {
+ return err
+ }
+ }
+
+ 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,
@@ -1200,6 +1276,9 @@ func (app *HelloTriangleApplication) createCommandBuffers() error {
buffer.CmdBindPipeline(core1_0.PipelineBindPointGraphics, app.graphicsPipeline)
buffer.CmdBindVertexBuffers(0, []core1_0.Buffer{app.vertexBuffer}, []int{0})
buffer.CmdBindIndexBuffer(app.indexBuffer, 0, core1_0.IndexTypeUInt16)
+ buffer.CmdBindDescriptorSets(core1_0.PipelineBindPointGraphics, app.pipelineLayout, 0, []core1_0.DescriptorSet{
+ app.descriptorSets[bufferIdx],
+ }, nil)
buffer.CmdDrawIndexed(len(indices), 1, 0, 0, 0)
buffer.CmdEndRenderPass()