-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathSimpleDemo.java
95 lines (76 loc) · 3.23 KB
/
SimpleDemo.java
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
package org.lwjgl.vulkan.awt;
import org.lwjgl.PointerBuffer;
import org.lwjgl.awt.AWT;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.KHRSurface;
import org.lwjgl.vulkan.VkApplicationInfo;
import org.lwjgl.vulkan.VkInstance;
import org.lwjgl.vulkan.VkInstanceCreateInfo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import static org.lwjgl.vulkan.KHRSurface.VK_KHR_SURFACE_EXTENSION_NAME;
import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.vulkan.awt.VKUtil.translateVulkanResult;
/**
* Shows how to create a simple Vulkan instance and a canvas using {@link AWTVK}.
*
* @author Kai Burjack
* @author SWinxy
*/
public class SimpleDemo {
/**
* Create a Vulkan instance using LWJGL 3.
*
* @return the VkInstance handle
*/
private static VkInstance createInstance() {
try (MemoryStack stack = MemoryStack.stackPush()) {
VkApplicationInfo appInfo = VkApplicationInfo
.calloc(stack)
.sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
.pApplicationName(stack.UTF8("AWT Vulkan Demo"))
.pEngineName(stack.UTF8(""))
.apiVersion(VK_MAKE_VERSION(1, 0, 2));
PointerBuffer ppEnabledExtensionNames = stack.pointers(stack.UTF8(VK_KHR_SURFACE_EXTENSION_NAME), stack.UTF8(AWTVK.getSurfaceExtensionName()));
VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo
.calloc(stack)
.sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
.pApplicationInfo(appInfo)
.ppEnabledExtensionNames(ppEnabledExtensionNames);
PointerBuffer pInstance = stack.mallocPointer(1);
int err = vkCreateInstance(pCreateInfo, null, pInstance);
if (err != VK_SUCCESS) {
throw new RuntimeException("Failed to create VkInstance: " + translateVulkanResult(err));
}
long instance = pInstance.get(0);
return new VkInstance(instance, pCreateInfo);
}
}
public static void main(String[] args) throws AWTException {
if (!AWT.isPlatformSupported()) {
throw new RuntimeException("Platform not supported.");
}
// Create the Vulkan instance
VkInstance instance = createInstance();
JFrame frame = new JFrame("AWT test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(600, 600));
Canvas canvas = new Canvas();
frame.add(canvas, BorderLayout.CENTER);
frame.pack(); // Packing causes the canvas to be lockable, and is the earliest time it can be used
long surface = AWTVK.create(canvas, instance);
// ... Do things with the surface
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
// Destroy the surface to prevent leaks and errors
KHRSurface.vkDestroySurfaceKHR(instance, surface, null);
}
});
}
}