diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index edf8ff8..437f74d 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -20,6 +20,8 @@ import org.lwjgl.PointerBuffer; import org.lwjgl.glfw.*; 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; /** @@ -1229,13 +1231,10 @@ public class VulkanStarter { private static DepthStencil depthStencil; public static void main(String[] args) throws IOException { - if (!glfwInit()) { - throw new RuntimeException("Failed to initialize GLFW"); - } - if (!glfwVulkanSupported()) { - throw new AssertionError("GLFW failed to find the Vulkan loader"); - } - + Window.create(1200, 600, "Vulkan Ginger3D", 60, RenderAPI.Vulkan); + width = Window.getWidth(); + height = Window.getHeight(); + /* Look for instance extensions */ PointerBuffer requiredExtensions = glfwGetRequiredInstanceExtensions(); if (requiredExtensions == null) { @@ -1257,13 +1256,8 @@ public class VulkanStarter { int queueFamilyIndex = deviceAndGraphicsQueueFamily.queueFamilyIndex; 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; - glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() { + glfwSetKeyCallback(Window.getWindow(), keyCallback = new GLFWKeyCallback() { public void invoke(long window, int key, int scancode, int action, int mods) { if (action != GLFW_RELEASE) return; @@ -1272,7 +1266,7 @@ public class VulkanStarter { } }); 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); if (err != VK_SUCCESS) { throw new AssertionError("Failed to create surface: " + VKUtils.translateVulkanResult(err)); @@ -1343,8 +1337,8 @@ public class VulkanStarter { VulkanStarter.height = height; } }; - glfwSetFramebufferSizeCallback(window, framebufferSizeCallback); - glfwShowWindow(window); + glfwSetFramebufferSizeCallback(Window.getWindow(), framebufferSizeCallback); + glfwShowWindow(Window.getWindow()); // Pre-allocate everything needed in the render loop @@ -1382,7 +1376,7 @@ public class VulkanStarter { // The render loop long lastTime = System.nanoTime(); float time = 0.0f; - while (!glfwWindowShouldClose(window)) { + while (!glfwWindowShouldClose(Window.getWindow())) { // Handle window messages. Resize events happen exactly here. // So it is safe to use the new swapchain images and framebuffers afterwards. glfwPollEvents(); @@ -1451,7 +1445,7 @@ public class VulkanStarter { framebufferSizeCallback.free(); keyCallback.free(); - glfwDestroyWindow(window); + glfwDestroyWindow(Window.getWindow()); glfwTerminate(); // We don't bother disposing of all Vulkan resources. diff --git a/src/main/java/com/github/hydos/ginger/engine/common/io/Window.java b/src/main/java/com/github/hydos/ginger/engine/common/io/Window.java index 4784b22..81fc333 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/io/Window.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/io/Window.java @@ -1,5 +1,7 @@ package com.github.hydos.ginger.engine.common.io; +import static org.lwjgl.glfw.GLFW.*; + import java.nio.*; 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_FORWARD_COMPAT, GL11.GL_TRUE); }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_RESIZABLE, GLFW.GLFW_TRUE); GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()); @@ -78,8 +85,10 @@ public class Window System.exit(-1); } GLFW.glfwMakeContextCurrent(getWindow()); - glContext = GL.createCapabilities(); - GL11.glEnable(GL11.GL_DEPTH_TEST); + if(api == RenderAPI.OpenGL) { + glContext = GL.createCapabilities(); + GL11.glEnable(GL11.GL_DEPTH_TEST); + } GLFW.glfwSetWindowPos(getWindow(), (vidmode.width() - actualWidth) / 2, (vidmode.height() - actualHeight) / 2); GLFW.glfwShowWindow(getWindow()); time = getTime(); diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKUtils.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKUtils.java index f6114fd..41a3179 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKUtils.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKUtils.java @@ -21,11 +21,6 @@ import org.lwjgl.util.shaderc.*; import com.github.hydos.ginger.engine.opengl.render.tools.IOUtil; -/** - * Utility functions for Vulkan. - * - * @author Kai Burjack - */ public class VKUtils { public static final int VK_FLAGS_NONE = 0;