-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch17VertexInput.diff
136 lines (117 loc) · 6.27 KB
/
Ch17VertexInput.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch16SwapChainRecreation.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch17VertexInput.java"
index a9e8226..4405dca 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch16SwapChainRecreation.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch17VertexInput.java"
@@ -1,6 +1,10 @@
package javavulkantutorial;
import javavulkantutorial.ShaderSPIRVUtils.SPIRV;
+import org.joml.Vector2f;
+import org.joml.Vector2fc;
+import org.joml.Vector3f;
+import org.joml.Vector3fc;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.Pointer;
@@ -29,7 +33,7 @@ import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch16SwapChainRecreation {
+public class Ch17VertexInput {
private static class HelloTriangleApplication {
@@ -113,6 +117,63 @@ public class Ch16SwapChainRecreation {
}
+ private static class Vertex {
+
+ private static final int SIZEOF = (2 + 3) * Float.BYTES;
+ private static final int OFFSETOF_POS = 0;
+ private static final int OFFSETOF_COLOR = 2 * Float.BYTES;
+
+ private Vector2fc pos;
+ private Vector3fc color;
+
+ public Vertex(Vector2fc pos, Vector3fc color) {
+ this.pos = pos;
+ this.color = color;
+ }
+
+ private static VkVertexInputBindingDescription.Buffer getBindingDescription(MemoryStack stack) {
+
+ VkVertexInputBindingDescription.Buffer bindingDescription =
+ VkVertexInputBindingDescription.calloc(1, stack);
+
+ bindingDescription.binding(0);
+ bindingDescription.stride(Vertex.SIZEOF);
+ bindingDescription.inputRate(VK_VERTEX_INPUT_RATE_VERTEX);
+
+ return bindingDescription;
+ }
+
+ private static VkVertexInputAttributeDescription.Buffer getAttributeDescriptions(MemoryStack stack) {
+
+ VkVertexInputAttributeDescription.Buffer attributeDescriptions =
+ VkVertexInputAttributeDescription.calloc(2);
+
+ // Position
+ VkVertexInputAttributeDescription posDescription = attributeDescriptions.get(0);
+ posDescription.binding(0);
+ posDescription.location(0);
+ posDescription.format(VK_FORMAT_R32G32_SFLOAT);
+ posDescription.offset(OFFSETOF_POS);
+
+ // Color
+ VkVertexInputAttributeDescription colorDescription = attributeDescriptions.get(1);
+ colorDescription.binding(0);
+ colorDescription.location(1);
+ colorDescription.format(VK_FORMAT_R32G32B32_SFLOAT);
+ colorDescription.offset(OFFSETOF_COLOR);
+
+ return attributeDescriptions.rewind();
+ }
+
+ }
+
+ private static final Vertex[] VERTICES = {
+ new Vertex(new Vector2f(0.0f, -0.5f), new Vector3f(1.0f, 0.0f, 0.0f)),
+ new Vertex(new Vector2f(0.5f, 0.5f), new Vector3f(0.0f, 1.0f, 0.0f)),
+ new Vertex(new Vector2f(-0.5f, 0.5f), new Vector3f(0.0f, 0.0f, 1.0f))
+ };
+
+
// ======= FIELDS ======= //
private long window;
@@ -616,8 +677,8 @@ public class Ch16SwapChainRecreation {
// Let's compile the GLSL shaders into SPIR-V at runtime using the shaderc library
// Check ShaderSPIRVUtils class to see how it can be done
- SPIRV vertShaderSPIRV = compileShaderFile("shaders/09_shader_base.vert", VERTEX_SHADER);
- SPIRV fragShaderSPIRV = compileShaderFile("shaders/09_shader_base.frag", FRAGMENT_SHADER);
+ SPIRV vertShaderSPIRV = compileShaderFile("shaders/17_shader_vertexbuffer.vert", VERTEX_SHADER);
+ SPIRV fragShaderSPIRV = compileShaderFile("shaders/17_shader_vertexbuffer.frag", FRAGMENT_SHADER);
long vertShaderModule = createShaderModule(vertShaderSPIRV.bytecode());
long fragShaderModule = createShaderModule(fragShaderSPIRV.bytecode());
@@ -644,6 +705,8 @@ public class Ch16SwapChainRecreation {
VkPipelineVertexInputStateCreateInfo vertexInputInfo = VkPipelineVertexInputStateCreateInfo.calloc(stack);
vertexInputInfo.sType(VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO);
+ vertexInputInfo.pVertexBindingDescriptions(Vertex.getBindingDescription(stack));
+ vertexInputInfo.pVertexAttributeDescriptions(Vertex.getAttributeDescriptions(stack));
// ===> ASSEMBLY STAGE <===
@@ -914,11 +977,14 @@ public class Ch16SwapChainRecreation {
IntBuffer pImageIndex = stack.mallocInt(1);
- int vkResult = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, thisFrame.imageAvailableSemaphore(), VK_NULL_HANDLE, pImageIndex);
+ int vkResult = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX,
+ thisFrame.imageAvailableSemaphore(), VK_NULL_HANDLE, pImageIndex);
if(vkResult == VK_ERROR_OUT_OF_DATE_KHR) {
recreateSwapChain();
return;
+ } else if(vkResult != VK_SUCCESS) {
+ throw new RuntimeException("Cannot get image");
}
final int imageIndex = pImageIndex.get(0);
@@ -942,8 +1008,9 @@ public class Ch16SwapChainRecreation {
vkResetFences(device, thisFrame.pFence());
- if(vkQueueSubmit(graphicsQueue, submitInfo, thisFrame.fence()) != VK_SUCCESS) {
- throw new RuntimeException("Failed to submit draw command buffer");
+ if((vkResult = vkQueueSubmit(graphicsQueue, submitInfo, thisFrame.fence())) != VK_SUCCESS) {
+ vkResetFences(device, thisFrame.pFence());
+ throw new RuntimeException("Failed to submit draw command buffer: " + vkResult);
}
VkPresentInfoKHR presentInfo = VkPresentInfoKHR.calloc(stack);