-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch04LogicalDevice.diff
88 lines (78 loc) · 3.53 KB
/
Ch04LogicalDevice.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch03PhysicalDeviceSelection.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch04LogicalDevice.java"
index a6e48df..10d7127 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch03PhysicalDeviceSelection.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch04LogicalDevice.java"
@@ -20,7 +20,7 @@ import static org.lwjgl.system.MemoryUtil.NULL;
import static org.lwjgl.vulkan.EXTDebugUtils.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch03PhysicalDeviceSelection {
+public class Ch04LogicalDevice {
private static class HelloTriangleApplication {
@@ -84,6 +84,8 @@ public class Ch03PhysicalDeviceSelection {
private VkInstance instance;
private long debugMessenger;
private VkPhysicalDevice physicalDevice;
+ private VkDevice device;
+ private VkQueue graphicsQueue;
// ======= METHODS ======= //
@@ -116,6 +118,7 @@ public class Ch03PhysicalDeviceSelection {
createInstance();
setupDebugMessenger();
pickPhysicalDevice();
+ createLogicalDevice();
}
private void mainLoop() {
@@ -128,6 +131,8 @@ public class Ch03PhysicalDeviceSelection {
private void cleanup() {
+ vkDestroyDevice(device, null);
+
if(ENABLE_VALIDATION_LAYERS) {
destroyDebugUtilsMessengerEXT(instance, debugMessenger, null);
}
@@ -244,6 +249,48 @@ public class Ch03PhysicalDeviceSelection {
}
}
+ private void createLogicalDevice() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
+
+ VkDeviceQueueCreateInfo.Buffer queueCreateInfos = VkDeviceQueueCreateInfo.calloc(1, stack);
+
+ queueCreateInfos.sType(VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO);
+ queueCreateInfos.queueFamilyIndex(indices.graphicsFamily);
+ queueCreateInfos.pQueuePriorities(stack.floats(1.0f));
+
+ VkPhysicalDeviceFeatures deviceFeatures = VkPhysicalDeviceFeatures.calloc(stack);
+
+ VkDeviceCreateInfo createInfo = VkDeviceCreateInfo.calloc(stack);
+
+ createInfo.sType(VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
+ createInfo.pQueueCreateInfos(queueCreateInfos);
+ // queueCreateInfoCount is automatically set
+
+ createInfo.pEnabledFeatures(deviceFeatures);
+
+ if(ENABLE_VALIDATION_LAYERS) {
+ createInfo.ppEnabledLayerNames(validationLayersAsPointerBuffer(stack));
+ }
+
+ PointerBuffer pDevice = stack.pointers(VK_NULL_HANDLE);
+
+ if(vkCreateDevice(physicalDevice, createInfo, null, pDevice) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create logical device");
+ }
+
+ device = new VkDevice(pDevice.get(0), physicalDevice, createInfo);
+
+ PointerBuffer pGraphicsQueue = stack.pointers(VK_NULL_HANDLE);
+
+ vkGetDeviceQueue(device, indices.graphicsFamily, 0, pGraphicsQueue);
+
+ graphicsQueue = new VkQueue(pGraphicsQueue.get(0), device);
+ }
+ }
+
private boolean isDeviceSuitable(VkPhysicalDevice device) {
QueueFamilyIndices indices = findQueueFamilies(device);