-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch22DescriptorSets.diff
210 lines (184 loc) · 9.75 KB
/
Ch22DescriptorSets.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch21DescriptorLayout.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch22DescriptorSets.java"
index a8b2fe0..6173ec8 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch21DescriptorLayout.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch22DescriptorSets.java"
@@ -16,6 +16,8 @@ import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toSet;
+import static javavulkantutorial.AlignmentUtils.alignas;
+import static javavulkantutorial.AlignmentUtils.alignof;
import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.FRAGMENT_SHADER;
import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.VERTEX_SHADER;
import static javavulkantutorial.ShaderSPIRVUtils.compileShaderFile;
@@ -26,12 +28,13 @@ import static org.lwjgl.system.Configuration.DEBUG;
import static org.lwjgl.system.MemoryStack.stackGet;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL;
+import static org.lwjgl.system.MemoryUtil.memSet;
import static org.lwjgl.vulkan.EXTDebugUtils.*;
import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch21DescriptorLayout {
+public class Ch22DescriptorSets {
private static class HelloTriangleApplication {
@@ -213,7 +216,9 @@ public class Ch21DescriptorLayout {
private List<Long> swapChainFramebuffers;
private long renderPass;
+ private long descriptorPool;
private long descriptorSetLayout;
+ private List<Long> descriptorSets;
private long pipelineLayout;
private long graphicsPipeline;
@@ -305,6 +310,8 @@ public class Ch21DescriptorLayout {
uniformBuffers.forEach(ubo -> vkDestroyBuffer(device, ubo, null));
uniformBuffersMemory.forEach(uboMemory -> vkFreeMemory(device, uboMemory, null));
+ vkDestroyDescriptorPool(device, descriptorPool, null);
+
swapChainFramebuffers.forEach(framebuffer -> vkDestroyFramebuffer(device, framebuffer, null));
try(MemoryStack stack = stackPush()) {vkFreeCommandBuffers(device, commandPool, asPointerBuffer(stack, commandBuffers));}
@@ -384,6 +391,8 @@ public class Ch21DescriptorLayout {
createGraphicsPipeline();
createFramebuffers();
createUniformBuffers();
+ createDescriptorPool();
+ createDescriptorSets();
createCommandBuffers();
}
@@ -808,7 +817,7 @@ public class Ch21DescriptorLayout {
rasterizer.polygonMode(VK_POLYGON_MODE_FILL);
rasterizer.lineWidth(1.0f);
rasterizer.cullMode(VK_CULL_MODE_BACK_BIT);
- rasterizer.frontFace(VK_FRONT_FACE_CLOCKWISE);
+ rasterizer.frontFace(VK_FRONT_FACE_COUNTER_CLOCKWISE);
rasterizer.depthBiasEnable(false);
// ===> MULTISAMPLING <===
@@ -1036,6 +1045,79 @@ public class Ch21DescriptorLayout {
}
}
+
+ private void createDescriptorPool() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ VkDescriptorPoolSize.Buffer poolSize = VkDescriptorPoolSize.calloc(1, stack);
+ poolSize.type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
+ poolSize.descriptorCount(swapChainImages.size());
+
+ VkDescriptorPoolCreateInfo poolInfo = VkDescriptorPoolCreateInfo.calloc(stack);
+ poolInfo.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO);
+ poolInfo.pPoolSizes(poolSize);
+ poolInfo.maxSets(swapChainImages.size());
+
+ LongBuffer pDescriptorPool = stack.mallocLong(1);
+
+ if(vkCreateDescriptorPool(device, poolInfo, null, pDescriptorPool) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create descriptor pool");
+ }
+
+ descriptorPool = pDescriptorPool.get(0);
+ }
+ }
+
+ private void createDescriptorSets() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ LongBuffer layouts = stack.mallocLong(swapChainImages.size());
+ for(int i = 0;i < layouts.capacity();i++) {
+ layouts.put(i, descriptorSetLayout);
+ }
+
+ VkDescriptorSetAllocateInfo allocInfo = VkDescriptorSetAllocateInfo.calloc(stack);
+ allocInfo.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO);
+ allocInfo.descriptorPool(descriptorPool);
+ allocInfo.pSetLayouts(layouts);
+
+ LongBuffer pDescriptorSets = stack.mallocLong(swapChainImages.size());
+
+ if(vkAllocateDescriptorSets(device, allocInfo, pDescriptorSets) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to allocate descriptor sets");
+ }
+
+ descriptorSets = new ArrayList<>(pDescriptorSets.capacity());
+
+ VkDescriptorBufferInfo.Buffer bufferInfo = VkDescriptorBufferInfo.calloc(1, stack);
+ bufferInfo.offset(0);
+ bufferInfo.range(UniformBufferObject.SIZEOF);
+
+ VkWriteDescriptorSet.Buffer descriptorWrite = VkWriteDescriptorSet.calloc(1, stack);
+ descriptorWrite.sType(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);
+ descriptorWrite.dstBinding(0);
+ descriptorWrite.dstArrayElement(0);
+ descriptorWrite.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
+ descriptorWrite.descriptorCount(1);
+ descriptorWrite.pBufferInfo(bufferInfo);
+
+ for(int i = 0;i < pDescriptorSets.capacity();i++) {
+
+ long descriptorSet = pDescriptorSets.get(i);
+
+ bufferInfo.buffer(uniformBuffers.get(i));
+
+ descriptorWrite.dstSet(descriptorSet);
+
+ vkUpdateDescriptorSets(device, descriptorWrite, null);
+
+ descriptorSets.add(descriptorSet);
+ }
+ }
+ }
+
private void createBuffer(long size, int usage, int properties, LongBuffer pBuffer, LongBuffer pBufferMemory) {
try(MemoryStack stack = stackPush()) {
@@ -1092,7 +1174,7 @@ public class Ch21DescriptorLayout {
}
vkEndCommandBuffer(commandBuffer);
- VkSubmitInfo submitInfo = VkSubmitInfo.calloc(stack);
+ VkSubmitInfo submitInfo = VkSubmitInfo.callocStack(stack);
submitInfo.sType(VK_STRUCTURE_TYPE_SUBMIT_INFO);
submitInfo.pCommandBuffers(pCommandBuffer);
@@ -1131,8 +1213,8 @@ public class Ch21DescriptorLayout {
final int mat4Size = 16 * Float.BYTES;
ubo.model.get(0, buffer);
- ubo.view.get(mat4Size, buffer);
- ubo.proj.get(mat4Size * 2, buffer);
+ ubo.view.get(alignas(mat4Size, alignof(ubo.view)), buffer);
+ ubo.proj.get(alignas(mat4Size * 2, alignof(ubo.view)), buffer);
}
private int findMemoryType(MemoryStack stack, int typeFilter, int properties) {
@@ -1157,7 +1239,7 @@ public class Ch21DescriptorLayout {
try(MemoryStack stack = stackPush()) {
- VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo.calloc(stack);
+ VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo.callocStack(stack);
allocInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO);
allocInfo.commandPool(commandPool);
allocInfo.level(VK_COMMAND_BUFFER_LEVEL_PRIMARY);
@@ -1173,20 +1255,20 @@ public class Ch21DescriptorLayout {
commandBuffers.add(new VkCommandBuffer(pCommandBuffers.get(i), device));
}
- VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.calloc(stack);
+ VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.callocStack(stack);
beginInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO);
- VkRenderPassBeginInfo renderPassInfo = VkRenderPassBeginInfo.calloc(stack);
+ VkRenderPassBeginInfo renderPassInfo = VkRenderPassBeginInfo.callocStack(stack);
renderPassInfo.sType(VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO);
renderPassInfo.renderPass(renderPass);
- VkRect2D renderArea = VkRect2D.calloc(stack);
- renderArea.offset(VkOffset2D.calloc(stack).set(0, 0));
+ VkRect2D renderArea = VkRect2D.callocStack(stack);
+ renderArea.offset(VkOffset2D.callocStack(stack).set(0, 0));
renderArea.extent(swapChainExtent);
renderPassInfo.renderArea(renderArea);
- VkClearValue.Buffer clearValues = VkClearValue.calloc(1, stack);
+ VkClearValue.Buffer clearValues = VkClearValue.callocStack(1, stack);
clearValues.color().float32(stack.floats(0.0f, 0.0f, 0.0f, 1.0f));
renderPassInfo.pClearValues(clearValues);
@@ -1211,6 +1293,9 @@ public class Ch21DescriptorLayout {
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
+ vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
+ pipelineLayout, 0, stack.longs(descriptorSets.get(i)), null);
+
vkCmdDrawIndexed(commandBuffer, INDICES.length, 1, 0, 0, 0);
}
vkCmdEndRenderPass(commandBuffer);