-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch14CommandBuffers.diff
144 lines (131 loc) · 6 KB
/
Ch14CommandBuffers.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch13Framebuffers.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch14CommandBuffers.java"
index a12bf81..1caa3ce 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch13Framebuffers.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch14CommandBuffers.java"
@@ -28,7 +28,7 @@ import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch13Framebuffers {
+public class Ch14CommandBuffers {
private static class HelloTriangleApplication {
@@ -134,6 +134,9 @@ public class Ch13Framebuffers {
private long pipelineLayout;
private long graphicsPipeline;
+ private long commandPool;
+ private List<VkCommandBuffer> commandBuffers;
+
// ======= METHODS ======= //
public void run() {
@@ -172,6 +175,8 @@ public class Ch13Framebuffers {
createRenderPass();
createGraphicsPipeline();
createFramebuffers();
+ createCommandPool();
+ createCommandBuffers();
}
private void mainLoop() {
@@ -184,6 +189,8 @@ public class Ch13Framebuffers {
private void cleanup() {
+ vkDestroyCommandPool(device, commandPool, null);
+
swapChainFramebuffers.forEach(framebuffer -> vkDestroyFramebuffer(device, framebuffer, null));
vkDestroyPipeline(device, graphicsPipeline, null);
@@ -479,7 +486,7 @@ public class Ch13Framebuffers {
createInfo.subresourceRange().baseArrayLayer(0);
createInfo.subresourceRange().layerCount(1);
- if(vkCreateImageView(device, createInfo, null, pImageView) != VK_SUCCESS) {
+ if (vkCreateImageView(device, createInfo, null, pImageView) != VK_SUCCESS) {
throw new RuntimeException("Failed to create image views");
}
@@ -698,6 +705,93 @@ public class Ch13Framebuffers {
}
}
+ private void createCommandPool() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
+
+ VkCommandPoolCreateInfo poolInfo = VkCommandPoolCreateInfo.calloc(stack);
+ poolInfo.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO);
+ poolInfo.queueFamilyIndex(queueFamilyIndices.graphicsFamily);
+
+ LongBuffer pCommandPool = stack.mallocLong(1);
+
+ if (vkCreateCommandPool(device, poolInfo, null, pCommandPool) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create command pool");
+ }
+
+ commandPool = pCommandPool.get(0);
+ }
+ }
+
+ private void createCommandBuffers() {
+
+ final int commandBuffersCount = swapChainFramebuffers.size();
+
+ commandBuffers = new ArrayList<>(commandBuffersCount);
+
+ try(MemoryStack stack = stackPush()) {
+
+ VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo.calloc(stack);
+ allocInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO);
+ allocInfo.commandPool(commandPool);
+ allocInfo.level(VK_COMMAND_BUFFER_LEVEL_PRIMARY);
+ allocInfo.commandBufferCount(commandBuffersCount);
+
+ PointerBuffer pCommandBuffers = stack.mallocPointer(commandBuffersCount);
+
+ if(vkAllocateCommandBuffers(device, allocInfo, pCommandBuffers) != VK_SUCCESS) {
+ throw new RuntimeException("Fialed to allocate command buffers");
+ }
+
+ for(int i = 0;i < commandBuffersCount;i++) {
+ commandBuffers.add(new VkCommandBuffer(pCommandBuffers.get(i), device));
+ }
+
+ VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.calloc(stack);
+ beginInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO);
+
+ VkRenderPassBeginInfo renderPassInfo = VkRenderPassBeginInfo.calloc(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));
+ renderArea.extent(swapChainExtent);
+ renderPassInfo.renderArea(renderArea);
+ VkClearValue.Buffer clearValues = VkClearValue.calloc(1, stack);
+ clearValues.color().float32(stack.floats(0.0f, 0.0f, 0.0f, 1.0f));
+ renderPassInfo.pClearValues(clearValues);
+
+ for(int i = 0;i < commandBuffersCount;i++) {
+
+ VkCommandBuffer commandBuffer = commandBuffers.get(i);
+
+ if(vkBeginCommandBuffer(commandBuffer, beginInfo) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to begin recording command buffer");
+ }
+
+ renderPassInfo.framebuffer(swapChainFramebuffers.get(i));
+
+
+ vkCmdBeginRenderPass(commandBuffer, renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
+ {
+ vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
+
+ vkCmdDraw(commandBuffer, 3, 1, 0, 0);
+ }
+ vkCmdEndRenderPass(commandBuffer);
+
+
+ if(vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to record command buffer");
+ }
+
+ }
+
+ }
+ }
+
private long createShaderModule(ByteBuffer spirvCode) {
try(MemoryStack stack = stackPush()) {