-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch18VertexBuffer.diff
132 lines (120 loc) · 5.6 KB
/
Ch18VertexBuffer.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch17VertexInput.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch18VertexBuffer.java"
index 4405dca..624eb7b 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch17VertexInput.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch18VertexBuffer.java"
@@ -33,7 +33,7 @@ import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch17VertexInput {
+public class Ch18VertexBuffer {
private static class HelloTriangleApplication {
@@ -200,6 +200,10 @@ public class Ch17VertexInput {
private long graphicsPipeline;
private long commandPool;
+
+ private long vertexBuffer;
+ private long vertexBufferMemory;
+
private List<VkCommandBuffer> commandBuffers;
private List<Frame> inFlightFrames;
@@ -256,6 +260,7 @@ public class Ch17VertexInput {
pickPhysicalDevice();
createLogicalDevice();
createCommandPool();
+ createVertexBuffer();
createSwapChainObjects();
createSyncObjects();
}
@@ -292,6 +297,9 @@ public class Ch17VertexInput {
cleanupSwapChain();
+ vkDestroyBuffer(device, vertexBuffer, null);
+ vkFreeMemory(device, vertexBufferMemory, null);
+
inFlightFrames.forEach(frame -> {
vkDestroySemaphore(device, frame.renderFinishedSemaphore(), null);
@@ -864,6 +872,76 @@ public class Ch17VertexInput {
}
}
+ private void createVertexBuffer() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ VkBufferCreateInfo bufferInfo = VkBufferCreateInfo.calloc(stack);
+ bufferInfo.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
+ bufferInfo.size(Vertex.SIZEOF * VERTICES.length);
+ bufferInfo.usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
+ bufferInfo.sharingMode(VK_SHARING_MODE_EXCLUSIVE);
+
+ LongBuffer pVertexBuffer = stack.mallocLong(1);
+
+ if(vkCreateBuffer(device, bufferInfo, null, pVertexBuffer) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create vertex buffer");
+ }
+ vertexBuffer = pVertexBuffer.get(0);
+
+ VkMemoryRequirements memRequirements = VkMemoryRequirements.malloc(stack);
+ vkGetBufferMemoryRequirements(device, vertexBuffer, memRequirements);
+
+ VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.calloc(stack);
+ allocInfo.sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
+ allocInfo.allocationSize(memRequirements.size());
+ allocInfo.memoryTypeIndex(findMemoryType(stack, memRequirements.memoryTypeBits(),
+ VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT));
+
+ LongBuffer pVertexBufferMemory = stack.mallocLong(1);
+
+ if(vkAllocateMemory(device, allocInfo, null, pVertexBufferMemory) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to allocate vertex buffer memory");
+ }
+ vertexBufferMemory = pVertexBufferMemory.get(0);
+
+ vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);
+
+ PointerBuffer data = stack.mallocPointer(1);
+
+ vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size(), 0, data);
+ {
+ memcpy(data.getByteBuffer(0, (int) bufferInfo.size()), VERTICES);
+ }
+ vkUnmapMemory(device, vertexBufferMemory);
+ }
+ }
+
+ private void memcpy(ByteBuffer buffer, Vertex[] vertices) {
+ for(Vertex vertex : vertices) {
+ buffer.putFloat(vertex.pos.x());
+ buffer.putFloat(vertex.pos.y());
+
+ buffer.putFloat(vertex.color.x());
+ buffer.putFloat(vertex.color.y());
+ buffer.putFloat(vertex.color.z());
+ }
+ }
+
+ private int findMemoryType(MemoryStack stack, int typeFilter, int properties) {
+
+ VkPhysicalDeviceMemoryProperties memProperties = VkPhysicalDeviceMemoryProperties.malloc(stack);
+ vkGetPhysicalDeviceMemoryProperties(physicalDevice, memProperties);
+
+ for(int i = 0;i < memProperties.memoryTypeCount();i++) {
+ if((typeFilter & (1 << i)) != 0 && (memProperties.memoryTypes(i).propertyFlags() & properties) == properties) {
+ return i;
+ }
+ }
+
+ throw new RuntimeException("Failed to find suitable memory type");
+ }
+
private void createCommandBuffers() {
final int commandBuffersCount = swapChainFramebuffers.size();
@@ -920,7 +998,11 @@ public class Ch17VertexInput {
{
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
- vkCmdDraw(commandBuffer, 3, 1, 0, 0);
+ LongBuffer vertexBuffers = stack.longs(vertexBuffer);
+ LongBuffer offsets = stack.longs(0);
+ vkCmdBindVertexBuffers(commandBuffer, 0, vertexBuffers, offsets);
+
+ vkCmdDraw(commandBuffer, VERTICES.length, 1, 0, 0);
}
vkCmdEndRenderPass(commandBuffer);