-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch09ShaderModules.diff
95 lines (86 loc) · 4.23 KB
/
Ch09ShaderModules.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch08GraphicsPipeline.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch09ShaderModules.java"
index 9d995aa..bbc4d8f 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch08GraphicsPipeline.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch09ShaderModules.java"
@@ -1,9 +1,11 @@
package javavulkantutorial;
+import javavulkantutorial.ShaderSPIRVUtils.SPIRV;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
+import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.util.*;
@@ -11,6 +13,9 @@ import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toSet;
+import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.FRAGMENT_SHADER;
+import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.VERTEX_SHADER;
+import static javavulkantutorial.ShaderSPIRVUtils.compileShaderFile;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFWVulkan.glfwCreateWindowSurface;
import static org.lwjgl.glfw.GLFWVulkan.glfwGetRequiredInstanceExtensions;
@@ -23,7 +28,7 @@ import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch08GraphicsPipeline {
+public class Ch09ShaderModules {
private static class HelloTriangleApplication {
@@ -471,6 +476,59 @@ public class Ch08GraphicsPipeline {
private void createGraphicsPipeline() {
+ try(MemoryStack stack = stackPush()) {
+
+ // 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);
+
+ long vertShaderModule = createShaderModule(vertShaderSPIRV.bytecode());
+ long fragShaderModule = createShaderModule(fragShaderSPIRV.bytecode());
+
+ ByteBuffer entryPoint = stack.UTF8("main");
+
+ VkPipelineShaderStageCreateInfo.Buffer shaderStages = VkPipelineShaderStageCreateInfo.calloc(2, stack);
+
+ VkPipelineShaderStageCreateInfo vertShaderStageInfo = shaderStages.get(0);
+
+ vertShaderStageInfo.sType(VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO);
+ vertShaderStageInfo.stage(VK_SHADER_STAGE_VERTEX_BIT);
+ vertShaderStageInfo.module(vertShaderModule);
+ vertShaderStageInfo.pName(entryPoint);
+
+ VkPipelineShaderStageCreateInfo fragShaderStageInfo = shaderStages.get(1);
+
+ fragShaderStageInfo.sType(VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO);
+ fragShaderStageInfo.stage(VK_SHADER_STAGE_FRAGMENT_BIT);
+ fragShaderStageInfo.module(fragShaderModule);
+ fragShaderStageInfo.pName(entryPoint);
+
+ vkDestroyShaderModule(device, vertShaderModule, null);
+ vkDestroyShaderModule(device, fragShaderModule, null);
+
+ vertShaderSPIRV.free();
+ fragShaderSPIRV.free();
+ }
+ }
+
+ private long createShaderModule(ByteBuffer spirvCode) {
+
+ try(MemoryStack stack = stackPush()) {
+
+ VkShaderModuleCreateInfo createInfo = VkShaderModuleCreateInfo.calloc(stack);
+
+ createInfo.sType(VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
+ createInfo.pCode(spirvCode);
+
+ LongBuffer pShaderModule = stack.mallocLong(1);
+
+ if(vkCreateShaderModule(device, createInfo, null, pShaderModule) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create shader module");
+ }
+
+ return pShaderModule.get(0);
+ }
}
private VkSurfaceFormatKHR chooseSwapSurfaceFormat(VkSurfaceFormatKHR.Buffer availableFormats) {