liteCraft
hYdos 2020-03-06 06:43:21 +10:00
parent 98b0936bc5
commit 3c07221b7c
4 changed files with 20 additions and 123 deletions

View File

@ -6,8 +6,6 @@ import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFWVulkan.*;
import static org.lwjgl.stb.STBImage.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.NULL;
import static org.lwjgl.vulkan.EXTDebugUtils.*;
import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
@ -31,7 +29,7 @@ import com.github.hydos.ginger.engine.vulkan.misc.*;
import com.github.hydos.ginger.engine.vulkan.misc.VKModelLoader.VKMesh;
import com.github.hydos.ginger.engine.vulkan.swapchain.VKSwapchainManager;
public class VulkanLitecraft {
public class VulkanExample {
public static class VulkanDemoGinger2 {
@ -40,51 +38,11 @@ public class VulkanLitecraft {
private static final int MAX_FRAMES_IN_FLIGHT = 2;
private static final boolean ENABLE_VALIDATION_LAYERS = false;
private static final Set<String> VALIDATION_LAYERS;
static {
if(ENABLE_VALIDATION_LAYERS) {
VALIDATION_LAYERS = new HashSet<>();
VALIDATION_LAYERS.add("VK_LAYER_KHRONOS_validation");
} else {
// We are not going to use it, so we don't create it
VALIDATION_LAYERS = null;
}
}
private static final Set<String> DEVICE_EXTENSIONS = Stream.of(VK_KHR_SWAPCHAIN_EXTENSION_NAME)
.collect(toSet());
private static int debugCallback(int messageSeverity, int messageType, long pCallbackData, long pUserData) {
VkDebugUtilsMessengerCallbackDataEXT callbackData = VkDebugUtilsMessengerCallbackDataEXT.create(pCallbackData);
System.err.println("Validation layer: " + callbackData.pMessageString());
return VK_FALSE;
}
private static int createDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerCreateInfoEXT createInfo,
VkAllocationCallbacks allocationCallbacks, LongBuffer pDebugMessenger) {
if(vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT") != NULL) {
return vkCreateDebugUtilsMessengerEXT(instance, createInfo, allocationCallbacks, pDebugMessenger);
}
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
private static void destroyDebugUtilsMessengerEXT(VkInstance instance, long debugMessenger, VkAllocationCallbacks allocationCallbacks) {
if(vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT") != NULL) {
vkDestroyDebugUtilsMessengerEXT(instance, debugMessenger, allocationCallbacks);
}
}
public static class QueueFamilyIndices {
// We use Integer to use null as the empty value
@ -186,7 +144,6 @@ public class VulkanLitecraft {
// ======= FIELDS ======= //
private VkInstance instance;
private long debugMessenger;
public static long surface;
public static VkPhysicalDevice physicalDevice;
@ -266,7 +223,6 @@ public class VulkanLitecraft {
private void initVulkan() {
createInstance();
setupDebugMessenger();
createSurface();
pickPhysicalDevice();
createLogicalDevice();
@ -324,10 +280,6 @@ public class VulkanLitecraft {
vkDestroyDevice(device, null);
if(ENABLE_VALIDATION_LAYERS) {
destroyDebugUtilsMessengerEXT(instance, debugMessenger, null);
}
vkDestroySurfaceKHR(instance, surface, null);
vkDestroyInstance(instance, null);
@ -359,15 +311,6 @@ public class VulkanLitecraft {
// enabledExtensionCount is implicitly set when you call ppEnabledExtensionNames
createInfo.ppEnabledExtensionNames(getRequiredExtensions());
if(ENABLE_VALIDATION_LAYERS) {
createInfo.ppEnabledLayerNames(asPointerBuffer(VALIDATION_LAYERS));
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = VkDebugUtilsMessengerCreateInfoEXT.callocStack(stack);
populateDebugMessengerCreateInfo(debugCreateInfo);
createInfo.pNext(debugCreateInfo.address());
}
// We need to retrieve the pointer of the created instance
PointerBuffer instancePtr = stack.mallocPointer(1);
@ -379,35 +322,6 @@ public class VulkanLitecraft {
}
}
private void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo) {
debugCreateInfo.sType(VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT);
debugCreateInfo.messageSeverity(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT);
debugCreateInfo.messageType(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT);
debugCreateInfo.pfnUserCallback(VulkanDemoGinger2::debugCallback);
}
private void setupDebugMessenger() {
if(!ENABLE_VALIDATION_LAYERS) {
return;
}
try(MemoryStack stack = stackPush()) {
VkDebugUtilsMessengerCreateInfoEXT createInfo = VkDebugUtilsMessengerCreateInfoEXT.callocStack(stack);
populateDebugMessengerCreateInfo(createInfo);
LongBuffer pDebugMessenger = stack.longs(VK_NULL_HANDLE);
if(createDebugUtilsMessengerEXT(instance, createInfo, null, pDebugMessenger) != VK_SUCCESS) {
throw new RuntimeException("Failed to set up debug messenger");
}
debugMessenger = pDebugMessenger.get(0);
}
}
private void createSurface() {
try(MemoryStack stack = stackPush()) {
@ -489,10 +403,6 @@ public class VulkanLitecraft {
createInfo.ppEnabledExtensionNames(asPointerBuffer(DEVICE_EXTENSIONS));
if(ENABLE_VALIDATION_LAYERS) {
createInfo.ppEnabledLayerNames(asPointerBuffer(VALIDATION_LAYERS));
}
PointerBuffer pDevice = stack.pointers(VK_NULL_HANDLE);
if(vkCreateDevice(physicalDevice, createInfo, null, pDevice) != VK_SUCCESS) {
@ -1941,19 +1851,6 @@ public class VulkanLitecraft {
PointerBuffer glfwExtensions = glfwGetRequiredInstanceExtensions();
if(ENABLE_VALIDATION_LAYERS) {
MemoryStack stack = stackGet();
PointerBuffer extensions = stack.mallocPointer(glfwExtensions.capacity() + 1);
extensions.put(glfwExtensions);
extensions.put(stack.UTF8(VK_EXT_DEBUG_UTILS_EXTENSION_NAME));
// Rewind the buffer before returning it to reset its position back to 0
return extensions.rewind();
}
return glfwExtensions;
}

View File

@ -8,8 +8,8 @@ import java.nio.*;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.VulkanLitecraft;
import com.github.hydos.ginger.VulkanLitecraft.VulkanDemoGinger2.Vertex;
import com.github.hydos.ginger.VulkanExample;
import com.github.hydos.ginger.VulkanExample.VulkanDemoGinger2.Vertex;
import com.github.hydos.ginger.engine.vulkan.shaders.*;
import com.github.hydos.ginger.engine.vulkan.shaders.VKShaderUtils.SPIRV;
@ -62,14 +62,14 @@ public class VKPipelineManager
VkViewport.Buffer viewport = VkViewport.callocStack(1, stack);
viewport.x(0.0f);
viewport.y(0.0f);
viewport.width(VulkanLitecraft.VulkanDemoGinger2.swapChainExtent.width());
viewport.height(VulkanLitecraft.VulkanDemoGinger2.swapChainExtent.height());
viewport.width(VulkanExample.VulkanDemoGinger2.swapChainExtent.width());
viewport.height(VulkanExample.VulkanDemoGinger2.swapChainExtent.height());
viewport.minDepth(0.0f);
viewport.maxDepth(1.0f);
VkRect2D.Buffer scissor = VkRect2D.callocStack(1, stack);
scissor.offset(VkOffset2D.callocStack(stack).set(0, 0));
scissor.extent(VulkanLitecraft.VulkanDemoGinger2.swapChainExtent);
scissor.extent(VulkanExample.VulkanDemoGinger2.swapChainExtent);
VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.callocStack(stack);
viewportState.sType(VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO);
@ -94,7 +94,7 @@ public class VKPipelineManager
multisampling.sType(VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO);
multisampling.sampleShadingEnable(true);
multisampling.minSampleShading(0.2f); // Enable sample shading in the pipeline
multisampling.rasterizationSamples(VulkanLitecraft.VulkanDemoGinger2.msaaSamples); // Min fraction for sample shading; closer to one is smoother
multisampling.rasterizationSamples(VulkanExample.VulkanDemoGinger2.msaaSamples); // Min fraction for sample shading; closer to one is smoother
VkPipelineDepthStencilStateCreateInfo depthStencil = VkPipelineDepthStencilStateCreateInfo.callocStack(stack);
depthStencil.sType(VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO);
@ -123,15 +123,15 @@ public class VKPipelineManager
VkPipelineLayoutCreateInfo pipelineLayoutInfo = VkPipelineLayoutCreateInfo.callocStack(stack);
pipelineLayoutInfo.sType(VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
pipelineLayoutInfo.pSetLayouts(stack.longs(VulkanLitecraft.VulkanDemoGinger2.descriptorSetLayout));
pipelineLayoutInfo.pSetLayouts(stack.longs(VulkanExample.VulkanDemoGinger2.descriptorSetLayout));
LongBuffer pPipelineLayout = stack.longs(VK_NULL_HANDLE);
if(vkCreatePipelineLayout(VulkanLitecraft.VulkanDemoGinger2.device, pipelineLayoutInfo, null, pPipelineLayout) != VK_SUCCESS) {
if(vkCreatePipelineLayout(VulkanExample.VulkanDemoGinger2.device, pipelineLayoutInfo, null, pPipelineLayout) != VK_SUCCESS) {
throw new RuntimeException("Failed to create pipeline layout");
}
VulkanLitecraft.VulkanDemoGinger2.pipelineLayout = pPipelineLayout.get(0);
VulkanExample.VulkanDemoGinger2.pipelineLayout = pPipelineLayout.get(0);
VkGraphicsPipelineCreateInfo.Buffer pipelineInfo = VkGraphicsPipelineCreateInfo.callocStack(1, stack);
pipelineInfo.sType(VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
@ -143,24 +143,24 @@ public class VKPipelineManager
pipelineInfo.pMultisampleState(multisampling);
pipelineInfo.pDepthStencilState(depthStencil);
pipelineInfo.pColorBlendState(colorBlending);
pipelineInfo.layout(VulkanLitecraft.VulkanDemoGinger2.pipelineLayout);
pipelineInfo.renderPass(VulkanLitecraft.VulkanDemoGinger2.renderPass);
pipelineInfo.layout(VulkanExample.VulkanDemoGinger2.pipelineLayout);
pipelineInfo.renderPass(VulkanExample.VulkanDemoGinger2.renderPass);
pipelineInfo.subpass(0);
pipelineInfo.basePipelineHandle(VK_NULL_HANDLE);
pipelineInfo.basePipelineIndex(-1);
LongBuffer pGraphicsPipeline = stack.mallocLong(1);
if(vkCreateGraphicsPipelines(VulkanLitecraft.VulkanDemoGinger2.device, VK_NULL_HANDLE, pipelineInfo, null, pGraphicsPipeline) != VK_SUCCESS) {
if(vkCreateGraphicsPipelines(VulkanExample.VulkanDemoGinger2.device, VK_NULL_HANDLE, pipelineInfo, null, pGraphicsPipeline) != VK_SUCCESS) {
throw new RuntimeException("Failed to create graphics pipeline");
}
VulkanLitecraft.VulkanDemoGinger2.graphicsPipeline = pGraphicsPipeline.get(0);
VulkanExample.VulkanDemoGinger2.graphicsPipeline = pGraphicsPipeline.get(0);
// Cleanup
vkDestroyShaderModule(VulkanLitecraft.VulkanDemoGinger2.device, vertShaderModule, null);
vkDestroyShaderModule(VulkanLitecraft.VulkanDemoGinger2.device, fragShaderModule, null);
vkDestroyShaderModule(VulkanExample.VulkanDemoGinger2.device, vertShaderModule, null);
vkDestroyShaderModule(VulkanExample.VulkanDemoGinger2.device, fragShaderModule, null);
vertShaderSPIRV.free();
fragShaderSPIRV.free();

View File

@ -8,7 +8,7 @@ import java.nio.*;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkShaderModuleCreateInfo;
import com.github.hydos.ginger.VulkanLitecraft;
import com.github.hydos.ginger.VulkanExample;
/**
* will be used in the future to manage multiple shaders
@ -28,7 +28,7 @@ public class VKShaderManager
LongBuffer pShaderModule = stack.mallocLong(1);
if(vkCreateShaderModule(VulkanLitecraft.VulkanDemoGinger2.device, createInfo, null, pShaderModule) != VK_SUCCESS) {
if(vkCreateShaderModule(VulkanExample.VulkanDemoGinger2.device, createInfo, null, pShaderModule) != VK_SUCCESS) {
throw new RuntimeException("Failed to create shader module");
}

View File

@ -12,8 +12,8 @@ import java.util.ArrayList;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.VulkanLitecraft.VulkanDemoGinger2;
import com.github.hydos.ginger.VulkanLitecraft.VulkanDemoGinger2.*;
import com.github.hydos.ginger.VulkanExample.VulkanDemoGinger2;
import com.github.hydos.ginger.VulkanExample.VulkanDemoGinger2.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.vulkan.pipelines.VKPipelineManager;