fix stuff for litecraft merge

liteCraft
hYdos 2020-03-05 07:39:23 +10:00
parent e4a4beb787
commit cd07f1872b
6 changed files with 209 additions and 35 deletions

View File

@ -4,7 +4,7 @@ import org.lwjgl.glfw.GLFW;
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.api.GingerVk;
import com.github.hydos.ginger.engine.vulkan.api.GingerVK;
/** @author hydos06
* the non ARR vulkan test example */
@ -29,7 +29,7 @@ public class VulkanStarter
}
private void initVulkan() {
new GingerVk().start("Vulkan demo");
new GingerVK().start("Vulkan demo");
}
private void mainLoop() {

View File

@ -2,9 +2,10 @@ package com.github.hydos.ginger.engine.vulkan;
import org.lwjgl.vulkan.*;
public class VkConstants
public class VKConstants
{
public static final long UINT64_MAX = -1L;
public static VkInstance vulkanInstance;
public static VkPhysicalDevice physicalDevice;
public static VkDevice device;

View File

@ -1,17 +1,17 @@
package com.github.hydos.ginger.engine.vulkan.api;
import com.github.hydos.ginger.engine.vulkan.io.VkWindow;
import com.github.hydos.ginger.engine.vulkan.utils.VkUtils;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
public class GingerVk
public class GingerVK
{
public void start(String gameName) {
System.out.println("Game " + gameName + " successfuly started in Vulkan mode.");
VkUtils.createInstance();
VKUtils.createInstance();
VkWindow.createSurface();
VkUtils.createPhysicalDevice();
VkUtils.createLogicalDevice();
VKUtils.createPhysicalDevice();
VKUtils.createLogicalDevice();
}

View File

@ -7,8 +7,8 @@ import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VK12;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.vulkan.VkConstants;
import com.github.hydos.ginger.engine.vulkan.utils.VkUtils;
import com.github.hydos.ginger.engine.vulkan.VKConstants;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
/**
* used for window related vulkan only things
@ -23,13 +23,13 @@ public class VkWindow
{
LongBuffer pSurface = stack.longs(VK12.VK_NULL_HANDLE);
int status = GLFWVulkan.glfwCreateWindowSurface(VkConstants.vulkanInstance, Window.getWindow(), null, pSurface);
int status = GLFWVulkan.glfwCreateWindowSurface(VKConstants.vulkanInstance, Window.getWindow(), null, pSurface);
if(status != VK12.VK_SUCCESS)
{
throw new VulkanException("Failed to create vulkan surface for window reason: " + VkUtils.translateVulkanResult(status));
throw new VulkanException("Failed to create vulkan surface for window reason: " + VKUtils.translateVulkanResult(status));
}
VkConstants.windowSurface = pSurface.get(0);
VKConstants.windowSurface = pSurface.get(0);
}
}
}

View File

@ -0,0 +1,170 @@
package com.github.hydos.ginger.engine.vulkan.render;
import java.nio.*;
import java.util.*;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.vulkan.VKConstants;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils.QueueFamilyIndices;
/**
* used for Vulkan FBO management as vulkan does not have a default FBO
* @author hydos
*
*/
public class Swapchain
{
public static long swapChain;
public static List<Long> swapChainImages;
public static int swapChainImageFormat;
public static VkExtent2D swapChainExtent;
public static class SwapChainSupportDetails
{
public VkSurfaceCapabilitiesKHR capabilities;
public VkSurfaceFormatKHR.Buffer formats;
public IntBuffer presentModes;
}
private static SwapChainSupportDetails querySwapChainSupport(MemoryStack stack) {
SwapChainSupportDetails details = new SwapChainSupportDetails();
details.capabilities = VkSurfaceCapabilitiesKHR.mallocStack(stack);
KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VKConstants.physicalDevice, VKConstants.windowSurface, details.capabilities);
IntBuffer count = stack.ints(0);
KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(VKConstants.physicalDevice, VKConstants.windowSurface, count, null);
if(count.get(0) != 0) {
details.formats = VkSurfaceFormatKHR.mallocStack(count.get(0), stack);
KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(VKConstants.physicalDevice, VKConstants.windowSurface, count, details.formats);
}
KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(VKConstants.physicalDevice,VKConstants.windowSurface, count, null);
if(count.get(0) != 0) {
details.presentModes = stack.mallocInt(count.get(0));
KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(VKConstants.physicalDevice, VKConstants.windowSurface, count, details.presentModes);
}
return details;
}
private static VkSurfaceFormatKHR chooseSwapSurfaceFormat(VkSurfaceFormatKHR.Buffer availableFormats) {
return availableFormats.stream()
.filter(availableFormat -> availableFormat.format() == VK12.VK_FORMAT_B8G8R8_UNORM)
.filter(availableFormat -> availableFormat.colorSpace() == KHRSurface.VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
.findAny()
.orElse(availableFormats.get(0));
}
private static int chooseSwapPresentMode(IntBuffer availablePresentModes) {
for(int i = 0;i < availablePresentModes.capacity();i++) {
if(availablePresentModes.get(i) == KHRSurface.VK_PRESENT_MODE_MAILBOX_KHR) {
return availablePresentModes.get(i);
}
}
return KHRSurface.VK_PRESENT_MODE_FIFO_KHR;
}
private static VkExtent2D chooseSwapExtent(VkSurfaceCapabilitiesKHR capabilities) {
if(capabilities.currentExtent().width() != VKConstants.UINT64_MAX) {
return capabilities.currentExtent();
}
VkExtent2D actualExtent = VkExtent2D.mallocStack().set(Window.getWidth(), Window.getHeight());
VkExtent2D minExtent = capabilities.minImageExtent();
VkExtent2D maxExtent = capabilities.maxImageExtent();
actualExtent.width(VKUtils.clamp(minExtent.width(), maxExtent.width(), actualExtent.width()));
actualExtent.height(VKUtils.clamp(minExtent.height(), maxExtent.height(), actualExtent.height()));
return actualExtent;
}
public static void createSwapChain()
{
try(MemoryStack stack = MemoryStack.stackPush()) {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(stack);
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
int presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
IntBuffer imageCount = stack.ints(swapChainSupport.capabilities.minImageCount() + 1);
if(swapChainSupport.capabilities.maxImageCount() > 0 && imageCount.get(0) > swapChainSupport.capabilities.maxImageCount()) {
imageCount.put(0, swapChainSupport.capabilities.maxImageCount());
}
VkSwapchainCreateInfoKHR createInfo = VkSwapchainCreateInfoKHR.callocStack(stack);
createInfo.sType(KHRSwapchain.VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
createInfo.surface(VKConstants.windowSurface);
// Image settings
createInfo.minImageCount(imageCount.get(0));
createInfo.imageFormat(surfaceFormat.format());
createInfo.imageColorSpace(surfaceFormat.colorSpace());
createInfo.imageExtent(extent);
createInfo.imageArrayLayers(1);
createInfo.imageUsage(VK12.VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
QueueFamilyIndices indices = VKUtils.findQueueFamilies();
if(!indices.graphicsFamily.equals(indices.presentFamily)) {
createInfo.imageSharingMode(VK12.VK_SHARING_MODE_CONCURRENT);
createInfo.pQueueFamilyIndices(stack.ints(indices.graphicsFamily, indices.presentFamily));
} else {
createInfo.imageSharingMode(VK12.VK_SHARING_MODE_EXCLUSIVE);
}
createInfo.preTransform(swapChainSupport.capabilities.currentTransform());
createInfo.compositeAlpha(KHRSurface.VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR);
createInfo.presentMode(presentMode);
createInfo.clipped(true);
createInfo.oldSwapchain(VK12.VK_NULL_HANDLE);
LongBuffer pSwapChain = stack.longs(VK12.VK_NULL_HANDLE);
int result = KHRSwapchain.vkCreateSwapchainKHR(
VKConstants.device, createInfo, null, pSwapChain);
if(result != VK12.VK_SUCCESS) {
throw new RuntimeException("Failed to create swap chain reason: " + VKUtils.translateVulkanResult(result));
}
swapChain = pSwapChain.get(0);
KHRSwapchain.vkGetSwapchainImagesKHR(VKConstants.device, swapChain, imageCount, null);
LongBuffer pSwapchainImages = stack.mallocLong(imageCount.get(0));
KHRSwapchain.vkGetSwapchainImagesKHR(VKConstants.device, swapChain, imageCount, pSwapchainImages);
swapChainImages = new ArrayList<>(imageCount.get(0));
for(int i = 0;i < pSwapchainImages.capacity();i++) {
swapChainImages.add(pSwapchainImages.get(i));
}
swapChainImageFormat = surfaceFormat.format();
swapChainExtent = VkExtent2D.create().set(extent);
}
}
}

View File

@ -4,13 +4,13 @@ import java.nio.IntBuffer;
import java.util.stream.IntStream;
import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.*;
import org.lwjgl.glfw.GLFWVulkan;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.vulkan.VkConstants;
import com.github.hydos.ginger.engine.vulkan.VKConstants;
public class VkUtils
public class VKUtils
{
private static PointerBuffer getRequiredExtensions() {
@ -19,7 +19,10 @@ public class VkUtils
return glfwExtensions;
}
public static int clamp(int min, int max, int value) {
return Math.max(min, Math.min(max, value));
}
public static void createInstance() {
@ -50,7 +53,7 @@ public class VkUtils
throw new RuntimeException("Failed to create instance");
}
VkConstants.vulkanInstance = new VkInstance(instancePtr.get(0), createInfo);
VKConstants.vulkanInstance = new VkInstance(instancePtr.get(0), createInfo);
}
}
@ -59,7 +62,7 @@ public class VkUtils
IntBuffer deviceCount = stack.ints(0);
VK12.vkEnumeratePhysicalDevices(VkConstants.vulkanInstance, deviceCount, null);
VK12.vkEnumeratePhysicalDevices(VKConstants.vulkanInstance, deviceCount, null);
if(deviceCount.get(0) == 0) {
throw new RuntimeException("Failed to find GPUs with Vulkan support");
@ -67,13 +70,13 @@ public class VkUtils
PointerBuffer ppPhysicalDevices = stack.mallocPointer(deviceCount.get(0));
VK12.vkEnumeratePhysicalDevices(VkConstants.vulkanInstance, deviceCount, ppPhysicalDevices);
VK12.vkEnumeratePhysicalDevices(VKConstants.vulkanInstance, deviceCount, ppPhysicalDevices);
VkPhysicalDevice device = null;
for(int i = 0;i < ppPhysicalDevices.capacity();i++) {
device = new VkPhysicalDevice(ppPhysicalDevices.get(i), VkConstants.vulkanInstance);
device = new VkPhysicalDevice(ppPhysicalDevices.get(i), VKConstants.vulkanInstance);
}
@ -81,7 +84,7 @@ public class VkUtils
throw new RuntimeException("Failed to find a suitable GPU");
}
VkConstants.physicalDevice = device;
VKConstants.physicalDevice = device;
}
}
@ -89,8 +92,8 @@ public class VkUtils
public static class QueueFamilyIndices {
// We use Integer to use null as the empty value
private Integer graphicsFamily;
private Integer presentFamily;
public Integer graphicsFamily;
public Integer presentFamily;
public boolean isComplete() {
return graphicsFamily != null && presentFamily != null;
@ -129,23 +132,23 @@ public class VkUtils
PointerBuffer pDevice = stack.pointers(VK12.VK_NULL_HANDLE);
if(VK12.vkCreateDevice(VkConstants.physicalDevice, createInfo, null, pDevice) != VK12.VK_SUCCESS) {
if(VK12.vkCreateDevice(VKConstants.physicalDevice, createInfo, null, pDevice) != VK12.VK_SUCCESS) {
throw new RuntimeException("Failed to create logical device");
}
VkConstants.device = new VkDevice(pDevice.get(0), VkConstants.physicalDevice, createInfo);
VKConstants.device = new VkDevice(pDevice.get(0), VKConstants.physicalDevice, createInfo);
PointerBuffer pQueue = stack.pointers(VK12.VK_NULL_HANDLE);
VK12.vkGetDeviceQueue(VkConstants.device, indices.graphicsFamily, 0, pQueue);
VkConstants.graphicsQueue = new VkQueue(pQueue.get(0), VkConstants.device);
VK12.vkGetDeviceQueue(VKConstants.device, indices.graphicsFamily, 0, pQueue);
VKConstants.graphicsQueue = new VkQueue(pQueue.get(0), VKConstants.device);
VK12.vkGetDeviceQueue(VkConstants.device, indices.presentFamily, 0, pQueue);
VkConstants.presentQueue = new VkQueue(pQueue.get(0), VkConstants.device);
VK12.vkGetDeviceQueue(VKConstants.device, indices.presentFamily, 0, pQueue);
VKConstants.presentQueue = new VkQueue(pQueue.get(0), VKConstants.device);
}
}
private static QueueFamilyIndices findQueueFamilies() {
public static QueueFamilyIndices findQueueFamilies() {
QueueFamilyIndices indices = new QueueFamilyIndices();
@ -153,11 +156,11 @@ public class VkUtils
IntBuffer queueFamilyCount = stack.ints(0);
VK12.vkGetPhysicalDeviceQueueFamilyProperties(VkConstants.physicalDevice, queueFamilyCount, null);
VK12.vkGetPhysicalDeviceQueueFamilyProperties(VKConstants.physicalDevice, queueFamilyCount, null);
VkQueueFamilyProperties.Buffer queueFamilies = VkQueueFamilyProperties.mallocStack(queueFamilyCount.get(0), stack);
VK12.vkGetPhysicalDeviceQueueFamilyProperties(VkConstants.physicalDevice, queueFamilyCount, queueFamilies);
VK12.vkGetPhysicalDeviceQueueFamilyProperties(VKConstants.physicalDevice, queueFamilyCount, queueFamilies);
IntBuffer presentSupport = stack.ints(VK12.VK_FALSE);
@ -168,8 +171,8 @@ public class VkUtils
}
KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR(
VkConstants.physicalDevice,
i, VkConstants.windowSurface,
VKConstants.physicalDevice,
i, VKConstants.windowSurface,
presentSupport);
if(presentSupport.get(0) == VK12.VK_TRUE) {