added vulkan support to Window class and implement it

pull/12/head
hYdos 2020-03-02 20:38:17 +10:00
parent a7e3541668
commit b281cb2f12
3 changed files with 24 additions and 26 deletions

View File

@ -20,6 +20,8 @@ import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.*; import org.lwjgl.glfw.*;
import org.lwjgl.vulkan.*; import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.common.info.RenderAPI;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
/** /**
@ -1229,13 +1231,10 @@ public class VulkanStarter {
private static DepthStencil depthStencil; private static DepthStencil depthStencil;
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
if (!glfwInit()) { Window.create(1200, 600, "Vulkan Ginger3D", 60, RenderAPI.Vulkan);
throw new RuntimeException("Failed to initialize GLFW"); width = Window.getWidth();
} height = Window.getHeight();
if (!glfwVulkanSupported()) {
throw new AssertionError("GLFW failed to find the Vulkan loader");
}
/* Look for instance extensions */ /* Look for instance extensions */
PointerBuffer requiredExtensions = glfwGetRequiredInstanceExtensions(); PointerBuffer requiredExtensions = glfwGetRequiredInstanceExtensions();
if (requiredExtensions == null) { if (requiredExtensions == null) {
@ -1257,13 +1256,8 @@ public class VulkanStarter {
int queueFamilyIndex = deviceAndGraphicsQueueFamily.queueFamilyIndex; int queueFamilyIndex = deviceAndGraphicsQueueFamily.queueFamilyIndex;
final VkPhysicalDeviceMemoryProperties memoryProperties = deviceAndGraphicsQueueFamily.memoryProperties; final VkPhysicalDeviceMemoryProperties memoryProperties = deviceAndGraphicsQueueFamily.memoryProperties;
// Create GLFW window
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
long window = glfwCreateWindow(800, 600, "GLFW Vulkan Demo", NULL, NULL);
GLFWKeyCallback keyCallback; GLFWKeyCallback keyCallback;
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() { glfwSetKeyCallback(Window.getWindow(), keyCallback = new GLFWKeyCallback() {
public void invoke(long window, int key, int scancode, int action, int mods) { public void invoke(long window, int key, int scancode, int action, int mods) {
if (action != GLFW_RELEASE) if (action != GLFW_RELEASE)
return; return;
@ -1272,7 +1266,7 @@ public class VulkanStarter {
} }
}); });
LongBuffer pSurface = memAllocLong(1); LongBuffer pSurface = memAllocLong(1);
int err = glfwCreateWindowSurface(instance, window, null, pSurface); int err = glfwCreateWindowSurface(instance, Window.getWindow(), null, pSurface);
final long surface = pSurface.get(0); final long surface = pSurface.get(0);
if (err != VK_SUCCESS) { if (err != VK_SUCCESS) {
throw new AssertionError("Failed to create surface: " + VKUtils.translateVulkanResult(err)); throw new AssertionError("Failed to create surface: " + VKUtils.translateVulkanResult(err));
@ -1343,8 +1337,8 @@ public class VulkanStarter {
VulkanStarter.height = height; VulkanStarter.height = height;
} }
}; };
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback); glfwSetFramebufferSizeCallback(Window.getWindow(), framebufferSizeCallback);
glfwShowWindow(window); glfwShowWindow(Window.getWindow());
// Pre-allocate everything needed in the render loop // Pre-allocate everything needed in the render loop
@ -1382,7 +1376,7 @@ public class VulkanStarter {
// The render loop // The render loop
long lastTime = System.nanoTime(); long lastTime = System.nanoTime();
float time = 0.0f; float time = 0.0f;
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(Window.getWindow())) {
// Handle window messages. Resize events happen exactly here. // Handle window messages. Resize events happen exactly here.
// So it is safe to use the new swapchain images and framebuffers afterwards. // So it is safe to use the new swapchain images and framebuffers afterwards.
glfwPollEvents(); glfwPollEvents();
@ -1451,7 +1445,7 @@ public class VulkanStarter {
framebufferSizeCallback.free(); framebufferSizeCallback.free();
keyCallback.free(); keyCallback.free();
glfwDestroyWindow(window); glfwDestroyWindow(Window.getWindow());
glfwTerminate(); glfwTerminate();
// We don't bother disposing of all Vulkan resources. // We don't bother disposing of all Vulkan resources.

View File

@ -1,5 +1,7 @@
package com.github.hydos.ginger.engine.common.io; package com.github.hydos.ginger.engine.common.io;
import static org.lwjgl.glfw.GLFW.*;
import java.nio.*; import java.nio.*;
import org.joml.*; import org.joml.*;
@ -67,7 +69,12 @@ public class Window
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE); GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE); GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
}else if (renderAPI == RenderAPI.Vulkan) }else if (renderAPI == RenderAPI.Vulkan)
{
GLFW.glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
if (!GLFWVulkan.glfwVulkanSupported()) {
throw new AssertionError("GLFW failed to find the Vulkan loader");
}
}
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE); GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE); GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()); GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
@ -78,8 +85,10 @@ public class Window
System.exit(-1); System.exit(-1);
} }
GLFW.glfwMakeContextCurrent(getWindow()); GLFW.glfwMakeContextCurrent(getWindow());
glContext = GL.createCapabilities(); if(api == RenderAPI.OpenGL) {
GL11.glEnable(GL11.GL_DEPTH_TEST); glContext = GL.createCapabilities();
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
GLFW.glfwSetWindowPos(getWindow(), (vidmode.width() - actualWidth) / 2, (vidmode.height() - actualHeight) / 2); GLFW.glfwSetWindowPos(getWindow(), (vidmode.width() - actualWidth) / 2, (vidmode.height() - actualHeight) / 2);
GLFW.glfwShowWindow(getWindow()); GLFW.glfwShowWindow(getWindow());
time = getTime(); time = getTime();

View File

@ -21,11 +21,6 @@ import org.lwjgl.util.shaderc.*;
import com.github.hydos.ginger.engine.opengl.render.tools.IOUtil; import com.github.hydos.ginger.engine.opengl.render.tools.IOUtil;
/**
* Utility functions for Vulkan.
*
* @author Kai Burjack
*/
public class VKUtils { public class VKUtils {
public static final int VK_FLAGS_NONE = 0; public static final int VK_FLAGS_NONE = 0;